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
aee26e833f8fd9f1949cb1d097993ce67d2ca8e1
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/field_theory/minimal_polynomial.lean
ea74a1448c1e66fbdbf3d33a5870073a0f6843f6
[ "Apache-2.0" ]
permissive
vaibhavkarve/mathlib
a574aaf68c0a431a47fa82ce0637f0f769826bfe
17f8340912468f49bdc30acdb9a9fa02eeb0473a
refs/heads/master
1,621,263,802,637
1,585,399,588,000
1,585,399,588,000
250,833,447
0
0
Apache-2.0
1,585,410,341,000
1,585,410,341,000
null
UTF-8
Lean
false
false
8,926
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johan Commelin -/ import ring_theory.integral_closure /-! # Minimal polynomials This file defines the minimal polynomial of an element x of an A-algebra B, under the assumption that x is integral over A. After stating the defining property we specialize to the setting of field extensions and derive some well-known properties, amongst which the fact that minimal polynomials are irreducible, and uniquely determined by their defining property. -/ universes u v w open_locale classical open polynomial set function variables {α : Type u} {β : Type v} section min_poly_def variables [comm_ring α] [comm_ring β] [algebra α β] /-- Let B be an A-algebra, and x an element of B that is integral over A. The minimal polynomial of x is a monic polynomial of smallest degree that has x as its root. -/ noncomputable def minimal_polynomial {x : β} (hx : is_integral α x) : polynomial α := well_founded.min polynomial.degree_lt_wf _ hx end min_poly_def namespace minimal_polynomial section ring variables [comm_ring α] [comm_ring β] [algebra α β] variables {x : β} (hx : is_integral α x) /--A minimal polynomial is monic.-/ lemma monic : monic (minimal_polynomial hx) := (well_founded.min_mem degree_lt_wf _ hx).1 /--An element is a root of its minimal polynomial.-/ @[simp] lemma aeval : aeval α β x (minimal_polynomial hx) = 0 := (well_founded.min_mem degree_lt_wf _ hx).2 /--The defining property of the minimal polynomial of an element x: it is the monic polynomial with smallest degree that has x as its root.-/ lemma min {p : polynomial α} (pmonic : p.monic) (hp : polynomial.aeval α β x p = 0) : degree (minimal_polynomial hx) ≤ degree p := le_of_not_lt $ well_founded.not_lt_min degree_lt_wf _ hx ⟨pmonic, hp⟩ end ring section field variables [field α] [field β] [algebra α β] variables {x : β} (hx : is_integral α x) /--A minimal polynomial is nonzero.-/ lemma ne_zero : (minimal_polynomial hx) ≠ 0 := ne_zero_of_monic (monic hx) /--If an element x is a root of a nonzero polynomial p, then the degree of p is at least the degree of the minimal polynomial of x.-/ lemma degree_le_of_ne_zero {p : polynomial α} (pnz : p ≠ 0) (hp : polynomial.aeval α β x p = 0) : degree (minimal_polynomial hx) ≤ degree p := calc degree (minimal_polynomial hx) ≤ degree (p * C (leading_coeff p)⁻¹) : min _ (monic_mul_leading_coeff_inv pnz) (by simp [hp]) ... = degree p : degree_mul_leading_coeff_inv p pnz /--The minimal polynomial of an element x is uniquely characterized by its defining property: if there is another monic polynomial of minimal degree that has x as a root, then this polynomial is equal to the minimal polynomial of x.-/ lemma unique {p : polynomial α} (pmonic : p.monic) (hp : polynomial.aeval α β x p = 0) (pmin : ∀ q : polynomial α, q.monic → polynomial.aeval α β x q = 0 → degree p ≤ degree q) : p = minimal_polynomial hx := begin symmetry, apply eq_of_sub_eq_zero, by_contra hnz, have := degree_le_of_ne_zero hx hnz (by simp [hp]), contrapose! this, apply degree_sub_lt _ (ne_zero hx), { rw [(monic hx).leading_coeff, pmonic.leading_coeff] }, { exact le_antisymm (min hx pmonic hp) (pmin (minimal_polynomial hx) (monic hx) (aeval hx)) }, end /--If an element x is a root of a polynomial p, then the minimal polynomial of x divides p.-/ lemma dvd {p : polynomial α} (hp : polynomial.aeval α β x p = 0) : minimal_polynomial hx ∣ p := begin rw ← dvd_iff_mod_by_monic_eq_zero (monic hx), by_contra hnz, have := degree_le_of_ne_zero hx hnz _, { contrapose! this, exact degree_mod_by_monic_lt _ (monic hx) (ne_zero hx) }, { rw ← mod_by_monic_add_div p (monic hx) at hp, simpa using hp } end /--The degree of a minimal polynomial is nonzero.-/ lemma degree_ne_zero : degree (minimal_polynomial hx) ≠ 0 := begin assume deg_eq_zero, have ndeg_eq_zero : nat_degree (minimal_polynomial hx) = 0, { simpa using congr_arg nat_degree (eq_C_of_degree_eq_zero deg_eq_zero) }, have eq_one : minimal_polynomial hx = 1, { rw eq_C_of_degree_eq_zero deg_eq_zero, congr, simpa [ndeg_eq_zero.symm] using (monic hx).leading_coeff }, simpa [eq_one, aeval_def] using aeval hx end /--A minimal polynomial is not a unit.-/ lemma not_is_unit : ¬ is_unit (minimal_polynomial hx) := assume H, degree_ne_zero hx $ degree_eq_zero_of_is_unit H /--The degree of a minimal polynomial is positive.-/ lemma degree_pos : 0 < degree (minimal_polynomial hx) := degree_pos_of_ne_zero_of_nonunit (ne_zero hx) (not_is_unit hx) /--A minimal polynomial is prime.-/ lemma prime : prime (minimal_polynomial hx) := begin refine ⟨ne_zero hx, not_is_unit hx, _⟩, rintros p q ⟨d, h⟩, have : polynomial.aeval α β x (p*q) = 0 := by simp [h, aeval hx], replace : polynomial.aeval α β x p = 0 ∨ polynomial.aeval α β x q = 0 := by simpa, cases this; [left, right]; apply dvd; assumption end /--A minimal polynomial is irreducible.-/ lemma irreducible : irreducible (minimal_polynomial hx) := irreducible_of_prime (prime hx) /--If L/K is a field extension, and x is an element of L in the image of K, then the minimal polynomial of x is X - C x.-/ @[simp] protected lemma algebra_map (a : α) (ha : is_integral α (algebra_map β a)) : minimal_polynomial ha = X - C a := begin refine (unique ha (monic_X_sub_C a) (by simp [aeval_def]) _).symm, intros q hq H, rw degree_X_sub_C, suffices : 0 < degree q, { -- This part is annoying and shouldn't be there. have q_ne_zero : q ≠ 0, { apply polynomial.ne_zero_of_degree_gt this }, rw degree_eq_nat_degree q_ne_zero at this ⊢, rw [← with_bot.coe_zero, with_bot.coe_lt_coe] at this, rwa [← with_bot.coe_one, with_bot.coe_le_coe], }, apply degree_pos_of_root (ne_zero_of_monic hq), show is_root q a, apply is_ring_hom.injective (algebra_map β : α → β), rw [is_ring_hom.map_zero (algebra_map β : α → β), ← H], convert polynomial.hom_eval₂ _ _ _ _, { exact is_semiring_hom.id }, { apply_instance } end variable (β) /--If L/K is a field extension, and x is an element of L in the image of K, then the minimal polynomial of x is X - C x.-/ lemma algebra_map' (a : α) : minimal_polynomial (@is_integral_algebra_map α β _ _ _ a) = X - C a := minimal_polynomial.algebra_map _ _ variable {β} /--The minimal polynomial of 0 is X.-/ @[simp] lemma zero {h₀ : is_integral α (0:β)} : minimal_polynomial h₀ = X := by simpa only [add_zero, polynomial.C_0, sub_eq_add_neg, neg_zero, algebra.map_zero] using algebra_map' β (0:α) /--The minimal polynomial of 1 is X - 1.-/ @[simp] lemma one {h₁ : is_integral α (1:β)} : minimal_polynomial h₁ = X - 1 := by simpa only [algebra.map_one, polynomial.C_1, sub_eq_add_neg] using algebra_map' β (1:α) /--If L/K is a field extension and an element y of K is a root of the minimal polynomial of an element x ∈ L, then y maps to x under the field embedding.-/ lemma root {x : β} (hx : is_integral α x) {y : α} (h : is_root (minimal_polynomial hx) y) : algebra_map β y = x := begin have ndeg_one : nat_degree (minimal_polynomial hx) = 1, { rw ← polynomial.degree_eq_iff_nat_degree_eq_of_pos (nat.zero_lt_one), exact degree_eq_one_of_irreducible_of_root (irreducible hx) h }, have coeff_one : (minimal_polynomial hx).coeff 1 = 1, { simpa only [ndeg_one, leading_coeff] using (monic hx).leading_coeff }, have hy : y = - coeff (minimal_polynomial hx) 0, { rw (minimal_polynomial hx).as_sum at h, apply eq_neg_of_add_eq_zero, simpa only [ndeg_one, coeff_one, C_1, eval_C, eval_X, eval_add, mul_one, one_mul, pow_zero, pow_one, is_root.def, finset.sum_range_succ, finset.insert_empty_eq_singleton, finset.sum_singleton, finset.range_one] using h, }, subst y, rw [algebra.map_neg, neg_eq_iff_add_eq_zero], have H := aeval hx, rw (minimal_polynomial hx).as_sum at H, simpa only [ndeg_one, coeff_one, aeval_def, C_1, eval₂_add, eval₂_C, eval₂_X, mul_one, one_mul, pow_one, pow_zero, add_comm, finset.sum_range_succ, finset.insert_empty_eq_singleton, finset.sum_singleton, finset.range_one] using H, end /--The constant coefficient of the minimal polynomial of x is 0 if and only if x = 0.-/ @[simp] lemma coeff_zero_eq_zero : coeff (minimal_polynomial hx) 0 = 0 ↔ x = 0 := begin split, { intro h, have zero_root := polynomial.zero_is_root_of_coeff_zero_eq_zero h, rw ← root hx zero_root, exact is_ring_hom.map_zero _ }, { rintro rfl, simp } end /--The minimal polynomial of a nonzero element has nonzero constant coefficient.-/ lemma coeff_zero_ne_zero (h : x ≠ 0) : coeff (minimal_polynomial hx) 0 ≠ 0 := by { contrapose! h, simpa using h } end field end minimal_polynomial
451902a737b2a69c0c2488247256d7a0c2c76155
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/topology/Top/presheaf_of_functions.lean
972d3490fa7a6c4df4ebd7e708945a8cb04768ba
[ "Apache-2.0" ]
permissive
ChrisHughes24/mathlib
98322577c460bc6b1fe5c21f42ce33ad1c3e5558
a2a867e827c2a6702beb9efc2b9282bd801d5f9a
refs/heads/master
1,583,848,251,477
1,565,164,247,000
1,565,164,247,000
129,409,993
0
1
Apache-2.0
1,565,164,817,000
1,523,628,059,000
Lean
UTF-8
Lean
false
false
2,630
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import topology.Top.presheaf import topology.algebra.TopCommRing.basic import category_theory.yoneda import ring_theory.subring import topology.algebra.continuous_functions universes v u open category_theory open topological_space open opposite namespace Top variables (X : Top.{v}) def presheaf_to_Top (T : Top.{v}) : X.presheaf (Type v) := (opens.to_Top X).op ⋙ (yoneda.obj T) -- TODO upgrade the result to TopCommRing? def continuous_functions (X : Top.{v}ᵒᵖ) (R : TopCommRing.{v}) : CommRing.{v} := { α := unop X ⟶ TopCommRing.forget_to_Top.obj R, str := _root_.continuous_comm_ring } namespace continuous_functions @[simp] lemma one (X : Top.{v}ᵒᵖ) (R : TopCommRing.{v}) (x) : (monoid.one ↥(continuous_functions X R)).val x = 1 := rfl @[simp] lemma add (X : Top.{v}ᵒᵖ) (R : TopCommRing.{v}) (f g : continuous_functions X R) (x) : (comm_ring.add f g).val x = f.1 x + g.1 x := rfl @[simp] lemma mul (X : Top.{v}ᵒᵖ) (R : TopCommRing.{v}) (f g : continuous_functions X R) (x) : (ring.mul f g).val x = f.1 x * g.1 x := rfl def pullback {X Y : Topᵒᵖ} (f : X ⟶ Y) (R : TopCommRing) : continuous_functions X R ⟶ continuous_functions Y R := { val := λ g, f.unop ≫ g, property := { map_one := rfl, map_add := by tidy, map_mul := by tidy } } local attribute [extensionality] subtype.eq def map (X : Topᵒᵖ) {R S : TopCommRing} (φ : R ⟶ S) : continuous_functions X R ⟶ continuous_functions X S := { val := λ g, g ≫ (TopCommRing.forget_to_Top.map φ), property := { map_one := begin ext1, ext1, simp only [one], exact φ.2.1.map_one end, map_add := λ x y, begin ext1, ext1, simp only [function.comp_app, add], apply φ.2.1.map_add end, map_mul := λ x y, begin ext1, ext1, simp only [function.comp_app, mul], apply φ.2.1.map_mul end } } end continuous_functions def CommRing_yoneda : TopCommRing ⥤ (Topᵒᵖ ⥤ CommRing) := { obj := λ R, { obj := λ X, continuous_functions X R, map := λ X Y f, continuous_functions.pullback f R }, map := λ R S φ, { app := λ X, continuous_functions.map X φ } } def presheaf_to_TopCommRing (T : TopCommRing.{v}) : X.presheaf CommRing.{v} := (opens.to_Top X).op ⋙ (CommRing_yoneda.obj T) noncomputable def presheaf_ℝ (Y : Top) : Y.presheaf CommRing := presheaf_to_TopCommRing Y (TopCommRing.of ℝ) noncomputable def presheaf_ℂ (Y : Top) : Y.presheaf CommRing := presheaf_to_TopCommRing Y (TopCommRing.of ℂ) end Top
2495b28f10f5c556d27d64d3b10cc269c6d3a7a2
d436468d80b739ba7e06843c4d0d2070e43448e5
/src/category_theory/limits/limits.lean
ad7725fa0cd248e4365cfa9ff68f65917fb023fe
[ "Apache-2.0" ]
permissive
roro47/mathlib
761fdc002aef92f77818f3fef06bf6ec6fc1a28e
80aa7d52537571a2ca62a3fdf71c9533a09422cf
refs/heads/master
1,599,656,410,625
1,573,649,488,000
1,573,649,488,000
221,452,951
0
0
Apache-2.0
1,573,647,693,000
1,573,647,692,000
null
UTF-8
Lean
false
false
36,675
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton, Mario Carneiro, Scott Morrison, Floris van Doorn -/ import category_theory.whiskering import category_theory.yoneda import category_theory.limits.cones import category_theory.eq_to_hom open category_theory category_theory.category category_theory.functor opposite namespace category_theory.limits universes v u u' u'' w -- declare the `v`'s first; see `category_theory.category` for an explanation -- See the notes at the top of cones.lean, explaining why we can't allow `J : Prop` here. variables {J K : Type v} [small_category J] [small_category K] variables {C : Type u} [𝒞 : category.{v} C] include 𝒞 variables {F : J ⥤ C} /-- A cone `t` on `F` is a limit cone if each cone on `F` admits a unique cone morphism to `t`. -/ structure is_limit (t : cone F) := (lift : Π (s : cone F), s.X ⟶ t.X) (fac' : ∀ (s : cone F) (j : J), lift s ≫ t.π.app j = s.π.app j . obviously) (uniq' : ∀ (s : cone F) (m : s.X ⟶ t.X) (w : ∀ j : J, m ≫ t.π.app j = s.π.app j), m = lift s . obviously) restate_axiom is_limit.fac' attribute [simp] is_limit.fac restate_axiom is_limit.uniq' namespace is_limit instance subsingleton {t : cone F} : subsingleton (is_limit t) := ⟨by intros P Q; cases P; cases Q; congr; ext; solve_by_elim⟩ /- Repackaging the definition in terms of cone morphisms. -/ def lift_cone_morphism {t : cone F} (h : is_limit t) (s : cone F) : s ⟶ t := { hom := h.lift s } lemma uniq_cone_morphism {s t : cone F} (h : is_limit t) {f f' : s ⟶ t} : f = f' := have ∀ {g : s ⟶ t}, g = h.lift_cone_morphism s, by intro g; ext; exact h.uniq _ _ g.w, this.trans this.symm def mk_cone_morphism {t : cone F} (lift : Π (s : cone F), s ⟶ t) (uniq' : ∀ (s : cone F) (m : s ⟶ t), m = lift s) : is_limit t := { lift := λ s, (lift s).hom, uniq' := λ s m w, have cone_morphism.mk m w = lift s, by apply uniq', congr_arg cone_morphism.hom this } /-- Limit cones on `F` are unique up to isomorphism. -/ def unique_up_to_iso {s t : cone F} (P : is_limit s) (Q : is_limit t) : s ≅ t := { hom := Q.lift_cone_morphism s, inv := P.lift_cone_morphism t, hom_inv_id' := P.uniq_cone_morphism, inv_hom_id' := Q.uniq_cone_morphism } def of_iso_limit {r t : cone F} (P : is_limit r) (i : r ≅ t) : is_limit t := is_limit.mk_cone_morphism (λ s, P.lift_cone_morphism s ≫ i.hom) (λ s m, by rw ←i.comp_inv_eq; apply P.uniq_cone_morphism) variables {t : cone F} lemma hom_lift (h : is_limit t) {W : C} (m : W ⟶ t.X) : m = h.lift { X := W, π := { app := λ b, m ≫ t.π.app b } } := h.uniq { X := W, π := { app := λ b, m ≫ t.π.app b } } m (λ b, rfl) /-- Two morphisms into a limit are equal if their compositions with each cone morphism are equal. -/ lemma hom_ext (h : is_limit t) {W : C} {f f' : W ⟶ t.X} (w : ∀ j, f ≫ t.π.app j = f' ≫ t.π.app j) : f = f' := by rw [h.hom_lift f, h.hom_lift f']; congr; exact funext w /-- The universal property of a limit cone: a map `W ⟶ X` is the same as a cone on `F` with vertex `W`. -/ def hom_iso (h : is_limit t) (W : C) : (W ⟶ t.X) ≅ ((const J).obj W ⟶ F) := { hom := λ f, (t.extend f).π, inv := λ π, h.lift { X := W, π := π }, hom_inv_id' := by ext f; apply h.hom_ext; intro j; simp; dsimp; refl } @[simp] lemma hom_iso_hom (h : is_limit t) {W : C} (f : W ⟶ t.X) : (is_limit.hom_iso h W).hom f = (t.extend f).π := rfl /-- The limit of `F` represents the functor taking `W` to the set of cones on `F` with vertex `W`. -/ def nat_iso (h : is_limit t) : yoneda.obj t.X ≅ F.cones := nat_iso.of_components (λ W, is_limit.hom_iso h (unop W)) (by tidy). def hom_iso' (h : is_limit t) (W : C) : ((W ⟶ t.X) : Type v) ≅ { p : Π j, W ⟶ F.obj j // ∀ {j j'} (f : j ⟶ j'), p j ≫ F.map f = p j' } := h.hom_iso W ≪≫ { hom := λ π, ⟨λ j, π.app j, λ j j' f, by convert ←(π.naturality f).symm; apply id_comp⟩, inv := λ p, { app := λ j, p.1 j, naturality' := λ j j' f, begin dsimp, rw [id_comp], exact (p.2 f).symm end } } /-- If G : C → D is a faithful functor which sends t to a limit cone, then it suffices to check that the induced maps for the image of t can be lifted to maps of C. -/ def of_faithful {t : cone F} {D : Type u'} [category.{v} D] (G : C ⥤ D) [faithful G] (ht : is_limit (G.map_cone t)) (lift : Π (s : cone F), s.X ⟶ t.X) (h : ∀ s, G.map (lift s) = ht.lift (G.map_cone s)) : is_limit t := { lift := lift, fac' := λ s j, by apply G.injectivity; rw [G.map_comp, h]; apply ht.fac, uniq' := λ s m w, begin apply G.injectivity, rw h, refine ht.uniq (G.map_cone s) _ (λ j, _), convert ←congr_arg (λ f, G.map f) (w j), apply G.map_comp end } def iso_unique_cone_morphism {t : cone F} : is_limit t ≅ Π s, unique (s ⟶ t) := { hom := λ h s, { default := h.lift_cone_morphism s, uniq := λ _, h.uniq_cone_morphism }, inv := λ h, { lift := λ s, (h s).default.hom, uniq' := λ s f w, congr_arg cone_morphism.hom ((h s).uniq ⟨f, w⟩) } } namespace of_nat_iso variables {X : C} (h : yoneda.obj X ≅ F.cones) /-- If `F.cones` is represented by `X`, each morphism `f : Y ⟶ X` gives a cone with cone point `Y`. -/ def cone_of_hom {Y : C} (f : Y ⟶ X) : cone F := { X := Y, π := h.hom.app (op Y) f } /-- If `F.cones` is represented by `X`, each cone `s` gives a morphism `s.X ⟶ X`. -/ def hom_of_cone (s : cone F) : s.X ⟶ X := h.inv.app (op s.X) s.π @[simp] lemma cone_of_hom_of_cone (s : cone F) : cone_of_hom h (hom_of_cone h s) = s := begin dsimp [cone_of_hom, hom_of_cone], cases s, congr, dsimp, exact congr_fun (congr_fun (congr_arg nat_trans.app h.inv_hom_id) (op s_X)) s_π, end @[simp] lemma hom_of_cone_of_hom {Y : C} (f : Y ⟶ X) : hom_of_cone h (cone_of_hom h f) = f := congr_fun (congr_fun (congr_arg nat_trans.app h.hom_inv_id) (op Y)) f /-- If `F.cones` is represented by `X`, the cone corresponding to the identity morphism on `X` will be a limit cone. -/ def limit_cone : cone F := cone_of_hom h (𝟙 X) /-- If `F.cones` is represented by `X`, the cone corresponding to a morphism `f : Y ⟶ X` is the limit cone extended by `f`. -/ lemma cone_of_hom_fac {Y : C} (f : Y ⟶ X) : cone_of_hom h f = (limit_cone h).extend f := begin dsimp [cone_of_hom, limit_cone, cone.extend], congr, ext j, have t := congr_fun (h.hom.naturality f.op) (𝟙 X), dsimp at t, simp only [comp_id] at t, rw congr_fun (congr_arg nat_trans.app t) j, refl, end /-- If `F.cones` is represented by `X`, any cone is the extension of the limit cone by the corresponding morphism. -/ lemma cone_fac (s : cone F) : (limit_cone h).extend (hom_of_cone h s) = s := begin rw ←cone_of_hom_of_cone h s, conv_lhs { simp only [hom_of_cone_of_hom] }, apply (cone_of_hom_fac _ _).symm, end end of_nat_iso section open of_nat_iso /-- If `F.cones` is representable, then the cone corresponding to the identity morphism on the representing object is a limit cone. -/ def of_nat_iso {X : C} (h : yoneda.obj X ≅ F.cones) : is_limit (limit_cone h) := { lift := λ s, hom_of_cone h s, fac' := λ s j, begin have h := cone_fac h s, cases s, injection h with h₁ h₂, simp only [heq_iff_eq] at h₂, conv_rhs { rw ← h₂ }, refl, end, uniq' := λ s m w, begin rw ←hom_of_cone_of_hom h m, congr, rw cone_of_hom_fac, dsimp, cases s, congr, ext j, exact w j, end } end end is_limit /-- A cocone `t` on `F` is a colimit cocone if each cocone on `F` admits a unique cocone morphism from `t`. -/ structure is_colimit (t : cocone F) := (desc : Π (s : cocone F), t.X ⟶ s.X) (fac' : ∀ (s : cocone F) (j : J), t.ι.app j ≫ desc s = s.ι.app j . obviously) (uniq' : ∀ (s : cocone F) (m : t.X ⟶ s.X) (w : ∀ j : J, t.ι.app j ≫ m = s.ι.app j), m = desc s . obviously) restate_axiom is_colimit.fac' attribute [simp] is_colimit.fac restate_axiom is_colimit.uniq' namespace is_colimit instance subsingleton {t : cocone F} : subsingleton (is_colimit t) := ⟨by intros P Q; cases P; cases Q; congr; ext; solve_by_elim⟩ /- Repackaging the definition in terms of cone morphisms. -/ def desc_cocone_morphism {t : cocone F} (h : is_colimit t) (s : cocone F) : t ⟶ s := { hom := h.desc s } lemma uniq_cocone_morphism {s t : cocone F} (h : is_colimit t) {f f' : t ⟶ s} : f = f' := have ∀ {g : t ⟶ s}, g = h.desc_cocone_morphism s, by intro g; ext; exact h.uniq _ _ g.w, this.trans this.symm def mk_cocone_morphism {t : cocone F} (desc : Π (s : cocone F), t ⟶ s) (uniq' : ∀ (s : cocone F) (m : t ⟶ s), m = desc s) : is_colimit t := { desc := λ s, (desc s).hom, uniq' := λ s m w, have cocone_morphism.mk m w = desc s, by apply uniq', congr_arg cocone_morphism.hom this } /-- Limit cones on `F` are unique up to isomorphism. -/ def unique_up_to_iso {s t : cocone F} (P : is_colimit s) (Q : is_colimit t) : s ≅ t := { hom := P.desc_cocone_morphism t, inv := Q.desc_cocone_morphism s, hom_inv_id' := P.uniq_cocone_morphism, inv_hom_id' := Q.uniq_cocone_morphism } def of_iso_colimit {r t : cocone F} (P : is_colimit r) (i : r ≅ t) : is_colimit t := is_colimit.mk_cocone_morphism (λ s, i.inv ≫ P.desc_cocone_morphism s) (λ s m, by rw i.eq_inv_comp; apply P.uniq_cocone_morphism) variables {t : cocone F} lemma hom_desc (h : is_colimit t) {W : C} (m : t.X ⟶ W) : m = h.desc { X := W, ι := { app := λ b, t.ι.app b ≫ m, naturality' := by intros; erw [←assoc, t.ι.naturality, comp_id, comp_id] } } := h.uniq { X := W, ι := { app := λ b, t.ι.app b ≫ m, naturality' := _ } } m (λ b, rfl) /-- Two morphisms out of a colimit are equal if their compositions with each cocone morphism are equal. -/ lemma hom_ext (h : is_colimit t) {W : C} {f f' : t.X ⟶ W} (w : ∀ j, t.ι.app j ≫ f = t.ι.app j ≫ f') : f = f' := by rw [h.hom_desc f, h.hom_desc f']; congr; exact funext w /-- The universal property of a colimit cocone: a map `X ⟶ W` is the same as a cocone on `F` with vertex `W`. -/ def hom_iso (h : is_colimit t) (W : C) : (t.X ⟶ W) ≅ (F ⟶ (const J).obj W) := { hom := λ f, (t.extend f).ι, inv := λ ι, h.desc { X := W, ι := ι }, hom_inv_id' := by ext f; apply h.hom_ext; intro j; simp; dsimp; refl } @[simp] lemma hom_iso_hom (h : is_colimit t) {W : C} (f : t.X ⟶ W) : (is_colimit.hom_iso h W).hom f = (t.extend f).ι := rfl /-- The colimit of `F` represents the functor taking `W` to the set of cocones on `F` with vertex `W`. -/ def nat_iso (h : is_colimit t) : coyoneda.obj (op t.X) ≅ F.cocones := nat_iso.of_components (is_colimit.hom_iso h) (by intros; ext; dsimp; rw ←assoc; refl) def hom_iso' (h : is_colimit t) (W : C) : ((t.X ⟶ W) : Type v) ≅ { p : Π j, F.obj j ⟶ W // ∀ {j j' : J} (f : j ⟶ j'), F.map f ≫ p j' = p j } := h.hom_iso W ≪≫ { hom := λ ι, ⟨λ j, ι.app j, λ j j' f, by convert ←(ι.naturality f); apply comp_id⟩, inv := λ p, { app := λ j, p.1 j, naturality' := λ j j' f, begin dsimp, rw [comp_id], exact (p.2 f) end } } /-- If G : C → D is a faithful functor which sends t to a colimit cocone, then it suffices to check that the induced maps for the image of t can be lifted to maps of C. -/ def of_faithful {t : cocone F} {D : Type u'} [category.{v} D] (G : C ⥤ D) [faithful G] (ht : is_colimit (G.map_cocone t)) (desc : Π (s : cocone F), t.X ⟶ s.X) (h : ∀ s, G.map (desc s) = ht.desc (G.map_cocone s)) : is_colimit t := { desc := desc, fac' := λ s j, by apply G.injectivity; rw [G.map_comp, h]; apply ht.fac, uniq' := λ s m w, begin apply G.injectivity, rw h, refine ht.uniq (G.map_cocone s) _ (λ j, _), convert ←congr_arg (λ f, G.map f) (w j), apply G.map_comp end } def iso_unique_cocone_morphism {t : cocone F} : is_colimit t ≅ Π s, unique (t ⟶ s) := { hom := λ h s, { default := h.desc_cocone_morphism s, uniq := λ _, h.uniq_cocone_morphism }, inv := λ h, { desc := λ s, (h s).default.hom, uniq' := λ s f w, congr_arg cocone_morphism.hom ((h s).uniq ⟨f, w⟩) } } namespace of_nat_iso variables {X : C} (h : coyoneda.obj (op X) ≅ F.cocones) /-- If `F.cocones` is corepresented by `X`, each morphism `f : X ⟶ Y` gives a cocone with cone point `Y`. -/ def cocone_of_hom {Y : C} (f : X ⟶ Y) : cocone F := { X := Y, ι := h.hom.app Y f } /-- If `F.cocones` is corepresented by `X`, each cocone `s` gives a morphism `X ⟶ s.X`. -/ def hom_of_cocone (s : cocone F) : X ⟶ s.X := h.inv.app s.X s.ι @[simp] lemma cocone_of_hom_of_cocone (s : cocone F) : cocone_of_hom h (hom_of_cocone h s) = s := begin dsimp [cocone_of_hom, hom_of_cocone], cases s, congr, dsimp, exact congr_fun (congr_fun (congr_arg nat_trans.app h.inv_hom_id) s_X) s_ι, end @[simp] lemma hom_of_cocone_of_hom {Y : C} (f : X ⟶ Y) : hom_of_cocone h (cocone_of_hom h f) = f := congr_fun (congr_fun (congr_arg nat_trans.app h.hom_inv_id) Y) f /-- If `F.cocones` is corepresented by `X`, the cocone corresponding to the identity morphism on `X` will be a colimit cocone. -/ def colimit_cocone : cocone F := cocone_of_hom h (𝟙 X) /-- If `F.cocones` is corepresented by `X`, the cocone corresponding to a morphism `f : Y ⟶ X` is the colimit cocone extended by `f`. -/ lemma cocone_of_hom_fac {Y : C} (f : X ⟶ Y) : cocone_of_hom h f = (colimit_cocone h).extend f := begin dsimp [cocone_of_hom, colimit_cocone, cocone.extend], congr, ext j, have t := congr_fun (h.hom.naturality f) (𝟙 X), dsimp at t, simp only [id_comp] at t, rw congr_fun (congr_arg nat_trans.app t) j, refl, end /-- If `F.cocones` is corepresented by `X`, any cocone is the extension of the colimit cocone by the corresponding morphism. -/ lemma cocone_fac (s : cocone F) : (colimit_cocone h).extend (hom_of_cocone h s) = s := begin rw ←cocone_of_hom_of_cocone h s, conv_lhs { simp only [hom_of_cocone_of_hom] }, apply (cocone_of_hom_fac _ _).symm, end end of_nat_iso section open of_nat_iso /-- If `F.cocones` is corepresentable, then the cocone corresponding to the identity morphism on the representing object is a colimit cocone. -/ def of_nat_iso {X : C} (h : coyoneda.obj (op X) ≅ F.cocones) : is_colimit (colimit_cocone h) := { desc := λ s, hom_of_cocone h s, fac' := λ s j, begin have h := cocone_fac h s, cases s, injection h with h₁ h₂, simp only [heq_iff_eq] at h₂, conv_rhs { rw ← h₂ }, refl, end, uniq' := λ s m w, begin rw ←hom_of_cocone_of_hom h m, congr, rw cocone_of_hom_fac, dsimp, cases s, congr, ext j, exact w j, end } end end is_colimit section limit /-- `has_limit F` represents a particular chosen limit of the diagram `F`. -/ class has_limit (F : J ⥤ C) := (cone : cone F) (is_limit : is_limit cone . tactic.apply_instance) variables (J C) /-- `C` has limits of shape `J` if we have chosen a particular limit of every functor `F : J ⥤ C`. -/ class has_limits_of_shape := (has_limit : Π F : J ⥤ C, has_limit F) /-- `C` has all (small) limits if it has limits of every shape. -/ class has_limits := (has_limits_of_shape : Π (J : Type v) [𝒥 : small_category J], has_limits_of_shape J C) variables {J C} instance has_limit_of_has_limits_of_shape {J : Type v} [small_category J] [H : has_limits_of_shape J C] (F : J ⥤ C) : has_limit F := has_limits_of_shape.has_limit F instance has_limits_of_shape_of_has_limits {J : Type v} [small_category J] [H : has_limits.{v} C] : has_limits_of_shape J C := has_limits.has_limits_of_shape C J /- Interface to the `has_limit` class. -/ def limit.cone (F : J ⥤ C) [has_limit F] : cone F := has_limit.cone F def limit (F : J ⥤ C) [has_limit F] := (limit.cone F).X def limit.π (F : J ⥤ C) [has_limit F] (j : J) : limit F ⟶ F.obj j := (limit.cone F).π.app j @[simp] lemma limit.cone_π {F : J ⥤ C} [has_limit F] (j : J) : (limit.cone F).π.app j = limit.π _ j := rfl @[simp] lemma limit.w (F : J ⥤ C) [has_limit F] {j j' : J} (f : j ⟶ j') : limit.π F j ≫ F.map f = limit.π F j' := (limit.cone F).w f def limit.is_limit (F : J ⥤ C) [has_limit F] : is_limit (limit.cone F) := has_limit.is_limit.{v} F def limit.lift (F : J ⥤ C) [has_limit F] (c : cone F) : c.X ⟶ limit F := (limit.is_limit F).lift c @[simp] lemma limit.is_limit_lift {F : J ⥤ C} [has_limit F] (c : cone F) : (limit.is_limit F).lift c = limit.lift F c := rfl @[simp, reassoc] lemma limit.lift_π {F : J ⥤ C} [has_limit F] (c : cone F) (j : J) : limit.lift F c ≫ limit.π F j = c.π.app j := is_limit.fac _ c j def limit.cone_morphism {F : J ⥤ C} [has_limit F] (c : cone F) : cone_morphism c (limit.cone F) := (limit.is_limit F).lift_cone_morphism c @[simp] lemma limit.cone_morphism_hom {F : J ⥤ C} [has_limit F] (c : cone F) : (limit.cone_morphism c).hom = limit.lift F c := rfl @[simp] lemma limit.cone_morphism_π {F : J ⥤ C} [has_limit F] (c : cone F) (j : J) : (limit.cone_morphism c).hom ≫ limit.π F j = c.π.app j := by erw is_limit.fac @[ext] lemma limit.hom_ext {F : J ⥤ C} [has_limit F] {X : C} {f f' : X ⟶ limit F} (w : ∀ j, f ≫ limit.π F j = f' ≫ limit.π F j) : f = f' := (limit.is_limit F).hom_ext w def limit.hom_iso (F : J ⥤ C) [has_limit F] (W : C) : (W ⟶ limit F) ≅ (F.cones.obj (op W)) := (limit.is_limit F).hom_iso W @[simp] lemma limit.hom_iso_hom (F : J ⥤ C) [has_limit F] {W : C} (f : W ⟶ limit F) : (limit.hom_iso F W).hom f = (const J).map f ≫ (limit.cone F).π := (limit.is_limit F).hom_iso_hom f def limit.hom_iso' (F : J ⥤ C) [has_limit F] (W : C) : ((W ⟶ limit F) : Type v) ≅ { p : Π j, W ⟶ F.obj j // ∀ {j j' : J} (f : j ⟶ j'), p j ≫ F.map f = p j' } := (limit.is_limit F).hom_iso' W lemma limit.lift_extend {F : J ⥤ C} [has_limit F] (c : cone F) {X : C} (f : X ⟶ c.X) : limit.lift F (c.extend f) = f ≫ limit.lift F c := by obviously def has_limit_of_iso {F G : J ⥤ C} [has_limit F] (α : F ≅ G) : has_limit G := { cone := (cones.postcompose α.hom).obj (limit.cone F), is_limit := { lift := λ s, limit.lift F ((cones.postcompose α.inv).obj s), fac' := λ s j, begin rw [cones.postcompose_obj_π, nat_trans.comp_app, limit.cone_π, ←category.assoc, limit.lift_π], simp end, uniq' := λ s m w, begin apply limit.hom_ext, intro j, rw [limit.lift_π, cones.postcompose_obj_π, nat_trans.comp_app, ←nat_iso.app_inv, iso.eq_comp_inv], simpa using w j end } } /-- If a functor `G` has the same collection of cones as a functor `F` which has a limit, then `G` also has a limit. -/ -- See the construction of limits from products and equalizers -- for an example usage. def has_limit.of_cones_iso {J K : Type v} [small_category J] [small_category K] (F : J ⥤ C) (G : K ⥤ C) (h : F.cones ≅ G.cones) [has_limit F] : has_limit G := ⟨_, is_limit.of_nat_iso ((is_limit.nat_iso (limit.is_limit F)) ≪≫ h)⟩ section pre variables (F) [has_limit F] (E : K ⥤ J) [has_limit (E ⋙ F)] def limit.pre : limit F ⟶ limit (E ⋙ F) := limit.lift (E ⋙ F) { X := limit F, π := { app := λ k, limit.π F (E.obj k) } } @[simp] lemma limit.pre_π (k : K) : limit.pre F E ≫ limit.π (E ⋙ F) k = limit.π F (E.obj k) := by erw is_limit.fac @[simp] lemma limit.lift_pre (c : cone F) : limit.lift F c ≫ limit.pre F E = limit.lift (E ⋙ F) (c.whisker E) := by ext; simp variables {L : Type v} [small_category L] variables (D : L ⥤ K) [has_limit (D ⋙ E ⋙ F)] @[simp] lemma limit.pre_pre : limit.pre F E ≫ limit.pre (E ⋙ F) D = limit.pre F (D ⋙ E) := by ext j; erw [assoc, limit.pre_π, limit.pre_π, limit.pre_π]; refl end pre section post variables {D : Type u'} [𝒟 : category.{v} D] include 𝒟 variables (F) [has_limit F] (G : C ⥤ D) [has_limit (F ⋙ G)] def limit.post : G.obj (limit F) ⟶ limit (F ⋙ G) := limit.lift (F ⋙ G) { X := G.obj (limit F), π := { app := λ j, G.map (limit.π F j), naturality' := by intros j j' f; erw [←G.map_comp, limits.cone.w, id_comp]; refl } } @[simp] lemma limit.post_π (j : J) : limit.post F G ≫ limit.π (F ⋙ G) j = G.map (limit.π F j) := by erw is_limit.fac @[simp] lemma limit.lift_post (c : cone F) : G.map (limit.lift F c) ≫ limit.post F G = limit.lift (F ⋙ G) (G.map_cone c) := by ext; rw [assoc, limit.post_π, ←G.map_comp, limit.lift_π, limit.lift_π]; refl @[simp] lemma limit.post_post {E : Type u''} [category.{v} E] (H : D ⥤ E) [has_limit ((F ⋙ G) ⋙ H)] : /- H G (limit F) ⟶ H (limit (F ⋙ G)) ⟶ limit ((F ⋙ G) ⋙ H) equals -/ /- H G (limit F) ⟶ limit (F ⋙ (G ⋙ H)) -/ H.map (limit.post F G) ≫ limit.post (F ⋙ G) H = limit.post F (G ⋙ H) := by ext; erw [assoc, limit.post_π, ←H.map_comp, limit.post_π, limit.post_π]; refl end post lemma limit.pre_post {D : Type u'} [category.{v} D] (E : K ⥤ J) (F : J ⥤ C) (G : C ⥤ D) [has_limit F] [has_limit (E ⋙ F)] [has_limit (F ⋙ G)] [has_limit ((E ⋙ F) ⋙ G)] : /- G (limit F) ⟶ G (limit (E ⋙ F)) ⟶ limit ((E ⋙ F) ⋙ G) vs -/ /- G (limit F) ⟶ limit F ⋙ G ⟶ limit (E ⋙ (F ⋙ G)) or -/ G.map (limit.pre F E) ≫ limit.post (E ⋙ F) G = limit.post F G ≫ limit.pre (F ⋙ G) E := by ext; erw [assoc, limit.post_π, ←G.map_comp, limit.pre_π, assoc, limit.pre_π, limit.post_π]; refl open category_theory.equivalence instance has_limit_equivalence_comp (e : K ≌ J) [has_limit F] : has_limit (e.functor ⋙ F) := { cone := cone.whisker e.functor (limit.cone F), is_limit := let e' := cones.postcompose (e.inv_fun_id_assoc F).hom in { lift := λ s, limit.lift F (e'.obj (cone.whisker e.inverse s)), fac' := λ s j, begin dsimp, rw [limit.lift_π], dsimp [e'], erw [inv_fun_id_assoc_hom_app, counit_functor, ←s.π.naturality, id_comp] end, uniq' := λ s m w, begin apply limit.hom_ext, intro j, erw [limit.lift_π, ←limit.w F (e.counit_iso.hom.app j)], slice_lhs 1 2 { erw [w (e.inverse.obj j)] }, simp end } } local attribute [elab_simple] inv_fun_id_assoc -- not entirely sure why this is needed def has_limit_of_equivalence_comp (e : K ≌ J) [has_limit (e.functor ⋙ F)] : has_limit F := begin haveI : has_limit (e.inverse ⋙ e.functor ⋙ F) := limits.has_limit_equivalence_comp e.symm, apply has_limit_of_iso (e.inv_fun_id_assoc F), end -- `has_limit_comp_equivalence` and `has_limit_of_comp_equivalence` -- are proved in `category_theory/adjunction/limits.lean`. section lim_functor variables [has_limits_of_shape J C] /-- `limit F` is functorial in `F`, when `C` has all limits of shape `J`. -/ def lim : (J ⥤ C) ⥤ C := { obj := λ F, limit F, map := λ F G α, limit.lift G { X := limit F, π := { app := λ j, limit.π F j ≫ α.app j, naturality' := λ j j' f, by erw [id_comp, assoc, ←α.naturality, ←assoc, limit.w] } }, map_comp' := λ F G H α β, by ext; erw [assoc, is_limit.fac, is_limit.fac, ←assoc, is_limit.fac, assoc]; refl } variables {F} {G : J ⥤ C} (α : F ⟶ G) @[simp, reassoc] lemma lim.map_π (j : J) : lim.map α ≫ limit.π G j = limit.π F j ≫ α.app j := by apply is_limit.fac @[simp] lemma limit.lift_map (c : cone F) : limit.lift F c ≫ lim.map α = limit.lift G ((cones.postcompose α).obj c) := by ext; rw [assoc, lim.map_π, ←assoc, limit.lift_π, limit.lift_π]; refl lemma limit.map_pre [has_limits_of_shape K C] (E : K ⥤ J) : lim.map α ≫ limit.pre G E = limit.pre F E ≫ lim.map (whisker_left E α) := by ext; rw [assoc, limit.pre_π, lim.map_π, assoc, lim.map_π, ←assoc, limit.pre_π]; refl lemma limit.map_pre' [has_limits_of_shape.{v} K C] (F : J ⥤ C) {E₁ E₂ : K ⥤ J} (α : E₁ ⟶ E₂) : limit.pre F E₂ = limit.pre F E₁ ≫ lim.map (whisker_right α F) := by ext1; simp [(category.assoc _ _ _ _).symm] lemma limit.id_pre (F : J ⥤ C) : limit.pre F (𝟭 _) = lim.map (functor.left_unitor F).inv := by tidy lemma limit.map_post {D : Type u'} [category.{v} D] [has_limits_of_shape J D] (H : C ⥤ D) : /- H (limit F) ⟶ H (limit G) ⟶ limit (G ⋙ H) vs H (limit F) ⟶ limit (F ⋙ H) ⟶ limit (G ⋙ H) -/ H.map (lim.map α) ≫ limit.post G H = limit.post F H ≫ lim.map (whisker_right α H) := begin ext, rw [assoc, limit.post_π, ←H.map_comp, lim.map_π, H.map_comp], rw [assoc, lim.map_π, ←assoc, limit.post_π], refl end def lim_yoneda : lim ⋙ yoneda ≅ category_theory.cones J C := nat_iso.of_components (λ F, nat_iso.of_components (λ W, limit.hom_iso F (unop W)) (by tidy)) (by tidy) end lim_functor def has_limits_of_shape_of_equivalence {J' : Type v} [small_category J'] (e : J ≌ J') [has_limits_of_shape J C] : has_limits_of_shape J' C := by { constructor, intro F, apply has_limit_of_equivalence_comp e, apply_instance } end limit section colimit /-- `has_colimit F` represents a particular chosen colimit of the diagram `F`. -/ class has_colimit (F : J ⥤ C) := (cocone : cocone F) (is_colimit : is_colimit cocone . tactic.apply_instance) variables (J C) /-- `C` has colimits of shape `J` if we have chosen a particular colimit of every functor `F : J ⥤ C`. -/ class has_colimits_of_shape := (has_colimit : Π F : J ⥤ C, has_colimit F) /-- `C` has all (small) colimits if it has colimits of every shape. -/ class has_colimits := (has_colimits_of_shape : Π (J : Type v) [𝒥 : small_category J], has_colimits_of_shape J C) variables {J C} instance has_colimit_of_has_colimits_of_shape {J : Type v} [small_category J] [H : has_colimits_of_shape J C] (F : J ⥤ C) : has_colimit F := has_colimits_of_shape.has_colimit F instance has_colimits_of_shape_of_has_colimits {J : Type v} [small_category J] [H : has_colimits.{v} C] : has_colimits_of_shape J C := has_colimits.has_colimits_of_shape C J /- Interface to the `has_colimit` class. -/ def colimit.cocone (F : J ⥤ C) [has_colimit F] : cocone F := has_colimit.cocone F def colimit (F : J ⥤ C) [has_colimit F] := (colimit.cocone F).X def colimit.ι (F : J ⥤ C) [has_colimit F] (j : J) : F.obj j ⟶ colimit F := (colimit.cocone F).ι.app j @[simp] lemma colimit.cocone_ι {F : J ⥤ C} [has_colimit F] (j : J) : (colimit.cocone F).ι.app j = colimit.ι _ j := rfl @[simp] lemma colimit.w (F : J ⥤ C) [has_colimit F] {j j' : J} (f : j ⟶ j') : F.map f ≫ colimit.ι F j' = colimit.ι F j := (colimit.cocone F).w f def colimit.is_colimit (F : J ⥤ C) [has_colimit F] : is_colimit (colimit.cocone F) := has_colimit.is_colimit.{v} F def colimit.desc (F : J ⥤ C) [has_colimit F] (c : cocone F) : colimit F ⟶ c.X := (colimit.is_colimit F).desc c @[simp] lemma colimit.is_colimit_desc {F : J ⥤ C} [has_colimit F] (c : cocone F) : (colimit.is_colimit F).desc c = colimit.desc F c := rfl /-- We have lots of lemmas describing how to simplify `colimit.ι F j ≫ _`, and combined with `colimit.ext` we rely on these lemmas for many calculations. However, since `category.assoc` is a `@[simp]` lemma, often expressions are right associated, and it's hard to apply these lemmas about `colimit.ι`. We thus use `reassoc` to define additional `@[simp]` lemmas, with an arbitrary extra morphism. (see `tactic/reassoc_axiom.lean`) -/ @[simp, reassoc] lemma colimit.ι_desc {F : J ⥤ C} [has_colimit F] (c : cocone F) (j : J) : colimit.ι F j ≫ colimit.desc F c = c.ι.app j := is_colimit.fac _ c j def colimit.cocone_morphism {F : J ⥤ C} [has_colimit F] (c : cocone F) : cocone_morphism (colimit.cocone F) c := (colimit.is_colimit F).desc_cocone_morphism c @[simp] lemma colimit.cocone_morphism_hom {F : J ⥤ C} [has_colimit F] (c : cocone F) : (colimit.cocone_morphism c).hom = colimit.desc F c := rfl @[simp] lemma colimit.ι_cocone_morphism {F : J ⥤ C} [has_colimit F] (c : cocone F) (j : J) : colimit.ι F j ≫ (colimit.cocone_morphism c).hom = c.ι.app j := by erw is_colimit.fac @[ext] lemma colimit.hom_ext {F : J ⥤ C} [has_colimit F] {X : C} {f f' : colimit F ⟶ X} (w : ∀ j, colimit.ι F j ≫ f = colimit.ι F j ≫ f') : f = f' := (colimit.is_colimit F).hom_ext w def colimit.hom_iso (F : J ⥤ C) [has_colimit F] (W : C) : (colimit F ⟶ W) ≅ (F.cocones.obj W) := (colimit.is_colimit F).hom_iso W @[simp] lemma colimit.hom_iso_hom (F : J ⥤ C) [has_colimit F] {W : C} (f : colimit F ⟶ W) : (colimit.hom_iso F W).hom f = (colimit.cocone F).ι ≫ (const J).map f := (colimit.is_colimit F).hom_iso_hom f def colimit.hom_iso' (F : J ⥤ C) [has_colimit F] (W : C) : ((colimit F ⟶ W) : Type v) ≅ { p : Π j, F.obj j ⟶ W // ∀ {j j'} (f : j ⟶ j'), F.map f ≫ p j' = p j } := (colimit.is_colimit F).hom_iso' W lemma colimit.desc_extend (F : J ⥤ C) [has_colimit F] (c : cocone F) {X : C} (f : c.X ⟶ X) : colimit.desc F (c.extend f) = colimit.desc F c ≫ f := begin ext1, rw [←category.assoc], simp end def has_colimit_of_iso {F G : J ⥤ C} [has_colimit F] (α : G ≅ F) : has_colimit G := { cocone := (cocones.precompose α.hom).obj (colimit.cocone F), is_colimit := { desc := λ s, colimit.desc F ((cocones.precompose α.inv).obj s), fac' := λ s j, begin rw [cocones.precompose_obj_ι, nat_trans.comp_app, colimit.cocone_ι], rw [category.assoc, colimit.ι_desc, ←nat_iso.app_hom, ←iso.eq_inv_comp], refl end, uniq' := λ s m w, begin apply colimit.hom_ext, intro j, rw [colimit.ι_desc, cocones.precompose_obj_ι, nat_trans.comp_app, ←nat_iso.app_inv, iso.eq_inv_comp], simpa using w j end } } /-- If a functor `G` has the same collection of cocones as a functor `F` which has a colimit, then `G` also has a colimit. -/ def has_colimit.of_cocones_iso {J K : Type v} [small_category J] [small_category K] (F : J ⥤ C) (G : K ⥤ C) (h : F.cocones ≅ G.cocones) [has_colimit F] : has_colimit G := ⟨_, is_colimit.of_nat_iso ((is_colimit.nat_iso (colimit.is_colimit F)) ≪≫ h)⟩ section pre variables (F) [has_colimit F] (E : K ⥤ J) [has_colimit (E ⋙ F)] def colimit.pre : colimit (E ⋙ F) ⟶ colimit F := colimit.desc (E ⋙ F) { X := colimit F, ι := { app := λ k, colimit.ι F (E.obj k) } } @[simp, reassoc] lemma colimit.ι_pre (k : K) : colimit.ι (E ⋙ F) k ≫ colimit.pre F E = colimit.ι F (E.obj k) := by erw is_colimit.fac @[simp] lemma colimit.pre_desc (c : cocone F) : colimit.pre F E ≫ colimit.desc F c = colimit.desc (E ⋙ F) (c.whisker E) := by ext; rw [←assoc, colimit.ι_pre]; simp variables {L : Type v} [small_category L] variables (D : L ⥤ K) [has_colimit (D ⋙ E ⋙ F)] @[simp] lemma colimit.pre_pre : colimit.pre (E ⋙ F) D ≫ colimit.pre F E = colimit.pre F (D ⋙ E) := begin ext j, rw [←assoc, colimit.ι_pre, colimit.ι_pre], letI : has_colimit ((D ⋙ E) ⋙ F) := show has_colimit (D ⋙ E ⋙ F), by apply_instance, exact (colimit.ι_pre F (D ⋙ E) j).symm end end pre section post variables {D : Type u'} [𝒟 : category.{v} D] include 𝒟 variables (F) [has_colimit F] (G : C ⥤ D) [has_colimit (F ⋙ G)] def colimit.post : colimit (F ⋙ G) ⟶ G.obj (colimit F) := colimit.desc (F ⋙ G) { X := G.obj (colimit F), ι := { app := λ j, G.map (colimit.ι F j), naturality' := by intros j j' f; erw [←G.map_comp, limits.cocone.w, comp_id]; refl } } @[simp, reassoc] lemma colimit.ι_post (j : J) : colimit.ι (F ⋙ G) j ≫ colimit.post F G = G.map (colimit.ι F j) := by erw is_colimit.fac @[simp] lemma colimit.post_desc (c : cocone F) : colimit.post F G ≫ G.map (colimit.desc F c) = colimit.desc (F ⋙ G) (G.map_cocone c) := by ext; rw [←assoc, colimit.ι_post, ←G.map_comp, colimit.ι_desc, colimit.ι_desc]; refl @[simp] lemma colimit.post_post {E : Type u''} [category.{v} E] (H : D ⥤ E) [has_colimit ((F ⋙ G) ⋙ H)] : /- H G (colimit F) ⟶ H (colimit (F ⋙ G)) ⟶ colimit ((F ⋙ G) ⋙ H) equals -/ /- H G (colimit F) ⟶ colimit (F ⋙ (G ⋙ H)) -/ colimit.post (F ⋙ G) H ≫ H.map (colimit.post F G) = colimit.post F (G ⋙ H) := begin ext, rw [←assoc, colimit.ι_post, ←H.map_comp, colimit.ι_post], exact (colimit.ι_post F (G ⋙ H) j).symm end end post lemma colimit.pre_post {D : Type u'} [category.{v} D] (E : K ⥤ J) (F : J ⥤ C) (G : C ⥤ D) [has_colimit F] [has_colimit (E ⋙ F)] [has_colimit (F ⋙ G)] [has_colimit ((E ⋙ F) ⋙ G)] : /- G (colimit F) ⟶ G (colimit (E ⋙ F)) ⟶ colimit ((E ⋙ F) ⋙ G) vs -/ /- G (colimit F) ⟶ colimit F ⋙ G ⟶ colimit (E ⋙ (F ⋙ G)) or -/ colimit.post (E ⋙ F) G ≫ G.map (colimit.pre F E) = colimit.pre (F ⋙ G) E ≫ colimit.post F G := begin ext, rw [←assoc, colimit.ι_post, ←G.map_comp, colimit.ι_pre, ←assoc], letI : has_colimit (E ⋙ F ⋙ G) := show has_colimit ((E ⋙ F) ⋙ G), by apply_instance, erw [colimit.ι_pre (F ⋙ G) E j, colimit.ι_post] end open category_theory.equivalence instance has_colimit_equivalence_comp (e : K ≌ J) [has_colimit F] : has_colimit (e.functor ⋙ F) := { cocone := cocone.whisker e.functor (colimit.cocone F), is_colimit := let e' := cocones.precompose (e.inv_fun_id_assoc F).inv in { desc := λ s, colimit.desc F (e'.obj (cocone.whisker e.inverse s)), fac' := λ s j, begin dsimp, rw [colimit.ι_desc], dsimp [e'], erw [inv_fun_id_assoc_inv_app, ←functor_unit, s.ι.naturality, comp_id], refl end, uniq' := λ s m w, begin apply colimit.hom_ext, intro j, erw [colimit.ι_desc], have := w (e.inverse.obj j), simp at this, erw [←colimit.w F (e.counit_iso.hom.app j)] at this, erw [assoc, ←iso.eq_inv_comp (F.map_iso $ e.counit_iso.app j)] at this, erw [this], simp end } } def has_colimit_of_equivalence_comp (e : K ≌ J) [has_colimit (e.functor ⋙ F)] : has_colimit F := begin haveI : has_colimit (e.inverse ⋙ e.functor ⋙ F) := limits.has_colimit_equivalence_comp e.symm, apply has_colimit_of_iso (e.inv_fun_id_assoc F).symm, end section colim_functor variables [has_colimits_of_shape J C] /-- `colimit F` is functorial in `F`, when `C` has all colimits of shape `J`. -/ def colim : (J ⥤ C) ⥤ C := { obj := λ F, colimit F, map := λ F G α, colimit.desc F { X := colimit G, ι := { app := λ j, α.app j ≫ colimit.ι G j, naturality' := λ j j' f, by erw [comp_id, ←assoc, α.naturality, assoc, colimit.w] } }, map_comp' := λ F G H α β, by ext; erw [←assoc, is_colimit.fac, is_colimit.fac, assoc, is_colimit.fac, ←assoc]; refl } variables {F} {G : J ⥤ C} (α : F ⟶ G) @[simp, reassoc] lemma colim.ι_map (j : J) : colimit.ι F j ≫ colim.map α = α.app j ≫ colimit.ι G j := by apply is_colimit.fac @[simp] lemma colimit.map_desc (c : cocone G) : colim.map α ≫ colimit.desc G c = colimit.desc F ((cocones.precompose α).obj c) := by ext; rw [←assoc, colim.ι_map, assoc, colimit.ι_desc, colimit.ι_desc]; refl lemma colimit.pre_map [has_colimits_of_shape K C] (E : K ⥤ J) : colimit.pre F E ≫ colim.map α = colim.map (whisker_left E α) ≫ colimit.pre G E := by ext; rw [←assoc, colimit.ι_pre, colim.ι_map, ←assoc, colim.ι_map, assoc, colimit.ι_pre]; refl lemma colimit.pre_map' [has_colimits_of_shape.{v} K C] (F : J ⥤ C) {E₁ E₂ : K ⥤ J} (α : E₁ ⟶ E₂) : colimit.pre F E₁ = colim.map (whisker_right α F) ≫ colimit.pre F E₂ := by ext1; simp [(category.assoc _ _ _ _).symm] lemma colimit.pre_id (F : J ⥤ C) : colimit.pre F (𝟭 _) = colim.map (functor.left_unitor F).hom := by tidy lemma colimit.map_post {D : Type u'} [category.{v} D] [has_colimits_of_shape J D] (H : C ⥤ D) : /- H (colimit F) ⟶ H (colimit G) ⟶ colimit (G ⋙ H) vs H (colimit F) ⟶ colimit (F ⋙ H) ⟶ colimit (G ⋙ H) -/ colimit.post F H ≫ H.map (colim.map α) = colim.map (whisker_right α H) ≫ colimit.post G H:= begin ext, rw [←assoc, colimit.ι_post, ←H.map_comp, colim.ι_map, H.map_comp], rw [←assoc, colim.ι_map, assoc, colimit.ι_post], refl end def colim_coyoneda : colim.op ⋙ coyoneda ≅ category_theory.cocones J C := nat_iso.of_components (λ F, nat_iso.of_components (colimit.hom_iso (unop F)) (by tidy)) (by tidy) end colim_functor def has_colimits_of_shape_of_equivalence {J' : Type v} [small_category J'] (e : J ≌ J') [has_colimits_of_shape J C] : has_colimits_of_shape J' C := by { constructor, intro F, apply has_colimit_of_equivalence_comp e, apply_instance } end colimit end category_theory.limits
5e0218a3274a267cae2f60caef22e59615acd7f3
737dc4b96c97368cb66b925eeea3ab633ec3d702
/src/Lean/Meta/Tactic/Split.lean
a5e74fb56d19153600d1ef9fdeae44777de0cfe6
[ "Apache-2.0" ]
permissive
Bioye97/lean4
1ace34638efd9913dc5991443777b01a08983289
bc3900cbb9adda83eed7e6affeaade7cfd07716d
refs/heads/master
1,690,589,820,211
1,631,051,000,000
1,631,067,598,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,185
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.Match.MatchEqs import Lean.Meta.Tactic.Generalize namespace Lean.Meta namespace Split private def getSimpMatchContext : MetaM Simp.Context := return { simpLemmas := {} congrLemmas := (← getCongrLemmas) config.zeta := false config.beta := false config.eta := false config.iota := false config.proj := false config.decide := false } private def simpMatchPre (matchDeclName : Name) (matchEqDeclName : Name) (e : Expr) : SimpM Simp.Step := do if e.isAppOf matchDeclName then -- First try to reduce matcher match (← reduceRecMatcher? e) with | some e' => return Simp.Step.done { expr := e' } | none => -- Try lemma match (← Simp.tryLemma? e { proof := mkConst matchEqDeclName, name? := matchEqDeclName } SplitIf.discharge?) with | none => return Simp.Step.visit { expr := e } | some r => return Simp.Step.done r else return Simp.Step.visit { expr := e } private def simpMatch (matchDeclName : Name) (matchEqDeclName : Name) (e : Expr) : MetaM Simp.Result := do Simp.main e (← getSimpMatchContext) (methods := { pre := simpMatchPre matchDeclName matchEqDeclName }) private def simpMatchTarget (mvarId : MVarId) (matchDeclName : Name) (matchEqDeclName : Name) : MetaM MVarId := do withMVarContext mvarId do let target ← instantiateMVars (← getMVarType mvarId) let r ← simpMatch matchDeclName matchEqDeclName target match r.proof? with | some proof => replaceTargetEq mvarId r.expr proof | none => replaceTargetDefEq mvarId r.expr private def generalizeMatchDiscrs (mvarId : MVarId) (discrs : Array Expr) : MetaM (Array FVarId × MVarId) := do if discrs.all (·.isFVar) then return (discrs.map (·.fvarId!), mvarId) else let args ← discrs.mapM fun d => return { expr := d, hName? := (← mkFreshUserName `h) : GeneralizeArg } let (fvarIds, mvarId) ← generalize mvarId args return (fvarIds[:discrs.size], mvarId) def splitMatch (mvarId : MVarId) (e : Expr) : MetaM (List MVarId) := do let some app ← matchMatcherApp? e | throwError "match application expected" let (discrFVarIds, mvarId) ← generalizeMatchDiscrs mvarId app.discrs trace[Meta.debug] "split [1]:\n{MessageData.ofGoal mvarId}" let (reverted, mvarId) ← revert mvarId discrFVarIds (preserveOrder := true) let (discrFVarIds, mvarId) ← introNP mvarId discrFVarIds.size let numExtra := reverted.size - discrFVarIds.size let discrs := discrFVarIds.map mkFVar let matchEqns ← Match.getEquationsFor app.matcherName withMVarContext mvarId do let motive ← mkLambdaFVars discrs (← getMVarType mvarId) -- Fix universe let mut us := app.matcherLevels if let some uElimPos := app.uElimPos? then -- Set universe elimination level to zero (Prop). us := us.set! uElimPos levelZero let splitter := mkAppN (mkConst matchEqns.splitterName us.toList) app.params let splitter := mkAppN (mkApp splitter motive) discrs check splitter -- TODO let mvarIds ← apply mvarId splitter let (_, mvarIds) ← mvarIds.foldlM (init := (0, [])) fun (i, mvarIds) mvarId => do let numParams := matchEqns.splitterAltNumParams[i] let (_, mvarId) ← introN mvarId numParams let (_, mvarId) ← introNP mvarId numExtra trace[Meta.debug] "before simpMatch:\n{MessageData.ofGoal mvarId}" let mvarId ← simpMatchTarget mvarId app.matcherName matchEqns.eqnNames[i] return (i+1, mvarId::mvarIds) return mvarIds.reverse /-- Return an `if-then-else` or `match-expr` to split. -/ partial def findSplit? (env : Environment) (e : Expr) : Option Expr := if let some target := e.find? fun e => !e.hasLooseBVars && (e.isIte || e.isDIte || isMatcherAppCore env e) then if e.isIte || e.isDIte then let cond := target.getArg! 1 5 -- Try to find a nested `if` in `cond` findSplit? env cond |>.getD target else some target else none end Split open Split def splitTarget? (mvarId : MVarId) : MetaM (Option (List MVarId)) := commitWhenSome? do if let some e := findSplit? (← getEnv) (← getMVarType mvarId) then if e.isIte || e.isDIte then return (← splitIfTarget? mvarId).map fun (s₁, s₂) => [s₁.mvarId, s₂.mvarId] else splitMatch mvarId e else return none def splitLocalDecl? (mvarId : MVarId) (fvarId : FVarId) : MetaM (Option (List MVarId)) := commitWhenSome? do withMVarContext mvarId do if let some e := findSplit? (← getEnv) (← inferType (mkFVar fvarId)) then if e.isIte || e.isDIte then return (← splitIfLocalDecl? mvarId fvarId).map fun (mvarId₁, mvarId₂) => [mvarId₁, mvarId₂] else let (fvarIds, mvarId) ← revert mvarId #[fvarId] let num := fvarIds.size let mvarIds ← splitMatch mvarId e let mvarIds ← mvarIds.mapM fun mvarId => return (← introNP mvarId num).2 return some mvarIds else return none end Lean.Meta
f3864c2a639c5760154da6986f1b09fe25f3570e
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/elim.lean
41f2820062a63abc815934168226a5e1bd097a4b
[ "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
215
lean
import logic constant p : num → num → num → Prop axiom H1 : ∃ x y z, p x y z axiom H2 : ∀ {x y z : num}, p x y z → p x x x theorem tst : ∃ x, p x x x := obtain a b c H, from H1, exists.intro a (H2 H)
eca8017e58c231598025ef7137ac760532389337
3dd1b66af77106badae6edb1c4dea91a146ead30
/tests/lean/run/e18.lean
400bc1362cbd11847ea7bf503a206c542d4db95e
[ "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
444
lean
inductive nat : Type := | zero : nat | succ : nat → nat inductive list (A : Type) : Type := | nil {} : list A | cons : A → list A → list A inductive int : Type := | of_nat : nat → int | neg : nat → int coercion of_nat variables n m : nat variables i j : int variable l : list nat check cons i (cons i nil) check cons n (cons n nil) check cons i (cons n nil) check cons n (cons i nil) check cons n (cons i (cons m (cons j nil)))
2c152d0be146dc4d7bd1f0c3e44be41cbd7aea37
3f7026ea8bef0825ca0339a275c03b911baef64d
/src/tactic/omega/int/main.lean
e8b956c5b32c34f4d9a6ef882f5c96ddc0043295
[ "Apache-2.0" ]
permissive
rspencer01/mathlib
b1e3afa5c121362ef0881012cc116513ab09f18c
c7d36292c6b9234dc40143c16288932ae38fdc12
refs/heads/master
1,595,010,346,708
1,567,511,503,000
1,567,511,503,000
206,071,681
0
0
Apache-2.0
1,567,513,643,000
1,567,513,643,000
null
UTF-8
Lean
false
false
2,571
lean
/- Copyright (c) 2019 Seul Baek. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Seul Baek Main procedure for linear integer arithmetic. -/ import tactic.omega.prove_unsats import tactic.omega.int.dnf open tactic namespace omega namespace int local notation x ` =* ` y := form.eq x y local notation x ` ≤* ` y := form.le x y local notation `¬* ` p := form.not p local notation p ` ∨* ` q := form.or p q local notation p ` ∧* ` q := form.and p q run_cmd mk_simp_attr `sugar attribute [sugar] not_le not_lt int.lt_iff_add_one_le or_false false_or and_true true_and ge gt mul_add add_mul one_mul mul_one mul_comm sub_eq_add_neg classical.imp_iff_not_or classical.iff_iff_not_or_and_or_not meta def desugar := `[try {simp only with sugar}] lemma univ_close_of_unsat_clausify (m : nat) (p : form) : clauses.unsat (dnf (¬* p)) → univ_close p (λ x, 0) m | h1 := begin apply univ_close_of_valid, apply valid_of_unsat_not, apply unsat_of_clauses_unsat, exact h1 end /- Given a (p : form), return the expr of a (t : univ_close m p) -/ meta def prove_univ_close (m : nat) (p : form) : tactic expr := do x ← prove_unsats (dnf (¬*p)), return `(univ_close_of_unsat_clausify %%`(m) %%`(p) %%x) meta def to_preterm : expr → tactic preterm | (expr.var k) := return (preterm.var 1 k) | `(-%%(expr.var k)) := return (preterm.var (-1 : int) k) | `(%%(expr.var k) * %%zx) := do z ← eval_expr int zx, return (preterm.var z k) | `(%%t1x + %%t2x) := do t1 ← to_preterm t1x, t2 ← to_preterm t2x, return (preterm.add t1 t2) | zx := do z ← eval_expr int zx, return (preterm.cst z) meta def to_form_core : expr → tactic form | `(%%tx1 = %%tx2) := do t1 ← to_preterm tx1, t2 ← to_preterm tx2, return (t1 =* t2) | `(%%tx1 ≤ %%tx2) := do t1 ← to_preterm tx1, t2 ← to_preterm tx2, return (t1 ≤* t2) | `(¬ %%px) := do p ← to_form_core px, return (¬* p) | `(%%px ∨ %%qx) := do p ← to_form_core px, q ← to_form_core qx, return (p ∨* q) | `(%%px ∧ %%qx) := do p ← to_form_core px, q ← to_form_core qx, return (p ∧* q) | _ := failed meta def to_form : nat → expr → tactic (form × nat) | m `(_ → %%px) := to_form (m+1) px | m x := do p ← to_form_core x, return (p,m) meta def prove_lia : tactic expr := do (p,m) ← target >>= to_form 0, prove_univ_close m p end int end omega open omega.int meta def omega_int : tactic unit := desugar >> prove_lia >>= apply >> skip
675d910512544a34c05fdad1c32bd91c2f7394bf
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/overload2.lean
1e10c4cc06d23fa52eeee8fdcadc4561c947c83f
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
729
lean
inductive F2 : Type | O : F2 | I : F2 namespace F2 definition add : F2 → F2 → F2 | O O := O | O I := I | I O := I | I I := O infix (name := add) + := F2.add end F2 open F2 nat #reduce (1 : nat) + 1 #reduce (1 : nat) + (1 : nat) example : true := begin have H : (1 : nat) + (1 : nat) = 2, reflexivity, constructor end example : true := begin have H : 1 + 1 = 2, reflexivity, constructor end example : true := begin have H : (1:nat) + 1 = 2, reflexivity, constructor end example : true := begin have H : I + O = I, reflexivity, constructor end example : true := begin have H1 : I + O = I, reflexivity, have H2 : 1 + 0 = 1, reflexivity, have H3 : (1:int) + 0 = 1, reflexivity, constructor end
7831f0b6d3ae4d9c1d8b93a49ee1d6913d83688e
36938939954e91f23dec66a02728db08a7acfcf9
/old-lean4/test_json_render.lean
229069a7b5024a9e875a1b0b55b275457c4c542e
[]
no_license
pnwamk/reopt-vcg
f8b56dd0279392a5e1c6aee721be8138e6b558d3
c9f9f185fbefc25c36c4b506bbc85fd1a03c3b6d
refs/heads/master
1,631,145,017,772
1,593,549,019,000
1,593,549,143,000
254,191,418
0
0
null
1,586,377,077,000
1,586,377,077,000
null
UTF-8
Lean
false
false
296
lean
import json open json def mkTree : ℕ → Value | 0 := Value.null | (Nat.succ n) := let t := mkTree n in Value.listObj [("a", t), ("b", t)] open IO.Fs def main (xs : List String) : IO Unit := do let t := mkTree xs.head.toNat, let val := toString t, IO.println ((toString t).length)
f0b6e4c324d93eb51e493239b611033e8d24a1df
a7dd8b83f933e72c40845fd168dde330f050b1c9
/src/category_theory/instances/CommRing/colimits.lean
d96a136545d8ee637e21fc531966c5b978e400aa
[ "Apache-2.0" ]
permissive
NeilStrickland/mathlib
10420e92ee5cb7aba1163c9a01dea2f04652ed67
3efbd6f6dff0fb9b0946849b43b39948560a1ffe
refs/heads/master
1,589,043,046,346
1,558,938,706,000
1,558,938,706,000
181,285,984
0
0
Apache-2.0
1,568,941,848,000
1,555,233,833,000
Lean
UTF-8
Lean
false
false
12,080
lean
import category_theory.instances.CommRing.basic import category_theory.limits.limits universes u v open category_theory open category_theory.instances open category_theory.limits -- [ROBOT VOICE]: -- You should pretend for now that this file was automatically generated. -- It follows the same template as colimits in Mon. /- `#print comm_ring` says: structure comm_ring : Type u → Type u fields: comm_ring.zero : Π (α : Type u) [c : comm_ring α], α comm_ring.one : Π (α : Type u) [c : comm_ring α], α comm_ring.neg : Π {α : Type u} [c : comm_ring α], α → α comm_ring.add : Π {α : Type u} [c : comm_ring α], α → α → α comm_ring.mul : Π {α : Type u} [c : comm_ring α], α → α → α comm_ring.zero_add : ∀ {α : Type u} [c : comm_ring α] (a : α), 0 + a = a comm_ring.add_zero : ∀ {α : Type u} [c : comm_ring α] (a : α), a + 0 = a comm_ring.one_mul : ∀ {α : Type u} [c : comm_ring α] (a : α), 1 * a = a comm_ring.mul_one : ∀ {α : Type u} [c : comm_ring α] (a : α), a * 1 = a comm_ring.add_left_neg : ∀ {α : Type u} [c : comm_ring α] (a : α), -a + a = 0 comm_ring.add_comm : ∀ {α : Type u} [c : comm_ring α] (a b : α), a + b = b + a comm_ring.mul_comm : ∀ {α : Type u} [c : comm_ring α] (a b : α), a * b = b * a comm_ring.add_assoc : ∀ {α : Type u} [c : comm_ring α] (a b c_1 : α), a + b + c_1 = a + (b + c_1) comm_ring.mul_assoc : ∀ {α : Type u} [c : comm_ring α] (a b c_1 : α), a * b * c_1 = a * (b * c_1) comm_ring.left_distrib : ∀ {α : Type u} [c : comm_ring α] (a b c_1 : α), a * (b + c_1) = a * b + a * c_1 comm_ring.right_distrib : ∀ {α : Type u} [c : comm_ring α] (a b c_1 : α), (a + b) * c_1 = a * c_1 + b * c_1 -/ namespace category_theory.instances.CommRing.colimits variables {J : Type v} [small_category J] (F : J ⥤ CommRing.{v}) inductive prequotient -- There's always `of` | of : Π (j : J) (x : (F.obj j).α), prequotient -- Then one generator for each operation | zero {} : prequotient | one {} : prequotient | neg : prequotient → prequotient | add : prequotient → prequotient → prequotient | mul : prequotient → prequotient → prequotient open prequotient inductive relation : prequotient F → prequotient F → Prop -- Make it an equivalence relation: | refl : Π (x), relation x x | symm : Π (x y) (h : relation x y), relation y x | trans : Π (x y z) (h : relation x y) (k : relation y z), relation x z -- There's always a `map` relation | map : Π (j j' : J) (f : j ⟶ j') (x : (F.obj j).α), relation (of j' ((F.map f) x)) (of j x) -- Then one relation per operation, describing the interaction with `of` | zero : Π (j), relation (of j 0) zero | one : Π (j), relation (of j 1) one | neg : Π (j) (x : (F.obj j).α), relation (of j (-x)) (neg (of j x)) | add : Π (j) (x y : (F.obj j).α), relation (of j (x + y)) (add (of j x) (of j y)) | mul : Π (j) (x y : (F.obj j).α), relation (of j (x * y)) (mul (of j x) (of j y)) -- Then one relation per argument of each operation | neg_1 : Π (x x') (r : relation x x'), relation (neg x) (neg x') | add_1 : Π (x x' y) (r : relation x x'), relation (add x y) (add x' y) | add_2 : Π (x y y') (r : relation y y'), relation (add x y) (add x y') | mul_1 : Π (x x' y) (r : relation x x'), relation (mul x y) (mul x' y) | mul_2 : Π (x y y') (r : relation y y'), relation (mul x y) (mul x y') -- And one relation per axiom | zero_add : Π (x), relation (add zero x) x | add_zero : Π (x), relation (add x zero) x | one_mul : Π (x), relation (mul one x) x | mul_one : Π (x), relation (mul x one) x | add_left_neg : Π (x), relation (add (neg x) x) zero | add_comm : Π (x y), relation (add x y) (add y x) | mul_comm : Π (x y), relation (mul x y) (mul y x) | add_assoc : Π (x y z), relation (add (add x y) z) (add x (add y z)) | mul_assoc : Π (x y z), relation (mul (mul x y) z) (mul x (mul y z)) | left_distrib : Π (x y z), relation (mul x (add y z)) (add (mul x y) (mul x z)) | right_distrib : Π (x y z), relation (mul (add x y) z) (add (mul x z) (mul y z)) def colimit_setoid : setoid (prequotient F) := { r := relation F, iseqv := ⟨relation.refl, relation.symm, relation.trans⟩ } attribute [instance] colimit_setoid def colimit_type : Type v := quotient (colimit_setoid F) instance : comm_ring (colimit_type F) := { zero := begin exact quot.mk _ zero end, one := begin exact quot.mk _ one end, neg := begin fapply @quot.lift, { intro x, exact quot.mk _ (neg x) }, { intros x x' r, apply quot.sound, exact relation.neg_1 _ _ r }, end, add := begin fapply @quot.lift _ _ ((colimit_type F) → (colimit_type F)), { intro x, fapply @quot.lift, { intro y, exact quot.mk _ (add x y) }, { intros y y' r, apply quot.sound, exact relation.add_2 _ _ _ r } }, { intros x x' r, funext y, induction y, dsimp, apply quot.sound, { exact relation.add_1 _ _ _ r }, { refl } }, end, mul := begin fapply @quot.lift _ _ ((colimit_type F) → (colimit_type F)), { intro x, fapply @quot.lift, { intro y, exact quot.mk _ (mul x y) }, { intros y y' r, apply quot.sound, exact relation.mul_2 _ _ _ r } }, { intros x x' r, funext y, induction y, dsimp, apply quot.sound, { exact relation.mul_1 _ _ _ r }, { refl } }, end, zero_add := λ x, begin induction x, dsimp, apply quot.sound, apply relation.zero_add, refl, end, add_zero := λ x, begin induction x, dsimp, apply quot.sound, apply relation.add_zero, refl, end, one_mul := λ x, begin induction x, dsimp, apply quot.sound, apply relation.one_mul, refl, end, mul_one := λ x, begin induction x, dsimp, apply quot.sound, apply relation.mul_one, refl, end, add_left_neg := λ x, begin induction x, dsimp, apply quot.sound, apply relation.add_left_neg, refl, end, add_comm := λ x y, begin induction x, induction y, dsimp, apply quot.sound, apply relation.add_comm, refl, refl, end, mul_comm := λ x y, begin induction x, induction y, dsimp, apply quot.sound, apply relation.mul_comm, refl, refl, end, add_assoc := λ x y z, begin induction x, induction y, induction z, dsimp, apply quot.sound, apply relation.add_assoc, refl, refl, refl, end, mul_assoc := λ x y z, begin induction x, induction y, induction z, dsimp, apply quot.sound, apply relation.mul_assoc, refl, refl, refl, end, left_distrib := λ x y z, begin induction x, induction y, induction z, dsimp, apply quot.sound, apply relation.left_distrib, refl, refl, refl, end, right_distrib := λ x y z, begin induction x, induction y, induction z, dsimp, apply quot.sound, apply relation.right_distrib, refl, refl, refl, end, } @[simp] lemma quot_zero : quot.mk setoid.r zero = (0 : colimit_type F) := rfl @[simp] lemma quot_one : quot.mk setoid.r one = (1 : colimit_type F) := rfl @[simp] lemma quot_neg (x) : quot.mk setoid.r (neg x) = (-(quot.mk setoid.r x) : colimit_type F) := rfl @[simp] lemma quot_add (x y) : quot.mk setoid.r (add x y) = ((quot.mk setoid.r x) + (quot.mk setoid.r y) : colimit_type F) := rfl @[simp] lemma quot_mul (x y) : quot.mk setoid.r (mul x y) = ((quot.mk setoid.r x) * (quot.mk setoid.r y) : colimit_type F) := rfl def colimit : CommRing := ⟨colimit_type F, by apply_instance⟩ def cocone_fun (j : J) (x : (F.obj j).α) : colimit_type F := quot.mk _ (of j x) instance cocone_is_hom (j : J) : is_ring_hom (cocone_fun F j) := { map_one := begin apply quot.sound, apply relation.one, end, map_mul := λ x y, begin apply quot.sound, apply relation.mul, end, map_add := λ x y, begin apply quot.sound, apply relation.add, end } def cocone_morphism (j : J) : F.obj j ⟶ colimit F := { val := cocone_fun F j, property := by apply_instance } @[simp] lemma cocone_naturality {j j' : J} (f : j ⟶ j') : F.map f ≫ (cocone_morphism F j') = cocone_morphism F j := begin ext, apply quot.sound, apply relation.map, end @[simp] lemma cocone_naturality_components (j j' : J) (f : j ⟶ j') (x : F.obj j): (F.map f ≫ (cocone_morphism F j')) x = (cocone_morphism F j) x := by rw cocone_naturality def colimit_cocone : cocone F := { X := colimit F, ι := { app := cocone_morphism F, } }. @[simp] def desc_fun_lift (s : cocone F) : prequotient F → s.X | (of j x) := (s.ι.app j) x | zero := 0 | one := 1 | (neg x) := -(desc_fun_lift x) | (add x y) := desc_fun_lift x + desc_fun_lift y | (mul x y) := desc_fun_lift x * desc_fun_lift y @[simp] lemma naturality_bundled {G : J ⥤ CommRing} (s : cocone G) {j j' : J} (f : j ⟶ j') (x : G.obj j) : (s.ι.app j') ((G.map f) x) = (s.ι.app j) x := congr_fun (congr_arg (λ k : G.obj j ⟶ s.X, (k : G.obj j → s.X)) (s.ι.naturality f)) x def desc_fun (s : cocone F) : colimit_type F → s.X := begin fapply quot.lift, { exact desc_fun_lift F s }, { intros x y r, induction r; try { dsimp }, -- refl { refl }, -- symm { exact r_ih.symm }, -- trans { exact eq.trans r_ih_h r_ih_k }, -- map { rw naturality_bundled, }, -- zero { erw is_ring_hom.map_zero ⇑((s.ι).app r), refl }, -- one { erw is_ring_hom.map_one ⇑((s.ι).app r), refl }, -- neg { rw is_ring_hom.map_neg ⇑((s.ι).app r_j) }, -- add { rw is_ring_hom.map_add ⇑((s.ι).app r_j) }, -- mul { rw is_ring_hom.map_mul ⇑((s.ι).app r_j) }, -- neg_1 { rw r_ih, }, -- add_1 { rw r_ih, }, -- add_2 { rw r_ih, }, -- mul_1 { rw r_ih, }, -- mul_2 { rw r_ih, }, -- zero_add { rw zero_add, }, -- add_zero { rw add_zero, }, -- one_mul { rw one_mul, }, -- mul_one { rw mul_one, }, -- add_left_neg { rw add_left_neg, }, -- add_comm { rw add_comm, }, -- mul_comm { rw mul_comm, }, -- add_assoc { rw add_assoc, }, -- mul_assoc { rw mul_assoc, }, -- left_distrib { rw left_distrib, }, -- right_distrib { rw right_distrib, }, } end instance desc_fun_is_morphism (s : cocone F) : is_ring_hom (desc_fun F s) := { map_one := rfl, map_add := λ x y, begin induction x, induction y, refl, refl, refl, end, map_mul := λ x y, begin induction x, induction y, refl, refl, refl, end, } @[simp] def desc_morphism (s : cocone F) : colimit F ⟶ s.X := { val := desc_fun F s, property := by apply_instance } def colimit_is_colimit : is_colimit (colimit_cocone F) := { desc := λ s, desc_morphism F s, uniq' := λ s m w, begin ext, induction x, induction x, { have w' := congr_fun (congr_arg (λ f : F.obj x_j ⟶ s.X, (f : F.obj x_j → s.X)) (w x_j)) x_x, erw w', refl, }, { simp only [desc_morphism, quot_zero], erw is_ring_hom.map_zero ⇑m, refl, }, { simp only [desc_morphism, quot_one], erw is_ring_hom.map_one ⇑m, refl, }, { simp only [desc_morphism, quot_neg], erw is_ring_hom.map_neg ⇑m, rw [x_ih], refl, }, { simp only [desc_morphism, quot_add], erw is_ring_hom.map_add ⇑m, rw [x_ih_a, x_ih_a_1], refl, }, { simp only [desc_morphism, quot_mul], erw is_ring_hom.map_mul ⇑m, rw [x_ih_a, x_ih_a_1], refl, }, refl end }. -- FIXME why is this infer_instance needed!? instance has_colimits_CommRing : @has_colimits CommRing.{v} infer_instance := { has_colimits_of_shape := λ J 𝒥, { has_colimit := λ F, by resetI; exact { cocone := colimit_cocone F, is_colimit := colimit_is_colimit F } } } end category_theory.instances.CommRing.colimits
d40b48cc60a8e9f767e2cbbf272980004494ba57
e38e95b38a38a99ecfa1255822e78e4b26f65bb0
/src/certigrad/expected_value.lean
521eec6de83120b114dd7da333b9316046ec0842
[ "Apache-2.0" ]
permissive
ColaDrill/certigrad
fefb1be3670adccd3bed2f3faf57507f156fd501
fe288251f623ac7152e5ce555f1cd9d3a20203c2
refs/heads/master
1,593,297,324,250
1,499,903,753,000
1,499,903,753,000
97,075,797
1
0
null
1,499,916,210,000
1,499,916,210,000
null
UTF-8
Lean
false
false
23,454
lean
/- Copyright (c) 2017 Daniel Selsam. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Daniel Selsam Expected values. -/ import .sprog .graph .tfacts .compute_grad .tcont .predicates .tactics namespace certigrad namespace E open sprog list lemma E_ret {oshape : S} : Π {shapes : list S} (xs : dvec T shapes) (f : dvec T shapes → T oshape), E (sprog.ret xs) f = f xs | [] ⟦⟧ f := rfl | [d] ⟦x⟧ f := rfl | (d₁::d₂::ds) (x₁ ::: x₂ ::: xs) f := rfl lemma E_bind {oshape : S} : Π {shapes₁ shapes₂ : list S} (start : sprog shapes₁) (rest : dvec T shapes₁ → sprog shapes₂) (f : dvec T shapes₂ → T oshape), E (sprog.bind start rest) f = (E start) (λ (x : dvec T shapes₁), (E (rest x) f)) | shapes₁ [] start rest f := rfl | shapes₁ [s] start rest f := rfl | shapes₁ (s₁::s₂::shapes₂) start rest f := rfl noncomputable def is_eintegrable {oshape : S} : Π {shapes : list S}, sprog shapes → (dvec T shapes → T oshape) → Prop | shapes (@sprog.ret .(shapes) xs) f := true | shapes (@sprog.bind shapes₁ .(shapes) start rest) f := is_eintegrable start (λ (x : dvec T shapes₁), E (rest x) f) ∧ ∀ (x : dvec T shapes₁), is_eintegrable (rest x) f | ([oshape]) (@sprog.prim ishapes .(oshape) pd args) f := T.is_integrable (λ (x : T oshape), pd^.pdf args x ⬝ f ⟦x⟧) lemma is_eintegrable_ret {oshape : S} : Π {shapes : list S} (xs : dvec T shapes) (f : dvec T shapes → T oshape), is_eintegrable (sprog.ret xs) f = true | [] ⟦⟧ f := rfl | [d] ⟦x⟧ f := rfl | (d₁::d₂::ds) (x₁ ::: x₂ ::: xs) f := rfl lemma is_eintegrable_bind {oshape : S} : Π {shapes₁ shapes₂ : list S} (start : sprog shapes₁) (rest : dvec T shapes₁ → sprog shapes₂) (f : dvec T shapes₂ → T oshape), is_eintegrable (sprog.bind start rest) f = (is_eintegrable start (λ (x : dvec T shapes₁), E (rest x) f) ∧ ∀ (x : dvec T shapes₁), is_eintegrable (rest x) f) | shapes₁ [] start rest f := rfl | shapes₁ [s] start rest f := rfl | shapes₁ (s₁::s₂::shapes₂) start rest f := rfl lemma E_add {fshape : S} : Π {shapes : list S} (d : sprog shapes) (f₁ f₂ : dvec T shapes → T fshape), is_eintegrable d f₁ → is_eintegrable d f₂ → E d (λ x, f₁ x + f₂ x) = E d f₁ + E d f₂ | shapes (@sprog.ret .(shapes) xs) f₁ f₂ Hf₁ Hf₂ := by simp only [E_ret] | shapes (@sprog.bind shapes₁ .(shapes) start rest) f₁ f₂ Hf₁ Hf₂ := have H₁ : ∀ x, is_eintegrable (rest x) f₁, begin simp only [is_eintegrable_bind] at Hf₁, exact Hf₁^.right end, have H₂ : ∀ x, is_eintegrable (rest x) f₂, begin simp only [is_eintegrable_bind] at Hf₂, exact Hf₂^.right end, have G₁ : is_eintegrable start (λ (x : dvec T shapes₁), E (rest x) f₁), begin simp only [is_eintegrable_bind] at Hf₁, exact Hf₁^.left end, have G₂ : is_eintegrable start (λ (x : dvec T shapes₁), E (rest x) f₂), begin simp only [is_eintegrable_bind] at Hf₂, exact Hf₂^.left end, by simp only [E_bind, (λ x, E_add (rest x) _ _ (H₁ x) (H₂ x)), E_add start _ _ G₁ G₂] | ([oshape]) (@sprog.prim ishapes .(oshape) pd args) f₁ f₂ Hf₁ Hf₂ := have H₁ : T.is_dintegrable (λ (xs : dvec T [oshape]), rand.op.pdf pd args (dvec.head xs) ⬝ f₁ xs), begin dunfold T.is_dintegrable T.dintegral dvec.head, split, exact Hf₁, intro ignore, exact trivial end, have H₂ : T.is_dintegrable (λ (xs : dvec T [oshape]), rand.op.pdf pd args (dvec.head xs) ⬝ f₂ xs), begin dunfold T.is_dintegrable T.dintegral dvec.head, split, exact Hf₂, intro ignore, exact trivial end, T.dintegral_add_middle _ _ _ H₁ H₂ lemma is_eintegrable_add₁ {oshape : S} : Π {shapes : list S} (d : sprog shapes) (f₁ f₂ : dvec T shapes → T oshape), (is_eintegrable d f₁ ∧ is_eintegrable d f₂) → is_eintegrable d (λ x, f₁ x + f₂ x) | .(shapes) (@sprog.ret shapes xs) f₁ f₂ := begin simp only [is_eintegrable_ret], intro, exact trivial end | .(shapes) (@sprog.bind shapes₁ shapes start rest) f₁ f₂ := begin simp only [is_eintegrable_bind], intro H, cases H with H₁ H₂, split, { simp only [(λ x, E_add (rest x) _ _ (H₁^.right x) (H₂^.right x))], apply is_eintegrable_add₁, apply and.intro H₁^.left H₂^.left }, { intro x, apply is_eintegrable_add₁, apply and.intro (H₁^.right x) (H₂^.right x) } end | .([oshape]) (@sprog.prim ishapes oshape pd args) f₁ f₂ := by apply iff.mp (T.is_integrable_add_middle _ _ _) lemma is_eintegrable_add₂ {oshape : S} : Π {shapes : list S} (d : sprog shapes) (f₁ f₂ : dvec T shapes → T oshape), is_eintegrable d (λ x, f₁ x + f₂ x) → (is_eintegrable d f₁ ∧ is_eintegrable d f₂) | .(shapes) (@sprog.ret shapes xs) f₁ f₂ := begin simp only [is_eintegrable_ret], intro, split, exact trivial, exact trivial end | .(shapes) (@sprog.bind shapes₁ shapes start rest) f₁ f₂ := begin simp only [is_eintegrable_bind], intro H, cases H with H_start H_rest, assert H_rest_next : ∀ x, is_eintegrable (rest x) f₁ ∧ is_eintegrable (rest x) f₂, intro x, apply is_eintegrable_add₂, exact H_rest x, simp only [(λ x, E_add (rest x) f₁ f₂ (H_rest_next x)^.left (H_rest_next x)^.right)] at H_start, split, split, exact (is_eintegrable_add₂ _ _ _ H_start)^.left, intro x, exact (H_rest_next x)^.left, split, exact (is_eintegrable_add₂ _ _ _ H_start)^.right, intro x, exact (H_rest_next x)^.right, end | .([oshape]) (@sprog.prim ishapes oshape pd args) f₁ f₂ := by apply iff.mpr (T.is_integrable_add_middle _ _ _) lemma is_eintegrable_add {oshape : S} : Π {shapes : list S} (d : sprog shapes) (f₁ f₂ : dvec T shapes → T oshape), (is_eintegrable d f₁ ∧ is_eintegrable d f₂) ↔ is_eintegrable d (λ x, f₁ x + f₂ x) := begin intros shapes d f₁ f₂, split, apply is_eintegrable_add₁, apply is_eintegrable_add₂ end lemma E_congr {shapes : list S} {oshape : S} (d₁ d₂ : sprog shapes) (f : dvec T shapes → T oshape) (H : d₁ = d₂) : E d₁ f = E d₂ f := by rw H lemma E_scale {oshape : S} (α : ℝ) : Π {shapes : list S} (d : sprog shapes) (f : dvec T shapes → T oshape), E d (λ x, α ⬝ f x) = α ⬝ E d f | shapes (@sprog.ret .(shapes) xs) f := by simp only [E_ret] | shapes (@sprog.bind shapes₁ .(shapes) start rest) f := by simp only [E_bind, (λ x, E_scale (rest x)), E_scale start] | ([oshape]) (@sprog.prim ishapes .(oshape) pd args) f := begin dunfold E, exact T.dintegral_scale_middle α _ f end lemma E_scale_mul (α : ℝ) : Π {shapes : list S} (d : sprog shapes) (f : dvec T shapes → ℝ), E d (λ x, α * f x) = α * E d f | shapes (@sprog.ret .(shapes) xs) f := by simp only [E_ret] | shapes (@sprog.bind shapes₁ .(shapes) start rest) f := by simp only [E_bind, (λ x, E_scale_mul (rest x)), E_scale_mul start] | ([oshape]) (@sprog.prim ishapes .(oshape) pd args) f := begin dunfold E, exact T.dintegral_mul_middle α _ f end lemma E_fscale {fshape : S} (y : T fshape) : Π {shapes : list S} (d : sprog shapes) (f : dvec T shapes → ℝ), E d (λ x, f x ⬝ y) = E d f ⬝ y | shapes (@sprog.ret .(shapes) xs) f := by simp only [E_ret] | shapes (@sprog.bind shapes₁ .(shapes) start rest) f := by simp only [E_bind, (λ x, E_fscale (rest x)), E_fscale start] | [oshape] (@sprog.prim ishapes .(oshape) pd args) f := begin dunfold E T.dintegral, assert H_lam : ∀ x, rand.op.pdf pd args (dvec.head ⟦x⟧) ⬝ (f ⟦x⟧ ⬝ y) = (rand.op.pdf pd args (dvec.head ⟦x⟧) ⬝ f ⟦x⟧) ⬝ y, { intro x, erw -T.smul_group, simp only [T.smul.def, T.const_scalar] }, rw [funext H_lam, T.integral_fscale] end lemma E_neg {oshape : S} : Π {shapes : list S} (d : sprog shapes) (f : dvec T shapes → T oshape), is_eintegrable d f → E d (λ x, - (f x)) = - (E d f) | shapes (@sprog.ret .(shapes) xs) f Hf := by simp only [E_ret] | shapes (@sprog.bind shapes₁ .(shapes) start rest) f Hf := begin simp only [is_eintegrable_bind] at Hf, simp only [E_bind, (λ x, E_neg (rest x) _ (Hf^.right x)), E_neg start _ Hf^.left], end | ([oshape]) (@sprog.prim ishapes .(oshape) pd args) f Hf := begin dunfold E, exact T.dintegral_neg_middle _ f end lemma E_const {shapes : list S} {oshape fshape : S} (op : rand.op shapes oshape) (parents : dvec T shapes) (H_op_pre : op^.pre parents) (y : T fshape) : E (sprog.prim op parents) (λ x, y) = y := T.dintegral_const_middle (λ (x : dvec T [oshape]), op^.pdf parents x^.head) (λ (x : dvec T [oshape]), op^.pdf_pos parents H_op_pre x^.head) (op^.pdf_int1 parents H_op_pre) y lemma E_bind_assoc : ∀ {shapes₁ shapes₂ shapes₃ : list S} {fshape : S} (d₁ : sprog shapes₁) (d₂ : dvec T shapes₁ → sprog shapes₂) (d₃ : dvec T shapes₂ → sprog shapes₃) (f : dvec T shapes₃ → T fshape), E (bind d₁ (λ xs₁, bind (d₂ xs₁) (λ xs₂, d₃ xs₂))) f = E (bind (bind d₁ (λ xs₁, d₂ xs₁)) (λ xs₂, d₃ xs₂)) f := assume shapes₁ shapes₂ shapes₃ fshape d₁ d₂ d₃ f, begin simp only [E_bind], end lemma E_pull_out_of_sum {X : Type} {ishapes : list S} {oshape fshape : S} (op : rand.op ishapes oshape) (parents : dvec T ishapes) (H_op_pre : op^.pre parents) (f : X → dvec T [oshape] → T fshape) : ∀ (xs : list X), is_eintegrable (sprog.prim op parents) (λ y, sumr (map (λ x, f x y) xs)) → sumr (map (λ x, E (sprog.prim op parents) (f x)) xs) = E (sprog.prim op parents) (λ y, sumr (map (λ x, f x y) xs)) | [] H_xs := begin dunfold sumr map, rw E_const, exact H_op_pre end | (x::xs) H_xs := begin dunfold sumr map, rw [E_add, E_pull_out_of_sum], dunfold map sumr at H_xs, exact (iff.mpr (is_eintegrable_add _ _ _) H_xs)^.right, exact (iff.mpr (is_eintegrable_add _ _ _) H_xs)^.left, exact (iff.mpr (is_eintegrable_add _ _ _) H_xs)^.right end lemma E_k_add {shape : S} (k₁ k₂ : env → T shape) : Π (m : env) (nodes : list node), is_gintegrable (λ m, ⟦k₁ m⟧) m nodes dvec.head → is_gintegrable (λ m, ⟦k₂ m⟧) m nodes dvec.head → E (graph.to_dist (λ (m : env), ⟦k₁ m + k₂ m⟧) m nodes) dvec.head = E (graph.to_dist (λ (m : env), ⟦k₁ m⟧) m nodes) dvec.head + E (graph.to_dist (λ (m : env), ⟦k₂ m⟧) m nodes) dvec.head | m [] Hk₁ Hk₂ := begin dunfold graph.to_dist, rw [E_ret, E_ret, E_ret], reflexivity end | m (⟨ref, parents, operator.det op⟩::nodes) Hk₁ Hk₂ := begin dunfold graph.to_dist operator.to_dist, simp only [E_ret, E_bind], rw E_k_add, exact Hk₁, exact Hk₂ end | m (⟨ref, parents, operator.rand op⟩::nodes) Hk₁ Hk₂ := begin dunfold graph.to_dist operator.to_dist, rw [E_bind, E_bind, E_bind], dunfold_occs E [1, 2, 3, 5], dunfold T.dintegral, dsimp, dsimp [is_gintegrable, dvec.head] at Hk₁ Hk₂, erw -T.integral_add _ _ Hk₁^.left Hk₂^.left, apply (congr_arg T.integral), apply funext, intro x, erw -T.smul_addr, rw -E_k_add _ _ (Hk₁^.right x) (Hk₂^.right x) end lemma E_g_pull_out_of_sum {X : Type} {fshape : S} (f : env → X → T fshape) : ∀ (m : env) (nodes : list node) (xs : list X), pdfs_exist_at nodes m → is_gintegrable (λ m', ⟦sumr (map (λ x, f m' x) xs)⟧) m nodes dvec.head → sumr (map (λ x, E (graph.to_dist (λ m', ⟦f m' x⟧) m nodes) dvec.head) xs) = E (graph.to_dist (λ m', ⟦sumr (map (λ x, f m' x) xs)⟧) m nodes) dvec.head | m [] xs H_pdfs_exist H_gint := by simp only [graph.to_dist, E.E_ret, dvec.head] | m (⟨ref, parents, operator.det op⟩::nodes) xs H_pdfs_exist H_gint := begin dunfold graph.to_dist operator.to_dist, simp only [E_ret, E_bind], apply E_g_pull_out_of_sum, exact H_pdfs_exist, exact H_gint end | m (⟨ref, parents, operator.rand op⟩::nodes) xs H_pdfs_exist H_gint := begin dunfold graph.to_dist operator.to_dist, simp only [E.E_ret, E.E_bind], rw E_pull_out_of_sum _ _ H_pdfs_exist^.left, apply congr_arg, apply funext, intro y, rw E_g_pull_out_of_sum _ _ _ (H_pdfs_exist^.right y^.head) (H_gint^.right y^.head), dsimp [is_eintegrable] without dvec.head, dsimp [is_gintegrable] without dvec.head at H_gint, simp only [λ (y : dvec T [ref.2]), E_g_pull_out_of_sum _ _ _ (H_pdfs_exist^.right y^.head) (H_gint^.right y^.head)], exact H_gint^.left end end E open list -- TODO(dhs): restructure the library lemma is_gintegrable_k_add {shape : S} (k₁ k₂ : env → T shape) : Π (m : env) (nodes : list node), (is_gintegrable (λ m, ⟦k₁ m⟧) m nodes dvec.head ∧ is_gintegrable (λ m, ⟦k₂ m⟧) m nodes dvec.head) ↔ is_gintegrable (λ m, ⟦k₁ m + k₂ m⟧) m nodes dvec.head | _ [] := begin dsimp [is_gintegrable], split, intro H, exact trivial, intro H, exact (and.intro trivial trivial) end | m (⟨ref, parents, operator.det op⟩ :: nodes) := begin dsimp [is_gintegrable], apply is_gintegrable_k_add end | m (⟨ref, parents, operator.rand op⟩ :: nodes) := begin dsimp [is_gintegrable], split, { intro H, split, { simp only [λ x, E.E_k_add k₁ k₂ _ _ (H^.left^.right x) (H^.right^.right x)], apply iff.mp (T.is_integrable_add_middle _ _ _) (and.intro H^.left^.left H^.right^.left) }, intro x, exact iff.mp (is_gintegrable_k_add _ _) (and.intro (H^.left^.right x) (H^.right^.right x)) }, { intro H, assert H_kint₁ : ∀ (x : T (ref.snd)), is_gintegrable (λ (m : env), ⟦k₁ m⟧) (env.insert ref x m) nodes dvec.head, { intro x, apply (iff.mpr (is_gintegrable_k_add _ _) (H^.right x))^.left }, assert H_kint₂ : ∀ (x : T (ref.snd)), is_gintegrable (λ (m : env), ⟦k₂ m⟧) (env.insert ref x m) nodes dvec.head, { intro x, apply (iff.mpr (is_gintegrable_k_add _ _) (H^.right x))^.right }, split, { simp only [λ x, E.E_k_add k₁ k₂ _ _ (H_kint₁ x) (H_kint₂ x)] at H, split, { exact (iff.mpr (T.is_integrable_add_middle _ _ _) H^.left)^.left }, { intro x, apply (iff.mpr (is_gintegrable_k_add _ _) (H^.right x))^.left } }, { simp only [λ x, E.E_k_add k₁ k₂ _ _ (H_kint₁ x) (H_kint₂ x)] at H, split, { exact (iff.mpr (T.is_integrable_add_middle _ _ _) H^.left)^.right }, { intro x, apply (iff.mpr (is_gintegrable_k_add _ _) (H^.right x))^.right } }, } end namespace E lemma E_k_tmulT {shape₁ shape₂ : S} (k : env → T shape₂) : Π (m : env) (nodes : list node) (M : T (shape₁ ++ shape₂)), E (graph.to_dist (λ (m : env), ⟦T.tmulT M (k m)⟧) m nodes) dvec.head = T.tmulT M (E (graph.to_dist (λ (m : env), ⟦k m⟧) m nodes) dvec.head) | m [] M := begin dunfold graph.to_dist, simp [E_ret] end | m (⟨ref, parents, operator.det op⟩::nodes) M := begin dunfold graph.to_dist operator.to_dist, simp [E_ret, E_bind], rw E_k_tmulT, end | m (⟨ref, parents, operator.rand op⟩::nodes) M := begin dunfold graph.to_dist operator.to_dist, simp [E_bind], simp [E_k_tmulT], dunfold E, rw T.dintegral_tmulT_middle, end end E open list lemma is_gintegrable_tmulT {ishape oshape : S} (M : T (ishape ++ oshape)) (k : env → T oshape) : Π (inputs : env) (nodes : list node), is_gintegrable (λ (m : env), ⟦k m⟧) inputs nodes dvec.head ↔ is_gintegrable (λ (m : env), ⟦T.tmulT M (k m)⟧) inputs nodes dvec.head | inputs [] := iff.intro (λ x, trivial) (λ x, trivial) | inputs (⟨ref, parents, operator.det op⟩ :: nodes) := by { dsimp [is_gintegrable], apply is_gintegrable_tmulT } | inputs (⟨ref, parents, operator.rand op⟩ :: nodes) := begin dsimp [is_gintegrable], split, { intro H , simp only [E.E_k_tmulT], split, { exact iff.mp (T.is_integrable_tmulT_middle _ _ _) H^.left }, { intro x, exact iff.mp (is_gintegrable_tmulT _ _) (H^.right x) } }, { intro H, simp only [E.E_k_tmulT] at H, split, { exact iff.mpr (T.is_integrable_tmulT_middle _ _ _) H^.left }, { intro x, exact iff.mpr (is_gintegrable_tmulT _ _) (H^.right x) } } end lemma is_gintegrable_of_sumr_map {X : Type} [inhabited X] {shape : S} (k : env → X → T shape) (m : env) (nodes : list node) : ∀ (xs : list X) (H_gint : is_gintegrable (λ (m : env), ⟦sumr (map (k m) xs)⟧) m nodes dvec.head) (x : X), x ∈ xs → is_gintegrable (λ (m : env), ⟦k m x⟧) m nodes dvec.head | [] _ x H_in := false.rec _ (not_mem_nil x H_in) | (x::xs) H_gint y H_in := begin dunfold sumr map at H_gint, cases (eq_or_mem_of_mem_cons H_in) with H_eq H_in_rec, { subst H_eq, exact (iff.mpr (is_gintegrable_k_add _ _ _ _) H_gint)^.left }, { apply is_gintegrable_of_sumr_map xs (iff.mpr (is_gintegrable_k_add _ _ _ _) H_gint)^.right y H_in_rec } end namespace E open sprog list lemma E_k_scale {shape : S} (k : env → ℝ) (y : T shape) : Π (m : env) (nodes : list node), E (graph.to_dist (λ (m : env), ⟦k m ⬝ y⟧) m nodes) dvec.head = E (graph.to_dist (λ (m : env), ⟦k m⟧) m nodes) dvec.head ⬝ y | m [] := begin dunfold graph.to_dist, simp [E_ret] end | m (⟨ref, parents, operator.det op⟩::nodes) := begin dunfold graph.to_dist operator.to_dist, simp [E_bind, E_ret], rw E_k_scale end | m (⟨ref, parents, operator.rand op⟩::nodes) := begin dunfold graph.to_dist operator.to_dist, simp [E_bind, E_k_scale, E_fscale], end lemma E_k_sum_map {X : Type} [inhabited X] {shape : S} (k : env → X → T shape) : Π (m : env) (nodes : list node) (xs : list X), pdfs_exist_at nodes m → is_gintegrable (λ m, ⟦sumr (map (k m) xs)⟧) m nodes dvec.head → E (graph.to_dist (λ (m : env), ⟦sumr (map (k m) xs)⟧) m nodes) dvec.head = sumr (map (λ (x : X), E (graph.to_dist (λ (m : env), ⟦k m x⟧) m nodes) dvec.head) xs) | m [] xs H_pdfs H_ints := begin dunfold graph.to_dist operator.to_dist, simp [E_ret, dvec.head], end | m (⟨ref, parents, operator.det op⟩::nodes) xs H_pdfs H_ints := begin dunfold graph.to_dist operator.to_dist, dsimp [pdfs_exist_at] at H_pdfs, simp [E_ret, E_bind, dvec.head], rw E_k_sum_map _ _ _ H_pdfs H_ints end | m (⟨ref, parents, operator.rand op⟩::nodes) xs H_pdfs H_ints := begin dunfold graph.to_dist operator.to_dist, simp only [E_bind], dunfold pdfs_exist_at at H_pdfs, dsimp [is_gintegrable] at H_ints, simp only [λ x, E_k_sum_map _ _ xs (H_pdfs^.right x) (H_ints^.right x)], rw E_pull_out_of_sum op _ H_pdfs^.left, dunfold is_eintegrable, -- TODO(dhs): bad form to do induction in the middle of a proof -- could make this a lemma induction xs with x xs IHxs, { dunfold sumr map, simp only [T.smul_zero], exact T.is_integrable_zero }, { dunfold sumr map, apply iff.mp (T.is_integrable_add_middle (rand.op.pdf op (env.get_ks parents m)) (λ x_1, E (graph.to_dist (λ (m : env), ⟦k m x⟧) (env.insert ref (dvec.head ⟦x_1⟧) m) nodes) dvec.head) (λ x_1, sumr (map (λ (x : X), E (graph.to_dist (λ (m : env), ⟦k m x⟧) (env.insert ref (dvec.head ⟦x_1⟧) m) nodes) dvec.head) xs))), dunfold sumr map at H_ints, simp only [λ x_1, E_k_add (λ m, k m x) (λ m, sumr (map (k m) xs)) (env.insert ref x_1 m) nodes (iff.mpr (is_gintegrable_k_add _ _ _ _) (H_ints^.right x_1))^.left (iff.mpr (is_gintegrable_k_add _ _ _ _) (H_ints^.right x_1))^.right] at H_ints, split, exact (iff.mpr (T.is_integrable_add_middle _ _ _) H_ints^.left)^.left, apply IHxs, split, exact (iff.mpr (T.is_integrable_add_middle _ _ _) H_ints^.left)^.right, intro y, exact (iff.mpr (is_gintegrable_k_add _ _ _ _) (H_ints^.right y))^.right, } end lemma E_continuous {ishapes : list S} {oshape tshape fshape : S} (pd : rand.op ishapes oshape) (args : T tshape → dvec T ishapes) (f : dvec T [oshape] → T tshape → T fshape) (θ : T tshape) : (∀ x, T.is_continuous (λ θ₀, pd^.pdf (args θ₀) x) θ) → (∀ x, T.is_continuous (f x) θ) → T.is_continuous (λ θ₀, E (sprog.prim pd (args θ₀)) (λ x₀, f x₀ θ₀)) θ := assume (H_pdf_continuous : ∀ x, T.is_continuous (λ θ₀, pd^.pdf (args θ₀) x) θ) (H_f_continuous : ∀ x, T.is_continuous (f x) θ), begin dunfold E T.dintegral, apply T.integral_continuous, intro x, apply T.continuous_scale_fs, apply H_pdf_continuous, apply H_f_continuous end lemma E_move_fn_to_continuation (shapes : list S) (fshape : S) (k : env → dvec T shapes) (f : dvec T shapes → T fshape) : Π (inputs : env) (nodes : list node), E (graph.to_dist k inputs nodes) f = E (graph.to_dist (λ m, ⟦f (k m)⟧) inputs nodes) dvec.head | m [] := begin dunfold graph.to_dist, simp [E_ret] end | m (⟨ref, parents, op⟩::nodes) := begin dunfold graph.to_dist, simp [E_bind], apply congr_arg, apply funext, intro x, rw E_move_fn_to_continuation end lemma E_of_lookup : ∀ {nodes : list node} {inputs : env} {loss : reference} {val : T loss.2}, loss ∉ map node.ref nodes → pdfs_exist_at nodes (env.insert loss val inputs) → E (graph.to_dist (λ (m : env), ⟦env.get loss m⟧) (env.insert loss val inputs) nodes) dvec.head = val | [] inputs loss val H_loss_unused H_pdfs_exist_at := begin dunfold graph.to_dist, simp [E_ret], dunfold dvec.head, rw env.get_insert_same end | (⟨ref, parents, operator.det op⟩::nodes) inputs loss val H_loss_unused H_pdfs_exist_at := begin dunfold graph.to_dist operator.to_dist, simp [E_bind, E_ret], assertv H_loss_neq_ref : loss ≠ ref := ne_of_not_mem_cons H_loss_unused, assertv H_loss_unused_next : loss ∉ map node.ref nodes := not_mem_of_not_mem_cons H_loss_unused, rw env.insert_insert_flip _ _ _ (ne.symm H_loss_neq_ref), dunfold pdfs_exist_at at H_pdfs_exist_at, rw env.insert_insert_flip _ _ _ (ne.symm H_loss_neq_ref) at H_pdfs_exist_at, exact (E_of_lookup H_loss_unused_next H_pdfs_exist_at), end | (⟨ref, parents, operator.rand op⟩::nodes) inputs loss val H_loss_unused H_pdfs_exist_at := begin dunfold graph.to_dist operator.to_dist, simp [E_bind], assertv H_loss_neq_ref : loss ≠ ref := ne_of_not_mem_cons H_loss_unused, assertv H_loss_unused_next : loss ∉ map node.ref nodes := not_mem_of_not_mem_cons H_loss_unused, assert H_inside : E (sprog.prim op (env.get_ks parents (env.insert loss val inputs))) (λ (x_1 : dvec T [ref^.snd]), E (graph.to_dist (λ (m : env), ⟦env.get loss m⟧) (env.insert ref (dvec.head x_1) (env.insert loss val inputs)) nodes) dvec.head) = E (sprog.prim op (env.get_ks parents (env.insert loss val inputs))) (λ (x_1 : dvec T [ref^.snd]), val), { apply congr_arg, apply funext, intro x, rw env.insert_insert_flip _ _ _ (ne.symm H_loss_neq_ref), dunfold pdfs_exist_at at H_pdfs_exist_at, note H_pdfs_exist_at_next := H_pdfs_exist_at^.right x^.head, dsimp [pdfs_exist_at] at H_pdfs_exist_at_next, rw env.insert_insert_flip _ _ _ (ne.symm H_loss_neq_ref) at H_pdfs_exist_at_next, exact (E_of_lookup H_loss_unused_next H_pdfs_exist_at_next) }, rw H_inside, clear H_inside, dunfold E T.dintegral dvec.head, dsimp, rw T.integral_fscale, assert H_pdf_1 : ∫ (λ (x : T (ref^.snd)), rand.op.pdf op (env.get_ks parents (env.insert loss val inputs)) (dvec.head ⟦x⟧)) = 1, { exact op^.pdf_int1 _ H_pdfs_exist_at^.left }, dunfold dvec.head at H_pdf_1, rw H_pdf_1, rw T.one_smul end end E end certigrad
e9bb653035eb3fbe976adbebff3dd13f530081bc
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/normed_space/lp_equiv.lean
9555744026e361e9c125c54a0117b2e54754e16f
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
7,348
lean
/- Copyright (c) 2022 Jireh Loreaux. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jireh Loreaux -/ import analysis.normed_space.lp_space import analysis.normed_space.pi_Lp import topology.continuous_function.bounded /-! # Equivalences among $L^p$ spaces > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we collect a variety of equivalences among various $L^p$ spaces. In particular, when `α` is a `fintype`, given `E : α → Type u` and `p : ℝ≥0∞`, there is a natural linear isometric equivalence `lp_pi_Lpₗᵢ : lp E p ≃ₗᵢ pi_Lp p E`. In addition, when `α` is a discrete topological space, the bounded continuous functions `α →ᵇ β` correspond exactly to `lp (λ _, β) ∞`. Here there can be more structure, including ring and algebra structures, and we implement these equivalences accordingly as well. We keep this as a separate file so that the various $L^p$ space files don't import the others. Recall that `pi_Lp` is just a type synonym for `Π i, E i` but given a different metric and norm structure, although the topological, uniform and bornological structures coincide definitionally. These structures are only defined on `pi_Lp` for `fintype α`, so there are no issues of convergence to consider. While `pre_lp` is also a type synonym for `Π i, E i`, it allows for infinite index types. On this type there is a predicate `mem_ℓp` which says that the relevant `p`-norm is finite and `lp E p` is the subtype of `pre_lp` satisfying `mem_ℓp`. ## TODO * Equivalence between `lp` and `measure_theory.Lp`, for `f : α → E` (i.e., functions rather than pi-types) and the counting measure on `α` -/ open_locale ennreal section lp_pi_Lp variables {α : Type*} {E : α → Type*} [Π i, normed_add_comm_group (E i)] {p : ℝ≥0∞} /-- When `α` is `finite`, every `f : pre_lp E p` satisfies `mem_ℓp f p`. -/ lemma mem_ℓp.all [finite α] (f : Π i, E i) : mem_ℓp f p := begin rcases p.trichotomy with (rfl | rfl | h), { exact mem_ℓp_zero_iff.mpr {i : α | f i ≠ 0}.to_finite, }, { exact mem_ℓp_infty_iff.mpr (set.finite.bdd_above (set.range (λ (i : α), ‖f i‖)).to_finite) }, { casesI nonempty_fintype α, exact mem_ℓp_gen ⟨finset.univ.sum _, has_sum_fintype _⟩ } end variables [fintype α] /-- The canonical `equiv` between `lp E p ≃ pi_Lp p E` when `E : α → Type u` with `[fintype α]`. -/ def equiv.lp_pi_Lp : lp E p ≃ pi_Lp p E := { to_fun := λ f, f, inv_fun := λ f, ⟨f, mem_ℓp.all f⟩, left_inv := λ f, lp.ext $ funext $ λ x, rfl, right_inv := λ f, funext $ λ x, rfl } lemma coe_equiv_lp_pi_Lp (f : lp E p) : equiv.lp_pi_Lp f = f := rfl lemma coe_equiv_lp_pi_Lp_symm (f : pi_Lp p E) : (equiv.lp_pi_Lp.symm f : Π i, E i) = f := rfl lemma equiv_lp_pi_Lp_norm (f : lp E p) : ‖equiv.lp_pi_Lp f‖ = ‖f‖ := begin unfreezingI { rcases p.trichotomy with (rfl | rfl | h) }, { rw [pi_Lp.norm_eq_card, lp.norm_eq_card_dsupport], refl }, { rw [pi_Lp.norm_eq_csupr, lp.norm_eq_csupr], refl }, { rw [pi_Lp.norm_eq_sum h, lp.norm_eq_tsum_rpow h, tsum_fintype], refl }, end /-- The canonical `add_equiv` between `lp E p` and `pi_Lp p E` when `E : α → Type u` with `[fintype α]` and `[fact (1 ≤ p)]`. -/ def add_equiv.lp_pi_Lp [fact (1 ≤ p)] : lp E p ≃+ pi_Lp p E := { map_add' := λ f g, rfl, .. equiv.lp_pi_Lp } lemma coe_add_equiv_lp_pi_Lp [fact (1 ≤ p)] (f : lp E p) : add_equiv.lp_pi_Lp f = f := rfl lemma coe_add_equiv_lp_pi_Lp_symm [fact (1 ≤ p)] (f : pi_Lp p E) : (add_equiv.lp_pi_Lp.symm f : Π i, E i) = f := rfl section equivₗᵢ variables (𝕜 : Type*) [nontrivially_normed_field 𝕜] [Π i, normed_space 𝕜 (E i)] /-- The canonical `linear_isometry_equiv` between `lp E p` and `pi_Lp p E` when `E : α → Type u` with `[fintype α]` and `[fact (1 ≤ p)]`. -/ noncomputable def lp_pi_Lpₗᵢ [fact (1 ≤ p)] : lp E p ≃ₗᵢ[𝕜] pi_Lp p E := { map_smul' := λ k f, rfl, norm_map' := equiv_lp_pi_Lp_norm, .. add_equiv.lp_pi_Lp } variables {𝕜} lemma coe_lp_pi_Lpₗᵢ [fact (1 ≤ p)] (f : lp E p) : lp_pi_Lpₗᵢ 𝕜 f = f := rfl lemma coe_lp_pi_Lpₗᵢ_symm [fact (1 ≤ p)] (f : pi_Lp p E) : ((lp_pi_Lpₗᵢ 𝕜).symm f : Π i, E i) = f := rfl end equivₗᵢ end lp_pi_Lp section lp_bcf open_locale bounded_continuous_function open bounded_continuous_function -- note: `R` and `A` are explicit because otherwise Lean has elaboration problems variables {α E : Type*} (R A 𝕜 : Type*) [topological_space α] [discrete_topology α] variables [normed_ring A] [norm_one_class A] [nontrivially_normed_field 𝕜] [normed_algebra 𝕜 A] variables [normed_add_comm_group E] [normed_space 𝕜 E] [non_unital_normed_ring R] section normed_add_comm_group /-- The canonical map between `lp (λ (_ : α), E) ∞` and `α →ᵇ E` as an `add_equiv`. -/ noncomputable def add_equiv.lp_bcf : lp (λ (_ : α), E) ∞ ≃+ (α →ᵇ E) := { to_fun := λ f, of_normed_add_comm_group_discrete f (‖f‖) $ le_csupr (mem_ℓp_infty_iff.mp f.prop), inv_fun := λ f, ⟨f, f.bdd_above_range_norm_comp⟩, left_inv := λ f, lp.ext rfl, right_inv := λ f, ext $ λ x, rfl, map_add' := λ f g, ext $ λ x, rfl } lemma coe_add_equiv_lp_bcf (f : lp (λ (_ : α), E) ∞) : (add_equiv.lp_bcf f : α → E) = f := rfl lemma coe_add_equiv_lp_bcf_symm (f : α →ᵇ E) : (add_equiv.lp_bcf.symm f : α → E) = f := rfl /-- The canonical map between `lp (λ (_ : α), E) ∞` and `α →ᵇ E` as a `linear_isometry_equiv`. -/ noncomputable def lp_bcfₗᵢ : lp (λ (_ : α), E) ∞ ≃ₗᵢ[𝕜] (α →ᵇ E) := { map_smul' := λ k f, rfl, norm_map' := λ f, by { simp only [norm_eq_supr_norm, lp.norm_eq_csupr], refl }, .. add_equiv.lp_bcf } variables {𝕜} lemma coe_lp_bcfₗᵢ (f : lp (λ (_ : α), E) ∞) : (lp_bcfₗᵢ 𝕜 f : α → E) = f := rfl lemma coe_lp_bcfₗᵢ_symm (f : α →ᵇ E) : ((lp_bcfₗᵢ 𝕜).symm f : α → E) = f := rfl end normed_add_comm_group section ring_algebra /-- The canonical map between `lp (λ (_ : α), R) ∞` and `α →ᵇ R` as a `ring_equiv`. -/ noncomputable def ring_equiv.lp_bcf : lp (λ (_ : α), R) ∞ ≃+* (α →ᵇ R) := { map_mul' := λ f g, ext $ λ x, rfl, .. @add_equiv.lp_bcf _ R _ _ _ } variables {R} lemma coe_ring_equiv_lp_bcf (f : lp (λ (_ : α), R) ∞) : (ring_equiv.lp_bcf R f : α → R) = f := rfl lemma coe_ring_equiv_lp_bcf_symm (f : α →ᵇ R) : ((ring_equiv.lp_bcf R).symm f : α → R) = f := rfl variables (α) -- even `α` needs to be explicit here for elaboration -- the `norm_one_class A` shouldn't really be necessary, but currently it is for -- `one_mem_ℓp_infty` to get the `ring` instance on `lp`. /-- The canonical map between `lp (λ (_ : α), A) ∞` and `α →ᵇ A` as an `alg_equiv`. -/ noncomputable def alg_equiv.lp_bcf : lp (λ (_ : α), A) ∞ ≃ₐ[𝕜] (α →ᵇ A) := { commutes' := λ k, rfl, .. ring_equiv.lp_bcf A } variables {α A 𝕜} lemma coe_alg_equiv_lp_bcf (f : lp (λ (_ : α), A) ∞) : (alg_equiv.lp_bcf α A 𝕜 f : α → A) = f := rfl lemma coe_alg_equiv_lp_bcf_symm (f : α →ᵇ A) : ((alg_equiv.lp_bcf α A 𝕜).symm f : α → A) = f := rfl end ring_algebra end lp_bcf
aed446d60adcb4754d852fc62fa99b4f6c0ee00b
618003631150032a5676f229d13a079ac875ff77
/src/data/finset.lean
12cb2a02bbe28ac529f5f1242550d7cb0c3adaf5
[ "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
123,142
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura, Jeremy Avigad, Minchao Wu, Mario Carneiro Finite sets. -/ import data.multiset import tactic.monotonicity import tactic.apply open multiset subtype nat variables {α : Type*} {β : Type*} {γ : Type*} /-- `finset α` is the type of finite sets of elements of `α`. It is implemented as a multiset (a list up to permutation) which has no duplicate elements. -/ structure finset (α : Type*) := (val : multiset α) (nodup : nodup val) namespace finset theorem eq_of_veq : ∀ {s t : finset α}, s.1 = t.1 → s = t | ⟨s, _⟩ ⟨t, _⟩ rfl := rfl @[simp] theorem val_inj {s t : finset α} : s.1 = t.1 ↔ s = t := ⟨eq_of_veq, congr_arg _⟩ @[simp] theorem erase_dup_eq_self [decidable_eq α] (s : finset α) : erase_dup s.1 = s.1 := erase_dup_eq_self.2 s.2 instance has_decidable_eq [decidable_eq α] : decidable_eq (finset α) | s₁ s₂ := decidable_of_iff _ val_inj /- membership -/ instance : has_mem α (finset α) := ⟨λ a s, a ∈ s.1⟩ theorem mem_def {a : α} {s : finset α} : a ∈ s ↔ a ∈ s.1 := iff.rfl @[simp] theorem mem_mk {a : α} {s nd} : a ∈ @finset.mk α s nd ↔ a ∈ s := iff.rfl instance decidable_mem [h : decidable_eq α] (a : α) (s : finset α) : decidable (a ∈ s) := multiset.decidable_mem _ _ /-! ### set coercion -/ /-- Convert a finset to a set in the natural way. -/ instance : has_lift (finset α) (set α) := ⟨λ s, {x | x ∈ s}⟩ @[simp] lemma mem_coe {a : α} {s : finset α} : a ∈ (↑s : set α) ↔ a ∈ s := iff.rfl @[simp] lemma set_of_mem {α} {s : finset α} : {a | a ∈ s} = ↑s := rfl instance decidable_mem' [decidable_eq α] (a : α) (s : finset α) : decidable (a ∈ (↑s : set α)) := s.decidable_mem _ /-! ### extensionality -/ theorem ext {s₁ s₂ : finset α} : s₁ = s₂ ↔ ∀ a, a ∈ s₁ ↔ a ∈ s₂ := val_inj.symm.trans $ nodup_ext s₁.2 s₂.2 @[ext] theorem ext' {s₁ s₂ : finset α} : (∀ a, a ∈ s₁ ↔ a ∈ s₂) → s₁ = s₂ := ext.2 @[simp] theorem coe_inj {s₁ s₂ : finset α} : (↑s₁ : set α) = ↑s₂ ↔ s₁ = s₂ := set.ext_iff.trans ext.symm lemma coe_injective {α} : function.injective (coe : finset α → set α) := λ s t, coe_inj.1 /-! ### subset -/ instance : has_subset (finset α) := ⟨λ s₁ s₂, ∀ ⦃a⦄, a ∈ s₁ → a ∈ s₂⟩ theorem subset_def {s₁ s₂ : finset α} : s₁ ⊆ s₂ ↔ s₁.1 ⊆ s₂.1 := iff.rfl @[simp] theorem subset.refl (s : finset α) : s ⊆ s := subset.refl _ theorem subset.trans {s₁ s₂ s₃ : finset α} : s₁ ⊆ s₂ → s₂ ⊆ s₃ → s₁ ⊆ s₃ := subset.trans theorem superset.trans {s₁ s₂ s₃ : finset α} : s₁ ⊇ s₂ → s₂ ⊇ s₃ → s₁ ⊇ s₃ := λ h' h, subset.trans h h' -- TODO: these should be global attributes, but this will require fixing other files local attribute [trans] subset.trans superset.trans theorem mem_of_subset {s₁ s₂ : finset α} {a : α} : s₁ ⊆ s₂ → a ∈ s₁ → a ∈ s₂ := mem_of_subset theorem subset.antisymm {s₁ s₂ : finset α} (H₁ : s₁ ⊆ s₂) (H₂ : s₂ ⊆ s₁) : s₁ = s₂ := ext.2 $ λ a, ⟨@H₁ a, @H₂ a⟩ theorem subset_iff {s₁ s₂ : finset α} : s₁ ⊆ s₂ ↔ ∀ ⦃x⦄, x ∈ s₁ → x ∈ s₂ := iff.rfl @[simp] theorem coe_subset {s₁ s₂ : finset α} : (↑s₁ : set α) ⊆ ↑s₂ ↔ s₁ ⊆ s₂ := iff.rfl @[simp] theorem val_le_iff {s₁ s₂ : finset α} : s₁.1 ≤ s₂.1 ↔ s₁ ⊆ s₂ := le_iff_subset s₁.2 instance : has_ssubset (finset α) := ⟨λa b, a ⊆ b ∧ ¬ b ⊆ a⟩ instance : partial_order (finset α) := { le := (⊆), lt := (⊂), le_refl := subset.refl, le_trans := @subset.trans _, le_antisymm := @subset.antisymm _ } theorem subset.antisymm_iff {s₁ s₂ : finset α} : s₁ = s₂ ↔ s₁ ⊆ s₂ ∧ s₂ ⊆ s₁ := le_antisymm_iff @[simp] theorem le_iff_subset {s₁ s₂ : finset α} : s₁ ≤ s₂ ↔ s₁ ⊆ s₂ := iff.rfl @[simp] theorem lt_iff_ssubset {s₁ s₂ : finset α} : s₁ < s₂ ↔ s₁ ⊂ s₂ := iff.rfl @[simp] lemma coe_ssubset {s₁ s₂ : finset α} : (↑s₁ : set α) ⊂ ↑s₂ ↔ s₁ ⊂ s₂ := show (↑s₁ : set α) ⊂ ↑s₂ ↔ s₁ ⊆ s₂ ∧ ¬s₂ ⊆ s₁, by simp only [set.ssubset_def, finset.coe_subset] @[simp] theorem val_lt_iff {s₁ s₂ : finset α} : s₁.1 < s₂.1 ↔ s₁ ⊂ s₂ := and_congr val_le_iff $ not_congr val_le_iff /-! ### Nonempty -/ /-- The property `s.nonempty` expresses the fact that the finset `s` is not empty. It should be used in theorem assumptions instead of `∃ x, x ∈ s` or `s ≠ ∅` as it gives access to a nice API thanks to the dot notation. -/ protected def nonempty (s : finset α) : Prop := ∃ x:α, x ∈ s @[norm_cast] lemma coe_nonempty {s : finset α} : (↑s:set α).nonempty ↔ s.nonempty := iff.rfl lemma nonempty.bex {s : finset α} (h : s.nonempty) : ∃ x:α, x ∈ s := h lemma nonempty.mono {s t : finset α} (hst : s ⊆ t) (hs : s.nonempty) : t.nonempty := set.nonempty.mono hst hs /-! ### empty -/ /-- The empty finset -/ protected def empty : finset α := ⟨0, nodup_zero⟩ instance : has_emptyc (finset α) := ⟨finset.empty⟩ instance : inhabited (finset α) := ⟨∅⟩ @[simp] theorem empty_val : (∅ : finset α).1 = 0 := rfl @[simp] theorem not_mem_empty (a : α) : a ∉ (∅ : finset α) := id @[simp] theorem ne_empty_of_mem {a : α} {s : finset α} (h : a ∈ s) : s ≠ ∅ := λ e, not_mem_empty a $ e ▸ h theorem nonempty.ne_empty {s : finset α} (h : s.nonempty) : s ≠ ∅ := exists.elim h $ λ a, ne_empty_of_mem @[simp] theorem empty_subset (s : finset α) : ∅ ⊆ s := zero_subset _ theorem eq_empty_of_forall_not_mem {s : finset α} (H : ∀x, x ∉ s) : s = ∅ := eq_of_veq (eq_zero_of_forall_not_mem H) lemma eq_empty_iff_forall_not_mem {s : finset α} : s = ∅ ↔ ∀ x, x ∉ s := ⟨by rintro rfl x; exact id, λ h, eq_empty_of_forall_not_mem h⟩ @[simp] theorem val_eq_zero {s : finset α} : s.1 = 0 ↔ s = ∅ := @val_inj _ s ∅ theorem subset_empty {s : finset α} : s ⊆ ∅ ↔ s = ∅ := subset_zero.trans val_eq_zero theorem nonempty_of_ne_empty {s : finset α} (h : s ≠ ∅) : s.nonempty := exists_mem_of_ne_zero (mt val_eq_zero.1 h) theorem nonempty_iff_ne_empty {s : finset α} : s.nonempty ↔ s ≠ ∅ := ⟨nonempty.ne_empty, nonempty_of_ne_empty⟩ theorem eq_empty_or_nonempty (s : finset α) : s = ∅ ∨ s.nonempty := classical.by_cases or.inl (λ h, or.inr (nonempty_of_ne_empty h)) @[simp] lemma coe_empty : ↑(∅ : finset α) = (∅ : set α) := rfl /-! ### singleton -/ /-- `{a} : finset a` is the set `{a}` containing `a` and nothing else. This differs from `insert a ∅` in that it does not require a `decidable_eq` instance for `α`. -/ instance : has_singleton α (finset α) := ⟨λ a, ⟨{a}, nodup_singleton a⟩⟩ @[simp] theorem singleton_val (a : α) : ({a} : finset α).1 = a :: 0 := rfl @[simp] theorem mem_singleton {a b : α} : b ∈ ({a} : finset α) ↔ b = a := mem_singleton theorem not_mem_singleton {a b : α} : a ∉ ({b} : finset α) ↔ a ≠ b := not_congr mem_singleton theorem mem_singleton_self (a : α) : a ∈ ({a} : finset α) := or.inl rfl theorem singleton_inj {a b : α} : ({a} : finset α) = {b} ↔ a = b := ⟨λ h, mem_singleton.1 (h ▸ mem_singleton_self _), congr_arg _⟩ theorem singleton_nonempty (a : α) : ({a} : finset α).nonempty := ⟨a, mem_singleton_self a⟩ @[simp] theorem singleton_ne_empty (a : α) : ({a} : finset α) ≠ ∅ := (singleton_nonempty a).ne_empty @[simp] lemma coe_singleton (a : α) : ↑({a} : finset α) = ({a} : set α) := by { ext, simp } lemma eq_singleton_iff_unique_mem {s : finset α} {a : α} : s = {a} ↔ a ∈ s ∧ ∀ x ∈ s, x = a := begin split; intro t, rw t, refine ⟨finset.mem_singleton_self _, λ _, finset.mem_singleton.1⟩, ext, rw finset.mem_singleton, refine ⟨t.right _, λ r, r.symm ▸ t.left⟩ end lemma singleton_iff_unique_mem (s : finset α) : (∃ a, s = {a}) ↔ ∃! a, a ∈ s := by simp only [eq_singleton_iff_unique_mem, exists_unique] lemma singleton_subset_set_iff {s : set α} {a : α} : ↑({a} : finset α) ⊆ s ↔ a ∈ s := by rw [coe_singleton, set.singleton_subset_iff] @[simp] lemma singleton_subset_iff {s : finset α} {a : α} : {a} ⊆ s ↔ a ∈ s := singleton_subset_set_iff /-! ### insert -/ section decidable_eq variables [decidable_eq α] /-- `insert a s` is the set `{a} ∪ s` containing `a` and the elements of `s`. -/ instance : has_insert α (finset α) := ⟨λ a s, ⟨_, nodup_ndinsert a s.2⟩⟩ theorem insert_def (a : α) (s : finset α) : insert a s = ⟨_, nodup_ndinsert a s.2⟩ := rfl @[simp] theorem insert_val (a : α) (s : finset α) : (insert a s).1 = ndinsert a s.1 := rfl theorem insert_val' (a : α) (s : finset α) : (insert a s).1 = erase_dup (a :: s.1) := by rw [erase_dup_cons, erase_dup_eq_self]; refl theorem insert_val_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : (insert a s).1 = a :: s.1 := by rw [insert_val, ndinsert_of_not_mem h] @[simp] theorem mem_insert {a b : α} {s : finset α} : a ∈ insert b s ↔ a = b ∨ a ∈ s := mem_ndinsert theorem mem_insert_self (a : α) (s : finset α) : a ∈ insert a s := mem_ndinsert_self a s.1 theorem mem_insert_of_mem {a b : α} {s : finset α} (h : a ∈ s) : a ∈ insert b s := mem_ndinsert_of_mem h theorem mem_of_mem_insert_of_ne {a b : α} {s : finset α} (h : b ∈ insert a s) : b ≠ a → b ∈ s := (mem_insert.1 h).resolve_left @[simp] lemma coe_insert (a : α) (s : finset α) : ↑(insert a s) = (insert a ↑s : set α) := set.ext $ λ x, by simp only [mem_coe, mem_insert, set.mem_insert_iff] instance : is_lawful_singleton α (finset α) := ⟨λ a, by { ext, simp }⟩ @[simp] theorem insert_eq_of_mem {a : α} {s : finset α} (h : a ∈ s) : insert a s = s := eq_of_veq $ ndinsert_of_mem h @[simp] theorem insert_singleton_self_eq (a : α) : ({a, a} : finset α) = {a} := insert_eq_of_mem $ mem_singleton_self _ theorem insert.comm (a b : α) (s : finset α) : insert a (insert b s) = insert b (insert a s) := ext.2 $ λ x, by simp only [finset.mem_insert, or.left_comm] @[simp] theorem insert_idem (a : α) (s : finset α) : insert a (insert a s) = insert a s := ext.2 $ λ x, by simp only [finset.mem_insert, or.assoc.symm, or_self] @[simp] theorem insert_ne_empty (a : α) (s : finset α) : insert a s ≠ ∅ := ne_empty_of_mem (mem_insert_self a s) lemma ne_insert_of_not_mem (s t : finset α) {a : α} (h : a ∉ s) : s ≠ insert a t := by { contrapose! h, simp [h] } theorem insert_subset {a : α} {s t : finset α} : insert a s ⊆ t ↔ a ∈ t ∧ s ⊆ t := by simp only [subset_iff, mem_insert, forall_eq, or_imp_distrib, forall_and_distrib] theorem subset_insert (a : α) (s : finset α) : s ⊆ insert a s := λ b, mem_insert_of_mem theorem insert_subset_insert (a : α) {s t : finset α} (h : s ⊆ t) : insert a s ⊆ insert a t := insert_subset.2 ⟨mem_insert_self _ _, subset.trans h (subset_insert _ _)⟩ lemma ssubset_iff {s t : finset α} : s ⊂ t ↔ (∃a, a ∉ s ∧ insert a s ⊆ t) := iff.intro (assume ⟨h₁, h₂⟩, have ∃a ∈ t, a ∉ s, by simpa only [finset.subset_iff, classical.not_forall] using h₂, let ⟨a, hat, has⟩ := this in ⟨a, has, insert_subset.mpr ⟨hat, h₁⟩⟩) (assume ⟨a, hat, has⟩, let ⟨h₁, h₂⟩ := insert_subset.mp has in ⟨h₂, assume h, hat $ h h₁⟩) lemma ssubset_insert {s : finset α} {a : α} (h : a ∉ s) : s ⊂ insert a s := ssubset_iff.mpr ⟨a, h, subset.refl _⟩ @[recursor 6] protected theorem induction {α : Type*} {p : finset α → Prop} [decidable_eq α] (h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α}, a ∉ s → p s → p (insert a s)) : ∀ s, p s | ⟨s, nd⟩ := multiset.induction_on s (λ _, h₁) (λ a s IH nd, begin cases nodup_cons.1 nd with m nd', rw [← (eq_of_veq _ : insert a (finset.mk s _) = ⟨a::s, nd⟩)], { exact h₂ (by exact m) (IH nd') }, { rw [insert_val, ndinsert_of_not_mem m] } end) nd /-- To prove a proposition about an arbitrary `finset α`, it suffices to prove it for the empty `finset`, and to show that if it holds for some `finset α`, then it holds for the `finset` obtained by inserting a new element. -/ @[elab_as_eliminator] protected theorem induction_on {α : Type*} {p : finset α → Prop} [decidable_eq α] (s : finset α) (h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α}, a ∉ s → p s → p (insert a s)) : p s := finset.induction h₁ h₂ s /-! ### union -/ /-- `s ∪ t` is the set such that `a ∈ s ∪ t` iff `a ∈ s` or `a ∈ t`. -/ instance : has_union (finset α) := ⟨λ s₁ s₂, ⟨_, nodup_ndunion s₁.1 s₂.2⟩⟩ theorem union_val_nd (s₁ s₂ : finset α) : (s₁ ∪ s₂).1 = ndunion s₁.1 s₂.1 := rfl @[simp] theorem union_val (s₁ s₂ : finset α) : (s₁ ∪ s₂).1 = s₁.1 ∪ s₂.1 := ndunion_eq_union s₁.2 @[simp] theorem mem_union {a : α} {s₁ s₂ : finset α} : a ∈ s₁ ∪ s₂ ↔ a ∈ s₁ ∨ a ∈ s₂ := mem_ndunion theorem mem_union_left {a : α} {s₁ : finset α} (s₂ : finset α) (h : a ∈ s₁) : a ∈ s₁ ∪ s₂ := mem_union.2 $ or.inl h theorem mem_union_right {a : α} {s₂ : finset α} (s₁ : finset α) (h : a ∈ s₂) : a ∈ s₁ ∪ s₂ := mem_union.2 $ or.inr h theorem not_mem_union {a : α} {s₁ s₂ : finset α} : a ∉ s₁ ∪ s₂ ↔ a ∉ s₁ ∧ a ∉ s₂ := by rw [mem_union, not_or_distrib] @[simp] lemma coe_union (s₁ s₂ : finset α) : ↑(s₁ ∪ s₂) = (↑s₁ ∪ ↑s₂ : set α) := set.ext $ λ x, mem_union theorem union_subset {s₁ s₂ s₃ : finset α} (h₁ : s₁ ⊆ s₃) (h₂ : s₂ ⊆ s₃) : s₁ ∪ s₂ ⊆ s₃ := val_le_iff.1 (ndunion_le.2 ⟨h₁, val_le_iff.2 h₂⟩) theorem subset_union_left (s₁ s₂ : finset α) : s₁ ⊆ s₁ ∪ s₂ := λ x, mem_union_left _ theorem subset_union_right (s₁ s₂ : finset α) : s₂ ⊆ s₁ ∪ s₂ := λ x, mem_union_right _ theorem union_comm (s₁ s₂ : finset α) : s₁ ∪ s₂ = s₂ ∪ s₁ := ext.2 $ λ x, by simp only [mem_union, or_comm] instance : is_commutative (finset α) (∪) := ⟨union_comm⟩ @[simp] theorem union_assoc (s₁ s₂ s₃ : finset α) : (s₁ ∪ s₂) ∪ s₃ = s₁ ∪ (s₂ ∪ s₃) := ext.2 $ λ x, by simp only [mem_union, or_assoc] instance : is_associative (finset α) (∪) := ⟨union_assoc⟩ @[simp] theorem union_idempotent (s : finset α) : s ∪ s = s := ext.2 $ λ _, mem_union.trans $ or_self _ instance : is_idempotent (finset α) (∪) := ⟨union_idempotent⟩ theorem union_left_comm (s₁ s₂ s₃ : finset α) : s₁ ∪ (s₂ ∪ s₃) = s₂ ∪ (s₁ ∪ s₃) := ext.2 $ λ _, by simp only [mem_union, or.left_comm] theorem union_right_comm (s₁ s₂ s₃ : finset α) : (s₁ ∪ s₂) ∪ s₃ = (s₁ ∪ s₃) ∪ s₂ := ext.2 $ λ x, by simp only [mem_union, or_assoc, or_comm (x ∈ s₂)] theorem union_self (s : finset α) : s ∪ s = s := union_idempotent s @[simp] theorem union_empty (s : finset α) : s ∪ ∅ = s := ext.2 $ λ x, mem_union.trans $ or_false _ @[simp] theorem empty_union (s : finset α) : ∅ ∪ s = s := ext.2 $ λ x, mem_union.trans $ false_or _ theorem insert_eq (a : α) (s : finset α) : insert a s = {a} ∪ s := rfl @[simp] theorem insert_union (a : α) (s t : finset α) : insert a s ∪ t = insert a (s ∪ t) := by simp only [insert_eq, union_assoc] @[simp] theorem union_insert (a : α) (s t : finset α) : s ∪ insert a t = insert a (s ∪ t) := by simp only [insert_eq, union_left_comm] theorem insert_union_distrib (a : α) (s t : finset α) : insert a (s ∪ t) = insert a s ∪ insert a t := by simp only [insert_union, union_insert, insert_idem] @[simp] lemma union_eq_left_iff_subset {s t : finset α} : s ∪ t = s ↔ t ⊆ s := begin split, { assume h, have : t ⊆ s ∪ t := subset_union_right _ _, rwa h at this }, { assume h, exact subset.antisymm (union_subset (subset.refl _) h) (subset_union_left _ _) } end @[simp] lemma left_eq_union_iff_subset {s t : finset α} : s = s ∪ t ↔ t ⊆ s := by rw [← union_eq_left_iff_subset, eq_comm] @[simp] lemma union_eq_right_iff_subset {s t : finset α} : t ∪ s = s ↔ t ⊆ s := by rw [union_comm, union_eq_left_iff_subset] @[simp] lemma right_eq_union_iff_subset {s t : finset α} : s = t ∪ s ↔ t ⊆ s := by rw [← union_eq_right_iff_subset, eq_comm] /-! ### inter -/ /-- `s ∩ t` is the set such that `a ∈ s ∩ t` iff `a ∈ s` and `a ∈ t`. -/ instance : has_inter (finset α) := ⟨λ s₁ s₂, ⟨_, nodup_ndinter s₂.1 s₁.2⟩⟩ theorem inter_val_nd (s₁ s₂ : finset α) : (s₁ ∩ s₂).1 = ndinter s₁.1 s₂.1 := rfl @[simp] theorem inter_val (s₁ s₂ : finset α) : (s₁ ∩ s₂).1 = s₁.1 ∩ s₂.1 := ndinter_eq_inter s₁.2 @[simp] theorem mem_inter {a : α} {s₁ s₂ : finset α} : a ∈ s₁ ∩ s₂ ↔ a ∈ s₁ ∧ a ∈ s₂ := mem_ndinter theorem mem_of_mem_inter_left {a : α} {s₁ s₂ : finset α} (h : a ∈ s₁ ∩ s₂) : a ∈ s₁ := (mem_inter.1 h).1 theorem mem_of_mem_inter_right {a : α} {s₁ s₂ : finset α} (h : a ∈ s₁ ∩ s₂) : a ∈ s₂ := (mem_inter.1 h).2 theorem mem_inter_of_mem {a : α} {s₁ s₂ : finset α} : a ∈ s₁ → a ∈ s₂ → a ∈ s₁ ∩ s₂ := and_imp.1 mem_inter.2 theorem inter_subset_left (s₁ s₂ : finset α) : s₁ ∩ s₂ ⊆ s₁ := λ a, mem_of_mem_inter_left theorem inter_subset_right (s₁ s₂ : finset α) : s₁ ∩ s₂ ⊆ s₂ := λ a, mem_of_mem_inter_right theorem subset_inter {s₁ s₂ s₃ : finset α} : s₁ ⊆ s₂ → s₁ ⊆ s₃ → s₁ ⊆ s₂ ∩ s₃ := by simp only [subset_iff, mem_inter] {contextual:=tt}; intros; split; trivial @[simp] lemma coe_inter (s₁ s₂ : finset α) : ↑(s₁ ∩ s₂) = (↑s₁ ∩ ↑s₂ : set α) := set.ext $ λ _, mem_inter @[simp] theorem union_inter_cancel_left {s t : finset α} : (s ∪ t) ∩ s = s := by rw [← coe_inj, coe_inter, coe_union, set.union_inter_cancel_left] @[simp] theorem union_inter_cancel_right {s t : finset α} : (s ∪ t) ∩ t = t := by rw [← coe_inj, coe_inter, coe_union, set.union_inter_cancel_right] theorem inter_comm (s₁ s₂ : finset α) : s₁ ∩ s₂ = s₂ ∩ s₁ := ext.2 $ λ _, by simp only [mem_inter, and_comm] @[simp] theorem inter_assoc (s₁ s₂ s₃ : finset α) : (s₁ ∩ s₂) ∩ s₃ = s₁ ∩ (s₂ ∩ s₃) := ext.2 $ λ _, by simp only [mem_inter, and_assoc] theorem inter_left_comm (s₁ s₂ s₃ : finset α) : s₁ ∩ (s₂ ∩ s₃) = s₂ ∩ (s₁ ∩ s₃) := ext.2 $ λ _, by simp only [mem_inter, and.left_comm] theorem inter_right_comm (s₁ s₂ s₃ : finset α) : (s₁ ∩ s₂) ∩ s₃ = (s₁ ∩ s₃) ∩ s₂ := ext.2 $ λ _, by simp only [mem_inter, and.right_comm] @[simp] theorem inter_self (s : finset α) : s ∩ s = s := ext.2 $ λ _, mem_inter.trans $ and_self _ @[simp] theorem inter_empty (s : finset α) : s ∩ ∅ = ∅ := ext.2 $ λ _, mem_inter.trans $ and_false _ @[simp] theorem empty_inter (s : finset α) : ∅ ∩ s = ∅ := ext.2 $ λ _, mem_inter.trans $ false_and _ @[simp] lemma inter_union_self (s t : finset α) : s ∩ (t ∪ s) = s := by rw [inter_comm, union_inter_cancel_right] @[simp] theorem insert_inter_of_mem {s₁ s₂ : finset α} {a : α} (h : a ∈ s₂) : insert a s₁ ∩ s₂ = insert a (s₁ ∩ s₂) := ext.2 $ λ x, have x = a ∨ x ∈ s₂ ↔ x ∈ s₂, from or_iff_right_of_imp $ by rintro rfl; exact h, by simp only [mem_inter, mem_insert, or_and_distrib_left, this] @[simp] theorem inter_insert_of_mem {s₁ s₂ : finset α} {a : α} (h : a ∈ s₁) : s₁ ∩ insert a s₂ = insert a (s₁ ∩ s₂) := by rw [inter_comm, insert_inter_of_mem h, inter_comm] @[simp] theorem insert_inter_of_not_mem {s₁ s₂ : finset α} {a : α} (h : a ∉ s₂) : insert a s₁ ∩ s₂ = s₁ ∩ s₂ := ext.2 $ λ x, have ¬ (x = a ∧ x ∈ s₂), by rintro ⟨rfl, H⟩; exact h H, by simp only [mem_inter, mem_insert, or_and_distrib_right, this, false_or] @[simp] theorem inter_insert_of_not_mem {s₁ s₂ : finset α} {a : α} (h : a ∉ s₁) : s₁ ∩ insert a s₂ = s₁ ∩ s₂ := by rw [inter_comm, insert_inter_of_not_mem h, inter_comm] @[simp] theorem singleton_inter_of_mem {a : α} {s : finset α} (H : a ∈ s) : {a} ∩ s = {a} := show insert a ∅ ∩ s = insert a ∅, by rw [insert_inter_of_mem H, empty_inter] @[simp] theorem singleton_inter_of_not_mem {a : α} {s : finset α} (H : a ∉ s) : {a} ∩ s = ∅ := eq_empty_of_forall_not_mem $ by simp only [mem_inter, mem_singleton]; rintro x ⟨rfl, h⟩; exact H h @[simp] theorem inter_singleton_of_mem {a : α} {s : finset α} (h : a ∈ s) : s ∩ {a} = {a} := by rw [inter_comm, singleton_inter_of_mem h] @[simp] theorem inter_singleton_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : s ∩ {a} = ∅ := by rw [inter_comm, singleton_inter_of_not_mem h] @[mono] lemma inter_subset_inter {x y s t : finset α} (h : x ⊆ y) (h' : s ⊆ t) : x ∩ s ⊆ y ∩ t := begin intros a a_in, rw finset.mem_inter at a_in ⊢, exact ⟨h a_in.1, h' a_in.2⟩ end lemma inter_subset_inter_right {x y s : finset α} (h : x ⊆ y) : x ∩ s ⊆ y ∩ s := finset.inter_subset_inter h (finset.subset.refl _) lemma inter_subset_inter_left {x y s : finset α} (h : x ⊆ y) : s ∩ x ⊆ s ∩ y := finset.inter_subset_inter (finset.subset.refl _) h /-! ### lattice laws -/ instance : lattice (finset α) := { sup := (∪), sup_le := assume a b c, union_subset, le_sup_left := subset_union_left, le_sup_right := subset_union_right, inf := (∩), le_inf := assume a b c, subset_inter, inf_le_left := inter_subset_left, inf_le_right := inter_subset_right, ..finset.partial_order } @[simp] theorem sup_eq_union (s t : finset α) : s ⊔ t = s ∪ t := rfl @[simp] theorem inf_eq_inter (s t : finset α) : s ⊓ t = s ∩ t := rfl instance : semilattice_inf_bot (finset α) := { bot := ∅, bot_le := empty_subset, ..finset.lattice } instance {α : Type*} [decidable_eq α] : semilattice_sup_bot (finset α) := { ..finset.semilattice_inf_bot, ..finset.lattice } instance : distrib_lattice (finset α) := { le_sup_inf := assume a b c, show (a ∪ b) ∩ (a ∪ c) ⊆ a ∪ b ∩ c, by simp only [subset_iff, mem_inter, mem_union, and_imp, or_imp_distrib] {contextual:=tt}; simp only [true_or, imp_true_iff, true_and, or_true], ..finset.lattice } theorem inter_distrib_left (s t u : finset α) : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) := inf_sup_left theorem inter_distrib_right (s t u : finset α) : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) := inf_sup_right theorem union_distrib_left (s t u : finset α) : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) := sup_inf_left theorem union_distrib_right (s t u : finset α) : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) := sup_inf_right lemma union_eq_empty_iff (A B : finset α) : A ∪ B = ∅ ↔ A = ∅ ∧ B = ∅ := sup_eq_bot_iff /-! ### erase -/ /-- `erase s a` is the set `s - {a}`, that is, the elements of `s` which are not equal to `a`. -/ def erase (s : finset α) (a : α) : finset α := ⟨_, nodup_erase_of_nodup a s.2⟩ @[simp] theorem erase_val (s : finset α) (a : α) : (erase s a).1 = s.1.erase a := rfl @[simp] theorem mem_erase {a b : α} {s : finset α} : a ∈ erase s b ↔ a ≠ b ∧ a ∈ s := mem_erase_iff_of_nodup s.2 theorem not_mem_erase (a : α) (s : finset α) : a ∉ erase s a := mem_erase_of_nodup s.2 @[simp] theorem erase_empty (a : α) : erase ∅ a = ∅ := rfl theorem ne_of_mem_erase {a b : α} {s : finset α} : b ∈ erase s a → b ≠ a := by simp only [mem_erase]; exact and.left theorem mem_of_mem_erase {a b : α} {s : finset α} : b ∈ erase s a → b ∈ s := mem_of_mem_erase theorem mem_erase_of_ne_of_mem {a b : α} {s : finset α} : a ≠ b → a ∈ s → a ∈ erase s b := by simp only [mem_erase]; exact and.intro theorem erase_insert {a : α} {s : finset α} (h : a ∉ s) : erase (insert a s) a = s := ext.2 $ assume x, by simp only [mem_erase, mem_insert, and_or_distrib_left, not_and_self, false_or]; apply and_iff_right_of_imp; rintro H rfl; exact h H theorem insert_erase {a : α} {s : finset α} (h : a ∈ s) : insert a (erase s a) = s := ext.2 $ assume x, by simp only [mem_insert, mem_erase, or_and_distrib_left, dec_em, true_and]; apply or_iff_right_of_imp; rintro rfl; exact h theorem erase_subset_erase (a : α) {s t : finset α} (h : s ⊆ t) : erase s a ⊆ erase t a := val_le_iff.1 $ erase_le_erase _ $ val_le_iff.2 h theorem erase_subset (a : α) (s : finset α) : erase s a ⊆ s := erase_subset _ _ @[simp] lemma coe_erase (a : α) (s : finset α) : ↑(erase s a) = (↑s \ {a} : set α) := set.ext $ λ _, mem_erase.trans $ by rw [and_comm, set.mem_diff, set.mem_singleton_iff]; refl lemma erase_ssubset {a : α} {s : finset α} (h : a ∈ s) : s.erase a ⊂ s := calc s.erase a ⊂ insert a (s.erase a) : ssubset_insert $ not_mem_erase _ _ ... = _ : insert_erase h theorem erase_eq_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : erase s a = s := eq_of_veq $ erase_of_not_mem h theorem subset_insert_iff {a : α} {s t : finset α} : s ⊆ insert a t ↔ erase s a ⊆ t := by simp only [subset_iff, or_iff_not_imp_left, mem_erase, mem_insert, and_imp]; exact forall_congr (λ x, forall_swap) theorem erase_insert_subset (a : α) (s : finset α) : erase (insert a s) a ⊆ s := subset_insert_iff.1 $ subset.refl _ theorem insert_erase_subset (a : α) (s : finset α) : s ⊆ insert a (erase s a) := subset_insert_iff.2 $ subset.refl _ /-! ### sdiff -/ /-- `s \ t` is the set consisting of the elements of `s` that are not in `t`. -/ instance : has_sdiff (finset α) := ⟨λs₁ s₂, ⟨s₁.1 - s₂.1, nodup_of_le (sub_le_self _ _) s₁.2⟩⟩ @[simp] theorem mem_sdiff {a : α} {s₁ s₂ : finset α} : a ∈ s₁ \ s₂ ↔ a ∈ s₁ ∧ a ∉ s₂ := mem_sub_of_nodup s₁.2 lemma not_mem_sdiff_of_mem_right {a : α} {s t : finset α} (h : a ∈ t) : a ∉ s \ t := by simp only [mem_sdiff, h, not_true, not_false_iff, and_false] theorem sdiff_union_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : (s₂ \ s₁) ∪ s₁ = s₂ := ext.2 $ λ a, by simpa only [mem_sdiff, mem_union, or_comm, or_and_distrib_left, dec_em, and_true] using or_iff_right_of_imp (@h a) theorem union_sdiff_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁ ∪ (s₂ \ s₁) = s₂ := (union_comm _ _).trans (sdiff_union_of_subset h) theorem inter_sdiff (s t u : finset α) : s ∩ (t \ u) = s ∩ t \ u := by { ext x, simp [and_assoc] } @[simp] theorem inter_sdiff_self (s₁ s₂ : finset α) : s₁ ∩ (s₂ \ s₁) = ∅ := eq_empty_of_forall_not_mem $ by simp only [mem_inter, mem_sdiff]; rintro x ⟨h, _, hn⟩; exact hn h @[simp] theorem sdiff_inter_self (s₁ s₂ : finset α) : (s₂ \ s₁) ∩ s₁ = ∅ := (inter_comm _ _).trans (inter_sdiff_self _ _) @[simp] theorem sdiff_self (s₁ : finset α) : s₁ \ s₁ = ∅ := by ext; simp theorem sdiff_inter_distrib_right (s₁ s₂ s₃ : finset α) : s₁ \ (s₂ ∩ s₃) = (s₁ \ s₂) ∪ (s₁ \ s₃) := by ext; simp only [and_or_distrib_left, mem_union, classical.not_and_distrib, mem_sdiff, mem_inter] @[simp] theorem sdiff_inter_self_left (s₁ s₂ : finset α) : s₁ \ (s₁ ∩ s₂) = s₁ \ s₂ := by simp only [sdiff_inter_distrib_right, sdiff_self, empty_union] @[simp] theorem sdiff_inter_self_right (s₁ s₂ : finset α) : s₁ \ (s₂ ∩ s₁) = s₁ \ s₂ := by simp only [sdiff_inter_distrib_right, sdiff_self, union_empty] @[simp] theorem sdiff_empty {s₁ : finset α} : s₁ \ ∅ = s₁ := ext.2 (by simp) @[mono] theorem sdiff_subset_sdiff {s₁ s₂ t₁ t₂ : finset α} (h₁ : t₁ ⊆ t₂) (h₂ : s₂ ⊆ s₁) : t₁ \ s₁ ⊆ t₂ \ s₂ := by simpa only [subset_iff, mem_sdiff, and_imp] using λ a m₁ m₂, and.intro (h₁ m₁) (mt (@h₂ _) m₂) theorem sdiff_subset_self {s₁ s₂ : finset α} : s₁ \ s₂ ⊆ s₁ := suffices s₁ \ s₂ ⊆ s₁ \ ∅, by simpa [sdiff_empty] using this, sdiff_subset_sdiff (subset.refl _) (empty_subset _) @[simp] lemma coe_sdiff (s₁ s₂ : finset α) : ↑(s₁ \ s₂) = (↑s₁ \ ↑s₂ : set α) := set.ext $ λ _, mem_sdiff @[simp] theorem union_sdiff_self_eq_union {s t : finset α} : s ∪ (t \ s) = s ∪ t := ext.2 $ λ a, by simp only [mem_union, mem_sdiff, or_iff_not_imp_left, imp_and_distrib, and_iff_left id] @[simp] theorem sdiff_union_self_eq_union {s t : finset α} : (s \ t) ∪ t = s ∪ t := by rw [union_comm, union_sdiff_self_eq_union, union_comm] lemma union_sdiff_symm {s t : finset α} : s ∪ (t \ s) = t ∪ (s \ t) := by rw [union_sdiff_self_eq_union, union_sdiff_self_eq_union, union_comm] lemma sdiff_union_inter (s t : finset α) : (s \ t) ∪ (s ∩ t) = s := by { simp only [ext, mem_union, mem_sdiff, mem_inter], tauto } @[simp] lemma sdiff_idem (s t : finset α) : s \ t \ t = s \ t := by { simp only [ext, mem_sdiff], tauto } lemma sdiff_eq_empty_iff_subset {s t : finset α} : s \ t = ∅ ↔ s ⊆ t := by { rw [subset_iff, ext], simp } @[simp] lemma empty_sdiff (s : finset α) : ∅ \ s = ∅ := by { rw sdiff_eq_empty_iff_subset, exact empty_subset _ } lemma insert_sdiff_of_not_mem (s : finset α) {t : finset α} {x : α} (h : x ∉ t) : (insert x s) \ t = insert x (s \ t) := begin rw [← coe_inj, coe_insert, coe_sdiff, coe_sdiff, coe_insert], exact set.insert_diff_of_not_mem ↑s h end lemma insert_sdiff_of_mem (s : finset α) {t : finset α} {x : α} (h : x ∈ t) : (insert x s) \ t = s \ t := begin rw [← coe_inj, coe_sdiff, coe_sdiff, coe_insert], exact set.insert_diff_of_mem ↑s h end @[simp] lemma sdiff_subset (s t : finset α) : s \ t ⊆ s := by simp [subset_iff, mem_sdiff] {contextual := tt} lemma union_sdiff_distrib (s₁ s₂ t : finset α) : (s₁ ∪ s₂) \ t = s₁ \ t ∪ s₂ \ t := by { simp only [ext, mem_sdiff, mem_union], tauto } lemma sdiff_union_distrib (s t₁ t₂ : finset α) : s \ (t₁ ∪ t₂) = (s \ t₁) ∩ (s \ t₂) := by { simp only [ext, mem_union, mem_sdiff, mem_inter], tauto } lemma union_sdiff_self (s t : finset α) : (s ∪ t) \ t = s \ t := by rw [union_sdiff_distrib, sdiff_self, union_empty] lemma sdiff_singleton_eq_erase (a : α) (s : finset α) : s \ singleton a = erase s a := by { ext, rw [mem_erase, mem_sdiff, mem_singleton], tauto } lemma sdiff_sdiff_self_left (s t : finset α) : s \ (s \ t) = s ∩ t := by { simp only [ext, mem_sdiff, mem_inter], tauto } lemma inter_eq_inter_of_sdiff_eq_sdiff {s t₁ t₂ : finset α} : s \ t₁ = s \ t₂ → s ∩ t₁ = s ∩ t₂ := by { simp only [ext, mem_sdiff, mem_inter], intros b c, replace b := b c, split; tauto } end decidable_eq /-! ### attach -/ /-- `attach s` takes the elements of `s` and forms a new set of elements of the subtype `{x // x ∈ s}`. -/ def attach (s : finset α) : finset {x // x ∈ s} := ⟨attach s.1, nodup_attach.2 s.2⟩ @[simp] theorem attach_val (s : finset α) : s.attach.1 = s.1.attach := rfl @[simp] theorem mem_attach (s : finset α) : ∀ x, x ∈ s.attach := mem_attach _ @[simp] theorem attach_empty : attach (∅ : finset α) = ∅ := rfl /-! ### piecewise -/ section piecewise /-- `s.piecewise f g` is the function equal to `f` on the finset `s`, and to `g` on its complement. -/ def piecewise {α : Type*} {δ : α → Sort*} (s : finset α) (f g : Πi, δ i) [∀j, decidable (j ∈ s)] : Πi, δ i := λi, if i ∈ s then f i else g i variables {δ : α → Sort*} (s : finset α) (f g : Πi, δ i) @[simp] lemma piecewise_insert_self [decidable_eq α] {j : α} [∀i, decidable (i ∈ insert j s)] : (insert j s).piecewise f g j = f j := by simp [piecewise] @[simp] lemma piecewise_empty [∀i : α, decidable (i ∈ (∅ : finset α))] : piecewise ∅ f g = g := by { ext i, simp [piecewise] } variable [∀j, decidable (j ∈ s)] @[norm_cast] lemma piecewise_coe [∀j, decidable (j ∈ (↑s : set α))] : (↑s : set α).piecewise f g = s.piecewise f g := by { ext, congr } @[simp, priority 980] lemma piecewise_eq_of_mem {i : α} (hi : i ∈ s) : s.piecewise f g i = f i := by simp [piecewise, hi] @[simp, priority 980] lemma piecewise_eq_of_not_mem {i : α} (hi : i ∉ s) : s.piecewise f g i = g i := by simp [piecewise, hi] @[simp, priority 990] lemma piecewise_insert_of_ne [decidable_eq α] {i j : α} [∀i, decidable (i ∈ insert j s)] (h : i ≠ j) : (insert j s).piecewise f g i = s.piecewise f g i := by simp [piecewise, h] lemma piecewise_insert [decidable_eq α] (j : α) [∀i, decidable (i ∈ insert j s)] : (insert j s).piecewise f g = function.update (s.piecewise f g) j (f j) := begin classical, rw [← piecewise_coe, ← piecewise_coe, ← set.piecewise_insert, ← coe_insert j s], congr end lemma update_eq_piecewise {β : Type*} [decidable_eq α] (f : α → β) (i : α) (v : β) : function.update f i v = piecewise (singleton i) (λj, v) f := begin ext j, by_cases h : j = i, { rw [h], simp }, { simp [h] } end end piecewise section decidable_pi_exists variables {s : finset α} instance decidable_dforall_finset {p : Πa∈s, Prop} [hp : ∀a (h : a ∈ s), decidable (p a h)] : decidable (∀a (h : a ∈ s), p a h) := multiset.decidable_dforall_multiset /-- decidable equality for functions whose domain is bounded by finsets -/ instance decidable_eq_pi_finset {β : α → Type*} [h : ∀a, decidable_eq (β a)] : decidable_eq (Πa∈s, β a) := multiset.decidable_eq_pi_multiset instance decidable_dexists_finset {p : Πa∈s, Prop} [hp : ∀a (h : a ∈ s), decidable (p a h)] : decidable (∃a (h : a ∈ s), p a h) := multiset.decidable_dexists_multiset end decidable_pi_exists /-! ### filter -/ section filter variables {p q : α → Prop} [decidable_pred p] [decidable_pred q] /-- `filter p s` is the set of elements of `s` that satisfy `p`. -/ def filter (p : α → Prop) [decidable_pred p] (s : finset α) : finset α := ⟨_, nodup_filter p s.2⟩ @[simp] theorem filter_val (s : finset α) : (filter p s).1 = s.1.filter p := rfl @[simp] theorem mem_filter {s : finset α} {a : α} : a ∈ s.filter p ↔ a ∈ s ∧ p a := mem_filter @[simp] theorem filter_subset (s : finset α) : s.filter p ⊆ s := filter_subset _ theorem filter_ssubset {s : finset α} : s.filter p ⊂ s ↔ ∃ x ∈ s, ¬ p x := ⟨λ h, let ⟨x, hs, hp⟩ := set.exists_of_ssubset h in ⟨x, hs, mt (λ hp, mem_filter.2 ⟨hs, hp⟩) hp⟩, λ ⟨x, hs, hp⟩, ⟨s.filter_subset, λ h, hp (mem_filter.1 (h hs)).2⟩⟩ theorem filter_filter (s : finset α) : (s.filter p).filter q = s.filter (λa, p a ∧ q a) := ext.2 $ assume a, by simp only [mem_filter, and_comm, and.left_comm] @[simp] lemma filter_true {s : finset α} [h : decidable_pred (λ _, true)] : @finset.filter α (λ _, true) h s = s := by ext; simp @[simp] theorem filter_false {h} (s : finset α) : @filter α (λa, false) h s = ∅ := ext.2 $ assume a, by simp only [mem_filter, and_false]; refl lemma filter_congr {s : finset α} (H : ∀ x ∈ s, p x ↔ q x) : filter p s = filter q s := eq_of_veq $ filter_congr H lemma filter_empty : filter p ∅ = ∅ := subset_empty.1 $ filter_subset _ lemma filter_subset_filter {s t : finset α} (h : s ⊆ t) : s.filter p ⊆ t.filter p := assume a ha, mem_filter.2 ⟨h (mem_filter.1 ha).1, (mem_filter.1 ha).2⟩ @[simp] lemma coe_filter (s : finset α) : ↑(s.filter p) = ({x ∈ ↑s | p x} : set α) := set.ext $ λ _, mem_filter theorem filter_singleton (a : α) : filter p (singleton a) = if p a then singleton a else ∅ := by { classical, ext x, simp, split_ifs with h; by_cases h' : x = a; simp [h, h'] } variable [decidable_eq α] theorem filter_union (s₁ s₂ : finset α) : (s₁ ∪ s₂).filter p = s₁.filter p ∪ s₂.filter p := ext.2 $ λ _, by simp only [mem_filter, mem_union, or_and_distrib_right] theorem filter_union_right (p q : α → Prop) [decidable_pred p] [decidable_pred q] (s : finset α) : s.filter p ∪ s.filter q = s.filter (λx, p x ∨ q x) := ext.2 $ λ x, by simp only [mem_filter, mem_union, and_or_distrib_left.symm] lemma filter_mem_eq_inter {s t : finset α} : s.filter (λ i, i ∈ t) = s ∩ t := ext' $ λ i, by rw [mem_filter, mem_inter] theorem filter_inter {s t : finset α} : filter p s ∩ t = filter p (s ∩ t) := by { ext, simp only [mem_inter, mem_filter, and.right_comm] } theorem inter_filter {s t : finset α} : s ∩ filter p t = filter p (s ∩ t) := by rw [inter_comm, filter_inter, inter_comm] theorem filter_insert (a : α) (s : finset α) : filter p (insert a s) = if p a then insert a (filter p s) else (filter p s) := by { ext x, simp, split_ifs with h; by_cases h' : x = a; simp [h, h'] } theorem filter_or [decidable_pred (λ a, p a ∨ q a)] (s : finset α) : s.filter (λ a, p a ∨ q a) = s.filter p ∪ s.filter q := ext.2 $ λ _, by simp only [mem_filter, mem_union, and_or_distrib_left] theorem filter_and [decidable_pred (λ a, p a ∧ q a)] (s : finset α) : s.filter (λ a, p a ∧ q a) = s.filter p ∩ s.filter q := ext.2 $ λ _, by simp only [mem_filter, mem_inter, and_comm, and.left_comm, and_self] theorem filter_not [decidable_pred (λ a, ¬ p a)] (s : finset α) : s.filter (λ a, ¬ p a) = s \ s.filter p := ext.2 $ by simpa only [mem_filter, mem_sdiff, and_comm, not_and] using λ a, and_congr_right $ λ h : a ∈ s, (imp_iff_right h).symm.trans imp_not_comm theorem sdiff_eq_filter (s₁ s₂ : finset α) : s₁ \ s₂ = filter (∉ s₂) s₁ := ext.2 $ λ _, by simp only [mem_sdiff, mem_filter] theorem sdiff_eq_self (s₁ s₂ : finset α) : s₁ \ s₂ = s₁ ↔ s₁ ∩ s₂ ⊆ ∅ := by { simp [subset.antisymm_iff,sdiff_subset_self], split; intro h, { transitivity' ((s₁ \ s₂) ∩ s₂), mono, simp }, { calc s₁ \ s₂ ⊇ s₁ \ (s₁ ∩ s₂) : by simp [(⊇)] ... ⊇ s₁ \ ∅ : by mono using [(⊇)] ... ⊇ s₁ : by simp [(⊇)] } } theorem filter_union_filter_neg_eq [decidable_pred (λ a, ¬ p a)] (s : finset α) : s.filter p ∪ s.filter (λa, ¬ p a) = s := by simp only [filter_not, union_sdiff_of_subset (filter_subset s)] theorem filter_inter_filter_neg_eq (s : finset α) : s.filter p ∩ s.filter (λa, ¬ p a) = ∅ := by simp only [filter_not, inter_sdiff_self] lemma subset_union_elim {s : finset α} {t₁ t₂ : set α} (h : ↑s ⊆ t₁ ∪ t₂) : ∃s₁ s₂ : finset α, s₁ ∪ s₂ = s ∧ ↑s₁ ⊆ t₁ ∧ ↑s₂ ⊆ t₂ \ t₁ := begin classical, refine ⟨s.filter (∈ t₁), s.filter (∉ t₁), _, _ , _⟩, { simp [filter_union_right, classical.or_not] }, { intro x, simp }, { intro x, simp, intros hx hx₂, refine ⟨or.resolve_left (h hx) hx₂, hx₂⟩ } end /- We can simplify an application of filter where the decidability is inferred in "the wrong way" -/ @[simp] lemma filter_congr_decidable {α} (s : finset α) (p : α → Prop) (h : decidable_pred p) [decidable_pred p] : @filter α p h s = s.filter p := by congr section classical open_locale classical /-- The following instance allows us to write `{ x ∈ s | p x }` for `finset.filter s p`. Since the former notation requires us to define this for all propositions `p`, and `finset.filter` only works for decidable propositions, the notation `{ x ∈ s | p x }` is only compatible with classical logic because it uses `classical.prop_decidable`. We don't want to redo all lemmas of `finset.filter` for `has_sep.sep`, so we make sure that `simp` unfolds the notation `{ x ∈ s | p x }` to `finset.filter s p`. If `p` happens to be decidable, the simp-lemma `filter_congr_decidable` will make sure that `finset.filter` uses the right instance for decidability. -/ noncomputable instance {α : Type*} : has_sep α (finset α) := ⟨λ p x, x.filter p⟩ @[simp] lemma sep_def {α : Type*} (s : finset α) (p : α → Prop) : {x ∈ s | p x} = s.filter p := rfl end classical /-- After filtering out everything that does not equal a given value, at most that value remains. This is equivalent to `filter_eq'` with the equality the other way. -/ -- This is not a good simp lemma, as it would prevent `finset.mem_filter` from firing -- on, e.g. `x ∈ s.filter(eq b)`. lemma filter_eq [decidable_eq β] (s : finset β) (b : β) : s.filter(eq b) = ite (b ∈ s) {b} ∅ := begin split_ifs, { ext, simp only [mem_filter, mem_singleton], exact ⟨λ h, h.2.symm, by { rintro ⟨h⟩, exact ⟨h, rfl⟩, }⟩ }, { ext, simp only [mem_filter, not_and, iff_false, not_mem_empty], rintros m ⟨e⟩, exact h m, } end /-- After filtering out everything that does not equal a given value, at most that value remains. This is equivalent to `filter_eq` with the equality the other way. -/ lemma filter_eq' [decidable_eq β] (s : finset β) (b : β) : s.filter (λ a, a = b) = ite (b ∈ s) {b} ∅ := trans (filter_congr (λ _ _, ⟨eq.symm, eq.symm⟩)) (filter_eq s b) end filter /-! ### range -/ section range variables {n m l : ℕ} /-- `range n` is the set of natural numbers less than `n`. -/ def range (n : ℕ) : finset ℕ := ⟨_, nodup_range n⟩ @[simp] theorem range_val (n : ℕ) : (range n).1 = multiset.range n := rfl @[simp] theorem mem_range : m ∈ range n ↔ m < n := mem_range @[simp] theorem range_zero : range 0 = ∅ := rfl @[simp] theorem range_one : range 1 = {0} := rfl theorem range_succ : range (succ n) = insert n (range n) := eq_of_veq $ (range_succ n).trans $ (ndinsert_of_not_mem not_mem_range_self).symm theorem range_add_one : range (n + 1) = insert n (range n) := range_succ @[simp] theorem not_mem_range_self : n ∉ range n := not_mem_range_self @[simp] theorem self_mem_range_succ (n : ℕ) : n ∈ range (n + 1) := multiset.self_mem_range_succ n @[simp] theorem range_subset {n m} : range n ⊆ range m ↔ n ≤ m := range_subset theorem range_mono : monotone range := λ _ _, range_subset.2 end range /- useful rules for calculations with quantifiers -/ theorem exists_mem_empty_iff (p : α → Prop) : (∃ x, x ∈ (∅ : finset α) ∧ p x) ↔ false := by simp only [not_mem_empty, false_and, exists_false] theorem exists_mem_insert [d : decidable_eq α] (a : α) (s : finset α) (p : α → Prop) : (∃ x, x ∈ insert a s ∧ p x) ↔ p a ∨ (∃ x, x ∈ s ∧ p x) := by simp only [mem_insert, or_and_distrib_right, exists_or_distrib, exists_eq_left] theorem forall_mem_empty_iff (p : α → Prop) : (∀ x, x ∈ (∅ : finset α) → p x) ↔ true := iff_true_intro $ λ _, false.elim theorem forall_mem_insert [d : decidable_eq α] (a : α) (s : finset α) (p : α → Prop) : (∀ x, x ∈ insert a s → p x) ↔ p a ∧ (∀ x, x ∈ s → p x) := by simp only [mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] end finset /-- Equivalence between the set of natural numbers which are `≥ k` and `ℕ`, given by `n → n - k`. -/ def not_mem_range_equiv (k : ℕ) : {n // n ∉ range k} ≃ ℕ := { to_fun := λ i, i.1 - k, inv_fun := λ j, ⟨j + k, by simp⟩, left_inv := begin assume j, rw subtype.ext, apply nat.sub_add_cancel, simpa using j.2 end, right_inv := λ j, nat.add_sub_cancel _ _ } @[simp] lemma coe_not_mem_range_equiv (k : ℕ) : (not_mem_range_equiv k : {n // n ∉ range k} → ℕ) = (λ i, i - k) := rfl @[simp] lemma coe_not_mem_range_equiv_symm (k : ℕ) : ((not_mem_range_equiv k).symm : ℕ → {n // n ∉ range k}) = λ j, ⟨j + k, by simp⟩ := rfl namespace option /-- Construct an empty or singleton finset from an `option` -/ def to_finset (o : option α) : finset α := match o with | none := ∅ | some a := {a} end @[simp] theorem to_finset_none : none.to_finset = (∅ : finset α) := rfl @[simp] theorem to_finset_some {a : α} : (some a).to_finset = {a} := rfl @[simp] theorem mem_to_finset {a : α} {o : option α} : a ∈ o.to_finset ↔ a ∈ o := by cases o; simp only [to_finset, finset.mem_singleton, option.mem_def, eq_comm]; refl end option /-! ### erase_dup on list and multiset -/ namespace multiset variable [decidable_eq α] /-- `to_finset s` removes duplicates from the multiset `s` to produce a finset. -/ def to_finset (s : multiset α) : finset α := ⟨_, nodup_erase_dup s⟩ @[simp] theorem to_finset_val (s : multiset α) : s.to_finset.1 = s.erase_dup := rfl theorem to_finset_eq {s : multiset α} (n : nodup s) : finset.mk s n = s.to_finset := finset.val_inj.1 (erase_dup_eq_self.2 n).symm @[simp] theorem mem_to_finset {a : α} {s : multiset α} : a ∈ s.to_finset ↔ a ∈ s := mem_erase_dup @[simp] lemma to_finset_zero : to_finset (0 : multiset α) = ∅ := rfl @[simp] lemma to_finset_cons (a : α) (s : multiset α) : to_finset (a :: s) = insert a (to_finset s) := finset.eq_of_veq erase_dup_cons @[simp] lemma to_finset_add (s t : multiset α) : to_finset (s + t) = to_finset s ∪ to_finset t := finset.ext' $ by simp @[simp] lemma to_finset_nsmul (s : multiset α) : ∀(n : ℕ) (hn : n ≠ 0), (n •ℕ s).to_finset = s.to_finset | 0 h := by contradiction | (n+1) h := begin by_cases n = 0, { rw [h, zero_add, one_nsmul] }, { rw [add_nsmul, to_finset_add, one_nsmul, to_finset_nsmul n h, finset.union_idempotent] } end @[simp] lemma to_finset_inter (s t : multiset α) : to_finset (s ∩ t) = to_finset s ∩ to_finset t := finset.ext' $ by simp theorem to_finset_eq_empty {m : multiset α} : m.to_finset = ∅ ↔ m = 0 := finset.val_inj.symm.trans multiset.erase_dup_eq_zero end multiset namespace list variable [decidable_eq α] /-- `to_finset l` removes duplicates from the list `l` to produce a finset. -/ def to_finset (l : list α) : finset α := multiset.to_finset l @[simp] theorem to_finset_val (l : list α) : l.to_finset.1 = (l.erase_dup : multiset α) := rfl theorem to_finset_eq {l : list α} (n : nodup l) : @finset.mk α l n = l.to_finset := multiset.to_finset_eq n @[simp] theorem mem_to_finset {a : α} {l : list α} : a ∈ l.to_finset ↔ a ∈ l := mem_erase_dup @[simp] theorem to_finset_nil : to_finset (@nil α) = ∅ := rfl @[simp] theorem to_finset_cons {a : α} {l : list α} : to_finset (a :: l) = insert a (to_finset l) := finset.eq_of_veq $ by by_cases h : a ∈ l; simp [finset.insert_val', multiset.erase_dup_cons, h] end list namespace finset /-! ### map -/ section map open function /-- When `f` is an embedding of `α` in `β` and `s` is a finset in `α`, then `s.map f` is the image finset in `β`. The embedding condition guarantees that there are no duplicates in the image. -/ def map (f : α ↪ β) (s : finset α) : finset β := ⟨s.1.map f, nodup_map f.2 s.2⟩ @[simp] theorem map_val (f : α ↪ β) (s : finset α) : (map f s).1 = s.1.map f := rfl @[simp] theorem map_empty (f : α ↪ β) : (∅ : finset α).map f = ∅ := rfl variables {f : α ↪ β} {s : finset α} @[simp] theorem mem_map {b : β} : b ∈ s.map f ↔ ∃ a ∈ s, f a = b := mem_map.trans $ by simp only [exists_prop]; refl theorem mem_map' (f : α ↪ β) {a} {s : finset α} : f a ∈ s.map f ↔ a ∈ s := mem_map_of_inj f.2 theorem mem_map_of_mem (f : α ↪ β) {a} {s : finset α} : a ∈ s → f a ∈ s.map f := (mem_map' _).2 @[simp] theorem coe_map (f : α ↪ β) (s : finset α) : (↑(s.map f) : set β) = f '' ↑s := set.ext $ λ x, mem_map.trans set.mem_image_iff_bex.symm theorem coe_map_subset_range (f : α ↪ β) (s : finset α) : (↑(s.map f) : set β) ⊆ set.range f := calc ↑(s.map f) = f '' ↑s : coe_map f s ... ⊆ set.range f : set.image_subset_range f ↑s theorem map_to_finset [decidable_eq α] [decidable_eq β] {s : multiset α} : s.to_finset.map f = (s.map f).to_finset := ext.2 $ λ _, by simp only [mem_map, multiset.mem_map, exists_prop, multiset.mem_to_finset] theorem map_refl : s.map (embedding.refl _) = s := ext.2 $ λ _, by simpa only [mem_map, exists_prop] using exists_eq_right theorem map_map {g : β ↪ γ} : (s.map f).map g = s.map (f.trans g) := eq_of_veq $ by simp only [map_val, multiset.map_map]; refl theorem map_subset_map {s₁ s₂ : finset α} : s₁.map f ⊆ s₂.map f ↔ s₁ ⊆ s₂ := ⟨λ h x xs, (mem_map' _).1 $ h $ (mem_map' f).2 xs, λ h, by simp [subset_def, map_subset_map h]⟩ theorem map_inj {s₁ s₂ : finset α} : s₁.map f = s₂.map f ↔ s₁ = s₂ := by simp only [subset.antisymm_iff, map_subset_map] /-- Associate to an embedding `f` from `α` to `β` the embedding that maps a finset to its image under `f`. -/ def map_embedding (f : α ↪ β) : finset α ↪ finset β := ⟨map f, λ s₁ s₂, map_inj.1⟩ @[simp] theorem map_embedding_apply : map_embedding f s = map f s := rfl theorem map_filter {p : β → Prop} [decidable_pred p] : (s.map f).filter p = (s.filter (p ∘ f)).map f := ext.2 $ λ b, by simp only [mem_filter, mem_map, exists_prop, and_assoc]; exact ⟨by rintro ⟨⟨x, h1, rfl⟩, h2⟩; exact ⟨x, h1, h2, rfl⟩, by rintro ⟨x, h1, h2, rfl⟩; exact ⟨⟨x, h1, rfl⟩, h2⟩⟩ theorem map_union [decidable_eq α] [decidable_eq β] {f : α ↪ β} (s₁ s₂ : finset α) : (s₁ ∪ s₂).map f = s₁.map f ∪ s₂.map f := ext.2 $ λ _, by simp only [mem_map, mem_union, exists_prop, or_and_distrib_right, exists_or_distrib] theorem map_inter [decidable_eq α] [decidable_eq β] {f : α ↪ β} (s₁ s₂ : finset α) : (s₁ ∩ s₂).map f = s₁.map f ∩ s₂.map f := ext.2 $ λ b, by simp only [mem_map, mem_inter, exists_prop]; exact ⟨by rintro ⟨a, ⟨m₁, m₂⟩, rfl⟩; exact ⟨⟨a, m₁, rfl⟩, ⟨a, m₂, rfl⟩⟩, by rintro ⟨⟨a, m₁, e⟩, ⟨a', m₂, rfl⟩⟩; cases f.2 e; exact ⟨_, ⟨m₁, m₂⟩, rfl⟩⟩ @[simp] theorem map_singleton (f : α ↪ β) (a : α) : map f {a} = {f a} := ext.2 $ λ _, by simp only [mem_map, mem_singleton, exists_prop, exists_eq_left]; exact eq_comm @[simp] theorem map_insert [decidable_eq α] [decidable_eq β] (f : α ↪ β) (a : α) (s : finset α) : (insert a s).map f = insert (f a) (s.map f) := by simp only [insert_eq, map_union, map_singleton] @[simp] theorem map_eq_empty : s.map f = ∅ ↔ s = ∅ := ⟨λ h, eq_empty_of_forall_not_mem $ λ a m, ne_empty_of_mem (mem_map_of_mem _ m) h, λ e, e.symm ▸ rfl⟩ lemma attach_map_val {s : finset α} : s.attach.map (embedding.subtype _) = s := eq_of_veq $ by rw [map_val, attach_val]; exact attach_map_val _ end map lemma range_add_one' (n : ℕ) : range (n + 1) = insert 0 ((range n).map ⟨λi, i + 1, assume i j, nat.succ_inj⟩) := by ext (⟨⟩ | ⟨n⟩); simp [nat.succ_eq_add_one, nat.zero_lt_succ n] /-! ### image -/ section image variables [decidable_eq β] /-- `image f s` is the forward image of `s` under `f`. -/ def image (f : α → β) (s : finset α) : finset β := (s.1.map f).to_finset @[simp] theorem image_val (f : α → β) (s : finset α) : (image f s).1 = (s.1.map f).erase_dup := rfl @[simp] theorem image_empty (f : α → β) : (∅ : finset α).image f = ∅ := rfl variables {f : α → β} {s : finset α} @[simp] theorem mem_image {b : β} : b ∈ s.image f ↔ ∃ a ∈ s, f a = b := by simp only [mem_def, image_val, mem_erase_dup, multiset.mem_map, exists_prop] theorem mem_image_of_mem (f : α → β) {a} {s : finset α} (h : a ∈ s) : f a ∈ s.image f := mem_image.2 ⟨_, h, rfl⟩ @[simp] lemma coe_image {f : α → β} : ↑(s.image f) = f '' ↑s := set.ext $ λ _, mem_image.trans set.mem_image_iff_bex.symm lemma nonempty.image (h : s.nonempty) (f : α → β) : (s.image f).nonempty := let ⟨a, ha⟩ := h in ⟨f a, mem_image_of_mem f ha⟩ theorem image_to_finset [decidable_eq α] {s : multiset α} : s.to_finset.image f = (s.map f).to_finset := ext.2 $ λ _, by simp only [mem_image, multiset.mem_to_finset, exists_prop, multiset.mem_map] theorem image_val_of_inj_on (H : ∀x∈s, ∀y∈s, f x = f y → x = y) : (image f s).1 = s.1.map f := multiset.erase_dup_eq_self.2 (nodup_map_on H s.2) theorem image_id [decidable_eq α] : s.image id = s := ext.2 $ λ _, by simp only [mem_image, exists_prop, id, exists_eq_right] theorem image_image [decidable_eq γ] {g : β → γ} : (s.image f).image g = s.image (g ∘ f) := eq_of_veq $ by simp only [image_val, erase_dup_map_erase_dup_eq, multiset.map_map] theorem image_subset_image {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁.image f ⊆ s₂.image f := by simp only [subset_def, image_val, subset_erase_dup', erase_dup_subset', multiset.map_subset_map h] theorem image_mono (f : α → β) : monotone (finset.image f) := λ _ _, image_subset_image theorem coe_image_subset_range : ↑(s.image f) ⊆ set.range f := calc ↑(s.image f) = f '' ↑s : coe_image ... ⊆ set.range f : set.image_subset_range f ↑s theorem image_filter {p : β → Prop} [decidable_pred p] : (s.image f).filter p = (s.filter (p ∘ f)).image f := ext.2 $ λ b, by simp only [mem_filter, mem_image, exists_prop]; exact ⟨by rintro ⟨⟨x, h1, rfl⟩, h2⟩; exact ⟨x, ⟨h1, h2⟩, rfl⟩, by rintro ⟨x, ⟨h1, h2⟩, rfl⟩; exact ⟨⟨x, h1, rfl⟩, h2⟩⟩ theorem image_union [decidable_eq α] {f : α → β} (s₁ s₂ : finset α) : (s₁ ∪ s₂).image f = s₁.image f ∪ s₂.image f := ext.2 $ λ _, by simp only [mem_image, mem_union, exists_prop, or_and_distrib_right, exists_or_distrib] theorem image_inter [decidable_eq α] (s₁ s₂ : finset α) (hf : ∀x y, f x = f y → x = y) : (s₁ ∩ s₂).image f = s₁.image f ∩ s₂.image f := ext.2 $ by simp only [mem_image, exists_prop, mem_inter]; exact λ b, ⟨λ ⟨a, ⟨m₁, m₂⟩, e⟩, ⟨⟨a, m₁, e⟩, ⟨a, m₂, e⟩⟩, λ ⟨⟨a, m₁, e₁⟩, ⟨a', m₂, e₂⟩⟩, ⟨a, ⟨m₁, hf _ _ (e₂.trans e₁.symm) ▸ m₂⟩, e₁⟩⟩. @[simp] theorem image_singleton (f : α → β) (a : α) : image f {a} = {f a} := ext.2 $ λ x, by simpa only [mem_image, exists_prop, mem_singleton, exists_eq_left] using eq_comm @[simp] theorem image_insert [decidable_eq α] (f : α → β) (a : α) (s : finset α) : (insert a s).image f = insert (f a) (s.image f) := by simp only [insert_eq, image_singleton, image_union] @[simp] theorem image_eq_empty : s.image f = ∅ ↔ s = ∅ := ⟨λ h, eq_empty_of_forall_not_mem $ λ a m, ne_empty_of_mem (mem_image_of_mem _ m) h, λ e, e.symm ▸ rfl⟩ lemma attach_image_val [decidable_eq α] {s : finset α} : s.attach.image subtype.val = s := eq_of_veq $ by rw [image_val, attach_val, multiset.attach_map_val, erase_dup_eq_self] @[simp] lemma attach_insert [decidable_eq α] {a : α} {s : finset α} : attach (insert a s) = insert (⟨a, mem_insert_self a s⟩ : {x // x ∈ insert a s}) ((attach s).image (λx, ⟨x.1, mem_insert_of_mem x.2⟩)) := ext.2 $ λ ⟨x, hx⟩, ⟨or.cases_on (mem_insert.1 hx) (assume h : x = a, λ _, mem_insert.2 $ or.inl $ subtype.eq h) (assume h : x ∈ s, λ _, mem_insert_of_mem $ mem_image.2 $ ⟨⟨x, h⟩, mem_attach _ _, subtype.eq rfl⟩), λ _, finset.mem_attach _ _⟩ theorem map_eq_image (f : α ↪ β) (s : finset α) : s.map f = s.image f := eq_of_veq $ (multiset.erase_dup_eq_self.2 (s.map f).2).symm lemma image_const {s : finset α} (h : s.nonempty) (b : β) : s.image (λa, b) = singleton b := ext.2 $ assume b', by simp only [mem_image, exists_prop, exists_and_distrib_right, h.bex, true_and, mem_singleton, eq_comm] /-- Given a finset `s` and a predicate `p`, `s.subtype p` is the finset of `subtype p` whose elements belong to `s`. -/ protected def subtype {α} (p : α → Prop) [decidable_pred p] (s : finset α) : finset (subtype p) := (s.filter p).attach.map ⟨λ x, ⟨x.1, (finset.mem_filter.1 x.2).2⟩, λ x y H, subtype.eq $ subtype.mk.inj H⟩ @[simp] lemma mem_subtype {p : α → Prop} [decidable_pred p] {s : finset α} : ∀{a : subtype p}, a ∈ s.subtype p ↔ a.val ∈ s | ⟨a, ha⟩ := by simp [finset.subtype, ha] lemma subset_image_iff {f : α → β} {s : finset β} {t : set α} : ↑s ⊆ f '' t ↔ ∃s' : finset α, ↑s' ⊆ t ∧ s'.image f = s := begin classical, split, swap, { rintro ⟨s, hs, rfl⟩, rw [coe_image], exact set.image_subset f hs }, intro h, induction s using finset.induction with a s has ih h, { refine ⟨∅, set.empty_subset _, _⟩, convert finset.image_empty _ }, rw [finset.coe_insert, set.insert_subset] at h, rcases ih h.2 with ⟨s', hst, hsi⟩, rcases h.1 with ⟨x, hxt, rfl⟩, refine ⟨insert x s', _, _⟩, { rw [finset.coe_insert, set.insert_subset], exact ⟨hxt, hst⟩ }, rw [finset.image_insert, hsi], congr end end image /-! ### card -/ section card /-- `card s` is the cardinality (number of elements) of `s`. -/ def card (s : finset α) : nat := s.1.card theorem card_def (s : finset α) : s.card = s.1.card := rfl @[simp] theorem card_empty : card (∅ : finset α) = 0 := rfl @[simp] theorem card_eq_zero {s : finset α} : card s = 0 ↔ s = ∅ := card_eq_zero.trans val_eq_zero theorem card_pos {s : finset α} : 0 < card s ↔ s.nonempty := pos_iff_ne_zero.trans $ (not_congr card_eq_zero).trans nonempty_iff_ne_empty.symm theorem card_ne_zero_of_mem {s : finset α} {a : α} (h : a ∈ s) : card s ≠ 0 := (not_congr card_eq_zero).2 (ne_empty_of_mem h) theorem card_eq_one {s : finset α} : s.card = 1 ↔ ∃ a, s = {a} := by cases s; simp only [multiset.card_eq_one, finset.card, ← val_inj, singleton_val] @[simp] theorem card_insert_of_not_mem [decidable_eq α] {a : α} {s : finset α} (h : a ∉ s) : card (insert a s) = card s + 1 := by simpa only [card_cons, card, insert_val] using congr_arg multiset.card (ndinsert_of_not_mem h) theorem card_insert_le [decidable_eq α] (a : α) (s : finset α) : card (insert a s) ≤ card s + 1 := by by_cases a ∈ s; [{rw [insert_eq_of_mem h], apply nat.le_add_right}, rw [card_insert_of_not_mem h]] @[simp] theorem card_singleton (a : α) : card ({a} : finset α) = 1 := card_singleton _ theorem card_erase_of_mem [decidable_eq α] {a : α} {s : finset α} : a ∈ s → card (erase s a) = pred (card s) := card_erase_of_mem theorem card_erase_lt_of_mem [decidable_eq α] {a : α} {s : finset α} : a ∈ s → card (erase s a) < card s := card_erase_lt_of_mem theorem card_erase_le [decidable_eq α] {a : α} {s : finset α} : card (erase s a) ≤ card s := card_erase_le @[simp] theorem card_range (n : ℕ) : card (range n) = n := card_range n @[simp] theorem card_attach {s : finset α} : card (attach s) = card s := multiset.card_attach end card end finset theorem multiset.to_finset_card_le [decidable_eq α] (m : multiset α) : m.to_finset.card ≤ m.card := card_le_of_le (erase_dup_le _) theorem list.to_finset_card_le [decidable_eq α] (l : list α) : l.to_finset.card ≤ l.length := multiset.to_finset_card_le ⟦l⟧ namespace finset section card theorem card_image_le [decidable_eq β] {f : α → β} {s : finset α} : card (image f s) ≤ card s := by simpa only [card_map] using (s.1.map f).to_finset_card_le theorem card_image_of_inj_on [decidable_eq β] {f : α → β} {s : finset α} (H : ∀x∈s, ∀y∈s, f x = f y → x = y) : card (image f s) = card s := by simp only [card, image_val_of_inj_on H, card_map] theorem card_image_of_injective [decidable_eq β] {f : α → β} (s : finset α) (H : function.injective f) : card (image f s) = card s := card_image_of_inj_on $ λ x _ y _ h, H h @[simp] lemma card_map {α β} (f : α ↪ β) {s : finset α} : (s.map f).card = s.card := multiset.card_map _ _ lemma card_eq_of_bijective {s : finset α} {n : ℕ} (f : ∀i, i < n → α) (hf : ∀a∈s, ∃i, ∃h:i<n, f i h = a) (hf' : ∀i (h : i < n), f i h ∈ s) (f_inj : ∀i j (hi : i < n) (hj : j < n), f i hi = f j hj → i = j) : card s = n := begin classical, have : ∀ (a : α), a ∈ s ↔ ∃i (hi : i ∈ range n), f i (mem_range.1 hi) = a, from assume a, ⟨assume ha, let ⟨i, hi, eq⟩ := hf a ha in ⟨i, mem_range.2 hi, eq⟩, assume ⟨i, hi, eq⟩, eq ▸ hf' i (mem_range.1 hi)⟩, have : s = ((range n).attach.image $ λi, f i.1 (mem_range.1 i.2)), by simpa only [ext, mem_image, exists_prop, subtype.exists, mem_attach, true_and], calc card s = card ((range n).attach.image $ λi, f i.1 (mem_range.1 i.2)) : by rw [this] ... = card ((range n).attach) : card_image_of_injective _ $ assume ⟨i, hi⟩ ⟨j, hj⟩ eq, subtype.eq $ f_inj i j (mem_range.1 hi) (mem_range.1 hj) eq ... = card (range n) : card_attach ... = n : card_range n end lemma card_eq_succ [decidable_eq α] {s : finset α} {n : ℕ} : s.card = n + 1 ↔ (∃a t, a ∉ t ∧ insert a t = s ∧ card t = n) := iff.intro (assume eq, have 0 < card s, from eq.symm ▸ nat.zero_lt_succ _, let ⟨a, has⟩ := card_pos.mp this in ⟨a, s.erase a, s.not_mem_erase a, insert_erase has, by simp only [eq, card_erase_of_mem has, pred_succ]⟩) (assume ⟨a, t, hat, s_eq, n_eq⟩, s_eq ▸ n_eq ▸ card_insert_of_not_mem hat) theorem card_le_of_subset {s t : finset α} : s ⊆ t → card s ≤ card t := multiset.card_le_of_le ∘ val_le_iff.mpr theorem eq_of_subset_of_card_le {s t : finset α} (h : s ⊆ t) (h₂ : card t ≤ card s) : s = t := eq_of_veq $ multiset.eq_of_le_of_card_le (val_le_iff.mpr h) h₂ lemma card_lt_card {s t : finset α} (h : s ⊂ t) : s.card < t.card := card_lt_of_lt (val_lt_iff.2 h) lemma card_le_card_of_inj_on {s : finset α} {t : finset β} (f : α → β) (hf : ∀a∈s, f a ∈ t) (f_inj : ∀a₁∈s, ∀a₂∈s, f a₁ = f a₂ → a₁ = a₂) : card s ≤ card t := begin classical, calc card s = card (s.image f) : by rw [card_image_of_inj_on f_inj] ... ≤ card t : card_le_of_subset $ assume x hx, match x, finset.mem_image.1 hx with _, ⟨a, ha, rfl⟩ := hf a ha end end lemma card_le_of_inj_on {n} {s : finset α} (f : ℕ → α) (hf : ∀i<n, f i ∈ s) (f_inj : ∀i j, i<n → j<n → f i = f j → i = j) : n ≤ card s := calc n = card (range n) : (card_range n).symm ... ≤ card s : card_le_card_of_inj_on f (by simpa only [mem_range]) (by simp only [mem_range]; exact assume a₁ h₁ a₂ h₂, f_inj a₁ a₂ h₁ h₂) /-- Suppose that, given objects defined on all strict subsets of any finset `s`, one knows how to define an object on `s`. Then one can inductively define an object on all finsets, starting from the empty set and iterating. This can be used either to define data, or to prove properties. -/ @[elab_as_eliminator] def strong_induction_on {p : finset α → Sort*} : ∀ (s : finset α), (∀s, (∀t ⊂ s, p t) → p s) → p s | ⟨s, nd⟩ ih := multiset.strong_induction_on s (λ s IH nd, ih ⟨s, nd⟩ (λ ⟨t, nd'⟩ ss, IH t (val_lt_iff.2 ss) nd')) nd @[elab_as_eliminator] lemma case_strong_induction_on [decidable_eq α] {p : finset α → Prop} (s : finset α) (h₀ : p ∅) (h₁ : ∀ a s, a ∉ s → (∀t ⊆ s, p t) → p (insert a s)) : p s := finset.strong_induction_on s $ λ s, finset.induction_on s (λ _, h₀) $ λ a s n _ ih, h₁ a s n $ λ t ss, ih _ (lt_of_le_of_lt ss (ssubset_insert n) : t < _) lemma card_congr {s : finset α} {t : finset β} (f : Π a ∈ s, β) (h₁ : ∀ a ha, f a ha ∈ t) (h₂ : ∀ a b ha hb, f a ha = f b hb → a = b) (h₃ : ∀ b ∈ t, ∃ a ha, f a ha = b) : s.card = t.card := by haveI := classical.prop_decidable; exact calc s.card = s.attach.card : card_attach.symm ... = (s.attach.image (λ (a : {a // a ∈ s}), f a.1 a.2)).card : eq.symm (card_image_of_injective _ (λ a b h, subtype.eq (h₂ _ _ _ _ h))) ... = t.card : congr_arg card (finset.ext.2 $ λ b, ⟨λ h, let ⟨a, ha₁, ha₂⟩ := mem_image.1 h in ha₂ ▸ h₁ _ _, λ h, let ⟨a, ha₁, ha₂⟩ := h₃ b h in mem_image.2 ⟨⟨a, ha₁⟩, by simp [ha₂]⟩⟩) lemma card_union_add_card_inter [decidable_eq α] (s t : finset α) : (s ∪ t).card + (s ∩ t).card = s.card + t.card := finset.induction_on t (by simp) $ λ a r har, by by_cases a ∈ s; simp *; cc lemma card_union_le [decidable_eq α] (s t : finset α) : (s ∪ t).card ≤ s.card + t.card := card_union_add_card_inter s t ▸ le_add_right _ _ lemma surj_on_of_inj_on_of_card_le {s : finset α} {t : finset β} (f : Π a ∈ s, β) (hf : ∀ a ha, f a ha ∈ t) (hinj : ∀ a₁ a₂ ha₁ ha₂, f a₁ ha₁ = f a₂ ha₂ → a₁ = a₂) (hst : card t ≤ card s) : (∀ b ∈ t, ∃ a ha, b = f a ha) := by haveI := classical.dec_eq β; exact λ b hb, have h : card (image (λ (a : {a // a ∈ s}), f (a.val) a.2) (attach s)) = card s, from @card_attach _ s ▸ card_image_of_injective _ (λ ⟨a₁, ha₁⟩ ⟨a₂, ha₂⟩ h, subtype.eq $ hinj _ _ _ _ h), have h₁ : image (λ a : {a // a ∈ s}, f a.1 a.2) s.attach = t := eq_of_subset_of_card_le (λ b h, let ⟨a, ha₁, ha₂⟩ := mem_image.1 h in ha₂ ▸ hf _ _) (by simp [hst, h]), begin rw ← h₁ at hb, rcases mem_image.1 hb with ⟨a, ha₁, ha₂⟩, exact ⟨a, a.2, ha₂.symm⟩, end open function lemma inj_on_of_surj_on_of_card_le {s : finset α} {t : finset β} (f : Π a ∈ s, β) (hf : ∀ a ha, f a ha ∈ t) (hsurj : ∀ b ∈ t, ∃ a ha, b = f a ha) (hst : card s ≤ card t) ⦃a₁ a₂⦄ (ha₁ : a₁ ∈ s) (ha₂ : a₂ ∈ s) (ha₁a₂: f a₁ ha₁ = f a₂ ha₂) : a₁ = a₂ := by haveI : inhabited {x // x ∈ s} := ⟨⟨a₁, ha₁⟩⟩; exact let f' : {x // x ∈ s} → {x // x ∈ t} := λ x, ⟨f x.1 x.2, hf x.1 x.2⟩ in let g : {x // x ∈ t} → {x // x ∈ s} := @surj_inv _ _ f' (λ x, let ⟨y, hy₁, hy₂⟩ := hsurj x.1 x.2 in ⟨⟨y, hy₁⟩, subtype.eq hy₂.symm⟩) in have hg : injective g, from function.injective_surj_inv _, have hsg : surjective g, from λ x, let ⟨y, hy⟩ := surj_on_of_inj_on_of_card_le (λ (x : {x // x ∈ t}) (hx : x ∈ t.attach), g x) (λ x _, show (g x) ∈ s.attach, from mem_attach _ _) (λ x y _ _ hxy, hg hxy) (by simpa) x (mem_attach _ _) in ⟨y, hy.snd.symm⟩, have hif : injective f', from (left_inverse_of_surjective_of_right_inverse hsg (right_inverse_surj_inv _)).injective, subtype.ext.1 (@hif ⟨a₁, ha₁⟩ ⟨a₂, ha₂⟩ (subtype.eq ha₁a₂)) end card /-! ### bind -/ section bind variables [decidable_eq β] {s : finset α} {t : α → finset β} /-- `bind s t` is the union of `t x` over `x ∈ s` -/ protected def bind (s : finset α) (t : α → finset β) : finset β := (s.1.bind (λ a, (t a).1)).to_finset @[simp] theorem bind_val (s : finset α) (t : α → finset β) : (s.bind t).1 = (s.1.bind (λ a, (t a).1)).erase_dup := rfl @[simp] theorem bind_empty : finset.bind ∅ t = ∅ := rfl @[simp] theorem mem_bind {b : β} : b ∈ s.bind t ↔ ∃a∈s, b ∈ t a := by simp only [mem_def, bind_val, mem_erase_dup, mem_bind, exists_prop] @[simp] theorem bind_insert [decidable_eq α] {a : α} : (insert a s).bind t = t a ∪ s.bind t := ext.2 $ λ x, by simp only [mem_bind, exists_prop, mem_union, mem_insert, or_and_distrib_right, exists_or_distrib, exists_eq_left] -- ext.2 $ λ x, by simp [or_and_distrib_right, exists_or_distrib] @[simp] lemma singleton_bind {a : α} : finset.bind {a} t = t a := begin classical, rw [← insert_emptyc_eq, bind_insert, bind_empty, union_empty] end theorem bind_inter (s : finset α) (f : α → finset β) (t : finset β) : s.bind f ∩ t = s.bind (λ x, f x ∩ t) := begin ext x, simp only [mem_bind, mem_inter], tauto end theorem inter_bind (t : finset β) (s : finset α) (f : α → finset β) : t ∩ s.bind f = s.bind (λ x, t ∩ f x) := by rw [inter_comm, bind_inter]; simp [inter_comm] theorem image_bind [decidable_eq γ] {f : α → β} {s : finset α} {t : β → finset γ} : (s.image f).bind t = s.bind (λa, t (f a)) := by haveI := classical.dec_eq α; exact finset.induction_on s rfl (λ a s has ih, by simp only [image_insert, bind_insert, ih]) theorem bind_image [decidable_eq γ] {s : finset α} {t : α → finset β} {f : β → γ} : (s.bind t).image f = s.bind (λa, (t a).image f) := by haveI := classical.dec_eq α; exact finset.induction_on s rfl (λ a s has ih, by simp only [bind_insert, image_union, ih]) theorem bind_to_finset [decidable_eq α] (s : multiset α) (t : α → multiset β) : (s.bind t).to_finset = s.to_finset.bind (λa, (t a).to_finset) := ext.2 $ λ x, by simp only [multiset.mem_to_finset, mem_bind, multiset.mem_bind, exists_prop] lemma bind_mono {t₁ t₂ : α → finset β} (h : ∀a∈s, t₁ a ⊆ t₂ a) : s.bind t₁ ⊆ s.bind t₂ := have ∀b a, a ∈ s → b ∈ t₁ a → (∃ (a : α), a ∈ s ∧ b ∈ t₂ a), from assume b a ha hb, ⟨a, ha, finset.mem_of_subset (h a ha) hb⟩, by simpa only [subset_iff, mem_bind, exists_imp_distrib, and_imp, exists_prop] lemma bind_subset_bind_of_subset_left {α : Type*} {s₁ s₂ : finset α} (t : α → finset β) (h : s₁ ⊆ s₂) : s₁.bind t ⊆ s₂.bind t := begin intro x, simp only [and_imp, mem_bind, exists_prop], exact Exists.imp (λ a ha, ⟨h ha.1, ha.2⟩) end lemma bind_singleton {f : α → β} : s.bind (λa, {f a}) = s.image f := ext.2 $ λ x, by simp only [mem_bind, mem_image, mem_singleton, eq_comm] lemma image_bind_filter_eq [decidable_eq α] (s : finset β) (g : β → α) : (s.image g).bind (λa, s.filter $ (λc, g c = a)) = s := begin ext b, suffices : (∃ a, a ∈ s ∧ b ∈ s ∧ g b = g a) ↔ b ∈ s, by simpa, exact ⟨λ ⟨a, ha, hb, hab⟩, hb, λ hb, ⟨b, hb, hb, rfl⟩⟩ end end bind /-! ### prod-/ section prod variables {s : finset α} {t : finset β} /-- `product s t` is the set of pairs `(a, b)` such that `a ∈ s` and `b ∈ t`. -/ protected def product (s : finset α) (t : finset β) : finset (α × β) := ⟨_, nodup_product s.2 t.2⟩ @[simp] theorem product_val : (s.product t).1 = s.1.product t.1 := rfl @[simp] theorem mem_product {p : α × β} : p ∈ s.product t ↔ p.1 ∈ s ∧ p.2 ∈ t := mem_product theorem product_eq_bind [decidable_eq α] [decidable_eq β] (s : finset α) (t : finset β) : s.product t = s.bind (λa, t.image $ λb, (a, b)) := ext.2 $ λ ⟨x, y⟩, by simp only [mem_product, mem_bind, mem_image, exists_prop, prod.mk.inj_iff, and.left_comm, exists_and_distrib_left, exists_eq_right, exists_eq_left] @[simp] theorem card_product (s : finset α) (t : finset β) : card (s.product t) = card s * card t := multiset.card_product _ _ end prod /-! ### sigma -/ section sigma variables {σ : α → Type*} {s : finset α} {t : Πa, finset (σ a)} /-- `sigma s t` is the set of dependent pairs `⟨a, b⟩` such that `a ∈ s` and `b ∈ t a`. -/ protected def sigma (s : finset α) (t : Πa, finset (σ a)) : finset (Σa, σ a) := ⟨_, nodup_sigma s.2 (λ a, (t a).2)⟩ @[simp] theorem mem_sigma {p : sigma σ} : p ∈ s.sigma t ↔ p.1 ∈ s ∧ p.2 ∈ t (p.1) := mem_sigma theorem sigma_mono {s₁ s₂ : finset α} {t₁ t₂ : Πa, finset (σ a)} (H1 : s₁ ⊆ s₂) (H2 : ∀a, t₁ a ⊆ t₂ a) : s₁.sigma t₁ ⊆ s₂.sigma t₂ := λ ⟨x, sx⟩ H, let ⟨H3, H4⟩ := mem_sigma.1 H in mem_sigma.2 ⟨H1 H3, H2 x H4⟩ theorem sigma_eq_bind [decidable_eq α] [∀a, decidable_eq (σ a)] (s : finset α) (t : Πa, finset (σ a)) : s.sigma t = s.bind (λa, (t a).image $ λb, ⟨a, b⟩) := ext.2 $ λ ⟨x, y⟩, by simp only [mem_sigma, mem_bind, mem_image, exists_prop, and.left_comm, exists_and_distrib_left, exists_eq_left, heq_iff_eq, exists_eq_right] end sigma /-! ### pi -/ section pi variables {δ : α → Type*} [decidable_eq α] /-- Given a finset `s` of `α` and for all `a : α` a finset `t a` of `δ a`, then one can define the finset `s.pi t` of all functions defined on elements of `s` taking values in `t a` for `a ∈ s`. Note that the elements of `s.pi t` are only partially defined, on `s`. -/ def pi (s : finset α) (t : Πa, finset (δ a)) : finset (Πa∈s, δ a) := ⟨s.1.pi (λ a, (t a).1), nodup_pi s.2 (λ a _, (t a).2)⟩ @[simp] lemma pi_val (s : finset α) (t : Πa, finset (δ a)) : (s.pi t).1 = s.1.pi (λ a, (t a).1) := rfl @[simp] lemma mem_pi {s : finset α} {t : Πa, finset (δ a)} {f : Πa∈s, δ a} : f ∈ s.pi t ↔ (∀a (h : a ∈ s), f a h ∈ t a) := mem_pi _ _ _ /-- The empty dependent product function, defined on the emptyset. The assumption `a ∈ ∅` is never satisfied. -/ def pi.empty (β : α → Sort*) (a : α) (h : a ∈ (∅ : finset α)) : β a := multiset.pi.empty β a h /-- Given a function `f` defined on a finset `s`, define a new function on the finset `s ∪ {a}`, equal to `f` on `s` and sending `a` to a given value `b`. This function is denoted `s.pi.cons a b f`. If `a` already belongs to `s`, the new function takes the value `b` at `a` anyway. -/ def pi.cons (s : finset α) (a : α) (b : δ a) (f : Πa, a ∈ s → δ a) (a' : α) (h : a' ∈ insert a s) : δ a' := multiset.pi.cons s.1 a b f _ (multiset.mem_cons.2 $ mem_insert.symm.2 h) @[simp] lemma pi.cons_same (s : finset α) (a : α) (b : δ a) (f : Πa, a ∈ s → δ a) (h : a ∈ insert a s) : pi.cons s a b f a h = b := multiset.pi.cons_same _ lemma pi.cons_ne {s : finset α} {a a' : α} {b : δ a} {f : Πa, a ∈ s → δ a} {h : a' ∈ insert a s} (ha : a ≠ a') : pi.cons s a b f a' h = f a' ((mem_insert.1 h).resolve_left ha.symm) := multiset.pi.cons_ne _ _ lemma injective_pi_cons {a : α} {b : δ a} {s : finset α} (hs : a ∉ s) : function.injective (pi.cons s a b) := assume e₁ e₂ eq, @multiset.injective_pi_cons α _ δ a b s.1 hs _ _ $ funext $ assume e, funext $ assume h, have pi.cons s a b e₁ e (by simpa only [mem_cons, mem_insert] using h) = pi.cons s a b e₂ e (by simpa only [mem_cons, mem_insert] using h), { rw [eq] }, this @[simp] lemma pi_empty {t : Πa:α, finset (δ a)} : pi (∅ : finset α) t = singleton (pi.empty δ) := rfl @[simp] lemma pi_insert [∀a, decidable_eq (δ a)] {s : finset α} {t : Πa:α, finset (δ a)} {a : α} (ha : a ∉ s) : pi (insert a s) t = (t a).bind (λb, (pi s t).image (pi.cons s a b)) := begin apply eq_of_veq, rw ← multiset.erase_dup_eq_self.2 (pi (insert a s) t).2, refine (λ s' (h : s' = a :: s.1), (_ : erase_dup (multiset.pi s' (λ a, (t a).1)) = erase_dup ((t a).1.bind $ λ b, erase_dup $ (multiset.pi s.1 (λ (a : α), (t a).val)).map $ λ f a' h', multiset.pi.cons s.1 a b f a' (h ▸ h')))) _ (insert_val_of_not_mem ha), subst s', rw pi_cons, congr, funext b, rw multiset.erase_dup_eq_self.2, exact multiset.nodup_map (multiset.injective_pi_cons ha) (pi s t).2, end lemma pi_subset {s : finset α} (t₁ t₂ : Πa, finset (δ a)) (h : ∀ a ∈ s, t₁ a ⊆ t₂ a) : s.pi t₁ ⊆ s.pi t₂ := λ g hg, mem_pi.2 $ λ a ha, h a ha (mem_pi.mp hg a ha) end pi /-! ### powerset -/ section powerset /-- When `s` is a finset, `s.powerset` is the finset of all subsets of `s` (seen as finsets). -/ def powerset (s : finset α) : finset (finset α) := ⟨s.1.powerset.pmap finset.mk (λ t h, nodup_of_le (mem_powerset.1 h) s.2), nodup_pmap (λ a ha b hb, congr_arg finset.val) (nodup_powerset.2 s.2)⟩ @[simp] theorem mem_powerset {s t : finset α} : s ∈ powerset t ↔ s ⊆ t := by cases s; simp only [powerset, mem_mk, mem_pmap, mem_powerset, exists_prop, exists_eq_right]; rw ← val_le_iff @[simp] theorem empty_mem_powerset (s : finset α) : ∅ ∈ powerset s := mem_powerset.2 (empty_subset _) @[simp] theorem mem_powerset_self (s : finset α) : s ∈ powerset s := mem_powerset.2 (subset.refl _) @[simp] lemma powerset_empty : finset.powerset (∅ : finset α) = {∅} := rfl @[simp] theorem powerset_mono {s t : finset α} : powerset s ⊆ powerset t ↔ s ⊆ t := ⟨λ h, (mem_powerset.1 $ h $ mem_powerset_self _), λ st u h, mem_powerset.2 $ subset.trans (mem_powerset.1 h) st⟩ @[simp] theorem card_powerset (s : finset α) : card (powerset s) = 2 ^ card s := (card_pmap _ _ _).trans (card_powerset s.1) lemma not_mem_of_mem_powerset_of_not_mem {s t : finset α} {a : α} (ht : t ∈ s.powerset) (h : a ∉ s) : a ∉ t := by { apply mt _ h, apply mem_powerset.1 ht } lemma powerset_insert [decidable_eq α] (s : finset α) (a : α) : powerset (insert a s) = s.powerset ∪ s.powerset.image (insert a) := begin ext t, simp only [exists_prop, mem_powerset, mem_image, mem_union, subset_insert_iff], by_cases h : a ∈ t, { split, { exact λH, or.inr ⟨_, H, insert_erase h⟩ }, { intros H, cases H, { exact subset.trans (erase_subset a t) H }, { rcases H with ⟨u, hu⟩, rw ← hu.2, exact subset.trans (erase_insert_subset a u) hu.1 } } }, { have : ¬ ∃ (u : finset α), u ⊆ s ∧ insert a u = t, by simp [ne.symm (ne_insert_of_not_mem _ _ h)], simp [finset.erase_eq_of_not_mem h, this] } end end powerset section powerset_len /-- Given an integer `n` and a finset `s`, then `powerset_len n s` is the finset of subsets of `s` of cardinality `n`.-/ def powerset_len (n : ℕ) (s : finset α) : finset (finset α) := ⟨(s.1.powerset_len n).pmap finset.mk (λ t h, nodup_of_le (mem_powerset_len.1 h).1 s.2), nodup_pmap (λ a ha b hb, congr_arg finset.val) (nodup_powerset_len s.2)⟩ theorem mem_powerset_len {n} {s t : finset α} : s ∈ powerset_len n t ↔ s ⊆ t ∧ card s = n := by cases s; simp [powerset_len, val_le_iff.symm]; refl @[simp] theorem powerset_len_mono {n} {s t : finset α} (h : s ⊆ t) : powerset_len n s ⊆ powerset_len n t := λ u h', mem_powerset_len.2 $ and.imp (λ h₂, subset.trans h₂ h) id (mem_powerset_len.1 h') @[simp] theorem card_powerset_len (n : ℕ) (s : finset α) : card (powerset_len n s) = nat.choose (card s) n := (card_pmap _ _ _).trans (card_powerset_len n s.1) end powerset_len /-! ### fold -/ section fold variables (op : β → β → β) [hc : is_commutative β op] [ha : is_associative β op] local notation a * b := op a b include hc ha /-- `fold op b f s` folds the commutative associative operation `op` over the `f`-image of `s`, i.e. `fold (+) b f {1,2,3} = `f 1 + f 2 + f 3 + b`. -/ def fold (b : β) (f : α → β) (s : finset α) : β := (s.1.map f).fold op b variables {op} {f : α → β} {b : β} {s : finset α} {a : α} @[simp] theorem fold_empty : (∅ : finset α).fold op b f = b := rfl @[simp] theorem fold_insert [decidable_eq α] (h : a ∉ s) : (insert a s).fold op b f = f a * s.fold op b f := by unfold fold; rw [insert_val, ndinsert_of_not_mem h, map_cons, fold_cons_left] @[simp] theorem fold_singleton : ({a} : finset α).fold op b f = f a * b := rfl @[simp] theorem fold_map {g : γ ↪ α} {s : finset γ} : (s.map g).fold op b f = s.fold op b (f ∘ g) := by simp only [fold, map, multiset.map_map] @[simp] theorem fold_image [decidable_eq α] {g : γ → α} {s : finset γ} (H : ∀ (x ∈ s) (y ∈ s), g x = g y → x = y) : (s.image g).fold op b f = s.fold op b (f ∘ g) := by simp only [fold, image_val_of_inj_on H, multiset.map_map] @[congr] theorem fold_congr {g : α → β} (H : ∀ x ∈ s, f x = g x) : s.fold op b f = s.fold op b g := by rw [fold, fold, map_congr H] theorem fold_op_distrib {f g : α → β} {b₁ b₂ : β} : s.fold op (b₁ * b₂) (λx, f x * g x) = s.fold op b₁ f * s.fold op b₂ g := by simp only [fold, fold_distrib] theorem fold_hom {op' : γ → γ → γ} [is_commutative γ op'] [is_associative γ op'] {m : β → γ} (hm : ∀x y, m (op x y) = op' (m x) (m y)) : s.fold op' (m b) (λx, m (f x)) = m (s.fold op b f) := by rw [fold, fold, ← fold_hom op hm, multiset.map_map] theorem fold_union_inter [decidable_eq α] {s₁ s₂ : finset α} {b₁ b₂ : β} : (s₁ ∪ s₂).fold op b₁ f * (s₁ ∩ s₂).fold op b₂ f = s₁.fold op b₂ f * s₂.fold op b₁ f := by unfold fold; rw [← fold_add op, ← map_add, union_val, inter_val, union_add_inter, map_add, hc.comm, fold_add] @[simp] theorem fold_insert_idem [decidable_eq α] [hi : is_idempotent β op] : (insert a s).fold op b f = f a * s.fold op b f := begin by_cases (a ∈ s), { rw [← insert_erase h], simp [← ha.assoc, hi.idempotent] }, { apply fold_insert h }, end lemma fold_op_rel_iff_and {r : β → β → Prop} (hr : ∀ {x y z}, r x (op y z) ↔ (r x y ∧ r x z)) {c : β} : r c (s.fold op b f) ↔ (r c b ∧ ∀ x∈s, r c (f x)) := begin classical, apply finset.induction_on s, { simp }, clear s, intros a s ha IH, rw [finset.fold_insert ha, hr, IH, ← and_assoc, and_comm (r c (f a)), and_assoc], apply and_congr iff.rfl, split, { rintro ⟨h₁, h₂⟩, intros b hb, rw finset.mem_insert at hb, rcases hb with rfl|hb; solve_by_elim }, { intro h, split, { exact h a (finset.mem_insert_self _ _), }, { intros b hb, apply h b, rw finset.mem_insert, right, exact hb } } end lemma fold_op_rel_iff_or {r : β → β → Prop} (hr : ∀ {x y z}, r x (op y z) ↔ (r x y ∨ r x z)) {c : β} : r c (s.fold op b f) ↔ (r c b ∨ ∃ x∈s, r c (f x)) := begin classical, apply finset.induction_on s, { simp }, clear s, intros a s ha IH, rw [finset.fold_insert ha, hr, IH, ← or_assoc, or_comm (r c (f a)), or_assoc], apply or_congr iff.rfl, split, { rintro (h₁|⟨x, hx, h₂⟩), { use a, simp [h₁] }, { refine ⟨x, by simp [hx], h₂⟩ } }, { rintro ⟨x, hx, h⟩, rw mem_insert at hx, cases hx, { left, rwa hx at h }, { right, exact ⟨x, hx, h⟩ } } end omit hc ha section order variables [decidable_linear_order β] (c : β) lemma le_fold_min : c ≤ s.fold min b f ↔ (c ≤ b ∧ ∀ x∈s, c ≤ f x) := fold_op_rel_iff_and $ λ x y z, le_min_iff lemma fold_min_le : s.fold min b f ≤ c ↔ (b ≤ c ∨ ∃ x∈s, f x ≤ c) := begin show _ ≥ _ ↔ _, apply fold_op_rel_iff_or, intros x y z, show _ ≤ _ ↔ _, exact min_le_iff end lemma lt_fold_min : c < s.fold min b f ↔ (c < b ∧ ∀ x∈s, c < f x) := fold_op_rel_iff_and $ λ x y z, lt_min_iff lemma fold_min_lt : s.fold min b f < c ↔ (b < c ∨ ∃ x∈s, f x < c) := begin show _ > _ ↔ _, apply fold_op_rel_iff_or, intros x y z, show _ < _ ↔ _, exact min_lt_iff end lemma fold_max_le : s.fold max b f ≤ c ↔ (b ≤ c ∧ ∀ x∈s, f x ≤ c) := begin show _ ≥ _ ↔ _, apply fold_op_rel_iff_and, intros x y z, show _ ≤ _ ↔ _, exact max_le_iff end lemma le_fold_max : c ≤ s.fold max b f ↔ (c ≤ b ∨ ∃ x∈s, c ≤ f x) := fold_op_rel_iff_or $ λ x y z, le_max_iff lemma fold_max_lt : s.fold max b f < c ↔ (b < c ∧ ∀ x∈s, f x < c) := begin show _ > _ ↔ _, apply fold_op_rel_iff_and, intros x y z, show _ < _ ↔ _, exact max_lt_iff end lemma lt_fold_max : c < s.fold max b f ↔ (c < b ∨ ∃ x∈s, c < f x) := fold_op_rel_iff_or $ λ x y z, lt_max_iff end order end fold /-! ### sup -/ section sup variables [semilattice_sup_bot α] /-- Supremum of a finite set: `sup {a, b, c} f = f a ⊔ f b ⊔ f c` -/ def sup (s : finset β) (f : β → α) : α := s.fold (⊔) ⊥ f variables {s s₁ s₂ : finset β} {f : β → α} lemma sup_val : s.sup f = (s.1.map f).sup := rfl @[simp] lemma sup_empty : (∅ : finset β).sup f = ⊥ := fold_empty @[simp] lemma sup_insert [decidable_eq β] {b : β} : (insert b s : finset β).sup f = f b ⊔ s.sup f := fold_insert_idem @[simp] lemma sup_singleton {b : β} : ({b} : finset β).sup f = f b := sup_singleton lemma sup_union [decidable_eq β] : (s₁ ∪ s₂).sup f = s₁.sup f ⊔ s₂.sup f := finset.induction_on s₁ (by rw [empty_union, sup_empty, bot_sup_eq]) $ λ a s has ih, by rw [insert_union, sup_insert, sup_insert, ih, sup_assoc] theorem sup_congr {f g : β → α} (hs : s₁ = s₂) (hfg : ∀a∈s₂, f a = g a) : s₁.sup f = s₂.sup g := by subst hs; exact finset.fold_congr hfg @[simp] lemma sup_le_iff {a : α} : s.sup f ≤ a ↔ (∀b ∈ s, f b ≤ a) := begin apply iff.trans multiset.sup_le, simp only [multiset.mem_map, and_imp, exists_imp_distrib], exact ⟨λ k b hb, k _ _ hb rfl, λ k a' b hb h, h ▸ k _ hb⟩, end lemma sup_le {a : α} : (∀b ∈ s, f b ≤ a) → s.sup f ≤ a := sup_le_iff.2 lemma le_sup {b : β} (hb : b ∈ s) : f b ≤ s.sup f := sup_le_iff.1 (le_refl _) _ hb lemma sup_mono_fun {g : β → α} (h : ∀b∈s, f b ≤ g b) : s.sup f ≤ s.sup g := sup_le (λ b hb, le_trans (h b hb) (le_sup hb)) lemma sup_mono (h : s₁ ⊆ s₂) : s₁.sup f ≤ s₂.sup f := sup_le $ assume b hb, le_sup (h hb) @[simp] lemma sup_lt_iff [is_total α (≤)] {a : α} (ha : ⊥ < a) : s.sup f < a ↔ (∀b ∈ s, f b < a) := by letI := classical.dec_eq β; from ⟨ λh b hb, lt_of_le_of_lt (le_sup hb) h, finset.induction_on s (by simp [ha]) (by simp {contextual := tt}) ⟩ lemma comp_sup_eq_sup_comp [is_total α (≤)] {γ : Type} [semilattice_sup_bot γ] (g : α → γ) (mono_g : monotone g) (bot : g ⊥ = ⊥) : g (s.sup f) = s.sup (g ∘ f) := have A : ∀x y, g (x ⊔ y) = g x ⊔ g y := begin assume x y, cases (@is_total.total _ (≤) _ x y) with h, { simp [sup_of_le_right h, sup_of_le_right (mono_g h)] }, { simp [sup_of_le_left h, sup_of_le_left (mono_g h)] } end, by letI := classical.dec_eq β; from finset.induction_on s (by simp [bot]) (by simp [A] {contextual := tt}) theorem subset_range_sup_succ (s : finset ℕ) : s ⊆ range (s.sup id).succ := λ n hn, mem_range.2 $ nat.lt_succ_of_le $ le_sup hn theorem exists_nat_subset_range (s : finset ℕ) : ∃n : ℕ, s ⊆ range n := ⟨_, s.subset_range_sup_succ⟩ end sup lemma sup_eq_supr [complete_lattice β] (s : finset α) (f : α → β) : s.sup f = (⨆a∈s, f a) := le_antisymm (finset.sup_le $ assume a ha, le_supr_of_le a $ le_supr _ ha) (supr_le $ assume a, supr_le $ assume ha, le_sup ha) /-! ### inf -/ section inf variables [semilattice_inf_top α] /-- Infimum of a finite set: `inf {a, b, c} f = f a ⊓ f b ⊓ f c` -/ def inf (s : finset β) (f : β → α) : α := s.fold (⊓) ⊤ f variables {s s₁ s₂ : finset β} {f : β → α} lemma inf_val : s.inf f = (s.1.map f).inf := rfl @[simp] lemma inf_empty : (∅ : finset β).inf f = ⊤ := fold_empty lemma le_inf_iff {a : α} : a ≤ s.inf f ↔ ∀b ∈ s, a ≤ f b := begin apply iff.trans multiset.le_inf, refine ⟨λ k b hb, k (f b) (multiset.mem_map_of_mem _ hb), λ k b hb, _⟩, rw multiset.mem_map at hb, rcases hb with ⟨a', ha', rfl⟩, apply k a' ha' end @[simp] lemma inf_insert [decidable_eq β] {b : β} : (insert b s : finset β).inf f = f b ⊓ s.inf f := fold_insert_idem @[simp] lemma inf_singleton {b : β} : ({b} : finset β).inf f = f b := inf_singleton lemma inf_union [decidable_eq β] : (s₁ ∪ s₂).inf f = s₁.inf f ⊓ s₂.inf f := finset.induction_on s₁ (by rw [empty_union, inf_empty, top_inf_eq]) $ λ a s has ih, by rw [insert_union, inf_insert, inf_insert, ih, inf_assoc] theorem inf_congr {f g : β → α} (hs : s₁ = s₂) (hfg : ∀a∈s₂, f a = g a) : s₁.inf f = s₂.inf g := by subst hs; exact finset.fold_congr hfg lemma inf_le {b : β} (hb : b ∈ s) : s.inf f ≤ f b := le_inf_iff.1 (le_refl _) _ hb lemma le_inf {a : α} : (∀b ∈ s, a ≤ f b) → a ≤ s.inf f := le_inf_iff.2 lemma inf_mono_fun {g : β → α} (h : ∀b∈s, f b ≤ g b) : s.inf f ≤ s.inf g := le_inf (λ b hb, le_trans (inf_le hb) (h b hb)) lemma inf_mono (h : s₁ ⊆ s₂) : s₂.inf f ≤ s₁.inf f := le_inf $ assume b hb, inf_le (h hb) lemma lt_inf [is_total α (≤)] {a : α} : (a < ⊤) → (∀b ∈ s, a < f b) → a < s.inf f := by letI := classical.dec_eq β; from finset.induction_on s (by simp) (by simp {contextual := tt}) lemma comp_inf_eq_inf_comp [is_total α (≤)] {γ : Type} [semilattice_inf_top γ] (g : α → γ) (mono_g : monotone g) (top : g ⊤ = ⊤) : g (s.inf f) = s.inf (g ∘ f) := have A : ∀x y, g (x ⊓ y) = g x ⊓ g y := begin assume x y, cases (@is_total.total _ (≤) _ x y) with h, { simp [inf_of_le_left h, inf_of_le_left (mono_g h)] }, { simp [inf_of_le_right h, inf_of_le_right (mono_g h)] } end, by letI := classical.dec_eq β; from finset.induction_on s (by simp [top]) (by simp [A] {contextual := tt}) end inf lemma inf_eq_infi [complete_lattice β] (s : finset α) (f : α → β) : s.inf f = (⨅a∈s, f a) := le_antisymm (le_infi $ assume a, le_infi $ assume ha, inf_le ha) (finset.le_inf $ assume a ha, infi_le_of_le a $ infi_le _ ha) /-! ### max and min of finite sets -/ section max_min variables [decidable_linear_order α] /-- Let `s` be a finset in a linear order. Then `s.max` is the maximum of `s` if `s` is not empty, and `none` otherwise. It belongs to `option α`. If you want to get an element of `α`, see `s.max'`. -/ protected def max : finset α → option α := fold (option.lift_or_get max) none some theorem max_eq_sup_with_bot (s : finset α) : s.max = @sup (with_bot α) α _ s some := rfl @[simp] theorem max_empty : (∅ : finset α).max = none := rfl @[simp] theorem max_insert {a : α} {s : finset α} : (insert a s).max = option.lift_or_get max (some a) s.max := fold_insert_idem @[simp] theorem max_singleton {a : α} : finset.max {a} = some a := by { rw [← insert_emptyc_eq], exact max_insert } theorem max_of_mem {s : finset α} {a : α} (h : a ∈ s) : ∃ b, b ∈ s.max := (@le_sup (with_bot α) _ _ _ _ _ h _ rfl).imp $ λ b, Exists.fst theorem max_of_nonempty {s : finset α} (h : s.nonempty) : ∃ a, a ∈ s.max := let ⟨a, ha⟩ := h in max_of_mem ha theorem max_eq_none {s : finset α} : s.max = none ↔ s = ∅ := ⟨λ h, s.eq_empty_or_nonempty.elim id (λ H, let ⟨a, ha⟩ := max_of_nonempty H in by rw h at ha; cases ha), λ h, h.symm ▸ max_empty⟩ theorem mem_of_max {s : finset α} : ∀ {a : α}, a ∈ s.max → a ∈ s := finset.induction_on s (λ _ H, by cases H) (λ b s _ (ih : ∀ {a}, a ∈ s.max → a ∈ s) a (h : a ∈ (insert b s).max), begin by_cases p : b = a, { induction p, exact mem_insert_self b s }, { cases option.lift_or_get_choice max_choice (some b) s.max with q q; rw [max_insert, q] at h, { cases h, cases p rfl }, { exact mem_insert_of_mem (ih h) } } end) theorem le_max_of_mem {s : finset α} {a b : α} (h₁ : a ∈ s) (h₂ : b ∈ s.max) : a ≤ b := by rcases @le_sup (with_bot α) _ _ _ _ _ h₁ _ rfl with ⟨b', hb, ab⟩; cases h₂.symm.trans hb; assumption /-- Let `s` be a finset in a linear order. Then `s.min` is the minimum of `s` if `s` is not empty, and `none` otherwise. It belongs to `option α`. If you want to get an element of `α`, see `s.min'`. -/ protected def min : finset α → option α := fold (option.lift_or_get min) none some theorem min_eq_inf_with_top (s : finset α) : s.min = @inf (with_top α) α _ s some := rfl @[simp] theorem min_empty : (∅ : finset α).min = none := rfl @[simp] theorem min_insert {a : α} {s : finset α} : (insert a s).min = option.lift_or_get min (some a) s.min := fold_insert_idem @[simp] theorem min_singleton {a : α} : finset.min {a} = some a := by { rw ← insert_emptyc_eq, exact min_insert } theorem min_of_mem {s : finset α} {a : α} (h : a ∈ s) : ∃ b, b ∈ s.min := (@inf_le (with_top α) _ _ _ _ _ h _ rfl).imp $ λ b, Exists.fst theorem min_of_nonempty {s : finset α} (h : s.nonempty) : ∃ a, a ∈ s.min := let ⟨a, ha⟩ := h in min_of_mem ha theorem min_eq_none {s : finset α} : s.min = none ↔ s = ∅ := ⟨λ h, s.eq_empty_or_nonempty.elim id (λ H, let ⟨a, ha⟩ := min_of_nonempty H in by rw h at ha; cases ha), λ h, h.symm ▸ min_empty⟩ theorem mem_of_min {s : finset α} : ∀ {a : α}, a ∈ s.min → a ∈ s := finset.induction_on s (λ _ H, by cases H) $ λ b s _ (ih : ∀ {a}, a ∈ s.min → a ∈ s) a (h : a ∈ (insert b s).min), begin by_cases p : b = a, { induction p, exact mem_insert_self b s }, { cases option.lift_or_get_choice min_choice (some b) s.min with q q; rw [min_insert, q] at h, { cases h, cases p rfl }, { exact mem_insert_of_mem (ih h) } } end theorem min_le_of_mem {s : finset α} {a b : α} (h₁ : b ∈ s) (h₂ : a ∈ s.min) : a ≤ b := by rcases @inf_le (with_top α) _ _ _ _ _ h₁ _ rfl with ⟨b', hb, ab⟩; cases h₂.symm.trans hb; assumption /-- Given a nonempty finset `s` in a linear order `α `, then `s.min' h` is its minimum, as an element of `α`, where `h` is a proof of nonemptiness. Without this assumption, use instead `s.min`, taking values in `option α`. -/ def min' (s : finset α) (H : s.nonempty) : α := @option.get _ s.min $ let ⟨k, hk⟩ := H in let ⟨b, hb⟩ := min_of_mem hk in by simp at hb; simp [hb] /-- Given a nonempty finset `s` in a linear order `α `, then `s.max' h` is its maximum, as an element of `α`, where `h` is a proof of nonemptiness. Without this assumption, use instead `s.max`, taking values in `option α`. -/ def max' (s : finset α) (H : s.nonempty) : α := @option.get _ s.max $ let ⟨k, hk⟩ := H in let ⟨b, hb⟩ := max_of_mem hk in by simp at hb; simp [hb] variables (s : finset α) (H : s.nonempty) theorem min'_mem : s.min' H ∈ s := mem_of_min $ by simp [min'] theorem min'_le (x) (H2 : x ∈ s) : s.min' H ≤ x := min_le_of_mem H2 $ option.get_mem _ theorem le_min' (x) (H2 : ∀ y ∈ s, x ≤ y) : x ≤ s.min' H := H2 _ $ min'_mem _ _ theorem max'_mem : s.max' H ∈ s := mem_of_max $ by simp [max'] theorem le_max' (x) (H2 : x ∈ s) : x ≤ s.max' H := le_max_of_mem H2 $ option.get_mem _ theorem max'_le (x) (H2 : ∀ y ∈ s, y ≤ x) : s.max' H ≤ x := H2 _ $ max'_mem _ _ theorem min'_lt_max' {i j} (H1 : i ∈ s) (H2 : j ∈ s) (H3 : i ≠ j) : s.min' H < s.max' H := begin rcases lt_trichotomy i j with H4 | H4 | H4, { have H5 := min'_le s H i H1, have H6 := le_max' s H j H2, apply lt_of_le_of_lt H5, apply lt_of_lt_of_le H4 H6 }, { cc }, { have H5 := min'_le s H j H2, have H6 := le_max' s H i H1, apply lt_of_le_of_lt H5, apply lt_of_lt_of_le H4 H6 } end /-- If there's more than 1 element, the min' is less than the max'. An alternate version of `min'_lt_max'` which is sometimes more convenient. -/ lemma min'_lt_max'_of_card (h₂ : 1 < card s) : s.min' H < s.max' H := begin apply lt_of_not_ge, intro a, apply not_le_of_lt h₂ (le_of_eq _), rw card_eq_one, use max' s H, rw eq_singleton_iff_unique_mem, refine ⟨max'_mem _ _, λ t Ht, le_antisymm (le_max' s H t Ht) (le_trans a (min'_le s H t Ht))⟩, end end max_min section exists_max_min variables [linear_order α] lemma exists_max_image (s : finset β) (f : β → α) (h : s.nonempty) : ∃ x ∈ s, ∀ x' ∈ s, f x' ≤ f x := begin letI := classical.DLO α, cases max_of_nonempty (h.image f) with y hy, rcases mem_image.mp (mem_of_max hy) with ⟨x, hx, rfl⟩, exact ⟨x, hx, λ x' hx', le_max_of_mem (mem_image_of_mem f hx') hy⟩, end lemma exists_min_image (s : finset β) (f : β → α) (h : s.nonempty) : ∃ x ∈ s, ∀ x' ∈ s, f x ≤ f x' := begin letI := classical.DLO α, cases min_of_nonempty (h.image f) with y hy, rcases mem_image.mp (mem_of_min hy) with ⟨x, hx, rfl⟩, exact ⟨x, hx, λ x' hx', min_le_of_mem (mem_image_of_mem f hx') hy⟩ end end exists_max_min /-! ### sort -/ section sort variables (r : α → α → Prop) [decidable_rel r] [is_trans α r] [is_antisymm α r] [is_total α r] /-- `sort s` constructs a sorted list from the unordered set `s`. (Uses merge sort algorithm.) -/ def sort (s : finset α) : list α := sort r s.1 @[simp] theorem sort_sorted (s : finset α) : list.sorted r (sort r s) := sort_sorted _ _ @[simp] theorem sort_eq (s : finset α) : ↑(sort r s) = s.1 := sort_eq _ _ @[simp] theorem sort_nodup (s : finset α) : (sort r s).nodup := (by rw sort_eq; exact s.2 : @multiset.nodup α (sort r s)) @[simp] theorem sort_to_finset [decidable_eq α] (s : finset α) : (sort r s).to_finset = s := list.to_finset_eq (sort_nodup r s) ▸ eq_of_veq (sort_eq r s) @[simp] theorem mem_sort {s : finset α} {a : α} : a ∈ sort r s ↔ a ∈ s := multiset.mem_sort _ @[simp] theorem length_sort {s : finset α} : (sort r s).length = s.card := multiset.length_sort _ end sort section sort_linear_order variables [decidable_linear_order α] theorem sort_sorted_lt (s : finset α) : list.sorted (<) (sort (≤) s) := (sort_sorted _ _).imp₂ (@lt_of_le_of_ne _ _) (sort_nodup _ _) lemma sorted_zero_eq_min' (s : finset α) (h : 0 < (s.sort (≤)).length) (H : s.nonempty) : (s.sort (≤)).nth_le 0 h = s.min' H := begin let l := s.sort (≤), apply le_antisymm, { have : s.min' H ∈ l := (finset.mem_sort (≤)).mpr (s.min'_mem H), obtain ⟨i, i_lt, hi⟩ : ∃ i (hi : i < l.length), l.nth_le i hi = s.min' H := list.mem_iff_nth_le.1 this, rw ← hi, exact list.nth_le_of_sorted_of_le (s.sort_sorted (≤)) (nat.zero_le i) }, { have : l.nth_le 0 h ∈ s := (finset.mem_sort (≤)).1 (list.nth_le_mem l 0 h), exact s.min'_le H _ this } end lemma sorted_last_eq_max' (s : finset α) (h : (s.sort (≤)).length - 1 < (s.sort (≤)).length) (H : s.nonempty) : (s.sort (≤)).nth_le ((s.sort (≤)).length - 1) h = s.max' H := begin let l := s.sort (≤), apply le_antisymm, { have : l.nth_le ((s.sort (≤)).length - 1) h ∈ s := (finset.mem_sort (≤)).1 (list.nth_le_mem l _ h), exact s.le_max' H _ this }, { have : s.max' H ∈ l := (finset.mem_sort (≤)).mpr (s.max'_mem H), obtain ⟨i, i_lt, hi⟩ : ∃ i (hi : i < l.length), l.nth_le i hi = s.max' H := list.mem_iff_nth_le.1 this, rw ← hi, have : i ≤ l.length - 1 := nat.le_pred_of_lt i_lt, exact list.nth_le_of_sorted_of_le (s.sort_sorted (≤)) (nat.le_pred_of_lt i_lt) }, end /-- Given a finset `s` of cardinal `k` in a linear order `α`, the map `mono_of_fin s h` is the increasing bijection between `fin k` and `s` as an `α`-valued map. Here, `h` is a proof that the cardinality of `s` is `k`. We use this instead of a map `fin s.card → α` to avoid casting issues in further uses of this function. -/ def mono_of_fin (s : finset α) {k : ℕ} (h : s.card = k) (i : fin k) : α := have A : (i : ℕ) < (s.sort (≤)).length, by simpa [h] using i.2, (s.sort (≤)).nth_le i A lemma mono_of_fin_strict_mono (s : finset α) {k : ℕ} (h : s.card = k) : strict_mono (s.mono_of_fin h) := begin assume i j hij, exact list.pairwise_iff_nth_le.1 s.sort_sorted_lt _ _ _ hij end lemma mono_of_fin_bij_on (s : finset α) {k : ℕ} (h : s.card = k) : set.bij_on (s.mono_of_fin h) set.univ ↑s := begin have A : ∀ j, j ∈ s ↔ j ∈ (s.sort (≤)) := λ j, by simp, apply set.bij_on.mk, { assume i hi, simp only [mono_of_fin, set.mem_preimage, mem_coe, list.nth_le, A], exact list.nth_le_mem _ _ _ }, { exact ((mono_of_fin_strict_mono s h).injective).inj_on _ }, { assume x hx, simp only [mem_coe, A] at hx, obtain ⟨i, il, hi⟩ : ∃ (i : ℕ) (h : i < (s.sort (≤)).length), (s.sort (≤)).nth_le i h = x := list.nth_le_of_mem hx, simp [h] at il, exact ⟨⟨i, il⟩, set.mem_univ _, hi⟩ } end lemma mono_of_fin_injective (s : finset α) {k : ℕ} (h : s.card = k) : function.injective (s.mono_of_fin h) := set.injective_iff_inj_on_univ.mpr (s.mono_of_fin_bij_on h).inj_on /-- The bijection `mono_of_fin s h` sends `0` to the minimum of `s`. -/ lemma mono_of_fin_zero {s : finset α} {k : ℕ} (h : s.card = k) (hs : s.nonempty) (hz : 0 < k) : mono_of_fin s h ⟨0, hz⟩ = s.min' hs := begin apply le_antisymm, { have : min' s hs ∈ s := min'_mem s hs, rcases (mono_of_fin_bij_on s h).surj_on this with ⟨a, _, ha⟩, rw ← ha, apply (mono_of_fin_strict_mono s h).monotone, exact zero_le a.val }, { have : mono_of_fin s h ⟨0, hz⟩ ∈ s := (mono_of_fin_bij_on s h).maps_to (set.mem_univ _), exact min'_le s hs _ this } end /-- The bijection `mono_of_fin s h` sends `k-1` to the maximum of `s`. -/ lemma mono_of_fin_last {s : finset α} {k : ℕ} (h : s.card = k) (hs : s.nonempty) (hz : 0 < k) : mono_of_fin s h ⟨k-1, buffer.lt_aux_2 hz⟩ = s.max' hs := begin have h'' : k - 1 < k := buffer.lt_aux_2 hz, apply le_antisymm, { have : mono_of_fin s h ⟨k-1, h''⟩ ∈ s := (mono_of_fin_bij_on s h).maps_to (set.mem_univ _), exact le_max' s hs _ this }, { have : max' s hs ∈ s := max'_mem s hs, rcases (mono_of_fin_bij_on s h).surj_on this with ⟨a, _, ha⟩, rw ← ha, apply (mono_of_fin_strict_mono s h).monotone, exact le_pred_of_lt a.2}, end /-- Any increasing bijection between `fin k` and a finset of cardinality `k` has to coincide with the increasing bijection `mono_of_fin s h`. For a statement assuming only that `f` maps `univ` to `s`, see `mono_of_fin_unique'`.-/ lemma mono_of_fin_unique {s : finset α} {k : ℕ} (h : s.card = k) {f : fin k → α} (hbij : set.bij_on f set.univ ↑s) (hmono : strict_mono f) : f = s.mono_of_fin h := begin ext i, rcases i with ⟨i, hi⟩, induction i using nat.strong_induction_on with i IH, rcases lt_trichotomy (f ⟨i, hi⟩) (mono_of_fin s h ⟨i, hi⟩) with H|H|H, { have A : f ⟨i, hi⟩ ∈ ↑s := hbij.maps_to (set.mem_univ _), rcases (mono_of_fin_bij_on s h).surj_on A with ⟨j, _, hj⟩, rw ← hj at H, have ji : j < ⟨i, hi⟩ := (mono_of_fin_strict_mono s h).lt_iff_lt.1 H, have : f j = mono_of_fin s h j, by { convert IH j.1 ji (lt_trans ji hi), rw fin.ext_iff }, rw ← this at hj, exact (ne_of_lt (hmono ji) hj).elim }, { exact H }, { have A : mono_of_fin s h ⟨i, hi⟩ ∈ ↑s := (mono_of_fin_bij_on s h).maps_to (set.mem_univ _), rcases hbij.surj_on A with ⟨j, _, hj⟩, rw ← hj at H, have ji : j < ⟨i, hi⟩ := hmono.lt_iff_lt.1 H, have : f j = mono_of_fin s h j, by { convert IH j.1 ji (lt_trans ji hi), rw fin.ext_iff }, rw this at hj, exact (ne_of_lt (mono_of_fin_strict_mono s h ji) hj).elim } end /-- Two parametrizations `mono_of_fin` of the same set take the same value on `i` and `j` if and only if `i = j`. Since they can be defined on a priori not defeq types `fin k` and `fin l` (although necessarily `k = l`), the conclusion is rather written `i.val = j.val`. -/ @[simp] lemma mono_of_fin_eq_mono_of_fin_iff {k l : ℕ} {s : finset α} {i : fin k} {j : fin l} {h : s.card = k} {h' : s.card = l} : s.mono_of_fin h i = s.mono_of_fin h' j ↔ i.val = j.val := begin have A : k = l, by rw [← h', ← h], have : s.mono_of_fin h = (s.mono_of_fin h') ∘ (λ j : (fin k), ⟨j.1, A ▸ j.2⟩) := rfl, rw [this, function.comp_app, (s.mono_of_fin_injective h').eq_iff, fin.ext_iff] end /-- Given a finset `s` of cardinal `k` in a linear order `α`, the equiv `mono_equiv_of_fin s h` is the increasing bijection between `fin k` and `s` as an `s`-valued map. Here, `h` is a proof that the cardinality of `s` is `k`. We use this instead of a map `fin s.card → α` to avoid casting issues in further uses of this function. -/ noncomputable def mono_equiv_of_fin (s : finset α) {k : ℕ} (h : s.card = k) : fin k ≃ {x // x ∈ s} := (s.mono_of_fin_bij_on h).equiv _ end sort_linear_order /-! ### disjoint -/ section disjoint variable [decidable_eq α] theorem disjoint_left {s t : finset α} : disjoint s t ↔ ∀ {a}, a ∈ s → a ∉ t := by simp only [_root_.disjoint, inf_eq_inter, le_iff_subset, subset_iff, mem_inter, not_and, and_imp]; refl theorem disjoint_val {s t : finset α} : disjoint s t ↔ s.1.disjoint t.1 := disjoint_left theorem disjoint_iff_inter_eq_empty {s t : finset α} : disjoint s t ↔ s ∩ t = ∅ := disjoint_iff instance decidable_disjoint (U V : finset α) : decidable (disjoint U V) := decidable_of_decidable_of_iff (by apply_instance) eq_bot_iff theorem disjoint_right {s t : finset α} : disjoint s t ↔ ∀ {a}, a ∈ t → a ∉ s := by rw [disjoint.comm, disjoint_left] theorem disjoint_iff_ne {s t : finset α} : disjoint s t ↔ ∀ a ∈ s, ∀ b ∈ t, a ≠ b := by simp only [disjoint_left, imp_not_comm, forall_eq'] theorem disjoint_of_subset_left {s t u : finset α} (h : s ⊆ u) (d : disjoint u t) : disjoint s t := disjoint_left.2 (λ x m₁, (disjoint_left.1 d) (h m₁)) theorem disjoint_of_subset_right {s t u : finset α} (h : t ⊆ u) (d : disjoint s u) : disjoint s t := disjoint_right.2 (λ x m₁, (disjoint_right.1 d) (h m₁)) @[simp] theorem disjoint_empty_left (s : finset α) : disjoint ∅ s := disjoint_bot_left @[simp] theorem disjoint_empty_right (s : finset α) : disjoint s ∅ := disjoint_bot_right @[simp] theorem singleton_disjoint {s : finset α} {a : α} : disjoint (singleton a) s ↔ a ∉ s := by simp only [disjoint_left, mem_singleton, forall_eq] @[simp] theorem disjoint_singleton {s : finset α} {a : α} : disjoint s (singleton a) ↔ a ∉ s := disjoint.comm.trans singleton_disjoint @[simp] theorem disjoint_insert_left {a : α} {s t : finset α} : disjoint (insert a s) t ↔ a ∉ t ∧ disjoint s t := by simp only [disjoint_left, mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] @[simp] theorem disjoint_insert_right {a : α} {s t : finset α} : disjoint s (insert a t) ↔ a ∉ s ∧ disjoint s t := disjoint.comm.trans $ by rw [disjoint_insert_left, disjoint.comm] @[simp] theorem disjoint_union_left {s t u : finset α} : disjoint (s ∪ t) u ↔ disjoint s u ∧ disjoint t u := by simp only [disjoint_left, mem_union, or_imp_distrib, forall_and_distrib] @[simp] theorem disjoint_union_right {s t u : finset α} : disjoint s (t ∪ u) ↔ disjoint s t ∧ disjoint s u := by simp only [disjoint_right, mem_union, or_imp_distrib, forall_and_distrib] lemma sdiff_disjoint {s t : finset α} : disjoint (t \ s) s := disjoint_left.2 $ assume a ha, (mem_sdiff.1 ha).2 lemma disjoint_sdiff {s t : finset α} : disjoint s (t \ s) := sdiff_disjoint.symm lemma disjoint_sdiff_inter (s t : finset α) : disjoint (s \ t) (s ∩ t) := disjoint_of_subset_right (inter_subset_right _ _) sdiff_disjoint lemma sdiff_eq_self_iff_disjoint {s t : finset α} : s \ t = s ↔ disjoint s t := by rw [sdiff_eq_self, subset_empty, disjoint_iff_inter_eq_empty] lemma sdiff_eq_self_of_disjoint {s t : finset α} (h : disjoint s t) : s \ t = s := sdiff_eq_self_iff_disjoint.2 h lemma disjoint_self_iff_empty (s : finset α) : disjoint s s ↔ s = ∅ := disjoint_self lemma disjoint_bind_left {ι : Type*} (s : finset ι) (f : ι → finset α) (t : finset α) : disjoint (s.bind f) t ↔ (∀i∈s, disjoint (f i) t) := begin classical, refine s.induction _ _, { simp only [forall_mem_empty_iff, bind_empty, disjoint_empty_left] }, { assume i s his ih, simp only [disjoint_union_left, bind_insert, his, forall_mem_insert, ih] } end lemma disjoint_bind_right {ι : Type*} (s : finset α) (t : finset ι) (f : ι → finset α) : disjoint s (t.bind f) ↔ (∀i∈t, disjoint s (f i)) := by simpa only [disjoint.comm] using disjoint_bind_left t f s @[simp] theorem card_disjoint_union {s t : finset α} (h : disjoint s t) : card (s ∪ t) = card s + card t := by rw [← card_union_add_card_inter, disjoint_iff_inter_eq_empty.1 h, card_empty, add_zero] theorem card_sdiff {s t : finset α} (h : s ⊆ t) : card (t \ s) = card t - card s := suffices card (t \ s) = card ((t \ s) ∪ s) - card s, by rwa sdiff_union_of_subset h at this, by rw [card_disjoint_union sdiff_disjoint, nat.add_sub_cancel] lemma disjoint_filter {s : finset α} {p q : α → Prop} [decidable_pred p] [decidable_pred q] : disjoint (s.filter p) (s.filter q) ↔ (∀ x ∈ s, p x → ¬ q x) := by split; simp [disjoint_left] {contextual := tt} lemma disjoint_filter_filter {s t : finset α} {p q : α → Prop} [decidable_pred p] [decidable_pred q] : (disjoint s t) → disjoint (s.filter p) (t.filter q) := disjoint.mono (filter_subset _) (filter_subset _) lemma pi_disjoint_of_disjoint {δ : α → Type*} [∀a, decidable_eq (δ a)] {s : finset α} [decidable_eq (Πa∈s, δ a)] (t₁ t₂ : Πa, finset (δ a)) {a : α} (ha : a ∈ s) (h : disjoint (t₁ a) (t₂ a)) : disjoint (s.pi t₁) (s.pi t₂) := disjoint_iff_ne.2 $ λ f₁ hf₁ f₂ hf₂ eq₁₂, disjoint_iff_ne.1 h (f₁ a ha) (mem_pi.mp hf₁ a ha) (f₂ a ha) (mem_pi.mp hf₂ a ha) $ congr_fun (congr_fun eq₁₂ a) ha lemma disjoint_iff_disjoint_coe {α : Type*} {a b : finset α} [decidable_eq α] : disjoint a b ↔ disjoint (↑a : set α) (↑b : set α) := by { rw [finset.disjoint_left, set.disjoint_left], refl } end disjoint /-- Given a set A and a set B inside it, we can shrink A to any appropriate size, and keep B inside it. -/ lemma exists_intermediate_set {A B : finset α} (i : ℕ) (h₁ : i + card B ≤ card A) (h₂ : B ⊆ A) : ∃ (C : finset α), B ⊆ C ∧ C ⊆ A ∧ card C = i + card B := begin classical, rcases nat.le.dest h₁ with ⟨k, _⟩, clear h₁, induction k with k ih generalizing A, { exact ⟨A, h₂, subset.refl _, h.symm⟩ }, { have : (A \ B).nonempty, { rw [← card_pos, card_sdiff h₂, ← h, nat.add_right_comm, nat.add_sub_cancel, nat.add_succ], apply nat.succ_pos }, rcases this with ⟨a, ha⟩, have z : i + card B + k = card (erase A a), { rw [card_erase_of_mem, ← h, nat.add_succ, nat.pred_succ], rw mem_sdiff at ha, exact ha.1 }, rcases ih _ z with ⟨B', hB', B'subA', cards⟩, { exact ⟨B', hB', trans B'subA' (erase_subset _ _), cards⟩ }, { rintros t th, apply mem_erase_of_ne_of_mem _ (h₂ th), rintro rfl, exact not_mem_sdiff_of_mem_right th ha } } end /-- We can shrink A to any smaller size. -/ lemma exists_smaller_set (A : finset α) (i : ℕ) (h₁ : i ≤ card A) : ∃ (B : finset α), B ⊆ A ∧ card B = i := let ⟨B, _, x₁, x₂⟩ := exists_intermediate_set i (by simpa) (empty_subset A) in ⟨B, x₁, x₂⟩ instance [has_repr α] : has_repr (finset α) := ⟨λ s, repr s.1⟩ /-- Given a finset `s` of `ℕ` contained in `{0,..., n-1}`, the corresponding finset in `fin n` is `s.attach_fin h` where `h` is a proof that all elements of `s` are less than `n`. -/ def attach_fin (s : finset ℕ) {n : ℕ} (h : ∀ m ∈ s, m < n) : finset (fin n) := ⟨s.1.pmap (λ a ha, ⟨a, ha⟩) h, multiset.nodup_pmap (λ _ _ _ _, fin.mk.inj) s.2⟩ @[simp] lemma mem_attach_fin {n : ℕ} {s : finset ℕ} (h : ∀ m ∈ s, m < n) {a : fin n} : a ∈ s.attach_fin h ↔ a.1 ∈ s := ⟨λ h, let ⟨b, hb₁, hb₂⟩ := multiset.mem_pmap.1 h in hb₂ ▸ hb₁, λ h, multiset.mem_pmap.2 ⟨a.1, h, fin.eta _ _⟩⟩ @[simp] lemma card_attach_fin {n : ℕ} (s : finset ℕ) (h : ∀ m ∈ s, m < n) : (s.attach_fin h).card = s.card := multiset.card_pmap _ _ _ /-! ### choose -/ section choose variables (p : α → Prop) [decidable_pred p] (l : finset α) /-- Given a finset `l` and a predicate `p`, associate to a proof that there is a unique element of `l` satisfying `p` this unique element, as an element of the corresponding subtype. -/ def choose_x (hp : (∃! a, a ∈ l ∧ p a)) : { a // a ∈ l ∧ p a } := multiset.choose_x p l.val hp /-- Given a finset `l` and a predicate `p`, associate to a proof that there is a unique element of `l` satisfying `p` this unique element, as an element of the ambient type. -/ def choose (hp : ∃! a, a ∈ l ∧ p a) : α := choose_x p l hp lemma choose_spec (hp : ∃! a, a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) := (choose_x p l hp).property lemma choose_mem (hp : ∃! a, a ∈ l ∧ p a) : choose p l hp ∈ l := (choose_spec _ _ _).1 lemma choose_property (hp : ∃! a, a ∈ l ∧ p a) : p (choose p l hp) := (choose_spec _ _ _).2 end choose theorem lt_wf {α} : well_founded (@has_lt.lt (finset α) _) := have H : subrelation (@has_lt.lt (finset α) _) (inv_image (<) card), from λ x y hxy, card_lt_card hxy, subrelation.wf H $ inv_image.wf _ $ nat.lt_wf /-! ### intervals -/ /- Ico (a closed open interval) -/ variables {n m l : ℕ} /-- `Ico n m` is the set of natural numbers `n ≤ k < m`. -/ def Ico (n m : ℕ) : finset ℕ := ⟨_, Ico.nodup n m⟩ namespace Ico @[simp] theorem val (n m : ℕ) : (Ico n m).1 = multiset.Ico n m := rfl @[simp] theorem to_finset (n m : ℕ) : (multiset.Ico n m).to_finset = Ico n m := (multiset.to_finset_eq _).symm theorem image_add (n m k : ℕ) : (Ico n m).image ((+) k) = Ico (n + k) (m + k) := by simp [image, multiset.Ico.map_add] theorem image_sub (n m k : ℕ) (h : k ≤ n) : (Ico n m).image (λ x, x - k) = Ico (n - k) (m - k) := begin dsimp [image], rw [multiset.Ico.map_sub _ _ _ h, ←multiset.to_finset_eq], refl, end theorem zero_bot (n : ℕ) : Ico 0 n = range n := eq_of_veq $ multiset.Ico.zero_bot _ @[simp] theorem card (n m : ℕ) : (Ico n m).card = m - n := multiset.Ico.card _ _ @[simp] theorem mem {n m l : ℕ} : l ∈ Ico n m ↔ n ≤ l ∧ l < m := multiset.Ico.mem theorem eq_empty_of_le {n m : ℕ} (h : m ≤ n) : Ico n m = ∅ := eq_of_veq $ multiset.Ico.eq_zero_of_le h @[simp] theorem self_eq_empty (n : ℕ) : Ico n n = ∅ := eq_empty_of_le $ le_refl n @[simp] theorem eq_empty_iff {n m : ℕ} : Ico n m = ∅ ↔ m ≤ n := iff.trans val_eq_zero.symm multiset.Ico.eq_zero_iff theorem subset_iff {m₁ n₁ m₂ n₂ : ℕ} (hmn : m₁ < n₁) : Ico m₁ n₁ ⊆ Ico m₂ n₂ ↔ (m₂ ≤ m₁ ∧ n₁ ≤ n₂) := begin simp only [subset_iff, mem], refine ⟨λ h, ⟨_, _⟩, _⟩, { exact (h ⟨le_refl _, hmn⟩).1 }, { refine le_of_pred_lt (@h (pred n₁) ⟨le_pred_of_lt hmn, pred_lt _⟩).2, exact ne_of_gt (lt_of_le_of_lt (nat.zero_le m₁) hmn) }, { rintros ⟨hm, hn⟩ k ⟨hmk, hkn⟩, exact ⟨le_trans hm hmk, lt_of_lt_of_le hkn hn⟩ } end protected theorem subset {m₁ n₁ m₂ n₂ : ℕ} (hmm : m₂ ≤ m₁) (hnn : n₁ ≤ n₂) : Ico m₁ n₁ ⊆ Ico m₂ n₂ := begin simp only [finset.subset_iff, Ico.mem], assume x hx, exact ⟨le_trans hmm hx.1, lt_of_lt_of_le hx.2 hnn⟩ end lemma union_consecutive {n m l : ℕ} (hnm : n ≤ m) (hml : m ≤ l) : Ico n m ∪ Ico m l = Ico n l := by rw [← to_finset, ← to_finset, ← multiset.to_finset_add, multiset.Ico.add_consecutive hnm hml, to_finset] @[simp] lemma inter_consecutive (n m l : ℕ) : Ico n m ∩ Ico m l = ∅ := begin rw [← to_finset, ← to_finset, ← multiset.to_finset_inter, multiset.Ico.inter_consecutive], simp, end lemma disjoint_consecutive (n m l : ℕ) : disjoint (Ico n m) (Ico m l) := le_of_eq $ inter_consecutive n m l @[simp] theorem succ_singleton (n : ℕ) : Ico n (n+1) = {n} := eq_of_veq $ multiset.Ico.succ_singleton theorem succ_top {n m : ℕ} (h : n ≤ m) : Ico n (m + 1) = insert m (Ico n m) := by rw [← to_finset, multiset.Ico.succ_top h, multiset.to_finset_cons, to_finset] theorem succ_top' {n m : ℕ} (h : n < m) : Ico n m = insert (m - 1) (Ico n (m - 1)) := begin have w : m = m - 1 + 1 := (nat.sub_add_cancel (nat.one_le_of_lt h)).symm, conv { to_lhs, rw w }, rw succ_top, exact nat.le_pred_of_lt h end theorem insert_succ_bot {n m : ℕ} (h : n < m) : insert n (Ico (n + 1) m) = Ico n m := by rw [eq_comm, ← to_finset, multiset.Ico.eq_cons h, multiset.to_finset_cons, to_finset] @[simp] theorem pred_singleton {m : ℕ} (h : 0 < m) : Ico (m - 1) m = {m - 1} := eq_of_veq $ multiset.Ico.pred_singleton h @[simp] theorem not_mem_top {n m : ℕ} : m ∉ Ico n m := multiset.Ico.not_mem_top lemma filter_lt_of_top_le {n m l : ℕ} (hml : m ≤ l) : (Ico n m).filter (λ x, x < l) = Ico n m := eq_of_veq $ multiset.Ico.filter_lt_of_top_le hml lemma filter_lt_of_le_bot {n m l : ℕ} (hln : l ≤ n) : (Ico n m).filter (λ x, x < l) = ∅ := eq_of_veq $ multiset.Ico.filter_lt_of_le_bot hln lemma filter_lt_of_ge {n m l : ℕ} (hlm : l ≤ m) : (Ico n m).filter (λ x, x < l) = Ico n l := eq_of_veq $ multiset.Ico.filter_lt_of_ge hlm @[simp] lemma filter_lt (n m l : ℕ) : (Ico n m).filter (λ x, x < l) = Ico n (min m l) := eq_of_veq $ multiset.Ico.filter_lt n m l lemma filter_le_of_le_bot {n m l : ℕ} (hln : l ≤ n) : (Ico n m).filter (λ x, l ≤ x) = Ico n m := eq_of_veq $ multiset.Ico.filter_le_of_le_bot hln lemma filter_le_of_top_le {n m l : ℕ} (hml : m ≤ l) : (Ico n m).filter (λ x, l ≤ x) = ∅ := eq_of_veq $ multiset.Ico.filter_le_of_top_le hml lemma filter_le_of_le {n m l : ℕ} (hnl : n ≤ l) : (Ico n m).filter (λ x, l ≤ x) = Ico l m := eq_of_veq $ multiset.Ico.filter_le_of_le hnl @[simp] lemma filter_le (n m l : ℕ) : (Ico n m).filter (λ x, l ≤ x) = Ico (max n l) m := eq_of_veq $ multiset.Ico.filter_le n m l @[simp] lemma diff_left (l n m : ℕ) : (Ico n m) \ (Ico n l) = Ico (max n l) m := by ext k; by_cases n ≤ k; simp [h, and_comm] @[simp] lemma diff_right (l n m : ℕ) : (Ico n m) \ (Ico l m) = Ico n (min m l) := have ∀k, (k < m ∧ (l ≤ k → m ≤ k)) ↔ (k < m ∧ k < l) := assume k, and_congr_right $ assume hk, by rw [← not_imp_not]; simp [hk], by ext k; by_cases n ≤ k; simp [h, this] lemma image_const_sub {k m n : ℕ} (hkn : k ≤ n) : (Ico k m).image (λ j, n - j) = Ico (n + 1 - m) (n + 1 - k) := begin rw [nat.sub_add_comm hkn], ext j, simp only [mem, mem_image, exists_prop, nat.lt_iff_add_one_le, add_le_add_iff_right], split, { rintros ⟨j, ⟨hjk, hjm⟩, rfl⟩, split, { simp only [← nat.add_sub_add_right n 1 j, nat.sub_le_sub_left, hjm] }, { exact nat.sub_le_sub_left _ hjk } }, { rintros ⟨hm, hk⟩, have hj : j ≤ n := le_trans hk (nat.sub_le_self _ _), refine ⟨n - j, ⟨_, _⟩, _⟩, { apply nat.le_sub_right_of_add_le, rwa nat.le_sub_left_iff_add_le hkn at hk }, { rwa [← nat.sub_add_comm hj, nat.sub_le_iff] }, { exact nat.sub_sub_self hj } } end end Ico lemma range_eq_Ico (n : ℕ) : finset.range n = finset.Ico 0 n := by { ext i, simp } lemma range_image_pred_top_sub (n : ℕ) : (finset.range n).image (λ j, n - 1 - j) = finset.range n := begin cases n, { simp }, { simp [range_eq_Ico, Ico.image_const_sub] } end -- TODO We don't yet attempt to reproduce the entire interface for `Ico` for `Ico_ℤ`. /-- `Ico_ℤ l u` is the set of integers `l ≤ k < u`. -/ def Ico_ℤ (l u : ℤ) : finset ℤ := (finset.range (u - l).to_nat).map { to_fun := λ n, n + l, inj' := λ n m h, by simpa using h } @[simp] lemma Ico_ℤ.mem {n m l : ℤ} : l ∈ Ico_ℤ n m ↔ n ≤ l ∧ l < m := begin dsimp [Ico_ℤ], simp only [int.lt_to_nat, exists_prop, mem_range, add_comm, function.embedding.coe_fn_mk, mem_map], split, { rintro ⟨a, ⟨h, rfl⟩⟩, exact ⟨int.le.intro rfl, lt_sub_iff_add_lt'.mp h⟩ }, { rintro ⟨h₁, h₂⟩, use (l - n).to_nat, split; simp [h₁, h₂], } end @[simp] lemma Ico_ℤ.card (l u : ℤ) : (Ico_ℤ l u).card = (u - l).to_nat := by simp [Ico_ℤ] lemma supr_coe [has_Sup β] (f : α → β) (s : finset α) : (⨆ x ∈ (↑s : set α), f x) = ⨆ x ∈ s, f x := rfl lemma infi_coe [has_Inf β] (f : α → β) (s : finset α) : (⨅ x ∈ (↑s : set α), f x) = ⨅ x ∈ s, f x := rfl end finset namespace multiset lemma count_sup [decidable_eq β] (s : finset α) (f : α → multiset β) (b : β) : count b (s.sup f) = s.sup (λa, count b (f a)) := begin letI := classical.dec_eq α, refine s.induction _ _, { exact count_zero _ }, { assume i s his ih, rw [finset.sup_insert, sup_eq_union, count_union, finset.sup_insert, ih], refl } end end multiset namespace list variable [decidable_eq α] theorem to_finset_card_of_nodup {l : list α} (h : l.nodup) : l.to_finset.card = l.length := congr_arg card $ (@multiset.erase_dup_eq_self α _ l).2 h end list section lattice variables {ι : Sort*} [complete_lattice α] lemma supr_eq_supr_finset (s : ι → α) : (⨆i, s i) = (⨆t:finset (plift ι), ⨆i∈t, s (plift.down i)) := begin classical, exact le_antisymm (supr_le $ assume b, le_supr_of_le {plift.up b} $ le_supr_of_le (plift.up b) $ le_supr_of_le (by simp) $ le_refl _) (supr_le $ assume t, supr_le $ assume b, supr_le $ assume hb, le_supr _ _) end lemma infi_eq_infi_finset (s : ι → α) : (⨅i, s i) = (⨅t:finset (plift ι), ⨅i∈t, s (plift.down i)) := begin classical, exact le_antisymm (le_infi $ assume t, le_infi $ assume b, le_infi $ assume hb, infi_le _ _) (le_infi $ assume b, infi_le_of_le {plift.up b} $ infi_le_of_le (plift.up b) $ infi_le_of_le (by simp) $ le_refl _) end end lattice namespace set variables {ι : Sort*} lemma Union_eq_Union_finset (s : ι → set α) : (⋃i, s i) = (⋃t:finset (plift ι), ⋃i∈t, s (plift.down i)) := supr_eq_supr_finset s lemma Inter_eq_Inter_finset (s : ι → set α) : (⋂i, s i) = (⋂t:finset (plift ι), ⋂i∈t, s (plift.down i)) := infi_eq_infi_finset s end set namespace finset namespace nat /-- The antidiagonal of a natural number `n` is the finset of pairs `(i,j)` such that `i+j = n`. -/ def antidiagonal (n : ℕ) : finset (ℕ × ℕ) := (multiset.nat.antidiagonal n).to_finset /-- A pair (i,j) is contained in the antidiagonal of `n` if and only if `i+j=n`. -/ @[simp] lemma mem_antidiagonal {n : ℕ} {x : ℕ × ℕ} : x ∈ antidiagonal n ↔ x.1 + x.2 = n := by rw [antidiagonal, multiset.mem_to_finset, multiset.nat.mem_antidiagonal] /-- The cardinality of the antidiagonal of `n` is `n+1`. -/ @[simp] lemma card_antidiagonal (n : ℕ) : (antidiagonal n).card = n+1 := by simpa using list.to_finset_card_of_nodup (list.nat.nodup_antidiagonal n) /-- The antidiagonal of `0` is the list `[(0,0)]` -/ @[simp] lemma antidiagonal_zero : antidiagonal 0 = {(0, 0)} := by { rw [antidiagonal, multiset.nat.antidiagonal_zero], refl } end nat end finset namespace finset /-! ### bUnion -/ @[simp] theorem bUnion_singleton (a : α) (s : α → set β) : (⋃ x ∈ ({a} : finset α), s x) = s a := by { simp only [set.Union, ← supr_coe], rw coe_singleton, exact supr_singleton } variables [decidable_eq α] theorem supr_union {α} [complete_lattice α] {β} [decidable_eq β] {f : β → α} {s t : finset β} : (⨆ x ∈ s ∪ t, f x) = (⨆x∈s, f x) ⊔ (⨆x∈t, f x) := calc (⨆ x ∈ s ∪ t, f x) = (⨆ x, (⨆h : x∈s, f x) ⊔ (⨆h : x∈t, f x)) : congr_arg _ $ funext $ λ x, by { convert supr_or, rw finset.mem_union, rw finset.mem_union, refl, refl } ... = (⨆x∈s, f x) ⊔ (⨆x∈t, f x) : supr_sup_eq lemma bUnion_union (s t : finset α) (u : α → set β) : (⋃ x ∈ s ∪ t, u x) = (⋃ x ∈ s, u x) ∪ (⋃ x ∈ t, u x) := supr_union @[simp] lemma bUnion_insert (a : α) (s : finset α) (t : α → set β) : (⋃ x ∈ insert a s, t x) = t a ∪ (⋃ x ∈ s, t x) := begin rw insert_eq, simp only [bUnion_union, finset.bUnion_singleton] end end finset
c511386512cfb23105e28977aecf4b256f76a482
367134ba5a65885e863bdc4507601606690974c1
/src/data/multiset/finset_ops.lean
e4b878c6896f579c1d7c12d07c96e653d23afb53
[ "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
8,763
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro -/ import data.multiset.erase_dup /-! # Preparations for defining operations on `finset`. The operations here ignore multiplicities, and preparatory for defining the corresponding operations on `finset`. -/ namespace multiset open list variables {α : Type*} [decidable_eq α] /-! ### finset insert -/ /-- `ndinsert a s` is the lift of the list `insert` operation. This operation does not respect multiplicities, unlike `cons`, but it is suitable as an insert operation on `finset`. -/ def ndinsert (a : α) (s : multiset α) : multiset α := quot.lift_on s (λ l, (l.insert a : multiset α)) (λ s t p, quot.sound (p.insert a)) @[simp] theorem coe_ndinsert (a : α) (l : list α) : ndinsert a l = (insert a l : list α) := rfl @[simp] theorem ndinsert_zero (a : α) : ndinsert a 0 = a ::ₘ 0 := rfl @[simp, priority 980] theorem ndinsert_of_mem {a : α} {s : multiset α} : a ∈ s → ndinsert a s = s := quot.induction_on s $ λ l h, congr_arg coe $ insert_of_mem h @[simp, priority 980] theorem ndinsert_of_not_mem {a : α} {s : multiset α} : a ∉ s → ndinsert a s = a ::ₘ s := quot.induction_on s $ λ l h, congr_arg coe $ insert_of_not_mem h @[simp] theorem mem_ndinsert {a b : α} {s : multiset α} : a ∈ ndinsert b s ↔ a = b ∨ a ∈ s := quot.induction_on s $ λ l, mem_insert_iff @[simp] theorem le_ndinsert_self (a : α) (s : multiset α) : s ≤ ndinsert a s := quot.induction_on s $ λ l, (sublist_of_suffix $ suffix_insert _ _).subperm @[simp] theorem mem_ndinsert_self (a : α) (s : multiset α) : a ∈ ndinsert a s := mem_ndinsert.2 (or.inl rfl) theorem mem_ndinsert_of_mem {a b : α} {s : multiset α} (h : a ∈ s) : a ∈ ndinsert b s := mem_ndinsert.2 (or.inr h) @[simp, priority 980] theorem length_ndinsert_of_mem {a : α} {s : multiset α} (h : a ∈ s) : card (ndinsert a s) = card s := by simp [h] @[simp, priority 980] theorem length_ndinsert_of_not_mem {a : α} {s : multiset α} (h : a ∉ s) : card (ndinsert a s) = card s + 1 := by simp [h] theorem erase_dup_cons {a : α} {s : multiset α} : erase_dup (a ::ₘ s) = ndinsert a (erase_dup s) := by by_cases a ∈ s; simp [h] theorem nodup_ndinsert (a : α) {s : multiset α} : nodup s → nodup (ndinsert a s) := quot.induction_on s $ λ l, nodup_insert theorem ndinsert_le {a : α} {s t : multiset α} : ndinsert a s ≤ t ↔ s ≤ t ∧ a ∈ t := ⟨λ h, ⟨le_trans (le_ndinsert_self _ _) h, mem_of_le h (mem_ndinsert_self _ _)⟩, λ ⟨l, m⟩, if h : a ∈ s then by simp [h, l] else by rw [ndinsert_of_not_mem h, ← cons_erase m, cons_le_cons_iff, ← le_cons_of_not_mem h, cons_erase m]; exact l⟩ lemma attach_ndinsert (a : α) (s : multiset α) : (s.ndinsert a).attach = ndinsert ⟨a, mem_ndinsert_self a s⟩ (s.attach.map $ λp, ⟨p.1, mem_ndinsert_of_mem p.2⟩) := have eq : ∀h : ∀(p : {x // x ∈ s}), p.1 ∈ s, (λ (p : {x // x ∈ s}), ⟨p.val, h p⟩ : {x // x ∈ s} → {x // x ∈ s}) = id, from assume h, funext $ assume p, subtype.eq rfl, have ∀t (eq : s.ndinsert a = t), t.attach = ndinsert ⟨a, eq ▸ mem_ndinsert_self a s⟩ (s.attach.map $ λp, ⟨p.1, eq ▸ mem_ndinsert_of_mem p.2⟩), begin intros t ht, by_cases a ∈ s, { rw [ndinsert_of_mem h] at ht, subst ht, rw [eq, map_id, ndinsert_of_mem (mem_attach _ _)] }, { rw [ndinsert_of_not_mem h] at ht, subst ht, simp [attach_cons, h] } end, this _ rfl @[simp] theorem disjoint_ndinsert_left {a : α} {s t : multiset α} : disjoint (ndinsert a s) t ↔ a ∉ t ∧ disjoint s t := iff.trans (by simp [disjoint]) disjoint_cons_left @[simp] theorem disjoint_ndinsert_right {a : α} {s t : multiset α} : disjoint s (ndinsert a t) ↔ a ∉ s ∧ disjoint s t := by rw [disjoint_comm, disjoint_ndinsert_left]; tauto /-! ### finset union -/ /-- `ndunion s t` is the lift of the list `union` operation. This operation does not respect multiplicities, unlike `s ∪ t`, but it is suitable as a union operation on `finset`. (`s ∪ t` would also work as a union operation on finset, but this is more efficient.) -/ def ndunion (s t : multiset α) : multiset α := quotient.lift_on₂ s t (λ l₁ l₂, (l₁.union l₂ : multiset α)) $ λ v₁ v₂ w₁ w₂ p₁ p₂, quot.sound $ p₁.union p₂ @[simp] theorem coe_ndunion (l₁ l₂ : list α) : @ndunion α _ l₁ l₂ = (l₁ ∪ l₂ : list α) := rfl @[simp] theorem zero_ndunion (s : multiset α) : ndunion 0 s = s := quot.induction_on s $ λ l, rfl @[simp] theorem cons_ndunion (s t : multiset α) (a : α) : ndunion (a ::ₘ s) t = ndinsert a (ndunion s t) := quotient.induction_on₂ s t $ λ l₁ l₂, rfl @[simp] theorem mem_ndunion {s t : multiset α} {a : α} : a ∈ ndunion s t ↔ a ∈ s ∨ a ∈ t := quotient.induction_on₂ s t $ λ l₁ l₂, list.mem_union theorem le_ndunion_right (s t : multiset α) : t ≤ ndunion s t := quotient.induction_on₂ s t $ λ l₁ l₂, (sublist_of_suffix $ suffix_union_right _ _).subperm theorem subset_ndunion_right (s t : multiset α) : t ⊆ ndunion s t := subset_of_le (le_ndunion_right s t) theorem ndunion_le_add (s t : multiset α) : ndunion s t ≤ s + t := quotient.induction_on₂ s t $ λ l₁ l₂, (union_sublist_append _ _).subperm theorem ndunion_le {s t u : multiset α} : ndunion s t ≤ u ↔ s ⊆ u ∧ t ≤ u := multiset.induction_on s (by simp) (by simp [ndinsert_le, and_comm, and.left_comm] {contextual := tt}) theorem subset_ndunion_left (s t : multiset α) : s ⊆ ndunion s t := λ a h, mem_ndunion.2 $ or.inl h theorem le_ndunion_left {s} (t : multiset α) (d : nodup s) : s ≤ ndunion s t := (le_iff_subset d).2 $ subset_ndunion_left _ _ theorem ndunion_le_union (s t : multiset α) : ndunion s t ≤ s ∪ t := ndunion_le.2 ⟨subset_of_le (le_union_left _ _), le_union_right _ _⟩ theorem nodup_ndunion (s : multiset α) {t : multiset α} : nodup t → nodup (ndunion s t) := quotient.induction_on₂ s t $ λ l₁ l₂, list.nodup_union _ @[simp, priority 980] theorem ndunion_eq_union {s t : multiset α} (d : nodup s) : ndunion s t = s ∪ t := le_antisymm (ndunion_le_union _ _) $ union_le (le_ndunion_left _ d) (le_ndunion_right _ _) theorem erase_dup_add (s t : multiset α) : erase_dup (s + t) = ndunion s (erase_dup t) := quotient.induction_on₂ s t $ λ l₁ l₂, congr_arg coe $ erase_dup_append _ _ /-! ### finset inter -/ /-- `ndinter s t` is the lift of the list `∩` operation. This operation does not respect multiplicities, unlike `s ∩ t`, but it is suitable as an intersection operation on `finset`. (`s ∩ t` would also work as a union operation on finset, but this is more efficient.) -/ def ndinter (s t : multiset α) : multiset α := filter (∈ t) s @[simp] theorem coe_ndinter (l₁ l₂ : list α) : @ndinter α _ l₁ l₂ = (l₁ ∩ l₂ : list α) := rfl @[simp] theorem zero_ndinter (s : multiset α) : ndinter 0 s = 0 := rfl @[simp, priority 980] theorem cons_ndinter_of_mem {a : α} (s : multiset α) {t : multiset α} (h : a ∈ t) : ndinter (a ::ₘ s) t = a ::ₘ (ndinter s t) := by simp [ndinter, h] @[simp, priority 980] theorem ndinter_cons_of_not_mem {a : α} (s : multiset α) {t : multiset α} (h : a ∉ t) : ndinter (a ::ₘ s) t = ndinter s t := by simp [ndinter, h] @[simp] theorem mem_ndinter {s t : multiset α} {a : α} : a ∈ ndinter s t ↔ a ∈ s ∧ a ∈ t := mem_filter @[simp] theorem nodup_ndinter {s : multiset α} (t : multiset α) : nodup s → nodup (ndinter s t) := nodup_filter _ theorem le_ndinter {s t u : multiset α} : s ≤ ndinter t u ↔ s ≤ t ∧ s ⊆ u := by simp [ndinter, le_filter, subset_iff] theorem ndinter_le_left (s t : multiset α) : ndinter s t ≤ s := (le_ndinter.1 (le_refl _)).1 theorem ndinter_subset_left (s t : multiset α) : ndinter s t ⊆ s := subset_of_le (ndinter_le_left s t) theorem ndinter_subset_right (s t : multiset α) : ndinter s t ⊆ t := (le_ndinter.1 (le_refl _)).2 theorem ndinter_le_right {s} (t : multiset α) (d : nodup s) : ndinter s t ≤ t := (le_iff_subset $ nodup_ndinter _ d).2 (ndinter_subset_right _ _) theorem inter_le_ndinter (s t : multiset α) : s ∩ t ≤ ndinter s t := le_ndinter.2 ⟨inter_le_left _ _, subset_of_le $ inter_le_right _ _⟩ @[simp, priority 980] theorem ndinter_eq_inter {s t : multiset α} (d : nodup s) : ndinter s t = s ∩ t := le_antisymm (le_inter (ndinter_le_left _ _) (ndinter_le_right _ d)) (inter_le_ndinter _ _) theorem ndinter_eq_zero_iff_disjoint {s t : multiset α} : ndinter s t = 0 ↔ disjoint s t := by rw ← subset_zero; simp [subset_iff, disjoint] end multiset
48859ad8f7542b1fcd68d861f4d2068be13fe6d3
22e97a5d648fc451e25a06c668dc03ac7ed7bc25
/src/linear_algebra/sesquilinear_form.lean
4a13035afd44631f4ba5e68380c45b7a8054df9f
[ "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
8,684
lean
/- Copyright (c) 2018 Andreas Swerdlow. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Andreas Swerdlow -/ import algebra.module import ring_theory.maps /-! # Sesquilinear form This file defines a sesquilinear form over a module. The definition requires a ring antiautomorphism on the scalar ring, which comes from the file ring_theory.involution. Basic ideas such as orthogonality are also introduced. A sesquilinear form on an R-module M, is a function from M x M to R, that is linear in the first argument and antilinear in the second, with respect to an antiautomorphism on R (an antiisomorphism from R to R). ## Notations Given any term S of type sesq_form, due to a coercion, can use the notation S x y to refer to the function field, ie. `S x y = S.sesq x y`. ## References * <https://en.wikipedia.org/wiki/Sesquilinear_form#Over_arbitrary_rings> ## Tags Sesquilinear form, -/ open ring_anti_equiv universes u v /-- A sesquilinear form over a module -/ structure sesq_form (R : Type u) (M : Type v) [ring R] (I : ring_anti_equiv R R) [add_comm_group M] [module R M] := (sesq : M → M → R) (sesq_add_left : ∀ (x y z : M), sesq (x + y) z = sesq x z + sesq y z) (sesq_smul_left : ∀ (a : R) (x y : M), sesq (a • x) y = a * (sesq x y)) (sesq_add_right : ∀ (x y z : M), sesq x (y + z) = sesq x y + sesq x z) (sesq_smul_right : ∀ (a : R) (x y : M), sesq x (a • y) = (I a) * (sesq x y)) namespace sesq_form section general_ring variables {R : Type u} {M : Type v} [ring R] [add_comm_group M] [module R M] {I : ring_anti_equiv R R} {S : sesq_form R M I} instance : has_coe_to_fun (sesq_form R M I) := ⟨_, λ S, S.sesq⟩ lemma add_left (x y z : M) : S (x + y) z = S x z + S y z := sesq_add_left S x y z lemma smul_left (a : R) (x y : M) : S (a • x) y = a * (S x y) := sesq_smul_left S a x y lemma add_right (x y z : M) : S x (y + z) = S x y + S x z := sesq_add_right S x y z lemma smul_right (a : R) (x y : M) : S x (a • y) = (I a) * (S x y) := sesq_smul_right S a x y lemma zero_left (x : M) : S 0 x = 0 := by {rw [←@zero_smul R _ _ _ _ (0 : M), smul_left, zero_mul]} lemma zero_right (x : M) : S x 0 = 0 := by rw [←@zero_smul _ _ _ _ _ (0 : M), smul_right, map_zero, ring.zero_mul] lemma neg_left (x y : M) : S (-x) y = -(S x y) := by rw [←@neg_one_smul R _ _, smul_left, neg_one_mul] lemma neg_right (x y : M) : S x (-y) = -(S x y) := by rw [←@neg_one_smul R _ _, smul_right, map_neg_one, neg_one_mul] lemma sub_left (x y z : M) : S (x - y) z = S x z - S y z := by rw [sub_eq_add_neg, add_left, neg_left]; refl lemma sub_right (x y z : M) : S x (y - z) = S x y - S x z := by rw [sub_eq_add_neg, add_right, neg_right]; refl variable {D : sesq_form R M I} @[ext] lemma ext (H : ∀ (x y : M), S x y = D x y) : S = D := by {cases S, cases D, congr, funext, exact H _ _} instance : add_comm_group (sesq_form R M I) := { add := λ S D, { sesq := λ x y, S x y + D x y, sesq_add_left := λ x y z, by {rw add_left, rw add_left, ac_refl}, sesq_smul_left := λ a x y, by {rw [smul_left, smul_left, mul_add]}, sesq_add_right := λ x y z, by {rw add_right, rw add_right, ac_refl}, sesq_smul_right := λ a x y, by {rw [smul_right, smul_right, mul_add]} }, add_assoc := by {intros, ext, unfold coe_fn has_coe_to_fun.coe sesq coe_fn has_coe_to_fun.coe sesq, rw add_assoc}, zero := { sesq := λ x y, 0, sesq_add_left := λ x y z, (add_zero 0).symm, sesq_smul_left := λ a x y, (mul_zero a).symm, sesq_add_right := λ x y z, (zero_add 0).symm, sesq_smul_right := λ a x y, (mul_zero (I a)).symm }, zero_add := by {intros, ext, unfold coe_fn has_coe_to_fun.coe sesq, rw zero_add}, add_zero := by {intros, ext, unfold coe_fn has_coe_to_fun.coe sesq, rw add_zero}, neg := λ S, { sesq := λ x y, - (S.1 x y), sesq_add_left := λ x y z, by rw [sesq_add_left, neg_add], sesq_smul_left := λ a x y, by rw [sesq_smul_left, mul_neg_eq_neg_mul_symm], sesq_add_right := λ x y z, by rw [sesq_add_right, neg_add], sesq_smul_right := λ a x y, by rw [sesq_smul_right, mul_neg_eq_neg_mul_symm] }, add_left_neg := by {intros, ext, unfold coe_fn has_coe_to_fun.coe sesq, rw neg_add_self}, add_comm := by {intros, ext, unfold coe_fn has_coe_to_fun.coe sesq, rw add_comm} } instance : inhabited (sesq_form R M I) := ⟨0⟩ /-- The proposition that two elements of a sesquilinear form space are orthogonal -/ def is_ortho (S : sesq_form R M I) (x y : M) : Prop := S x y = 0 lemma ortho_zero (x : M) : is_ortho S (0 : M) x := zero_left x end general_ring section comm_ring variables {R : Type*} [comm_ring R] {M : Type v} [add_comm_group M] [module R M] {J : ring_anti_equiv R R} (F : sesq_form R M J) (f : M → M) instance to_module : module R (sesq_form R M J) := { smul := λ c S, { sesq := λ x y, c * S x y, sesq_add_left := λ x y z, by {unfold coe_fn has_coe_to_fun.coe sesq, rw [sesq_add_left, left_distrib]}, sesq_smul_left := λ a x y, by {unfold coe_fn has_coe_to_fun.coe sesq, rw [sesq_smul_left, ←mul_assoc, mul_comm c, mul_assoc]}, sesq_add_right := λ x y z, by {unfold coe_fn has_coe_to_fun.coe sesq, rw [sesq_add_right, left_distrib]}, sesq_smul_right := λ a x y, by {unfold coe_fn has_coe_to_fun.coe sesq, rw [sesq_smul_right, ←mul_assoc, mul_comm c, mul_assoc], refl} }, smul_add := λ c S D, by {ext, unfold coe_fn has_coe_to_fun.coe sesq, rw left_distrib}, add_smul := λ c S D, by {ext, unfold coe_fn has_coe_to_fun.coe sesq, rw right_distrib}, mul_smul := λ a c D, by {ext, unfold coe_fn has_coe_to_fun.coe sesq, rw mul_assoc}, one_smul := λ S, by {ext, unfold coe_fn has_coe_to_fun.coe sesq, rw one_mul}, zero_smul := λ S, by {ext, unfold coe_fn has_coe_to_fun.coe sesq, rw zero_mul}, smul_zero := λ S, by {ext, unfold coe_fn has_coe_to_fun.coe sesq, rw mul_zero} } end comm_ring section domain variables {R : Type*} [domain R] {M : Type v} [add_comm_group M] [module R M] {K : ring_anti_equiv R R} {G : sesq_form R M K} theorem ortho_smul_left {x y : M} {a : R} (ha : a ≠ 0) : (is_ortho G x y) ↔ (is_ortho G (a • x) y) := begin dunfold is_ortho, split; intro H, { rw [smul_left, H, ring.mul_zero] }, { rw [smul_left, mul_eq_zero] at H, cases H, { trivial }, { exact H }} end theorem ortho_smul_right {x y : M} {a : R} (ha : a ≠ 0) : (is_ortho G x y) ↔ (is_ortho G x (a • y)) := begin dunfold is_ortho, split; intro H, { rw [smul_right, H, ring.mul_zero] }, { rw [smul_right, mul_eq_zero] at H, cases H, { rw map_zero_iff at H, trivial }, { exact H }} end end domain end sesq_form namespace refl_sesq_form open refl_sesq_form sesq_form variables {R : Type*} {M : Type*} [ring R] [add_comm_group M] [module R M] {I : ring_anti_equiv R R} {S : sesq_form R M I} /-- The proposition that a sesquilinear form is reflexive -/ def is_refl (S : sesq_form R M I) : Prop := ∀ (x y : M), S x y = 0 → S y x = 0 variable (H : is_refl S) lemma eq_zero : ∀ {x y : M}, S x y = 0 → S y x = 0 := λ x y, H x y lemma ortho_sym {x y : M} : is_ortho S x y ↔ is_ortho S y x := ⟨eq_zero H, eq_zero H⟩ end refl_sesq_form namespace sym_sesq_form open sym_sesq_form sesq_form variables {R : Type*} {M : Type*} [ring R] [add_comm_group M] [module R M] {I : ring_anti_equiv R R} {S : sesq_form R M I} /-- The proposition that a sesquilinear form is symmetric -/ def is_sym (S : sesq_form R M I) : Prop := ∀ (x y : M), I (S x y) = S y x variable (H : is_sym S) include H lemma sym (x y : M) : I (S x y) = S y x := H x y lemma is_refl : refl_sesq_form.is_refl S := λ x y H1, by rw [←H, map_zero_iff, H1] lemma ortho_sym {x y : M} : is_ortho S x y ↔ is_ortho S y x := refl_sesq_form.ortho_sym (is_refl H) end sym_sesq_form namespace alt_sesq_form open alt_sesq_form sesq_form variables {R : Type*} {M : Type*} [ring R] [add_comm_group M] [module R M] {I : ring_anti_equiv R R} {S : sesq_form R M I} /-- The proposition that a sesquilinear form is alternating -/ def is_alt (S : sesq_form R M I) : Prop := ∀ (x : M), S x x = 0 variable (H : is_alt S) include H lemma self_eq_zero (x : M) : S x x = 0 := H x lemma neg (x y : M) : - S x y = S y x := begin have H1 : S (x + y) (x + y) = 0, { exact self_eq_zero H (x + y) }, rw [add_left, add_right, add_right, self_eq_zero H, self_eq_zero H, ring.zero_add, ring.add_zero, add_eq_zero_iff_neg_eq] at H1, exact H1, end end alt_sesq_form
41fa3a25b309298e5e1ad7d5abbb183eb0e05316
b7f22e51856f4989b970961f794f1c435f9b8f78
/library/data/real/complete.lean
0beece400652313e3806796073b6be78d4e3d17e
[ "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
32,118
lean
/- Copyright (c) 2015 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Robert Y. Lewis The real numbers, constructed as equivalence classes of Cauchy sequences of rationals. This construction follows Bishop and Bridges (1985). At this point, we no longer proceed constructively: this file makes heavy use of decidability, excluded middle, and Hilbert choice. Two sets of definitions of Cauchy sequences, convergence, etc are available in the libray, one with rates and one without. The definitions here, with rates, are amenable to be used constructively if and when that development takes place. The second set of definitions available in /library/theories/analysis/metric_space.lean are the usual classical ones. Here, we show that ℝ is complete. The proofs of Cauchy completeness and the supremum property are independent of each other. -/ import data.real.basic data.real.order data.real.division data.rat data.nat data.pnat open rat local postfix ⁻¹ := pnat.inv open eq.ops pnat classical namespace rat_seq theorem rat_approx {s : seq} (H : regular s) : ∀ n : ℕ+, ∃ q : ℚ, ∃ N : ℕ+, ∀ m : ℕ+, m ≥ N → abs (s m - q) ≤ n⁻¹ := begin intro n, existsi (s (2 * n)), existsi 2 * n, intro m Hm, apply le.trans, apply H, rewrite -(pnat.add_halves n), apply add_le_add_right, apply inv_ge_of_le Hm end theorem rat_approx_seq {s : seq} (H : regular s) : ∀ n : ℕ+, ∃ q : ℚ, s_le (s_abs (sadd s (sneg (const q)))) (const n⁻¹) := begin intro m, rewrite ↑s_le, cases rat_approx H m with [q, Hq], cases Hq with [N, HN], existsi q, apply nonneg_of_bdd_within, repeat (apply reg_add_reg | apply reg_neg_reg | apply abs_reg_of_reg | apply const_reg | assumption), intro n, existsi N, intro p Hp, rewrite ↑[sadd, sneg, s_abs, const], apply le.trans, rotate 1, rewrite -sub_eq_add_neg, apply sub_le_sub_left, apply HN, apply le.trans, apply Hp, rewrite -*pnat.mul_assoc, apply pnat.mul_le_mul_left, rewrite [sub_self, -neg_zero], apply neg_le_neg, apply rat.le_of_lt, apply pnat.inv_pos end theorem r_rat_approx (s : reg_seq) : ∀ n : ℕ+, ∃ q : ℚ, r_le (r_abs (radd s (rneg (r_const q)))) (r_const n⁻¹) := rat_approx_seq (reg_seq.is_reg s) theorem const_bound {s : seq} (Hs : regular s) (n : ℕ+) : s_le (s_abs (sadd s (sneg (const (s n))))) (const n⁻¹) := begin rewrite ↑[s_le, nonneg, s_abs, sadd, sneg, const], intro m, rewrite -sub_eq_add_neg, apply iff.mp !le_add_iff_neg_le_sub_left, apply le.trans, apply Hs, apply add_le_add_right, rewrite -*pnat.mul_assoc, apply inv_ge_of_le, apply pnat.mul_le_mul_left end theorem abs_const (a : ℚ) : const (abs a) ≡ s_abs (const a) := by apply equiv.refl theorem r_abs_const (a : ℚ) : requiv (r_const (abs a) ) (r_abs (r_const a)) := abs_const a theorem equiv_abs_of_ge_zero {s : seq} (Hs : regular s) (Hz : s_le zero s) : s_abs s ≡ s := begin apply eq_of_bdd, apply abs_reg_of_reg Hs, apply Hs, intro j, rewrite ↑s_abs, let Hz' := s_nonneg_of_ge_zero Hs Hz, existsi 2 * j, intro n Hn, cases em (s n ≥ 0) with [Hpos, Hneg], rewrite [abs_of_nonneg Hpos, sub_self, abs_zero], apply rat.le_of_lt, apply pnat.inv_pos, let Hneg' := lt_of_not_ge Hneg, have Hsn : -s n - s n > 0, from add_pos (neg_pos_of_neg Hneg') (neg_pos_of_neg Hneg'), rewrite [abs_of_neg Hneg', abs_of_pos Hsn], apply le.trans, apply add_le_add, repeat (apply neg_le_neg; apply Hz'), rewrite neg_neg, apply le.trans, apply add_le_add, repeat (apply inv_ge_of_le; apply Hn), krewrite pnat.add_halves, end theorem equiv_neg_abs_of_le_zero {s : seq} (Hs : regular s) (Hz : s_le s zero) : s_abs s ≡ sneg s := begin apply eq_of_bdd, apply abs_reg_of_reg Hs, apply reg_neg_reg Hs, intro j, rewrite [↑s_abs, ↑s_le at Hz], have Hz' : nonneg (sneg s), begin apply nonneg_of_nonneg_equiv, rotate 3, apply Hz, rotate 2, apply s_zero_add, repeat (apply Hs | apply zero_is_reg | apply reg_neg_reg | apply reg_add_reg) end, existsi 2 * j, intro n Hn, cases em (s n ≥ 0) with [Hpos, Hneg], have Hsn : s n + s n ≥ 0, from add_nonneg Hpos Hpos, rewrite [abs_of_nonneg Hpos, ↑sneg, sub_neg_eq_add, abs_of_nonneg Hsn], rewrite [↑nonneg at Hz', ↑sneg at Hz'], apply le.trans, apply add_le_add, repeat apply (le_of_neg_le_neg !Hz'), apply le.trans, apply add_le_add, repeat (apply inv_ge_of_le; apply Hn), krewrite pnat.add_halves, let Hneg' := lt_of_not_ge Hneg, rewrite [abs_of_neg Hneg', ↑sneg, sub_neg_eq_add, neg_add_eq_sub, sub_self, abs_zero], apply rat.le_of_lt, apply pnat.inv_pos end theorem r_equiv_abs_of_ge_zero {s : reg_seq} (Hz : r_le r_zero s) : requiv (r_abs s) s := equiv_abs_of_ge_zero (reg_seq.is_reg s) Hz theorem r_equiv_neg_abs_of_le_zero {s : reg_seq} (Hz : r_le s r_zero) : requiv (r_abs s) (-s) := equiv_neg_abs_of_le_zero (reg_seq.is_reg s) Hz end rat_seq namespace real open [class] rat_seq private theorem rewrite_helper9 (a b c : ℝ) : b - c = (b - a) - (c - a) := by rewrite [-sub_add_eq_sub_sub_swap, sub_add_cancel] private theorem rewrite_helper10 (a b c d : ℝ) : c - d = (c - a) + (a - b) + (b - d) := by rewrite [*add_sub, *sub_add_cancel] noncomputable definition rep (x : ℝ) : rat_seq.reg_seq := some (quot.exists_rep x) definition re_abs (x : ℝ) : ℝ := quot.lift_on x (λ a, quot.mk (rat_seq.r_abs a)) (take a b Hab, quot.sound (rat_seq.r_abs_well_defined Hab)) theorem r_abs_nonneg {x : ℝ} : zero ≤ x → re_abs x = x := quot.induction_on x (λ a Ha, quot.sound (rat_seq.r_equiv_abs_of_ge_zero Ha)) theorem r_abs_nonpos {x : ℝ} : x ≤ zero → re_abs x = -x := quot.induction_on x (λ a Ha, quot.sound (rat_seq.r_equiv_neg_abs_of_le_zero Ha)) private theorem abs_const' (a : ℚ) : of_rat (abs a) = re_abs (of_rat a) := quot.sound (rat_seq.r_abs_const a) private theorem re_abs_is_abs : re_abs = abs := funext (begin intro x, apply eq.symm, cases em (zero ≤ x) with [Hor1, Hor2], rewrite [abs_of_nonneg Hor1, r_abs_nonneg Hor1], have Hor2' : x ≤ zero, from le_of_lt (lt_of_not_ge Hor2), rewrite [abs_of_neg (lt_of_not_ge Hor2), r_abs_nonpos Hor2'] end) theorem abs_const (a : ℚ) : of_rat (abs a) = abs (of_rat a) := by rewrite -re_abs_is_abs private theorem rat_approx' (x : ℝ) : ∀ n : ℕ+, ∃ q : ℚ, re_abs (x - of_rat q) ≤ of_rat n⁻¹ := quot.induction_on x (λ s n, rat_seq.r_rat_approx s n) theorem rat_approx (x : ℝ) : ∀ n : ℕ+, ∃ q : ℚ, abs (x - of_rat q) ≤ of_rat n⁻¹ := by rewrite -re_abs_is_abs; apply rat_approx' noncomputable definition approx (x : ℝ) (n : ℕ+) := some (rat_approx x n) theorem approx_spec (x : ℝ) (n : ℕ+) : abs (x - (of_rat (approx x n))) ≤ of_rat n⁻¹ := some_spec (rat_approx x n) theorem approx_spec' (x : ℝ) (n : ℕ+) : abs ((of_rat (approx x n)) - x) ≤ of_rat n⁻¹ := by rewrite abs_sub; apply approx_spec theorem ex_rat_pos_lower_bound_of_pos {x : ℝ} (H : x > 0) : ∃ q : ℚ, q > 0 ∧ of_rat q ≤ x := if Hgeo : x ≥ 1 then exists.intro 1 (and.intro zero_lt_one Hgeo) else have Hdp : 1 / x > 0, from one_div_pos_of_pos H, begin cases rat_approx (1 / x) 2 with q Hq, have Hqp : q > 0, begin apply lt_of_not_ge, intro Hq2, note Hx' := one_div_lt_one_div_of_lt H (lt_of_not_ge Hgeo), rewrite div_one at Hx', have Horqn : of_rat q ≤ 0, begin krewrite -of_rat_zero, apply of_rat_le_of_rat_of_le Hq2 end, have Hgt1 : 1 / x - of_rat q > 1, from calc 1 / x - of_rat q = 1 / x + -of_rat q : sub_eq_add_neg ... ≥ 1 / x : le_add_of_nonneg_right (neg_nonneg_of_nonpos Horqn) ... > 1 : Hx', have Hpos : 1 / x - of_rat q > 0, from gt.trans Hgt1 zero_lt_one, rewrite [abs_of_pos Hpos at Hq], apply not_le_of_gt Hgt1, apply le.trans, apply Hq, krewrite -of_rat_one, apply of_rat_le_of_rat_of_le, apply inv_le_one end, existsi 1 / (2⁻¹ + q), split, apply div_pos_of_pos_of_pos, exact zero_lt_one, apply add_pos, apply pnat.inv_pos, exact Hqp, note Hle2 := sub_le_of_abs_sub_le_right Hq, note Hle3 := le_add_of_sub_left_le Hle2, note Hle4 := one_div_le_of_one_div_le_of_pos H Hle3, rewrite [of_rat_divide, of_rat_add], exact Hle4 end theorem ex_rat_neg_upper_bound_of_neg {x : ℝ} (H : x < 0) : ∃ q : ℚ, q < 0 ∧ x ≤ of_rat q := have H' : -x > 0, from neg_pos_of_neg H, obtain q [Hq1 Hq2], from ex_rat_pos_lower_bound_of_pos H', exists.intro (-q) (and.intro (neg_neg_of_pos Hq1) (le_neg_of_le_neg Hq2)) notation `r_seq` := ℕ+ → ℝ noncomputable definition converges_to_with_rate (X : r_seq) (a : ℝ) (N : ℕ+ → ℕ+) := ∀ k : ℕ+, ∀ n : ℕ+, n ≥ N k → abs (X n - a) ≤ of_rat k⁻¹ noncomputable definition cauchy_with_rate (X : r_seq) (M : ℕ+ → ℕ+) := ∀ k : ℕ+, ∀ m n : ℕ+, m ≥ M k → n ≥ M k → abs (X m - X n) ≤ of_rat k⁻¹ theorem cauchy_with_rate_of_converges_to_with_rate {X : r_seq} {a : ℝ} {N : ℕ+ → ℕ+} (Hc : converges_to_with_rate X a N) : cauchy_with_rate X (λ k, N (2 * k)) := begin intro k m n Hm Hn, rewrite (rewrite_helper9 a), apply le.trans, apply abs_add_le_abs_add_abs, apply le.trans, apply add_le_add, apply Hc, apply Hm, krewrite abs_neg, apply Hc, apply Hn, xrewrite -of_rat_add, apply of_rat_le_of_rat_of_le, krewrite pnat.add_halves, end private definition Nb (M : ℕ+ → ℕ+) := λ k, max (3 * k) (M (2 * k)) private theorem Nb_spec_right (M : ℕ+ → ℕ+) (k : ℕ+) : M (2 * k) ≤ Nb M k := !le_max_right private theorem Nb_spec_left (M : ℕ+ → ℕ+) (k : ℕ+) : 3 * k ≤ Nb M k := !le_max_left section lim_seq parameter {X : r_seq} parameter {M : ℕ+ → ℕ+} hypothesis Hc : cauchy_with_rate X M include Hc noncomputable definition lim_seq : ℕ+ → ℚ := λ k, approx (X (Nb M k)) (2 * k) private theorem lim_seq_reg_helper {m n : ℕ+} (Hmn : M (2 * n) ≤M (2 * m)) : abs (of_rat (lim_seq m) - X (Nb M m)) + abs (X (Nb M m) - X (Nb M n)) + abs (X (Nb M n) - of_rat (lim_seq n)) ≤ of_rat (m⁻¹ + n⁻¹) := begin apply le.trans, apply add_le_add_three, apply approx_spec', rotate 1, apply approx_spec, rotate 1, apply Hc, rotate 1, apply Nb_spec_right, rotate 1, apply le.trans, apply Hmn, apply Nb_spec_right, krewrite [-+of_rat_add], change of_rat ((2 * m)⁻¹ + (2 * n)⁻¹ + (2 * n)⁻¹) ≤ of_rat (m⁻¹ + n⁻¹), rewrite [add.assoc], krewrite pnat.add_halves, apply of_rat_le_of_rat_of_le, apply add_le_add_right, apply inv_ge_of_le, apply pnat.mul_le_mul_left end theorem lim_seq_reg : rat_seq.regular lim_seq := begin rewrite ↑rat_seq.regular, intro m n, apply le_of_of_rat_le_of_rat, rewrite [abs_const, of_rat_sub, (rewrite_helper10 (X (Nb M m)) (X (Nb M n)))], apply le.trans, apply abs_add_three, cases em (M (2 * m) ≥ M (2 * n)) with [Hor1, Hor2], apply lim_seq_reg_helper Hor1, let Hor2' := le_of_lt (lt_of_not_ge Hor2), krewrite [abs_sub (X (Nb M n)), abs_sub (X (Nb M m)), abs_sub, rat.add_comm, add_comm_three], apply lim_seq_reg_helper Hor2' end theorem lim_seq_spec (k : ℕ+) : rat_seq.s_le (rat_seq.s_abs (rat_seq.sadd lim_seq (rat_seq.sneg (rat_seq.const (lim_seq k))))) (rat_seq.const k⁻¹) := by apply rat_seq.const_bound; apply lim_seq_reg private noncomputable definition r_lim_seq : rat_seq.reg_seq := rat_seq.reg_seq.mk lim_seq lim_seq_reg private theorem r_lim_seq_spec (k : ℕ+) : rat_seq.r_le (rat_seq.r_abs ((rat_seq.radd r_lim_seq (rat_seq.rneg (rat_seq.r_const ((rat_seq.reg_seq.sq r_lim_seq) k)))))) (rat_seq.r_const k⁻¹) := lim_seq_spec k noncomputable definition lim : ℝ := quot.mk r_lim_seq theorem re_lim_spec (k : ℕ+) : re_abs (lim - (of_rat (lim_seq k))) ≤ of_rat k⁻¹ := r_lim_seq_spec k theorem lim_spec' (k : ℕ+) : abs (lim - (of_rat (lim_seq k))) ≤ of_rat k⁻¹ := by rewrite -re_abs_is_abs; apply re_lim_spec theorem lim_spec (k : ℕ+) : abs ((of_rat (lim_seq k)) - lim) ≤ of_rat k⁻¹ := by rewrite abs_sub; apply lim_spec' theorem converges_to_with_rate_of_cauchy_with_rate : converges_to_with_rate X lim (Nb M) := begin intro k n Hn, rewrite (rewrite_helper10 (X (Nb M n)) (of_rat (lim_seq n))), apply le.trans, apply abs_add_three, apply le.trans, apply add_le_add_three, apply Hc, apply le.trans, rotate 1, apply Hn, rotate_right 1, apply Nb_spec_right, have HMk : M (2 * k) ≤ Nb M n, begin apply le.trans, apply Nb_spec_right, apply le.trans, apply Hn, apply le.trans, apply pnat.mul_le_mul_left 3, apply Nb_spec_left end, apply HMk, rewrite ↑lim_seq, apply approx_spec, apply lim_spec, krewrite [-+of_rat_add], change of_rat ((2 * k)⁻¹ + (2 * n)⁻¹ + n⁻¹) ≤ of_rat k⁻¹, apply of_rat_le_of_rat_of_le, apply le.trans, apply add_le_add_three, apply rat.le_refl, apply inv_ge_of_le, apply pnat_mul_le_mul_left', apply le.trans, rotate 1, apply Hn, rotate_right 1, apply Nb_spec_left, apply inv_ge_of_le, apply le.trans, rotate 1, apply Hn, rotate_right 1, apply Nb_spec_left, rewrite -*pnat.mul_assoc, krewrite pnat.p_add_fractions, end end lim_seq ------------------------------------------- -- int embedding theorems -- archimedean properties, integer floor and ceiling section ints open int theorem archimedean_upper (x : ℝ) : ∃ z : ℤ, x ≤ of_int z := begin apply quot.induction_on x, intro s, cases rat_seq.bdd_of_regular (rat_seq.reg_seq.is_reg s) with [b, Hb], existsi ubound b, have H : rat_seq.s_le (rat_seq.reg_seq.sq s) (rat_seq.const (rat.of_nat (ubound b))), begin apply rat_seq.s_le_of_le_pointwise (rat_seq.reg_seq.is_reg s), apply rat_seq.const_reg, intro n, apply le.trans, apply Hb, apply ubound_ge end, apply H end theorem archimedean_upper_strict (x : ℝ) : ∃ z : ℤ, x < of_int z := begin cases archimedean_upper x with [z, Hz], existsi z + 1, apply lt_of_le_of_lt, apply Hz, apply of_int_lt_of_int_of_lt, apply lt_add_of_pos_right, apply dec_trivial end theorem archimedean_lower (x : ℝ) : ∃ z : ℤ, x ≥ of_int z := begin cases archimedean_upper (-x) with [z, Hz], existsi -z, rewrite [of_int_neg], apply iff.mp !neg_le_iff_neg_le Hz end theorem archimedean_lower_strict (x : ℝ) : ∃ z : ℤ, x > of_int z := begin cases archimedean_upper_strict (-x) with [z, Hz], existsi -z, rewrite [of_int_neg], apply iff.mp !neg_lt_iff_neg_lt Hz end private definition ex_floor (x : ℝ) := (@exists_greatest_of_bdd (λ z, x ≥ of_int z) _ (begin existsi some (archimedean_upper_strict x), let Har := some_spec (archimedean_upper_strict x), intros z Hz, apply not_le_of_gt, apply lt_of_lt_of_le, apply Har, have H : of_int (some (archimedean_upper_strict x)) ≤ of_int z, begin apply of_int_le_of_int_of_le, apply Hz end, exact H end) (by existsi some (archimedean_lower x); apply some_spec (archimedean_lower x))) noncomputable definition floor (x : ℝ) : ℤ := some (ex_floor x) noncomputable definition ceil (x : ℝ) : ℤ := - floor (-x) theorem floor_le (x : ℝ) : floor x ≤ x := and.left (some_spec (ex_floor x)) theorem lt_of_floor_lt {x : ℝ} {z : ℤ} (Hz : floor x < z) : x < z := begin apply lt_of_not_ge, cases some_spec (ex_floor x), apply a_1 _ Hz end theorem le_ceil (x : ℝ) : x ≤ ceil x := begin rewrite [↑ceil, of_int_neg], apply iff.mp !le_neg_iff_le_neg, apply floor_le end theorem lt_of_lt_ceil {x : ℝ} {z : ℤ} (Hz : z < ceil x) : z < x := begin rewrite ↑ceil at Hz, note Hz' := lt_of_floor_lt (iff.mp !lt_neg_iff_lt_neg Hz), rewrite [of_int_neg at Hz'], apply lt_of_neg_lt_neg Hz' end theorem floor_succ (x : ℝ) : floor (x + 1) = floor x + 1 := begin apply by_contradiction, intro H, cases lt_or_gt_of_ne H with [Hgt, Hlt], note Hl := lt_of_floor_lt Hgt, rewrite [of_int_add at Hl], apply not_le_of_gt (lt_of_add_lt_add_right Hl) !floor_le, note Hl := lt_of_floor_lt (iff.mp !add_lt_iff_lt_sub_right Hlt), rewrite [of_int_sub at Hl], apply not_le_of_gt (iff.mpr !add_lt_iff_lt_sub_right Hl) !floor_le end theorem floor_sub_one_lt_floor (x : ℝ) : floor (x - 1) < floor x := begin apply @lt_of_add_lt_add_right ℤ _ _ 1, rewrite [-floor_succ (x - 1), sub_add_cancel], apply lt_add_of_pos_right dec_trivial end theorem ceil_lt_ceil_succ (x : ℝ) : ceil x < ceil (x + 1) := begin rewrite [↑ceil, neg_add], apply neg_lt_neg, apply floor_sub_one_lt_floor end open nat theorem archimedean_small {ε : ℝ} (H : ε > 0) : ∃ (n : ℕ), 1 / succ n < ε := let n := int.nat_abs (ceil (2 / ε)) in have int.of_nat n ≥ ceil (2 / ε), by rewrite of_nat_nat_abs; apply le_abs_self, have int.of_nat (succ n) ≥ ceil (2 / ε), begin apply le.trans, exact this, apply int.of_nat_le_of_nat_of_le, apply le_succ end, have H₁ : int.succ n ≥ ceil (2 / ε), from of_int_le_of_int_of_le this, have H₂ : succ n ≥ 2 / ε, from !le.trans !le_ceil H₁, have H₃ : 2 / ε > 0, from div_pos_of_pos_of_pos two_pos H, have 1 / succ n < ε, from calc 1 / succ n ≤ 1 / (2 / ε) : one_div_le_one_div_of_le H₃ H₂ ... = ε / 2 : one_div_div ... < ε : div_two_lt_of_pos H, exists.intro n this end ints -------------------------------------------------- -- supremum property -- this development roughly follows the proof of completeness done in Isabelle. -- It does not depend on the previous proof of Cauchy completeness. Much of the same -- machinery can be used to show that Cauchy completeness implies the supremum property. section supremum open prod nat local postfix `~` := nat_of_pnat -- The top part of this section could be refactored. What is the appropriate place to define -- bounds, supremum, etc? In algebra/ordered_field? They potentially apply to more than just ℝ. parameter X : ℝ → Prop definition ub (x : ℝ) := ∀ y : ℝ, X y → y ≤ x definition is_sup (x : ℝ) := ub x ∧ ∀ y : ℝ, ub y → x ≤ y definition lb (x : ℝ) := ∀ y : ℝ, X y → x ≤ y definition is_inf (x : ℝ) := lb x ∧ ∀ y : ℝ, lb y → y ≤ x parameter elt : ℝ hypothesis inh : X elt parameter bound : ℝ hypothesis bdd : ub bound include inh bdd private definition avg (a b : ℚ) := a / 2 + b / 2 private noncomputable definition bisect (ab : ℚ × ℚ) := if ub (avg (pr1 ab) (pr2 ab)) then (pr1 ab, (avg (pr1 ab) (pr2 ab))) else (avg (pr1 ab) (pr2 ab), pr2 ab) private noncomputable definition under : ℚ := rat.of_int (floor (elt - 1)) private theorem under_spec1 : of_rat under < elt := have H : of_rat under < of_int (floor elt), begin apply of_int_lt_of_int_of_lt, apply floor_sub_one_lt_floor end, lt_of_lt_of_le H !floor_le private theorem under_spec : ¬ ub under := begin rewrite ↑ub, apply not_forall_of_exists_not, existsi elt, apply iff.mpr !not_implies_iff_and_not, apply and.intro, apply inh, apply not_le_of_gt under_spec1 end private noncomputable definition over : ℚ := rat.of_int (ceil (bound + 1)) -- b private theorem over_spec1 : bound < of_rat over := have H : of_int (ceil bound) < of_rat over, begin apply of_int_lt_of_int_of_lt, apply ceil_lt_ceil_succ end, lt_of_le_of_lt !le_ceil H private theorem over_spec : ub over := begin rewrite ↑ub, intro y Hy, apply le_of_lt, apply lt_of_le_of_lt, apply bdd, apply Hy, apply over_spec1 end private noncomputable definition under_seq := λ n : ℕ, pr1 (iterate bisect n (under, over)) -- A private noncomputable definition over_seq := λ n : ℕ, pr2 (iterate bisect n (under, over)) -- B private noncomputable definition avg_seq := λ n : ℕ, avg (over_seq n) (under_seq n) -- C private theorem avg_symm (n : ℕ) : avg_seq n = avg (under_seq n) (over_seq n) := by rewrite [↑avg_seq, ↑avg, add.comm] private theorem over_0 : over_seq 0 = over := rfl private theorem under_0 : under_seq 0 = under := rfl private theorem succ_helper (n : ℕ) : avg (pr1 (iterate bisect n (under, over))) (pr2 (iterate bisect n (under, over))) = avg_seq n := by rewrite avg_symm private theorem under_succ (n : ℕ) : under_seq (succ n) = (if ub (avg_seq n) then under_seq n else avg_seq n) := begin cases em (ub (avg_seq n)) with [Hub, Hub], rewrite [if_pos Hub], have H : pr1 (bisect (iterate bisect n (under, over))) = under_seq n, by rewrite [↑under_seq, ↑bisect at {2}, -succ_helper at Hub, if_pos Hub], apply H, rewrite [if_neg Hub], have H : pr1 (bisect (iterate bisect n (under, over))) = avg_seq n, by rewrite [↑bisect at {2}, -succ_helper at Hub, if_neg Hub, avg_symm], apply H end private theorem over_succ (n : ℕ) : over_seq (succ n) = (if ub (avg_seq n) then avg_seq n else over_seq n) := begin cases em (ub (avg_seq n)) with [Hub, Hub], rewrite [if_pos Hub], have H : pr2 (bisect (iterate bisect n (under, over))) = avg_seq n, by rewrite [↑bisect at {2}, -succ_helper at Hub, if_pos Hub, avg_symm], apply H, rewrite [if_neg Hub], have H : pr2 (bisect (iterate bisect n (under, over))) = over_seq n, by rewrite [↑over_seq, ↑bisect at {2}, -succ_helper at Hub, if_neg Hub], apply H end private theorem nat.zero_eq_0 : (zero : ℕ) = 0 := rfl private theorem width (n : ℕ) : over_seq n - under_seq n = (over - under) / ((2^n) : ℚ) := nat.induction_on n (by xrewrite [nat.zero_eq_0, over_0, under_0, pow_zero, div_one]) (begin intro a Ha, rewrite [over_succ, under_succ], let Hou := calc (over_seq a) / 2 - (under_seq a) / 2 = ((over - under) / 2^a) / 2 : by rewrite [div_sub_div_same, Ha] ... = (over - under) / ((2^a) * 2) : by rewrite div_div_eq_div_mul ... = (over - under) / 2^(a + 1) : by rewrite pow_add, cases em (ub (avg_seq a)), rewrite [*if_pos a_1, -add_one, -Hou, ↑avg_seq, ↑avg, sub_eq_add_neg, add.assoc, -sub_eq_add_neg, div_two_sub_self], rewrite [*if_neg a_1, -add_one, -Hou, ↑avg_seq, ↑avg, sub_add_eq_sub_sub, sub_self_div_two] end) private theorem width_narrows : ∃ n : ℕ, over_seq n - under_seq n ≤ 1 := begin cases binary_bound (over - under) with [a, Ha], existsi a, rewrite (width a), apply div_le_of_le_mul, apply pow_pos dec_trivial, rewrite rat.mul_one, apply Ha end private noncomputable definition over' := over_seq (some width_narrows) private noncomputable definition under' := under_seq (some width_narrows) private noncomputable definition over_seq' := λ n, over_seq (n + some width_narrows) private noncomputable definition under_seq' := λ n, under_seq (n + some width_narrows) private theorem over_seq'0 : over_seq' 0 = over' := by rewrite [↑over_seq', nat.zero_add] private theorem under_seq'0 : under_seq' 0 = under' := by rewrite [↑under_seq', nat.zero_add] private theorem under_over' : over' - under' ≤ 1 := some_spec width_narrows private theorem width' (n : ℕ) : over_seq' n - under_seq' n ≤ 1 / 2^n := nat.induction_on n (begin xrewrite [nat.zero_eq_0, over_seq'0, under_seq'0, pow_zero, div_one], apply under_over' end) (begin intros a Ha, rewrite [↑over_seq' at *, ↑under_seq' at *, *succ_add at *, width at *, -add_one, -(add_one a), pow_add, pow_add _ a 1, *pow_one], apply div_mul_le_div_mul_of_div_le_div_pos' Ha dec_trivial end) private theorem PA (n : ℕ) : ¬ ub (under_seq n) := nat.induction_on n (by rewrite under_0; apply under_spec) (begin intro a Ha, rewrite under_succ, cases em (ub (avg_seq a)), rewrite (if_pos a_1), assumption, rewrite (if_neg a_1), assumption end) private theorem PB (n : ℕ) : ub (over_seq n) := nat.induction_on n (by rewrite over_0; apply over_spec) (begin intro a Ha, rewrite over_succ, cases em (ub (avg_seq a)), rewrite (if_pos a_1), assumption, rewrite (if_neg a_1), assumption end) private theorem under_lt_over : under < over := begin cases exists_not_of_not_forall under_spec with [x, Hx], cases and_not_of_not_implies Hx with [HXx, Hxu], apply lt_of_of_rat_lt_of_rat, apply lt_of_lt_of_le, apply lt_of_not_ge Hxu, apply over_spec _ HXx end private theorem under_seq_lt_over_seq : ∀ m n : ℕ, under_seq m < over_seq n := begin intros, cases exists_not_of_not_forall (PA m) with [x, Hx], cases iff.mp !not_implies_iff_and_not Hx with [HXx, Hxu], apply lt_of_of_rat_lt_of_rat, apply lt_of_lt_of_le, apply lt_of_not_ge Hxu, apply PB, apply HXx end private theorem under_seq_lt_over_seq_single : ∀ n : ℕ, under_seq n < over_seq n := by intros; apply under_seq_lt_over_seq private theorem under_seq'_lt_over_seq' : ∀ m n : ℕ, under_seq' m < over_seq' n := by intros; apply under_seq_lt_over_seq private theorem under_seq'_lt_over_seq'_single : ∀ n : ℕ, under_seq' n < over_seq' n := by intros; apply under_seq_lt_over_seq private theorem under_seq_mono_helper (i k : ℕ) : under_seq i ≤ under_seq (i + k) := (nat.induction_on k (by rewrite nat.add_zero; apply rat.le_refl) (begin intros a Ha, rewrite [add_succ, under_succ], cases em (ub (avg_seq (i + a))) with [Havg, Havg], rewrite (if_pos Havg), apply Ha, rewrite [if_neg Havg, ↑avg_seq, ↑avg], apply le.trans, apply Ha, rewrite -(add_halves (under_seq (i + a))) at {1}, apply add_le_add_right, apply div_le_div_of_le_of_pos, apply rat.le_of_lt, apply under_seq_lt_over_seq, apply dec_trivial end)) private theorem under_seq_mono (i j : ℕ) (H : i ≤ j) : under_seq i ≤ under_seq j := begin cases le.elim H with [k, Hk'], rewrite -Hk', apply under_seq_mono_helper end private theorem over_seq_mono_helper (i k : ℕ) : over_seq (i + k) ≤ over_seq i := nat.induction_on k (by rewrite nat.add_zero; apply rat.le_refl) (begin intros a Ha, rewrite [add_succ, over_succ], cases em (ub (avg_seq (i + a))) with [Havg, Havg], rewrite [if_pos Havg, ↑avg_seq, ↑avg], apply le.trans, rotate 1, apply Ha, rotate 1, apply add_le_of_le_sub_left, rewrite sub_self_div_two, apply div_le_div_of_le_of_pos, apply rat.le_of_lt, apply under_seq_lt_over_seq, apply dec_trivial, rewrite [if_neg Havg], apply Ha end) private theorem over_seq_mono (i j : ℕ) (H : i ≤ j) : over_seq j ≤ over_seq i := begin cases le.elim H with [k, Hk'], rewrite -Hk', apply over_seq_mono_helper end private theorem rat_power_two_inv_ge (k : ℕ+) : 1 / 2^k~ ≤ k⁻¹ := one_div_le_one_div_of_le !rat_of_pnat_is_pos !rat_power_two_le open rat_seq private theorem regular_lemma_helper {s : seq} {m n : ℕ+} (Hm : m ≤ n) (H : ∀ n i : ℕ+, i ≥ n → under_seq' n~ ≤ s i ∧ s i ≤ over_seq' n~) : abs (s m - s n) ≤ m⁻¹ + n⁻¹ := begin cases H m n Hm with [T1under, T1over], cases H m m (!le.refl) with [T2under, T2over], apply le.trans, apply dist_bdd_within_interval, apply under_seq'_lt_over_seq'_single, rotate 1, repeat assumption, apply le.trans, apply width', apply le.trans, apply rat_power_two_inv_ge, apply le_add_of_nonneg_right, apply rat.le_of_lt (!pnat.inv_pos) end private theorem regular_lemma (s : seq) (H : ∀ n i : ℕ+, i ≥ n → under_seq' n~ ≤ s i ∧ s i ≤ over_seq' n~) : regular s := begin rewrite ↑regular, intros, cases em (m ≤ n) with [Hm, Hn], apply regular_lemma_helper Hm H, note T := regular_lemma_helper (le_of_lt (lt_of_not_ge Hn)) H, rewrite [abs_sub at T, {n⁻¹ + _}add.comm at T], exact T end private noncomputable definition p_under_seq : seq := λ n : ℕ+, under_seq' n~ private noncomputable definition p_over_seq : seq := λ n : ℕ+, over_seq' n~ private theorem under_seq_regular : regular p_under_seq := begin apply regular_lemma, intros n i Hni, apply and.intro, apply under_seq_mono, apply add_le_add_right, apply Hni, apply rat.le_of_lt, apply under_seq_lt_over_seq end private theorem over_seq_regular : regular p_over_seq := begin apply regular_lemma, intros n i Hni, apply and.intro, apply rat.le_of_lt, apply under_seq_lt_over_seq, apply over_seq_mono, apply add_le_add_right, apply Hni end private noncomputable definition sup_over : ℝ := quot.mk (reg_seq.mk p_over_seq over_seq_regular) private noncomputable definition sup_under : ℝ := quot.mk (reg_seq.mk p_under_seq under_seq_regular) private theorem over_bound : ub sup_over := begin rewrite ↑ub, intros y Hy, apply le_of_le_reprs, intro n, apply PB, apply Hy end private theorem under_lowest_bound : ∀ y : ℝ, ub y → sup_under ≤ y := begin intros y Hy, apply le_of_reprs_le, intro n, cases exists_not_of_not_forall (PA _) with [x, Hx], cases and_not_of_not_implies Hx with [HXx, Hxn], apply le.trans, apply le_of_lt, apply lt_of_not_ge Hxn, apply Hy, apply HXx end private theorem under_over_equiv : p_under_seq ≡ p_over_seq := begin intros, apply le.trans, have H : p_under_seq n < p_over_seq n, from !under_seq_lt_over_seq, rewrite [abs_of_neg (iff.mpr !sub_neg_iff_lt H), neg_sub], apply width', apply le.trans, apply rat_power_two_inv_ge, apply le_add_of_nonneg_left, apply rat.le_of_lt !pnat.inv_pos end private theorem under_over_eq : sup_under = sup_over := quot.sound under_over_equiv theorem exists_is_sup_of_inh_of_bdd : ∃ x : ℝ, is_sup x := exists.intro sup_over (and.intro over_bound (under_over_eq ▸ under_lowest_bound)) end supremum definition bounding_set (X : ℝ → Prop) (x : ℝ) : Prop := ∀ y : ℝ, X y → x ≤ y theorem exists_is_inf_of_inh_of_bdd (X : ℝ → Prop) (elt : ℝ) (inh : X elt) (bound : ℝ) (bdd : lb X bound) : ∃ x : ℝ, is_inf X x := begin have Hinh : bounding_set X bound, begin intros y Hy, apply bdd, apply Hy end, have Hub : ub (bounding_set X) elt, begin intros y Hy, apply Hy, apply inh end, cases exists_is_sup_of_inh_of_bdd _ _ Hinh _ Hub with [supr, Hsupr], existsi supr, cases Hsupr with [Hubs1, Hubs2], apply and.intro, intros, apply Hubs2, intros z Hz, apply Hz, apply a, intros y Hlby, apply Hubs1, intros z Hz, apply Hlby, apply Hz end end real
30fdfdad4f9061a81521900b4b65a6e5adca99ef
5756a081670ba9c1d1d3fca7bd47cb4e31beae66
/Mathport/Util/SmartNaming.lean
1c3241bc6cb792cc068706b6684125dd82f60f5c
[ "Apache-2.0" ]
permissive
leanprover-community/mathport
2c9bdc8292168febf59799efdc5451dbf0450d4a
13051f68064f7638970d39a8fecaede68ffbf9e1
refs/heads/master
1,693,841,364,079
1,693,813,111,000
1,693,813,111,000
379,357,010
27
10
Apache-2.0
1,691,309,132,000
1,624,384,521,000
Lean
UTF-8
Lean
false
false
3,106
lean
import Mathport.Binary.Basic open Lean -- Ways to write `xs` as `ys ++ zs`, with `ys` nonempty. def List.splits1 : List a → List (List a × List a) | [] => [] | (x :: xs) => ([x], xs) :: (splits1 xs).map (fun (ys, zs) => (x :: ys, zs)) namespace Mathport.Binary open Mathlib.Prelude.Rename def reverseName (rm : RenameMap) (n4 : Name) : BinportM (List Name) := do match rm.toLean3.find? n4 with | none => return [] | some (n3, n3s) => return n3 :: n3s -- Collect all constant names that appear in ty -- and find corresponding Lean 3 names. -- Return a map (Lean 3 suffix ↦ Lean 4 suffix). -- (The NameMap keys are just the suffix part, as are the values.) def collectLean3Names (ty : Expr) : BinportM (NameMap String) := do let rm := Mathlib.Prelude.Rename.getRenameMap (← getEnv) let rec visit : Expr → NameMap String → BinportM (NameMap String) | .bvar _, nm | .fvar _, nm | .mvar _, nm | .sort _, nm | .lit _, nm => return nm | .app fn arg, nm => do visit fn (← visit arg nm) | .lam _ binderType body _, nm | .forallE _ binderType body _, nm => do visit binderType (← visit body nm) | .letE _ binderType value body _, nm => do visit binderType (← visit value (← visit body nm)) | .mdata _ e, nm => visit e nm | .proj _ _ e, nm => visit e nm | .const declName _, nm => do match declName with | .str _ s4 => let mut nm' := nm for n3 in (← reverseName rm declName) do if let .str _ s3 := n3 then nm' := nm'.insert s3 s4 return nm' | _ => return nm visit ty (mkNameMap _) /-- Decapitalize the string `s`, which is supposed to be the Lean 4 name of something being referred to in the name of a theorem. In some cases, we do something else--currently: don't decapitalize the names of the intervals. -/ def smartDecapitalize (s : String) : String := match exceptions.lookup s with | some s' => s' | none => s.decapitalize where exceptions : List (String × String) := ["Ioo", "Ico", "Iio", "Icc", "Iic", "Ioc", "Ici", "Ioi", "Ixx"].map (fun s => (s, s)) -- Greedily try to translate parts of the name `s` (separated by underscores) -- from Lean 3 to Lean 4, guided by the constants appearing within `ty` -- (which is a Lean 4 expression) and their Lean 3 counterparts. partial def smartNameAux (s : String) (ty : Expr) : BinportM String := do let nm ← collectLean3Names ty -- Translate a name that has already been split into "words". let rec go : List String → BinportM (List String) := fun l => do -- Look for longest initial string of "words" -- present as a key in nm. for (first, rest) in l.splits1.reverse do let n3 := "_".intercalate first if let some n4 := nm.find? n3 then return smartDecapitalize n4 :: (← go rest) -- Didn't manage to use first component. match l with | [] => return [] | x :: xs => return x :: (← go xs) let res ← go (s.splitOn "_") return "_".intercalate res def smartName (s : String) (ty : Option Expr) : BinportM String := match ty with | none => return s | some ty => smartNameAux s ty
fa245996e5d43201baf7ff732754e093db47174f
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/category_theory/limits/creates.lean
7a52c115ddb4db6a435dca63e2331e007b85b1ab
[ "Apache-2.0" ]
permissive
agjftucker/mathlib
d634cd0d5256b6325e3c55bb7fb2403548371707
87fe50de17b00af533f72a102d0adefe4a2285e8
refs/heads/master
1,625,378,131,941
1,599,166,526,000
1,599,166,526,000
160,748,509
0
0
Apache-2.0
1,544,141,789,000
1,544,141,789,000
null
UTF-8
Lean
false
false
16,491
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import category_theory.limits.preserves.basic open category_theory category_theory.limits namespace category_theory universes v u₁ u₂ u₃ variables {C : Type u₁} [category.{v} C] section creates variables {D : Type u₂} [category.{v} D] variables {J : Type v} [small_category J] {K : J ⥤ C} /-- Define the lift of a cone: For a cone `c` for `K ⋙ F`, give a cone for `K` which is a lift of `c`, i.e. the image of it under `F` is (iso) to `c`. We will then use this as part of the definition of creation of limits: every limit cone has a lift. Note this definition is really only useful when `c` is a limit already. -/ structure liftable_cone (K : J ⥤ C) (F : C ⥤ D) (c : cone (K ⋙ F)) := (lifted_cone : cone K) (valid_lift : F.map_cone lifted_cone ≅ c) /-- Define the lift of a cocone: For a cocone `c` for `K ⋙ F`, give a cocone for `K` which is a lift of `c`, i.e. the image of it under `F` is (iso) to `c`. We will then use this as part of the definition of creation of colimits: every limit cocone has a lift. Note this definition is really only useful when `c` is a colimit already. -/ structure liftable_cocone (K : J ⥤ C) (F : C ⥤ D) (c : cocone (K ⋙ F)) := (lifted_cocone : cocone K) (valid_lift : F.map_cocone lifted_cocone ≅ c) set_option default_priority 100 /-- Definition 3.3.1 of [Riehl]. We say that `F` creates limits of `K` if, given any limit cone `c` for `K ⋙ F` (i.e. below) we can lift it to a cone "above", and further that `F` reflects limits for `K`. If `F` reflects isomorphisms, it suffices to show only that the lifted cone is a limit - see `creates_limit_of_reflects_iso`. -/ class creates_limit (K : J ⥤ C) (F : C ⥤ D) extends reflects_limit K F := (lifts : Π c, is_limit c → liftable_cone K F c) /-- `F` creates limits of shape `J` if `F` creates the limit of any diagram `K : J ⥤ C`. -/ class creates_limits_of_shape (J : Type v) [small_category J] (F : C ⥤ D) := (creates_limit : Π {K : J ⥤ C}, creates_limit K F) /-- `F` creates limits if it creates limits of shape `J` for any small `J`. -/ class creates_limits (F : C ⥤ D) := (creates_limits_of_shape : Π {J : Type v} {𝒥 : small_category J}, by exactI creates_limits_of_shape J F) /-- Dual of definition 3.3.1 of [Riehl]. We say that `F` creates colimits of `K` if, given any limit cocone `c` for `K ⋙ F` (i.e. below) we can lift it to a cocone "above", and further that `F` reflects limits for `K`. If `F` reflects isomorphisms, it suffices to show only that the lifted cocone is a limit - see `creates_limit_of_reflects_iso`. -/ class creates_colimit (K : J ⥤ C) (F : C ⥤ D) extends reflects_colimit K F := (lifts : Π c, is_colimit c → liftable_cocone K F c) /-- `F` creates colimits of shape `J` if `F` creates the colimit of any diagram `K : J ⥤ C`. -/ class creates_colimits_of_shape (J : Type v) [small_category J] (F : C ⥤ D) := (creates_colimit : Π {K : J ⥤ C}, creates_colimit K F) /-- `F` creates colimits if it creates colimits of shape `J` for any small `J`. -/ class creates_colimits (F : C ⥤ D) := (creates_colimits_of_shape : Π {J : Type v} {𝒥 : small_category J}, by exactI creates_colimits_of_shape J F) attribute [instance, priority 100] -- see Note [lower instance priority] creates_limits_of_shape.creates_limit creates_limits.creates_limits_of_shape creates_colimits_of_shape.creates_colimit creates_colimits.creates_colimits_of_shape /- Interface to the `creates_limit` class. -/ /-- `lift_limit t` is the cone for `K` given by lifting the limit `t` for `K ⋙ F`. -/ def lift_limit {K : J ⥤ C} {F : C ⥤ D} [creates_limit K F] {c : cone (K ⋙ F)} (t : is_limit c) : cone K := (creates_limit.lifts c t).lifted_cone /-- The lifted cone has an image isomorphic to the original cone. -/ def lifted_limit_maps_to_original {K : J ⥤ C} {F : C ⥤ D} [creates_limit K F] {c : cone (K ⋙ F)} (t : is_limit c) : F.map_cone (lift_limit t) ≅ c := (creates_limit.lifts c t).valid_lift /-- The lifted cone is a limit. -/ def lifted_limit_is_limit {K : J ⥤ C} {F : C ⥤ D} [creates_limit K F] {c : cone (K ⋙ F)} (t : is_limit c) : is_limit (lift_limit t) := reflects_limit.reflects (is_limit.of_iso_limit t (lifted_limit_maps_to_original t).symm) /-- If `F` creates the limit of `K` and `K ⋙ F` has a limit, then `K` has a limit. -/ def has_limit_of_created (K : J ⥤ C) (F : C ⥤ D) [has_limit (K ⋙ F)] [creates_limit K F] : has_limit K := { cone := lift_limit (limit.is_limit (K ⋙ F)), is_limit := lifted_limit_is_limit _ } /- Interface to the `creates_colimit` class. -/ /-- `lift_colimit t` is the cocone for `K` given by lifting the colimit `t` for `K ⋙ F`. -/ def lift_colimit {K : J ⥤ C} {F : C ⥤ D} [creates_colimit K F] {c : cocone (K ⋙ F)} (t : is_colimit c) : cocone K := (creates_colimit.lifts c t).lifted_cocone /-- The lifted cocone has an image isomorphic to the original cocone. -/ def lifted_colimit_maps_to_original {K : J ⥤ C} {F : C ⥤ D} [creates_colimit K F] {c : cocone (K ⋙ F)} (t : is_colimit c) : F.map_cocone (lift_colimit t) ≅ c := (creates_colimit.lifts c t).valid_lift /-- The lifted cocone is a colimit. -/ def lifted_colimit_is_colimit {K : J ⥤ C} {F : C ⥤ D} [creates_colimit K F] {c : cocone (K ⋙ F)} (t : is_colimit c) : is_colimit (lift_colimit t) := reflects_colimit.reflects (is_colimit.of_iso_colimit t (lifted_colimit_maps_to_original t).symm) /-- If `F` creates the limit of `K` and `K ⋙ F` has a limit, then `K` has a limit. -/ def has_colimit_of_created (K : J ⥤ C) (F : C ⥤ D) [has_colimit (K ⋙ F)] [creates_colimit K F] : has_colimit K := { cocone := lift_colimit (colimit.is_colimit (K ⋙ F)), is_colimit := lifted_colimit_is_colimit _ } /-- A helper to show a functor creates limits. In particular, if we can show that for any limit cone `c` for `K ⋙ F`, there is a lift of it which is a limit and `F` reflects isomorphisms, then `F` creates limits. Usually, `F` creating limits says that _any_ lift of `c` is a limit, but here we only need to show that our particular lift of `c` is a limit. -/ structure lifts_to_limit (K : J ⥤ C) (F : C ⥤ D) (c : cone (K ⋙ F)) (t : is_limit c) extends liftable_cone K F c := (makes_limit : is_limit lifted_cone) /-- A helper to show a functor creates colimits. In particular, if we can show that for any limit cocone `c` for `K ⋙ F`, there is a lift of it which is a limit and `F` reflects isomorphisms, then `F` creates colimits. Usually, `F` creating colimits says that _any_ lift of `c` is a colimit, but here we only need to show that our particular lift of `c` is a colimit. -/ structure lifts_to_colimit (K : J ⥤ C) (F : C ⥤ D) (c : cocone (K ⋙ F)) (t : is_colimit c) extends liftable_cocone K F c := (makes_colimit : is_colimit lifted_cocone) /-- If `F` reflects isomorphisms and we can lift any limit cone to a limit cone, then `F` creates limits. In particular here we don't need to assume that F reflects limits. -/ def creates_limit_of_reflects_iso {K : J ⥤ C} {F : C ⥤ D} [reflects_isomorphisms F] (h : Π c t, lifts_to_limit K F c t) : creates_limit K F := { lifts := λ c t, (h c t).to_liftable_cone, to_reflects_limit := { reflects := λ (d : cone K) (hd : is_limit (F.map_cone d)), begin let d' : cone K := (h (F.map_cone d) hd).to_liftable_cone.lifted_cone, let i : F.map_cone d' ≅ F.map_cone d := (h (F.map_cone d) hd).to_liftable_cone.valid_lift, let hd' : is_limit d' := (h (F.map_cone d) hd).makes_limit, let f : d ⟶ d' := hd'.lift_cone_morphism d, have : (cones.functoriality K F).map f = i.inv := (hd.of_iso_limit i.symm).uniq_cone_morphism, haveI : is_iso ((cones.functoriality K F).map f) := (by { rw this, apply_instance }), haveI : is_iso f := is_iso_of_reflects_iso f (cones.functoriality K F), exact is_limit.of_iso_limit hd' (as_iso f).symm, end } } /-- When `F` is fully faithful, and `has_limit (K ⋙ F)`, to show that `F` creates the limit for `K` it suffices to exhibit a lift of the chosen limit cone for `K ⋙ F`. -/ -- Notice however that even if the isomorphism is `iso.refl _`, -- this construction will insert additional identity morphisms in the cone maps, -- so the constructed limits may not be ideal, definitionally. def creates_limit_of_fully_faithful_of_lift {K : J ⥤ C} {F : C ⥤ D} [full F] [faithful F] [has_limit (K ⋙ F)] (c : cone K) (i : F.map_cone c ≅ limit.cone (K ⋙ F)) : creates_limit K F := creates_limit_of_reflects_iso (λ c' t, { lifted_cone := c, valid_lift := i.trans (is_limit.unique_up_to_iso (limit.is_limit _) t), makes_limit := is_limit.of_faithful F (is_limit.of_iso_limit (limit.is_limit _) i.symm) (λ s, F.preimage _) (λ s, F.image_preimage _) }) /-- When `F` is fully faithful, and `has_limit (K ⋙ F)`, to show that `F` creates the limit for `K` it suffices to show that the chosen limit point is in the essential image of `F`. -/ -- Notice however that even if the isomorphism is `iso.refl _`, -- this construction will insert additional identity morphisms in the cone maps, -- so the constructed limits may not be ideal, definitionally. def creates_limit_of_fully_faithful_of_iso {K : J ⥤ C} {F : C ⥤ D} [full F] [faithful F] [has_limit (K ⋙ F)] (X : C) (i : F.obj X ≅ limit (K ⋙ F)) : creates_limit K F := creates_limit_of_fully_faithful_of_lift ({ X := X, π := { app := λ j, F.preimage (i.hom ≫ limit.π (K ⋙ F) j), naturality' := λ Y Z f, F.map_injective (by { dsimp, simp, erw limit.w (K ⋙ F), }) }} : cone K) (by { fapply cones.ext, exact i, tidy, }) /-- `F` preserves the limit of `K` if it creates the limit and `K ⋙ F` has the limit. -/ instance preserves_limit_of_creates_limit_and_has_limit (K : J ⥤ C) (F : C ⥤ D) [creates_limit K F] [has_limit (K ⋙ F)] : preserves_limit K F := { preserves := λ c t, is_limit.of_iso_limit (limit.is_limit _) ((lifted_limit_maps_to_original (limit.is_limit _)).symm ≪≫ ((cones.functoriality K F).map_iso ((lifted_limit_is_limit (limit.is_limit _)).unique_up_to_iso t))) } /-- `F` preserves the limit of shape `J` if it creates these limits and `D` has them. -/ instance preserves_limit_of_shape_of_creates_limits_of_shape_and_has_limits_of_shape (F : C ⥤ D) [creates_limits_of_shape J F] [has_limits_of_shape J D] : preserves_limits_of_shape J F := { preserves_limit := λ K, category_theory.preserves_limit_of_creates_limit_and_has_limit K F } /-- `F` preserves limits if it creates limits and `D` has limits. -/ instance preserves_limits_of_creates_limits_and_has_limits (F : C ⥤ D) [creates_limits F] [has_limits D] : preserves_limits F := { preserves_limits_of_shape := λ J 𝒥, by exactI category_theory.preserves_limit_of_shape_of_creates_limits_of_shape_and_has_limits_of_shape F } /-- If `F` reflects isomorphisms and we can lift any limit cocone to a limit cocone, then `F` creates colimits. In particular here we don't need to assume that F reflects colimits. -/ def creates_colimit_of_reflects_iso {K : J ⥤ C} {F : C ⥤ D} [reflects_isomorphisms F] (h : Π c t, lifts_to_colimit K F c t) : creates_colimit K F := { lifts := λ c t, (h c t).to_liftable_cocone, to_reflects_colimit := { reflects := λ (d : cocone K) (hd : is_colimit (F.map_cocone d)), begin let d' : cocone K := (h (F.map_cocone d) hd).to_liftable_cocone.lifted_cocone, let i : F.map_cocone d' ≅ F.map_cocone d := (h (F.map_cocone d) hd).to_liftable_cocone.valid_lift, let hd' : is_colimit d' := (h (F.map_cocone d) hd).makes_colimit, let f : d' ⟶ d := hd'.desc_cocone_morphism d, have : (cocones.functoriality K F).map f = i.hom := (hd.of_iso_colimit i.symm).uniq_cocone_morphism, haveI : is_iso ((cocones.functoriality K F).map f) := (by { rw this, apply_instance }), haveI := is_iso_of_reflects_iso f (cocones.functoriality K F), exact is_colimit.of_iso_colimit hd' (as_iso f), end } } /-- `F` preserves the colimit of `K` if it creates the colimit and `K ⋙ F` has the colimit. -/ instance preserves_colimit_of_creates_colimit_and_has_colimit (K : J ⥤ C) (F : C ⥤ D) [creates_colimit K F] [has_colimit (K ⋙ F)] : preserves_colimit K F := { preserves := λ c t, is_colimit.of_iso_colimit (colimit.is_colimit _) ((lifted_colimit_maps_to_original (colimit.is_colimit _)).symm ≪≫ ((cocones.functoriality K F).map_iso ((lifted_colimit_is_colimit (colimit.is_colimit _)).unique_up_to_iso t))) } /-- `F` preserves the colimit of shape `J` if it creates these colimits and `D` has them. -/ instance preserves_colimit_of_shape_of_creates_colimits_of_shape_and_has_colimits_of_shape (F : C ⥤ D) [creates_colimits_of_shape J F] [has_colimits_of_shape J D] : preserves_colimits_of_shape J F := { preserves_colimit := λ K, category_theory.preserves_colimit_of_creates_colimit_and_has_colimit K F } /-- `F` preserves limits if it creates limits and `D` has limits. -/ instance preserves_colimits_of_creates_colimits_and_has_colimits (F : C ⥤ D) [creates_colimits F] [has_colimits D] : preserves_colimits F := { preserves_colimits_of_shape := λ J 𝒥, by exactI category_theory.preserves_colimit_of_shape_of_creates_colimits_of_shape_and_has_colimits_of_shape F } -- For the inhabited linter later. /-- If F creates the limit of K, any cone lifts to a limit. -/ def lifts_to_limit_of_creates (K : J ⥤ C) (F : C ⥤ D) [creates_limit K F] (c : cone (K ⋙ F)) (t : is_limit c) : lifts_to_limit K F c t := { lifted_cone := lift_limit t, valid_lift := lifted_limit_maps_to_original t, makes_limit := lifted_limit_is_limit t } -- For the inhabited linter later. /-- If F creates the colimit of K, any cocone lifts to a colimit. -/ def lifts_to_colimit_of_creates (K : J ⥤ C) (F : C ⥤ D) [creates_colimit K F] (c : cocone (K ⋙ F)) (t : is_colimit c) : lifts_to_colimit K F c t := { lifted_cocone := lift_colimit t, valid_lift := lifted_colimit_maps_to_original t, makes_colimit := lifted_colimit_is_colimit t } /-- Any cone lifts through the identity functor. -/ def id_lifts_cone (c : cone (K ⋙ 𝟭 C)) : liftable_cone K (𝟭 C) c := { lifted_cone := { X := c.X, π := c.π ≫ K.right_unitor.hom }, valid_lift := cones.ext (iso.refl _) (by tidy) } /-- The identity functor creates all limits. -/ instance id_creates_limits : creates_limits (𝟭 C) := { creates_limits_of_shape := λ J 𝒥, by exactI { creates_limit := λ F, { lifts := λ c t, id_lifts_cone c } } } /-- Any cocone lifts through the identity functor. -/ def id_lifts_cocone (c : cocone (K ⋙ 𝟭 C)) : liftable_cocone K (𝟭 C) c := { lifted_cocone := { X := c.X, ι := K.right_unitor.inv ≫ c.ι }, valid_lift := cocones.ext (iso.refl _) (by tidy) } /-- The identity functor creates all colimits. -/ instance id_creates_colimits : creates_colimits (𝟭 C) := { creates_colimits_of_shape := λ J 𝒥, by exactI { creates_colimit := λ F, { lifts := λ c t, id_lifts_cocone c } } } /-- Satisfy the inhabited linter -/ instance inhabited_liftable_cone (c : cone (K ⋙ 𝟭 C)) : inhabited (liftable_cone K (𝟭 C) c) := ⟨id_lifts_cone c⟩ instance inhabited_liftable_cocone (c : cocone (K ⋙ 𝟭 C)) : inhabited (liftable_cocone K (𝟭 C) c) := ⟨id_lifts_cocone c⟩ /-- Satisfy the inhabited linter -/ instance inhabited_lifts_to_limit (K : J ⥤ C) (F : C ⥤ D) [creates_limit K F] (c : cone (K ⋙ F)) (t : is_limit c) : inhabited (lifts_to_limit _ _ _ t) := ⟨lifts_to_limit_of_creates K F c t⟩ instance inhabited_lifts_to_colimit (K : J ⥤ C) (F : C ⥤ D) [creates_colimit K F] (c : cocone (K ⋙ F)) (t : is_colimit c) : inhabited (lifts_to_colimit _ _ _ t) := ⟨lifts_to_colimit_of_creates K F c t⟩ section comp variables {E : Type u₃} [ℰ : category.{v} E] variables (F : C ⥤ D) (G : D ⥤ E) instance comp_creates_limit [i₁ : creates_limit K F] [i₂ : creates_limit (K ⋙ F) G] : creates_limit K (F ⋙ G) := { lifts := λ c t, { lifted_cone := lift_limit (lifted_limit_is_limit t), valid_lift := (cones.functoriality (K ⋙ F) G).map_iso (lifted_limit_maps_to_original (lifted_limit_is_limit t)) ≪≫ (lifted_limit_maps_to_original t), } } end comp end creates end category_theory
5fd4c130ca1cbd0869b75ce062172ba596cae3e5
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/measure_theory/measure/null_measurable.lean
3786a0640063145d83e22bad7c3f58d5806329c0
[ "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
18,773
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Yury Kudryashov -/ import measure_theory.measure.ae_disjoint /-! # Null measurable sets and complete measures ## Main definitions ### Null measurable sets and functions A set `s : set α` is called *null measurable* (`measure_theory.null_measurable_set`) if it satisfies any of the following equivalent conditions: * there exists a measurable set `t` such that `s =ᵐ[μ] t` (this is used as a definition); * `measure_theory.to_measurable μ s =ᵐ[μ] s`; * there exists a measurable subset `t ⊆ s` such that `t =ᵐ[μ] s` (in this case the latter equality means that `μ (s \ t) = 0`); * `s` can be represented as a union of a measurable set and a set of measure zero; * `s` can be represented as a difference of a measurable set and a set of measure zero. Null measurable sets form a σ-algebra that is registered as a `measurable_space` instance on `measure_theory.null_measurable_space α μ`. We also say that `f : α → β` is `measure_theory.null_measurable` if the preimage of a measurable set is a null measurable set. In other words, `f : α → β` is null measurable if it is measurable as a function `measure_theory.null_measurable_space α μ → β`. ### Complete measures We say that a measure `μ` is complete w.r.t. the `measurable_space α` σ-algebra (or the σ-algebra is complete w.r.t measure `μ`) if every set of measure zero is measurable. In this case all null measurable sets and functions are measurable. For each measure `μ`, we define `measure_theory.measure.completion μ` to be the same measure interpreted as a measure on `measure_theory.null_measurable_space α μ` and prove that this is a complete measure. ## Implementation notes We define `measure_theory.null_measurable_set` as `@measurable_set (null_measurable_space α μ) _` so that theorems about `measurable_set`s like `measurable_set.union` can be applied to `null_measurable_set`s. However, these lemmas output terms of the same form `@measurable_set (null_measurable_space α μ) _ _`. While this is definitionally equal to the expected output `null_measurable_set s μ`, it looks different and may be misleading. So we copy all standard lemmas about measurable sets to the `measure_theory.null_measurable_set` namespace and fix the output type. ## Tags measurable, measure, null measurable, completion -/ open filter set encodable variables {ι α β γ : Type*} namespace measure_theory /-- A type tag for `α` with `measurable_set` given by `null_measurable_set`. -/ @[nolint unused_arguments] def null_measurable_space (α : Type*) [measurable_space α] (μ : measure α . volume_tac) : Type* := α section variables {m0 : measurable_space α} {μ : measure α} {s t : set α} instance [h : inhabited α] : inhabited (null_measurable_space α μ) := h instance [h : subsingleton α] : subsingleton (null_measurable_space α μ) := h instance : measurable_space (null_measurable_space α μ) := { measurable_set' := λ s, ∃ t, measurable_set t ∧ s =ᵐ[μ] t, measurable_set_empty := ⟨∅, measurable_set.empty, ae_eq_refl _⟩, measurable_set_compl := λ s ⟨t, htm, hts⟩, ⟨tᶜ, htm.compl, hts.compl⟩, measurable_set_Union := λ s hs, by { choose t htm hts using hs, exact ⟨⋃ i, t i, measurable_set.Union htm, eventually_eq.countable_Union hts⟩ } } /-- A set is called `null_measurable_set` if it can be approximated by a measurable set up to a set of null measure. -/ def null_measurable_set [measurable_space α] (s : set α) (μ : measure α . volume_tac) : Prop := @measurable_set (null_measurable_space α μ) _ s @[simp] lemma _root_.measurable_set.null_measurable_set (h : measurable_set s) : null_measurable_set s μ := ⟨s, h, ae_eq_refl _⟩ @[simp] lemma null_measurable_set_empty : null_measurable_set ∅ μ := measurable_set.empty @[simp] lemma null_measurable_set_univ : null_measurable_set univ μ := measurable_set.univ namespace null_measurable_set lemma of_null (h : μ s = 0) : null_measurable_set s μ := ⟨∅, measurable_set.empty, ae_eq_empty.2 h⟩ lemma compl (h : null_measurable_set s μ) : null_measurable_set sᶜ μ := h.compl lemma of_compl (h : null_measurable_set sᶜ μ) : null_measurable_set s μ := h.of_compl @[simp] lemma compl_iff : null_measurable_set sᶜ μ ↔ null_measurable_set s μ := measurable_set.compl_iff @[nontriviality] lemma of_subsingleton [subsingleton α] : null_measurable_set s μ := subsingleton.measurable_set protected lemma congr (hs : null_measurable_set s μ) (h : s =ᵐ[μ] t) : null_measurable_set t μ := let ⟨s', hm, hs'⟩ := hs in ⟨s', hm, h.symm.trans hs'⟩ protected lemma Union {ι : Sort*} [countable ι] {s : ι → set α} (h : ∀ i, null_measurable_set (s i) μ) : null_measurable_set (⋃ i, s i) μ := measurable_set.Union h protected lemma bUnion_decode₂ [encodable ι] ⦃f : ι → set α⦄ (h : ∀ i, null_measurable_set (f i) μ) (n : ℕ) : null_measurable_set (⋃ b ∈ encodable.decode₂ ι n, f b) μ := measurable_set.bUnion_decode₂ h n protected lemma bUnion {f : ι → set α} {s : set ι} (hs : s.countable) (h : ∀ b ∈ s, null_measurable_set (f b) μ) : null_measurable_set (⋃ b ∈ s, f b) μ := measurable_set.bUnion hs h protected lemma sUnion {s : set (set α)} (hs : s.countable) (h : ∀ t ∈ s, null_measurable_set t μ) : null_measurable_set (⋃₀ s) μ := by { rw sUnion_eq_bUnion, exact measurable_set.bUnion hs h } protected lemma Inter {ι : Sort*} [countable ι] {f : ι → set α} (h : ∀ i, null_measurable_set (f i) μ) : null_measurable_set (⋂ i, f i) μ := measurable_set.Inter h protected lemma bInter {f : β → set α} {s : set β} (hs : s.countable) (h : ∀ b ∈ s, null_measurable_set (f b) μ) : null_measurable_set (⋂ b ∈ s, f b) μ := measurable_set.bInter hs h protected lemma sInter {s : set (set α)} (hs : s.countable) (h : ∀ t ∈ s, null_measurable_set t μ) : null_measurable_set (⋂₀ s) μ := measurable_set.sInter hs h @[simp] protected lemma union (hs : null_measurable_set s μ) (ht : null_measurable_set t μ) : null_measurable_set (s ∪ t) μ := hs.union ht protected lemma union_null (hs : null_measurable_set s μ) (ht : μ t = 0) : null_measurable_set (s ∪ t) μ := hs.union (of_null ht) @[simp] protected lemma inter (hs : null_measurable_set s μ) (ht : null_measurable_set t μ) : null_measurable_set (s ∩ t) μ := hs.inter ht @[simp] protected lemma diff (hs : null_measurable_set s μ) (ht : null_measurable_set t μ) : null_measurable_set (s \ t) μ := hs.diff ht @[simp] protected lemma disjointed {f : ℕ → set α} (h : ∀ i, null_measurable_set (f i) μ) (n) : null_measurable_set (disjointed f n) μ := measurable_set.disjointed h n @[simp] protected lemma const (p : Prop) : null_measurable_set {a : α | p} μ := measurable_set.const p instance [measurable_singleton_class α] : measurable_singleton_class (null_measurable_space α μ) := ⟨λ x, (@measurable_set_singleton α _ _ x).null_measurable_set⟩ protected lemma insert [measurable_singleton_class (null_measurable_space α μ)] (hs : null_measurable_set s μ) (a : α) : null_measurable_set (insert a s) μ := hs.insert a lemma exists_measurable_superset_ae_eq (h : null_measurable_set s μ) : ∃ t ⊇ s, measurable_set t ∧ t =ᵐ[μ] s := begin rcases h with ⟨t, htm, hst⟩, refine ⟨t ∪ to_measurable μ (s \ t), _, htm.union (measurable_set_to_measurable _ _), _⟩, { exact diff_subset_iff.1 (subset_to_measurable _ _) }, { have : to_measurable μ (s \ t) =ᵐ[μ] (∅ : set α), by simp [ae_le_set.1 hst.le], simpa only [union_empty] using hst.symm.union this } end lemma to_measurable_ae_eq (h : null_measurable_set s μ) : to_measurable μ s =ᵐ[μ] s := begin rw [to_measurable, dif_pos], exact h.exists_measurable_superset_ae_eq.some_spec.snd.2 end lemma compl_to_measurable_compl_ae_eq (h : null_measurable_set s μ) : (to_measurable μ sᶜ)ᶜ =ᵐ[μ] s := by simpa only [compl_compl] using h.compl.to_measurable_ae_eq.compl lemma exists_measurable_subset_ae_eq (h : null_measurable_set s μ) : ∃ t ⊆ s, measurable_set t ∧ t =ᵐ[μ] s := ⟨(to_measurable μ sᶜ)ᶜ, compl_subset_comm.2 $ subset_to_measurable _ _, (measurable_set_to_measurable _ _).compl, h.compl_to_measurable_compl_ae_eq⟩ end null_measurable_set /-- If `sᵢ` is a countable family of (null) measurable pairwise `μ`-a.e. disjoint sets, then there exists a subordinate family `tᵢ ⊆ sᵢ` of measurable pairwise disjoint sets such that `tᵢ =ᵐ[μ] sᵢ`. -/ lemma exists_subordinate_pairwise_disjoint [countable ι] {s : ι → set α} (h : ∀ i, null_measurable_set (s i) μ) (hd : pairwise (ae_disjoint μ on s)) : ∃ t : ι → set α, (∀ i, t i ⊆ s i) ∧ (∀ i, s i =ᵐ[μ] t i) ∧ (∀ i, measurable_set (t i)) ∧ pairwise (disjoint on t) := begin choose t ht_sub htm ht_eq using λ i, (h i).exists_measurable_subset_ae_eq, rcases exists_null_pairwise_disjoint_diff hd with ⟨u, hum, hu₀, hud⟩, exact ⟨λ i, t i \ u i, λ i, (diff_subset _ _).trans (ht_sub _), λ i, (ht_eq _).symm.trans (diff_null_ae_eq_self (hu₀ i)).symm, λ i, (htm i).diff (hum i), hud.mono $ λ i j h, h.mono (diff_subset_diff_left (ht_sub i)) (diff_subset_diff_left (ht_sub j))⟩ end lemma measure_Union {m0 : measurable_space α} {μ : measure α} [countable ι] {f : ι → set α} (hn : pairwise (disjoint on f)) (h : ∀ i, measurable_set (f i)) : μ (⋃ i, f i) = ∑' i, μ (f i) := begin rw [measure_eq_extend (measurable_set.Union h), extend_Union measurable_set.empty _ measurable_set.Union _ hn h], { simp [measure_eq_extend, h] }, { exact μ.empty }, { exact μ.m_Union } end lemma measure_Union₀ [countable ι] {f : ι → set α} (hd : pairwise (ae_disjoint μ on f)) (h : ∀ i, null_measurable_set (f i) μ) : μ (⋃ i, f i) = ∑' i, μ (f i) := begin rcases exists_subordinate_pairwise_disjoint h hd with ⟨t, ht_sub, ht_eq, htm, htd⟩, calc μ (⋃ i, f i) = μ (⋃ i, t i) : measure_congr (eventually_eq.countable_Union ht_eq) ... = ∑' i, μ (t i) : measure_Union htd htm ... = ∑' i, μ (f i) : tsum_congr (λ i, measure_congr (ht_eq _).symm) end lemma measure_union₀_aux (hs : null_measurable_set s μ) (ht : null_measurable_set t μ) (hd : ae_disjoint μ s t) : μ (s ∪ t) = μ s + μ t := begin rw [union_eq_Union, measure_Union₀, tsum_fintype, fintype.sum_bool, cond, cond], exacts [(pairwise_on_bool ae_disjoint.symmetric).2 hd, λ b, bool.cases_on b ht hs] end /-- A null measurable set `t` is Carathéodory measurable: for any `s`, we have `μ (s ∩ t) + μ (s \ t) = μ s`. -/ lemma measure_inter_add_diff₀ (s : set α) (ht : null_measurable_set t μ) : μ (s ∩ t) + μ (s \ t) = μ s := begin refine le_antisymm _ _, { rcases exists_measurable_superset μ s with ⟨s', hsub, hs'm, hs'⟩, replace hs'm : null_measurable_set s' μ := hs'm.null_measurable_set, calc μ (s ∩ t) + μ (s \ t) ≤ μ (s' ∩ t) + μ (s' \ t) : add_le_add (measure_mono $ inter_subset_inter_left _ hsub) (measure_mono $ diff_subset_diff_left hsub) ... = μ (s' ∩ t ∪ s' \ t) : (measure_union₀_aux (hs'm.inter ht) (hs'm.diff ht) $ (@disjoint_inf_sdiff _ s' t _).ae_disjoint).symm ... = μ s' : congr_arg μ (inter_union_diff _ _) ... = μ s : hs' }, { calc μ s = μ (s ∩ t ∪ s \ t) : by rw inter_union_diff ... ≤ μ (s ∩ t) + μ (s \ t) : measure_union_le _ _ } end lemma measure_union_add_inter₀ (s : set α) (ht : null_measurable_set t μ) : μ (s ∪ t) + μ (s ∩ t) = μ s + μ t := by rw [← measure_inter_add_diff₀ (s ∪ t) ht, union_inter_cancel_right, union_diff_right, ← measure_inter_add_diff₀ s ht, add_comm, ← add_assoc, add_right_comm] lemma measure_union_add_inter₀' (hs : null_measurable_set s μ) (t : set α) : μ (s ∪ t) + μ (s ∩ t) = μ s + μ t := by rw [union_comm, inter_comm, measure_union_add_inter₀ t hs, add_comm] lemma measure_union₀ (ht : null_measurable_set t μ) (hd : ae_disjoint μ s t) : μ (s ∪ t) = μ s + μ t := by rw [← measure_union_add_inter₀ s ht, hd.eq, add_zero] lemma measure_union₀' (hs : null_measurable_set s μ) (hd : ae_disjoint μ s t) : μ (s ∪ t) = μ s + μ t := by rw [union_comm, measure_union₀ hs hd.symm, add_comm] lemma measure_add_measure_compl₀ {s : set α} (hs : null_measurable_set s μ) : μ s + μ sᶜ = μ univ := by rw [← measure_union₀' hs ae_disjoint_compl_right, union_compl_self] section measurable_singleton_class variable [measurable_singleton_class (null_measurable_space α μ)] lemma null_measurable_set_singleton (x : α) : null_measurable_set {x} μ := measurable_set_singleton x @[simp] lemma null_measurable_set_insert {a : α} {s : set α} : null_measurable_set (insert a s) μ ↔ null_measurable_set s μ := measurable_set_insert lemma null_measurable_set_eq {a : α} : null_measurable_set {x | x = a} μ := null_measurable_set_singleton a protected lemma _root_.set.finite.null_measurable_set (hs : s.finite) : null_measurable_set s μ := finite.measurable_set hs protected lemma _root_.finset.null_measurable_set (s : finset α) : null_measurable_set ↑s μ := finset.measurable_set s end measurable_singleton_class lemma _root_.set.finite.null_measurable_set_bUnion {f : ι → set α} {s : set ι} (hs : s.finite) (h : ∀ b ∈ s, null_measurable_set (f b) μ) : null_measurable_set (⋃ b ∈ s, f b) μ := finite.measurable_set_bUnion hs h lemma _root_.finset.null_measurable_set_bUnion {f : ι → set α} (s : finset ι) (h : ∀ b ∈ s, null_measurable_set (f b) μ) : null_measurable_set (⋃ b ∈ s, f b) μ := finset.measurable_set_bUnion s h lemma _root_.set.finite.null_measurable_set_sUnion {s : set (set α)} (hs : s.finite) (h : ∀ t ∈ s, null_measurable_set t μ) : null_measurable_set (⋃₀ s) μ := finite.measurable_set_sUnion hs h lemma _root_.set.finite.null_measurable_set_bInter {f : ι → set α} {s : set ι} (hs : s.finite) (h : ∀ b ∈ s, null_measurable_set (f b) μ) : null_measurable_set (⋂ b ∈ s, f b) μ := finite.measurable_set_bInter hs h lemma _root_.finset.null_measurable_set_bInter {f : ι → set α} (s : finset ι) (h : ∀ b ∈ s, null_measurable_set (f b) μ) : null_measurable_set (⋂ b ∈ s, f b) μ := s.finite_to_set.null_measurable_set_bInter h lemma _root_.set.finite.null_measurable_set_sInter {s : set (set α)} (hs : s.finite) (h : ∀ t ∈ s, null_measurable_set t μ) : null_measurable_set (⋂₀ s) μ := null_measurable_set.sInter hs.countable h lemma null_measurable_set_to_measurable : null_measurable_set (to_measurable μ s) μ := (measurable_set_to_measurable _ _).null_measurable_set end section null_measurable variables [measurable_space α] [measurable_space β] [measurable_space γ] {f : α → β} {μ : measure α} /-- A function `f : α → β` is null measurable if the preimage of a measurable set is a null measurable set. -/ def null_measurable (f : α → β) (μ : measure α . volume_tac) : Prop := ∀ ⦃s : set β⦄, measurable_set s → null_measurable_set (f ⁻¹' s) μ protected lemma _root_.measurable.null_measurable (h : measurable f) : null_measurable f μ := λ s hs, (h hs).null_measurable_set protected lemma null_measurable.measurable' (h : null_measurable f μ) : @measurable (null_measurable_space α μ) β _ _ f := h lemma measurable.comp_null_measurable {g : β → γ} (hg : measurable g) (hf : null_measurable f μ) : null_measurable (g ∘ f) μ := hg.comp hf lemma null_measurable.congr {g : α → β} (hf : null_measurable f μ) (hg : f =ᵐ[μ] g) : null_measurable g μ := λ s hs, (hf hs).congr $ eventually_eq_set.2 $ hg.mono $ λ x hx, by rw [mem_preimage, mem_preimage, hx] end null_measurable section is_complete /-- A measure is complete if every null set is also measurable. A null set is a subset of a measurable set with measure `0`. Since every measure is defined as a special case of an outer measure, we can more simply state that a set `s` is null if `μ s = 0`. -/ class measure.is_complete {_ : measurable_space α} (μ : measure α) : Prop := (out' : ∀ s, μ s = 0 → measurable_set s) variables {m0 : measurable_space α} {μ : measure α} {s t : set α} theorem measure.is_complete_iff : μ.is_complete ↔ ∀ s, μ s = 0 → measurable_set s := ⟨λ h, h.1, λ h, ⟨h⟩⟩ theorem measure.is_complete.out (h : μ.is_complete) : ∀ s, μ s = 0 → measurable_set s := h.1 theorem measurable_set_of_null [μ.is_complete] (hs : μ s = 0) : measurable_set s := measure_theory.measure.is_complete.out' s hs theorem null_measurable_set.measurable_of_complete (hs : null_measurable_set s μ) [μ.is_complete] : measurable_set s := diff_diff_cancel_left (subset_to_measurable μ s) ▸ (measurable_set_to_measurable _ _).diff (measurable_set_of_null (ae_le_set.1 hs.to_measurable_ae_eq.le)) theorem null_measurable.measurable_of_complete [μ.is_complete] {m1 : measurable_space β} {f : α → β} (hf : null_measurable f μ) : measurable f := λ s hs, (hf hs).measurable_of_complete lemma _root_.measurable.congr_ae {α β} [measurable_space α] [measurable_space β] {μ : measure α} [hμ : μ.is_complete] {f g : α → β} (hf : measurable f) (hfg : f =ᵐ[μ] g) : measurable g := (hf.null_measurable.congr hfg).measurable_of_complete namespace measure /-- Given a measure we can complete it to a (complete) measure on all null measurable sets. -/ def completion {_ : measurable_space α} (μ : measure α) : @measure_theory.measure (null_measurable_space α μ) _ := { to_outer_measure := μ.to_outer_measure, m_Union := λ s hs hd, measure_Union₀ (hd.mono $ λ i j h, h.ae_disjoint) hs, trimmed := begin refine le_antisymm (λ s, _) (outer_measure.le_trim _), rw outer_measure.trim_eq_infi, simp only [to_outer_measure_apply], refine (infi₂_mono _).trans_eq (measure_eq_infi _).symm, exact λ t ht, infi_mono' (λ h, ⟨h.null_measurable_set, le_rfl⟩) end } instance completion.is_complete {m : measurable_space α} (μ : measure α) : μ.completion.is_complete := ⟨λ z hz, null_measurable_set.of_null hz⟩ @[simp] lemma coe_completion {_ : measurable_space α} (μ : measure α) : ⇑μ.completion = μ := rfl lemma completion_apply {_ : measurable_space α} (μ : measure α) (s : set α) : μ.completion s = μ s := rfl end measure end is_complete end measure_theory
2e46883e5c5f7d87000496c03d1e3e898237e5fe
618003631150032a5676f229d13a079ac875ff77
/src/topology/instances/real_vector_space.lean
4b97a552f691f4af273f1b395af78dd14ae26543
[ "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
1,491
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Yury Kudryashov -/ import topology.algebra.module import topology.instances.real /-! # Continuous additive maps are `ℝ`-linear In this file we prove that a continuous map `f : E →+ F` between two topological vector spaces over `ℝ` is `ℝ`-linear -/ variables {E : Type*} [add_comm_group E] [vector_space ℝ E] [topological_space E] [topological_vector_space ℝ E] {F : Type*} [add_comm_group F] [vector_space ℝ F] [topological_space F] [topological_vector_space ℝ F] [t2_space F] namespace add_monoid_hom /-- A continuous additive map between two vector spaces over `ℝ` is `ℝ`-linear. -/ lemma map_real_smul (f : E →+ F) (hf : continuous f) (c : ℝ) (x : E) : f (c • x) = c • f x := suffices (λ c : ℝ, f (c • x)) = λ c : ℝ, c • f x, from congr_fun this c, dense_embedding_of_rat.dense.equalizer (hf.comp $ continuous_id.smul continuous_const) (continuous_id.smul continuous_const) (funext $ λ r, f.map_rat_cast_smul r x) /-- Reinterpret a continuous additive homomorphism between two real vector spaces as a continuous real-linear map. -/ def to_real_linear_map (f : E →+ F) (hf : continuous f) : E →L[ℝ] F := ⟨⟨f, f.map_add, f.map_real_smul hf⟩, hf⟩ @[simp] lemma coe_to_real_linear_map (f : E →+ F) (hf : continuous f) : ⇑(f.to_real_linear_map hf) = f := rfl end add_monoid_hom
ed03accf7d8cebcec923f41432b88a3d45e18932
9dc8cecdf3c4634764a18254e94d43da07142918
/src/category_theory/simple.lean
c7f2433aeaccdc6f6357ada20d31d94cbe2a315f
[ "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
8,692
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel, Scott Morrison -/ import category_theory.limits.shapes.zero_morphisms import category_theory.limits.shapes.kernels import category_theory.abelian.basic import category_theory.subobject.lattice import order.atoms /-! # Simple objects We define simple objects in any category with zero morphisms. A simple object is an object `Y` such that any monomorphism `f : X ⟶ Y` is either an isomorphism or zero (but not both). This is formalized as a `Prop` valued typeclass `simple X`. In some contexts, especially representation theory, simple objects are called "irreducibles". If a morphism `f` out of a simple object is nonzero and has a kernel, then that kernel is zero. (We state this as `kernel.ι f = 0`, but should add `kernel f ≅ 0`.) When the category is abelian, being simple is the same as being cosimple (although we do not state a separate typeclass for this). As a consequence, any nonzero epimorphism out of a simple object is an isomorphism, and any nonzero morphism into a simple object has trivial cokernel. We show that any simple object is indecomposable. -/ noncomputable theory open category_theory.limits namespace category_theory universes v u variables {C : Type u} [category.{v} C] section variables [has_zero_morphisms C] /-- An object is simple if monomorphisms into it are (exclusively) either isomorphisms or zero. -/ class simple (X : C) : Prop := (mono_is_iso_iff_nonzero : ∀ {Y : C} (f : Y ⟶ X) [mono f], is_iso f ↔ (f ≠ 0)) /-- A nonzero monomorphism to a simple object is an isomorphism. -/ lemma is_iso_of_mono_of_nonzero {X Y : C} [simple Y] {f : X ⟶ Y} [mono f] (w : f ≠ 0) : is_iso f := (simple.mono_is_iso_iff_nonzero f).mpr w lemma simple.of_iso {X Y : C} [simple Y] (i : X ≅ Y) : simple X := { mono_is_iso_iff_nonzero := λ Z f m, begin resetI, haveI : mono (f ≫ i.hom) := mono_comp _ _, split, { introsI h w, haveI j : is_iso (f ≫ i.hom), apply_instance, rw simple.mono_is_iso_iff_nonzero at j, unfreezingI { subst w, }, simpa using j, }, { intro h, haveI j : is_iso (f ≫ i.hom), { apply is_iso_of_mono_of_nonzero, intro w, apply h, simpa using (cancel_mono i.inv).2 w, }, rw [←category.comp_id f, ←i.hom_inv_id, ←category.assoc], apply_instance, }, end } lemma kernel_zero_of_nonzero_from_simple {X Y : C} [simple X] {f : X ⟶ Y} [has_kernel f] (w : f ≠ 0) : kernel.ι f = 0 := begin classical, by_contra, haveI := is_iso_of_mono_of_nonzero h, exact w (eq_zero_of_epi_kernel f), end /-- A nonzero morphism `f` to a simple object is an epimorphism (assuming `f` has an image, and `C` has equalizers). -/ -- See also `mono_of_nonzero_from_simple`, which requires `preadditive C`. lemma epi_of_nonzero_to_simple [has_equalizers C] {X Y : C} [simple Y] {f : X ⟶ Y} [has_image f] (w : f ≠ 0) : epi f := begin rw ←image.fac f, haveI : is_iso (image.ι f) := is_iso_of_mono_of_nonzero (λ h, w (eq_zero_of_image_eq_zero h)), apply epi_comp, end lemma mono_to_simple_zero_of_not_iso {X Y : C} [simple Y] {f : X ⟶ Y} [mono f] (w : is_iso f → false) : f = 0 := begin classical, by_contra, exact w (is_iso_of_mono_of_nonzero h) end lemma id_nonzero (X : C) [simple.{v} X] : 𝟙 X ≠ 0 := (simple.mono_is_iso_iff_nonzero (𝟙 X)).mp (by apply_instance) instance (X : C) [simple.{v} X] : nontrivial (End X) := nontrivial_of_ne 1 0 (id_nonzero X) section lemma simple.not_is_zero (X : C) [simple X] : ¬ is_zero X := by simpa [limits.is_zero.iff_id_eq_zero] using id_nonzero X variable [has_zero_object C] open_locale zero_object variables (C) /-- We don't want the definition of 'simple' to include the zero object, so we check that here. -/ lemma zero_not_simple [simple (0 : C)] : false := (simple.mono_is_iso_iff_nonzero (0 : (0 : C) ⟶ (0 : C))).mp ⟨⟨0, by tidy⟩⟩ rfl end end -- We next make the dual arguments, but for this we must be in an abelian category. section abelian variables [abelian C] /-- In an abelian category, an object satisfying the dual of the definition of a simple object is simple. -/ lemma simple_of_cosimple (X : C) (h : ∀ {Z : C} (f : X ⟶ Z) [epi f], is_iso f ↔ (f ≠ 0)) : simple X := ⟨λ Y f I, begin classical, fsplit, { introsI, have hx := cokernel.π_of_epi f, by_contra, substI h, exact (h _).mp (cokernel.π_of_zero _ _) hx }, { intro hf, suffices : epi f, { exactI is_iso_of_mono_of_epi _ }, apply preadditive.epi_of_cokernel_zero, by_contra h', exact cokernel_not_iso_of_nonzero hf ((h _).mpr h') } end⟩ /-- A nonzero epimorphism from a simple object is an isomorphism. -/ lemma is_iso_of_epi_of_nonzero {X Y : C} [simple X] {f : X ⟶ Y} [epi f] (w : f ≠ 0) : is_iso f := begin -- `f ≠ 0` means that `kernel.ι f` is not an iso, and hence zero, and hence `f` is a mono. haveI : mono f := preadditive.mono_of_kernel_zero (mono_to_simple_zero_of_not_iso (kernel_not_iso_of_nonzero w)), exact is_iso_of_mono_of_epi f, end lemma cokernel_zero_of_nonzero_to_simple {X Y : C} [simple Y] {f : X ⟶ Y} (w : f ≠ 0) : cokernel.π f = 0 := begin classical, by_contradiction h, haveI := is_iso_of_epi_of_nonzero h, exact w (eq_zero_of_mono_cokernel f), end lemma epi_from_simple_zero_of_not_iso {X Y : C} [simple X] {f : X ⟶ Y} [epi f] (w : is_iso f → false) : f = 0 := begin classical, by_contra, exact w (is_iso_of_epi_of_nonzero h), end end abelian section indecomposable variables [preadditive C] [has_binary_biproducts C] -- There are another three potential variations of this lemma, -- but as any one suffices to prove `indecomposable_of_simple` we will not give them all. lemma biprod.is_iso_inl_iff_is_zero (X Y : C) : is_iso (biprod.inl : X ⟶ X ⊞ Y) ↔ is_zero Y := begin rw [biprod.is_iso_inl_iff_id_eq_fst_comp_inl, ←biprod.total, add_right_eq_self], split, { intro h, replace h := h =≫ biprod.snd, simpa [←is_zero.iff_is_split_epi_eq_zero (biprod.snd : X ⊞ Y ⟶ Y)] using h, }, { intro h, rw is_zero.iff_is_split_epi_eq_zero (biprod.snd : X ⊞ Y ⟶ Y) at h, rw [h, zero_comp], }, end /-- Any simple object in a preadditive category is indecomposable. -/ lemma indecomposable_of_simple (X : C) [simple X] : indecomposable X := ⟨simple.not_is_zero X, λ Y Z i, begin refine or_iff_not_imp_left.mpr (λ h, _), rw is_zero.iff_is_split_mono_eq_zero (biprod.inl : Y ⟶ Y ⊞ Z) at h, change biprod.inl ≠ 0 at h, rw ←(simple.mono_is_iso_iff_nonzero biprod.inl) at h, { rwa biprod.is_iso_inl_iff_is_zero at h, }, { exact simple.of_iso i.symm, }, { apply_instance, }, end⟩ end indecomposable section subobject variables [has_zero_morphisms C] [has_zero_object C] open_locale zero_object open subobject instance {X : C} [simple X] : nontrivial (subobject X) := nontrivial_of_not_is_zero (simple.not_is_zero X) instance {X : C} [simple X] : is_simple_order (subobject X) := { eq_bot_or_eq_top := begin rintro ⟨⟨⟨(Y : C), ⟨⟨⟩⟩, (f : Y ⟶ X)⟩, (m : mono f)⟩⟩, resetI, change mk f = ⊥ ∨ mk f = ⊤, by_cases h : f = 0, { exact or.inl (mk_eq_bot_iff_zero.mpr h), }, { refine or.inr ((is_iso_iff_mk_eq_top _).mp ((simple.mono_is_iso_iff_nonzero f).mpr h)), } end, } /-- If `X` has subobject lattice `{⊥, ⊤}`, then `X` is simple. -/ lemma simple_of_is_simple_order_subobject (X : C) [is_simple_order (subobject X)] : simple X := begin split, introsI, split, { introI i, rw subobject.is_iso_iff_mk_eq_top at i, intro w, rw ←subobject.mk_eq_bot_iff_zero at w, exact is_simple_order.bot_ne_top (w.symm.trans i), }, { intro i, rcases is_simple_order.eq_bot_or_eq_top (subobject.mk f) with h|h, { rw subobject.mk_eq_bot_iff_zero at h, exact false.elim (i h), }, { exact (subobject.is_iso_iff_mk_eq_top _).mpr h, }, } end /-- `X` is simple iff it has subobject lattice `{⊥, ⊤}`. -/ lemma simple_iff_subobject_is_simple_order (X : C) : simple X ↔ is_simple_order (subobject X) := ⟨by { introI h, apply_instance, }, by { introI h, exact simple_of_is_simple_order_subobject X, }⟩ /-- A subobject is simple iff it is an atom in the subobject lattice. -/ lemma subobject_simple_iff_is_atom {X : C} (Y : subobject X) : simple (Y : C) ↔ is_atom Y := (simple_iff_subobject_is_simple_order _).trans ((order_iso.is_simple_order_iff (subobject_order_iso Y)).trans set.is_simple_order_Iic_iff_is_atom) end subobject end category_theory
17f88dbe435ca4f6af98df3d34a6393f648c9f8c
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/real/ennreal.lean
eb95ca8a41007a7cdaae4c1770221ba6c36364ec
[ "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
84,660
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, Yury Kudryashov -/ import data.real.nnreal import algebra.order.sub.with_top /-! # Extended non-negative reals We define `ennreal = ℝ≥0∞ := with_top ℝ≥0` to be the type of extended nonnegative real numbers, i.e., the interval `[0, +∞]`. This type is used as the codomain of a `measure_theory.measure`, and of the extended distance `edist` in a `emetric_space`. In this file we define some algebraic operations and a linear order on `ℝ≥0∞` and prove basic properties of these operations, order, and conversions to/from `ℝ`, `ℝ≥0`, and `ℕ`. ## Main definitions * `ℝ≥0∞`: the extended nonnegative real numbers `[0, ∞]`; defined as `with_top ℝ≥0`; it is equipped with the following structures: - coercion from `ℝ≥0` defined in the natural way; - the natural structure of a complete dense linear order: `↑p ≤ ↑q ↔ p ≤ q` and `∀ a, a ≤ ∞`; - `a + b` is defined so that `↑p + ↑q = ↑(p + q)` for `(p q : ℝ≥0)` and `a + ∞ = ∞ + a = ∞`; - `a * b` is defined so that `↑p * ↑q = ↑(p * q)` for `(p q : ℝ≥0)`, `0 * ∞ = ∞ * 0 = 0`, and `a * ∞ = ∞ * a = ∞` for `a ≠ 0`; - `a - b` is defined as the minimal `d` such that `a ≤ d + b`; this way we have `↑p - ↑q = ↑(p - q)`, `∞ - ↑p = ∞`, `↑p - ∞ = ∞ - ∞ = 0`; note that there is no negation, only subtraction; - `a⁻¹` is defined as `Inf {b | 1 ≤ a * b}`. This way we have `(↑p)⁻¹ = ↑(p⁻¹)` for `p : ℝ≥0`, `p ≠ 0`, `0⁻¹ = ∞`, and `∞⁻¹ = 0`. - `a / b` is defined as `a * b⁻¹`. The addition and multiplication defined this way together with `0 = ↑0` and `1 = ↑1` turn `ℝ≥0∞` into a canonically ordered commutative semiring of characteristic zero. * Coercions to/from other types: - coercion `ℝ≥0 → ℝ≥0∞` is defined as `has_coe`, so one can use `(p : ℝ≥0)` in a context that expects `a : ℝ≥0∞`, and Lean will apply `coe` automatically; - `ennreal.to_nnreal` sends `↑p` to `p` and `∞` to `0`; - `ennreal.to_real := coe ∘ ennreal.to_nnreal` sends `↑p`, `p : ℝ≥0` to `(↑p : ℝ)` and `∞` to `0`; - `ennreal.of_real := coe ∘ real.to_nnreal` sends `x : ℝ` to `↑⟨max x 0, _⟩` - `ennreal.ne_top_equiv_nnreal` is an equivalence between `{a : ℝ≥0∞ // a ≠ 0}` and `ℝ≥0`. ## Implementation notes We define a `can_lift ℝ≥0∞ ℝ≥0` instance, so one of the ways to prove theorems about an `ℝ≥0∞` number `a` is to consider the cases `a = ∞` and `a ≠ ∞`, and use the tactic `lift a to ℝ≥0 using ha` in the second case. This instance is even more useful if one already has `ha : a ≠ ∞` in the context, or if we have `(f : α → ℝ≥0∞) (hf : ∀ x, f x ≠ ∞)`. ## Notations * `ℝ≥0∞`: the type of the extended nonnegative real numbers; * `ℝ≥0`: the type of nonnegative real numbers `[0, ∞)`; defined in `data.real.nnreal`; * `∞`: a localized notation in `ℝ≥0∞` for `⊤ : ℝ≥0∞`. -/ open classical set open_locale classical big_operators nnreal variables {α : Type*} {β : Type*} /-- The extended nonnegative real numbers. This is usually denoted [0, ∞], and is relevant as the codomain of a measure. -/ @[derive [ has_zero, add_comm_monoid_with_one, semilattice_sup, distrib_lattice, order_bot, bounded_order, canonically_ordered_comm_semiring, complete_linear_order, densely_ordered, nontrivial, canonically_linear_ordered_add_monoid, has_sub, has_ordered_sub, linear_ordered_add_comm_monoid_with_top]] def ennreal := with_top ℝ≥0 localized "notation (name := ennreal) `ℝ≥0∞` := ennreal" in ennreal localized "notation (name := ennreal.top) `∞` := (⊤ : ennreal)" in ennreal namespace ennreal variables {a b c d : ℝ≥0∞} {r p q : ℝ≥0} -- TODO: why are the two covariant instances necessary? why aren't they inferred? instance covariant_class_mul_le : covariant_class ℝ≥0∞ ℝ≥0∞ (*) (≤) := canonically_ordered_comm_semiring.to_covariant_mul_le instance covariant_class_add_le : covariant_class ℝ≥0∞ ℝ≥0∞ (+) (≤) := ordered_add_comm_monoid.to_covariant_class_left ℝ≥0∞ noncomputable instance : linear_ordered_comm_monoid_with_zero ℝ≥0∞ := { mul_le_mul_left := λ a b, mul_le_mul_left', zero_le_one := zero_le 1, .. ennreal.linear_ordered_add_comm_monoid_with_top, .. (show comm_semiring ℝ≥0∞, from infer_instance) } instance : inhabited ℝ≥0∞ := ⟨0⟩ instance : has_coe ℝ≥0 ℝ≥0∞ := ⟨ option.some ⟩ instance can_lift : can_lift ℝ≥0∞ ℝ≥0 coe (λ r, r ≠ ∞) := { prf := λ x hx, ⟨option.get $ option.ne_none_iff_is_some.1 hx, option.some_get _⟩ } @[simp] lemma none_eq_top : (none : ℝ≥0∞) = ∞ := rfl @[simp] lemma some_eq_coe (a : ℝ≥0) : (some a : ℝ≥0∞) = (↑a : ℝ≥0∞) := rfl /-- `to_nnreal x` returns `x` if it is real, otherwise 0. -/ protected def to_nnreal : ℝ≥0∞ → ℝ≥0 := with_top.untop' 0 /-- `to_real x` returns `x` if it is real, `0` otherwise. -/ protected def to_real (a : ℝ≥0∞) : real := coe (a.to_nnreal) /-- `of_real x` returns `x` if it is nonnegative, `0` otherwise. -/ protected noncomputable def of_real (r : real) : ℝ≥0∞ := coe (real.to_nnreal r) @[simp, norm_cast] lemma to_nnreal_coe : (r : ℝ≥0∞).to_nnreal = r := rfl @[simp] lemma coe_to_nnreal : ∀{a:ℝ≥0∞}, a ≠ ∞ → ↑(a.to_nnreal) = a | (some r) h := rfl | none h := (h rfl).elim @[simp] lemma of_real_to_real {a : ℝ≥0∞} (h : a ≠ ∞) : ennreal.of_real (a.to_real) = a := by simp [ennreal.to_real, ennreal.of_real, h] @[simp] lemma to_real_of_real {r : ℝ} (h : 0 ≤ r) : ennreal.to_real (ennreal.of_real r) = r := by simp [ennreal.to_real, ennreal.of_real, real.coe_to_nnreal _ h] lemma to_real_of_real' {r : ℝ} : ennreal.to_real (ennreal.of_real r) = max r 0 := rfl lemma coe_to_nnreal_le_self : ∀{a:ℝ≥0∞}, ↑(a.to_nnreal) ≤ a | (some r) := by rw [some_eq_coe, to_nnreal_coe]; exact le_rfl | none := le_top lemma coe_nnreal_eq (r : ℝ≥0) : (r : ℝ≥0∞) = ennreal.of_real r := by { rw [ennreal.of_real, real.to_nnreal], cases r with r h, congr, dsimp, rw max_eq_left h } lemma of_real_eq_coe_nnreal {x : ℝ} (h : 0 ≤ x) : ennreal.of_real x = @coe ℝ≥0 ℝ≥0∞ _ (⟨x, h⟩ : ℝ≥0) := by { rw [coe_nnreal_eq], refl } @[simp] lemma of_real_coe_nnreal : ennreal.of_real p = p := (coe_nnreal_eq p).symm @[simp, norm_cast] lemma coe_zero : ↑(0 : ℝ≥0) = (0 : ℝ≥0∞) := rfl @[simp, norm_cast] lemma coe_one : ↑(1 : ℝ≥0) = (1 : ℝ≥0∞) := rfl @[simp] lemma to_real_nonneg {a : ℝ≥0∞} : 0 ≤ a.to_real := by simp [ennreal.to_real] @[simp] lemma top_to_nnreal : ∞.to_nnreal = 0 := rfl @[simp] lemma top_to_real : ∞.to_real = 0 := rfl @[simp] lemma one_to_real : (1 : ℝ≥0∞).to_real = 1 := rfl @[simp] lemma one_to_nnreal : (1 : ℝ≥0∞).to_nnreal = 1 := rfl @[simp] lemma coe_to_real (r : ℝ≥0) : (r : ℝ≥0∞).to_real = r := rfl @[simp] lemma zero_to_nnreal : (0 : ℝ≥0∞).to_nnreal = 0 := rfl @[simp] lemma zero_to_real : (0 : ℝ≥0∞).to_real = 0 := rfl @[simp] lemma of_real_zero : ennreal.of_real (0 : ℝ) = 0 := by simp [ennreal.of_real]; refl @[simp] lemma of_real_one : ennreal.of_real (1 : ℝ) = (1 : ℝ≥0∞) := by simp [ennreal.of_real] lemma of_real_to_real_le {a : ℝ≥0∞} : ennreal.of_real (a.to_real) ≤ a := if ha : a = ∞ then ha.symm ▸ le_top else le_of_eq (of_real_to_real ha) lemma forall_ennreal {p : ℝ≥0∞ → Prop} : (∀a, p a) ↔ (∀r:ℝ≥0, p r) ∧ p ∞ := ⟨assume h, ⟨assume r, h _, h _⟩, assume ⟨h₁, h₂⟩ a, match a with some r := h₁ _ | none := h₂ end⟩ lemma forall_ne_top {p : ℝ≥0∞ → Prop} : (∀ a ≠ ∞, p a) ↔ ∀ r : ℝ≥0, p r := option.ball_ne_none lemma exists_ne_top {p : ℝ≥0∞ → Prop} : (∃ a ≠ ∞, p a) ↔ ∃ r : ℝ≥0, p r := option.bex_ne_none lemma to_nnreal_eq_zero_iff (x : ℝ≥0∞) : x.to_nnreal = 0 ↔ x = 0 ∨ x = ∞ := ⟨begin cases x, { simp [none_eq_top] }, { rintro (rfl : x = 0), exact or.inl rfl }, end, by rintro (h | h); simp [h]⟩ lemma to_real_eq_zero_iff (x : ℝ≥0∞) : x.to_real = 0 ↔ x = 0 ∨ x = ∞ := by simp [ennreal.to_real, to_nnreal_eq_zero_iff] lemma to_nnreal_eq_one_iff (x : ℝ≥0∞) : x.to_nnreal = 1 ↔ x = 1 := begin refine ⟨λ h, _, congr_arg _⟩, cases x, { exact false.elim (zero_ne_one $ ennreal.top_to_nnreal.symm.trans h) }, { exact congr_arg _ h } end lemma to_real_eq_one_iff (x : ℝ≥0∞) : x.to_real = 1 ↔ x = 1 := by rw [ennreal.to_real, nnreal.coe_eq_one, ennreal.to_nnreal_eq_one_iff] @[simp] lemma coe_ne_top : (r : ℝ≥0∞) ≠ ∞ := with_top.coe_ne_top @[simp] lemma top_ne_coe : ∞ ≠ (r : ℝ≥0∞) := with_top.top_ne_coe @[simp] lemma of_real_ne_top {r : ℝ} : ennreal.of_real r ≠ ∞ := by simp [ennreal.of_real] @[simp] lemma of_real_lt_top {r : ℝ} : ennreal.of_real r < ∞ := lt_top_iff_ne_top.2 of_real_ne_top @[simp] lemma top_ne_of_real {r : ℝ} : ∞ ≠ ennreal.of_real r := by simp [ennreal.of_real] @[simp] lemma zero_ne_top : 0 ≠ ∞ := coe_ne_top @[simp] lemma top_ne_zero : ∞ ≠ 0 := top_ne_coe @[simp] lemma one_ne_top : 1 ≠ ∞ := coe_ne_top @[simp] lemma top_ne_one : ∞ ≠ 1 := top_ne_coe @[simp, norm_cast] lemma coe_eq_coe : (↑r : ℝ≥0∞) = ↑q ↔ r = q := with_top.coe_eq_coe @[simp, norm_cast] lemma coe_le_coe : (↑r : ℝ≥0∞) ≤ ↑q ↔ r ≤ q := with_top.coe_le_coe @[simp, norm_cast] lemma coe_lt_coe : (↑r : ℝ≥0∞) < ↑q ↔ r < q := with_top.coe_lt_coe lemma coe_mono : monotone (coe : ℝ≥0 → ℝ≥0∞) := λ _ _, coe_le_coe.2 @[simp, norm_cast] lemma coe_eq_zero : (↑r : ℝ≥0∞) = 0 ↔ r = 0 := coe_eq_coe @[simp, norm_cast] lemma zero_eq_coe : 0 = (↑r : ℝ≥0∞) ↔ 0 = r := coe_eq_coe @[simp, norm_cast] lemma coe_eq_one : (↑r : ℝ≥0∞) = 1 ↔ r = 1 := coe_eq_coe @[simp, norm_cast] lemma one_eq_coe : 1 = (↑r : ℝ≥0∞) ↔ 1 = r := coe_eq_coe @[simp] lemma coe_nonneg : 0 ≤ (↑r : ℝ≥0∞) := coe_le_coe.2 $ zero_le _ @[simp, norm_cast] lemma coe_pos : 0 < (↑r : ℝ≥0∞) ↔ 0 < r := coe_lt_coe lemma coe_ne_zero : (r : ℝ≥0∞) ≠ 0 ↔ r ≠ 0 := not_congr coe_eq_coe @[simp, norm_cast] lemma coe_add : ↑(r + p) = (r + p : ℝ≥0∞) := with_top.coe_add @[simp, norm_cast] lemma coe_mul : ↑(r * p) = (r * p : ℝ≥0∞) := with_top.coe_mul @[simp, norm_cast] lemma coe_bit0 : (↑(bit0 r) : ℝ≥0∞) = bit0 r := coe_add @[simp, norm_cast] lemma coe_bit1 : (↑(bit1 r) : ℝ≥0∞) = bit1 r := by simp [bit1] lemma coe_two : ((2:ℝ≥0) : ℝ≥0∞) = 2 := by norm_cast lemma to_nnreal_eq_to_nnreal_iff (x y : ℝ≥0∞) : x.to_nnreal = y.to_nnreal ↔ x = y ∨ x = 0 ∧ y = ⊤ ∨ x = ⊤ ∧ y = 0 := begin cases x, { simp only [@eq_comm ℝ≥0 _ y.to_nnreal, @eq_comm ℝ≥0∞ _ y, to_nnreal_eq_zero_iff, none_eq_top, top_to_nnreal, top_ne_zero, false_and, eq_self_iff_true, true_and, false_or, or_comm (y = ⊤)] }, { cases y; simp } end lemma to_real_eq_to_real_iff (x y : ℝ≥0∞) : x.to_real = y.to_real ↔ x = y ∨ (x = 0 ∧ y = ⊤) ∨ (x = ⊤ ∧ y = 0) := by simp only [ennreal.to_real, nnreal.coe_eq, to_nnreal_eq_to_nnreal_iff] lemma to_nnreal_eq_to_nnreal_iff' {x y : ℝ≥0∞} (hx : x ≠ ⊤) (hy : y ≠ ⊤) : x.to_nnreal = y.to_nnreal ↔ x = y := by simp only [ennreal.to_nnreal_eq_to_nnreal_iff x y, hx, hy, and_false, false_and, or_false] lemma to_real_eq_to_real_iff' {x y : ℝ≥0∞} (hx : x ≠ ⊤) (hy : y ≠ ⊤) : x.to_real = y.to_real ↔ x = y := by simp only [ennreal.to_real, nnreal.coe_eq, to_nnreal_eq_to_nnreal_iff' hx hy] protected lemma zero_lt_one : 0 < (1 : ℝ≥0∞) := zero_lt_one @[simp] lemma one_lt_two : (1 : ℝ≥0∞) < 2 := coe_one ▸ coe_two ▸ by exact_mod_cast (one_lt_two : 1 < 2) @[simp] lemma zero_lt_two : (0 : ℝ≥0∞) < 2 := lt_trans zero_lt_one one_lt_two lemma two_ne_zero : (2 : ℝ≥0∞) ≠ 0 := (ne_of_lt zero_lt_two).symm lemma two_ne_top : (2 : ℝ≥0∞) ≠ ∞ := coe_two ▸ coe_ne_top /-- `(1 : ℝ≥0∞) ≤ 1`, recorded as a `fact` for use with `Lp` spaces. -/ instance _root_.fact_one_le_one_ennreal : fact ((1 : ℝ≥0∞) ≤ 1) := ⟨le_rfl⟩ /-- `(1 : ℝ≥0∞) ≤ 2`, recorded as a `fact` for use with `Lp` spaces. -/ instance _root_.fact_one_le_two_ennreal : fact ((1 : ℝ≥0∞) ≤ 2) := ⟨one_le_two⟩ /-- `(1 : ℝ≥0∞) ≤ ∞`, recorded as a `fact` for use with `Lp` spaces. -/ instance _root_.fact_one_le_top_ennreal : fact ((1 : ℝ≥0∞) ≤ ∞) := ⟨le_top⟩ /-- The set of numbers in `ℝ≥0∞` that are not equal to `∞` is equivalent to `ℝ≥0`. -/ def ne_top_equiv_nnreal : {a | a ≠ ∞} ≃ ℝ≥0 := { to_fun := λ x, ennreal.to_nnreal x, inv_fun := λ x, ⟨x, coe_ne_top⟩, left_inv := λ ⟨x, hx⟩, subtype.eq $ coe_to_nnreal hx, right_inv := λ x, to_nnreal_coe } lemma cinfi_ne_top [has_Inf α] (f : ℝ≥0∞ → α) : (⨅ x : {x // x ≠ ∞}, f x) = ⨅ x : ℝ≥0, f x := eq.symm $ ne_top_equiv_nnreal.symm.surjective.infi_congr _$ λ x, rfl lemma infi_ne_top [complete_lattice α] (f : ℝ≥0∞ → α) : (⨅ x ≠ ∞, f x) = ⨅ x : ℝ≥0, f x := by rw [infi_subtype', cinfi_ne_top] lemma csupr_ne_top [has_Sup α] (f : ℝ≥0∞ → α) : (⨆ x : {x // x ≠ ∞}, f x) = ⨆ x : ℝ≥0, f x := @cinfi_ne_top αᵒᵈ _ _ lemma supr_ne_top [complete_lattice α] (f : ℝ≥0∞ → α) : (⨆ x ≠ ∞, f x) = ⨆ x : ℝ≥0, f x := @infi_ne_top αᵒᵈ _ _ lemma infi_ennreal {α : Type*} [complete_lattice α] {f : ℝ≥0∞ → α} : (⨅ n, f n) = (⨅ n : ℝ≥0, f n) ⊓ f ∞ := le_antisymm (le_inf (le_infi $ assume i, infi_le _ _) (infi_le _ _)) (le_infi $ forall_ennreal.2 ⟨λ r, inf_le_of_left_le $ infi_le _ _, inf_le_right⟩) lemma supr_ennreal {α : Type*} [complete_lattice α] {f : ℝ≥0∞ → α} : (⨆ n, f n) = (⨆ n : ℝ≥0, f n) ⊔ f ∞ := @infi_ennreal αᵒᵈ _ _ @[simp] lemma add_top : a + ∞ = ∞ := add_top _ @[simp] lemma top_add : ∞ + a = ∞ := top_add _ /-- Coercion `ℝ≥0 → ℝ≥0∞` as a `ring_hom`. -/ def of_nnreal_hom : ℝ≥0 →+* ℝ≥0∞ := ⟨coe, coe_one, λ _ _, coe_mul, coe_zero, λ _ _, coe_add⟩ @[simp] lemma coe_of_nnreal_hom : ⇑of_nnreal_hom = coe := rfl section actions /-- A `mul_action` over `ℝ≥0∞` restricts to a `mul_action` over `ℝ≥0`. -/ noncomputable instance {M : Type*} [mul_action ℝ≥0∞ M] : mul_action ℝ≥0 M := mul_action.comp_hom M of_nnreal_hom.to_monoid_hom lemma smul_def {M : Type*} [mul_action ℝ≥0∞ M] (c : ℝ≥0) (x : M) : c • x = (c : ℝ≥0∞) • x := rfl instance {M N : Type*} [mul_action ℝ≥0∞ M] [mul_action ℝ≥0∞ N] [has_smul M N] [is_scalar_tower ℝ≥0∞ M N] : is_scalar_tower ℝ≥0 M N := { smul_assoc := λ r, (smul_assoc (r : ℝ≥0∞) : _)} instance smul_comm_class_left {M N : Type*} [mul_action ℝ≥0∞ N] [has_smul M N] [smul_comm_class ℝ≥0∞ M N] : smul_comm_class ℝ≥0 M N := { smul_comm := λ r, (smul_comm (r : ℝ≥0∞) : _)} instance smul_comm_class_right {M N : Type*} [mul_action ℝ≥0∞ N] [has_smul M N] [smul_comm_class M ℝ≥0∞ N] : smul_comm_class M ℝ≥0 N := { smul_comm := λ m r, (smul_comm m (r : ℝ≥0∞) : _)} /-- A `distrib_mul_action` over `ℝ≥0∞` restricts to a `distrib_mul_action` over `ℝ≥0`. -/ noncomputable instance {M : Type*} [add_monoid M] [distrib_mul_action ℝ≥0∞ M] : distrib_mul_action ℝ≥0 M := distrib_mul_action.comp_hom M of_nnreal_hom.to_monoid_hom /-- A `module` over `ℝ≥0∞` restricts to a `module` over `ℝ≥0`. -/ noncomputable instance {M : Type*} [add_comm_monoid M] [module ℝ≥0∞ M] : module ℝ≥0 M := module.comp_hom M of_nnreal_hom /-- An `algebra` over `ℝ≥0∞` restricts to an `algebra` over `ℝ≥0`. -/ noncomputable instance {A : Type*} [semiring A] [algebra ℝ≥0∞ A] : algebra ℝ≥0 A := { smul := (•), commutes' := λ r x, by simp [algebra.commutes], smul_def' := λ r x, by simp [←algebra.smul_def (r : ℝ≥0∞) x, smul_def], to_ring_hom := ((algebra_map ℝ≥0∞ A).comp (of_nnreal_hom : ℝ≥0 →+* ℝ≥0∞)) } -- verify that the above produces instances we might care about noncomputable example : algebra ℝ≥0 ℝ≥0∞ := infer_instance noncomputable example : distrib_mul_action ℝ≥0ˣ ℝ≥0∞ := infer_instance lemma coe_smul {R} (r : R) (s : ℝ≥0) [has_smul R ℝ≥0] [has_smul R ℝ≥0∞] [is_scalar_tower R ℝ≥0 ℝ≥0] [is_scalar_tower R ℝ≥0 ℝ≥0∞] : (↑(r • s) : ℝ≥0∞) = r • ↑s := by rw [←smul_one_smul ℝ≥0 r (s: ℝ≥0∞), smul_def, smul_eq_mul, ←ennreal.coe_mul, smul_mul_assoc, one_mul] end actions @[simp, norm_cast] lemma coe_indicator {α} (s : set α) (f : α → ℝ≥0) (a : α) : ((s.indicator f a : ℝ≥0) : ℝ≥0∞) = s.indicator (λ x, f x) a := (of_nnreal_hom : ℝ≥0 →+ ℝ≥0∞).map_indicator _ _ _ @[simp, norm_cast] lemma coe_pow (n : ℕ) : (↑(r^n) : ℝ≥0∞) = r^n := of_nnreal_hom.map_pow r n @[simp] lemma add_eq_top : a + b = ∞ ↔ a = ∞ ∨ b = ∞ := with_top.add_eq_top @[simp] lemma add_lt_top : a + b < ∞ ↔ a < ∞ ∧ b < ∞ := with_top.add_lt_top lemma to_nnreal_add {r₁ r₂ : ℝ≥0∞} (h₁ : r₁ ≠ ∞) (h₂ : r₂ ≠ ∞) : (r₁ + r₂).to_nnreal = r₁.to_nnreal + r₂.to_nnreal := by { lift r₁ to ℝ≥0 using h₁, lift r₂ to ℝ≥0 using h₂, refl } lemma not_lt_top {x : ℝ≥0∞} : ¬ x < ∞ ↔ x = ∞ := by rw [lt_top_iff_ne_top, not_not] lemma add_ne_top : a + b ≠ ∞ ↔ a ≠ ∞ ∧ b ≠ ∞ := by simpa only [lt_top_iff_ne_top] using add_lt_top lemma mul_top : a * ∞ = (if a = 0 then 0 else ∞) := begin split_ifs, { simp [h] }, { exact with_top.mul_top h } end lemma top_mul : ∞ * a = (if a = 0 then 0 else ∞) := begin split_ifs, { simp [h] }, { exact with_top.top_mul h } end @[simp] lemma top_mul_top : ∞ * ∞ = ∞ := with_top.top_mul_top lemma top_pow {n:ℕ} (h : 0 < n) : ∞^n = ∞ := nat.le_induction (pow_one _) (λ m hm hm', by rw [pow_succ, hm', top_mul_top]) _ (nat.succ_le_of_lt h) lemma mul_eq_top : a * b = ∞ ↔ (a ≠ 0 ∧ b = ∞) ∨ (a = ∞ ∧ b ≠ 0) := with_top.mul_eq_top_iff lemma mul_lt_top : a ≠ ∞ → b ≠ ∞ → a * b < ∞ := with_top.mul_lt_top lemma mul_ne_top : a ≠ ∞ → b ≠ ∞ → a * b ≠ ∞ := by simpa only [lt_top_iff_ne_top] using mul_lt_top lemma lt_top_of_mul_ne_top_left (h : a * b ≠ ∞) (hb : b ≠ 0) : a < ∞ := lt_top_iff_ne_top.2 $ λ ha, h $ mul_eq_top.2 (or.inr ⟨ha, hb⟩) lemma lt_top_of_mul_ne_top_right (h : a * b ≠ ∞) (ha : a ≠ 0) : b < ∞ := lt_top_of_mul_ne_top_left (by rwa [mul_comm]) ha lemma mul_lt_top_iff {a b : ℝ≥0∞} : a * b < ∞ ↔ (a < ∞ ∧ b < ∞) ∨ a = 0 ∨ b = 0 := begin split, { intro h, rw [← or_assoc, or_iff_not_imp_right, or_iff_not_imp_right], intros hb ha, exact ⟨lt_top_of_mul_ne_top_left h.ne hb, lt_top_of_mul_ne_top_right h.ne ha⟩ }, { rintro (⟨ha, hb⟩|rfl|rfl); [exact mul_lt_top ha.ne hb.ne, simp, simp] } end lemma mul_self_lt_top_iff {a : ℝ≥0∞} : a * a < ⊤ ↔ a < ⊤ := by { rw [ennreal.mul_lt_top_iff, and_self, or_self, or_iff_left_iff_imp], rintro rfl, norm_num } lemma mul_pos_iff : 0 < a * b ↔ 0 < a ∧ 0 < b := canonically_ordered_comm_semiring.mul_pos lemma mul_pos (ha : a ≠ 0) (hb : b ≠ 0) : 0 < a * b := mul_pos_iff.2 ⟨pos_iff_ne_zero.2 ha, pos_iff_ne_zero.2 hb⟩ @[simp] lemma pow_eq_top_iff {n : ℕ} : a ^ n = ∞ ↔ a = ∞ ∧ n ≠ 0 := begin induction n with n ihn, { simp }, rw [pow_succ, mul_eq_top, ihn], fsplit, { rintro (⟨-,rfl,h0⟩|⟨rfl,h0⟩); exact ⟨rfl, n.succ_ne_zero⟩ }, { rintro ⟨rfl, -⟩, exact or.inr ⟨rfl, pow_ne_zero n top_ne_zero⟩ } end lemma pow_eq_top (n : ℕ) (h : a ^ n = ∞) : a = ∞ := (pow_eq_top_iff.1 h).1 lemma pow_ne_top (h : a ≠ ∞) {n:ℕ} : a^n ≠ ∞ := mt (pow_eq_top n) h lemma pow_lt_top : a < ∞ → ∀ n:ℕ, a^n < ∞ := by simpa only [lt_top_iff_ne_top] using pow_ne_top @[simp, norm_cast] lemma coe_finset_sum {s : finset α} {f : α → ℝ≥0} : ↑(∑ a in s, f a) = (∑ a in s, f a : ℝ≥0∞) := of_nnreal_hom.map_sum f s @[simp, norm_cast] lemma coe_finset_prod {s : finset α} {f : α → ℝ≥0} : ↑(∏ a in s, f a) = ((∏ a in s, f a) : ℝ≥0∞) := of_nnreal_hom.map_prod f s section order @[simp] lemma bot_eq_zero : (⊥ : ℝ≥0∞) = 0 := rfl @[simp] lemma coe_lt_top : coe r < ∞ := with_top.coe_lt_top r @[simp] lemma not_top_le_coe : ¬ ∞ ≤ ↑r := with_top.not_top_le_coe r @[simp, norm_cast] lemma one_le_coe_iff : (1:ℝ≥0∞) ≤ ↑r ↔ 1 ≤ r := coe_le_coe @[simp, norm_cast] lemma coe_le_one_iff : ↑r ≤ (1:ℝ≥0∞) ↔ r ≤ 1 := coe_le_coe @[simp, norm_cast] lemma coe_lt_one_iff : (↑p : ℝ≥0∞) < 1 ↔ p < 1 := coe_lt_coe @[simp, norm_cast] lemma one_lt_coe_iff : 1 < (↑p : ℝ≥0∞) ↔ 1 < p := coe_lt_coe @[simp, norm_cast] lemma coe_nat (n : ℕ) : ((n : ℝ≥0) : ℝ≥0∞) = n := with_top.coe_nat n @[simp] lemma of_real_coe_nat (n : ℕ) : ennreal.of_real n = n := by simp [ennreal.of_real] @[simp] lemma nat_ne_top (n : ℕ) : (n : ℝ≥0∞) ≠ ∞ := with_top.nat_ne_top n @[simp] lemma top_ne_nat (n : ℕ) : ∞ ≠ n := with_top.top_ne_nat n @[simp] lemma one_lt_top : 1 < ∞ := coe_lt_top @[simp, norm_cast] lemma to_nnreal_nat (n : ℕ) : (n : ℝ≥0∞).to_nnreal = n := by conv_lhs { rw [← ennreal.coe_nat n, ennreal.to_nnreal_coe] } @[simp, norm_cast] lemma to_real_nat (n : ℕ) : (n : ℝ≥0∞).to_real = n := by conv_lhs { rw [← ennreal.of_real_coe_nat n, ennreal.to_real_of_real (nat.cast_nonneg _)] } lemma le_coe_iff : a ≤ ↑r ↔ (∃p:ℝ≥0, a = p ∧ p ≤ r) := with_top.le_coe_iff lemma coe_le_iff : ↑r ≤ a ↔ (∀p:ℝ≥0, a = p → r ≤ p) := with_top.coe_le_iff lemma lt_iff_exists_coe : a < b ↔ (∃p:ℝ≥0, a = p ∧ ↑p < b) := with_top.lt_iff_exists_coe lemma to_real_le_coe_of_le_coe {a : ℝ≥0∞} {b : ℝ≥0} (h : a ≤ b) : a.to_real ≤ b := show ↑a.to_nnreal ≤ ↑b, begin have : ↑a.to_nnreal = a := ennreal.coe_to_nnreal (lt_of_le_of_lt h coe_lt_top).ne, rw ← this at h, exact_mod_cast h end @[simp, norm_cast] lemma coe_finset_sup {s : finset α} {f : α → ℝ≥0} : ↑(s.sup f) = s.sup (λ x, (f x : ℝ≥0∞)) := finset.comp_sup_eq_sup_comp_of_is_total _ coe_mono rfl lemma pow_le_pow {n m : ℕ} (ha : 1 ≤ a) (h : n ≤ m) : a ^ n ≤ a ^ m := begin cases a, { cases m, { rw eq_bot_iff.mpr h, exact le_rfl }, { rw [none_eq_top, top_pow (nat.succ_pos m)], exact le_top } }, { rw [some_eq_coe, ← coe_pow, ← coe_pow, coe_le_coe], exact pow_le_pow (by simpa using ha) h } end lemma one_le_pow_of_one_le (ha : 1 ≤ a) (n : ℕ) : 1 ≤ a ^ n := by simpa using pow_le_pow ha (zero_le n) @[simp] lemma max_eq_zero_iff : max a b = 0 ↔ a = 0 ∧ b = 0 := by simp only [nonpos_iff_eq_zero.symm, max_le_iff] @[simp] lemma max_zero_left : max 0 a = a := max_eq_right (zero_le a) @[simp] lemma max_zero_right : max a 0 = a := max_eq_left (zero_le a) @[simp] lemma sup_eq_max : a ⊔ b = max a b := rfl protected lemma pow_pos : 0 < a → ∀ n : ℕ, 0 < a^n := canonically_ordered_comm_semiring.pow_pos protected lemma pow_ne_zero : a ≠ 0 → ∀ n : ℕ, a^n ≠ 0 := by simpa only [pos_iff_ne_zero] using ennreal.pow_pos @[simp] lemma not_lt_zero : ¬ a < 0 := by simp protected lemma le_of_add_le_add_left : a ≠ ∞ → a + b ≤ a + c → b ≤ c := with_top.le_of_add_le_add_left protected lemma le_of_add_le_add_right : a ≠ ∞ → b + a ≤ c + a → b ≤ c := with_top.le_of_add_le_add_right protected lemma add_lt_add_left : a ≠ ∞ → b < c → a + b < a + c := with_top.add_lt_add_left protected lemma add_lt_add_right : a ≠ ∞ → b < c → b + a < c + a := with_top.add_lt_add_right protected lemma add_le_add_iff_left : a ≠ ∞ → (a + b ≤ a + c ↔ b ≤ c) := with_top.add_le_add_iff_left protected lemma add_le_add_iff_right : a ≠ ∞ → (b + a ≤ c + a ↔ b ≤ c) := with_top.add_le_add_iff_right protected lemma add_lt_add_iff_left : a ≠ ∞ → (a + b < a + c ↔ b < c) := with_top.add_lt_add_iff_left protected lemma add_lt_add_iff_right : a ≠ ∞ → (b + a < c + a ↔ b < c) := with_top.add_lt_add_iff_right protected lemma add_lt_add_of_le_of_lt : a ≠ ∞ → a ≤ b → c < d → a + c < b + d := with_top.add_lt_add_of_le_of_lt protected lemma add_lt_add_of_lt_of_le : c ≠ ∞ → a < b → c ≤ d → a + c < b + d := with_top.add_lt_add_of_lt_of_le instance contravariant_class_add_lt : contravariant_class ℝ≥0∞ ℝ≥0∞ (+) (<) := with_top.contravariant_class_add_lt lemma lt_add_right (ha : a ≠ ∞) (hb : b ≠ 0) : a < a + b := by rwa [← pos_iff_ne_zero, ←ennreal.add_lt_add_iff_left ha, add_zero] at hb lemma le_of_forall_pos_le_add : ∀{a b : ℝ≥0∞}, (∀ε : ℝ≥0, 0 < ε → b < ∞ → a ≤ b + ε) → a ≤ b | a none h := le_top | none (some a) h := have ∞ ≤ ↑a + ↑(1:ℝ≥0), from h 1 zero_lt_one coe_lt_top, by rw [← coe_add] at this; exact (not_top_le_coe this).elim | (some a) (some b) h := by simp only [none_eq_top, some_eq_coe, coe_add.symm, coe_le_coe, coe_lt_top, true_implies_iff] at *; exact nnreal.le_of_forall_pos_le_add h lemma lt_iff_exists_rat_btwn : a < b ↔ (∃q:ℚ, 0 ≤ q ∧ a < real.to_nnreal q ∧ (real.to_nnreal q:ℝ≥0∞) < b) := ⟨λ h, begin rcases lt_iff_exists_coe.1 h with ⟨p, rfl, _⟩, rcases exists_between h with ⟨c, pc, cb⟩, rcases lt_iff_exists_coe.1 cb with ⟨r, rfl, _⟩, rcases (nnreal.lt_iff_exists_rat_btwn _ _).1 (coe_lt_coe.1 pc) with ⟨q, hq0, pq, qr⟩, exact ⟨q, hq0, coe_lt_coe.2 pq, lt_trans (coe_lt_coe.2 qr) cb⟩ end, λ ⟨q, q0, qa, qb⟩, lt_trans qa qb⟩ lemma lt_iff_exists_real_btwn : a < b ↔ (∃r:ℝ, 0 ≤ r ∧ a < ennreal.of_real r ∧ (ennreal.of_real r:ℝ≥0∞) < b) := ⟨λ h, let ⟨q, q0, aq, qb⟩ := ennreal.lt_iff_exists_rat_btwn.1 h in ⟨q, rat.cast_nonneg.2 q0, aq, qb⟩, λ ⟨q, q0, qa, qb⟩, lt_trans qa qb⟩ lemma lt_iff_exists_nnreal_btwn : a < b ↔ (∃r:ℝ≥0, a < r ∧ (r : ℝ≥0∞) < b) := with_top.lt_iff_exists_coe_btwn lemma lt_iff_exists_add_pos_lt : a < b ↔ (∃ r : ℝ≥0, 0 < r ∧ a + r < b) := begin refine ⟨λ hab, _, λ ⟨r, rpos, hr⟩, lt_of_le_of_lt (le_self_add) hr⟩, cases a, { simpa using hab }, rcases lt_iff_exists_real_btwn.1 hab with ⟨c, c_nonneg, ac, cb⟩, let d : ℝ≥0 := ⟨c, c_nonneg⟩, have ad : a < d, { rw of_real_eq_coe_nnreal c_nonneg at ac, exact coe_lt_coe.1 ac }, refine ⟨d-a, tsub_pos_iff_lt.2 ad, _⟩, rw [some_eq_coe, ← coe_add], convert cb, have : real.to_nnreal c = d, by { rw [← nnreal.coe_eq, real.coe_to_nnreal _ c_nonneg], refl }, rw [add_comm, this], exact tsub_add_cancel_of_le ad.le end lemma coe_nat_lt_coe {n : ℕ} : (n : ℝ≥0∞) < r ↔ ↑n < r := ennreal.coe_nat n ▸ coe_lt_coe lemma coe_lt_coe_nat {n : ℕ} : (r : ℝ≥0∞) < n ↔ r < n := ennreal.coe_nat n ▸ coe_lt_coe @[simp, norm_cast] lemma coe_nat_lt_coe_nat {m n : ℕ} : (m : ℝ≥0∞) < n ↔ m < n := ennreal.coe_nat n ▸ coe_nat_lt_coe.trans nat.cast_lt lemma coe_nat_mono : strict_mono (coe : ℕ → ℝ≥0∞) := λ _ _, coe_nat_lt_coe_nat.2 @[simp, norm_cast] lemma coe_nat_le_coe_nat {m n : ℕ} : (m : ℝ≥0∞) ≤ n ↔ m ≤ n := coe_nat_mono.le_iff_le instance : char_zero ℝ≥0∞ := ⟨coe_nat_mono.injective⟩ protected lemma exists_nat_gt {r : ℝ≥0∞} (h : r ≠ ∞) : ∃n:ℕ, r < n := begin lift r to ℝ≥0 using h, rcases exists_nat_gt r with ⟨n, hn⟩, exact ⟨n, coe_lt_coe_nat.2 hn⟩, end @[simp] lemma Union_Iio_coe_nat : (⋃ n : ℕ, Iio (n : ℝ≥0∞)) = {∞}ᶜ := begin ext x, rw [mem_Union], exact ⟨λ ⟨n, hn⟩, ne_top_of_lt hn, ennreal.exists_nat_gt⟩ end @[simp] lemma Union_Iic_coe_nat : (⋃ n : ℕ, Iic (n : ℝ≥0∞)) = {∞}ᶜ := subset.antisymm (Union_subset $ λ n x hx, ne_top_of_le_ne_top (nat_ne_top n) hx) $ Union_Iio_coe_nat ▸ Union_mono (λ n, Iio_subset_Iic_self) @[simp] lemma Union_Ioc_coe_nat : (⋃ n : ℕ, Ioc a n) = Ioi a \ {∞} := by simp only [← Ioi_inter_Iic, ← inter_Union, Union_Iic_coe_nat, diff_eq] @[simp] lemma Union_Ioo_coe_nat : (⋃ n : ℕ, Ioo a n) = Ioi a \ {∞} := by simp only [← Ioi_inter_Iio, ← inter_Union, Union_Iio_coe_nat, diff_eq] @[simp] lemma Union_Icc_coe_nat : (⋃ n : ℕ, Icc a n) = Ici a \ {∞} := by simp only [← Ici_inter_Iic, ← inter_Union, Union_Iic_coe_nat, diff_eq] @[simp] lemma Union_Ico_coe_nat : (⋃ n : ℕ, Ico a n) = Ici a \ {∞} := by simp only [← Ici_inter_Iio, ← inter_Union, Union_Iio_coe_nat, diff_eq] @[simp] lemma Inter_Ici_coe_nat : (⋂ n : ℕ, Ici (n : ℝ≥0∞)) = {∞} := by simp only [← compl_Iio, ← compl_Union, Union_Iio_coe_nat, compl_compl] @[simp] lemma Inter_Ioi_coe_nat : (⋂ n : ℕ, Ioi (n : ℝ≥0∞)) = {∞} := by simp only [← compl_Iic, ← compl_Union, Union_Iic_coe_nat, compl_compl] lemma add_lt_add (ac : a < c) (bd : b < d) : a + b < c + d := begin lift a to ℝ≥0 using ne_top_of_lt ac, lift b to ℝ≥0 using ne_top_of_lt bd, cases c, { simp }, cases d, { simp }, simp only [← coe_add, some_eq_coe, coe_lt_coe] at *, exact add_lt_add ac bd end @[norm_cast] lemma coe_min : ((min r p:ℝ≥0):ℝ≥0∞) = min r p := coe_mono.map_min @[norm_cast] lemma coe_max : ((max r p:ℝ≥0):ℝ≥0∞) = max r p := coe_mono.map_max lemma le_of_top_imp_top_of_to_nnreal_le {a b : ℝ≥0∞} (h : a = ⊤ → b = ⊤) (h_nnreal : a ≠ ⊤ → b ≠ ⊤ → a.to_nnreal ≤ b.to_nnreal) : a ≤ b := begin by_cases ha : a = ⊤, { rw h ha, exact le_top, }, by_cases hb : b = ⊤, { rw hb, exact le_top, }, rw [←coe_to_nnreal hb, ←coe_to_nnreal ha, coe_le_coe], exact h_nnreal ha hb, end end order section complete_lattice lemma coe_Sup {s : set ℝ≥0} : bdd_above s → (↑(Sup s) : ℝ≥0∞) = (⨆a∈s, ↑a) := with_top.coe_Sup lemma coe_Inf {s : set ℝ≥0} : s.nonempty → (↑(Inf s) : ℝ≥0∞) = (⨅a∈s, ↑a) := with_top.coe_Inf @[simp] lemma top_mem_upper_bounds {s : set ℝ≥0∞} : ∞ ∈ upper_bounds s := assume x hx, le_top lemma coe_mem_upper_bounds {s : set ℝ≥0} : ↑r ∈ upper_bounds ((coe : ℝ≥0 → ℝ≥0∞) '' s) ↔ r ∈ upper_bounds s := by simp [upper_bounds, ball_image_iff, -mem_image, *] {contextual := tt} end complete_lattice section mul @[mono] lemma mul_le_mul : a ≤ b → c ≤ d → a * c ≤ b * d := mul_le_mul' @[mono] lemma mul_lt_mul (ac : a < c) (bd : b < d) : a * b < c * d := begin rcases lt_iff_exists_nnreal_btwn.1 ac with ⟨a', aa', a'c⟩, lift a to ℝ≥0 using ne_top_of_lt aa', rcases lt_iff_exists_nnreal_btwn.1 bd with ⟨b', bb', b'd⟩, lift b to ℝ≥0 using ne_top_of_lt bb', norm_cast at *, calc ↑(a * b) < ↑(a' * b') : coe_lt_coe.2 (mul_lt_mul' aa'.le bb' (zero_le _) ((zero_le a).trans_lt aa')) ... = ↑a' * ↑b' : coe_mul ... ≤ c * d : mul_le_mul a'c.le b'd.le end lemma mul_left_mono : monotone ((*) a) := λ b c, mul_le_mul le_rfl lemma mul_right_mono : monotone (λ x, x * a) := λ b c h, mul_le_mul h le_rfl lemma pow_strict_mono {n : ℕ} (hn : n ≠ 0) : strict_mono (λ (x : ℝ≥0∞), x^n) := begin assume x y hxy, obtain ⟨n, rfl⟩ := nat.exists_eq_succ_of_ne_zero hn, induction n with n IH, { simp only [hxy, pow_one] }, { simp only [pow_succ _ n.succ, mul_lt_mul hxy (IH (nat.succ_pos _).ne')] } end lemma max_mul : max a b * c = max (a * c) (b * c) := mul_right_mono.map_max lemma mul_max : a * max b c = max (a * b) (a * c) := mul_left_mono.map_max lemma mul_eq_mul_left : a ≠ 0 → a ≠ ∞ → (a * b = a * c ↔ b = c) := begin cases a; cases b; cases c; simp [none_eq_top, some_eq_coe, mul_top, top_mul, -coe_mul, coe_mul.symm, nnreal.mul_eq_mul_left] {contextual := tt}, end lemma mul_eq_mul_right : c ≠ 0 → c ≠ ∞ → (a * c = b * c ↔ a = b) := mul_comm c a ▸ mul_comm c b ▸ mul_eq_mul_left lemma mul_le_mul_left : a ≠ 0 → a ≠ ∞ → (a * b ≤ a * c ↔ b ≤ c) := begin cases a; cases b; cases c; simp [none_eq_top, some_eq_coe, mul_top, top_mul, -coe_mul, coe_mul.symm] {contextual := tt}, assume h, exact mul_le_mul_left (pos_iff_ne_zero.2 h) end lemma mul_le_mul_right : c ≠ 0 → c ≠ ∞ → (a * c ≤ b * c ↔ a ≤ b) := mul_comm c a ▸ mul_comm c b ▸ mul_le_mul_left lemma mul_lt_mul_left : a ≠ 0 → a ≠ ∞ → (a * b < a * c ↔ b < c) := λ h0 ht, by simp only [mul_le_mul_left h0 ht, lt_iff_le_not_le] lemma mul_lt_mul_right : c ≠ 0 → c ≠ ∞ → (a * c < b * c ↔ a < b) := mul_comm c a ▸ mul_comm c b ▸ mul_lt_mul_left end mul section cancel /-- An element `a` is `add_le_cancellable` if `a + b ≤ a + c` implies `b ≤ c` for all `b` and `c`. This is true in `ℝ≥0∞` for all elements except `∞`. -/ lemma add_le_cancellable_iff_ne {a : ℝ≥0∞} : add_le_cancellable a ↔ a ≠ ∞ := begin split, { rintro h rfl, refine zero_lt_one.not_le (h _), simp, }, { rintro h b c hbc, apply ennreal.le_of_add_le_add_left h hbc } end /-- This lemma has an abbreviated name because it is used frequently. -/ lemma cancel_of_ne {a : ℝ≥0∞} (h : a ≠ ∞) : add_le_cancellable a := add_le_cancellable_iff_ne.mpr h /-- This lemma has an abbreviated name because it is used frequently. -/ lemma cancel_of_lt {a : ℝ≥0∞} (h : a < ∞) : add_le_cancellable a := cancel_of_ne h.ne /-- This lemma has an abbreviated name because it is used frequently. -/ lemma cancel_of_lt' {a b : ℝ≥0∞} (h : a < b) : add_le_cancellable a := cancel_of_ne h.ne_top /-- This lemma has an abbreviated name because it is used frequently. -/ lemma cancel_coe {a : ℝ≥0} : add_le_cancellable (a : ℝ≥0∞) := cancel_of_ne coe_ne_top lemma add_right_inj (h : a ≠ ∞) : a + b = a + c ↔ b = c := (cancel_of_ne h).inj lemma add_left_inj (h : a ≠ ∞) : b + a = c + a ↔ b = c := (cancel_of_ne h).inj_left end cancel section sub lemma sub_eq_Inf {a b : ℝ≥0∞} : a - b = Inf {d | a ≤ d + b} := le_antisymm (le_Inf $ λ c, tsub_le_iff_right.mpr) $ Inf_le le_tsub_add /-- This is a special case of `with_top.coe_sub` in the `ennreal` namespace -/ lemma coe_sub : (↑(r - p) : ℝ≥0∞) = ↑r - ↑p := with_top.coe_sub /-- This is a special case of `with_top.top_sub_coe` in the `ennreal` namespace -/ lemma top_sub_coe : ∞ - ↑r = ∞ := with_top.top_sub_coe /-- This is a special case of `with_top.sub_top` in the `ennreal` namespace -/ lemma sub_top : a - ∞ = 0 := with_top.sub_top lemma sub_eq_top_iff : a - b = ∞ ↔ a = ∞ ∧ b ≠ ∞ := by { cases a; cases b; simp [← with_top.coe_sub] } lemma sub_ne_top (ha : a ≠ ∞) : a - b ≠ ∞ := mt sub_eq_top_iff.mp $ mt and.left ha @[simp, norm_cast] lemma nat_cast_sub (m n : ℕ) : ↑(m - n) = (m - n : ℝ≥0∞) := by rw [← coe_nat, nat.cast_tsub, coe_sub, coe_nat, coe_nat] protected lemma sub_eq_of_eq_add (hb : b ≠ ∞) : a = c + b → a - b = c := (cancel_of_ne hb).tsub_eq_of_eq_add protected lemma eq_sub_of_add_eq (hc : c ≠ ∞) : a + c = b → a = b - c := (cancel_of_ne hc).eq_tsub_of_add_eq protected lemma sub_eq_of_eq_add_rev (hb : b ≠ ∞) : a = b + c → a - b = c := (cancel_of_ne hb).tsub_eq_of_eq_add_rev lemma sub_eq_of_add_eq (hb : b ≠ ∞) (hc : a + b = c) : c - b = a := ennreal.sub_eq_of_eq_add hb hc.symm @[simp] protected lemma add_sub_cancel_left (ha : a ≠ ∞) : a + b - a = b := (cancel_of_ne ha).add_tsub_cancel_left @[simp] protected lemma add_sub_cancel_right (hb : b ≠ ∞) : a + b - b = a := (cancel_of_ne hb).add_tsub_cancel_right protected lemma lt_add_of_sub_lt_left (h : a ≠ ∞ ∨ b ≠ ∞) : a - b < c → a < b + c := begin obtain rfl | hb := eq_or_ne b ∞, { rw [top_add, lt_top_iff_ne_top], exact λ _, h.resolve_right (not_not.2 rfl) }, { exact (cancel_of_ne hb).lt_add_of_tsub_lt_left } end protected lemma lt_add_of_sub_lt_right (h : a ≠ ∞ ∨ c ≠ ∞) : a - c < b → a < b + c := add_comm c b ▸ ennreal.lt_add_of_sub_lt_left h lemma le_sub_of_add_le_left (ha : a ≠ ∞) : a + b ≤ c → b ≤ c - a := (cancel_of_ne ha).le_tsub_of_add_le_left lemma le_sub_of_add_le_right (hb : b ≠ ∞) : a + b ≤ c → a ≤ c - b := (cancel_of_ne hb).le_tsub_of_add_le_right protected lemma sub_lt_of_lt_add (hac : c ≤ a) (h : a < b + c) : a - c < b := ((cancel_of_lt' $ hac.trans_lt h).tsub_lt_iff_right hac).mpr h protected lemma sub_lt_iff_lt_right (hb : b ≠ ∞) (hab : b ≤ a) : a - b < c ↔ a < c + b := (cancel_of_ne hb).tsub_lt_iff_right hab protected lemma sub_lt_self (ha : a ≠ ∞) (ha₀ : a ≠ 0) (hb : b ≠ 0) : a - b < a := (cancel_of_ne ha).tsub_lt_self (pos_iff_ne_zero.2 ha₀) (pos_iff_ne_zero.2 hb) protected lemma sub_lt_self_iff (ha : a ≠ ∞) : a - b < a ↔ 0 < a ∧ 0 < b := (cancel_of_ne ha).tsub_lt_self_iff lemma sub_lt_of_sub_lt (h₂ : c ≤ a) (h₃ : a ≠ ∞ ∨ b ≠ ∞) (h₁ : a - b < c) : a - c < b := ennreal.sub_lt_of_lt_add h₂ (add_comm c b ▸ ennreal.lt_add_of_sub_lt_right h₃ h₁) lemma sub_sub_cancel (h : a ≠ ∞) (h2 : b ≤ a) : a - (a - b) = b := (cancel_of_ne $ sub_ne_top h).tsub_tsub_cancel_of_le h2 lemma sub_right_inj {a b c : ℝ≥0∞} (ha : a ≠ ∞) (hb : b ≤ a) (hc : c ≤ a) : a - b = a - c ↔ b = c := (cancel_of_ne ha).tsub_right_inj (cancel_of_ne $ ne_top_of_le_ne_top ha hb) (cancel_of_ne $ ne_top_of_le_ne_top ha hc) hb hc lemma sub_mul (h : 0 < b → b < a → c ≠ ∞) : (a - b) * c = a * c - b * c := begin cases le_or_lt a b with hab hab, { simp [hab, mul_right_mono hab] }, rcases eq_or_lt_of_le (zero_le b) with rfl|hb, { simp }, exact (cancel_of_ne $ mul_ne_top hab.ne_top (h hb hab)).tsub_mul end lemma mul_sub (h : 0 < c → c < b → a ≠ ∞) : a * (b - c) = a * b - a * c := by { simp only [mul_comm a], exact sub_mul h } end sub section sum open finset /-- A product of finite numbers is still finite -/ lemma prod_lt_top {s : finset α} {f : α → ℝ≥0∞} (h : ∀ a ∈ s, f a ≠ ∞) : (∏ a in s, f a) < ∞ := with_top.prod_lt_top h /-- A sum of finite numbers is still finite -/ lemma sum_lt_top {s : finset α} {f : α → ℝ≥0∞} (h : ∀ a ∈ s, f a ≠ ∞) : ∑ a in s, f a < ∞ := with_top.sum_lt_top h /-- A sum of finite numbers is still finite -/ lemma sum_lt_top_iff {s : finset α} {f : α → ℝ≥0∞} : ∑ a in s, f a < ∞ ↔ (∀ a ∈ s, f a < ∞) := with_top.sum_lt_top_iff /-- A sum of numbers is infinite iff one of them is infinite -/ lemma sum_eq_top_iff {s : finset α} {f : α → ℝ≥0∞} : (∑ x in s, f x) = ∞ ↔ (∃ a ∈ s, f a = ∞) := with_top.sum_eq_top_iff lemma lt_top_of_sum_ne_top {s : finset α} {f : α → ℝ≥0∞} (h : (∑ x in s, f x) ≠ ∞) {a : α} (ha : a ∈ s) : f a < ∞ := sum_lt_top_iff.1 h.lt_top a ha /-- seeing `ℝ≥0∞` as `ℝ≥0` does not change their sum, unless one of the `ℝ≥0∞` is infinity -/ lemma to_nnreal_sum {s : finset α} {f : α → ℝ≥0∞} (hf : ∀a∈s, f a ≠ ∞) : ennreal.to_nnreal (∑ a in s, f a) = ∑ a in s, ennreal.to_nnreal (f a) := begin rw [← coe_eq_coe, coe_to_nnreal, coe_finset_sum, sum_congr rfl], { intros x hx, exact (coe_to_nnreal (hf x hx)).symm }, { exact (sum_lt_top hf).ne } end /-- seeing `ℝ≥0∞` as `real` does not change their sum, unless one of the `ℝ≥0∞` is infinity -/ lemma to_real_sum {s : finset α} {f : α → ℝ≥0∞} (hf : ∀ a ∈ s, f a ≠ ∞) : ennreal.to_real (∑ a in s, f a) = ∑ a in s, ennreal.to_real (f a) := by { rw [ennreal.to_real, to_nnreal_sum hf, nnreal.coe_sum], refl } lemma of_real_sum_of_nonneg {s : finset α} {f : α → ℝ} (hf : ∀ i, i ∈ s → 0 ≤ f i) : ennreal.of_real (∑ i in s, f i) = ∑ i in s, ennreal.of_real (f i) := begin simp_rw [ennreal.of_real, ←coe_finset_sum, coe_eq_coe], exact real.to_nnreal_sum_of_nonneg hf, end theorem sum_lt_sum_of_nonempty {s : finset α} (hs : s.nonempty) {f g : α → ℝ≥0∞} (Hlt : ∀ i ∈ s, f i < g i) : ∑ i in s, f i < ∑ i in s, g i := begin induction hs using finset.nonempty.cons_induction with a a s as hs IH, { simp [Hlt _ (finset.mem_singleton_self _)] }, { simp only [as, finset.sum_cons, not_false_iff], exact ennreal.add_lt_add (Hlt _ (finset.mem_cons_self _ _)) (IH (λ i hi, Hlt _ (finset.mem_cons.2 $ or.inr hi))) } end theorem exists_le_of_sum_le {s : finset α} (hs : s.nonempty) {f g : α → ℝ≥0∞} (Hle : ∑ i in s, f i ≤ ∑ i in s, g i) : ∃ i ∈ s, f i ≤ g i := begin contrapose! Hle, apply ennreal.sum_lt_sum_of_nonempty hs Hle, end end sum section interval variables {x y z : ℝ≥0∞} {ε ε₁ ε₂ : ℝ≥0∞} {s : set ℝ≥0∞} protected lemma Ico_eq_Iio : (Ico 0 y) = (Iio y) := Ico_bot lemma mem_Iio_self_add : x ≠ ∞ → ε ≠ 0 → x ∈ Iio (x + ε) := assume xt ε0, lt_add_right xt ε0 lemma mem_Ioo_self_sub_add : x ≠ ∞ → x ≠ 0 → ε₁ ≠ 0 → ε₂ ≠ 0 → x ∈ Ioo (x - ε₁) (x + ε₂) := assume xt x0 ε0 ε0', ⟨ennreal.sub_lt_self xt x0 ε0, lt_add_right xt ε0'⟩ end interval section bit @[mono] lemma bit0_strict_mono : strict_mono (bit0 : ℝ≥0∞ → ℝ≥0∞) := λ a b h, add_lt_add h h lemma bit0_injective : function.injective (bit0 : ℝ≥0∞ → ℝ≥0∞) := bit0_strict_mono.injective @[simp] lemma bit0_lt_bit0 : bit0 a < bit0 b ↔ a < b := bit0_strict_mono.lt_iff_lt @[simp, mono] lemma bit0_le_bit0 : bit0 a ≤ bit0 b ↔ a ≤ b := bit0_strict_mono.le_iff_le @[simp] lemma bit0_inj : bit0 a = bit0 b ↔ a = b := bit0_injective.eq_iff @[simp] lemma bit0_eq_zero_iff : bit0 a = 0 ↔ a = 0 := bit0_injective.eq_iff' bit0_zero @[simp] lemma bit0_top : bit0 ∞ = ∞ := add_top @[simp] lemma bit0_eq_top_iff : bit0 a = ∞ ↔ a = ∞ := bit0_injective.eq_iff' bit0_top @[mono] lemma bit1_strict_mono : strict_mono (bit1 : ℝ≥0∞ → ℝ≥0∞) := λ a b h, ennreal.add_lt_add_right one_ne_top (bit0_strict_mono h) lemma bit1_injective : function.injective (bit1 : ℝ≥0∞ → ℝ≥0∞) := bit1_strict_mono.injective @[simp] lemma bit1_lt_bit1 : bit1 a < bit1 b ↔ a < b := bit1_strict_mono.lt_iff_lt @[simp, mono] lemma bit1_le_bit1 : bit1 a ≤ bit1 b ↔ a ≤ b := bit1_strict_mono.le_iff_le @[simp] lemma bit1_inj : bit1 a = bit1 b ↔ a = b := bit1_injective.eq_iff @[simp] lemma bit1_ne_zero : bit1 a ≠ 0 := by simp [bit1] @[simp] lemma bit1_top : bit1 ∞ = ∞ := by rw [bit1, bit0_top, top_add] @[simp] lemma bit1_eq_top_iff : bit1 a = ∞ ↔ a = ∞ := bit1_injective.eq_iff' bit1_top @[simp] lemma bit1_eq_one_iff : bit1 a = 1 ↔ a = 0 := bit1_injective.eq_iff' bit1_zero end bit section inv noncomputable theory instance : has_inv ℝ≥0∞ := ⟨λa, Inf {b | 1 ≤ a * b}⟩ instance : div_inv_monoid ℝ≥0∞ := { inv := has_inv.inv, .. (infer_instance : monoid ℝ≥0∞) } lemma div_eq_inv_mul : a / b = b⁻¹ * a := by rw [div_eq_mul_inv, mul_comm] @[simp] lemma inv_zero : (0 : ℝ≥0∞)⁻¹ = ∞ := show Inf {b : ℝ≥0∞ | 1 ≤ 0 * b} = ∞, by simp; refl @[simp] lemma inv_top : ∞⁻¹ = 0 := bot_unique $ le_of_forall_le_of_dense $ λ a (h : a > 0), Inf_le $ by simp [*, ne_of_gt h, top_mul] lemma coe_inv_le : (↑r⁻¹ : ℝ≥0∞) ≤ (↑r)⁻¹ := le_Inf $ assume b (hb : 1 ≤ ↑r * b), coe_le_iff.2 $ by { rintro b rfl, apply nnreal.inv_le_of_le_mul, rwa [← coe_mul, ← coe_one, coe_le_coe] at hb } @[simp, norm_cast] lemma coe_inv (hr : r ≠ 0) : (↑r⁻¹ : ℝ≥0∞) = (↑r)⁻¹ := coe_inv_le.antisymm $ Inf_le $ le_of_eq $ by rw [← coe_mul, mul_inv_cancel hr, coe_one] @[norm_cast] lemma coe_inv_two : ((2⁻¹ : ℝ≥0) : ℝ≥0∞) = 2⁻¹ := by rw [coe_inv _root_.two_ne_zero, coe_two] @[simp, norm_cast] lemma coe_div (hr : r ≠ 0) : (↑(p / r) : ℝ≥0∞) = p / r := by rw [div_eq_mul_inv, div_eq_mul_inv, coe_mul, coe_inv hr] lemma div_zero (h : a ≠ 0) : a / 0 = ∞ := by simp [div_eq_mul_inv, h] instance : div_inv_one_monoid ℝ≥0∞ := { inv_one := by simpa only [coe_inv one_ne_zero, coe_one] using coe_eq_coe.2 inv_one, ..ennreal.div_inv_monoid } protected lemma inv_pow {n : ℕ} : (a^n)⁻¹ = (a⁻¹)^n := begin cases n, { simp only [pow_zero, inv_one] }, induction a using with_top.rec_top_coe, { simp [top_pow n.succ_pos] }, rcases eq_or_ne a 0 with rfl|ha, { simp [top_pow, zero_pow, n.succ_pos] }, rw [← coe_inv ha, ← coe_pow, ← coe_inv (pow_ne_zero _ ha), ← inv_pow, coe_pow] end lemma mul_inv_cancel (h0 : a ≠ 0) (ht : a ≠ ∞) : a * a⁻¹ = 1 := begin lift a to ℝ≥0 using ht, norm_cast at *, exact mul_inv_cancel h0 end lemma inv_mul_cancel (h0 : a ≠ 0) (ht : a ≠ ∞) : a⁻¹ * a = 1 := mul_comm a a⁻¹ ▸ mul_inv_cancel h0 ht lemma div_mul_cancel (h0 : a ≠ 0) (hI : a ≠ ∞) : (b / a) * a = b := by rw [div_eq_mul_inv, mul_assoc, inv_mul_cancel h0 hI, mul_one] lemma mul_div_cancel' (h0 : a ≠ 0) (hI : a ≠ ∞) : a * (b / a) = b := by rw [mul_comm, div_mul_cancel h0 hI] instance : has_involutive_inv ℝ≥0∞ := { inv := has_inv.inv, inv_inv := λ a, by by_cases a = 0; cases a; simp [*, none_eq_top, some_eq_coe, -coe_inv, (coe_inv _).symm] at * } @[simp] lemma inv_eq_top : a⁻¹ = ∞ ↔ a = 0 := inv_zero ▸ inv_inj lemma inv_ne_top : a⁻¹ ≠ ∞ ↔ a ≠ 0 := by simp @[simp] lemma inv_lt_top {x : ℝ≥0∞} : x⁻¹ < ∞ ↔ 0 < x := by { simp only [lt_top_iff_ne_top, inv_ne_top, pos_iff_ne_zero] } lemma div_lt_top {x y : ℝ≥0∞} (h1 : x ≠ ∞) (h2 : y ≠ 0) : x / y < ∞ := mul_lt_top h1 (inv_ne_top.mpr h2) @[simp] lemma inv_eq_zero : a⁻¹ = 0 ↔ a = ∞ := inv_top ▸ inv_inj lemma inv_ne_zero : a⁻¹ ≠ 0 ↔ a ≠ ∞ := by simp lemma mul_inv {a b : ℝ≥0∞} (ha : a ≠ 0 ∨ b ≠ ∞) (hb : a ≠ ∞ ∨ b ≠ 0) : (a * b)⁻¹ = a⁻¹ * b⁻¹ := begin induction b using with_top.rec_top_coe, { replace ha : a ≠ 0 := ha.neg_resolve_right rfl, simp [ha], }, induction a using with_top.rec_top_coe, { replace hb : b ≠ 0 := coe_ne_zero.1 (hb.neg_resolve_left rfl), simp [hb] }, by_cases h'a : a = 0, { simp only [h'a, with_top.top_mul, ennreal.inv_zero, ennreal.coe_ne_top, zero_mul, ne.def, not_false_iff, ennreal.coe_zero, ennreal.inv_eq_zero] }, by_cases h'b : b = 0, { simp only [h'b, ennreal.inv_zero, ennreal.coe_ne_top, with_top.mul_top, ne.def, not_false_iff, mul_zero, ennreal.coe_zero, ennreal.inv_eq_zero] }, rw [← ennreal.coe_mul, ← ennreal.coe_inv, ← ennreal.coe_inv h'a, ← ennreal.coe_inv h'b, ← ennreal.coe_mul, mul_inv_rev, mul_comm], simp [h'a, h'b], end protected lemma sub_div (h : 0 < b → b < a → c ≠ 0) : (a - b) / c = a / c - b / c := by { simp_rw div_eq_mul_inv, exact ennreal.sub_mul (by simpa using h) } @[simp] lemma inv_pos : 0 < a⁻¹ ↔ a ≠ ∞ := pos_iff_ne_zero.trans inv_ne_zero lemma inv_strict_anti : strict_anti (has_inv.inv : ℝ≥0∞ → ℝ≥0∞) := begin intros a b h, lift a to ℝ≥0 using h.ne_top, induction b using with_top.rec_top_coe, { simp }, rw [coe_lt_coe] at h, rcases eq_or_ne a 0 with rfl|ha, { simp [h] }, rw [← coe_inv h.ne_bot, ← coe_inv ha, coe_lt_coe], exact nnreal.inv_lt_inv ha h end @[simp] lemma inv_lt_inv : a⁻¹ < b⁻¹ ↔ b < a := inv_strict_anti.lt_iff_lt lemma inv_lt_iff_inv_lt : a⁻¹ < b ↔ b⁻¹ < a := by simpa only [inv_inv] using @inv_lt_inv a b⁻¹ lemma lt_inv_iff_lt_inv : a < b⁻¹ ↔ b < a⁻¹ := by simpa only [inv_inv] using @inv_lt_inv a⁻¹ b @[simp, priority 1100] -- higher than le_inv_iff_mul_le lemma inv_le_inv : a⁻¹ ≤ b⁻¹ ↔ b ≤ a := inv_strict_anti.le_iff_le lemma inv_le_iff_inv_le : a⁻¹ ≤ b ↔ b⁻¹ ≤ a := by simpa only [inv_inv] using @inv_le_inv a b⁻¹ lemma le_inv_iff_le_inv : a ≤ b⁻¹ ↔ b ≤ a⁻¹ := by simpa only [inv_inv] using @inv_le_inv a⁻¹ b @[simp] lemma inv_le_one : a⁻¹ ≤ 1 ↔ 1 ≤ a := inv_le_iff_inv_le.trans $ by rw inv_one lemma one_le_inv : 1 ≤ a⁻¹ ↔ a ≤ 1 := le_inv_iff_le_inv.trans $ by rw inv_one @[simp] lemma inv_lt_one : a⁻¹ < 1 ↔ 1 < a := inv_lt_iff_inv_lt.trans $ by rw [inv_one] /-- The inverse map `λ x, x⁻¹` is an order isomorphism between `ℝ≥0∞` and its `order_dual` -/ @[simps apply] def _root_.order_iso.inv_ennreal : ℝ≥0∞ ≃o ℝ≥0∞ᵒᵈ := { map_rel_iff' := λ a b, ennreal.inv_le_inv, to_equiv := (equiv.inv ℝ≥0∞).trans order_dual.to_dual } @[simp] lemma _root_.order_iso.inv_ennreal_symm_apply : order_iso.inv_ennreal.symm a = (order_dual.of_dual a)⁻¹ := rfl lemma pow_le_pow_of_le_one {n m : ℕ} (ha : a ≤ 1) (h : n ≤ m) : a ^ m ≤ a ^ n := begin rw [←inv_inv a, ← ennreal.inv_pow, ← @ennreal.inv_pow a⁻¹, inv_le_inv], exact pow_le_pow (one_le_inv.2 ha) h end @[simp] lemma div_top : a / ∞ = 0 := by rw [div_eq_mul_inv, inv_top, mul_zero] @[simp] lemma top_div_coe : ∞ / p = ∞ := by simp [div_eq_mul_inv, top_mul] lemma top_div_of_ne_top (h : a ≠ ∞) : ∞ / a = ∞ := by { lift a to ℝ≥0 using h, exact top_div_coe } lemma top_div_of_lt_top (h : a < ∞) : ∞ / a = ∞ := top_div_of_ne_top h.ne lemma top_div : ∞ / a = if a = ∞ then 0 else ∞ := by by_cases a = ∞; simp [top_div_of_ne_top, *] @[simp] lemma zero_div : 0 / a = 0 := zero_mul a⁻¹ lemma div_eq_top : a / b = ∞ ↔ (a ≠ 0 ∧ b = 0) ∨ (a = ∞ ∧ b ≠ ∞) := by simp [div_eq_mul_inv, ennreal.mul_eq_top] lemma le_div_iff_mul_le (h0 : b ≠ 0 ∨ c ≠ 0) (ht : b ≠ ∞ ∨ c ≠ ∞) : a ≤ c / b ↔ a * b ≤ c := begin induction b using with_top.rec_top_coe, { lift c to ℝ≥0 using ht.neg_resolve_left rfl, rw [div_top, nonpos_iff_eq_zero, mul_top], rcases eq_or_ne a 0 with rfl|ha; simp * }, rcases eq_or_ne b 0 with (rfl | hb), { have hc : c ≠ 0, from h0.neg_resolve_left rfl, simp [div_zero hc] }, { rw [← coe_ne_zero] at hb, rw [← ennreal.mul_le_mul_right hb coe_ne_top, div_mul_cancel hb coe_ne_top] }, end lemma div_le_iff_le_mul (hb0 : b ≠ 0 ∨ c ≠ ∞) (hbt : b ≠ ∞ ∨ c ≠ 0) : a / b ≤ c ↔ a ≤ c * b := begin suffices : a * b⁻¹ ≤ c ↔ a ≤ c / b⁻¹, by simpa [div_eq_mul_inv], refine (le_div_iff_mul_le _ _).symm; simpa end lemma lt_div_iff_mul_lt (hb0 : b ≠ 0 ∨ c ≠ ∞) (hbt : b ≠ ∞ ∨ c ≠ 0) : c < a / b ↔ c * b < a := lt_iff_lt_of_le_iff_le (div_le_iff_le_mul hb0 hbt) lemma div_le_of_le_mul (h : a ≤ b * c) : a / c ≤ b := begin by_cases h0 : c = 0, { have : a = 0, by simpa [h0] using h, simp [*] }, by_cases hinf : c = ∞, by simp [hinf], exact (div_le_iff_le_mul (or.inl h0) (or.inl hinf)).2 h end lemma div_le_of_le_mul' (h : a ≤ b * c) : a / b ≤ c := div_le_of_le_mul $ mul_comm b c ▸ h lemma mul_le_of_le_div (h : a ≤ b / c) : a * c ≤ b := begin rw [← inv_inv c], exact div_le_of_le_mul h, end lemma mul_le_of_le_div' (h : a ≤ b / c) : c * a ≤ b := mul_comm a c ▸ mul_le_of_le_div h protected lemma div_lt_iff (h0 : b ≠ 0 ∨ c ≠ 0) (ht : b ≠ ∞ ∨ c ≠ ∞) : c / b < a ↔ c < a * b := lt_iff_lt_of_le_iff_le $ le_div_iff_mul_le h0 ht lemma mul_lt_of_lt_div (h : a < b / c) : a * c < b := by { contrapose! h, exact ennreal.div_le_of_le_mul h } lemma mul_lt_of_lt_div' (h : a < b / c) : c * a < b := mul_comm a c ▸ mul_lt_of_lt_div h lemma inv_le_iff_le_mul (h₁ : b = ∞ → a ≠ 0) (h₂ : a = ∞ → b ≠ 0) : a⁻¹ ≤ b ↔ 1 ≤ a * b := begin rw [← one_div, div_le_iff_le_mul, mul_comm], exacts [or_not_of_imp h₁, not_or_of_imp h₂] end @[simp] lemma le_inv_iff_mul_le : a ≤ b⁻¹ ↔ a * b ≤ 1 := by rw [← one_div, le_div_iff_mul_le]; { right, simp } lemma div_le_div {a b c d : ℝ≥0∞} (hab : a ≤ b) (hdc : d ≤ c) : a / c ≤ b / d := div_eq_mul_inv b d ▸ div_eq_mul_inv a c ▸ ennreal.mul_le_mul hab (ennreal.inv_le_inv.mpr hdc) protected lemma div_le_div_left (h : a ≤ b) (c : ℝ≥0∞) : c / b ≤ c / a := ennreal.div_le_div le_rfl h protected lemma div_le_div_right (h : a ≤ b) (c : ℝ≥0∞) : a / c ≤ b / c := ennreal.div_le_div h le_rfl lemma eq_inv_of_mul_eq_one_left (h : a * b = 1) : a = b⁻¹ := begin have hb : b ≠ ∞, { rintro rfl, simpa [left_ne_zero_of_mul_eq_one h] using h }, rw [← mul_one a, ← mul_inv_cancel (right_ne_zero_of_mul_eq_one h) hb, ← mul_assoc, h, one_mul] end lemma mul_le_iff_le_inv {a b r : ℝ≥0∞} (hr₀ : r ≠ 0) (hr₁ : r ≠ ∞) : (r * a ≤ b ↔ a ≤ r⁻¹ * b) := by rw [← @ennreal.mul_le_mul_left _ a _ hr₀ hr₁, ← mul_assoc, mul_inv_cancel hr₀ hr₁, one_mul] lemma le_of_forall_nnreal_lt {x y : ℝ≥0∞} (h : ∀ r : ℝ≥0, ↑r < x → ↑r ≤ y) : x ≤ y := begin refine le_of_forall_ge_of_dense (λ r hr, _), lift r to ℝ≥0 using ne_top_of_lt hr, exact h r hr end lemma le_of_forall_pos_nnreal_lt {x y : ℝ≥0∞} (h : ∀ r : ℝ≥0, 0 < r → ↑r < x → ↑r ≤ y) : x ≤ y := le_of_forall_nnreal_lt $ λ r hr, (zero_le r).eq_or_lt.elim (λ h, h ▸ zero_le _) (λ h0, h r h0 hr) lemma eq_top_of_forall_nnreal_le {x : ℝ≥0∞} (h : ∀ r : ℝ≥0, ↑r ≤ x) : x = ∞ := top_unique $ le_of_forall_nnreal_lt $ λ r hr, h r lemma add_div : (a + b) / c = a / c + b / c := right_distrib a b (c⁻¹) lemma div_add_div_same {a b c : ℝ≥0∞} : a / c + b / c = (a + b) / c := add_div.symm lemma div_self (h0 : a ≠ 0) (hI : a ≠ ∞) : a / a = 1 := mul_inv_cancel h0 hI lemma mul_div_le : a * (b / a) ≤ b := mul_le_of_le_div' le_rfl -- TODO: add this lemma for an `is_unit` in any `division_monoid` lemma eq_div_iff (ha : a ≠ 0) (ha' : a ≠ ∞) : b = c / a ↔ a * b = c := ⟨λ h, by rw [h, mul_div_cancel' ha ha'], λ h, by rw [← h, mul_div_assoc, mul_div_cancel' ha ha']⟩ lemma div_eq_div_iff (ha : a ≠ 0) (ha' : a ≠ ∞) (hb : b ≠ 0) (hb' : b ≠ ∞) : c / b = d / a ↔ a * c = b * d := begin rw eq_div_iff ha ha', conv_rhs { rw eq_comm }, rw [← eq_div_iff hb hb', mul_div_assoc, eq_comm], end lemma inv_two_add_inv_two : (2:ℝ≥0∞)⁻¹ + 2⁻¹ = 1 := by rw [← two_mul, ← div_eq_mul_inv, div_self two_ne_zero two_ne_top] lemma inv_three_add_inv_three : (3 : ℝ≥0∞)⁻¹ + 3⁻¹ + 3⁻¹ = 1 := begin rw [show (3 : ℝ≥0∞)⁻¹ + 3⁻¹ + 3⁻¹ = 3 * 3⁻¹, by ring, ← div_eq_mul_inv, ennreal.div_self]; simp, end @[simp] lemma add_halves (a : ℝ≥0∞) : a / 2 + a / 2 = a := by rw [div_eq_mul_inv, ← mul_add, inv_two_add_inv_two, mul_one] @[simp] lemma add_thirds (a : ℝ≥0∞) : a / 3 + a / 3 + a / 3 = a := by rw [div_eq_mul_inv, ← mul_add, ← mul_add, inv_three_add_inv_three, mul_one] @[simp] lemma div_zero_iff : a / b = 0 ↔ a = 0 ∨ b = ∞ := by simp [div_eq_mul_inv] @[simp] lemma div_pos_iff : 0 < a / b ↔ a ≠ 0 ∧ b ≠ ∞ := by simp [pos_iff_ne_zero, not_or_distrib] lemma half_pos {a : ℝ≥0∞} (h : a ≠ 0) : 0 < a / 2 := by simp [h] lemma one_half_lt_one : (2⁻¹ : ℝ≥0∞) < 1 := inv_lt_one.2 $ one_lt_two lemma half_lt_self {a : ℝ≥0∞} (hz : a ≠ 0) (ht : a ≠ ∞) : a / 2 < a := begin lift a to ℝ≥0 using ht, rw coe_ne_zero at hz, rw [← coe_two, ← coe_div, coe_lt_coe], exacts [nnreal.half_lt_self hz, two_ne_zero' _] end lemma half_le_self : a / 2 ≤ a := le_add_self.trans_eq (add_halves _) lemma sub_half (h : a ≠ ∞) : a - a / 2 = a / 2 := begin lift a to ℝ≥0 using h, exact sub_eq_of_add_eq (mul_ne_top coe_ne_top $ by simp) (add_halves a) end @[simp] lemma one_sub_inv_two : (1:ℝ≥0∞) - 2⁻¹ = 2⁻¹ := by simpa only [div_eq_mul_inv, one_mul] using sub_half one_ne_top /-- The birational order isomorphism between `ℝ≥0∞` and the unit interval `set.Iic (1 : ℝ≥0∞)`. -/ @[simps apply_coe] def order_iso_Iic_one_birational : ℝ≥0∞ ≃o Iic (1 : ℝ≥0∞) := begin refine strict_mono.order_iso_of_right_inverse (λ x, ⟨(x⁻¹ + 1)⁻¹, inv_le_one.2 $ le_add_self⟩) (λ x y hxy, _) (λ x, (x⁻¹ - 1)⁻¹) (λ x, subtype.ext _), { simpa only [subtype.mk_lt_mk, inv_lt_inv, ennreal.add_lt_add_iff_right one_ne_top] }, { have : (1 : ℝ≥0∞) ≤ x⁻¹, from one_le_inv.2 x.2, simp only [inv_inv, subtype.coe_mk, tsub_add_cancel_of_le this] } end @[simp] lemma order_iso_Iic_one_birational_symm_apply (x : Iic (1 : ℝ≥0∞)) : order_iso_Iic_one_birational.symm x = (x⁻¹ - 1)⁻¹ := rfl /-- Order isomorphism between an initial interval in `ℝ≥0∞` and an initial interval in `ℝ≥0`. -/ @[simps apply_coe] def order_iso_Iic_coe (a : ℝ≥0) : Iic (a : ℝ≥0∞) ≃o Iic a := order_iso.symm { to_fun := λ x, ⟨x, coe_le_coe.2 x.2⟩, inv_fun := λ x, ⟨ennreal.to_nnreal x, coe_le_coe.1 $ coe_to_nnreal_le_self.trans x.2⟩, left_inv := λ x, subtype.ext $ to_nnreal_coe, right_inv := λ x, subtype.ext $ coe_to_nnreal (ne_top_of_le_ne_top coe_ne_top x.2), map_rel_iff' := λ x y, by simp only [equiv.coe_fn_mk, subtype.mk_le_mk, coe_coe, coe_le_coe, subtype.coe_le_coe] } @[simp] lemma order_iso_Iic_coe_symm_apply_coe (a : ℝ≥0) (b : Iic a) : ((order_iso_Iic_coe a).symm b : ℝ≥0∞) = b := rfl /-- An order isomorphism between the extended nonnegative real numbers and the unit interval. -/ def order_iso_unit_interval_birational : ℝ≥0∞ ≃o Icc (0 : ℝ) 1 := order_iso_Iic_one_birational.trans $ (order_iso_Iic_coe 1).trans $ (nnreal.order_iso_Icc_zero_coe 1).symm @[simp] lemma order_iso_unit_interval_birational_apply_coe (x : ℝ≥0∞) : (order_iso_unit_interval_birational x : ℝ) = (x⁻¹ + 1)⁻¹.to_real := rfl lemma exists_inv_nat_lt {a : ℝ≥0∞} (h : a ≠ 0) : ∃n:ℕ, (n:ℝ≥0∞)⁻¹ < a := inv_inv a ▸ by simp only [inv_lt_inv, ennreal.exists_nat_gt (inv_ne_top.2 h)] lemma exists_nat_pos_mul_gt (ha : a ≠ 0) (hb : b ≠ ∞) : ∃ n > 0, b < (n : ℕ) * a := begin have : b / a ≠ ∞, from mul_ne_top hb (inv_ne_top.2 ha), refine (ennreal.exists_nat_gt this).imp (λ n hn, _), have : ↑0 < (n : ℝ≥0∞), from lt_of_le_of_lt (by simp) hn, refine ⟨coe_nat_lt_coe_nat.1 this, _⟩, rwa [← ennreal.div_lt_iff (or.inl ha) (or.inr hb)] end lemma exists_nat_mul_gt (ha : a ≠ 0) (hb : b ≠ ∞) : ∃ n : ℕ, b < n * a := (exists_nat_pos_mul_gt ha hb).imp $ λ n, Exists.snd lemma exists_nat_pos_inv_mul_lt (ha : a ≠ ∞) (hb : b ≠ 0) : ∃ n > 0, ((n : ℕ) : ℝ≥0∞)⁻¹ * a < b := begin rcases exists_nat_pos_mul_gt hb ha with ⟨n, npos, hn⟩, have : (n : ℝ≥0∞) ≠ 0 := nat.cast_ne_zero.2 npos.lt.ne', use [n, npos], rwa [← one_mul b, ← inv_mul_cancel this (nat_ne_top n), mul_assoc, mul_lt_mul_left (inv_ne_zero.2 $ nat_ne_top _) (inv_ne_top.2 this)] end lemma exists_nnreal_pos_mul_lt (ha : a ≠ ∞) (hb : b ≠ 0) : ∃ n > 0, ↑(n : ℝ≥0) * a < b := begin rcases exists_nat_pos_inv_mul_lt ha hb with ⟨n, npos : 0 < n, hn⟩, use (n : ℝ≥0)⁻¹, simp [*, npos.ne', zero_lt_one] end lemma exists_inv_two_pow_lt (ha : a ≠ 0) : ∃ n : ℕ, 2⁻¹ ^ n < a := begin rcases exists_inv_nat_lt ha with ⟨n, hn⟩, refine ⟨n, lt_trans _ hn⟩, rw [← ennreal.inv_pow, inv_lt_inv], norm_cast, exact n.lt_two_pow end @[simp, norm_cast] lemma coe_zpow (hr : r ≠ 0) (n : ℤ) : (↑(r^n) : ℝ≥0∞) = r^n := begin cases n, { simp only [int.of_nat_eq_coe, coe_pow, zpow_coe_nat] }, { have : r ^ n.succ ≠ 0 := pow_ne_zero (n+1) hr, simp only [zpow_neg_succ_of_nat, coe_inv this, coe_pow] } end lemma zpow_pos (ha : a ≠ 0) (h'a : a ≠ ∞) (n : ℤ) : 0 < a ^ n := begin cases n, { exact ennreal.pow_pos ha.bot_lt n }, { simp only [h'a, pow_eq_top_iff, zpow_neg_succ_of_nat, ne.def, not_false_iff, inv_pos, false_and] } end lemma zpow_lt_top (ha : a ≠ 0) (h'a : a ≠ ∞) (n : ℤ) : a ^ n < ∞ := begin cases n, { exact ennreal.pow_lt_top h'a.lt_top _ }, { simp only [ennreal.pow_pos ha.bot_lt (n + 1), zpow_neg_succ_of_nat, inv_lt_top] } end lemma exists_mem_Ico_zpow {x y : ℝ≥0∞} (hx : x ≠ 0) (h'x : x ≠ ∞) (hy : 1 < y) (h'y : y ≠ ⊤) : ∃ n : ℤ, x ∈ Ico (y ^ n) (y ^ (n + 1)) := begin lift x to ℝ≥0 using h'x, lift y to ℝ≥0 using h'y, have A : y ≠ 0, { simpa only [ne.def, coe_eq_zero] using (zero_lt_one.trans hy).ne' }, obtain ⟨n, hn, h'n⟩ : ∃ n : ℤ, y ^ n ≤ x ∧ x < y ^ (n + 1), { refine nnreal.exists_mem_Ico_zpow _ (one_lt_coe_iff.1 hy), simpa only [ne.def, coe_eq_zero] using hx }, refine ⟨n, _, _⟩, { rwa [← ennreal.coe_zpow A, ennreal.coe_le_coe] }, { rwa [← ennreal.coe_zpow A, ennreal.coe_lt_coe] } end lemma exists_mem_Ioc_zpow {x y : ℝ≥0∞} (hx : x ≠ 0) (h'x : x ≠ ∞) (hy : 1 < y) (h'y : y ≠ ⊤) : ∃ n : ℤ, x ∈ Ioc (y ^ n) (y ^ (n + 1)) := begin lift x to ℝ≥0 using h'x, lift y to ℝ≥0 using h'y, have A : y ≠ 0, { simpa only [ne.def, coe_eq_zero] using (zero_lt_one.trans hy).ne' }, obtain ⟨n, hn, h'n⟩ : ∃ n : ℤ, y ^ n < x ∧ x ≤ y ^ (n + 1), { refine nnreal.exists_mem_Ioc_zpow _ (one_lt_coe_iff.1 hy), simpa only [ne.def, coe_eq_zero] using hx }, refine ⟨n, _, _⟩, { rwa [← ennreal.coe_zpow A, ennreal.coe_lt_coe] }, { rwa [← ennreal.coe_zpow A, ennreal.coe_le_coe] } end lemma Ioo_zero_top_eq_Union_Ico_zpow {y : ℝ≥0∞} (hy : 1 < y) (h'y : y ≠ ⊤) : Ioo (0 : ℝ≥0∞) (∞ : ℝ≥0∞) = ⋃ (n : ℤ), Ico (y^n) (y^(n+1)) := begin ext x, simp only [mem_Union, mem_Ioo, mem_Ico], split, { rintros ⟨hx, h'x⟩, exact exists_mem_Ico_zpow hx.ne' h'x.ne hy h'y }, { rintros ⟨n, hn, h'n⟩, split, { apply lt_of_lt_of_le _ hn, exact ennreal.zpow_pos (zero_lt_one.trans hy).ne' h'y _ }, { apply lt_trans h'n _, exact ennreal.zpow_lt_top (zero_lt_one.trans hy).ne' h'y _ } } end lemma zpow_le_of_le {x : ℝ≥0∞} (hx : 1 ≤ x) {a b : ℤ} (h : a ≤ b) : x ^ a ≤ x ^ b := begin induction a with a a; induction b with b b, { simp only [int.of_nat_eq_coe, zpow_coe_nat], exact pow_le_pow hx (int.le_of_coe_nat_le_coe_nat h), }, { apply absurd h (not_le_of_gt _), exact lt_of_lt_of_le (int.neg_succ_lt_zero _) (int.of_nat_nonneg _) }, { simp only [zpow_neg_succ_of_nat, int.of_nat_eq_coe, zpow_coe_nat], refine le_trans (inv_le_one.2 _) _; exact ennreal.one_le_pow_of_one_le hx _, }, { simp only [zpow_neg_succ_of_nat, inv_le_inv], apply pow_le_pow hx, simpa only [←int.coe_nat_le_coe_nat_iff, neg_le_neg_iff, int.coe_nat_add, int.coe_nat_one, int.neg_succ_of_nat_eq] using h } end lemma monotone_zpow {x : ℝ≥0∞} (hx : 1 ≤ x) : monotone ((^) x : ℤ → ℝ≥0∞) := λ a b h, zpow_le_of_le hx h lemma zpow_add {x : ℝ≥0∞} (hx : x ≠ 0) (h'x : x ≠ ∞) (m n : ℤ) : x ^ (m + n) = x ^ m * x ^ n := begin lift x to ℝ≥0 using h'x, replace hx : x ≠ 0, by simpa only [ne.def, coe_eq_zero] using hx, simp only [← coe_zpow hx, zpow_add₀ hx, coe_mul] end end inv section real lemma to_real_add (ha : a ≠ ∞) (hb : b ≠ ∞) : (a+b).to_real = a.to_real + b.to_real := begin lift a to ℝ≥0 using ha, lift b to ℝ≥0 using hb, refl end lemma to_real_sub_of_le {a b : ℝ≥0∞} (h : b ≤ a) (ha : a ≠ ∞): (a - b).to_real = a.to_real - b.to_real := begin lift b to ℝ≥0 using ne_top_of_le_ne_top ha h, lift a to ℝ≥0 using ha, simp only [← ennreal.coe_sub, ennreal.coe_to_real, nnreal.coe_sub (ennreal.coe_le_coe.mp h)], end lemma le_to_real_sub {a b : ℝ≥0∞} (hb : b ≠ ∞) : a.to_real - b.to_real ≤ (a - b).to_real := begin lift b to ℝ≥0 using hb, induction a using with_top.rec_top_coe, { simp }, { simp only [←coe_sub, nnreal.sub_def, real.coe_to_nnreal', coe_to_real], exact le_max_left _ _ } end lemma to_real_add_le : (a+b).to_real ≤ a.to_real + b.to_real := if ha : a = ∞ then by simp only [ha, top_add, top_to_real, zero_add, to_real_nonneg] else if hb : b = ∞ then by simp only [hb, add_top, top_to_real, add_zero, to_real_nonneg] else le_of_eq (to_real_add ha hb) lemma of_real_add {p q : ℝ} (hp : 0 ≤ p) (hq : 0 ≤ q) : ennreal.of_real (p + q) = ennreal.of_real p + ennreal.of_real q := by rw [ennreal.of_real, ennreal.of_real, ennreal.of_real, ← coe_add, coe_eq_coe, real.to_nnreal_add hp hq] lemma of_real_add_le {p q : ℝ} : ennreal.of_real (p + q) ≤ ennreal.of_real p + ennreal.of_real q := coe_le_coe.2 real.to_nnreal_add_le @[simp] lemma to_real_le_to_real (ha : a ≠ ∞) (hb : b ≠ ∞) : a.to_real ≤ b.to_real ↔ a ≤ b := begin lift a to ℝ≥0 using ha, lift b to ℝ≥0 using hb, norm_cast end lemma to_real_mono (hb : b ≠ ∞) (h : a ≤ b) : a.to_real ≤ b.to_real := (to_real_le_to_real (h.trans_lt (lt_top_iff_ne_top.2 hb)).ne hb).2 h @[simp] lemma to_real_lt_to_real (ha : a ≠ ∞) (hb : b ≠ ∞) : a.to_real < b.to_real ↔ a < b := begin lift a to ℝ≥0 using ha, lift b to ℝ≥0 using hb, norm_cast end lemma to_real_strict_mono (hb : b ≠ ∞) (h : a < b) : a.to_real < b.to_real := (to_real_lt_to_real (h.trans (lt_top_iff_ne_top.2 hb)).ne hb).2 h lemma to_nnreal_mono (hb : b ≠ ∞) (h : a ≤ b) : a.to_nnreal ≤ b.to_nnreal := by simpa [←ennreal.coe_le_coe, hb, (h.trans_lt hb.lt_top).ne] @[simp] lemma to_nnreal_le_to_nnreal (ha : a ≠ ∞) (hb : b ≠ ∞) : a.to_nnreal ≤ b.to_nnreal ↔ a ≤ b := ⟨λ h, by rwa [←coe_to_nnreal ha, ←coe_to_nnreal hb, coe_le_coe], to_nnreal_mono hb⟩ lemma to_nnreal_strict_mono (hb : b ≠ ∞) (h : a < b) : a.to_nnreal < b.to_nnreal := by simpa [←ennreal.coe_lt_coe, hb, (h.trans hb.lt_top).ne] @[simp] lemma to_nnreal_lt_to_nnreal (ha : a ≠ ∞) (hb : b ≠ ∞) : a.to_nnreal < b.to_nnreal ↔ a < b := ⟨λ h, by rwa [←coe_to_nnreal ha, ←coe_to_nnreal hb, coe_lt_coe], to_nnreal_strict_mono hb⟩ lemma to_real_max (hr : a ≠ ∞) (hp : b ≠ ∞) : ennreal.to_real (max a b) = max (ennreal.to_real a) (ennreal.to_real b) := (le_total a b).elim (λ h, by simp only [h, (ennreal.to_real_le_to_real hr hp).2 h, max_eq_right]) (λ h, by simp only [h, (ennreal.to_real_le_to_real hp hr).2 h, max_eq_left]) lemma to_real_min {a b : ℝ≥0∞} (hr : a ≠ ∞) (hp : b ≠ ∞) : ennreal.to_real (min a b) = min (ennreal.to_real a) (ennreal.to_real b) := (le_total a b).elim (λ h, by simp only [h, (ennreal.to_real_le_to_real hr hp).2 h, min_eq_left]) (λ h, by simp only [h, (ennreal.to_real_le_to_real hp hr).2 h, min_eq_right]) lemma to_real_sup {a b : ℝ≥0∞} : a ≠ ∞ → b ≠ ∞ → (a ⊔ b).to_real = a.to_real ⊔ b.to_real := to_real_max lemma to_real_inf {a b : ℝ≥0∞} : a ≠ ∞ → b ≠ ∞ → (a ⊓ b).to_real = a.to_real ⊓ b.to_real := to_real_min lemma to_nnreal_pos_iff : 0 < a.to_nnreal ↔ (0 < a ∧ a < ∞) := by { induction a using with_top.rec_top_coe; simp } lemma to_nnreal_pos {a : ℝ≥0∞} (ha₀ : a ≠ 0) (ha_top : a ≠ ∞) : 0 < a.to_nnreal := to_nnreal_pos_iff.mpr ⟨bot_lt_iff_ne_bot.mpr ha₀, lt_top_iff_ne_top.mpr ha_top⟩ lemma to_real_pos_iff : 0 < a.to_real ↔ (0 < a ∧ a < ∞):= (nnreal.coe_pos).trans to_nnreal_pos_iff lemma to_real_pos {a : ℝ≥0∞} (ha₀ : a ≠ 0) (ha_top : a ≠ ∞) : 0 < a.to_real := to_real_pos_iff.mpr ⟨bot_lt_iff_ne_bot.mpr ha₀, lt_top_iff_ne_top.mpr ha_top⟩ lemma of_real_le_of_real {p q : ℝ} (h : p ≤ q) : ennreal.of_real p ≤ ennreal.of_real q := by simp [ennreal.of_real, real.to_nnreal_le_to_nnreal h] lemma of_real_le_of_le_to_real {a : ℝ} {b : ℝ≥0∞} (h : a ≤ ennreal.to_real b) : ennreal.of_real a ≤ b := (of_real_le_of_real h).trans of_real_to_real_le @[simp] lemma of_real_le_of_real_iff {p q : ℝ} (h : 0 ≤ q) : ennreal.of_real p ≤ ennreal.of_real q ↔ p ≤ q := by rw [ennreal.of_real, ennreal.of_real, coe_le_coe, real.to_nnreal_le_to_nnreal_iff h] @[simp] lemma of_real_eq_of_real_iff {p q : ℝ} (hp : 0 ≤ p) (hq : 0 ≤ q) : ennreal.of_real p = ennreal.of_real q ↔ p = q := by rw [ennreal.of_real, ennreal.of_real, coe_eq_coe, real.to_nnreal_eq_to_nnreal_iff hp hq] @[simp] lemma of_real_lt_of_real_iff {p q : ℝ} (h : 0 < q) : ennreal.of_real p < ennreal.of_real q ↔ p < q := by rw [ennreal.of_real, ennreal.of_real, coe_lt_coe, real.to_nnreal_lt_to_nnreal_iff h] lemma of_real_lt_of_real_iff_of_nonneg {p q : ℝ} (hp : 0 ≤ p) : ennreal.of_real p < ennreal.of_real q ↔ p < q := by rw [ennreal.of_real, ennreal.of_real, coe_lt_coe, real.to_nnreal_lt_to_nnreal_iff_of_nonneg hp] @[simp] lemma of_real_pos {p : ℝ} : 0 < ennreal.of_real p ↔ 0 < p := by simp [ennreal.of_real] @[simp] lemma of_real_eq_zero {p : ℝ} : ennreal.of_real p = 0 ↔ p ≤ 0 := by simp [ennreal.of_real] @[simp] lemma zero_eq_of_real {p : ℝ} : 0 = ennreal.of_real p ↔ p ≤ 0 := eq_comm.trans of_real_eq_zero alias of_real_eq_zero ↔ _ of_real_of_nonpos lemma of_real_sub (p : ℝ) {q : ℝ} (hq : 0 ≤ q) : ennreal.of_real (p - q) = ennreal.of_real p - ennreal.of_real q := begin obtain h | h := le_total p q, { rw [of_real_of_nonpos (sub_nonpos_of_le h), tsub_eq_zero_of_le (of_real_le_of_real h)] }, refine ennreal.eq_sub_of_add_eq of_real_ne_top _, rw [←of_real_add (sub_nonneg_of_le h) hq, sub_add_cancel], end lemma of_real_le_iff_le_to_real {a : ℝ} {b : ℝ≥0∞} (hb : b ≠ ∞) : ennreal.of_real a ≤ b ↔ a ≤ ennreal.to_real b := begin lift b to ℝ≥0 using hb, simpa [ennreal.of_real, ennreal.to_real] using real.to_nnreal_le_iff_le_coe end lemma of_real_lt_iff_lt_to_real {a : ℝ} {b : ℝ≥0∞} (ha : 0 ≤ a) (hb : b ≠ ∞) : ennreal.of_real a < b ↔ a < ennreal.to_real b := begin lift b to ℝ≥0 using hb, simpa [ennreal.of_real, ennreal.to_real] using real.to_nnreal_lt_iff_lt_coe ha end lemma le_of_real_iff_to_real_le {a : ℝ≥0∞} {b : ℝ} (ha : a ≠ ∞) (hb : 0 ≤ b) : a ≤ ennreal.of_real b ↔ ennreal.to_real a ≤ b := begin lift a to ℝ≥0 using ha, simpa [ennreal.of_real, ennreal.to_real] using real.le_to_nnreal_iff_coe_le hb end lemma to_real_le_of_le_of_real {a : ℝ≥0∞} {b : ℝ} (hb : 0 ≤ b) (h : a ≤ ennreal.of_real b) : ennreal.to_real a ≤ b := have ha : a ≠ ∞, from ne_top_of_le_ne_top of_real_ne_top h, (le_of_real_iff_to_real_le ha hb).1 h lemma lt_of_real_iff_to_real_lt {a : ℝ≥0∞} {b : ℝ} (ha : a ≠ ∞) : a < ennreal.of_real b ↔ ennreal.to_real a < b := begin lift a to ℝ≥0 using ha, simpa [ennreal.of_real, ennreal.to_real] using real.lt_to_nnreal_iff_coe_lt end lemma of_real_mul {p q : ℝ} (hp : 0 ≤ p) : ennreal.of_real (p * q) = ennreal.of_real p * ennreal.of_real q := by simp only [ennreal.of_real, ← coe_mul, real.to_nnreal_mul hp] lemma of_real_mul' {p q : ℝ} (hq : 0 ≤ q) : ennreal.of_real (p * q) = ennreal.of_real p * ennreal.of_real q := by rw [mul_comm, of_real_mul hq, mul_comm] lemma of_real_pow {p : ℝ} (hp : 0 ≤ p) (n : ℕ) : ennreal.of_real (p ^ n) = ennreal.of_real p ^ n := by rw [of_real_eq_coe_nnreal hp, ← coe_pow, ← of_real_coe_nnreal, nnreal.coe_pow, nnreal.coe_mk] lemma of_real_nsmul {x : ℝ} {n : ℕ} : ennreal.of_real (n • x) = n • ennreal.of_real x := by simp only [nsmul_eq_mul, ← of_real_coe_nat n, ← of_real_mul n.cast_nonneg] lemma of_real_inv_of_pos {x : ℝ} (hx : 0 < x) : (ennreal.of_real x)⁻¹ = ennreal.of_real x⁻¹ := by rw [ennreal.of_real, ennreal.of_real, ←@coe_inv (real.to_nnreal x) (by simp [hx]), coe_eq_coe, real.to_nnreal_inv.symm] lemma of_real_div_of_pos {x y : ℝ} (hy : 0 < y) : ennreal.of_real (x / y) = ennreal.of_real x / ennreal.of_real y := by rw [div_eq_mul_inv, div_eq_mul_inv, of_real_mul' (inv_nonneg.2 hy.le), of_real_inv_of_pos hy] @[simp] lemma to_nnreal_mul {a b : ℝ≥0∞} : (a * b).to_nnreal = a.to_nnreal * b.to_nnreal := with_top.untop'_zero_mul a b lemma to_nnreal_mul_top (a : ℝ≥0∞) : ennreal.to_nnreal (a * ∞) = 0 := by simp lemma to_nnreal_top_mul (a : ℝ≥0∞) : ennreal.to_nnreal (∞ * a) = 0 := by simp @[simp] lemma smul_to_nnreal (a : ℝ≥0) (b : ℝ≥0∞) : (a • b).to_nnreal = a * b.to_nnreal := begin change ((a : ℝ≥0∞) * b).to_nnreal = a * b.to_nnreal, simp only [ennreal.to_nnreal_mul, ennreal.to_nnreal_coe], end /-- `ennreal.to_nnreal` as a `monoid_hom`. -/ def to_nnreal_hom : ℝ≥0∞ →* ℝ≥0 := { to_fun := ennreal.to_nnreal, map_one' := to_nnreal_coe, map_mul' := λ _ _, to_nnreal_mul } @[simp] lemma to_nnreal_pow (a : ℝ≥0∞) (n : ℕ) : (a ^ n).to_nnreal = a.to_nnreal ^ n := to_nnreal_hom.map_pow a n @[simp] lemma to_nnreal_prod {ι : Type*} {s : finset ι} {f : ι → ℝ≥0∞} : (∏ i in s, f i).to_nnreal = ∏ i in s, (f i).to_nnreal := to_nnreal_hom.map_prod _ _ /-- `ennreal.to_real` as a `monoid_hom`. -/ def to_real_hom : ℝ≥0∞ →* ℝ := (nnreal.to_real_hom : ℝ≥0 →* ℝ).comp to_nnreal_hom @[simp] lemma to_real_mul : (a * b).to_real = a.to_real * b.to_real := to_real_hom.map_mul a b @[simp] lemma to_real_pow (a : ℝ≥0∞) (n : ℕ) : (a ^ n).to_real = a.to_real ^ n := to_real_hom.map_pow a n @[simp] lemma to_real_prod {ι : Type*} {s : finset ι} {f : ι → ℝ≥0∞} : (∏ i in s, f i).to_real = ∏ i in s, (f i).to_real := to_real_hom.map_prod _ _ lemma to_real_of_real_mul (c : ℝ) (a : ℝ≥0∞) (h : 0 ≤ c) : ennreal.to_real ((ennreal.of_real c) * a) = c * ennreal.to_real a := by rw [ennreal.to_real_mul, ennreal.to_real_of_real h] lemma to_real_mul_top (a : ℝ≥0∞) : ennreal.to_real (a * ∞) = 0 := by rw [to_real_mul, top_to_real, mul_zero] lemma to_real_top_mul (a : ℝ≥0∞) : ennreal.to_real (∞ * a) = 0 := by { rw mul_comm, exact to_real_mul_top _ } lemma to_real_eq_to_real (ha : a ≠ ∞) (hb : b ≠ ∞) : ennreal.to_real a = ennreal.to_real b ↔ a = b := begin lift a to ℝ≥0 using ha, lift b to ℝ≥0 using hb, simp only [coe_eq_coe, nnreal.coe_eq, coe_to_real], end lemma to_real_smul (r : ℝ≥0) (s : ℝ≥0∞) : (r • s).to_real = r • s.to_real := by { rw [ennreal.smul_def, smul_eq_mul, to_real_mul, coe_to_real], refl } protected lemma trichotomy (p : ℝ≥0∞) : p = 0 ∨ p = ∞ ∨ 0 < p.to_real := by simpa only [or_iff_not_imp_left] using to_real_pos protected lemma trichotomy₂ {p q : ℝ≥0∞} (hpq : p ≤ q) : (p = 0 ∧ q = 0) ∨ (p = 0 ∧ q = ∞) ∨ (p = 0 ∧ 0 < q.to_real) ∨ (p = ∞ ∧ q = ∞) ∨ (0 < p.to_real ∧ q = ∞) ∨ (0 < p.to_real ∧ 0 < q.to_real ∧ p.to_real ≤ q.to_real) := begin rcases eq_or_lt_of_le (bot_le : 0 ≤ p) with (rfl : 0 = p) | (hp : 0 < p), { simpa using q.trichotomy }, rcases eq_or_lt_of_le (le_top : q ≤ ∞) with rfl | hq, { simpa using p.trichotomy }, repeat { right }, have hq' : 0 < q := lt_of_lt_of_le hp hpq, have hp' : p < ∞ := lt_of_le_of_lt hpq hq, simp [ennreal.to_real_le_to_real hp'.ne hq.ne, ennreal.to_real_pos_iff, hpq, hp, hp', hq', hq], end protected lemma dichotomy (p : ℝ≥0∞) [fact (1 ≤ p)] : p = ∞ ∨ 1 ≤ p.to_real := begin have : p = ⊤ ∨ 0 < p.to_real ∧ 1 ≤ p.to_real, { simpa using ennreal.trichotomy₂ (fact.out _ : 1 ≤ p) }, exact this.imp_right (λ h, h.2) end lemma to_real_pos_iff_ne_top (p : ℝ≥0∞) [fact (1 ≤ p)] : 0 < p.to_real ↔ p ≠ ∞ := ⟨λ h hp, let this : (0 : ℝ) ≠ 0 := top_to_real ▸ (hp ▸ h.ne : 0 ≠ ∞.to_real) in this rfl, λ h, zero_lt_one.trans_le (p.dichotomy.resolve_left h)⟩ lemma to_nnreal_inv (a : ℝ≥0∞) : (a⁻¹).to_nnreal = (a.to_nnreal)⁻¹ := begin induction a using with_top.rec_top_coe, { simp }, rcases eq_or_ne a 0 with rfl|ha, { simp }, rw [← coe_inv ha, to_nnreal_coe, to_nnreal_coe] end lemma to_nnreal_div (a b : ℝ≥0∞) : (a / b).to_nnreal = a.to_nnreal / b.to_nnreal := by rw [div_eq_mul_inv, to_nnreal_mul, to_nnreal_inv, div_eq_mul_inv] lemma to_real_inv (a : ℝ≥0∞) : (a⁻¹).to_real = (a.to_real)⁻¹ := by { simp_rw ennreal.to_real, norm_cast, exact to_nnreal_inv a, } lemma to_real_div (a b : ℝ≥0∞) : (a / b).to_real = a.to_real / b.to_real := by rw [div_eq_mul_inv, to_real_mul, to_real_inv, div_eq_mul_inv] lemma of_real_prod_of_nonneg {s : finset α} {f : α → ℝ} (hf : ∀ i, i ∈ s → 0 ≤ f i) : ennreal.of_real (∏ i in s, f i) = ∏ i in s, ennreal.of_real (f i) := begin simp_rw [ennreal.of_real, ←coe_finset_prod, coe_eq_coe], exact real.to_nnreal_prod_of_nonneg hf, end @[simp] lemma to_nnreal_bit0 {x : ℝ≥0∞} : (bit0 x).to_nnreal = bit0 (x.to_nnreal) := begin induction x using with_top.rec_top_coe, { simp }, { exact to_nnreal_add coe_ne_top coe_ne_top } end @[simp] lemma to_nnreal_bit1 {x : ℝ≥0∞} (hx_top : x ≠ ∞) : (bit1 x).to_nnreal = bit1 (x.to_nnreal) := by simp [bit1, bit1, to_nnreal_add (by rwa [ne.def, bit0_eq_top_iff]) ennreal.one_ne_top] @[simp] lemma to_real_bit0 {x : ℝ≥0∞} : (bit0 x).to_real = bit0 (x.to_real) := by simp [ennreal.to_real] @[simp] lemma to_real_bit1 {x : ℝ≥0∞} (hx_top : x ≠ ∞) : (bit1 x).to_real = bit1 (x.to_real) := by simp [ennreal.to_real, hx_top] @[simp] lemma of_real_bit0 (r : ℝ) : ennreal.of_real (bit0 r) = bit0 (ennreal.of_real r) := by simp [ennreal.of_real] @[simp] lemma of_real_bit1 {r : ℝ} (hr : 0 ≤ r) : ennreal.of_real (bit1 r) = bit1 (ennreal.of_real r) := (of_real_add (by simp [hr]) zero_le_one).trans (by simp [real.to_nnreal_one, bit1]) end real section infi variables {ι : Sort*} {f g : ι → ℝ≥0∞} lemma infi_add : infi f + a = ⨅i, f i + a := le_antisymm (le_infi $ assume i, add_le_add (infi_le _ _) $ le_rfl) (tsub_le_iff_right.1 $ le_infi $ assume i, tsub_le_iff_right.2 $ infi_le _ _) lemma supr_sub : (⨆i, f i) - a = (⨆i, f i - a) := le_antisymm (tsub_le_iff_right.2 $ supr_le $ assume i, tsub_le_iff_right.1 $ le_supr _ i) (supr_le $ assume i, tsub_le_tsub (le_supr _ _) (le_refl a)) lemma sub_infi : a - (⨅i, f i) = (⨆i, a - f i) := begin refine (eq_of_forall_ge_iff $ λ c, _), rw [tsub_le_iff_right, add_comm, infi_add], simp [tsub_le_iff_right, sub_eq_add_neg, add_comm], end lemma Inf_add {s : set ℝ≥0∞} : Inf s + a = ⨅b∈s, b + a := by simp [Inf_eq_infi, infi_add] lemma add_infi {a : ℝ≥0∞} : a + infi f = ⨅b, a + f b := by rw [add_comm, infi_add]; simp [add_comm] lemma infi_add_infi (h : ∀i j, ∃k, f k + g k ≤ f i + g j) : infi f + infi g = (⨅a, f a + g a) := suffices (⨅a, f a + g a) ≤ infi f + infi g, from le_antisymm (le_infi $ assume a, add_le_add (infi_le _ _) (infi_le _ _)) this, calc (⨅a, f a + g a) ≤ (⨅ a a', f a + g a') : le_infi $ assume a, le_infi $ assume a', let ⟨k, h⟩ := h a a' in infi_le_of_le k h ... = infi f + infi g : by simp [add_infi, infi_add] lemma infi_sum {f : ι → α → ℝ≥0∞} {s : finset α} [nonempty ι] (h : ∀(t : finset α) (i j : ι), ∃k, ∀a∈t, f k a ≤ f i a ∧ f k a ≤ f j a) : (⨅i, ∑ a in s, f i a) = ∑ a in s, ⨅i, f i a := begin induction s using finset.induction_on with a s ha ih, { simp }, have : ∀ (i j : ι), ∃ (k : ι), f k a + ∑ b in s, f k b ≤ f i a + ∑ b in s, f j b, { intros i j, obtain ⟨k, hk⟩ := h (insert a s) i j, exact ⟨k, add_le_add (hk a (finset.mem_insert_self _ _)).left $ finset.sum_le_sum $ λ a ha, (hk _ $ finset.mem_insert_of_mem ha).right⟩ }, simp [ha, ih.symm, infi_add_infi this] end /-- If `x ≠ 0` and `x ≠ ∞`, then right multiplication by `x` maps infimum to infimum. See also `ennreal.infi_mul` that assumes `[nonempty ι]` but does not require `x ≠ 0`. -/ lemma infi_mul_of_ne {ι} {f : ι → ℝ≥0∞} {x : ℝ≥0∞} (h0 : x ≠ 0) (h : x ≠ ∞) : infi f * x = ⨅ i, f i * x := le_antisymm mul_right_mono.map_infi_le ((div_le_iff_le_mul (or.inl h0) $ or.inl h).mp $ le_infi $ λ i, (div_le_iff_le_mul (or.inl h0) $ or.inl h).mpr $ infi_le _ _) /-- If `x ≠ ∞`, then right multiplication by `x` maps infimum over a nonempty type to infimum. See also `ennreal.infi_mul_of_ne` that assumes `x ≠ 0` but does not require `[nonempty ι]`. -/ lemma infi_mul {ι} [nonempty ι] {f : ι → ℝ≥0∞} {x : ℝ≥0∞} (h : x ≠ ∞) : infi f * x = ⨅ i, f i * x := begin by_cases h0 : x = 0, { simp only [h0, mul_zero, infi_const] }, { exact infi_mul_of_ne h0 h } end /-- If `x ≠ ∞`, then left multiplication by `x` maps infimum over a nonempty type to infimum. See also `ennreal.mul_infi_of_ne` that assumes `x ≠ 0` but does not require `[nonempty ι]`. -/ lemma mul_infi {ι} [nonempty ι] {f : ι → ℝ≥0∞} {x : ℝ≥0∞} (h : x ≠ ∞) : x * infi f = ⨅ i, x * f i := by simpa only [mul_comm] using infi_mul h /-- If `x ≠ 0` and `x ≠ ∞`, then left multiplication by `x` maps infimum to infimum. See also `ennreal.mul_infi` that assumes `[nonempty ι]` but does not require `x ≠ 0`. -/ lemma mul_infi_of_ne {ι} {f : ι → ℝ≥0∞} {x : ℝ≥0∞} (h0 : x ≠ 0) (h : x ≠ ∞) : x * infi f = ⨅ i, x * f i := by simpa only [mul_comm] using infi_mul_of_ne h0 h /-! `supr_mul`, `mul_supr` and variants are in `topology.instances.ennreal`. -/ end infi section supr @[simp] lemma supr_eq_zero {ι : Sort*} {f : ι → ℝ≥0∞} : (⨆ i, f i) = 0 ↔ ∀ i, f i = 0 := supr_eq_bot @[simp] lemma supr_zero_eq_zero {ι : Sort*} : (⨆ i : ι, (0 : ℝ≥0∞)) = 0 := by simp lemma sup_eq_zero {a b : ℝ≥0∞} : a ⊔ b = 0 ↔ a = 0 ∧ b = 0 := sup_eq_bot_iff lemma supr_coe_nat : (⨆n:ℕ, (n : ℝ≥0∞)) = ∞ := (supr_eq_top _).2 $ assume b hb, ennreal.exists_nat_gt (lt_top_iff_ne_top.1 hb) end supr end ennreal namespace set namespace ord_connected variables {s : set ℝ} {t : set ℝ≥0} {u : set ℝ≥0∞} lemma preimage_coe_nnreal_ennreal (h : u.ord_connected) : (coe ⁻¹' u : set ℝ≥0).ord_connected := h.preimage_mono ennreal.coe_mono lemma image_coe_nnreal_ennreal (h : t.ord_connected) : (coe '' t : set ℝ≥0∞).ord_connected := begin refine ⟨ball_image_iff.2 $ λ x hx, ball_image_iff.2 $ λ y hy z hz, _⟩, rcases ennreal.le_coe_iff.1 hz.2 with ⟨z, rfl, hzy⟩, exact mem_image_of_mem _ (h.out hx hy ⟨ennreal.coe_le_coe.1 hz.1, ennreal.coe_le_coe.1 hz.2⟩) end lemma preimage_ennreal_of_real (h : u.ord_connected) : (ennreal.of_real ⁻¹' u).ord_connected := h.preimage_coe_nnreal_ennreal.preimage_real_to_nnreal lemma image_ennreal_of_real (h : s.ord_connected) : (ennreal.of_real '' s).ord_connected := by simpa only [image_image] using h.image_real_to_nnreal.image_coe_nnreal_ennreal end ord_connected end set namespace tactic open positivity private lemma nnreal_coe_pos {r : ℝ≥0} : 0 < r → 0 < (r : ℝ≥0∞) := ennreal.coe_pos.2 /-- Extension for the `positivity` tactic: cast from `ℝ≥0` to `ℝ≥0∞`. -/ @[positivity] meta def positivity_coe_nnreal_ennreal : expr → tactic strictness | `(@coe _ _ %%inst %%a) := do unify inst `(@coe_to_lift _ _ $ @coe_base _ _ ennreal.has_coe), positive p ← core a, -- We already know `0 ≤ r` for all `r : ℝ≥0∞` positive <$> mk_app ``nnreal_coe_pos [p] | e := pp e >>= fail ∘ format.bracket "The expression " " is not of the form `(r : ℝ≥0∞)` for `r : ℝ≥0`" private lemma ennreal_of_real_pos {r : ℝ} : 0 < r → 0 < ennreal.of_real r := ennreal.of_real_pos.2 /-- Extension for the `positivity` tactic: `ennreal.of_real` is positive if its input is. -/ @[positivity] meta def positivity_ennreal_of_real : expr → tactic strictness | `(ennreal.of_real %%r) := do positive p ← core r, positive <$> mk_app ``ennreal_of_real_pos [p] -- This case is handled by `tactic.positivity_canon` | e := pp e >>= fail ∘ format.bracket "The expression `" "` is not of the form `ennreal.of_real r`" end tactic
d6e0eca2acbaa4730b8925c3a0cc7a4d4d19f087
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/algebra/Mon/colimits.lean
3d6bbe71d37eb3ad8f837ed385813dbd3ce245c6
[ "Apache-2.0" ]
permissive
ChrisHughes24/mathlib
98322577c460bc6b1fe5c21f42ce33ad1c3e5558
a2a867e827c2a6702beb9efc2b9282bd801d5f9a
refs/heads/master
1,583,848,251,477
1,565,164,247,000
1,565,164,247,000
129,409,993
0
1
Apache-2.0
1,565,164,817,000
1,523,628,059,000
Lean
UTF-8
Lean
false
false
6,688
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import algebra.Mon.basic import category_theory.limits.limits /-! We build colimits of monoids. We do so knowing nothing about monoids. In particular, I want to claim that this file could be produced by a python script that just looks at the output of `#print monoid`: -- structure monoid : Type u → Type u -- fields: -- monoid.mul : Π {α : Type u} [c : monoid α], α → α → α -- monoid.mul_assoc : ∀ {α : Type u} [c : monoid α] (a b c_1 : α), a * b * c_1 = a * (b * c_1) -- monoid.one : Π (α : Type u) [c : monoid α], α -- monoid.one_mul : ∀ {α : Type u} [c : monoid α] (a : α), 1 * a = a -- monoid.mul_one : ∀ {α : Type u} [c : monoid α] (a : α), a * 1 = a and if we'd fed it the output of `#print comm_ring`, this file would instead build colimits of commutative rings. A slightly bolder claim is that we could do this with tactics, as well. -/ universes v open category_theory open category_theory.limits namespace Mon.colimits variables {J : Type v} [small_category J] (F : J ⥤ Mon.{v}) inductive prequotient -- There's always `of` | of : Π (j : J) (x : (F.obj j).α), prequotient -- Then one generator for each operation | one {} : prequotient | mul : prequotient → prequotient → prequotient open prequotient inductive relation : prequotient F → prequotient F → Prop -- Make it an equivalence relation: | refl : Π (x), relation x x | symm : Π (x y) (h : relation x y), relation y x | trans : Π (x y z) (h : relation x y) (k : relation y z), relation x z -- There's always a `map` relation | map : Π (j j' : J) (f : j ⟶ j') (x : (F.obj j).α), relation (of j' ((F.map f) x)) (of j x) -- Then one relation per operation, describing the interaction with `of` | mul : Π (j) (x y : (F.obj j).α), relation (of j (x * y)) (mul (of j x) (of j y)) | one : Π (j), relation (of j 1) one -- Then one relation per argument of each operation | mul_1 : Π (x x' y) (r : relation x x'), relation (mul x y) (mul x' y) | mul_2 : Π (x y y') (r : relation y y'), relation (mul x y) (mul x y') -- And one relation per axiom | mul_assoc : Π (x y z), relation (mul (mul x y) z) (mul x (mul y z)) | one_mul : Π (x), relation (mul one x) x | mul_one : Π (x), relation (mul x one) x def colimit_setoid : setoid (prequotient F) := { r := relation F, iseqv := ⟨relation.refl, relation.symm, relation.trans⟩ } attribute [instance] colimit_setoid def colimit_type : Type v := quotient (colimit_setoid F) instance monoid_colimit_type : monoid (colimit_type F) := { mul := begin fapply @quot.lift _ _ ((colimit_type F) → (colimit_type F)), { intro x, fapply @quot.lift, { intro y, exact quot.mk _ (mul x y) }, { intros y y' r, apply quot.sound, exact relation.mul_2 _ _ _ r } }, { intros x x' r, funext y, induction y, dsimp, apply quot.sound, { exact relation.mul_1 _ _ _ r }, { refl } }, end, one := begin exact quot.mk _ one end, mul_assoc := λ x y z, begin induction x, induction y, induction z, dsimp, apply quot.sound, apply relation.mul_assoc, refl, refl, refl, end, one_mul := λ x, begin induction x, dsimp, apply quot.sound, apply relation.one_mul, refl, end, mul_one := λ x, begin induction x, dsimp, apply quot.sound, apply relation.mul_one, refl, end } @[simp] lemma quot_one : quot.mk setoid.r one = (1 : colimit_type F) := rfl @[simp] lemma quot_mul (x y) : quot.mk setoid.r (mul x y) = ((quot.mk setoid.r x) * (quot.mk setoid.r y) : colimit_type F) := rfl def colimit : Mon := ⟨colimit_type F, by apply_instance⟩ def cocone_fun (j : J) (x : (F.obj j).α) : colimit_type F := quot.mk _ (of j x) instance cocone_is_hom (j : J) : is_monoid_hom (cocone_fun F j) := { map_one := begin apply quot.sound, apply relation.one, end, map_mul := λ x y, begin apply quot.sound, apply relation.mul, end } def cocone_morphism (j : J) : F.obj j ⟶ colimit F := { val := cocone_fun F j, property := by apply_instance } @[simp] lemma cocone_naturality {j j' : J} (f : j ⟶ j') : F.map f ≫ (cocone_morphism F j') = cocone_morphism F j := begin ext, apply quot.sound, apply relation.map, end @[simp] lemma cocone_naturality_components (j j' : J) (f : j ⟶ j') (x : F.obj j): (cocone_morphism F j') (F.map f x) = (cocone_morphism F j) x := by { rw ←cocone_naturality F f, refl } def colimit_cocone : cocone F := { X := colimit F, ι := { app := cocone_morphism F, } }. @[simp] def desc_fun_lift (s : cocone F) : prequotient F → s.X | (of j x) := (s.ι.app j) x | one := 1 | (mul x y) := desc_fun_lift x * desc_fun_lift y def desc_fun (s : cocone F) : colimit_type F → s.X := begin fapply quot.lift, { exact desc_fun_lift F s }, { intros x y r, induction r; try { dsimp }, -- refl { refl }, -- symm { exact r_ih.symm }, -- trans { exact eq.trans r_ih_h r_ih_k }, -- map { rw cocone.naturality_bundled, }, -- mul { rw is_monoid_hom.map_mul ⇑((s.ι).app r_j) }, -- one { erw is_monoid_hom.map_one ⇑((s.ι).app r), refl }, -- mul_1 { rw r_ih, }, -- mul_2 { rw r_ih, }, -- mul_assoc { rw mul_assoc, }, -- one_mul { rw one_mul, }, -- mul_one { rw mul_one, } } end instance desc_fun_is_morphism (s : cocone F) : is_monoid_hom (desc_fun F s) := { map_one := rfl, map_mul := λ x y, begin induction x, induction y, refl, refl, refl, end, } @[simp] def desc_morphism (s : cocone F) : colimit F ⟶ s.X := { val := desc_fun F s, property := by apply_instance } def colimit_is_colimit : is_colimit (colimit_cocone F) := { desc := λ s, desc_morphism F s, uniq' := λ s m w, begin ext, induction x, induction x, { have w' := congr_fun (congr_arg (λ f : F.obj x_j ⟶ s.X, (f : F.obj x_j → s.X)) (w x_j)) x_x, erw w', refl, }, { simp only [desc_morphism, quot_one], erw is_monoid_hom.map_one ⇑m, refl, }, { simp only [desc_morphism, quot_mul], erw is_monoid_hom.map_mul ⇑m, rw [x_ih_a, x_ih_a_1], refl, }, refl end }. -- FIXME why is this infer_instance needed!? instance has_colimits_Mon : @has_colimits Mon.{v} infer_instance := { has_colimits_of_shape := λ J 𝒥, { has_colimit := λ F, by exactI { cocone := colimit_cocone F, is_colimit := colimit_is_colimit F } } } end Mon.colimits
4de9cd008e3a39cb6d1d7985d21034faff6fdbce
4950bf76e5ae40ba9f8491647d0b6f228ddce173
/src/data/pi.lean
571b9d2098f08dd6e8dfdc3ed1e33e265571dc91
[ "Apache-2.0" ]
permissive
ntzwq/mathlib
ca50b21079b0a7c6781c34b62199a396dd00cee2
36eec1a98f22df82eaccd354a758ef8576af2a7f
refs/heads/master
1,675,193,391,478
1,607,822,996,000
1,607,822,996,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,047
lean
/- Copyright (c) 2020 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Patrick Massot, Eric Wieser -/ import tactic.split_ifs import tactic.simpa import algebra.group.to_additive /-! # Instances and theorems on pi types This file provides basic definitions and notation instances for Pi types. Instances of more sophisticated classes are defined in `pi.lean` files elsewhere. -/ universes u v w variable {I : Type u} -- The indexing type variable {f : I → Type v} -- The family of types already equipped with instances variables (x y : Π i, f i) (i : I) namespace pi /-! `1`, `0`, `+`, `*`, `-`, `⁻¹`, and `/` are defined pointwise. -/ @[to_additive] instance has_one [∀ i, has_one $ f i] : has_one (Π i : I, f i) := ⟨λ _, 1⟩ @[simp, to_additive] lemma one_apply [∀ i, has_one $ f i] : (1 : Π i, f i) i = 1 := rfl @[to_additive] instance has_mul [∀ i, has_mul $ f i] : has_mul (Π i : I, f i) := ⟨λ f g i, f i * g i⟩ @[simp, to_additive] lemma mul_apply [∀ i, has_mul $ f i] : (x * y) i = x i * y i := rfl @[to_additive] instance has_inv [∀ i, has_inv $ f i] : has_inv (Π i : I, f i) := ⟨λ f i, (f i)⁻¹⟩ @[simp, to_additive] lemma inv_apply [∀ i, has_inv $ f i] : x⁻¹ i = (x i)⁻¹ := rfl instance has_div [Π i, has_div $ f i] : has_div (Π i : I, f i) := ⟨λ f g i, f i / g i⟩ @[simp] lemma div_apply [Π i, has_div $ f i] : (x / y) i = x i / y i := rfl section variables [decidable_eq I] variables [Π i, has_zero (f i)] /-- The function supported at `i`, with value `x` there. -/ def single (i : I) (x : f i) : Π i, f i := function.update 0 i x @[simp] lemma single_eq_same (i : I) (x : f i) : single i x i = x := function.update_same i x _ @[simp] lemma single_eq_of_ne {i i' : I} (h : i' ≠ i) (x : f i) : single i x i' = 0 := function.update_noteq h x _ variables (f) lemma single_injective (i : I) : function.injective (single i : f i → Π i, f i) := function.update_injective _ i end end pi
515ab84cd60eae9bedc30cb524155d49ce7eadf7
efa51dd2edbbbbd6c34bd0ce436415eb405832e7
/20161026_ICTAC_Tutorial/ex7.lean
7296be284c38392068a1083fc75b060a6c47e2e2
[ "Apache-2.0" ]
permissive
leanprover/presentations
dd031a05bcb12c8855676c77e52ed84246bd889a
3ce2d132d299409f1de269fa8e95afa1333d644e
refs/heads/master
1,688,703,388,796
1,686,838,383,000
1,687,465,742,000
29,750,158
12
9
Apache-2.0
1,540,211,670,000
1,422,042,683,000
Lean
UTF-8
Lean
false
false
98
lean
check list check prod check prod Type nat check Type × nat check Type 4 × nat check list Type
70eb49b764eb37458d53a8d5b7b1ba9b65d68ab2
ea11767c9c6a467c4b7710ec6f371c95cfc023fd
/src/monoidal_categories/lemmas/pentagon_in_terms_of_natural_transformations.lean
6a0cd4bea9621baca5054bf99cdd1ab99ba7ef6b
[]
no_license
RitaAhmadi/lean-monoidal-categories
68a23f513e902038e44681336b87f659bbc281e0
81f43e1e0d623a96695aa8938951d7422d6d7ba6
refs/heads/master
1,651,458,686,519
1,529,824,613,000
1,529,824,613,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,227
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Stephen Morgan, Scott Morrison import .pentagon_in_terms_of_natural_transformations_definitions import tidy.its open categories open categories.functor open categories.products open categories.natural_transformation namespace categories.monoidal_category universe variables u v variables (C : Type u) [𝒞 : monoidal_category.{u v} C] include 𝒞 local attribute [tidy] dsimp_all' set_option trace.check true -- TODO tidy this up lemma pentagon_in_terms_of_natural_transformations : pentagon_3step C = pentagon_2step C := begin dsimp', apply NaturalTransformations_componentwise_equal, intros WXYZ, induction WXYZ with WXY Z, induction WXY with WX Y, induction WX with W X, { tidy, -- erw rewrite_tensor_as_otimes, -- FIXME terrifying: equalities between objects are evil, and hence rewriting along them is hard have p := monoidal_category.pentagon C W X Y Z, -- have p := monoidal_category.pentagon C X_fst_fst_fst X_fst_fst_snd X_fst_snd X_snd, obviously, -- FIXME }, end end categories.monoidal_category
23e90e3736bb8b4453d227d14381494fe2a41e23
b3fced0f3ff82d577384fe81653e47df68bb2fa1
/src/category/monad/writer.lean
69019b832402b19efbc0855511a0f6ba1839062f
[ "Apache-2.0" ]
permissive
ratmice/mathlib
93b251ef5df08b6fd55074650ff47fdcc41a4c75
3a948a6a4cd5968d60e15ed914b1ad2f4423af8d
refs/heads/master
1,599,240,104,318
1,572,981,183,000
1,572,981,183,000
219,830,178
0
0
Apache-2.0
1,572,980,897,000
1,572,980,896,000
null
UTF-8
Lean
false
false
7,027
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon The writer monad transformer for passing immutable state. -/ import tactic.basic category.monad.basic universes u v w structure writer_t (ω : Type u) (m : Type u → Type v) (α : Type u) : Type (max u v) := (run : m (α × ω)) @[reducible] def writer (ω : Type u) := writer_t ω id attribute [pp_using_anonymous_constructor] writer_t namespace writer_t section variable {ω : Type u} variable {m : Type u → Type v} variable [monad m] variables {α β : Type u} open function @[extensionality] protected lemma ext (x x' : writer_t ω m α) (h : x.run = x'.run) : x = x' := by cases x; cases x'; congr; apply h @[inline] protected def tell (w : ω) : writer_t ω m punit := ⟨pure (punit.star, w)⟩ @[inline] protected def listen : writer_t ω m α → writer_t ω m (α × ω) | ⟨ cmd ⟩ := ⟨ (λ x : α × ω, ((x.1,x.2),x.2)) <$> cmd ⟩ @[inline] protected def pass : writer_t ω m (α × (ω → ω)) → writer_t ω m α | ⟨ cmd ⟩ := ⟨ uncurry (uncurry $ λ x (f : ω → ω) w, (x,f w)) <$> cmd ⟩ @[inline] protected def pure [has_one ω] (a : α) : writer_t ω m α := ⟨ pure (a,1) ⟩ @[inline] protected def bind [has_mul ω] (x : writer_t ω m α) (f : α → writer_t ω m β) : writer_t ω m β := ⟨ do x ← x.run, x' ← (f x.1).run, pure (x'.1,x.2 * x'.2) ⟩ instance [has_one ω] [has_mul ω] : monad (writer_t ω m) := { pure := λ α, writer_t.pure, bind := λ α β, writer_t.bind } instance [monoid ω] [is_lawful_monad m] : is_lawful_monad (writer_t ω m) := { id_map := by { intros, cases x, simp [(<$>),writer_t.bind,writer_t.pure] }, pure_bind := by { intros, simp [has_pure.pure,writer_t.pure,(>>=),writer_t.bind], ext; refl }, bind_assoc := by { intros, simp [(>>=),writer_t.bind,mul_assoc] with functor_norm } } @[inline] protected def lift [has_one ω] (a : m α) : writer_t ω m α := ⟨ flip prod.mk 1 <$> a ⟩ instance (m) [monad m] [has_one ω] : has_monad_lift m (writer_t ω m) := ⟨ λ α, writer_t.lift ⟩ @[inline] protected def monad_map {m m'} [monad m] [monad m'] {α} (f : Π {α}, m α → m' α) : writer_t ω m α → writer_t ω m' α := λ x, ⟨ f x.run ⟩ instance (m m') [monad m] [monad m'] : monad_functor m m' (writer_t ω m) (writer_t ω m') := ⟨@writer_t.monad_map ω m m' _ _⟩ @[inline] protected def adapt {ω' : Type u} {α : Type u} (f : ω → ω') : writer_t ω m α → writer_t ω' m α := λ x, ⟨prod.map id f <$> x.run⟩ instance (ε) [has_one ω] [monad m] [monad_except ε m] : monad_except ε (writer_t ω m) := { throw := λ α, writer_t.lift ∘ throw, catch := λ α x c, ⟨catch x.run (λ e, (c e).run)⟩ } end end writer_t /-- An implementation of [MonadReader](https://hackage.haskell.org/package/mtl-2.2.2/docs/Control-Monad-Reader-Class.html#t:MonadReader). It does not contain `local` because this function cannot be lifted using `monad_lift`. Instead, the `monad_reader_adapter` class provides the more general `adapt_reader` function. Note: This class can be seen as a simplification of the more "principled" definition ``` class monad_reader (ρ : out_param (Type u)) (n : Type u → Type u) := (lift {} {α : Type u} : (∀ {m : Type u → Type u} [monad m], reader_t ρ m α) → n α) ``` -/ class monad_writer (ω : out_param (Type u)) (m : Type u → Type v) := (tell {} (w : ω) : m punit) (listen {α} : m α → m (α × ω)) (pass {α : Type u} : m (α × (ω → ω)) → m α) export monad_writer instance {ω : Type u} {m : Type u → Type v} [monad m] : monad_writer ω (writer_t ω m) := { tell := writer_t.tell, listen := λ α, writer_t.listen, pass := λ α, writer_t.pass } instance {ω ρ : Type u} {m : Type u → Type v} [monad m] [monad_writer ω m] : monad_writer ω (reader_t ρ m) := { tell := λ x, monad_lift (tell x : m punit), listen := λ α ⟨ cmd ⟩, ⟨ λ r, listen (cmd r) ⟩, pass := λ α ⟨ cmd ⟩, ⟨ λ r, pass (cmd r) ⟩ } def swap_right {α β γ} : (α × β) × γ → (α × γ) × β | ⟨⟨x,y⟩,z⟩ := ((x,z),y) instance {ω σ : Type u} {m : Type u → Type v} [monad m] [monad_writer ω m] : monad_writer ω (state_t σ m) := { tell := λ x, monad_lift (tell x : m punit), listen := λ α ⟨ cmd ⟩, ⟨ λ r, swap_right <$> listen (cmd r) ⟩, pass := λ α ⟨ cmd ⟩, ⟨ λ r, pass (swap_right <$> cmd r) ⟩ } open function def except_t.pass_aux {ε α ω} : except ε (α × (ω → ω)) → except ε α × (ω → ω) | (except.error a) := (except.error a,id) | (except.ok (x,y)) := (except.ok x,y) instance {ω ε : Type u} {m : Type u → Type v} [monad m] [monad_writer ω m] : monad_writer ω (except_t ε m) := { tell := λ x, monad_lift (tell x : m punit), listen := λ α ⟨ cmd ⟩, ⟨ uncurry (λ x y, flip prod.mk y <$> x) <$> listen cmd ⟩, pass := λ α ⟨ cmd ⟩, ⟨ pass (except_t.pass_aux <$> cmd) ⟩ } def option_t.pass_aux {α ω} : option (α × (ω → ω)) → option α × (ω → ω) | none := (none ,id) | (some (x,y)) := (some x,y) instance {ω : Type u} {m : Type u → Type v} [monad m] [monad_writer ω m] : monad_writer ω (option_t m) := { tell := λ x, monad_lift (tell x : m punit), listen := λ α ⟨ cmd ⟩, ⟨ uncurry (λ x y, flip prod.mk y <$> x) <$> listen cmd ⟩, pass := λ α ⟨ cmd ⟩, ⟨ pass (option_t.pass_aux <$> cmd) ⟩ } /-- Adapt a monad stack, changing the type of its top-most environment. This class is comparable to [Control.Lens.Magnify](https://hackage.haskell.org/package/lens-4.15.4/docs/Control-Lens-Zoom.html#t:Magnify), but does not use lenses (why would it), and is derived automatically for any transformer implementing `monad_functor`. Note: This class can be seen as a simplification of the more "principled" definition ``` class monad_reader_functor (ρ ρ' : out_param (Type u)) (n n' : Type u → Type u) := (map {} {α : Type u} : (∀ {m : Type u → Type u} [monad m], reader_t ρ m α → reader_t ρ' m α) → n α → n' α) ``` -/ class monad_writer_adapter (ω ω' : out_param (Type u)) (m m' : Type u → Type v) := (adapt_writer {} {α : Type u} : (ω → ω') → m α → m' α) export monad_writer_adapter (adapt_writer) section variables {ω ω' : Type u} {m m' : Type u → Type v} instance monad_writer_adapter_trans {n n' : Type u → Type v} [monad_functor m m' n n'] [monad_writer_adapter ω ω' m m'] : monad_writer_adapter ω ω' n n' := ⟨λ α f, monad_map (λ α, (adapt_writer f : m α → m' α))⟩ instance [monad m] : monad_writer_adapter ω ω' (writer_t ω m) (writer_t ω' m) := ⟨λ α, writer_t.adapt⟩ end instance (ω : Type u) (m out) [monad_run out m] : monad_run (λ α, out (α × ω)) (writer_t ω m) := ⟨λ α x, run $ x.run ⟩
7e377a88ea546dee07af32993d5d9823fc7c1d63
cbcb0199842f03e7606d4e43666573fc15dd07a5
/src/topology/algebra/monoid.lean
e3b6d5f8e6126efbfbbedea033ab0af97258a780
[ "Apache-2.0" ]
permissive
truonghoangle/mathlib
a6a7c14b3767ec71156239d8ea97f6921fe79627
673bae584febcd830c2c9256eb7e7a81e27ed303
refs/heads/master
1,590,347,998,944
1,559,728,860,000
1,559,728,860,000
187,431,971
0
0
null
1,558,238,525,000
1,558,238,525,000
null
UTF-8
Lean
false
false
5,171
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro Theory of topological monoids. TODO: generalize `topological_monoid` and `topological_add_monoid` to semigroups, or add a type class `topological_operator α (*)`. -/ import topology.constructions import algebra.pi_instances open classical set lattice filter topological_space local attribute [instance] classical.prop_decidable universes u v w variables {α : Type u} {β : Type v} {γ : Type w} section topological_monoid /-- A topological monoid is a monoid in which the multiplication is continuous as a function `α × α → α`. -/ class topological_monoid (α : Type u) [topological_space α] [monoid α] : Prop := (continuous_mul : continuous (λp:α×α, p.1 * p.2)) /-- A topological (additive) monoid is a monoid in which the addition is continuous as a function `α × α → α`. -/ class topological_add_monoid (α : Type u) [topological_space α] [add_monoid α] : Prop := (continuous_add : continuous (λp:α×α, p.1 + p.2)) attribute [to_additive topological_add_monoid] topological_monoid attribute [to_additive topological_add_monoid.mk] topological_monoid.mk attribute [to_additive topological_add_monoid.continuous_add] topological_monoid.continuous_mul section variables [topological_space α] [monoid α] [topological_monoid α] @[to_additive continuous_add'] lemma continuous_mul' : continuous (λp:α×α, p.1 * p.2) := topological_monoid.continuous_mul α @[to_additive continuous_add] lemma continuous_mul [topological_space β] {f : β → α} {g : β → α} (hf : continuous f) (hg : continuous g) : continuous (λx, f x * g x) := continuous_mul'.comp (hf.prod_mk hg) -- @[to_additive continuous_smul] lemma continuous_pow : ∀ n : ℕ, continuous (λ a : α, a ^ n) | 0 := by simpa using continuous_const | (k+1) := show continuous (λ (a : α), a * a ^ k), from continuous_mul continuous_id (continuous_pow _) @[to_additive tendsto_add'] lemma tendsto_mul' {a b : α} : tendsto (λp:α×α, p.fst * p.snd) (nhds (a, b)) (nhds (a * b)) := continuous_iff_continuous_at.mp (topological_monoid.continuous_mul α) (a, b) @[to_additive tendsto_add] lemma tendsto_mul {f : β → α} {g : β → α} {x : filter β} {a b : α} (hf : tendsto f x (nhds a)) (hg : tendsto g x (nhds b)) : tendsto (λx, f x * g x) x (nhds (a * b)) := (hf.prod_mk hg).comp (by rw [←nhds_prod_eq]; exact tendsto_mul') @[to_additive tendsto_list_sum] lemma tendsto_list_prod {f : γ → β → α} {x : filter β} {a : γ → α} : ∀l:list γ, (∀c∈l, tendsto (f c) x (nhds (a c))) → tendsto (λb, (l.map (λc, f c b)).prod) x (nhds ((l.map a).prod)) | [] _ := by simp [tendsto_const_nhds] | (f :: l) h := begin simp, exact tendsto_mul (h f (list.mem_cons_self _ _)) (tendsto_list_prod l (assume c hc, h c (list.mem_cons_of_mem _ hc))) end @[to_additive continuous_list_sum] lemma continuous_list_prod [topological_space β] {f : γ → β → α} (l : list γ) (h : ∀c∈l, continuous (f c)) : continuous (λa, (l.map (λc, f c a)).prod) := continuous_iff_continuous_at.2 $ assume x, tendsto_list_prod l $ assume c hc, continuous_iff_continuous_at.1 (h c hc) x @[to_additive prod.topological_add_monoid] instance [topological_space β] [monoid β] [topological_monoid β] : topological_monoid (α × β) := ⟨continuous.prod_mk (continuous_mul (continuous_fst.comp continuous_fst) (continuous_fst.comp continuous_snd)) (continuous_mul (continuous_snd.comp continuous_fst) (continuous_snd.comp continuous_snd)) ⟩ attribute [instance] prod.topological_add_monoid end section variables [topological_space α] [comm_monoid α] [topological_monoid α] @[to_additive tendsto_multiset_sum] lemma tendsto_multiset_prod {f : γ → β → α} {x : filter β} {a : γ → α} (s : multiset γ) : (∀c∈s, tendsto (f c) x (nhds (a c))) → tendsto (λb, (s.map (λc, f c b)).prod) x (nhds ((s.map a).prod)) := by { rcases s with ⟨l⟩, simp, exact tendsto_list_prod l } @[to_additive tendsto_finset_sum] lemma tendsto_finset_prod {f : γ → β → α} {x : filter β} {a : γ → α} (s : finset γ) : (∀c∈s, tendsto (f c) x (nhds (a c))) → tendsto (λb, s.prod (λc, f c b)) x (nhds (s.prod a)) := tendsto_multiset_prod _ @[to_additive continuous_multiset_sum] lemma continuous_multiset_prod [topological_space β] {f : γ → β → α} (s : multiset γ) : (∀c∈s, continuous (f c)) → continuous (λa, (s.map (λc, f c a)).prod) := by { rcases s with ⟨l⟩, simp, exact continuous_list_prod l } @[to_additive continuous_finset_sum] lemma continuous_finset_prod [topological_space β] {f : γ → β → α} (s : finset γ) : (∀c∈s, continuous (f c)) → continuous (λa, s.prod (λc, f c a)) := continuous_multiset_prod _ @[to_additive is_add_submonoid.mem_nhds_zero] lemma is_submonoid.mem_nhds_one (β : set α) [is_submonoid β] (oβ : is_open β) : β ∈ nhds (1 : α) := mem_nhds_sets_iff.2 ⟨β, (by refl), oβ, is_submonoid.one_mem _⟩ end end topological_monoid
e7b6e54b32e94843dde73a8610e64435b95b910b
4727251e0cd73359b15b664c3170e5d754078599
/src/data/finset/lattice.lean
1f9561d3225195ee8b8a57aeec6f62032ff28f47
[ "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
57,273
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 data.finset.fold import data.finset.option import data.finset.prod import data.multiset.lattice import order.complete_lattice /-! # Lattice operations on finsets -/ variables {α β γ ι : Type*} namespace finset open multiset order_dual /-! ### sup -/ section sup -- TODO: define with just `[has_bot α]` where some lemmas hold without requiring `[order_bot α]` variables [semilattice_sup α] [order_bot α] /-- Supremum of a finite set: `sup {a, b, c} f = f a ⊔ f b ⊔ f c` -/ def sup (s : finset β) (f : β → α) : α := s.fold (⊔) ⊥ f variables {s s₁ s₂ : finset β} {f g : β → α} lemma sup_def : s.sup f = (s.1.map f).sup := rfl @[simp] lemma sup_empty : (∅ : finset β).sup f = ⊥ := fold_empty @[simp] lemma sup_cons {b : β} (h : b ∉ s) : (cons b s h).sup f = f b ⊔ s.sup f := fold_cons h @[simp] lemma sup_insert [decidable_eq β] {b : β} : (insert b s : finset β).sup f = f b ⊔ s.sup f := fold_insert_idem lemma sup_image [decidable_eq β] (s : finset γ) (f : γ → β) (g : β → α) : (s.image f).sup g = s.sup (g ∘ f) := fold_image_idem @[simp] lemma sup_map (s : finset γ) (f : γ ↪ β) (g : β → α) : (s.map f).sup g = s.sup (g ∘ f) := fold_map @[simp] lemma sup_singleton {b : β} : ({b} : finset β).sup f = f b := sup_singleton lemma sup_union [decidable_eq β] : (s₁ ∪ s₂).sup f = s₁.sup f ⊔ s₂.sup f := finset.induction_on s₁ (by rw [empty_union, sup_empty, bot_sup_eq]) $ λ a s has ih, by rw [insert_union, sup_insert, sup_insert, ih, sup_assoc] lemma sup_sup : s.sup (f ⊔ g) = s.sup f ⊔ s.sup g := begin refine finset.cons_induction_on s _ (λ b t _ h, _), { rw [sup_empty, sup_empty, sup_empty, bot_sup_eq] }, { rw [sup_cons, sup_cons, sup_cons, h], exact sup_sup_sup_comm _ _ _ _ } end theorem sup_congr {f g : β → α} (hs : s₁ = s₂) (hfg : ∀a∈s₂, f a = g a) : s₁.sup f = s₂.sup g := by subst hs; exact finset.fold_congr hfg @[simp] lemma sup_le_iff {a : α} : s.sup f ≤ a ↔ (∀b ∈ s, f b ≤ a) := begin apply iff.trans multiset.sup_le, simp only [multiset.mem_map, and_imp, exists_imp_distrib], exact ⟨λ k b hb, k _ _ hb rfl, λ k a' b hb h, h ▸ k _ hb⟩, end @[simp] lemma sup_bUnion [decidable_eq β] (s : finset γ) (t : γ → finset β) : (s.bUnion t).sup f = s.sup (λ x, (t x).sup f) := eq_of_forall_ge_iff $ λ c, by simp [@forall_swap _ β] lemma sup_const {s : finset β} (h : s.nonempty) (c : α) : s.sup (λ _, c) = c := eq_of_forall_ge_iff $ λ b, sup_le_iff.trans h.forall_const @[simp] lemma sup_bot (s : finset β) : s.sup (λ _, ⊥) = (⊥ : α) := begin obtain rfl | hs := s.eq_empty_or_nonempty, { exact sup_empty }, { exact sup_const hs _ } end lemma sup_ite (p : β → Prop) [decidable_pred p] : s.sup (λ i, ite (p i) (f i) (g i)) = (s.filter p).sup f ⊔ (s.filter (λ i, ¬ p i)).sup g := fold_ite _ lemma sup_le {a : α} : (∀b ∈ s, f b ≤ a) → s.sup f ≤ a := sup_le_iff.2 lemma le_sup {b : β} (hb : b ∈ s) : f b ≤ s.sup f := sup_le_iff.1 le_rfl _ hb lemma sup_mono_fun {g : β → α} (h : ∀b∈s, f b ≤ g b) : s.sup f ≤ s.sup g := sup_le (λ b hb, le_trans (h b hb) (le_sup hb)) lemma sup_mono (h : s₁ ⊆ s₂) : s₁.sup f ≤ s₂.sup f := sup_le $ assume b hb, le_sup (h hb) lemma sup_comm (s : finset β) (t : finset γ) (f : β → γ → α) : s.sup (λ b, t.sup (f b)) = t.sup (λ c, s.sup (λ b, f b c)) := begin refine eq_of_forall_ge_iff (λ a, _), simp_rw sup_le_iff, exact ⟨λ h c hc b hb, h b hb c hc, λ h b hb c hc, h c hc b hb⟩, end @[simp] lemma sup_attach (s : finset β) (f : β → α) : s.attach.sup (λ x, f x) = s.sup f := (s.attach.sup_map (function.embedding.subtype _) f).symm.trans $ congr_arg _ attach_map_val /-- See also `finset.product_bUnion`. -/ lemma sup_product_left (s : finset β) (t : finset γ) (f : β × γ → α) : (s.product t).sup f = s.sup (λ i, t.sup $ λ i', f ⟨i, i'⟩) := begin refine le_antisymm _ (sup_le (λ i hi, sup_le $ λ i' hi', le_sup $ mem_product.2 ⟨hi, hi'⟩)), refine sup_le _, rintro ⟨i, i'⟩ hi, rw mem_product at hi, refine le_trans _ (le_sup hi.1), convert le_sup hi.2, end lemma sup_product_right (s : finset β) (t : finset γ) (f : β × γ → α) : (s.product t).sup f = t.sup (λ i', s.sup $ λ i, f ⟨i, i'⟩) := by rw [sup_product_left, sup_comm] @[simp] lemma sup_erase_bot [decidable_eq α] (s : finset α) : (s.erase ⊥).sup id = s.sup id := begin refine (sup_mono (s.erase_subset _)).antisymm (finset.sup_le_iff.2 $ λ a ha, _), obtain rfl | ha' := eq_or_ne a ⊥, { exact bot_le }, { exact le_sup (mem_erase.2 ⟨ha', ha⟩) } end lemma sup_sdiff_right {α β : Type*} [generalized_boolean_algebra α] (s : finset β) (f : β → α) (a : α) : s.sup (λ b, f b \ a) = s.sup f \ a := begin refine finset.cons_induction_on s _ (λ b t _ h, _), { rw [sup_empty, sup_empty, bot_sdiff] }, { rw [sup_cons, sup_cons, h, sup_sdiff] } end lemma comp_sup_eq_sup_comp [semilattice_sup γ] [order_bot γ] {s : finset β} {f : β → α} (g : α → γ) (g_sup : ∀ x y, g (x ⊔ y) = g x ⊔ g y) (bot : g ⊥ = ⊥) : g (s.sup f) = s.sup (g ∘ f) := finset.cons_induction_on s bot (λ c t hc ih, by rw [sup_cons, sup_cons, g_sup, ih]) /-- Computing `sup` in a subtype (closed under `sup`) is the same as computing it in `α`. -/ lemma sup_coe {P : α → Prop} {Pbot : P ⊥} {Psup : ∀{{x y}}, P x → P y → P (x ⊔ y)} (t : finset β) (f : β → {x : α // P x}) : (@sup _ _ (subtype.semilattice_sup Psup) (subtype.order_bot Pbot) t f : α) = t.sup (λ x, f x) := by { rw [comp_sup_eq_sup_comp coe]; intros; refl } @[simp] lemma sup_to_finset {α β} [decidable_eq β] (s : finset α) (f : α → multiset β) : (s.sup f).to_finset = s.sup (λ x, (f x).to_finset) := comp_sup_eq_sup_comp multiset.to_finset to_finset_union rfl theorem subset_range_sup_succ (s : finset ℕ) : s ⊆ range (s.sup id).succ := λ n hn, mem_range.2 $ nat.lt_succ_of_le $ le_sup hn theorem exists_nat_subset_range (s : finset ℕ) : ∃n : ℕ, s ⊆ range n := ⟨_, s.subset_range_sup_succ⟩ lemma sup_induction {p : α → Prop} (hb : p ⊥) (hp : ∀ a₁, p a₁ → ∀ a₂, p a₂ → p (a₁ ⊔ a₂)) (hs : ∀ b ∈ s, p (f b)) : p (s.sup f) := begin induction s using finset.cons_induction with c s hc ih, { exact hb, }, { rw sup_cons, apply hp, { exact hs c (mem_cons.2 (or.inl rfl)), }, { exact ih (λ b h, hs b (mem_cons.2 (or.inr h))), }, }, end lemma sup_le_of_le_directed {α : Type*} [semilattice_sup α] [order_bot α] (s : set α) (hs : s.nonempty) (hdir : directed_on (≤) s) (t : finset α) : (∀ x ∈ t, ∃ y ∈ s, x ≤ y) → ∃ x, x ∈ s ∧ t.sup id ≤ x := begin classical, apply finset.induction_on t, { simpa only [forall_prop_of_true, and_true, forall_prop_of_false, bot_le, not_false_iff, sup_empty, forall_true_iff, not_mem_empty], }, { intros a r har ih h, have incs : ↑r ⊆ ↑(insert a r), by { rw finset.coe_subset, apply finset.subset_insert, }, -- x ∈ s is above the sup of r obtain ⟨x, ⟨hxs, hsx_sup⟩⟩ := ih (λ x hx, h x $ incs hx), -- y ∈ s is above a obtain ⟨y, hys, hay⟩ := h a (finset.mem_insert_self a r), -- z ∈ s is above x and y obtain ⟨z, hzs, ⟨hxz, hyz⟩⟩ := hdir x hxs y hys, use [z, hzs], rw [sup_insert, id.def, _root_.sup_le_iff], exact ⟨le_trans hay hyz, le_trans hsx_sup hxz⟩, }, end -- If we acquire sublattices -- the hypotheses should be reformulated as `s : subsemilattice_sup_bot` lemma sup_mem (s : set α) (w₁ : ⊥ ∈ s) (w₂ : ∀ x y ∈ s, x ⊔ y ∈ s) {ι : Type*} (t : finset ι) (p : ι → α) (h : ∀ i ∈ t, p i ∈ s) : t.sup p ∈ s := @sup_induction _ _ _ _ _ _ (∈ s) w₁ w₂ h @[simp] lemma sup_eq_bot_iff (f : β → α) (S : finset β) : S.sup f = ⊥ ↔ ∀ s ∈ S, f s = ⊥ := begin classical, induction S using finset.induction with a S haS hi; simp [*], end end sup lemma sup_eq_supr [complete_lattice β] (s : finset α) (f : α → β) : s.sup f = (⨆a∈s, f a) := le_antisymm (finset.sup_le $ assume a ha, le_supr_of_le a $ le_supr _ ha) (supr_le $ assume a, supr_le $ assume ha, le_sup ha) lemma sup_id_eq_Sup [complete_lattice α] (s : finset α) : s.sup id = Sup s := by simp [Sup_eq_supr, sup_eq_supr] lemma sup_id_set_eq_sUnion (s : finset (set α)) : s.sup id = ⋃₀(↑s) := sup_id_eq_Sup _ @[simp] lemma sup_set_eq_bUnion (s : finset α) (f : α → set β) : s.sup f = ⋃ x ∈ s, f x := sup_eq_supr _ _ lemma sup_eq_Sup_image [complete_lattice β] (s : finset α) (f : α → β) : s.sup f = Sup (f '' s) := begin classical, rw [←finset.coe_image, ←sup_id_eq_Sup, sup_image, function.comp.left_id], end /-! ### inf -/ section inf -- TODO: define with just `[has_top α]` where some lemmas hold without requiring `[order_top α]` variables [semilattice_inf α] [order_top α] /-- Infimum of a finite set: `inf {a, b, c} f = f a ⊓ f b ⊓ f c` -/ def inf (s : finset β) (f : β → α) : α := s.fold (⊓) ⊤ f variables {s s₁ s₂ : finset β} {f g : β → α} lemma inf_def : s.inf f = (s.1.map f).inf := rfl @[simp] lemma inf_empty : (∅ : finset β).inf f = ⊤ := fold_empty @[simp] lemma inf_cons {b : β} (h : b ∉ s) : (cons b s h).inf f = f b ⊓ s.inf f := @sup_cons αᵒᵈ _ _ _ _ _ _ h @[simp] lemma inf_insert [decidable_eq β] {b : β} : (insert b s : finset β).inf f = f b ⊓ s.inf f := fold_insert_idem lemma inf_image [decidable_eq β] (s : finset γ) (f : γ → β) (g : β → α) : (s.image f).inf g = s.inf (g ∘ f) := fold_image_idem @[simp] lemma inf_map (s : finset γ) (f : γ ↪ β) (g : β → α) : (s.map f).inf g = s.inf (g ∘ f) := fold_map @[simp] lemma inf_singleton {b : β} : ({b} : finset β).inf f = f b := inf_singleton lemma inf_union [decidable_eq β] : (s₁ ∪ s₂).inf f = s₁.inf f ⊓ s₂.inf f := @sup_union αᵒᵈ _ _ _ _ _ _ _ lemma inf_inf : s.inf (f ⊓ g) = s.inf f ⊓ s.inf g := @sup_sup αᵒᵈ _ _ _ _ _ _ theorem inf_congr {f g : β → α} (hs : s₁ = s₂) (hfg : ∀a∈s₂, f a = g a) : s₁.inf f = s₂.inf g := by subst hs; exact finset.fold_congr hfg @[simp] lemma inf_bUnion [decidable_eq β] (s : finset γ) (t : γ → finset β) : (s.bUnion t).inf f = s.inf (λ x, (t x).inf f) := @sup_bUnion αᵒᵈ _ _ _ _ _ _ _ _ lemma inf_const {s : finset β} (h : s.nonempty) (c : α) : s.inf (λ _, c) = c := @sup_const αᵒᵈ _ _ _ _ h _ @[simp] lemma inf_top (s : finset β) : s.inf (λ _, ⊤) = (⊤ : α) := @sup_bot αᵒᵈ _ _ _ _ lemma le_inf_iff {a : α} : a ≤ s.inf f ↔ ∀ b ∈ s, a ≤ f b := @sup_le_iff αᵒᵈ _ _ _ _ _ _ lemma inf_le {b : β} (hb : b ∈ s) : s.inf f ≤ f b := le_inf_iff.1 le_rfl _ hb lemma le_inf {a : α} : (∀b ∈ s, a ≤ f b) → a ≤ s.inf f := le_inf_iff.2 lemma inf_mono_fun {g : β → α} (h : ∀b∈s, f b ≤ g b) : s.inf f ≤ s.inf g := le_inf (λ b hb, le_trans (inf_le hb) (h b hb)) lemma inf_mono (h : s₁ ⊆ s₂) : s₂.inf f ≤ s₁.inf f := le_inf $ assume b hb, inf_le (h hb) lemma inf_attach (s : finset β) (f : β → α) : s.attach.inf (λ x, f x) = s.inf f := @sup_attach αᵒᵈ _ _ _ _ _ lemma inf_comm (s : finset β) (t : finset γ) (f : β → γ → α) : s.inf (λ b, t.inf (f b)) = t.inf (λ c, s.inf (λ b, f b c)) := @sup_comm αᵒᵈ _ _ _ _ _ _ _ lemma inf_product_left (s : finset β) (t : finset γ) (f : β × γ → α) : (s.product t).inf f = s.inf (λ i, t.inf $ λ i', f ⟨i, i'⟩) := @sup_product_left αᵒᵈ _ _ _ _ _ _ _ lemma inf_product_right (s : finset β) (t : finset γ) (f : β × γ → α) : (s.product t).inf f = t.inf (λ i', s.inf $ λ i, f ⟨i, i'⟩) := @sup_product_right αᵒᵈ _ _ _ _ _ _ _ @[simp] lemma inf_erase_top [decidable_eq α] (s : finset α) : (s.erase ⊤).inf id = s.inf id := @sup_erase_bot αᵒᵈ _ _ _ _ lemma sup_sdiff_left {α β : Type*} [boolean_algebra α] (s : finset β) (f : β → α) (a : α) : s.sup (λ b, a \ f b) = a \ s.inf f := begin refine finset.cons_induction_on s _ (λ b t _ h, _), { rw [sup_empty, inf_empty, sdiff_top] }, { rw [sup_cons, inf_cons, h, sdiff_inf] } end lemma inf_sdiff_left {α β : Type*} [boolean_algebra α] {s : finset β} (hs : s.nonempty) (f : β → α) (a : α) : s.inf (λ b, a \ f b) = a \ s.sup f := begin induction hs using finset.nonempty.cons_induction with b b t _ _ h, { rw [sup_singleton, inf_singleton] }, { rw [sup_cons, inf_cons, h, sdiff_sup] } end lemma inf_sdiff_right {α β : Type*} [boolean_algebra α] {s : finset β} (hs : s.nonempty) (f : β → α) (a : α) : s.inf (λ b, f b \ a) = s.inf f \ a := begin induction hs using finset.nonempty.cons_induction with b b t _ _ h, { rw [inf_singleton, inf_singleton] }, { rw [inf_cons, inf_cons, h, inf_sdiff] } end lemma comp_inf_eq_inf_comp [semilattice_inf γ] [order_top γ] {s : finset β} {f : β → α} (g : α → γ) (g_inf : ∀ x y, g (x ⊓ y) = g x ⊓ g y) (top : g ⊤ = ⊤) : g (s.inf f) = s.inf (g ∘ f) := @comp_sup_eq_sup_comp αᵒᵈ _ γᵒᵈ _ _ _ _ _ _ _ g_inf top /-- Computing `inf` in a subtype (closed under `inf`) is the same as computing it in `α`. -/ lemma inf_coe {P : α → Prop} {Ptop : P ⊤} {Pinf : ∀{{x y}}, P x → P y → P (x ⊓ y)} (t : finset β) (f : β → {x : α // P x}) : (@inf _ _ (subtype.semilattice_inf Pinf) (subtype.order_top Ptop) t f : α) = t.inf (λ x, f x) := @sup_coe αᵒᵈ _ _ _ _ Ptop Pinf t f lemma inf_induction {p : α → Prop} (ht : p ⊤) (hp : ∀ a₁, p a₁ → ∀ a₂, p a₂ → p (a₁ ⊓ a₂)) (hs : ∀ b ∈ s, p (f b)) : p (s.inf f) := @sup_induction αᵒᵈ _ _ _ _ _ _ ht hp hs lemma inf_mem (s : set α) (w₁ : ⊤ ∈ s) (w₂ : ∀ x y ∈ s, x ⊓ y ∈ s) {ι : Type*} (t : finset ι) (p : ι → α) (h : ∀ i ∈ t, p i ∈ s) : t.inf p ∈ s := @inf_induction _ _ _ _ _ _ (∈ s) w₁ w₂ h @[simp] lemma inf_eq_top_iff (f : β → α) (S : finset β) : S.inf f = ⊤ ↔ ∀ s ∈ S, f s = ⊤ := @finset.sup_eq_bot_iff αᵒᵈ _ _ _ _ _ end inf @[simp] lemma to_dual_sup [semilattice_sup α] [order_bot α] (s : finset β) (f : β → α) : to_dual (s.sup f) = s.inf (to_dual ∘ f) := rfl @[simp] lemma to_dual_inf [semilattice_inf α] [order_top α] (s : finset β) (f : β → α) : to_dual (s.inf f) = s.sup (to_dual ∘ f) := rfl @[simp] lemma of_dual_sup [semilattice_inf α] [order_top α] (s : finset β) (f : β → αᵒᵈ) : of_dual (s.sup f) = s.inf (of_dual ∘ f) := rfl @[simp] lemma of_dual_inf [semilattice_sup α] [order_bot α] (s : finset β) (f : β → αᵒᵈ) : of_dual (s.inf f) = s.sup (of_dual ∘ f) := rfl section distrib_lattice variables [distrib_lattice α] section order_bot variables [order_bot α] {s : finset β} {f : β → α} {a : α} lemma sup_inf_distrib_left (s : finset ι) (f : ι → α) (a : α) : a ⊓ s.sup f = s.sup (λ i, a ⊓ f i) := begin induction s using finset.cons_induction with i s hi h, { simp_rw [finset.sup_empty, inf_bot_eq] }, { rw [sup_cons, sup_cons, inf_sup_left, h] } end lemma sup_inf_distrib_right (s : finset ι) (f : ι → α) (a : α) : s.sup f ⊓ a = s.sup (λ i, f i ⊓ a) := by { rw [_root_.inf_comm, s.sup_inf_distrib_left], simp_rw _root_.inf_comm } lemma disjoint_sup_right : disjoint a (s.sup f) ↔ ∀ i ∈ s, disjoint a (f i) := by simp only [disjoint_iff, sup_inf_distrib_left, sup_eq_bot_iff] lemma disjoint_sup_left : disjoint (s.sup f) a ↔ ∀ i ∈ s, disjoint (f i) a := by simp only [disjoint_iff, sup_inf_distrib_right, sup_eq_bot_iff] end order_bot section order_top variables [order_top α] lemma inf_sup_distrib_left (s : finset ι) (f : ι → α) (a : α) : a ⊔ s.inf f = s.inf (λ i, a ⊔ f i) := @sup_inf_distrib_left αᵒᵈ _ _ _ _ _ _ lemma inf_sup_distrib_right (s : finset ι) (f : ι → α) (a : α) : s.inf f ⊔ a = s.inf (λ i, f i ⊔ a) := @sup_inf_distrib_right αᵒᵈ _ _ _ _ _ _ end order_top end distrib_lattice section linear_order variables [linear_order α] section order_bot variables [order_bot α] {s : finset ι} {f : ι → α} {a : α} lemma comp_sup_eq_sup_comp_of_is_total [semilattice_sup β] [order_bot β] (g : α → β) (mono_g : monotone g) (bot : g ⊥ = ⊥) : g (s.sup f) = s.sup (g ∘ f) := comp_sup_eq_sup_comp g mono_g.map_sup bot @[simp] protected lemma le_sup_iff (ha : ⊥ < a) : a ≤ s.sup f ↔ ∃ b ∈ s, a ≤ f b := ⟨finset.cons_induction_on s (λ h, absurd h (not_le_of_lt ha)) (λ c t hc ih, by simpa using @or.rec _ _ (∃ b, (b = c ∨ b ∈ t) ∧ a ≤ f b) (λ h, ⟨c, or.inl rfl, h⟩) (λ h, let ⟨b, hb, hle⟩ := ih h in ⟨b, or.inr hb, hle⟩)), (λ ⟨b, hb, hle⟩, trans hle (le_sup hb))⟩ @[simp] protected lemma lt_sup_iff : a < s.sup f ↔ ∃ b ∈ s, a < f b := ⟨finset.cons_induction_on s (λ h, absurd h not_lt_bot) (λ c t hc ih, by simpa using @or.rec _ _ (∃ b, (b = c ∨ b ∈ t) ∧ a < f b) (λ h, ⟨c, or.inl rfl, h⟩) (λ h, let ⟨b, hb, hlt⟩ := ih h in ⟨b, or.inr hb, hlt⟩)), (λ ⟨b, hb, hlt⟩, lt_of_lt_of_le hlt (le_sup hb))⟩ @[simp] protected lemma sup_lt_iff (ha : ⊥ < a) : s.sup f < a ↔ ∀ b ∈ s, f b < a := ⟨(λ hs b hb, lt_of_le_of_lt (le_sup hb) hs), finset.cons_induction_on s (λ _, ha) (λ c t hc, by simpa only [sup_cons, sup_lt_iff, mem_cons, forall_eq_or_imp] using and.imp_right)⟩ end order_bot section order_top variables [order_top α] {s : finset ι} {f : ι → α} {a : α} lemma comp_inf_eq_inf_comp_of_is_total [semilattice_inf β] [order_top β] (g : α → β) (mono_g : monotone g) (top : g ⊤ = ⊤) : g (s.inf f) = s.inf (g ∘ f) := comp_inf_eq_inf_comp g mono_g.map_inf top @[simp] protected lemma inf_le_iff (ha : a < ⊤) : s.inf f ≤ a ↔ ∃ b ∈ s, f b ≤ a := @finset.le_sup_iff αᵒᵈ _ _ _ _ _ _ ha @[simp] protected lemma inf_lt_iff : s.inf f < a ↔ ∃ b ∈ s, f b < a := @finset.lt_sup_iff αᵒᵈ _ _ _ _ _ _ @[simp] protected lemma lt_inf_iff (ha : a < ⊤) : a < s.inf f ↔ ∀ b ∈ s, a < f b := @finset.sup_lt_iff αᵒᵈ _ _ _ _ _ _ ha end order_top end linear_order lemma inf_eq_infi [complete_lattice β] (s : finset α) (f : α → β) : s.inf f = ⨅ a ∈ s, f a := @sup_eq_supr _ βᵒᵈ _ _ _ lemma inf_id_eq_Inf [complete_lattice α] (s : finset α) : s.inf id = Inf s := @sup_id_eq_Sup αᵒᵈ _ _ lemma inf_id_set_eq_sInter (s : finset (set α)) : s.inf id = ⋂₀(↑s) := inf_id_eq_Inf _ @[simp] lemma inf_set_eq_bInter (s : finset α) (f : α → set β) : s.inf f = ⋂ x ∈ s, f x := inf_eq_infi _ _ lemma inf_eq_Inf_image [complete_lattice β] (s : finset α) (f : α → β) : s.inf f = Inf (f '' s) := @sup_eq_Sup_image _ βᵒᵈ _ _ _ section sup' variables [semilattice_sup α] lemma sup_of_mem {s : finset β} (f : β → α) {b : β} (h : b ∈ s) : ∃ (a : α), s.sup (coe ∘ f : β → with_bot α) = ↑a := Exists.imp (λ a, Exists.fst) (@le_sup (with_bot α) _ _ _ _ _ _ h (f b) rfl) /-- Given nonempty finset `s` then `s.sup' H f` is the supremum of its image under `f` in (possibly unbounded) join-semilattice `α`, where `H` is a proof of nonemptiness. If `α` has a bottom element you may instead use `finset.sup` which does not require `s` nonempty. -/ def sup' (s : finset β) (H : s.nonempty) (f : β → α) : α := option.get $ let ⟨b, hb⟩ := H in option.is_some_iff_exists.2 (sup_of_mem f hb) variables {s : finset β} (H : s.nonempty) (f : β → α) @[simp] lemma coe_sup' : ((s.sup' H f : α) : with_bot α) = s.sup (coe ∘ f) := by rw [sup', ←with_bot.some_eq_coe, option.some_get] @[simp] lemma sup'_cons {b : β} {hb : b ∉ s} {h : (cons b s hb).nonempty} : (cons b s hb).sup' h f = f b ⊔ s.sup' H f := by { rw ←with_bot.coe_eq_coe, simp only [coe_sup', sup_cons, with_bot.coe_sup], } @[simp] lemma sup'_insert [decidable_eq β] {b : β} {h : (insert b s).nonempty} : (insert b s).sup' h f = f b ⊔ s.sup' H f := by { rw ←with_bot.coe_eq_coe, simp only [coe_sup', sup_insert, with_bot.coe_sup], } @[simp] lemma sup'_singleton {b : β} {h : ({b} : finset β).nonempty} : ({b} : finset β).sup' h f = f b := rfl lemma sup'_le {a : α} (hs : ∀ b ∈ s, f b ≤ a) : s.sup' H f ≤ a := by { rw [←with_bot.coe_le_coe, coe_sup'], exact sup_le (λ b h, with_bot.coe_le_coe.2 $ hs b h), } lemma le_sup' {b : β} (h : b ∈ s) : f b ≤ s.sup' ⟨b, h⟩ f := by { rw [←with_bot.coe_le_coe, coe_sup'], exact le_sup h, } @[simp] lemma sup'_const (a : α) : s.sup' H (λ b, a) = a := begin apply le_antisymm, { apply sup'_le, intros, exact le_rfl, }, { apply le_sup' (λ b, a) H.some_spec, } end @[simp] lemma sup'_le_iff {a : α} : s.sup' H f ≤ a ↔ ∀ b ∈ s, f b ≤ a := iff.intro (λ h b hb, trans (le_sup' f hb) h) (sup'_le H f) lemma sup'_bUnion [decidable_eq β] {s : finset γ} (Hs : s.nonempty) {t : γ → finset β} (Ht : ∀ b, (t b).nonempty) : (s.bUnion t).sup' (Hs.bUnion (λ b _, Ht b)) f = s.sup' Hs (λ b, (t b).sup' (Ht b) f) := eq_of_forall_ge_iff $ λ c, by simp [@forall_swap _ β] lemma comp_sup'_eq_sup'_comp [semilattice_sup γ] {s : finset β} (H : s.nonempty) {f : β → α} (g : α → γ) (g_sup : ∀ x y, g (x ⊔ y) = g x ⊔ g y) : g (s.sup' H f) = s.sup' H (g ∘ f) := begin rw [←with_bot.coe_eq_coe, coe_sup'], let g' : with_bot α → with_bot γ := with_bot.rec_bot_coe ⊥ (λ x, ↑(g x)), show g' ↑(s.sup' H f) = s.sup (λ a, g' ↑(f a)), rw coe_sup', refine comp_sup_eq_sup_comp g' _ rfl, intros f₁ f₂, cases f₁, { rw [with_bot.none_eq_bot, bot_sup_eq], exact bot_sup_eq.symm, }, { cases f₂, refl, exact congr_arg coe (g_sup f₁ f₂), }, end lemma sup'_induction {p : α → Prop} (hp : ∀ a₁, p a₁ → ∀ a₂, p a₂ → p (a₁ ⊔ a₂)) (hs : ∀ b ∈ s, p (f b)) : p (s.sup' H f) := begin show @with_bot.rec_bot_coe α (λ _, Prop) true p ↑(s.sup' H f), rw coe_sup', refine sup_induction trivial _ hs, rintro (_|a₁) h₁ a₂ h₂, { rw [with_bot.none_eq_bot, bot_sup_eq], exact h₂ }, cases a₂, exacts [h₁, hp a₁ h₁ a₂ h₂] end lemma sup'_mem (s : set α) (w : ∀ x y ∈ s, x ⊔ y ∈ s) {ι : Type*} (t : finset ι) (H : t.nonempty) (p : ι → α) (h : ∀ i ∈ t, p i ∈ s) : t.sup' H p ∈ s := sup'_induction H p w h @[congr] lemma sup'_congr {t : finset β} {f g : β → α} (h₁ : s = t) (h₂ : ∀ x ∈ s, f x = g x) : s.sup' H f = t.sup' (h₁ ▸ H) g := begin subst s, refine eq_of_forall_ge_iff (λ c, _), simp only [sup'_le_iff, h₂] { contextual := tt } end end sup' section inf' variables [semilattice_inf α] lemma inf_of_mem {s : finset β} (f : β → α) {b : β} (h : b ∈ s) : ∃ (a : α), s.inf (coe ∘ f : β → with_top α) = ↑a := @sup_of_mem αᵒᵈ _ _ _ f _ h /-- Given nonempty finset `s` then `s.inf' H f` is the infimum of its image under `f` in (possibly unbounded) meet-semilattice `α`, where `H` is a proof of nonemptiness. If `α` has a top element you may instead use `finset.inf` which does not require `s` nonempty. -/ def inf' (s : finset β) (H : s.nonempty) (f : β → α) : α := @sup' αᵒᵈ _ _ s H f variables {s : finset β} (H : s.nonempty) (f : β → α) {a : α} {b : β} @[simp] lemma coe_inf' : ((s.inf' H f : α) : with_top α) = s.inf (coe ∘ f) := @coe_sup' αᵒᵈ _ _ _ H f @[simp] lemma inf'_cons {b : β} {hb : b ∉ s} {h : (cons b s hb).nonempty} : (cons b s hb).inf' h f = f b ⊓ s.inf' H f := @sup'_cons αᵒᵈ _ _ _ H f _ _ _ @[simp] lemma inf'_insert [decidable_eq β] {b : β} {h : (insert b s).nonempty} : (insert b s).inf' h f = f b ⊓ s.inf' H f := @sup'_insert αᵒᵈ _ _ _ H f _ _ _ @[simp] lemma inf'_singleton {b : β} {h : ({b} : finset β).nonempty} : ({b} : finset β).inf' h f = f b := rfl lemma le_inf' (hs : ∀ b ∈ s, a ≤ f b) : a ≤ s.inf' H f := @sup'_le αᵒᵈ _ _ _ H f _ hs lemma inf'_le (h : b ∈ s) : s.inf' ⟨b, h⟩ f ≤ f b := @le_sup' αᵒᵈ _ _ _ f _ h @[simp] lemma inf'_const (a : α) : s.inf' H (λ b, a) = a := @sup'_const αᵒᵈ _ _ _ _ _ @[simp] lemma le_inf'_iff : a ≤ s.inf' H f ↔ ∀ b ∈ s, a ≤ f b := @sup'_le_iff αᵒᵈ _ _ _ H f _ lemma inf'_bUnion [decidable_eq β] {s : finset γ} (Hs : s.nonempty) {t : γ → finset β} (Ht : ∀ b, (t b).nonempty) : (s.bUnion t).inf' (Hs.bUnion (λ b _, Ht b)) f = s.inf' Hs (λ b, (t b).inf' (Ht b) f) := @sup'_bUnion αᵒᵈ _ _ _ _ _ _ Hs _ Ht lemma comp_inf'_eq_inf'_comp [semilattice_inf γ] {s : finset β} (H : s.nonempty) {f : β → α} (g : α → γ) (g_inf : ∀ x y, g (x ⊓ y) = g x ⊓ g y) : g (s.inf' H f) = s.inf' H (g ∘ f) := @comp_sup'_eq_sup'_comp αᵒᵈ _ γᵒᵈ _ _ _ H f g g_inf lemma inf'_induction {p : α → Prop} (hp : ∀ a₁, p a₁ → ∀ a₂, p a₂ → p (a₁ ⊓ a₂)) (hs : ∀ b ∈ s, p (f b)) : p (s.inf' H f) := @sup'_induction αᵒᵈ _ _ _ H f _ hp hs lemma inf'_mem (s : set α) (w : ∀ x y ∈ s, x ⊓ y ∈ s) {ι : Type*} (t : finset ι) (H : t.nonempty) (p : ι → α) (h : ∀ i ∈ t, p i ∈ s) : t.inf' H p ∈ s := inf'_induction H p w h @[congr] lemma inf'_congr {t : finset β} {f g : β → α} (h₁ : s = t) (h₂ : ∀ x ∈ s, f x = g x) : s.inf' H f = t.inf' (h₁ ▸ H) g := @sup'_congr αᵒᵈ _ _ _ H _ _ _ h₁ h₂ end inf' section sup variables [semilattice_sup α] [order_bot α] lemma sup'_eq_sup {s : finset β} (H : s.nonempty) (f : β → α) : s.sup' H f = s.sup f := le_antisymm (sup'_le H f (λ b, le_sup)) (sup_le (λ b, le_sup' f)) lemma sup_closed_of_sup_closed {s : set α} (t : finset α) (htne : t.nonempty) (h_subset : ↑t ⊆ s) (h : ∀ a b ∈ s, a ⊔ b ∈ s) : t.sup id ∈ s := sup'_eq_sup htne id ▸ sup'_induction _ _ h h_subset lemma coe_sup_of_nonempty {s : finset β} (h : s.nonempty) (f : β → α) : (↑(s.sup f) : with_bot α) = s.sup (coe ∘ f) := by simp only [←sup'_eq_sup h, coe_sup' h] end sup section inf variables [semilattice_inf α] [order_top α] lemma inf'_eq_inf {s : finset β} (H : s.nonempty) (f : β → α) : s.inf' H f = s.inf f := @sup'_eq_sup αᵒᵈ _ _ _ _ H f lemma inf_closed_of_inf_closed {s : set α} (t : finset α) (htne : t.nonempty) (h_subset : ↑t ⊆ s) (h : ∀ a b ∈ s, a ⊓ b ∈ s) : t.inf id ∈ s := @sup_closed_of_sup_closed αᵒᵈ _ _ _ t htne h_subset h lemma coe_inf_of_nonempty {s : finset β} (h : s.nonempty) (f : β → α): (↑(s.inf f) : with_top α) = s.inf (λ i, f i) := @coe_sup_of_nonempty αᵒᵈ _ _ _ _ h f end inf section sup variables {C : β → Type*} [Π (b : β), semilattice_sup (C b)] [Π (b : β), order_bot (C b)] @[simp] protected lemma sup_apply (s : finset α) (f : α → Π (b : β), C b) (b : β) : s.sup f b = s.sup (λ a, f a b) := comp_sup_eq_sup_comp (λ x : Π b : β, C b, x b) (λ i j, rfl) rfl end sup section inf variables {C : β → Type*} [Π (b : β), semilattice_inf (C b)] [Π (b : β), order_top (C b)] @[simp] protected lemma inf_apply (s : finset α) (f : α → Π (b : β), C b) (b : β) : s.inf f b = s.inf (λ a, f a b) := @finset.sup_apply _ _ (λ b, (C b)ᵒᵈ) _ _ s f b end inf section sup' variables {C : β → Type*} [Π (b : β), semilattice_sup (C b)] @[simp] protected lemma sup'_apply {s : finset α} (H : s.nonempty) (f : α → Π (b : β), C b) (b : β) : s.sup' H f b = s.sup' H (λ a, f a b) := comp_sup'_eq_sup'_comp H (λ x : Π b : β, C b, x b) (λ i j, rfl) end sup' section inf' variables {C : β → Type*} [Π (b : β), semilattice_inf (C b)] @[simp] protected lemma inf'_apply {s : finset α} (H : s.nonempty) (f : α → Π (b : β), C b) (b : β) : s.inf' H f b = s.inf' H (λ a, f a b) := @finset.sup'_apply _ _ (λ b, (C b)ᵒᵈ) _ _ H f b end inf' @[simp] lemma to_dual_sup' [semilattice_sup α] {s : finset ι} (hs : s.nonempty) (f : ι → α) : to_dual (s.sup' hs f) = s.inf' hs (to_dual ∘ f) := rfl @[simp] lemma to_dual_inf' [semilattice_inf α] {s : finset ι} (hs : s.nonempty) (f : ι → α) : to_dual (s.inf' hs f) = s.sup' hs (to_dual ∘ f) := rfl @[simp] lemma of_dual_sup' [semilattice_inf α] {s : finset ι} (hs : s.nonempty) (f : ι → αᵒᵈ) : of_dual (s.sup' hs f) = s.inf' hs (of_dual ∘ f) := rfl @[simp] lemma of_dual_inf' [semilattice_sup α] {s : finset ι} (hs : s.nonempty) (f : ι → αᵒᵈ) : of_dual (s.inf' hs f) = s.sup' hs (of_dual ∘ f) := rfl section linear_order variables [linear_order α] {s : finset ι} (H : s.nonempty) {f : ι → α} {a : α} @[simp] lemma le_sup'_iff : a ≤ s.sup' H f ↔ ∃ b ∈ s, a ≤ f b := begin rw [←with_bot.coe_le_coe, coe_sup', finset.le_sup_iff (with_bot.bot_lt_coe a)], exact bex_congr (λ b hb, with_bot.coe_le_coe), end @[simp] lemma lt_sup'_iff : a < s.sup' H f ↔ ∃ b ∈ s, a < f b := begin rw [←with_bot.coe_lt_coe, coe_sup', finset.lt_sup_iff], exact bex_congr (λ b hb, with_bot.coe_lt_coe), end @[simp] lemma sup'_lt_iff : s.sup' H f < a ↔ ∀ i ∈ s, f i < a := begin rw [←with_bot.coe_lt_coe, coe_sup', finset.sup_lt_iff (with_bot.bot_lt_coe a)], exact ball_congr (λ b hb, with_bot.coe_lt_coe), end @[simp] lemma inf'_le_iff : s.inf' H f ≤ a ↔ ∃ i ∈ s, f i ≤ a := @le_sup'_iff αᵒᵈ _ _ _ H f _ @[simp] lemma inf'_lt_iff : s.inf' H f < a ↔ ∃ i ∈ s, f i < a := @lt_sup'_iff αᵒᵈ _ _ _ H f _ @[simp] lemma lt_inf'_iff : a < s.inf' H f ↔ ∀ i ∈ s, a < f i := @sup'_lt_iff αᵒᵈ _ _ _ H f _ lemma exists_mem_eq_sup' (f : ι → α) : ∃ i, i ∈ s ∧ s.sup' H f = f i := begin refine H.cons_induction (λ c, _) (λ c s hc hs ih, _), { exact ⟨c, mem_singleton_self c, rfl⟩, }, { rcases ih with ⟨b, hb, h'⟩, rw [sup'_cons hs, h'], cases total_of (≤) (f b) (f c) with h h, { exact ⟨c, mem_cons.2 (or.inl rfl), sup_eq_left.2 h⟩, }, { exact ⟨b, mem_cons.2 (or.inr hb), sup_eq_right.2 h⟩, }, }, end lemma exists_mem_eq_inf' (f : ι → α) : ∃ i, i ∈ s ∧ s.inf' H f = f i := @exists_mem_eq_sup' αᵒᵈ _ _ _ H f lemma exists_mem_eq_sup [order_bot α] (s : finset ι) (h : s.nonempty) (f : ι → α) : ∃ i, i ∈ s ∧ s.sup f = f i := sup'_eq_sup h f ▸ exists_mem_eq_sup' h f lemma exists_mem_eq_inf [order_top α] (s : finset ι) (h : s.nonempty) (f : ι → α) : ∃ i, i ∈ s ∧ s.inf f = f i := @exists_mem_eq_sup αᵒᵈ _ _ _ _ h f end linear_order /-! ### max and min of finite sets -/ section max_min variables [linear_order α] /-- Let `s` be a finset in a linear order. Then `s.max` is the maximum of `s` if `s` is not empty, and `none` otherwise. It belongs to `option α`. If you want to get an element of `α`, see `s.max'`. -/ protected def max : finset α → option α := fold (option.lift_or_get max) none some theorem max_eq_sup_with_bot (s : finset α) : s.max = @sup (with_bot α) α _ _ s some := rfl @[simp] theorem max_empty : (∅ : finset α).max = none := rfl @[simp] theorem max_insert {a : α} {s : finset α} : (insert a s).max = option.lift_or_get max (some a) s.max := fold_insert_idem @[simp] theorem max_singleton {a : α} : finset.max {a} = some a := by { rw [← insert_emptyc_eq], exact max_insert } theorem max_of_mem {s : finset α} {a : α} (h : a ∈ s) : ∃ b, b ∈ s.max := (@le_sup (with_bot α) _ _ _ _ _ _ h _ rfl).imp $ λ b, Exists.fst theorem max_of_nonempty {s : finset α} (h : s.nonempty) : ∃ a, a ∈ s.max := let ⟨a, ha⟩ := h in max_of_mem ha theorem max_eq_none {s : finset α} : s.max = none ↔ s = ∅ := ⟨λ h, s.eq_empty_or_nonempty.elim id (λ H, let ⟨a, ha⟩ := max_of_nonempty H in by rw h at ha; cases ha), λ h, h.symm ▸ max_empty⟩ theorem mem_of_max {s : finset α} : ∀ {a : α}, a ∈ s.max → a ∈ s := finset.induction_on s (λ _ H, by cases H) (λ b s _ (ih : ∀ {a}, a ∈ s.max → a ∈ s) a (h : a ∈ (insert b s).max), begin by_cases p : b = a, { induction p, exact mem_insert_self b s }, { cases option.lift_or_get_choice max_choice (some b) s.max with q q; rw [max_insert, q] at h, { cases h, cases p rfl }, { exact mem_insert_of_mem (ih h) } } end) theorem le_max_of_mem {s : finset α} {a b : α} (h₁ : a ∈ s) (h₂ : b ∈ s.max) : a ≤ b := by rcases @le_sup (with_bot α) _ _ _ _ _ _ h₁ _ rfl with ⟨b', hb, ab⟩; cases h₂.symm.trans hb; assumption /-- Let `s` be a finset in a linear order. Then `s.min` is the minimum of `s` if `s` is not empty, and `none` otherwise. It belongs to `option α`. If you want to get an element of `α`, see `s.min'`. -/ protected def min : finset α → option α := fold (option.lift_or_get min) none some theorem min_eq_inf_with_top (s : finset α) : s.min = @inf (with_top α) α _ _ s some := rfl @[simp] theorem min_empty : (∅ : finset α).min = none := rfl @[simp] theorem min_insert {a : α} {s : finset α} : (insert a s).min = option.lift_or_get min (some a) s.min := fold_insert_idem @[simp] theorem min_singleton {a : α} : finset.min {a} = some a := by { rw ← insert_emptyc_eq, exact min_insert } theorem min_of_mem {s : finset α} {a : α} (h : a ∈ s) : ∃ b, b ∈ s.min := (@inf_le (with_top α) _ _ _ _ _ _ h _ rfl).imp $ λ b, Exists.fst theorem min_of_nonempty {s : finset α} (h : s.nonempty) : ∃ a, a ∈ s.min := let ⟨a, ha⟩ := h in min_of_mem ha theorem min_eq_none {s : finset α} : s.min = none ↔ s = ∅ := ⟨λ h, s.eq_empty_or_nonempty.elim id (λ H, let ⟨a, ha⟩ := min_of_nonempty H in by rw h at ha; cases ha), λ h, h.symm ▸ min_empty⟩ theorem mem_of_min {s : finset α} : ∀ {a : α}, a ∈ s.min → a ∈ s := @mem_of_max αᵒᵈ _ s theorem min_le_of_mem {s : finset α} {a b : α} (h₁ : b ∈ s) (h₂ : a ∈ s.min) : a ≤ b := by rcases @inf_le (with_top α) _ _ _ _ _ _ h₁ _ rfl with ⟨b', hb, ab⟩; cases h₂.symm.trans hb; assumption /-- Given a nonempty finset `s` in a linear order `α `, then `s.min' h` is its minimum, as an element of `α`, where `h` is a proof of nonemptiness. Without this assumption, use instead `s.min`, taking values in `option α`. -/ def min' (s : finset α) (H : s.nonempty) : α := @option.get _ s.min $ let ⟨k, hk⟩ := H in let ⟨b, hb⟩ := min_of_mem hk in by simp at hb; simp [hb] /-- Given a nonempty finset `s` in a linear order `α `, then `s.max' h` is its maximum, as an element of `α`, where `h` is a proof of nonemptiness. Without this assumption, use instead `s.max`, taking values in `option α`. -/ def max' (s : finset α) (H : s.nonempty) : α := @option.get _ s.max $ let ⟨k, hk⟩ := H in let ⟨b, hb⟩ := max_of_mem hk in by simp at hb; simp [hb] variables (s : finset α) (H : s.nonempty) {x : α} theorem min'_mem : s.min' H ∈ s := mem_of_min $ by simp [min'] theorem min'_le (x) (H2 : x ∈ s) : s.min' ⟨x, H2⟩ ≤ x := min_le_of_mem H2 $ option.get_mem _ theorem le_min' (x) (H2 : ∀ y ∈ s, x ≤ y) : x ≤ s.min' H := H2 _ $ min'_mem _ _ theorem is_least_min' : is_least ↑s (s.min' H) := ⟨min'_mem _ _, min'_le _⟩ @[simp] lemma le_min'_iff {x} : x ≤ s.min' H ↔ ∀ y ∈ s, x ≤ y := le_is_glb_iff (is_least_min' s H).is_glb /-- `{a}.min' _` is `a`. -/ @[simp] lemma min'_singleton (a : α) : ({a} : finset α).min' (singleton_nonempty _) = a := by simp [min'] theorem max'_mem : s.max' H ∈ s := mem_of_max $ by simp [max'] theorem le_max' (x) (H2 : x ∈ s) : x ≤ s.max' ⟨x, H2⟩ := le_max_of_mem H2 $ option.get_mem _ theorem max'_le (x) (H2 : ∀ y ∈ s, y ≤ x) : s.max' H ≤ x := H2 _ $ max'_mem _ _ theorem is_greatest_max' : is_greatest ↑s (s.max' H) := ⟨max'_mem _ _, le_max' _⟩ @[simp] lemma max'_le_iff {x} : s.max' H ≤ x ↔ ∀ y ∈ s, y ≤ x := is_lub_le_iff (is_greatest_max' s H).is_lub @[simp] lemma max'_lt_iff {x} : s.max' H < x ↔ ∀ y ∈ s, y < x := ⟨λ Hlt y hy, (s.le_max' y hy).trans_lt Hlt, λ H, H _ $ s.max'_mem _⟩ @[simp] lemma lt_min'_iff : x < s.min' H ↔ ∀ y ∈ s, x < y := @max'_lt_iff αᵒᵈ _ _ H _ lemma max'_eq_sup' : s.max' H = s.sup' H id := eq_of_forall_ge_iff $ λ a, (max'_le_iff _ _).trans (sup'_le_iff _ _).symm lemma min'_eq_inf' : s.min' H = s.inf' H id := @max'_eq_sup' αᵒᵈ _ s H /-- `{a}.max' _` is `a`. -/ @[simp] lemma max'_singleton (a : α) : ({a} : finset α).max' (singleton_nonempty _) = a := by simp [max'] theorem min'_lt_max' {i j} (H1 : i ∈ s) (H2 : j ∈ s) (H3 : i ≠ j) : s.min' ⟨i, H1⟩ < s.max' ⟨i, H1⟩ := is_glb_lt_is_lub_of_ne (s.is_least_min' _).is_glb (s.is_greatest_max' _).is_lub H1 H2 H3 /-- If there's more than 1 element, the min' is less than the max'. An alternate version of `min'_lt_max'` which is sometimes more convenient. -/ lemma min'_lt_max'_of_card (h₂ : 1 < card s) : s.min' (finset.card_pos.mp $ lt_trans zero_lt_one h₂) < s.max' (finset.card_pos.mp $ lt_trans zero_lt_one h₂) := begin rcases one_lt_card.1 h₂ with ⟨a, ha, b, hb, hab⟩, exact s.min'_lt_max' ha hb hab end lemma map_of_dual_min (s : finset αᵒᵈ) : s.min.map of_dual = (s.image of_dual).max := by { rw [max_eq_sup_with_bot, sup_image], exact congr_fun option.map_id _ } lemma map_of_dual_max (s : finset αᵒᵈ) : s.max.map of_dual = (s.image of_dual).min := by { rw [min_eq_inf_with_top, inf_image], exact congr_fun option.map_id _ } lemma map_to_dual_min (s : finset α) : s.min.map to_dual = (s.image to_dual).max := by { rw [max_eq_sup_with_bot, sup_image], exact congr_fun option.map_id _ } lemma map_to_dual_max (s : finset α) : s.max.map to_dual = (s.image to_dual).min := by { rw [min_eq_inf_with_top, inf_image], exact congr_fun option.map_id _ } lemma of_dual_min' {s : finset αᵒᵈ} (hs : s.nonempty) : of_dual (min' s hs) = max' (s.image of_dual) (hs.image _) := by { convert rfl, exact image_id } lemma of_dual_max' {s : finset αᵒᵈ} (hs : s.nonempty) : of_dual (max' s hs) = min' (s.image of_dual) (hs.image _) := by { convert rfl, exact image_id } lemma to_dual_min' {s : finset α} (hs : s.nonempty) : to_dual (min' s hs) = max' (s.image to_dual) (hs.image _) := by { convert rfl, exact image_id } lemma to_dual_max' {s : finset α} (hs : s.nonempty) : to_dual (max' s hs) = min' (s.image to_dual) (hs.image _) := by { convert rfl, exact image_id } lemma max'_subset {s t : finset α} (H : s.nonempty) (hst : s ⊆ t) : s.max' H ≤ t.max' (H.mono hst) := le_max' _ _ (hst (s.max'_mem H)) lemma min'_subset {s t : finset α} (H : s.nonempty) (hst : s ⊆ t) : t.min' (H.mono hst) ≤ s.min' H := min'_le _ _ (hst (s.min'_mem H)) lemma max'_insert (a : α) (s : finset α) (H : s.nonempty) : (insert a s).max' (s.insert_nonempty a) = max (s.max' H) a := (is_greatest_max' _ _).unique $ by { rw [coe_insert, max_comm], exact (is_greatest_max' _ _).insert _ } lemma min'_insert (a : α) (s : finset α) (H : s.nonempty) : (insert a s).min' (s.insert_nonempty a) = min (s.min' H) a := (is_least_min' _ _).unique $ by { rw [coe_insert, min_comm], exact (is_least_min' _ _).insert _ } lemma lt_max'_of_mem_erase_max' [decidable_eq α] {a : α} (ha : a ∈ s.erase (s.max' H)) : a < s.max' H := lt_of_le_of_ne (le_max' _ _ (mem_of_mem_erase ha)) $ ne_of_mem_of_not_mem ha $ not_mem_erase _ _ lemma min'_lt_of_mem_erase_min' [decidable_eq α] {a : α} (ha : a ∈ s.erase (s.min' H)) : s.min' H < a := @lt_max'_of_mem_erase_max' αᵒᵈ _ s H _ a ha @[simp] lemma max'_image [linear_order β] {f : α → β} (hf : monotone f) (s : finset α) (h : (s.image f).nonempty) : (s.image f).max' h = f (s.max' ((nonempty.image_iff f).mp h)) := begin refine le_antisymm (max'_le _ _ _ (λ y hy, _)) (le_max' _ _ (mem_image.mpr ⟨_, max'_mem _ _, rfl⟩)), obtain ⟨x, hx, rfl⟩ := mem_image.mp hy, exact hf (le_max' _ _ hx) end @[simp] lemma min'_image [linear_order β] {f : α → β} (hf : monotone f) (s : finset α) (h : (s.image f).nonempty) : (s.image f).min' h = f (s.min' ((nonempty.image_iff f).mp h)) := begin refine le_antisymm (min'_le _ _ (mem_image.mpr ⟨_, min'_mem _ _, rfl⟩)) (le_min' _ _ _ (λ y hy, _)), obtain ⟨x, hx, rfl⟩ := mem_image.mp hy, exact hf (min'_le _ _ hx) end /-- Induction principle for `finset`s in a linearly ordered type: a predicate is true on all `s : finset α` provided that: * it is true on the empty `finset`, * for every `s : finset α` and an element `a` strictly greater than all elements of `s`, `p s` implies `p (insert a s)`. -/ @[elab_as_eliminator] lemma induction_on_max [decidable_eq α] {p : finset α → Prop} (s : finset α) (h0 : p ∅) (step : ∀ a s, (∀ x ∈ s, x < a) → p s → p (insert a s)) : p s := begin induction s using finset.strong_induction_on with s ihs, rcases s.eq_empty_or_nonempty with rfl|hne, { exact h0 }, { have H : s.max' hne ∈ s, from max'_mem s hne, rw ← insert_erase H, exact step _ _ (λ x, s.lt_max'_of_mem_erase_max' hne) (ihs _ $ erase_ssubset H) } end /-- Induction principle for `finset`s in a linearly ordered type: a predicate is true on all `s : finset α` provided that: * it is true on the empty `finset`, * for every `s : finset α` and an element `a` strictly less than all elements of `s`, `p s` implies `p (insert a s)`. -/ @[elab_as_eliminator] lemma induction_on_min [decidable_eq α] {p : finset α → Prop} (s : finset α) (h0 : p ∅) (step : ∀ a s, (∀ x ∈ s, a < x) → p s → p (insert a s)) : p s := @induction_on_max αᵒᵈ _ _ _ s h0 step end max_min section max_min_induction_value variables [linear_order α] [linear_order β] /-- Induction principle for `finset`s in any type from which a given function `f` maps to a linearly ordered type : a predicate is true on all `s : finset α` provided that: * it is true on the empty `finset`, * for every `s : finset α` and an element `a` such that for elements of `s` denoted by `x` we have `f x ≤ f a`, `p s` implies `p (insert a s)`. -/ @[elab_as_eliminator] lemma induction_on_max_value [decidable_eq ι] (f : ι → α) {p : finset ι → Prop} (s : finset ι) (h0 : p ∅) (step : ∀ a s, a ∉ s → (∀ x ∈ s, f x ≤ f a) → p s → p (insert a s)) : p s := begin induction s using finset.strong_induction_on with s ihs, rcases (s.image f).eq_empty_or_nonempty with hne|hne, { simp only [image_eq_empty] at hne, simp only [hne, h0] }, { have H : (s.image f).max' hne ∈ (s.image f), from max'_mem (s.image f) hne, simp only [mem_image, exists_prop] at H, rcases H with ⟨a, has, hfa⟩, rw ← insert_erase has, refine step _ _ (not_mem_erase a s) (λ x hx, _) (ihs _ $ erase_ssubset has), rw hfa, exact le_max' _ _ (mem_image_of_mem _ $ mem_of_mem_erase hx) } end /-- Induction principle for `finset`s in any type from which a given function `f` maps to a linearly ordered type : a predicate is true on all `s : finset α` provided that: * it is true on the empty `finset`, * for every `s : finset α` and an element `a` such that for elements of `s` denoted by `x` we have `f a ≤ f x`, `p s` implies `p (insert a s)`. -/ @[elab_as_eliminator] lemma induction_on_min_value [decidable_eq ι] (f : ι → α) {p : finset ι → Prop} (s : finset ι) (h0 : p ∅) (step : ∀ a s, a ∉ s → (∀ x ∈ s, f a ≤ f x) → p s → p (insert a s)) : p s := @induction_on_max_value αᵒᵈ ι _ _ _ _ s h0 step end max_min_induction_value section exists_max_min variables [linear_order α] lemma exists_max_image (s : finset β) (f : β → α) (h : s.nonempty) : ∃ x ∈ s, ∀ x' ∈ s, f x' ≤ f x := begin cases max_of_nonempty (h.image f) with y hy, rcases mem_image.mp (mem_of_max hy) with ⟨x, hx, rfl⟩, exact ⟨x, hx, λ x' hx', le_max_of_mem (mem_image_of_mem f hx') hy⟩, end lemma exists_min_image (s : finset β) (f : β → α) (h : s.nonempty) : ∃ x ∈ s, ∀ x' ∈ s, f x ≤ f x' := @exists_max_image αᵒᵈ β _ s f h end exists_max_min end finset namespace multiset lemma map_finset_sup [decidable_eq α] [decidable_eq β] (s : finset γ) (f : γ → multiset β) (g : β → α) (hg : function.injective g) : map g (s.sup f) = s.sup (map g ∘ f) := finset.comp_sup_eq_sup_comp _ (λ _ _, map_union hg) (map_zero _) lemma count_finset_sup [decidable_eq β] (s : finset α) (f : α → multiset β) (b : β) : count b (s.sup f) = s.sup (λa, count b (f a)) := begin letI := classical.dec_eq α, refine s.induction _ _, { exact count_zero _ }, { assume i s his ih, rw [finset.sup_insert, sup_eq_union, count_union, finset.sup_insert, ih], refl } end lemma mem_sup {α β} [decidable_eq β] {s : finset α} {f : α → multiset β} {x : β} : x ∈ s.sup f ↔ ∃ v ∈ s, x ∈ f v := begin classical, apply s.induction_on, { simp }, { intros a s has hxs, rw [finset.sup_insert, multiset.sup_eq_union, multiset.mem_union], split, { intro hxi, cases hxi with hf hf, { refine ⟨a, _, hf⟩, simp only [true_or, eq_self_iff_true, finset.mem_insert] }, { rcases hxs.mp hf with ⟨v, hv, hfv⟩, refine ⟨v, _, hfv⟩, simp only [hv, or_true, finset.mem_insert] } }, { rintros ⟨v, hv, hfv⟩, rw [finset.mem_insert] at hv, rcases hv with rfl | hv, { exact or.inl hfv }, { refine or.inr (hxs.mpr ⟨v, hv, hfv⟩) } } }, end end multiset namespace finset lemma mem_sup {α β} [decidable_eq β] {s : finset α} {f : α → finset β} {x : β} : x ∈ s.sup f ↔ ∃ v ∈ s, x ∈ f v := begin change _ ↔ ∃ v ∈ s, x ∈ (f v).val, rw [←multiset.mem_sup, ←multiset.mem_to_finset, sup_to_finset], simp_rw [val_to_finset], end lemma sup_eq_bUnion {α β} [decidable_eq β] (s : finset α) (t : α → finset β) : s.sup t = s.bUnion t := by { ext, rw [mem_sup, mem_bUnion], } @[simp] lemma sup_singleton'' [decidable_eq α] (s : finset β) (f : β → α) : s.sup (λ b, {f b}) = s.image f := by { ext a, rw [mem_sup, mem_image], simp only [mem_singleton, eq_comm] } @[simp] lemma sup_singleton' [decidable_eq α] (s : finset α) : s.sup singleton = s := (s.sup_singleton'' _).trans image_id end finset section lattice variables {ι' : Sort*} [complete_lattice α] /-- Supremum of `s i`, `i : ι`, is equal to the supremum over `t : finset ι` of suprema `⨆ i ∈ t, s i`. This version assumes `ι` is a `Type*`. See `supr_eq_supr_finset'` for a version that works for `ι : Sort*`. -/ lemma supr_eq_supr_finset (s : ι → α) : (⨆i, s i) = (⨆t:finset ι, ⨆i∈t, s i) := begin classical, exact le_antisymm (supr_le $ assume b, le_supr_of_le {b} $ le_supr_of_le b $ le_supr_of_le (by simp) $ le_rfl) (supr_le $ assume t, supr_le $ assume b, supr_le $ assume hb, le_supr _ _) end /-- Supremum of `s i`, `i : ι`, is equal to the supremum over `t : finset ι` of suprema `⨆ i ∈ t, s i`. This version works for `ι : Sort*`. See `supr_eq_supr_finset` for a version that assumes `ι : Type*` but has no `plift`s. -/ lemma supr_eq_supr_finset' (s : ι' → α) : (⨆i, s i) = (⨆t:finset (plift ι'), ⨆i∈t, s (plift.down i)) := by rw [← supr_eq_supr_finset, ← equiv.plift.surjective.supr_comp]; refl /-- Infimum of `s i`, `i : ι`, is equal to the infimum over `t : finset ι` of infima `⨅ i ∈ t, s i`. This version assumes `ι` is a `Type*`. See `infi_eq_infi_finset'` for a version that works for `ι : Sort*`. -/ lemma infi_eq_infi_finset (s : ι → α) : (⨅ i, s i) = ⨅ (t : finset ι) (i ∈ t), s i := @supr_eq_supr_finset αᵒᵈ _ _ _ /-- Infimum of `s i`, `i : ι`, is equal to the infimum over `t : finset ι` of infima `⨅ i ∈ t, s i`. This version works for `ι : Sort*`. See `infi_eq_infi_finset` for a version that assumes `ι : Type*` but has no `plift`s. -/ lemma infi_eq_infi_finset' (s : ι' → α) : (⨅i, s i) = (⨅t:finset (plift ι'), ⨅i∈t, s (plift.down i)) := @supr_eq_supr_finset' αᵒᵈ _ _ _ end lattice namespace set variables {ι' : Sort*} /-- Union of an indexed family of sets `s : ι → set α` is equal to the union of the unions of finite subfamilies. This version assumes `ι : Type*`. See also `Union_eq_Union_finset'` for a version that works for `ι : Sort*`. -/ lemma Union_eq_Union_finset (s : ι → set α) : (⋃i, s i) = (⋃t:finset ι, ⋃i∈t, s i) := supr_eq_supr_finset s /-- Union of an indexed family of sets `s : ι → set α` is equal to the union of the unions of finite subfamilies. This version works for `ι : Sort*`. See also `Union_eq_Union_finset` for a version that assumes `ι : Type*` but avoids `plift`s in the right hand side. -/ lemma Union_eq_Union_finset' (s : ι' → set α) : (⋃i, s i) = (⋃t:finset (plift ι'), ⋃i∈t, s (plift.down i)) := supr_eq_supr_finset' s /-- Intersection of an indexed family of sets `s : ι → set α` is equal to the intersection of the intersections of finite subfamilies. This version assumes `ι : Type*`. See also `Inter_eq_Inter_finset'` for a version that works for `ι : Sort*`. -/ lemma Inter_eq_Inter_finset (s : ι → set α) : (⋂i, s i) = (⋂t:finset ι, ⋂i∈t, s i) := infi_eq_infi_finset s /-- Intersection of an indexed family of sets `s : ι → set α` is equal to the intersection of the intersections of finite subfamilies. This version works for `ι : Sort*`. See also `Inter_eq_Inter_finset` for a version that assumes `ι : Type*` but avoids `plift`s in the right hand side. -/ lemma Inter_eq_Inter_finset' (s : ι' → set α) : (⋂i, s i) = (⋂t:finset (plift ι'), ⋂i∈t, s (plift.down i)) := infi_eq_infi_finset' s end set namespace finset /-! ### Interaction with ordered algebra structures -/ lemma sup_mul_le_mul_sup_of_nonneg [linear_ordered_semiring α] [order_bot α] {a b : ι → α} (s : finset ι) (ha : ∀ i ∈ s, 0 ≤ a i) (hb : ∀ i ∈ s, 0 ≤ b i) : s.sup (a * b) ≤ s.sup a * s.sup b := finset.sup_le $ λ i hi, mul_le_mul (le_sup hi) (le_sup hi) (hb _ hi) ((ha _ hi).trans $ le_sup hi) lemma mul_inf_le_inf_mul_of_nonneg [linear_ordered_semiring α] [order_top α] {a b : ι → α} (s : finset ι) (ha : ∀ i ∈ s, 0 ≤ a i) (hb : ∀ i ∈ s, 0 ≤ b i) : s.inf a * s.inf b ≤ s.inf (a * b) := finset.le_inf $ λ i hi, mul_le_mul (inf_le hi) (inf_le hi) (finset.le_inf hb) (ha i hi) lemma sup'_mul_le_mul_sup'_of_nonneg [linear_ordered_semiring α] {a b : ι → α} (s : finset ι) (H : s.nonempty) (ha : ∀ i ∈ s, 0 ≤ a i) (hb : ∀ i ∈ s, 0 ≤ b i) : s.sup' H (a * b) ≤ s.sup' H a * s.sup' H b := sup'_le _ _ $ λ i hi, mul_le_mul (le_sup' _ hi) (le_sup' _ hi) (hb _ hi) ((ha _ hi).trans $ le_sup' _ hi) lemma inf'_mul_le_mul_inf'_of_nonneg [linear_ordered_semiring α] {a b : ι → α} (s : finset ι) (H : s.nonempty) (ha : ∀ i ∈ s, 0 ≤ a i) (hb : ∀ i ∈ s, 0 ≤ b i) : s.inf' H a * s.inf' H b ≤ s.inf' H (a * b) := le_inf' _ _ $ λ i hi, mul_le_mul (inf'_le _ hi) (inf'_le _ hi) (le_inf' _ _ hb) (ha _ hi) open function /-! ### Interaction with big lattice/set operations -/ section lattice lemma supr_coe [has_Sup β] (f : α → β) (s : finset α) : (⨆ x ∈ (↑s : set α), f x) = ⨆ x ∈ s, f x := rfl lemma infi_coe [has_Inf β] (f : α → β) (s : finset α) : (⨅ x ∈ (↑s : set α), f x) = ⨅ x ∈ s, f x := rfl variables [complete_lattice β] theorem supr_singleton (a : α) (s : α → β) : (⨆ x ∈ ({a} : finset α), s x) = s a := by simp theorem infi_singleton (a : α) (s : α → β) : (⨅ x ∈ ({a} : finset α), s x) = s a := by simp lemma supr_option_to_finset (o : option α) (f : α → β) : (⨆ x ∈ o.to_finset, f x) = ⨆ x ∈ o, f x := by simp lemma infi_option_to_finset (o : option α) (f : α → β) : (⨅ x ∈ o.to_finset, f x) = ⨅ x ∈ o, f x := @supr_option_to_finset _ βᵒᵈ _ _ _ variables [decidable_eq α] theorem supr_union {f : α → β} {s t : finset α} : (⨆ x ∈ s ∪ t, f x) = (⨆x∈s, f x) ⊔ (⨆x∈t, f x) := by simp [supr_or, supr_sup_eq] theorem infi_union {f : α → β} {s t : finset α} : (⨅ x ∈ s ∪ t, f x) = (⨅ x ∈ s, f x) ⊓ (⨅ x ∈ t, f x) := @supr_union α βᵒᵈ _ _ _ _ _ lemma supr_insert (a : α) (s : finset α) (t : α → β) : (⨆ x ∈ insert a s, t x) = t a ⊔ (⨆ x ∈ s, t x) := by { rw insert_eq, simp only [supr_union, finset.supr_singleton] } lemma infi_insert (a : α) (s : finset α) (t : α → β) : (⨅ x ∈ insert a s, t x) = t a ⊓ (⨅ x ∈ s, t x) := @supr_insert α βᵒᵈ _ _ _ _ _ lemma supr_finset_image {f : γ → α} {g : α → β} {s : finset γ} : (⨆ x ∈ s.image f, g x) = (⨆ y ∈ s, g (f y)) := by rw [← supr_coe, coe_image, supr_image, supr_coe] lemma sup_finset_image {β γ : Type*} [semilattice_sup β] [order_bot β] (f : γ → α) (g : α → β) (s : finset γ) : (s.image f).sup g = s.sup (g ∘ f) := begin classical, induction s using finset.induction_on with a s' ha ih; simp * end lemma infi_finset_image {f : γ → α} {g : α → β} {s : finset γ} : (⨅ x ∈ s.image f, g x) = (⨅ y ∈ s, g (f y)) := by rw [← infi_coe, coe_image, infi_image, infi_coe] lemma supr_insert_update {x : α} {t : finset α} (f : α → β) {s : β} (hx : x ∉ t) : (⨆ (i ∈ insert x t), function.update f x s i) = (s ⊔ ⨆ (i ∈ t), f i) := begin simp only [finset.supr_insert, update_same], rcongr i hi, apply update_noteq, rintro rfl, exact hx hi end lemma infi_insert_update {x : α} {t : finset α} (f : α → β) {s : β} (hx : x ∉ t) : (⨅ (i ∈ insert x t), update f x s i) = (s ⊓ ⨅ (i ∈ t), f i) := @supr_insert_update α βᵒᵈ _ _ _ _ f _ hx lemma supr_bUnion (s : finset γ) (t : γ → finset α) (f : α → β) : (⨆ y ∈ s.bUnion t, f y) = ⨆ (x ∈ s) (y ∈ t x), f y := by simp [@supr_comm _ α, supr_and] lemma infi_bUnion (s : finset γ) (t : γ → finset α) (f : α → β) : (⨅ y ∈ s.bUnion t, f y) = ⨅ (x ∈ s) (y ∈ t x), f y := @supr_bUnion _ βᵒᵈ _ _ _ _ _ _ end lattice theorem set_bUnion_coe (s : finset α) (t : α → set β) : (⋃ x ∈ (↑s : set α), t x) = ⋃ x ∈ s, t x := rfl theorem set_bInter_coe (s : finset α) (t : α → set β) : (⋂ x ∈ (↑s : set α), t x) = ⋂ x ∈ s, t x := rfl theorem set_bUnion_singleton (a : α) (s : α → set β) : (⋃ x ∈ ({a} : finset α), s x) = s a := supr_singleton a s theorem set_bInter_singleton (a : α) (s : α → set β) : (⋂ x ∈ ({a} : finset α), s x) = s a := infi_singleton a s @[simp] lemma set_bUnion_preimage_singleton (f : α → β) (s : finset β) : (⋃ y ∈ s, f ⁻¹' {y}) = f ⁻¹' s := set.bUnion_preimage_singleton f s lemma set_bUnion_option_to_finset (o : option α) (f : α → set β) : (⋃ x ∈ o.to_finset, f x) = ⋃ x ∈ o, f x := supr_option_to_finset o f lemma set_bInter_option_to_finset (o : option α) (f : α → set β) : (⋂ x ∈ o.to_finset, f x) = ⋂ x ∈ o, f x := infi_option_to_finset o f lemma subset_set_bUnion_of_mem {s : finset α} {f : α → set β} {x : α} (h : x ∈ s) : f x ⊆ ⋃ (y ∈ s), f y := show f x ≤ (⨆ y ∈ s, f y), from le_supr_of_le x $ le_supr _ h variables [decidable_eq α] lemma set_bUnion_union (s t : finset α) (u : α → set β) : (⋃ x ∈ s ∪ t, u x) = (⋃ x ∈ s, u x) ∪ (⋃ x ∈ t, u x) := supr_union lemma set_bInter_inter (s t : finset α) (u : α → set β) : (⋂ x ∈ s ∪ t, u x) = (⋂ x ∈ s, u x) ∩ (⋂ x ∈ t, u x) := infi_union lemma set_bUnion_insert (a : α) (s : finset α) (t : α → set β) : (⋃ x ∈ insert a s, t x) = t a ∪ (⋃ x ∈ s, t x) := supr_insert a s t lemma set_bInter_insert (a : α) (s : finset α) (t : α → set β) : (⋂ x ∈ insert a s, t x) = t a ∩ (⋂ x ∈ s, t x) := infi_insert a s t lemma set_bUnion_finset_image {f : γ → α} {g : α → set β} {s : finset γ} : (⋃x ∈ s.image f, g x) = (⋃y ∈ s, g (f y)) := supr_finset_image lemma set_bInter_finset_image {f : γ → α} {g : α → set β} {s : finset γ} : (⋂ x ∈ s.image f, g x) = (⋂ y ∈ s, g (f y)) := infi_finset_image lemma set_bUnion_insert_update {x : α} {t : finset α} (f : α → set β) {s : set β} (hx : x ∉ t) : (⋃ (i ∈ insert x t), @update _ _ _ f x s i) = (s ∪ ⋃ (i ∈ t), f i) := supr_insert_update f hx lemma set_bInter_insert_update {x : α} {t : finset α} (f : α → set β) {s : set β} (hx : x ∉ t) : (⋂ (i ∈ insert x t), @update _ _ _ f x s i) = (s ∩ ⋂ (i ∈ t), f i) := infi_insert_update f hx lemma set_bUnion_bUnion (s : finset γ) (t : γ → finset α) (f : α → set β) : (⋃ y ∈ s.bUnion t, f y) = ⋃ (x ∈ s) (y ∈ t x), f y := supr_bUnion s t f lemma set_bInter_bUnion (s : finset γ) (t : γ → finset α) (f : α → set β) : (⋂ y ∈ s.bUnion t, f y) = ⋂ (x ∈ s) (y ∈ t x), f y := infi_bUnion s t f end finset
07137b23737ae1212fff201b200b586636976093
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/linear_algebra/quadratic_form/basic.lean
bea24f88c4f4daed97ee762ef8aabfbc1546c893
[ "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
37,892
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen, Kexing Ying, Eric Wieser -/ import algebra.invertible import linear_algebra.matrix.determinant import linear_algebra.matrix.bilinear_form import linear_algebra.matrix.symmetric /-! # Quadratic forms This file defines quadratic forms over a `R`-module `M`. A quadratic form on a ring `R` is a map `Q : M → R` such that: * `quadratic_form.map_smul`: `Q (a • x) = a * a * Q x` * `quadratic_form.polar_add_left`, `quadratic_form.polar_add_right`, `quadratic_form.polar_smul_left`, `quadratic_form.polar_smul_right`: the map `quadratic_form.polar Q := λ x y, Q (x + y) - Q x - Q y` is bilinear. This notion generalizes to semirings using the approach in [izhakian2016][] which requires that there be a (possibly non-unique) companion bilinear form `B` such that `∀ x y, Q (x + y) = Q x + Q y + B x y`. Over a ring, this `B` is precisely `quadratic_form.polar Q`. To build a `quadratic_form` from the `polar` axioms, use `quadratic_form.of_polar`. Quadratic forms come with a scalar multiplication, `(a • Q) x = Q (a • x) = a * a * Q x`, and composition with linear maps `f`, `Q.comp f x = Q (f x)`. ## Main definitions * `quadratic_form.of_polar`: a more familiar constructor that works on rings * `quadratic_form.associated`: associated bilinear form * `quadratic_form.pos_def`: positive definite quadratic forms * `quadratic_form.anisotropic`: anisotropic quadratic forms * `quadratic_form.discr`: discriminant of a quadratic form ## Main statements * `quadratic_form.associated_left_inverse`, * `quadratic_form.associated_right_inverse`: in a commutative ring where 2 has an inverse, there is a correspondence between quadratic forms and symmetric bilinear forms * `bilin_form.exists_orthogonal_basis`: There exists an orthogonal basis with respect to any nondegenerate, symmetric bilinear form `B`. ## Notation In this file, the variable `R` is used when a `ring` structure is sufficient and `R₁` is used when specifically a `comm_ring` is required. This allows us to keep `[module R M]` and `[module R₁ M]` assumptions in the variables without confusion between `*` from `ring` and `*` from `comm_ring`. The variable `S` is used when `R` itself has a `•` action. ## References * https://en.wikipedia.org/wiki/Quadratic_form * https://en.wikipedia.org/wiki/Discriminant#Quadratic_forms ## Tags quadratic form, homogeneous polynomial, quadratic polynomial -/ universes u v w variables {S : Type*} variables {R R₁: Type*} {M : Type*} open_locale big_operators section polar variables [ring R] [comm_ring R₁] [add_comm_group M] namespace quadratic_form /-- Up to a factor 2, `Q.polar` is the associated bilinear form for a quadratic form `Q`. Source of this name: https://en.wikipedia.org/wiki/Quadratic_form#Generalization -/ def polar (f : M → R) (x y : M) := f (x + y) - f x - f y lemma polar_add (f g : M → R) (x y : M) : polar (f + g) x y = polar f x y + polar g x y := by { simp only [polar, pi.add_apply], abel } lemma polar_neg (f : M → R) (x y : M) : polar (-f) x y = - polar f x y := by { simp only [polar, pi.neg_apply, sub_eq_add_neg, neg_add] } lemma polar_smul [monoid S] [distrib_mul_action S R] (f : M → R) (s : S) (x y : M) : polar (s • f) x y = s • polar f x y := by { simp only [polar, pi.smul_apply, smul_sub] } lemma polar_comm (f : M → R) (x y : M) : polar f x y = polar f y x := by rw [polar, polar, add_comm, sub_sub, sub_sub, add_comm (f x) (f y)] /-- Auxiliary lemma to express bilinearity of `quadratic_form.polar` without subtraction. -/ lemma polar_add_left_iff {f : M → R} {x x' y : M} : polar f (x + x') y = polar f x y + polar f x' y ↔ f (x + x' + y) + (f x + f x' + f y) = f (x + x') + f (x' + y) + f (y + x) := begin simp only [←add_assoc], simp only [polar, sub_eq_iff_eq_add, eq_sub_iff_add_eq, sub_add_eq_add_sub, add_sub], simp only [add_right_comm _ (f y) _, add_right_comm _ (f x') (f x)], rw [add_comm y x, add_right_comm _ _ (f (x + y)), add_comm _ (f (x + y)), add_right_comm (f (x + y)), add_left_inj], end lemma polar_comp {F : Type*} [ring S] [add_monoid_hom_class F R S] (f : M → R) (g : F) (x y : M) : polar (g ∘ f) x y = g (polar f x y) := by simp only [polar, pi.smul_apply, function.comp_apply, map_sub] end quadratic_form end polar /-- A quadratic form over a module. For a more familiar constructor when `R` is a ring, see `quadratic_form.of_polar`. -/ structure quadratic_form (R : Type u) (M : Type v) [semiring R] [add_comm_monoid M] [module R M] := (to_fun : M → R) (to_fun_smul : ∀ (a : R) (x : M), to_fun (a • x) = a * a * to_fun x) (exists_companion' : ∃ B : bilin_form R M, ∀ x y, to_fun (x + y) = to_fun x + to_fun y + B x y) namespace quadratic_form section fun_like variables [semiring R] [add_comm_monoid M] [module R M] variables {Q Q' : quadratic_form R M} instance fun_like : fun_like (quadratic_form R M) M (λ _, R) := { coe := to_fun, coe_injective' := λ x y h, by cases x; cases y; congr' } /-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ instance : has_coe_to_fun (quadratic_form R M) (λ _, M → R) := ⟨to_fun⟩ variables (Q) /-- The `simp` normal form for a quadratic form is `coe_fn`, not `to_fun`. -/ @[simp] lemma to_fun_eq_coe : Q.to_fun = ⇑Q := rfl -- this must come after the coe_to_fun definition initialize_simps_projections quadratic_form (to_fun → apply) variables {Q} @[ext] lemma ext (H : ∀ (x : M), Q x = Q' x) : Q = Q' := fun_like.ext _ _ H lemma congr_fun (h : Q = Q') (x : M) : Q x = Q' x := fun_like.congr_fun h _ lemma ext_iff : Q = Q' ↔ (∀ x, Q x = Q' x) := fun_like.ext_iff /-- Copy of a `quadratic_form` with a new `to_fun` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (Q : quadratic_form R M) (Q' : M → R) (h : Q' = ⇑Q) : quadratic_form R M := { to_fun := Q', to_fun_smul := h.symm ▸ Q.to_fun_smul, exists_companion' := h.symm ▸ Q.exists_companion' } @[simp] lemma coe_copy (Q : quadratic_form R M) (Q' : M → R) (h : Q' = ⇑Q) : ⇑(Q.copy Q' h) = Q' := rfl lemma copy_eq (Q : quadratic_form R M) (Q' : M → R) (h : Q' = ⇑Q) : Q.copy Q' h = Q := fun_like.ext' h end fun_like section semiring variables [semiring R] [add_comm_monoid M] [module R M] variables (Q : quadratic_form R M) lemma map_smul (a : R) (x : M) : Q (a • x) = a * a * Q x := Q.to_fun_smul a x lemma exists_companion : ∃ B : bilin_form R M, ∀ x y, Q (x + y) = Q x + Q y + B x y := Q.exists_companion' lemma map_add_add_add_map (x y z : M) : Q (x + y + z) + (Q x + Q y + Q z) = Q (x + y) + Q (y + z) + Q (z + x) := begin obtain ⟨B, h⟩ := Q.exists_companion, rw [add_comm z x], simp [h], abel, end lemma map_add_self (x : M) : Q (x + x) = 4 * Q x := by { rw [←one_smul R x, ←add_smul, map_smul], norm_num } @[simp] lemma map_zero : Q 0 = 0 := by rw [←@zero_smul R _ _ _ _ (0 : M), map_smul, zero_mul, zero_mul] instance zero_hom_class : zero_hom_class (quadratic_form R M) M R := { map_zero := map_zero, ..quadratic_form.fun_like } lemma map_smul_of_tower [comm_semiring S] [algebra S R] [module S M] [is_scalar_tower S R M] (a : S) (x : M) : Q (a • x) = (a * a) • Q x := by rw [←is_scalar_tower.algebra_map_smul R a x, map_smul, ←ring_hom.map_mul, algebra.smul_def] end semiring section ring variables [ring R] [comm_ring R₁] [add_comm_group M] variables [module R M] (Q : quadratic_form R M) @[simp] lemma map_neg (x : M) : Q (-x) = Q x := by rw [←@neg_one_smul R _ _ _ _ x, map_smul, neg_one_mul, neg_neg, one_mul] lemma map_sub (x y : M) : Q (x - y) = Q (y - x) := by rw [←neg_sub, map_neg] @[simp] lemma polar_zero_left (y : M) : polar Q 0 y = 0 := by simp only [polar, zero_add, quadratic_form.map_zero, sub_zero, sub_self] @[simp] lemma polar_add_left (x x' y : M) : polar Q (x + x') y = polar Q x y + polar Q x' y := polar_add_left_iff.mpr $ Q.map_add_add_add_map x x' y @[simp] lemma polar_smul_left (a : R) (x y : M) : polar Q (a • x) y = a * polar Q x y := begin obtain ⟨B, h⟩ := Q.exists_companion, simp_rw [polar, h, Q.map_smul, bilin_form.smul_left, sub_sub, add_sub_cancel'], end @[simp] lemma polar_neg_left (x y : M) : polar Q (-x) y = -polar Q x y := by rw [←neg_one_smul R x, polar_smul_left, neg_one_mul] @[simp] lemma polar_sub_left (x x' y : M) : polar Q (x - x') y = polar Q x y - polar Q x' y := by rw [sub_eq_add_neg, sub_eq_add_neg, polar_add_left, polar_neg_left] @[simp] lemma polar_zero_right (y : M) : polar Q y 0 = 0 := by simp only [add_zero, polar, quadratic_form.map_zero, sub_self] @[simp] lemma polar_add_right (x y y' : M) : polar Q x (y + y') = polar Q x y + polar Q x y' := by rw [polar_comm Q x, polar_comm Q x, polar_comm Q x, polar_add_left] @[simp] lemma polar_smul_right (a : R) (x y : M) : polar Q x (a • y) = a * polar Q x y := by rw [polar_comm Q x, polar_comm Q x, polar_smul_left] @[simp] lemma polar_neg_right (x y : M) : polar Q x (-y) = -polar Q x y := by rw [←neg_one_smul R y, polar_smul_right, neg_one_mul] @[simp] lemma polar_sub_right (x y y' : M) : polar Q x (y - y') = polar Q x y - polar Q x y' := by rw [sub_eq_add_neg, sub_eq_add_neg, polar_add_right, polar_neg_right] @[simp] lemma polar_self (x : M) : polar Q x x = 2 * Q x := begin rw [polar, map_add_self, sub_sub, sub_eq_iff_eq_add, ←two_mul, ←two_mul, ←mul_assoc], norm_num end /-- `quadratic_form.polar` as a bilinear form -/ @[simps] def polar_bilin : bilin_form R M := { bilin := polar Q, bilin_add_left := polar_add_left Q, bilin_smul_left := polar_smul_left Q, bilin_add_right := λ x y z, by simp_rw [polar_comm _ x, polar_add_left Q], bilin_smul_right := λ r x y, by simp_rw [polar_comm _ x, polar_smul_left Q] } variables [comm_semiring S] [algebra S R] [module S M] [is_scalar_tower S R M] @[simp] lemma polar_smul_left_of_tower (a : S) (x y : M) : polar Q (a • x) y = a • polar Q x y := by rw [←is_scalar_tower.algebra_map_smul R a x, polar_smul_left, algebra.smul_def] @[simp] lemma polar_smul_right_of_tower (a : S) (x y : M) : polar Q x (a • y) = a • polar Q x y := by rw [←is_scalar_tower.algebra_map_smul R a y, polar_smul_right, algebra.smul_def] /-- An alternative constructor to `quadratic_form.mk`, for rings where `polar` can be used. -/ @[simps] def of_polar (to_fun : M → R) (to_fun_smul : ∀ (a : R) (x : M), to_fun (a • x) = a * a * to_fun x) (polar_add_left : ∀ (x x' y : M), polar to_fun (x + x') y = polar to_fun x y + polar to_fun x' y) (polar_smul_left : ∀ (a : R) (x y : M), polar to_fun (a • x) y = a • polar to_fun x y) : quadratic_form R M := { to_fun := to_fun, to_fun_smul := to_fun_smul, exists_companion' := ⟨ { bilin := polar to_fun, bilin_add_left := polar_add_left, bilin_smul_left := polar_smul_left, bilin_add_right := λ x y z, by simp_rw [polar_comm _ x, polar_add_left], bilin_smul_right := λ r x y, by simp_rw [polar_comm _ x, polar_smul_left, smul_eq_mul] }, λ x y, by rw [bilin_form.coe_fn_mk, polar, sub_sub, add_sub_cancel'_right]⟩ } /-- In a ring the companion bilinear form is unique and equal to `quadratic_form.polar`. -/ lemma some_exists_companion : Q.exists_companion.some = polar_bilin Q := bilin_form.ext $ λ x y, by rw [polar_bilin_apply, polar, Q.exists_companion.some_spec, sub_sub, add_sub_cancel'] end ring section semiring_operators variables [semiring R] [add_comm_monoid M] [module R M] section has_smul variables [monoid S] [distrib_mul_action S R] [smul_comm_class S R R] /-- `quadratic_form R M` inherits the scalar action from any algebra over `R`. When `R` is commutative, this provides an `R`-action via `algebra.id`. -/ instance : has_smul S (quadratic_form R M) := ⟨ λ a Q, { to_fun := a • Q, to_fun_smul := λ b x, by rw [pi.smul_apply, map_smul, pi.smul_apply, mul_smul_comm], exists_companion' := let ⟨B, h⟩ := Q.exists_companion in ⟨a • B, by simp [h]⟩ } ⟩ @[simp] lemma coe_fn_smul (a : S) (Q : quadratic_form R M) : ⇑(a • Q) = a • Q := rfl @[simp] lemma smul_apply (a : S) (Q : quadratic_form R M) (x : M) : (a • Q) x = a • Q x := rfl end has_smul instance : has_zero (quadratic_form R M) := ⟨ { to_fun := λ x, 0, to_fun_smul := λ a x, by simp only [mul_zero], exists_companion' := ⟨0, λ x y, by simp only [add_zero, bilin_form.zero_apply]⟩ } ⟩ @[simp] lemma coe_fn_zero : ⇑(0 : quadratic_form R M) = 0 := rfl @[simp] lemma zero_apply (x : M) : (0 : quadratic_form R M) x = 0 := rfl instance : inhabited (quadratic_form R M) := ⟨0⟩ instance : has_add (quadratic_form R M) := ⟨ λ Q Q', { to_fun := Q + Q', to_fun_smul := λ a x, by simp only [pi.add_apply, map_smul, mul_add], exists_companion' := let ⟨B, h⟩ := Q.exists_companion, ⟨B', h'⟩ := Q'.exists_companion in ⟨B + B', λ x y, by simp_rw [pi.add_apply, h, h', bilin_form.add_apply, add_add_add_comm] ⟩ } ⟩ @[simp] lemma coe_fn_add (Q Q' : quadratic_form R M) : ⇑(Q + Q') = Q + Q' := rfl @[simp] lemma add_apply (Q Q' : quadratic_form R M) (x : M) : (Q + Q') x = Q x + Q' x := rfl instance : add_comm_monoid (quadratic_form R M) := fun_like.coe_injective.add_comm_monoid _ coe_fn_zero coe_fn_add (λ _ _, coe_fn_smul _ _) /-- `@coe_fn (quadratic_form R M)` as an `add_monoid_hom`. This API mirrors `add_monoid_hom.coe_fn`. -/ @[simps apply] def coe_fn_add_monoid_hom : quadratic_form R M →+ (M → R) := { to_fun := coe_fn, map_zero' := coe_fn_zero, map_add' := coe_fn_add } /-- Evaluation on a particular element of the module `M` is an additive map over quadratic forms. -/ @[simps apply] def eval_add_monoid_hom (m : M) : quadratic_form R M →+ R := (pi.eval_add_monoid_hom _ m).comp coe_fn_add_monoid_hom section sum @[simp] lemma coe_fn_sum {ι : Type*} (Q : ι → quadratic_form R M) (s : finset ι) : ⇑(∑ i in s, Q i) = ∑ i in s, Q i := (coe_fn_add_monoid_hom : _ →+ (M → R)).map_sum Q s @[simp] lemma sum_apply {ι : Type*} (Q : ι → quadratic_form R M) (s : finset ι) (x : M) : (∑ i in s, Q i) x = ∑ i in s, Q i x := (eval_add_monoid_hom x : _ →+ R).map_sum Q s end sum instance [monoid S] [distrib_mul_action S R] [smul_comm_class S R R] : distrib_mul_action S (quadratic_form R M) := { mul_smul := λ a b Q, ext (λ x, by simp only [smul_apply, mul_smul]), one_smul := λ Q, ext (λ x, by simp only [quadratic_form.smul_apply, one_smul]), smul_add := λ a Q Q', by { ext, simp only [add_apply, smul_apply, smul_add] }, smul_zero := λ a, by { ext, simp only [zero_apply, smul_apply, smul_zero] }, } instance [semiring S] [module S R] [smul_comm_class S R R] : module S (quadratic_form R M) := { zero_smul := λ Q, by { ext, simp only [zero_apply, smul_apply, zero_smul] }, add_smul := λ a b Q, by { ext, simp only [add_apply, smul_apply, add_smul] } } end semiring_operators section ring_operators variables [ring R] [add_comm_group M] [module R M] instance : has_neg (quadratic_form R M) := ⟨ λ Q, { to_fun := -Q, to_fun_smul := λ a x, by simp only [pi.neg_apply, map_smul, mul_neg], exists_companion' := let ⟨B, h⟩ := Q.exists_companion in ⟨-B, λ x y, by simp_rw [pi.neg_apply, h, bilin_form.neg_apply, neg_add] ⟩ } ⟩ @[simp] lemma coe_fn_neg (Q : quadratic_form R M) : ⇑(-Q) = -Q := rfl @[simp] lemma neg_apply (Q : quadratic_form R M) (x : M) : (-Q) x = -Q x := rfl instance : has_sub (quadratic_form R M) := ⟨ λ Q Q', (Q + -Q').copy (Q - Q') (sub_eq_add_neg _ _) ⟩ @[simp] lemma coe_fn_sub (Q Q' : quadratic_form R M) : ⇑(Q - Q') = Q - Q' := rfl @[simp] lemma sub_apply (Q Q' : quadratic_form R M) (x : M) : (Q - Q') x = Q x - Q' x := rfl instance : add_comm_group (quadratic_form R M) := fun_like.coe_injective.add_comm_group _ coe_fn_zero coe_fn_add coe_fn_neg coe_fn_sub (λ _ _, coe_fn_smul _ _) (λ _ _, coe_fn_smul _ _) end ring_operators section comp variables [semiring R] [add_comm_monoid M] [module R M] variables {N : Type v} [add_comm_monoid N] [module R N] /-- Compose the quadratic form with a linear function. -/ def comp (Q : quadratic_form R N) (f : M →ₗ[R] N) : quadratic_form R M := { to_fun := λ x, Q (f x), to_fun_smul := λ a x, by simp only [map_smul, f.map_smul], exists_companion' := let ⟨B, h⟩ := Q.exists_companion in ⟨B.comp f f, λ x y, by simp_rw [f.map_add, h, bilin_form.comp_apply]⟩ } @[simp] lemma comp_apply (Q : quadratic_form R N) (f : M →ₗ[R] N) (x : M) : (Q.comp f) x = Q (f x) := rfl /-- Compose a quadratic form with a linear function on the left. -/ @[simps {simp_rhs := tt}] def _root_.linear_map.comp_quadratic_form {S : Type*} [comm_semiring S] [algebra S R] [module S M] [is_scalar_tower S R M] (f : R →ₗ[S] S) (Q : quadratic_form R M) : quadratic_form S M := { to_fun := λ x, f (Q x), to_fun_smul := λ b x, by rw [Q.map_smul_of_tower b x, f.map_smul, smul_eq_mul], exists_companion' := let ⟨B, h⟩ := Q.exists_companion in ⟨f.comp_bilin_form B, λ x y, by simp_rw [h, f.map_add, linear_map.comp_bilin_form_apply]⟩ } end comp section comm_ring variables [comm_semiring R] [add_comm_monoid M] [module R M] /-- The product of linear forms is a quadratic form. -/ def lin_mul_lin (f g : M →ₗ[R] R) : quadratic_form R M := { to_fun := f * g, to_fun_smul := λ a x, by { simp only [smul_eq_mul, ring_hom.id_apply, pi.mul_apply, linear_map.map_smulₛₗ], ring }, exists_companion' := ⟨ bilin_form.lin_mul_lin f g + bilin_form.lin_mul_lin g f, λ x y, by { simp, ring }⟩ } @[simp] lemma lin_mul_lin_apply (f g : M →ₗ[R] R) (x) : lin_mul_lin f g x = f x * g x := rfl @[simp] lemma add_lin_mul_lin (f g h : M →ₗ[R] R) : lin_mul_lin (f + g) h = lin_mul_lin f h + lin_mul_lin g h := ext (λ x, add_mul _ _ _) @[simp] lemma lin_mul_lin_add (f g h : M →ₗ[R] R) : lin_mul_lin f (g + h) = lin_mul_lin f g + lin_mul_lin f h := ext (λ x, mul_add _ _ _) variables {N : Type v} [add_comm_monoid N] [module R N] @[simp] lemma lin_mul_lin_comp (f g : M →ₗ[R] R) (h : N →ₗ[R] M) : (lin_mul_lin f g).comp h = lin_mul_lin (f.comp h) (g.comp h) := rfl variables {n : Type*} /-- `sq` is the quadratic form mapping the vector `x : R₁` to `x * x` -/ @[simps] def sq : quadratic_form R R := lin_mul_lin linear_map.id linear_map.id /-- `proj i j` is the quadratic form mapping the vector `x : n → R₁` to `x i * x j` -/ def proj (i j : n) : quadratic_form R (n → R) := lin_mul_lin (@linear_map.proj _ _ _ (λ _, R) _ _ i) (@linear_map.proj _ _ _ (λ _, R) _ _ j) @[simp] lemma proj_apply (i j : n) (x : n → R) : proj i j x = x i * x j := rfl end comm_ring end quadratic_form /-! ### Associated bilinear forms Over a commutative ring with an inverse of 2, the theory of quadratic forms is basically identical to that of symmetric bilinear forms. The map from quadratic forms to bilinear forms giving this identification is called the `associated` quadratic form. -/ namespace bilin_form open quadratic_form section semiring variables [semiring R] [add_comm_monoid M] [module R M] variables {B : bilin_form R M} /-- A bilinear form gives a quadratic form by applying the argument twice. -/ def to_quadratic_form (B : bilin_form R M) : quadratic_form R M := { to_fun := λ x, B x x, to_fun_smul := λ a x, by simp only [mul_assoc, smul_right, smul_left], exists_companion' := ⟨B + bilin_form.flip_hom ℕ B, λ x y, by { simp [add_add_add_comm, add_comm] }⟩ } @[simp] lemma to_quadratic_form_apply (B : bilin_form R M) (x : M) : B.to_quadratic_form x = B x x := rfl section variables (R M) @[simp] lemma to_quadratic_form_zero : (0 : bilin_form R M).to_quadratic_form = 0 := rfl end @[simp] lemma to_quadratic_form_add (B₁ B₂ : bilin_form R M) : (B₁ + B₂).to_quadratic_form = B₁.to_quadratic_form + B₂.to_quadratic_form := rfl @[simp] lemma to_quadratic_form_smul [monoid S] [distrib_mul_action S R] [smul_comm_class S R R] (a : S) (B : bilin_form R M) : (a • B).to_quadratic_form = a • B.to_quadratic_form := rfl section variables (R M) /-- `bilin_form.to_quadratic_form` as an additive homomorphism -/ @[simps] def to_quadratic_form_add_monoid_hom : bilin_form R M →+ quadratic_form R M := { to_fun := to_quadratic_form, map_zero' := to_quadratic_form_zero _ _, map_add' := to_quadratic_form_add } end @[simp] lemma to_quadratic_form_list_sum (B : list (bilin_form R M)) : B.sum.to_quadratic_form = (B.map to_quadratic_form).sum := map_list_sum (to_quadratic_form_add_monoid_hom R M) B @[simp] lemma to_quadratic_form_multiset_sum (B : multiset (bilin_form R M)) : B.sum.to_quadratic_form = (B.map to_quadratic_form).sum := map_multiset_sum (to_quadratic_form_add_monoid_hom R M) B @[simp] lemma to_quadratic_form_sum {ι : Type*} (s : finset ι) (B : ι → bilin_form R M) : (∑ i in s, B i).to_quadratic_form = ∑ i in s, (B i).to_quadratic_form := map_sum (to_quadratic_form_add_monoid_hom R M) B s end semiring section ring variables [ring R] [add_comm_group M] [module R M] variables {B : bilin_form R M} lemma polar_to_quadratic_form (x y : M) : polar (λ x, B x x) x y = B x y + B y x := by { simp only [add_assoc, add_sub_cancel', add_right, polar, add_left_inj, add_neg_cancel_left, add_left, sub_eq_add_neg _ (B y y), add_comm (B y x) _] } @[simp] lemma to_quadratic_form_neg (B : bilin_form R M) : (-B).to_quadratic_form = -B.to_quadratic_form := rfl @[simp] lemma to_quadratic_form_sub (B₁ B₂ : bilin_form R M) : (B₁ - B₂).to_quadratic_form = B₁.to_quadratic_form - B₂.to_quadratic_form := rfl end ring end bilin_form namespace quadratic_form open bilin_form section associated_hom variables [ring R] [comm_ring R₁] [add_comm_group M] [module R M] [module R₁ M] variables (S) [comm_semiring S] [algebra S R] variables [invertible (2 : R)] {B₁ : bilin_form R M} /-- `associated_hom` is the map that sends a quadratic form on a module `M` over `R` to its associated symmetric bilinear form. As provided here, this has the structure of an `S`-linear map where `S` is a commutative subring of `R`. Over a commutative ring, use `associated`, which gives an `R`-linear map. Over a general ring with no nontrivial distinguished commutative subring, use `associated'`, which gives an additive homomorphism (or more precisely a `ℤ`-linear map.) -/ def associated_hom : quadratic_form R M →ₗ[S] bilin_form R M := { to_fun := λ Q, ((•) : submonoid.center R → bilin_form R M → bilin_form R M) (⟨⅟2, λ x, (commute.one_right x).bit0_right.inv_of_right⟩) Q.polar_bilin, map_add' := λ Q Q', by { ext, simp only [bilin_form.add_apply, bilin_form.smul_apply, coe_fn_mk, polar_bilin_apply, polar_add, coe_fn_add, smul_add] }, map_smul' := λ s Q, by { ext, simp only [ring_hom.id_apply, polar_smul, smul_comm s, polar_bilin_apply, coe_fn_mk, coe_fn_smul, bilin_form.smul_apply] } } variables (Q : quadratic_form R M) (S) @[simp] lemma associated_apply (x y : M) : associated_hom S Q x y = ⅟2 * (Q (x + y) - Q x - Q y) := rfl lemma associated_is_symm : (associated_hom S Q).is_symm := λ x y, by simp only [associated_apply, add_comm, add_left_comm, sub_eq_add_neg] @[simp] lemma associated_comp {N : Type v} [add_comm_group N] [module R N] (f : N →ₗ[R] M) : associated_hom S (Q.comp f) = (associated_hom S Q).comp f f := by { ext, simp only [quadratic_form.comp_apply, bilin_form.comp_apply, associated_apply, linear_map.map_add] } lemma associated_to_quadratic_form (B : bilin_form R M) (x y : M) : associated_hom S B.to_quadratic_form x y = ⅟2 * (B x y + B y x) := by simp only [associated_apply, ← polar_to_quadratic_form, polar, to_quadratic_form_apply] lemma associated_left_inverse (h : B₁.is_symm) : associated_hom S (B₁.to_quadratic_form) = B₁ := bilin_form.ext $ λ x y, by rw [associated_to_quadratic_form, is_symm.eq h x y, ←two_mul, ←mul_assoc, inv_of_mul_self, one_mul] lemma to_quadratic_form_associated : (associated_hom S Q).to_quadratic_form = Q := quadratic_form.ext $ λ x, calc (associated_hom S Q).to_quadratic_form x = ⅟2 * (Q x + Q x) : by simp only [add_assoc, add_sub_cancel', one_mul, to_quadratic_form_apply, add_mul, associated_apply, map_add_self, bit0] ... = Q x : by rw [← two_mul (Q x), ←mul_assoc, inv_of_mul_self, one_mul] -- note: usually `right_inverse` lemmas are named the other way around, but this is consistent -- with historical naming in this file. lemma associated_right_inverse : function.right_inverse (associated_hom S) (bilin_form.to_quadratic_form : _ → quadratic_form R M) := λ Q, to_quadratic_form_associated S Q lemma associated_eq_self_apply (x : M) : associated_hom S Q x x = Q x := begin rw [associated_apply, map_add_self], suffices : (⅟2) * (2 * Q x) = Q x, { convert this, simp only [bit0, add_mul, one_mul], abel }, simp only [← mul_assoc, one_mul, inv_of_mul_self], end /-- `associated'` is the `ℤ`-linear map that sends a quadratic form on a module `M` over `R` to its associated symmetric bilinear form. -/ abbreviation associated' : quadratic_form R M →ₗ[ℤ] bilin_form R M := associated_hom ℤ /-- Symmetric bilinear forms can be lifted to quadratic forms -/ instance can_lift : can_lift (bilin_form R M) (quadratic_form R M) (associated_hom ℕ) bilin_form.is_symm := { prf := λ B hB, ⟨B.to_quadratic_form, associated_left_inverse _ hB⟩ } /-- There exists a non-null vector with respect to any quadratic form `Q` whose associated bilinear form is non-zero, i.e. there exists `x` such that `Q x ≠ 0`. -/ lemma exists_quadratic_form_ne_zero {Q : quadratic_form R M} (hB₁ : Q.associated' ≠ 0) : ∃ x, Q x ≠ 0 := begin rw ←not_forall, intro h, apply hB₁, rw [(quadratic_form.ext h : Q = 0), linear_map.map_zero], end end associated_hom section associated variables [comm_ring R₁] [add_comm_group M] [module R₁ M] variables [invertible (2 : R₁)] -- Note: When possible, rather than writing lemmas about `associated`, write a lemma applying to -- the more general `associated_hom` and place it in the previous section. /-- `associated` is the linear map that sends a quadratic form over a commutative ring to its associated symmetric bilinear form. -/ abbreviation associated : quadratic_form R₁ M →ₗ[R₁] bilin_form R₁ M := associated_hom R₁ @[simp] lemma associated_lin_mul_lin (f g : M →ₗ[R₁] R₁) : (lin_mul_lin f g).associated = ⅟(2 : R₁) • (bilin_form.lin_mul_lin f g + bilin_form.lin_mul_lin g f) := by { ext, simp only [smul_add, algebra.id.smul_eq_mul, bilin_form.lin_mul_lin_apply, quadratic_form.lin_mul_lin_apply, bilin_form.smul_apply, associated_apply, bilin_form.add_apply, linear_map.map_add], ring } end associated section anisotropic section semiring variables [semiring R] [add_comm_monoid M] [module R M] /-- An anisotropic quadratic form is zero only on zero vectors. -/ def anisotropic (Q : quadratic_form R M) : Prop := ∀ x, Q x = 0 → x = 0 lemma not_anisotropic_iff_exists (Q : quadratic_form R M) : ¬anisotropic Q ↔ ∃ x ≠ 0, Q x = 0 := by simp only [anisotropic, not_forall, exists_prop, and_comm] lemma anisotropic.eq_zero_iff {Q : quadratic_form R M} (h : anisotropic Q) {x : M} : Q x = 0 ↔ x = 0 := ⟨h x, λ h, h.symm ▸ map_zero Q⟩ end semiring section ring variables [ring R] [add_comm_group M] [module R M] /-- The associated bilinear form of an anisotropic quadratic form is nondegenerate. -/ lemma nondegenerate_of_anisotropic [invertible (2 : R)] (Q : quadratic_form R M) (hB : Q.anisotropic) : Q.associated'.nondegenerate := begin intros x hx, refine hB _ _, rw ← hx x, exact (associated_eq_self_apply _ _ x).symm, end end ring end anisotropic section pos_def variables {R₂ : Type u} [ordered_ring R₂] [add_comm_monoid M] [module R₂ M] variables {Q₂ : quadratic_form R₂ M} /-- A positive definite quadratic form is positive on nonzero vectors. -/ def pos_def (Q₂ : quadratic_form R₂ M) : Prop := ∀ x ≠ 0, 0 < Q₂ x lemma pos_def.smul {R} [linear_ordered_comm_ring R] [module R M] {Q : quadratic_form R M} (h : pos_def Q) {a : R} (a_pos : 0 < a) : pos_def (a • Q) := λ x hx, mul_pos a_pos (h x hx) variables {n : Type*} lemma pos_def.nonneg {Q : quadratic_form R₂ M} (hQ : pos_def Q) (x : M) : 0 ≤ Q x := (eq_or_ne x 0).elim (λ h, h.symm ▸ (map_zero Q).symm.le) (λ h, (hQ _ h).le) lemma pos_def.anisotropic {Q : quadratic_form R₂ M} (hQ : Q.pos_def) : Q.anisotropic := λ x hQx, classical.by_contradiction $ λ hx, lt_irrefl (0 : R₂) $ begin have := hQ _ hx, rw hQx at this, exact this, end lemma pos_def_of_nonneg {Q : quadratic_form R₂ M} (h : ∀ x, 0 ≤ Q x) (h0 : Q.anisotropic) : pos_def Q := λ x hx, lt_of_le_of_ne (h x) (ne.symm $ λ hQx, hx $ h0 _ hQx) lemma pos_def_iff_nonneg {Q : quadratic_form R₂ M} : pos_def Q ↔ (∀ x, 0 ≤ Q x) ∧ Q.anisotropic := ⟨λ h, ⟨h.nonneg, h.anisotropic⟩, λ ⟨n, a⟩, pos_def_of_nonneg n a⟩ lemma pos_def.add (Q Q' : quadratic_form R₂ M) (hQ : pos_def Q) (hQ' : pos_def Q') : pos_def (Q + Q') := λ x hx, add_pos (hQ x hx) (hQ' x hx) lemma lin_mul_lin_self_pos_def {R} [linear_ordered_comm_ring R] [module R M] (f : M →ₗ[R] R) (hf : linear_map.ker f = ⊥) : pos_def (lin_mul_lin f f) := λ x hx, mul_self_pos.2 (λ h, hx $ linear_map.ker_eq_bot'.mp hf _ h) end pos_def end quadratic_form section /-! ### Quadratic forms and matrices Connect quadratic forms and matrices, in order to explicitly compute with them. The convention is twos out, so there might be a factor 2⁻¹ in the entries of the matrix. The determinant of the matrix is the discriminant of the quadratic form. -/ variables {n : Type w} [fintype n] [decidable_eq n] variables [comm_ring R₁] [add_comm_monoid M] [module R₁ M] /-- `M.to_quadratic_form` is the map `λ x, col x ⬝ M ⬝ row x` as a quadratic form. -/ def matrix.to_quadratic_form' (M : matrix n n R₁) : quadratic_form R₁ (n → R₁) := M.to_bilin'.to_quadratic_form variables [invertible (2 : R₁)] /-- A matrix representation of the quadratic form. -/ def quadratic_form.to_matrix' (Q : quadratic_form R₁ (n → R₁)) : matrix n n R₁ := Q.associated.to_matrix' open quadratic_form lemma quadratic_form.to_matrix'_smul (a : R₁) (Q : quadratic_form R₁ (n → R₁)) : (a • Q).to_matrix' = a • Q.to_matrix' := by simp only [to_matrix', linear_equiv.map_smul, linear_map.map_smul] lemma quadratic_form.is_symm_to_matrix' (Q : quadratic_form R₁ (n → R₁)) : Q.to_matrix'.is_symm := begin ext i j, rw [to_matrix', bilin_form.to_matrix'_apply, bilin_form.to_matrix'_apply, associated_is_symm] end end namespace quadratic_form variables {n : Type w} [fintype n] variables [comm_ring R₁] [decidable_eq n] [invertible (2 : R₁)] variables {m : Type w} [decidable_eq m] [fintype m] open_locale matrix @[simp] lemma to_matrix'_comp (Q : quadratic_form R₁ (m → R₁)) (f : (n → R₁) →ₗ[R₁] (m → R₁)) : (Q.comp f).to_matrix' = f.to_matrix'ᵀ ⬝ Q.to_matrix' ⬝ f.to_matrix' := by { ext, simp only [quadratic_form.associated_comp, bilin_form.to_matrix'_comp, to_matrix'] } section discriminant variables {Q : quadratic_form R₁ (n → R₁)} /-- The discriminant of a quadratic form generalizes the discriminant of a quadratic polynomial. -/ def discr (Q : quadratic_form R₁ (n → R₁)) : R₁ := Q.to_matrix'.det lemma discr_smul (a : R₁) : (a • Q).discr = a ^ fintype.card n * Q.discr := by simp only [discr, to_matrix'_smul, matrix.det_smul] lemma discr_comp (f : (n → R₁) →ₗ[R₁] (n → R₁)) : (Q.comp f).discr = f.to_matrix'.det * f.to_matrix'.det * Q.discr := by simp only [matrix.det_transpose, mul_left_comm, quadratic_form.to_matrix'_comp, mul_comm, matrix.det_mul, discr] end discriminant end quadratic_form namespace quadratic_form end quadratic_form namespace bilin_form section semiring variables [semiring R] [add_comm_monoid M] [module R M] /-- A bilinear form is nondegenerate if the quadratic form it is associated with is anisotropic. -/ lemma nondegenerate_of_anisotropic {B : bilin_form R M} (hB : B.to_quadratic_form.anisotropic) : B.nondegenerate := λ x hx, hB _ (hx x) end semiring variables [ring R] [add_comm_group M] [module R M] /-- There exists a non-null vector with respect to any symmetric, nonzero bilinear form `B` on a module `M` over a ring `R` with invertible `2`, i.e. there exists some `x : M` such that `B x x ≠ 0`. -/ lemma exists_bilin_form_self_ne_zero [htwo : invertible (2 : R)] {B : bilin_form R M} (hB₁ : B ≠ 0) (hB₂ : B.is_symm) : ∃ x, ¬ B.is_ortho x x := begin lift B to quadratic_form R M using hB₂ with Q, obtain ⟨x, hx⟩ := quadratic_form.exists_quadratic_form_ne_zero hB₁, exact ⟨x, λ h, hx (Q.associated_eq_self_apply ℕ x ▸ h)⟩, end open finite_dimensional variables {V : Type u} {K : Type v} [field K] [add_comm_group V] [module K V] variable [finite_dimensional K V] /-- Given a symmetric bilinear form `B` on some vector space `V` over a field `K` in which `2` is invertible, there exists an orthogonal basis with respect to `B`. -/ lemma exists_orthogonal_basis [hK : invertible (2 : K)] {B : bilin_form K V} (hB₂ : B.is_symm) : ∃ (v : basis (fin (finrank K V)) K V), B.is_Ortho v := begin unfreezingI { induction hd : finrank K V with d ih generalizing V }, { exact ⟨basis_of_finrank_zero hd, λ _ _ _, zero_left _⟩ }, haveI := finrank_pos_iff.1 (hd.symm ▸ nat.succ_pos d : 0 < finrank K V), -- either the bilinear form is trivial or we can pick a non-null `x` obtain rfl | hB₁ := eq_or_ne B 0, { let b := finite_dimensional.fin_basis K V, rw hd at b, refine ⟨b, λ i j hij, rfl⟩, }, obtain ⟨x, hx⟩ := exists_bilin_form_self_ne_zero hB₁ hB₂, rw [← submodule.finrank_add_eq_of_is_compl (is_compl_span_singleton_orthogonal hx).symm, finrank_span_singleton (ne_zero_of_not_is_ortho_self x hx)] at hd, let B' := B.restrict (B.orthogonal $ K ∙ x), obtain ⟨v', hv₁⟩ := ih (B.restrict_symm hB₂ _ : B'.is_symm) (nat.succ.inj hd), -- concatenate `x` with the basis obtained by induction let b := basis.mk_fin_cons x v' (begin rintros c y hy hc, rw add_eq_zero_iff_neg_eq at hc, rw [← hc, submodule.neg_mem_iff] at hy, have := (is_compl_span_singleton_orthogonal hx).disjoint, rw submodule.disjoint_def at this, have := this (c • x) (submodule.smul_mem _ _ $ submodule.mem_span_singleton_self _) hy, exact (smul_eq_zero.1 this).resolve_right (λ h, hx $ h.symm ▸ zero_left _), end) (begin intro y, refine ⟨-B x y/B x x, λ z hz, _⟩, obtain ⟨c, rfl⟩ := submodule.mem_span_singleton.1 hz, rw [is_ortho, smul_left, add_right, smul_right, div_mul_cancel _ hx, add_neg_self, mul_zero], end), refine ⟨b, _⟩, { rw basis.coe_mk_fin_cons, intros j i, refine fin.cases _ (λ i, _) i; refine fin.cases _ (λ j, _) j; intro hij; simp only [function.on_fun, fin.cons_zero, fin.cons_succ, function.comp_apply], { exact (hij rfl).elim }, { rw [is_ortho, hB₂], exact (v' j).prop _ (submodule.mem_span_singleton_self x) }, { exact (v' i).prop _ (submodule.mem_span_singleton_self x) }, { exact hv₁ (ne_of_apply_ne _ hij), }, } end end bilin_form namespace quadratic_form open finset bilin_form variables {M₁ : Type*} [semiring R] [comm_semiring R₁] [add_comm_monoid M] [add_comm_monoid M₁] variables [module R M] [module R M₁] variables {ι : Type*} [fintype ι] {v : basis ι R M} /-- Given a quadratic form `Q` and a basis, `basis_repr` is the basis representation of `Q`. -/ noncomputable def basis_repr (Q : quadratic_form R M) (v : basis ι R M) : quadratic_form R (ι → R) := Q.comp v.equiv_fun.symm @[simp] lemma basis_repr_apply (Q : quadratic_form R M) (w : ι → R) : Q.basis_repr v w = Q (∑ i : ι, w i • v i) := by { rw ← v.equiv_fun_symm_apply, refl } section variables (R₁) /-- The weighted sum of squares with respect to some weight as a quadratic form. The weights are applied using `•`; typically this definition is used either with `S = R₁` or `[algebra S R₁]`, although this is stated more generally. -/ def weighted_sum_squares [monoid S] [distrib_mul_action S R₁] [smul_comm_class S R₁ R₁] (w : ι → S) : quadratic_form R₁ (ι → R₁) := ∑ i : ι, w i • proj i i end @[simp] lemma weighted_sum_squares_apply [monoid S] [distrib_mul_action S R₁] [smul_comm_class S R₁ R₁] (w : ι → S) (v : ι → R₁) : weighted_sum_squares R₁ w v = ∑ i : ι, w i • (v i * v i) := quadratic_form.sum_apply _ _ _ /-- On an orthogonal basis, the basis representation of `Q` is just a sum of squares. -/ lemma basis_repr_eq_of_is_Ortho {R₁ M} [comm_ring R₁] [add_comm_group M] [module R₁ M] [invertible (2 : R₁)] (Q : quadratic_form R₁ M) (v : basis ι R₁ M) (hv₂ : (associated Q).is_Ortho v) : Q.basis_repr v = weighted_sum_squares _ (λ i, Q (v i)) := begin ext w, rw [basis_repr_apply, ←@associated_eq_self_apply R₁, sum_left, weighted_sum_squares_apply], refine sum_congr rfl (λ j hj, _), rw [←@associated_eq_self_apply R₁, sum_right, sum_eq_single_of_mem j hj], { rw [smul_left, smul_right, smul_eq_mul], ring }, { intros i _ hij, rw [smul_left, smul_right, show associated_hom R₁ Q (v j) (v i) = 0, from hv₂ hij.symm, mul_zero, mul_zero] }, end end quadratic_form
f890354db1d7f288950aa3d8fda0d8b42eba0d02
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/ring_theory/rees_algebra.lean
437facd972edeb30c9c4dc628e3d5ad1c2e6d9ca
[ "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
4,081
lean
/- Copyright (c) 2022 Andrew Yang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Andrew Yang -/ import ring_theory.finite_type /-! # Rees algebra The Rees algebra of an ideal `I` is the subalgebra `R[It]` of `R[t]` defined as `R[It] = ⨁ₙ Iⁿ tⁿ`. This is used to prove the Artin-Rees lemma, and will potentially enable us to calculate some blowup in the future. ## Main definition - `rees_algebra` : The Rees algebra of an ideal `I`, defined as a subalgebra of `R[X]`. - `adjoin_monomial_eq_rees_algebra` : The Rees algebra is generated by the degree one elements. - `rees_algebra.fg` : The Rees algebra of a f.g. ideal is of finite type. In particular, this implies that the rees algebra over a noetherian ring is still noetherian. -/ universes u v variables {R M : Type u} [comm_ring R] [add_comm_group M] [module R M] (I : ideal R) open polynomial open_locale polynomial big_operators /-- The Rees algebra of an ideal `I`, defined as the subalgebra of `R[X]` whose `i`-th coefficient falls in `I ^ i`. -/ def rees_algebra : subalgebra R R[X] := { carrier := { f | ∀ i, f.coeff i ∈ I ^ i }, mul_mem' := λ f g hf hg i, begin rw coeff_mul, apply ideal.sum_mem, rintros ⟨j, k⟩ e, rw [← finset.nat.mem_antidiagonal.mp e, pow_add], exact ideal.mul_mem_mul (hf j) (hg k) end, one_mem' := λ i, begin rw coeff_one, split_ifs, { subst h, simp }, { simp } end, add_mem' := λ f g hf hg i, begin rw coeff_add, exact ideal.add_mem _ (hf i) (hg i) end, zero_mem' := λ i, ideal.zero_mem _, algebra_map_mem' := λ r i, begin rw [algebra_map_apply, coeff_C], split_ifs, { subst h, simp }, { simp } end } lemma mem_rees_algebra_iff (f : R[X]) : f ∈ rees_algebra I ↔ ∀ i, f.coeff i ∈ I ^ i := iff.rfl lemma mem_rees_algebra_iff_support (f : R[X]) : f ∈ rees_algebra I ↔ ∀ i ∈ f.support, f.coeff i ∈ I ^ i := begin apply forall_congr, intro a, rw [mem_support_iff, iff.comm, imp_iff_right_iff, ne.def, ← imp_iff_not_or], exact λ e, e.symm ▸ (I ^ a).zero_mem end lemma rees_algebra.monomial_mem {I : ideal R} {i : ℕ} {r : R} : monomial i r ∈ rees_algebra I ↔ r ∈ I ^ i := by simp [mem_rees_algebra_iff_support, coeff_monomial, ← imp_iff_not_or] { contextual := tt } lemma monomial_mem_adjoin_monomial {I : ideal R} {n : ℕ} {r : R} (hr : r ∈ I ^ n) : monomial n r ∈ algebra.adjoin R (submodule.map (monomial 1 : R →ₗ[R] R[X]) I : set R[X]) := begin induction n with n hn generalizing r, { exact subalgebra.algebra_map_mem _ _ }, { rw pow_succ at hr, apply submodule.smul_induction_on hr, { intros r hr s hs, rw [nat.succ_eq_one_add, smul_eq_mul, ← monomial_mul_monomial], exact subalgebra.mul_mem _ (algebra.subset_adjoin (set.mem_image_of_mem _ hr)) (hn hs) }, { intros x y hx hy, rw monomial_add, exact subalgebra.add_mem _ hx hy } } end lemma adjoin_monomial_eq_rees_algebra : algebra.adjoin R (submodule.map (monomial 1 : R →ₗ[R] R[X]) I : set R[X]) = rees_algebra I := begin apply le_antisymm, { apply algebra.adjoin_le _, rintro _ ⟨r, hr, rfl⟩, exact rees_algebra.monomial_mem.mpr (by rwa pow_one) }, { intros p hp, rw p.as_sum_support, apply subalgebra.sum_mem _ _, rintros i -, exact monomial_mem_adjoin_monomial (hp i) } end variables {I} lemma rees_algebra.fg (hI : I.fg) : (rees_algebra I).fg := begin classical, obtain ⟨s, hs⟩ := hI, rw [← adjoin_monomial_eq_rees_algebra, ← hs], use s.image (monomial 1), rw finset.coe_image, change _ = algebra.adjoin R (submodule.map (monomial 1 : R →ₗ[R] R[X]) (submodule.span R ↑s) : set R[X]), rw [submodule.map_span, algebra.adjoin_span] end instance [is_noetherian_ring R] : algebra.finite_type R (rees_algebra I) := ⟨(rees_algebra I).fg_top.mpr (rees_algebra.fg $ is_noetherian.noetherian I)⟩ instance [is_noetherian_ring R] : is_noetherian_ring (rees_algebra I) := algebra.finite_type.is_noetherian_ring R _
510a003791c82a8831bc14213dd97436f21b230a
59a4b050600ed7b3d5826a8478db0a9bdc190252
/src/category_theory/discrete_category.lean
07d966a5525a90fac5117bebc5ba62af985c5a3f
[]
no_license
rwbarton/lean-category-theory
f720268d800b62a25d69842ca7b5d27822f00652
00df814d463406b7a13a56f5dcda67758ba1b419
refs/heads/master
1,585,366,296,767
1,536,151,349,000
1,536,151,349,000
147,652,096
0
0
null
1,536,226,960,000
1,536,226,960,000
null
UTF-8
Lean
false
false
1,736
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Stephen Morgan, Scott Morrison import category_theory.functor import category_theory.tactics.obviously import category_theory.equivalence namespace category_theory universes u₁ v₁ u₂ def discrete (α : Type u₁) := α instance discrete_category (α : Type u₁) : small_category (discrete α) := { hom := λ X Y, ulift (plift (X = Y)), id := by obviously, comp := by obviously } instance pempty_category : small_category pempty := (by apply_instance : small_category (discrete pempty)) instance punit_category : category.{u₁ v₁} punit := { hom := λ X Y, punit, id := by obviously, comp := by obviously } example : Equivalence.{u₁ u₁ u₁ u₁} punit (discrete punit) := by obviously def EmptyFunctor (C : Type (u₂+1)) [large_category C] : pempty ⥤ C := by obviously -- TODO find a home for these in mathlib. https://leanprover.zulipchat.com/#narrow/stream/113488-general/subject/transport.20through.20trivial.20bundles/near/125769004 @[simp] lemma plift.rec.constant {α : Sort u₁} {β : Sort u₂} (b : β) : @plift.rec α (λ _, β) (λ _, b) = λ _, b := begin apply funext, intros, cases x, refl, end @[simp] lemma ulift.rec.constant {α : Type u₁} {β : Sort u₂} (b : β) : @ulift.rec α (λ _, β) (λ _, b) = λ _, b := begin apply funext, intros, cases x, refl, end namespace functor def of_function {C : Type (u₂+1)} [large_category C] {I : Type u₁} (F : I → C) : (discrete I) ⥤ C := { obj := F, map' := λ X Y f, begin cases f, cases f, cases f, exact 𝟙 (F X) end } end functor end category_theory
d718fdf02fffd49c6b288fc46573f5362951a7b2
302b541ac2e998a523ae04da7673fd0932ded126
/tests/bench/pair.lean
e0a7e7451e62073eb7f939804b2a105e6a041818
[]
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
237
lean
set_option trace.compiler.ir.init true inductive L | Nil | Cons1 : Nat -> L -> L | Cons2 : Nat -> Nat -> L -> L open L def fold : L -> L | Nil => Nil | Cons1 n l => Cons1 n (fold l) | Cons2 x y l => Cons1 (x + y) (fold l)
aa78446bd639f5729add6cc858ad1ad69f73c63e
500f65bb93c499cd35c3254d894d762208cae042
/src/category_theory/adjunction/basic.lean
d5179b7adeed06f3f31e8e29970ce2632a544ba6
[ "Apache-2.0" ]
permissive
PatrickMassot/mathlib
c39dc0ff18bbde42f1c93a1642f6e429adad538c
45df75b3c9da159fe3192fa7f769dfbec0bd6bda
refs/heads/master
1,623,168,646,390
1,566,940,765,000
1,566,940,765,000
115,220,590
0
1
null
1,514,061,524,000
1,514,061,524,000
null
UTF-8
Lean
false
false
10,304
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.equivalence import data.equiv.basic namespace category_theory open category universes v₁ v₂ v₃ u₁ u₂ u₃ -- declare the `v`'s first; see `category_theory.category` for an explanation local attribute [elab_simple] whisker_left whisker_right variables {C : Type u₁} [𝒞 : category.{v₁} C] {D : Type u₂} [𝒟 : category.{v₂} D] include 𝒞 𝒟 /-- `F ⊣ G` represents the data of an adjunction between two functors `F : C ⥤ D` and `G : D ⥤ C`. `F` is the left adjoint and `G` is the right adjoint. -/ structure adjunction (F : C ⥤ D) (G : D ⥤ C) := (hom_equiv : Π (X Y), (F.obj X ⟶ Y) ≃ (X ⟶ G.obj Y)) (unit : functor.id C ⟶ F.comp G) (counit : G.comp F ⟶ functor.id D) (hom_equiv_unit' : Π {X Y f}, (hom_equiv X Y) f = (unit : _ ⟶ _).app X ≫ G.map f . obviously) (hom_equiv_counit' : Π {X Y g}, (hom_equiv X Y).symm g = F.map g ≫ counit.app Y . obviously) infix ` ⊣ `:15 := adjunction class is_left_adjoint (left : C ⥤ D) := (right : D ⥤ C) (adj : left ⊣ right) class is_right_adjoint (right : D ⥤ C) := (left : C ⥤ D) (adj : left ⊣ right) def left_adjoint (R : D ⥤ C) [is_right_adjoint R] : C ⥤ D := is_right_adjoint.left R def right_adjoint (L : C ⥤ D) [is_left_adjoint L] : D ⥤ C := is_left_adjoint.right L namespace adjunction restate_axiom hom_equiv_unit' restate_axiom hom_equiv_counit' attribute [simp, priority 1] hom_equiv_unit hom_equiv_counit section variables {F : C ⥤ D} {G : D ⥤ C} (adj : F ⊣ G) {X' X : C} {Y Y' : D} @[simp, priority 1] lemma hom_equiv_naturality_left_symm (f : X' ⟶ X) (g : X ⟶ G.obj Y) : (adj.hom_equiv X' Y).symm (f ≫ g) = F.map f ≫ (adj.hom_equiv X Y).symm g := by rw [hom_equiv_counit, F.map_comp, assoc, adj.hom_equiv_counit.symm] @[simp] lemma hom_equiv_naturality_left (f : X' ⟶ X) (g : F.obj X ⟶ Y) : (adj.hom_equiv X' Y) (F.map f ≫ g) = f ≫ (adj.hom_equiv X Y) g := by rw [← equiv.eq_symm_apply]; simp [-hom_equiv_unit] @[simp, priority 1] lemma hom_equiv_naturality_right (f : F.obj X ⟶ Y) (g : Y ⟶ Y') : (adj.hom_equiv X Y') (f ≫ g) = (adj.hom_equiv X Y) f ≫ G.map g := by rw [hom_equiv_unit, G.map_comp, ← assoc, ←hom_equiv_unit] @[simp] lemma hom_equiv_naturality_right_symm (f : X ⟶ G.obj Y) (g : Y ⟶ Y') : (adj.hom_equiv X Y').symm (f ≫ G.map g) = (adj.hom_equiv X Y).symm f ≫ g := by rw [equiv.symm_apply_eq]; simp [-hom_equiv_counit] @[simp] lemma left_triangle : (whisker_right adj.unit F) ≫ (whisker_left F adj.counit) = nat_trans.id _ := begin ext1 X, dsimp, erw [← adj.hom_equiv_counit, equiv.symm_apply_eq, adj.hom_equiv_unit], simp end @[simp] lemma right_triangle : (whisker_left G adj.unit) ≫ (whisker_right adj.counit G) = nat_trans.id _ := begin ext1 Y, dsimp, erw [← adj.hom_equiv_unit, ← equiv.eq_symm_apply, adj.hom_equiv_counit], simp end @[simp, reassoc] lemma left_triangle_components : F.map (adj.unit.app X) ≫ adj.counit.app (F.obj X) = 𝟙 (F.obj X) := congr_arg (λ (t : nat_trans _ (functor.id C ⋙ F)), t.app X) adj.left_triangle @[simp, reassoc] lemma right_triangle_components {Y : D} : adj.unit.app (G.obj Y) ≫ G.map (adj.counit.app Y) = 𝟙 (G.obj Y) := congr_arg (λ (t : nat_trans _ (G ⋙ functor.id C)), t.app Y) adj.right_triangle @[simp, reassoc] lemma counit_naturality {X Y : D} (f : X ⟶ Y) : F.map (G.map f) ≫ (adj.counit).app Y = (adj.counit).app X ≫ f := adj.counit.naturality f @[simp, reassoc] lemma unit_naturality {X Y : C} (f : X ⟶ Y) : (adj.unit).app X ≫ G.map (F.map f) = f ≫ (adj.unit).app Y := (adj.unit.naturality f).symm end end adjunction namespace adjunction structure core_hom_equiv (F : C ⥤ D) (G : D ⥤ C) := (hom_equiv : Π (X Y), (F.obj X ⟶ Y) ≃ (X ⟶ G.obj Y)) (hom_equiv_naturality_left_symm' : Π {X' X Y} (f : X' ⟶ X) (g : X ⟶ G.obj Y), (hom_equiv X' Y).symm (f ≫ g) = F.map f ≫ (hom_equiv X Y).symm g . obviously) (hom_equiv_naturality_right' : Π {X Y Y'} (f : F.obj X ⟶ Y) (g : Y ⟶ Y'), (hom_equiv X Y') (f ≫ g) = (hom_equiv X Y) f ≫ G.map g . obviously) namespace core_hom_equiv restate_axiom hom_equiv_naturality_left_symm' restate_axiom hom_equiv_naturality_right' attribute [simp, priority 1] hom_equiv_naturality_left_symm hom_equiv_naturality_right variables {F : C ⥤ D} {G : D ⥤ C} (adj : core_hom_equiv F G) {X' X : C} {Y Y' : D} @[simp] lemma hom_equiv_naturality_left (f : X' ⟶ X) (g : F.obj X ⟶ Y) : (adj.hom_equiv X' Y) (F.map f ≫ g) = f ≫ (adj.hom_equiv X Y) g := by rw [← equiv.eq_symm_apply]; simp @[simp] lemma hom_equiv_naturality_right_symm (f : X ⟶ G.obj Y) (g : Y ⟶ Y') : (adj.hom_equiv X Y').symm (f ≫ G.map g) = (adj.hom_equiv X Y).symm f ≫ g := by rw [equiv.symm_apply_eq]; simp end core_hom_equiv structure core_unit_counit (F : C ⥤ D) (G : D ⥤ C) := (unit : functor.id C ⟶ F.comp G) (counit : G.comp F ⟶ functor.id D) (left_triangle' : whisker_right unit F ≫ whisker_left F counit = nat_trans.id _ . obviously) (right_triangle' : whisker_left G unit ≫ whisker_right counit G = nat_trans.id _ . obviously) namespace core_unit_counit restate_axiom left_triangle' restate_axiom right_triangle' attribute [simp] left_triangle right_triangle end core_unit_counit variables {F : C ⥤ D} {G : D ⥤ C} def mk_of_hom_equiv (adj : core_hom_equiv F G) : F ⊣ G := { unit := { app := λ X, (adj.hom_equiv X (F.obj X)) (𝟙 (F.obj X)), naturality' := begin intros, erw [← adj.hom_equiv_naturality_left, ← adj.hom_equiv_naturality_right], dsimp, simp end }, counit := { app := λ Y, (adj.hom_equiv _ _).inv_fun (𝟙 (G.obj Y)), naturality' := begin intros, erw [← adj.hom_equiv_naturality_left_symm, ← adj.hom_equiv_naturality_right_symm], dsimp, simp end }, hom_equiv_unit' := λ X Y f, by erw [← adj.hom_equiv_naturality_right]; simp, hom_equiv_counit' := λ X Y f, by erw [← adj.hom_equiv_naturality_left_symm]; simp, .. adj } def mk_of_unit_counit (adj : core_unit_counit F G) : F ⊣ G := { hom_equiv := λ X Y, { to_fun := λ f, adj.unit.app X ≫ G.map f, inv_fun := λ g, F.map g ≫ adj.counit.app Y, left_inv := λ f, begin change F.map (_ ≫ _) ≫ _ = _, rw [F.map_comp, assoc, ←functor.comp_map, adj.counit.naturality, ←assoc], convert id_comp _ f, exact congr_arg (λ t : nat_trans _ _, t.app _) adj.left_triangle end, right_inv := λ g, begin change _ ≫ G.map (_ ≫ _) = _, rw [G.map_comp, ←assoc, ←functor.comp_map, ←adj.unit.naturality, assoc], convert comp_id _ g, exact congr_arg (λ t : nat_trans _ _, t.app _) adj.right_triangle end }, .. adj } section omit 𝒟 def id : functor.id C ⊣ functor.id C := { hom_equiv := λ X Y, equiv.refl _, unit := 𝟙 _, counit := 𝟙 _ } end section variables {E : Type u₃} [ℰ : category.{v₃} E] (H : D ⥤ E) (I : E ⥤ D) def comp (adj₁ : F ⊣ G) (adj₂ : H ⊣ I) : F ⋙ H ⊣ I ⋙ G := { hom_equiv := λ X Z, equiv.trans (adj₂.hom_equiv _ _) (adj₁.hom_equiv _ _), unit := adj₁.unit ≫ (whisker_left F $ whisker_right adj₂.unit G) ≫ (functor.associator _ _ _).inv, counit := (functor.associator _ _ _).hom ≫ (whisker_left I $ whisker_right adj₁.counit H) ≫ adj₂.counit } end section construct_left -- Construction of a left adjoint. In order to construct a left -- adjoint to a functor G : D → C, it suffices to give the object part -- of a functor F : C → D together with isomorphisms Hom(FX, Y) ≃ -- Hom(X, GY) natural in Y. The action of F on morphisms can be -- constructed from this data. variables {F_obj : C → D} {G} variables (e : Π X Y, (F_obj X ⟶ Y) ≃ (X ⟶ G.obj Y)) variables (he : Π X Y Y' g h, e X Y' (h ≫ g) = e X Y h ≫ G.map g) include he private lemma he' {X Y Y'} (f g) : (e X Y').symm (f ≫ G.map g) = (e X Y).symm f ≫ g := by intros; rw [equiv.symm_apply_eq, he]; simp def left_adjoint_of_equiv : C ⥤ D := { obj := F_obj, map := λ X X' f, (e X (F_obj X')).symm (f ≫ e X' (F_obj X') (𝟙 _)), map_comp' := λ X X' X'' f f', begin rw [equiv.symm_apply_eq, he, equiv.apply_symm_apply], conv { to_rhs, rw [assoc, ←he, id_comp, equiv.apply_symm_apply] }, simp end } def adjunction_of_equiv_left : left_adjoint_of_equiv e he ⊣ G := mk_of_hom_equiv { hom_equiv := e, hom_equiv_naturality_left_symm' := begin intros, erw [← he' e he, ← equiv.apply_eq_iff_eq], simp [(he _ _ _ _ _).symm] end } end construct_left section construct_right -- Construction of a right adjoint, analogous to the above. variables {F} {G_obj : D → C} variables (e : Π X Y, (F.obj X ⟶ Y) ≃ (X ⟶ G_obj Y)) variables (he : Π X' X Y f g, e X' Y (F.map f ≫ g) = f ≫ e X Y g) include he private lemma he' {X' X Y} (f g) : F.map f ≫ (e X Y).symm g = (e X' Y).symm (f ≫ g) := by intros; rw [equiv.eq_symm_apply, he]; simp def right_adjoint_of_equiv : D ⥤ C := { obj := G_obj, map := λ Y Y' g, (e (G_obj Y) Y') ((e (G_obj Y) Y).symm (𝟙 _) ≫ g), map_comp' := λ Y Y' Y'' g g', begin rw [← equiv.eq_symm_apply, ← he' e he, equiv.symm_apply_apply], conv { to_rhs, rw [← assoc, he' e he, comp_id, equiv.symm_apply_apply] }, simp end } def adjunction_of_equiv_right : F ⊣ right_adjoint_of_equiv e he := mk_of_hom_equiv { hom_equiv := e, hom_equiv_naturality_left_symm' := by intros; rw [equiv.symm_apply_eq, he]; simp, hom_equiv_naturality_right' := begin intros X Y Y' g h, erw [←he, equiv.apply_eq_iff_eq, ←assoc, he' e he, comp_id, equiv.symm_apply_apply] end } end construct_right end adjunction open adjunction namespace equivalence def to_adjunction (e : C ≌ D) : e.functor ⊣ e.inverse := mk_of_unit_counit ⟨e.unit, e.counit, by { ext, exact e.functor_unit_comp X }, by { ext, exact e.unit_inverse_comp X }⟩ end equivalence namespace functor def adjunction (E : C ⥤ D) [is_equivalence E] : E ⊣ E.inv := (E.as_equivalence).to_adjunction end functor end category_theory
9d9ec6080485efff3be5be5b930c9633315d0dad
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/category_theory/eq_to_hom.lean
3dc47fe4e1f459153274e6fbf5ffe0e921ba6fe4
[ "Apache-2.0" ]
permissive
ChrisHughes24/mathlib
98322577c460bc6b1fe5c21f42ce33ad1c3e5558
a2a867e827c2a6702beb9efc2b9282bd801d5f9a
refs/heads/master
1,583,848,251,477
1,565,164,247,000
1,565,164,247,000
129,409,993
0
1
Apache-2.0
1,565,164,817,000
1,523,628,059,000
Lean
UTF-8
Lean
false
false
2,849
lean
/- Copyright (c) 2018 Reid Barton. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton, Scott Morrison -/ import category_theory.isomorphism import category_theory.functor_category import category_theory.opposites universes v v' u u' -- declare the `v`'s first; see `category_theory.category` for an explanation namespace category_theory open opposite variables {C : Type u} [𝒞 : category.{v} C] include 𝒞 def eq_to_hom {X Y : C} (p : X = Y) : X ⟶ Y := by rw p; exact 𝟙 _ @[simp] lemma eq_to_hom_refl (X : C) (p : X = X) : eq_to_hom p = 𝟙 X := rfl @[simp] lemma eq_to_hom_trans {X Y Z : C} (p : X = Y) (q : Y = Z) : eq_to_hom p ≫ eq_to_hom q = eq_to_hom (p.trans q) := by cases p; cases q; simp @[simp] lemma eq_to_hom_trans_assoc {X Y Z W : C} (p : X = Y) (q : Y = Z) (f : Z ⟶ W) : eq_to_hom p ≫ (eq_to_hom q ≫ f) = eq_to_hom (p.trans q) ≫ f := by cases p; cases q; simp def eq_to_iso {X Y : C} (p : X = Y) : X ≅ Y := ⟨eq_to_hom p, eq_to_hom p.symm, by simp, by simp⟩ @[simp] lemma eq_to_iso.hom {X Y : C} (p : X = Y) : (eq_to_iso p).hom = eq_to_hom p := rfl @[simp] lemma eq_to_iso_refl (X : C) (p : X = X) : eq_to_iso p = iso.refl X := rfl @[simp] lemma eq_to_iso_trans {X Y Z : C} (p : X = Y) (q : Y = Z) : eq_to_iso p ≪≫ eq_to_iso q = eq_to_iso (p.trans q) := by ext; simp @[simp] lemma eq_to_hom_op (X Y : C) (h : X = Y) : (eq_to_hom h).op = eq_to_hom (congr_arg op h.symm) := begin cases h, refl end variables {D : Type u'} [𝒟 : category.{v'} D] include 𝒟 namespace functor /-- Proving equality between functors. This isn't an extensionality lemma, because usually you don't really want to do this. -/ lemma ext {F G : C ⥤ D} (h_obj : ∀ X, F.obj X = G.obj X) (h_map : ∀ X Y f, F.map f = eq_to_hom (h_obj X) ≫ G.map f ≫ eq_to_hom (h_obj Y).symm) : F = G := begin cases F with F_obj _ _ _, cases G with G_obj _ _ _, have : F_obj = G_obj, by ext X; apply h_obj, subst this, congr, funext X Y f, simpa using h_map X Y f end -- Using equalities between functors. lemma congr_obj {F G : C ⥤ D} (h : F = G) (X) : F.obj X = G.obj X := by subst h lemma congr_hom {F G : C ⥤ D} (h : F = G) {X Y} (f : X ⟶ Y) : F.map f = eq_to_hom (congr_obj h X) ≫ G.map f ≫ eq_to_hom (congr_obj h Y).symm := by subst h; simp end functor @[simp] lemma eq_to_hom_map (F : C ⥤ D) {X Y : C} (p : X = Y) : F.map (eq_to_hom p) = eq_to_hom (congr_arg F.obj p) := by cases p; simp @[simp] lemma eq_to_iso_map (F : C ⥤ D) {X Y : C} (p : X = Y) : F.map_iso (eq_to_iso p) = eq_to_iso (congr_arg F.obj p) := by ext; cases p; simp @[simp] lemma eq_to_hom_app {F G : C ⥤ D} (h : F = G) (X : C) : (eq_to_hom h : F ⟶ G).app X = eq_to_hom (functor.congr_obj h X) := by subst h; refl end category_theory
3d6aa5b65687b312907adb8613a0cb6c524d7f71
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/linear_algebra/projection.lean
ac662dac37e42b3836fd5dda28a07b2d4f3b3640
[ "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
16,736
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 linear_algebra.quotient import linear_algebra.prod /-! # Projection to a subspace In this file we define * `linear_proj_of_is_compl (p q : submodule R E) (h : is_compl p q)`: the projection of a module `E` to a submodule `p` along its complement `q`; it is the unique linear map `f : E → p` such that `f x = x` for `x ∈ p` and `f x = 0` for `x ∈ q`. * `is_compl_equiv_proj p`: equivalence between submodules `q` such that `is_compl p q` and projections `f : E → p`, `∀ x ∈ p, f x = x`. We also provide some lemmas justifying correctness of our definitions. ## Tags projection, complement subspace -/ section ring variables {R : Type*} [ring R] {E : Type*} [add_comm_group E] [module R E] {F : Type*} [add_comm_group F] [module R F] {G : Type*} [add_comm_group G] [module R G] (p q : submodule R E) variables {S : Type*} [semiring S] {M : Type*} [add_comm_monoid M] [module S M] (m : submodule S M) noncomputable theory namespace linear_map variable {p} open submodule lemma ker_id_sub_eq_of_proj {f : E →ₗ[R] p} (hf : ∀ x : p, f x = x) : ker (id - p.subtype.comp f) = p := begin ext x, simp only [comp_apply, mem_ker, subtype_apply, sub_apply, id_apply, sub_eq_zero], exact ⟨λ h, h.symm ▸ submodule.coe_mem _, λ hx, by erw [hf ⟨x, hx⟩, subtype.coe_mk]⟩ end lemma range_eq_of_proj {f : E →ₗ[R] p} (hf : ∀ x : p, f x = x) : range f = ⊤ := range_eq_top.2 $ λ x, ⟨x, hf x⟩ lemma is_compl_of_proj {f : E →ₗ[R] p} (hf : ∀ x : p, f x = x) : is_compl p f.ker := begin split, { rw disjoint_iff_inf_le, rintros x ⟨hpx, hfx⟩, erw [set_like.mem_coe, mem_ker, hf ⟨x, hpx⟩, mk_eq_zero] at hfx, simp only [hfx, set_like.mem_coe, zero_mem] }, { rw codisjoint_iff_le_sup, intros x hx, rw [mem_sup'], refine ⟨f x, ⟨x - f x, _⟩, add_sub_cancel'_right _ _⟩, rw [mem_ker, linear_map.map_sub, hf, sub_self] } end end linear_map namespace submodule open linear_map /-- If `q` is a complement of `p`, then `M/p ≃ q`. -/ def quotient_equiv_of_is_compl (h : is_compl p q) : (E ⧸ p) ≃ₗ[R] q := linear_equiv.symm $ linear_equiv.of_bijective (p.mkq.comp q.subtype) (by rw [← ker_eq_bot, ker_comp, ker_mkq, disjoint_iff_comap_eq_bot.1 h.symm.disjoint]) (by rw [← range_eq_top, range_comp, range_subtype, map_mkq_eq_top, h.sup_eq_top]) @[simp] lemma quotient_equiv_of_is_compl_symm_apply (h : is_compl p q) (x : q) : (quotient_equiv_of_is_compl p q h).symm x = quotient.mk x := rfl @[simp] lemma quotient_equiv_of_is_compl_apply_mk_coe (h : is_compl p q) (x : q) : quotient_equiv_of_is_compl p q h (quotient.mk x) = x := (quotient_equiv_of_is_compl p q h).apply_symm_apply x @[simp] lemma mk_quotient_equiv_of_is_compl_apply (h : is_compl p q) (x : E ⧸ p) : (quotient.mk (quotient_equiv_of_is_compl p q h x) : E ⧸ p) = x := (quotient_equiv_of_is_compl p q h).symm_apply_apply x /-- If `q` is a complement of `p`, then `p × q` is isomorphic to `E`. It is the unique linear map `f : E → p` such that `f x = x` for `x ∈ p` and `f x = 0` for `x ∈ q`. -/ def prod_equiv_of_is_compl (h : is_compl p q) : (p × q) ≃ₗ[R] E := begin apply linear_equiv.of_bijective (p.subtype.coprod q.subtype), { rw [← ker_eq_bot, ker_coprod_of_disjoint_range, ker_subtype, ker_subtype, prod_bot], rw [range_subtype, range_subtype], exact h.1 }, { rw [← range_eq_top, ← sup_eq_range, h.sup_eq_top] } end @[simp] lemma coe_prod_equiv_of_is_compl (h : is_compl p q) : (prod_equiv_of_is_compl p q h : (p × q) →ₗ[R] E) = p.subtype.coprod q.subtype := rfl @[simp] lemma coe_prod_equiv_of_is_compl' (h : is_compl p q) (x : p × q) : prod_equiv_of_is_compl p q h x = x.1 + x.2 := rfl @[simp] lemma prod_equiv_of_is_compl_symm_apply_left (h : is_compl p q) (x : p) : (prod_equiv_of_is_compl p q h).symm x = (x, 0) := (prod_equiv_of_is_compl p q h).symm_apply_eq.2 $ by simp @[simp] lemma prod_equiv_of_is_compl_symm_apply_right (h : is_compl p q) (x : q) : (prod_equiv_of_is_compl p q h).symm x = (0, x) := (prod_equiv_of_is_compl p q h).symm_apply_eq.2 $ by simp @[simp] lemma prod_equiv_of_is_compl_symm_apply_fst_eq_zero (h : is_compl p q) {x : E} : ((prod_equiv_of_is_compl p q h).symm x).1 = 0 ↔ x ∈ q := begin conv_rhs { rw [← (prod_equiv_of_is_compl p q h).apply_symm_apply x] }, rw [coe_prod_equiv_of_is_compl', submodule.add_mem_iff_left _ (submodule.coe_mem _), mem_right_iff_eq_zero_of_disjoint h.disjoint] end @[simp] lemma prod_equiv_of_is_compl_symm_apply_snd_eq_zero (h : is_compl p q) {x : E} : ((prod_equiv_of_is_compl p q h).symm x).2 = 0 ↔ x ∈ p := begin conv_rhs { rw [← (prod_equiv_of_is_compl p q h).apply_symm_apply x] }, rw [coe_prod_equiv_of_is_compl', submodule.add_mem_iff_right _ (submodule.coe_mem _), mem_left_iff_eq_zero_of_disjoint h.disjoint] end @[simp] lemma prod_comm_trans_prod_equiv_of_is_compl (h : is_compl p q) : linear_equiv.prod_comm R q p ≪≫ₗ prod_equiv_of_is_compl p q h = prod_equiv_of_is_compl q p h.symm := linear_equiv.ext $ λ _, add_comm _ _ /-- Projection to a submodule along its complement. -/ def linear_proj_of_is_compl (h : is_compl p q) : E →ₗ[R] p := (linear_map.fst R p q) ∘ₗ ↑(prod_equiv_of_is_compl p q h).symm variables {p q} @[simp] lemma linear_proj_of_is_compl_apply_left (h : is_compl p q) (x : p) : linear_proj_of_is_compl p q h x = x := by simp [linear_proj_of_is_compl] @[simp] lemma linear_proj_of_is_compl_range (h : is_compl p q) : (linear_proj_of_is_compl p q h).range = ⊤ := range_eq_of_proj (linear_proj_of_is_compl_apply_left h) @[simp] lemma linear_proj_of_is_compl_apply_eq_zero_iff (h : is_compl p q) {x : E} : linear_proj_of_is_compl p q h x = 0 ↔ x ∈ q:= by simp [linear_proj_of_is_compl] lemma linear_proj_of_is_compl_apply_right' (h : is_compl p q) (x : E) (hx : x ∈ q) : linear_proj_of_is_compl p q h x = 0 := (linear_proj_of_is_compl_apply_eq_zero_iff h).2 hx @[simp] lemma linear_proj_of_is_compl_apply_right (h : is_compl p q) (x : q) : linear_proj_of_is_compl p q h x = 0 := linear_proj_of_is_compl_apply_right' h x x.2 @[simp] lemma linear_proj_of_is_compl_ker (h : is_compl p q) : (linear_proj_of_is_compl p q h).ker = q := ext $ λ x, mem_ker.trans (linear_proj_of_is_compl_apply_eq_zero_iff h) lemma linear_proj_of_is_compl_comp_subtype (h : is_compl p q) : (linear_proj_of_is_compl p q h).comp p.subtype = id := linear_map.ext $ linear_proj_of_is_compl_apply_left h lemma linear_proj_of_is_compl_idempotent (h : is_compl p q) (x : E) : linear_proj_of_is_compl p q h (linear_proj_of_is_compl p q h x) = linear_proj_of_is_compl p q h x := linear_proj_of_is_compl_apply_left h _ lemma exists_unique_add_of_is_compl_prod (hc : is_compl p q) (x : E) : ∃! (u : p × q), (u.fst : E) + u.snd = x := (prod_equiv_of_is_compl _ _ hc).to_equiv.bijective.exists_unique _ lemma exists_unique_add_of_is_compl (hc : is_compl p q) (x : E) : ∃ (u : p) (v : q), ((u : E) + v = x ∧ ∀ (r : p) (s : q), (r : E) + s = x → r = u ∧ s = v) := let ⟨u, hu₁, hu₂⟩ := exists_unique_add_of_is_compl_prod hc x in ⟨u.1, u.2, hu₁, λ r s hrs, prod.eq_iff_fst_eq_snd_eq.1 (hu₂ ⟨r, s⟩ hrs)⟩ lemma linear_proj_add_linear_proj_of_is_compl_eq_self (hpq : is_compl p q) (x : E) : (p.linear_proj_of_is_compl q hpq x + q.linear_proj_of_is_compl p hpq.symm x : E) = x := begin dunfold linear_proj_of_is_compl, rw ←prod_comm_trans_prod_equiv_of_is_compl _ _ hpq, exact (prod_equiv_of_is_compl _ _ hpq).apply_symm_apply x, end end submodule namespace linear_map open submodule /-- Given linear maps `φ` and `ψ` from complement submodules, `of_is_compl` is the induced linear map over the entire module. -/ def of_is_compl {p q : submodule R E} (h : is_compl p q) (φ : p →ₗ[R] F) (ψ : q →ₗ[R] F) : E →ₗ[R] F := (linear_map.coprod φ ψ) ∘ₗ ↑(submodule.prod_equiv_of_is_compl _ _ h).symm variables {p q} @[simp] lemma of_is_compl_left_apply (h : is_compl p q) {φ : p →ₗ[R] F} {ψ : q →ₗ[R] F} (u : p) : of_is_compl h φ ψ (u : E) = φ u := by simp [of_is_compl] @[simp] lemma of_is_compl_right_apply (h : is_compl p q) {φ : p →ₗ[R] F} {ψ : q →ₗ[R] F} (v : q) : of_is_compl h φ ψ (v : E) = ψ v := by simp [of_is_compl] lemma of_is_compl_eq (h : is_compl p q) {φ : p →ₗ[R] F} {ψ : q →ₗ[R] F} {χ : E →ₗ[R] F} (hφ : ∀ u, φ u = χ u) (hψ : ∀ u, ψ u = χ u) : of_is_compl h φ ψ = χ := begin ext x, obtain ⟨_, _, rfl, _⟩ := exists_unique_add_of_is_compl h x, simp [of_is_compl, hφ, hψ] end lemma of_is_compl_eq' (h : is_compl p q) {φ : p →ₗ[R] F} {ψ : q →ₗ[R] F} {χ : E →ₗ[R] F} (hφ : φ = χ.comp p.subtype) (hψ : ψ = χ.comp q.subtype) : of_is_compl h φ ψ = χ := of_is_compl_eq h (λ _, hφ.symm ▸ rfl) (λ _, hψ.symm ▸ rfl) @[simp] lemma of_is_compl_zero (h : is_compl p q) : (of_is_compl h 0 0 : E →ₗ[R] F) = 0 := of_is_compl_eq _ (λ _, rfl) (λ _, rfl) @[simp] lemma of_is_compl_add (h : is_compl p q) {φ₁ φ₂ : p →ₗ[R] F} {ψ₁ ψ₂ : q →ₗ[R] F} : of_is_compl h (φ₁ + φ₂) (ψ₁ + ψ₂) = of_is_compl h φ₁ ψ₁ + of_is_compl h φ₂ ψ₂ := of_is_compl_eq _ (by simp) (by simp) @[simp] lemma of_is_compl_smul {R : Type*} [comm_ring R] {E : Type*} [add_comm_group E] [module R E] {F : Type*} [add_comm_group F] [module R F] {p q : submodule R E} (h : is_compl p q) {φ : p →ₗ[R] F} {ψ : q →ₗ[R] F} (c : R) : of_is_compl h (c • φ) (c • ψ) = c • of_is_compl h φ ψ := of_is_compl_eq _ (by simp) (by simp) section variables {R₁ : Type*} [comm_ring R₁] [module R₁ E] [module R₁ F] /-- The linear map from `(p →ₗ[R₁] F) × (q →ₗ[R₁] F)` to `E →ₗ[R₁] F`. -/ def of_is_compl_prod {p q : submodule R₁ E} (h : is_compl p q) : ((p →ₗ[R₁] F) × (q →ₗ[R₁] F)) →ₗ[R₁] (E →ₗ[R₁] F) := { to_fun := λ φ, of_is_compl h φ.1 φ.2, map_add' := by { intros φ ψ, rw [prod.snd_add, prod.fst_add, of_is_compl_add] }, map_smul' := by { intros c φ, simp [prod.smul_snd, prod.smul_fst, of_is_compl_smul] } } @[simp] lemma of_is_compl_prod_apply {p q : submodule R₁ E} (h : is_compl p q) (φ : (p →ₗ[R₁] F) × (q →ₗ[R₁] F)) : of_is_compl_prod h φ = of_is_compl h φ.1 φ.2 := rfl /-- The natural linear equivalence between `(p →ₗ[R₁] F) × (q →ₗ[R₁] F)` and `E →ₗ[R₁] F`. -/ def of_is_compl_prod_equiv {p q : submodule R₁ E} (h : is_compl p q) : ((p →ₗ[R₁] F) × (q →ₗ[R₁] F)) ≃ₗ[R₁] (E →ₗ[R₁] F) := { inv_fun := λ φ, ⟨φ.dom_restrict p, φ.dom_restrict q⟩, left_inv := begin intros φ, ext, { exact of_is_compl_left_apply h x }, { exact of_is_compl_right_apply h x } end, right_inv := begin intro φ, ext, obtain ⟨a, b, hab, _⟩ := exists_unique_add_of_is_compl h x, rw [← hab], simp, end, .. of_is_compl_prod h } end @[simp] lemma linear_proj_of_is_compl_of_proj (f : E →ₗ[R] p) (hf : ∀ x : p, f x = x) : p.linear_proj_of_is_compl f.ker (is_compl_of_proj hf) = f := begin ext x, have : x ∈ p ⊔ f.ker, { simp only [(is_compl_of_proj hf).sup_eq_top, mem_top] }, rcases mem_sup'.1 this with ⟨x, y, rfl⟩, simp [hf] end /-- If `f : E →ₗ[R] F` and `g : E →ₗ[R] G` are two surjective linear maps and their kernels are complement of each other, then `x ↦ (f x, g x)` defines a linear equivalence `E ≃ₗ[R] F × G`. -/ def equiv_prod_of_surjective_of_is_compl (f : E →ₗ[R] F) (g : E →ₗ[R] G) (hf : f.range = ⊤) (hg : g.range = ⊤) (hfg : is_compl f.ker g.ker) : E ≃ₗ[R] F × G := linear_equiv.of_bijective (f.prod g) (by simp [← ker_eq_bot, hfg.inf_eq_bot]) (by { rw [←range_eq_top], simp [range_prod_eq hfg.sup_eq_top, *] }) @[simp] lemma coe_equiv_prod_of_surjective_of_is_compl {f : E →ₗ[R] F} {g : E →ₗ[R] G} (hf : f.range = ⊤) (hg : g.range = ⊤) (hfg : is_compl f.ker g.ker) : (equiv_prod_of_surjective_of_is_compl f g hf hg hfg : E →ₗ[R] F × G) = f.prod g := rfl @[simp] lemma equiv_prod_of_surjective_of_is_compl_apply {f : E →ₗ[R] F} {g : E →ₗ[R] G} (hf : f.range = ⊤) (hg : g.range = ⊤) (hfg : is_compl f.ker g.ker) (x : E): equiv_prod_of_surjective_of_is_compl f g hf hg hfg x = (f x, g x) := rfl end linear_map namespace submodule open linear_map /-- Equivalence between submodules `q` such that `is_compl p q` and linear maps `f : E →ₗ[R] p` such that `∀ x : p, f x = x`. -/ def is_compl_equiv_proj : {q // is_compl p q} ≃ {f : E →ₗ[R] p // ∀ x : p, f x = x} := { to_fun := λ q, ⟨linear_proj_of_is_compl p q q.2, linear_proj_of_is_compl_apply_left q.2⟩, inv_fun := λ f, ⟨(f : E →ₗ[R] p).ker, is_compl_of_proj f.2⟩, left_inv := λ ⟨q, hq⟩, by simp only [linear_proj_of_is_compl_ker, subtype.coe_mk], right_inv := λ ⟨f, hf⟩, subtype.eq $ f.linear_proj_of_is_compl_of_proj hf } @[simp] lemma coe_is_compl_equiv_proj_apply (q : {q // is_compl p q}) : (p.is_compl_equiv_proj q : E →ₗ[R] p) = linear_proj_of_is_compl p q q.2 := rfl @[simp] lemma coe_is_compl_equiv_proj_symm_apply (f : {f : E →ₗ[R] p // ∀ x : p, f x = x}) : (p.is_compl_equiv_proj.symm f : submodule R E) = (f : E →ₗ[R] p).ker := rfl end submodule namespace linear_map open submodule /-- A linear endomorphism of a module `E` is a projection onto a submodule `p` if it sends every element of `E` to `p` and fixes every element of `p`. The definition allow more generally any `fun_like` type and not just linear maps, so that it can be used for example with `continuous_linear_map` or `matrix`. -/ structure is_proj {F : Type*} [fun_like F M (λ _, M)] (f : F) : Prop := (map_mem : ∀ x, f x ∈ m) (map_id : ∀ x ∈ m, f x = x) lemma is_proj_iff_idempotent (f : M →ₗ[S] M) : (∃ p : submodule S M, is_proj p f) ↔ f ∘ₗ f = f := begin split, { intro h, obtain ⟨p, hp⟩ := h, ext, rw comp_apply, exact hp.map_id (f x) (hp.map_mem x), }, { intro h, use f.range, split, { intro x, exact mem_range_self f x, }, { intros x hx, obtain ⟨y, hy⟩ := mem_range.1 hx, rw [←hy, ←comp_apply, h], }, }, end namespace is_proj variables {p m} /-- Restriction of the codomain of a projection of onto a subspace `p` to `p` instead of the whole space. -/ def cod_restrict {f : M →ₗ[S] M} (h : is_proj m f) : M →ₗ[S] m := f.cod_restrict m h.map_mem @[simp] lemma cod_restrict_apply {f : M →ₗ[S] M} (h : is_proj m f) (x : M) : ↑(h.cod_restrict x) = f x := f.cod_restrict_apply m x @[simp] lemma cod_restrict_apply_cod {f : M →ₗ[S] M} (h : is_proj m f) (x : m) : h.cod_restrict x = x := by {ext, rw [cod_restrict_apply], exact h.map_id x x.2} lemma cod_restrict_ker {f : M →ₗ[S] M} (h : is_proj m f) : h.cod_restrict.ker = f.ker := f.ker_cod_restrict m _ lemma is_compl {f : E →ₗ[R] E} (h : is_proj p f) : is_compl p f.ker := by { rw ←cod_restrict_ker, exact is_compl_of_proj h.cod_restrict_apply_cod, } lemma eq_conj_prod_map' {f : E →ₗ[R] E} (h : is_proj p f) : f = (p.prod_equiv_of_is_compl f.ker h.is_compl).to_linear_map ∘ₗ prod_map id 0 ∘ₗ (p.prod_equiv_of_is_compl f.ker h.is_compl).symm.to_linear_map := begin refine (linear_map.cancel_right (p.prod_equiv_of_is_compl f.ker h.is_compl).surjective).1 _, ext, { simp only [coe_comp, linear_equiv.coe_to_linear_map, coe_inl, function.comp_app, linear_equiv.of_top_apply, linear_equiv.of_injective_apply, coprod_apply, submodule.coe_subtype, coe_zero, add_zero, prod_equiv_of_is_compl_symm_apply_left, prod_map_apply, id_coe, id.def, zero_apply, coe_prod_equiv_of_is_compl', h.map_id x x.2], }, {simp only [coe_comp, linear_equiv.coe_to_linear_map, coe_inr, function.comp_app, linear_equiv.of_top_apply, linear_equiv.of_injective_apply, coprod_apply, submodule.coe_subtype, coe_zero, zero_add, map_coe_ker, prod_equiv_of_is_compl_symm_apply_right, prod_map_apply, id_coe, id.def, zero_apply, coe_prod_equiv_of_is_compl'], } end end is_proj end linear_map end ring section comm_ring namespace linear_map variables {R : Type*} [comm_ring R] {E : Type*} [add_comm_group E] [module R E] {p : submodule R E} lemma is_proj.eq_conj_prod_map {f : E →ₗ[R] E} (h : is_proj p f) : f = (p.prod_equiv_of_is_compl f.ker h.is_compl).conj (prod_map id 0) := by {rw linear_equiv.conj_apply, exact h.eq_conj_prod_map'} end linear_map end comm_ring
c72fa789330c65b8725861f6b2f65df11b73590b
31f556cdeb9239ffc2fad8f905e33987ff4feab9
/stage0/src/Lean/Compiler/LCNF/CompilerM.lean
ae9c0e49d56e67286a0d0ae59a53871ed5d53329
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
tobiasgrosser/lean4
ce0fd9cca0feba1100656679bf41f0bffdbabb71
ebdbdc10436a4d9d6b66acf78aae7a23f5bd073f
refs/heads/master
1,673,103,412,948
1,664,930,501,000
1,664,930,501,000
186,870,185
0
0
Apache-2.0
1,665,129,237,000
1,557,939,901,000
Lean
UTF-8
Lean
false
false
18,725
lean
/- Copyright (c) 2022 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.CoreM import Lean.Compiler.LCNF.Basic import Lean.Compiler.LCNF.LCtx import Lean.Compiler.LCNF.ConfigOptions namespace Lean.Compiler.LCNF /-- The pipeline phase a certain `Pass` is supposed to happen in. -/ inductive Phase where /-- Here we still carry most of the original type information, most of the dependent portion is already (partially) erased though. -/ | base /-- In this phase polymorphism has been eliminated. -/ | mono /-- In this phase impure stuff such as RC or efficient BaseIO transformations happen. -/ | impure deriving Inhabited /-- The state managed by the `CompilerM` `Monad`. -/ structure CompilerM.State where /-- A `LocalContext` to store local declarations from let binders and other constructs in as we move through `Expr`s. -/ lctx : LCtx := {} /-- Next auxiliary variable suffix -/ nextIdx : Nat := 1 deriving Inhabited structure CompilerM.Context where phase : Phase config : ConfigOptions deriving Inhabited abbrev CompilerM := ReaderT CompilerM.Context $ StateRefT CompilerM.State CoreM @[inline] def withPhase (phase : Phase) (x : CompilerM α) : CompilerM α := withReader (fun ctx => { ctx with phase }) x def getPhase : CompilerM Phase := return (← read).phase instance : AddMessageContext CompilerM where addMessageContext msgData := do let env ← getEnv let lctx := (← get).lctx.toLocalContext let opts ← getOptions return MessageData.withContext { env, lctx, opts, mctx := {} } msgData def getType (fvarId : FVarId) : CompilerM Expr := do let lctx := (← get).lctx if let some decl := lctx.letDecls.find? fvarId then return decl.type else if let some decl := lctx.params.find? fvarId then return decl.type else if let some decl := lctx.funDecls.find? fvarId then return decl.type else throwError "unknown free variable {fvarId.name}" def getBinderName (fvarId : FVarId) : CompilerM Name := do let lctx := (← get).lctx if let some decl := lctx.letDecls.find? fvarId then return decl.binderName else if let some decl := lctx.params.find? fvarId then return decl.binderName else if let some decl := lctx.funDecls.find? fvarId then return decl.binderName else throwError "unknown free variable {fvarId.name}" def findParam? (fvarId : FVarId) : CompilerM (Option Param) := return (← get).lctx.params.find? fvarId def findLetDecl? (fvarId : FVarId) : CompilerM (Option LetDecl) := return (← get).lctx.letDecls.find? fvarId def findFunDecl? (fvarId : FVarId) : CompilerM (Option FunDecl) := return (← get).lctx.funDecls.find? fvarId def getParam (fvarId : FVarId) : CompilerM Param := do let some param ← findParam? fvarId | throwError "unknown parameter {fvarId.name}" return param def getLetDecl (fvarId : FVarId) : CompilerM LetDecl := do let some decl ← findLetDecl? fvarId | throwError "unknown let-declaration {fvarId.name}" return decl def getFunDecl (fvarId : FVarId) : CompilerM FunDecl := do let some decl ← findFunDecl? fvarId | throwError "unknown local function {fvarId.name}" return decl @[inline] def modifyLCtx (f : LCtx → LCtx) : CompilerM Unit := do modify fun s => { s with lctx := f s.lctx } def eraseLetDecl (decl : LetDecl) : CompilerM Unit := do modifyLCtx fun lctx => lctx.eraseLetDecl decl def eraseFunDecl (decl : FunDecl) (recursive := true) : CompilerM Unit := do modifyLCtx fun lctx => lctx.eraseFunDecl decl recursive def eraseCode (code : Code) : CompilerM Unit := do modifyLCtx fun lctx => lctx.eraseCode code def eraseParam (param : Param) : CompilerM Unit := modifyLCtx fun lctx => lctx.eraseParam param def eraseParams (params : Array Param) : CompilerM Unit := modifyLCtx fun lctx => lctx.eraseParams params def eraseCodeDecl (decl : CodeDecl) : CompilerM Unit := do match decl with | .let decl => eraseLetDecl decl | .jp decl | .fun decl => eraseFunDecl decl /-- Erase all free variables occurring in `decls` from the local context. -/ def eraseCodeDecls (decls : Array CodeDecl) : CompilerM Unit := do decls.forM fun decl => eraseCodeDecl decl def eraseDecl (decl : Decl) : CompilerM Unit := do eraseParams decl.params eraseCode decl.value /-- A free variable substitution. We use these substitutions when inlining definitions and "internalizing" LCNF code into `CompilerM`. During the internalization process, we ensure all free variables in the LCNF code do not collide with existing ones at the `CompilerM` local context. Remark: in LCNF, (computationally relevant) data is in A-normal form, but this is not the case for types and type formers. So, when inlining we often want to replace a free variable with a type or type former. The substitution contains entries `fvarId ↦ e` s.t., `e` is a valid LCNF argument. That is, it is a free variable, a type (or type former), or `lcErased`. `Check.lean` contains a substitution validator. -/ abbrev FVarSubst := HashMap FVarId Expr /-- Replace the free variables in `e` using the given substitution. If `translator = true`, then we assume the free variables occurring in the range of the substitution are in another local context. For example, `translator = true` during internalization where we are making sure all free variables in a given expression are replaced with new ones that do not collide with the ones in the current local context. If `translator = false`, we assume the substitution contains free variable replacements in the same local context, and given entries such as `x₁ ↦ x₂`, `x₂ ↦ x₃`, ..., `xₙ₋₁ ↦ xₙ`, and the expression `f x₁ x₂`, we want the resulting expression to be `f xₙ xₙ`. We use this setting, for example, in the simplifier. -/ private partial def normExprImp (s : FVarSubst) (e : Expr) (translator : Bool) : Expr := go e where goApp (e : Expr) : Expr := match e with | .app f a => e.updateApp! (goApp f) (go a) | _ => go e go (e : Expr) : Expr := if e.hasFVar then match e with | .fvar fvarId => match s.find? fvarId with | some e => if translator then e else go e | none => e | .lit .. | .const .. | .sort .. | .mvar .. | .bvar .. => e | .app f a => e.updateApp! (goApp f) (go a) |>.headBeta | .mdata _ b => e.updateMData! (go b) | .proj _ _ b => e.updateProj! (go b) | .forallE _ d b _ => e.updateForallE! (go d) (go b) | .lam _ d b _ => e.updateLambdaE! (go d) (go b) | .letE .. => unreachable! -- Valid LCNF does not contain `let`-declarations else e /-- Normalize the given free variable. See `normExprImp` for documentation on the `translator` parameter. This function is meant to be used in contexts where the input free-variable is computationally relevant. This function panics if the substitution is mapping `fvarId` to an expression that is not another free variable. That is, it is not a type (or type former), nor `lcErased`. Recall that a valid `FVarSubst` contains only expressions that are free variables, `lcErased`, or type formers. -/ private partial def normFVarImp (s : FVarSubst) (fvarId : FVarId) (translator : Bool) : FVarId := match s.find? fvarId with | some (.fvar fvarId') => if translator then fvarId' else normFVarImp s fvarId' translator | some e => panic! s!"invalid LCNF substitution of free variable with expression {e}" | none => fvarId /-- Interface for monads that have a free substitutions. -/ class MonadFVarSubst (m : Type → Type) (translator : outParam Bool) where getSubst : m FVarSubst export MonadFVarSubst (getSubst) instance (m n) [MonadLift m n] [MonadFVarSubst m t] : MonadFVarSubst n t where getSubst := liftM (getSubst : m _) class MonadFVarSubstState (m : Type → Type) where modifySubst : (FVarSubst → FVarSubst) → m Unit export MonadFVarSubstState (modifySubst) instance (m n) [MonadLift m n] [MonadFVarSubstState m] : MonadFVarSubstState n where modifySubst f := liftM (modifySubst f : m _) /-- Add the entry `fvarId ↦ fvarId'` to the free variable substitution. -/ @[inline] def addFVarSubst [MonadFVarSubstState m] (fvarId : FVarId) (fvarId' : FVarId) : m Unit := modifySubst fun s => s.insert fvarId (.fvar fvarId') /-- Add the substitution `fvarId ↦ e`, `e` must be a valid LCNF argument. That is, it must be a free variable, type (or type former), or `lcErased`. See `Check.lean` for the free variable substitution checker. -/ @[inline] def addSubst [MonadFVarSubstState m] (fvarId : FVarId) (e : Expr) : m Unit := modifySubst fun s => s.insert fvarId e @[inline, inheritDoc normFVarImp] def normFVar [MonadFVarSubst m t] [Monad m] (fvarId : FVarId) : m FVarId := return normFVarImp (← getSubst) fvarId t @[inline, inheritDoc normExprImp] def normExpr [MonadFVarSubst m t] [Monad m] (e : Expr) : m Expr := return normExprImp (← getSubst) e t /-- Normalize the given expressions using the current substitution. -/ def normExprs [MonadFVarSubst m t] [Monad m] (es : Array Expr) : m (Array Expr) := es.mapMonoM normExpr def mkFreshBinderName (binderName := `_x): CompilerM Name := do let declName := .num binderName (← get).nextIdx modify fun s => { s with nextIdx := s.nextIdx + 1 } return declName private def refreshBinderName (binderName : Name) : CompilerM Name := do match binderName with | .num p _ => let r := .num p (← get).nextIdx modify fun s => { s with nextIdx := s.nextIdx + 1 } return r | _ => return binderName namespace Internalize abbrev InternalizeM := StateRefT FVarSubst CompilerM /- TODO: during internalization we must convert eliminate data that became computationally irrelevant. See note on "erasure confusion" for examples on how this can happen. -/ /-- The `InternalizeM` monad is a translator. It "translates" the free variables in the input expressions and `Code`, into new fresh free variables in the local context. -/ instance : MonadFVarSubst InternalizeM true where getSubst := get instance : MonadFVarSubstState InternalizeM where modifySubst := modify private def mkNewFVarId (fvarId : FVarId) : InternalizeM FVarId := do let fvarId' ← Lean.mkFreshFVarId addFVarSubst fvarId fvarId' return fvarId' def internalizeParam (p : Param) : InternalizeM Param := do let binderName ← refreshBinderName p.binderName let type ← normExpr p.type let fvarId ← mkNewFVarId p.fvarId let p := { p with binderName, fvarId, type } modifyLCtx fun lctx => lctx.addParam p return p def internalizeLetDecl (decl : LetDecl) : InternalizeM LetDecl := do let binderName ← refreshBinderName decl.binderName let type ← normExpr decl.type let value ← normExpr decl.value let fvarId ← mkNewFVarId decl.fvarId let decl := { decl with binderName, fvarId, type, value } modifyLCtx fun lctx => lctx.addLetDecl decl return decl mutual partial def internalizeFunDecl (decl : FunDecl) : InternalizeM FunDecl := do let type ← normExpr decl.type let binderName ← refreshBinderName decl.binderName let params ← decl.params.mapM internalizeParam let value ← internalizeCode decl.value let fvarId ← mkNewFVarId decl.fvarId let decl := { decl with binderName, fvarId, params, type, value } modifyLCtx fun lctx => lctx.addFunDecl decl return decl partial def internalizeCode (code : Code) : InternalizeM Code := do match code with | .let decl k => return .let (← internalizeLetDecl decl) (← internalizeCode k) | .fun decl k => return .fun (← internalizeFunDecl decl) (← internalizeCode k) | .jp decl k => return .jp (← internalizeFunDecl decl) (← internalizeCode k) | .return fvarId => return .return (← normFVar fvarId) | .jmp fvarId args => return .jmp (← normFVar fvarId) (← args.mapM normExpr) | .unreach type => return .unreach (← normExpr type) | .cases c => let resultType ← normExpr c.resultType let discr ← normFVar c.discr let alts ← c.alts.mapM fun | .alt ctorName params k => return .alt ctorName (← params.mapM internalizeParam) (← internalizeCode k) | .default k => return .default (← internalizeCode k) return .cases { c with discr, alts, resultType } end partial def internalizeCodeDecl (decl : CodeDecl) : InternalizeM CodeDecl := do match decl with | .let decl => return .let (← internalizeLetDecl decl) | .fun decl => return .fun (← internalizeFunDecl decl) | .jp decl => return .jp (← internalizeFunDecl decl) end Internalize /-- Refresh free variables ids in `code`, and store their declarations in the local context. -/ partial def Code.internalize (code : Code) (s : FVarSubst := {}) : CompilerM Code := Internalize.internalizeCode code |>.run' s open Internalize in def Decl.internalize (decl : Decl) (s : FVarSubst := {}): CompilerM Decl := go decl |>.run' s where go (decl : Decl) : InternalizeM Decl := do let type ← normExpr decl.type let params ← decl.params.mapM internalizeParam let value ← internalizeCode decl.value return { decl with type, params, value } /-! Helper functions for creating LCNF local declarations. -/ def mkParam (binderName : Name) (type : Expr) (borrow : Bool) : CompilerM Param := do let fvarId ← mkFreshFVarId let param := { fvarId, binderName, type, borrow } modifyLCtx fun lctx => lctx.addParam param return param def mkLetDecl (binderName : Name) (type : Expr) (value : Expr) : CompilerM LetDecl := do let fvarId ← mkFreshFVarId let decl := { fvarId, binderName, type, value } modifyLCtx fun lctx => lctx.addLetDecl decl return decl def mkFunDecl (binderName : Name) (type : Expr) (params : Array Param) (value : Code) : CompilerM FunDecl := do let fvarId ← mkFreshFVarId let funDecl := { fvarId, binderName, type, params, value } modifyLCtx fun lctx => lctx.addFunDecl funDecl return funDecl private unsafe def updateParamImp (p : Param) (type : Expr) : CompilerM Param := do if ptrEq type p.type then return p else let p := { p with type } modifyLCtx fun lctx => lctx.addParam p return p @[implementedBy updateParamImp] opaque Param.update (p : Param) (type : Expr) : CompilerM Param private unsafe def updateLetDeclImp (decl : LetDecl) (type : Expr) (value : Expr) : CompilerM LetDecl := do if ptrEq type decl.type && ptrEq value decl.value then return decl else let decl := { decl with type, value } modifyLCtx fun lctx => lctx.addLetDecl decl return decl @[implementedBy updateLetDeclImp] opaque LetDecl.update (decl : LetDecl) (type : Expr) (value : Expr) : CompilerM LetDecl def LetDecl.updateValue (decl : LetDecl) (value : Expr) : CompilerM LetDecl := decl.update decl.type value private unsafe def updateFunDeclImp (decl: FunDecl) (type : Expr) (params : Array Param) (value : Code) : CompilerM FunDecl := do if ptrEq type decl.type && ptrEq params decl.params && ptrEq value decl.value then return decl else let decl := { decl with type, params, value } modifyLCtx fun lctx => lctx.addFunDecl decl return decl @[implementedBy updateFunDeclImp] opaque FunDeclCore.update (decl: FunDecl) (type : Expr) (params : Array Param) (value : Code) : CompilerM FunDecl abbrev FunDeclCore.update' (decl : FunDecl) (type : Expr) (value : Code) : CompilerM FunDecl := decl.update type decl.params value abbrev FunDeclCore.updateValue (decl : FunDecl) (value : Code) : CompilerM FunDecl := decl.update decl.type decl.params value @[inline] def normParam [MonadLiftT CompilerM m] [Monad m] [MonadFVarSubst m t] (p : Param) : m Param := do p.update (← normExpr p.type) def normParams [MonadLiftT CompilerM m] [Monad m] [MonadFVarSubst m t] (ps : Array Param) : m (Array Param) := ps.mapMonoM normParam def normLetDecl [MonadLiftT CompilerM m] [Monad m] [MonadFVarSubst m t] (decl : LetDecl) : m LetDecl := do decl.update (← normExpr decl.type) (← normExpr decl.value) abbrev NormalizerM (_translator : Bool) := ReaderT FVarSubst CompilerM instance : MonadFVarSubst (NormalizerM t) t where getSubst := read mutual partial def normFunDeclImp (decl : FunDecl) : NormalizerM t FunDecl := do let type ← normExpr decl.type let params ← normParams decl.params let value ← normCodeImp decl.value decl.update type params value partial def normCodeImp (code : Code) : NormalizerM t Code := do match code with | .let decl k => return code.updateLet! (← normLetDecl decl) (← normCodeImp k) | .fun decl k | .jp decl k => return code.updateFun! (← normFunDeclImp decl) (← normCodeImp k) | .return fvarId => return code.updateReturn! (← normFVar fvarId) | .jmp fvarId args => return code.updateJmp! (← normFVar fvarId) (← normExprs args) | .unreach type => return code.updateUnreach! (← normExpr type) | .cases c => let resultType ← normExpr c.resultType let discr ← normFVar c.discr let alts ← c.alts.mapMonoM fun alt => match alt with | .alt _ params k => return alt.updateAlt! (← normParams params) (← normCodeImp k) | .default k => return alt.updateCode (← normCodeImp k) return code.updateCases! resultType discr alts end @[inline] def normFunDecl [MonadLiftT CompilerM m] [Monad m] [MonadFVarSubst m t] (decl : FunDecl) : m FunDecl := do normFunDeclImp (t := t) decl (← getSubst) /-- Similar to `internalize`, but does not refresh `FVarId`s. -/ @[inline] def normCode [MonadLiftT CompilerM m] [Monad m] [MonadFVarSubst m t] (code : Code) : m Code := do normCodeImp (t := t) code (← getSubst) def replaceExprFVars (e : Expr) (s : FVarSubst) (translator : Bool) : CompilerM Expr := (normExpr e : NormalizerM translator Expr).run s def replaceFVars (code : Code) (s : FVarSubst) (translator : Bool) : CompilerM Code := (normCode code : NormalizerM translator Code).run s def mkFreshJpName : CompilerM Name := do mkFreshBinderName `_jp def mkAuxParam (type : Expr) (borrow := false) : CompilerM Param := do mkParam (← mkFreshBinderName `_y) type borrow /-- Create a fresh local context and internalize the given decls. -/ def cleanup (decl : Array Decl) : CompilerM (Array Decl) := do modify fun _ => {} decl.mapM fun decl => do modify fun s => { s with nextIdx := 1 } decl.internalize def getConfig : CompilerM ConfigOptions := return (← read).config def CompilerM.run (x : CompilerM α) (s : State := {}) (phase : Phase := .base) : CoreM α := do x { phase, config := toConfigOptions (← getOptions) } |>.run' s end Lean.Compiler.LCNF
dca239cf29c3659959b0bd20fbf2ad71f0c56266
0c1546a496eccfb56620165cad015f88d56190c5
/library/init/meta/smt/congruence_closure.lean
bfe07135a17b0ff83ff178fa44a7c0f4b127eda8
[ "Apache-2.0" ]
permissive
Solertis/lean
491e0939957486f664498fbfb02546e042699958
84188c5aa1673fdf37a082b2de8562dddf53df3f
refs/heads/master
1,610,174,257,606
1,486,263,620,000
1,486,263,620,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,857
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.meta.tactic init.meta.set_get_option_tactics structure cc_config := /- If tt, congruence closure will treat implicit instance arguments as constants. -/ (ignore_instances : bool := tt) /- If tt, congruence closure modulo AC. -/ (ac : bool := tt) /- If ho_fns is (some fns), then full (and more expensive) support for higher-order functions is *only* considered for the functions in fns and local functions. The performance overhead is described in the paper "Congruence Closure in Intensional Type Theory". If ho_fns is none, then full support is provided for *all* constants. -/ (ho_fns : option (list name) := none) /- If true, then use excluded middle -/ (em : bool := tt) /- Congruence closure state -/ meta constant cc_state : Type meta constant cc_state.mk_core : cc_config → cc_state /- Create a congruence closure state object using the hypotheses in the current goal. -/ meta constant cc_state.mk_using_hs_core : cc_config → tactic cc_state meta constant cc_state.next : cc_state → expr → expr meta constant cc_state.roots_core : cc_state → bool → list expr meta constant cc_state.root : cc_state → expr → expr meta constant cc_state.mt : cc_state → expr → nat meta constant cc_state.gmt : cc_state → nat meta constant cc_state.inc_gmt : cc_state → cc_state meta constant cc_state.is_cg_root : cc_state → expr → bool meta constant cc_state.pp_eqc : cc_state → expr → tactic format meta constant cc_state.pp_core : cc_state → bool → tactic format meta constant cc_state.internalize : cc_state → expr → tactic cc_state meta constant cc_state.add : cc_state → expr → tactic cc_state meta constant cc_state.is_eqv : cc_state → expr → expr → tactic bool meta constant cc_state.is_not_eqv : cc_state → expr → expr → tactic bool meta constant cc_state.eqv_proof : cc_state → expr → expr → tactic expr meta constant cc_state.inconsistent : cc_state → bool /- (proof_for cc e) constructs a proof for e if it is equivalent to true in cc_state -/ meta constant cc_state.proof_for : cc_state → expr → tactic expr /- (refutation_for cc e) constructs a proof for (not e) if it is equivalent to false in cc_state -/ meta constant cc_state.refutation_for : cc_state → expr → tactic expr /- If the given state is inconsistent, return a proof for false. Otherwise fail. -/ meta constant cc_state.proof_for_false : cc_state → tactic expr namespace cc_state meta def mk : cc_state := cc_state.mk_core {} meta def mk_using_hs : tactic cc_state := cc_state.mk_using_hs_core {} meta def roots (s : cc_state) : list expr := cc_state.roots_core s tt meta def pp (s : cc_state) : tactic format := cc_state.pp_core s tt meta def eqc_of_core (s : cc_state) : expr → expr → list expr → list expr | e f r := let n := s^.next e in if n = f then e::r else eqc_of_core n f (e::r) meta def eqc_of (s : cc_state) (e : expr) : list expr := s^.eqc_of_core e e [] meta def in_singlenton_eqc (s : cc_state) (e : expr) : bool := to_bool (s^.next e = e) meta def eqc_size (s : cc_state) (e : expr) : nat := (s^.eqc_of e)^.length end cc_state open tactic meta def tactic.cc_core (cfg : cc_config) : tactic unit := do intros, s ← cc_state.mk_using_hs_core cfg, t ← target, s ← s^.internalize t, if s^.inconsistent then do { pr ← s^.proof_for_false, mk_app `false.elim [t, pr] >>= exact} else do { tr ← return $ expr.const `true [], b ← s^.is_eqv t tr, if b then do { pr ← s^.eqv_proof t tr, mk_app `of_eq_true [pr] >>= exact } else do { dbg ← get_bool_option `trace.cc.failure ff, if dbg then do { ccf ← s^.pp, msg ← return $ to_fmt "cc tactic failed, equivalence classes: " ++ format.line ++ ccf, fail msg } else do { fail "cc tactic failed" } } } meta def tactic.cc : tactic unit := tactic.cc_core {} meta def tactic.cc_dbg_core (cfg : cc_config) : tactic unit := save_options $ set_bool_option `trace.cc.failure tt >> tactic.cc_core cfg meta def tactic.cc_dbg : tactic unit := tactic.cc_dbg_core {} meta def tactic.ac_refl : tactic unit := do (lhs, rhs) ← target >>= match_eq, s ← return $ cc_state.mk, s ← s^.internalize lhs, s ← s^.internalize rhs, b ← s^.is_eqv lhs rhs, if b then do { s^.eqv_proof lhs rhs >>= exact } else do { fail "ac_refl failed" }
cb3e0d5b9c4aec76c8251a8d03e385b9d661d8cd
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/ring_theory/integral_closure.lean
ed9139d3588668acc25be2470abcbf5a10f784ac
[ "Apache-2.0" ]
permissive
abentkamp/mathlib
d9a75d291ec09f4637b0f30cc3880ffb07549ee5
5360e476391508e092b5a1e5210bd0ed22dc0755
refs/heads/master
1,682,382,954,948
1,622,106,077,000
1,622,106,077,000
149,285,665
0
0
null
null
null
null
UTF-8
Lean
false
false
25,823
lean
/- Copyright (c) 2019 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import ring_theory.adjoin.basic import ring_theory.polynomial.scale_roots import ring_theory.polynomial.tower /-! # Integral closure of a subring. If A is an R-algebra then `a : A` is integral over R if it is a root of a monic polynomial with coefficients in R. Enough theory is developed to prove that integral elements form a sub-R-algebra of A. ## Main definitions Let `R` be a `comm_ring` and let `A` be an R-algebra. * `ring_hom.is_integral_elem (f : R →+* A) (x : A)` : `x` is integral with respect to the map `f`, * `is_integral (x : A)` : `x` is integral over `R`, i.e., is a root of a monic polynomial with coefficients in `R`. * `integral_closure R A` : the integral closure of `R` in `A`, regarded as a sub-`R`-algebra of `A`. -/ open_locale classical open_locale big_operators open polynomial submodule section ring variables {R S A : Type*} variables [comm_ring R] [ring A] [ring S] (f : R →+* S) /-- An element `x` of `A` is said to be integral over `R` with respect to `f` if it is a root of a monic polynomial `p : polynomial R` evaluated under `f` -/ def ring_hom.is_integral_elem (f : R →+* A) (x : A) := ∃ p : polynomial R, monic p ∧ eval₂ f x p = 0 /-- A ring homomorphism `f : R →+* A` is said to be integral if every element `A` is integral with respect to the map `f` -/ def ring_hom.is_integral (f : R →+* A) := ∀ x : A, f.is_integral_elem x variables [algebra R A] (R) /-- An element `x` of an algebra `A` over a commutative ring `R` is said to be *integral*, if it is a root of some monic polynomial `p : polynomial R`. Equivalently, the element is integral over `R` with respect to the induced `algebra_map` -/ def is_integral (x : A) : Prop := (algebra_map R A).is_integral_elem x variable (A) /-- An algebra is integral if every element of the extension is integral over the base ring -/ def algebra.is_integral : Prop := (algebra_map R A).is_integral variables {R A} lemma ring_hom.is_integral_map {x : R} : f.is_integral_elem (f x) := ⟨X - C x, monic_X_sub_C _, by simp⟩ theorem is_integral_algebra_map {x : R} : is_integral R (algebra_map R A x) := (algebra_map R A).is_integral_map theorem is_integral_of_noetherian (H : is_noetherian R A) (x : A) : is_integral R x := begin let leval : @linear_map R (polynomial R) A _ _ _ _ _ := (aeval x).to_linear_map, let D : ℕ → submodule R A := λ n, (degree_le R n).map leval, let M := well_founded.min (is_noetherian_iff_well_founded.1 H) (set.range D) ⟨_, ⟨0, rfl⟩⟩, have HM : M ∈ set.range D := well_founded.min_mem _ _ _, cases HM with N HN, have HM : ¬M < D (N+1) := well_founded.not_lt_min (is_noetherian_iff_well_founded.1 H) (set.range D) _ ⟨N+1, rfl⟩, rw ← HN at HM, have HN2 : D (N+1) ≤ D N := classical.by_contradiction (λ H, HM (lt_of_le_not_le (map_mono (degree_le_mono (with_bot.coe_le_coe.2 (nat.le_succ N)))) H)), have HN3 : leval (X^(N+1)) ∈ D N, { exact HN2 (mem_map_of_mem (mem_degree_le.2 (degree_X_pow_le _))) }, rcases HN3 with ⟨p, hdp, hpe⟩, refine ⟨X^(N+1) - p, monic_X_pow_sub (mem_degree_le.1 hdp), _⟩, show leval (X ^ (N + 1) - p) = 0, rw [linear_map.map_sub, hpe, sub_self] end theorem is_integral_of_submodule_noetherian (S : subalgebra R A) (H : is_noetherian R S.to_submodule) (x : A) (hx : x ∈ S) : is_integral R x := begin suffices : is_integral R (show S, from ⟨x, hx⟩), { rcases this with ⟨p, hpm, hpx⟩, replace hpx := congr_arg S.val hpx, refine ⟨p, hpm, eq.trans _ hpx⟩, simp only [aeval_def, eval₂, sum_def], rw S.val.map_sum, refine finset.sum_congr rfl (λ n hn, _), rw [S.val.map_mul, S.val.map_pow, S.val.commutes, S.val_apply, subtype.coe_mk], }, refine is_integral_of_noetherian H ⟨x, hx⟩ end end ring section variables {R A B S : Type*} variables [comm_ring R] [comm_ring A] [comm_ring B] [comm_ring S] variables [algebra R A] [algebra R B] (f : R →+* S) theorem is_integral_alg_hom (f : A →ₐ[R] B) {x : A} (hx : is_integral R x) : is_integral R (f x) := let ⟨p, hp, hpx⟩ := hx in ⟨p, hp, by rw [← aeval_def, aeval_alg_hom_apply, aeval_def, hpx, f.map_zero]⟩ theorem is_integral_of_is_scalar_tower [algebra A B] [is_scalar_tower R A B] (x : B) (hx : is_integral R x) : is_integral A x := let ⟨p, hp, hpx⟩ := hx in ⟨p.map $ algebra_map R A, monic_map _ hp, by rw [← aeval_def, ← is_scalar_tower.aeval_apply, aeval_def, hpx]⟩ section local attribute [instance] subset.comm_ring algebra.of_is_subring theorem is_integral_of_subring {x : A} (T : set R) [is_subring T] (hx : is_integral T x) : is_integral R x := is_integral_of_is_scalar_tower x hx lemma is_integral_algebra_map_iff [algebra A B] [is_scalar_tower R A B] {x : A} (hAB : function.injective (algebra_map A B)) : is_integral R (algebra_map A B x) ↔ is_integral R x := begin split; rintros ⟨f, hf, hx⟩; use [f, hf], { exact is_scalar_tower.aeval_eq_zero_of_aeval_algebra_map_eq_zero R A B hAB hx }, { rw [is_scalar_tower.algebra_map_eq R A B, ← hom_eval₂, hx, ring_hom.map_zero] } end theorem is_integral_iff_is_integral_closure_finite {r : A} : is_integral R r ↔ ∃ s : set R, s.finite ∧ is_integral (ring.closure s) r := begin split; intro hr, { rcases hr with ⟨p, hmp, hpr⟩, refine ⟨_, set.finite_mem_finset _, p.restriction, monic_restriction.2 hmp, _⟩, erw [← aeval_def, is_scalar_tower.aeval_apply _ R, map_restriction, aeval_def, hpr] }, rcases hr with ⟨s, hs, hsr⟩, exact is_integral_of_subring _ hsr end end theorem fg_adjoin_singleton_of_integral (x : A) (hx : is_integral R x) : (algebra.adjoin R ({x} : set A)).to_submodule.fg := begin rcases hx with ⟨f, hfm, hfx⟩, existsi finset.image ((^) x) (finset.range (nat_degree f + 1)), apply le_antisymm, { rw span_le, intros s hs, rw finset.mem_coe at hs, rcases finset.mem_image.1 hs with ⟨k, hk, rfl⟩, clear hk, exact is_submonoid.pow_mem (algebra.subset_adjoin (set.mem_singleton _)) }, intros r hr, change r ∈ algebra.adjoin R ({x} : set A) at hr, rw algebra.adjoin_singleton_eq_range at hr, rcases (aeval x).mem_range.mp hr with ⟨p, rfl⟩, rw ← mod_by_monic_add_div p hfm, rw ← aeval_def at hfx, rw [alg_hom.map_add, alg_hom.map_mul, hfx, zero_mul, add_zero], have : degree (p %ₘ f) ≤ degree f := degree_mod_by_monic_le p hfm, generalize_hyp : p %ₘ f = q at this ⊢, rw [← sum_C_mul_X_eq q, aeval_def, eval₂_sum, sum_def], refine sum_mem _ (λ k hkq, _), rw [eval₂_mul, eval₂_C, eval₂_pow, eval₂_X, ← algebra.smul_def], refine smul_mem _ _ (subset_span _), rw finset.mem_coe, refine finset.mem_image.2 ⟨_, _, rfl⟩, rw [finset.mem_range, nat.lt_succ_iff], refine le_of_not_lt (λ hk, _), rw [degree_le_iff_coeff_zero] at this, rw [mem_support_iff] at hkq, apply hkq, apply this, exact lt_of_le_of_lt degree_le_nat_degree (with_bot.coe_lt_coe.2 hk) end theorem fg_adjoin_of_finite {s : set A} (hfs : s.finite) (his : ∀ x ∈ s, is_integral R x) : (algebra.adjoin R s).to_submodule.fg := set.finite.induction_on hfs (λ _, ⟨{1}, submodule.ext $ λ x, by { erw [algebra.adjoin_empty, finset.coe_singleton, ← one_eq_span, one_eq_map_top, map_top, linear_map.mem_range, algebra.mem_bot], refl }⟩) (λ a s has hs ih his, by rw [← set.union_singleton, algebra.adjoin_union_coe_submodule]; exact fg_mul _ _ (ih $ λ i hi, his i $ set.mem_insert_of_mem a hi) (fg_adjoin_singleton_of_integral _ $ his a $ set.mem_insert a s)) his theorem is_integral_of_mem_of_fg (S : subalgebra R A) (HS : S.to_submodule.fg) (x : A) (hx : x ∈ S) : is_integral R x := begin cases HS with y hy, obtain ⟨lx, hlx1, hlx2⟩ : ∃ (l : A →₀ R) (H : l ∈ finsupp.supported R R ↑y), (finsupp.total A A R id) l = x, { rwa [←(@finsupp.mem_span_iff_total A A R _ _ _ id ↑y x), set.image_id ↑y, hy] }, have hyS : ∀ {p}, p ∈ y → p ∈ S := λ p hp, show p ∈ S.to_submodule, by { rw ← hy, exact subset_span hp }, have : ∀ (jk : (↑(y.product y) : set (A × A))), jk.1.1 * jk.1.2 ∈ S.to_submodule := λ jk, S.mul_mem (hyS (finset.mem_product.1 jk.2).1) (hyS (finset.mem_product.1 jk.2).2), rw [← hy, ← set.image_id ↑y] at this, simp only [finsupp.mem_span_iff_total] at this, choose ly hly1 hly2, let S₀ : set R := ring.closure ↑(lx.frange ∪ finset.bUnion finset.univ (finsupp.frange ∘ ly)), refine is_integral_of_subring S₀ _, letI : comm_ring S₀ := @subtype.comm_ring _ _ _ ring.closure.is_subring, letI : algebra S₀ A := algebra.of_is_subring _, have : span S₀ (insert 1 ↑y : set A) * span S₀ (insert 1 ↑y : set A) ≤ span S₀ (insert 1 ↑y : set A), { rw span_mul_span, refine span_le.2 (λ z hz, _), rcases set.mem_mul.1 hz with ⟨p, q, rfl | hp, hq, rfl⟩, { rw one_mul, exact subset_span hq }, rcases hq with rfl | hq, { rw mul_one, exact subset_span (or.inr hp) }, erw ← hly2 ⟨(p, q), finset.mem_product.2 ⟨hp, hq⟩⟩, rw [finsupp.total_apply, finsupp.sum], refine (span S₀ (insert 1 ↑y : set A)).sum_mem (λ t ht, _), have : ly ⟨(p, q), finset.mem_product.2 ⟨hp, hq⟩⟩ t ∈ S₀ := ring.subset_closure (finset.mem_union_right _ $ finset.mem_bUnion.2 ⟨⟨(p, q), finset.mem_product.2 ⟨hp, hq⟩⟩, finset.mem_univ _, finsupp.mem_frange.2 ⟨finsupp.mem_support_iff.1 ht, _, rfl⟩⟩), change (⟨_, this⟩ : S₀) • t ∈ _, exact smul_mem _ _ (subset_span $ or.inr $ hly1 _ ht) }, haveI : is_subring (span S₀ (insert 1 ↑y : set A) : set A) := { one_mem := subset_span $ or.inl rfl, mul_mem := λ p q hp hq, this $ mul_mem_mul hp hq, zero_mem := (span S₀ (insert 1 ↑y : set A)).zero_mem, add_mem := λ _ _, (span S₀ (insert 1 ↑y : set A)).add_mem, neg_mem := λ _, (span S₀ (insert 1 ↑y : set A)).neg_mem }, have : span S₀ (insert 1 ↑y : set A) = (algebra.adjoin S₀ (↑y : set A)).to_submodule, { refine le_antisymm (span_le.2 $ set.insert_subset.2 ⟨(algebra.adjoin S₀ ↑y).one_mem, algebra.subset_adjoin⟩) (λ z hz, _), rw [subalgebra.mem_to_submodule, algebra.mem_adjoin_iff] at hz, rw ← set_like.mem_coe, refine ring.closure_subset (set.union_subset (set.range_subset_iff.2 $ λ t, _) (λ t ht, subset_span $ or.inr ht)) hz, rw algebra.algebra_map_eq_smul_one, exact smul_mem (span S₀ (insert 1 ↑y : set A)) _ (subset_span $ or.inl rfl) }, haveI : is_noetherian_ring ↥S₀ := is_noetherian_ring_closure _ (finset.finite_to_set _), refine is_integral_of_submodule_noetherian (algebra.adjoin S₀ ↑y) (is_noetherian_of_fg_of_noetherian _ ⟨insert 1 y, by rw [finset.coe_insert, this]⟩) _ _, rw [← hlx2, finsupp.total_apply, finsupp.sum], refine subalgebra.sum_mem _ (λ r hr, _), have : lx r ∈ S₀ := ring.subset_closure (finset.mem_union_left _ (finset.mem_image_of_mem _ hr)), change (⟨_, this⟩ : S₀) • r ∈ _, rw finsupp.mem_supported at hlx1, exact subalgebra.smul_mem _ (algebra.subset_adjoin $ hlx1 hr) _ end lemma ring_hom.is_integral_of_mem_closure {x y z : S} (hx : f.is_integral_elem x) (hy : f.is_integral_elem y) (hz : z ∈ ring.closure ({x, y} : set S)) : f.is_integral_elem z := begin letI : algebra R S := f.to_algebra, have := fg_mul _ _ (fg_adjoin_singleton_of_integral x hx) (fg_adjoin_singleton_of_integral y hy), rw [← algebra.adjoin_union_coe_submodule, set.singleton_union] at this, exact is_integral_of_mem_of_fg (algebra.adjoin R {x, y}) this z (algebra.mem_adjoin_iff.2 $ ring.closure_mono (set.subset_union_right _ _) hz), end theorem is_integral_of_mem_closure {x y z : A} (hx : is_integral R x) (hy : is_integral R y) (hz : z ∈ ring.closure ({x, y} : set A)) : is_integral R z := (algebra_map R A).is_integral_of_mem_closure hx hy hz lemma ring_hom.is_integral_zero : f.is_integral_elem 0 := f.map_zero ▸ f.is_integral_map theorem is_integral_zero : is_integral R (0:A) := (algebra_map R A).is_integral_zero lemma ring_hom.is_integral_one : f.is_integral_elem 1 := f.map_one ▸ f.is_integral_map theorem is_integral_one : is_integral R (1:A) := (algebra_map R A).is_integral_one lemma ring_hom.is_integral_add {x y : S} (hx : f.is_integral_elem x) (hy : f.is_integral_elem y) : f.is_integral_elem (x + y) := f.is_integral_of_mem_closure hx hy (is_add_submonoid.add_mem (ring.subset_closure (or.inl rfl)) (ring.subset_closure (or.inr rfl))) theorem is_integral_add {x y : A} (hx : is_integral R x) (hy : is_integral R y) : is_integral R (x + y) := (algebra_map R A).is_integral_add hx hy lemma ring_hom.is_integral_neg {x : S} (hx : f.is_integral_elem x) : f.is_integral_elem (-x) := f.is_integral_of_mem_closure hx hx (is_add_subgroup.neg_mem (ring.subset_closure (or.inl rfl))) theorem is_integral_neg {x : A} (hx : is_integral R x) : is_integral R (-x) := (algebra_map R A).is_integral_neg hx lemma ring_hom.is_integral_sub {x y : S} (hx : f.is_integral_elem x) (hy : f.is_integral_elem y) : f.is_integral_elem (x - y) := by simpa only [sub_eq_add_neg] using f.is_integral_add hx (f.is_integral_neg hy) theorem is_integral_sub {x y : A} (hx : is_integral R x) (hy : is_integral R y) : is_integral R (x - y) := (algebra_map R A).is_integral_sub hx hy lemma ring_hom.is_integral_mul {x y : S} (hx : f.is_integral_elem x) (hy : f.is_integral_elem y) : f.is_integral_elem (x * y) := f.is_integral_of_mem_closure hx hy (is_submonoid.mul_mem (ring.subset_closure (or.inl rfl)) (ring.subset_closure (or.inr rfl))) theorem is_integral_mul {x y : A} (hx : is_integral R x) (hy : is_integral R y) : is_integral R (x * y) := (algebra_map R A).is_integral_mul hx hy variables (R A) /-- The integral closure of R in an R-algebra A. -/ def integral_closure : subalgebra R A := { carrier := { r | is_integral R r }, zero_mem' := is_integral_zero, one_mem' := is_integral_one, add_mem' := λ _ _, is_integral_add, mul_mem' := λ _ _, is_integral_mul, algebra_map_mem' := λ x, is_integral_algebra_map } theorem mem_integral_closure_iff_mem_fg {r : A} : r ∈ integral_closure R A ↔ ∃ M : subalgebra R A, M.to_submodule.fg ∧ r ∈ M := ⟨λ hr, ⟨algebra.adjoin R {r}, fg_adjoin_singleton_of_integral _ hr, algebra.subset_adjoin rfl⟩, λ ⟨M, Hf, hrM⟩, is_integral_of_mem_of_fg M Hf _ hrM⟩ variables {R} {A} /-- Mapping an integral closure along an `alg_equiv` gives the integral closure. -/ lemma integral_closure_map_alg_equiv (f : A ≃ₐ[R] B) : (integral_closure R A).map (f : A →ₐ[R] B) = integral_closure R B := begin ext y, rw subalgebra.mem_map, split, { rintros ⟨x, hx, rfl⟩, exact is_integral_alg_hom f hx }, { intro hy, use [f.symm y, is_integral_alg_hom (f.symm : B →ₐ[R] A) hy], simp } end lemma integral_closure.is_integral (x : integral_closure R A) : is_integral R x := let ⟨p, hpm, hpx⟩ := x.2 in ⟨p, hpm, subtype.eq $ by rwa [← aeval_def, subtype.val_eq_coe, ← subalgebra.val_apply, aeval_alg_hom_apply] at hpx⟩ lemma ring_hom.is_integral_of_is_integral_mul_unit (x y : S) (r : R) (hr : f r * y = 1) (hx : f.is_integral_elem (x * y)) : f.is_integral_elem x := begin obtain ⟨p, ⟨p_monic, hp⟩⟩ := hx, refine ⟨scale_roots p r, ⟨(monic_scale_roots_iff r).2 p_monic, _⟩⟩, convert scale_roots_eval₂_eq_zero f hp, rw [mul_comm x y, ← mul_assoc, hr, one_mul], end theorem is_integral_of_is_integral_mul_unit {x y : A} {r : R} (hr : algebra_map R A r * y = 1) (hx : is_integral R (x * y)) : is_integral R x := (algebra_map R A).is_integral_of_is_integral_mul_unit x y r hr hx /-- Generalization of `is_integral_of_mem_closure` bootstrapped up from that lemma -/ lemma is_integral_of_mem_closure' (G : set A) (hG : ∀ x ∈ G, is_integral R x) : ∀ x ∈ (subring.closure G), is_integral R x := λ x hx, subring.closure_induction hx hG is_integral_zero is_integral_one (λ _ _, is_integral_add) (λ _, is_integral_neg) (λ _ _, is_integral_mul) lemma is_integral_of_mem_closure'' {S : Type*} [comm_ring S] {f : R →+* S} (G : set S) (hG : ∀ x ∈ G, f.is_integral_elem x) : ∀ x ∈ (subring.closure G), f.is_integral_elem x := λ x hx, @is_integral_of_mem_closure' R S _ _ f.to_algebra G hG x hx end section algebra open algebra variables {R A B S T : Type*} variables [comm_ring R] [comm_ring A] [comm_ring B] [comm_ring S] [comm_ring T] variables [algebra A B] [algebra R B] (f : R →+* S) (g : S →+* T) lemma is_integral_trans_aux (x : B) {p : polynomial A} (pmonic : monic p) (hp : aeval x p = 0) : is_integral (adjoin R (↑(p.map $ algebra_map A B).frange : set B)) x := begin generalize hS : (↑(p.map $ algebra_map A B).frange : set B) = S, have coeffs_mem : ∀ i, (p.map $ algebra_map A B).coeff i ∈ adjoin R S, { intro i, by_cases hi : (p.map $ algebra_map A B).coeff i = 0, { rw hi, exact subalgebra.zero_mem _ }, rw ← hS, exact subset_adjoin (coeff_mem_frange _ _ hi) }, obtain ⟨q, hq⟩ : ∃ q : polynomial (adjoin R S), q.map (algebra_map (adjoin R S) B) = (p.map $ algebra_map A B), { rw ← set.mem_range, exact (polynomial.mem_map_range _).2 (λ i, ⟨⟨_, coeffs_mem i⟩, rfl⟩) }, use q, split, { suffices h : (q.map (algebra_map (adjoin R S) B)).monic, { refine monic_of_injective _ h, exact subtype.val_injective }, { rw hq, exact monic_map _ pmonic } }, { convert hp using 1, replace hq := congr_arg (eval x) hq, convert hq using 1; symmetry; apply eval_map }, end variables [algebra R A] [is_scalar_tower R A B] /-- If A is an R-algebra all of whose elements are integral over R, and x is an element of an A-algebra that is integral over A, then x is integral over R.-/ lemma is_integral_trans (A_int : is_integral R A) (x : B) (hx : is_integral A x) : is_integral R x := begin rcases hx with ⟨p, pmonic, hp⟩, let S : set B := ↑(p.map $ algebra_map A B).frange, refine is_integral_of_mem_of_fg (adjoin R (S ∪ {x})) _ _ (subset_adjoin $ or.inr rfl), refine fg_trans (fg_adjoin_of_finite (finset.finite_to_set _) (λ x hx, _)) _, { rw [finset.mem_coe, frange, finset.mem_image] at hx, rcases hx with ⟨i, _, rfl⟩, rw coeff_map, exact is_integral_alg_hom (is_scalar_tower.to_alg_hom R A B) (A_int _) }, { apply fg_adjoin_singleton_of_integral, exact is_integral_trans_aux _ pmonic hp } end /-- If A is an R-algebra all of whose elements are integral over R, and B is an A-algebra all of whose elements are integral over A, then all elements of B are integral over R.-/ lemma algebra.is_integral_trans (hA : is_integral R A) (hB : is_integral A B) : is_integral R B := λ x, is_integral_trans hA x (hB x) lemma ring_hom.is_integral_trans (hf : f.is_integral) (hg : g.is_integral) : (g.comp f).is_integral := @algebra.is_integral_trans R S T _ _ _ g.to_algebra (g.comp f).to_algebra f.to_algebra (@is_scalar_tower.of_algebra_map_eq R S T _ _ _ f.to_algebra g.to_algebra (g.comp f).to_algebra (ring_hom.comp_apply g f)) hf hg lemma ring_hom.is_integral_of_surjective (hf : function.surjective f) : f.is_integral := λ x, (hf x).rec_on (λ y hy, (hy ▸ f.is_integral_map : f.is_integral_elem x)) lemma is_integral_of_surjective (h : function.surjective (algebra_map R A)) : is_integral R A := (algebra_map R A).is_integral_of_surjective h /-- If `R → A → B` is an algebra tower with `A → B` injective, then if the entire tower is an integral extension so is `R → A` -/ lemma is_integral_tower_bot_of_is_integral (H : function.injective (algebra_map A B)) {x : A} (h : is_integral R (algebra_map A B x)) : is_integral R x := begin rcases h with ⟨p, ⟨hp, hp'⟩⟩, refine ⟨p, ⟨hp, _⟩⟩, rw [is_scalar_tower.algebra_map_eq R A B, ← eval₂_map, eval₂_hom, ← ring_hom.map_zero (algebra_map A B)] at hp', rw [eval₂_eq_eval_map], exact H hp', end lemma ring_hom.is_integral_tower_bot_of_is_integral (hg : function.injective g) (hfg : (g.comp f).is_integral) : f.is_integral := λ x, @is_integral_tower_bot_of_is_integral R S T _ _ _ g.to_algebra (g.comp f).to_algebra f.to_algebra (@is_scalar_tower.of_algebra_map_eq R S T _ _ _ f.to_algebra g.to_algebra (g.comp f).to_algebra (ring_hom.comp_apply g f)) hg x (hfg (g x)) lemma is_integral_tower_bot_of_is_integral_field {R A B : Type*} [comm_ring R] [field A] [comm_ring B] [nontrivial B] [algebra R A] [algebra A B] [algebra R B] [is_scalar_tower R A B] {x : A} (h : is_integral R (algebra_map A B x)) : is_integral R x := is_integral_tower_bot_of_is_integral (algebra_map A B).injective h lemma ring_hom.is_integral_elem_of_is_integral_elem_comp {x : T} (h : (g.comp f).is_integral_elem x) : g.is_integral_elem x := let ⟨p, ⟨hp, hp'⟩⟩ := h in ⟨p.map f, monic_map f hp, by rwa ← eval₂_map at hp'⟩ lemma ring_hom.is_integral_tower_top_of_is_integral (h : (g.comp f).is_integral) : g.is_integral := λ x, ring_hom.is_integral_elem_of_is_integral_elem_comp f g (h x) /-- If `R → A → B` is an algebra tower, then if the entire tower is an integral extension so is `A → B`. -/ lemma is_integral_tower_top_of_is_integral {x : B} (h : is_integral R x) : is_integral A x := begin rcases h with ⟨p, ⟨hp, hp'⟩⟩, refine ⟨p.map (algebra_map R A), ⟨monic_map (algebra_map R A) hp, _⟩⟩, rw [is_scalar_tower.algebra_map_eq R A B, ← eval₂_map] at hp', exact hp', end lemma ring_hom.is_integral_quotient_of_is_integral {I : ideal S} (hf : f.is_integral) : (ideal.quotient_map I f le_rfl).is_integral := begin rintros ⟨x⟩, obtain ⟨p, ⟨p_monic, hpx⟩⟩ := hf x, refine ⟨p.map (ideal.quotient.mk _), ⟨monic_map _ p_monic, _⟩⟩, simpa only [hom_eval₂, eval₂_map] using congr_arg (ideal.quotient.mk I) hpx end lemma is_integral_quotient_of_is_integral {I : ideal A} (hRA : is_integral R A) : is_integral (I.comap (algebra_map R A)).quotient I.quotient := (algebra_map R A).is_integral_quotient_of_is_integral hRA lemma is_integral_quotient_map_iff {I : ideal S} : (ideal.quotient_map I f le_rfl).is_integral ↔ ((ideal.quotient.mk I).comp f : R →+* I.quotient).is_integral := begin let g := ideal.quotient.mk (I.comap f), have := ideal.quotient_map_comp_mk le_rfl, refine ⟨λ h, _, λ h, ring_hom.is_integral_tower_top_of_is_integral g _ (this ▸ h)⟩, refine this ▸ ring_hom.is_integral_trans g (ideal.quotient_map I f le_rfl) _ h, exact ring_hom.is_integral_of_surjective g ideal.quotient.mk_surjective, end /-- If the integral extension `R → S` is injective, and `S` is a field, then `R` is also a field. -/ lemma is_field_of_is_integral_of_is_field {R S : Type*} [integral_domain R] [integral_domain S] [algebra R S] (H : is_integral R S) (hRS : function.injective (algebra_map R S)) (hS : is_field S) : is_field R := begin refine ⟨⟨0, 1, zero_ne_one⟩, mul_comm, λ a ha, _⟩, -- Let `a_inv` be the inverse of `algebra_map R S a`, -- then we need to show that `a_inv` is of the form `algebra_map R S b`. obtain ⟨a_inv, ha_inv⟩ := hS.mul_inv_cancel (λ h, ha (hRS (trans h (ring_hom.map_zero _).symm))), -- Let `p : polynomial R` be monic with root `a_inv`, -- and `q` be `p` with coefficients reversed (so `q(a) = q'(a) * a + 1`). -- We claim that `q(a) = 0`, so `-q'(a)` is the inverse of `a`. obtain ⟨p, p_monic, hp⟩ := H a_inv, use -∑ (i : ℕ) in finset.range p.nat_degree, (p.coeff i) * a ^ (p.nat_degree - i - 1), -- `q(a) = 0`, because multiplying everything with `a_inv^n` gives `p(a_inv) = 0`. -- TODO: this could be a lemma for `polynomial.reverse`. have hq : ∑ (i : ℕ) in finset.range (p.nat_degree + 1), (p.coeff i) * a ^ (p.nat_degree - i) = 0, { apply (algebra_map R S).injective_iff.mp hRS, have a_inv_ne_zero : a_inv ≠ 0 := right_ne_zero_of_mul (mt ha_inv.symm.trans one_ne_zero), refine (mul_eq_zero.mp _).resolve_right (pow_ne_zero p.nat_degree a_inv_ne_zero), rw [eval₂_eq_sum_range] at hp, rw [ring_hom.map_sum, finset.sum_mul], refine (finset.sum_congr rfl (λ i hi, _)).trans hp, rw [ring_hom.map_mul, mul_assoc], congr, have : a_inv ^ p.nat_degree = a_inv ^ (p.nat_degree - i) * a_inv ^ i, { rw [← pow_add a_inv, nat.sub_add_cancel (nat.le_of_lt_succ (finset.mem_range.mp hi))] }, rw [ring_hom.map_pow, this, ← mul_assoc, ← mul_pow, ha_inv, one_pow, one_mul] }, -- Since `q(a) = 0` and `q(a) = q'(a) * a + 1`, we have `a * -q'(a) = 1`. -- TODO: we could use a lemma for `polynomial.div_X` here. rw [finset.sum_range_succ_comm, p_monic.coeff_nat_degree, one_mul, nat.sub_self, pow_zero, add_eq_zero_iff_eq_neg, eq_comm] at hq, rw [mul_comm, ← neg_mul_eq_neg_mul, finset.sum_mul], convert hq using 2, refine finset.sum_congr rfl (λ i hi, _), have : 1 ≤ p.nat_degree - i := nat.le_sub_left_of_add_le (finset.mem_range.mp hi), rw [mul_assoc, ← pow_succ', nat.sub_add_cancel this] end end algebra section local attribute [instance] subset.comm_ring algebra.of_is_subring theorem integral_closure_idem {R : Type*} {A : Type*} [comm_ring R] [comm_ring A] [algebra R A] : integral_closure (integral_closure R A : set A) A = ⊥ := eq_bot_iff.2 $ λ x hx, algebra.mem_bot.2 ⟨⟨x, @is_integral_trans _ _ _ _ _ _ _ _ (integral_closure R A).algebra _ integral_closure.is_integral x hx⟩, rfl⟩ end section integral_domain variables {R S : Type*} [comm_ring R] [integral_domain S] [algebra R S] instance : integral_domain (integral_closure R S) := infer_instance end integral_domain
db41b32ba7c7bc31621968a414717fbd4c7b216f
367134ba5a65885e863bdc4507601606690974c1
/src/algebra/linear_ordered_comm_group_with_zero.lean
e2d289a9f66df7e5414b1d71bdd30e4625ff97c2
[ "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
7,808
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Johan Commelin, Patrick Massot -/ import algebra.ordered_group import algebra.group_with_zero import algebra.group_with_zero.power import tactic.abel /-! # Linearly ordered commutative groups and monoids with a zero element adjoined This file sets up a special class of linearly ordered commutative monoids that show up as the target of so-called “valuations” in algebraic number theory. Usually, in the informal literature, these objects are constructed by taking a linearly ordered commutative group Γ and formally adjoining a zero element: Γ ∪ {0}. The disadvantage is that a type such as `nnreal` is not of that form, whereas it is a very common target for valuations. The solutions is to use a typeclass, and that is exactly what we do in this file. Note that to avoid issues with import cycles, `linear_ordered_comm_monoid_with_zero` is defined in another file. However, the lemmas about it are stated here. -/ set_option old_structure_cmd true /-- A linearly ordered commutative group with a zero element. -/ class linear_ordered_comm_group_with_zero (α : Type*) extends linear_ordered_comm_monoid_with_zero α, comm_group_with_zero α variables {α : Type*} variables {a b c d x y z : α} instance [linear_ordered_add_comm_monoid_with_top α] : linear_ordered_comm_monoid_with_zero (multiplicative (order_dual α)) := { zero := multiplicative.of_add (⊤ : α), zero_mul := top_add, mul_zero := add_top, zero_le_one := (le_top : (0 : α) ≤ ⊤), ..multiplicative.ordered_comm_monoid, ..multiplicative.linear_order } section linear_ordered_comm_monoid variables [linear_ordered_comm_monoid_with_zero α] /- The following facts are true more generally in a (linearly) ordered commutative monoid. -/ /-- Pullback a `linear_ordered_comm_monoid_with_zero` under an injective map. -/ def function.injective.linear_ordered_comm_monoid_with_zero {β : Type*} [has_zero β] [has_one β] [has_mul β] (f : β → α) (hf : function.injective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : linear_ordered_comm_monoid_with_zero β := { zero_le_one := show f 0 ≤ f 1, by simp only [zero, one, linear_ordered_comm_monoid_with_zero.zero_le_one], ..linear_order.lift f hf, ..hf.ordered_comm_monoid f one mul, ..hf.comm_monoid_with_zero f zero one mul } lemma one_le_pow_of_one_le' {n : ℕ} (H : 1 ≤ x) : 1 ≤ x^n := begin induction n with n ih, { exact le_refl 1 }, { exact one_le_mul H ih } end lemma pow_le_one_of_le_one {n : ℕ} (H : x ≤ 1) : x^n ≤ 1 := begin induction n with n ih, { exact le_refl 1 }, { exact mul_le_one' H ih } end lemma eq_one_of_pow_eq_one {n : ℕ} (hn : n ≠ 0) (H : x ^ n = 1) : x = 1 := begin rcases nat.exists_eq_succ_of_ne_zero hn with ⟨n, rfl⟩, clear hn, induction n with n ih, { simpa using H }, { cases le_total x 1 with h, all_goals { have h1 := mul_le_mul_right' h (x ^ (n + 1)), rw pow_succ at H, rw [H, one_mul] at h1 }, { have h2 := pow_le_one_of_le_one h, exact ih (le_antisymm h2 h1) }, { have h2 := one_le_pow_of_one_le' h, exact ih (le_antisymm h1 h2) } } end lemma pow_eq_one_iff {n : ℕ} (hn : n ≠ 0) : x ^ n = 1 ↔ x = 1 := ⟨eq_one_of_pow_eq_one hn, by { rintro rfl, exact one_pow _ }⟩ lemma one_le_pow_iff {n : ℕ} (hn : n ≠ 0) : 1 ≤ x^n ↔ 1 ≤ x := begin refine ⟨_, one_le_pow_of_one_le'⟩, contrapose!, intro h, apply lt_of_le_of_ne (pow_le_one_of_le_one (le_of_lt h)), rw [ne.def, pow_eq_one_iff hn], exact ne_of_lt h, end lemma pow_le_one_iff {n : ℕ} (hn : n ≠ 0) : x^n ≤ 1 ↔ x ≤ 1 := begin refine ⟨_, pow_le_one_of_le_one⟩, contrapose!, intro h, apply lt_of_le_of_ne (one_le_pow_of_one_le' (le_of_lt h)), rw [ne.def, eq_comm, pow_eq_one_iff hn], exact ne_of_gt h, end lemma zero_le_one' : (0 : α) ≤ 1 := linear_ordered_comm_monoid_with_zero.zero_le_one @[simp] lemma zero_le' : 0 ≤ a := by simpa only [mul_zero, mul_one] using mul_le_mul_left' (@zero_le_one' α _) a @[simp] lemma not_lt_zero' : ¬a < 0 := not_lt_of_le zero_le' @[simp] lemma le_zero_iff : a ≤ 0 ↔ a = 0 := ⟨λ h, le_antisymm h zero_le', λ h, h ▸ le_refl _⟩ lemma zero_lt_iff : 0 < a ↔ a ≠ 0 := ⟨ne_of_gt, λ h, lt_of_le_of_ne zero_le' h.symm⟩ lemma ne_zero_of_lt (h : b < a) : a ≠ 0 := λ h1, not_lt_zero' $ show b < 0, from h1 ▸ h instance : linear_ordered_add_comm_monoid_with_top (additive (order_dual α)) := { top := (0 : α), top_add' := λ a, (zero_mul a : (0 : α) * a = 0), le_top := λ _, zero_le', ..additive.ordered_add_comm_monoid, ..additive.linear_order } end linear_ordered_comm_monoid variables [linear_ordered_comm_group_with_zero α] lemma zero_lt_one'' : (0 : α) < 1 := lt_of_le_of_ne zero_le_one' zero_ne_one lemma le_of_le_mul_right (h : c ≠ 0) (hab : a * c ≤ b * c) : a ≤ b := by simpa only [mul_inv_cancel_right' h] using (mul_le_mul_right' hab c⁻¹) lemma le_mul_inv_of_mul_le (h : c ≠ 0) (hab : a * c ≤ b) : a ≤ b * c⁻¹ := le_of_le_mul_right h (by simpa [h] using hab) lemma mul_inv_le_of_le_mul (h : c ≠ 0) (hab : a ≤ b * c) : a * c⁻¹ ≤ b := le_of_le_mul_right h (by simpa [h] using hab) lemma div_le_div' (a b c d : α) (hb : b ≠ 0) (hd : d ≠ 0) : a * b⁻¹ ≤ c * d⁻¹ ↔ a * d ≤ c * b := begin by_cases ha : a = 0, { simp [ha] }, by_cases hc : c = 0, { simp [inv_ne_zero hb, hc, hd], }, exact @div_le_div_iff' _ _ (units.mk0 a ha) (units.mk0 b hb) (units.mk0 c hc) (units.mk0 d hd) end @[simp] lemma units.zero_lt (u : units α) : (0 : α) < u := zero_lt_iff.2 $ u.ne_zero lemma mul_lt_mul'''' (hab : a < b) (hcd : c < d) : a * c < b * d := have hb : b ≠ 0 := ne_zero_of_lt hab, have hd : d ≠ 0 := ne_zero_of_lt hcd, if ha : a = 0 then by { rw [ha, zero_mul, zero_lt_iff], exact mul_ne_zero hb hd } else if hc : c = 0 then by { rw [hc, mul_zero, zero_lt_iff], exact mul_ne_zero hb hd } else @mul_lt_mul''' _ _ (units.mk0 a ha) (units.mk0 b hb) (units.mk0 c hc) (units.mk0 d hd) hab hcd lemma mul_inv_lt_of_lt_mul' (h : x < y * z) : x * z⁻¹ < y := have hz : z ≠ 0 := (mul_ne_zero_iff.1 $ ne_zero_of_lt h).2, by { contrapose! h, simpa only [inv_inv'] using mul_inv_le_of_le_mul (inv_ne_zero hz) h } lemma mul_lt_right' (c : α) (h : a < b) (hc : c ≠ 0) : a * c < b * c := by { contrapose! h, exact le_of_le_mul_right hc h } lemma pow_lt_pow_succ {x : α} {n : ℕ} (hx : 1 < x) : x ^ n < x ^ n.succ := by { rw ← one_mul (x ^ n), exact mul_lt_right' _ hx (pow_ne_zero _ $ ne_of_gt (lt_trans zero_lt_one'' hx)) } lemma pow_lt_pow' {x : α} {m n : ℕ} (hx : 1 < x) (hmn : m < n) : x ^ m < x ^ n := by { induction hmn with n hmn ih, exacts [pow_lt_pow_succ hx, lt_trans ih (pow_lt_pow_succ hx)] } lemma inv_lt_inv'' (ha : a ≠ 0) (hb : b ≠ 0) : a⁻¹ < b⁻¹ ↔ b < a := @inv_lt_inv_iff _ _ (units.mk0 a ha) (units.mk0 b hb) lemma inv_le_inv'' (ha : a ≠ 0) (hb : b ≠ 0) : a⁻¹ ≤ b⁻¹ ↔ b ≤ a := @inv_le_inv_iff _ _ (units.mk0 a ha) (units.mk0 b hb) namespace monoid_hom variables {R : Type*} [ring R] (f : R →* α) theorem map_neg_one : f (-1) = 1 := begin apply eq_one_of_pow_eq_one (nat.succ_ne_zero 1) (_ : _ ^ 2 = _), rw [pow_two, ← f.map_mul, neg_one_mul, neg_neg, f.map_one], end @[simp] lemma map_neg (x : R) : f (-x) = f x := calc f (-x) = f (-1 * x) : by rw [neg_one_mul] ... = f (-1) * f x : map_mul _ _ _ ... = f x : by rw [f.map_neg_one, one_mul] lemma map_sub_swap (x y : R) : f (x - y) = f (y - x) := calc f (x - y) = f (-(y - x)) : by rw show x - y = -(y-x), by abel ... = _ : map_neg _ _ end monoid_hom
6e3354023587082dec37e1a706ecb60700f1da85
57aec6ee746bc7e3a3dd5e767e53bd95beb82f6d
/tests/lean/run/IO_test.lean
8e0e2ed8595b88638d34da7298a771c8e28c2098
[ "Apache-2.0" ]
permissive
collares/lean4
861a9269c4592bce49b71059e232ff0bfe4594cc
52a4f535d853a2c7c7eea5fee8a4fa04c682c1ee
refs/heads/master
1,691,419,031,324
1,618,678,138,000
1,618,678,138,000
358,989,750
0
0
Apache-2.0
1,618,696,333,000
1,618,696,333,000
null
UTF-8
Lean
false
false
2,880
lean
prelude import Init.System.IO import Init.Data.List.Control import Init.Data.ToString open IO.FS def check_eq {α} [BEq α] [Repr α] (tag : String) (expected actual : α) : IO Unit := unless (expected == actual) do throw $ IO.userError $ s!"assertion failure \"{tag}\":\n expected: {repr expected}\n actual: {repr actual}" def test : IO Unit := do let xs : ByteArray := ⟨#[1,2,3,4]⟩; let fn := "foo.txt"; withFile fn Mode.write fun h => do h.write xs; h.write xs; pure (); let ys ← withFile "foo.txt" Mode.read $ fun h => h.read 10; check_eq "1" (xs.toList ++ xs.toList) ys.toList; withFile fn Mode.append fun h => do h.write ⟨#[5,6,7,8]⟩; pure (); withFile "foo.txt" Mode.read fun h => do let ys ← h.read 10 check_eq "2" [1,2,3,4,1,2,3,4,5,6] ys.toList let ys ← h.read 2 check_eq "3" [7,8] ys.toList let b ← h.isEof unless !b do throw $ IO.userError $ "wrong (4): " let ys ← h.read 2 check_eq "5" [] ys.toList let b ← h.isEof unless b do throw $ IO.userError $ "wrong (6): " pure () #eval test def test2 : IO Unit := do let fn2 := "foo2.txt"; let xs₀ : String := "⟨[₂,α]⟩"; let xs₁ := "⟨[6,8,@]⟩"; let xs₂ := "/* Handle.getLine : Handle → IO Unit */" ++ "/* The line returned by `lean_io_prim_handle_get_line` */" ++ "/* is truncated at the first \'\\0\' character and the */" ++ "/* rest of the line is discarded. */"; -- multi-buffer line withFile fn2 Mode.write $ fun h => pure (); withFile fn2 Mode.write $ fun h => do { h.putStr xs₀; h.putStrLn xs₀; h.putStrLn xs₂; h.putStrLn xs₁; pure () }; let ys ← withFile fn2 Mode.read $ fun h => h.getLine; IO.println ys; check_eq "1" (xs₀ ++ xs₀ ++ "\n") ys; IO.println ys; withFile fn2 Mode.append $ fun h => do { h.putStrLn xs₁; pure () }; let ys ← withFile fn2 Mode.read $ fun h => do { let ys ← (List.iota 4).mapM $ fun i => do { let ln ← h.getLine; IO.println i; IO.println ∘ repr $ ln; let b ← h.isEof; unless i == 1 || !b do throw $ IO.userError "isEof" pure ln }; pure ys }; IO.println ys; let rs := [xs₀ ++ xs₀ ++ "\n", xs₂ ++ "\n", xs₁ ++ "\n", xs₁ ++ "\n"]; check_eq "2" rs ys; let ys ← readFile fn2; check_eq "3" (String.join rs) ys; pure () #eval test2 def test3 : IO Unit := do let fn3 := "foo3.txt" let xs₀ := "abc" let xs₁ := "" let xs₂ := "hello" let xs₃ := "world" withFile fn3 Mode.write $ fun h => do { pure () } let ys ← lines fn3 IO.println $ repr ys check_eq "1" ys #[] withFile fn3 Mode.write $ fun h => do h.putStrLn xs₀ h.putStrLn xs₁ h.putStrLn xs₂ h.putStrLn xs₃ let ys ← lines fn3 IO.println $ repr ys check_eq "2" ys #[xs₀, xs₁, xs₂, xs₃] pure () #eval test3
6043e66acbd09956a1ac4f055d4ac343d51dc67c
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/ring_theory/multiplicity.lean
a0bfb28b600319f376c38948ce90c5ebd90a04aa
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
22,821
lean
/- Copyright (c) 2018 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis, Chris Hughes -/ import algebra.associated import algebra.big_operators.basic import ring_theory.valuation.basic /-! # Multiplicity of a divisor > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. For a commutative monoid, this file introduces the notion of multiplicity of a divisor and proves several basic results on it. ## Main definitions * `multiplicity a b`: for two elements `a` and `b` of a commutative monoid returns the largest number `n` such that `a ^ n ∣ b` or infinity, written `⊤`, if `a ^ n ∣ b` for all natural numbers `n`. * `multiplicity.finite a b`: a predicate denoting that the multiplicity of `a` in `b` is finite. -/ variables {α : Type*} open nat part open_locale big_operators /-- `multiplicity a b` returns the largest natural number `n` such that `a ^ n ∣ b`, as an `part_enat` or natural with infinity. If `∀ n, a ^ n ∣ b`, then it returns `⊤`-/ def multiplicity [monoid α] [decidable_rel ((∣) : α → α → Prop)] (a b : α) : part_enat := part_enat.find $ λ n, ¬a ^ (n + 1) ∣ b namespace multiplicity section monoid variables [monoid α] /-- `multiplicity.finite a b` indicates that the multiplicity of `a` in `b` is finite. -/ @[reducible] def finite (a b : α) : Prop := ∃ n : ℕ, ¬a ^ (n + 1) ∣ b lemma finite_iff_dom [decidable_rel ((∣) : α → α → Prop)] {a b : α} : finite a b ↔ (multiplicity a b).dom := iff.rfl lemma finite_def {a b : α} : finite a b ↔ ∃ n : ℕ, ¬a ^ (n + 1) ∣ b := iff.rfl lemma not_dvd_one_of_finite_one_right {a : α} : finite a 1 → ¬a ∣ 1 := λ ⟨n, hn⟩ ⟨d, hd⟩, hn ⟨d ^ (n + 1), (pow_mul_pow_eq_one (n + 1) hd.symm).symm⟩ @[norm_cast] theorem int.coe_nat_multiplicity (a b : ℕ) : multiplicity (a : ℤ) (b : ℤ) = multiplicity a b := begin apply part.ext', { repeat { rw [← finite_iff_dom, finite_def] }, norm_cast }, { intros h1 h2, apply _root_.le_antisymm; { apply nat.find_mono, norm_cast, simp } } end lemma not_finite_iff_forall {a b : α} : (¬ finite a b) ↔ ∀ n : ℕ, a ^ n ∣ b := ⟨λ h n, nat.cases_on n (by { rw pow_zero, exact one_dvd _ }) (by simpa [finite, not_not] using h), by simp [finite, multiplicity, not_not]; tauto⟩ lemma not_unit_of_finite {a b : α} (h : finite a b) : ¬is_unit a := let ⟨n, hn⟩ := h in hn ∘ is_unit.dvd ∘ is_unit.pow (n + 1) lemma finite_of_finite_mul_right {a b c : α} : finite a (b * c) → finite a b := λ ⟨n, hn⟩, ⟨n, λ h, hn (h.trans (dvd_mul_right _ _))⟩ variable [decidable_rel ((∣) : α → α → Prop)] lemma pow_dvd_of_le_multiplicity {a b : α} {k : ℕ} : (k : part_enat) ≤ multiplicity a b → a ^ k ∣ b := by { rw ← part_enat.some_eq_coe, exact nat.cases_on k (λ _, by { rw pow_zero, exact one_dvd _ }) (λ k ⟨h₁, h₂⟩, by_contradiction (λ hk, (nat.find_min _ (lt_of_succ_le (h₂ ⟨k, hk⟩)) hk))) } lemma pow_multiplicity_dvd {a b : α} (h : finite a b) : a ^ get (multiplicity a b) h ∣ b := pow_dvd_of_le_multiplicity (by rw part_enat.coe_get) lemma is_greatest {a b : α} {m : ℕ} (hm : multiplicity a b < m) : ¬a ^ m ∣ b := λ h, by rw [part_enat.lt_coe_iff] at hm; exact nat.find_spec hm.fst ((pow_dvd_pow _ hm.snd).trans h) lemma is_greatest' {a b : α} {m : ℕ} (h : finite a b) (hm : get (multiplicity a b) h < m) : ¬a ^ m ∣ b := is_greatest (by rwa [← part_enat.coe_lt_coe, part_enat.coe_get] at hm) lemma pos_of_dvd {a b : α} (hfin : finite a b) (hdiv : a ∣ b) : 0 < (multiplicity a b).get hfin := begin refine zero_lt_iff.2 (λ h, _), simpa [hdiv] using (is_greatest' hfin (lt_one_iff.mpr h)), end lemma unique {a b : α} {k : ℕ} (hk : a ^ k ∣ b) (hsucc : ¬a ^ (k + 1) ∣ b) : (k : part_enat) = multiplicity a b := le_antisymm (le_of_not_gt (λ hk', is_greatest hk' hk)) $ have finite a b, from ⟨k, hsucc⟩, by { rw [part_enat.le_coe_iff], exact ⟨this, nat.find_min' _ hsucc⟩ } lemma unique' {a b : α} {k : ℕ} (hk : a ^ k ∣ b) (hsucc : ¬ a ^ (k + 1) ∣ b) : k = get (multiplicity a b) ⟨k, hsucc⟩ := by rw [← part_enat.coe_inj, part_enat.coe_get, unique hk hsucc] lemma le_multiplicity_of_pow_dvd {a b : α} {k : ℕ} (hk : a ^ k ∣ b) : (k : part_enat) ≤ multiplicity a b := le_of_not_gt $ λ hk', is_greatest hk' hk lemma pow_dvd_iff_le_multiplicity {a b : α} {k : ℕ} : a ^ k ∣ b ↔ (k : part_enat) ≤ multiplicity a b := ⟨le_multiplicity_of_pow_dvd, pow_dvd_of_le_multiplicity⟩ lemma multiplicity_lt_iff_neg_dvd {a b : α} {k : ℕ} : multiplicity a b < (k : part_enat) ↔ ¬ a ^ k ∣ b := by { rw [pow_dvd_iff_le_multiplicity, not_le] } lemma eq_coe_iff {a b : α} {n : ℕ} : multiplicity a b = (n : part_enat) ↔ a ^ n ∣ b ∧ ¬a ^ (n + 1) ∣ b := begin rw [← part_enat.some_eq_coe], exact ⟨λ h, let ⟨h₁, h₂⟩ := eq_some_iff.1 h in h₂ ▸ ⟨pow_multiplicity_dvd _, is_greatest (by { rw [part_enat.lt_coe_iff], exact ⟨h₁, lt_succ_self _⟩ })⟩, λ h, eq_some_iff.2 ⟨⟨n, h.2⟩, eq.symm $ unique' h.1 h.2⟩⟩ end lemma eq_top_iff {a b : α} : multiplicity a b = ⊤ ↔ ∀ n : ℕ, a ^ n ∣ b := (part_enat.find_eq_top_iff _).trans $ by { simp only [not_not], exact ⟨λ h n, nat.cases_on n (by { rw pow_zero, exact one_dvd _}) (λ n, h _), λ h n, h _⟩ } @[simp] lemma is_unit_left {a : α} (b : α) (ha : is_unit a) : multiplicity a b = ⊤ := eq_top_iff.2 (λ _, is_unit.dvd (ha.pow _)) @[simp] lemma one_left (b : α) : multiplicity 1 b = ⊤ := is_unit_left b is_unit_one @[simp] lemma get_one_right {a : α} (ha : finite a 1) : get (multiplicity a 1) ha = 0 := begin rw [part_enat.get_eq_iff_eq_coe, eq_coe_iff, pow_zero], simp [not_dvd_one_of_finite_one_right ha], end @[simp] lemma unit_left (a : α) (u : αˣ) : multiplicity (u : α) a = ⊤ := is_unit_left a u.is_unit lemma multiplicity_eq_zero {a b : α} : multiplicity a b = 0 ↔ ¬ a ∣ b := by { rw [← nat.cast_zero, eq_coe_iff], simp } lemma multiplicity_ne_zero {a b : α} : multiplicity a b ≠ 0 ↔ a ∣ b := multiplicity_eq_zero.not_left lemma eq_top_iff_not_finite {a b : α} : multiplicity a b = ⊤ ↔ ¬ finite a b := part.eq_none_iff' lemma ne_top_iff_finite {a b : α} : multiplicity a b ≠ ⊤ ↔ finite a b := by rw [ne.def, eq_top_iff_not_finite, not_not] lemma lt_top_iff_finite {a b : α} : multiplicity a b < ⊤ ↔ finite a b := by rw [lt_top_iff_ne_top, ne_top_iff_finite] lemma exists_eq_pow_mul_and_not_dvd {a b : α} (hfin : finite a b) : ∃ (c : α), b = a ^ ((multiplicity a b).get hfin) * c ∧ ¬ a ∣ c := begin obtain ⟨c, hc⟩ := multiplicity.pow_multiplicity_dvd hfin, refine ⟨c, hc, _⟩, rintro ⟨k, hk⟩, rw [hk, ← mul_assoc, ← pow_succ'] at hc, have h₁ : a ^ ((multiplicity a b).get hfin + 1) ∣ b := ⟨k, hc⟩, exact (multiplicity.eq_coe_iff.1 (by simp)).2 h₁, end open_locale classical lemma multiplicity_le_multiplicity_iff {a b c d : α} : multiplicity a b ≤ multiplicity c d ↔ (∀ n : ℕ, a ^ n ∣ b → c ^ n ∣ d) := ⟨λ h n hab, (pow_dvd_of_le_multiplicity (le_trans (le_multiplicity_of_pow_dvd hab) h)), λ h, if hab : finite a b then by rw [← part_enat.coe_get (finite_iff_dom.1 hab)]; exact le_multiplicity_of_pow_dvd (h _ (pow_multiplicity_dvd _)) else have ∀ n : ℕ, c ^ n ∣ d, from λ n, h n (not_finite_iff_forall.1 hab _), by rw [eq_top_iff_not_finite.2 hab, eq_top_iff_not_finite.2 (not_finite_iff_forall.2 this)]⟩ lemma multiplicity_eq_multiplicity_iff {a b c d : α} : multiplicity a b = multiplicity c d ↔ (∀ n : ℕ, a ^ n ∣ b ↔ c ^ n ∣ d) := ⟨λ h n, ⟨multiplicity_le_multiplicity_iff.mp h.le n, multiplicity_le_multiplicity_iff.mp h.ge n⟩, λ h, le_antisymm (multiplicity_le_multiplicity_iff.mpr (λ n, (h n).mp)) (multiplicity_le_multiplicity_iff.mpr (λ n, (h n).mpr))⟩ lemma multiplicity_le_multiplicity_of_dvd_right {a b c : α} (h : b ∣ c) : multiplicity a b ≤ multiplicity a c := multiplicity_le_multiplicity_iff.2 $ λ n hb, hb.trans h lemma eq_of_associated_right {a b c : α} (h : associated b c) : multiplicity a b = multiplicity a c := le_antisymm (multiplicity_le_multiplicity_of_dvd_right h.dvd) (multiplicity_le_multiplicity_of_dvd_right h.symm.dvd) lemma dvd_of_multiplicity_pos {a b : α} (h : (0 : part_enat) < multiplicity a b) : a ∣ b := begin rw ← pow_one a, apply pow_dvd_of_le_multiplicity, simpa only [nat.cast_one, part_enat.pos_iff_one_le] using h end lemma dvd_iff_multiplicity_pos {a b : α} : (0 : part_enat) < multiplicity a b ↔ a ∣ b := ⟨dvd_of_multiplicity_pos, λ hdvd, lt_of_le_of_ne (zero_le _) (λ heq, is_greatest (show multiplicity a b < ↑1, by simpa only [heq, nat.cast_zero] using part_enat.coe_lt_coe.mpr zero_lt_one) (by rwa pow_one a))⟩ lemma finite_nat_iff {a b : ℕ} : finite a b ↔ (a ≠ 1 ∧ 0 < b) := begin rw [← not_iff_not, not_finite_iff_forall, not_and_distrib, ne.def, not_not, not_lt, le_zero_iff], exact ⟨λ h, or_iff_not_imp_right.2 (λ hb, have ha : a ≠ 0, from λ ha, by simpa [ha] using h 1, by_contradiction (λ ha1 : a ≠ 1, have ha_gt_one : 1 < a, from lt_of_not_ge (λ ha', by { clear h, revert ha ha1, dec_trivial! }), not_lt_of_ge (le_of_dvd (nat.pos_of_ne_zero hb) (h b)) (lt_pow_self ha_gt_one b))), λ h, by cases h; simp *⟩ end alias dvd_iff_multiplicity_pos ↔ _ _root_.has_dvd.dvd.multiplicity_pos end monoid section comm_monoid variables [comm_monoid α] lemma finite_of_finite_mul_left {a b c : α} : finite a (b * c) → finite a c := by rw mul_comm; exact finite_of_finite_mul_right variable [decidable_rel ((∣) : α → α → Prop)] lemma is_unit_right {a b : α} (ha : ¬is_unit a) (hb : is_unit b) : multiplicity a b = 0 := eq_coe_iff.2 ⟨show a ^ 0 ∣ b, by simp only [pow_zero, one_dvd], by { rw pow_one, exact λ h, mt (is_unit_of_dvd_unit h) ha hb }⟩ lemma one_right {a : α} (ha : ¬is_unit a) : multiplicity a 1 = 0 := is_unit_right ha is_unit_one lemma unit_right {a : α} (ha : ¬is_unit a) (u : αˣ) : multiplicity a u = 0 := is_unit_right ha u.is_unit open_locale classical lemma multiplicity_le_multiplicity_of_dvd_left {a b c : α} (hdvd : a ∣ b) : multiplicity b c ≤ multiplicity a c := multiplicity_le_multiplicity_iff.2 $ λ n h, (pow_dvd_pow_of_dvd hdvd n).trans h lemma eq_of_associated_left {a b c : α} (h : associated a b) : multiplicity b c = multiplicity a c := le_antisymm (multiplicity_le_multiplicity_of_dvd_left h.dvd) (multiplicity_le_multiplicity_of_dvd_left h.symm.dvd) alias dvd_iff_multiplicity_pos ↔ _ _root_.has_dvd.dvd.multiplicity_pos end comm_monoid section monoid_with_zero variable [monoid_with_zero α] lemma ne_zero_of_finite {a b : α} (h : finite a b) : b ≠ 0 := let ⟨n, hn⟩ := h in λ hb, by simpa [hb] using hn variable [decidable_rel ((∣) : α → α → Prop)] @[simp] protected lemma zero (a : α) : multiplicity a 0 = ⊤ := part.eq_none_iff.2 (λ n ⟨⟨k, hk⟩, _⟩, hk (dvd_zero _)) @[simp] lemma multiplicity_zero_eq_zero_of_ne_zero (a : α) (ha : a ≠ 0) : multiplicity 0 a = 0 := multiplicity.multiplicity_eq_zero.2 $ mt zero_dvd_iff.1 ha end monoid_with_zero section comm_monoid_with_zero variable [comm_monoid_with_zero α] variable [decidable_rel ((∣) : α → α → Prop)] lemma multiplicity_mk_eq_multiplicity [decidable_rel ((∣) : associates α → associates α → Prop)] {a b : α} : multiplicity (associates.mk a) (associates.mk b) = multiplicity a b := begin by_cases h : finite a b, { rw ← part_enat.coe_get (finite_iff_dom.mp h), refine (multiplicity.unique (show (associates.mk a)^(((multiplicity a b).get h)) ∣ associates.mk b, from _) _).symm ; rw [← associates.mk_pow, associates.mk_dvd_mk], { exact pow_multiplicity_dvd h }, { exact is_greatest ((part_enat.lt_coe_iff _ _).mpr (exists.intro (finite_iff_dom.mp h) (nat.lt_succ_self _))) } }, { suffices : ¬ (finite (associates.mk a) (associates.mk b)), { rw [finite_iff_dom, part_enat.not_dom_iff_eq_top] at h this, rw [h, this] }, refine not_finite_iff_forall.mpr (λ n, by { rw [← associates.mk_pow, associates.mk_dvd_mk], exact not_finite_iff_forall.mp h n }) } end end comm_monoid_with_zero section semiring variables [semiring α] [decidable_rel ((∣) : α → α → Prop)] lemma min_le_multiplicity_add {p a b : α} : min (multiplicity p a) (multiplicity p b) ≤ multiplicity p (a + b) := (le_total (multiplicity p a) (multiplicity p b)).elim (λ h, by rw [min_eq_left h, multiplicity_le_multiplicity_iff]; exact λ n hn, dvd_add hn (multiplicity_le_multiplicity_iff.1 h n hn)) (λ h, by rw [min_eq_right h, multiplicity_le_multiplicity_iff]; exact λ n hn, dvd_add (multiplicity_le_multiplicity_iff.1 h n hn) hn) end semiring section ring variables [ring α] [decidable_rel ((∣) : α → α → Prop)] @[simp] protected lemma neg (a b : α) : multiplicity a (-b) = multiplicity a b := part.ext' (by simp only [multiplicity, part_enat.find, dvd_neg]) (λ h₁ h₂, part_enat.coe_inj.1 (by rw [part_enat.coe_get]; exact eq.symm (unique (pow_multiplicity_dvd _).neg_right (mt dvd_neg.1 (is_greatest' _ (lt_succ_self _)))))) theorem int.nat_abs (a : ℕ) (b : ℤ) : multiplicity a b.nat_abs = multiplicity (a : ℤ) b := begin cases int.nat_abs_eq b with h h; conv_rhs { rw h }, { rw [int.coe_nat_multiplicity], }, { rw [multiplicity.neg, int.coe_nat_multiplicity], }, end lemma multiplicity_add_of_gt {p a b : α} (h : multiplicity p b < multiplicity p a) : multiplicity p (a + b) = multiplicity p b := begin apply le_antisymm, { apply part_enat.le_of_lt_add_one, cases part_enat.ne_top_iff.mp (part_enat.ne_top_of_lt h) with k hk, rw [hk], rw_mod_cast [multiplicity_lt_iff_neg_dvd, dvd_add_right], intro h_dvd, apply multiplicity.is_greatest _ h_dvd, rw [hk], apply_mod_cast nat.lt_succ_self, rw [pow_dvd_iff_le_multiplicity, nat.cast_add, ← hk, nat.cast_one], exact part_enat.add_one_le_of_lt h }, { convert min_le_multiplicity_add, rw [min_eq_right (le_of_lt h)] } end lemma multiplicity_sub_of_gt {p a b : α} (h : multiplicity p b < multiplicity p a) : multiplicity p (a - b) = multiplicity p b := by { rw [sub_eq_add_neg, multiplicity_add_of_gt]; rwa [multiplicity.neg] } lemma multiplicity_add_eq_min {p a b : α} (h : multiplicity p a ≠ multiplicity p b) : multiplicity p (a + b) = min (multiplicity p a) (multiplicity p b) := begin rcases lt_trichotomy (multiplicity p a) (multiplicity p b) with hab|hab|hab, { rw [add_comm, multiplicity_add_of_gt hab, min_eq_left], exact le_of_lt hab }, { contradiction }, { rw [multiplicity_add_of_gt hab, min_eq_right], exact le_of_lt hab}, end end ring section cancel_comm_monoid_with_zero variables [cancel_comm_monoid_with_zero α] lemma finite_mul_aux {p : α} (hp : prime p) : ∀ {n m : ℕ} {a b : α}, ¬p ^ (n + 1) ∣ a → ¬p ^ (m + 1) ∣ b → ¬p ^ (n + m + 1) ∣ a * b | n m := λ a b ha hb ⟨s, hs⟩, have p ∣ a * b, from ⟨p ^ (n + m) * s, by simp [hs, pow_add, mul_comm, mul_assoc, mul_left_comm]⟩, (hp.2.2 a b this).elim (λ ⟨x, hx⟩, have hn0 : 0 < n, from nat.pos_of_ne_zero (λ hn0, by clear _fun_match _fun_match; simpa [hx, hn0] using ha), have wf : (n - 1) < n, from tsub_lt_self hn0 dec_trivial, have hpx : ¬ p ^ (n - 1 + 1) ∣ x, from λ ⟨y, hy⟩, ha (hx.symm ▸ ⟨y, mul_right_cancel₀ hp.1 $ by rw [tsub_add_cancel_of_le (succ_le_of_lt hn0)] at hy; simp [hy, pow_add, mul_comm, mul_assoc, mul_left_comm]⟩), have 1 ≤ n + m, from le_trans hn0 (nat.le_add_right n m), finite_mul_aux hpx hb ⟨s, mul_right_cancel₀ hp.1 begin rw [tsub_add_eq_add_tsub (succ_le_of_lt hn0), tsub_add_cancel_of_le this], clear _fun_match _fun_match finite_mul_aux, simp [*, mul_comm, mul_assoc, mul_left_comm, pow_add] at * end⟩) (λ ⟨x, hx⟩, have hm0 : 0 < m, from nat.pos_of_ne_zero (λ hm0, by clear _fun_match _fun_match; simpa [hx, hm0] using hb), have wf : (m - 1) < m, from tsub_lt_self hm0 dec_trivial, have hpx : ¬ p ^ (m - 1 + 1) ∣ x, from λ ⟨y, hy⟩, hb (hx.symm ▸ ⟨y, mul_right_cancel₀ hp.1 $ by rw [tsub_add_cancel_of_le (succ_le_of_lt hm0)] at hy; simp [hy, pow_add, mul_comm, mul_assoc, mul_left_comm]⟩), finite_mul_aux ha hpx ⟨s, mul_right_cancel₀ hp.1 begin rw [add_assoc, tsub_add_cancel_of_le (succ_le_of_lt hm0)], clear _fun_match _fun_match finite_mul_aux, simp [*, mul_comm, mul_assoc, mul_left_comm, pow_add] at * end⟩) lemma finite_mul {p a b : α} (hp : prime p) : finite p a → finite p b → finite p (a * b) := λ ⟨n, hn⟩ ⟨m, hm⟩, ⟨n + m, finite_mul_aux hp hn hm⟩ lemma finite_mul_iff {p a b : α} (hp : prime p) : finite p (a * b) ↔ finite p a ∧ finite p b := ⟨λ h, ⟨finite_of_finite_mul_right h, finite_of_finite_mul_left h⟩, λ h, finite_mul hp h.1 h.2⟩ lemma finite_pow {p a : α} (hp : prime p) : Π {k : ℕ} (ha : finite p a), finite p (a ^ k) | 0 ha := ⟨0, by simp [mt is_unit_iff_dvd_one.2 hp.2.1]⟩ | (k+1) ha := by rw [pow_succ]; exact finite_mul hp ha (finite_pow ha) variable [decidable_rel ((∣) : α → α → Prop)] @[simp] lemma multiplicity_self {a : α} (ha : ¬is_unit a) (ha0 : a ≠ 0) : multiplicity a a = 1 := by { rw ← nat.cast_one, exact eq_coe_iff.2 ⟨by simp, λ ⟨b, hb⟩, ha (is_unit_iff_dvd_one.2 ⟨b, mul_left_cancel₀ ha0 $ by { clear _fun_match, simpa [pow_succ, mul_assoc] using hb }⟩)⟩ } @[simp] lemma get_multiplicity_self {a : α} (ha : finite a a) : get (multiplicity a a) ha = 1 := part_enat.get_eq_iff_eq_coe.2 (eq_coe_iff.2 ⟨by simp, λ ⟨b, hb⟩, by rw [← mul_one a, pow_add, pow_one, mul_assoc, mul_assoc, mul_right_inj' (ne_zero_of_finite ha)] at hb; exact mt is_unit_iff_dvd_one.2 (not_unit_of_finite ha) ⟨b, by clear _fun_match; simp * at *⟩⟩) protected lemma mul' {p a b : α} (hp : prime p) (h : (multiplicity p (a * b)).dom) : get (multiplicity p (a * b)) h = get (multiplicity p a) ((finite_mul_iff hp).1 h).1 + get (multiplicity p b) ((finite_mul_iff hp).1 h).2 := have hdiva : p ^ get (multiplicity p a) ((finite_mul_iff hp).1 h).1 ∣ a, from pow_multiplicity_dvd _, have hdivb : p ^ get (multiplicity p b) ((finite_mul_iff hp).1 h).2 ∣ b, from pow_multiplicity_dvd _, have hpoweq : p ^ (get (multiplicity p a) ((finite_mul_iff hp).1 h).1 + get (multiplicity p b) ((finite_mul_iff hp).1 h).2) = p ^ get (multiplicity p a) ((finite_mul_iff hp).1 h).1 * p ^ get (multiplicity p b) ((finite_mul_iff hp).1 h).2, by simp [pow_add], have hdiv : p ^ (get (multiplicity p a) ((finite_mul_iff hp).1 h).1 + get (multiplicity p b) ((finite_mul_iff hp).1 h).2) ∣ a * b, by rw [hpoweq]; apply mul_dvd_mul; assumption, have hsucc : ¬p ^ ((get (multiplicity p a) ((finite_mul_iff hp).1 h).1 + get (multiplicity p b) ((finite_mul_iff hp).1 h).2) + 1) ∣ a * b, from λ h, by exact not_or (is_greatest' _ (lt_succ_self _)) (is_greatest' _ (lt_succ_self _)) (_root_.succ_dvd_or_succ_dvd_of_succ_sum_dvd_mul hp hdiva hdivb h), by rw [← part_enat.coe_inj, part_enat.coe_get, eq_coe_iff]; exact ⟨hdiv, hsucc⟩ open_locale classical protected lemma mul {p a b : α} (hp : prime p) : multiplicity p (a * b) = multiplicity p a + multiplicity p b := if h : finite p a ∧ finite p b then by rw [← part_enat.coe_get (finite_iff_dom.1 h.1), ← part_enat.coe_get (finite_iff_dom.1 h.2), ← part_enat.coe_get (finite_iff_dom.1 (finite_mul hp h.1 h.2)), ← nat.cast_add, part_enat.coe_inj, multiplicity.mul' hp]; refl else begin rw [eq_top_iff_not_finite.2 (mt (finite_mul_iff hp).1 h)], cases not_and_distrib.1 h with h h; simp [eq_top_iff_not_finite.2 h] end lemma finset.prod {β : Type*} {p : α} (hp : prime p) (s : finset β) (f : β → α) : multiplicity p (∏ x in s, f x) = ∑ x in s, multiplicity p (f x) := begin classical, induction s using finset.induction with a s has ih h, { simp only [finset.sum_empty, finset.prod_empty], convert one_right hp.not_unit }, { simp [has, ← ih], convert multiplicity.mul hp } end protected lemma pow' {p a : α} (hp : prime p) (ha : finite p a) : ∀ {k : ℕ}, get (multiplicity p (a ^ k)) (finite_pow hp ha) = k * get (multiplicity p a) ha | 0 := by simp [one_right hp.not_unit] | (k+1) := have multiplicity p (a ^ (k + 1)) = multiplicity p (a * a ^ k), by rw pow_succ, by rw [get_eq_get_of_eq _ _ this, multiplicity.mul' hp, pow', add_mul, one_mul, add_comm] lemma pow {p a : α} (hp : prime p) : ∀ {k : ℕ}, multiplicity p (a ^ k) = k • (multiplicity p a) | 0 := by simp [one_right hp.not_unit] | (succ k) := by simp [pow_succ, succ_nsmul, pow, multiplicity.mul hp] lemma multiplicity_pow_self {p : α} (h0 : p ≠ 0) (hu : ¬ is_unit p) (n : ℕ) : multiplicity p (p ^ n) = n := by { rw [eq_coe_iff], use dvd_rfl, rw [pow_dvd_pow_iff h0 hu], apply nat.not_succ_le_self } lemma multiplicity_pow_self_of_prime {p : α} (hp : prime p) (n : ℕ) : multiplicity p (p ^ n) = n := multiplicity_pow_self hp.ne_zero hp.not_unit n end cancel_comm_monoid_with_zero section valuation variables {R : Type*} [comm_ring R] [is_domain R] {p : R} [decidable_rel (has_dvd.dvd : R → R → Prop)] /-- `multiplicity` of a prime inan integral domain as an additive valuation to `part_enat`. -/ noncomputable def add_valuation (hp : prime p) : add_valuation R part_enat := add_valuation.of (multiplicity p) (multiplicity.zero _) (one_right hp.not_unit) (λ _ _, min_le_multiplicity_add) (λ a b, multiplicity.mul hp) @[simp] lemma add_valuation_apply {hp : prime p} {r : R} : add_valuation hp r = multiplicity p r := rfl end valuation end multiplicity section nat open multiplicity lemma multiplicity_eq_zero_of_coprime {p a b : ℕ} (hp : p ≠ 1) (hle : multiplicity p a ≤ multiplicity p b) (hab : nat.coprime a b) : multiplicity p a = 0 := begin rw [multiplicity_le_multiplicity_iff] at hle, rw [← nonpos_iff_eq_zero, ← not_lt, part_enat.pos_iff_one_le, ← nat.cast_one, ← pow_dvd_iff_le_multiplicity], assume h, have := nat.dvd_gcd h (hle _ h), rw [coprime.gcd_eq_one hab, nat.dvd_one, pow_one] at this, exact hp this end end nat
09e90e9ae6490fbd731ff8cb3b26501fb4ffcf50
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/run/stateRef.lean
9438d59ad973ae1e7753e7b797f442aa2cb493c6
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,580
lean
new_frontend def f (v : Nat) : StateRefT Nat IO Nat := do IO.println "hello" modify fun s => s - v get def g : IO Nat := f 5 $.run' 20 #eval (f 5).run' 20 #eval (do set 100; f 5 : StateRefT Nat IO Nat).run' 0 def f2 : ReaderT Nat (StateRefT Nat IO) Nat := do let v ← read IO.println $ "context " ++ toString v modify fun s => s + v get #eval (f2.run 10).run' 20 def f3 : StateT String (StateRefT Nat IO) Nat := do let s ← get let n ← getThe Nat set $ s ++ ", " ++ toString n let s ← get IO.println s set (n+1) getThe Nat #eval (f3.run' "test").run' 10 structure Label {β : Type} (v : β) (α : Type) := (val : α) class HasGetAt {β : Type} (v : β) (α : outParam Type) (m : Type → Type) := (getAt : m α) instance monadState.hasGetAt (β : Type) (v : β) (α : Type) (m : Type → Type) [Monad m] [MonadStateOf (Label v α) m] : HasGetAt v α m := { getAt := do let a ← getThe (Label v α); pure a.val } export HasGetAt (getAt) abbrev M := StateRefT (Label 0 Nat) $ StateRefT (Label 1 Nat) $ StateRefT (Label 2 Nat) IO def f4 : M Nat := do let a0 : Nat ← getAt 0 let a1 ← getAt 1 let a2 ← getAt 2 IO.println $ "state0 " ++ toString a0 IO.println $ "state1 " ++ toString a1 IO.println $ "state1 " ++ toString a2 pure (a0 + a1 + a2) #eval f4.run' ⟨10⟩ $.run' ⟨20⟩ $.run' ⟨30⟩ abbrev S (ω : Type) := StateRefT Nat $ StateRefT String $ ST ω def f5 {ω} : S ω Unit := do let s ← getThe String modify fun n => n + s.length pure () def f5Pure (n : Nat) (s : String) := runST fun _ => f5.run n $.run s #eval f5Pure 10 "hello world"
b82d5deff17b550325cece92187d10109b1555d8
130c49f47783503e462c16b2eff31933442be6ff
/stage0/src/Lean/Server/FileWorker/RequestHandling.lean
e90334d4f6c1495ddc54fb358ba54f29f9dc00ec
[ "Apache-2.0" ]
permissive
Hazel-Brown/lean4
8aa5860e282435ffc30dcdfccd34006c59d1d39c
79e6732fc6bbf5af831b76f310f9c488d44e7a16
refs/heads/master
1,689,218,208,951
1,629,736,869,000
1,629,736,896,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
17,407
lean
/- Copyright (c) 2021 Wojciech Nawrocki. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Wojciech Nawrocki, Marc Huisinga -/ import Lean.DeclarationRange import Lean.Data.Json import Lean.Data.Lsp import Lean.Server.FileWorker.Utils import Lean.Server.Requests import Lean.Server.Completion namespace Lean.Server.FileWorker open Lsp open RequestM open Snapshots partial def handleCompletion (p : CompletionParams) : RequestM (RequestTask CompletionList) := do let doc ← readDoc let text := doc.meta.text let pos := text.lspPosToUtf8Pos p.position -- dbg_trace ">> handleCompletion invoked {pos}" -- NOTE: use `>=` since the cursor can be *after* the input withWaitFindSnap doc (fun s => s.endPos >= pos) (notFoundX := pure { items := #[], isIncomplete := true }) fun snap => do for infoTree in snap.cmdState.infoState.trees do -- for (ctx, info) in infoTree.getCompletionInfos do -- dbg_trace "{← info.format ctx}" if let some r ← Completion.find? doc.meta.text pos infoTree then return r return { items := #[ ], isIncomplete := true } open Elab in partial def handleHover (p : HoverParams) : RequestM (RequestTask (Option Hover)) := do let doc ← readDoc let text := doc.meta.text let mkHover (s : String) (f : String.Pos) (t : String.Pos) : Hover := { contents := { kind := MarkupKind.markdown value := s } range? := some { start := text.utf8PosToLspPos f «end» := text.utf8PosToLspPos t } } let hoverPos := text.lspPosToUtf8Pos p.position withWaitFindSnap doc (fun s => s.endPos > hoverPos) (notFoundX := pure none) fun snap => do for t in snap.cmdState.infoState.trees do if let some (ci, i) := t.hoverableInfoAt? hoverPos then if let some hoverFmt ← i.fmtHover? ci then return some <| mkHover (toString hoverFmt) i.pos?.get! i.tailPos?.get! return none inductive GoToKind | declaration | definition | type deriving DecidableEq open Elab GoToKind in partial def handleDefinition (kind : GoToKind) (p : TextDocumentPositionParams) : RequestM (RequestTask (Array LocationLink)) := do let rc ← read let doc ← readDoc let text := doc.meta.text let hoverPos := text.lspPosToUtf8Pos p.position let locationLinksFromDecl (i : Elab.Info) (n : Name) := do let mod? ← findModuleOf? n let modUri? ← match mod? with | some modName => let modFname? ← rc.srcSearchPath.findWithExt "lean" modName pure <| modFname?.map toFileUri | none => pure <| some doc.meta.uri let ranges? ← findDeclarationRanges? n if let (some ranges, some modUri) := (ranges?, modUri?) then let declRangeToLspRange (r : DeclarationRange) : Lsp.Range := { start := ⟨r.pos.line - 1, r.charUtf16⟩ «end» := ⟨r.endPos.line - 1, r.endCharUtf16⟩ } let ll : LocationLink := { originSelectionRange? := (·.toLspRange text) <$> i.range? targetUri := modUri targetRange := declRangeToLspRange ranges.range targetSelectionRange := declRangeToLspRange ranges.selectionRange } return #[ll] return #[] let locationLinksFromBinder (t : InfoTree) (i : Elab.Info) (id : FVarId) := do if let some i' := t.findInfo? fun | Info.ofTermInfo { isBinder := true, expr := Expr.fvar id' .., .. } => id' == id | _ => false then if let some r := i'.range? then let r := r.toLspRange text let ll : LocationLink := { originSelectionRange? := (·.toLspRange text) <$> i.range? targetUri := p.textDocument.uri targetRange := r targetSelectionRange := r } return #[ll] return #[] withWaitFindSnap doc (fun s => s.endPos > hoverPos) (notFoundX := pure #[]) fun snap => do for t in snap.cmdState.infoState.trees do if let some (ci, i) := t.hoverableInfoAt? hoverPos then if let Info.ofTermInfo ti := i then let mut expr := ti.expr if kind = type then expr ← ci.runMetaM i.lctx do Expr.getAppFn (← Meta.instantiateMVars (← Meta.inferType expr)) match expr with | Expr.const n .. => return ← ci.runMetaM i.lctx <| locationLinksFromDecl i n | Expr.fvar id .. => return ← ci.runMetaM i.lctx <| locationLinksFromBinder t i id | _ => pure () if let Info.ofFieldInfo fi := i then if kind = type then let expr ← ci.runMetaM i.lctx do Meta.instantiateMVars (← Meta.inferType fi.val) if let some n := expr.getAppFn.constName? then return ← ci.runMetaM i.lctx <| locationLinksFromDecl i n else return ← ci.runMetaM i.lctx <| locationLinksFromDecl i fi.projName -- If other go-tos fail, we try to show the elaborator or parser if let some ei := i.toElabInfo? then if kind = declaration ∧ ci.env.contains ei.stx.getKind then return ← ci.runMetaM i.lctx <| locationLinksFromDecl i ei.stx.getKind if kind = definition ∧ ci.env.contains ei.elaborator then return ← ci.runMetaM i.lctx <| locationLinksFromDecl i ei.elaborator return #[] open Elab in partial def handlePlainGoal (p : PlainGoalParams) : RequestM (RequestTask (Option PlainGoal)) := do let doc ← readDoc let text := doc.meta.text let hoverPos := text.lspPosToUtf8Pos p.position -- NOTE: use `>=` since the cursor can be *after* the input withWaitFindSnap doc (fun s => s.endPos >= hoverPos) (notFoundX := return none) fun snap => do for t in snap.cmdState.infoState.trees do if let rs@(_ :: _) := t.goalsAt? doc.meta.text hoverPos then let goals ← List.join <$> rs.mapM fun { ctxInfo := ci, tacticInfo := ti, useAfter := useAfter } => let ci := if useAfter then { ci with mctx := ti.mctxAfter } else { ci with mctx := ti.mctxBefore } let goals := if useAfter then ti.goalsAfter else ti.goalsBefore ci.runMetaM {} <| goals.mapM (fun g => Meta.withPPInaccessibleNames (Meta.ppGoal g)) let md := if goals.isEmpty then "no goals" else let goals := goals.map fun goal => s!"```lean {goal} ```" String.intercalate "\n---\n" goals return some { goals := goals.map toString |>.toArray, rendered := md } return none open Elab in open Meta in partial def handlePlainTermGoal (p : PlainTermGoalParams) : RequestM (RequestTask (Option PlainTermGoal)) := do let doc ← readDoc let text := doc.meta.text let hoverPos := text.lspPosToUtf8Pos p.position withWaitFindSnap doc (fun s => s.endPos > hoverPos) (notFoundX := pure none) fun snap => do for t in snap.cmdState.infoState.trees do if let some (ci, i@(Info.ofTermInfo ti)) := t.termGoalAt? hoverPos then let ty ← ci.runMetaM i.lctx do instantiateMVars <| ti.expectedType?.getD (← inferType ti.expr) -- for binders, hide the last hypothesis (the binder itself) let lctx' := if ti.isBinder then i.lctx.pop else i.lctx let goal ← ci.runMetaM lctx' do withPPInaccessibleNames <| Meta.ppGoal (← mkFreshExprMVar ty).mvarId! let range := if let some r := i.range? then r.toLspRange text else ⟨p.position, p.position⟩ return some { goal := toString goal, range } return none partial def handleDocumentHighlight (p : DocumentHighlightParams) : RequestM (RequestTask (Array DocumentHighlight)) := do let doc ← readDoc let text := doc.meta.text let pos := text.lspPosToUtf8Pos p.position let rec highlightReturn? (doRange? : Option Range) : Syntax → Option DocumentHighlight | stx@`(doElem|return%$i $e) => do if let some range := i.getRange? then if range.contains pos then return some { range := doRange?.getD (range.toLspRange text), kind? := DocumentHighlightKind.text } highlightReturn? doRange? e | `(do%$i $elems) => highlightReturn? (i.getRange?.get!.toLspRange text) elems | stx => stx.getArgs.findSome? (highlightReturn? doRange?) withWaitFindSnap doc (fun s => s.endPos > pos) (notFoundX := pure #[]) fun snap => do if let some hi := highlightReturn? none snap.stx then return #[hi] return #[] section -- TODO https://github.com/leanprover/lean4/issues/529 open Parser.Command partial def handleDocumentSymbol (p : DocumentSymbolParams) : RequestM (RequestTask DocumentSymbolResult) := do let doc ← readDoc asTask do let ⟨cmdSnaps, e?⟩ ← doc.cmdSnaps.updateFinishedPrefix let mut stxs := cmdSnaps.finishedPrefix.map Snapshot.stx match e? with | some ElabTaskError.aborted => throw RequestError.fileChanged | some (ElabTaskError.ioError e) => throwThe IO.Error e | _ => () let lastSnap := cmdSnaps.finishedPrefix.getLastD doc.headerSnap stxs := stxs ++ (← parseAhead doc.meta.text.source lastSnap).toList let (syms, _) := toDocumentSymbols doc.meta.text stxs return { syms := syms.toArray } where toDocumentSymbols (text : FileMap) | [] => ([], []) | stx::stxs => match stx with | `(namespace $id) => sectionLikeToDocumentSymbols text stx stxs (id.getId.toString) SymbolKind.namespace id | `(section $(id)?) => sectionLikeToDocumentSymbols text stx stxs ((·.getId.toString) <$> id |>.getD "<section>") SymbolKind.namespace (id.getD stx) | `(end $(id)?) => ([], stx::stxs) | _ => do let (syms, stxs') := toDocumentSymbols text stxs unless stx.isOfKind ``Lean.Parser.Command.declaration do return (syms, stxs') if let some stxRange := stx.getRange? then let (name, selection) := match stx with | `($dm:declModifiers $ak:attrKind instance $[$np:namedPrio]? $[$id:ident$[.{$ls,*}]?]? $sig:declSig $val) => ((·.getId.toString) <$> id |>.getD s!"instance {sig.reprint.getD ""}", id.getD sig) | _ => match stx[1][1] with | `(declId|$id:ident$[.{$ls,*}]?) => (id.getId.toString, id) | _ => (stx[1][0].isIdOrAtom?.getD "<unknown>", stx[1][0]) if let some selRange := selection.getRange? then return (DocumentSymbol.mk { name := name kind := SymbolKind.method range := stxRange.toLspRange text selectionRange := selRange.toLspRange text } :: syms, stxs') return (syms, stxs') sectionLikeToDocumentSymbols (text : FileMap) (stx : Syntax) (stxs : List Syntax) (name : String) (kind : SymbolKind) (selection : Syntax) := let (syms, stxs') := toDocumentSymbols text stxs -- discard `end` let (syms', stxs'') := toDocumentSymbols text (stxs'.drop 1) let endStx := match stxs' with | endStx::_ => endStx | [] => (stx::stxs').getLast! -- we can assume that commands always have at least one position (see `parseCommand`) let range := (mkNullNode #[stx, endStx]).getRange?.get!.toLspRange text (DocumentSymbol.mk { name kind range selectionRange := selection.getRange? |>.map (·.toLspRange text) |>.getD range children? := syms.toArray } :: syms', stxs'') end def noHighlightKinds : Array SyntaxNodeKind := #[ -- usually have special highlighting by the client ``Lean.Parser.Term.sorry, ``Lean.Parser.Term.type, ``Lean.Parser.Term.prop, -- not really keywords `antiquotName, ``Lean.Parser.Command.docComment] structure SemanticTokensContext where beginPos : String.Pos endPos : String.Pos text : FileMap infoState : Elab.InfoState structure SemanticTokensState where data : Array Nat lastLspPos : Lsp.Position partial def handleSemanticTokens (beginPos endPos : String.Pos) : RequestM (RequestTask SemanticTokens) := do let doc ← readDoc let text := doc.meta.text let t ← doc.cmdSnaps.waitAll (·.beginPos < endPos) mapTask t fun (snaps, _) => StateT.run' (s := { data := #[], lastLspPos := ⟨0, 0⟩ : SemanticTokensState }) do for s in snaps do if s.endPos <= beginPos then continue ReaderT.run (r := SemanticTokensContext.mk beginPos endPos text s.cmdState.infoState) <| go s.stx return { data := (← get).data } where go (stx : Syntax) := do match stx with | `($e.$id:ident) => go e; addToken id SemanticTokenType.property -- indistinguishable from next pattern --| `(level|$id:ident) => addToken id SemanticTokenType.variable | `($id:ident) => highlightId id | _ => if !noHighlightKinds.contains stx.getKind then highlightKeyword stx if stx.isOfKind choiceKind then go stx[0] else stx.getArgs.forM go highlightId (stx : Syntax) : ReaderT SemanticTokensContext (StateT SemanticTokensState RequestM) _ := do if let some range := stx.getRange? then for t in (← read).infoState.trees do for ti in t.deepestNodes (fun | _, i@(Elab.Info.ofTermInfo ti), _ => match i.pos? with | some ipos => if range.contains ipos then some ti else none | _ => none | _, _, _ => none) do match ti.expr with | Expr.fvar .. => addToken ti.stx SemanticTokenType.variable | _ => if ti.stx.getPos?.get! > range.start then addToken ti.stx SemanticTokenType.property highlightKeyword stx := do if let Syntax.atom info val := stx then if val.bsize > 0 && val[0].isAlpha then addToken stx SemanticTokenType.keyword addToken stx type := do let ⟨beginPos, endPos, text, _⟩ ← read if let (some pos, some tailPos) := (stx.getPos?, stx.getTailPos?) then if beginPos <= pos && pos < endPos then let lspPos := (← get).lastLspPos let lspPos' := text.utf8PosToLspPos pos let deltaLine := lspPos'.line - lspPos.line let deltaStart := lspPos'.character - (if lspPos'.line == lspPos.line then lspPos.character else 0) let length := (text.utf8PosToLspPos tailPos).character - lspPos'.character let tokenType := type.toNat let tokenModifiers := 0 modify fun st => { data := st.data ++ #[deltaLine, deltaStart, length, tokenType, tokenModifiers] lastLspPos := lspPos' } def handleSemanticTokensFull (p : SemanticTokensParams) : RequestM (RequestTask SemanticTokens) := do handleSemanticTokens 0 (1 <<< 16) def handleSemanticTokensRange (p : SemanticTokensRangeParams) : RequestM (RequestTask SemanticTokens) := do let doc ← readDoc let text := doc.meta.text let beginPos := text.lspPosToUtf8Pos p.range.start let endPos := text.lspPosToUtf8Pos p.range.end handleSemanticTokens beginPos endPos partial def handleWaitForDiagnostics (p : WaitForDiagnosticsParams) : RequestM (RequestTask WaitForDiagnostics) := do let rec waitLoop : RequestM EditableDocument := do let doc ← readDoc if p.version ≤ doc.meta.version then return doc else IO.sleep 50 waitLoop let t ← RequestM.asTask waitLoop RequestM.bindTask t fun doc? => do let doc ← doc? let t₁ ← doc.cmdSnaps.waitAll return t₁.map fun _ => pure WaitForDiagnostics.mk builtin_initialize registerLspRequestHandler "textDocument/waitForDiagnostics" WaitForDiagnosticsParams WaitForDiagnostics handleWaitForDiagnostics registerLspRequestHandler "textDocument/completion" CompletionParams CompletionList handleCompletion registerLspRequestHandler "textDocument/hover" HoverParams (Option Hover) handleHover registerLspRequestHandler "textDocument/declaration" TextDocumentPositionParams (Array LocationLink) (handleDefinition GoToKind.declaration) registerLspRequestHandler "textDocument/definition" TextDocumentPositionParams (Array LocationLink) (handleDefinition GoToKind.definition) registerLspRequestHandler "textDocument/typeDefinition" TextDocumentPositionParams (Array LocationLink) (handleDefinition GoToKind.type) registerLspRequestHandler "textDocument/documentHighlight" DocumentHighlightParams DocumentHighlightResult handleDocumentHighlight registerLspRequestHandler "textDocument/documentSymbol" DocumentSymbolParams DocumentSymbolResult handleDocumentSymbol registerLspRequestHandler "textDocument/semanticTokens/full" SemanticTokensParams SemanticTokens handleSemanticTokensFull registerLspRequestHandler "textDocument/semanticTokens/range" SemanticTokensRangeParams SemanticTokens handleSemanticTokensRange registerLspRequestHandler "$/lean/plainGoal" PlainGoalParams (Option PlainGoal) handlePlainGoal registerLspRequestHandler "$/lean/plainTermGoal" PlainTermGoalParams (Option PlainTermGoal) handlePlainTermGoal end Lean.Server.FileWorker
65ed74da2dfd0a94e0b3defd20c5cdb1b30a1c1e
76df16d6c3760cb415f1294caee997cc4736e09b
/lean/src/cs/lib.lean
5adfbc591952a3cb3f566f90bad61e5c4b7374e3
[ "MIT" ]
permissive
uw-unsat/leanette-popl22-artifact
70409d9cbd8921d794d27b7992bf1d9a4087e9fe
80fea2519e61b45a283fbf7903acdf6d5528dbe7
refs/heads/master
1,681,592,449,670
1,637,037,431,000
1,637,037,431,000
414,331,908
6
1
null
null
null
null
UTF-8
Lean
false
false
16,686
lean
import tactic.basic import tactic.omega import tactic.apply_fun import data.option.basic import .util import .lang import .sym import .svm namespace sym --- The following lemmas depend on (has_eval Model SymB SymV SymR). -- section eval_generic variables {Model SymB SymV SymR : Type} (ev : has_eval Model SymB SymV SymR) {m : Model} lemma choices.one_of_ite {α} {b b' : SymB} {a1 a2 : α} : ev.evalB m b' = ¬ ev.evalB m b → choices.one ev m [⟨b, a1⟩, ⟨b', a2⟩] := begin simp only [choices.one, choices.true, list.filter, list.map], intro hb, split_ifs, any_goals { simp only [list.length_singleton], }, { simp only [h, to_bool_false_eq_ff, not_true] at hb, rewrite ←bool_iff_false at hb, contradiction, }, { simp only [h, to_bool_true_eq_tt, not_false_iff] at hb, rewrite bool.tt_eq_true at hb, contradiction, }, end lemma choices.eq_one {α β} {gvs : choices SymB α} {gws : choices SymB β} : gvs.map choice.guard = gws.map choice.guard → gvs.one ev m → gws.one ev m := begin intros heq, simp only [choices.one, choices.true, heq, imp_self], end lemma choices.not_some_is_none {α} {gvs : choices SymB α} : ¬ (gvs.any (λ gv, ev.evalB m gv.guard)) → gvs.none ev m := begin intro h, simp only [choices.none, choices.true, list.length_eq_zero, list.filter_eq_nil], intros g hg, simp only [list.any_iff_exists, not_exists, exists_prop, eq_ff_eq_not_eq_tt, not_and] at h, simp only [list.mem_map] at hg, rcases hg with ⟨gv, hgv, hgg⟩, specialize h gv hgv, simp only [hgg] at h, simp only [h, not_false_iff, coe_sort_ff], end lemma choices.some_ge_one {α} {gvs : choices SymB α} : (gvs.any (λ gv, ev.evalB m gv.guard)) → (gvs.true ev m).length ≥ 1 := begin simp only [choices.true, ge_iff_le], intro hsm, induction gvs, { simp only [list.any_nil, coe_sort_ff] at hsm, contradiction, }, { simp only [list.map, list.filter], split_ifs, { simp only [list.length, zero_le, le_add_iff_nonneg_left], }, { apply gvs_ih, simp only [h, bor_coe_iff, false_or, list.any_cons] at hsm, exact hsm, } } end lemma choices.lone_some_one {α} {gvs : choices SymB α} : gvs.lone ev m → (gvs.any (λ gv, ev.evalB m gv.guard)) → gvs.one ev m := begin simp only [choices.lone, choices.one], intros hln hsm, rcases (choices.some_ge_one ev hsm) with hge, omega, end lemma choices.one_implies_filter {gvs : choices SymB SymV} : gvs.one ev m → ∃ (i : ℕ) (hi : i < gvs.length), gvs.filter (λ gv, ev.evalB m gv.guard) = [gvs.nth_le i hi] := begin simp only [choices.one, choices.true], intro h, induction gvs, { simp only [list.filter_nil, list.length, nat.zero_ne_one, list.map] at h, contradiction, }, { simp only [list.filter, list.map] at h, split_ifs at h, { apply exists.intro 0, have hi : 0 < (gvs_hd :: gvs_tl).length := by { simp only [nat.succ_pos', list.length], }, apply exists.intro hi, simp only [list.filter, h_1, true_and, if_true, list.nth_le, eq_self_iff_true], simp only [add_left_eq_self, list.length] at h, rewrite ←list.length_eq_zero, rcases (@list.filter_map_length_eq (choice SymB SymV) SymB gvs_tl choice.guard (λ (g : SymB), ↥(ev.evalB m g)) _) with heq, rewrite heq at h, simp only at h, exact h, }, { simp only [h, eq_self_iff_true, forall_true_left] at gvs_ih, rcases gvs_ih with ⟨i, hi, ih⟩, apply exists.intro (i+1), have hi' : i + 1 < (gvs_hd :: gvs_tl).length := by { simp only [hi, list.length, add_lt_add_iff_right], }, apply exists.intro hi', simp only [list.nth_le, list.filter, h_1, if_false], apply ih, } } end lemma choices.filter_implies_one {gvs : choices SymB SymV} : (∃ (i : ℕ) (hi : i < gvs.length), gvs.filter (λ gv, ev.evalB m gv.guard) = [gvs.nth_le i hi]) → gvs.one ev m := begin simp only [choices.one, choices.true], intro h, rcases (@list.map_filter SymB (choice SymB SymV) (λ b, ev.evalB m b) _ choice.guard gvs) with hmf, rcases h with ⟨i, hi, h⟩, apply_fun (list.map choice.guard) at h, simp only [list.map] at h, rewrite hmf, simp only [h, list.length_singleton], end lemma choices.one_iff_filter {gvs : choices SymB SymV} : gvs.one ev m ↔ ∃ (i : ℕ) (hi : i < gvs.length), gvs.filter (λ gv, ev.evalB m gv.guard) = [gvs.nth_le i hi] := begin constructor, apply choices.one_implies_filter, apply choices.filter_implies_one, end lemma choices.eval_filter {gvs : choices SymB SymV} {default : SymR} : choices.eval ev m default gvs = choices.eval ev m default (list.filter (λ (gv : choice SymB SymV), ev.evalB m gv.guard) gvs) := begin induction gvs, { simp only [list.filter_nil], }, { cases gvs_hd, simp only [choices.eval], split_ifs, { simp only [list.filter, h, choices.eval, if_true], }, { simp only [list.filter, h, gvs_ih, if_false], } } end lemma choices.filter_one_guard {gvs : choices SymB SymV} {gv : choice SymB SymV} : gvs.filter (λ gv, ev.evalB m gv.guard) = [gv] → ev.evalB m gv.guard := begin intro h, have hgv : gv ∈ [gv] := by { simp only [list.mem_singleton], }, rewrite ←h at hgv, simp only [list.mem_filter] at hgv, simp only [hgv], end lemma choices.filter_one_eval {gvs : choices SymB SymV} {gv : choice SymB SymV} (default : SymR) : gvs.filter (λ gv, ev.evalB m gv.guard) = [gv] → choices.eval ev m default gvs = ev.evalV m gv.value := begin intro h, rcases (choices.filter_one_guard ev h) with hg, rewrite [choices.eval_filter, h], cases gv, simp only [choices.eval, hg, if_true], end lemma choices.filter_one_rest {gvs : choices SymB SymV} {i j : ℕ} (hi : i < gvs.length) (hj : j < gvs.length) : gvs.filter (λ gv, ev.evalB m gv.guard) = [gvs.nth_le i hi] → i ≠ j → ¬ ev.evalB m (gvs.nth_le j hj).guard := begin intros hu hij, by_contradiction, rcases (choices.filter_one_guard ev hu) with hg, rcases (@list.filter_length_gt_one (choice SymB SymV) gvs i j ((λ (g : choice SymB SymV), ↥(ev.evalB m g.guard))) _ hi hj hij ) with hlen, simp only [h, hu, hg, lt_self_iff_false, gt_iff_lt, list.length_singleton, forall_true_left] at hlen, contradiction, end lemma state.eqv_legal {σ σ' : state SymB} : σ.legal ev m → σ.eqv ev m σ' → σ'.legal ev m := begin simp only [state.legal, state.eqv, and_imp, bor_coe_iff, bool.to_bool_coe, bool.to_bool_or], finish, end lemma state.eqv_normal {σ σ' : state SymB} : σ.normal ev m → state.eqv ev m σ σ' → σ'.normal ev m := begin simp only [state.eqv, state.normal, and_imp, eq_ff_eq_not_eq_tt, not_and, bool.to_bool_and, bool.to_bool_coe, band_coe_iff], intros h1 h2 h3 h4, simp only [h3, h4] at h1 h2, simp only [h1, h2, and_self], end lemma state.eqv_abnormal {σ σ' : state SymB} : ¬ σ.normal ev m → state.eqv ev m σ σ' → ¬ σ'.normal ev m := begin simp only [state.eqv, state.normal, and_imp, eq_ff_eq_not_eq_tt, not_and, bool.to_bool_and, bool.to_bool_coe, band_coe_iff], intros h1 h2 h3 h4, rewrite ←h3, apply h1, rewrite h2, exact h4, end lemma state.eqv_aborted {σ σ' : state SymB} : state.eqv ev m σ σ' → σ.aborted ev m = σ'.aborted ev m := begin simp only [state.eqv, state.aborted], rintro ⟨h1, h2⟩, simp [h1, h2] end lemma state.errored_not_aborted {σ : state SymB} : σ.errored ev m → (σ.aborted ev m) = ff := begin simp only [state.errored, state.aborted, and_imp, eq_ff_eq_not_eq_tt, not_and, bool.of_to_bool_iff, to_bool_ff_iff], intros, assumption, end lemma state.errored_not_normal {σ : state SymB} : σ.errored ev m → (σ.normal ev m) = ff := begin simp only [state.errored, state.normal, and_imp, eq_ff_eq_not_eq_tt, bool.of_to_bool_iff, to_bool_ff_iff, bool.to_bool_and, band_eq_false_eq_eq_ff_or_eq_ff], intros h1 h2, simp only [h2, eq_self_iff_true, or_true], end lemma state.normal_is_legal {σ : state SymB} : σ.normal ev m → σ.legal ev m := begin simp only [state.normal, state.legal, and_imp, bor_coe_iff, bool.to_bool_and, bool.to_bool_coe, band_coe_iff, bool.to_bool_or], cases (ev.evalB m σ.assumes), { simp only [forall_false_left, coe_sort_ff], }, { simp only [implies_true_iff, true_or, coe_sort_tt], } end end eval_generic -- The following lemmas depend on (has_eval Model SymB SymV (lang.val D O))). -- section eval_lang variables {Model SymB SymV D O : Type} (ev : has_eval Model SymB SymV (lang.val D O)) {m : Model} lemma env.eval_length {ε : env SymV} {e : lang.env D O} : env.eval ev m ε = e → ε.length = e.length := begin intro hε, rewrite ←hε, simp only [env.eval, list.length_map], end lemma env.eval_nth_le {ε : env SymV} {e : lang.env D O} {y : ℕ} (hy : y < ε.length) : env.eval ev m ε = e → ∃ (hy' : y < e.length), ev.evalV m (ε.nth_le y hy) = (e.nth_le y hy') := begin intro hε, rcases (env.eval_length ev hε) with hln, have hy' : y < e.length := by { linarith, }, apply exists.intro hy', simp only [env.eval] at hε, rcases (list.nth_le_of_eq (eq.symm hε) hy') with h, simp only [list.nth_le_map'] at h, simp only [h], end lemma env.eval_update {ε : env SymV} {e : lang.env D O} (y : ℕ) {v : SymV} {w : lang.val D O} : env.eval ev m ε = e → ev.evalV m v = w → env.eval ev m (ε.update_nth y v) = (e.update_nth y w) := begin intros hε hv, simp only [env.eval], simp only [env.eval] at hε, rewrite ←hε, apply list.ext_le, { simp only [list.length_map, list.update_nth_length], }, { intros i h1 h2, simp only [list.nth_le_map'], cases classical.em (y = i), { have hy : y < list.length ε := by { simp only [list.length_map, list.update_nth_length] at h1, simp only [h1, h], }, have h3 : y < (list.update_nth ε y v).length := by { simp only [hy, list.update_nth_length], }, have h4 : y < ((list.map (ev.evalV m) ε).update_nth y w).length := by { simp only [hy, list.length_map, list.update_nth_length], }, rewrite ←list.nth_le_eq_idx (list.update_nth ε y v) h h3, rewrite ←list.nth_le_eq_idx ((list.map (ev.evalV m) ε).update_nth y w) h h4, simp only [hv, list.nth_le_update_nth_eq], }, { rewrite [←ne.def] at h, rewrite list.nth_le_update_nth_of_ne h v, rewrite list.nth_le_update_nth_of_ne h w, simp only [list.nth_le_map'], } } end lemma clos.evals_to_clos {c : clos D O SymV} {w : lang.val D O} : clos.eval ev m c = w → w.is_clos := begin intro h, simp only [clos.eval] at h, rewrite ←h, simp only [lang.val.is_clos, coe_sort_tt], end lemma result.eval_ans {σ : state SymB} {v : SymV} {w : lang.val D O} : σ.normal ev m → ev.evalV m v = w → (result.ans σ v).eval ev m = lang.result.ans w := begin intros hn hw, simp only [result.eval, hw, hn, if_true], end lemma result.eval_halt {ρ : result SymB SymV} : ¬ ρ.state.normal ev m → ρ.eval ev m = lang.result.halt (ρ.state.aborted ev m) := begin intro hn, cases ρ with σ v σ, { simp only [result.state, eq_ff_eq_not_eq_tt] at hn, simp only [result.eval, result.state, hn, if_false, coe_sort_ff], }, { simp only [result.eval, result.state, state.aborted, eq_ff_eq_not_eq_tt, bool.to_bool_eq], } end end eval_lang -- The following lemmas depend on the factory interface. -- section factory variables {Model SymB SymV D O : Type} [inhabited Model] [inhabited SymV] (f : factory Model SymB SymV D O) {m : Model} lemma factory.tt_neq_ff : f.mk_tt ≠ f.mk_ff := begin have h : ∀ m, f.evalB m f.mk_ff ≠ f.evalB m f.mk_tt, { simp [f.mk_ff_sound, f.mk_tt_sound] }, specialize h (default Model), tauto, end lemma factory.all_eq_all {α} {fn : α → SymB} {xs : list α} : f.evalB m (f.all fn xs) = xs.all (λ x, f.evalB m (fn x)) := begin simp only [factory.all, list.all], induction xs, { simp only [list.foldr_nil], apply f.mk_tt_sound, }, { simp only [list.foldr], rewrite [←xs_ih, f.and_sound], simp only [bool.to_bool_and, bool.to_bool_coe], } end lemma factory.all_iff_forall {α} {fn : α → SymB} {xs : list α} : f.evalB m (f.all fn xs) ↔ ∀ (i : ℕ) (hi : i < xs.length), f.evalB m (fn (xs.nth_le i hi)) := begin rewrite [f.all_eq_all, list.all_iff_forall], apply list.forall_mem_iff_forall_nth_le, end lemma factory.some_eq_any {α} {fn : α → SymB} {xs : list α} : f.evalB m (f.some fn xs) = xs.any (λ x, f.evalB m (fn x)) := begin simp only [factory.some, list.any], induction xs, { simp only [f.mk_ff_sound, list.foldr_nil], }, { simp only [list.foldr], rewrite [←xs_ih, f.or_sound], simp only [bool.to_bool_coe, bool.to_bool_or], } end lemma factory.assume_normal_true {σ : state SymB} {b : SymB} : σ.normal f.to_has_eval m → f.evalB m b → (f.assume σ b).normal f.to_has_eval m := begin simp only [state.normal, factory.assume, f.and_sound, f.imp_sound, and_imp, eq_ff_eq_not_eq_tt, not_and, bool.of_to_bool_iff], intros h1 h2 h3, cases f.to_has_eval.evalB m σ.asserts, { simp only [coe_sort_ff] at h2, contradiction, }, { simp only [h1, h3, coe_sort_tt, forall_true_left, and_self], } end lemma factory.assume_normal_false {σ : state SymB} {b : SymB} : σ.normal f.to_has_eval m → ¬ f.evalB m b → ¬ (f.assume σ b).normal f.to_has_eval m := begin simp only [state.normal, factory.assume, f.and_sound, f.imp_sound, and_imp, eq_ff_eq_not_eq_tt, not_and, bool.of_to_bool_iff], intros h1 h2 h3 h4, cases f.to_has_eval.evalB m σ.asserts, { simp only [forall_false_left, forall_true_left, coe_sort_ff], }, { simp only [h3, forall_false_left, coe_sort_tt, forall_true_left, coe_sort_ff], } end lemma factory.assert_normal_true {σ : state SymB} {b : SymB} : σ.normal f.to_has_eval m → f.evalB m b → (f.assert σ b).normal f.to_has_eval m := begin simp only [state.normal, factory.assert, f.and_sound, f.imp_sound, and_imp, eq_ff_eq_not_eq_tt, not_and, bool.of_to_bool_iff], intros h1 h2 h3, cases f.to_has_eval.evalB m σ.asserts, { simp only [coe_sort_ff] at h2, contradiction, }, { simp only [h1, h3, coe_sort_tt, forall_true_left, and_self], } end lemma factory.assert_normal_guard {σ : state SymB} {b : SymB} : (f.assert σ b).normal f.to_has_eval m → f.evalB m b := begin simp only [factory.assert, state.normal, f.and_sound, f.imp_sound, and_imp, bool.of_to_bool_iff], intros h1, simp only [h1, imp_self, implies_true_iff, forall_true_left], end lemma factory.cast_lone {c : SymV} : (f.cast c).lone f.to_has_eval m := begin simp only [choices.lone, choices.true], rcases (f.cast_sound m c) with h, cases (f.to_has_eval.evalV m c).is_clos, { simp only [true_and, forall_false_left, forall_true_left, not_false_iff, coe_sort_ff] at h, simp only [choices.none, choices.true, has_eval_clos] at h, linarith, }, { simp only [forall_false_left, and_true, not_true, coe_sort_tt, forall_true_left] at h, cases h with h _, simp only [choices.one, choices.true, has_eval_clos] at h, linarith, } end lemma factory.assert_some_cast_one {c : SymV} {σ σ' : state SymB} : σ.normal f.to_has_eval m → σ'.normal f.to_has_eval m → σ' = f.assert σ (f.some choice.guard (f.cast c)) → (f.cast c).one f.to_has_eval m := begin intros hn hn' heq, apply choices.lone_some_one f.to_has_eval, { apply f.cast_lone, }, { rewrite ←f.some_eq_any, rewrite heq at hn', apply f.assert_normal_guard hn', } end lemma top_normal : state.normal f.to_has_eval m ⟨f.mk_tt, f.mk_tt⟩ := by { simp only [state.normal, f.mk_tt_sound, to_bool_true_eq_tt, coe_sort_tt, and_self], } lemma top_legal : state.legal f.to_has_eval m ⟨f.mk_tt, f.mk_tt⟩ := by { apply state.normal_is_legal f.to_has_eval, apply top_normal, } end factory end sym
15dad7c3a850143eaf924ba1d144de2b6c88bd11
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/order/conditionally_complete_lattice.lean
f0385e4d019d5369a8cc0c41ec8e2cb2096e351c
[ "Apache-2.0" ]
permissive
vaibhavkarve/mathlib
a574aaf68c0a431a47fa82ce0637f0f769826bfe
17f8340912468f49bdc30acdb9a9fa02eeb0473a
refs/heads/master
1,621,263,802,637
1,585,399,588,000
1,585,399,588,000
250,833,447
0
0
Apache-2.0
1,585,410,341,000
1,585,410,341,000
null
UTF-8
Lean
false
false
28,028
lean
/- Copyright (c) 2018 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel Adapted from the corresponding theory for complete lattices. -/ import order.lattice order.complete_lattice order.bounds tactic.finish data.set.finite /-! # Theory of conditionally complete lattices. A conditionally complete lattice is a lattice in which every non-empty bounded subset s has a least upper bound and a greatest lower bound, denoted below by Sup s and Inf s. Typical examples are real, nat, int with their usual orders. The theory is very comparable to the theory of complete lattices, except that suitable boundedness and nonemptiness assumptions have to be added to most statements. We introduce two predicates bdd_above and bdd_below to express this boundedness, prove their basic properties, and then go on to prove most useful properties of Sup and Inf in conditionally complete lattices. To differentiate the statements between complete lattices and conditionally complete lattices, we prefix Inf and Sup in the statements by c, giving cInf and cSup. For instance, Inf_le is a statement in complete lattices ensuring Inf s ≤ x, while cInf_le is the same statement in conditionally complete lattices with an additional assumption that s is bounded below. -/ set_option old_structure_cmd true open set universes u v w variables {α : Type u} {β : Type v} {ι : Sort w} section /-! Extension of Sup and Inf from a preorder `α` to `with_top α` and `with_bot α` -/ open_locale classical noncomputable instance {α : Type*} [preorder α] [has_Sup α] : has_Sup (with_top α) := ⟨λ S, if ⊤ ∈ S then ⊤ else if bdd_above (coe ⁻¹' S : set α) then ↑(Sup (coe ⁻¹' S : set α)) else ⊤⟩ noncomputable instance {α : Type*} [has_Inf α] : has_Inf (with_top α) := ⟨λ S, if S ⊆ {⊤} then ⊤ else ↑(Inf (coe ⁻¹' S : set α))⟩ noncomputable instance {α : Type*} [has_Sup α] : has_Sup (with_bot α) := ⟨(@with_top.has_Inf (order_dual α) _).Inf⟩ noncomputable instance {α : Type*} [preorder α] [has_Inf α] : has_Inf (with_bot α) := ⟨(@with_top.has_Sup (order_dual α) _ _).Sup⟩ end -- section section prio set_option default_priority 100 -- see Note [default priority] /-- A conditionally complete lattice is a lattice in which every nonempty subset which is bounded above has a supremum, and every nonempty subset which is bounded below has an infimum. Typical examples are real numbers or natural numbers. To differentiate the statements from the corresponding statements in (unconditional) complete lattices, we prefix Inf and Sup by a c everywhere. The same statements should hold in both worlds, sometimes with additional assumptions of nonemptiness or boundedness.-/ class conditionally_complete_lattice (α : Type u) extends lattice α, has_Sup α, has_Inf α := (le_cSup : ∀s a, bdd_above s → a ∈ s → a ≤ Sup s) (cSup_le : ∀ s a, set.nonempty s → a ∈ upper_bounds s → Sup s ≤ a) (cInf_le : ∀s a, bdd_below s → a ∈ s → Inf s ≤ a) (le_cInf : ∀s a, set.nonempty s → a ∈ lower_bounds s → a ≤ Inf s) class conditionally_complete_linear_order (α : Type u) extends conditionally_complete_lattice α, decidable_linear_order α class conditionally_complete_linear_order_bot (α : Type u) extends conditionally_complete_lattice α, decidable_linear_order α, order_bot α := (cSup_empty : Sup ∅ = ⊥) end prio /- A complete lattice is a conditionally complete lattice, as there are no restrictions on the properties of Inf and Sup in a complete lattice.-/ @[priority 100] -- see Note [lower instance priority] instance conditionally_complete_lattice_of_complete_lattice [complete_lattice α]: conditionally_complete_lattice α := { le_cSup := by intros; apply le_Sup; assumption, cSup_le := by intros; apply Sup_le; assumption, cInf_le := by intros; apply Inf_le; assumption, le_cInf := by intros; apply le_Inf; assumption, ..‹complete_lattice α› } @[priority 100] -- see Note [lower instance priority] instance conditionally_complete_linear_order_of_complete_linear_order [complete_linear_order α]: conditionally_complete_linear_order α := { ..conditionally_complete_lattice_of_complete_lattice, .. ‹complete_linear_order α› } section conditionally_complete_lattice variables [conditionally_complete_lattice α] {s t : set α} {a b : α} theorem le_cSup (h₁ : bdd_above s) (h₂ : a ∈ s) : a ≤ Sup s := conditionally_complete_lattice.le_cSup s a h₁ h₂ theorem cSup_le (h₁ : s.nonempty) (h₂ : ∀b∈s, b ≤ a) : Sup s ≤ a := conditionally_complete_lattice.cSup_le s a h₁ h₂ theorem cInf_le (h₁ : bdd_below s) (h₂ : a ∈ s) : Inf s ≤ a := conditionally_complete_lattice.cInf_le s a h₁ h₂ theorem le_cInf (h₁ : s.nonempty) (h₂ : ∀b∈s, a ≤ b) : a ≤ Inf s := conditionally_complete_lattice.le_cInf s a h₁ h₂ theorem le_cSup_of_le (_ : bdd_above s) (hb : b ∈ s) (h : a ≤ b) : a ≤ Sup s := le_trans h (le_cSup ‹bdd_above s› hb) theorem cInf_le_of_le (_ : bdd_below s) (hb : b ∈ s) (h : b ≤ a) : Inf s ≤ a := le_trans (cInf_le ‹bdd_below s› hb) h theorem cSup_le_cSup (_ : bdd_above t) (_ : s.nonempty) (h : s ⊆ t) : Sup s ≤ Sup t := cSup_le ‹_› (assume (a) (ha : a ∈ s), le_cSup ‹bdd_above t› (h ha)) theorem cInf_le_cInf (_ : bdd_below t) (_ : s.nonempty) (h : s ⊆ t) : Inf t ≤ Inf s := le_cInf ‹_› (assume (a) (ha : a ∈ s), cInf_le ‹bdd_below t› (h ha)) lemma is_lub_cSup (ne : s.nonempty) (H : bdd_above s) : is_lub s (Sup s) := ⟨assume x, le_cSup H, assume x, cSup_le ne⟩ lemma is_glb_cInf (ne : s.nonempty) (H : bdd_below s) : is_glb s (Inf s) := ⟨assume x, cInf_le H, assume x, le_cInf ne⟩ lemma is_lub.cSup_eq (H : is_lub s a) (ne : s.nonempty) : Sup s = a := (is_lub_cSup ne ⟨a, H.1⟩).unique H /-- A greatest element of a set is the supremum of this set. -/ lemma is_greatest.cSup_eq (H : is_greatest s a) : Sup s = a := H.is_lub.cSup_eq H.nonempty lemma is_glb.cInf_eq (H : is_glb s a) (ne : s.nonempty) : Inf s = a := (is_glb_cInf ne ⟨a, H.1⟩).unique H /-- A least element of a set is the infimum of this set. -/ lemma is_least.cInf_eq (H : is_least s a) : Inf s = a := H.is_glb.cInf_eq H.nonempty theorem cSup_le_iff (hb : bdd_above s) (ne : s.nonempty) : Sup s ≤ a ↔ (∀b ∈ s, b ≤ a) := is_lub_le_iff (is_lub_cSup ne hb) theorem le_cInf_iff (hb : bdd_below s) (ne : s.nonempty) : a ≤ Inf s ↔ (∀b ∈ s, a ≤ b) := le_is_glb_iff (is_glb_cInf ne hb) lemma cSup_lower_bounds_eq_cInf {s : set α} (h : bdd_below s) (hs : s.nonempty) : Sup (lower_bounds s) = Inf s := (is_lub_cSup h $ hs.mono $ λ x hx y hy, hy hx).unique (is_glb_cInf hs h).is_lub lemma cInf_upper_bounds_eq_cSup {s : set α} (h : bdd_above s) (hs : s.nonempty) : Inf (upper_bounds s) = Sup s := (is_glb_cInf h $ hs.mono $ λ x hx y hy, hy hx).unique (is_lub_cSup hs h).is_glb /--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`.-/ theorem cSup_intro (_ : s.nonempty) (_ : ∀a∈s, a ≤ b) (H : ∀w, w < b → (∃a∈s, w < a)) : Sup s = b := have bdd_above s := ⟨b, by assumption⟩, have (Sup s < b) ∨ (Sup s = b) := lt_or_eq_of_le (cSup_le ‹_› ‹∀a∈s, a ≤ b›), have ¬(Sup s < b) := assume: Sup s < b, let ⟨a, _, _⟩ := (H (Sup s) ‹Sup s < b›) in /- a ∈ s, Sup s < a-/ have Sup s < Sup s := lt_of_lt_of_le ‹Sup s < a› (le_cSup ‹bdd_above s› ‹a ∈ s›), show false, by finish [lt_irrefl (Sup s)], show Sup s = b, by finish /--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`.-/ theorem cInf_intro (_ : s.nonempty) (_ : ∀a∈s, b ≤ a) (H : ∀w, b < w → (∃a∈s, a < w)) : Inf s = b := have bdd_below s := ⟨b, by assumption⟩, have (b < Inf s) ∨ (b = Inf s) := lt_or_eq_of_le (le_cInf ‹_› ‹∀a∈s, b ≤ a›), have ¬(b < Inf s) := assume: b < Inf s, let ⟨a, _, _⟩ := (H (Inf s) ‹b < Inf s›) in /- a ∈ s, a < Inf s-/ have Inf s < Inf s := lt_of_le_of_lt (cInf_le ‹bdd_below s› ‹a ∈ s›) ‹a < Inf s› , show false, by finish [lt_irrefl (Inf s)], show Inf s = b, by finish /--b < Sup s when there is an element a in s with b < a, when s is bounded above. This is essentially an iff, except that the assumptions for the two implications are slightly different (one needs boundedness above for one direction, nonemptiness and linear order for the other one), so we formulate separately the two implications, contrary to the complete_lattice case.-/ lemma lt_cSup_of_lt (_ : bdd_above s) (_ : a ∈ s) (_ : b < a) : b < Sup s := lt_of_lt_of_le ‹b < a› (le_cSup ‹bdd_above s› ‹a ∈ s›) /--Inf s < b when there is an element a in s with a < b, when s is bounded below. This is essentially an iff, except that the assumptions for the two implications are slightly different (one needs boundedness below for one direction, nonemptiness and linear order for the other one), so we formulate separately the two implications, contrary to the complete_lattice case.-/ lemma cInf_lt_of_lt (_ : bdd_below s) (_ : a ∈ s) (_ : a < b) : Inf s < b := lt_of_le_of_lt (cInf_le ‹bdd_below s› ‹a ∈ s›) ‹a < b› /-- If all elements of a nonempty set `s` are less than or equal to all elements of a nonempty set `t`, then there exists an element between these sets. -/ lemma exists_between_of_forall_le (sne : s.nonempty) (tne : t.nonempty) (hst : ∀ (x ∈ s) (y ∈ t), x ≤ y) : (upper_bounds s ∩ lower_bounds t).nonempty := ⟨Inf t, λ x hx, le_cInf tne $ hst x hx, λ y hy, cInf_le (sne.mono hst) hy⟩ /--The supremum of a singleton is the element of the singleton-/ @[simp] theorem cSup_singleton (a : α) : Sup {a} = a := is_greatest_singleton.cSup_eq /--The infimum of a singleton is the element of the singleton-/ @[simp] theorem cInf_singleton (a : α) : Inf {a} = a := is_least_singleton.cInf_eq /--If a set is bounded below and above, and nonempty, its infimum is less than or equal to its supremum.-/ theorem cInf_le_cSup (hb : bdd_below s) (ha : bdd_above s) (ne : s.nonempty) : Inf s ≤ Sup s := is_glb_le_is_lub (is_glb_cInf ne hb) (is_lub_cSup ne ha) ne /--The sup of a union of two sets is the max of the suprema of each subset, under the assumptions that all sets are bounded above and nonempty.-/ theorem cSup_union (hs : bdd_above s) (sne : s.nonempty) (ht : bdd_above t) (tne : t.nonempty) : Sup (s ∪ t) = Sup s ⊔ Sup t := ((is_lub_cSup sne hs).union (is_lub_cSup tne ht)).cSup_eq sne.inl /--The inf of a union of two sets is the min of the infima of each subset, under the assumptions that all sets are bounded below and nonempty.-/ theorem cInf_union (hs : bdd_below s) (sne : s.nonempty) (ht : bdd_below t) (tne : t.nonempty) : Inf (s ∪ t) = Inf s ⊓ Inf t := ((is_glb_cInf sne hs).union (is_glb_cInf tne ht)).cInf_eq sne.inl /--The supremum of an intersection of two sets is bounded by the minimum of the suprema of each set, if all sets are bounded above and nonempty.-/ theorem cSup_inter_le (_ : bdd_above s) (_ : bdd_above t) (hst : (s ∩ t).nonempty) : Sup (s ∩ t) ≤ Sup s ⊓ Sup t := begin apply cSup_le hst, simp only [le_inf_iff, and_imp, set.mem_inter_eq], intros b _ _, split, apply le_cSup ‹bdd_above s› ‹b ∈ s›, apply le_cSup ‹bdd_above t› ‹b ∈ t› end /--The infimum of an intersection of two sets is bounded below by the maximum of the infima of each set, if all sets are bounded below and nonempty.-/ theorem le_cInf_inter (_ : bdd_below s) (_ : bdd_below t) (hst : (s ∩ t).nonempty) : Inf s ⊔ Inf t ≤ Inf (s ∩ t) := begin apply le_cInf hst, simp only [and_imp, set.mem_inter_eq, sup_le_iff], intros b _ _, split, apply cInf_le ‹bdd_below s› ‹b ∈ s›, apply cInf_le ‹bdd_below t› ‹b ∈ t› end /-- The supremum of insert a s is the maximum of a and the supremum of s, if s is nonempty and bounded above.-/ theorem cSup_insert (hs : bdd_above s) (sne : s.nonempty) : Sup (insert a s) = a ⊔ Sup s := ((is_lub_cSup sne hs).insert a).cSup_eq (insert_nonempty a s) /-- The infimum of insert a s is the minimum of a and the infimum of s, if s is nonempty and bounded below.-/ theorem cInf_insert (hs : bdd_below s) (sne : s.nonempty) : Inf (insert a s) = a ⊓ Inf s := ((is_glb_cInf sne hs).insert a).cInf_eq (insert_nonempty a s) @[simp] lemma cInf_Ici : Inf (Ici a) = a := is_least_Ici.cInf_eq @[simp] lemma cSup_Iic : Sup (Iic a) = a := is_greatest_Iic.cSup_eq /--The indexed supremum of two functions are comparable if the functions are pointwise comparable-/ lemma csupr_le_csupr {f g : ι → α} (B : bdd_above (range g)) (H : ∀x, f x ≤ g x) : supr f ≤ supr g := begin classical, by_cases hι : nonempty ι, { have Rf : (range f).nonempty := range_nonempty _, apply cSup_le Rf, rintros y ⟨x, rfl⟩, have : g x ∈ range g := ⟨x, rfl⟩, exact le_cSup_of_le B this (H x) }, { have Rf : range f = ∅, from range_eq_empty.2 hι, have Rg : range g = ∅, from range_eq_empty.2 hι, unfold supr, rw [Rf, Rg] } end /--The indexed supremum of a function is bounded above by a uniform bound-/ lemma csupr_le [nonempty ι] {f : ι → α} {c : α} (H : ∀x, f x ≤ c) : supr f ≤ c := cSup_le (range_nonempty f) (by rwa forall_range_iff) /--The indexed supremum of a function is bounded below by the value taken at one point-/ lemma le_csupr {f : ι → α} (H : bdd_above (range f)) {c : ι} : f c ≤ supr f := le_cSup H (mem_range_self _) /--The indexed infimum of two functions are comparable if the functions are pointwise comparable-/ lemma cinfi_le_cinfi {f g : ι → α} (B : bdd_below (range f)) (H : ∀x, f x ≤ g x) : infi f ≤ infi g := begin classical, by_cases hι : nonempty ι, { have Rg : (range g).nonempty, from range_nonempty _, apply le_cInf Rg, rintros y ⟨x, rfl⟩, have : f x ∈ range f := ⟨x, rfl⟩, exact cInf_le_of_le B this (H x) }, { have Rf : range f = ∅, from range_eq_empty.2 hι, have Rg : range g = ∅, from range_eq_empty.2 hι, unfold infi, rw [Rf, Rg] } end /--The indexed minimum of a function is bounded below by a uniform lower bound-/ lemma le_cinfi [nonempty ι] {f : ι → α} {c : α} (H : ∀x, c ≤ f x) : c ≤ infi f := le_cInf (range_nonempty f) (by rwa forall_range_iff) /--The indexed infimum of a function is bounded above by the value taken at one point-/ lemma cinfi_le {f : ι → α} (H : bdd_below (range f)) {c : ι} : infi f ≤ f c := cInf_le H (mem_range_self _) @[simp] theorem cinfi_const [hι : nonempty ι] {a : α} : (⨅ b:ι, a) = a := by rw [infi, range_const, cInf_singleton] @[simp] theorem csupr_const [hι : nonempty ι] {a : α} : (⨆ b:ι, a) = a := by rw [supr, range_const, cSup_singleton] end conditionally_complete_lattice section conditionally_complete_linear_order variables [conditionally_complete_linear_order α] {s t : set α} {a b : α} /-- When b < Sup s, there is an element a in s with b < a, if s is nonempty and the order is a linear order. -/ lemma exists_lt_of_lt_cSup (hs : s.nonempty) (hb : b < Sup s) : ∃a∈s, b < a := begin classical, contrapose! hb, exact cSup_le hs hb end /-- Indexed version of the above lemma `exists_lt_of_lt_cSup`. When `b < supr f`, there is an element `i` such that `b < f i`. -/ lemma exists_lt_of_lt_csupr [nonempty ι] {f : ι → α} (h : b < supr f) : ∃i, b < f i := let ⟨_, ⟨i, rfl⟩, h⟩ := exists_lt_of_lt_cSup (range_nonempty f) h in ⟨i, h⟩ /--When Inf s < b, there is an element a in s with a < b, if s is nonempty and the order is a linear order.-/ lemma exists_lt_of_cInf_lt (hs : s.nonempty) (hb : Inf s < b) : ∃a∈s, a < b := begin classical, contrapose! hb, exact le_cInf hs hb end /-- Indexed version of the above lemma `exists_lt_of_cInf_lt` When `infi f < a`, there is an element `i` such that `f i < a`. -/ lemma exists_lt_of_cinfi_lt [nonempty ι] {f : ι → α} (h : infi f < a) : (∃i, f i < a) := let ⟨_, ⟨i, rfl⟩, h⟩ := exists_lt_of_cInf_lt (range_nonempty f) h in ⟨i, h⟩ /--Introduction rule to prove that b is the supremum of s: it suffices to check that 1) b is an upper bound 2) every other upper bound b' satisfies b ≤ b'.-/ theorem cSup_intro' (_ : s.nonempty) (h_is_ub : ∀ a ∈ s, a ≤ b) (h_b_le_ub : ∀ub, (∀ a ∈ s, a ≤ ub) → (b ≤ ub)) : Sup s = b := le_antisymm (show Sup s ≤ b, from cSup_le ‹s.nonempty› h_is_ub) (show b ≤ Sup s, from h_b_le_ub _ $ assume a, le_cSup ⟨b, h_is_ub⟩) end conditionally_complete_linear_order section conditionally_complete_linear_order_bot lemma cSup_empty [conditionally_complete_linear_order_bot α] : (Sup ∅ : α) = ⊥ := conditionally_complete_linear_order_bot.cSup_empty α end conditionally_complete_linear_order_bot section open_locale classical noncomputable instance : has_Inf ℕ := ⟨λs, if h : ∃n, n ∈ s then @nat.find (λn, n ∈ s) _ h else 0⟩ noncomputable instance : has_Sup ℕ := ⟨λs, if h : ∃n, ∀a∈s, a ≤ n then @nat.find (λn, ∀a∈s, a ≤ n) _ h else 0⟩ lemma Inf_nat_def {s : set ℕ} (h : ∃n, n ∈ s) : Inf s = @nat.find (λn, n ∈ s) _ h := dif_pos _ lemma Sup_nat_def {s : set ℕ} (h : ∃n, ∀a∈s, a ≤ n) : Sup s = @nat.find (λn, ∀a∈s, a ≤ n) _ h := dif_pos _ /-- This instance is necessary, otherwise the lattice operations would be derived via conditionally_complete_linear_order_bot and marked as noncomputable. -/ instance : lattice ℕ := infer_instance noncomputable instance : conditionally_complete_linear_order_bot ℕ := { Sup := Sup, Inf := Inf, le_cSup := assume s a hb ha, by rw [Sup_nat_def hb]; revert a ha; exact @nat.find_spec _ _ hb, cSup_le := assume s a hs ha, by rw [Sup_nat_def ⟨a, ha⟩]; exact nat.find_min' _ ha, le_cInf := assume s a hs hb, by rw [Inf_nat_def hs]; exact hb (@nat.find_spec (λn, n ∈ s) _ _), cInf_le := assume s a hb ha, by rw [Inf_nat_def ⟨a, ha⟩]; exact nat.find_min' _ ha, cSup_empty := begin simp only [Sup_nat_def, set.mem_empty_eq, forall_const, forall_prop_of_false, not_false_iff, exists_const], apply bot_unique (nat.find_min' _ _), trivial end, .. (infer_instance : order_bot ℕ), .. (infer_instance : lattice ℕ), .. (infer_instance : decidable_linear_order ℕ) } end namespace with_top open_locale classical variables [conditionally_complete_linear_order_bot α] /-- The Sup of a non-empty set is its least upper bound for a conditionally complete lattice with a top. -/ lemma is_lub_Sup' {β : Type*} [conditionally_complete_lattice β] {s : set (with_top β)} (hs : s.nonempty) : is_lub s (Sup s) := begin split, { show ite _ _ _ ∈ _, split_ifs, { intros _ _, exact le_top }, { rintro (⟨⟩|a) ha, { contradiction }, apply some_le_some.2, exact le_cSup h_1 ha }, { intros _ _, exact le_top } }, { show ite _ _ _ ∈ _, split_ifs, { rintro (⟨⟩|a) ha, { exact _root_.le_refl _ }, { exact false.elim (not_top_le_coe a (ha h)) } }, { rintro (⟨⟩|b) hb, { exact le_top }, refine some_le_some.2 (cSup_le _ _), { rcases hs with ⟨⟨⟩|b, hb⟩, { exact absurd hb h }, { exact ⟨b, hb⟩ } }, { intros a ha, exact some_le_some.1 (hb ha) } }, { rintro (⟨⟩|b) hb, { exact _root_.le_refl _ }, { exfalso, apply h_1, use b, intros a ha, exact some_le_some.1 (hb ha) } } } end lemma is_lub_Sup (s : set (with_top α)) : is_lub s (Sup s) := begin cases s.eq_empty_or_nonempty with hs hs, { rw hs, show is_lub ∅ (ite _ _ _), split_ifs, { cases h }, { rw [preimage_empty, cSup_empty], exact is_lub_empty }, { exfalso, apply h_1, use ⊥, rintro a ⟨⟩ } }, exact is_lub_Sup' hs, end /-- The Inf of a bounded-below set is its greatest lower bound for a conditionally complete lattice with a top. -/ lemma is_glb_Inf' {β : Type*} [conditionally_complete_lattice β] {s : set (with_top β)} (hs : bdd_below s) : is_glb s (Inf s) := begin split, { show ite _ _ _ ∈ _, split_ifs, { intros a ha, exact top_le_iff.2 (set.mem_singleton_iff.1 (h ha)) }, { rintro (⟨⟩|a) ha, { exact le_top }, refine some_le_some.2 (cInf_le _ ha), rcases hs with ⟨⟨⟩|b, hb⟩, { exfalso, apply h, intros c hc, rw [mem_singleton_iff, ←top_le_iff], exact hb hc }, use b, intros c hc, exact some_le_some.1 (hb hc) } }, { show ite _ _ _ ∈ _, split_ifs, { intros _ _, exact le_top }, { rintro (⟨⟩|a) ha, { exfalso, apply h, intros b hb, exact set.mem_singleton_iff.2 (top_le_iff.1 (ha hb)) }, { refine some_le_some.2 (le_cInf _ _), { classical, contrapose! h, rintros (⟨⟩|a) ha, { exact mem_singleton ⊤ }, { exact (h ⟨a, ha⟩).elim }}, { intros b hb, rw ←some_le_some, exact ha hb } } } } end lemma is_glb_Inf (s : set (with_top α)) : is_glb s (Inf s) := begin by_cases hs : bdd_below s, { exact is_glb_Inf' hs }, { exfalso, apply hs, use ⊥, intros _ _, exact bot_le }, end noncomputable instance : complete_linear_order (with_top α) := { Sup := Sup, le_Sup := assume s, (is_lub_Sup s).1, Sup_le := assume s, (is_lub_Sup s).2, Inf := Inf, le_Inf := assume s, (is_glb_Inf s).2, Inf_le := assume s, (is_glb_Inf s).1, decidable_le := classical.dec_rel _, .. with_top.linear_order, ..with_top.lattice, ..with_top.order_top, ..with_top.order_bot } lemma coe_Sup {s : set α} (hb : bdd_above s) : (↑(Sup s) : with_top α) = (⨆a∈s, ↑a) := begin cases s.eq_empty_or_nonempty with hs hs, { rw [hs, cSup_empty], simp only [set.mem_empty_eq, supr_bot, supr_false], refl }, apply le_antisymm, { refine ((coe_le_iff _ _).2 $ assume b hb, cSup_le hs $ assume a has, coe_le_coe.1 $ hb ▸ _), exact (le_supr_of_le a $ le_supr_of_le has $ _root_.le_refl _) }, { exact (supr_le $ assume a, supr_le $ assume ha, coe_le_coe.2 $ le_cSup hb ha) } end lemma coe_Inf {s : set α} (hs : s.nonempty) : (↑(Inf s) : with_top α) = (⨅a∈s, ↑a) := let ⟨x, hx⟩ := hs in have (⨅a∈s, ↑a : with_top α) ≤ x, from infi_le_of_le x $ infi_le_of_le hx $ _root_.le_refl _, let ⟨r, r_eq, hr⟩ := (le_coe_iff _ _).1 this in le_antisymm (le_infi $ assume a, le_infi $ assume ha, coe_le_coe.2 $ cInf_le (order_bot.bdd_below s) ha) begin refine (r_eq.symm ▸ coe_le_coe.2 $ le_cInf hs $ assume a has, coe_le_coe.1 $ _), refine (r_eq ▸ infi_le_of_le a _), exact (infi_le_of_le has $ _root_.le_refl _), end end with_top namespace enat open_locale classical noncomputable instance : complete_linear_order enat := { Sup := λ s, with_top_equiv.symm $ Sup (with_top_equiv '' s), Inf := λ s, with_top_equiv.symm $ Inf (with_top_equiv '' s), le_Sup := by intros; rw ← with_top_equiv_le; simp; apply le_Sup _; simpa, Inf_le := by intros; rw ← with_top_equiv_le; simp; apply Inf_le _; simpa, Sup_le := begin intros s a h1, rw [← with_top_equiv_le, with_top_equiv.right_inverse_symm], apply Sup_le _, rintros b ⟨x, h2, rfl⟩, rw with_top_equiv_le, apply h1, assumption end, le_Inf := begin intros s a h1, rw [← with_top_equiv_le, with_top_equiv.right_inverse_symm], apply le_Inf _, rintros b ⟨x, h2, rfl⟩, rw with_top_equiv_le, apply h1, assumption end, ..enat.decidable_linear_order, ..enat.bounded_lattice } end enat section order_dual instance (α : Type*) [conditionally_complete_lattice α] : conditionally_complete_lattice (order_dual α) := { le_cSup := @cInf_le α _, cSup_le := @le_cInf α _, le_cInf := @cSup_le α _, cInf_le := @le_cSup α _, ..order_dual.has_Inf α, ..order_dual.has_Sup α, ..order_dual.lattice α } instance (α : Type*) [conditionally_complete_linear_order α] : conditionally_complete_linear_order (order_dual α) := { ..order_dual.conditionally_complete_lattice α, ..order_dual.decidable_linear_order α } end order_dual section with_top_bot /-! ### Complete lattice structure on `with_top (with_bot α)` If `α` is a `conditionally_complete_lattice`, then we show that `with_top α` and `with_bot α` also inherit the structure of conditionally complete lattices. Furthermore, we show that `with_top (with_bot α)` naturally inherits the structure of a complete lattice. Note that for α a conditionally complete lattice, `Sup` and `Inf` both return junk values for sets which are empty or unbounded. The extension of `Sup` to `with_top α` fixes the unboundedness problem and the extension to `with_bot α` fixes the problem with the empty set. This result can be used to show that the extended reals [-∞, ∞] are a complete lattice. -/ open_locale classical /-- Adding a top element to a conditionally complete lattice gives a conditionally complete lattice -/ noncomputable instance with_top.conditionally_complete_lattice {α : Type*} [conditionally_complete_lattice α] : conditionally_complete_lattice (with_top α) := { le_cSup := λ S a hS haS, (with_top.is_lub_Sup' ⟨a, haS⟩).1 haS, cSup_le := λ S a hS haS, (with_top.is_lub_Sup' hS).2 haS, cInf_le := λ S a hS haS, (with_top.is_glb_Inf' hS).1 haS, le_cInf := λ S a hS haS, (with_top.is_glb_Inf' ⟨a, haS⟩).2 haS, ..with_top.lattice, ..with_top.has_Sup, ..with_top.has_Inf } /-- Adding a bottom element to a conditionally complete lattice gives a conditionally complete lattice -/ noncomputable instance with_bot.conditionally_complete_lattice {α : Type*} [conditionally_complete_lattice α] : conditionally_complete_lattice (with_bot α) := { le_cSup := (@with_top.conditionally_complete_lattice (order_dual α) _).cInf_le, cSup_le := (@with_top.conditionally_complete_lattice (order_dual α) _).le_cInf, cInf_le := (@with_top.conditionally_complete_lattice (order_dual α) _).le_cSup, le_cInf := (@with_top.conditionally_complete_lattice (order_dual α) _).cSup_le, ..with_bot.lattice, ..with_bot.has_Sup, ..with_bot.has_Inf } /-- Adding a bottom and a top to a conditionally complete lattice gives a bounded lattice-/ noncomputable instance with_top.with_bot.bounded_lattice {α : Type*} [conditionally_complete_lattice α] : bounded_lattice (with_top (with_bot α)) := { ..with_top.order_bot, ..with_top.order_top, ..conditionally_complete_lattice.to_lattice _ } theorem with_bot.cSup_empty {α : Type*} [conditionally_complete_lattice α] : Sup (∅ : set (with_bot α)) = ⊥ := begin show ite _ _ _ = ⊥, split_ifs; finish, end noncomputable instance with_top.with_bot.complete_lattice {α : Type*} [conditionally_complete_lattice α] : complete_lattice (with_top (with_bot α)) := { le_Sup := λ S a haS, (with_top.is_lub_Sup' ⟨a, haS⟩).1 haS, Sup_le := λ S a ha, begin cases S.eq_empty_or_nonempty with h, { show ite _ _ _ ≤ a, split_ifs, { rw h at h_1, cases h_1 }, { convert bot_le, convert with_bot.cSup_empty, rw h, refl }, { exfalso, apply h_2, use ⊥, rw h, rintro b ⟨⟩ } }, { refine (with_top.is_lub_Sup' h).2 ha } end, Inf_le := λ S a haS, show ite _ _ _ ≤ a, begin split_ifs, { cases a with a, exact _root_.le_refl _, cases (h haS); tauto }, { cases a, { exact le_top }, { apply with_top.some_le_some.2, refine cInf_le _ haS, use ⊥, intros b hb, exact bot_le } } end, le_Inf := λ S a haS, (with_top.is_glb_Inf' ⟨a, haS⟩).2 haS, ..with_top.has_Inf, ..with_top.has_Sup, ..with_top.with_bot.bounded_lattice } end with_top_bot
da42b3f76a972b7ac9d8af57b8e1b13bd36baae5
827a8a5c2041b1d7f55e128581f583dfbd65ecf6
/susp_K.hlean
6c35144b8a5f3f6dc26287a33d7235d0a221f637
[ "Apache-2.0" ]
permissive
fpvandoorn/leansnippets
6af0499f6f3fd2c07e4b580734d77b67574e7c27
601bafbe07e9534af76f60994d6bdf741996ef93
refs/heads/master
1,590,063,910,882
1,545,093,878,000
1,545,093,878,000
36,044,957
2
2
null
1,442,619,708,000
1,432,256,875,000
Lean
UTF-8
Lean
false
false
1,794
hlean
import .susp_set types.unit open susp eq is_trunc definition is_contr_loop [instance] {A : Type} [is_set A] (a : A) : is_contr (a = a) := is_contr_of_inhabited_prop idp namespace susp universe variable u variable {P : Prop.{u}} definition code_K {x y : susp P} (p : x = y) : trunctype.{u} (-2) := begin induction x using susp.rec_prop: induction y using susp.rec_prop, { exact trunctype.mk (p = idp) _ }, { exact trunctype.mk (lift unit) _ }, { exact trunctype.mk (lift unit) _ }, { exact trunctype.mk (p = idp) _ }, end definition axiom_K {x y : susp P} (p : x = y) : code_K p := begin induction p, induction x using susp.rec_prop, exact idp, exact idp end definition easy_code_K {x : susp P} (p : north = x) : trunctype.{u} (-2) := begin induction x using susp.rec_prop, { exact trunctype.mk (p = idp) _ }, { exact trunctype.mk (lift unit) _ } end definition easy_axiom_K' {x : susp P} (p : north = x) : easy_code_K p := begin induction p, exact idp end definition easy_axiom_K (p : north = north :> susp P) : p = idp := easy_axiom_K' p definition easy_axiom_K_idp : easy_axiom_K idp = idpath (idpath (@north P)) := idp end susp namespace interval definition interval.rec_prop [unfold 5] [recursor 5] {P : interval → Type} [Πx, is_prop (P x)] (h₀ : P zero) (h₁ : P one) (x : interval) : P x := interval.rec h₀ h₁ !is_prop.elimo x definition code_K {x y : interval} (p : x = y) : trunctype.{0} (-2) := begin induction x using interval.rec_prop: induction y using interval.rec_prop, { exact trunctype.mk (p = idp) _ }, { exact trunctype.mk (lift unit) _ }, { exact trunctype.mk (lift unit) _ }, { exact trunctype.mk (p = idp) _ }, end end interval
3167a8313dda49f97766ad900ba820384611962c
4727251e0cd73359b15b664c3170e5d754078599
/src/category_theory/preadditive/injective_resolution.lean
1ef24b230c5a945f801afe8eaa56ec8f48b1149c
[ "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
3,874
lean
/- Copyright (c) 2022 Jujian Zhang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jujian Zhang, Scott Morrison -/ import category_theory.preadditive.injective import algebra.homology.single /-! # Injective resolutions A injective resolution `I : InjectiveResolution Z` of an object `Z : C` consists of a `ℕ`-indexed cochain complex `I.cocomplex` of injective objects, along with a cochain map `I.ι` from cochain complex consisting just of `Z` in degree zero to `C`, so that the augmented cochain complex is exact. ``` Z ----> 0 ----> ... ----> 0 ----> ... | | | | | | v v v I⁰ ---> I¹ ---> ... ----> Iⁿ ---> ... ``` -/ noncomputable theory open category_theory open category_theory.limits universes v u namespace category_theory variables {C : Type u} [category.{v} C] open injective variables [has_zero_object C] [has_zero_morphisms C] [has_equalizers C] [has_images C] /-- An `InjectiveResolution Z` consists of a bundled `ℕ`-indexed cochain complex of injective objects, along with a quasi-isomorphism to the complex consisting of just `Z` supported in degree `0`. Except in situations where you want to provide a particular injective resolution (for example to compute a derived functor), you will not typically need to use this bundled object, and will instead use * `injective_resolution Z`: the `ℕ`-indexed cochain complex (equipped with `injective` and `exact` instances) * `injective_resolution.ι Z`: the cochain map from `(single C _ 0).obj Z` to `injective_resolution Z` (all the components are equipped with `mono` instances, and when the category is `abelian` we will show `ι` is a quasi-iso). -/ @[nolint has_inhabited_instance] structure InjectiveResolution (Z : C) := (cocomplex : cochain_complex C ℕ) (ι: ((cochain_complex.single₀ C).obj Z) ⟶ cocomplex) (injective : ∀ n, injective (cocomplex.X n) . tactic.apply_instance) (exact₀ : exact (ι.f 0) (cocomplex.d 0 1) . tactic.apply_instance) (exact : ∀ n, exact (cocomplex.d n (n+1)) (cocomplex.d (n+1) (n+2)) . tactic.apply_instance) (mono : mono (ι.f 0) . tactic.apply_instance) attribute [instance] InjectiveResolution.injective InjectiveResolution.mono /-- An object admits a injective resolution. -/ class has_injective_resolution (Z : C) : Prop := (out [] : nonempty (InjectiveResolution Z)) section variables (C) /-- You will rarely use this typeclass directly: it is implied by the combination `[enough_injectives C]` and `[abelian C]`. -/ class has_injective_resolutions : Prop := (out : ∀ Z : C, has_injective_resolution Z) attribute [instance, priority 100] has_injective_resolutions.out end namespace InjectiveResolution @[simp] lemma ι_f_succ {Z : C} (I : InjectiveResolution Z) (n : ℕ) : I.ι.f (n+1) = 0 := begin apply zero_of_source_iso_zero, dsimp, refl, end @[simp] lemma ι_f_zero_comp_complex_d {Z : C} (I : InjectiveResolution Z) : I.ι.f 0 ≫ I.cocomplex.d 0 1 = 0 := I.exact₀.w @[simp] lemma complex_d_comp {Z : C} (I : InjectiveResolution Z) (n : ℕ) : I.cocomplex.d n (n + 1) ≫ I.cocomplex.d (n + 1) (n + 2) = 0 := (I.exact _).w instance {Z : C} (I : InjectiveResolution Z) (n : ℕ) : category_theory.mono (I.ι.f n) := by cases n; apply_instance /-- An injective object admits a trivial injective resolution: itself in degree 0. -/ def self (Z : C) [category_theory.injective Z] : InjectiveResolution Z := { cocomplex := (cochain_complex.single₀ C).obj Z, ι := 𝟙 ((cochain_complex.single₀ C).obj Z), injective := λ n, begin cases n; { dsimp, apply_instance }, end, exact₀ := by { dsimp, exact exact_epi_zero _ }, exact := λ n, by { dsimp, exact exact_of_zero _ _ }, mono := by { dsimp, apply_instance, }, } end InjectiveResolution end category_theory
9676aca6ebbee725933d957179c3a09df8c17d3d
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/library/theories/analysis/sqrt.lean
027e6870ad930d6b197c2c469f829a2d00a82140
[ "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
3,188
lean
/- Copyright (c) 2015 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis, Jeremy Avigad The square root function. -/ import .ivt open analysis real classical noncomputable theory private definition sqr_lb (x : ℝ) : ℝ := 0 private theorem sqr_lb_is_lb (x : ℝ) (H : x ≥ 0) : (sqr_lb x) * (sqr_lb x) ≤ x := by rewrite [↑sqr_lb, zero_mul]; assumption private definition sqr_ub (x : ℝ) : ℝ := x + 1 private theorem sqr_ub_is_ub (x : ℝ) (H : x ≥ 0) : (sqr_ub x) * (sqr_ub x) ≥ x := begin rewrite [↑sqr_ub, left_distrib, mul_one, right_distrib, one_mul, {x + 1}add.comm, -*add.assoc], apply le_add_of_nonneg_left, repeat apply add_nonneg, apply mul_nonneg, repeat assumption, apply zero_le_one end private theorem lb_le_ub (x : ℝ) (H : x ≥ 0) : sqr_lb x ≤ sqr_ub x := begin rewrite [↑sqr_lb, ↑sqr_ub], apply add_nonneg, assumption, apply zero_le_one end private lemma sqr_cts : continuous (λ x : ℝ, x * x) := continuous_mul_of_continuous id_continuous id_continuous definition sqrt (x : ℝ) : ℝ := if H : x ≥ 0 then some (intermediate_value_incr_weak sqr_cts (lb_le_ub x H) (sqr_lb_is_lb x H) (sqr_ub_is_ub x H)) else 0 private theorem sqrt_spec {x : ℝ} (H : x ≥ 0) : sqrt x * sqrt x = x ∧ sqrt x ≥ 0 := begin rewrite [↑sqrt, dif_pos H], note Hs := some_spec (intermediate_value_incr_weak sqr_cts (lb_le_ub x H) (sqr_lb_is_lb x H) (sqr_ub_is_ub x H)), cases Hs with Hs1 Hs2, cases Hs2 with Hs2a Hs2b, exact and.intro Hs2b Hs1 end theorem sqrt_mul_self {x : ℝ} (H : x ≥ 0) : sqrt x * sqrt x = x := and.left (sqrt_spec H) theorem sqrt_nonneg (x : ℝ) : sqrt x ≥ 0 := if H : x ≥ 0 then and.right (sqrt_spec H) else by rewrite [↑sqrt, dif_neg H]; exact le.refl 0 theorem sqrt_squared {x : ℝ} (H : x ≥ 0) : (sqrt x)^2 = x := by krewrite [pow_two, sqrt_mul_self H] theorem sqrt_zero : sqrt (0 : ℝ) = 0 := have sqrt 0 * sqrt 0 = 0, from sqrt_mul_self !le.refl, or.elim (eq_zero_or_eq_zero_of_mul_eq_zero this) (λ H, H) (λ H, H) theorem sqrt_squared_of_nonneg {x : ℝ} (H : x ≥ 0) : sqrt (x^2) = x := have sqrt (x^2)^2 = x^2, from sqrt_squared (squared_nonneg x), eq_of_squared_eq_squared_of_nonneg (sqrt_nonneg (x^2)) H this theorem sqrt_squared' (x : ℝ) : sqrt (x^2) = abs x := have x^2 = (abs x)^2, by krewrite [+pow_two, -abs_mul, abs_mul_self], using this, by rewrite [this, sqrt_squared_of_nonneg (abs_nonneg x)] theorem sqrt_mul {x y : ℝ} (Hx : x ≥ 0) (Hy : y ≥ 0) : sqrt (x * y) = sqrt x * sqrt y := have (sqrt (x * y))^2 = (sqrt x * sqrt y)^2, from calc (sqrt (x * y))^2 = x * y : by rewrite [sqrt_squared (mul_nonneg Hx Hy)] ... = (sqrt x)^2 * (sqrt y)^2 : by rewrite [sqrt_squared Hx, sqrt_squared Hy] ... = (sqrt x * sqrt y)^2 : by krewrite [*pow_two]; rewrite [*mul.assoc, mul.left_comm (sqrt y)], eq_of_squared_eq_squared_of_nonneg !sqrt_nonneg (mul_nonneg !sqrt_nonneg !sqrt_nonneg) this
f2ab5b7b30d4fce5ec49461645401f7b03f28e29
4bcaca5dc83d49803f72b7b5920b75b6e7d9de2d
/stage0/src/Lean.lean
c0f2146003f3da8e3865ff7ebbdb36019aad758c
[ "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
794
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.Data import Lean.Compiler import Lean.Environment import Lean.Modifiers import Lean.ProjFns import Lean.Runtime import Lean.ResolveName import Lean.Attributes import Lean.Parser import Lean.ReducibilityAttrs import Lean.Elab import Lean.Class import Lean.LocalContext import Lean.MetavarContext import Lean.AuxRecursor import Lean.Meta import Lean.Util import Lean.Eval import Lean.Structure import Lean.PrettyPrinter import Lean.CoreM import Lean.InternalExceptionId import Lean.Server import Lean.ScopedEnvExtension import Lean.DocString import Lean.DeclarationRange import Lean.LazyInitExtension import Lean.Widget
db04144c2964677099cf90a98abcb3e9e37e99ce
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/order/filter/filter_product.lean
ef6bfa38056468f93873a7fec53f03c8371fdb38
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
7,288
lean
/- Copyright (c) 2019 Abhimanyu Pallavi Sudhir. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Abhimanyu Pallavi Sudhir, Yury Kudryashov -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.order.filter.ultrafilter import Mathlib.order.filter.germ import Mathlib.PostPort universes u v namespace Mathlib /-! # Ultraproducts If `φ` is an ultrafilter, then the space of germs of functions `f : α → β` at `φ` is called the *ultraproduct*. In this file we prove properties of ultraproducts that rely on `φ` being an ultrafilter. Definitions and properties that work for any filter should go to `order.filter.germ`. ## Tags ultrafilter, ultraproduct -/ namespace filter namespace germ /-- If `φ` is an ultrafilter then the ultraproduct is a division ring. -/ protected instance division_ring {α : Type u} {β : Type v} {φ : ultrafilter α} [division_ring β] : division_ring (germ (↑φ) β) := division_ring.mk ring.add sorry ring.zero sorry sorry ring.neg ring.sub sorry sorry ring.mul sorry ring.one sorry sorry sorry sorry div_inv_monoid.inv div_inv_monoid.div sorry sorry sorry /-- If `φ` is an ultrafilter then the ultraproduct is a field. -/ protected instance field {α : Type u} {β : Type v} {φ : ultrafilter α} [field β] : field (germ (↑φ) β) := field.mk comm_ring.add sorry comm_ring.zero sorry sorry comm_ring.neg comm_ring.sub sorry sorry comm_ring.mul sorry comm_ring.one sorry sorry sorry sorry sorry division_ring.inv sorry sorry sorry /-- If `φ` is an ultrafilter then the ultraproduct is a linear order. -/ protected instance linear_order {α : Type u} {β : Type v} {φ : ultrafilter α} [linear_order β] : linear_order (germ (↑φ) β) := linear_order.mk partial_order.le partial_order.lt sorry sorry sorry sorry (fun (a b : germ (↑φ) β) => classical.prop_decidable (a ≤ b)) Mathlib.decidable_eq_of_decidable_le Mathlib.decidable_lt_of_decidable_le @[simp] theorem const_div {α : Type u} {β : Type v} {φ : ultrafilter α} [division_ring β] (x : β) (y : β) : ↑(x / y) = ↑x / ↑y := rfl theorem coe_lt {α : Type u} {β : Type v} {φ : ultrafilter α} [preorder β] {f : α → β} {g : α → β} : ↑f < ↑g ↔ filter.eventually (fun (x : α) => f x < g x) ↑φ := sorry theorem coe_pos {α : Type u} {β : Type v} {φ : ultrafilter α} [preorder β] [HasZero β] {f : α → β} : 0 < ↑f ↔ filter.eventually (fun (x : α) => 0 < f x) ↑φ := coe_lt theorem const_lt {α : Type u} {β : Type v} {φ : ultrafilter α} [preorder β] {x : β} {y : β} : ↑x < ↑y ↔ x < y := iff.trans coe_lt lift_rel_const_iff theorem lt_def {α : Type u} {β : Type v} {φ : ultrafilter α} [preorder β] : Less = lift_rel Less := sorry /-- If `φ` is an ultrafilter then the ultraproduct is an ordered ring. -/ protected instance ordered_ring {α : Type u} {β : Type v} {φ : ultrafilter α} [ordered_ring β] : ordered_ring (germ (↑φ) β) := ordered_ring.mk ring.add sorry ring.zero sorry sorry ring.neg ring.sub sorry sorry ring.mul sorry ring.one sorry sorry sorry sorry ordered_add_comm_group.le ordered_add_comm_group.lt sorry sorry sorry sorry sorry sorry /-- If `φ` is an ultrafilter then the ultraproduct is a linear ordered ring. -/ protected instance linear_ordered_ring {α : Type u} {β : Type v} {φ : ultrafilter α} [linear_ordered_ring β] : linear_ordered_ring (germ (↑φ) β) := linear_ordered_ring.mk ordered_ring.add sorry ordered_ring.zero sorry sorry ordered_ring.neg ordered_ring.sub sorry sorry ordered_ring.mul sorry ordered_ring.one sorry sorry sorry sorry ordered_ring.le ordered_ring.lt sorry sorry sorry sorry sorry sorry sorry linear_order.decidable_le linear_order.decidable_eq linear_order.decidable_lt sorry /-- If `φ` is an ultrafilter then the ultraproduct is a linear ordered field. -/ protected instance linear_ordered_field {α : Type u} {β : Type v} {φ : ultrafilter α} [linear_ordered_field β] : linear_ordered_field (germ (↑φ) β) := linear_ordered_field.mk linear_ordered_ring.add sorry linear_ordered_ring.zero sorry sorry linear_ordered_ring.neg linear_ordered_ring.sub sorry sorry linear_ordered_ring.mul sorry linear_ordered_ring.one sorry sorry sorry sorry linear_ordered_ring.le linear_ordered_ring.lt sorry sorry sorry sorry sorry sorry sorry linear_ordered_ring.decidable_le linear_ordered_ring.decidable_eq linear_ordered_ring.decidable_lt sorry sorry field.inv sorry sorry /-- If `φ` is an ultrafilter then the ultraproduct is a linear ordered commutative ring. -/ protected instance linear_ordered_comm_ring {α : Type u} {β : Type v} {φ : ultrafilter α} [linear_ordered_comm_ring β] : linear_ordered_comm_ring (germ (↑φ) β) := linear_ordered_comm_ring.mk linear_ordered_ring.add sorry linear_ordered_ring.zero sorry sorry linear_ordered_ring.neg linear_ordered_ring.sub sorry sorry linear_ordered_ring.mul sorry linear_ordered_ring.one sorry sorry sorry sorry linear_ordered_ring.le linear_ordered_ring.lt sorry sorry sorry sorry sorry sorry sorry linear_ordered_ring.decidable_le linear_ordered_ring.decidable_eq linear_ordered_ring.decidable_lt sorry sorry /-- If `φ` is an ultrafilter then the ultraproduct is a decidable linear ordered commutative group. -/ protected instance linear_ordered_add_comm_group {α : Type u} {β : Type v} {φ : ultrafilter α} [linear_ordered_add_comm_group β] : linear_ordered_add_comm_group (germ (↑φ) β) := linear_ordered_add_comm_group.mk ordered_add_comm_group.add sorry ordered_add_comm_group.zero sorry sorry ordered_add_comm_group.neg ordered_add_comm_group.sub sorry sorry ordered_add_comm_group.le ordered_add_comm_group.lt sorry sorry sorry sorry linear_order.decidable_le linear_order.decidable_eq linear_order.decidable_lt sorry theorem max_def {α : Type u} {β : Type v} {φ : ultrafilter α} [linear_order β] (x : germ (↑φ) β) (y : germ (↑φ) β) : max x y = map₂ max x y := sorry theorem min_def {α : Type u} {β : Type v} {φ : ultrafilter α} [K : linear_order β] (x : germ (↑φ) β) (y : germ (↑φ) β) : min x y = map₂ min x y := sorry theorem abs_def {α : Type u} {β : Type v} {φ : ultrafilter α} [linear_ordered_add_comm_group β] (x : germ (↑φ) β) : abs x = map abs x := sorry @[simp] theorem const_max {α : Type u} {β : Type v} {φ : ultrafilter α} [linear_order β] (x : β) (y : β) : ↑(max x y) = max ↑x ↑y := eq.mpr (id (Eq._oldrec (Eq.refl (↑(max x y) = max ↑x ↑y)) (max_def ↑x ↑y))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑(max x y) = map₂ max ↑x ↑y)) (map₂_const (↑φ) x y max))) (Eq.refl ↑(max x y))) @[simp] theorem const_min {α : Type u} {β : Type v} {φ : ultrafilter α} [linear_order β] (x : β) (y : β) : ↑(min x y) = min ↑x ↑y := eq.mpr (id (Eq._oldrec (Eq.refl (↑(min x y) = min ↑x ↑y)) (min_def ↑x ↑y))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑(min x y) = map₂ min ↑x ↑y)) (map₂_const (↑φ) x y min))) (Eq.refl ↑(min x y))) @[simp] theorem const_abs {α : Type u} {β : Type v} {φ : ultrafilter α} [linear_ordered_add_comm_group β] (x : β) : ↑(abs x) = abs ↑x := const_max x (-x)
5feb2dd440b574850710c9348b92d9d0e3d4ab39
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/data/int/gcd.lean
74ad4895f189128b61d61971bc9ebc2d347b835b
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
5,149
lean
/- Copyright (c) 2018 Guy Leroy. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sangwoo Jo (aka Jason), Guy Leroy, Johannes Hölzl, Mario Carneiro -/ import data.int.basic data.nat.prime /-! # Extended GCD and divisibility over ℤ ## Main definitions * Given `x y : ℕ`, `xgcd x y` computes the pair of integers `(a, b)` such that `gcd x y = x * a + y * b`. `gcd_a x y` and `gcd_b x y` are defined to be `a` and `b`, respectively. ## Main statements * `gcd_eq_gcd_ab`: Bézout's lemma, given `x y : ℕ`, `gcd x y = x * gcd_a x y + y * gcd_b x y`. -/ /-! ### Extended Euclidean algorithm -/ namespace nat /-- Helper function for the extended GCD algorithm (`nat.xgcd`). -/ def xgcd_aux : ℕ → ℤ → ℤ → ℕ → ℤ → ℤ → ℕ × ℤ × ℤ | 0 s t r' s' t' := (r', s', t') | r@(succ _) s t r' s' t' := have r' % r < r, from mod_lt _ $ succ_pos _, let q := r' / r in xgcd_aux (r' % r) (s' - q * s) (t' - q * t) r s t @[simp] theorem xgcd_zero_left {s t r' s' t'} : xgcd_aux 0 s t r' s' t' = (r', s', t') := by simp [xgcd_aux] @[simp] theorem xgcd_aux_rec {r s t r' s' t'} (h : 0 < r) : xgcd_aux r s t r' s' t' = xgcd_aux (r' % r) (s' - (r' / r) * s) (t' - (r' / r) * t) r s t := by cases r; [exact absurd h (lt_irrefl _), {simp only [xgcd_aux], refl}] /-- Use the extended GCD algorithm to generate the `a` and `b` values satisfying `gcd x y = x * a + y * b`. -/ def xgcd (x y : ℕ) : ℤ × ℤ := (xgcd_aux x 1 0 y 0 1).2 /-- The extended GCD `a` value in the equation `gcd x y = x * a + y * b`. -/ def gcd_a (x y : ℕ) : ℤ := (xgcd x y).1 /-- The extended GCD `b` value in the equation `gcd x y = x * a + y * b`. -/ def gcd_b (x y : ℕ) : ℤ := (xgcd x y).2 @[simp] theorem xgcd_aux_fst (x y) : ∀ s t s' t', (xgcd_aux x s t y s' t').1 = gcd x y := gcd.induction x y (by simp) (λ x y h IH s t s' t', by simp [h, IH]; rw ← gcd_rec) theorem xgcd_aux_val (x y) : xgcd_aux x 1 0 y 0 1 = (gcd x y, xgcd x y) := by rw [xgcd, ← xgcd_aux_fst x y 1 0 0 1]; cases xgcd_aux x 1 0 y 0 1; refl theorem xgcd_val (x y) : xgcd x y = (gcd_a x y, gcd_b x y) := by unfold gcd_a gcd_b; cases xgcd x y; refl section parameters (x y : ℕ) private def P : ℕ × ℤ × ℤ → Prop | (r, s, t) := (r : ℤ) = x * s + y * t theorem xgcd_aux_P {r r'} : ∀ {s t s' t'}, P (r, s, t) → P (r', s', t') → P (xgcd_aux r s t r' s' t') := gcd.induction r r' (by simp) $ λ a b h IH s t s' t' p p', begin rw [xgcd_aux_rec h], refine IH _ p, dsimp [P] at *, rw [int.mod_def], generalize : (b / a : ℤ) = k, rw [p, p'], simp [mul_add, mul_comm, mul_left_comm, add_comm, add_left_comm, sub_eq_neg_add] end /-- Bézout's lemma: given `x y : ℕ`, `gcd x y = x * a + y * b`, where `a = gcd_a x y` and `b = gcd_b x y` are computed by the extended Euclidean algorithm. -/ theorem gcd_eq_gcd_ab : (gcd x y : ℤ) = x * gcd_a x y + y * gcd_b x y := by have := @xgcd_aux_P x y x y 1 0 0 1 (by simp [P]) (by simp [P]); rwa [xgcd_aux_val, xgcd_val] at this end end nat /-! ### Divisibility over ℤ -/ namespace int theorem nat_abs_div (a b : ℤ) (H : b ∣ a) : nat_abs (a / b) = (nat_abs a) / (nat_abs b) := begin cases (nat.eq_zero_or_pos (nat_abs b)), {rw eq_zero_of_nat_abs_eq_zero h, simp [int.div_zero]}, calc nat_abs (a / b) = nat_abs (a / b) * 1 : by rw mul_one ... = nat_abs (a / b) * (nat_abs b / nat_abs b) : by rw nat.div_self h ... = nat_abs (a / b) * nat_abs b / nat_abs b : by rw (nat.mul_div_assoc _ (dvd_refl _)) ... = nat_abs (a / b * b) / nat_abs b : by rw (nat_abs_mul (a / b) b) ... = nat_abs a / nat_abs b : by rw int.div_mul_cancel H, end theorem nat_abs_dvd_abs_iff {i j : ℤ} : i.nat_abs ∣ j.nat_abs ↔ i ∣ j := ⟨assume (H : i.nat_abs ∣ j.nat_abs), dvd_nat_abs.mp (nat_abs_dvd.mp (coe_nat_dvd.mpr H)), assume H : (i ∣ j), coe_nat_dvd.mp (dvd_nat_abs.mpr (nat_abs_dvd.mpr H))⟩ lemma succ_dvd_or_succ_dvd_of_succ_sum_dvd_mul {p : ℕ} (p_prime : nat.prime p) {m n : ℤ} {k l : ℕ} (hpm : ↑(p ^ k) ∣ m) (hpn : ↑(p ^ l) ∣ n) (hpmn : ↑(p ^ (k+l+1)) ∣ m*n) : ↑(p ^ (k+1)) ∣ m ∨ ↑(p ^ (l+1)) ∣ n := have hpm' : p ^ k ∣ m.nat_abs, from int.coe_nat_dvd.1 $ int.dvd_nat_abs.2 hpm, have hpn' : p ^ l ∣ n.nat_abs, from int.coe_nat_dvd.1 $ int.dvd_nat_abs.2 hpn, have hpmn' : (p ^ (k+l+1)) ∣ m.nat_abs*n.nat_abs, by rw ←int.nat_abs_mul; apply (int.coe_nat_dvd.1 $ int.dvd_nat_abs.2 hpmn), let hsd := nat.succ_dvd_or_succ_dvd_of_succ_sum_dvd_mul p_prime hpm' hpn' hpmn' in hsd.elim (λ hsd1, or.inl begin apply int.dvd_nat_abs.1, apply int.coe_nat_dvd.2 hsd1 end) (λ hsd2, or.inr begin apply int.dvd_nat_abs.1, apply int.coe_nat_dvd.2 hsd2 end) theorem dvd_of_mul_dvd_mul_left {i j k : ℤ} (k_non_zero : k ≠ 0) (H : k * i ∣ k * j) : i ∣ j := dvd.elim H (λl H1, by rw mul_assoc at H1; exact ⟨_, eq_of_mul_eq_mul_left k_non_zero H1⟩) theorem dvd_of_mul_dvd_mul_right {i j k : ℤ} (k_non_zero : k ≠ 0) (H : i * k ∣ j * k) : i ∣ j := by rw [mul_comm i k, mul_comm j k] at H; exact dvd_of_mul_dvd_mul_left k_non_zero H end int
5895d5b08720a6b84b1bc15e0f67fb346eaea703
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/tests/lean/derivingRepr.lean
e88cc34f51a538b019b6d553dbece97570fa5d75
[ "Apache-2.0" ]
permissive
williamdemeo/lean4
72161c58fe65c3ad955d6a3050bb7d37c04c0d54
6d00fcf1d6d873e195f9220c668ef9c58e9c4a35
refs/heads/master
1,678,305,356,877
1,614,708,995,000
1,614,708,995,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
979
lean
structure Foo where name : String val : List Nat lower : Nat := List.length val inv : val.length >= lower flag : Bool deriving Repr #eval { name := "Joe", val := List.iota 40, flag := true, inv := by decide! : Foo } inductive Tree (α : Type) where | node : List (Tree α) → Bool → Tree α | leaf : α → Tree α deriving Repr #eval Tree.node (List.iota 10 |>.map fun i => Tree.node [Tree.leaf i] (i%2==0)) true inductive StructureLikeInductive where | field : Nat -> StructureLikeInductive deriving Repr #eval StructureLikeInductive.field 5 namespace Foo mutual inductive Tree (α : Type u) where | node : TreeList α → Tree α | leaf : α → Tree α deriving Repr inductive TreeList (α : Type u) where | nil : TreeList α | cons : Tree α → TreeList α → TreeList α deriving Repr end #eval Tree.node (TreeList.cons (Tree.leaf 30) (TreeList.cons (Tree.leaf 20) (TreeList.cons (Tree.leaf 10) TreeList.nil))) end Foo
ed5a40c346d2caee41cea725cae7059fa5b3e6dc
94e33a31faa76775069b071adea97e86e218a8ee
/src/linear_algebra/vandermonde.lean
b548a2cb00e323ade9ff69840477ccc09dc96f73
[ "Apache-2.0" ]
permissive
urkud/mathlib
eab80095e1b9f1513bfb7f25b4fa82fa4fd02989
6379d39e6b5b279df9715f8011369a301b634e41
refs/heads/master
1,658,425,342,662
1,658,078,703,000
1,658,078,703,000
186,910,338
0
0
Apache-2.0
1,568,512,083,000
1,557,958,709,000
Lean
UTF-8
Lean
false
false
5,567
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 algebra.big_operators.fin import algebra.geom_sum import group_theory.perm.fin import linear_algebra.matrix.determinant /-! # Vandermonde matrix This file defines the `vandermonde` matrix and gives its determinant. ## Main definitions - `vandermonde v`: a square matrix with the `i, j`th entry equal to `v i ^ j`. ## Main results - `det_vandermonde`: `det (vandermonde v)` is the product of `v i - v j`, where `(i, j)` ranges over the unordered pairs. -/ variables {R : Type*} [comm_ring R] open equiv finset open_locale big_operators matrix namespace matrix /-- `vandermonde v` is the square matrix with `i`th row equal to `1, v i, v i ^ 2, v i ^ 3, ...`. -/ def vandermonde {n : ℕ} (v : fin n → R) : matrix (fin n) (fin n) R := λ i j, v i ^ (j : ℕ) @[simp] lemma vandermonde_apply {n : ℕ} (v : fin n → R) (i j) : vandermonde v i j = v i ^ (j : ℕ) := rfl @[simp] lemma vandermonde_cons {n : ℕ} (v0 : R) (v : fin n → R) : vandermonde (fin.cons v0 v : fin n.succ → R) = fin.cons (λ j, v0 ^ (j : ℕ)) (λ i, fin.cons 1 (λ j, v i * vandermonde v i j)) := begin ext i j, refine fin.cases (by simp) (λ i, _) i, refine fin.cases (by simp) (λ j, _) j, simp [pow_succ] end lemma vandermonde_succ {n : ℕ} (v : fin n.succ → R) : vandermonde v = fin.cons (λ j, v 0 ^ (j : ℕ)) (λ i, fin.cons 1 (λ j, v i.succ * vandermonde (fin.tail v) i j)) := begin conv_lhs { rw [← fin.cons_self_tail v, vandermonde_cons] }, simp only [fin.tail] end lemma vandermonde_mul_vandermonde_transpose {n : ℕ} (v w : fin n → R) (i j) : (vandermonde v ⬝ (vandermonde w)ᵀ) i j = ∑ (k : fin n), (v i * w j) ^ (k : ℕ) := by simp only [vandermonde_apply, matrix.mul_apply, matrix.transpose_apply, mul_pow] lemma vandermonde_transpose_mul_vandermonde {n : ℕ} (v : fin n → R) (i j) : ((vandermonde v)ᵀ ⬝ vandermonde v) i j = ∑ (k : fin n), v k ^ (i + j : ℕ) := by simp only [vandermonde_apply, matrix.mul_apply, matrix.transpose_apply, pow_add] lemma det_vandermonde {n : ℕ} (v : fin n → R) : det (vandermonde v) = ∏ i : fin n, ∏ j in Ioi i, (v j - v i) := begin unfold vandermonde, induction n with n ih, { exact det_eq_one_of_card_eq_zero (fintype.card_fin 0) }, calc det (of $ λ (i j : fin n.succ), v i ^ (j : ℕ)) = det (of $ λ (i j : fin n.succ), matrix.vec_cons (v 0 ^ (j : ℕ)) (λ i, v (fin.succ i) ^ (j : ℕ) - v 0 ^ (j : ℕ)) i) : det_eq_of_forall_row_eq_smul_add_const (matrix.vec_cons 0 1) 0 (fin.cons_zero _ _) _ ... = det (of $ λ (i j : fin n), matrix.vec_cons (v 0 ^ (j.succ : ℕ)) (λ (i : fin n), v (fin.succ i) ^ (j.succ : ℕ) - v 0 ^ (j.succ : ℕ)) (fin.succ_above 0 i)) : by simp_rw [det_succ_column_zero, fin.sum_univ_succ, of_apply, matrix.cons_val_zero, minor, of_apply, matrix.cons_val_succ, fin.coe_zero, pow_zero, one_mul, sub_self, mul_zero, zero_mul, finset.sum_const_zero, add_zero] ... = det (of $ λ (i j : fin n), (v (fin.succ i) - v 0) * (∑ k in finset.range (j + 1 : ℕ), v i.succ ^ k * v 0 ^ (j - k : ℕ)) : matrix _ _ R) : by { congr, ext i j, rw [fin.succ_above_zero, matrix.cons_val_succ, fin.coe_succ, mul_comm], exact (geom_sum₂_mul (v i.succ) (v 0) (j + 1 : ℕ)).symm } ... = (∏ (i : fin n), (v (fin.succ i) - v 0)) * det (λ (i j : fin n), (∑ k in finset.range (j + 1 : ℕ), v i.succ ^ k * v 0 ^ (j - k : ℕ))) : det_mul_column (λ i, v (fin.succ i) - v 0) _ ... = (∏ (i : fin n), (v (fin.succ i) - v 0)) * det (λ (i j : fin n), v (fin.succ i) ^ (j : ℕ)) : congr_arg ((*) _) _ ... = ∏ i : fin n.succ, ∏ j in Ioi i, (v j - v i) : by simp_rw [ih (v ∘ fin.succ), fin.prod_univ_succ, fin.prod_Ioi_zero, fin.prod_Ioi_succ], { intros i j, simp_rw [of_apply], rw matrix.cons_val_zero, refine fin.cases _ (λ i, _) i, { simp }, rw [matrix.cons_val_succ, matrix.cons_val_succ, pi.one_apply], ring }, { cases n, { simp only [det_eq_one_of_card_eq_zero (fintype.card_fin 0)] }, apply det_eq_of_forall_col_eq_smul_add_pred (λ i, v 0), { intro j, simp }, { intros i j, simp only [smul_eq_mul, pi.add_apply, fin.coe_succ, fin.coe_cast_succ, pi.smul_apply], rw [finset.sum_range_succ, add_comm, tsub_self, pow_zero, mul_one, finset.mul_sum], congr' 1, refine finset.sum_congr rfl (λ i' hi', _), rw [mul_left_comm (v 0), nat.succ_sub, pow_succ], exact nat.lt_succ_iff.mp (finset.mem_range.mp hi') } } end lemma det_vandermonde_eq_zero_iff [is_domain R] {n : ℕ} {v : fin n → R} : det (vandermonde v) = 0 ↔ ∃ (i j : fin n), v i = v j ∧ i ≠ j := begin split, { simp only [det_vandermonde v, finset.prod_eq_zero_iff, sub_eq_zero, forall_exists_index], exact λ i _ j h₁ h₂, ⟨j, i, h₂, (mem_Ioi.mp h₁).ne'⟩ }, { simp only [ne.def, forall_exists_index, and_imp], refine λ i j h₁ h₂, matrix.det_zero_of_row_eq h₂ (funext $ λ k, _), rw [vandermonde_apply, vandermonde_apply, h₁], } end lemma det_vandermonde_ne_zero_iff [is_domain R] {n : ℕ} {v : fin n → R} : det (vandermonde v) ≠ 0 ↔ function.injective v := by simpa only [det_vandermonde_eq_zero_iff, ne.def, not_exists, not_and, not_not] end matrix
e3202767e0d6b77292743aa6d1d9ba5c2680a68f
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/order/fixed_points.lean
881bfb6cc6e9eb4749437695566daf96800267cd
[]
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
6,973
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Kenny Lau -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.order.complete_lattice import Mathlib.dynamics.fixed_points.basic import Mathlib.PostPort universes u v namespace Mathlib /-! # Fixed point construction on complete lattices -/ /-- Least fixed point of a monotone function -/ /-- Greatest fixed point of a monotone function -/ def lfp {α : Type u} [complete_lattice α] (f : α → α) : α := Inf (set_of fun (a : α) => f a ≤ a) def gfp {α : Type u} [complete_lattice α] (f : α → α) : α := Sup (set_of fun (a : α) => a ≤ f a) theorem lfp_le {α : Type u} [complete_lattice α] {f : α → α} {a : α} (h : f a ≤ a) : lfp f ≤ a := Inf_le h theorem le_lfp {α : Type u} [complete_lattice α] {f : α → α} {a : α} (h : ∀ (b : α), f b ≤ b → a ≤ b) : a ≤ lfp f := le_Inf h theorem lfp_eq {α : Type u} [complete_lattice α] {f : α → α} (m : monotone f) : lfp f = f (lfp f) := (fun (this : f (lfp f) ≤ lfp f) => le_antisymm (lfp_le (m this)) this) (le_lfp fun (b : α) (h : f b ≤ b) => le_trans (m (lfp_le h)) h) theorem lfp_induct {α : Type u} [complete_lattice α] {f : α → α} {p : α → Prop} (m : monotone f) (step : ∀ (a : α), p a → a ≤ lfp f → p (f a)) (sup : ∀ (s : set α), (∀ (a : α), a ∈ s → p a) → p (Sup s)) : p (lfp f) := sorry theorem monotone_lfp {α : Type u} [complete_lattice α] : monotone lfp := fun (f g : α → α) (this : f ≤ g) => le_lfp fun (a : α) (this_1 : g a ≤ a) => lfp_le (le_trans (this a) this_1) theorem le_gfp {α : Type u} [complete_lattice α] {f : α → α} {a : α} (h : a ≤ f a) : a ≤ gfp f := le_Sup h theorem gfp_le {α : Type u} [complete_lattice α] {f : α → α} {a : α} (h : ∀ (b : α), b ≤ f b → b ≤ a) : gfp f ≤ a := Sup_le h theorem gfp_eq {α : Type u} [complete_lattice α] {f : α → α} (m : monotone f) : gfp f = f (gfp f) := (fun (this : gfp f ≤ f (gfp f)) => le_antisymm this (le_gfp (m this))) (gfp_le fun (b : α) (h : b ≤ f b) => le_trans h (m (le_gfp h))) theorem gfp_induct {α : Type u} [complete_lattice α] {f : α → α} {p : α → Prop} (m : monotone f) (step : ∀ (a : α), p a → gfp f ≤ a → p (f a)) (inf : ∀ (s : set α), (∀ (a : α), a ∈ s → p a) → p (Inf s)) : p (gfp f) := sorry theorem monotone_gfp {α : Type u} [complete_lattice α] : monotone gfp := fun (f g : α → α) (this : f ≤ g) => gfp_le fun (a : α) (this_1 : a ≤ f a) => le_gfp (le_trans this_1 (this a)) -- Rolling rule theorem lfp_comp {α : Type u} {β : Type v} [complete_lattice α] [complete_lattice β] {f : β → α} {g : α → β} (m_f : monotone f) (m_g : monotone g) : lfp (f ∘ g) = f (lfp (g ∘ f)) := sorry theorem gfp_comp {α : Type u} {β : Type v} [complete_lattice α] [complete_lattice β] {f : β → α} {g : α → β} (m_f : monotone f) (m_g : monotone g) : gfp (f ∘ g) = f (gfp (g ∘ f)) := sorry -- Diagonal rule theorem lfp_lfp {α : Type u} [complete_lattice α] {h : α → α → α} (m : ∀ {a b c d : α}, a ≤ b → c ≤ d → h a c ≤ h b d) : lfp (lfp ∘ h) = lfp fun (x : α) => h x x := sorry theorem gfp_gfp {α : Type u} [complete_lattice α] {h : α → α → α} (m : ∀ {a b c d : α}, a ≤ b → c ≤ d → h a c ≤ h b d) : gfp (gfp ∘ h) = gfp fun (x : α) => h x x := sorry /- The complete lattice of fixed points of a function f -/ namespace fixed_points def prev {α : Type u} [complete_lattice α] (f : α → α) (x : α) : α := gfp fun (z : α) => x ⊓ f z def next {α : Type u} [complete_lattice α] (f : α → α) (x : α) : α := lfp fun (z : α) => x ⊔ f z theorem prev_le {α : Type u} [complete_lattice α] {f : α → α} {x : α} : prev f x ≤ x := gfp_le fun (z : α) (hz : z ≤ x ⊓ f z) => le_trans hz inf_le_left theorem prev_eq {α : Type u} [complete_lattice α] {f : α → α} (hf : monotone f) {a : α} (h : f a ≤ a) : prev f a = f (prev f a) := sorry def prev_fixed {α : Type u} [complete_lattice α] {f : α → α} (hf : monotone f) (a : α) (h : f a ≤ a) : ↥(function.fixed_points f) := { val := prev f a, property := sorry } theorem next_le {α : Type u} [complete_lattice α] {f : α → α} {x : α} : x ≤ next f x := le_lfp fun (z : α) (hz : x ⊔ f z ≤ z) => le_trans le_sup_left hz theorem next_eq {α : Type u} [complete_lattice α] {f : α → α} (hf : monotone f) {a : α} (h : a ≤ f a) : next f a = f (next f a) := sorry def next_fixed {α : Type u} [complete_lattice α] {f : α → α} (hf : monotone f) (a : α) (h : a ≤ f a) : ↥(function.fixed_points f) := { val := next f a, property := sorry } theorem sup_le_f_of_fixed_points {α : Type u} [complete_lattice α] (f : α → α) (hf : monotone f) (x : ↥(function.fixed_points f)) (y : ↥(function.fixed_points f)) : subtype.val x ⊔ subtype.val y ≤ f (subtype.val x ⊔ subtype.val y) := sorry theorem f_le_inf_of_fixed_points {α : Type u} [complete_lattice α] (f : α → α) (hf : monotone f) (x : ↥(function.fixed_points f)) (y : ↥(function.fixed_points f)) : f (subtype.val x ⊓ subtype.val y) ≤ subtype.val x ⊓ subtype.val y := sorry theorem Sup_le_f_of_fixed_points {α : Type u} [complete_lattice α] (f : α → α) (hf : monotone f) (A : set α) (HA : A ⊆ function.fixed_points f) : Sup A ≤ f (Sup A) := Sup_le fun (x : α) (hxA : x ∈ A) => HA hxA ▸ hf (le_Sup hxA) theorem f_le_Inf_of_fixed_points {α : Type u} [complete_lattice α] (f : α → α) (hf : monotone f) (A : set α) (HA : A ⊆ function.fixed_points f) : f (Inf A) ≤ Inf A := le_Inf fun (x : α) (hxA : x ∈ A) => HA hxA ▸ hf (Inf_le hxA) /-- The fixed points of `f` form a complete lattice. This cannot be an instance, since it depends on the monotonicity of `f`. -/ protected def complete_lattice {α : Type u} [complete_lattice α] (f : α → α) (hf : monotone f) : complete_lattice ↥(function.fixed_points f) := complete_lattice.mk (fun (x y : ↥(function.fixed_points f)) => next_fixed hf (subtype.val x ⊔ subtype.val y) (sup_le_f_of_fixed_points f hf x y)) (fun (x y : ↥(function.fixed_points f)) => subtype.val x ≤ subtype.val y) (bounded_lattice.lt._default fun (x y : ↥(function.fixed_points f)) => subtype.val x ≤ subtype.val y) sorry sorry sorry sorry sorry sorry (fun (x y : ↥(function.fixed_points f)) => prev_fixed hf (subtype.val x ⊓ subtype.val y) (f_le_inf_of_fixed_points f hf x y)) sorry sorry sorry (prev_fixed hf ⊤ sorry) sorry (next_fixed hf ⊥ sorry) sorry (fun (A : set ↥(function.fixed_points f)) => next_fixed hf (Sup (subtype.val '' A)) sorry) (fun (A : set ↥(function.fixed_points f)) => prev_fixed hf (Inf (subtype.val '' A)) sorry) sorry sorry sorry sorry
292ae5056ef3761944fad53f86393c7a84d08b7e
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/group_theory/finiteness.lean
7a47344442139836d26c9e87f16b874ddfea242b
[ "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
15,304
lean
/- Copyright (c) 2021 Riccardo Brasca. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Riccardo Brasca -/ import data.set.pointwise.finite import group_theory.quotient_group import group_theory.submonoid.operations import group_theory.subgroup.basic import set_theory.cardinal.finite import data.finset.preimage /-! # Finitely generated monoids and groups > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. We define finitely generated monoids and groups. See also `submodule.fg` and `module.finite` for finitely-generated modules. ## Main definition * `submonoid.fg S`, `add_submonoid.fg S` : A submonoid `S` is finitely generated. * `monoid.fg M`, `add_monoid.fg M` : A typeclass indicating a type `M` is finitely generated as a monoid. * `subgroup.fg S`, `add_subgroup.fg S` : A subgroup `S` is finitely generated. * `group.fg M`, `add_group.fg M` : A typeclass indicating a type `M` is finitely generated as a group. -/ /-! ### Monoids and submonoids -/ open_locale pointwise variables {M N : Type*} [monoid M] [add_monoid N] section submonoid /-- A submonoid of `M` is finitely generated if it is the closure of a finite subset of `M`. -/ @[to_additive] def submonoid.fg (P : submonoid M) : Prop := ∃ S : finset M, submonoid.closure ↑S = P /-- An additive submonoid of `N` is finitely generated if it is the closure of a finite subset of `M`. -/ add_decl_doc add_submonoid.fg /-- An equivalent expression of `submonoid.fg` in terms of `set.finite` instead of `finset`. -/ @[to_additive "An equivalent expression of `add_submonoid.fg` in terms of `set.finite` instead of `finset`."] lemma submonoid.fg_iff (P : submonoid M) : submonoid.fg P ↔ ∃ S : set M, submonoid.closure S = P ∧ S.finite := ⟨λ ⟨S, hS⟩, ⟨S, hS, finset.finite_to_set S⟩, λ ⟨S, hS, hf⟩, ⟨set.finite.to_finset hf, by simp [hS]⟩⟩ lemma submonoid.fg_iff_add_fg (P : submonoid M) : P.fg ↔ P.to_add_submonoid.fg := ⟨λ h, let ⟨S, hS, hf⟩ := (submonoid.fg_iff _).1 h in (add_submonoid.fg_iff _).mpr ⟨additive.to_mul ⁻¹' S, by simp [← submonoid.to_add_submonoid_closure, hS], hf⟩, λ h, let ⟨T, hT, hf⟩ := (add_submonoid.fg_iff _).1 h in (submonoid.fg_iff _).mpr ⟨multiplicative.of_add ⁻¹' T, by simp [← add_submonoid.to_submonoid'_closure, hT], hf⟩⟩ lemma add_submonoid.fg_iff_mul_fg (P : add_submonoid N) : P.fg ↔ P.to_submonoid.fg := begin convert (submonoid.fg_iff_add_fg P.to_submonoid).symm, exact set_like.ext' rfl end end submonoid section monoid variables (M N) /-- A monoid is finitely generated if it is finitely generated as a submonoid of itself. -/ class monoid.fg : Prop := (out : (⊤ : submonoid M).fg) /-- An additive monoid is finitely generated if it is finitely generated as an additive submonoid of itself. -/ class add_monoid.fg : Prop := (out : (⊤ : add_submonoid N).fg) attribute [to_additive] monoid.fg variables {M N} lemma monoid.fg_def : monoid.fg M ↔ (⊤ : submonoid M).fg := ⟨λ h, h.1, λ h, ⟨h⟩⟩ lemma add_monoid.fg_def : add_monoid.fg N ↔ (⊤ : add_submonoid N).fg := ⟨λ h, h.1, λ h, ⟨h⟩⟩ /-- An equivalent expression of `monoid.fg` in terms of `set.finite` instead of `finset`. -/ @[to_additive "An equivalent expression of `add_monoid.fg` in terms of `set.finite` instead of `finset`."] lemma monoid.fg_iff : monoid.fg M ↔ ∃ S : set M, submonoid.closure S = (⊤ : submonoid M) ∧ S.finite := ⟨λ h, (submonoid.fg_iff ⊤).1 h.out, λ h, ⟨(submonoid.fg_iff ⊤).2 h⟩⟩ lemma monoid.fg_iff_add_fg : monoid.fg M ↔ add_monoid.fg (additive M) := ⟨λ h, ⟨(submonoid.fg_iff_add_fg ⊤).1 h.out⟩, λ h, ⟨(submonoid.fg_iff_add_fg ⊤).2 h.out⟩⟩ lemma add_monoid.fg_iff_mul_fg : add_monoid.fg N ↔ monoid.fg (multiplicative N) := ⟨λ h, ⟨(add_submonoid.fg_iff_mul_fg ⊤).1 h.out⟩, λ h, ⟨(add_submonoid.fg_iff_mul_fg ⊤).2 h.out⟩⟩ instance add_monoid.fg_of_monoid_fg [monoid.fg M] : add_monoid.fg (additive M) := monoid.fg_iff_add_fg.1 ‹_› instance monoid.fg_of_add_monoid_fg [add_monoid.fg N] : monoid.fg (multiplicative N) := add_monoid.fg_iff_mul_fg.1 ‹_› @[to_additive, priority 100] instance monoid.fg_of_finite [finite M] : monoid.fg M := by { casesI nonempty_fintype M, exact ⟨⟨finset.univ, by rw finset.coe_univ; exact submonoid.closure_univ⟩⟩ } end monoid @[to_additive] lemma submonoid.fg.map {M' : Type*} [monoid M'] {P : submonoid M} (h : P.fg) (e : M →* M') : (P.map e).fg := begin classical, obtain ⟨s, rfl⟩ := h, exact ⟨s.image e, by rw [finset.coe_image, monoid_hom.map_mclosure]⟩ end @[to_additive] lemma submonoid.fg.map_injective {M' : Type*} [monoid M'] {P : submonoid M} (e : M →* M') (he : function.injective e) (h : (P.map e).fg) : P.fg := begin obtain ⟨s, hs⟩ := h, use s.preimage e (he.inj_on _), apply submonoid.map_injective_of_injective he, rw [← hs, e.map_mclosure, finset.coe_preimage], congr, rw [set.image_preimage_eq_iff, ← e.coe_mrange, ← submonoid.closure_le, hs, e.mrange_eq_map], exact submonoid.monotone_map le_top end @[simp, to_additive] lemma monoid.fg_iff_submonoid_fg (N : submonoid M) : monoid.fg N ↔ N.fg := begin conv_rhs { rw [← N.range_subtype, monoid_hom.mrange_eq_map] }, exact ⟨λ h, h.out.map N.subtype, λ h, ⟨h.map_injective N.subtype subtype.coe_injective⟩⟩ end @[to_additive] lemma monoid.fg_of_surjective {M' : Type*} [monoid M'] [monoid.fg M] (f : M →* M') (hf : function.surjective f) : monoid.fg M' := begin classical, obtain ⟨s, hs⟩ := monoid.fg_def.mp ‹_›, use s.image f, rwa [finset.coe_image, ← monoid_hom.map_mclosure, hs, ← monoid_hom.mrange_eq_map, monoid_hom.mrange_top_iff_surjective], end @[to_additive] instance monoid.fg_range {M' : Type*} [monoid M'] [monoid.fg M] (f : M →* M') : monoid.fg f.mrange := monoid.fg_of_surjective f.mrange_restrict f.mrange_restrict_surjective @[to_additive add_submonoid.multiples_fg] lemma submonoid.powers_fg (r : M) : (submonoid.powers r).fg := ⟨{r}, (finset.coe_singleton r).symm ▸ (submonoid.powers_eq_closure r).symm⟩ @[to_additive add_monoid.multiples_fg] instance monoid.powers_fg (r : M) : monoid.fg (submonoid.powers r) := (monoid.fg_iff_submonoid_fg _).mpr (submonoid.powers_fg r) @[to_additive] instance monoid.closure_finset_fg (s : finset M) : monoid.fg (submonoid.closure (s : set M)) := begin refine ⟨⟨s.preimage coe (subtype.coe_injective.inj_on _), _⟩⟩, rw [finset.coe_preimage, submonoid.closure_closure_coe_preimage], end @[to_additive] instance monoid.closure_finite_fg (s : set M) [finite s] : monoid.fg (submonoid.closure s) := begin haveI := fintype.of_finite s, exact s.coe_to_finset ▸ monoid.closure_finset_fg s.to_finset, end /-! ### Groups and subgroups -/ variables {G H : Type*} [group G] [add_group H] section subgroup /-- A subgroup of `G` is finitely generated if it is the closure of a finite subset of `G`. -/ @[to_additive] def subgroup.fg (P : subgroup G) : Prop := ∃ S : finset G, subgroup.closure ↑S = P /-- An additive subgroup of `H` is finitely generated if it is the closure of a finite subset of `H`. -/ add_decl_doc add_subgroup.fg /-- An equivalent expression of `subgroup.fg` in terms of `set.finite` instead of `finset`. -/ @[to_additive "An equivalent expression of `add_subgroup.fg` in terms of `set.finite` instead of `finset`."] lemma subgroup.fg_iff (P : subgroup G) : subgroup.fg P ↔ ∃ S : set G, subgroup.closure S = P ∧ S.finite := ⟨λ⟨S, hS⟩, ⟨S, hS, finset.finite_to_set S⟩, λ⟨S, hS, hf⟩, ⟨set.finite.to_finset hf, by simp [hS]⟩⟩ /-- A subgroup is finitely generated if and only if it is finitely generated as a submonoid. -/ @[to_additive add_subgroup.fg_iff_add_submonoid.fg "An additive subgroup is finitely generated if and only if it is finitely generated as an additive submonoid."] lemma subgroup.fg_iff_submonoid_fg (P : subgroup G) : P.fg ↔ P.to_submonoid.fg := begin split, { rintro ⟨S, rfl⟩, rw submonoid.fg_iff, refine ⟨S ∪ S⁻¹, _, S.finite_to_set.union S.finite_to_set.inv⟩, exact (subgroup.closure_to_submonoid _).symm }, { rintro ⟨S, hS⟩, refine ⟨S, le_antisymm _ _⟩, { rw [subgroup.closure_le, ←subgroup.coe_to_submonoid, ←hS], exact submonoid.subset_closure }, { rw [← subgroup.to_submonoid_le, ← hS, submonoid.closure_le], exact subgroup.subset_closure } } end lemma subgroup.fg_iff_add_fg (P : subgroup G) : P.fg ↔ P.to_add_subgroup.fg := begin rw [subgroup.fg_iff_submonoid_fg, add_subgroup.fg_iff_add_submonoid.fg], exact (subgroup.to_submonoid P).fg_iff_add_fg end lemma add_subgroup.fg_iff_mul_fg (P : add_subgroup H) : P.fg ↔ P.to_subgroup.fg := begin rw [add_subgroup.fg_iff_add_submonoid.fg, subgroup.fg_iff_submonoid_fg], exact add_submonoid.fg_iff_mul_fg (add_subgroup.to_add_submonoid P) end end subgroup section group variables (G H) /-- A group is finitely generated if it is finitely generated as a submonoid of itself. -/ class group.fg : Prop := (out : (⊤ : subgroup G).fg) /-- An additive group is finitely generated if it is finitely generated as an additive submonoid of itself. -/ class add_group.fg : Prop := (out : (⊤ : add_subgroup H).fg) attribute [to_additive] group.fg variables {G H} lemma group.fg_def : group.fg G ↔ (⊤ : subgroup G).fg := ⟨λ h, h.1, λ h, ⟨h⟩⟩ lemma add_group.fg_def : add_group.fg H ↔ (⊤ : add_subgroup H).fg := ⟨λ h, h.1, λ h, ⟨h⟩⟩ /-- An equivalent expression of `group.fg` in terms of `set.finite` instead of `finset`. -/ @[to_additive "An equivalent expression of `add_group.fg` in terms of `set.finite` instead of `finset`."] lemma group.fg_iff : group.fg G ↔ ∃ S : set G, subgroup.closure S = (⊤ : subgroup G) ∧ S.finite := ⟨λ h, (subgroup.fg_iff ⊤).1 h.out, λ h, ⟨(subgroup.fg_iff ⊤).2 h⟩⟩ @[to_additive] lemma group.fg_iff' : group.fg G ↔ ∃ n (S : finset G), S.card = n ∧ subgroup.closure (S : set G) = ⊤ := group.fg_def.trans ⟨λ ⟨S, hS⟩, ⟨S.card, S, rfl, hS⟩, λ ⟨n, S, hn, hS⟩, ⟨S, hS⟩⟩ /-- A group is finitely generated if and only if it is finitely generated as a monoid. -/ @[to_additive add_group.fg_iff_add_monoid.fg "An additive group is finitely generated if and only if it is finitely generated as an additive monoid."] lemma group.fg_iff_monoid.fg : group.fg G ↔ monoid.fg G := ⟨λ h, monoid.fg_def.2 $ (subgroup.fg_iff_submonoid_fg ⊤).1 (group.fg_def.1 h), λ h, group.fg_def.2 $ (subgroup.fg_iff_submonoid_fg ⊤).2 (monoid.fg_def.1 h)⟩ lemma group_fg.iff_add_fg : group.fg G ↔ add_group.fg (additive G) := ⟨λ h, ⟨(subgroup.fg_iff_add_fg ⊤).1 h.out⟩, λ h, ⟨(subgroup.fg_iff_add_fg ⊤).2 h.out⟩⟩ lemma add_group.fg_iff_mul_fg : add_group.fg H ↔ group.fg (multiplicative H) := ⟨λ h, ⟨(add_subgroup.fg_iff_mul_fg ⊤).1 h.out⟩, λ h, ⟨(add_subgroup.fg_iff_mul_fg ⊤).2 h.out⟩⟩ instance add_group.fg_of_group_fg [group.fg G] : add_group.fg (additive G) := group_fg.iff_add_fg.1 ‹_› instance group.fg_of_mul_group_fg [add_group.fg H] : group.fg (multiplicative H) := add_group.fg_iff_mul_fg.1 ‹_› @[to_additive, priority 100] instance group.fg_of_finite [finite G] : group.fg G := by { casesI nonempty_fintype G, exact ⟨⟨finset.univ, by rw finset.coe_univ; exact subgroup.closure_univ⟩⟩ } @[to_additive] lemma group.fg_of_surjective {G' : Type*} [group G'] [hG : group.fg G] {f : G →* G'} (hf : function.surjective f) : group.fg G' := group.fg_iff_monoid.fg.mpr $ @monoid.fg_of_surjective G _ G' _ (group.fg_iff_monoid.fg.mp hG) f hf @[to_additive] instance group.fg_range {G' : Type*} [group G'] [group.fg G] (f : G →* G') : group.fg f.range := group.fg_of_surjective f.range_restrict_surjective @[to_additive] instance group.closure_finset_fg (s : finset G) : group.fg (subgroup.closure (s : set G)) := begin refine ⟨⟨s.preimage coe (subtype.coe_injective.inj_on _), _⟩⟩, rw [finset.coe_preimage, ←subgroup.coe_subtype, subgroup.closure_preimage_eq_top], end @[to_additive] instance group.closure_finite_fg (s : set G) [finite s] : group.fg (subgroup.closure s) := begin haveI := fintype.of_finite s, exact s.coe_to_finset ▸ group.closure_finset_fg s.to_finset, end variables (G) /-- The minimum number of generators of a group. -/ @[to_additive "The minimum number of generators of an additive group"] noncomputable def group.rank [h : group.fg G] := @nat.find _ (classical.dec_pred _) (group.fg_iff'.mp h) @[to_additive] lemma group.rank_spec [h : group.fg G] : ∃ S : finset G, S.card = group.rank G ∧ subgroup.closure (S : set G) = ⊤ := @nat.find_spec _ (classical.dec_pred _) (group.fg_iff'.mp h) @[to_additive] lemma group.rank_le [h : group.fg G] {S : finset G} (hS : subgroup.closure (S : set G) = ⊤) : group.rank G ≤ S.card := @nat.find_le _ _ (classical.dec_pred _) (group.fg_iff'.mp h) ⟨S, rfl, hS⟩ variables {G} {G' : Type*} [group G'] @[to_additive] lemma group.rank_le_of_surjective [group.fg G] [group.fg G'] (f : G →* G') (hf : function.surjective f) : group.rank G' ≤ group.rank G := begin classical, obtain ⟨S, hS1, hS2⟩ := group.rank_spec G, transitivity (S.image f).card, { apply group.rank_le, rw [finset.coe_image, ←monoid_hom.map_closure, hS2, subgroup.map_top_of_surjective f hf] }, { exact finset.card_image_le.trans_eq hS1 }, end @[to_additive] lemma group.rank_range_le [group.fg G] {f : G →* G'} : group.rank f.range ≤ group.rank G := group.rank_le_of_surjective f.range_restrict f.range_restrict_surjective @[to_additive] lemma group.rank_congr [group.fg G] [group.fg G'] (f : G ≃* G') : group.rank G = group.rank G' := le_antisymm (group.rank_le_of_surjective f.symm f.symm.surjective) (group.rank_le_of_surjective f f.surjective) end group namespace subgroup @[to_additive] lemma rank_congr {H K : subgroup G} [group.fg H] [group.fg K] (h : H = K) : group.rank H = group.rank K := by unfreezingI { subst h } @[to_additive] lemma rank_closure_finset_le_card (s : finset G) : group.rank (closure (s : set G)) ≤ s.card := begin classical, let t : finset (closure (s : set G)) := s.preimage coe (subtype.coe_injective.inj_on _), have ht : closure (t : set (closure (s : set G))) = ⊤, { rw finset.coe_preimage, exact closure_preimage_eq_top s }, apply (group.rank_le (closure (s : set G)) ht).trans, rw [←finset.card_image_of_inj_on, finset.image_preimage], { apply finset.card_filter_le }, { apply subtype.coe_injective.inj_on }, end @[to_additive] lemma rank_closure_finite_le_nat_card (s : set G) [finite s] : group.rank (closure s) ≤ nat.card s := begin haveI := fintype.of_finite s, rw [nat.card_eq_fintype_card, ←s.to_finset_card, ←rank_congr (congr_arg _ s.coe_to_finset)], exact rank_closure_finset_le_card s.to_finset, end end subgroup section quotient_group @[to_additive] instance quotient_group.fg [group.fg G] (N : subgroup G) [subgroup.normal N] : group.fg $ G ⧸ N := group.fg_of_surjective $ quotient_group.mk'_surjective N end quotient_group
d6a5637986923349f00df9e50e2187b33518065c
94e33a31faa76775069b071adea97e86e218a8ee
/src/algebra/group_with_zero/basic.lean
5ac2e047923e4c7ab1a62b71efbeb5f893eb62ba
[ "Apache-2.0" ]
permissive
urkud/mathlib
eab80095e1b9f1513bfb7f25b4fa82fa4fd02989
6379d39e6b5b279df9715f8011369a301b634e41
refs/heads/master
1,658,425,342,662
1,658,078,703,000
1,658,078,703,000
186,910,338
0
0
Apache-2.0
1,568,512,083,000
1,557,958,709,000
Lean
UTF-8
Lean
false
false
43,247
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import algebra.group.inj_surj import algebra.group_with_zero.defs import algebra.hom.units import logic.nontrivial import group_theory.group_action.units /-! # Groups with an adjoined zero element This file describes structures that are not usually studied on their own right in mathematics, namely a special sort of monoid: apart from a distinguished “zero element” they form a group, or in other words, they are groups with an adjoined zero element. Examples are: * division rings; * the value monoid of a multiplicative valuation; * in particular, the non-negative real numbers. ## Main definitions Various lemmas about `group_with_zero` and `comm_group_with_zero`. To reduce import dependencies, the type-classes themselves are in `algebra.group_with_zero.defs`. ## Implementation details As is usual in mathlib, we extend the inverse function to the zero element, and require `0⁻¹ = 0`. -/ set_option old_structure_cmd true open_locale classical open function variables {M₀ G₀ M₀' G₀' : Type*} section section mul_zero_class variables [mul_zero_class M₀] {a b : M₀} /-- Pullback a `mul_zero_class` instance along an injective function. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.mul_zero_class [has_mul M₀'] [has_zero M₀'] (f : M₀' → M₀) (hf : injective f) (zero : f 0 = 0) (mul : ∀ a b, f (a * b) = f a * f b) : mul_zero_class M₀' := { mul := (*), zero := 0, zero_mul := λ a, hf $ by simp only [mul, zero, zero_mul], mul_zero := λ a, hf $ by simp only [mul, zero, mul_zero] } /-- Pushforward a `mul_zero_class` instance along an surjective function. See note [reducible non-instances]. -/ @[reducible] protected def function.surjective.mul_zero_class [has_mul M₀'] [has_zero M₀'] (f : M₀ → M₀') (hf : surjective f) (zero : f 0 = 0) (mul : ∀ a b, f (a * b) = f a * f b) : mul_zero_class M₀' := { mul := (*), zero := 0, mul_zero := hf.forall.2 $ λ x, by simp only [← zero, ← mul, mul_zero], zero_mul := hf.forall.2 $ λ x, by simp only [← zero, ← mul, zero_mul] } lemma mul_eq_zero_of_left (h : a = 0) (b : M₀) : a * b = 0 := h.symm ▸ zero_mul b lemma mul_eq_zero_of_right (a : M₀) (h : b = 0) : a * b = 0 := h.symm ▸ mul_zero a lemma left_ne_zero_of_mul : a * b ≠ 0 → a ≠ 0 := mt (λ h, mul_eq_zero_of_left h b) lemma right_ne_zero_of_mul : a * b ≠ 0 → b ≠ 0 := mt (mul_eq_zero_of_right a) lemma ne_zero_and_ne_zero_of_mul (h : a * b ≠ 0) : a ≠ 0 ∧ b ≠ 0 := ⟨left_ne_zero_of_mul h, right_ne_zero_of_mul h⟩ lemma mul_eq_zero_of_ne_zero_imp_eq_zero {a b : M₀} (h : a ≠ 0 → b = 0) : a * b = 0 := if ha : a = 0 then by rw [ha, zero_mul] else by rw [h ha, mul_zero] /-- To match `one_mul_eq_id`. -/ lemma zero_mul_eq_const : ((*) (0 : M₀)) = function.const _ 0 := funext zero_mul /-- To match `mul_one_eq_id`. -/ lemma mul_zero_eq_const : (* (0 : M₀)) = function.const _ 0 := funext mul_zero end mul_zero_class /-- Pushforward a `no_zero_divisors` instance along an injective function. -/ protected lemma function.injective.no_zero_divisors [has_mul M₀] [has_zero M₀] [has_mul M₀'] [has_zero M₀'] [no_zero_divisors M₀'] (f : M₀ → M₀') (hf : injective f) (zero : f 0 = 0) (mul : ∀ x y, f (x * y) = f x * f y) : no_zero_divisors M₀ := { eq_zero_or_eq_zero_of_mul_eq_zero := λ x y H, have f x * f y = 0, by rw [← mul, H, zero], (eq_zero_or_eq_zero_of_mul_eq_zero this).imp (λ H, hf $ by rwa zero) (λ H, hf $ by rwa zero) } lemma eq_zero_of_mul_self_eq_zero [has_mul M₀] [has_zero M₀] [no_zero_divisors M₀] {a : M₀} (h : a * a = 0) : a = 0 := (eq_zero_or_eq_zero_of_mul_eq_zero h).elim id id section variables [mul_zero_class M₀] [no_zero_divisors M₀] {a b : M₀} /-- If `α` has no zero divisors, then the product of two elements equals zero iff one of them equals zero. -/ @[simp] theorem mul_eq_zero : a * b = 0 ↔ a = 0 ∨ b = 0 := ⟨eq_zero_or_eq_zero_of_mul_eq_zero, λo, o.elim (λ h, mul_eq_zero_of_left h b) (mul_eq_zero_of_right a)⟩ /-- If `α` has no zero divisors, then the product of two elements equals zero iff one of them equals zero. -/ @[simp] theorem zero_eq_mul : 0 = a * b ↔ a = 0 ∨ b = 0 := by rw [eq_comm, mul_eq_zero] /-- If `α` has no zero divisors, then the product of two elements is nonzero iff both of them are nonzero. -/ theorem mul_ne_zero_iff : a * b ≠ 0 ↔ a ≠ 0 ∧ b ≠ 0 := (not_congr mul_eq_zero).trans not_or_distrib @[field_simps] theorem mul_ne_zero (ha : a ≠ 0) (hb : b ≠ 0) : a * b ≠ 0 := mul_ne_zero_iff.2 ⟨ha, hb⟩ /-- If `α` has no zero divisors, then for elements `a, b : α`, `a * b` equals zero iff so is `b * a`. -/ theorem mul_eq_zero_comm : a * b = 0 ↔ b * a = 0 := mul_eq_zero.trans $ (or_comm _ _).trans mul_eq_zero.symm /-- If `α` has no zero divisors, then for elements `a, b : α`, `a * b` is nonzero iff so is `b * a`. -/ theorem mul_ne_zero_comm : a * b ≠ 0 ↔ b * a ≠ 0 := not_congr mul_eq_zero_comm lemma mul_self_eq_zero : a * a = 0 ↔ a = 0 := by simp lemma zero_eq_mul_self : 0 = a * a ↔ a = 0 := by simp lemma mul_self_ne_zero : a * a ≠ 0 ↔ a ≠ 0 := not_congr mul_self_eq_zero lemma zero_ne_mul_self : 0 ≠ a * a ↔ a ≠ 0 := not_congr zero_eq_mul_self end end section variables [mul_zero_one_class M₀] /-- Pullback a `mul_zero_one_class` instance along an injective function. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.mul_zero_one_class [has_mul M₀'] [has_zero M₀'] [has_one M₀'] (f : M₀' → M₀) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ a b, f (a * b) = f a * f b) : mul_zero_one_class M₀' := { ..hf.mul_zero_class f zero mul, ..hf.mul_one_class f one mul } /-- Pushforward a `mul_zero_one_class` instance along an surjective function. See note [reducible non-instances]. -/ @[reducible] protected def function.surjective.mul_zero_one_class [has_mul M₀'] [has_zero M₀'] [has_one M₀'] (f : M₀ → M₀') (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ a b, f (a * b) = f a * f b) : mul_zero_one_class M₀' := { ..hf.mul_zero_class f zero mul, ..hf.mul_one_class f one mul } /-- In a monoid with zero, if zero equals one, then zero is the only element. -/ lemma eq_zero_of_zero_eq_one (h : (0 : M₀) = 1) (a : M₀) : a = 0 := by rw [← mul_one a, ← h, mul_zero] /-- In a monoid with zero, if zero equals one, then zero is the unique element. Somewhat arbitrarily, we define the default element to be `0`. All other elements will be provably equal to it, but not necessarily definitionally equal. -/ def unique_of_zero_eq_one (h : (0 : M₀) = 1) : unique M₀ := { default := 0, uniq := eq_zero_of_zero_eq_one h } /-- In a monoid with zero, zero equals one if and only if all elements of that semiring are equal. -/ theorem subsingleton_iff_zero_eq_one : (0 : M₀) = 1 ↔ subsingleton M₀ := ⟨λ h, @unique.subsingleton _ (unique_of_zero_eq_one h), λ h, @subsingleton.elim _ h _ _⟩ alias subsingleton_iff_zero_eq_one ↔ subsingleton_of_zero_eq_one _ lemma eq_of_zero_eq_one (h : (0 : M₀) = 1) (a b : M₀) : a = b := @subsingleton.elim _ (subsingleton_of_zero_eq_one h) a b /-- In a monoid with zero, either zero and one are nonequal, or zero is the only element. -/ lemma zero_ne_one_or_forall_eq_0 : (0 : M₀) ≠ 1 ∨ (∀a:M₀, a = 0) := not_or_of_imp eq_zero_of_zero_eq_one end section variables [mul_zero_one_class M₀] [nontrivial M₀] {a b : M₀} /-- In a nontrivial monoid with zero, zero and one are different. -/ @[simp] lemma zero_ne_one : 0 ≠ (1:M₀) := begin assume h, rcases exists_pair_ne M₀ with ⟨x, y, hx⟩, apply hx, calc x = 1 * x : by rw [one_mul] ... = 0 : by rw [← h, zero_mul] ... = 1 * y : by rw [← h, zero_mul] ... = y : by rw [one_mul] end @[simp] lemma one_ne_zero : (1:M₀) ≠ 0 := zero_ne_one.symm lemma ne_zero_of_eq_one {a : M₀} (h : a = 1) : a ≠ 0 := calc a = 1 : h ... ≠ 0 : one_ne_zero lemma left_ne_zero_of_mul_eq_one (h : a * b = 1) : a ≠ 0 := left_ne_zero_of_mul $ ne_zero_of_eq_one h lemma right_ne_zero_of_mul_eq_one (h : a * b = 1) : b ≠ 0 := right_ne_zero_of_mul $ ne_zero_of_eq_one h /-- Pullback a `nontrivial` instance along a function sending `0` to `0` and `1` to `1`. -/ protected lemma pullback_nonzero [has_zero M₀'] [has_one M₀'] (f : M₀' → M₀) (zero : f 0 = 0) (one : f 1 = 1) : nontrivial M₀' := ⟨⟨0, 1, mt (congr_arg f) $ by { rw [zero, one], exact zero_ne_one }⟩⟩ end section semigroup_with_zero /-- Pullback a `semigroup_with_zero` class along an injective function. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.semigroup_with_zero [has_zero M₀'] [has_mul M₀'] [semigroup_with_zero M₀] (f : M₀' → M₀) (hf : injective f) (zero : f 0 = 0) (mul : ∀ x y, f (x * y) = f x * f y) : semigroup_with_zero M₀' := { .. hf.mul_zero_class f zero mul, .. ‹has_zero M₀'›, .. hf.semigroup f mul } /-- Pushforward a `semigroup_with_zero` class along an surjective function. See note [reducible non-instances]. -/ @[reducible] protected def function.surjective.semigroup_with_zero [semigroup_with_zero M₀] [has_zero M₀'] [has_mul M₀'] (f : M₀ → M₀') (hf : surjective f) (zero : f 0 = 0) (mul : ∀ x y, f (x * y) = f x * f y) : semigroup_with_zero M₀' := { .. hf.mul_zero_class f zero mul, .. ‹has_zero M₀'›, .. hf.semigroup f mul } end semigroup_with_zero section monoid_with_zero /-- Pullback a `monoid_with_zero` class along an injective function. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.monoid_with_zero [has_zero M₀'] [has_mul M₀'] [has_one M₀'] [has_pow M₀' ℕ] [monoid_with_zero M₀] (f : M₀' → M₀) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) : monoid_with_zero M₀' := { .. hf.monoid f one mul npow, .. hf.mul_zero_class f zero mul } /-- Pushforward a `monoid_with_zero` class along a surjective function. See note [reducible non-instances]. -/ @[reducible] protected def function.surjective.monoid_with_zero [has_zero M₀'] [has_mul M₀'] [has_one M₀'] [has_pow M₀' ℕ] [monoid_with_zero M₀] (f : M₀ → M₀') (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) : monoid_with_zero M₀' := { .. hf.monoid f one mul npow, .. hf.mul_zero_class f zero mul } /-- Pullback a `monoid_with_zero` class along an injective function. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.comm_monoid_with_zero [has_zero M₀'] [has_mul M₀'] [has_one M₀'] [has_pow M₀' ℕ] [comm_monoid_with_zero M₀] (f : M₀' → M₀) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) : comm_monoid_with_zero M₀' := { .. hf.comm_monoid f one mul npow, .. hf.mul_zero_class f zero mul } /-- Pushforward a `monoid_with_zero` class along a surjective function. See note [reducible non-instances]. -/ @[reducible] protected def function.surjective.comm_monoid_with_zero [has_zero M₀'] [has_mul M₀'] [has_one M₀'] [has_pow M₀' ℕ] [comm_monoid_with_zero M₀] (f : M₀ → M₀') (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) : comm_monoid_with_zero M₀' := { .. hf.comm_monoid f one mul npow, .. hf.mul_zero_class f zero mul } variables [monoid_with_zero M₀] namespace units /-- An element of the unit group of a nonzero monoid with zero represented as an element of the monoid is nonzero. -/ @[simp] lemma ne_zero [nontrivial M₀] (u : M₀ˣ) : (u : M₀) ≠ 0 := left_ne_zero_of_mul_eq_one u.mul_inv -- We can't use `mul_eq_zero` + `units.ne_zero` in the next two lemmas because we don't assume -- `nonzero M₀`. @[simp] lemma mul_left_eq_zero (u : M₀ˣ) {a : M₀} : a * u = 0 ↔ a = 0 := ⟨λ h, by simpa using mul_eq_zero_of_left h ↑u⁻¹, λ h, mul_eq_zero_of_left h u⟩ @[simp] lemma mul_right_eq_zero (u : M₀ˣ) {a : M₀} : ↑u * a = 0 ↔ a = 0 := ⟨λ h, by simpa using mul_eq_zero_of_right ↑u⁻¹ h, mul_eq_zero_of_right u⟩ end units namespace is_unit lemma ne_zero [nontrivial M₀] {a : M₀} (ha : is_unit a) : a ≠ 0 := let ⟨u, hu⟩ := ha in hu ▸ u.ne_zero lemma mul_right_eq_zero {a b : M₀} (ha : is_unit a) : a * b = 0 ↔ b = 0 := let ⟨u, hu⟩ := ha in hu ▸ u.mul_right_eq_zero lemma mul_left_eq_zero {a b : M₀} (hb : is_unit b) : a * b = 0 ↔ a = 0 := let ⟨u, hu⟩ := hb in hu ▸ u.mul_left_eq_zero end is_unit @[simp] theorem is_unit_zero_iff : is_unit (0 : M₀) ↔ (0:M₀) = 1 := ⟨λ ⟨⟨_, a, (a0 : 0 * a = 1), _⟩, rfl⟩, by rwa zero_mul at a0, λ h, @is_unit_of_subsingleton _ _ (subsingleton_of_zero_eq_one h) 0⟩ @[simp] theorem not_is_unit_zero [nontrivial M₀] : ¬ is_unit (0 : M₀) := mt is_unit_zero_iff.1 zero_ne_one namespace ring open_locale classical /-- Introduce a function `inverse` on a monoid with zero `M₀`, which sends `x` to `x⁻¹` if `x` is invertible and to `0` otherwise. This definition is somewhat ad hoc, but one needs a fully (rather than partially) defined inverse function for some purposes, including for calculus. Note that while this is in the `ring` namespace for brevity, it requires the weaker assumption `monoid_with_zero M₀` instead of `ring M₀`. -/ noncomputable def inverse : M₀ → M₀ := λ x, if h : is_unit x then ((h.unit⁻¹ : M₀ˣ) : M₀) else 0 /-- By definition, if `x` is invertible then `inverse x = x⁻¹`. -/ @[simp] lemma inverse_unit (u : M₀ˣ) : inverse (u : M₀) = (u⁻¹ : M₀ˣ) := begin simp only [units.is_unit, inverse, dif_pos], exact units.inv_unique rfl end /-- By definition, if `x` is not invertible then `inverse x = 0`. -/ @[simp] lemma inverse_non_unit (x : M₀) (h : ¬(is_unit x)) : inverse x = 0 := dif_neg h lemma mul_inverse_cancel (x : M₀) (h : is_unit x) : x * inverse x = 1 := by { rcases h with ⟨u, rfl⟩, rw [inverse_unit, units.mul_inv], } lemma inverse_mul_cancel (x : M₀) (h : is_unit x) : inverse x * x = 1 := by { rcases h with ⟨u, rfl⟩, rw [inverse_unit, units.inv_mul], } lemma mul_inverse_cancel_right (x y : M₀) (h : is_unit x) : y * x * inverse x = y := by rw [mul_assoc, mul_inverse_cancel x h, mul_one] lemma inverse_mul_cancel_right (x y : M₀) (h : is_unit x) : y * inverse x * x = y := by rw [mul_assoc, inverse_mul_cancel x h, mul_one] lemma mul_inverse_cancel_left (x y : M₀) (h : is_unit x) : x * (inverse x * y) = y := by rw [← mul_assoc, mul_inverse_cancel x h, one_mul] lemma inverse_mul_cancel_left (x y : M₀) (h : is_unit x) : inverse x * (x * y) = y := by rw [← mul_assoc, inverse_mul_cancel x h, one_mul] variables (M₀) @[simp] lemma inverse_one : inverse (1 : M₀) = 1 := inverse_unit 1 @[simp] lemma inverse_zero : inverse (0 : M₀) = 0 := by { nontriviality, exact inverse_non_unit _ not_is_unit_zero } variables {M₀} lemma mul_inverse_rev' {a b : M₀} (h : commute a b) : inverse (a * b) = inverse b * inverse a := begin by_cases hab : is_unit (a * b), { obtain ⟨⟨a, rfl⟩, b, rfl⟩ := h.is_unit_mul_iff.mp hab, rw [←units.coe_mul, inverse_unit, inverse_unit, inverse_unit, ←units.coe_mul, mul_inv_rev], }, obtain ha | hb := not_and_distrib.mp (mt h.is_unit_mul_iff.mpr hab), { rw [inverse_non_unit _ hab, inverse_non_unit _ ha, mul_zero]}, { rw [inverse_non_unit _ hab, inverse_non_unit _ hb, zero_mul]}, end lemma mul_inverse_rev {M₀} [comm_monoid_with_zero M₀] (a b : M₀) : ring.inverse (a * b) = inverse b * inverse a := mul_inverse_rev' (commute.all _ _) end ring lemma is_unit.ring_inverse {a : M₀} : is_unit a → is_unit (ring.inverse a) | ⟨u, hu⟩ := hu ▸ ⟨u⁻¹, (ring.inverse_unit u).symm⟩ @[simp] lemma is_unit_ring_inverse {a : M₀} : is_unit (ring.inverse a) ↔ is_unit a := ⟨λ h, begin casesI subsingleton_or_nontrivial M₀, { convert h }, { contrapose h, rw ring.inverse_non_unit _ h, exact not_is_unit_zero, }, end, is_unit.ring_inverse⟩ lemma commute.ring_inverse_ring_inverse {a b : M₀} (h : commute a b) : commute (ring.inverse a) (ring.inverse b) := (ring.mul_inverse_rev' h.symm).symm.trans $ (congr_arg _ h.symm.eq).trans $ ring.mul_inverse_rev' h variable (M₀) end monoid_with_zero section cancel_monoid_with_zero variables [cancel_monoid_with_zero M₀] {a b c : M₀} @[priority 10] -- see Note [lower instance priority] instance cancel_monoid_with_zero.to_no_zero_divisors : no_zero_divisors M₀ := ⟨λ a b ab0, by { by_cases a = 0, { left, exact h }, right, apply cancel_monoid_with_zero.mul_left_cancel_of_ne_zero h, rw [ab0, mul_zero], }⟩ lemma mul_left_inj' (hc : c ≠ 0) : a * c = b * c ↔ a = b := (mul_left_injective₀ hc).eq_iff lemma mul_right_inj' (ha : a ≠ 0) : a * b = a * c ↔ b = c := (mul_right_injective₀ ha).eq_iff @[simp] lemma mul_eq_mul_right_iff : a * c = b * c ↔ a = b ∨ c = 0 := by by_cases hc : c = 0; [simp [hc], simp [mul_left_inj', hc]] @[simp] lemma mul_eq_mul_left_iff : a * b = a * c ↔ b = c ∨ a = 0 := by by_cases ha : a = 0; [simp [ha], simp [mul_right_inj', ha]] lemma mul_right_eq_self₀ : a * b = a ↔ b = 1 ∨ a = 0 := calc a * b = a ↔ a * b = a * 1 : by rw mul_one ... ↔ b = 1 ∨ a = 0 : mul_eq_mul_left_iff lemma mul_left_eq_self₀ : a * b = b ↔ a = 1 ∨ b = 0 := calc a * b = b ↔ a * b = 1 * b : by rw one_mul ... ↔ a = 1 ∨ b = 0 : mul_eq_mul_right_iff /-- Pullback a `monoid_with_zero` class along an injective function. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.cancel_monoid_with_zero [has_zero M₀'] [has_mul M₀'] [has_one M₀'] [has_pow M₀' ℕ] (f : M₀' → M₀) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) : cancel_monoid_with_zero M₀' := { mul_left_cancel_of_ne_zero := λ x y z hx H, hf $ mul_left_cancel₀ ((hf.ne_iff' zero).2 hx) $ by erw [← mul, ← mul, H]; refl, mul_right_cancel_of_ne_zero := λ x y z hx H, hf $ mul_right_cancel₀ ((hf.ne_iff' zero).2 hx) $ by erw [← mul, ← mul, H]; refl, .. hf.monoid f one mul npow, .. hf.mul_zero_class f zero mul } /-- An element of a `cancel_monoid_with_zero` fixed by right multiplication by an element other than one must be zero. -/ theorem eq_zero_of_mul_eq_self_right (h₁ : b ≠ 1) (h₂ : a * b = a) : a = 0 := classical.by_contradiction $ λ ha, h₁ $ mul_left_cancel₀ ha $ h₂.symm ▸ (mul_one a).symm /-- An element of a `cancel_monoid_with_zero` fixed by left multiplication by an element other than one must be zero. -/ theorem eq_zero_of_mul_eq_self_left (h₁ : b ≠ 1) (h₂ : b * a = a) : a = 0 := classical.by_contradiction $ λ ha, h₁ $ mul_right_cancel₀ ha $ h₂.symm ▸ (one_mul a).symm end cancel_monoid_with_zero section cancel_comm_monoid_with_zero variables [cancel_comm_monoid_with_zero M₀] {a b c : M₀} /-- Pullback a `cancel_comm_monoid_with_zero` class along an injective function. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.cancel_comm_monoid_with_zero [has_zero M₀'] [has_mul M₀'] [has_one M₀'] [has_pow M₀' ℕ] (f : M₀' → M₀) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) : cancel_comm_monoid_with_zero M₀' := { .. hf.comm_monoid_with_zero f zero one mul npow, .. hf.cancel_monoid_with_zero f zero one mul npow } end cancel_comm_monoid_with_zero section group_with_zero variables [group_with_zero G₀] {a b c g h x : G₀} /-- Pullback a `group_with_zero` class along an injective function. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.group_with_zero [has_zero G₀'] [has_mul G₀'] [has_one G₀'] [has_inv G₀'] [has_div G₀'] [has_pow G₀' ℕ] [has_pow G₀' ℤ] (f : G₀' → G₀) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f x⁻¹ = (f x)⁻¹) (div : ∀ x y, f (x / y) = f x / f y) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) (zpow : ∀ x (n : ℤ), f (x ^ n) = f x ^ n) : group_with_zero G₀' := { inv_zero := hf $ by erw [inv, zero, inv_zero], mul_inv_cancel := λ x hx, hf $ by erw [one, mul, inv, mul_inv_cancel ((hf.ne_iff' zero).2 hx)], .. hf.monoid_with_zero f zero one mul npow, .. hf.div_inv_monoid f one mul inv div npow zpow, .. pullback_nonzero f zero one, } /-- Pushforward a `group_with_zero` class along an surjective function. See note [reducible non-instances]. -/ @[reducible] protected def function.surjective.group_with_zero [has_zero G₀'] [has_mul G₀'] [has_one G₀'] [has_inv G₀'] [has_div G₀'] [has_pow G₀' ℕ] [has_pow G₀' ℤ] (h01 : (0:G₀') ≠ 1) (f : G₀ → G₀') (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f x⁻¹ = (f x)⁻¹) (div : ∀ x y, f (x / y) = f x / f y) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) (zpow : ∀ x (n : ℤ), f (x ^ n) = f x ^ n): group_with_zero G₀' := { inv_zero := by erw [← zero, ← inv, inv_zero], mul_inv_cancel := hf.forall.2 $ λ x hx, by erw [← inv, ← mul, mul_inv_cancel (mt (congr_arg f) $ trans_rel_left ne hx zero.symm)]; exact one, exists_pair_ne := ⟨0, 1, h01⟩, .. hf.monoid_with_zero f zero one mul npow, .. hf.div_inv_monoid f one mul inv div npow zpow } @[simp] lemma mul_inv_cancel_right₀ (h : b ≠ 0) (a : G₀) : (a * b) * b⁻¹ = a := calc (a * b) * b⁻¹ = a * (b * b⁻¹) : mul_assoc _ _ _ ... = a : by simp [h] @[simp] lemma mul_inv_cancel_left₀ (h : a ≠ 0) (b : G₀) : a * (a⁻¹ * b) = b := calc a * (a⁻¹ * b) = (a * a⁻¹) * b : (mul_assoc _ _ _).symm ... = b : by simp [h] lemma inv_ne_zero (h : a ≠ 0) : a⁻¹ ≠ 0 := assume a_eq_0, by simpa [a_eq_0] using mul_inv_cancel h @[simp] lemma inv_mul_cancel (h : a ≠ 0) : a⁻¹ * a = 1 := calc a⁻¹ * a = (a⁻¹ * a) * a⁻¹ * a⁻¹⁻¹ : by simp [inv_ne_zero h] ... = a⁻¹ * a⁻¹⁻¹ : by simp [h] ... = 1 : by simp [inv_ne_zero h] lemma group_with_zero.mul_left_injective (h : x ≠ 0) : function.injective (λ y, x * y) := λ y y' w, by simpa only [←mul_assoc, inv_mul_cancel h, one_mul] using congr_arg (λ y, x⁻¹ * y) w lemma group_with_zero.mul_right_injective (h : x ≠ 0) : function.injective (λ y, y * x) := λ y y' w, by simpa only [mul_assoc, mul_inv_cancel h, mul_one] using congr_arg (λ y, y * x⁻¹) w @[simp] lemma inv_mul_cancel_right₀ (h : b ≠ 0) (a : G₀) : (a * b⁻¹) * b = a := calc (a * b⁻¹) * b = a * (b⁻¹ * b) : mul_assoc _ _ _ ... = a : by simp [h] @[simp] lemma inv_mul_cancel_left₀ (h : a ≠ 0) (b : G₀) : a⁻¹ * (a * b) = b := calc a⁻¹ * (a * b) = (a⁻¹ * a) * b : (mul_assoc _ _ _).symm ... = b : by simp [h] private lemma inv_eq_of_mul (h : a * b = 1) : a⁻¹ = b := by rw [← inv_mul_cancel_left₀ (left_ne_zero_of_mul_eq_one h) b, h, mul_one] @[priority 100] -- See note [lower instance priority] instance group_with_zero.to_division_monoid : division_monoid G₀ := { inv := has_inv.inv, inv_inv := λ a, begin by_cases h : a = 0, { simp [h] }, { exact left_inv_eq_right_inv (inv_mul_cancel $ inv_ne_zero h) (inv_mul_cancel h) } end, mul_inv_rev := λ a b, begin by_cases ha : a = 0, { simp [ha] }, by_cases hb : b = 0, { simp [hb] }, refine inv_eq_of_mul _, simp [mul_assoc, ha, hb] end, inv_eq_of_mul := λ a b, inv_eq_of_mul, ..‹group_with_zero G₀› } end group_with_zero namespace units variables [group_with_zero G₀] variables {a b : G₀} /-- Embed a non-zero element of a `group_with_zero` into the unit group. By combining this function with the operations on units, or the `/ₚ` operation, it is possible to write a division as a partial function with three arguments. -/ def mk0 (a : G₀) (ha : a ≠ 0) : G₀ˣ := ⟨a, a⁻¹, mul_inv_cancel ha, inv_mul_cancel ha⟩ @[simp] lemma mk0_one (h := one_ne_zero) : mk0 (1 : G₀) h = 1 := by { ext, refl } @[simp] lemma coe_mk0 {a : G₀} (h : a ≠ 0) : (mk0 a h : G₀) = a := rfl @[simp] lemma mk0_coe (u : G₀ˣ) (h : (u : G₀) ≠ 0) : mk0 (u : G₀) h = u := units.ext rfl @[simp] lemma mul_inv' (u : G₀ˣ) : (u : G₀) * u⁻¹ = 1 := mul_inv_cancel u.ne_zero @[simp] lemma inv_mul' (u : G₀ˣ) : (u⁻¹ : G₀) * u = 1 := inv_mul_cancel u.ne_zero @[simp] lemma mk0_inj {a b : G₀} (ha : a ≠ 0) (hb : b ≠ 0) : units.mk0 a ha = units.mk0 b hb ↔ a = b := ⟨λ h, by injection h, λ h, units.ext h⟩ /-- In a group with zero, an existential over a unit can be rewritten in terms of `units.mk0`. -/ lemma exists0 {p : G₀ˣ → Prop} : (∃ g : G₀ˣ, p g) ↔ ∃ (g : G₀) (hg : g ≠ 0), p (units.mk0 g hg) := ⟨λ ⟨g, pg⟩, ⟨g, g.ne_zero, (g.mk0_coe g.ne_zero).symm ▸ pg⟩, λ ⟨g, hg, pg⟩, ⟨units.mk0 g hg, pg⟩⟩ /-- An alternative version of `units.exists0`. This one is useful if Lean cannot figure out `p` when using `units.exists0` from right to left. -/ lemma exists0' {p : Π g : G₀, g ≠ 0 → Prop} : (∃ (g : G₀) (hg : g ≠ 0), p g hg) ↔ ∃ g : G₀ˣ, p g g.ne_zero := iff.trans (by simp_rw [coe_mk0]) exists0.symm @[simp] lemma exists_iff_ne_zero {x : G₀} : (∃ u : G₀ˣ, ↑u = x) ↔ x ≠ 0 := by simp [exists0] lemma _root_.group_with_zero.eq_zero_or_unit (a : G₀) : a = 0 ∨ ∃ u : G₀ˣ, a = u := begin by_cases h : a = 0, { left, exact h }, { right, simpa only [eq_comm] using units.exists_iff_ne_zero.mpr h } end @[simp] lemma smul_mk0 {α : Type*} [has_smul G₀ α] {g : G₀} (hg : g ≠ 0) (a : α) : (mk0 g hg) • a = g • a := rfl end units section group_with_zero variables [group_with_zero G₀] {a b c : G₀} lemma is_unit.mk0 (x : G₀) (hx : x ≠ 0) : is_unit x := (units.mk0 x hx).is_unit lemma is_unit_iff_ne_zero : is_unit a ↔ a ≠ 0 := units.exists_iff_ne_zero alias is_unit_iff_ne_zero ↔ _ ne.is_unit attribute [protected] ne.is_unit @[priority 10] -- see Note [lower instance priority] instance group_with_zero.no_zero_divisors : no_zero_divisors G₀ := { eq_zero_or_eq_zero_of_mul_eq_zero := λ a b h, begin contrapose! h, exact ((units.mk0 a h.1) * (units.mk0 b h.2)).ne_zero end, .. (‹_› : group_with_zero G₀) } @[priority 10] -- see Note [lower instance priority] instance group_with_zero.cancel_monoid_with_zero : cancel_monoid_with_zero G₀ := { mul_left_cancel_of_ne_zero := λ x y z hx h, by rw [← inv_mul_cancel_left₀ hx y, h, inv_mul_cancel_left₀ hx z], mul_right_cancel_of_ne_zero := λ x y z hy h, by rw [← mul_inv_cancel_right₀ hy x, h, mul_inv_cancel_right₀ hy z], .. (‹_› : group_with_zero G₀) } -- Can't be put next to the other `mk0` lemmas because it depends on the -- `no_zero_divisors` instance, which depends on `mk0`. @[simp] lemma units.mk0_mul (x y : G₀) (hxy) : units.mk0 (x * y) hxy = units.mk0 x (mul_ne_zero_iff.mp hxy).1 * units.mk0 y (mul_ne_zero_iff.mp hxy).2 := by { ext, refl } @[simp] lemma div_self (h : a ≠ 0) : a / a = 1 := h.is_unit.div_self lemma eq_mul_inv_iff_mul_eq₀ (hc : c ≠ 0) : a = b * c⁻¹ ↔ a * c = b := hc.is_unit.eq_mul_inv_iff_mul_eq lemma eq_inv_mul_iff_mul_eq₀ (hb : b ≠ 0) : a = b⁻¹ * c ↔ b * a = c := hb.is_unit.eq_inv_mul_iff_mul_eq lemma inv_mul_eq_iff_eq_mul₀ (ha : a ≠ 0) : a⁻¹ * b = c ↔ b = a * c := ha.is_unit.inv_mul_eq_iff_eq_mul lemma mul_inv_eq_iff_eq_mul₀ (hb : b ≠ 0) : a * b⁻¹ = c ↔ a = c * b := hb.is_unit.mul_inv_eq_iff_eq_mul lemma mul_inv_eq_one₀ (hb : b ≠ 0) : a * b⁻¹ = 1 ↔ a = b := hb.is_unit.mul_inv_eq_one lemma inv_mul_eq_one₀ (ha : a ≠ 0) : a⁻¹ * b = 1 ↔ a = b := ha.is_unit.inv_mul_eq_one lemma mul_eq_one_iff_eq_inv₀ (hb : b ≠ 0) : a * b = 1 ↔ a = b⁻¹ := hb.is_unit.mul_eq_one_iff_eq_inv lemma mul_eq_one_iff_inv_eq₀ (ha : a ≠ 0) : a * b = 1 ↔ a⁻¹ = b := ha.is_unit.mul_eq_one_iff_inv_eq @[simp] lemma div_mul_cancel (a : G₀) (h : b ≠ 0) : a / b * b = a := h.is_unit.div_mul_cancel _ @[simp] lemma mul_div_cancel (a : G₀) (h : b ≠ 0) : a * b / b = a := h.is_unit.mul_div_cancel _ lemma mul_one_div_cancel (h : a ≠ 0) : a * (1 / a) = 1 := h.is_unit.mul_one_div_cancel lemma one_div_mul_cancel (h : a ≠ 0) : (1 / a) * a = 1 := h.is_unit.one_div_mul_cancel lemma div_left_inj' (hc : c ≠ 0) : a / c = b / c ↔ a = b := hc.is_unit.div_left_inj @[field_simps] lemma div_eq_iff (hb : b ≠ 0) : a / b = c ↔ a = c * b := hb.is_unit.div_eq_iff @[field_simps] lemma eq_div_iff (hb : b ≠ 0) : c = a / b ↔ c * b = a := hb.is_unit.eq_div_iff lemma div_eq_iff_mul_eq (hb : b ≠ 0) : a / b = c ↔ c * b = a := hb.is_unit.div_eq_iff.trans eq_comm lemma eq_div_iff_mul_eq (hc : c ≠ 0) : a = b / c ↔ a * c = b := hc.is_unit.eq_div_iff lemma div_eq_of_eq_mul (hb : b ≠ 0) : a = c * b → a / b = c := hb.is_unit.div_eq_of_eq_mul lemma eq_div_of_mul_eq (hc : c ≠ 0) : a * c = b → a = b / c := hc.is_unit.eq_div_of_mul_eq lemma div_eq_one_iff_eq (hb : b ≠ 0) : a / b = 1 ↔ a = b := hb.is_unit.div_eq_one_iff_eq lemma div_mul_left (hb : b ≠ 0) : b / (a * b) = 1 / a := hb.is_unit.div_mul_left lemma mul_div_mul_right (a b : G₀) (hc : c ≠ 0) : (a * c) / (b * c) = a / b := hc.is_unit.mul_div_mul_right _ _ lemma mul_mul_div (a : G₀) (hb : b ≠ 0) : a = a * b * (1 / b) := (hb.is_unit.mul_mul_div _).symm lemma div_div_div_cancel_right (a : G₀) (hc : c ≠ 0) : (a / c) / (b / c) = a / b := by rw [div_div_eq_mul_div, div_mul_cancel _ hc] lemma div_mul_div_cancel (a : G₀) (hc : c ≠ 0) : (a / c) * (c / b) = a / b := by rw [← mul_div_assoc, div_mul_cancel _ hc] @[simp] lemma zero_div (a : G₀) : 0 / a = 0 := by rw [div_eq_mul_inv, zero_mul] @[simp] lemma div_zero (a : G₀) : a / 0 = 0 := by rw [div_eq_mul_inv, inv_zero, mul_zero] /-- Multiplying `a` by itself and then by its inverse results in `a` (whether or not `a` is zero). -/ @[simp] lemma mul_self_mul_inv (a : G₀) : a * a * a⁻¹ = a := begin by_cases h : a = 0, { rw [h, inv_zero, mul_zero] }, { rw [mul_assoc, mul_inv_cancel h, mul_one] } end /-- Multiplying `a` by its inverse and then by itself results in `a` (whether or not `a` is zero). -/ @[simp] lemma mul_inv_mul_self (a : G₀) : a * a⁻¹ * a = a := begin by_cases h : a = 0, { rw [h, inv_zero, mul_zero] }, { rw [mul_inv_cancel h, one_mul] } end /-- Multiplying `a⁻¹` by `a` twice results in `a` (whether or not `a` is zero). -/ @[simp] lemma inv_mul_mul_self (a : G₀) : a⁻¹ * a * a = a := begin by_cases h : a = 0, { rw [h, inv_zero, mul_zero] }, { rw [inv_mul_cancel h, one_mul] } end /-- Multiplying `a` by itself and then dividing by itself results in `a`, whether or not `a` is zero. -/ @[simp] lemma mul_self_div_self (a : G₀) : a * a / a = a := by rw [div_eq_mul_inv, mul_self_mul_inv a] /-- Dividing `a` by itself and then multiplying by itself results in `a`, whether or not `a` is zero. -/ @[simp] lemma div_self_mul_self (a : G₀) : a / a * a = a := by rw [div_eq_mul_inv, mul_inv_mul_self a] lemma div_mul_cancel_of_imp {a b : G₀} (h : b = 0 → a = 0) : a / b * b = a := classical.by_cases (λ hb : b = 0, by simp [*]) (div_mul_cancel a) lemma mul_div_cancel_of_imp {a b : G₀} (h : b = 0 → a = 0) : a * b / b = a := classical.by_cases (λ hb : b = 0, by simp [*]) (mul_div_cancel a) local attribute [simp] div_eq_mul_inv mul_comm mul_assoc mul_left_comm @[simp] lemma div_self_mul_self' (a : G₀) : a / (a * a) = a⁻¹ := calc a / (a * a) = a⁻¹⁻¹ * a⁻¹ * a⁻¹ : by simp [mul_inv_rev] ... = a⁻¹ : inv_mul_mul_self _ lemma one_div_ne_zero {a : G₀} (h : a ≠ 0) : 1 / a ≠ 0 := by simpa only [one_div] using inv_ne_zero h @[simp] lemma inv_eq_zero {a : G₀} : a⁻¹ = 0 ↔ a = 0 := by rw [inv_eq_iff_inv_eq, inv_zero, eq_comm] @[simp] lemma zero_eq_inv {a : G₀} : 0 = a⁻¹ ↔ 0 = a := eq_comm.trans $ inv_eq_zero.trans eq_comm @[simp] theorem divp_mk0 (a : G₀) {b : G₀} (hb : b ≠ 0) : a /ₚ units.mk0 b hb = a / b := divp_eq_div _ _ lemma div_ne_zero (ha : a ≠ 0) (hb : b ≠ 0) : a / b ≠ 0 := by { rw div_eq_mul_inv, exact mul_ne_zero ha (inv_ne_zero hb) } @[simp] lemma div_eq_zero_iff : a / b = 0 ↔ a = 0 ∨ b = 0:= by simp [div_eq_mul_inv] lemma div_ne_zero_iff : a / b ≠ 0 ↔ a ≠ 0 ∧ b ≠ 0 := (not_congr div_eq_zero_iff).trans not_or_distrib lemma ring.inverse_eq_inv (a : G₀) : ring.inverse a = a⁻¹ := begin obtain rfl | ha := eq_or_ne a 0, { simp }, { exact ring.inverse_unit (units.mk0 a ha) } end @[simp] lemma ring.inverse_eq_inv' : (ring.inverse : G₀ → G₀) = has_inv.inv := funext ring.inverse_eq_inv /-- Dividing `a` by the result of dividing `a` by itself results in `a` (whether or not `a` is zero). -/ @[simp] lemma div_div_self (a : G₀) : a / (a / a) = a := begin rw div_div_eq_mul_div, exact mul_self_div_self a end lemma ne_zero_of_one_div_ne_zero {a : G₀} (h : 1 / a ≠ 0) : a ≠ 0 := assume ha : a = 0, begin rw [ha, div_zero] at h, contradiction end lemma eq_zero_of_one_div_eq_zero {a : G₀} (h : 1 / a = 0) : a = 0 := classical.by_cases (assume ha, ha) (assume ha, ((one_div_ne_zero ha) h).elim) end group_with_zero section comm_group_with_zero -- comm variables [comm_group_with_zero G₀] {a b c d : G₀} @[priority 10] -- see Note [lower instance priority] instance comm_group_with_zero.cancel_comm_monoid_with_zero : cancel_comm_monoid_with_zero G₀ := { ..group_with_zero.cancel_monoid_with_zero, ..comm_group_with_zero.to_comm_monoid_with_zero G₀ } @[priority 100] -- See note [lower instance priority] instance comm_group_with_zero.to_division_comm_monoid : division_comm_monoid G₀ := { ..‹comm_group_with_zero G₀›, ..group_with_zero.to_division_monoid } /-- Pullback a `comm_group_with_zero` class along an injective function. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.comm_group_with_zero [has_zero G₀'] [has_mul G₀'] [has_one G₀'] [has_inv G₀'] [has_div G₀'] [has_pow G₀' ℕ] [has_pow G₀' ℤ] (f : G₀' → G₀) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f x⁻¹ = (f x)⁻¹) (div : ∀ x y, f (x / y) = f x / f y) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) (zpow : ∀ x (n : ℤ), f (x ^ n) = f x ^ n) : comm_group_with_zero G₀' := { .. hf.group_with_zero f zero one mul inv div npow zpow, .. hf.comm_semigroup f mul } /-- Pushforward a `comm_group_with_zero` class along a surjective function. -/ protected def function.surjective.comm_group_with_zero [has_zero G₀'] [has_mul G₀'] [has_one G₀'] [has_inv G₀'] [has_div G₀'] [has_pow G₀' ℕ] [has_pow G₀' ℤ] (h01 : (0:G₀') ≠ 1) (f : G₀ → G₀') (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) (inv : ∀ x, f x⁻¹ = (f x)⁻¹) (div : ∀ x y, f (x / y) = f x / f y) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) (zpow : ∀ x (n : ℤ), f (x ^ n) = f x ^ n) : comm_group_with_zero G₀' := { .. hf.group_with_zero h01 f zero one mul inv div npow zpow, .. hf.comm_semigroup f mul } lemma div_mul_right (b : G₀) (ha : a ≠ 0) : a / (a * b) = 1 / b := ha.is_unit.div_mul_right _ lemma mul_div_cancel_left_of_imp {a b : G₀} (h : a = 0 → b = 0) : a * b / a = b := by rw [mul_comm, mul_div_cancel_of_imp h] lemma mul_div_cancel_left (b : G₀) (ha : a ≠ 0) : a * b / a = b := ha.is_unit.mul_div_cancel_left _ lemma mul_div_cancel_of_imp' {a b : G₀} (h : b = 0 → a = 0) : b * (a / b) = a := by rw [mul_comm, div_mul_cancel_of_imp h] lemma mul_div_cancel' (a : G₀) (hb : b ≠ 0) : b * (a / b) = a := hb.is_unit.mul_div_cancel' _ lemma mul_div_mul_left (a b : G₀) (hc : c ≠ 0) : (c * a) / (c * b) = a / b := hc.is_unit.mul_div_mul_left _ _ lemma mul_eq_mul_of_div_eq_div (a : G₀) {b : G₀} (c : G₀) {d : G₀} (hb : b ≠ 0) (hd : d ≠ 0) (h : a / b = c / d) : a * d = c * b := by rw [←mul_one a, ←div_self hb, ←mul_comm_div, h, div_mul_eq_mul_div, div_mul_cancel _ hd] @[field_simps] lemma div_eq_div_iff (hb : b ≠ 0) (hd : d ≠ 0) : a / b = c / d ↔ a * d = c * b := hb.is_unit.div_eq_div_iff hd.is_unit lemma div_div_cancel' (ha : a ≠ 0) : a / (a / b) = b := ha.is_unit.div_div_cancel lemma div_helper (b : G₀) (h : a ≠ 0) : 1 / (a * b) * a = 1 / b := by rw [div_mul_eq_mul_div, one_mul, div_mul_right _ h] end comm_group_with_zero namespace semiconj_by @[simp] lemma zero_right [mul_zero_class G₀] (a : G₀) : semiconj_by a 0 0 := by simp only [semiconj_by, mul_zero, zero_mul] @[simp] lemma zero_left [mul_zero_class G₀] (x y : G₀) : semiconj_by 0 x y := by simp only [semiconj_by, mul_zero, zero_mul] variables [group_with_zero G₀] {a x y x' y' : G₀} @[simp] lemma inv_symm_left_iff₀ : semiconj_by a⁻¹ x y ↔ semiconj_by a y x := classical.by_cases (λ ha : a = 0, by simp only [ha, inv_zero, semiconj_by.zero_left]) (λ ha, @units_inv_symm_left_iff _ _ (units.mk0 a ha) _ _) lemma inv_symm_left₀ (h : semiconj_by a x y) : semiconj_by a⁻¹ y x := semiconj_by.inv_symm_left_iff₀.2 h lemma inv_right₀ (h : semiconj_by a x y) : semiconj_by a x⁻¹ y⁻¹ := begin by_cases ha : a = 0, { simp only [ha, zero_left] }, by_cases hx : x = 0, { subst x, simp only [semiconj_by, mul_zero, @eq_comm _ _ (y * a), mul_eq_zero] at h, simp [h.resolve_right ha] }, { have := mul_ne_zero ha hx, rw [h.eq, mul_ne_zero_iff] at this, exact @units_inv_right _ _ _ (units.mk0 x hx) (units.mk0 y this.1) h }, end @[simp] lemma inv_right_iff₀ : semiconj_by a x⁻¹ y⁻¹ ↔ semiconj_by a x y := ⟨λ h, inv_inv x ▸ inv_inv y ▸ h.inv_right₀, inv_right₀⟩ lemma div_right (h : semiconj_by a x y) (h' : semiconj_by a x' y') : semiconj_by a (x / x') (y / y') := by { rw [div_eq_mul_inv, div_eq_mul_inv], exact h.mul_right h'.inv_right₀ } end semiconj_by namespace commute @[simp] theorem zero_right [mul_zero_class G₀] (a : G₀) :commute a 0 := semiconj_by.zero_right a @[simp] theorem zero_left [mul_zero_class G₀] (a : G₀) : commute 0 a := semiconj_by.zero_left a a variables [group_with_zero G₀] {a b c : G₀} @[simp] theorem inv_left_iff₀ : commute a⁻¹ b ↔ commute a b := semiconj_by.inv_symm_left_iff₀ theorem inv_left₀ (h : commute a b) : commute a⁻¹ b := inv_left_iff₀.2 h @[simp] theorem inv_right_iff₀ : commute a b⁻¹ ↔ commute a b := semiconj_by.inv_right_iff₀ theorem inv_right₀ (h : commute a b) : commute a b⁻¹ := inv_right_iff₀.2 h @[simp] theorem div_right (hab : commute a b) (hac : commute a c) : commute a (b / c) := hab.div_right hac @[simp] theorem div_left (hac : commute a c) (hbc : commute b c) : commute (a / b) c := by { rw div_eq_mul_inv, exact hac.mul_left hbc.inv_left₀ } end commute namespace monoid_with_zero_hom variables [group_with_zero G₀] [group_with_zero G₀'] [monoid_with_zero M₀] [nontrivial M₀] section monoid_with_zero variables (f : G₀ →*₀ M₀) {a : G₀} lemma map_ne_zero : f a ≠ 0 ↔ a ≠ 0 := ⟨λ hfa ha, hfa $ ha.symm ▸ f.map_zero, λ ha, ((is_unit.mk0 a ha).map f.to_monoid_hom).ne_zero⟩ @[simp] lemma map_eq_zero : f a = 0 ↔ a = 0 := not_iff_not.1 f.map_ne_zero end monoid_with_zero section group_with_zero variables (f : G₀ →*₀ G₀') (a b : G₀) /-- A monoid homomorphism between groups with zeros sending `0` to `0` sends `a⁻¹` to `(f a)⁻¹`. -/ @[simp] lemma map_inv : f a⁻¹ = (f a)⁻¹ := begin by_cases h : a = 0, by simp [h], apply eq_inv_of_mul_eq_one_left, rw [← f.map_mul, inv_mul_cancel h, f.map_one] end @[simp] lemma map_div : f (a / b) = f a / f b := by simpa only [div_eq_mul_inv] using ((f.map_mul _ _).trans $ _root_.congr_arg _ $ f.map_inv b) end group_with_zero end monoid_with_zero_hom /-- We define the inverse as a `monoid_with_zero_hom` by extending the inverse map by zero on non-units. -/ noncomputable def monoid_with_zero.inverse {M : Type*} [comm_monoid_with_zero M] : M →*₀ M := { to_fun := ring.inverse, map_zero' := ring.inverse_zero _, map_one' := ring.inverse_one _, map_mul' := λ x y, (ring.mul_inverse_rev x y).trans (mul_comm _ _) } @[simp] lemma monoid_with_zero.coe_inverse {M : Type*} [comm_monoid_with_zero M] : (monoid_with_zero.inverse : M → M) = ring.inverse := rfl @[simp] lemma monoid_with_zero.inverse_apply {M : Type*} [comm_monoid_with_zero M] (a : M) : monoid_with_zero.inverse a = ring.inverse a := rfl /-- Inversion on a commutative group with zero, considered as a monoid with zero homomorphism. -/ def inv_monoid_with_zero_hom {G₀ : Type*} [comm_group_with_zero G₀] : G₀ →*₀ G₀ := { map_zero' := inv_zero, ..inv_monoid_hom } @[simp] lemma monoid_hom.map_units_inv {M G₀ : Type*} [monoid M] [group_with_zero G₀] (f : M →* G₀) (u : Mˣ) : f ↑u⁻¹ = (f u)⁻¹ := by rw [← units.coe_map, ← units.coe_map, ← units.coe_inv, monoid_hom.map_inv] @[simp] lemma monoid_with_zero_hom.map_units_inv {M G₀ : Type*} [monoid_with_zero M] [group_with_zero G₀] (f : M →*₀ G₀) (u : Mˣ) : f ↑u⁻¹ = (f u)⁻¹ := f.to_monoid_hom.map_units_inv u section noncomputable_defs variables {M : Type*} [nontrivial M] /-- Constructs a `group_with_zero` structure on a `monoid_with_zero` consisting only of units and 0. -/ noncomputable def group_with_zero_of_is_unit_or_eq_zero [hM : monoid_with_zero M] (h : ∀ (a : M), is_unit a ∨ a = 0) : group_with_zero M := { inv := λ a, if h0 : a = 0 then 0 else ↑((h a).resolve_right h0).unit⁻¹, inv_zero := dif_pos rfl, mul_inv_cancel := λ a h0, by { change a * (if h0 : a = 0 then 0 else ↑((h a).resolve_right h0).unit⁻¹) = 1, rw [dif_neg h0, units.mul_inv_eq_iff_eq_mul, one_mul, is_unit.unit_spec] }, exists_pair_ne := nontrivial.exists_pair_ne, .. hM } /-- Constructs a `comm_group_with_zero` structure on a `comm_monoid_with_zero` consisting only of units and 0. -/ noncomputable def comm_group_with_zero_of_is_unit_or_eq_zero [hM : comm_monoid_with_zero M] (h : ∀ (a : M), is_unit a ∨ a = 0) : comm_group_with_zero M := { .. (group_with_zero_of_is_unit_or_eq_zero h), .. hM } end noncomputable_defs
c5e0e45baf9fc6ee7307d6837b4a2b0e8766c574
07c76fbd96ea1786cc6392fa834be62643cea420
/hott/algebra/category/constructions/comma.hlean
29342b46d5a9f1cdabe4bee86c2408e5872a5607
[ "Apache-2.0" ]
permissive
fpvandoorn/lean2
5a430a153b570bf70dc8526d06f18fc000a60ad9
0889cf65b7b3cebfb8831b8731d89c2453dd1e9f
refs/heads/master
1,592,036,508,364
1,545,093,958,000
1,545,093,958,000
75,436,854
0
0
null
1,480,718,780,000
1,480,718,780,000
null
UTF-8
Lean
false
false
6,745
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn Comma category -/ import ..functor.basic ..strict ..category open eq functor equiv sigma sigma.ops is_trunc iso is_equiv namespace category structure comma_object {A B C : Precategory} (S : A ⇒ C) (T : B ⇒ C) := (a : A) (b : B) (f : S a ⟶ T b) abbreviation ob1 [unfold 6] := @comma_object.a abbreviation ob2 [unfold 6] := @comma_object.b abbreviation mor [unfold 6] := @comma_object.f variables {A B C : Precategory} (S : A ⇒ C) (T : B ⇒ C) definition comma_object_sigma_char : (Σ(a : A) (b : B), S a ⟶ T b) ≃ comma_object S T := begin fapply equiv.MK, { intro u, exact comma_object.mk u.1 u.2.1 u.2.2}, { intro x, cases x with a b f, exact ⟨a, b, f⟩}, { intro x, cases x, reflexivity}, { intro u, cases u with u1 u2, cases u2, reflexivity}, end theorem is_trunc_comma_object (n : trunc_index) [HA : is_trunc n A] [HB : is_trunc n B] [H : Π(s d : C), is_trunc n (hom s d)] : is_trunc n (comma_object S T) := is_trunc_equiv_closed n !comma_object_sigma_char _ variables {S T} definition comma_object_eq' {x y : comma_object S T} (p : ob1 x = ob1 y) (q : ob2 x = ob2 y) (r : mor x =[ap011 (@hom C C) (ap (to_fun_ob S) p) (ap (to_fun_ob T) q)] mor y) : x = y := begin cases x with a b f, cases y with a' b' f', cases p, cases q, esimp [ap011,congr,ap,subst] at r, eapply (idp_rec_on r), reflexivity end --TODO: remove. This is a different version where Hq is not in square brackets -- definition eq_comp_inverse_of_comp_eq' {ob : Type} {C : precategory ob} {d c b : ob} {r : hom c d} -- {q : hom b c} {x : hom b d} {Hq : is_iso q} (p : r ∘ q = x) : r = x ∘ q⁻¹ʰ := -- sorry -- := sorry --eq_inverse_comp_of_comp_eq p definition comma_object_eq {x y : comma_object S T} (p : ob1 x = ob1 y) (q : ob2 x = ob2 y) (r : T (hom_of_eq q) ∘ mor x ∘ S (inv_of_eq p) = mor y) : x = y := begin cases x with a b f, cases y with a' b' f', cases p, cases q, apply ap (comma_object.mk a' b'), rewrite [▸* at r, -r, +respect_id, id_leftright] end definition ap_ob1_comma_object_eq' (x y : comma_object S T) (p : ob1 x = ob1 y) (q : ob2 x = ob2 y) (r : mor x =[ap011 (@hom C C) (ap (to_fun_ob S) p) (ap (to_fun_ob T) q)] mor y) : ap ob1 (comma_object_eq' p q r) = p := begin cases x with a b f, cases y with a' b' f', cases p, cases q, eapply (idp_rec_on r), reflexivity end definition ap_ob2_comma_object_eq' (x y : comma_object S T) (p : ob1 x = ob1 y) (q : ob2 x = ob2 y) (r : mor x =[ap011 (@hom C C) (ap (to_fun_ob S) p) (ap (to_fun_ob T) q)] mor y) : ap ob2 (comma_object_eq' p q r) = q := begin cases x with a b f, cases y with a' b' f', cases p, cases q, eapply (idp_rec_on r), reflexivity end structure comma_morphism (x y : comma_object S T) := mk' :: (g : ob1 x ⟶ ob1 y) (h : ob2 x ⟶ ob2 y) (p : T h ∘ mor x = mor y ∘ S g) (p' : mor y ∘ S g = T h ∘ mor x) abbreviation mor1 := @comma_morphism.g abbreviation mor2 := @comma_morphism.h abbreviation coh := @comma_morphism.p abbreviation coh' := @comma_morphism.p' protected definition comma_morphism.mk [constructor] [reducible] {x y : comma_object S T} (g h p) : comma_morphism x y := comma_morphism.mk' g h p p⁻¹ variables (x y z w : comma_object S T) definition comma_morphism_sigma_char : (Σ(g : ob1 x ⟶ ob1 y) (h : ob2 x ⟶ ob2 y), T h ∘ mor x = mor y ∘ S g) ≃ comma_morphism x y := begin fapply equiv.MK, { intro u, exact (comma_morphism.mk u.1 u.2.1 u.2.2)}, { intro f, cases f with g h p p', exact ⟨g, h, p⟩}, { intro f, cases f with g h p p', esimp, apply ap (comma_morphism.mk' g h p), apply is_prop.elim}, { intro u, cases u with u1 u2, cases u2 with u2 u3, reflexivity}, end theorem is_trunc_comma_morphism (n : trunc_index) [H1 : is_trunc n (ob1 x ⟶ ob1 y)] [H2 : is_trunc n (ob2 x ⟶ ob2 y)] [Hp : Πm1 m2, is_trunc n (T m2 ∘ mor x = mor y ∘ S m1)] : is_trunc n (comma_morphism x y) := is_trunc_equiv_closed n !comma_morphism_sigma_char _ variables {x y z w} definition comma_morphism_eq {f f' : comma_morphism x y} (p : mor1 f = mor1 f') (q : mor2 f = mor2 f') : f = f' := begin cases f with g h p₁ p₁', cases f' with g' h' p₂ p₂', cases p, cases q, apply ap011 (comma_morphism.mk' g' h'), apply is_prop.elim, apply is_prop.elim end definition comma_compose (g : comma_morphism y z) (f : comma_morphism x y) : comma_morphism x z := comma_morphism.mk (mor1 g ∘ mor1 f) (mor2 g ∘ mor2 f) (by rewrite [+respect_comp,-assoc,coh,assoc,coh,-assoc]) local infix ` ∘∘ `:60 := comma_compose definition comma_id : comma_morphism x x := comma_morphism.mk id id (by rewrite [+respect_id,id_left,id_right]) theorem comma_assoc (h : comma_morphism z w) (g : comma_morphism y z) (f : comma_morphism x y) : h ∘∘ (g ∘∘ f) = (h ∘∘ g) ∘∘ f := comma_morphism_eq !assoc !assoc theorem comma_id_left (f : comma_morphism x y) : comma_id ∘∘ f = f := comma_morphism_eq !id_left !id_left theorem comma_id_right (f : comma_morphism x y) : f ∘∘ comma_id = f := comma_morphism_eq !id_right !id_right variables (S T) definition comma_category [constructor] : Precategory := precategory.MK (comma_object S T) comma_morphism (λa b, !is_trunc_comma_morphism) (@comma_compose _ _ _ _ _) (@comma_id _ _ _ _ _) (@comma_assoc _ _ _ _ _) (@comma_id_left _ _ _ _ _) (@comma_id_right _ _ _ _ _) --TODO: this definition doesn't use category structure of A and B definition strict_precategory_comma [HA : strict_precategory A] [HB : strict_precategory B] : strict_precategory (comma_object S T) := strict_precategory.mk (comma_category S T) !is_trunc_comma_object /- --set_option pp.notation false definition is_univalent_comma (HA : is_univalent A) (HB : is_univalent B) : is_univalent (comma_category S T) := begin intros c d, fapply adjointify, { intro i, cases i with f s, cases s with g l r, cases f with fA fB fp, cases g with gA gB gp, esimp at *, fapply comma_object_eq, {apply iso_of_eq⁻¹ᶠ, exact (iso.MK fA gA (ap mor1 l) (ap mor1 r))}, {apply iso_of_eq⁻¹ᶠ, exact (iso.MK fB gB (ap mor2 l) (ap mor2 r))}, { apply sorry /-rewrite hom_of_eq_eq_of_iso,-/ }}, { apply sorry}, { apply sorry}, end -/ end category
c4fc15e3fa2c6f0349948956501327af2d672b1e
fef48cac17c73db8662678da38fd75888db97560
/src/for_mathlib/exactly_divides.lean
da7959feefcfc63a10ebf9b4fb23d6acf319f8a9
[]
no_license
kbuzzard/lean-squares-in-fibonacci
6c0d924f799d6751e19798bb2530ee602ec7087e
8cea20e5ce88ab7d17b020932d84d316532a84a8
refs/heads/master
1,584,524,504,815
1,582,387,156,000
1,582,387,156,000
134,576,655
3
1
null
1,541,538,497,000
1,527,083,406,000
Lean
UTF-8
Lean
false
false
4,496
lean
import data.nat.prime import algebra.order_functions open nat -- local attribute [trans] dvd_trans -- Definition def exactly_divides (p : ℕ) (r : ℕ) (n : ℕ) := p^r ∣ n ∧ ¬(p^(succ r) ∣ n) notation p`^`r `∣∣`:50 n:50 := exactly_divides p r n section variable {p : ℕ} -- Equivalent definitions lemma exactly_divides' {r : ℕ} {n : ℕ} : p^r ∣∣ n ↔ (∀ i, p^i ∣ n ↔ i ≤ r) := begin apply iff.intro, { intros prn i, apply iff.intro, { intro pin, apply (le_or_gt _ _).resolve_right, intro i_gt_r, exact absurd (dvd_trans (nat.pow_dvd_pow p i_gt_r) pin) prn.right }, { intro i_le_r, exact dvd.trans (pow_dvd_pow p i_le_r) (and.left prn) } }, { intro h, split, { exact (h r).mpr (refl r) }, { exact mt (h (succ r)).mp (not_succ_le_self r) } } end lemma exactly_divides'' {r : ℕ} {n : ℕ} (p_pos : p > 0) : p^r ∣∣ n ↔ ∃ k, n = p^r * k ∧ ¬(p ∣ k) := begin apply iff.intro, { intro prn, existsi n / (p^r), split, { exact (nat.mul_div_cancel' prn.left).symm }, { intro d, have := calc p^(succ r) = p^r * p : rfl ... ∣ p^r * (n / (p^r)) : mul_dvd_mul_left (p^r) d ... = n : nat.mul_div_cancel' prn.left, have := prn.right, contradiction } }, { intro h, rcases h with ⟨k, h1, h2⟩, split, { exact ⟨k,h1⟩ }, { intro d, rw h1 at d, have : p ∣ k := dvd_of_mul_dvd_mul_left (pos_pow_of_pos r p_pos) d, contradiction } } end -- Uniqueness lemma exactly_divides_unique {r s : ℕ} {n : ℕ} : p^r ∣∣ n → p^s ∣∣ n → r = s := λ prn psn, have ri : ∀ i, p^i ∣ n ↔ i ≤ r := exactly_divides'.mp prn, have si : ∀ i, p^i ∣ n ↔ i ≤ s := exactly_divides'.mp psn, have this : ∀ i, i ≤ r ↔ i ≤ s := λ i, (ri i).symm.trans (si i), le_antisymm ((this r).mp (le_refl r)) ((this s).mpr (le_refl s)) -- Recursion relations lemma exactly_divides_zero {n : ℕ} (p_pos : p > 0) : ¬(p ∣ n) ↔ p^0 ∣∣ n := begin rw exactly_divides'' p_pos, apply iff.intro, { intro h, exact ⟨n, by simp, h⟩ }, { intro h, rcases h with ⟨k, e, nd⟩, simp [e, nd] } end lemma exactly_divides_succ {r n : ℕ} (p_pos : p > 0) : p^r ∣∣ n ↔ p^(succ r) ∣∣ (p * n) := begin rw [exactly_divides'' p_pos, exactly_divides'' p_pos], apply iff.intro, { intro prn, rcases prn with ⟨k, h1, h2⟩, have h1' : p * n = p^(succ r) * k := calc p * n = p * (p^r * k) : by rw h1 ... = (p^r * p) * k : by ac_refl, exact ⟨k, h1', h2⟩, }, { intro psrpn, rcases psrpn with ⟨k, h1, h2⟩, have h1' : p * n = p * (p^r * k) := calc p * n = p^(succ r) * k : h1 ... = p * (p^r * k) : by unfold pow nat.pow; ac_refl, exact ⟨k, eq_of_mul_eq_mul_left p_pos h1', h2⟩ } end -- Multiplicative lemma exactly_divides_one (hp : prime p) : p^0 ∣∣ 1 := (exactly_divides_zero (lt.trans dec_trivial hp.one_lt)).mp (prime.not_dvd_one hp) lemma exactly_divides_mul {r s a b : ℕ} (hp : prime p) : p^r ∣∣ a → p^s ∣∣ b → p^(r+s) ∣∣ a*b := begin intros pra psb, rw exactly_divides'' (lt.trans dec_trivial hp.one_lt) at ⊢ pra psb, rcases pra with ⟨ka, ha1, ha2⟩, rcases psb with ⟨kb, hb1, hb2⟩, existsi ka * kb, split, { exact calc a * b = (p^r * ka) * (p^s * kb) : by rw [ha1, hb1] ... = (p^r * p^s) * (ka * kb) : by ac_refl ... = p^(r+s) * (ka * kb) : by rw nat.pow_add }, { exact prime.not_dvd_mul hp ha2 hb2 } end lemma exactly_divides_pow {r k a : ℕ} (hp : prime p) : p^r ∣∣ a → p^(k * r) ∣∣ a^k := mul_comm r k ▸ λ pra, nat.rec_on k (exactly_divides_one hp) (λ k' ih, (exactly_divides_mul hp ih pra)) -- Gcd lemma dvd_gcd_iff {a b k : ℕ} : k ∣ gcd a b ↔ k ∣ a ∧ k ∣ b := iff.intro (λ d, ⟨dvd_trans d (gcd_dvd a b).left, dvd_trans d (gcd_dvd a b).right⟩) (λ p, and.elim p dvd_gcd) lemma exactly_divides_gcd {r s a b : ℕ} : p^r ∣∣ a → p^s ∣∣ b → p^(min r s) ∣∣ gcd a b := begin intros pra psb, rw exactly_divides' at ⊢ pra psb, intro i, exact calc p^i ∣ gcd a b ↔ p^i ∣ a ∧ p^i ∣ b : nat.dvd_gcd_iff ... ↔ i ≤ r ∧ i ≤ s : and_congr (pra i) (psb i) ... ↔ i ≤ min r s : le_min_iff.symm end end
0bdec7a3cb5151b285f5d3563746baf9a028250a
3dd1b66af77106badae6edb1c4dea91a146ead30
/tests/lean/t11.lean
88f7bb7311459f35c52178083aca660dbecf162b
[ "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
380
lean
variable A : Type.{1} definition bool : Type.{1} := Type.{0} variable Exists (P : A → bool) : bool notation `exists` binders `,` b:(scoped b, Exists b) := b notation `∃` binders `,` b:(scoped b, Exists b) := b variable p : A → bool variable q : A → A → bool check exists x : A, p x check ∃ x y : A, q x y notation `{` binder `|` b:scoped `}` := b check {x : A | x}
13c401167a67a34ab9361f1b69c26eaa3d94e889
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/stage0/src/Lean/Elab/Open.lean
9d0576079cbcc8fc256313d0a23860b774671d64
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
4,302
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Log import Lean.Elab.Util namespace Lean.Elab namespace OpenDecl variable [Monad m] [STWorld IO.RealWorld m] [MonadEnv m] variable [MonadExceptOf Exception m] [MonadRef m] [AddErrorMessageContext m] variable [AddMessageContext m] [MonadLiftT (ST IO.RealWorld) m] [MonadLog m] structure State where openDecls : List OpenDecl currNamespace : Name abbrev M := StateRefT State m instance : MonadResolveName (M (m := m)) where getCurrNamespace := return (← get).currNamespace getOpenDecls := return (← get).openDecls def resolveId (ns : Name) (idStx : Syntax) : M (m := m) Name := do let declName := ns ++ idStx.getId if (← getEnv).contains declName then return declName else withRef idStx <| resolveGlobalConstNoOverloadCore declName private def addOpenDecl (decl : OpenDecl) : M (m:=m) Unit := modify fun s => { s with openDecls := decl :: s.openDecls } private def elabOpenSimple (n : Syntax) : M (m:=m) Unit := -- `open` id+ for ns in n[0].getArgs do for ns in (← resolveNamespace ns.getId) do addOpenDecl (OpenDecl.simple ns []) activateScoped ns private def elabOpenScoped (n : Syntax) : M (m:=m) Unit := -- `open` `scoped` id+ for ns in n[1].getArgs do for ns in (← resolveNamespace ns.getId) do activateScoped ns private def resolveNameUsingNamespacesCore (nss : List Name) (idStx : Syntax) : M (m:=m) Name := do let mut exs := #[] let mut result := #[] for ns in nss do try let declName ← resolveId ns idStx result := result.push declName catch ex => exs := exs.push ex if exs.size == nss.length then withRef idStx do if exs.size == 1 then throw exs[0]! else throwErrorWithNestedErrors "failed to open" exs if result.size == 1 then return result[0]! else withRef idStx do throwError "ambiguous identifier '{idStx.getId}', possible interpretations: {result.map mkConst}" -- `open` id `(` id+ `)` private def elabOpenOnly (n : Syntax) : M (m:=m) Unit := do let nss ← resolveNamespace n[0].getId for idStx in n[2].getArgs do let declName ← resolveNameUsingNamespacesCore nss idStx addOpenDecl (OpenDecl.explicit idStx.getId declName) -- `open` id `hiding` id+ private def elabOpenHiding (n : Syntax) : M (m:=m) Unit := do let ns ← resolveUniqueNamespace n[0].getId let mut ids : List Name := [] for idStx in n[2].getArgs do let _ ← resolveId ns idStx let id := idStx.getId ids := id::ids addOpenDecl (OpenDecl.simple ns ids) -- `open` id `renaming` sepBy (id `->` id) `,` private def elabOpenRenaming (n : Syntax) : M (m:=m) Unit := do let ns ← resolveUniqueNamespace n[0].getId for stx in n[2].getSepArgs do let fromStx := stx[0] let toId := stx[2].getId let declName ← resolveId ns fromStx addOpenDecl (OpenDecl.explicit toId declName) def elabOpenDecl [MonadResolveName m] (openDeclStx : Syntax) : m (List OpenDecl) := do StateRefT'.run' (s := { openDecls := (← getOpenDecls), currNamespace := (← getCurrNamespace) }) do if openDeclStx.getKind == ``Parser.Command.openSimple then elabOpenSimple openDeclStx else if openDeclStx.getKind == ``Parser.Command.openScoped then elabOpenScoped openDeclStx else if openDeclStx.getKind == ``Parser.Command.openOnly then elabOpenOnly openDeclStx else if openDeclStx.getKind == ``Parser.Command.openHiding then elabOpenHiding openDeclStx else elabOpenRenaming openDeclStx return (← get).openDecls def resolveOpenDeclId [MonadResolveName m] (ns : Name) (idStx : Syntax) : m Name := do StateRefT'.run' (s := { openDecls := (← getOpenDecls), currNamespace := (← getCurrNamespace) }) do OpenDecl.resolveId ns idStx def resolveNameUsingNamespaces [MonadResolveName m] (nss : List Name) (idStx : Syntax) : m Name := do StateRefT'.run' (s := { openDecls := (← getOpenDecls), currNamespace := (← getCurrNamespace) }) do resolveNameUsingNamespacesCore nss idStx end OpenDecl export OpenDecl (elabOpenDecl resolveOpenDeclId resolveNameUsingNamespaces) end Lean.Elab
d446380885cf0acf50195c54ed23c3be0324a0e9
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/500_lean3.lean
67a3de01d18adf74f7fa7fe95c57df838f8a5cc4
[ "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
125
lean
example (foo bar : Option Nat) : False := by have : do { let x ← bar; foo } = bar >>= fun x => foo := rfl admit done
8009447c904ba5c91d34aa965af34b0edbe375cd
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/hott/algebra/category/strict.hlean
a39a413cad23e5c69607a80c35790411d07fa7c2
[ "Apache-2.0" ]
permissive
jroesch/lean
30ef0860fa905d35b9ad6f76de1a4f65c9af6871
3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2
refs/heads/master
1,586,090,835,348
1,455,142,203,000
1,455,142,277,000
51,536,958
1
0
null
1,455,215,811,000
1,455,215,811,000
null
UTF-8
Lean
false
false
1,502
hlean
/- Copyright (c) 2015 Jakob von Raumer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Jakob von Raumer -/ import .functor.basic open is_trunc eq namespace category structure strict_precategory [class] (ob : Type) extends precategory ob := mk' :: (is_hset_ob : is_hset ob) attribute strict_precategory.is_hset_ob [instance] definition strict_precategory.mk [reducible] {ob : Type} (C : precategory ob) (H : is_hset ob) : strict_precategory ob := precategory.rec_on C strict_precategory.mk' H structure Strict_precategory : Type := (carrier : Type) (struct : strict_precategory carrier) attribute Strict_precategory.struct [instance] [coercion] definition Strict_precategory.to_Precategory [coercion] [reducible] (C : Strict_precategory) : Precategory := Precategory.mk (Strict_precategory.carrier C) _ open functor -- TODO: move to constructions.cat? definition precategory_strict_precategory [constructor] : precategory Strict_precategory := precategory.mk (λ A B, A ⇒ B) (λ A B C G F, G ∘f F) (λ A, 1) (λ A B C D, functor.assoc) (λ A B, functor.id_left) (λ A B, functor.id_right) definition Precategory_strict_precategory [constructor] := precategory.Mk precategory_strict_precategory namespace ops abbreviation Cat := Precategory_strict_precategory end ops end category
f3a1417e1ce862efee223b6323ce1812e091eb77
01ae0d022f2e2fefdaaa898938c1ac1fbce3b3ab
/categories/products/associator.lean
7ab05e6e099993f522842436c4e34430b99454ac
[]
no_license
PatrickMassot/lean-category-theory
0f56a83464396a253c28a42dece16c93baf8ad74
ef239978e91f2e1c3b8e88b6e9c64c155dc56c99
refs/heads/master
1,629,739,187,316
1,512,422,659,000
1,512,422,659,000
113,098,786
0
0
null
1,512,424,022,000
1,512,424,022,000
null
UTF-8
Lean
false
false
1,057
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Stephen Morgan, Scott Morrison import ..products open categories open categories.functor open categories.natural_transformation namespace categories.products -- PROJECT; by aggressively allowing "assumption" we could do this completely automatically -- locally tag "assumption" with @[tidy]? -- or define an aggressive version of tidy (perhaps "follow_your_nose"?) definition ProductCategoryAssociator ( C D E: Category ) : Functor ((C × D) × E) (C × (D × E)) := { onObjects := λ X, (X.1.1, (X.1.2, X.2)), onMorphisms := λ _ _ f, (f.1.1, (f.1.2, f.2)), identities := ♮, functoriality := ♮ } definition ProductCategoryInverseAssociator ( C D E: Category ) : Functor (C × (D × E)) ((C × D) × E) := { onObjects := λ X, ((X.1, X.2.1), X.2.2), onMorphisms := λ _ _ f, ((f.1, f.2.1), f.2.2), identities := ♮, functoriality := ♮ } end categories.products
9c5daf5f803a94628599288135020f00fb6cdd17
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/algebra/quadratic_discriminant.lean
9ff7b107a4acc86857d5c72f91cc21ee370beb38
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
5,760
lean
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou -/ import algebra.char_p.invertible import order.filter.at_top_bot import tactic.linarith /-! # Quadratic discriminants and roots of a quadratic This file defines the discriminant of a quadratic and gives the solution to a quadratic equation. ## Main definition - `discrim a b c`: the discriminant of a quadratic `a * x * x + b * x + c` is `b * b - 4 * a * c`. ## Main statements - `quadratic_eq_zero_iff`: roots of a quadratic can be written as `(-b + s) / (2 * a)` or `(-b - s) / (2 * a)`, where `s` is a square root of the discriminant. - `quadratic_ne_zero_of_discrim_ne_sq`: if the discriminant has no square root, then the corresponding quadratic has no root. - `discrim_le_zero`: if a quadratic is always non-negative, then its discriminant is non-positive. ## Tags polynomial, quadratic, discriminant, root -/ open filter section ring variables {R : Type*} /-- Discriminant of a quadratic -/ def discrim [ring R] (a b c : R) : R := b^2 - 4 * a * c variables [comm_ring R] [is_domain R] {a b c : R} /-- A quadratic has roots if and only if its discriminant equals some square. -/ lemma quadratic_eq_zero_iff_discrim_eq_sq (h2 : (2 : R) ≠ 0) (ha : a ≠ 0) (x : R) : a * x * x + b * x + c = 0 ↔ discrim a b c = (2 * a * x + b) ^ 2 := begin split, { assume h, calc discrim a b c = 4 * a * (a * x * x + b * x + c) + b * b - 4 * a * c : by { rw [h, discrim], ring } ... = (2*a*x + b)^2 : by ring }, { assume h, have ha : 2 * 2 * a ≠ 0 := mul_ne_zero (mul_ne_zero h2 h2) ha, apply mul_left_cancel₀ ha, calc 2 * 2 * a * (a * x * x + b * x + c) = (2 * a * x + b) ^ 2 - (b ^ 2 - 4 * a * c) : by ring ... = 0 : by { rw [← h, discrim], ring } ... = 2*2*a*0 : by ring } end /-- A quadratic has no root if its discriminant has no square root. -/ lemma quadratic_ne_zero_of_discrim_ne_sq (h2 : (2 : R) ≠ 0) (ha : a ≠ 0) (h : ∀ s : R, discrim a b c ≠ s * s) (x : R) : a * x * x + b * x + c ≠ 0 := begin assume h', rw [quadratic_eq_zero_iff_discrim_eq_sq h2 ha, sq] at h', exact h _ h' end end ring section field variables {K : Type*} [field K] [invertible (2 : K)] {a b c x : K} /-- Roots of a quadratic -/ lemma quadratic_eq_zero_iff (ha : a ≠ 0) {s : K} (h : discrim a b c = s * s) (x : K) : a * x * x + b * x + c = 0 ↔ x = (-b + s) / (2 * a) ∨ x = (-b - s) / (2 * a) := begin have h2 : (2 : K) ≠ 0 := nonzero_of_invertible 2, rw [quadratic_eq_zero_iff_discrim_eq_sq h2 ha, h, sq, mul_self_eq_mul_self_iff], have ne : 2 * a ≠ 0 := mul_ne_zero h2 ha, have : x = 2 * a * x / (2 * a) := (mul_div_cancel_left x ne).symm, have h₁ : 2 * a * ((-b + s) / (2 * a)) = -b + s := mul_div_cancel' _ ne, have h₂ : 2 * a * ((-b - s) / (2 * a)) = -b - s := mul_div_cancel' _ ne, split, { intro h', rcases h', { left, rw h', simpa [add_comm] }, { right, rw h', simpa [add_comm, sub_eq_add_neg] } }, { intro h', rcases h', { left, rw [h', h₁], ring }, { right, rw [h', h₂], ring } } end /-- A quadratic has roots if its discriminant has square roots -/ lemma exists_quadratic_eq_zero (ha : a ≠ 0) (h : ∃ s, discrim a b c = s * s) : ∃ x, a * x * x + b * x + c = 0 := begin rcases h with ⟨s, hs⟩, use (-b + s) / (2 * a), rw quadratic_eq_zero_iff ha hs, simp end /-- Root of a quadratic when its discriminant equals zero -/ lemma quadratic_eq_zero_iff_of_discrim_eq_zero (ha : a ≠ 0) (h : discrim a b c = 0) (x : K) : a * x * x + b * x + c = 0 ↔ x = -b / (2 * a) := begin have : discrim a b c = 0 * 0, by rw [h, mul_zero], rw [quadratic_eq_zero_iff ha this, add_zero, sub_zero, or_self] end end field section linear_ordered_field variables {K : Type*} [linear_ordered_field K] {a b c : K} /-- If a polynomial of degree 2 is always nonnegative, then its discriminant is nonpositive -/ lemma discrim_le_zero (h : ∀ x : K, 0 ≤ a * x * x + b * x + c) : discrim a b c ≤ 0 := begin rw [discrim, sq], obtain ha|rfl|ha : a < 0 ∨ a = 0 ∨ 0 < a := lt_trichotomy a 0, -- if a < 0 { have : tendsto (λ x, (a * x + b) * x + c) at_top at_bot := tendsto_at_bot_add_const_right _ c ((tendsto_at_bot_add_const_right _ b (tendsto_id.neg_const_mul_at_top ha)).at_bot_mul_at_top tendsto_id), rcases (this.eventually (eventually_lt_at_bot 0)).exists with ⟨x, hx⟩, exact false.elim ((h x).not_lt $ by rwa ← add_mul) }, -- if a = 0 { rcases em (b = 0) with (rfl|hb), { simp }, { have := h ((-c - 1) / b), rw [mul_div_cancel' _ hb] at this, linarith } }, -- if a > 0 { have := calc 4 * a * (a * (-(b / a) * (1 / 2)) * (-(b / a) * (1 / 2)) + b * (-(b / a) * (1 / 2)) + c) = (a * (b / a)) * (a * (b / a)) - 2 * (a * (b / a)) * b + 4 * a * c : by ring ... = -(b * b - 4 * a * c) : by { simp only [mul_div_cancel' b (ne_of_gt ha)], ring }, have ha' : 0 ≤ 4 * a, by linarith, have h := (mul_nonneg ha' (h (-(b / a) * (1 / 2)))), rw this at h, rwa ← neg_nonneg } end /-- If a polynomial of degree 2 is always positive, then its discriminant is negative, at least when the coefficient of the quadratic term is nonzero. -/ lemma discrim_lt_zero (ha : a ≠ 0) (h : ∀ x : K, 0 < a * x * x + b * x + c) : discrim a b c < 0 := begin have : ∀ x : K, 0 ≤ a*x*x + b*x + c := assume x, le_of_lt (h x), refine lt_of_le_of_ne (discrim_le_zero this) _, assume h', have := h (-b / (2 * a)), have : a * (-b / (2 * a)) * (-b / (2 * a)) + b * (-b / (2 * a)) + c = 0, { rw [quadratic_eq_zero_iff_of_discrim_eq_zero ha h' (-b / (2 * a))] }, linarith end end linear_ordered_field
7958909606a11b33166a3113f8d3c99147b4ce76
37da0369b6c03e380e057bf680d81e6c9fdf9219
/hott/hit/pushout.hlean
070fd7c46996704cb3f83d890c3e48fe450a0a07
[ "Apache-2.0" ]
permissive
kodyvajjha/lean2
72b120d95c3a1d77f54433fa90c9810e14a931a4
227fcad22ab2bc27bb7471be7911075d101ba3f9
refs/heads/master
1,627,157,512,295
1,501,855,676,000
1,504,809,427,000
109,317,326
0
0
null
1,509,839,253,000
1,509,655,713,000
C++
UTF-8
Lean
false
false
14,463
hlean
/- Copyright (c) 2015-16 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Ulrik Buchholtz, Jakob von Raumer Declaration and properties of the pushout -/ import .quotient types.sigma types.arrow_2 open quotient eq sum equiv is_trunc pointed namespace pushout section parameters {TL BL TR : Type} (f : TL → BL) (g : TL → TR) local abbreviation A := BL + TR inductive pushout_rel : A → A → Type := | Rmk : Π(x : TL), pushout_rel (inl (f x)) (inr (g x)) open pushout_rel local abbreviation R := pushout_rel definition pushout : Type := quotient R -- TODO: define this in root namespace parameters {f g} definition inl (x : BL) : pushout := class_of R (inl x) definition inr (x : TR) : pushout := class_of R (inr x) definition glue (x : TL) : inl (f x) = inr (g x) := eq_of_rel pushout_rel (Rmk f g x) protected definition rec {P : pushout → Type} (Pinl : Π(x : BL), P (inl x)) (Pinr : Π(x : TR), P (inr x)) (Pglue : Π(x : TL), Pinl (f x) =[glue x] Pinr (g x)) (y : pushout) : P y := begin induction y, { cases a, apply Pinl, apply Pinr}, { cases H, apply Pglue} end protected definition rec_on [reducible] {P : pushout → Type} (y : pushout) (Pinl : Π(x : BL), P (inl x)) (Pinr : Π(x : TR), P (inr x)) (Pglue : Π(x : TL), Pinl (f x) =[glue x] Pinr (g x)) : P y := rec Pinl Pinr Pglue y theorem rec_glue {P : pushout → Type} (Pinl : Π(x : BL), P (inl x)) (Pinr : Π(x : TR), P (inr x)) (Pglue : Π(x : TL), Pinl (f x) =[glue x] Pinr (g x)) (x : TL) : apd (rec Pinl Pinr Pglue) (glue x) = Pglue x := !rec_eq_of_rel protected definition elim {P : Type} (Pinl : BL → P) (Pinr : TR → P) (Pglue : Π(x : TL), Pinl (f x) = Pinr (g x)) (y : pushout) : P := rec Pinl Pinr (λx, pathover_of_eq _ (Pglue x)) y protected definition elim_on [reducible] {P : Type} (y : pushout) (Pinl : BL → P) (Pinr : TR → P) (Pglue : Π(x : TL), Pinl (f x) = Pinr (g x)) : P := elim Pinl Pinr Pglue y theorem elim_glue {P : Type} (Pinl : BL → P) (Pinr : TR → P) (Pglue : Π(x : TL), Pinl (f x) = Pinr (g x)) (x : TL) : ap (elim Pinl Pinr Pglue) (glue x) = Pglue x := begin apply eq_of_fn_eq_fn_inv !(pathover_constant (glue x)), rewrite [▸*,-apd_eq_pathover_of_eq_ap,↑pushout.elim,rec_glue], end protected definition elim_type (Pinl : BL → Type) (Pinr : TR → Type) (Pglue : Π(x : TL), Pinl (f x) ≃ Pinr (g x)) : pushout → Type := quotient.elim_type (sum.rec Pinl Pinr) begin intro v v' r, induction r, apply Pglue end protected definition elim_type_on [reducible] (y : pushout) (Pinl : BL → Type) (Pinr : TR → Type) (Pglue : Π(x : TL), Pinl (f x) ≃ Pinr (g x)) : Type := elim_type Pinl Pinr Pglue y theorem elim_type_glue (Pinl : BL → Type) (Pinr : TR → Type) (Pglue : Π(x : TL), Pinl (f x) ≃ Pinr (g x)) (x : TL) : transport (elim_type Pinl Pinr Pglue) (glue x) = Pglue x := !elim_type_eq_of_rel_fn theorem elim_type_glue_inv (Pinl : BL → Type) (Pinr : TR → Type) (Pglue : Π(x : TL), Pinl (f x) ≃ Pinr (g x)) (x : TL) : transport (elim_type Pinl Pinr Pglue) (glue x)⁻¹ = to_inv (Pglue x) := !elim_type_eq_of_rel_inv protected definition rec_prop {P : pushout → Type} [H : Πx, is_prop (P x)] (Pinl : Π(x : BL), P (inl x)) (Pinr : Π(x : TR), P (inr x)) (y : pushout) := rec Pinl Pinr (λx, !is_prop.elimo) y protected definition elim_prop {P : Type} [H : is_prop P] (Pinl : BL → P) (Pinr : TR → P) (y : pushout) : P := elim Pinl Pinr (λa, !is_prop.elim) y end end pushout attribute pushout.inl pushout.inr [constructor] attribute pushout.rec pushout.elim [unfold 10] [recursor 10] attribute pushout.elim_type [unfold 9] attribute pushout.rec_on pushout.elim_on [unfold 7] attribute pushout.elim_type_on [unfold 6] open sigma namespace pushout variables {TL BL TR : Type} (f : TL → BL) (g : TL → TR) protected theorem elim_inl {P : Type} (Pinl : BL → P) (Pinr : TR → P) (Pglue : Π(x : TL), Pinl (f x) = Pinr (g x)) {b b' : BL} (p : b = b') : ap (pushout.elim Pinl Pinr Pglue) (ap inl p) = ap Pinl p := !ap_compose⁻¹ protected theorem elim_inr {P : Type} (Pinl : BL → P) (Pinr : TR → P) (Pglue : Π(x : TL), Pinl (f x) = Pinr (g x)) {b b' : TR} (p : b = b') : ap (pushout.elim Pinl Pinr Pglue) (ap inr p) = ap Pinr p := !ap_compose⁻¹ /- The non-dependent universal property -/ definition pushout_arrow_equiv (C : Type) : (pushout f g → C) ≃ (Σ(i : BL → C) (j : TR → C), Πc, i (f c) = j (g c)) := begin fapply equiv.MK, { intro f, exact ⟨λx, f (inl x), λx, f (inr x), λx, ap f (glue x)⟩}, { intro v x, induction v with i w, induction w with j p, induction x, exact (i a), exact (j a), exact (p x)}, { intro v, induction v with i w, induction w with j p, esimp, apply ap (λp, ⟨i, j, p⟩), apply eq_of_homotopy, intro x, apply elim_glue}, { intro f, apply eq_of_homotopy, intro x, induction x: esimp, apply eq_pathover, apply hdeg_square, esimp, apply elim_glue}, end /- glue squares -/ protected definition glue_square {x x' : TL} (p : x = x') : square (glue x) (glue x') (ap inl (ap f p)) (ap inr (ap g p)) := by cases p; apply vrefl end pushout open function sigma.ops namespace pushout /- The flattening lemma -/ section universe variable u parameters {TL BL TR : Type} (f : TL → BL) (g : TL → TR) (Pinl : BL → Type.{u}) (Pinr : TR → Type.{u}) (Pglue : Π(x : TL), Pinl (f x) ≃ Pinr (g x)) include Pglue local abbreviation A := BL + TR local abbreviation R : A → A → Type := pushout_rel f g local abbreviation P [unfold 5] := pushout.elim_type Pinl Pinr Pglue local abbreviation F : sigma (Pinl ∘ f) → sigma Pinl := λz, ⟨ f z.1 , z.2 ⟩ local abbreviation G : sigma (Pinl ∘ f) → sigma Pinr := λz, ⟨ g z.1 , Pglue z.1 z.2 ⟩ protected definition flattening : sigma P ≃ pushout F G := begin apply equiv.trans !quotient.flattening.flattening_lemma, fapply equiv.MK, { intro q, induction q with z z z' fr, { induction z with a p, induction a with x x, { exact inl ⟨x, p⟩ }, { exact inr ⟨x, p⟩ } }, { induction fr with a a' r p, induction r with x, exact glue ⟨x, p⟩ } }, { intro q, induction q with xp xp xp, { exact class_of _ ⟨sum.inl xp.1, xp.2⟩ }, { exact class_of _ ⟨sum.inr xp.1, xp.2⟩ }, { apply eq_of_rel, constructor } }, { intro q, induction q with xp xp xp: induction xp with x p, { apply ap inl, reflexivity }, { apply ap inr, reflexivity }, { unfold F, unfold G, apply eq_pathover, rewrite [ap_id,ap_compose' (quotient.elim _ _)], krewrite elim_glue, krewrite elim_eq_of_rel, apply hrefl } }, { intro q, induction q with z z z' fr, { induction z with a p, induction a with x x, { reflexivity }, { reflexivity } }, { induction fr with a a' r p, induction r with x, esimp, apply eq_pathover, rewrite [ap_id,ap_compose' (pushout.elim _ _ _)], krewrite elim_eq_of_rel, krewrite elim_glue, apply hrefl } } end end -- Commutativity of pushouts section variables {TL BL TR : Type} (f : TL → BL) (g : TL → TR) protected definition transpose [unfold 6] : pushout f g → pushout g f := begin intro x, induction x, apply inr a, apply inl a, apply !glue⁻¹ end --TODO prove without krewrite? protected definition transpose_involutive (x : pushout f g) : pushout.transpose g f (pushout.transpose f g x) = x := begin induction x, apply idp, apply idp, apply eq_pathover, refine _ ⬝hp !ap_id⁻¹, refine !(ap_compose (pushout.transpose _ _)) ⬝ph _, esimp[pushout.transpose], krewrite [elim_glue, ap_inv, elim_glue, inv_inv], apply hrfl end protected definition symm [constructor] : pushout f g ≃ pushout g f := begin fapply equiv.MK, do 2 exact !pushout.transpose, do 2 (intro x; apply pushout.transpose_involutive), end end -- Functoriality of pushouts section section lemmas variables {X : Type} {x₀ x₁ x₂ x₃ : X} (p : x₀ = x₁) (q : x₁ = x₂) (r : x₂ = x₃) private definition is_equiv_functor_lemma₁ : (r ⬝ ((p ⬝ q ⬝ r)⁻¹ ⬝ p)) = q⁻¹ := by cases p; cases r; cases q; reflexivity private definition is_equiv_functor_lemma₂ : (p ⬝ q ⬝ r)⁻¹ ⬝ (p ⬝ q) = r⁻¹ := by cases p; cases r; cases q; reflexivity end lemmas variables {TL BL TR : Type} {f : TL → BL} {g : TL → TR} {TL' BL' TR' : Type} {f' : TL' → BL'} {g' : TL' → TR'} (tl : TL → TL') (bl : BL → BL') (tr : TR → TR') (fh : bl ∘ f ~ f' ∘ tl) (gh : tr ∘ g ~ g' ∘ tl) include fh gh protected definition functor [unfold 16] : pushout f g → pushout f' g' := begin intro x, induction x with a b z, { exact inl (bl a) }, { exact inr (tr b) }, { exact (ap inl (fh z)) ⬝ glue (tl z) ⬝ (ap inr (gh z)⁻¹) } end protected definition ap_functor_inl [unfold 18] {x x' : BL} (p : x = x') : ap (pushout.functor tl bl tr fh gh) (ap inl p) = ap inl (ap bl p) := by cases p; reflexivity protected definition ap_functor_inr [unfold 18] {x x' : TR} (p : x = x') : ap (pushout.functor tl bl tr fh gh) (ap inr p) = ap inr (ap tr p) := by cases p; reflexivity variables [ietl : is_equiv tl] [iebl : is_equiv bl] [ietr : is_equiv tr] include ietl iebl ietr open equiv is_equiv arrow protected definition is_equiv_functor [instance] [constructor] : is_equiv (pushout.functor tl bl tr fh gh) := adjointify (pushout.functor tl bl tr fh gh) (pushout.functor tl⁻¹ bl⁻¹ tr⁻¹ (inv_commute_of_commute tl bl f f' fh) (inv_commute_of_commute tl tr g g' gh)) abstract begin intro x', induction x' with a' b' z', { apply ap inl, apply right_inv }, { apply ap inr, apply right_inv }, { apply eq_pathover, rewrite [ap_id,ap_compose' (pushout.functor tl bl tr fh gh)], krewrite elim_glue, rewrite [ap_inv,ap_con,ap_inv], krewrite [pushout.ap_functor_inr], rewrite ap_con, krewrite [pushout.ap_functor_inl,elim_glue], apply transpose, apply move_top_of_right, apply move_top_of_left', krewrite [-(ap_inv inl),-ap_con,-(ap_inv inr),-ap_con], apply move_top_of_right, apply move_top_of_left', krewrite [-ap_con,-(ap_inv inl),-ap_con], rewrite ap_bot_inv_commute_of_commute, apply eq_hconcat (ap02 inl (is_equiv_functor_lemma₁ (right_inv bl (f' z')) (ap f' (right_inv tl z')⁻¹) (fh (tl⁻¹ z'))⁻¹)), rewrite [ap_inv f',inv_inv], rewrite ap_bot_inv_commute_of_commute, refine hconcat_eq _ (ap02 inr (is_equiv_functor_lemma₁ (right_inv tr (g' z')) (ap g' (right_inv tl z')⁻¹) (gh (tl⁻¹ z'))⁻¹))⁻¹, rewrite [ap_inv g',inv_inv], apply pushout.glue_square } end end abstract begin intro x, induction x with a b z, { apply ap inl, apply left_inv }, { apply ap inr, apply left_inv }, { apply eq_pathover, rewrite [ap_id,ap_compose' (pushout.functor tl⁻¹ bl⁻¹ tr⁻¹ _ _) (pushout.functor tl bl tr _ _)], krewrite elim_glue, rewrite [ap_inv,ap_con,ap_inv], krewrite [pushout.ap_functor_inr], rewrite ap_con, krewrite [pushout.ap_functor_inl,elim_glue], apply transpose, apply move_top_of_right, apply move_top_of_left', krewrite [-(ap_inv inl),-ap_con,-(ap_inv inr),-ap_con], apply move_top_of_right, apply move_top_of_left', krewrite [-ap_con,-(ap_inv inl),-ap_con], rewrite inv_commute_of_commute_top, apply eq_hconcat (ap02 inl (is_equiv_functor_lemma₂ (ap bl⁻¹ (fh z))⁻¹ (left_inv bl (f z)) (ap f (left_inv tl z)⁻¹))), rewrite [ap_inv f,inv_inv], rewrite inv_commute_of_commute_top, refine hconcat_eq _ (ap02 inr (is_equiv_functor_lemma₂ (ap tr⁻¹ (gh z))⁻¹ (left_inv tr (g z)) (ap g (left_inv tl z)⁻¹)))⁻¹, rewrite [ap_inv g,inv_inv], apply pushout.glue_square } end end end /- version giving the equivalence -/ section variables {TL BL TR : Type} (f : TL → BL) (g : TL → TR) {TL' BL' TR' : Type} (f' : TL' → BL') (g' : TL' → TR') (tl : TL ≃ TL') (bl : BL ≃ BL') (tr : TR ≃ TR') (fh : bl ∘ f ~ f' ∘ tl) (gh : tr ∘ g ~ g' ∘ tl) include fh gh protected definition equiv [constructor] : pushout f g ≃ pushout f' g' := equiv.mk (pushout.functor tl bl tr fh gh) _ end definition pointed_pushout [instance] [constructor] {TL BL TR : Type} [HTL : pointed TL] [HBL : pointed BL] [HTR : pointed TR] (f : TL → BL) (g : TL → TR) : pointed (pushout f g) := pointed.mk (inl (point _)) end pushout open pushout definition ppushout [constructor] {TL BL TR : Type*} (f : TL →* BL) (g : TL →* TR) : Type* := pointed.mk' (pushout f g) namespace pushout section parameters {TL BL TR : Type*} (f : TL →* BL) (g : TL →* TR) parameters {f g} definition pinl [constructor] : BL →* ppushout f g := pmap.mk inl idp definition pinr [constructor] : TR →* ppushout f g := pmap.mk inr ((ap inr (respect_pt g))⁻¹ ⬝ !glue⁻¹ ⬝ (ap inl (respect_pt f))) definition pglue (x : TL) : pinl (f x) = pinr (g x) := -- TODO do we need this? !glue end section variables {TL BL TR : Type*} (f : TL →* BL) (g : TL →* TR) protected definition psymm [constructor] : ppushout f g ≃* ppushout g f := begin fapply pequiv_of_equiv, { apply pushout.symm }, { exact ap inr (respect_pt f)⁻¹ ⬝ !glue⁻¹ ⬝ ap inl (respect_pt g) } end end end pushout
7ef7373eb78d94e2044ffbba580fca4764a94833
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/using_smt2.lean
bf0c31e0bf1324396c39e02e48f7662798360606
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
1,275
lean
open smt_tactic lemma ex (p q : Prop) : p → q → p := by using_smt $ intros lemma ex2 (p q : Prop) : ¬ p → q → ¬ p := by using_smt $ intros lemma ex3 (p q : Prop) : p → (p ↔ q) → q := by using_smt $ intros lemma ex4 (p q : Prop) : p → (p → q) → q := by using_smt $ intros lemma ex5 (p q : Prop) : (p → q) → p → q := by using_smt $ intros lemma ex6 (p q r : Prop) : (p → r → q) → r → p → q := by using_smt $ intros lemma ex7 (p q r s t o : Prop) : (p ∨ t → o ∨ r → q ∧ s) → r → p → q := by using_smt $ intros lemma ex8 (p q : Prop) (a b c : nat) : (p ∨ q → a = b ∨ a = c) → a ≠ b → p → c = a := by using_smt $ intros lemma ex9 (p q : Prop) [decidable p] [decidable q] (a b c : nat) : (if p ∨ q then (a = b ∨ a = c) else (a = 0)) → p → a ≠ b → c = a := by using_smt $ intros lemma ex10 (p q : Prop) : p → (p ↔ ¬q) → ¬q := by using_smt $ intros lemma ex11 (p q r s : Prop) : (p ∨ q → not (r ∨ s)) → p → not r := by using_smt $ intros -- lemma ex12 (p q r : Prop) (a b c : nat): (p → q ∧ r ∧ a = b + c) → p → (c + b = a ∧ r) := -- by using_smt $ intros lemma ex13 (a b c d : nat) : b = d → c = d → (if a > 10 then b else c) = b := by using_smt $ intros
baaaac149dc024e8a12e5813c86ddc14d690293c
491068d2ad28831e7dade8d6dff871c3e49d9431
/tests/lean/run/st_options.lean
f59581b23bb1bbb28b877756f609f6b2d576344a
[ "Apache-2.0" ]
permissive
davidmueller13/lean
65a3ed141b4088cd0a268e4de80eb6778b21a0e9
c626e2e3c6f3771e07c32e82ee5b9e030de5b050
refs/heads/master
1,611,278,313,401
1,444,021,177,000
1,444,021,177,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,938
lean
set_option structure.eta_thm true set_option structure.proj_mk_thm true structure has_mul [class] (A : Type) := (mul : A → A → A) structure has_add [class] (A : Type) := (add : A → A → A) structure has_one [class] (A : Type) := (one : A) structure has_zero [class] (A : Type) := (zero : A) structure has_inv [class] (A : Type) := (inv : A → A) structure has_neg [class] (A : Type) := (neg : A → A) structure semigroup [class] (A : Type) extends has_mul A := (mul_assoc : ∀a b c, mul (mul a b) c = mul a (mul b c)) structure comm_semigroup [class] (A : Type) extends semigroup A := (mul_comm : ∀a b, mul a b = mul b a) structure left_cancel_semigroup [class] (A : Type) extends semigroup A := (mul_left_cancel : ∀a b c, mul a b = mul a c → b = c) structure right_cancel_semigroup [class] (A : Type) extends semigroup A := (mul_right_cancel : ∀a b c, mul a b = mul c b → a = c) structure add_semigroup [class] (A : Type) extends has_add A := (add_assoc : ∀a b c, add (add a b) c = add a (add b c)) structure add_comm_semigroup [class] (A : Type) extends add_semigroup A := (add_comm : ∀a b, add a b = add b a) structure add_left_cancel_semigroup [class] (A : Type) extends add_semigroup A := (add_left_cancel : ∀a b c, add a b = add a c → b = c) structure add_right_cancel_semigroup [class] (A : Type) extends add_semigroup A := (add_right_cancel : ∀a b c, add a b = add c b → a = c) structure monoid [class] (A : Type) extends semigroup A, has_one A := (one_mul : ∀a, mul one a = a) (mul_one : ∀a, mul a one = a) structure comm_monoid [class] (A : Type) extends monoid A, comm_semigroup A structure add_monoid [class] (A : Type) extends add_semigroup A, has_zero A := (zero_add : ∀a, add zero a = a) (add_zero : ∀a, add a zero = a) structure add_comm_monoid [class] (A : Type) extends add_monoid A, add_comm_semigroup A structure group [class] (A : Type) extends monoid A, has_inv A := (mul_left_inv : ∀a, mul (inv a) a = one) structure comm_group [class] (A : Type) extends group A, comm_monoid A structure add_group [class] (A : Type) extends add_monoid A, has_neg A := (add_left_inv : ∀a, add (neg a) a = zero) structure add_comm_group [class] (A : Type) extends add_group A, add_comm_monoid A structure distrib [class] (A : Type) extends has_mul A, has_add A := (left_distrib : ∀a b c, mul a (add b c) = add (mul a b) (mul a c)) (right_distrib : ∀a b c, mul (add a b) c = add (mul a c) (mul b c)) structure mul_zero_class [class] (A : Type) extends has_mul A, has_zero A := (zero_mul : ∀a, mul zero a = zero) (mul_zero : ∀a, mul a zero = zero) structure zero_ne_one_class [class] (A : Type) extends has_zero A, has_one A := (zero_ne_one : zero ≠ one) structure semiring [class] (A : Type) extends add_comm_monoid A, monoid A, distrib A, mul_zero_class A, zero_ne_one_class A set_option pp.implicit true check @semiring.mul.mk check @semiring.eta
689f40934e2d30c8e98e5e23711280b3190ccf0a
ebbdcbd7ddc89a9ef7c3b397b301d5f5272a918f
/qp/p1_categories/c7_cat_of_cats.lean
7858cc78ca0cdbd2371ec5050862bdef79e89841
[]
no_license
intoverflow/qvr
34b9ef23604738381ca20b7d622fd0399d88f2dd
0cfcd33fe4bf8d93851a00cec5bfd21e77105d74
refs/heads/master
1,616,591,570,371
1,492,575,772,000
1,492,575,772,000
80,061,627
0
0
null
null
null
null
UTF-8
Lean
false
false
67
lean
import .c7_cat_of_cats.s1_basic import .c7_cat_of_cats.s2_conduche
8881f4a68de886954c1ae61503b925b62ae42b31
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/data/list/range.lean
443509f7701e83e2c41b13e876be7088ec5ae271
[ "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
11,950
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Kenny Lau, Scott Morrison -/ import data.list.chain import data.list.nodup import data.list.of_fn import data.list.zip /-! # Ranges of naturals as lists This file shows basic results about `list.iota`, `list.range`, `list.range'` (all defined in `data.list.defs`) and defines `list.fin_range`. `fin_range n` is the list of elements of `fin n`. `iota n = [1, ..., n]` and `range n = [0, ..., n - 1]` are basic list constructions used for tactics. `range' a b = [a, ..., a + b - 1]` is there to help prove properties about them. Actual maths should use `list.Ico` instead. -/ universe u open nat namespace list variables {α : Type u} @[simp] theorem length_range' : ∀ (s n : ℕ), length (range' s n) = n | s 0 := rfl | s (n+1) := congr_arg succ (length_range' _ _) @[simp] theorem range'_eq_nil {s n : ℕ} : range' s n = [] ↔ n = 0 := by rw [← length_eq_zero, length_range'] @[simp] theorem mem_range' {m : ℕ} : ∀ {s n : ℕ}, m ∈ range' s n ↔ s ≤ m ∧ m < s + n | s 0 := (false_iff _).2 $ λ ⟨H1, H2⟩, not_le_of_lt H2 H1 | s (succ n) := have m = s → m < s + n + 1, from λ e, e ▸ lt_succ_of_le (nat.le_add_right _ _), have l : m = s ∨ s + 1 ≤ m ↔ s ≤ m, by simpa only [eq_comm] using (@decidable.le_iff_eq_or_lt _ _ _ s m).symm, (mem_cons_iff _ _ _).trans $ by simp only [mem_range', or_and_distrib_left, or_iff_right_of_imp this, l, add_right_comm]; refl theorem map_add_range' (a) : ∀ s n : ℕ, map ((+) a) (range' s n) = range' (a + s) n | s 0 := rfl | s (n+1) := congr_arg (cons _) (map_add_range' (s+1) n) theorem map_sub_range' (a) : ∀ (s n : ℕ) (h : a ≤ s), map (λ x, x - a) (range' s n) = range' (s - a) n | s 0 _ := rfl | s (n+1) h := begin convert congr_arg (cons (s-a)) (map_sub_range' (s+1) n (nat.le_succ_of_le h)), rw nat.succ_sub h, refl, end theorem chain_succ_range' : ∀ s n : ℕ, chain (λ a b, b = succ a) s (range' (s+1) n) | s 0 := chain.nil | s (n+1) := (chain_succ_range' (s+1) n).cons rfl theorem chain_lt_range' (s n : ℕ) : chain (<) s (range' (s+1) n) := (chain_succ_range' s n).imp (λ a b e, e.symm ▸ lt_succ_self _) theorem pairwise_lt_range' : ∀ s n : ℕ, pairwise (<) (range' s n) | s 0 := pairwise.nil | s (n+1) := chain_iff_pairwise.1 (chain_lt_range' s n) theorem nodup_range' (s n : ℕ) : nodup (range' s n) := (pairwise_lt_range' s n).imp (λ a b, ne_of_lt) @[simp] theorem range'_append : ∀ s m n : ℕ, range' s m ++ range' (s+m) n = range' s (n+m) | s 0 n := rfl | s (m+1) n := show s :: (range' (s+1) m ++ range' (s+m+1) n) = s :: range' (s+1) (n+m), by rw [add_right_comm, range'_append] theorem range'_sublist_right {s m n : ℕ} : range' s m <+ range' s n ↔ m ≤ n := ⟨λ h, by simpa only [length_range'] using length_le_of_sublist h, λ h, by rw [← tsub_add_cancel_of_le h, ← range'_append]; apply sublist_append_left⟩ theorem range'_subset_right {s m n : ℕ} : range' s m ⊆ range' s n ↔ m ≤ n := ⟨λ h, le_of_not_lt $ λ hn, lt_irrefl (s+n) $ (mem_range'.1 $ h $ mem_range'.2 ⟨nat.le_add_right _ _, nat.add_lt_add_left hn s⟩).2, λ h, (range'_sublist_right.2 h).subset⟩ theorem nth_range' : ∀ s {m n : ℕ}, m < n → nth (range' s n) m = some (s + m) | s 0 (n+1) _ := rfl | s (m+1) (n+1) h := (nth_range' (s+1) (lt_of_add_lt_add_right h)).trans $ by rw add_right_comm; refl @[simp] lemma nth_le_range' {n m} (i) (H : i < (range' n m).length) : nth_le (range' n m) i H = n + i := option.some.inj $ by rw [←nth_le_nth _, nth_range' _ (by simpa using H)] theorem range'_concat (s n : ℕ) : range' s (n + 1) = range' s n ++ [s+n] := by rw add_comm n 1; exact (range'_append s n 1).symm theorem range_core_range' : ∀ s n : ℕ, range_core s (range' s n) = range' 0 (n + s) | 0 n := rfl | (s+1) n := by rw [show n+(s+1) = n+1+s, from add_right_comm n s 1]; exact range_core_range' s (n+1) theorem range_eq_range' (n : ℕ) : range n = range' 0 n := (range_core_range' n 0).trans $ by rw zero_add theorem range_succ_eq_map (n : ℕ) : range (n + 1) = 0 :: map succ (range n) := by rw [range_eq_range', range_eq_range', range', add_comm, ← map_add_range']; congr; exact funext one_add theorem range'_eq_map_range (s n : ℕ) : range' s n = map ((+) s) (range n) := by rw [range_eq_range', map_add_range']; refl @[simp] theorem length_range (n : ℕ) : length (range n) = n := by simp only [range_eq_range', length_range'] @[simp] theorem range_eq_nil {n : ℕ} : range n = [] ↔ n = 0 := by rw [← length_eq_zero, length_range] theorem pairwise_lt_range (n : ℕ) : pairwise (<) (range n) := by simp only [range_eq_range', pairwise_lt_range'] theorem nodup_range (n : ℕ) : nodup (range n) := by simp only [range_eq_range', nodup_range'] theorem range_sublist {m n : ℕ} : range m <+ range n ↔ m ≤ n := by simp only [range_eq_range', range'_sublist_right] theorem range_subset {m n : ℕ} : range m ⊆ range n ↔ m ≤ n := by simp only [range_eq_range', range'_subset_right] @[simp] theorem mem_range {m n : ℕ} : m ∈ range n ↔ m < n := by simp only [range_eq_range', mem_range', nat.zero_le, true_and, zero_add] @[simp] theorem not_mem_range_self {n : ℕ} : n ∉ range n := mt mem_range.1 $ lt_irrefl _ @[simp] theorem self_mem_range_succ (n : ℕ) : n ∈ range (n + 1) := by simp only [succ_pos', lt_add_iff_pos_right, mem_range] theorem nth_range {m n : ℕ} (h : m < n) : nth (range n) m = some m := by simp only [range_eq_range', nth_range' _ h, zero_add] theorem range_succ (n : ℕ) : range (succ n) = range n ++ [n] := by simp only [range_eq_range', range'_concat, zero_add] @[simp] lemma range_zero : range 0 = [] := rfl theorem chain'_range_succ (r : ℕ → ℕ → Prop) (n : ℕ) : chain' r (range n.succ) ↔ ∀ m < n, r m m.succ := begin rw range_succ, induction n with n hn, { simp }, { rw range_succ, simp only [append_assoc, singleton_append, chain'_append_cons_cons, chain'_singleton, and_true], rw [hn, forall_lt_succ] } end theorem chain_range_succ (r : ℕ → ℕ → Prop) (n a : ℕ) : chain r a (range n.succ) ↔ r a 0 ∧ ∀ m < n, r m m.succ := begin rw [range_succ_eq_map, chain_cons, and.congr_right_iff, ←chain'_range_succ, range_succ_eq_map], exact λ _, iff.rfl end lemma range_add (a : ℕ) : ∀ b, range (a + b) = range a ++ (range b).map (λ x, a + x) | 0 := by rw [add_zero, range_zero, map_nil, append_nil] | (b + 1) := by rw [nat.add_succ, range_succ, range_add b, range_succ, map_append, map_singleton, append_assoc] theorem iota_eq_reverse_range' : ∀ n : ℕ, iota n = reverse (range' 1 n) | 0 := rfl | (n+1) := by simp only [iota, range'_concat, iota_eq_reverse_range' n, reverse_append, add_comm]; refl @[simp] theorem length_iota (n : ℕ) : length (iota n) = n := by simp only [iota_eq_reverse_range', length_reverse, length_range'] theorem pairwise_gt_iota (n : ℕ) : pairwise (>) (iota n) := by simp only [iota_eq_reverse_range', pairwise_reverse, pairwise_lt_range'] theorem nodup_iota (n : ℕ) : nodup (iota n) := by simp only [iota_eq_reverse_range', nodup_reverse, nodup_range'] theorem mem_iota {m n : ℕ} : m ∈ iota n ↔ 1 ≤ m ∧ m ≤ n := by simp only [iota_eq_reverse_range', mem_reverse, mem_range', add_comm, lt_succ_iff] theorem reverse_range' : ∀ s n : ℕ, reverse (range' s n) = map (λ i, s + n - 1 - i) (range n) | s 0 := rfl | s (n+1) := by rw [range'_concat, reverse_append, range_succ_eq_map]; simpa only [show s + (n + 1) - 1 = s + n, from rfl, (∘), λ a i, show a - 1 - i = a - succ i, from pred_sub _ _, reverse_singleton, map_cons, tsub_zero, cons_append, nil_append, eq_self_iff_true, true_and, map_map] using reverse_range' s n /-- All elements of `fin n`, from `0` to `n-1`. The corresponding finset is `finset.univ`. -/ def fin_range (n : ℕ) : list (fin n) := (range n).pmap fin.mk (λ _, list.mem_range.1) @[simp] lemma fin_range_zero : fin_range 0 = [] := rfl @[simp] lemma mem_fin_range {n : ℕ} (a : fin n) : a ∈ fin_range n := mem_pmap.2 ⟨a.1, mem_range.2 a.2, fin.eta _ _⟩ lemma nodup_fin_range (n : ℕ) : (fin_range n).nodup := (nodup_range _).pmap $ λ _ _ _ _, fin.veq_of_eq @[simp] lemma length_fin_range (n : ℕ) : (fin_range n).length = n := by rw [fin_range, length_pmap, length_range] @[simp] lemma fin_range_eq_nil {n : ℕ} : fin_range n = [] ↔ n = 0 := by rw [← length_eq_zero, length_fin_range] @[simp] lemma map_coe_fin_range (n : ℕ) : (fin_range n).map coe = list.range n := begin simp_rw [fin_range, map_pmap, fin.mk, subtype.coe_mk, pmap_eq_map], exact list.map_id _ end lemma fin_range_succ_eq_map (n : ℕ) : fin_range n.succ = 0 :: (fin_range n).map fin.succ := begin apply map_injective_iff.mpr subtype.coe_injective, rw [map_cons, map_coe_fin_range, range_succ_eq_map, fin.coe_zero, ←map_coe_fin_range, map_map, map_map, function.comp, function.comp], congr' 2 with x, exact (fin.coe_succ _).symm, end @[to_additive] theorem prod_range_succ {α : Type u} [monoid α] (f : ℕ → α) (n : ℕ) : ((range n.succ).map f).prod = ((range n).map f).prod * f n := by rw [range_succ, map_append, map_singleton, prod_append, prod_cons, prod_nil, mul_one] /-- A variant of `prod_range_succ` which pulls off the first term in the product rather than the last.-/ @[to_additive "A variant of `sum_range_succ` which pulls off the first term in the sum rather than the last."] theorem prod_range_succ' {α : Type u} [monoid α] (f : ℕ → α) (n : ℕ) : ((range n.succ).map f).prod = f 0 * ((range n).map (λ i, f (succ i))).prod := nat.rec_on n (show 1 * f 0 = f 0 * 1, by rw [one_mul, mul_one]) (λ _ hd, by rw [list.prod_range_succ, hd, mul_assoc, ←list.prod_range_succ]) @[simp] theorem enum_from_map_fst : ∀ n (l : list α), map prod.fst (enum_from n l) = range' n l.length | n [] := rfl | n (a :: l) := congr_arg (cons _) (enum_from_map_fst _ _) @[simp] theorem enum_map_fst (l : list α) : map prod.fst (enum l) = range l.length := by simp only [enum, enum_from_map_fst, range_eq_range'] lemma enum_eq_zip_range (l : list α) : l.enum = (range l.length).zip l := zip_of_prod (enum_map_fst _) (enum_map_snd _) @[simp] lemma unzip_enum_eq_prod (l : list α) : l.enum.unzip = (range l.length, l) := by simp only [enum_eq_zip_range, unzip_zip, length_range] lemma enum_from_eq_zip_range' (l : list α) {n : ℕ} : l.enum_from n = (range' n l.length).zip l := zip_of_prod (enum_from_map_fst _ _) (enum_from_map_snd _ _) @[simp] lemma unzip_enum_from_eq_prod (l : list α) {n : ℕ} : (l.enum_from n).unzip = (range' n l.length, l) := by simp only [enum_from_eq_zip_range', unzip_zip, length_range'] @[simp] lemma nth_le_range {n} (i) (H : i < (range n).length) : nth_le (range n) i H = i := option.some.inj $ by rw [← nth_le_nth _, nth_range (by simpa using H)] @[simp] lemma nth_le_fin_range {n : ℕ} {i : ℕ} (h) : (fin_range n).nth_le i h = ⟨i, length_fin_range n ▸ h⟩ := by simp only [fin_range, nth_le_range, nth_le_pmap, fin.mk_eq_subtype_mk] theorem of_fn_eq_pmap {α n} {f : fin n → α} : of_fn f = pmap (λ i hi, f ⟨i, hi⟩) (range n) (λ _, mem_range.1) := by rw [pmap_eq_map_attach]; from ext_le (by simp) (λ i hi1 hi2, by { simp at hi1, simp [nth_le_of_fn f ⟨i, hi1⟩, -subtype.val_eq_coe] }) theorem of_fn_id (n) : of_fn id = fin_range n := of_fn_eq_pmap theorem of_fn_eq_map {α n} {f : fin n → α} : of_fn f = (fin_range n).map f := by rw [← of_fn_id, map_of_fn, function.right_id] theorem nodup_of_fn {α n} {f : fin n → α} (hf : function.injective f) : nodup (of_fn f) := by { rw of_fn_eq_pmap, exact (nodup_range n).pmap (λ _ _ _ _ H, fin.veq_of_eq $ hf H) } end list
63814c72be9cef5871580ecdb02a527ea855db5f
9cba98daa30c0804090f963f9024147a50292fa0
/old/src/classical_acceleration.lean
f9fb560fc8e3c91f2760e49419da606cd1876d4f
[]
no_license
kevinsullivan/phys
dcb192f7b3033797541b980f0b4a7e75d84cea1a
ebc2df3779d3605ff7a9b47eeda25c2a551e011f
refs/heads/master
1,637,490,575,500
1,629,899,064,000
1,629,899,064,000
168,012,884
0
3
null
1,629,644,436,000
1,548,699,832,000
Lean
UTF-8
Lean
false
false
571
lean
import .classical_velocity import .classical_time -- id serves as unique ID for a given acceleration space structure classicalAcceleration : Type := mk :: (id : ℕ) (v : classicalVelocity) (t : classicalTime) -- provide standard 3D acceleration object def worldAcceleration := classicalAcceleration.mk 0 worldVelocity worldTime --returns vector space of the same dimension as the built in geometric space noncomputable def classicalAccelerationAlgebra : classicalAcceleration → Algebra | (classicalAcceleration.mk n v t) := Algebra.vector_space (to_vector v.g.dim)
753826b942b86ed88db6f2a54457aa354e1a3195
c777c32c8e484e195053731103c5e52af26a25d1
/src/ring_theory/trace.lean
5f16361c7da3877a6b40ee94916d2da853a07d2b
[ "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
23,502
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 linear_algebra.matrix.bilinear_form import linear_algebra.matrix.charpoly.minpoly import linear_algebra.determinant import linear_algebra.finite_dimensional import linear_algebra.vandermonde import linear_algebra.trace import field_theory.is_alg_closed.algebraic_closure import field_theory.primitive_element import field_theory.galois import ring_theory.power_basis /-! # Trace for (finite) ring extensions. Suppose we have an `R`-algebra `S` with a finite basis. For each `s : S`, the trace of the linear map given by multiplying by `s` gives information about the roots of the minimal polynomial of `s` over `R`. ## Main definitions * `algebra.trace R S x`: the trace of an element `s` of an `R`-algebra `S` * `algebra.trace_form R S`: bilinear form sending `x`, `y` to the trace of `x * y` * `algebra.trace_matrix R b`: the matrix whose `(i j)`-th element is the trace of `b i * b j`. * `algebra.embeddings_matrix A C b : matrix κ (B →ₐ[A] C) C` is the matrix whose `(i, σ)` coefficient is `σ (b i)`. * `algebra.embeddings_matrix_reindex A C b e : matrix κ κ C` is the matrix whose `(i, j)` coefficient is `σⱼ (b i)`, where `σⱼ : B →ₐ[A] C` is the embedding corresponding to `j : κ` given by a bijection `e : κ ≃ (B →ₐ[A] C)`. ## Main results * `trace_algebra_map_of_basis`, `trace_algebra_map`: if `x : K`, then `Tr_{L/K} x = [L : K] x` * `trace_trace_of_basis`, `trace_trace`: `Tr_{L/K} (Tr_{F/L} x) = Tr_{F/K} x` * `trace_eq_sum_roots`: the trace of `x : K(x)` is the sum of all conjugate roots of `x` * `trace_eq_sum_embeddings`: the trace of `x : K(x)` is the sum of all embeddings of `x` into an algebraically closed field * `trace_form_nondegenerate`: the trace form over a separable extension is a nondegenerate bilinear form ## Implementation notes Typically, the trace is defined specifically for finite field extensions. The definition is as general as possible and the assumption that we have fields or that the extension is finite is added to the lemmas as needed. We only define the trace for left multiplication (`algebra.left_mul_matrix`, i.e. `linear_map.mul_left`). For now, the definitions assume `S` is commutative, so the choice doesn't matter anyway. ## References * https://en.wikipedia.org/wiki/Field_trace -/ universes u v w z variables {R S T : Type*} [comm_ring R] [comm_ring S] [comm_ring T] variables [algebra R S] [algebra R T] variables {K L : Type*} [field K] [field L] [algebra K L] variables {ι κ : Type w} [fintype ι] open finite_dimensional open linear_map open matrix open_locale big_operators open_locale matrix namespace algebra variables (b : basis ι R S) variables (R S) /-- The trace of an element `s` of an `R`-algebra is the trace of `(*) s`, as an `R`-linear map. -/ noncomputable def trace : S →ₗ[R] R := (linear_map.trace R S).comp (lmul R S).to_linear_map variables {S} -- Not a `simp` lemma since there are more interesting ways to rewrite `trace R S x`, -- for example `trace_trace` lemma trace_apply (x) : trace R S x = linear_map.trace R S (lmul R S x) := rfl lemma trace_eq_zero_of_not_exists_basis (h : ¬ ∃ (s : finset S), nonempty (basis s R S)) : trace R S = 0 := by { ext s, simp [trace_apply, linear_map.trace, h] } include b variables {R} -- Can't be a `simp` lemma because it depends on a choice of basis lemma trace_eq_matrix_trace [decidable_eq ι] (b : basis ι R S) (s : S) : trace R S s = matrix.trace (algebra.left_mul_matrix b s) := by { rw [trace_apply, linear_map.trace_eq_matrix_trace _ b, ←to_matrix_lmul_eq], refl } /-- If `x` is in the base field `K`, then the trace is `[L : K] * x`. -/ lemma trace_algebra_map_of_basis (x : R) : trace R S (algebra_map R S x) = fintype.card ι • x := begin haveI := classical.dec_eq ι, rw [trace_apply, linear_map.trace_eq_matrix_trace R b, matrix.trace], convert finset.sum_const _, ext i, simp [-coe_lmul_eq_mul], end omit b /-- If `x` is in the base field `K`, then the trace is `[L : K] * x`. (If `L` is not finite-dimensional over `K`, then `trace` and `finrank` return `0`.) -/ @[simp] lemma trace_algebra_map (x : K) : trace K L (algebra_map K L x) = finrank K L • x := begin by_cases H : ∃ (s : finset L), nonempty (basis s K L), { rw [trace_algebra_map_of_basis H.some_spec.some, finrank_eq_card_basis H.some_spec.some] }, { simp [trace_eq_zero_of_not_exists_basis K H, finrank_eq_zero_of_not_exists_basis_finset H] } end lemma trace_trace_of_basis [algebra S T] [is_scalar_tower R S T] {ι κ : Type*} [finite ι] [finite κ] (b : basis ι R S) (c : basis κ S T) (x : T) : trace R S (trace S T x) = trace R T x := begin haveI := classical.dec_eq ι, haveI := classical.dec_eq κ, casesI nonempty_fintype ι, casesI nonempty_fintype κ, rw [trace_eq_matrix_trace (b.smul c), trace_eq_matrix_trace b, trace_eq_matrix_trace c, matrix.trace, matrix.trace, matrix.trace, ← finset.univ_product_univ, finset.sum_product], refine finset.sum_congr rfl (λ i _, _), simp only [alg_hom.map_sum, smul_left_mul_matrix, finset.sum_apply, matrix.diag, -- The unifier is not smart enough to apply this one by itself: finset.sum_apply i _ (λ y, left_mul_matrix b (left_mul_matrix c x y y))] end lemma trace_comp_trace_of_basis [algebra S T] [is_scalar_tower R S T] {ι κ : Type*} [finite ι] [fintype κ] (b : basis ι R S) (c : basis κ S T) : (trace R S).comp ((trace S T).restrict_scalars R) = trace R T := by { ext, rw [linear_map.comp_apply, linear_map.restrict_scalars_apply, trace_trace_of_basis b c] } @[simp] lemma trace_trace [algebra K T] [algebra L T] [is_scalar_tower K L T] [finite_dimensional K L] [finite_dimensional L T] (x : T) : trace K L (trace L T x) = trace K T x := trace_trace_of_basis (basis.of_vector_space K L) (basis.of_vector_space L T) x @[simp] lemma trace_comp_trace [algebra K T] [algebra L T] [is_scalar_tower K L T] [finite_dimensional K L] [finite_dimensional L T] : (trace K L).comp ((trace L T).restrict_scalars K) = trace K T := by { ext, rw [linear_map.comp_apply, linear_map.restrict_scalars_apply, trace_trace] } @[simp] lemma trace_prod_apply [module.free R S] [module.free R T] [module.finite R S] [module.finite R T] (x : S × T) : trace R (S × T) x = trace R S x.fst + trace R T x.snd := begin nontriviality R, let f := (lmul R S).to_linear_map.prod_map (lmul R T).to_linear_map, have : (lmul R (S × T)).to_linear_map = (prod_map_linear R S T S T R).comp f := linear_map.ext₂ prod.mul_def, simp_rw [trace, this], exact trace_prod_map' _ _, end lemma trace_prod [module.free R S] [module.free R T] [module.finite R S] [module.finite R T] : trace R (S × T) = (trace R S).coprod (trace R T) := linear_map.ext $ λ p, by rw [coprod_apply, trace_prod_apply] section trace_form variables (R S) /-- The `trace_form` maps `x y : S` to the trace of `x * y`. It is a symmetric bilinear form and is nondegenerate if the extension is separable. -/ noncomputable def trace_form : bilin_form R S := (linear_map.compr₂ (lmul R S).to_linear_map (trace R S)).to_bilin variables {S} -- This is a nicer lemma than the one produced by `@[simps] def trace_form`. @[simp] lemma trace_form_apply (x y : S) : trace_form R S x y = trace R S (x * y) := rfl lemma trace_form_is_symm : (trace_form R S).is_symm := λ x y, congr_arg (trace R S) (mul_comm _ _) lemma trace_form_to_matrix [decidable_eq ι] (i j) : bilin_form.to_matrix b (trace_form R S) i j = trace R S (b i * b j) := by rw [bilin_form.to_matrix_apply, trace_form_apply] lemma trace_form_to_matrix_power_basis (h : power_basis R S) : bilin_form.to_matrix h.basis (trace_form R S) = of (λ i j, trace R S (h.gen ^ (↑i + ↑j : ℕ))) := by { ext, rw [trace_form_to_matrix, of_apply, pow_add, h.basis_eq_pow, h.basis_eq_pow] } end trace_form end algebra section eq_sum_roots open algebra polynomial variables {F : Type*} [field F] variables [algebra K S] [algebra K F] /-- Given `pb : power_basis K S`, the trace of `pb.gen` is `-(minpoly K pb.gen).next_coeff`. -/ lemma power_basis.trace_gen_eq_next_coeff_minpoly [nontrivial S] (pb : power_basis K S) : algebra.trace K S pb.gen = -(minpoly K pb.gen).next_coeff := begin have d_pos : 0 < pb.dim := power_basis.dim_pos pb, have d_pos' : 0 < (minpoly K pb.gen).nat_degree, { simpa }, haveI : nonempty (fin pb.dim) := ⟨⟨0, d_pos⟩⟩, rw [trace_eq_matrix_trace pb.basis, trace_eq_neg_charpoly_coeff, charpoly_left_mul_matrix, ← pb.nat_degree_minpoly, fintype.card_fin, ← next_coeff_of_pos_nat_degree _ d_pos'] end /-- Given `pb : power_basis K S`, then the trace of `pb.gen` is `((minpoly K pb.gen).map (algebra_map K F)).roots.sum`. -/ lemma power_basis.trace_gen_eq_sum_roots [nontrivial S] (pb : power_basis K S) (hf : (minpoly K pb.gen).splits (algebra_map K F)) : algebra_map K F (trace K S pb.gen) = ((minpoly K pb.gen).map (algebra_map K F)).roots.sum := begin rw [power_basis.trace_gen_eq_next_coeff_minpoly, ring_hom.map_neg, ← next_coeff_map (algebra_map K F).injective, sum_roots_eq_next_coeff_of_monic_of_split ((minpoly.monic (power_basis.is_integral_gen _)).map _) ((splits_id_iff_splits _).2 hf), neg_neg] end namespace intermediate_field.adjoin_simple open intermediate_field lemma trace_gen_eq_zero {x : L} (hx : ¬ is_integral K x) : algebra.trace K K⟮x⟯ (adjoin_simple.gen K x) = 0 := begin rw [trace_eq_zero_of_not_exists_basis, linear_map.zero_apply], contrapose! hx, obtain ⟨s, ⟨b⟩⟩ := hx, refine is_integral_of_mem_of_fg (K⟮x⟯).to_subalgebra _ x _, { exact (submodule.fg_iff_finite_dimensional _).mpr (finite_dimensional.of_fintype_basis b) }, { exact subset_adjoin K _ (set.mem_singleton x) } end lemma trace_gen_eq_sum_roots (x : L) (hf : (minpoly K x).splits (algebra_map K F)) : algebra_map K F (trace K K⟮x⟯ (adjoin_simple.gen K x)) = ((minpoly K x).map (algebra_map K F)).roots.sum := begin have injKxL := (algebra_map K⟮x⟯ L).injective, by_cases hx : is_integral K x, swap, { simp [minpoly.eq_zero hx, trace_gen_eq_zero hx], }, have hx' : is_integral K (adjoin_simple.gen K x), { rwa [← is_integral_algebra_map_iff injKxL, adjoin_simple.algebra_map_gen], apply_instance }, rw [← adjoin.power_basis_gen hx, (adjoin.power_basis hx).trace_gen_eq_sum_roots]; rw [adjoin.power_basis_gen hx, minpoly.eq_of_algebra_map_eq injKxL hx']; try { simp only [adjoin_simple.algebra_map_gen _ _] }, exact hf end end intermediate_field.adjoin_simple open intermediate_field variables (K) lemma trace_eq_trace_adjoin [finite_dimensional K L] (x : L) : algebra.trace K L x = finrank K⟮x⟯ L • trace K K⟮x⟯ (adjoin_simple.gen K x) := begin rw ← @trace_trace _ _ K K⟮x⟯ _ _ _ _ _ _ _ _ x, conv in x { rw ← intermediate_field.adjoin_simple.algebra_map_gen K x }, rw [trace_algebra_map, linear_map.map_smul_of_tower], end variables {K} lemma trace_eq_sum_roots [finite_dimensional K L] {x : L} (hF : (minpoly K x).splits (algebra_map K F)) : algebra_map K F (algebra.trace K L x) = finrank K⟮x⟯ L • ((minpoly K x).map (algebra_map K _)).roots.sum := by rw [trace_eq_trace_adjoin K x, algebra.smul_def, ring_hom.map_mul, ← algebra.smul_def, intermediate_field.adjoin_simple.trace_gen_eq_sum_roots _ hF, is_scalar_tower.algebra_map_smul] end eq_sum_roots variables {F : Type*} [field F] variables [algebra R L] [algebra L F] [algebra R F] [is_scalar_tower R L F] open polynomial lemma algebra.is_integral_trace [finite_dimensional L F] {x : F} (hx : is_integral R x) : is_integral R (algebra.trace L F x) := begin have hx' : is_integral L x := is_integral_of_is_scalar_tower hx, rw [← is_integral_algebra_map_iff (algebra_map L (algebraic_closure F)).injective, trace_eq_sum_roots], { refine (is_integral.multiset_sum _).nsmul _, intros y hy, rw mem_roots_map (minpoly.ne_zero hx') at hy, use [minpoly R x, minpoly.monic hx], rw ← aeval_def at ⊢ hy, exact minpoly.aeval_of_is_scalar_tower R x y hy }, { apply is_alg_closed.splits_codomain }, { apply_instance } end section eq_sum_embeddings variables [algebra K F] [is_scalar_tower K L F] open algebra intermediate_field variables (F) (E : Type*) [field E] [algebra K E] lemma trace_eq_sum_embeddings_gen (pb : power_basis K L) (hE : (minpoly K pb.gen).splits (algebra_map K E)) (hfx : (minpoly K pb.gen).separable) : algebra_map K E (algebra.trace K L pb.gen) = (@@finset.univ (power_basis.alg_hom.fintype pb)).sum (λ σ, σ pb.gen) := begin letI := classical.dec_eq E, rw [pb.trace_gen_eq_sum_roots hE, fintype.sum_equiv pb.lift_equiv', finset.sum_mem_multiset, finset.sum_eq_multiset_sum, multiset.to_finset_val, multiset.dedup_eq_self.mpr _, multiset.map_id], { exact nodup_roots ((separable_map _).mpr hfx) }, { intro x, refl }, { intro σ, rw [power_basis.lift_equiv'_apply_coe, id.def] } end variables [is_alg_closed E] lemma sum_embeddings_eq_finrank_mul [finite_dimensional K F] [is_separable K F] (pb : power_basis K L) : ∑ σ : F →ₐ[K] E, σ (algebra_map L F pb.gen) = finrank L F • (@@finset.univ (power_basis.alg_hom.fintype pb)).sum (λ σ : L →ₐ[K] E, σ pb.gen) := begin haveI : finite_dimensional L F := finite_dimensional.right K L F, haveI : is_separable L F := is_separable_tower_top_of_is_separable K L F, letI : fintype (L →ₐ[K] E) := power_basis.alg_hom.fintype pb, letI : ∀ (f : L →ₐ[K] E), fintype (@@alg_hom L F E _ _ _ _ f.to_ring_hom.to_algebra) := _, -- will be solved by unification rw [fintype.sum_equiv alg_hom_equiv_sigma (λ (σ : F →ₐ[K] E), _) (λ σ, σ.1 pb.gen), ← finset.univ_sigma_univ, finset.sum_sigma, ← finset.sum_nsmul], refine finset.sum_congr rfl (λ σ _, _), { letI : algebra L E := σ.to_ring_hom.to_algebra, simp only [finset.sum_const, finset.card_univ], rw alg_hom.card L F E }, { intros σ, simp only [alg_hom_equiv_sigma, equiv.coe_fn_mk, alg_hom.restrict_domain, alg_hom.comp_apply, is_scalar_tower.coe_to_alg_hom'] } end lemma trace_eq_sum_embeddings [finite_dimensional K L] [is_separable K L] {x : L} : algebra_map K E (algebra.trace K L x) = ∑ σ : L →ₐ[K] E, σ x := begin have hx := is_separable.is_integral K x, rw [trace_eq_trace_adjoin K x, algebra.smul_def, ring_hom.map_mul, ← adjoin.power_basis_gen hx, trace_eq_sum_embeddings_gen E (adjoin.power_basis hx) (is_alg_closed.splits_codomain _), ← algebra.smul_def, algebra_map_smul], { exact (sum_embeddings_eq_finrank_mul L E (adjoin.power_basis hx)).symm }, { haveI := is_separable_tower_bot_of_is_separable K K⟮x⟯ L, exact is_separable.separable K _ } end lemma trace_eq_sum_automorphisms (x : L) [finite_dimensional K L] [is_galois K L] : algebra_map K L (algebra.trace K L x) = ∑ (σ : L ≃ₐ[K] L), σ x := begin apply no_zero_smul_divisors.algebra_map_injective L (algebraic_closure L), rw map_sum (algebra_map L (algebraic_closure L)), rw ← fintype.sum_equiv (normal.alg_hom_equiv_aut K (algebraic_closure L) L), { rw ←trace_eq_sum_embeddings (algebraic_closure L), { simp only [algebra_map_eq_smul_one, smul_one_smul] }, { exact is_galois.to_is_separable } }, { intro σ, simp only [normal.alg_hom_equiv_aut, alg_hom.restrict_normal', equiv.coe_fn_mk, alg_equiv.coe_of_bijective, alg_hom.restrict_normal_commutes, id.map_eq_id, ring_hom.id_apply] }, end end eq_sum_embeddings section det_ne_zero namespace algebra variables (A : Type u) {B : Type v} (C : Type z) variables [comm_ring A] [comm_ring B] [algebra A B] [comm_ring C] [algebra A C] open finset /-- Given an `A`-algebra `B` and `b`, an `κ`-indexed family of elements of `B`, we define `trace_matrix A b` as the matrix whose `(i j)`-th element is the trace of `b i * b j`. -/ noncomputable def trace_matrix (b : κ → B) : matrix κ κ A := of $ λ i j, trace_form A B (b i) (b j) -- TODO: set as an equation lemma for `trace_matrix`, see mathlib4#3024 @[simp] lemma trace_matrix_apply (b : κ → B) (i j) : trace_matrix A b i j = trace_form A B (b i) (b j) := rfl lemma trace_matrix_reindex {κ' : Type*} (b : basis κ A B) (f : κ ≃ κ') : trace_matrix A (b.reindex f) = reindex f f (trace_matrix A b) := by {ext x y, simp} variables {A} lemma trace_matrix_of_matrix_vec_mul [fintype κ] (b : κ → B) (P : matrix κ κ A) : trace_matrix A ((P.map (algebra_map A B)).vec_mul b) = Pᵀ ⬝ (trace_matrix A b) ⬝ P := begin ext α β, rw [trace_matrix_apply, vec_mul, dot_product, vec_mul, dot_product, matrix.mul_apply, bilin_form.sum_left, fintype.sum_congr _ _ (λ (i : κ), @bilin_form.sum_right _ _ _ _ _ _ _ _ (b i * P.map (algebra_map A B) i α) (λ (y : κ), b y * P.map (algebra_map A B) y β)), sum_comm], congr, ext x, rw [matrix.mul_apply, sum_mul], congr, ext y, rw [map_apply, trace_form_apply, mul_comm (b y), ← smul_def], simp only [id.smul_eq_mul, ring_hom.id_apply, map_apply, transpose_apply, linear_map.map_smulₛₗ, trace_form_apply, algebra.smul_mul_assoc], rw [mul_comm (b x), ← smul_def], ring_nf, simp [mul_comm], end lemma trace_matrix_of_matrix_mul_vec [fintype κ] (b : κ → B) (P : matrix κ κ A) : trace_matrix A ((P.map (algebra_map A B)).mul_vec b) = P ⬝ (trace_matrix A b) ⬝ Pᵀ := begin refine add_equiv.injective (transpose_add_equiv _ _ _) _, rw [transpose_add_equiv_apply, transpose_add_equiv_apply, ← vec_mul_transpose, ← transpose_map, trace_matrix_of_matrix_vec_mul, transpose_transpose, transpose_mul, transpose_transpose, transpose_mul] end lemma trace_matrix_of_basis [fintype κ] [decidable_eq κ] (b : basis κ A B) : trace_matrix A b = bilin_form.to_matrix b (trace_form A B) := begin ext i j, rw [trace_matrix_apply, trace_form_apply, trace_form_to_matrix] end lemma trace_matrix_of_basis_mul_vec (b : basis ι A B) (z : B) : (trace_matrix A b).mul_vec (b.equiv_fun z) = (λ i, trace A B (z * (b i))) := begin ext i, rw [← col_apply ((trace_matrix A b).mul_vec (b.equiv_fun z)) i unit.star, col_mul_vec, matrix.mul_apply, trace_matrix], simp only [col_apply, trace_form_apply], conv_lhs { congr, skip, funext, rw [mul_comm _ (b.equiv_fun z _), ← smul_eq_mul, of_apply, ← linear_map.map_smul] }, rw [← linear_map.map_sum], congr, conv_lhs { congr, skip, funext, rw [← mul_smul_comm] }, rw [← finset.mul_sum, mul_comm z], congr, rw [b.sum_equiv_fun ] end variable (A) /-- `embeddings_matrix A C b : matrix κ (B →ₐ[A] C) C` is the matrix whose `(i, σ)` coefficient is `σ (b i)`. It is mostly useful for fields when `fintype.card κ = finrank A B` and `C` is algebraically closed. -/ def embeddings_matrix (b : κ → B) : matrix κ (B →ₐ[A] C) C := of $ λ i (σ : B →ₐ[A] C), σ (b i) -- TODO: set as an equation lemma for `embeddings_matrix`, see mathlib4#3024 @[simp] lemma embeddings_matrix_apply (b : κ → B) (i) (σ : B →ₐ[A] C) : embeddings_matrix A C b i σ = σ (b i) := rfl /-- `embeddings_matrix_reindex A C b e : matrix κ κ C` is the matrix whose `(i, j)` coefficient is `σⱼ (b i)`, where `σⱼ : B →ₐ[A] C` is the embedding corresponding to `j : κ` given by a bijection `e : κ ≃ (B →ₐ[A] C)`. It is mostly useful for fields and `C` is algebraically closed. In this case, in presence of `h : fintype.card κ = finrank A B`, one can take `e := equiv_of_card_eq ((alg_hom.card A B C).trans h.symm)`. -/ def embeddings_matrix_reindex (b : κ → B) (e : κ ≃ (B →ₐ[A] C)) := reindex (equiv.refl κ) e.symm (embeddings_matrix A C b) variable {A} lemma embeddings_matrix_reindex_eq_vandermonde (pb : power_basis A B) (e : fin pb.dim ≃ (B →ₐ[A] C)) : embeddings_matrix_reindex A C pb.basis e = (vandermonde (λ i, e i pb.gen))ᵀ := by { ext i j, simp [embeddings_matrix_reindex, embeddings_matrix] } section field variables (K) {L} (E : Type z) [field E] variables [algebra K E] variables [module.finite K L] [is_separable K L] [is_alg_closed E] variables (b : κ → L) (pb : power_basis K L) lemma trace_matrix_eq_embeddings_matrix_mul_trans : (trace_matrix K b).map (algebra_map K E) = (embeddings_matrix K E b) ⬝ (embeddings_matrix K E b)ᵀ := by { ext i j, simp [trace_eq_sum_embeddings, embeddings_matrix, matrix.mul_apply] } lemma trace_matrix_eq_embeddings_matrix_reindex_mul_trans [fintype κ] (e : κ ≃ (L →ₐ[K] E)) : (trace_matrix K b).map (algebra_map K E) = (embeddings_matrix_reindex K E b e) ⬝ (embeddings_matrix_reindex K E b e)ᵀ := by rw [trace_matrix_eq_embeddings_matrix_mul_trans, embeddings_matrix_reindex, reindex_apply, transpose_submatrix, ← submatrix_mul_transpose_submatrix, ← equiv.coe_refl, equiv.refl_symm] end field end algebra open algebra variables (pb : power_basis K L) lemma det_trace_matrix_ne_zero' [is_separable K L] : det (trace_matrix K pb.basis) ≠ 0 := begin suffices : algebra_map K (algebraic_closure L) (det (trace_matrix K pb.basis)) ≠ 0, { refine mt (λ ht, _) this, rw [ht, ring_hom.map_zero] }, haveI : finite_dimensional K L := pb.finite_dimensional, let e : fin pb.dim ≃ (L →ₐ[K] algebraic_closure L) := (fintype.equiv_fin_of_card_eq _).symm, rw [ring_hom.map_det, ring_hom.map_matrix_apply, trace_matrix_eq_embeddings_matrix_reindex_mul_trans K _ _ e, embeddings_matrix_reindex_eq_vandermonde, det_mul, det_transpose], refine mt mul_self_eq_zero.mp _, { simp only [det_vandermonde, finset.prod_eq_zero_iff, not_exists, sub_eq_zero], intros i _ j hij h, exact (finset.mem_Ioi.mp hij).ne' (e.injective $ pb.alg_hom_ext h) }, { rw [alg_hom.card, pb.finrank] } end lemma det_trace_form_ne_zero [is_separable K L] [decidable_eq ι] (b : basis ι K L) : det (bilin_form.to_matrix b (trace_form K L)) ≠ 0 := begin haveI : finite_dimensional K L := finite_dimensional.of_fintype_basis b, let pb : power_basis K L := field.power_basis_of_finite_of_separable _ _, rw [← bilin_form.to_matrix_mul_basis_to_matrix pb.basis b, ← det_comm' (pb.basis.to_matrix_mul_to_matrix_flip b) _, ← matrix.mul_assoc, det_mul], swap, { apply basis.to_matrix_mul_to_matrix_flip }, refine mul_ne_zero (is_unit_of_mul_eq_one _ ((b.to_matrix pb.basis)ᵀ ⬝ b.to_matrix pb.basis).det _).ne_zero _, { calc (pb.basis.to_matrix b ⬝ (pb.basis.to_matrix b)ᵀ).det * ((b.to_matrix pb.basis)ᵀ ⬝ b.to_matrix pb.basis).det = (pb.basis.to_matrix b ⬝ (b.to_matrix pb.basis ⬝ pb.basis.to_matrix b)ᵀ ⬝ b.to_matrix pb.basis).det : by simp only [← det_mul, matrix.mul_assoc, matrix.transpose_mul] ... = 1 : by simp only [basis.to_matrix_mul_to_matrix_flip, matrix.transpose_one, matrix.mul_one, matrix.det_one] }, simpa only [trace_matrix_of_basis] using det_trace_matrix_ne_zero' pb end variables (K L) theorem trace_form_nondegenerate [finite_dimensional K L] [is_separable K L] : (trace_form K L).nondegenerate := bilin_form.nondegenerate_of_det_ne_zero (trace_form K L) _ (det_trace_form_ne_zero (finite_dimensional.fin_basis K L)) end det_ne_zero
1c91a32731c6c8d7185f70030e24f45ad98b30ee
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/algebra/order_functions.lean
0d851a82e276f7a06209428c2b6e1f74cfbf34c8
[ "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
5,119
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import algebra.order 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 : α → β} {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 max_le_iff : max a b ≤ c ↔ a ≤ c ∧ b ≤ c := sup_le_iff lemma max_le_max : a ≤ c → b ≤ d → max a b ≤ max c d := sup_le_sup lemma min_le_min : a ≤ c → b ≤ d → min a b ≤ min c d := inf_le_inf lemma le_max_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 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 (order_dual α) _ a b c /-- 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 @[simp] lemma max_lt_iff : max a b < c ↔ (a < c ∧ b < c) := sup_lt_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 := @lt_max_iff (order_dual α) _ _ _ _ @[simp] lemma min_le_iff : min a b ≤ c ↔ a ≤ c ∨ b ≤ c := inf_le_iff @[simp] lemma le_max_iff : a ≤ max b c ↔ a ≤ b ∨ a ≤ c := @min_le_iff (order_dual α) _ _ _ _ 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 (order_dual α) _ _ _ _ _ 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.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.order_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 (order_dual α) _ 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
33736b7a74735745c8bccbaefc19d7d840a94929
38bf3fd2bb651ab70511408fcf70e2029e2ba310
/src/category_theory/monad/limits.lean
30383f13b4b8a193a0d6f94b783e8f0fc02156e4
[ "Apache-2.0" ]
permissive
JaredCorduan/mathlib
130392594844f15dad65a9308c242551bae6cd2e
d5de80376088954d592a59326c14404f538050a1
refs/heads/master
1,595,862,206,333
1,570,816,457,000
1,570,816,457,000
209,134,499
0
0
Apache-2.0
1,568,746,811,000
1,568,746,811,000
null
UTF-8
Lean
false
false
4,698
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.monad.adjunction import category_theory.adjunction.limits namespace category_theory open category open category_theory.limits universes v₁ v₂ u₁ u₂ -- declare the `v`'s first; see `category_theory.category` for an explanation namespace monad variables {C : Type u₁} [𝒞 : category.{v₁} C] include 𝒞 variables {T : C ⥤ C} [monad.{v₁} T] variables {J : Type v₁} [𝒥 : small_category J] include 𝒥 namespace forget_creates_limits variables (D : J ⥤ algebra T) [has_limit.{v₁} (D ⋙ forget T)] def γ : (D ⋙ forget T ⋙ T) ⟶ (D ⋙ forget T) := { app := λ j, (D.obj j).a } @[simp] lemma γ_app (j) : (γ D).app j = (D.obj j).a := rfl def c : cone (D ⋙ forget T) := { X := T.obj (limit (D ⋙ forget T)), π := (functor.const_comp _ _ T).inv ≫ whisker_right (limit.cone (D ⋙ forget T)).π T ≫ (γ D) } @[simp] lemma c_π (j) : (c D).π.app j = 𝟙 _ ≫ T.map (limit.π (D ⋙ forget T) j) ≫ (D.obj j).a := rfl def cone_point (D : J ⥤ algebra T) [has_limit.{v₁} (D ⋙ forget T)] : algebra T := { A := limit (D ⋙ forget T), a := limit.lift _ (c D), unit' := begin ext1, rw [category.assoc, limit.lift_π], dsimp, erw [id_comp, ←category.assoc, ←nat_trans.naturality, id_comp, category.assoc, algebra.unit, comp_id], refl, end, assoc' := begin ext1, dsimp, simp only [limit.lift_π, γ_app, c_π, limit.cone_π, id_comp, functor.const_comp, whisker_right.app, nat_trans.comp_app, category.assoc], conv { to_rhs, rw [←category.assoc, ←T.map_comp, limit.lift_π], dsimp [c], rw [id_comp], }, conv { to_lhs, rw [←category.assoc, ←nat_trans.naturality, category.assoc], erw [algebra.assoc (D.obj j), ←category.assoc, ←T.map_comp], }, end } @[simp] lemma cone_point_a (D : J ⥤ algebra T) [has_limit.{v₁} (D ⋙ forget T)] : (cone_point D).a = limit.lift _ ( let μ := limit.cone (D ⋙ forget T) in { X := T.obj μ.X, π := (functor.const_comp _ _ T).inv ≫ whisker_right μ.π T ≫ (γ D) }) := rfl end forget_creates_limits -- Theorem 5.6.5 from [Riehl][riehl2017] def forget_creates_limits (D : J ⥤ algebra T) [has_limit.{v₁} (D ⋙ forget T)] : has_limit D := { cone := { X := forget_creates_limits.cone_point D, π := { app := λ j, { f := limit.π (D ⋙ forget T) j }, naturality' := λ X Y f, by { ext, dsimp, erw [id_comp, limit.w] } } }, is_limit := { lift := λ s, { f := limit.lift _ ((forget T).map_cone s), h' := begin ext, dsimp, simp only [limit.lift_π, limit.cone_π, forget_map, id_comp, functor.const_comp, whisker_right.app, nat_trans.comp_app, category.assoc, functor.map_cone_π], dsimp, rw [id_comp, ←category.assoc, ←T.map_comp], simp only [limit.lift_π, monad.forget_map, algebra.hom.h, functor.map_cone_π], end }, uniq' := λ s m w, by { ext1, ext1, simpa using congr_arg algebra.hom.f (w j) } } } end monad variables {C : Type u₁} [𝒞 : category.{v₁} C] {D : Type u₁} [𝒟 : category.{v₁} D] include 𝒞 𝒟 variables {J : Type v₁} [𝒥 : small_category J] include 𝒥 instance comp_comparison_forget_has_limit (F : J ⥤ D) (R : D ⥤ C) [monadic_right_adjoint R] [has_limit.{v₁} (F ⋙ R)] : has_limit ((F ⋙ monad.comparison R) ⋙ monad.forget ((left_adjoint R) ⋙ R)) := (@has_limit_of_iso _ _ _ _ (F ⋙ R) _ _ (iso_whisker_left F (monad.comparison_forget R).symm)) instance comp_comparison_has_limit (F : J ⥤ D) (R : D ⥤ C) [monadic_right_adjoint R] [has_limit.{v₁} (F ⋙ R)] : has_limit (F ⋙ monad.comparison R) := monad.forget_creates_limits (F ⋙ monad.comparison R) def monadic_creates_limits (F : J ⥤ D) (R : D ⥤ C) [monadic_right_adjoint R] [has_limit.{v₁} (F ⋙ R)] : has_limit F := adjunction.has_limit_of_comp_equivalence _ (monad.comparison R) omit 𝒥 section def has_limits_of_reflective (R : D ⥤ C) [reflective R] [has_limits.{v₁} C] : has_limits.{v₁} D := { has_limits_of_shape := λ J 𝒥, by exactI { has_limit := λ F, monadic_creates_limits F R } } local attribute [instance] has_limits_of_reflective include 𝒥 -- We verify that, even jumping through these monadic hoops, -- the limit is actually calculated in the obvious way: example (R : D ⥤ C) [reflective R] [has_limits.{v₁} C] (F : J ⥤ D) : limit F = (left_adjoint R).obj (limit (F ⋙ R)) := rfl end end category_theory
feb8fd10f4a6f1c94319c92fe23e231f025c46ed
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/topology/metric_space/hausdorff_distance.lean
ad1ea0644a4e024849d1c504ce1c67fbf929260a
[ "Apache-2.0" ]
permissive
ChrisHughes24/mathlib
98322577c460bc6b1fe5c21f42ce33ad1c3e5558
a2a867e827c2a6702beb9efc2b9282bd801d5f9a
refs/heads/master
1,583,848,251,477
1,565,164,247,000
1,565,164,247,000
129,409,993
0
1
Apache-2.0
1,565,164,817,000
1,523,628,059,000
Lean
UTF-8
Lean
false
false
32,321
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Sébastien Gouëzel The Hausdorff distance on subsets of a metric (or emetric) space. Given two subsets `s` and `t` of a metric space, their Hausdorff distance is the smallest `d` such that any point `s` is within `d` of a point in `t`, and conversely. This quantity is often infinite (think of `s` bounded and `t` unbounded), and therefore better expressed in the setting of emetric spaces. This files introduces: * `inf_edist x s`, the infimum edistance of a point `x` to a set `s` in an emetric space * `Hausdorff_edist s t`, the Hausdorff edistance of two sets in an emetric space * Versions of these notions on metric spaces, called respectively `inf_dist` and `Hausdorff_dist`. -/ import topology.metric_space.isometry topology.instances.ennreal topology.metric_space.lipschitz noncomputable theory local attribute [instance, priority 0] classical.prop_decidable universes u v w open classical lattice set function topological_space filter namespace emetric section inf_edist local notation `∞` := (⊤ : ennreal) variables {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {x y : α} {s t : set α} {Φ : α → β} /-- The minimal edistance of a point to a set -/ def inf_edist (x : α) (s : set α) : ennreal := Inf ((edist x) '' s) @[simp] lemma inf_edist_empty : inf_edist x ∅ = ∞ := by unfold inf_edist; simp /-- The edist to a union is the minimum of the edists -/ @[simp] lemma inf_edist_union : inf_edist x (s ∪ t) = inf_edist x s ⊓ inf_edist x t := by simp [inf_edist, image_union, Inf_union] /-- The edist to a singleton is the edistance to the single point of this singleton -/ @[simp] lemma inf_edist_singleton : inf_edist x {y} = edist x y := by simp [inf_edist] /-- The edist to a set is bounded above by the edist to any of its points -/ lemma inf_edist_le_edist_of_mem (h : y ∈ s) : inf_edist x s ≤ edist x y := Inf_le ((mem_image _ _ _).2 ⟨y, h, by refl⟩) /-- If a point `x` belongs to `s`, then its edist to `s` vanishes -/ lemma inf_edist_zero_of_mem (h : x ∈ s) : inf_edist x s = 0 := le_zero_iff_eq.1 $ @edist_self _ _ x ▸ inf_edist_le_edist_of_mem h /-- The edist is monotonous with respect to inclusion -/ lemma inf_edist_le_inf_edist_of_subset (h : s ⊆ t) : inf_edist x t ≤ inf_edist x s := Inf_le_Inf (image_subset _ h) /-- If the edist to a set is `< r`, there exists a point in the set at edistance `< r` -/ lemma exists_edist_lt_of_inf_edist_lt {r : ennreal} (h : inf_edist x s < r) : ∃y∈s, edist x y < r := let ⟨t, ⟨ht, tr⟩⟩ := Inf_lt_iff.1 h in let ⟨y, ⟨ys, hy⟩⟩ := (mem_image _ _ _).1 ht in ⟨y, ys, by rwa ← hy at tr⟩ /-- The edist of `x` to `s` is bounded by the sum of the edist of `y` to `s` and the edist from `x` to `y` -/ lemma inf_edist_le_inf_edist_add_edist : inf_edist x s ≤ inf_edist y s + edist x y := begin have : ∀z ∈ s, Inf (edist x '' s) ≤ edist y z + edist x y := λz hz, calc Inf (edist x '' s) ≤ edist x z : Inf_le ((mem_image _ _ _).2 ⟨z, hz, by refl⟩) ... ≤ edist x y + edist y z : edist_triangle _ _ _ ... = edist y z + edist x y : add_comm _ _, have : (λz, z + edist x y) (Inf (edist y '' s)) = Inf ((λz, z + edist x y) '' (edist y '' s)), { refine Inf_of_continuous _ _ (by simp), { exact continuous_add continuous_id continuous_const }, { assume a b h, simp, apply add_le_add_right' h }}, simp only [inf_edist] at this, rw [inf_edist, inf_edist, this, ← image_comp], simpa only [and_imp, function.comp_app, lattice.le_Inf_iff, exists_imp_distrib, ball_image_iff] end /-- The edist to a set depends continuously on the point -/ lemma continuous_inf_edist : continuous (λx, inf_edist x s) := continuous_of_le_add_edist 1 (by simp) $ by simp only [one_mul, inf_edist_le_inf_edist_add_edist, forall_2_true_iff] /-- The edist to a set and to its closure coincide -/ lemma inf_edist_closure : inf_edist x (closure s) = inf_edist x s := begin refine le_antisymm (inf_edist_le_inf_edist_of_subset subset_closure) _, refine ennreal.le_of_forall_epsilon_le (λε εpos h, _), have εpos' : (0 : ennreal) < ε := by simpa, have : inf_edist x (closure s) < inf_edist x (closure s) + ε/2 := ennreal.lt_add_right h (ennreal.half_pos εpos'), rcases exists_edist_lt_of_inf_edist_lt this with ⟨y, ycs, hy⟩, -- y : α, ycs : y ∈ closure s, hy : edist x y < inf_edist x (closure s) + ↑ε / 2 rcases emetric.mem_closure_iff'.1 ycs (ε/2) (ennreal.half_pos εpos') with ⟨z, zs, dyz⟩, -- z : α, zs : z ∈ s, dyz : edist y z < ↑ε / 2 calc inf_edist x s ≤ edist x z : inf_edist_le_edist_of_mem zs ... ≤ edist x y + edist y z : edist_triangle _ _ _ ... ≤ (inf_edist x (closure s) + ε / 2) + (ε/2) : add_le_add' (le_of_lt hy) (le_of_lt dyz) ... = inf_edist x (closure s) + ↑ε : by simp [ennreal.add_halves] end /-- A point belongs to the closure of `s` iff its infimum edistance to this set vanishes -/ lemma mem_closure_iff_inf_edist_zero : x ∈ closure s ↔ inf_edist x s = 0 := ⟨λh, by rw ← inf_edist_closure; exact inf_edist_zero_of_mem h, λh, emetric.mem_closure_iff'.2 $ λε εpos, exists_edist_lt_of_inf_edist_lt (by rwa h)⟩ /-- Given a closed set `s`, a point belongs to `s` iff its infimum edistance to this set vanishes -/ lemma mem_iff_ind_edist_zero_of_closed (h : is_closed s) : x ∈ s ↔ inf_edist x s = 0 := begin convert ← mem_closure_iff_inf_edist_zero, exact closure_eq_iff_is_closed.2 h end /-- The infimum edistance is invariant under isometries -/ lemma inf_edist_image (hΦ : isometry Φ) : inf_edist (Φ x) (Φ '' t) = inf_edist x t := begin simp only [inf_edist], apply congr_arg, ext b, split, { assume hb, rcases (mem_image _ _ _).1 hb with ⟨y, ⟨hy, hy'⟩⟩, rcases (mem_image _ _ _).1 hy with ⟨z, ⟨hz, hz'⟩⟩, rw [← hy', ← hz', hΦ x z], exact mem_image_of_mem _ hz }, { assume hb, rcases (mem_image _ _ _).1 hb with ⟨y, ⟨hy, hy'⟩⟩, rw [← hy', ← hΦ x y], exact mem_image_of_mem _ (mem_image_of_mem _ hy) } end end inf_edist --section /-- The Hausdorff edistance between two sets is the smallest `r` such that each set is contained in the `r`-neighborhood of the other one -/ def Hausdorff_edist {α : Type u} [emetric_space α] (s t : set α) : ennreal := Sup ((λx, inf_edist x t) '' s) ⊔ Sup ((λx, inf_edist x s) '' t) lemma Hausdorff_edist_def {α : Type u} [emetric_space α] (s t : set α) : Hausdorff_edist s t = Sup ((λx, inf_edist x t) '' s) ⊔ Sup ((λx, inf_edist x s) '' t) := rfl attribute [irreducible] Hausdorff_edist section Hausdorff_edist local notation `∞` := (⊤ : ennreal) variables {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {x y : α} {s t u : set α} {Φ : α → β} /-- The Hausdorff edistance of a set to itself vanishes -/ @[simp] lemma Hausdorff_edist_self : Hausdorff_edist s s = 0 := begin erw [Hausdorff_edist_def, lattice.sup_idem, ← le_bot_iff], apply Sup_le _, simp [le_bot_iff, inf_edist_zero_of_mem] {contextual := tt}, end /-- The Haudorff edistances of `s` to `t` and of `t` to `s` coincide -/ lemma Hausdorff_edist_comm : Hausdorff_edist s t = Hausdorff_edist t s := by unfold Hausdorff_edist; apply sup_comm /-- Bounding the Hausdorff edistance by bounding the edistance of any point in each set to the other set -/ lemma Hausdorff_edist_le_of_inf_edist {r : ennreal} (H1 : ∀x ∈ s, inf_edist x t ≤ r) (H2 : ∀x ∈ t, inf_edist x s ≤ r) : Hausdorff_edist s t ≤ r := begin simp only [Hausdorff_edist, -mem_image, set.ball_image_iff, lattice.Sup_le_iff, lattice.sup_le_iff], exact ⟨H1, H2⟩ end /-- Bounding the Hausdorff edistance by exhibiting, for any point in each set, another point in the other set at controlled distance -/ lemma Hausdorff_edist_le_of_mem_edist {r : ennreal} (H1 : ∀x ∈ s, ∃y ∈ t, edist x y ≤ r) (H2 : ∀x ∈ t, ∃y ∈ s, edist x y ≤ r) : Hausdorff_edist s t ≤ r := begin refine Hausdorff_edist_le_of_inf_edist _ _, { assume x xs, rcases H1 x xs with ⟨y, yt, hy⟩, exact le_trans (inf_edist_le_edist_of_mem yt) hy }, { assume x xt, rcases H2 x xt with ⟨y, ys, hy⟩, exact le_trans (inf_edist_le_edist_of_mem ys) hy } end /-- The distance to a set is controlled by the Hausdorff distance -/ lemma inf_edist_le_Hausdorff_edist_of_mem (h : x ∈ s) : inf_edist x t ≤ Hausdorff_edist s t := begin rw Hausdorff_edist_def, refine le_trans (le_Sup _) le_sup_left, exact mem_image_of_mem _ h end /-- If the Hausdorff distance is `<r`, then any point in one of the sets has a corresponding point at distance `<r` in the other set -/ lemma exists_edist_lt_of_Hausdorff_edist_lt {r : ennreal} (h : x ∈ s) (H : Hausdorff_edist s t < r) : ∃y∈t, edist x y < r := exists_edist_lt_of_inf_edist_lt $ calc inf_edist x t ≤ Sup ((λx, inf_edist x t) '' s) : le_Sup (mem_image_of_mem _ h) ... ≤ Sup ((λx, inf_edist x t) '' s) ⊔ Sup ((λx, inf_edist x s) '' t) : le_sup_left ... < r : by rwa Hausdorff_edist_def at H /-- The distance from `x` to `s`or `t` is controlled in terms of the Hausdorff distance between `s` and `t` -/ lemma inf_edist_le_inf_edist_add_Hausdorff_edist : inf_edist x t ≤ inf_edist x s + Hausdorff_edist s t := ennreal.le_of_forall_epsilon_le $ λε εpos h, begin have εpos' : (0 : ennreal) < ε := by simpa, have : inf_edist x s < inf_edist x s + ε/2 := ennreal.lt_add_right (ennreal.add_lt_top.1 h).1 (ennreal.half_pos εpos'), rcases exists_edist_lt_of_inf_edist_lt this with ⟨y, ys, dxy⟩, -- y : α, ys : y ∈ s, dxy : edist x y < inf_edist x s + ↑ε / 2 have : Hausdorff_edist s t < Hausdorff_edist s t + ε/2 := ennreal.lt_add_right (ennreal.add_lt_top.1 h).2 (ennreal.half_pos εpos'), rcases exists_edist_lt_of_Hausdorff_edist_lt ys this with ⟨z, zt, dyz⟩, -- z : α, zt : z ∈ t, dyz : edist y z < Hausdorff_edist s t + ↑ε / 2 calc inf_edist x t ≤ edist x z : inf_edist_le_edist_of_mem zt ... ≤ edist x y + edist y z : edist_triangle _ _ _ ... ≤ (inf_edist x s + ε/2) + (Hausdorff_edist s t + ε/2) : add_le_add' (le_of_lt dxy) (le_of_lt dyz) ... = inf_edist x s + Hausdorff_edist s t + ε : by simp [ennreal.add_halves, add_comm] end /-- The Hausdorff edistance is invariant under eisometries -/ lemma Hausdorff_edist_image (h : isometry Φ) : Hausdorff_edist (Φ '' s) (Φ '' t) = Hausdorff_edist s t := begin unfold Hausdorff_edist, congr, { ext b, split, { assume hb, rcases (mem_image _ _ _ ).1 hb with ⟨y, ⟨hy, hy'⟩⟩, rcases (mem_image _ _ _ ).1 hy with ⟨z, ⟨hz, hz'⟩⟩, rw [← hy', ← hz', inf_edist_image h], exact mem_image_of_mem _ hz }, { assume hb, rcases (mem_image _ _ _ ).1 hb with ⟨y, ⟨hy, hy'⟩⟩, rw [← hy', ← inf_edist_image h], exact mem_image_of_mem _ (mem_image_of_mem _ hy) }}, { ext b, split, { assume hb, rcases (mem_image _ _ _ ).1 hb with ⟨y, ⟨hy, hy'⟩⟩, rcases (mem_image _ _ _ ).1 hy with ⟨z, ⟨hz, hz'⟩⟩, rw [← hy', ← hz', inf_edist_image h], exact mem_image_of_mem _ hz }, { assume hb, rcases (mem_image _ _ _ ).1 hb with ⟨y, ⟨hy, hy'⟩⟩, rw [← hy', ← inf_edist_image h], exact mem_image_of_mem _ (mem_image_of_mem _ hy) }} end /-- The Hausdorff distance is controlled by the diameter of the union -/ lemma Hausdorff_edist_le_ediam (hs : s ≠ ∅) (ht : t ≠ ∅) : Hausdorff_edist s t ≤ diam (s ∪ t) := begin rcases ne_empty_iff_exists_mem.1 hs with ⟨x, xs⟩, rcases ne_empty_iff_exists_mem.1 ht with ⟨y, yt⟩, refine Hausdorff_edist_le_of_mem_edist _ _, { exact λz hz, ⟨y, yt, edist_le_diam_of_mem (subset_union_left _ _ hz) (subset_union_right _ _ yt)⟩ }, { exact λz hz, ⟨x, xs, edist_le_diam_of_mem (subset_union_right _ _ hz) (subset_union_left _ _ xs)⟩ } end /-- The Hausdorff distance satisfies the triangular inequality -/ lemma Hausdorff_edist_triangle : Hausdorff_edist s u ≤ Hausdorff_edist s t + Hausdorff_edist t u := begin rw Hausdorff_edist_def, simp only [and_imp, set.mem_image, lattice.Sup_le_iff, exists_imp_distrib, lattice.sup_le_iff, -mem_image, set.ball_image_iff], split, show ∀x ∈ s, inf_edist x u ≤ Hausdorff_edist s t + Hausdorff_edist t u, from λx xs, calc inf_edist x u ≤ inf_edist x t + Hausdorff_edist t u : inf_edist_le_inf_edist_add_Hausdorff_edist ... ≤ Hausdorff_edist s t + Hausdorff_edist t u : add_le_add_right' (inf_edist_le_Hausdorff_edist_of_mem xs), show ∀x ∈ u, inf_edist x s ≤ Hausdorff_edist s t + Hausdorff_edist t u, from λx xu, calc inf_edist x s ≤ inf_edist x t + Hausdorff_edist t s : inf_edist_le_inf_edist_add_Hausdorff_edist ... ≤ Hausdorff_edist u t + Hausdorff_edist t s : add_le_add_right' (inf_edist_le_Hausdorff_edist_of_mem xu) ... = Hausdorff_edist s t + Hausdorff_edist t u : by simp [Hausdorff_edist_comm, add_comm] end /-- The Hausdorff edistance between a set and its closure vanishes -/ @[simp] lemma Hausdorff_edist_self_closure : Hausdorff_edist s (closure s) = 0 := begin erw ← le_bot_iff, simp only [Hausdorff_edist, inf_edist_closure, -le_zero_iff_eq, and_imp, set.mem_image, lattice.Sup_le_iff, exists_imp_distrib, lattice.sup_le_iff, set.ball_image_iff, ennreal.bot_eq_zero, -mem_image], simp only [inf_edist_zero_of_mem, mem_closure_iff_inf_edist_zero, le_refl, and_self, forall_true_iff] {contextual := tt} end /-- Replacing a set by its closure does not change the Hausdorff edistance. -/ @[simp] lemma Hausdorff_edist_closure₁ : Hausdorff_edist (closure s) t = Hausdorff_edist s t := begin refine le_antisymm _ _, { calc _ ≤ Hausdorff_edist (closure s) s + Hausdorff_edist s t : Hausdorff_edist_triangle ... = Hausdorff_edist s t : by simp [Hausdorff_edist_comm] }, { calc _ ≤ Hausdorff_edist s (closure s) + Hausdorff_edist (closure s) t : Hausdorff_edist_triangle ... = Hausdorff_edist (closure s) t : by simp } end /-- Replacing a set by its closure does not change the Hausdorff edistance. -/ @[simp] lemma Hausdorff_edist_closure₂ : Hausdorff_edist s (closure t) = Hausdorff_edist s t := by simp [@Hausdorff_edist_comm _ _ s _] /-- The Hausdorff edistance between sets or their closures is the same -/ @[simp] lemma Hausdorff_edist_closure : Hausdorff_edist (closure s) (closure t) = Hausdorff_edist s t := by simp /-- Two sets are at zero Hausdorff edistance if and only if they have the same closure -/ lemma Hausdorff_edist_zero_iff_closure_eq_closure : Hausdorff_edist s t = 0 ↔ closure s = closure t := ⟨begin assume h, refine subset.antisymm _ _, { have : s ⊆ closure t := λx xs, mem_closure_iff_inf_edist_zero.2 $ begin erw ← le_bot_iff, have := @inf_edist_le_Hausdorff_edist_of_mem _ _ _ _ t xs, rwa h at this, end, by rw ← @closure_closure _ _ t; exact closure_mono this }, { have : t ⊆ closure s := λx xt, mem_closure_iff_inf_edist_zero.2 $ begin erw ← le_bot_iff, have := @inf_edist_le_Hausdorff_edist_of_mem _ _ _ _ s xt, rw Hausdorff_edist_comm at h, rwa h at this, end, by rw ← @closure_closure _ _ s; exact closure_mono this } end, λh, by rw [← Hausdorff_edist_closure, h, Hausdorff_edist_self]⟩ /-- Two closed sets are at zero Hausdorff edistance if and only if they coincide -/ lemma Hausdorff_edist_zero_iff_eq_of_closed (hs : is_closed s) (ht : is_closed t) : Hausdorff_edist s t = 0 ↔ s = t := by rw [Hausdorff_edist_zero_iff_closure_eq_closure, closure_eq_iff_is_closed.2 hs, closure_eq_iff_is_closed.2 ht] /-- The Haudorff edistance to the empty set is infinite -/ lemma Hausdorff_edist_empty (ne : s ≠ ∅) : Hausdorff_edist s ∅ = ∞ := begin rcases exists_mem_of_ne_empty ne with ⟨x, xs⟩, have : inf_edist x ∅ ≤ Hausdorff_edist s ∅ := inf_edist_le_Hausdorff_edist_of_mem xs, simpa using this, end /-- If a set is at finite Hausdorff edistance of a nonempty set, it is nonempty -/ lemma ne_empty_of_Hausdorff_edist_ne_top (hs : s ≠ ∅) (fin : Hausdorff_edist s t ≠ ⊤) : t ≠ ∅ := begin by_contradiction h, simp only [not_not, ne.def] at h, rw [h, Hausdorff_edist_empty hs] at fin, simpa using fin end end Hausdorff_edist -- section end emetric --namespace /-Now, we turn to the same notions in metric spaces. To avoid the difficulties related to Inf and Sup on ℝ (which is only conditionnally complete), we use the notions in ennreal formulated in terms of the edistance, and coerce them to ℝ. Then their properties follow readily from the corresponding properties in ennreal, modulo some tedious rewriting of inequalities from one to the other -/ namespace metric section variables {α : Type u} {β : Type v} [metric_space α] [metric_space β] {s t u : set α} {x y : α} {Φ : α → β} open emetric /-- The minimal distance of a point to a set -/ def inf_dist (x : α) (s : set α) : ℝ := ennreal.to_real (inf_edist x s) /-- the minimal distance is always nonnegative -/ lemma inf_dist_nonneg : 0 ≤ inf_dist x s := by simp [inf_dist] /-- the minimal distance to the empty set is 0 (if you want to have the more reasonable value ∞ instead, use `inf_edist`, which takes values in ennreal) -/ @[simp] lemma inf_dist_empty : inf_dist x ∅ = 0 := by simp [inf_dist] /-- In a metric space, the minimal edistance to a nonempty set is finite -/ lemma inf_edist_ne_top (h : s ≠ ∅) : inf_edist x s ≠ ⊤ := begin rcases exists_mem_of_ne_empty h with ⟨y, hy⟩, apply lt_top_iff_ne_top.1, calc inf_edist x s ≤ edist x y : inf_edist_le_edist_of_mem hy ... < ⊤ : lt_top_iff_ne_top.2 (edist_ne_top _ _) end /-- The minimal distance of a point to a set containing it vanishes -/ lemma inf_dist_zero_of_mem (h : x ∈ s) : inf_dist x s = 0 := by simp [inf_edist_zero_of_mem h, inf_dist] /-- The minimal distance to a singleton is the distance to the unique point in this singleton -/ @[simp] lemma inf_dist_singleton : inf_dist x {y} = dist x y := by simp [inf_dist, inf_edist, dist_edist] /-- The minimal distance to a set is bounded by the distance to any point in this set -/ lemma inf_dist_le_dist_of_mem (h : y ∈ s) : inf_dist x s ≤ dist x y := begin rw [dist_edist, inf_dist, ennreal.to_real_le_to_real (inf_edist_ne_top (ne_empty_of_mem h)) (edist_ne_top _ _)], exact inf_edist_le_edist_of_mem h end /-- The minimal distance is monotonous with respect to inclusion -/ lemma inf_dist_le_inf_dist_of_subset (h : s ⊆ t) (hs : s ≠ ∅) : inf_dist x t ≤ inf_dist x s := begin rcases ne_empty_iff_exists_mem.1 hs with ⟨y, hy⟩, have ht : t ≠ ∅ := ne_empty_of_mem (h hy), rw [inf_dist, inf_dist, ennreal.to_real_le_to_real (inf_edist_ne_top ht) (inf_edist_ne_top hs)], exact inf_edist_le_inf_edist_of_subset h end /-- If the minimal distance to a set is `<r`, there exists a point in this set at distance `<r` -/ lemma exists_dist_lt_of_inf_dist_lt {r : real} (h : inf_dist x s < r) (hs : s ≠ ∅) : ∃y∈s, dist x y < r := begin have rpos : 0 < r := lt_of_le_of_lt inf_dist_nonneg h, have : inf_edist x s < ennreal.of_real r, { rwa [inf_dist, ← ennreal.to_real_of_real (le_of_lt rpos), ennreal.to_real_lt_to_real (inf_edist_ne_top hs)] at h, simp }, rcases exists_edist_lt_of_inf_edist_lt this with ⟨y, ys, hy⟩, rw [edist_dist, ennreal.of_real_lt_of_real_iff rpos] at hy, exact ⟨y, ys, hy⟩, end /-- The minimal distance from `x` to `s` is bounded by the distance from `y` to `s`, modulo the distance between `x` and `y` -/ lemma inf_dist_le_inf_dist_add_dist : inf_dist x s ≤ inf_dist y s + dist x y := begin by_cases hs : s = ∅, { by simp [hs, dist_nonneg] }, { rw [inf_dist, inf_dist, dist_edist, ← ennreal.to_real_add (inf_edist_ne_top hs) (edist_ne_top _ _), ennreal.to_real_le_to_real (inf_edist_ne_top hs)], { apply inf_edist_le_inf_edist_add_edist }, { simp [ennreal.add_eq_top, inf_edist_ne_top hs, edist_ne_top] }} end /-- The minimal distance to a set is uniformly continuous -/ lemma uniform_continuous_inf_dist : uniform_continuous (λx, inf_dist x s) := uniform_continuous_of_le_add 1 (by simp [inf_dist_le_inf_dist_add_dist]) /-- The minimal distance to a set is continuous -/ lemma continuous_inf_dist : continuous (λx, inf_dist x s) := uniform_continuous_inf_dist.continuous /-- The minimal distance to a set and its closure coincide -/ lemma inf_dist_eq_closure : inf_dist x (closure s) = inf_dist x s := by simp [inf_dist, inf_edist_closure] /-- A point belongs to the closure of `s` iff its infimum distance to this set vanishes -/ lemma mem_closure_iff_inf_dist_zero (h : s ≠ ∅) : x ∈ closure s ↔ inf_dist x s = 0 := by simp [mem_closure_iff_inf_edist_zero, inf_dist, ennreal.to_real_eq_zero_iff, inf_edist_ne_top h] /-- Given a closed set `s`, a point belongs to `s` iff its infimum distance to this set vanishes -/ lemma mem_iff_ind_dist_zero_of_closed (h : is_closed s) (hs : s ≠ ∅) : x ∈ s ↔ inf_dist x s = 0 := begin have := @mem_closure_iff_inf_dist_zero _ _ s x hs, rwa closure_eq_iff_is_closed.2 h at this end /-- The infimum distance is invariant under isometries -/ lemma inf_dist_image (hΦ : isometry Φ) : inf_dist (Φ x) (Φ '' t) = inf_dist x t := by simp [inf_dist, inf_edist_image hΦ] /-- The Hausdorff distance between two sets is the smallest nonnegative `r` such that each set is included in the `r`-neighborhood of the other. If there is no such `r`, it is defined to be `0`, arbitrarily -/ def Hausdorff_dist (s t : set α) : ℝ := ennreal.to_real (Hausdorff_edist s t) /-- The Hausdorff distance is nonnegative -/ lemma Hausdorff_dist_nonneg : 0 ≤ Hausdorff_dist s t := by simp [Hausdorff_dist] /-- If two sets are nonempty and bounded in a metric space, they are at finite Hausdorff edistance -/ lemma Hausdorff_edist_ne_top_of_ne_empty_of_bounded (hs : s ≠ ∅) (ht : t ≠ ∅) (bs : bounded s) (bt : bounded t) : Hausdorff_edist s t ≠ ⊤ := begin rcases ne_empty_iff_exists_mem.1 hs with ⟨cs, hcs⟩, rcases ne_empty_iff_exists_mem.1 ht with ⟨ct, hct⟩, rcases (bounded_iff_subset_ball ct).1 bs with ⟨rs, hrs⟩, rcases (bounded_iff_subset_ball cs).1 bt with ⟨rt, hrt⟩, have : Hausdorff_edist s t ≤ ennreal.of_real (max rs rt), { apply Hausdorff_edist_le_of_mem_edist, { assume x xs, existsi [ct, hct], have : dist x ct ≤ max rs rt := le_trans (hrs xs) (le_max_left _ _), rwa [edist_dist, ennreal.of_real_le_of_real_iff], exact le_trans dist_nonneg this }, { assume x xt, existsi [cs, hcs], have : dist x cs ≤ max rs rt := le_trans (hrt xt) (le_max_right _ _), rwa [edist_dist, ennreal.of_real_le_of_real_iff], exact le_trans dist_nonneg this }}, exact ennreal.lt_top_iff_ne_top.1 (lt_of_le_of_lt this (by simp [lt_top_iff_ne_top])) end /-- The Hausdorff distance between a set and itself is zero -/ @[simp] lemma Hausdorff_dist_self_zero : Hausdorff_dist s s = 0 := by simp [Hausdorff_dist] /-- The Hausdorff distance from `s` to `t` and from `t` to `s` coincide -/ lemma Hausdorff_dist_comm : Hausdorff_dist s t = Hausdorff_dist t s := by simp [Hausdorff_dist, Hausdorff_edist_comm] /-- The Hausdorff distance to the empty set vanishes (if you want to have the more reasonable value ∞ instead, use `Hausdorff_edist`, which takes values in ennreal) -/ @[simp] lemma Hausdorff_dist_empty : Hausdorff_dist s ∅ = 0 := begin by_cases h : s = ∅, { simp [h] }, { simp [Hausdorff_dist, Hausdorff_edist_empty h] } end /-- The Hausdorff distance to the empty set vanishes (if you want to have the more reasonable value ∞ instead, use `Hausdorff_edist`, which takes values in ennreal) -/ @[simp] lemma Hausdorff_dist_empty' : Hausdorff_dist ∅ s = 0 := by simp [Hausdorff_dist_comm] /-- Bounding the Hausdorff distance by bounding the distance of any point in each set to the other set -/ lemma Hausdorff_dist_le_of_inf_dist {r : ℝ} (hr : r ≥ 0) (H1 : ∀x ∈ s, inf_dist x t ≤ r) (H2 : ∀x ∈ t, inf_dist x s ≤ r) : Hausdorff_dist s t ≤ r := begin by_cases h : (Hausdorff_edist s t = ⊤) ∨ (s = ∅) ∨ (t = ∅), { rcases h with h1 | h2 | h3, { simpa [Hausdorff_dist, h1] }, { simpa [h2] }, { simpa [h3] }}, { simp only [not_or_distrib] at h, have : Hausdorff_edist s t ≤ ennreal.of_real r, { apply Hausdorff_edist_le_of_inf_edist _ _, { assume x hx, have I := H1 x hx, rwa [inf_dist, ← ennreal.to_real_of_real hr, ennreal.to_real_le_to_real (inf_edist_ne_top h.2.2) ennreal.of_real_ne_top] at I }, { assume x hx, have I := H2 x hx, rwa [inf_dist, ← ennreal.to_real_of_real hr, ennreal.to_real_le_to_real (inf_edist_ne_top h.2.1) ennreal.of_real_ne_top] at I }}, rwa [Hausdorff_dist, ← ennreal.to_real_of_real hr, ennreal.to_real_le_to_real h.1 ennreal.of_real_ne_top] } end /-- Bounding the Hausdorff distance by exhibiting, for any point in each set, another point in the other set at controlled distance -/ lemma Hausdorff_dist_le_of_mem_dist {r : ℝ} (hr : 0 ≤ r) (H1 : ∀x ∈ s, ∃y ∈ t, dist x y ≤ r) (H2 : ∀x ∈ t, ∃y ∈ s, dist x y ≤ r) : Hausdorff_dist s t ≤ r := begin apply Hausdorff_dist_le_of_inf_dist hr, { assume x xs, rcases H1 x xs with ⟨y, yt, hy⟩, exact le_trans (inf_dist_le_dist_of_mem yt) hy }, { assume x xt, rcases H2 x xt with ⟨y, ys, hy⟩, exact le_trans (inf_dist_le_dist_of_mem ys) hy } end /-- The Hausdorff distance is controlled by the diameter of the union -/ lemma Hausdorff_dist_le_diam (hs : s ≠ ∅) (bs : bounded s) (ht : t ≠ ∅) (bt : bounded t) : Hausdorff_dist s t ≤ diam (s ∪ t) := begin rcases ne_empty_iff_exists_mem.1 hs with ⟨x, xs⟩, rcases ne_empty_iff_exists_mem.1 ht with ⟨y, yt⟩, refine Hausdorff_dist_le_of_mem_dist diam_nonneg _ _, { exact λz hz, ⟨y, yt, dist_le_diam_of_mem (bounded_union.2 ⟨bs, bt⟩) (subset_union_left _ _ hz) (subset_union_right _ _ yt)⟩ }, { exact λz hz, ⟨x, xs, dist_le_diam_of_mem (bounded_union.2 ⟨bs, bt⟩) (subset_union_right _ _ hz) (subset_union_left _ _ xs)⟩ } end /-- The distance to a set is controlled by the Hausdorff distance -/ lemma inf_dist_le_Hausdorff_dist_of_mem (hx : x ∈ s) (fin : Hausdorff_edist s t ≠ ⊤) : inf_dist x t ≤ Hausdorff_dist s t := begin have ht : t ≠ ∅ := ne_empty_of_Hausdorff_edist_ne_top (ne_empty_of_mem hx) fin, rw [Hausdorff_dist, inf_dist, ennreal.to_real_le_to_real (inf_edist_ne_top ht) fin], exact inf_edist_le_Hausdorff_edist_of_mem hx end /-- If the Hausdorff distance is `<r`, then any point in one of the sets is at distance `<r` of a point in the other set -/ lemma exists_dist_lt_of_Hausdorff_dist_lt {r : ℝ} (h : x ∈ s) (H : Hausdorff_dist s t < r) (fin : Hausdorff_edist s t ≠ ⊤) : ∃y∈t, dist x y < r := begin have r0 : 0 < r := lt_of_le_of_lt (Hausdorff_dist_nonneg) H, have : Hausdorff_edist s t < ennreal.of_real r, by rwa [Hausdorff_dist, ← ennreal.to_real_of_real (le_of_lt r0), ennreal.to_real_lt_to_real fin (ennreal.of_real_ne_top)] at H, rcases exists_edist_lt_of_Hausdorff_edist_lt h this with ⟨y, hy, yr⟩, rw [edist_dist, ennreal.of_real_lt_of_real_iff r0] at yr, exact ⟨y, hy, yr⟩ end /-- If the Hausdorff distance is `<r`, then any point in one of the sets is at distance `<r` of a point in the other set -/ lemma exists_dist_lt_of_Hausdorff_dist_lt' {r : ℝ} (h : y ∈ t) (H : Hausdorff_dist s t < r) (fin : Hausdorff_edist s t ≠ ⊤) : ∃x∈s, dist x y < r := begin rw Hausdorff_dist_comm at H, rw Hausdorff_edist_comm at fin, simpa [dist_comm] using exists_dist_lt_of_Hausdorff_dist_lt h H fin end /-- The infimum distance to `s` and `t` are the same, up to the Hausdorff distance between `s` and `t` -/ lemma inf_dist_le_inf_dist_add_Hausdorff_dist (fin : Hausdorff_edist s t ≠ ⊤) : inf_dist x t ≤ inf_dist x s + Hausdorff_dist s t := begin by_cases (s = ∅) ∨ (t = ∅), { rcases h with h1 |h2, { have : t = ∅, { by_contradiction ht, rw Hausdorff_edist_comm at fin, exact ne_empty_of_Hausdorff_edist_ne_top ht fin h1 }, simp [‹s = ∅›, ‹t = ∅›] }, { have : s = ∅, { by_contradiction hs, exact ne_empty_of_Hausdorff_edist_ne_top hs fin h2 }, simp [‹s = ∅›, ‹t = ∅›] }}, { rw not_or_distrib at h, rw [inf_dist, inf_dist, Hausdorff_dist, ← ennreal.to_real_add (inf_edist_ne_top h.1) fin, ennreal.to_real_le_to_real (inf_edist_ne_top h.2)], { exact inf_edist_le_inf_edist_add_Hausdorff_edist }, { simp [ennreal.add_eq_top, not_or_distrib, fin, inf_edist_ne_top h.1] }} end /-- The Hausdorff distance is invariant under isometries -/ lemma Hausdorff_dist_image (h : isometry Φ) : Hausdorff_dist (Φ '' s) (Φ '' t) = Hausdorff_dist s t := by simp [Hausdorff_dist, Hausdorff_edist_image h] /-- The Hausdorff distance satisfies the triangular inequality -/ lemma Hausdorff_dist_triangle (fin : Hausdorff_edist s t ≠ ⊤) : Hausdorff_dist s u ≤ Hausdorff_dist s t + Hausdorff_dist t u := begin by_cases Hausdorff_edist s u = ⊤, { calc Hausdorff_dist s u = 0 + 0 : by simp [Hausdorff_dist, h] ... ≤ Hausdorff_dist s t + Hausdorff_dist t u : add_le_add (Hausdorff_dist_nonneg) (Hausdorff_dist_nonneg) }, { have Dtu : Hausdorff_edist t u < ⊤ := calc Hausdorff_edist t u ≤ Hausdorff_edist t s + Hausdorff_edist s u : Hausdorff_edist_triangle ... = Hausdorff_edist s t + Hausdorff_edist s u : by simp [Hausdorff_edist_comm] ... < ⊤ : by simp [ennreal.add_lt_top]; simp [ennreal.lt_top_iff_ne_top, h, fin], rw [Hausdorff_dist, Hausdorff_dist, Hausdorff_dist, ← ennreal.to_real_add fin (lt_top_iff_ne_top.1 Dtu), ennreal.to_real_le_to_real h], { exact Hausdorff_edist_triangle }, { simp [ennreal.add_eq_top, lt_top_iff_ne_top.1 Dtu, fin] }} end /-- The Hausdorff distance satisfies the triangular inequality -/ lemma Hausdorff_dist_triangle' (fin : Hausdorff_edist t u ≠ ⊤) : Hausdorff_dist s u ≤ Hausdorff_dist s t + Hausdorff_dist t u := begin rw Hausdorff_edist_comm at fin, have I : Hausdorff_dist u s ≤ Hausdorff_dist u t + Hausdorff_dist t s := Hausdorff_dist_triangle fin, simpa [add_comm, Hausdorff_dist_comm] using I end /-- The Hausdorff distance between a set and its closure vanish -/ @[simp] lemma Hausdorff_dist_self_closure : Hausdorff_dist s (closure s) = 0 := by simp [Hausdorff_dist] /-- Replacing a set by its closure does not change the Hausdorff distance. -/ @[simp] lemma Hausdorff_dist_closure₁ : Hausdorff_dist (closure s) t = Hausdorff_dist s t := by simp [Hausdorff_dist] /-- Replacing a set by its closure does not change the Hausdorff distance. -/ @[simp] lemma Hausdorff_dist_closure₂ : Hausdorff_dist s (closure t) = Hausdorff_dist s t := by simp [Hausdorff_dist] /-- The Hausdorff distance between two sets and their closures coincide -/ @[simp] lemma Hausdorff_dist_closure : Hausdorff_dist (closure s) (closure t) = Hausdorff_dist s t := by simp [Hausdorff_dist] /-- Two sets are at zero Hausdorff distance if and only if they have the same closures -/ lemma Hausdorff_dist_zero_iff_closure_eq_closure (fin : Hausdorff_edist s t ≠ ⊤) : Hausdorff_dist s t = 0 ↔ closure s = closure t := by simp [Hausdorff_edist_zero_iff_closure_eq_closure.symm, Hausdorff_dist, ennreal.to_real_eq_zero_iff, fin] /-- Two closed sets are at zero Hausdorff distance if and only if they coincide -/ lemma Hausdorff_dist_zero_iff_eq_of_closed (hs : is_closed s) (ht : is_closed t) (fin : Hausdorff_edist s t ≠ ⊤) : Hausdorff_dist s t = 0 ↔ s = t := by simp [(Hausdorff_edist_zero_iff_eq_of_closed hs ht).symm, Hausdorff_dist, ennreal.to_real_eq_zero_iff, fin] end --section end metric --namespace
7902211ee07e8691a5db5b44bfe9870810488e03
5fbbd711f9bfc21ee168f46a4be146603ece8835
/lean/natural_number_game/multiplication/3.lean
fe7c2e8d497401c11ec85af438a5d4648c05211d
[ "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
144
lean
lemma one_mul (m : mynat) : 1 * m = m := induction m with n hn, rwa mul_zero, rw [mul_succ, hn, one_eq_succ_zero, add_succ], simp, end
d8dcfb85c83824aa75ffb7b0f679976a710c7ed2
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebra/group/to_additive_auto.lean
4e5626849cda9b959f4ce4aa718cc21ac4873ba8
[]
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
2,857
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Yury Kudryashov. -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.tactic.transform_decl import Mathlib.tactic.algebra import Mathlib.PostPort universes l namespace Mathlib /-! # Transport multiplicative to additive This file defines an attribute `to_additive` that can be used to automatically transport theorems and definitions (but not inductive types and structures) from a multiplicative theory to an additive theory. Usage information is contained in the doc string of `to_additive.attr`. ### Missing features * Automatically transport structures and other inductive types. * For structures, automatically generate theorems like `group α ↔ add_group (additive α)`. * Rewrite rules for the last part of the name that work in more cases. E.g., we can replace `monoid` with `add_monoid` etc. -/ namespace to_additive /-- An auxiliary attribute used to store the names of the additive versions of declarations that have been processed by `to_additive`. -/ /-- A command that can be used to have future uses of `to_additive` change the `src` namespace to the `tgt` namespace. For example: ``` run_cmd to_additive.map_namespace `quotient_group `quotient_add_group ``` Later uses of `to_additive` on declarations in the `quotient_group` namespace will be created in the `quotient_add_group` namespaces. -/ /-- `value_type` is the type of the arguments that can be provided to `to_additive`. `to_additive.parser` parses the provided arguments into `name` for the target and an optional doc string. -/ structure value_type where tgt : name doc : Option string /-- `add_comm_prefix x s` returns `"comm_" ++ s` if `x = tt` and `s` otherwise. -/ /-- Dictionary used by `to_additive.guess_name` to autogenerate names. -/ /-- Autogenerate target name for `to_additive`. -/ /-- Return the provided target name or autogenerate one if one was not provided. -/ /-- the parser for the arguments to `to_additive` -/ /-- Add the `aux_attr` attribute to the structure fields of `src` so that future uses of `to_additive` will map them to the corresponding `tgt` fields. -/ /-- The attribute `to_additive` can be used to automatically transport theorems and definitions (but not inductive types and structures) from a multiplicative theory to an additive theory. To use this attribute, just write: ``` @[to_additive] theorem mul_comm' {α} [comm_semigroup α] (x y : α) : x * y = y * x := comm_semigroup.mul_comm ``` This code will generate a theorem named `add_comm'`. It is also possible to manually specify the name of the new declaration, and provide a documentation string: ``` @[to_additive add_foo "add_foo doc string"] /-- foo doc string -/ end Mathlib
1ec7448fce9b82c98767e2fb9d781321f09156a5
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/analysis/analytic/linear.lean
cd667f725283e48801d6a22e811f9a7839927617
[ "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
4,257
lean
/- Copyright (c) 2021 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov -/ import analysis.analytic.basic /-! # Linear functions are analytic In this file we prove that a `continuous_linear_map` defines an analytic function with the formal power series `f x = f a + f (x - a)`. -/ variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {F : Type*} [normed_group F] [normed_space 𝕜 F] {G : Type*} [normed_group G] [normed_space 𝕜 G] open_locale topological_space classical big_operators nnreal ennreal open set filter asymptotics noncomputable theory namespace continuous_linear_map /-- Formal power series of a continuous linear map `f : E →L[𝕜] F` at `x : E`: `f y = f x + f (y - x)`. -/ @[simp] def fpower_series (f : E →L[𝕜] F) (x : E) : formal_multilinear_series 𝕜 E F | 0 := continuous_multilinear_map.curry0 𝕜 _ (f x) | 1 := (continuous_multilinear_curry_fin1 𝕜 E F).symm f | _ := 0 @[simp] lemma fpower_series_apply_add_two (f : E →L[𝕜] F) (x : E) (n : ℕ) : f.fpower_series x (n + 2) = 0 := rfl @[simp] lemma fpower_series_radius (f : E →L[𝕜] F) (x : E) : (f.fpower_series x).radius = ∞ := (f.fpower_series x).radius_eq_top_of_forall_image_add_eq_zero 2 $ λ n, rfl protected theorem has_fpower_series_on_ball (f : E →L[𝕜] F) (x : E) : has_fpower_series_on_ball f (f.fpower_series x) x ∞ := { r_le := by simp, r_pos := ennreal.coe_lt_top, has_sum := λ y _, (has_sum_nat_add_iff' 2).1 $ by simp [finset.sum_range_succ, ← sub_sub, has_sum_zero] } protected theorem has_fpower_series_at (f : E →L[𝕜] F) (x : E) : has_fpower_series_at f (f.fpower_series x) x := ⟨∞, f.has_fpower_series_on_ball x⟩ protected theorem analytic_at (f : E →L[𝕜] F) (x : E) : analytic_at 𝕜 f x := (f.has_fpower_series_at x).analytic_at /-- Reinterpret a bilinear map `f : E →L[𝕜] F →L[𝕜] G` as a multilinear map `(E × F) [×2]→L[𝕜] G`. This multilinear map is the second term in the formal multilinear series expansion of `uncurry f`. It is given by `f.uncurry_bilinear ![(x, y), (x', y')] = f x y'`. -/ def uncurry_bilinear (f : E →L[𝕜] F →L[𝕜] G) : (E × F) [×2]→L[𝕜] G := @continuous_linear_map.uncurry_left 𝕜 1 (λ _, E × F) G _ _ _ _ _ $ (↑(continuous_multilinear_curry_fin1 𝕜 (E × F) G).symm : (E × F →L[𝕜] G) →L[𝕜] _).comp $ f.bilinear_comp (fst _ _ _) (snd _ _ _) @[simp] lemma uncurry_bilinear_apply (f : E →L[𝕜] F →L[𝕜] G) (m : fin 2 → E × F) : f.uncurry_bilinear m = f (m 0).1 (m 1).2 := rfl /-- Formal multilinear series expansion of a bilinear function `f : E →L[𝕜] F →L[𝕜] G`. -/ @[simp] def fpower_series_bilinear (f : E →L[𝕜] F →L[𝕜] G) (x : E × F) : formal_multilinear_series 𝕜 (E × F) G | 0 := continuous_multilinear_map.curry0 𝕜 _ (f x.1 x.2) | 1 := (continuous_multilinear_curry_fin1 𝕜 (E × F) G).symm (f.deriv₂ x) | 2 := f.uncurry_bilinear | _ := 0 @[simp] lemma fpower_series_bilinear_radius (f : E →L[𝕜] F →L[𝕜] G) (x : E × F) : (f.fpower_series_bilinear x).radius = ∞ := (f.fpower_series_bilinear x).radius_eq_top_of_forall_image_add_eq_zero 3 $ λ n, rfl protected theorem has_fpower_series_on_ball_bilinear (f : E →L[𝕜] F →L[𝕜] G) (x : E × F) : has_fpower_series_on_ball (λ x : E × F, f x.1 x.2) (f.fpower_series_bilinear x) x ∞ := { r_le := by simp, r_pos := ennreal.coe_lt_top, has_sum := λ y _, (has_sum_nat_add_iff' 3).1 $ begin simp only [finset.sum_range_succ, finset.sum_range_one, prod.fst_add, prod.snd_add, f.map_add₂], dsimp, simp only [add_comm, sub_self, has_sum_zero] end } protected theorem has_fpower_series_at_bilinear (f : E →L[𝕜] F →L[𝕜] G) (x : E × F) : has_fpower_series_at (λ x : E × F, f x.1 x.2) (f.fpower_series_bilinear x) x := ⟨∞, f.has_fpower_series_on_ball_bilinear x⟩ protected theorem analytic_at_bilinear (f : E →L[𝕜] F →L[𝕜] G) (x : E × F) : analytic_at 𝕜 (λ x : E × F, f x.1 x.2) x := (f.has_fpower_series_at_bilinear x).analytic_at end continuous_linear_map
9fcbe933380eb4c11b732ac42c41022051b4f47e
32025d5c2d6e33ad3b6dd8a3c91e1e838066a7f7
/stage0/src/Lean/Elab/ResolveName.lean
b7d03feb70652c4290f7e09f88b8a8a68e2e79f5
[ "Apache-2.0" ]
permissive
walterhu1015/lean4
b2c71b688975177402758924eaa513475ed6ce72
2214d81e84646a905d0b20b032c89caf89c737ad
refs/heads/master
1,671,342,096,906
1,599,695,985,000
1,599,695,985,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,782
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ import Lean.Hygiene import Lean.Modifiers import Lean.Elab.Alias namespace Lean namespace Elab inductive OpenDecl | simple (ns : Name) (except : List Name) | explicit (id : Name) (declName : Name) namespace OpenDecl instance : Inhabited OpenDecl := ⟨simple Name.anonymous []⟩ instance : HasToString OpenDecl := ⟨fun decl => match decl with | explicit id decl => toString id ++ " → " ++ toString decl | simple ns ex => toString ns ++ (if ex == [] then "" else " hiding " ++ toString ex)⟩ end OpenDecl def rootNamespace := `_root_ def removeRoot (n : Name) : Name := n.replacePrefix rootNamespace Name.anonymous /- Global name resolution -/ /- Check whether `ns ++ id` is a valid namepace name and/or there are aliases names `ns ++ id`. -/ private def resolveQualifiedName (env : Environment) (ns : Name) (id : Name) : List Name := let resolvedId := ns ++ id; let resolvedIds := getAliases env resolvedId; if env.contains resolvedId && (!id.isAtomic || !isProtected env resolvedId) then resolvedId :: resolvedIds else -- Check whether environment contains the private version. That is, `_private.<module_name>.ns.id`. let resolvedIdPrv := mkPrivateName env resolvedId; if env.contains resolvedIdPrv then resolvedIdPrv :: resolvedIds else resolvedIds /- Check surrounding namespaces -/ private def resolveUsingNamespace (env : Environment) (id : Name) : Name → List Name | ns@(Name.str p _ _) => match resolveQualifiedName env ns id with | [] => resolveUsingNamespace p | resolvedIds => resolvedIds | _ => [] /- Check exact name -/ private def resolveExact (env : Environment) (id : Name) : Option Name := if id.isAtomic then none else let resolvedId := id.replacePrefix rootNamespace Name.anonymous; if env.contains resolvedId then some resolvedId else -- We also allow `_root` when accessing private declarations. -- If we change our minds, we should just replace `resolvedId` with `id` let resolvedIdPrv := mkPrivateName env resolvedId; if env.contains resolvedIdPrv then some resolvedIdPrv else none /- Check open namespaces -/ private def resolveOpenDecls (env : Environment) (id : Name) : List OpenDecl → List Name → List Name | [], resolvedIds => resolvedIds | OpenDecl.simple ns exs :: openDecls, resolvedIds => if exs.elem id then resolveOpenDecls openDecls resolvedIds else let newResolvedIds := resolveQualifiedName env ns id; resolveOpenDecls openDecls (newResolvedIds ++ resolvedIds) | OpenDecl.explicit openedId resolvedId :: openDecls, resolvedIds => let resolvedIds := if id == openedId then resolvedId :: resolvedIds else resolvedIds; resolveOpenDecls openDecls resolvedIds private def resolveGlobalNameAux (env : Environment) (ns : Name) (openDecls : List OpenDecl) (scpView : MacroScopesView) : Name → List String → List (Name × List String) | id@(Name.str p s _), projs => -- NOTE: we assume that macro scopes always belong to the projected constant, not the projections let id := { scpView with name := id }.review; match resolveUsingNamespace env id ns with | resolvedIds@(_ :: _) => resolvedIds.eraseDups.map $ fun id => (id, projs) | [] => match resolveExact env id with | some newId => [(newId, projs)] | none => let resolvedIds := if env.contains id then [id] else []; let idPrv := mkPrivateName env id; let resolvedIds := if env.contains idPrv then [idPrv] ++ resolvedIds else resolvedIds; let resolvedIds := resolveOpenDecls env id openDecls resolvedIds; let resolvedIds := getAliases env id ++ resolvedIds; match resolvedIds with | resolvedIds@(_ :: _) => resolvedIds.eraseDups.map $ fun id => (id, projs) | [] => resolveGlobalNameAux p (s::projs) | _, _ => [] def resolveGlobalName (env : Environment) (ns : Name) (openDecls : List OpenDecl) (id : Name) : List (Name × List String) := -- decode macro scopes from name before recursion let extractionResult := extractMacroScopes id; resolveGlobalNameAux env ns openDecls extractionResult extractionResult.name [] /- Namespace resolution -/ def resolveNamespaceUsingScope (env : Environment) (n : Name) : Name → Option Name | Name.anonymous => none | ns@(Name.str p _ _) => if isNamespace env (ns ++ n) then some (ns ++ n) else resolveNamespaceUsingScope p | _ => unreachable! def resolveNamespaceUsingOpenDecls (env : Environment) (n : Name) : List OpenDecl → Option Name | [] => none | OpenDecl.simple ns [] :: ds => if isNamespace env (ns ++ n) then some (ns ++ n) else resolveNamespaceUsingOpenDecls ds | _ :: ds => resolveNamespaceUsingOpenDecls ds /- Given a name `id` try to find namespace it refers to. The resolution procedure works as follows 1- If `id` is the extact name of an existing namespace, then return `id` 2- If `id` is in the scope of `namespace` commands the namespace `s_1. ... . s_n`, then return `s_1 . ... . s_i ++ n` if it is the name of an existing namespace. We search "backwards". 3- Finally, for each command `open N`, return `N ++ n` if it is the name of an existing namespace. We search "backwards" again. That is, we try the most recent `open` command first. We only consider simple `open` commands. -/ def resolveNamespace (env : Environment) (ns : Name) (openDecls : List OpenDecl) (id : Name) : Option Name := if isNamespace env id then some id else match resolveNamespaceUsingScope env id ns with | some n => some n | none => match resolveNamespaceUsingOpenDecls env id openDecls with | some n => some n | none => none end Elab end Lean
6f22e429c876cdbaf27dfc312affaa7cc7dc9e91
3c453925e3aae41b20e0567130c33a61fb1c5854
/src/vec_space.lean
86b1d2febb81b1e95c716adf5a65e233d676b309
[]
no_license
agusakov/vector_spaces
f7e272e9403a87a72b49d92ce759e4ebe9e4d55c
b23954c19b357a689e2a73e07fcf6c9e4a74713a
refs/heads/master
1,670,493,487,137
1,596,908,778,000
1,596,908,778,000
283,599,211
0
0
null
null
null
null
UTF-8
Lean
false
false
3,167
lean
import algebra.field import algebra.module universes u v w x --class has_scalar (F : Type u) (α : Type v) := (smul : F → α → α) --infixr ` • `:73 := has_scalar.smul -- modules for a ring class vec_space (F : Type u) (α : Type v) [field F] [add_comm_group α] extends has_scalar F α := (smul_add : ∀ (r : F) (x y : α), r • (x + y) = r • x + r • y) (add_smul : ∀(r s : F) (x : α), (r + s) • x = r • x + s • x) (mul_smul : ∀ (r s : F) (x : α), (r * s) • x = r • s • x) (one_smul : ∀ x : α, (1 : F) • x = x) instance set_functions (F : Type u) (S : Type v) [field F] : add_comm_group (S → F) := { add := λ f g, λ x, f x + g x, -- (f + g)(x) = f(x) + g(x) add_assoc := λ a b c, funext (λ x, add_assoc _ _ _), zero := λ x, (0 : F), zero_add := λ a, funext (λ x, zero_add (a x)), add_zero := λ a, funext (λ x, add_zero (a x)), neg := λ a, λ x, -(a x), add_left_neg := λ a, funext (λ x, neg_add_self (a x)), add_comm := λ a b, funext (λ x, add_comm (a x) (b x))} instance vec_space_pi (F : Type u) (S : Type v) [field F] : vec_space F (S → F) := { smul := λ a, λ f, λ x, a * (f x), smul_add := λ a, λ f g, funext (λ x, mul_add a (f x) (g x)), add_smul := λ a b, λ f, funext (λ x, add_mul a b (f x)), mul_smul := λ a b, λ f, funext (λ x, mul_assoc a b (f x)), one_smul := λ f, funext (λ x, one_mul (f x)) } lemma unique_add_id (α : Type v) [add_comm_group α] : ∀ x : α, (∀ b : α, x + b = b) → x = 0 := begin intros x hyp, specialize hyp 0, rw eq_comm at hyp, rw hyp, symmetry, exact add_zero x, end lemma unique_add_inv (α : Type v) [add_comm_group α] : ∀ x y : α, x + y = 0 → y = - x := begin intros x y hyp, rw ← neg_add_self x at hyp, rw add_comm (-x) x at hyp, exact add_left_cancel hyp, end lemma zero_smul_zero (F : Type u) (α : Type v) [field F] [add_comm_group α] [vec_space F α] : ∀ v : α, (0 : F) • v = 0 := begin intro v, -- 0 • v = 0 -> 0 • v + v = v -> 0 • v + 1 • v = v -> (0 + 1) • v = v -> 1 • v = v apply @add_right_cancel _ _ _ v, rw ← vec_space.one_smul v, rw ← vec_space.mul_smul, rw zero_mul, rw ← vec_space.add_smul, rw [zero_add, zero_add], end #check add_comm_group.neg lemma neg_one_mul' (F : Type u) (α : Type v) [field F] [add_comm_group α] [vec_space F α] : ∀ v : α, ((-1) : F) • v = - v := begin intro v, apply @add_right_cancel _ _ _ v, rw ← vec_space.one_smul v, rw ← vec_space.mul_smul, rw neg_add_self, rw neg_one_mul, rw ← vec_space.add_smul, rw neg_add_self, apply zero_smul_zero, end lemma smul_zero_zero (F : Type u) (α : Type v) [field F] [add_comm_group α] [vec_space F α] : ∀ a : F, a • (0 : α) = 0 := begin intro a, have hyp : (0 : α) = (a • 0) + - (a • 0) := by rw add_neg_self (a • (0 : α)), conv_rhs {rw hyp}, rw ← neg_one_mul' F _ (a • (0 : α)), rw ← vec_space.mul_smul, rw mul_comm, rw vec_space.mul_smul, rw ← vec_space.smul_add, rw neg_one_mul', rw add_neg_self, end
4dd0226b5b7b2a6cfa847e20aa57961e2ca8e3a6
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/partialIssue.lean
55dc64c0d5cd1a3ec467f1b4f36e1b5a1613ab68
[ "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
252
lean
import Lean.CoreM #eval Lean.addDecl <| .mutualDefnDecl [{ name := `False_intro levelParams := [] type := .const ``False [] value := .const `False_intro [] hints := .opaque safety := .partial }] theorem False.intro : False := False_intro
1bd3951f803ccc5c1a8d9f09e479a18f6f00b652
ea5678cc400c34ff95b661fa26d15024e27ea8cd
/gourp1.lean
bb9afe946f05be1a2b1549ec1b88c90a3a2d9390
[]
no_license
ChrisHughes24/leanstuff
dca0b5349c3ed893e8792ffbd98cbcadaff20411
9efa85f72efaccd1d540385952a6acc18fce8687
refs/heads/master
1,654,883,241,759
1,652,873,885,000
1,652,873,885,000
134,599,537
1
0
null
null
null
null
UTF-8
Lean
false
false
20,312
lean
import tactic.find data.equiv algebra.group_power data.fintype data.set.finite set_theory.cardinal data.set.lattice noncomputable theory local attribute [instance, priority 0] classical.prop_decidable classical.dec_pred local attribute [instance] set_fintype open fintype local infix `^` := gpow universe u instance {α : Type*} (p : α → Prop) [fintype α] : fintype {a // p a} := set_fintype p instance {α : Type*} [fintype α] (r : α → α → Prop) : fintype (quot r) := of_surjective (quot.mk r) (λ x, quot.exists_rep _) lemma finset.sum_const {α β : Type*} [decidable_eq α] [add_comm_monoid β] (s : finset α) (b : β) : finset.sum s (λ a, b) = add_monoid.smul b (finset.card s) := finset.induction_on s (by simp) $ λ a s h hi, by simp [finset.sum_insert h, finset.card_insert_of_not_mem h, hi, smul_succ] theorem card_quot {α : Type*} [fintype α] (r : α → α → Prop) : card α = (finset.univ : finset (quot r)).sum (λ x, card {a // quot.mk r a = x}) := card_sigma (λ x, {a // quot.mk r a = x}) ▸ card_congr ⟨λ x, ⟨quot.mk r x, ⟨x, rfl⟩⟩, λ x, x.2, λ _, rfl, λ ⟨_, _, ⟨_⟩⟩, rfl⟩ instance {α : Type*} [fintype α] (s : setoid α) : fintype (quotient s) := quot.fintype s.r theorem card_quotient {α : Type*} [fintype α] (s : setoid α) : card α = (finset.univ : finset (quotient s)).sum (λ x, card {a // ⟦a⟧ = x}) := card_quot s.r instance {α : Type*} [fintype α] : fintype (set α) := pi.fintype section variable {α : Type*} open function set finset set_option pp.implicit true #print eq.drec_on lemma set.card_eq_zero {α : Type*} {s : set α} [hf : fintype s] : card s = 0 ↔ s = ∅ := ⟨λ h, by_contradiction $ λ h₁, let ⟨x, hx⟩ := set.exists_mem_of_ne_empty h₁ in finset.not_mem_empty (⟨x, hx⟩ : s) $ finset.card_eq_zero.1 h ▸ finset.mem_univ (⟨x, hx⟩ : s), λ h, by rw ← set.empty_card; congr; assumption⟩ lemma set.to_finset_union_distrib {α : Type*} (s t : set α) [hs : fintype s] [ht : fintype t] : @set.to_finset α (s ∪ t) (classical.choice $ set.finite_union ⟨hs⟩ ⟨ht⟩) = set.to_finset s ∪ set.to_finset t := finset.ext.2 $ λ x, by simp [set.mem_to_finset, finset.mem_union] lemma set.card_disjoint_union {α : Type*} {s t : set α} (hs : fintype s) (ht : fintype t) (hst : disjoint s t) : @finset.card (s ∪ t : set α) (classical.choice $ set.finite_union ⟨hs⟩ ⟨ht⟩) = card s + card t := begin have h := set.card_fintype_of_finset _ (@set.mem_to_finset _ s hs), have h₁ : ∀ {s : set α} (hs : fintype s), @card s hs = @card ↥s (@set.fintype_of_finset α s (@set.to_finset α s hs) (@set.mem_to_finset _ s hs)) := λ s hs, by congr, have h₃ := set.card_fintype_of_finset _ (@set.mem_to_finset _ t ht), have h₄ := set.card_fintype_of_finset _ (@set.mem_to_finset _ _ (classical.choice $ set.finite_union ⟨hs⟩ ⟨ht⟩)), rw [h₁ hs, h₁ ht, h₁ (classical.choice $ set.finite_union ⟨hs⟩ ⟨ht⟩), h, h₃, h₄, set.to_finset_union_distrib], rw finset.card_disjoint_union, simp [finset.disjoint], exact hst, end lemma blah {p q : Prop} (hp : p) (hq : q) : hp == hq := @eq.drec_on _ _ (λ r h₁, hp == (h₁ ▸ hp : r)) _ (propext ⟨λ h, hq, λ h, hp⟩) (heq.refl _) class subgroup {α : Type*} [group α] (s : set α) : Prop := (one_mem : (1 : α) ∈ s) (mul_mem : ∀ {x y}, x ∈ s → y ∈ s → x * y ∈ s) (inv_mem : ∀ {x}, x ∈ s → x⁻¹ ∈ s) end namespace subgroup variables {α : Type*} [g : group α] include g instance group (s : set α) [subgroup s] : group s := { mul := λ ⟨x, hx⟩ ⟨y, hy⟩, ⟨x * y, mul_mem hx hy⟩, mul_assoc := λ ⟨x, hx⟩ ⟨y, hy⟩ ⟨z, hz⟩, subtype.eq $ mul_assoc _ _ _, one := ⟨1, one_mem s⟩, one_mul := λ ⟨x, hx⟩, subtype.eq $ one_mul _, mul_one := λ ⟨x, hx⟩, subtype.eq $ mul_one _, inv := λ ⟨x, hx⟩, ⟨x⁻¹, inv_mem hx⟩, mul_left_inv := λ ⟨x, hx⟩, subtype.eq $ mul_left_inv _ } @[simp] lemma coe_one (S : set α) [subgroup S] : ((1 : S) : α) = 1 := rfl @[simp] lemma coe_mul {S : set α} [subgroup S] : ∀ a b : S, ((a * b : S) : α) = (a : α) * (b : α) := λ ⟨a, ha⟩ ⟨b, hb⟩, rfl @[simp] lemma coe_inv {S : set α} [subgroup S] : ∀ a : S, ((a⁻¹ : S) : α) = (a : α)⁻¹ := λ ⟨a, ha⟩, rfl @[simp] lemma coe_pow {S : set α} [subgroup S] (a : S) (n : ℕ) : ((monoid.pow a n : S) : α) = monoid.pow a n := by induction n; simp[pow_succ, *] @[simp] lemma coe_gpow {S : set α} [subgroup S] (a : S) (i : ℤ) : ((gpow a i : S) : α) = gpow a i := by cases i; simp end subgroup section cyclic class cyclic_group (α : Type*) extends group α := (cyclic : ∃ a, ∀ b : α, ∃ i : ℤ, a^i = b) instance cyclic_group.comm_group {α : Type*} [h : cyclic_group α] : comm_group α := { mul_comm := λ x y, let ⟨a, ha⟩ := cyclic_group.cyclic α in let ⟨i, hi⟩ := ha x in let ⟨j, hj⟩ := ha y in hi ▸ hj ▸ gpow_mul_comm a i j, ..h } variables {α : Type*} [group α] def cycle (a : α) := {b : α | ∃ i : ℤ, a^i = b} instance [fintype α] (a : α) : fintype (cycle a) := set_fintype _ instance (a : α) : subgroup (cycle a) := { one_mem := ⟨0, by simp⟩, inv_mem := λ b ⟨i, hi⟩, ⟨-i, hi ▸ gpow_neg _ _⟩, mul_mem := λ b c ⟨i, hi⟩ ⟨j, hj⟩, ⟨i + j, hi ▸ (hj ▸ (gpow_add _ _ _))⟩ } instance (a : α) : cyclic_group (cycle a) := { cyclic := ⟨⟨a, 1, pow_one _⟩, λ ⟨b, ⟨i, hi⟩⟩, ⟨i, subtype.eq ((subgroup.coe_gpow (⟨a, _⟩ : cycle a) i).trans hi)⟩ ⟩ } lemma mem_cycle_self (a : α) [fintype (cycle a)] : a ∈ cycle a := ⟨1, by simp⟩ lemma exists_int_pow_eq_one_of_finite_cycle (a : α) [fintype (cycle a)] : ∃ i : ℤ, i ≠ 0 ∧ a^i = 1 := by_contradiction $ λ h, @not_injective_nat_fintype _ _ _ (λ n, (⟨a^(nat.succ n), ⟨int.nat_abs (nat.succ n), by rw [int.nat_abs_of_nat] ⟩ ⟩ : cycle a)) $ have h : ∀ i : ℤ, ¬(i ≠ 0 ∧ a^i = 1) := not_exists.mp h, have h₁ : ∀ (i : ℤ), a^i = 1 → ¬i ≠ 0 := λ i, not_and'.mp (h i), λ m n hmn, have hmn' : a^(nat.succ m) =a^(nat.succ n) := subtype.mk.inj hmn, begin rw [← sub_add_cancel ((nat.succ m) : ℤ) (nat.succ n), gpow_add, ← mul_right_inj (a^nat.succ n)⁻¹, mul_inv_self, mul_assoc, mul_inv_self, mul_one] at hmn', exact nat.succ_inj (int.coe_nat_inj $ eq_of_sub_eq_zero $ not_not.mp $ h₁ _ hmn') end lemma exists_nat_pow_eq_one_of_finite_cycle (a : α) [fintype (cycle a)] : ∃ n : ℕ, 0 < n ∧ monoid.pow a n = 1 := let ⟨i, hi⟩ := exists_int_pow_eq_one_of_finite_cycle a in ⟨int.nat_abs i, ⟨int.nat_abs_pos_of_ne_zero hi.1, or.by_cases (int.nat_abs_eq i) (λ h, by rw h at hi; exact (gpow_coe_nat _ _).symm.trans hi.2) (λ h, by rw [h, gpow_neg] at hi; exact (gpow_coe_nat _ _).symm.trans (inv_eq_one.1 hi.2)) ⟩ ⟩ def ord (a : α) [fintype (cycle a)] := nat.find $ exists_nat_pow_eq_one_of_finite_cycle a variables (a : α) [fintype (cycle a)] @[simp] lemma pow_ord : monoid.pow a (ord a) = 1 := (nat.find_spec $ exists_nat_pow_eq_one_of_finite_cycle a).2 lemma ord_pos : 0 < ord a := (nat.find_spec $ exists_nat_pow_eq_one_of_finite_cycle a).1 lemma ord_le {a : α} [fintype (cycle a)] {n : ℕ} (ha0 : 0 < n) (han : monoid.pow a n = 1) : ord a ≤ n := nat.find_min' (exists_nat_pow_eq_one_of_finite_cycle a) ⟨ha0, han⟩ lemma lt_ord {a : α} [fintype (cycle a)] {n : ℕ} (hn : n < ord a) : n = 0 ∨ monoid.pow a n ≠ 1 := have h : ¬0 < n ∨ ¬ monoid.pow a n = 1 := (decidable.not_and_iff_or_not _ _).1 (nat.find_min (exists_nat_pow_eq_one_of_finite_cycle a) hn), by rwa [nat.pos_iff_ne_zero', not_not] at h lemma ord_dvd_int_iff (i : ℤ) : (ord a : ℤ) ∣ i ↔ a^i = 1 := ⟨λ h₁, let ⟨k, hk⟩ := exists_eq_mul_right_of_dvd h₁ in by simp [hk, gpow_mul, pow_ord], λ h₁, by_contradiction $ λ h₂, begin rw int.dvd_iff_mod_eq_zero at h₂, have h₃ : gpow a (i % ↑(ord a)) = 1, { rw ← int.mod_add_div i (ord a) at h₁, simp [gpow_add, gpow_mul] at h₁, exact h₁ }, have hzc := int.coe_nat_ne_zero.2 (ne_of_lt (ord_pos a)).symm, have h₄ : (int.nat_abs (i % ord a) : ℤ) = (i % ord a) := int.nat_abs_of_nonneg (int.mod_nonneg _ hzc), have h₅ : monoid.pow a (int.nat_abs (i % ↑(ord a))) = 1 := by rwa [← gpow_coe_nat, h₄], have h₆ : (ord a : ℤ) ≤ (i % ↑(ord a)) := by rw [← h₄, int.coe_nat_le]; exact ord_le (int.nat_abs_pos_of_ne_zero h₂) h₅, have h₇ := int.mod_lt i hzc, rw abs_of_nonneg (int.coe_nat_le.2 (nat.zero_le _)) at h₇, exact not_le_of_gt h₇ h₆, end⟩ lemma ord_dvd_nat_iff (n : ℕ) : ord a ∣ n ↔ monoid.pow a n = 1 := let h := ord_dvd_int_iff a n in by simp [int.coe_nat_dvd] at h; assumption lemma fintype_cycle_of_pow_eq_one (i : ℤ) (a : α) (hi : i ≠ 0) (h : a^i = 1) : fintype (cycle a) := fintype.of_surjective (λ n : fin (int.nat_abs i), (⟨monoid.pow a n.val, n.val, by simp⟩ : cycle a)) $ λ ⟨b, j, hj⟩, have hji : 0 ≤ j % i := int.mod_nonneg _ hi, ⟨⟨int.nat_abs (j % i), by rw [← int.coe_nat_lt, int.nat_abs_of_nonneg hji, ← int.abs_eq_nat_abs]; exact int.mod_lt _ hi⟩, subtype.eq $ show monoid.pow a (int.nat_abs (j % i)) = b, by rw [← gpow_coe_nat, int.nat_abs_of_nonneg hji, int.mod_def, sub_eq_add_neg, gpow_add, gpow_neg, gpow_mul, h, hj]; simp⟩ -- lemma ord_eq_cycle_card (a : α) [fintype (cycle a)] : ord a = card (cycle a) := -- card_fin (ord a) ▸ card_congr -- ⟨λ (n : fin (ord a)), (⟨monoid.pow a n.val, ⟨n.val, by simp⟩⟩ : cycle a), -- λ ⟨b, hb⟩, have ho : (0 : ℤ) < ord a := int.coe_nat_lt.2 (ord_pos a), -- have h₁ : classical.some hb % ↑(ord a) ≥ 0 := int.mod_nonneg _ (ne_of_lt ho).symm, -- ⟨int.nat_abs (classical.some hb % (ord a : ℤ)), -- int.coe_nat_lt.1 ((int.nat_abs_of_nonneg h₁).symm ▸ int.mod_lt_of_pos _ ho)⟩, -- λ ⟨n, hn⟩, begin cases lt_ord hn with h h, -- simp [h], -- end,sorry⟩ -- too long lemma ord_eq_cycle_card (a : α) [fintype (cycle a)] : ord a = fintype.card (cycle a) := fintype.card_fin (ord a) ▸ (fintype.card_congr $ equiv.of_bijective $ show function.bijective (λ (n : fin (ord a)), (⟨monoid.pow a n.val, ⟨n.val, by simp⟩⟩ : cycle a)), from ⟨λ n m h, fin.eq_of_veq $ --injective proof begin cases n with n hn, cases m with m hm, wlog h : m ≤ n using m n, { exact (this hm hn h_1.symm).symm }, replace h_1 : monoid.pow a n = monoid.pow a m := subtype.mk.inj h_1, rw [← nat.sub_add_cancel h, ← one_mul (monoid.pow a m), pow_add, mul_right_inj] at h_1, have h₁ := or.neg_resolve_right (lt_ord (lt_of_le_of_lt (nat.sub_le n m) hn)) h_1, exact le_antisymm (nat.sub_eq_zero_iff_le.1 (or.neg_resolve_right (lt_ord (lt_of_le_of_lt (nat.sub_le n m) hn)) h_1)) h, end, λ ⟨x, ⟨i, hi⟩ ⟩, -- surjective proof have ho : (ord a : ℤ) ≠ 0 := int.coe_nat_ne_zero.2 (ne_of_lt (ord_pos _)).symm, have hio : (int.to_nat (i % ↑(ord a)) : ℤ) = i % ord a := int.to_nat_of_nonneg (int.mod_nonneg _ ho), ⟨⟨int.to_nat (i % (ord a : ℤ)), int.coe_nat_lt.1 $ by rw [hio, ← abs_of_nonneg (int.coe_nat_le.2 (nat.zero_le (ord a))), int.mod_abs]; exact int.mod_lt _ ho⟩, begin simp, rw [← gpow_coe_nat, hio, int.mod_def], simp [gpow_add, gpow_mul, gpow_neg, hi] end⟩⟩) #print ord_eq_cycle_card end cyclic section coset open subgroup variables {α : Type*} [group α] (S : set α) [subgroup S] def lcoset (b : α) := {a : α | ∃ s ∈ S, b * s = a} def rcoset (b : α) := {a : α | ∃ s ∈ S, s * b = a} lemma lcoset_eq {S : set α} [subgroup S] {a b : α} : b ∈ lcoset S a → lcoset S a = lcoset S b := λ ⟨sa, hSsa, hsa⟩, set.ext $ λ x, ⟨λ ⟨sb, hSsb, hsb⟩, ⟨sa⁻¹ * sb, ⟨mul_mem (inv_mem hSsa) hSsb, hsa ▸ by simpa [mul_assoc] ⟩ ⟩, λ ⟨sb, hSsb, hsb⟩, ⟨sa * sb, ⟨mul_mem hSsa hSsb, mul_assoc a sa sb ▸ hsa.symm ▸ hsb ⟩ ⟩ ⟩ lemma rcoset_eq {S : set α} [subgroup S] {a b : α} : b ∈ rcoset S a → rcoset S a = rcoset S b := λ ⟨sa, hSsa, hsa⟩, set.ext $ λ x, ⟨λ ⟨sb, hSsb, hsb⟩, ⟨sb * sa⁻¹, ⟨mul_mem hSsb (inv_mem hSsa), hsa ▸ by simpa [mul_assoc] ⟩ ⟩, λ ⟨sb, hSsb, hsb⟩, ⟨sb * sa, ⟨mul_mem hSsb hSsa, (mul_assoc sb sa a).symm ▸ hsa.symm ▸ hsb ⟩ ⟩ ⟩ @[simp] lemma mem_lcoset_self (a : α) : a ∈ lcoset S a := ⟨1, one_mem _, mul_one _⟩ @[simp] lemma mem_rcoset_self (a : α) : a ∈ rcoset S a := ⟨1, one_mem _, one_mul _⟩ lemma lcoset_card [fintype α] (a : α) : card (lcoset S a) = fintype.card S := fintype.card_congr $ @equiv.of_bijective (lcoset S a) S (λ x, ⟨a⁻¹ * x, let ⟨y, hyS, (hy : a * y = x)⟩ := x.2 in by rwa ← hy; simpa⟩) ⟨λ x y h, by simp at h; exact subtype.eq h, λ x, ⟨ ⟨a * x, ⟨x, ⟨x.2, rfl⟩ ⟩ ⟩, by simp ⟩ ⟩ lemma rcoset_card [fintype α] (a : α) : card (rcoset S a) = fintype.card S := fintype.card_congr $ @equiv.of_bijective (rcoset S a) S (λ x, ⟨x * a⁻¹, let ⟨y, hyS, (hy : y * a = x)⟩ := x.2 in by rwa ← hy; simpa⟩) ⟨λ x y h, by simp at h; exact subtype.eq h, λ x, ⟨ ⟨x * a, ⟨x, ⟨x.2, rfl⟩ ⟩ ⟩, by simp ⟩ ⟩ instance lcoset_setoid : setoid α := { r := λ a b, b ∈ lcoset S a, iseqv := ⟨mem_lcoset_self S, λ a b h, by rw ← lcoset_eq h; exact mem_lcoset_self S a, λ a b c hab hbc, by rwa lcoset_eq hab⟩ } def index [fintype α] := card (quotient (lcoset_setoid S)) theorem lagrange [fintype α] : card α = card S * index S := have h : (λ x : quotient (lcoset_setoid S), card {a // ⟦a⟧ = x}) = (λ x, card S) := funext (λ x, begin simp only [@eq_comm _ _ x], rw [← quot.out_eq x, ← lcoset_card S (quot.out x)], congr, exact set.ext (λ y, quotient.eq) end), by rw [card_quotient (lcoset_setoid S), h, finset.sum_const, nat.smul_eq_mul]; refl end coset lemma card_eq_ord_mul_index {α : Type*} [group α] [fintype α] (a : α) : card α = ord a * index (cycle a) := (ord_eq_cycle_card a).symm ▸ lagrange _ @[simp] lemma pow_card {α : Type*} [group α] [fintype α] (a : α) : monoid.pow a (card α) = 1 := (ord_dvd_nat_iff _ _).1 $ (card_eq_ord_mul_index a).symm ▸ dvd_mul_right _ _ open equiv namespace perm variables {α : Type*} lemma mul_apply (a b : perm α) (x : α) : (a * b) x = (a (b x)) := rfl @[simp] lemma one_apply (x : α) : (1 : perm α) x = x := rfl instance [decidable_eq α] (h : fintype α): fintype (perm α) := fintype.of_equiv {y : (α → α) × (α → α) // function.left_inverse y.2 y.1 ∧ function.right_inverse y.2 y.1} ⟨λ x, ⟨x.1.1, x.1.2, x.2.1, x.2.2⟩, λ x, ⟨⟨x.1, x.2⟩, ⟨x.3, x.4⟩⟩, λ ⟨⟨_, _⟩, _, _⟩, rfl, λ ⟨_, _, _, _⟩, rfl⟩ instance perm.cycle.fintype [h : fintype α] (a : perm α) : fintype (cycle a) := @cycle.fintype (perm α) _ (perm.fintype h) a def support (a : perm α) : set α := {x : α | a x ≠ x} example (f g : perm α) : support (g * f * g⁻¹) = set.image g (support f) := set.ext $ λ y, ⟨λ h : _ ≠ _, ⟨g⁻¹ y, λ h₁, by rw [mul_apply, mul_apply, h₁, ← mul_apply, mul_inv_self] at h; exact h rfl, show (g * g⁻¹) y = y,by rw mul_inv_self; refl⟩, λ ⟨x, (hx : _ ≠ _ ∧ _)⟩, show _ ≠ _, from begin rw [mul_apply, ← hx.2, ← mul_apply, ← mul_apply, mul_assoc, inv_mul_self, mul_one, mul_apply], assume h, rw (equiv.bijective g).1 h at hx, exact hx.1 rfl end⟩ def disjoint (a b : perm α) := _root_.disjoint (support a) (support b) lemma disjoint_or {a b : perm α} : disjoint a b ↔ ∀ x : α, a x = x ∨ b x = x := have h : disjoint a b ↔ ∀ x : α, ¬ (a x ≠ x ∧ b x ≠ x) := ⟨λ (h : (λ x, a x ≠ x ∧ b x ≠ x : set α) = ∅) x, show x ∉ {x : α | a x ≠ x ∧ b x ≠ x}, by rw h; simp, λ h, set.ext $ λ x, ⟨λ h₁, absurd h₁ (h x), λ h₁, absurd h₁ (set.not_mem_empty _)⟩⟩, by rw h; simp only [or_iff_not_and_not] lemma disjoint_comm {a b : perm α} (hd : disjoint a b) : a * b = b * a := equiv.ext _ _ $ λ x, show a (b x) = b (a x), from or.by_cases (disjoint_or.1 hd x) (λ h, h.symm ▸ or.by_cases (disjoint_or.1 hd $ b x) (λ h₁, h₁) (λ h₁, ((equiv.bijective b).1 h₁).symm ▸ h)) (λ h, h.symm ▸ or.by_cases (disjoint_or.1 hd $ a x) (λ h₁, ((equiv.bijective a).1 h₁.symm) ▸ h.symm) (λ h₁, h₁.symm)) lemma eq_one_of_support_eq_empty {a : perm α} [fintype (support a)] (h : support a = ∅) : a = 1 := ext _ _ $ λ x, have hx : x ∉ support a := h.symm ▸ set.not_mem_empty x, not_not.1 hx lemma exists_mem_support_of_ne_one {a : perm α} (h : a ≠ 1) : ∃ x : α, x ∈ support a := by_contradiction $ λ h₁, h $ ext _ _ $ by simp [support, *] at * def same_cycle (a : perm α) (x y : α) := ∃ i : ℤ, (a^i) x = y @[refl] lemma same_cycle.refl {a : perm α} (x : α) : same_cycle a x x := ⟨0, by rw gpow_zero; refl⟩ @[symm] lemma same_cycle.symm {a : perm α} {x y : α} (h : same_cycle a x y) : same_cycle a y x := let ⟨i, hi⟩ := h in ⟨-i, by rw [← hi, ←mul_apply, ← gpow_add, neg_add_self, gpow_zero, one_apply] ⟩ @[trans] lemma same_cycle.trans {a : perm α} {x y z : α} (hxy : same_cycle a x y) (hyz : same_cycle a y z) : same_cycle a x z := let ⟨i, hi⟩ := hxy in let ⟨j, hj⟩ := hyz in ⟨j + i, begin rw [gpow_add, mul_apply, hi], simp [hi, hj] end⟩ lemma same_cycle_apply {a : perm α} {x y : α} (h : same_cycle a x y) (i : ℤ) : same_cycle a ((a^i) x) y := same_cycle.trans (same_cycle.symm (⟨i, rfl⟩ : same_cycle a x ((a^i) x))) h def cycle_of (a : perm α) (x : α) : perm α := { to_fun := λ y, ite (same_cycle a x y) (a y) y, inv_fun := λ y, ite (same_cycle a x y) (a⁻¹ y) y, left_inv := λ y, dite (same_cycle a x y) (λ h, have h₁ : same_cycle a x (a y) := same_cycle.symm (same_cycle_apply (same_cycle.symm h) 1), by simp [h, h₁]; rw [← mul_apply, inv_mul_self, one_apply]) (λ h, by simp [h]), right_inv := λ y, dite (same_cycle a x y) (λ h, have h₁ : same_cycle a x (a⁻¹ y) := same_cycle.symm (same_cycle_apply (same_cycle.symm h) (-1)), by simp [h, h₁]; rw [← mul_apply, mul_inv_self, one_apply]) (λ h, by simp [h]) } def is_cycle (a : perm α) := a ≠ 1 ∧ ∀ x y : α, x ∈ support a → y ∈ support a → same_cycle a x y lemma support_disjoint_mul {a b : perm α} (h : disjoint a b) : support (a * b) = support a ∪ support b := set.ext $ λ x, or.by_cases (disjoint_or.1 h x) (λ h₁, ⟨λ h₂, or.inr (have h₂ : b (a x) ≠ x := by rw disjoint_comm h at h₂; exact h₂, by rw h₁ at h₂; exact h₂), λ h₂, by rw disjoint_comm h; exact show b (a x) ≠ x, by rw h₁; exact or.resolve_left h₂ (not_not.2 h₁)⟩) (λ h₁, ⟨λ (h₂ : a (b x) ≠ x), or.inl $ by rw h₁ at h₂; exact h₂, λ h₂, show a (b x) ≠ x, by rw h₁; exact or.resolve_right h₂ (not_not.2 h₁)⟩) lemma exists_disjoint_factors [fintype α] (a : perm α) (h : ¬ is_cycle a) (h₁ : a ≠ 1) : ∃ b c : perm α, disjoint b c ∧ b * c = a ∧ card (support b) < card (support a) ∧ card (support c) < card (support a) := begin suffices : ∀ (n : ℕ) (a : perm α), ¬ is_cycle a → a ≠ 1 → card (support a) ≤ n → ∃ b c : perm α, disjoint b c ∧ b * c = a ∧ card (support b) < card (support a) ∧ card (support c) < card (support a), exact this (card (support a)) a h h₁ (le_refl _), assume n, induction n with n hi, { assume a h h₁ h₂, exact absurd (eq_one_of_support_eq_empty (set.card_eq_zero.1 (nat.eq_zero_of_le_zero h₂))) h₁ }, { assume a h h₁ h₂, cases exists_mem_support_of_ne_one h₁ with x hx, } end -- lemma product_disjoint [fintype α] (a : perm α) : ∃ l : list (perm α), (∀ b ∈ l, is_cycle b ∧ b ≠ 1) ∧ (∀ b c ∈ l, disjoint b c) ∧ list.prod l = a := -- begin -- suffices : ∀ (n : ℕ) (a : perm α), ord a ≤ n → ∃ l : list (perm α), (∀ b ∈ l, is_cycle b ∧ b ≠ 1) ∧ (∀ b c ∈ l, disjoint b c) ∧ list.prod l = a, -- { exact this (ord a) a (le_refl _) }, -- assume n, -- induction n with n hi, -- { exact λ a ha, absurd (lt_of_lt_of_le (ord_pos a) ha) dec_trivial }, -- assume a ha, -- end end perm
7d3a0392840c246f71e8ec57e0759471c74e6f87
d436468d80b739ba7e06843c4d0d2070e43448e5
/src/algebra/order.lean
fa106d3de01d14d20680c409e1183ffa9b157887
[ "Apache-2.0" ]
permissive
roro47/mathlib
761fdc002aef92f77818f3fef06bf6ec6fc1a28e
80aa7d52537571a2ca62a3fdf71c9533a09422cf
refs/heads/master
1,599,656,410,625
1,573,649,488,000
1,573,649,488,000
221,452,951
0
0
Apache-2.0
1,573,647,693,000
1,573,647,692,000
null
UTF-8
Lean
false
false
9,443
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ universe u variables {α : Type u} @[simp] lemma ge_iff_le [preorder α] {a b : α} : a ≥ b ↔ b ≤ a := iff.rfl @[simp] lemma gt_iff_lt [preorder α] {a b : α} : a > b ↔ b < a := iff.rfl lemma not_le_of_lt [preorder α] {a b : α} (h : a < b) : ¬ b ≤ a := (le_not_le_of_lt h).right lemma not_lt_of_le [preorder α] {a b : α} (h : a ≤ b) : ¬ b < a | hab := not_le_of_gt hab h lemma le_iff_eq_or_lt [partial_order α] {a b : α} : a ≤ b ↔ a = b ∨ a < b := le_iff_lt_or_eq.trans or.comm lemma lt_of_le_of_ne' [partial_order α] {a b : α} (h₁ : a ≤ b) (h₂ : a ≠ b) : a < b := lt_of_le_not_le h₁ $ mt (le_antisymm h₁) h₂ lemma lt_iff_le_and_ne [partial_order α] {a b : α} : a < b ↔ a ≤ b ∧ a ≠ b := ⟨λ h, ⟨le_of_lt h, ne_of_lt h⟩, λ ⟨h1, h2⟩, lt_of_le_of_ne h1 h2⟩ lemma eq_iff_le_not_lt [partial_order α] {a b : α} : a = b ↔ a ≤ b ∧ ¬ a < b := ⟨λ h, ⟨le_of_eq h, h ▸ lt_irrefl _⟩, λ ⟨h₁, h₂⟩, le_antisymm h₁ $ classical.by_contradiction $ λ h₃, h₂ (lt_of_le_not_le h₁ h₃)⟩ lemma eq_or_lt_of_le [partial_order α] {a b : α} (h : a ≤ b) : a = b ∨ a < b := (lt_or_eq_of_le h).symm lemma lt_of_not_ge' [linear_order α] {a b : α} (h : ¬ b ≤ a) : a < b := lt_of_le_not_le ((le_total _ _).resolve_right h) h lemma lt_iff_not_ge' [linear_order α] {x y : α} : x < y ↔ ¬ y ≤ x := ⟨not_le_of_gt, lt_of_not_ge'⟩ @[simp] lemma not_lt [linear_order α] {a b : α} : ¬ a < b ↔ b ≤ a := ⟨le_of_not_gt, not_lt_of_ge⟩ lemma le_of_not_lt [linear_order α] {a b : α} : ¬ a < b → b ≤ a := not_lt.1 @[simp] lemma not_le [linear_order α] {a b : α} : ¬ a ≤ b ↔ b < a := lt_iff_not_ge'.symm lemma lt_or_le [linear_order α] : ∀ a b : α, a < b ∨ b ≤ a := lt_or_ge lemma le_or_lt [linear_order α] : ∀ a b : α, a ≤ b ∨ b < a := le_or_gt lemma not_lt_iff_eq_or_lt [linear_order α] {a b : α} : ¬ a < b ↔ a = b ∨ b < a := not_lt.trans $ le_iff_eq_or_lt.trans $ or_congr eq_comm iff.rfl lemma exists_ge_of_linear [linear_order α] (a b : α) : ∃ c, a ≤ c ∧ b ≤ c := match le_total a b with | or.inl h := ⟨_, h, le_refl _⟩ | or.inr h := ⟨_, le_refl _, h⟩ end lemma lt_imp_lt_of_le_imp_le {β} [linear_order α] [preorder β] {a b : α} {c d : β} (H : a ≤ b → c ≤ d) (h : d < c) : b < a := lt_of_not_ge' $ λ h', not_lt_of_ge (H h') h lemma le_imp_le_of_lt_imp_lt {β} [preorder α] [linear_order β] {a b : α} {c d : β} (H : d < c → b < a) (h : a ≤ b) : c ≤ d := le_of_not_gt $ λ h', not_le_of_gt (H h') h lemma le_imp_le_iff_lt_imp_lt {β} [linear_order α] [linear_order β] {a b : α} {c d : β} : (a ≤ b → c ≤ d) ↔ (d < c → b < a) := ⟨lt_imp_lt_of_le_imp_le, le_imp_le_of_lt_imp_lt⟩ lemma lt_iff_lt_of_le_iff_le' {β} [preorder α] [preorder β] {a b : α} {c d : β} (H : a ≤ b ↔ c ≤ d) (H' : b ≤ a ↔ d ≤ c) : b < a ↔ d < c := lt_iff_le_not_le.trans $ (and_congr H' (not_congr H)).trans lt_iff_le_not_le.symm lemma lt_iff_lt_of_le_iff_le {β} [linear_order α] [linear_order β] {a b : α} {c d : β} (H : a ≤ b ↔ c ≤ d) : b < a ↔ d < c := not_le.symm.trans $ iff.trans (not_congr H) $ not_le lemma le_iff_le_iff_lt_iff_lt {β} [linear_order α] [linear_order β] {a b : α} {c d : β} : (a ≤ b ↔ c ≤ d) ↔ (b < a ↔ d < c) := ⟨lt_iff_lt_of_le_iff_le, λ H, not_lt.symm.trans $ iff.trans (not_congr H) $ not_lt⟩ lemma eq_of_forall_le_iff [partial_order α] {a b : α} (H : ∀ c, c ≤ a ↔ c ≤ b) : a = b := le_antisymm ((H _).1 (le_refl _)) ((H _).2 (le_refl _)) lemma le_of_forall_le [preorder α] {a b : α} (H : ∀ c, c ≤ a → c ≤ b) : a ≤ b := H _ (le_refl _) lemma le_of_forall_le' [preorder α] {a b : α} (H : ∀ c, a ≤ c → b ≤ c) : b ≤ a := H _ (le_refl _) lemma le_of_forall_lt [linear_order α] {a b : α} (H : ∀ c, c < a → c < b) : a ≤ b := le_of_not_lt $ λ h, lt_irrefl _ (H _ h) lemma le_of_forall_lt' [linear_order α] {a b : α} (H : ∀ c, a < c → b < c) : b ≤ a := le_of_not_lt $ λ h, lt_irrefl _ (H _ h) lemma eq_of_forall_ge_iff [partial_order α] {a b : α} (H : ∀ c, a ≤ c ↔ b ≤ c) : a = b := le_antisymm ((H _).2 (le_refl _)) ((H _).1 (le_refl _)) /-- monotonicity of `≤` with respect to `→` -/ lemma le_implies_le_of_le_of_le {a b c d : α} [preorder α] (h₀ : c ≤ a) (h₁ : b ≤ d) : a ≤ b → c ≤ d := assume h₂ : a ≤ b, calc c ≤ a : h₀ ... ≤ b : h₂ ... ≤ d : h₁ namespace decidable lemma lt_or_eq_of_le [partial_order α] [@decidable_rel α (≤)] {a b : α} (hab : a ≤ b) : a < b ∨ a = b := if hba : b ≤ a then or.inr (le_antisymm hab hba) else or.inl (lt_of_le_not_le hab hba) lemma eq_or_lt_of_le [partial_order α] [@decidable_rel α (≤)] {a b : α} (hab : a ≤ b) : a = b ∨ a < b := (lt_or_eq_of_le hab).swap lemma le_iff_lt_or_eq [partial_order α] [@decidable_rel α (≤)] {a b : α} : a ≤ b ↔ a < b ∨ a = b := ⟨lt_or_eq_of_le, le_of_lt_or_eq⟩ lemma le_of_not_lt [decidable_linear_order α] {a b : α} (h : ¬ b < a) : a ≤ b := decidable.by_contradiction $ λ h', h $ lt_of_le_not_le ((le_total _ _).resolve_right h') h' lemma not_lt [decidable_linear_order α] {a b : α} : ¬ a < b ↔ b ≤ a := ⟨le_of_not_lt, not_lt_of_ge⟩ lemma lt_or_le [decidable_linear_order α] (a b : α) : a < b ∨ b ≤ a := if hba : b ≤ a then or.inr hba else or.inl $ not_le.1 hba lemma le_or_lt [decidable_linear_order α] (a b : α) : a ≤ b ∨ b < a := (lt_or_le b a).swap lemma lt_trichotomy [decidable_linear_order α] (a b : α) : a < b ∨ a = b ∨ b < a := (lt_or_le _ _).imp_right $ λ h, (eq_or_lt_of_le h).imp_left eq.symm lemma lt_or_gt_of_ne [decidable_linear_order α] {a b : α} (h : a ≠ b) : a < b ∨ b < a := (lt_trichotomy a b).imp_right $ λ h', h'.resolve_left h def lt_by_cases [decidable_linear_order α] (x y : α) {P : Sort*} (h₁ : x < y → P) (h₂ : x = y → P) (h₃ : y < x → P) : P := begin by_cases h : x < y, { exact h₁ h }, by_cases h' : y < x, { exact h₃ h' }, apply h₂, apply le_antisymm; apply le_of_not_gt; assumption end lemma ne_iff_lt_or_gt [decidable_linear_order α] {a b : α} : a ≠ b ↔ a < b ∨ b < a := ⟨lt_or_gt_of_ne, λo, o.elim ne_of_lt ne_of_gt⟩ lemma le_imp_le_of_lt_imp_lt {β} [preorder α] [decidable_linear_order β] {a b : α} {c d : β} (H : d < c → b < a) (h : a ≤ b) : c ≤ d := le_of_not_lt $ λ h', not_le_of_gt (H h') h lemma le_imp_le_iff_lt_imp_lt {β} [linear_order α] [decidable_linear_order β] {a b : α} {c d : β} : (a ≤ b → c ≤ d) ↔ (d < c → b < a) := ⟨lt_imp_lt_of_le_imp_le, le_imp_le_of_lt_imp_lt⟩ lemma le_iff_le_iff_lt_iff_lt {β} [decidable_linear_order α] [decidable_linear_order β] {a b : α} {c d : β} : (a ≤ b ↔ c ≤ d) ↔ (b < a ↔ d < c) := ⟨lt_iff_lt_of_le_iff_le, λ H, not_lt.symm.trans $ iff.trans (not_congr H) $ not_lt⟩ lemma min_le_max [decidable_linear_order α] (a b : α) : min a b ≤ max a b := le_trans (min_le_left a b) (le_max_left a b) end decidable namespace ordering /-- `compares o a b` means that `a` and `b` have the ordering relation `o` between them, assuming that the relation `a < b` is defined -/ @[simp] def compares [has_lt α] : ordering → α → α → Prop | lt a b := a < b | eq a b := a = b | gt a b := a > b theorem compares.eq_lt [preorder α] : ∀ {o} {a b : α}, compares o a b → (o = lt ↔ a < b) | lt a b h := ⟨λ _, h, λ _, rfl⟩ | eq a b h := ⟨λ h, by injection h, λ h', (ne_of_lt h' h).elim⟩ | gt a b h := ⟨λ h, by injection h, λ h', (lt_asymm h h').elim⟩ theorem compares.eq_eq [preorder α] : ∀ {o} {a b : α}, compares o a b → (o = eq ↔ a = b) | lt a b h := ⟨λ h, by injection h, λ h', (ne_of_lt h h').elim⟩ | eq a b h := ⟨λ _, h, λ _, rfl⟩ | gt a b h := ⟨λ h, by injection h, λ h', (ne_of_gt h h').elim⟩ theorem compares.eq_gt [preorder α] : ∀ {o} {a b : α}, compares o a b → (o = gt ↔ a > b) | lt a b h := ⟨λ h, by injection h, λ h', (lt_asymm h h').elim⟩ | eq a b h := ⟨λ h, by injection h, λ h', (ne_of_gt h' h).elim⟩ | gt a b h := ⟨λ _, h, λ _, rfl⟩ theorem compares.inj [preorder α] {o₁} : ∀ {o₂} {a b : α}, compares o₁ a b → compares o₂ a b → o₁ = o₂ | lt a b h₁ h₂ := h₁.eq_lt.2 h₂ | eq a b h₁ h₂ := h₁.eq_eq.2 h₂ | gt a b h₁ h₂ := h₁.eq_gt.2 h₂ theorem swap_or_else (o₁ o₂) : (or_else o₁ o₂).swap = or_else o₁.swap o₂.swap := by cases o₁; try {refl}; cases o₂; refl theorem or_else_eq_lt (o₁ o₂) : or_else o₁ o₂ = lt ↔ o₁ = lt ∨ (o₁ = eq ∧ o₂ = lt) := by cases o₁; cases o₂; exact dec_trivial end ordering theorem cmp_compares [decidable_linear_order α] (a b : α) : (cmp a b).compares a b := begin unfold cmp cmp_using, by_cases a < b; simp [h], by_cases h₂ : b < a; simp [h₂, gt], exact (lt_or_eq_of_le (le_of_not_gt h₂)).resolve_left h end theorem cmp_swap [preorder α] [@decidable_rel α (<)] (a b : α) : (cmp a b).swap = cmp b a := begin unfold cmp cmp_using, by_cases a < b; by_cases h₂ : b < a; simp [h, h₂, gt, ordering.swap], exact lt_asymm h h₂ end
0701c090db8dbdcdc862bbe3535cd11c76e02ccd
a7eef317ddec01b9fc6cfbb876fe7ac00f205ac7
/src/data/support.lean
6ef0c804df3fb2d3565b590e0b762c6594f3a72f
[ "Apache-2.0" ]
permissive
kmill/mathlib
ea5a007b67ae4e9e18dd50d31d8aa60f650425ee
1a419a9fea7b959317eddd556e1bb9639f4dcc05
refs/heads/master
1,668,578,197,719
1,593,629,163,000
1,593,629,163,000
276,482,939
0
0
null
1,593,637,960,000
1,593,637,959,000
null
UTF-8
Lean
false
false
4,542
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 /-! # 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 := classical.not_not lemma support_binop_subset [has_zero A] (op : A → A → A) (op0 : op 0 0 = 0) (f g : α → A) : support (λ x, op (f x) (g x)) ⊆ support f ∪ support g := λ x hx, classical.by_cases (λ hf : f x = 0, or.inr $ λ hg, hx $ by simp only [hf, hg, op0]) or.inl lemma support_add [add_monoid A] (f g : α → A) : support (λ x, f x + g x) ⊆ support f ∪ support g := support_binop_subset (+) (zero_add _) f g @[simp] lemma support_neg [add_group A] (f : α → A) : support (λ x, -f x) = support f := set.ext $ λ x, not_congr neg_eq_zero lemma support_sub [add_group A] (f g : α → A) : support (λ x, f x - g x) ⊆ support f ∪ support g := support_binop_subset (has_sub.sub) (sub_self _) f g @[simp] lemma support_mul [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 -- TODO: Drop `classical` once #2332 is merged lemma support_prod [integral_domain A] (s : finset α) (f : α → β → A) : support (λ x, ∏ i in s, f i x) = ⋂ i ∈ s, support (f i) := set.ext $ λ x, by classical; simp only [support, ne.def, finset.prod_eq_zero_iff, mem_set_of_eq, set.mem_Inter, not_exists] lemma support_comp [has_zero A] [has_zero B] (g : A → B) (hg : g 0 = 0) (f : α → A) : support (g ∘ f) ⊆ support f := λ x, mt $ λ h, by simp [(∘), *] lemma support_comp' [has_zero A] [has_zero B] (g : A → B) (hg : g 0 = 0) (f : α → A) : support (λ x, g (f x)) ⊆ support f := support_comp g hg f lemma support_comp_eq [has_zero A] [has_zero B] (g : A → B) (hg : ∀ {x}, g x = 0 ↔ x = 0) (f : α → A) : support (g ∘ f) = support f := set.ext $ λ x, not_congr hg lemma support_comp_eq' [has_zero A] [has_zero B] (g : A → B) (hg : ∀ {x}, g x = 0 ↔ x = 0) (f : α → A) : support (λ x, g (f x)) = support f := support_comp_eq g @hg f end function
3d1f559155456409d213ae0c35aa9f7f4b51c7ff
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/depFieldIssue.lean
1583584138429e81e6dec1e10982553073d03cc4
[ "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,156
lean
import Lean inductive SF : Type u → Type u → Type (u+1) where | seq {as bs cs: Type u}: SF as bs → SF bs cs → SF as cs | fan {as bs cs: Type u}: SF as (bs × cs) inductive SF' (m: Type (u+1) → Type u)[Monad m]: Type u → Type u → Type (u+1) where | seq {as bs cs: Type u}: SF' m as bs → SF' m bs cs → SF' m as cs | fan {as bs cs: Type u}: SF' m as (bs × cs) | rswitcher {as bs cs: Type u}: SF' m as (bs × cs) → SF' m as bs def SF.step [Monad m] (sa: as): SF as bs → SF' m as bs × bs | seq sf₁ sf₂ => let (sf₁', sb) := sf₁.step sa let (sf₂', sc) := sf₂.step sb (sf₁'.seq sf₂', sc) | fan => sorry open Lean.Compiler set_option trace.Compiler.result true set_option pp.funBinderTypes true set_option pp.letVarTypes true #eval Lean.Compiler.compile #[``SF.step] def SF'.step [Monad m] (sa: as): SF' m as bs → SF'.{u} m as bs × bs | seq sf₁ sf₂ => let (sf₁', sb) := sf₁.step sa let (sf₂', sc) := sf₂.step sb (sf₁'.seq sf₂', sc) | fan => sorry | rswitcher f => let x := f.step sa let (_, (sb, _)) := x (rswitcher f, sb) #eval Lean.Compiler.compile #[``SF'.step]
319f27ee143985ae9a10e3f76a933e3139a9d5d1
4727251e0cd73359b15b664c3170e5d754078599
/src/deprecated/submonoid.lean
1cec680bb520167fe8cd2c51f62a932b27ee1843
[ "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
16,086
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Kenny Lau, Johan Commelin, Mario Carneiro, Kevin Buzzard -/ import group_theory.submonoid.basic import algebra.big_operators.basic import deprecated.group /-! # Unbundled submonoids This file defines unbundled multiplicative and additive submonoids `is_submonoid` and `is_add_submonoid`. These are not the preferred way to talk about submonoids and should not be used for any new projects. The preferred way in mathlib are the bundled versions `submonoid G` and `add_submonoid G`. ## Main definitions `is_add_submonoid (S : set G)` : the predicate that `S` is the underlying subset of an additive submonoid of `G`. The bundled variant `add_subgroup G` should be used in preference to this. `is_submonoid (S : set G)` : the predicate that `S` is the underlying subset of a submonoid of `G`. The bundled variant `submonoid G` should be used in preference to this. ## Tags subgroup, subgroups, is_subgroup ## Tags submonoid, submonoids, is_submonoid -/ open_locale big_operators variables {M : Type*} [monoid M] {s : set M} variables {A : Type*} [add_monoid A] {t : set A} /-- `s` is an additive submonoid: a set containing 0 and closed under addition. Note that this structure is deprecated, and the bundled variant `add_submonoid A` should be preferred. -/ structure is_add_submonoid (s : set A) : Prop := (zero_mem : (0:A) ∈ s) (add_mem {a b} : a ∈ s → b ∈ s → a + b ∈ s) /-- `s` is a submonoid: a set containing 1 and closed under multiplication. Note that this structure is deprecated, and the bundled variant `submonoid M` should be preferred. -/ @[to_additive] structure is_submonoid (s : set M) : Prop := (one_mem : (1:M) ∈ s) (mul_mem {a b} : a ∈ s → b ∈ s → a * b ∈ s) lemma additive.is_add_submonoid {s : set M} : ∀ (is : is_submonoid s), @is_add_submonoid (additive M) _ s | ⟨h₁, h₂⟩ := ⟨h₁, @h₂⟩ theorem additive.is_add_submonoid_iff {s : set M} : @is_add_submonoid (additive M) _ s ↔ is_submonoid s := ⟨λ ⟨h₁, h₂⟩, ⟨h₁, @h₂⟩, additive.is_add_submonoid⟩ lemma multiplicative.is_submonoid {s : set A} : ∀ (is : is_add_submonoid s), @is_submonoid (multiplicative A) _ s | ⟨h₁, h₂⟩ := ⟨h₁, @h₂⟩ theorem multiplicative.is_submonoid_iff {s : set A} : @is_submonoid (multiplicative A) _ s ↔ is_add_submonoid s := ⟨λ ⟨h₁, h₂⟩, ⟨h₁, @h₂⟩, multiplicative.is_submonoid⟩ /-- The intersection of two submonoids of a monoid `M` is a submonoid of `M`. -/ @[to_additive "The intersection of two `add_submonoid`s of an `add_monoid` `M` is an `add_submonoid` of M."] lemma is_submonoid.inter {s₁ s₂ : set M} (is₁ : is_submonoid s₁) (is₂ : is_submonoid s₂) : is_submonoid (s₁ ∩ s₂) := { one_mem := ⟨is₁.one_mem, is₂.one_mem⟩, mul_mem := λ x y hx hy, ⟨is₁.mul_mem hx.1 hy.1, is₂.mul_mem hx.2 hy.2⟩ } /-- The intersection of an indexed set of submonoids of a monoid `M` is a submonoid of `M`. -/ @[to_additive "The intersection of an indexed set of `add_submonoid`s of an `add_monoid` `M` is an `add_submonoid` of `M`."] lemma is_submonoid.Inter {ι : Sort*} {s : ι → set M} (h : ∀ y : ι, is_submonoid (s y)) : is_submonoid (set.Inter s) := { one_mem := set.mem_Inter.2 $ λ y, (h y).one_mem, mul_mem := λ x₁ x₂ h₁ h₂, set.mem_Inter.2 $ λ y, (h y).mul_mem (set.mem_Inter.1 h₁ y) (set.mem_Inter.1 h₂ y) } /-- The union of an indexed, directed, nonempty set of submonoids of a monoid `M` is a submonoid of `M`. -/ @[to_additive "The union of an indexed, directed, nonempty set of `add_submonoid`s of an `add_monoid` `M` is an `add_submonoid` of `M`. "] lemma is_submonoid_Union_of_directed {ι : Type*} [hι : nonempty ι] {s : ι → set M} (hs : ∀ i, is_submonoid (s i)) (directed : ∀ i j, ∃ k, s i ⊆ s k ∧ s j ⊆ s k) : is_submonoid (⋃i, s i) := { one_mem := let ⟨i⟩ := hι in set.mem_Union.2 ⟨i, (hs i).one_mem⟩, mul_mem := λ a b ha hb, let ⟨i, hi⟩ := set.mem_Union.1 ha in let ⟨j, hj⟩ := set.mem_Union.1 hb in let ⟨k, hk⟩ := directed i j in set.mem_Union.2 ⟨k, (hs k).mul_mem (hk.1 hi) (hk.2 hj)⟩ } section powers /-- The set of natural number powers `1, x, x², ...` of an element `x` of a monoid. -/ @[to_additive multiples "The set of natural number multiples `0, x, 2x, ...` of an element `x` of an `add_monoid`."] def powers (x : M) : set M := {y | ∃ n:ℕ, x^n = y} /-- 1 is in the set of natural number powers of an element of a monoid. -/ @[to_additive "0 is in the set of natural number multiples of an element of an `add_monoid`."] lemma powers.one_mem {x : M} : (1 : M) ∈ powers x := ⟨0, pow_zero _⟩ /-- An element of a monoid is in the set of that element's natural number powers. -/ @[to_additive "An element of an `add_monoid` is in the set of that element's natural number multiples."] lemma powers.self_mem {x : M} : x ∈ powers x := ⟨1, pow_one _⟩ /-- The set of natural number powers of an element of a monoid is closed under multiplication. -/ @[to_additive "The set of natural number multiples of an element of an `add_monoid` is closed under addition."] lemma powers.mul_mem {x y z : M} : (y ∈ powers x) → (z ∈ powers x) → (y * z ∈ powers x) := λ ⟨n₁, h₁⟩ ⟨n₂, h₂⟩, ⟨n₁ + n₂, by simp only [pow_add, *]⟩ /-- The set of natural number powers of an element of a monoid `M` is a submonoid of `M`. -/ @[to_additive "The set of natural number multiples of an element of an `add_monoid` `M` is an `add_submonoid` of `M`."] lemma powers.is_submonoid (x : M) : is_submonoid (powers x) := { one_mem := powers.one_mem, mul_mem := λ y z, powers.mul_mem } /-- A monoid is a submonoid of itself. -/ @[to_additive "An `add_monoid` is an `add_submonoid` of itself."] lemma univ.is_submonoid : is_submonoid (@set.univ M) := by split; simp /-- The preimage of a submonoid under a monoid hom is a submonoid of the domain. -/ @[to_additive "The preimage of an `add_submonoid` under an `add_monoid` hom is an `add_submonoid` of the domain."] lemma is_submonoid.preimage {N : Type*} [monoid N] {f : M → N} (hf : is_monoid_hom f) {s : set N} (hs : is_submonoid s) : is_submonoid (f ⁻¹' s) := { one_mem := show f 1 ∈ s, by rw is_monoid_hom.map_one hf; exact hs.one_mem, mul_mem := λ a b (ha : f a ∈ s) (hb : f b ∈ s), show f (a * b) ∈ s, by rw is_monoid_hom.map_mul hf; exact hs.mul_mem ha hb } /-- The image of a submonoid under a monoid hom is a submonoid of the codomain. -/ @[to_additive "The image of an `add_submonoid` under an `add_monoid` hom is an `add_submonoid` of the codomain."] lemma is_submonoid.image {γ : Type*} [monoid γ] {f : M → γ} (hf : is_monoid_hom f) {s : set M} (hs : is_submonoid s) : is_submonoid (f '' s) := { one_mem := ⟨1, hs.one_mem, hf.map_one⟩, mul_mem := λ a b ⟨x, hx⟩ ⟨y, hy⟩, ⟨x * y, hs.mul_mem hx.1 hy.1, by rw [hf.map_mul, hx.2, hy.2]⟩ } /-- The image of a monoid hom is a submonoid of the codomain. -/ @[to_additive "The image of an `add_monoid` hom is an `add_submonoid` of the codomain."] lemma range.is_submonoid {γ : Type*} [monoid γ] {f : M → γ} (hf : is_monoid_hom f) : is_submonoid (set.range f) := by { rw ← set.image_univ, exact univ.is_submonoid.image hf } /-- Submonoids are closed under natural powers. -/ @[to_additive is_add_submonoid.smul_mem "An `add_submonoid` is closed under multiplication by naturals."] lemma is_submonoid.pow_mem {a : M} (hs : is_submonoid s) (h : a ∈ s) : ∀ {n : ℕ}, a ^ n ∈ s | 0 := by { rw pow_zero, exact hs.one_mem } | (n + 1) := by { rw pow_succ, exact hs.mul_mem h is_submonoid.pow_mem } /-- The set of natural number powers of an element of a submonoid is a subset of the submonoid. -/ @[to_additive is_add_submonoid.multiples_subset "The set of natural number multiples of an element of an `add_submonoid` is a subset of the `add_submonoid`."] lemma is_submonoid.power_subset {a : M} (hs : is_submonoid s) (h : a ∈ s) : powers a ⊆ s := assume x ⟨n, hx⟩, hx ▸ hs.pow_mem h end powers namespace is_submonoid /-- The product of a list of elements of a submonoid is an element of the submonoid. -/ @[to_additive "The sum of a list of elements of an `add_submonoid` is an element of the `add_submonoid`."] lemma list_prod_mem (hs : is_submonoid s) : ∀{l : list M}, (∀x∈l, x ∈ s) → l.prod ∈ s | [] h := hs.one_mem | (a::l) h := suffices a * l.prod ∈ s, by simpa, have a ∈ s ∧ (∀x∈l, x ∈ s), by simpa using h, hs.mul_mem this.1 (list_prod_mem this.2) /-- The product of a multiset of elements of a submonoid of a `comm_monoid` is an element of the submonoid. -/ @[to_additive "The sum of a multiset of elements of an `add_submonoid` of an `add_comm_monoid` is an element of the `add_submonoid`. "] lemma multiset_prod_mem {M} [comm_monoid M] {s : set M} (hs : is_submonoid s) (m : multiset M) : (∀a∈m, a ∈ s) → m.prod ∈ s := begin refine quotient.induction_on m (assume l hl, _), rw [multiset.quot_mk_to_coe, multiset.coe_prod], exact list_prod_mem hs hl end /-- The product of elements of a submonoid of a `comm_monoid` indexed by a `finset` is an element of the submonoid. -/ @[to_additive "The sum of elements of an `add_submonoid` of an `add_comm_monoid` indexed by a `finset` is an element of the `add_submonoid`."] lemma finset_prod_mem {M A} [comm_monoid M] {s : set M} (hs : is_submonoid s) (f : A → M) : ∀(t : finset A), (∀b∈t, f b ∈ s) → ∏ b in t, f b ∈ s | ⟨m, hm⟩ _ := multiset_prod_mem hs _ (by simpa) end is_submonoid namespace add_monoid /-- The inductively defined membership predicate for the submonoid generated by a subset of a monoid. -/ inductive in_closure (s : set A) : A → Prop | basic {a : A} : a ∈ s → in_closure a | zero : in_closure 0 | add {a b : A} : in_closure a → in_closure b → in_closure (a + b) end add_monoid namespace monoid /-- The inductively defined membership predicate for the `submonoid` generated by a subset of an monoid. -/ @[to_additive] inductive in_closure (s : set M) : M → Prop | basic {a : M} : a ∈ s → in_closure a | one : in_closure 1 | mul {a b : M} : in_closure a → in_closure b → in_closure (a * b) /-- The inductively defined submonoid generated by a subset of a monoid. -/ @[to_additive "The inductively defined `add_submonoid` genrated by a subset of an `add_monoid`."] def closure (s : set M) : set M := {a | in_closure s a } @[to_additive] lemma closure.is_submonoid (s : set M) : is_submonoid (closure s) := { one_mem := in_closure.one, mul_mem := assume a b, in_closure.mul } /-- A subset of a monoid is contained in the submonoid it generates. -/ @[to_additive "A subset of an `add_monoid` is contained in the `add_submonoid` it generates."] theorem subset_closure {s : set M} : s ⊆ closure s := assume a, in_closure.basic /-- The submonoid generated by a set is contained in any submonoid that contains the set. -/ @[to_additive "The `add_submonoid` generated by a set is contained in any `add_submonoid` that contains the set."] theorem closure_subset {s t : set M} (ht : is_submonoid t) (h : s ⊆ t) : closure s ⊆ t := assume a ha, by induction ha; simp [h _, *, is_submonoid.one_mem, is_submonoid.mul_mem] /-- Given subsets `t` and `s` of a monoid `M`, if `s ⊆ t`, the submonoid of `M` generated by `s` is contained in the submonoid generated by `t`. -/ @[to_additive "Given subsets `t` and `s` of an `add_monoid M`, if `s ⊆ t`, the `add_submonoid` of `M` generated by `s` is contained in the `add_submonoid` generated by `t`."] theorem closure_mono {s t : set M} (h : s ⊆ t) : closure s ⊆ closure t := closure_subset (closure.is_submonoid t) $ set.subset.trans h subset_closure /-- The submonoid generated by an element of a monoid equals the set of natural number powers of the element. -/ @[to_additive "The `add_submonoid` generated by an element of an `add_monoid` equals the set of natural number multiples of the element."] theorem closure_singleton {x : M} : closure ({x} : set M) = powers x := set.eq_of_subset_of_subset (closure_subset (powers.is_submonoid x) $ set.singleton_subset_iff.2 $ powers.self_mem) $ is_submonoid.power_subset (closure.is_submonoid _) $ set.singleton_subset_iff.1 $ subset_closure /-- The image under a monoid hom of the submonoid generated by a set equals the submonoid generated by the image of the set under the monoid hom. -/ @[to_additive "The image under an `add_monoid` hom of the `add_submonoid` generated by a set equals the `add_submonoid` generated by the image of the set under the `add_monoid` hom."] lemma image_closure {A : Type*} [monoid A] {f : M → A} (hf : is_monoid_hom f) (s : set M) : f '' closure s = closure (f '' s) := le_antisymm begin rintros _ ⟨x, hx, rfl⟩, apply in_closure.rec_on hx; intros, { solve_by_elim [subset_closure, set.mem_image_of_mem] }, { rw [hf.map_one], apply is_submonoid.one_mem (closure.is_submonoid (f '' s))}, { rw [hf.map_mul], solve_by_elim [(closure.is_submonoid _).mul_mem] } end (closure_subset (is_submonoid.image hf (closure.is_submonoid _)) $ set.image_subset _ subset_closure) /-- Given an element `a` of the submonoid of a monoid `M` generated by a set `s`, there exists a list of elements of `s` whose product is `a`. -/ @[to_additive "Given an element `a` of the `add_submonoid` of an `add_monoid M` generated by a set `s`, there exists a list of elements of `s` whose sum is `a`."] theorem exists_list_of_mem_closure {s : set M} {a : M} (h : a ∈ closure s) : (∃l:list M, (∀x∈l, x ∈ s) ∧ l.prod = a) := begin induction h, case in_closure.basic : a ha { existsi ([a]), simp [ha] }, case in_closure.one { existsi ([]), simp }, case in_closure.mul : a b _ _ ha hb { rcases ha with ⟨la, ha, eqa⟩, rcases hb with ⟨lb, hb, eqb⟩, existsi (la ++ lb), simp [eqa.symm, eqb.symm, or_imp_distrib], exact assume a, ⟨ha a, hb a⟩ } end /-- Given sets `s, t` of a commutative monoid `M`, `x ∈ M` is in the submonoid of `M` generated by `s ∪ t` iff there exists an element of the submonoid generated by `s` and an element of the submonoid generated by `t` whose product is `x`. -/ @[to_additive "Given sets `s, t` of a commutative `add_monoid M`, `x ∈ M` is in the `add_submonoid` of `M` generated by `s ∪ t` iff there exists an element of the `add_submonoid` generated by `s` and an element of the `add_submonoid` generated by `t` whose sum is `x`."] theorem mem_closure_union_iff {M : Type*} [comm_monoid M] {s t : set M} {x : M} : x ∈ closure (s ∪ t) ↔ ∃ y ∈ closure s, ∃ z ∈ closure t, y * z = x := ⟨λ hx, let ⟨L, HL1, HL2⟩ := exists_list_of_mem_closure hx in HL2 ▸ list.rec_on L (λ _, ⟨1, (closure.is_submonoid _).one_mem, 1, (closure.is_submonoid _).one_mem, mul_one _⟩) (λ hd tl ih HL1, let ⟨y, hy, z, hz, hyzx⟩ := ih (list.forall_mem_of_forall_mem_cons HL1) in or.cases_on (HL1 hd $ list.mem_cons_self _ _) (λ hs, ⟨hd * y, (closure.is_submonoid _).mul_mem (subset_closure hs) hy, z, hz, by rw [mul_assoc, list.prod_cons, ← hyzx]; refl⟩) (λ ht, ⟨y, hy, z * hd, (closure.is_submonoid _).mul_mem hz (subset_closure ht), by rw [← mul_assoc, list.prod_cons, ← hyzx, mul_comm hd]; refl⟩)) HL1, λ ⟨y, hy, z, hz, hyzx⟩, hyzx ▸ (closure.is_submonoid _).mul_mem (closure_mono (set.subset_union_left _ _) hy) (closure_mono (set.subset_union_right _ _) hz)⟩ end monoid /-- Create a bundled submonoid from a set `s` and `[is_submonoid s]`. -/ @[to_additive "Create a bundled additive submonoid from a set `s` and `[is_add_submonoid s]`."] def submonoid.of {s : set M} (h : is_submonoid s) : submonoid M := ⟨s, h.2, h.1⟩ @[to_additive] lemma submonoid.is_submonoid (S : submonoid M) : is_submonoid (S : set M) := ⟨S.3, S.2⟩
bbfc6386e63599c5d6e64dc47fe514da7ecc05eb
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebra/group/basic.lean
d69c65d41f6cba2910a9d86e9e1cad06d05b6c45
[]
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
23,111
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Simon Hudon, Mario Carneiro -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.group.defs import Mathlib.logic.function.basic import Mathlib.PostPort universes u u_1 namespace Mathlib /-- Composing two associative operations of `f : α → α → α` on the left is equal to an associative operation on the left. -/ theorem comp_assoc_left {α : Type u} (f : α → α → α) [is_associative α f] (x : α) (y : α) : f x ∘ f y = f (f x y) := sorry /-- Composing two associative operations of `f : α → α → α` on the right is equal to an associative operation on the right. -/ theorem comp_assoc_right {α : Type u} (f : α → α → α) [is_associative α f] (x : α) (y : α) : ((fun (z : α) => f z x) ∘ fun (z : α) => f z y) = fun (z : α) => f z (f y x) := sorry /-- Composing two multiplications on the left by `y` then `x` is equal to a multiplication on the left by `x * y`. -/ @[simp] theorem comp_mul_left {α : Type u_1} [semigroup α] (x : α) (y : α) : Mul.mul x ∘ Mul.mul y = Mul.mul (x * y) := comp_assoc_left Mul.mul x y /-- Composing two multiplications on the right by `y` and `x` is equal to a multiplication on the right by `y * x`. -/ @[simp] theorem comp_add_right {α : Type u_1} [add_semigroup α] (x : α) (y : α) : ((fun (_x : α) => _x + x) ∘ fun (_x : α) => _x + y) = fun (_x : α) => _x + (y + x) := comp_assoc_right Add.add x y theorem ite_add_zero {M : Type u} [add_monoid M] {P : Prop} [Decidable P] {a : M} {b : M} : ite P (a + b) 0 = ite P a 0 + ite P b 0 := sorry theorem eq_one_iff_eq_one_of_mul_eq_one {M : Type u} [monoid M] {a : M} {b : M} (h : a * b = 1) : a = 1 ↔ b = 1 := sorry theorem add_left_comm {G : Type u} [add_comm_semigroup G] (a : G) (b : G) (c : G) : a + (b + c) = b + (a + c) := left_comm Add.add add_comm add_assoc theorem mul_right_comm {G : Type u} [comm_semigroup G] (a : G) (b : G) (c : G) : a * b * c = a * c * b := right_comm Mul.mul mul_comm mul_assoc theorem add_add_add_comm {G : Type u} [add_comm_semigroup G] (a : G) (b : G) (c : G) (d : G) : a + b + (c + d) = a + c + (b + d) := sorry @[simp] theorem bit0_zero {M : Type u} [add_monoid M] : bit0 0 = 0 := add_zero 0 @[simp] theorem bit1_zero {M : Type u} [add_monoid M] [HasOne M] : bit1 0 = 1 := eq.mpr (id (Eq._oldrec (Eq.refl (bit1 0 = 1)) (bit1.equations._eqn_1 0))) (eq.mpr (id (Eq._oldrec (Eq.refl (bit0 0 + 1 = 1)) bit0_zero)) (eq.mpr (id (Eq._oldrec (Eq.refl (0 + 1 = 1)) (zero_add 1))) (Eq.refl 1))) theorem neg_unique {M : Type u} [add_comm_monoid M] {x : M} {y : M} {z : M} (hy : x + y = 0) (hz : x + z = 0) : y = z := left_neg_eq_right_neg (trans (add_comm y x) hy) hz @[simp] theorem mul_eq_left_iff {M : Type u} [left_cancel_monoid M] {a : M} {b : M} : a * b = a ↔ b = 1 := iff.trans (eq.mpr (id (Eq._oldrec (Eq.refl (a * b = a ↔ a * b = a * 1)) (mul_one a))) (iff.refl (a * b = a))) mul_left_cancel_iff @[simp] theorem left_eq_add_iff {M : Type u} [add_left_cancel_monoid M] {a : M} {b : M} : a = a + b ↔ b = 0 := iff.trans eq_comm add_eq_left_iff @[simp] theorem mul_eq_right_iff {M : Type u} [right_cancel_monoid M] {a : M} {b : M} : a * b = b ↔ a = 1 := iff.trans (eq.mpr (id (Eq._oldrec (Eq.refl (a * b = b ↔ a * b = 1 * b)) (one_mul b))) (iff.refl (a * b = b))) mul_right_cancel_iff @[simp] theorem right_eq_mul_iff {M : Type u} [right_cancel_monoid M] {a : M} {b : M} : b = a * b ↔ a = 1 := iff.trans eq_comm mul_eq_right_iff theorem neg_eq_zero_sub {G : Type u} [sub_neg_monoid G] (x : G) : -x = 0 - x := eq.mpr (id (Eq._oldrec (Eq.refl (-x = 0 - x)) (sub_eq_add_neg 0 x))) (eq.mpr (id (Eq._oldrec (Eq.refl (-x = 0 + -x)) (zero_add (-x)))) (Eq.refl (-x))) theorem mul_one_div {G : Type u} [div_inv_monoid G] (x : G) (y : G) : x * (1 / y) = x / y := eq.mpr (id (Eq._oldrec (Eq.refl (x * (1 / y) = x / y)) (div_eq_mul_inv 1 y))) (eq.mpr (id (Eq._oldrec (Eq.refl (x * (1 * (y⁻¹)) = x / y)) (one_mul (y⁻¹)))) (eq.mpr (id (Eq._oldrec (Eq.refl (x * (y⁻¹) = x / y)) (div_eq_mul_inv x y))) (Eq.refl (x * (y⁻¹))))) theorem mul_div_assoc {G : Type u} [div_inv_monoid G] {a : G} {b : G} {c : G} : a * b / c = a * (b / c) := sorry theorem mul_div_assoc' {G : Type u} [div_inv_monoid G] (a : G) (b : G) (c : G) : a * (b / c) = a * b / c := Eq.symm mul_div_assoc @[simp] theorem one_div {G : Type u} [div_inv_monoid G] (a : G) : 1 / a = (a⁻¹) := Eq.symm (inv_eq_one_div a) @[simp] theorem neg_add_cancel_right {G : Type u} [add_group G] (a : G) (b : G) : a + -b + b = a := sorry @[simp] theorem neg_zero {G : Type u} [add_group G] : -0 = 0 := neg_eq_of_add_eq_zero (zero_add 0) theorem left_inverse_inv (G : Type u_1) [group G] : function.left_inverse (fun (a : G) => a⁻¹) fun (a : G) => a⁻¹ := inv_inv @[simp] theorem inv_involutive {G : Type u} [group G] : function.involutive has_inv.inv := inv_inv theorem neg_injective {G : Type u} [add_group G] : function.injective Neg.neg := function.involutive.injective neg_involutive @[simp] theorem neg_inj {G : Type u} [add_group G] {a : G} {b : G} : -a = -b ↔ a = b := function.injective.eq_iff neg_injective @[simp] theorem add_neg_cancel_left {G : Type u} [add_group G] (a : G) (b : G) : a + (-a + b) = b := eq.mpr (id (Eq._oldrec (Eq.refl (a + (-a + b) = b)) (Eq.symm (add_assoc a (-a) b)))) (eq.mpr (id (Eq._oldrec (Eq.refl (a + -a + b = b)) (add_right_neg a))) (eq.mpr (id (Eq._oldrec (Eq.refl (0 + b = b)) (zero_add b))) (Eq.refl b))) theorem add_left_surjective {G : Type u} [add_group G] (a : G) : function.surjective (Add.add a) := fun (x : G) => Exists.intro (-a + x) (add_neg_cancel_left a x) theorem add_right_surjective {G : Type u} [add_group G] (a : G) : function.surjective fun (x : G) => x + a := fun (x : G) => Exists.intro (x + -a) (neg_add_cancel_right x a) @[simp] theorem mul_inv_rev {G : Type u} [group G] (a : G) (b : G) : a * b⁻¹ = b⁻¹ * (a⁻¹) := sorry theorem eq_neg_of_eq_neg {G : Type u} [add_group G] {a : G} {b : G} (h : a = -b) : b = -a := sorry theorem eq_neg_of_add_eq_zero {G : Type u} [add_group G] {a : G} {b : G} (h : a + b = 0) : a = -b := sorry theorem eq_add_neg_of_add_eq {G : Type u} [add_group G] {a : G} {b : G} {c : G} (h : a + c = b) : a = b + -c := sorry theorem eq_neg_add_of_add_eq {G : Type u} [add_group G] {a : G} {b : G} {c : G} (h : b + a = c) : a = -b + c := sorry theorem neg_add_eq_of_eq_add {G : Type u} [add_group G] {a : G} {b : G} {c : G} (h : b = a + c) : -a + b = c := sorry theorem mul_inv_eq_of_eq_mul {G : Type u} [group G] {a : G} {b : G} {c : G} (h : a = c * b) : a * (b⁻¹) = c := sorry theorem eq_add_of_add_neg_eq {G : Type u} [add_group G] {a : G} {b : G} {c : G} (h : a + -c = b) : a = b + c := sorry theorem eq_mul_of_inv_mul_eq {G : Type u} [group G] {a : G} {b : G} {c : G} (h : b⁻¹ * a = c) : a = b * c := sorry theorem mul_eq_of_eq_inv_mul {G : Type u} [group G] {a : G} {b : G} {c : G} (h : b = a⁻¹ * c) : a * b = c := eq.mpr (id (Eq._oldrec (Eq.refl (a * b = c)) h)) (eq.mpr (id (Eq._oldrec (Eq.refl (a * (a⁻¹ * c) = c)) (mul_inv_cancel_left a c))) (Eq.refl c)) theorem mul_eq_of_eq_mul_inv {G : Type u} [group G] {a : G} {b : G} {c : G} (h : a = c * (b⁻¹)) : a * b = c := sorry theorem add_self_iff_eq_zero {G : Type u} [add_group G] {a : G} : a + a = a ↔ a = 0 := eq.mp (Eq._oldrec (Eq.refl (a + a = a + 0 ↔ a = 0)) (add_zero a)) (add_right_inj a) @[simp] theorem neg_eq_zero {G : Type u} [add_group G] {a : G} : -a = 0 ↔ a = 0 := eq.mpr (id (Eq._oldrec (Eq.refl (-a = 0 ↔ a = 0)) (Eq.symm (propext neg_inj)))) (eq.mpr (id (Eq._oldrec (Eq.refl (-a = 0 ↔ -a = -0)) neg_zero)) (iff.refl (-a = 0))) @[simp] theorem zero_eq_neg {G : Type u} [add_group G] {a : G} : 0 = -a ↔ a = 0 := eq.mpr (id (Eq._oldrec (Eq.refl (0 = -a ↔ a = 0)) (propext eq_comm))) (eq.mpr (id (Eq._oldrec (Eq.refl (-a = 0 ↔ a = 0)) (propext neg_eq_zero))) (iff.refl (a = 0))) theorem neg_ne_zero {G : Type u} [add_group G] {a : G} : -a ≠ 0 ↔ a ≠ 0 := not_congr neg_eq_zero theorem eq_neg_iff_eq_neg {G : Type u} [add_group G] {a : G} {b : G} : a = -b ↔ b = -a := { mp := eq_neg_of_eq_neg, mpr := eq_neg_of_eq_neg } theorem neg_eq_iff_neg_eq {G : Type u} [add_group G] {a : G} {b : G} : -a = b ↔ -b = a := iff.trans eq_comm (iff.trans eq_neg_iff_eq_neg eq_comm) theorem mul_eq_one_iff_eq_inv {G : Type u} [group G] {a : G} {b : G} : a * b = 1 ↔ a = (b⁻¹) := sorry theorem mul_eq_one_iff_inv_eq {G : Type u} [group G] {a : G} {b : G} : a * b = 1 ↔ a⁻¹ = b := eq.mpr (id (Eq._oldrec (Eq.refl (a * b = 1 ↔ a⁻¹ = b)) (propext mul_eq_one_iff_eq_inv))) (eq.mpr (id (Eq._oldrec (Eq.refl (a = (b⁻¹) ↔ a⁻¹ = b)) (propext eq_inv_iff_eq_inv))) (eq.mpr (id (Eq._oldrec (Eq.refl (b = (a⁻¹) ↔ a⁻¹ = b)) (propext eq_comm))) (iff.refl (a⁻¹ = b)))) theorem eq_neg_iff_add_eq_zero {G : Type u} [add_group G] {a : G} {b : G} : a = -b ↔ a + b = 0 := iff.symm add_eq_zero_iff_eq_neg theorem neg_eq_iff_add_eq_zero {G : Type u} [add_group G] {a : G} {b : G} : -a = b ↔ a + b = 0 := iff.symm add_eq_zero_iff_neg_eq theorem eq_mul_inv_iff_mul_eq {G : Type u} [group G] {a : G} {b : G} {c : G} : a = b * (c⁻¹) ↔ a * c = b := sorry theorem eq_inv_mul_iff_mul_eq {G : Type u} [group G] {a : G} {b : G} {c : G} : a = b⁻¹ * c ↔ b * a = c := sorry theorem inv_mul_eq_iff_eq_mul {G : Type u} [group G] {a : G} {b : G} {c : G} : a⁻¹ * b = c ↔ b = a * c := sorry theorem add_neg_eq_iff_eq_add {G : Type u} [add_group G] {a : G} {b : G} {c : G} : a + -b = c ↔ a = c + b := sorry theorem mul_inv_eq_one {G : Type u} [group G] {a : G} {b : G} : a * (b⁻¹) = 1 ↔ a = b := eq.mpr (id (Eq._oldrec (Eq.refl (a * (b⁻¹) = 1 ↔ a = b)) (propext mul_eq_one_iff_eq_inv))) (eq.mpr (id (Eq._oldrec (Eq.refl (a = (b⁻¹⁻¹) ↔ a = b)) (inv_inv b))) (iff.refl (a = b))) theorem inv_mul_eq_one {G : Type u} [group G] {a : G} {b : G} : a⁻¹ * b = 1 ↔ a = b := eq.mpr (id (Eq._oldrec (Eq.refl (a⁻¹ * b = 1 ↔ a = b)) (propext mul_eq_one_iff_eq_inv))) (eq.mpr (id (Eq._oldrec (Eq.refl (a⁻¹ = (b⁻¹) ↔ a = b)) (propext inv_inj))) (iff.refl (a = b))) @[simp] theorem mul_left_eq_self {G : Type u} [group G] {a : G} {b : G} : a * b = b ↔ a = 1 := sorry @[simp] theorem add_right_eq_self {G : Type u} [add_group G] {a : G} {b : G} : a + b = a ↔ b = 0 := sorry theorem sub_left_injective {G : Type u} [add_group G] {b : G} : function.injective fun (a : G) => a - b := sorry theorem div_right_injective {G : Type u} [group G] {b : G} : function.injective fun (a : G) => b / a := sorry @[simp] theorem sub_self {G : Type u} [add_group G] (a : G) : a - a = 0 := eq.mpr (id (Eq._oldrec (Eq.refl (a - a = 0)) (sub_eq_add_neg a a))) (eq.mpr (id (Eq._oldrec (Eq.refl (a + -a = 0)) (add_right_neg a))) (Eq.refl 0)) @[simp] theorem sub_add_cancel {G : Type u} [add_group G] (a : G) (b : G) : a - b + b = a := eq.mpr (id (Eq._oldrec (Eq.refl (a - b + b = a)) (sub_eq_add_neg a b))) (eq.mpr (id (Eq._oldrec (Eq.refl (a + -b + b = a)) (neg_add_cancel_right a b))) (Eq.refl a)) @[simp] theorem add_sub_cancel {G : Type u} [add_group G] (a : G) (b : G) : a + b - b = a := eq.mpr (id (Eq._oldrec (Eq.refl (a + b - b = a)) (sub_eq_add_neg (a + b) b))) (eq.mpr (id (Eq._oldrec (Eq.refl (a + b + -b = a)) (add_neg_cancel_right a b))) (Eq.refl a)) theorem add_sub_assoc {G : Type u} [add_group G] (a : G) (b : G) (c : G) : a + b - c = a + (b - c) := sorry theorem eq_of_sub_eq_zero {G : Type u} [add_group G] {a : G} {b : G} (h : a - b = 0) : a = b := sorry theorem sub_eq_zero_of_eq {G : Type u} [add_group G] {a : G} {b : G} (h : a = b) : a - b = 0 := eq.mpr (id (Eq._oldrec (Eq.refl (a - b = 0)) h)) (eq.mpr (id (Eq._oldrec (Eq.refl (b - b = 0)) (sub_self b))) (Eq.refl 0)) theorem sub_eq_zero_iff_eq {G : Type u} [add_group G] {a : G} {b : G} : a - b = 0 ↔ a = b := { mp := eq_of_sub_eq_zero, mpr := sub_eq_zero_of_eq } @[simp] theorem sub_zero {G : Type u} [add_group G] (a : G) : a - 0 = a := eq.mpr (id (Eq._oldrec (Eq.refl (a - 0 = a)) (sub_eq_add_neg a 0))) (eq.mpr (id (Eq._oldrec (Eq.refl (a + -0 = a)) neg_zero)) (eq.mpr (id (Eq._oldrec (Eq.refl (a + 0 = a)) (add_zero a))) (Eq.refl a))) theorem sub_ne_zero_of_ne {G : Type u} [add_group G] {a : G} {b : G} (h : a ≠ b) : a - b ≠ 0 := id fun (hab : a - b = 0) => h (eq_of_sub_eq_zero hab) @[simp] theorem sub_neg_eq_add {G : Type u} [add_group G] (a : G) (b : G) : a - -b = a + b := eq.mpr (id (Eq._oldrec (Eq.refl (a - -b = a + b)) (sub_eq_add_neg a (-b)))) (eq.mpr (id (Eq._oldrec (Eq.refl (a + --b = a + b)) (neg_neg b))) (Eq.refl (a + b))) @[simp] theorem neg_sub {G : Type u} [add_group G] (a : G) (b : G) : -(a - b) = b - a := sorry theorem add_sub {G : Type u} [add_group G] (a : G) (b : G) (c : G) : a + (b - c) = a + b - c := sorry theorem sub_add_eq_sub_sub_swap {G : Type u} [add_group G] (a : G) (b : G) (c : G) : a - (b + c) = a - c - b := sorry @[simp] theorem add_sub_add_right_eq_sub {G : Type u} [add_group G] (a : G) (b : G) (c : G) : a + c - (b + c) = a - b := sorry theorem eq_sub_of_add_eq {G : Type u} [add_group G] {a : G} {b : G} {c : G} (h : a + c = b) : a = b - c := sorry theorem sub_eq_of_eq_add {G : Type u} [add_group G] {a : G} {b : G} {c : G} (h : a = c + b) : a - b = c := sorry theorem eq_add_of_sub_eq {G : Type u} [add_group G] {a : G} {b : G} {c : G} (h : a - c = b) : a = b + c := sorry theorem add_eq_of_eq_sub {G : Type u} [add_group G] {a : G} {b : G} {c : G} (h : a = c - b) : a + b = c := sorry @[simp] theorem sub_right_inj {G : Type u} [add_group G] {a : G} {b : G} {c : G} : a - b = a - c ↔ b = c := function.injective.eq_iff sub_right_injective @[simp] theorem sub_left_inj {G : Type u} [add_group G] {a : G} {b : G} {c : G} : b - a = c - a ↔ b = c := eq.mpr (id (Eq._oldrec (Eq.refl (b - a = c - a ↔ b = c)) (sub_eq_add_neg b a))) (eq.mpr (id (Eq._oldrec (Eq.refl (b + -a = c - a ↔ b = c)) (sub_eq_add_neg c a))) (add_left_inj (-a))) theorem sub_add_sub_cancel {G : Type u} [add_group G] (a : G) (b : G) (c : G) : a - b + (b - c) = a - c := eq.mpr (id (Eq._oldrec (Eq.refl (a - b + (b - c) = a - c)) (Eq.symm (add_sub_assoc (a - b) b c)))) (eq.mpr (id (Eq._oldrec (Eq.refl (a - b + b - c = a - c)) (sub_add_cancel a b))) (Eq.refl (a - c))) theorem sub_sub_sub_cancel_right {G : Type u} [add_group G] (a : G) (b : G) (c : G) : a - c - (b - c) = a - b := eq.mpr (id (Eq._oldrec (Eq.refl (a - c - (b - c) = a - b)) (Eq.symm (neg_sub c b)))) (eq.mpr (id (Eq._oldrec (Eq.refl (a - c - -(c - b) = a - b)) (sub_neg_eq_add (a - c) (c - b)))) (eq.mpr (id (Eq._oldrec (Eq.refl (a - c + (c - b) = a - b)) (sub_add_sub_cancel a c b))) (Eq.refl (a - b)))) theorem sub_sub_assoc_swap {G : Type u} [add_group G] {a : G} {b : G} {c : G} : a - (b - c) = a + c - b := sorry theorem sub_eq_zero {G : Type u} [add_group G] {a : G} {b : G} : a - b = 0 ↔ a = b := sorry theorem sub_ne_zero {G : Type u} [add_group G] {a : G} {b : G} : a - b ≠ 0 ↔ a ≠ b := not_congr sub_eq_zero theorem eq_sub_iff_add_eq {G : Type u} [add_group G] {a : G} {b : G} {c : G} : a = b - c ↔ a + c = b := eq.mpr (id (Eq._oldrec (Eq.refl (a = b - c ↔ a + c = b)) (sub_eq_add_neg b c))) (eq.mpr (id (Eq._oldrec (Eq.refl (a = b + -c ↔ a + c = b)) (propext eq_add_neg_iff_add_eq))) (iff.refl (a + c = b))) theorem sub_eq_iff_eq_add {G : Type u} [add_group G] {a : G} {b : G} {c : G} : a - b = c ↔ a = c + b := eq.mpr (id (Eq._oldrec (Eq.refl (a - b = c ↔ a = c + b)) (sub_eq_add_neg a b))) (eq.mpr (id (Eq._oldrec (Eq.refl (a + -b = c ↔ a = c + b)) (propext add_neg_eq_iff_eq_add))) (iff.refl (a = c + b))) theorem eq_iff_eq_of_sub_eq_sub {G : Type u} [add_group G] {a : G} {b : G} {c : G} {d : G} (H : a - b = c - d) : a = b ↔ c = d := eq.mpr (id (Eq._oldrec (Eq.refl (a = b ↔ c = d)) (Eq.symm (propext sub_eq_zero)))) (eq.mpr (id (Eq._oldrec (Eq.refl (a - b = 0 ↔ c = d)) H)) (eq.mpr (id (Eq._oldrec (Eq.refl (c - d = 0 ↔ c = d)) (propext sub_eq_zero))) (iff.refl (c = d)))) theorem left_inverse_sub_add_left {G : Type u} [add_group G] (c : G) : function.left_inverse (fun (x : G) => x - c) fun (x : G) => x + c := fun (x : G) => add_sub_cancel x c theorem left_inverse_add_left_sub {G : Type u} [add_group G] (c : G) : function.left_inverse (fun (x : G) => x + c) fun (x : G) => x - c := fun (x : G) => sub_add_cancel x c theorem left_inverse_add_right_neg_add {G : Type u} [add_group G] (c : G) : function.left_inverse (fun (x : G) => c + x) fun (x : G) => -c + x := fun (x : G) => add_neg_cancel_left c x theorem left_inverse_neg_add_add_right {G : Type u} [add_group G] (c : G) : function.left_inverse (fun (x : G) => -c + x) fun (x : G) => c + x := fun (x : G) => neg_add_cancel_left c x theorem neg_add {G : Type u} [add_comm_group G] (a : G) (b : G) : -(a + b) = -a + -b := eq.mpr (id (Eq._oldrec (Eq.refl (-(a + b) = -a + -b)) (neg_add_rev a b))) (eq.mpr (id (Eq._oldrec (Eq.refl (-b + -a = -a + -b)) (add_comm (-b) (-a)))) (Eq.refl (-a + -b))) theorem sub_add_eq_sub_sub {G : Type u} [add_comm_group G] (a : G) (b : G) (c : G) : a - (b + c) = a - b - c := sorry theorem neg_add_eq_sub {G : Type u} [add_comm_group G] (a : G) (b : G) : -a + b = b - a := sorry theorem sub_add_eq_add_sub {G : Type u} [add_comm_group G] (a : G) (b : G) (c : G) : a - b + c = a + c - b := sorry theorem sub_sub {G : Type u} [add_comm_group G] (a : G) (b : G) (c : G) : a - b - c = a - (b + c) := sorry theorem sub_add {G : Type u} [add_comm_group G] (a : G) (b : G) (c : G) : a - b + c = a - (b - c) := sorry @[simp] theorem add_sub_add_left_eq_sub {G : Type u} [add_comm_group G] (a : G) (b : G) (c : G) : c + a - (c + b) = a - b := sorry theorem eq_sub_of_add_eq' {G : Type u} [add_comm_group G] {a : G} {b : G} {c : G} (h : c + a = b) : a = b - c := sorry theorem sub_eq_of_eq_add' {G : Type u} [add_comm_group G] {a : G} {b : G} {c : G} (h : a = b + c) : a - b = c := sorry theorem eq_add_of_sub_eq' {G : Type u} [add_comm_group G] {a : G} {b : G} {c : G} (h : a - b = c) : a = b + c := sorry theorem add_eq_of_eq_sub' {G : Type u} [add_comm_group G] {a : G} {b : G} {c : G} (h : b = c - a) : a + b = c := sorry theorem sub_sub_self {G : Type u} [add_comm_group G] (a : G) (b : G) : a - (a - b) = b := sorry theorem add_sub_comm {G : Type u} [add_comm_group G] (a : G) (b : G) (c : G) (d : G) : a + b - (c + d) = a - c + (b - d) := sorry theorem sub_eq_sub_add_sub {G : Type u} [add_comm_group G] (a : G) (b : G) (c : G) : a - b = c - b + (a - c) := sorry theorem neg_neg_sub_neg {G : Type u} [add_comm_group G] (a : G) (b : G) : -(-a - -b) = a - b := sorry @[simp] theorem sub_sub_cancel {G : Type u} [add_comm_group G] (a : G) (b : G) : a - (a - b) = b := sub_sub_self a b theorem sub_eq_neg_add {G : Type u} [add_comm_group G] (a : G) (b : G) : a - b = -b + a := eq.mpr (id (Eq._oldrec (Eq.refl (a - b = -b + a)) (sub_eq_add_neg a b))) (eq.mpr (id (Eq._oldrec (Eq.refl (a + -b = -b + a)) (add_comm a (-b)))) (Eq.refl (-b + a))) theorem neg_add' {G : Type u} [add_comm_group G] (a : G) (b : G) : -(a + b) = -a - b := eq.mpr (id (Eq._oldrec (Eq.refl (-(a + b) = -a - b)) (sub_eq_add_neg (-a) b))) (eq.mpr (id (Eq._oldrec (Eq.refl (-(a + b) = -a + -b)) (neg_add a b))) (Eq.refl (-a + -b))) @[simp] theorem neg_sub_neg {G : Type u} [add_comm_group G] (a : G) (b : G) : -a - -b = b - a := sorry theorem eq_sub_iff_add_eq' {G : Type u} [add_comm_group G] {a : G} {b : G} {c : G} : a = b - c ↔ c + a = b := eq.mpr (id (Eq._oldrec (Eq.refl (a = b - c ↔ c + a = b)) (propext eq_sub_iff_add_eq))) (eq.mpr (id (Eq._oldrec (Eq.refl (a + c = b ↔ c + a = b)) (add_comm a c))) (iff.refl (c + a = b))) theorem sub_eq_iff_eq_add' {G : Type u} [add_comm_group G] {a : G} {b : G} {c : G} : a - b = c ↔ a = b + c := eq.mpr (id (Eq._oldrec (Eq.refl (a - b = c ↔ a = b + c)) (propext sub_eq_iff_eq_add))) (eq.mpr (id (Eq._oldrec (Eq.refl (a = c + b ↔ a = b + c)) (add_comm c b))) (iff.refl (a = b + c))) @[simp] theorem add_sub_cancel' {G : Type u} [add_comm_group G] (a : G) (b : G) : a + b - a = b := eq.mpr (id (Eq._oldrec (Eq.refl (a + b - a = b)) (sub_eq_neg_add (a + b) a))) (eq.mpr (id (Eq._oldrec (Eq.refl (-a + (a + b) = b)) (neg_add_cancel_left a b))) (Eq.refl b)) @[simp] theorem add_sub_cancel'_right {G : Type u} [add_comm_group G] (a : G) (b : G) : a + (b - a) = b := eq.mpr (id (Eq._oldrec (Eq.refl (a + (b - a) = b)) (Eq.symm (add_sub_assoc a b a)))) (eq.mpr (id (Eq._oldrec (Eq.refl (a + b - a = b)) (add_sub_cancel' a b))) (Eq.refl b)) -- This lemma is in the `simp` set under the name `add_neg_cancel_comm_assoc`, -- defined in `algebra/group/commute` theorem add_add_neg_cancel'_right {G : Type u} [add_comm_group G] (a : G) (b : G) : a + (b + -a) = b := eq.mpr (id (Eq._oldrec (Eq.refl (a + (b + -a) = b)) (Eq.symm (sub_eq_add_neg b a)))) (eq.mpr (id (Eq._oldrec (Eq.refl (a + (b - a) = b)) (add_sub_cancel'_right a b))) (Eq.refl b)) theorem sub_right_comm {G : Type u} [add_comm_group G] (a : G) (b : G) (c : G) : a - b - c = a - c - b := sorry @[simp] theorem add_add_sub_cancel {G : Type u} [add_comm_group G] (a : G) (b : G) (c : G) : a + c + (b - c) = a + b := eq.mpr (id (Eq._oldrec (Eq.refl (a + c + (b - c) = a + b)) (add_assoc a c (b - c)))) (eq.mpr (id (Eq._oldrec (Eq.refl (a + (c + (b - c)) = a + b)) (add_sub_cancel'_right c b))) (Eq.refl (a + b))) @[simp] theorem sub_add_add_cancel {G : Type u} [add_comm_group G] (a : G) (b : G) (c : G) : a - c + (b + c) = a + b := eq.mpr (id (Eq._oldrec (Eq.refl (a - c + (b + c) = a + b)) (add_left_comm (a - c) b c))) (eq.mpr (id (Eq._oldrec (Eq.refl (b + (a - c + c) = a + b)) (sub_add_cancel a c))) (eq.mpr (id (Eq._oldrec (Eq.refl (b + a = a + b)) (add_comm b a))) (Eq.refl (a + b)))) @[simp] theorem sub_add_sub_cancel' {G : Type u} [add_comm_group G] (a : G) (b : G) (c : G) : a - b + (c - a) = c - b := eq.mpr (id (Eq._oldrec (Eq.refl (a - b + (c - a) = c - b)) (add_comm (a - b) (c - a)))) (sub_add_sub_cancel c a b) @[simp] theorem add_sub_sub_cancel {G : Type u} [add_comm_group G] (a : G) (b : G) (c : G) : a + b - (a - c) = b + c := eq.mpr (id (Eq._oldrec (Eq.refl (a + b - (a - c) = b + c)) (Eq.symm (sub_add (a + b) a c)))) (eq.mpr (id (Eq._oldrec (Eq.refl (a + b - a + c = b + c)) (add_sub_cancel' a b))) (Eq.refl (b + c))) @[simp] theorem sub_sub_sub_cancel_left {G : Type u} [add_comm_group G] (a : G) (b : G) (c : G) : c - a - (c - b) = b - a := sorry theorem sub_eq_sub_iff_add_eq_add {G : Type u} [add_comm_group G] {a : G} {b : G} {c : G} {d : G} : a - b = c - d ↔ a + d = c + b := sorry theorem sub_eq_sub_iff_sub_eq_sub {G : Type u} [add_comm_group G] {a : G} {b : G} {c : G} {d : G} : a - b = c - d ↔ a - c = b - d := sorry
d93e1c2bd9f4c6c0a5b40195d1b6243ffc3e7bd8
49ffcd4736fa3bdcc1cdbb546d4c855d67c0f28a
/library/init/algebra/ordered_group.lean
7f71d40289fa5d953558e938c5398d9d25e29006
[ "Apache-2.0" ]
permissive
black13/lean
979e24d09e17b2fdf8ec74aac160583000086bc8
1a80ea9c8e28902cadbfb612896bcd45ba4ce697
refs/heads/master
1,626,839,620,164
1,509,113,016,000
1,509,122,889,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
20,011
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura -/ prelude import init.algebra.order init.algebra.group /- Make sure instances defined in this file have lower priority than the ones defined for concrete structures -/ set_option default_priority 100 set_option old_structure_cmd true universe u class ordered_cancel_comm_monoid (α : Type u) extends add_comm_monoid α, add_left_cancel_semigroup α, add_right_cancel_semigroup α, partial_order α := (add_le_add_left : ∀ a b : α, a ≤ b → ∀ c : α, c + a ≤ c + b) (le_of_add_le_add_left : ∀ a b c : α, a + b ≤ a + c → b ≤ c) section ordered_cancel_comm_monoid variable {α : Type u} variable [s : ordered_cancel_comm_monoid α] lemma add_le_add_left {a b : α} (h : a ≤ b) (c : α) : c + a ≤ c + b := @ordered_cancel_comm_monoid.add_le_add_left α s a b h c lemma le_of_add_le_add_left {a b c : α} (h : a + b ≤ a + c) : b ≤ c := @ordered_cancel_comm_monoid.le_of_add_le_add_left α s a b c h end ordered_cancel_comm_monoid section ordered_cancel_comm_monoid variable {α : Type u} variable [ordered_cancel_comm_monoid α] lemma add_lt_add_left {a b : α} (h : a < b) (c : α) : c + a < c + b := lt_of_le_not_le (add_le_add_left (le_of_lt h) _) $ mt le_of_add_le_add_left (not_le_of_gt h) lemma lt_of_add_lt_add_left {a b c : α} (h : a + b < a + c) : b < c := lt_of_le_not_le (le_of_add_le_add_left (le_of_lt h)) $ mt (λ h, add_le_add_left h _) (not_le_of_gt h) lemma add_le_add_right {a b : α} (h : a ≤ b) (c : α) : a + c ≤ b + c := add_comm c a ▸ add_comm c b ▸ add_le_add_left h c theorem add_lt_add_right {a b : α} (h : a < b) (c : α) : a + c < b + c := begin rw [add_comm a c, add_comm b c], exact (add_lt_add_left h c) end lemma add_le_add {a b c d : α} (h₁ : a ≤ b) (h₂ : c ≤ d) : a + c ≤ b + d := le_trans (add_le_add_right h₁ c) (add_le_add_left h₂ b) lemma le_add_of_nonneg_right {a b : α} (h : b ≥ 0) : a ≤ a + b := have a + b ≥ a + 0, from add_le_add_left h a, by rwa add_zero at this lemma le_add_of_nonneg_left {a b : α} (h : b ≥ 0) : a ≤ b + a := have 0 + a ≤ b + a, from add_le_add_right h a, by rwa zero_add at this lemma add_lt_add {a b c d : α} (h₁ : a < b) (h₂ : c < d) : a + c < b + d := lt_trans (add_lt_add_right h₁ c) (add_lt_add_left h₂ b) lemma add_lt_add_of_le_of_lt {a b c d : α} (h₁ : a ≤ b) (h₂ : c < d) : a + c < b + d := lt_of_le_of_lt (add_le_add_right h₁ c) (add_lt_add_left h₂ b) lemma add_lt_add_of_lt_of_le {a b c d : α} (h₁ : a < b) (h₂ : c ≤ d) : a + c < b + d := lt_of_lt_of_le (add_lt_add_right h₁ c) (add_le_add_left h₂ b) lemma lt_add_of_pos_right (a : α) {b : α} (h : b > 0) : a < a + b := have a + 0 < a + b, from add_lt_add_left h a, by rwa [add_zero] at this lemma lt_add_of_pos_left (a : α) {b : α} (h : b > 0) : a < b + a := have 0 + a < b + a, from add_lt_add_right h a, by rwa [zero_add] at this lemma le_of_add_le_add_right {a b c : α} (h : a + b ≤ c + b) : a ≤ c := le_of_add_le_add_left (show b + a ≤ b + c, begin rw [add_comm b a, add_comm b c], assumption end) lemma lt_of_add_lt_add_right {a b c : α} (h : a + b < c + b) : a < c := lt_of_add_lt_add_left (show b + a < b + c, begin rw [add_comm b a, add_comm b c], assumption end) -- here we start using properties of zero. lemma add_nonneg {a b : α} (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a + b := zero_add (0:α) ▸ (add_le_add ha hb) lemma add_pos {a b : α} (ha : 0 < a) (hb : 0 < b) : 0 < a + b := zero_add (0:α) ▸ (add_lt_add ha hb) lemma add_pos_of_pos_of_nonneg {a b : α} (ha : 0 < a) (hb : 0 ≤ b) : 0 < a + b := zero_add (0:α) ▸ (add_lt_add_of_lt_of_le ha hb) lemma add_pos_of_nonneg_of_pos {a b : α} (ha : 0 ≤ a) (hb : 0 < b) : 0 < a + b := zero_add (0:α) ▸ (add_lt_add_of_le_of_lt ha hb) lemma add_nonpos {a b : α} (ha : a ≤ 0) (hb : b ≤ 0) : a + b ≤ 0 := zero_add (0:α) ▸ (add_le_add ha hb) lemma add_neg {a b : α} (ha : a < 0) (hb : b < 0) : a + b < 0 := zero_add (0:α) ▸ (add_lt_add ha hb) lemma add_neg_of_neg_of_nonpos {a b : α} (ha : a < 0) (hb : b ≤ 0) : a + b < 0 := zero_add (0:α) ▸ (add_lt_add_of_lt_of_le ha hb) lemma add_neg_of_nonpos_of_neg {a b : α} (ha : a ≤ 0) (hb : b < 0) : a + b < 0 := zero_add (0:α) ▸ (add_lt_add_of_le_of_lt ha hb) lemma add_eq_zero_iff_eq_zero_and_eq_zero_of_nonneg_of_nonneg {a b : α} (ha : 0 ≤ a) (hb : 0 ≤ b) : a + b = 0 ↔ a = 0 ∧ b = 0 := iff.intro (assume hab : a + b = 0, have ha' : a ≤ 0, from calc a = a + 0 : by rw add_zero ... ≤ a + b : add_le_add_left hb _ ... = 0 : hab, have haz : a = 0, from le_antisymm ha' ha, have hb' : b ≤ 0, from calc b = 0 + b : by rw zero_add ... ≤ a + b : by exact add_le_add_right ha _ ... = 0 : hab, have hbz : b = 0, from le_antisymm hb' hb, and.intro haz hbz) (assume ⟨ha', hb'⟩, by rw [ha', hb', add_zero]) lemma le_add_of_nonneg_of_le {a b c : α} (ha : 0 ≤ a) (hbc : b ≤ c) : b ≤ a + c := zero_add b ▸ add_le_add ha hbc lemma le_add_of_le_of_nonneg {a b c : α} (hbc : b ≤ c) (ha : 0 ≤ a) : b ≤ c + a := add_zero b ▸ add_le_add hbc ha lemma lt_add_of_pos_of_le {a b c : α} (ha : 0 < a) (hbc : b ≤ c) : b < a + c := zero_add b ▸ add_lt_add_of_lt_of_le ha hbc lemma lt_add_of_le_of_pos {a b c : α} (hbc : b ≤ c) (ha : 0 < a) : b < c + a := add_zero b ▸ add_lt_add_of_le_of_lt hbc ha lemma add_le_of_nonpos_of_le {a b c : α} (ha : a ≤ 0) (hbc : b ≤ c) : a + b ≤ c := zero_add c ▸ add_le_add ha hbc lemma add_le_of_le_of_nonpos {a b c : α} (hbc : b ≤ c) (ha : a ≤ 0) : b + a ≤ c := add_zero c ▸ add_le_add hbc ha lemma add_lt_of_neg_of_le {a b c : α} (ha : a < 0) (hbc : b ≤ c) : a + b < c := zero_add c ▸ add_lt_add_of_lt_of_le ha hbc lemma add_lt_of_le_of_neg {a b c : α} (hbc : b ≤ c) (ha : a < 0) : b + a < c := add_zero c ▸ add_lt_add_of_le_of_lt hbc ha lemma lt_add_of_nonneg_of_lt {a b c : α} (ha : 0 ≤ a) (hbc : b < c) : b < a + c := zero_add b ▸ add_lt_add_of_le_of_lt ha hbc lemma lt_add_of_lt_of_nonneg {a b c : α} (hbc : b < c) (ha : 0 ≤ a) : b < c + a := add_zero b ▸ add_lt_add_of_lt_of_le hbc ha lemma lt_add_of_pos_of_lt {a b c : α} (ha : 0 < a) (hbc : b < c) : b < a + c := zero_add b ▸ add_lt_add ha hbc lemma lt_add_of_lt_of_pos {a b c : α} (hbc : b < c) (ha : 0 < a) : b < c + a := add_zero b ▸ add_lt_add hbc ha lemma add_lt_of_nonpos_of_lt {a b c : α} (ha : a ≤ 0) (hbc : b < c) : a + b < c := zero_add c ▸ add_lt_add_of_le_of_lt ha hbc lemma add_lt_of_lt_of_nonpos {a b c : α} (hbc : b < c) (ha : a ≤ 0) : b + a < c := add_zero c ▸ add_lt_add_of_lt_of_le hbc ha lemma add_lt_of_neg_of_lt {a b c : α} (ha : a < 0) (hbc : b < c) : a + b < c := zero_add c ▸ add_lt_add ha hbc lemma add_lt_of_lt_of_neg {a b c : α} (hbc : b < c) (ha : a < 0) : b + a < c := add_zero c ▸ add_lt_add hbc ha end ordered_cancel_comm_monoid class ordered_comm_group (α : Type u) extends add_comm_group α, partial_order α := (add_le_add_left : ∀ a b : α, a ≤ b → ∀ c : α, c + a ≤ c + b) (add_lt_add_left : ∀ a b : α, a < b → ∀ c : α, c + a < c + b) section ordered_comm_group variable {α : Type u} variable [ordered_comm_group α] lemma ordered_comm_group.le_of_add_le_add_left {a b c : α} (h : a + b ≤ a + c) : b ≤ c := have -a + (a + b) ≤ -a + (a + c), from ordered_comm_group.add_le_add_left _ _ h _, begin simp [neg_add_cancel_left] at this, assumption end lemma ordered_comm_group.lt_of_add_lt_add_left {a b c : α} (h : a + b < a + c) : b < c := have -a + (a + b) < -a + (a + c), from ordered_comm_group.add_lt_add_left _ _ h _, begin simp [neg_add_cancel_left] at this, assumption end end ordered_comm_group instance ordered_comm_group.to_ordered_cancel_comm_monoid (α : Type u) [s : ordered_comm_group α] : ordered_cancel_comm_monoid α := { s with add_left_cancel := @add_left_cancel α _, add_right_cancel := @add_right_cancel α _, le_of_add_le_add_left := @ordered_comm_group.le_of_add_le_add_left α _ } section ordered_comm_group variables {α : Type u} [ordered_comm_group α] lemma neg_le_neg {a b : α} (h : a ≤ b) : -b ≤ -a := have 0 ≤ -a + b, from add_left_neg a ▸ add_le_add_left h (-a), have 0 + -b ≤ -a + b + -b, from add_le_add_right this (-b), by rwa [add_neg_cancel_right, zero_add] at this lemma le_of_neg_le_neg {a b : α} (h : -b ≤ -a) : a ≤ b := suffices -(-a) ≤ -(-b), from begin simp [neg_neg] at this, assumption end, neg_le_neg h lemma nonneg_of_neg_nonpos {a : α} (h : -a ≤ 0) : 0 ≤ a := have -a ≤ -0, by rwa neg_zero, le_of_neg_le_neg this lemma neg_nonpos_of_nonneg {a : α} (h : 0 ≤ a) : -a ≤ 0 := have -a ≤ -0, from neg_le_neg h, by rwa neg_zero at this lemma nonpos_of_neg_nonneg {a : α} (h : 0 ≤ -a) : a ≤ 0 := have -0 ≤ -a, by rwa neg_zero, le_of_neg_le_neg this lemma neg_nonneg_of_nonpos {a : α} (h : a ≤ 0) : 0 ≤ -a := have -0 ≤ -a, from neg_le_neg h, by rwa neg_zero at this lemma neg_lt_neg {a b : α} (h : a < b) : -b < -a := have 0 < -a + b, from add_left_neg a ▸ add_lt_add_left h (-a), have 0 + -b < -a + b + -b, from add_lt_add_right this (-b), by rwa [add_neg_cancel_right, zero_add] at this lemma lt_of_neg_lt_neg {a b : α} (h : -b < -a) : a < b := neg_neg a ▸ neg_neg b ▸ neg_lt_neg h lemma pos_of_neg_neg {a : α} (h : -a < 0) : 0 < a := have -a < -0, by rwa neg_zero, lt_of_neg_lt_neg this lemma neg_neg_of_pos {a : α} (h : 0 < a) : -a < 0 := have -a < -0, from neg_lt_neg h, by rwa neg_zero at this lemma neg_of_neg_pos {a : α} (h : 0 < -a) : a < 0 := have -0 < -a, by rwa neg_zero, lt_of_neg_lt_neg this lemma neg_pos_of_neg {a : α} (h : a < 0) : 0 < -a := have -0 < -a, from neg_lt_neg h, by rwa neg_zero at this lemma le_neg_of_le_neg {a b : α} (h : a ≤ -b) : b ≤ -a := begin have h := neg_le_neg h, rwa neg_neg at h end lemma neg_le_of_neg_le {a b : α} (h : -a ≤ b) : -b ≤ a := begin have h := neg_le_neg h, rwa neg_neg at h end lemma lt_neg_of_lt_neg {a b : α} (h : a < -b) : b < -a := begin have h := neg_lt_neg h, rwa neg_neg at h end lemma neg_lt_of_neg_lt {a b : α} (h : -a < b) : -b < a := begin have h := neg_lt_neg h, rwa neg_neg at h end lemma sub_nonneg_of_le {a b : α} (h : b ≤ a) : 0 ≤ a - b := begin have h := add_le_add_right h (-b), rwa add_right_neg at h end lemma le_of_sub_nonneg {a b : α} (h : 0 ≤ a - b) : b ≤ a := begin have h := add_le_add_right h b, rwa [sub_add_cancel, zero_add] at h end lemma sub_nonpos_of_le {a b : α} (h : a ≤ b) : a - b ≤ 0 := begin have h := add_le_add_right h (-b), rwa add_right_neg at h end lemma le_of_sub_nonpos {a b : α} (h : a - b ≤ 0) : a ≤ b := begin have h := add_le_add_right h b, rwa [sub_add_cancel, zero_add] at h end lemma sub_pos_of_lt {a b : α} (h : b < a) : 0 < a - b := begin have h := add_lt_add_right h (-b), rwa add_right_neg at h end lemma lt_of_sub_pos {a b : α} (h : 0 < a - b) : b < a := begin have h := add_lt_add_right h b, rwa [sub_add_cancel, zero_add] at h end lemma sub_neg_of_lt {a b : α} (h : a < b) : a - b < 0 := begin have h := add_lt_add_right h (-b), rwa add_right_neg at h end lemma lt_of_sub_neg {a b : α} (h : a - b < 0) : a < b := begin have h := add_lt_add_right h b, rwa [sub_add_cancel, zero_add] at h end lemma add_le_of_le_neg_add {a b c : α} (h : b ≤ -a + c) : a + b ≤ c := begin have h := add_le_add_left h a, rwa add_neg_cancel_left at h end lemma le_neg_add_of_add_le {a b c : α} (h : a + b ≤ c) : b ≤ -a + c := begin have h := add_le_add_left h (-a), rwa neg_add_cancel_left at h end lemma add_le_of_le_sub_left {a b c : α} (h : b ≤ c - a) : a + b ≤ c := begin have h := add_le_add_left h a, rwa [← add_sub_assoc, add_comm a c, add_sub_cancel] at h end lemma le_sub_left_of_add_le {a b c : α} (h : a + b ≤ c) : b ≤ c - a := begin have h := add_le_add_right h (-a), rwa [add_comm a b, add_neg_cancel_right] at h end lemma add_le_of_le_sub_right {a b c : α} (h : a ≤ c - b) : a + b ≤ c := begin have h := add_le_add_right h b, rwa sub_add_cancel at h end lemma le_sub_right_of_add_le {a b c : α} (h : a + b ≤ c) : a ≤ c - b := begin have h := add_le_add_right h (-b), rwa add_neg_cancel_right at h end lemma le_add_of_neg_add_le {a b c : α} (h : -b + a ≤ c) : a ≤ b + c := begin have h := add_le_add_left h b, rwa add_neg_cancel_left at h end lemma neg_add_le_of_le_add {a b c : α} (h : a ≤ b + c) : -b + a ≤ c := begin have h := add_le_add_left h (-b), rwa neg_add_cancel_left at h end lemma le_add_of_sub_left_le {a b c : α} (h : a - b ≤ c) : a ≤ b + c := begin have h := add_le_add_right h b, rwa [sub_add_cancel, add_comm] at h end lemma sub_left_le_of_le_add {a b c : α} (h : a ≤ b + c) : a - b ≤ c := begin have h := add_le_add_right h (-b), rwa [add_comm b c, add_neg_cancel_right] at h end lemma le_add_of_sub_right_le {a b c : α} (h : a - c ≤ b) : a ≤ b + c := begin have h := add_le_add_right h c, rwa sub_add_cancel at h end lemma sub_right_le_of_le_add {a b c : α} (h : a ≤ b + c) : a - c ≤ b := begin have h := add_le_add_right h (-c), rwa add_neg_cancel_right at h end lemma le_add_of_neg_add_le_left {a b c : α} (h : -b + a ≤ c) : a ≤ b + c := begin rw add_comm at h, exact le_add_of_sub_left_le h end lemma neg_add_le_left_of_le_add {a b c : α} (h : a ≤ b + c) : -b + a ≤ c := begin rw add_comm, exact sub_left_le_of_le_add h end lemma le_add_of_neg_add_le_right {a b c : α} (h : -c + a ≤ b) : a ≤ b + c := begin rw add_comm at h, exact le_add_of_sub_right_le h end lemma neg_add_le_right_of_le_add {a b c : α} (h : a ≤ b + c) : -c + a ≤ b := begin rw add_comm at h, apply neg_add_le_left_of_le_add h end lemma le_add_of_neg_le_sub_left {a b c : α} (h : -a ≤ b - c) : c ≤ a + b := le_add_of_neg_add_le_left (add_le_of_le_sub_right h) lemma neg_le_sub_left_of_le_add {a b c : α} (h : c ≤ a + b) : -a ≤ b - c := begin have h := le_neg_add_of_add_le (sub_left_le_of_le_add h), rwa add_comm at h end lemma le_add_of_neg_le_sub_right {a b c : α} (h : -b ≤ a - c) : c ≤ a + b := le_add_of_sub_right_le (add_le_of_le_sub_left h) lemma neg_le_sub_right_of_le_add {a b c : α} (h : c ≤ a + b) : -b ≤ a - c := le_sub_left_of_add_le (sub_right_le_of_le_add h) lemma sub_le_of_sub_le {a b c : α} (h : a - b ≤ c) : a - c ≤ b := sub_left_le_of_le_add (le_add_of_sub_right_le h) lemma sub_le_sub_left {a b : α} (h : a ≤ b) (c : α) : c - b ≤ c - a := add_le_add_left (neg_le_neg h) c lemma sub_le_sub_right {a b : α} (h : a ≤ b) (c : α) : a - c ≤ b - c := add_le_add_right h (-c) lemma sub_le_sub {a b c d : α} (hab : a ≤ b) (hcd : c ≤ d) : a - d ≤ b - c := add_le_add hab (neg_le_neg hcd) lemma add_lt_of_lt_neg_add {a b c : α} (h : b < -a + c) : a + b < c := begin have h := add_lt_add_left h a, rwa add_neg_cancel_left at h end lemma lt_neg_add_of_add_lt {a b c : α} (h : a + b < c) : b < -a + c := begin have h := add_lt_add_left h (-a), rwa neg_add_cancel_left at h end lemma add_lt_of_lt_sub_left {a b c : α} (h : b < c - a) : a + b < c := begin have h := add_lt_add_left h a, rwa [← add_sub_assoc, add_comm a c, add_sub_cancel] at h end lemma lt_sub_left_of_add_lt {a b c : α} (h : a + b < c) : b < c - a := begin have h := add_lt_add_right h (-a), rwa [add_comm a b, add_neg_cancel_right] at h end lemma add_lt_of_lt_sub_right {a b c : α} (h : a < c - b) : a + b < c := begin have h := add_lt_add_right h b, rwa sub_add_cancel at h end lemma lt_sub_right_of_add_lt {a b c : α} (h : a + b < c) : a < c - b := begin have h := add_lt_add_right h (-b), rwa add_neg_cancel_right at h end lemma lt_add_of_neg_add_lt {a b c : α} (h : -b + a < c) : a < b + c := begin have h := add_lt_add_left h b, rwa add_neg_cancel_left at h end lemma neg_add_lt_of_lt_add {a b c : α} (h : a < b + c) : -b + a < c := begin have h := add_lt_add_left h (-b), rwa neg_add_cancel_left at h end lemma lt_add_of_sub_left_lt {a b c : α} (h : a - b < c) : a < b + c := begin have h := add_lt_add_right h b, rwa [sub_add_cancel, add_comm] at h end lemma sub_left_lt_of_lt_add {a b c : α} (h : a < b + c) : a - b < c := begin have h := add_lt_add_right h (-b), rwa [add_comm b c, add_neg_cancel_right] at h end lemma lt_add_of_sub_right_lt {a b c : α} (h : a - c < b) : a < b + c := begin have h := add_lt_add_right h c, rwa sub_add_cancel at h end lemma sub_right_lt_of_lt_add {a b c : α} (h : a < b + c) : a - c < b := begin have h := add_lt_add_right h (-c), rwa add_neg_cancel_right at h end lemma lt_add_of_neg_add_lt_left {a b c : α} (h : -b + a < c) : a < b + c := begin rw add_comm at h, exact lt_add_of_sub_left_lt h end lemma neg_add_lt_left_of_lt_add {a b c : α} (h : a < b + c) : -b + a < c := begin rw add_comm, exact sub_left_lt_of_lt_add h end lemma lt_add_of_neg_add_lt_right {a b c : α} (h : -c + a < b) : a < b + c := begin rw add_comm at h, exact lt_add_of_sub_right_lt h end lemma neg_add_lt_right_of_lt_add {a b c : α} (h : a < b + c) : -c + a < b := begin rw add_comm at h, apply neg_add_lt_left_of_lt_add h end lemma lt_add_of_neg_lt_sub_left {a b c : α} (h : -a < b - c) : c < a + b := lt_add_of_neg_add_lt_left (add_lt_of_lt_sub_right h) lemma neg_lt_sub_left_of_lt_add {a b c : α} (h : c < a + b) : -a < b - c := begin have h := lt_neg_add_of_add_lt (sub_left_lt_of_lt_add h), rwa add_comm at h end lemma lt_add_of_neg_lt_sub_right {a b c : α} (h : -b < a - c) : c < a + b := lt_add_of_sub_right_lt (add_lt_of_lt_sub_left h) lemma neg_lt_sub_right_of_lt_add {a b c : α} (h : c < a + b) : -b < a - c := lt_sub_left_of_add_lt (sub_right_lt_of_lt_add h) lemma sub_lt_of_sub_lt {a b c : α} (h : a - b < c) : a - c < b := sub_left_lt_of_lt_add (lt_add_of_sub_right_lt h) lemma sub_lt_sub_left {a b : α} (h : a < b) (c : α) : c - b < c - a := add_lt_add_left (neg_lt_neg h) c lemma sub_lt_sub_right {a b : α} (h : a < b) (c : α) : a - c < b - c := add_lt_add_right h (-c) lemma sub_lt_sub {a b c d : α} (hab : a < b) (hcd : c < d) : a - d < b - c := add_lt_add hab (neg_lt_neg hcd) lemma sub_lt_sub_of_le_of_lt {a b c d : α} (hab : a ≤ b) (hcd : c < d) : a - d < b - c := add_lt_add_of_le_of_lt hab (neg_lt_neg hcd) lemma sub_lt_sub_of_lt_of_le {a b c d : α} (hab : a < b) (hcd : c ≤ d) : a - d < b - c := add_lt_add_of_lt_of_le hab (neg_le_neg hcd) lemma sub_le_self (a : α) {b : α} (h : b ≥ 0) : a - b ≤ a := calc a - b = a + -b : rfl ... ≤ a + 0 : add_le_add_left (neg_nonpos_of_nonneg h) _ ... = a : by rw add_zero lemma sub_lt_self (a : α) {b : α} (h : b > 0) : a - b < a := calc a - b = a + -b : rfl ... < a + 0 : add_lt_add_left (neg_neg_of_pos h) _ ... = a : by rw add_zero lemma add_le_add_three {a b c d e f : α} (h₁ : a ≤ d) (h₂ : b ≤ e) (h₃ : c ≤ f) : a + b + c ≤ d + e + f := begin apply le_trans, apply add_le_add, apply add_le_add, repeat {assumption}, apply le_refl end end ordered_comm_group class decidable_linear_ordered_comm_group (α : Type u) extends add_comm_group α, decidable_linear_order α := (add_le_add_left : ∀ a b : α, a ≤ b → ∀ c : α, c + a ≤ c + b) (add_lt_add_left : ∀ a b : α, a < b → ∀ c : α, c + a < c + b) instance decidable_linear_ordered_comm_group.to_ordered_comm_group (α : Type u) [s : decidable_linear_ordered_comm_group α] : ordered_comm_group α := { s with add := s.add } class decidable_linear_ordered_cancel_comm_monoid (α : Type u) extends ordered_cancel_comm_monoid α, decidable_linear_order α
6555f75c4f53b8fb51dd64d83551747f943e8df9
26ac254ecb57ffcb886ff709cf018390161a9225
/src/order/complete_lattice.lean
b14b56aa9a85fdd9d0cdf2a385e197e8a3373e4d
[ "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
40,766
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import order.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 We use `Sup`/`Inf`/`supr`/`infi` for the corresponding functions in the statement. Sometimes we also use `bsupr`/`binfi` for "bounded` supremum or infimum, i.e. one of `⨆ i ∈ s, f i`, `⨆ i (hi : p i), f i`, or more generally `⨆ i (hi : p i), f i hi`. ## 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 set variables {α β β₂ : Type*} {ι ι₂ : 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 (order_dual α) := ⟨(Inf : set α → α)⟩ instance (α) [has_Sup α] : has_Inf (order_dual α) := ⟨(Sup : set α → α)⟩ section prio set_option default_priority 100 -- see Note [default priority] /-- A complete lattice is a bounded lattice which has suprema and infima for every subset. -/ class complete_lattice (α : Type*) extends bounded_lattice α, has_Sup α, has_Inf α := (le_Sup : ∀s, ∀a∈s, a ≤ Sup s) (Sup_le : ∀s a, (∀b∈s, b ≤ a) → Sup s ≤ a) (Inf_le : ∀s, ∀a∈s, Inf s ≤ a) (le_Inf : ∀s a, (∀b∈s, a ≤ b) → a ≤ Inf s) /-- 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, so it should be used with `.. complete_lattice_of_Inf α _`. -/ 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 } /-- 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, so it should be used with `.. complete_lattice_of_Sup α _`. -/ 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 } /-- A complete linear order is a linear order whose lattice structure is complete. -/ class complete_linear_order (α : Type*) extends complete_lattice α, decidable_linear_order α end prio section variables [complete_lattice α] {s t : set α} {a b : α} @[ematch] theorem le_Sup : a ∈ s → a ≤ Sup s := complete_lattice.le_Sup s a theorem Sup_le : (∀b∈s, b ≤ a) → Sup s ≤ a := complete_lattice.Sup_le s a @[ematch] theorem Inf_le : a ∈ s → Inf s ≤ a := complete_lattice.Inf_le s a theorem le_Inf : (∀b∈s, a ≤ b) → a ≤ Inf s := complete_lattice.le_Inf s a lemma is_lub_Sup (s : set α) : is_lub s (Sup s) := ⟨assume x, le_Sup, assume x, Sup_le⟩ lemma is_lub.Sup_eq (h : is_lub s a) : Sup s = a := (is_lub_Sup s).unique h lemma is_glb_Inf (s : set α) : is_glb s (Inf s) := ⟨assume a, Inf_le, assume a, le_Inf⟩ lemma is_glb.Inf_eq (h : is_glb s a) : Inf s = a := (is_glb_Inf s).unique h theorem le_Sup_of_le (hb : b ∈ s) (h : a ≤ b) : a ≤ Sup s := le_trans h (le_Sup hb) theorem Inf_le_of_le (hb : b ∈ s) (h : b ≤ a) : Inf s ≤ a := le_trans (Inf_le hb) h theorem Sup_le_Sup (h : s ⊆ t) : Sup s ≤ Sup t := (is_lub_Sup s).mono (is_lub_Sup t) h theorem Inf_le_Inf (h : s ⊆ t) : Inf t ≤ Inf s := (is_glb_Inf s).mono (is_glb_Inf t) h @[simp] theorem Sup_le_iff : Sup s ≤ a ↔ (∀b ∈ s, b ≤ a) := is_lub_le_iff (is_lub_Sup s) @[simp] theorem le_Inf_iff : a ≤ Inf s ↔ (∀b ∈ s, a ≤ b) := le_is_glb_iff (is_glb_Inf s) 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 -- TODO: it is weird that we have to add union_def 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 Sup_inter_le {s t : set α} : Sup (s ∩ t) ≤ Sup s ⊓ Sup t := by finish /- Sup_le (assume a ⟨a_s, a_t⟩, le_inf (le_Sup a_s) (le_Sup a_t)) -/ 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 le_Inf_inter {s t : set α} : Inf s ⊔ Inf t ≤ Inf (s ∩ t) := by finish /- le_Inf (assume a ⟨a_s, a_t⟩, sup_le (Inf_le a_s) (Inf_le a_t)) -/ @[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 -- We will generalize this to conditionally complete lattices in `cSup_singleton`. theorem Sup_singleton {a : α} : Sup {a} = a := is_lub_singleton.Sup_eq -- We will generalize this to conditionally complete lattices in `cInf_singleton`. theorem Inf_singleton {a : α} : Inf {a} = a := is_glb_singleton.Inf_eq 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] theorem Inf_eq_top : Inf s = ⊤ ↔ (∀a∈s, a = ⊤) := iff.intro (assume h a ha, top_unique $ h ▸ Inf_le ha) (assume h, top_unique $ le_Inf $ assume a ha, top_le_iff.2 $ h a ha) @[simp] theorem Sup_eq_bot : Sup s = ⊥ ↔ (∀a∈s, a = ⊥) := iff.intro (assume h a ha, bot_unique $ h ▸ le_Sup ha) (assume h, bot_unique $ Sup_le $ assume a ha, le_bot_iff.2 $ h a ha) end section complete_linear_order variables [complete_linear_order α] {s t : set α} {a b : α} lemma Inf_lt_iff : Inf s < b ↔ (∃a∈s, a < b) := is_glb_lt_iff (is_glb_Inf s) lemma lt_Sup_iff : b < Sup s ↔ (∃a∈s, b < a) := lt_is_lub_iff (is_lub_Sup s) lemma Sup_eq_top : Sup s = ⊤ ↔ (∀b<⊤, ∃a∈s, b < a) := iff.intro (assume (h : Sup s = ⊤) b hb, by rwa [←h, lt_Sup_iff] at hb) (assume h, top_unique $ le_of_not_gt $ assume h', let ⟨a, ha, h⟩ := h _ h' in lt_irrefl a $ lt_of_le_of_lt (le_Sup ha) h) @[nolint ge_or_gt] -- see Note [nolint_ge] lemma Inf_eq_bot : Inf s = ⊥ ↔ (∀b>⊥, ∃a∈s, a < b) := iff.intro (assume (h : Inf s = ⊥) b (hb : ⊥ < b), by rwa [←h, Inf_lt_iff] at hb) (assume h, bot_unique $ le_of_not_gt $ assume h', let ⟨a, ha, h⟩ := h _ h' in lt_irrefl a $ lt_of_lt_of_le h (Inf_le ha)) 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 variables [complete_lattice α] {s t : ι → α} {a b : α} -- TODO: this declaration gives error when starting smt state --@[ematch] theorem le_supr (s : ι → α) (i : ι) : s i ≤ supr s := le_Sup ⟨i, rfl⟩ @[ematch] theorem le_supr' (s : ι → α) (i : ι) : (: s i ≤ supr s :) := le_Sup ⟨i, rfl⟩ /- TODO: this version would be more powerful, but, alas, the pattern matcher doesn't accept it. @[ematch] theorem le_supr' (s : ι → α) (i : ι) : (: s i :) ≤ (: supr s :) := le_Sup ⟨i, rfl⟩ -/ lemma is_lub_supr : is_lub (range s) (⨆j, s j) := is_lub_Sup _ lemma is_lub.supr_eq (h : is_lub (range s) a) : (⨆j, s j) = a := h.Sup_eq lemma is_glb_infi : is_glb (range s) (⨅j, s j) := is_glb_Inf _ lemma is_glb.infi_eq (h : is_glb (range s) a) : (⨅j, s j) = a := h.Inf_eq theorem le_supr_of_le (i : ι) (h : a ≤ s i) : a ≤ supr s := le_trans h (le_supr _ i) theorem le_bsupr {p : ι → Prop} {f : Π i (h : p i), α} (i : ι) (hi : p i) : f i hi ≤ ⨆ i hi, f i hi := le_supr_of_le i $ le_supr (f i) hi theorem supr_le (h : ∀i, s i ≤ a) : supr s ≤ a := Sup_le $ assume b ⟨i, eq⟩, eq ▸ h i theorem bsupr_le {p : ι → Prop} {f : Π i (h : p i), α} (h : ∀ i hi, f i hi ≤ a) : (⨆ i (hi : p i), f i hi) ≤ a := supr_le $ λ i, supr_le $ h i theorem supr_le_supr (h : ∀i, s i ≤ t i) : supr s ≤ supr t := supr_le $ assume i, le_supr_of_le i (h i) theorem supr_le_supr2 {t : ι₂ → α} (h : ∀i, ∃j, s i ≤ t j) : supr s ≤ supr t := supr_le $ assume j, exists.elim (h j) le_supr_of_le theorem bsupr_le_bsupr {p : ι → Prop} {f g : Π i (hi : p i), α} (h : ∀ i hi, f i hi ≤ g i hi) : (⨆ i hi, f i hi) ≤ ⨆ i hi, g i hi := bsupr_le $ λ i hi, le_trans (h i hi) (le_bsupr i hi) theorem supr_le_supr_const (h : ι → ι₂) : (⨆ i:ι, a) ≤ (⨆ j:ι₂, a) := supr_le $ le_supr _ ∘ h @[simp] theorem supr_le_iff : supr s ≤ a ↔ (∀i, s i ≤ a) := (is_lub_le_iff is_lub_supr).trans forall_range_iff theorem Sup_eq_supr {s : set α} : Sup s = (⨆a ∈ s, a) := le_antisymm (Sup_le $ assume b h, le_supr_of_le b $ le_supr _ h) (supr_le $ assume b, supr_le $ assume h, le_Sup h) lemma le_supr_iff : (a ≤ supr s) ↔ (∀ b, (∀ i, s i ≤ b) → a ≤ b) := ⟨λ h b hb, le_trans h (supr_le hb), λ h, h _ $ λ i, le_supr s i⟩ 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 monotone.le_map_supr2 [complete_lattice β] {f : α → β} (hf : monotone f) {ι' : ι → Sort*} (s : Π i, ι' i → α) : (⨆ i (h : ι' i), f (s i h)) ≤ f (⨆ i (h : ι' i), s i h) := calc (⨆ i h, f (s i h)) ≤ (⨆ i, f (⨆ h, s i h)) : supr_le_supr $ λ i, hf.le_map_supr ... ≤ f (⨆ i (h : ι' i), s i h) : hf.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_supr2 _ lemma supr_comp_le {ι' : Sort*} (f : ι' → α) (g : ι → ι') : (⨆ x, f (g x)) ≤ ⨆ y, f y := supr_le_supr2 $ λ x, ⟨_, le_refl _⟩ 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_le_supr2 $ λ x, (hs x).imp $ λ i hi, hf hi) lemma supr_congr {f : β → α} {g : β₂ → α} (h : β → β₂) (h1 : function.surjective h) (h2 : ∀ x, g (h x) = f x) : (⨆ x, f x) = ⨆ y, g y := by { unfold supr, congr' 1, convert h1.range_comp g, ext, rw ←h2 } -- TODO: finish doesn't do well here. @[congr] theorem supr_congr_Prop {α : Type*} [has_Sup α] {p q : Prop} {f₁ : p → α} {f₂ : q → α} (pq : p ↔ q) (f : ∀x, f₁ (pq.mpr x) = f₂ x) : supr f₁ = supr f₂ := begin unfold supr, apply congr_arg, ext, simp, split, exact λ⟨h, W⟩, ⟨pq.1 h, eq.trans (f (pq.1 h)).symm W⟩, exact λ⟨h, W⟩, ⟨pq.2 h, eq.trans (f h) W⟩ end theorem infi_le (s : ι → α) (i : ι) : infi s ≤ s i := Inf_le ⟨i, rfl⟩ @[ematch] theorem infi_le' (s : ι → α) (i : ι) : (: infi s ≤ s i :) := Inf_le ⟨i, rfl⟩ /- I wanted to see if this would help for infi_comm; it doesn't. @[ematch] theorem infi_le₂' (s : ι → ι₂ → α) (i : ι) (j : ι₂) : (: ⨅ i j, s i j :) ≤ (: s i j :) := begin transitivity, apply (infi_le (λ i, ⨅ j, s i j) i), apply infi_le end -/ theorem infi_le_of_le (i : ι) (h : s i ≤ a) : infi s ≤ a := le_trans (infi_le _ i) h theorem binfi_le {p : ι → Prop} {f : Π i (hi : p i), α} (i : ι) (hi : p i) : (⨅ i hi, f i hi) ≤ f i hi := infi_le_of_le i $ infi_le (f i) hi theorem le_infi (h : ∀i, a ≤ s i) : a ≤ infi s := le_Inf $ assume b ⟨i, eq⟩, eq ▸ h i theorem le_binfi {p : ι → Prop} {f : Π i (h : p i), α} (h : ∀ i hi, a ≤ f i hi) : a ≤ ⨅ i hi, f i hi := le_infi $ λ i, le_infi $ h i theorem infi_le_infi (h : ∀i, s i ≤ t i) : infi s ≤ infi t := le_infi $ assume i, infi_le_of_le i (h i) theorem infi_le_infi2 {t : ι₂ → α} (h : ∀j, ∃i, s i ≤ t j) : infi s ≤ infi t := le_infi $ assume j, exists.elim (h j) infi_le_of_le theorem binfi_le_binfi {p : ι → Prop} {f g : Π i (h : p i), α} (h : ∀ i hi, f i hi ≤ g i hi) : (⨅ i hi, f i hi) ≤ ⨅ i hi, g i hi := le_binfi $ λ i hi, le_trans (binfi_le i hi) (h i hi) theorem infi_le_infi_const (h : ι₂ → ι) : (⨅ i:ι, a) ≤ (⨅ j:ι₂, a) := le_infi $ infi_le _ ∘ h @[simp] theorem le_infi_iff : a ≤ infi s ↔ (∀i, a ≤ s i) := ⟨assume : a ≤ infi s, assume i, le_trans this (infi_le _ _), le_infi⟩ theorem Inf_eq_infi {s : set α} : Inf s = (⨅a ∈ s, a) := le_antisymm (le_infi $ assume b, le_infi $ assume h, Inf_le h) (le_Inf $ assume b h, infi_le_of_le b $ infi_le _ h) lemma monotone.map_infi_le [complete_lattice β] {f : α → β} (hf : monotone f) : f (infi s) ≤ (⨅ i, f (s i)) := le_infi $ λ i, hf $ infi_le _ _ lemma monotone.map_infi2_le [complete_lattice β] {f : α → β} (hf : monotone f) {ι' : ι → Sort*} (s : Π i, ι' i → α) : f (⨅ i (h : ι' i), s i h) ≤ (⨅ i (h : ι' i), f (s i h)) := calc f (⨅ i (h : ι' i), s i h) ≤ (⨅ i, f (⨅ h, s i h)) : hf.map_infi_le ... ≤ (⨅ i h, f (s i h)) : infi_le_infi $ λ i, hf.map_infi_le lemma monotone.map_Inf_le [complete_lattice β] {s : set α} {f : α → β} (hf : monotone f) : f (Inf s) ≤ ⨅ a∈s, f a := by rw [Inf_eq_infi]; exact hf.map_infi2_le _ lemma le_infi_comp {ι' : Sort*} (f : ι' → α) (g : ι → ι') : (⨅ y, f y) ≤ ⨅ x, f (g x) := infi_le_infi2 $ λ x, ⟨_, le_refl _⟩ 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_le_infi2 $ λ x, (hs x).imp $ λ i hi, hf hi) (le_infi_comp _ _) lemma infi_congr {f : β → α} {g : β₂ → α} (h : β → β₂) (h1 : function.surjective h) (h2 : ∀ x, g (h x) = f x) : (⨅ x, f x) = ⨅ y, g y := by { unfold infi, congr' 1, convert h1.range_comp g, ext, rw ←h2 } @[congr] theorem infi_congr_Prop {α : Type*} [has_Inf α] {p q : Prop} {f₁ : p → α} {f₂ : q → α} (pq : p ↔ q) (f : ∀x, f₁ (pq.mpr x) = f₂ x) : infi f₁ = infi f₂ := begin unfold infi, apply congr_arg, ext, simp, split, exact λ⟨h, W⟩, ⟨pq.1 h, eq.trans (f (pq.1 h)).symm W⟩, exact λ⟨h, W⟩, ⟨pq.2 h, eq.trans (f h) W⟩ end -- We will generalize this to conditionally complete lattices in `cinfi_const`. theorem infi_const [nonempty ι] {a : α} : (⨅ b:ι, a) = a := by rw [infi, range_const, Inf_singleton] -- We will generalize this to conditionally complete lattices in `csupr_const`. theorem supr_const [nonempty ι] {a : α} : (⨆ b:ι, a) = a := by rw [supr, range_const, Sup_singleton] @[simp] lemma infi_top : (⨅i:ι, ⊤ : α) = ⊤ := top_unique $ le_infi $ assume i, le_refl _ @[simp] lemma supr_bot : (⨆i:ι, ⊥ : α) = ⊥ := bot_unique $ supr_le $ assume i, le_refl _ @[simp] lemma infi_eq_top : infi s = ⊤ ↔ (∀i, s i = ⊤) := iff.intro (assume eq i, top_unique $ eq ▸ infi_le _ _) (assume h, top_unique $ le_infi $ assume i, top_le_iff.2 $ h i) @[simp] lemma supr_eq_bot : supr s = ⊥ ↔ (∀i, s i = ⊥) := iff.intro (assume eq i, bot_unique $ eq ▸ le_supr _ _) (assume h, bot_unique $ supr_le $ assume i, le_bot_iff.2 $ h i) @[simp] lemma infi_pos {p : Prop} {f : p → α} (hp : p) : (⨅ h : p, f h) = f hp := le_antisymm (infi_le _ _) (le_infi $ assume h, le_refl _) @[simp] lemma infi_neg {p : Prop} {f : p → α} (hp : ¬ p) : (⨅ h : p, f h) = ⊤ := le_antisymm le_top $ le_infi $ assume h, (hp h).elim @[simp] lemma supr_pos {p : Prop} {f : p → α} (hp : p) : (⨆ h : p, f h) = f hp := le_antisymm (supr_le $ assume h, le_refl _) (le_supr _ _) @[simp] lemma supr_neg {p : Prop} {f : p → α} (hp : ¬ p) : (⨆ h : p, f h) = ⊥ := le_antisymm (supr_le $ assume h, (hp h).elim) bot_le 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 ⊥) := by rw [supr_eq_dif, dif_eq_if] lemma infi_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 infi_eq_if {p : Prop} [decidable p] (a : α) : (⨅h:p, a) = (if p then a else ⊤) := by rw [infi_eq_dif, dif_eq_if] -- TODO: should this be @[simp]? theorem infi_comm {f : ι → ι₂ → α} : (⨅i, ⨅j, f i j) = (⨅j, ⨅i, f i j) := le_antisymm (le_infi $ assume i, le_infi $ assume j, infi_le_of_le j $ infi_le _ i) (le_infi $ assume j, le_infi $ assume i, infi_le_of_le i $ infi_le _ j) /- 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 -/ -- TODO: should this be @[simp]? theorem supr_comm {f : ι → ι₂ → α} : (⨆i, ⨆j, f i j) = (⨆j, ⨆i, f i j) := le_antisymm (supr_le $ assume i, supr_le $ assume j, le_supr_of_le j $ le_supr _ i) (supr_le $ assume j, supr_le $ assume i, le_supr_of_le i $ le_supr _ j) @[simp] theorem infi_infi_eq_left {b : β} {f : Πx:β, x = b → α} : (⨅x, ⨅h:x = b, f x h) = f b rfl := le_antisymm (infi_le_of_le b $ infi_le _ rfl) (le_infi $ assume b', le_infi $ assume eq, match b', eq with ._, rfl := le_refl _ end) @[simp] theorem infi_infi_eq_right {b : β} {f : Πx:β, b = x → α} : (⨅x, ⨅h:b = x, f x h) = f b rfl := le_antisymm (infi_le_of_le b $ infi_le _ rfl) (le_infi $ assume b', le_infi $ assume eq, match b', eq with ._, rfl := le_refl _ end) @[simp] theorem supr_supr_eq_left {b : β} {f : Πx:β, x = b → α} : (⨆x, ⨆h : x = b, f x h) = f b rfl := le_antisymm (supr_le $ assume b', supr_le $ assume eq, match b', eq with ._, rfl := le_refl _ end) (le_supr_of_le b $ le_supr _ rfl) @[simp] theorem supr_supr_eq_right {b : β} {f : Πx:β, b = x → α} : (⨆x, ⨆h : b = x, f x h) = f b rfl := le_antisymm (supr_le $ assume b', supr_le $ assume eq, match b', eq with ._, rfl := le_refl _ end) (le_supr_of_le b $ le_supr _ rfl) attribute [ematch] le_refl theorem infi_inf_eq {f g : ι → α} : (⨅ x, f x ⊓ g x) = (⨅ x, f x) ⊓ (⨅ x, g x) := le_antisymm (le_inf (le_infi $ assume i, infi_le_of_le i inf_le_left) (le_infi $ assume i, infi_le_of_le i inf_le_right)) (le_infi $ assume i, le_inf (inf_le_left_of_le $ infi_le _ _) (inf_le_right_of_le $ infi_le _ _)) /- 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 infi_inf {f : ι → α} {a : α} (i : ι) : (⨅x, f x) ⊓ a = (⨅ x, f x ⊓ a) := le_antisymm (le_infi $ assume i, le_inf (inf_le_left_of_le $ infi_le _ _) inf_le_right) (le_inf (infi_le_infi $ assume i, inf_le_left) (infi_le_of_le i inf_le_right)) lemma inf_infi {f : ι → α} {a : α} (i : ι) : a ⊓ (⨅x, f x) = (⨅ x, a ⊓ f x) := by rw [inf_comm, infi_inf i]; simp [inf_comm] lemma binfi_inf {p : ι → Prop} {f : Π i (hi : p i), α} {a : α} {i : ι} (hi : p i) : (⨅i (h : p i), f i h) ⊓ a = (⨅ i (h : p i), f i h ⊓ a) := le_antisymm (le_infi $ assume i, le_infi $ assume hi, le_inf (inf_le_left_of_le $ infi_le_of_le i $ infi_le _ _) inf_le_right) (le_inf (infi_le_infi $ assume i, infi_le_infi $ assume hi, inf_le_left) (infi_le_of_le i $ infi_le_of_le hi $ inf_le_right)) theorem supr_sup_eq {f g : β → α} : (⨆ x, f x ⊔ g x) = (⨆ x, f x) ⊔ (⨆ x, g x) := le_antisymm (supr_le $ assume i, sup_le (le_sup_left_of_le $ le_supr _ _) (le_sup_right_of_le $ le_supr _ _)) (sup_le (supr_le $ assume i, le_supr_of_le i le_sup_left) (supr_le $ assume i, le_supr_of_le i le_sup_right)) /- supr and infi under Prop -/ @[simp] theorem infi_false {s : false → α} : infi s = ⊤ := le_antisymm le_top (le_infi $ assume i, false.elim i) @[simp] theorem supr_false {s : false → α} : supr s = ⊥ := le_antisymm (supr_le $ assume i, false.elim i) bot_le @[simp] theorem infi_true {s : true → α} : infi s = s trivial := le_antisymm (infi_le _ _) (le_infi $ assume ⟨⟩, le_refl _) @[simp] theorem supr_true {s : true → α} : supr s = s trivial := le_antisymm (supr_le $ assume ⟨⟩, le_refl _) (le_supr _ _) @[simp] theorem infi_exists {p : ι → Prop} {f : Exists p → α} : (⨅ x, f x) = (⨅ i, ⨅ h:p i, f ⟨i, h⟩) := le_antisymm (le_infi $ assume i, le_infi $ assume : p i, infi_le _ _) (le_infi $ assume ⟨i, h⟩, infi_le_of_le i $ infi_le _ _) @[simp] theorem supr_exists {p : ι → Prop} {f : Exists p → α} : (⨆ x, f x) = (⨆ i, ⨆ h:p i, f ⟨i, h⟩) := le_antisymm (supr_le $ assume ⟨i, h⟩, le_supr_of_le i $ le_supr (λh:p i, f ⟨i, h⟩) _) (supr_le $ assume i, supr_le $ assume : p i, le_supr _ _) theorem infi_and {p q : Prop} {s : p ∧ q → α} : infi s = (⨅ h₁ h₂, s ⟨h₁, h₂⟩) := le_antisymm (le_infi $ assume i, le_infi $ assume j, infi_le _ _) (le_infi $ assume ⟨i, h⟩, infi_le_of_le i $ infi_le _ _) /-- 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 := by { symmetry, exact infi_and } theorem supr_and {p q : Prop} {s : p ∧ q → α} : supr s = (⨆ h₁ h₂, s ⟨h₁, h₂⟩) := le_antisymm (supr_le $ assume ⟨i, h⟩, le_supr_of_le i $ le_supr (λj, s ⟨i, j⟩) _) (supr_le $ assume i, supr_le $ assume j, le_supr _ _) /-- 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 := by { symmetry, exact supr_and } theorem infi_or {p q : Prop} {s : p ∨ q → α} : infi s = (⨅ h : p, s (or.inl h)) ⊓ (⨅ h : q, s (or.inr h)) := le_antisymm (le_inf (infi_le_infi2 $ assume j, ⟨_, le_refl _⟩) (infi_le_infi2 $ assume j, ⟨_, le_refl _⟩)) (le_infi $ assume i, match i with | or.inl i := inf_le_left_of_le $ infi_le _ _ | or.inr j := inf_le_right_of_le $ infi_le _ _ end) 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 $ assume s, match s with | or.inl i := le_sup_left_of_le $ le_supr _ i | or.inr j := le_sup_right_of_le $ le_supr _ j end) (sup_le (supr_le_supr2 $ assume i, ⟨or.inl i, le_refl _⟩) (supr_le_supr2 $ assume j, ⟨or.inr j, le_refl _⟩)) lemma Sup_range {α : Type*} [has_Sup α] {f : ι → α} : Sup (range f) = supr f := rfl lemma Inf_range {α : Type*} [has_Inf α] {f : ι → α} : Inf (range f) = infi f := rfl lemma supr_range {g : β → α} {f : ι → β} : (⨆b∈range f, g b) = (⨆i, g (f i)) := le_antisymm (supr_le $ assume b, supr_le $ assume ⟨i, (h : f i = b)⟩, h ▸ le_supr _ i) (supr_le $ assume i, le_supr_of_le (f i) $ le_supr (λp, g (f i)) (mem_range_self _)) lemma infi_range {g : β → α} {f : ι → β} : (⨅b∈range f, g b) = (⨅i, g (f i)) := le_antisymm (le_infi $ assume i, infi_le_of_le (f i) $ infi_le (λp, g (f i)) (mem_range_self _)) (le_infi $ assume b, le_infi $ assume ⟨i, (h : f i = b)⟩, h ▸ infi_le _ i) theorem Inf_image {s : set β} {f : β → α} : Inf (f '' s) = (⨅ a ∈ s, f a) := calc Inf (set.image f s) = (⨅a, ⨅h : ∃b, b ∈ s ∧ f b = a, a) : Inf_eq_infi ... = (⨅a, ⨅b, ⨅h : f b = a ∧ b ∈ s, a) : by simp [and_comm] ... = (⨅a, ⨅b, ⨅h : a = f b, ⨅h : b ∈ s, a) : by simp [infi_and, eq_comm] ... = (⨅b, ⨅a, ⨅h : a = f b, ⨅h : b ∈ s, a) : by rw [infi_comm] ... = (⨅a∈s, f a) : congr_arg infi $ by funext x; rw [infi_infi_eq_left] theorem Sup_image {s : set β} {f : β → α} : Sup (f '' s) = (⨆ a ∈ s, f a) := calc Sup (set.image f s) = (⨆a, ⨆h : ∃b, b ∈ s ∧ f b = a, a) : Sup_eq_supr ... = (⨆a, ⨆b, ⨆h : f b = a ∧ b ∈ s, a) : by simp [and_comm] ... = (⨆a, ⨆b, ⨆h : a = f b, ⨆h : b ∈ s, a) : by simp [supr_and, eq_comm] ... = (⨆b, ⨆a, ⨆h : a = f b, ⨆h : b ∈ s, a) : by rw [supr_comm] ... = (⨆a∈s, f a) : congr_arg supr $ by funext x; rw [supr_supr_eq_left] /- supr and infi under set constructions -/ theorem infi_emptyset {f : β → α} : (⨅ x ∈ (∅ : set β), f x) = ⊤ := by simp theorem supr_emptyset {f : β → α} : (⨆ x ∈ (∅ : set β), f x) = ⊥ := by simp theorem infi_univ {f : β → α} : (⨅ x ∈ (univ : set β), f x) = (⨅ x, f x) := by simp theorem supr_univ {f : β → α} : (⨆ x ∈ (univ : set β), f x) = (⨆ x, f x) := by simp theorem infi_union {f : β → α} {s t : set β} : (⨅ x ∈ s ∪ t, f x) = (⨅x∈s, f x) ⊓ (⨅x∈t, f x) := calc (⨅ x ∈ s ∪ t, f x) = (⨅ x, (⨅h : x∈s, f x) ⊓ (⨅h : x∈t, f x)) : congr_arg infi $ funext $ assume x, infi_or ... = (⨅x∈s, f x) ⊓ (⨅x∈t, f x) : infi_inf_eq lemma infi_split (f : β → α) (p : β → Prop) : (⨅ i, f i) = (⨅ i (h : p i), f i) ⊓ (⨅ i (h : ¬ p i), f i) := by simpa [classical.em] using @infi_union _ _ _ f {i | p i} {i | ¬ p i} lemma infi_split_single (f : β → α) (i₀ : β) : (⨅ i, f i) = f i₀ ⊓ (⨅ i (h : i ≠ i₀), f i) := by convert infi_split _ _; simp theorem infi_le_infi_of_subset {f : β → α} {s t : set β} (h : s ⊆ t) : (⨅ x ∈ t, f x) ≤ (⨅ x ∈ s, f x) := by rw [(union_eq_self_of_subset_left h).symm, infi_union]; exact inf_le_left theorem supr_union {f : β → α} {s t : set β} : (⨆ x ∈ s ∪ t, f x) = (⨆x∈s, f x) ⊔ (⨆x∈t, f x) := calc (⨆ x ∈ s ∪ t, f x) = (⨆ x, (⨆h : x∈s, f x) ⊔ (⨆h : x∈t, f x)) : congr_arg supr $ funext $ assume x, supr_or ... = (⨆x∈s, f x) ⊔ (⨆x∈t, f x) : supr_sup_eq 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 supr_split_single (f : β → α) (i₀ : β) : (⨆ i, f i) = f i₀ ⊔ (⨆ i (h : i ≠ i₀), f i) := by convert supr_split _ _; simp theorem supr_le_supr_of_subset {f : β → α} {s t : set β} (h : s ⊆ t) : (⨆ x ∈ s, f x) ≤ (⨆ x ∈ t, f x) := by rw [(union_eq_self_of_subset_left h).symm, supr_union]; exact le_sup_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_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_singleton {f : β → α} {b : β} : (⨅ x ∈ (singleton b : set β), f x) = f b := by simp theorem infi_pair {f : β → α} {a b : β} : (⨅ x ∈ ({a, b} : set β), f x) = f a ⊓ f b := by rw [infi_insert, infi_singleton] theorem supr_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] lemma infi_image {γ} {f : β → γ} {g : γ → α} {t : set β} : (⨅ c ∈ f '' t, g c) = (⨅ b ∈ t, g (f b)) := le_antisymm (le_infi $ assume b, le_infi $ assume hbt, infi_le_of_le (f b) $ infi_le (λ_, g (f b)) (mem_image_of_mem f hbt)) (le_infi $ assume c, le_infi $ assume ⟨b, hbt, eq⟩, eq ▸ infi_le_of_le b $ infi_le (λ_, g (f b)) hbt) lemma supr_image {γ} {f : β → γ} {g : γ → α} {t : set β} : (⨆ c ∈ f '' t, g c) = (⨆ b ∈ t, g (f b)) := le_antisymm (supr_le $ assume c, supr_le $ assume ⟨b, hbt, eq⟩, eq ▸ le_supr_of_le b $ le_supr (λ_, g (f b)) hbt) (supr_le $ assume b, supr_le $ assume hbt, le_supr_of_le (f b) $ le_supr (λ_, g (f b)) (mem_image_of_mem f hbt)) /- supr and infi under Type -/ @[simp] theorem infi_empty {s : empty → α} : infi s = ⊤ := le_antisymm le_top (le_infi $ assume i, empty.rec_on _ i) @[simp] theorem supr_empty {s : empty → α} : supr s = ⊥ := le_antisymm (supr_le $ assume i, empty.rec_on _ i) bot_le @[simp] theorem infi_unit {f : unit → α} : (⨅ x, f x) = f () := le_antisymm (infi_le _ _) (le_infi $ assume ⟨⟩, le_refl _) @[simp] theorem supr_unit {f : unit → α} : (⨆ x, f x) = f () := le_antisymm (supr_le $ assume ⟨⟩, le_refl _) (le_supr _ _) lemma supr_bool_eq {f : bool → α} : (⨆b:bool, f b) = f tt ⊔ f ff := le_antisymm (supr_le $ assume b, match b with tt := le_sup_left | ff := le_sup_right end) (sup_le (le_supr _ _) (le_supr _ _)) lemma infi_bool_eq {f : bool → α} : (⨅b:bool, f b) = f tt ⊓ f ff := le_antisymm (le_inf (infi_le _ _) (infi_le _ _)) (le_infi $ assume b, match b with tt := inf_le_left | ff := inf_le_right end) theorem infi_subtype {p : ι → Prop} {f : subtype p → α} : (⨅ x, f x) = (⨅ i (h:p i), f ⟨i, h⟩) := le_antisymm (le_infi $ assume i, le_infi $ assume : p i, infi_le _ _) (le_infi $ assume ⟨i, h⟩, infi_le_of_le i $ infi_le _ _) lemma infi_subtype' {p : ι → Prop} {f : ∀ i, p i → α} : (⨅ i (h : p i), f i h) = (⨅ x : subtype p, f x.val x.property) := (@infi_subtype _ _ _ p (λ x, f x.val x.property)).symm lemma infi_subtype'' {ι} (s : set ι) (f : ι → α) : (⨅ i : s, f i) = ⨅ (t : ι) (H : t ∈ s), f t := infi_subtype 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) theorem supr_subtype {p : ι → Prop} {f : subtype p → α} : (⨆ x, f x) = (⨆ i (h:p i), f ⟨i, h⟩) := le_antisymm (supr_le $ assume ⟨i, h⟩, le_supr_of_le i $ le_supr (λh:p i, f ⟨i, h⟩) _) (supr_le $ assume i, supr_le $ assume : p i, le_supr _ _) lemma supr_subtype' {p : ι → Prop} {f : ∀ i, p i → α} : (⨆ i (h : p i), f i h) = (⨆ x : subtype p, f x x.property) := (@supr_subtype _ _ _ p (λ x, f x.val x.property)).symm lemma Sup_eq_supr' {s : set α} : Sup s = ⨆ x : s, (x : α) := by rw [Sup_eq_supr, supr_subtype']; refl 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 infi_sigma {p : β → Type*} {f : sigma p → α} : (⨅ x, f x) = (⨅ i (h:p i), f ⟨i, h⟩) := le_antisymm (le_infi $ assume i, le_infi $ assume : p i, infi_le _ _) (le_infi $ assume ⟨i, h⟩, infi_le_of_le i $ infi_le _ _) theorem supr_sigma {p : β → Type*} {f : sigma p → α} : (⨆ x, f x) = (⨆ i (h:p i), f ⟨i, h⟩) := le_antisymm (supr_le $ assume ⟨i, h⟩, le_supr_of_le i $ le_supr (λh:p i, f ⟨i, h⟩) _) (supr_le $ assume i, supr_le $ assume : p i, le_supr _ _) theorem infi_prod {γ : Type*} {f : β × γ → α} : (⨅ x, f x) = (⨅ i j, f (i, j)) := le_antisymm (le_infi $ assume i, le_infi $ assume j, infi_le _ _) (le_infi $ assume ⟨i, h⟩, infi_le_of_le i $ infi_le _ _) theorem supr_prod {γ : Type*} {f : β × γ → α} : (⨆ x, f x) = (⨆ i j, f (i, j)) := le_antisymm (supr_le $ assume ⟨i, h⟩, le_supr_of_le i $ le_supr (λj, f ⟨i, j⟩) _) (supr_le $ assume i, supr_le $ assume j, le_supr _ _) theorem infi_sum {γ : Type*} {f : β ⊕ γ → α} : (⨅ x, f x) = (⨅ i, f (sum.inl i)) ⊓ (⨅ j, f (sum.inr j)) := le_antisymm (le_inf (infi_le_infi2 $ assume i, ⟨_, le_refl _⟩) (infi_le_infi2 $ assume j, ⟨_, le_refl _⟩)) (le_infi $ assume s, match s with | sum.inl i := inf_le_left_of_le $ infi_le _ _ | sum.inr j := inf_le_right_of_le $ infi_le _ _ end) theorem supr_sum {γ : Type*} {f : β ⊕ γ → α} : (⨆ x, f x) = (⨆ i, f (sum.inl i)) ⊔ (⨆ j, f (sum.inr j)) := le_antisymm (supr_le $ assume s, match s with | sum.inl i := le_sup_left_of_le $ le_supr _ i | sum.inr j := le_sup_right_of_le $ le_supr _ j end) (sup_le (supr_le_supr2 $ assume i, ⟨sum.inl i, le_refl _⟩) (supr_le_supr2 $ assume j, ⟨sum.inr j, le_refl _⟩)) 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] @[nolint ge_or_gt] -- see Note [nolint_ge] 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 complete_lattice_Prop : complete_lattice Prop := { Sup := λs, ∃a∈s, a, le_Sup := assume s a h p, ⟨a, h, p⟩, Sup_le := assume s a h ⟨b, h', p⟩, h b h' p, Inf := λs, ∀a:Prop, a∈s → a, Inf_le := assume s a h p, p a h, le_Inf := assume s a h p b hb, h b hb p, .. bounded_distrib_lattice_Prop } lemma Inf_Prop_eq {s : set Prop} : Inf s = (∀p ∈ s, p) := rfl lemma Sup_Prop_eq {s : set Prop} : Sup s = (∃p ∈ s, p) := rfl lemma infi_Prop_eq {ι : Sort*} {p : ι → Prop} : (⨅i, p i) = (∀i, p i) := le_antisymm (assume h i, h _ ⟨i, rfl⟩ ) (assume h p ⟨i, eq⟩, eq ▸ h i) lemma supr_Prop_eq {ι : Sort*} {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⟩) 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_lattice } lemma Inf_apply {α : Type*} {β : α → Type*} [Π i, has_Inf (β i)] {s : set (Πa, β a)} {a : α} : (Inf s) a = (⨅ f : s, (f : Πa, β a) a) := rfl lemma infi_apply {α : Type*} {β : α → Type*} {ι : Sort*} [Π i, has_Inf (β i)] {f : ι → Πa, β a} {a : α} : (⨅i, f i) a = (⨅i, f i a) := by rw [infi, Inf_apply, infi, infi, ← image_eq_range (λ f : Π i, β i, f a) (range f), ← range_comp] 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 supr_apply {α : Type*} {β : α → Type*} {ι : Sort*} [Π i, has_Sup (β i)] {f : ι → Πa, β a} {a : α} : (⨆i, f i) a = (⨆i, f i a) := @infi_apply α (λ i, order_dual (β i)) _ _ f a section complete_lattice variables [preorder α] [complete_lattice β] theorem monotone_Sup_of_monotone {s : set (α → β)} (m_s : ∀f∈s, monotone f) : monotone (Sup s) := assume x y h, supr_le $ λ f, le_supr_of_le f $ m_s f f.2 h theorem monotone_Inf_of_monotone {s : set (α → β)} (m_s : ∀f∈s, monotone f) : monotone (Inf s) := assume x y h, le_infi $ λ f, infi_le_of_le f $ m_s f f.2 h end complete_lattice namespace order_dual variable (α) instance [complete_lattice α] : complete_lattice (order_dual α) := { 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.bounded_lattice α, ..order_dual.has_Sup α, ..order_dual.has_Inf α } instance [complete_linear_order α] : complete_linear_order (order_dual α) := { .. order_dual.complete_lattice α, .. order_dual.decidable_linear_order α } end order_dual namespace prod variables (α β) instance [has_Inf α] [has_Inf β] : has_Inf (α × β) := ⟨λs, (Inf (prod.fst '' s), Inf (prod.snd '' s))⟩ instance [has_Sup α] [has_Sup β] : has_Sup (α × β) := ⟨λs, (Sup (prod.fst '' s), Sup (prod.snd '' s))⟩ instance [complete_lattice α] [complete_lattice β] : complete_lattice (α × β) := { le_Sup := assume s p hab, ⟨le_Sup $ mem_image_of_mem _ hab, le_Sup $ mem_image_of_mem _ hab⟩, Sup_le := assume s p h, ⟨ Sup_le $ ball_image_of_ball $ assume p hp, (h p hp).1, Sup_le $ ball_image_of_ball $ assume p hp, (h p hp).2⟩, Inf_le := assume s p hab, ⟨Inf_le $ mem_image_of_mem _ hab, Inf_le $ mem_image_of_mem _ hab⟩, le_Inf := assume s p h, ⟨ le_Inf $ ball_image_of_ball $ assume p hp, (h p hp).1, le_Inf $ ball_image_of_ball $ assume p hp, (h p hp).2⟩, .. prod.bounded_lattice α β, .. prod.has_Sup α β, .. prod.has_Inf α β } end prod
3a9846ba71b3bffa1067ace9a83bbcc039a96d52
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/ring_theory/nullstellensatz.lean
cc6e6adabe9b96fa5b40d365924f6f69efe74a0b
[ "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
8,177
lean
/- Copyright (c) 2021 Devon Tuma. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Devon Tuma -/ import ring_theory.jacobson import field_theory.is_alg_closed.basic import field_theory.mv_polynomial import algebraic_geometry.prime_spectrum /-! # Nullstellensatz This file establishes a version of Hilbert's classical Nullstellensatz for `mv_polynomial`s. The main statement of the theorem is `vanishing_ideal_zero_locus_eq_radical`. The statement is in terms of new definitions `vanishing_ideal` and `zero_locus`. Mathlib already has versions of these in terms of the prime spectrum of a ring, but those are not well-suited for expressing this result. Suggestions for better ways to state this theorem or organize things are welcome. The machinery around `vanishing_ideal` and `zero_locus` is also minimal, I only added lemmas directly needed in this proof, since I'm not sure if they are the right approach. -/ open ideal noncomputable theory namespace mv_polynomial open mv_polynomial variables {k : Type*} [field k] variables {σ : Type*} /-- Set of points that are zeroes of all polynomials in an ideal -/ def zero_locus (I : ideal (mv_polynomial σ k)) : set (σ → k) := {x : σ → k | ∀ p ∈ I, eval x p = 0} @[simp] lemma mem_zero_locus_iff {I : ideal (mv_polynomial σ k)} {x : σ → k} : x ∈ zero_locus I ↔ ∀ p ∈ I, eval x p = 0 := iff.rfl lemma zero_locus_anti_mono {I J : ideal (mv_polynomial σ k)} (h : I ≤ J) : zero_locus J ≤ zero_locus I := λ x hx p hp, hx p $ h hp lemma zero_locus_bot : zero_locus (⊥ : ideal (mv_polynomial σ k)) = ⊤ := eq_top_iff.2 (λ x hx p hp, trans (congr_arg (eval x) (mem_bot.1 hp)) (eval x).map_zero) lemma zero_locus_top : zero_locus (⊤ : ideal (mv_polynomial σ k)) = ⊥ := eq_bot_iff.2 $ λ x hx, one_ne_zero ((eval x).map_one ▸ (hx 1 submodule.mem_top) : (1 : k) = 0) /-- Ideal of polynomials with common zeroes at all elements of a set -/ def vanishing_ideal (V : set (σ → k)) : ideal (mv_polynomial σ k) := { carrier := {p | ∀ x ∈ V, eval x p = 0}, zero_mem' := λ x hx, ring_hom.map_zero _, add_mem' := λ p q hp hq x hx, by simp only [hq x hx, hp x hx, add_zero, ring_hom.map_add], smul_mem' := λ p q hq x hx, by simp only [hq x hx, algebra.id.smul_eq_mul, mul_zero, ring_hom.map_mul] } @[simp] lemma mem_vanishing_ideal_iff {V : set (σ → k)} {p : mv_polynomial σ k} : p ∈ vanishing_ideal V ↔ ∀ x ∈ V, eval x p = 0 := iff.rfl lemma vanishing_ideal_anti_mono {A B : set (σ → k)} (h : A ≤ B) : vanishing_ideal B ≤ vanishing_ideal A := λ p hp x hx, hp x $ h hx lemma vanishing_ideal_empty : vanishing_ideal (∅ : set (σ → k)) = ⊤ := le_antisymm le_top (λ p hp x hx, absurd hx (set.not_mem_empty x)) lemma le_vanishing_ideal_zero_locus (I : ideal (mv_polynomial σ k)) : I ≤ vanishing_ideal (zero_locus I) := λ p hp x hx, hx p hp lemma zero_locus_vanishing_ideal_le (V : set (σ → k)) : V ≤ zero_locus (vanishing_ideal V) := λ V hV p hp, hp V hV theorem zero_locus_vanishing_ideal_galois_connection : @galois_connection (ideal (mv_polynomial σ k)) (order_dual (set (σ → k))) _ _ zero_locus vanishing_ideal := λ I V, ⟨λ h, le_trans (le_vanishing_ideal_zero_locus I) (vanishing_ideal_anti_mono h), λ h, le_trans (zero_locus_anti_mono h) (zero_locus_vanishing_ideal_le V)⟩ lemma mem_vanishing_ideal_singleton_iff (x : σ → k) (p : mv_polynomial σ k) : p ∈ (vanishing_ideal {x} : ideal (mv_polynomial σ k)) ↔ (eval x p = 0) := ⟨λ h, h x rfl, λ hpx y hy, hy.symm ▸ hpx⟩ instance vanishing_ideal_singleton_is_maximal {x : σ → k} : (vanishing_ideal {x} : ideal (mv_polynomial σ k)).is_maximal := begin have : (vanishing_ideal {x} : ideal (mv_polynomial σ k)).quotient ≃+* k := ring_equiv.of_bijective (ideal.quotient.lift _ (eval x) (λ p h, (mem_vanishing_ideal_singleton_iff x p).mp h)) begin refine ⟨(ring_hom.injective_iff _).mpr (λ p hp, _), λ z, ⟨(ideal.quotient.mk (vanishing_ideal {x} : ideal (mv_polynomial σ k))) (C z), by simp⟩⟩, obtain ⟨q, rfl⟩ := quotient.mk_surjective p, rwa [ideal.quotient.lift_mk, ← mem_vanishing_ideal_singleton_iff, ← quotient.eq_zero_iff_mem] at hp, end, rw [← bot_quotient_is_maximal_iff, ring_equiv.bot_maximal_iff this], exact bot_is_maximal, end lemma radical_le_vanishing_ideal_zero_locus (I : ideal (mv_polynomial σ k)) : I.radical ≤ vanishing_ideal (zero_locus I) := begin intros p hp x hx, rw ← mem_vanishing_ideal_singleton_iff, rw radical_eq_Inf at hp, refine (mem_Inf.mp hp) ⟨le_trans (le_vanishing_ideal_zero_locus I) (vanishing_ideal_anti_mono (λ y hy, hy.symm ▸ hx)), is_maximal.is_prime' _⟩, end /-- The point in the prime spectrum assosiated to a given point -/ def point_to_point (x : σ → k) : prime_spectrum (mv_polynomial σ k) := ⟨(vanishing_ideal {x} : ideal (mv_polynomial σ k)), by apply_instance⟩ @[simp] lemma vanishing_ideal_point_to_point (V : set (σ → k)) : prime_spectrum.vanishing_ideal (point_to_point '' V) = mv_polynomial.vanishing_ideal V := le_antisymm (λ p hp x hx, (((prime_spectrum.mem_vanishing_ideal _ _).1 hp) ⟨vanishing_ideal {x}, by apply_instance⟩ ⟨x, ⟨hx, rfl⟩⟩) x rfl) (λ p hp, (prime_spectrum.mem_vanishing_ideal _ _).2 (λ I hI, let ⟨x, hx⟩ := hI in hx.2 ▸ λ x' hx', (set.mem_singleton_iff.1 hx').symm ▸ hp x hx.1)) lemma point_to_point_zero_locus_le (I : ideal (mv_polynomial σ k)) : point_to_point '' (mv_polynomial.zero_locus I) ≤ prime_spectrum.zero_locus ↑I := λ J hJ, let ⟨x, hx⟩ := hJ in (le_trans (le_vanishing_ideal_zero_locus I) (hx.2 ▸ vanishing_ideal_anti_mono (set.singleton_subset_iff.2 hx.1)) : I ≤ J.as_ideal) variables [is_alg_closed k] [fintype σ] lemma is_maximal_iff_eq_vanishing_ideal_singleton (I : ideal (mv_polynomial σ k)) : I.is_maximal ↔ ∃ (x : σ → k), I = vanishing_ideal {x} := begin refine ⟨λ hI, _, λ h, let ⟨x, hx⟩ := h in hx.symm ▸ (mv_polynomial.vanishing_ideal_singleton_is_maximal)⟩, letI : I.is_maximal := hI, letI : field I.quotient := quotient.field I, let ϕ : k →+* I.quotient := (ideal.quotient.mk I).comp C, have hϕ : function.bijective ϕ := ⟨quotient_mk_comp_C_injective _ _ I hI.ne_top, is_alg_closed.algebra_map_surjective_of_is_integral' ϕ (mv_polynomial.comp_C_integral_of_surjective_of_jacobson _ quotient.mk_surjective)⟩, obtain ⟨φ, hφ⟩ := function.surjective.has_right_inverse hϕ.2, let x : σ → k := λ s, φ ((ideal.quotient.mk I) (X s)), have hx : ∀ s : σ, ϕ (x s) = (ideal.quotient.mk I) (X s) := λ s, hφ ((ideal.quotient.mk I) (X s)), refine ⟨x, (is_maximal.eq_of_le (by apply_instance) hI.ne_top _).symm⟩, intros p hp, rw [← quotient.eq_zero_iff_mem, map_mv_polynomial_eq_eval₂ (ideal.quotient.mk I) p, eval₂_eq'], rw [mem_vanishing_ideal_singleton_iff, eval_eq'] at hp, convert (trans (congr_arg ϕ hp) ϕ.map_zero), simp only [ϕ.map_sum, ϕ.map_mul, ϕ.map_prod, ϕ.map_pow, hx], end /-- Main statement of the Nullstellensatz -/ @[simp] theorem vanishing_ideal_zero_locus_eq_radical (I : ideal (mv_polynomial σ k)) : vanishing_ideal (zero_locus I) = I.radical := begin rw I.radical_eq_jacobson, refine le_antisymm (le_Inf _) (λ p hp x hx, _), { rintros J ⟨hJI, hJ⟩, obtain ⟨x, hx⟩ := (is_maximal_iff_eq_vanishing_ideal_singleton J).1 hJ, refine hx.symm ▸ vanishing_ideal_anti_mono (λ y hy p hp, _), rw [← mem_vanishing_ideal_singleton_iff, set.mem_singleton_iff.1 hy, ← hx], refine hJI hp }, { rw ← mem_vanishing_ideal_singleton_iff x p, refine (mem_Inf.mp hp) ⟨le_trans (le_vanishing_ideal_zero_locus I) (vanishing_ideal_anti_mono (λ y hy, hy.symm ▸ hx)), mv_polynomial.vanishing_ideal_singleton_is_maximal⟩ }, end @[simp] lemma is_prime.vanishing_ideal_zero_locus (P : ideal (mv_polynomial σ k)) [h : P.is_prime] : vanishing_ideal (zero_locus P) = P := trans (vanishing_ideal_zero_locus_eq_radical P) h.radical end mv_polynomial
a2193eb6696413be5510e537fac0d66cf2ede7db
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebra/category/Mon/limits.lean
d297a826df94852c9e880c2143935b8e0c7321d8
[]
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
8,586
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.group.pi import Mathlib.algebra.category.Mon.basic import Mathlib.group_theory.submonoid.default import Mathlib.category_theory.limits.types import Mathlib.category_theory.limits.creates import Mathlib.PostPort universes u u_1 namespace Mathlib /-! # The category of (commutative) (additive) monoids has all limits Further, these limits are preserved by the forgetful functor --- that is, the underlying types are just the limits in the category of types. -/ namespace Mon protected instance monoid_obj {J : Type u} [category_theory.small_category J] (F : J ⥤ Mon) (j : J) : monoid (category_theory.functor.obj (F ⋙ category_theory.forget Mon) j) := id (Mon.monoid (category_theory.functor.obj F j)) /-- The flat sections of a functor into `Mon` form a submonoid of all sections. -/ def Mathlib.AddMon.sections_add_submonoid {J : Type u} [category_theory.small_category J] (F : J ⥤ AddMon) : add_submonoid ((j : J) → ↥(category_theory.functor.obj F j)) := add_submonoid.mk (category_theory.functor.sections (F ⋙ category_theory.forget AddMon)) sorry sorry protected instance Mathlib.AddMon.limit_add_monoid {J : Type u} [category_theory.small_category J] (F : J ⥤ AddMon) : add_monoid (category_theory.limits.cone.X (category_theory.limits.types.limit_cone (F ⋙ category_theory.forget AddMon))) := add_submonoid.to_add_monoid (AddMon.sections_add_submonoid F) /-- `limit.π (F ⋙ forget Mon) j` as a `monoid_hom`. -/ def Mathlib.AddMon.limit_π_add_monoid_hom {J : Type u} [category_theory.small_category J] (F : J ⥤ AddMon) (j : J) : category_theory.limits.cone.X (category_theory.limits.types.limit_cone (F ⋙ category_theory.forget AddMon)) →+ category_theory.functor.obj (F ⋙ category_theory.forget AddMon) j := add_monoid_hom.mk (category_theory.nat_trans.app (category_theory.limits.cone.π (category_theory.limits.types.limit_cone (F ⋙ category_theory.forget AddMon))) j) sorry sorry namespace has_limits -- The next two definitions are used in the construction of `has_limits Mon`. -- After that, the limits should be constructed using the generic limits API, -- e.g. `limit F`, `limit.cone F`, and `limit.is_limit F`. /-- Construction of a limit cone in `Mon`. (Internal use only; use the limits API.) -/ def Mathlib.AddMon.has_limits.limit_cone {J : Type u} [category_theory.small_category J] (F : J ⥤ AddMon) : category_theory.limits.cone F := category_theory.limits.cone.mk (AddMon.of (category_theory.limits.cone.X (category_theory.limits.types.limit_cone (F ⋙ category_theory.forget AddMon)))) (category_theory.nat_trans.mk (AddMon.limit_π_add_monoid_hom F)) /-- Witness that the limit cone in `Mon` is a limit cone. (Internal use only; use the limits API.) -/ def Mathlib.AddMon.has_limits.limit_cone_is_limit {J : Type u} [category_theory.small_category J] (F : J ⥤ AddMon) : category_theory.limits.is_limit (AddMon.has_limits.limit_cone F) := category_theory.limits.is_limit.of_faithful (category_theory.forget AddMon) (category_theory.limits.types.limit_cone_is_limit (F ⋙ category_theory.forget AddMon)) (fun (s : category_theory.limits.cone F) => add_monoid_hom.mk (fun (v : category_theory.limits.cone.X (category_theory.functor.map_cone (category_theory.forget AddMon) s)) => { val := fun (j : J) => category_theory.nat_trans.app (category_theory.limits.cone.π (category_theory.functor.map_cone (category_theory.forget AddMon) s)) j v, property := sorry }) sorry sorry) sorry end has_limits /-- The category of monoids has all limits. -/ protected instance Mathlib.AddMon.has_limits : category_theory.limits.has_limits AddMon := category_theory.limits.has_limits.mk fun (J : Type u_1) (𝒥 : category_theory.small_category J) => category_theory.limits.has_limits_of_shape.mk fun (F : J ⥤ AddMon) => category_theory.limits.has_limit.mk (category_theory.limits.limit_cone.mk sorry sorry) /-- The forgetful functor from monoids to types preserves all limits. (That is, the underlying types could have been computed instead as limits in the category of types.) -/ protected instance Mathlib.AddMon.forget_preserves_limits : category_theory.limits.preserves_limits (category_theory.forget AddMon) := category_theory.limits.preserves_limits.mk fun (J : Type u_1) (𝒥 : category_theory.small_category J) => category_theory.limits.preserves_limits_of_shape.mk fun (F : J ⥤ AddMon) => category_theory.limits.preserves_limit_of_preserves_limit_cone (AddMon.has_limits.limit_cone_is_limit F) (category_theory.limits.types.limit_cone_is_limit (F ⋙ category_theory.forget AddMon)) end Mon namespace CommMon protected instance comm_monoid_obj {J : Type u} [category_theory.small_category J] (F : J ⥤ CommMon) (j : J) : comm_monoid (category_theory.functor.obj (F ⋙ category_theory.forget CommMon) j) := id (CommMon.comm_monoid (category_theory.functor.obj F j)) protected instance limit_comm_monoid {J : Type u} [category_theory.small_category J] (F : J ⥤ CommMon) : comm_monoid (category_theory.limits.cone.X (category_theory.limits.types.limit_cone (F ⋙ category_theory.forget CommMon))) := submonoid.to_comm_monoid (Mon.sections_submonoid (F ⋙ category_theory.forget₂ CommMon Mon)) /-- We show that the forgetful functor `CommMon ⥤ Mon` creates limits. All we need to do is notice that the limit point has a `comm_monoid` instance available, and then reuse the existing limit. -/ protected instance category_theory.forget₂.category_theory.creates_limit {J : Type u} [category_theory.small_category J] (F : J ⥤ CommMon) : category_theory.creates_limit F (category_theory.forget₂ CommMon Mon) := sorry /-- A choice of limit cone for a functor into `CommMon`. (Generally, you'll just want to use `limit F`.) -/ def Mathlib.AddCommMon.limit_cone {J : Type u} [category_theory.small_category J] (F : J ⥤ AddCommMon) : category_theory.limits.cone F := category_theory.lift_limit (category_theory.limits.limit.is_limit (F ⋙ category_theory.forget₂ AddCommMon AddMon)) /-- The chosen cone is a limit cone. (Generally, you'll just want to use `limit.cone F`.) -/ def Mathlib.AddCommMon.limit_cone_is_limit {J : Type u} [category_theory.small_category J] (F : J ⥤ AddCommMon) : category_theory.limits.is_limit (AddCommMon.limit_cone F) := category_theory.lifted_limit_is_limit (category_theory.limits.limit.is_limit (F ⋙ category_theory.forget₂ AddCommMon AddMon)) /-- The category of commutative monoids has all limits. -/ protected instance has_limits : category_theory.limits.has_limits CommMon := category_theory.limits.has_limits.mk fun (J : Type u_1) (𝒥 : category_theory.small_category J) => category_theory.limits.has_limits_of_shape.mk fun (F : J ⥤ CommMon) => category_theory.has_limit_of_created F (category_theory.forget₂ CommMon Mon) /-- The forgetful functor from commutative monoids to monoids preserves all limits. (That is, the underlying monoid could have been computed instead as limits in the category of monoids.) -/ protected instance Mathlib.AddCommMon.forget₂_AddMon_preserves_limits : category_theory.limits.preserves_limits (category_theory.forget₂ AddCommMon AddMon) := category_theory.limits.preserves_limits.mk fun (J : Type u_1) (𝒥 : category_theory.small_category J) => category_theory.limits.preserves_limits_of_shape.mk fun (F : J ⥤ AddCommMon) => category_theory.preserves_limit_of_creates_limit_and_has_limit F (category_theory.forget₂ AddCommMon AddMon) /-- The forgetful functor from commutative monoids to types preserves all limits. (That is, the underlying types could have been computed instead as limits in the category of types.) -/ protected instance forget_preserves_limits : category_theory.limits.preserves_limits (category_theory.forget CommMon) := category_theory.limits.preserves_limits.mk fun (J : Type u_1) (𝒥 : category_theory.small_category J) => category_theory.limits.preserves_limits_of_shape.mk fun (F : J ⥤ CommMon) => category_theory.limits.comp_preserves_limit (category_theory.forget₂ CommMon Mon) (category_theory.forget Mon)
dcd1f5c8c34a9869c3b046aa09a5137ae9d02ef7
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/set_theory/ordinal/basic.lean
c9ccd28a73e4811983e94ef93802651ba5d8c2c8
[ "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
48,616
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Floris van Doorn -/ import data.sum.order import order.initial_seg import set_theory.cardinal.basic /-! # Ordinals > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Ordinals are defined as equivalences of well-ordered sets under order isomorphism. They are endowed with a total order, where an ordinal is smaller than another one if it embeds into it as an initial segment (or, equivalently, in any way). This total order is well founded. ## Main definitions * `ordinal`: the type of ordinals (in a given universe) * `ordinal.type r`: given a well-founded order `r`, this is the corresponding ordinal * `ordinal.typein r a`: given a well-founded order `r` on a type `α`, and `a : α`, the ordinal corresponding to all elements smaller than `a`. * `enum r o h`: given a well-order `r` on a type `α`, and an ordinal `o` strictly smaller than the ordinal corresponding to `r` (this is the assumption `h`), returns the `o`-th element of `α`. In other words, the elements of `α` can be enumerated using ordinals up to `type r`. * `ordinal.card o`: the cardinality of an ordinal `o`. * `ordinal.lift` lifts an ordinal in universe `u` to an ordinal in universe `max u v`. For a version registering additionally that this is an initial segment embedding, see `ordinal.lift.initial_seg`. For a version regiserting that it is a principal segment embedding if `u < v`, see `ordinal.lift.principal_seg`. * `ordinal.omega` or `ω` is the order type of `ℕ`. This definition is universe polymorphic: `ordinal.omega.{u} : ordinal.{u}` (contrast with `ℕ : Type`, which lives in a specific universe). In some cases the universe level has to be given explicitly. * `o₁ + o₂` is the order on the disjoint union of `o₁` and `o₂` obtained by declaring that every element of `o₁` is smaller than every element of `o₂`. The main properties of addition (and the other operations on ordinals) are stated and proved in `ordinal_arithmetic.lean`. Here, we only introduce it and prove its basic properties to deduce the fact that the order on ordinals is total (and well founded). * `succ o` is the successor of the ordinal `o`. * `cardinal.ord c`: when `c` is a cardinal, `ord c` is the smallest ordinal with this cardinality. It is the canonical way to represent a cardinal with an ordinal. A conditionally complete linear order with bot structure is registered on ordinals, where `⊥` is `0`, the ordinal corresponding to the empty type, and `Inf` is the minimum for nonempty sets and `0` for the empty set by convention. ## Notations * `ω` is a notation for the first infinite ordinal in the locale `ordinal`. -/ noncomputable theory open function cardinal set equiv order open_locale classical cardinal initial_seg universes u v w variables {α : Type*} {β : Type*} {γ : Type*} {r : α → α → Prop} {s : β → β → Prop} {t : γ → γ → Prop} /-! ### Well order on an arbitrary type -/ section well_ordering_thm parameter {σ : Type u} open function theorem nonempty_embedding_to_cardinal : nonempty (σ ↪ cardinal.{u}) := (embedding.total _ _).resolve_left $ λ ⟨⟨f, hf⟩⟩, let g : σ → cardinal.{u} := inv_fun f in let ⟨x, (hx : g x = 2 ^ sum g)⟩ := inv_fun_surjective hf (2 ^ sum g) in have g x ≤ sum g, from le_sum.{u u} g x, not_le_of_gt (by rw hx; exact cantor _) this /-- An embedding of any type to the set of cardinals. -/ def embedding_to_cardinal : σ ↪ cardinal.{u} := classical.choice nonempty_embedding_to_cardinal /-- Any type can be endowed with a well order, obtained by pulling back the well order over cardinals by some embedding. -/ def well_ordering_rel : σ → σ → Prop := embedding_to_cardinal ⁻¹'o (<) instance well_ordering_rel.is_well_order : is_well_order σ well_ordering_rel := (rel_embedding.preimage _ _).is_well_order instance is_well_order.subtype_nonempty : nonempty {r // is_well_order σ r} := ⟨⟨well_ordering_rel, infer_instance⟩⟩ end well_ordering_thm /-! ### Definition of ordinals -/ /-- Bundled structure registering a well order on a type. Ordinals will be defined as a quotient of this type. -/ structure Well_order : Type (u+1) := (α : Type u) (r : α → α → Prop) (wo : is_well_order α r) attribute [instance] Well_order.wo namespace Well_order instance : inhabited Well_order := ⟨⟨pempty, _, empty_relation.is_well_order⟩⟩ @[simp] lemma eta (o : Well_order) : mk o.α o.r o.wo = o := by { cases o, refl } end Well_order /-- Equivalence relation on well orders on arbitrary types in universe `u`, given by order isomorphism. -/ instance ordinal.is_equivalent : setoid Well_order := { r := λ ⟨α, r, wo⟩ ⟨β, s, wo'⟩, nonempty (r ≃r s), iseqv := ⟨λ ⟨α, r, _⟩, ⟨rel_iso.refl _⟩, λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨e⟩, ⟨e.symm⟩, λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩ ⟨e₁⟩ ⟨e₂⟩, ⟨e₁.trans e₂⟩⟩ } /-- `ordinal.{u}` is the type of well orders in `Type u`, up to order isomorphism. -/ def ordinal : Type (u + 1) := quotient ordinal.is_equivalent instance has_well_founded_out (o : ordinal) : has_well_founded o.out.α := ⟨o.out.r, o.out.wo.wf⟩ instance linear_order_out (o : ordinal) : linear_order o.out.α := is_well_order.linear_order o.out.r instance is_well_order_out_lt (o : ordinal) : is_well_order o.out.α (<) := o.out.wo namespace ordinal /- ### Basic properties of the order type -/ /-- The order type of a well order is an ordinal. -/ def type (r : α → α → Prop) [wo : is_well_order α r] : ordinal := ⟦⟨α, r, wo⟩⟧ instance : has_zero ordinal := ⟨type $ @empty_relation pempty⟩ instance : inhabited ordinal := ⟨0⟩ instance : has_one ordinal := ⟨type $ @empty_relation punit⟩ /-- The order type of an element inside a well order. For the embedding as a principal segment, see `typein.principal_seg`. -/ def typein (r : α → α → Prop) [is_well_order α r] (a : α) : ordinal := type (subrel r {b | r b a}) @[simp] theorem type_def' (w : Well_order) : ⟦w⟧ = type w.r := by { cases w, refl } @[simp] theorem type_def (r) [wo : is_well_order α r] : (⟦⟨α, r, wo⟩⟧ : ordinal) = type r := rfl @[simp] lemma type_out (o : ordinal) : ordinal.type o.out.r = o := by rw [ordinal.type, Well_order.eta, quotient.out_eq] theorem type_eq {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] : type r = type s ↔ nonempty (r ≃r s) := quotient.eq theorem _root_.rel_iso.ordinal_type_eq {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] (h : r ≃r s) : type r = type s := type_eq.2 ⟨h⟩ @[simp] theorem type_lt (o : ordinal) : type ((<) : o.out.α → o.out.α → Prop) = o := (type_def' _).symm.trans $ quotient.out_eq o theorem type_eq_zero_of_empty (r) [is_well_order α r] [is_empty α] : type r = 0 := (rel_iso.rel_iso_of_is_empty r _).ordinal_type_eq @[simp] theorem type_eq_zero_iff_is_empty [is_well_order α r] : type r = 0 ↔ is_empty α := ⟨λ h, let ⟨s⟩ := type_eq.1 h in s.to_equiv.is_empty, @type_eq_zero_of_empty α r _⟩ theorem type_ne_zero_iff_nonempty [is_well_order α r] : type r ≠ 0 ↔ nonempty α := by simp theorem type_ne_zero_of_nonempty (r) [is_well_order α r] [h : nonempty α] : type r ≠ 0 := type_ne_zero_iff_nonempty.2 h theorem type_pempty : type (@empty_relation pempty) = 0 := rfl theorem type_empty : type (@empty_relation empty) = 0 := type_eq_zero_of_empty _ theorem type_eq_one_of_unique (r) [is_well_order α r] [unique α] : type r = 1 := (rel_iso.rel_iso_of_unique_of_irrefl r _).ordinal_type_eq @[simp] theorem type_eq_one_iff_unique [is_well_order α r] : type r = 1 ↔ nonempty (unique α) := ⟨λ h, let ⟨s⟩ := type_eq.1 h in ⟨s.to_equiv.unique⟩, λ ⟨h⟩, @type_eq_one_of_unique α r _ h⟩ theorem type_punit : type (@empty_relation punit) = 1 := rfl theorem type_unit : type (@empty_relation unit) = 1 := rfl @[simp] theorem out_empty_iff_eq_zero {o : ordinal} : is_empty o.out.α ↔ o = 0 := by rw [←@type_eq_zero_iff_is_empty o.out.α (<), type_lt] lemma eq_zero_of_out_empty (o : ordinal) [h : is_empty o.out.α] : o = 0 := out_empty_iff_eq_zero.1 h instance is_empty_out_zero : is_empty (0 : ordinal).out.α := out_empty_iff_eq_zero.2 rfl @[simp] theorem out_nonempty_iff_ne_zero {o : ordinal} : nonempty o.out.α ↔ o ≠ 0 := by rw [←@type_ne_zero_iff_nonempty o.out.α (<), type_lt] lemma ne_zero_of_out_nonempty (o : ordinal) [h : nonempty o.out.α] : o ≠ 0 := out_nonempty_iff_ne_zero.1 h protected lemma one_ne_zero : (1 : ordinal) ≠ 0 := type_ne_zero_of_nonempty _ instance : nontrivial ordinal.{u} := ⟨⟨1, 0, ordinal.one_ne_zero⟩⟩ @[simp] theorem type_preimage {α β : Type u} (r : α → α → Prop) [is_well_order α r] (f : β ≃ α) : type (f ⁻¹'o r) = type r := (rel_iso.preimage f r).ordinal_type_eq @[elab_as_eliminator] theorem induction_on {C : ordinal → Prop} (o : ordinal) (H : ∀ α r [is_well_order α r], by exactI C (type r)) : C o := quot.induction_on o $ λ ⟨α, r, wo⟩, @H α r wo /-! ### The order on ordinals -/ instance : partial_order ordinal := { le := λ a b, quotient.lift_on₂ a b (λ ⟨α, r, wo⟩ ⟨β, s, wo'⟩, nonempty (r ≼i s)) $ λ ⟨α₁, r₁, o₁⟩ ⟨α₂, r₂, o₂⟩ ⟨β₁, s₁, p₁⟩ ⟨β₂, s₂, p₂⟩ ⟨f⟩ ⟨g⟩, propext ⟨ λ ⟨h⟩, ⟨(initial_seg.of_iso f.symm).trans $ h.trans (initial_seg.of_iso g)⟩, λ ⟨h⟩, ⟨(initial_seg.of_iso f).trans $ h.trans (initial_seg.of_iso g.symm)⟩⟩, lt := λ a b, quotient.lift_on₂ a b (λ ⟨α, r, wo⟩ ⟨β, s, wo'⟩, nonempty (r ≺i s)) $ λ ⟨α₁, r₁, o₁⟩ ⟨α₂, r₂, o₂⟩ ⟨β₁, s₁, p₁⟩ ⟨β₂, s₂, p₂⟩ ⟨f⟩ ⟨g⟩, by exactI propext ⟨ λ ⟨h⟩, ⟨principal_seg.equiv_lt f.symm $ h.lt_le (initial_seg.of_iso g)⟩, λ ⟨h⟩, ⟨principal_seg.equiv_lt f $ h.lt_le (initial_seg.of_iso g.symm)⟩⟩, le_refl := quot.ind $ by exact λ ⟨α, r, wo⟩, ⟨initial_seg.refl _⟩, le_trans := λ a b c, quotient.induction_on₃ a b c $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩ ⟨f⟩ ⟨g⟩, ⟨f.trans g⟩, lt_iff_le_not_le := λ a b, quotient.induction_on₂ a b $ λ ⟨α, r, _⟩ ⟨β, s, _⟩, by exactI ⟨λ ⟨f⟩, ⟨⟨f⟩, λ ⟨g⟩, (f.lt_le g).irrefl⟩, λ ⟨⟨f⟩, h⟩, sum.rec_on f.lt_or_eq (λ g, ⟨g⟩) (λ g, (h ⟨initial_seg.of_iso g.symm⟩).elim)⟩, le_antisymm := λ a b, quotient.induction_on₂ a b $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨h₁⟩ ⟨h₂⟩, by exactI quot.sound ⟨initial_seg.antisymm h₁ h₂⟩ } /-- Ordinal less-equal is defined such that well orders `r` and `s` satisfy `type r ≤ type s` if there exists a function embedding `r` as an initial segment of `s`. -/ add_decl_doc ordinal.partial_order.le /-- Ordinal less-than is defined such that well orders `r` and `s` satisfy `type r < type s` if there exists a function embedding `r` as a principal segment of `s`. -/ add_decl_doc ordinal.partial_order.lt theorem type_le_iff {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] : type r ≤ type s ↔ nonempty (r ≼i s) := iff.rfl theorem type_le_iff' {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] : type r ≤ type s ↔ nonempty (r ↪r s) := ⟨λ ⟨f⟩, ⟨f⟩, λ ⟨f⟩, ⟨f.collapse⟩⟩ theorem _root_.initial_seg.ordinal_type_le {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] (h : r ≼i s) : type r ≤ type s := ⟨h⟩ theorem _root_.rel_embedding.ordinal_type_le {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] (h : r ↪r s) : type r ≤ type s := ⟨h.collapse⟩ @[simp] theorem type_lt_iff {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] : type r < type s ↔ nonempty (r ≺i s) := iff.rfl theorem _root_.principal_seg.ordinal_type_lt {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] (h : r ≺i s) : type r < type s := ⟨h⟩ protected theorem zero_le (o : ordinal) : 0 ≤ o := induction_on o $ λ α r _, by exactI (initial_seg.of_is_empty _ r).ordinal_type_le instance : order_bot ordinal := ⟨0, ordinal.zero_le⟩ @[simp] lemma bot_eq_zero : (⊥ : ordinal) = 0 := rfl @[simp] protected theorem le_zero {o : ordinal} : o ≤ 0 ↔ o = 0 := le_bot_iff protected theorem pos_iff_ne_zero {o : ordinal} : 0 < o ↔ o ≠ 0 := bot_lt_iff_ne_bot protected theorem not_lt_zero (o : ordinal) : ¬ o < 0 := not_lt_bot theorem eq_zero_or_pos : ∀ a : ordinal, a = 0 ∨ 0 < a := eq_bot_or_bot_lt instance : zero_le_one_class ordinal := ⟨ordinal.zero_le _⟩ instance ne_zero.one : ne_zero (1 : ordinal) := ⟨ordinal.one_ne_zero⟩ /-- Given two ordinals `α ≤ β`, then `initial_seg_out α β` is the initial segment embedding of `α` to `β`, as map from a model type for `α` to a model type for `β`. -/ def initial_seg_out {α β : ordinal} (h : α ≤ β) : initial_seg ((<) : α.out.α → α.out.α → Prop) ((<) : β.out.α → β.out.α → Prop) := begin change α.out.r ≼i β.out.r, rw [←quotient.out_eq α, ←quotient.out_eq β] at h, revert h, cases quotient.out α, cases quotient.out β, exact classical.choice end /-- Given two ordinals `α < β`, then `principal_seg_out α β` is the principal segment embedding of `α` to `β`, as map from a model type for `α` to a model type for `β`. -/ def principal_seg_out {α β : ordinal} (h : α < β) : principal_seg ((<) : α.out.α → α.out.α → Prop) ((<) : β.out.α → β.out.α → Prop) := begin change α.out.r ≺i β.out.r, rw [←quotient.out_eq α, ←quotient.out_eq β] at h, revert h, cases quotient.out α, cases quotient.out β, exact classical.choice end theorem typein_lt_type (r : α → α → Prop) [is_well_order α r] (a : α) : typein r a < type r := ⟨principal_seg.of_element _ _⟩ theorem typein_lt_self {o : ordinal} (i : o.out.α) : typein (<) i < o := by { simp_rw ←type_lt o, apply typein_lt_type } @[simp] theorem typein_top {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] (f : r ≺i s) : typein s f.top = type r := eq.symm $ quot.sound ⟨rel_iso.of_surjective (rel_embedding.cod_restrict _ f f.lt_top) (λ ⟨a, h⟩, by rcases f.down.1 h with ⟨b, rfl⟩; exact ⟨b, rfl⟩)⟩ @[simp] theorem typein_apply {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] (f : r ≼i s) (a : α) : ordinal.typein s (f a) = ordinal.typein r a := eq.symm $ quotient.sound ⟨rel_iso.of_surjective (rel_embedding.cod_restrict _ ((subrel.rel_embedding _ _).trans f) (λ ⟨x, h⟩, by rw [rel_embedding.trans_apply]; exact f.to_rel_embedding.map_rel_iff.2 h)) (λ ⟨y, h⟩, by rcases f.init h with ⟨a, rfl⟩; exact ⟨⟨a, f.to_rel_embedding.map_rel_iff.1 h⟩, subtype.eq $ rel_embedding.trans_apply _ _ _⟩)⟩ @[simp] theorem typein_lt_typein (r : α → α → Prop) [is_well_order α r] {a b : α} : typein r a < typein r b ↔ r a b := ⟨λ ⟨f⟩, begin have : f.top.1 = a, { let f' := principal_seg.of_element r a, let g' := f.trans (principal_seg.of_element r b), have : g'.top = f'.top, {rw subsingleton.elim f' g'}, exact this }, rw ← this, exact f.top.2 end, λ h, ⟨principal_seg.cod_restrict _ (principal_seg.of_element r a) (λ x, @trans _ r _ _ _ _ x.2 h) h⟩⟩ theorem typein_surj (r : α → α → Prop) [is_well_order α r] {o} (h : o < type r) : ∃ a, typein r a = o := induction_on o (λ β s _ ⟨f⟩, by exactI ⟨f.top, typein_top _⟩) h lemma typein_injective (r : α → α → Prop) [is_well_order α r] : injective (typein r) := injective_of_increasing r (<) (typein r) (λ x y, (typein_lt_typein r).2) @[simp] theorem typein_inj (r : α → α → Prop) [is_well_order α r] {a b} : typein r a = typein r b ↔ a = b := (typein_injective r).eq_iff /-- Principal segment version of the `typein` function, embedding a well order into ordinals as a principal segment. -/ def typein.principal_seg (r : α → α → Prop) [is_well_order α r] : r ≺i ((<) : ordinal → ordinal → Prop) := ⟨⟨⟨typein r, typein_injective r⟩, λ a b, typein_lt_typein r⟩, type r, λ o, ⟨typein_surj r, λ ⟨a, h⟩, h ▸ typein_lt_type r a⟩⟩ @[simp] theorem typein.principal_seg_coe (r : α → α → Prop) [is_well_order α r] : (typein.principal_seg r : α → ordinal) = typein r := rfl /-! ### Enumerating elements in a well-order with ordinals. -/ /-- `enum r o h` is the `o`-th element of `α` ordered by `r`. That is, `enum` maps an initial segment of the ordinals, those less than the order type of `r`, to the elements of `α`. -/ def enum (r : α → α → Prop) [is_well_order α r] (o) (h : o < type r) : α := (typein.principal_seg r).subrel_iso ⟨o, h⟩ theorem enum_type {α β} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] (f : s ≺i r) {h : type s < type r} : enum r (type s) h = f.top := (typein.principal_seg r).injective $ ((typein.principal_seg r).apply_subrel_iso _).trans (typein_top _).symm @[simp] theorem enum_typein (r : α → α → Prop) [is_well_order α r] (a : α) : enum r (typein r a) (typein_lt_type r a) = a := enum_type (principal_seg.of_element r a) @[simp] theorem typein_enum (r : α → α → Prop) [is_well_order α r] {o} (h : o < type r) : typein r (enum r o h) = o := let ⟨a, e⟩ := typein_surj r h in by clear _let_match; subst e; rw enum_typein theorem enum_lt_enum {r : α → α → Prop} [is_well_order α r] {o₁ o₂ : ordinal} (h₁ : o₁ < type r) (h₂ : o₂ < type r) : r (enum r o₁ h₁) (enum r o₂ h₂) ↔ o₁ < o₂ := by rw [← typein_lt_typein r, typein_enum, typein_enum] lemma rel_iso_enum' {α β : Type u} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] (f : r ≃r s) (o : ordinal) : ∀(hr : o < type r) (hs : o < type s), f (enum r o hr) = enum s o hs := begin refine induction_on o _, rintros γ t wo ⟨g⟩ ⟨h⟩, resetI, rw [enum_type g, enum_type (principal_seg.lt_equiv g f)], refl end lemma rel_iso_enum {α β : Type u} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] (f : r ≃r s) (o : ordinal) (hr : o < type r) : f (enum r o hr) = enum s o (by {convert hr using 1, apply quotient.sound, exact ⟨f.symm⟩ }) := rel_iso_enum' _ _ _ _ theorem lt_wf : @well_founded ordinal (<) := well_founded_iff_well_founded_subrel.mpr $ λ a, induction_on a $ λ α r wo, by exactI rel_hom_class.well_founded (typein.principal_seg r).subrel_iso wo.wf instance : has_well_founded ordinal := ⟨(<), lt_wf⟩ /-- Reformulation of well founded induction on ordinals as a lemma that works with the `induction` tactic, as in `induction i using ordinal.induction with i IH`. -/ lemma induction {p : ordinal.{u} → Prop} (i : ordinal.{u}) (h : ∀ j, (∀ k, k < j → p k) → p j) : p i := lt_wf.induction i h /-! ### Cardinality of ordinals -/ /-- The cardinal of an ordinal is the cardinality of any type on which a relation with that order type is defined. -/ def card : ordinal → cardinal := quotient.map Well_order.α $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨e⟩, ⟨e.to_equiv⟩ @[simp] theorem card_type (r : α → α → Prop) [is_well_order α r] : card (type r) = #α := rfl @[simp] lemma card_typein {r : α → α → Prop} [wo : is_well_order α r] (x : α) : #{y // r y x} = (typein r x).card := rfl theorem card_le_card {o₁ o₂ : ordinal} : o₁ ≤ o₂ → card o₁ ≤ card o₂ := induction_on o₁ $ λ α r _, induction_on o₂ $ λ β s _ ⟨⟨⟨f, _⟩, _⟩⟩, ⟨f⟩ @[simp] theorem card_zero : card 0 = 0 := rfl @[simp] theorem card_eq_zero {o} : card o = 0 ↔ o = 0 := ⟨induction_on o $ λ α r _ h, begin haveI := cardinal.mk_eq_zero_iff.1 h, apply type_eq_zero_of_empty end, λ e, by simp only [e, card_zero]⟩ @[simp] theorem card_one : card 1 = 1 := rfl /-! ### Lifting ordinals to a higher universe -/ /-- The universe lift operation for ordinals, which embeds `ordinal.{u}` as a proper initial segment of `ordinal.{v}` for `v > u`. For the initial segment version, see `lift.initial_seg`. -/ def lift (o : ordinal.{v}) : ordinal.{max v u} := quotient.lift_on o (λ w, type $ ulift.down ⁻¹'o w.r) $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨f⟩, quot.sound ⟨(rel_iso.preimage equiv.ulift r).trans $ f.trans (rel_iso.preimage equiv.ulift s).symm⟩ @[simp] theorem type_ulift (r : α → α → Prop) [is_well_order α r] : type (ulift.down ⁻¹'o r) = lift.{v} (type r) := rfl theorem _root_.rel_iso.ordinal_lift_type_eq {α : Type u} {β : Type v} {r : α → α → Prop} {s : β → β → Prop} [is_well_order α r] [is_well_order β s] (f : r ≃r s) : lift.{v} (type r) = lift.{u} (type s) := ((rel_iso.preimage equiv.ulift r).trans $ f.trans (rel_iso.preimage equiv.ulift s).symm).ordinal_type_eq @[simp] theorem type_lift_preimage {α : Type u} {β : Type v} (r : α → α → Prop) [is_well_order α r] (f : β ≃ α) : lift.{u} (type (f ⁻¹'o r)) = lift.{v} (type r) := (rel_iso.preimage f r).ordinal_lift_type_eq /-- `lift.{(max u v) u}` equals `lift.{v u}`. Using `set_option pp.universes true` will make it much easier to understand what's happening when using this lemma. -/ @[simp] theorem lift_umax : lift.{(max u v) u} = lift.{v u} := funext $ λ a, induction_on a $ λ α r _, quotient.sound ⟨(rel_iso.preimage equiv.ulift r).trans (rel_iso.preimage equiv.ulift r).symm⟩ /-- `lift.{(max v u) u}` equals `lift.{v u}`. Using `set_option pp.universes true` will make it much easier to understand what's happening when using this lemma. -/ @[simp] theorem lift_umax' : lift.{(max v u) u} = lift.{v u} := lift_umax /-- An ordinal lifted to a lower or equal universe equals itself. -/ @[simp] theorem lift_id' (a : ordinal) : lift a = a := induction_on a $ λ α r _, quotient.sound ⟨rel_iso.preimage equiv.ulift r⟩ /-- An ordinal lifted to the same universe equals itself. -/ @[simp] theorem lift_id : ∀ a, lift.{u u} a = a := lift_id'.{u u} /-- An ordinal lifted to the zero universe equals itself. -/ @[simp] theorem lift_uzero (a : ordinal.{u}) : lift.{0} a = a := lift_id'.{0 u} a @[simp] theorem lift_lift (a : ordinal) : lift.{w} (lift.{v} a) = lift.{max v w} a := induction_on a $ λ α r _, quotient.sound ⟨(rel_iso.preimage equiv.ulift _).trans $ (rel_iso.preimage equiv.ulift _).trans (rel_iso.preimage equiv.ulift _).symm⟩ theorem lift_type_le {α : Type u} {β : Type v} {r s} [is_well_order α r] [is_well_order β s] : lift.{max v w} (type r) ≤ lift.{max u w} (type s) ↔ nonempty (r ≼i s) := ⟨λ ⟨f⟩, ⟨(initial_seg.of_iso (rel_iso.preimage equiv.ulift r).symm).trans $ f.trans (initial_seg.of_iso (rel_iso.preimage equiv.ulift s))⟩, λ ⟨f⟩, ⟨(initial_seg.of_iso (rel_iso.preimage equiv.ulift r)).trans $ f.trans (initial_seg.of_iso (rel_iso.preimage equiv.ulift s).symm)⟩⟩ theorem lift_type_eq {α : Type u} {β : Type v} {r s} [is_well_order α r] [is_well_order β s] : lift.{max v w} (type r) = lift.{max u w} (type s) ↔ nonempty (r ≃r s) := quotient.eq.trans ⟨λ ⟨f⟩, ⟨(rel_iso.preimage equiv.ulift r).symm.trans $ f.trans (rel_iso.preimage equiv.ulift s)⟩, λ ⟨f⟩, ⟨(rel_iso.preimage equiv.ulift r).trans $ f.trans (rel_iso.preimage equiv.ulift s).symm⟩⟩ theorem lift_type_lt {α : Type u} {β : Type v} {r s} [is_well_order α r] [is_well_order β s] : lift.{max v w} (type r) < lift.{max u w} (type s) ↔ nonempty (r ≺i s) := by haveI := @rel_embedding.is_well_order _ _ (@equiv.ulift.{max v w} α ⁻¹'o r) r (rel_iso.preimage equiv.ulift.{max v w} r) _; haveI := @rel_embedding.is_well_order _ _ (@equiv.ulift.{max u w} β ⁻¹'o s) s (rel_iso.preimage equiv.ulift.{max u w} s) _; exact ⟨λ ⟨f⟩, ⟨(f.equiv_lt (rel_iso.preimage equiv.ulift r).symm).lt_le (initial_seg.of_iso (rel_iso.preimage equiv.ulift s))⟩, λ ⟨f⟩, ⟨(f.equiv_lt (rel_iso.preimage equiv.ulift r)).lt_le (initial_seg.of_iso (rel_iso.preimage equiv.ulift s).symm)⟩⟩ @[simp] theorem lift_le {a b : ordinal} : lift.{u v} a ≤ lift b ↔ a ≤ b := induction_on a $ λ α r _, induction_on b $ λ β s _, by { rw ← lift_umax, exactI lift_type_le } @[simp] theorem lift_inj {a b : ordinal} : lift a = lift b ↔ a = b := by simp only [le_antisymm_iff, lift_le] @[simp] theorem lift_lt {a b : ordinal} : lift a < lift b ↔ a < b := by simp only [lt_iff_le_not_le, lift_le] @[simp] theorem lift_zero : lift 0 = 0 := type_eq_zero_of_empty _ @[simp] theorem lift_one : lift 1 = 1 := type_eq_one_of_unique _ @[simp] theorem lift_card (a) : (card a).lift = card (lift a) := induction_on a $ λ α r _, rfl theorem lift_down' {a : cardinal.{u}} {b : ordinal.{max u v}} (h : card b ≤ a.lift) : ∃ a', lift a' = b := let ⟨c, e⟩ := cardinal.lift_down h in cardinal.induction_on c (λ α, induction_on b $ λ β s _ e', begin resetI, rw [card_type, ← cardinal.lift_id'.{(max u v) u} (#β), ← cardinal.lift_umax.{u v}, lift_mk_eq.{u (max u v) (max u v)}] at e', cases e' with f, have g := rel_iso.preimage f s, haveI := (g : ⇑f ⁻¹'o s ↪r s).is_well_order, have := lift_type_eq.{u (max u v) (max u v)}.2 ⟨g⟩, rw [lift_id, lift_umax.{u v}] at this, exact ⟨_, this⟩ end) e theorem lift_down {a : ordinal.{u}} {b : ordinal.{max u v}} (h : b ≤ lift a) : ∃ a', lift a' = b := @lift_down' (card a) _ (by rw lift_card; exact card_le_card h) theorem le_lift_iff {a : ordinal.{u}} {b : ordinal.{max u v}} : b ≤ lift a ↔ ∃ a', lift a' = b ∧ a' ≤ a := ⟨λ h, let ⟨a', e⟩ := lift_down h in ⟨a', e, lift_le.1 $ e.symm ▸ h⟩, λ ⟨a', e, h⟩, e ▸ lift_le.2 h⟩ theorem lt_lift_iff {a : ordinal.{u}} {b : ordinal.{max u v}} : b < lift a ↔ ∃ a', lift a' = b ∧ a' < a := ⟨λ h, let ⟨a', e⟩ := lift_down (le_of_lt h) in ⟨a', e, lift_lt.1 $ e.symm ▸ h⟩, λ ⟨a', e, h⟩, e ▸ lift_lt.2 h⟩ /-- Initial segment version of the lift operation on ordinals, embedding `ordinal.{u}` in `ordinal.{v}` as an initial segment when `u ≤ v`. -/ def lift.initial_seg : @initial_seg ordinal.{u} ordinal.{max u v} (<) (<) := ⟨⟨⟨lift.{v}, λ a b, lift_inj.1⟩, λ a b, lift_lt⟩, λ a b h, lift_down (le_of_lt h)⟩ @[simp] theorem lift.initial_seg_coe : (lift.initial_seg : ordinal → ordinal) = lift := rfl /-! ### The first infinite ordinal `omega` -/ /-- `ω` is the first infinite ordinal, defined as the order type of `ℕ`. -/ def omega : ordinal.{u} := lift $ @type ℕ (<) _ localized "notation (name := ordinal.omega) `ω` := ordinal.omega" in ordinal /-- Note that the presence of this lemma makes `simp [omega]` form a loop. -/ @[simp] theorem type_nat_lt : @type ℕ (<) _ = ω := (lift_id _).symm @[simp] theorem card_omega : card ω = ℵ₀ := rfl @[simp] theorem lift_omega : lift ω = ω := lift_lift _ /-! ### Definition and first properties of addition on ordinals In this paragraph, we introduce the addition on ordinals, and prove just enough properties to deduce that the order on ordinals is total (and therefore well-founded). Further properties of the addition, together with properties of the other operations, are proved in `ordinal_arithmetic.lean`. -/ /-- `o₁ + o₂` is the order on the disjoint union of `o₁` and `o₂` obtained by declaring that every element of `o₁` is smaller than every element of `o₂`. -/ instance : has_add ordinal.{u} := ⟨λ o₁ o₂, quotient.lift_on₂ o₁ o₂ (λ ⟨α, r, wo⟩ ⟨β, s, wo'⟩, by exactI type (sum.lex r s)) $ λ ⟨α₁, r₁, o₁⟩ ⟨α₂, r₂, o₂⟩ ⟨β₁, s₁, p₁⟩ ⟨β₂, s₂, p₂⟩ ⟨f⟩ ⟨g⟩, quot.sound ⟨rel_iso.sum_lex_congr f g⟩⟩ instance : add_monoid_with_one ordinal.{u} := { add := (+), zero := 0, one := 1, zero_add := λ o, induction_on o $ λ α r _, eq.symm $ quotient.sound ⟨⟨(empty_sum pempty α).symm, λ a b, sum.lex_inr_inr⟩⟩, add_zero := λ o, induction_on o $ λ α r _, eq.symm $ quotient.sound ⟨⟨(sum_empty α pempty).symm, λ a b, sum.lex_inl_inl⟩⟩, add_assoc := λ o₁ o₂ o₃, quotient.induction_on₃ o₁ o₂ o₃ $ λ ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩, quot.sound ⟨⟨sum_assoc _ _ _, λ a b, begin rcases a with ⟨a|a⟩|a; rcases b with ⟨b|b⟩|b; simp only [sum_assoc_apply_inl_inl, sum_assoc_apply_inl_inr, sum_assoc_apply_inr, sum.lex_inl_inl, sum.lex_inr_inr, sum.lex.sep, sum.lex_inr_inl] end⟩⟩ } @[simp] theorem card_add (o₁ o₂ : ordinal) : card (o₁ + o₂) = card o₁ + card o₂ := induction_on o₁ $ λ α r _, induction_on o₂ $ λ β s _, rfl @[simp] theorem type_sum_lex {α β : Type u} (r : α → α → Prop) (s : β → β → Prop) [is_well_order α r] [is_well_order β s] : type (sum.lex r s) = type r + type s := rfl @[simp] theorem card_nat (n : ℕ) : card.{u} n = n := by induction n; [refl, simp only [card_add, card_one, nat.cast_succ, *]] instance add_covariant_class_le : covariant_class ordinal.{u} ordinal.{u} (+) (≤) := ⟨λ c a b h, begin revert h c, exact ( induction_on a $ λ α₁ r₁ _, induction_on b $ λ α₂ r₂ _ ⟨⟨⟨f, fo⟩, fi⟩⟩ c, induction_on c $ λ β s _, ⟨⟨⟨(embedding.refl _).sum_map f, λ a b, match a, b with | sum.inl a, sum.inl b := sum.lex_inl_inl.trans sum.lex_inl_inl.symm | sum.inl a, sum.inr b := by apply iff_of_true; apply sum.lex.sep | sum.inr a, sum.inl b := by apply iff_of_false; exact sum.lex_inr_inl | sum.inr a, sum.inr b := sum.lex_inr_inr.trans $ fo.trans sum.lex_inr_inr.symm end⟩, λ a b H, match a, b, H with | _, sum.inl b, _ := ⟨sum.inl b, rfl⟩ | sum.inl a, sum.inr b, H := (sum.lex_inr_inl H).elim | sum.inr a, sum.inr b, H := let ⟨w, h⟩ := fi _ _ (sum.lex_inr_inr.1 H) in ⟨sum.inr w, congr_arg sum.inr h⟩ end⟩⟩) end⟩ instance add_swap_covariant_class_le : covariant_class ordinal.{u} ordinal.{u} (swap (+)) (≤) := ⟨λ c a b h, begin revert h c, exact ( induction_on a $ λ α₁ r₁ hr₁, induction_on b $ λ α₂ r₂ hr₂ ⟨⟨⟨f, fo⟩, fi⟩⟩ c, induction_on c $ λ β s hs, by exactI @rel_embedding.ordinal_type_le _ _ (sum.lex r₁ s) (sum.lex r₂ s) _ _ ⟨f.sum_map (embedding.refl _), λ a b, begin split; intro H, { cases a with a a; cases b with b b; cases H; constructor; [rwa ← fo, assumption] }, { cases H; constructor; [rwa fo, assumption] } end⟩) end⟩ theorem le_add_right (a b : ordinal) : a ≤ a + b := by simpa only [add_zero] using add_le_add_left (ordinal.zero_le b) a theorem le_add_left (a b : ordinal) : a ≤ b + a := by simpa only [zero_add] using add_le_add_right (ordinal.zero_le b) a instance : linear_order ordinal := { le_total := λ a b, match lt_or_eq_of_le (le_add_left b a), lt_or_eq_of_le (le_add_right a b) with | or.inr h, _ := by rw h; exact or.inl (le_add_right _ _) | _, or.inr h := by rw h; exact or.inr (le_add_left _ _) | or.inl h₁, or.inl h₂ := induction_on a (λ α₁ r₁ _, induction_on b $ λ α₂ r₂ _ ⟨f⟩ ⟨g⟩, begin resetI, rw [← typein_top f, ← typein_top g, le_iff_lt_or_eq, le_iff_lt_or_eq, typein_lt_typein, typein_lt_typein], rcases trichotomous_of (sum.lex r₁ r₂) g.top f.top with h|h|h; [exact or.inl (or.inl h), {left, right, rw h}, exact or.inr (or.inl h)] end) h₁ h₂ end, decidable_le := classical.dec_rel _, ..ordinal.partial_order } instance : well_founded_lt ordinal := ⟨lt_wf⟩ instance : is_well_order ordinal (<) := { } instance : conditionally_complete_linear_order_bot ordinal := is_well_order.conditionally_complete_linear_order_bot _ @[simp] lemma max_zero_left : ∀ a : ordinal, max 0 a = a := max_bot_left @[simp] lemma max_zero_right : ∀ a : ordinal, max a 0 = a := max_bot_right @[simp] lemma max_eq_zero {a b : ordinal} : max a b = 0 ↔ a = 0 ∧ b = 0 := max_eq_bot @[simp] theorem Inf_empty : Inf (∅ : set ordinal) = 0 := dif_neg not_nonempty_empty /- ### Successor order properties -/ private theorem succ_le_iff' {a b : ordinal} : a + 1 ≤ b ↔ a < b := ⟨lt_of_lt_of_le (induction_on a $ λ α r _, ⟨⟨⟨⟨λ x, sum.inl x, λ _ _, sum.inl.inj⟩, λ _ _, sum.lex_inl_inl⟩, sum.inr punit.star, λ b, sum.rec_on b (λ x, ⟨λ _, ⟨x, rfl⟩, λ _, sum.lex.sep _ _⟩) (λ x, sum.lex_inr_inr.trans ⟨false.elim, λ ⟨x, H⟩, sum.inl_ne_inr H⟩)⟩⟩), induction_on a $ λ α r hr, induction_on b $ λ β s hs ⟨⟨f, t, hf⟩⟩, begin haveI := hs, refine ⟨⟨@rel_embedding.of_monotone (α ⊕ punit) β _ _ _ _ (sum.rec _ _) (λ a b, _), λ a b, _⟩⟩, { exact f }, { exact λ _, t }, { rcases a with a|_; rcases b with b|_, { simpa only [sum.lex_inl_inl] using f.map_rel_iff.2 }, { intro _, rw hf, exact ⟨_, rfl⟩ }, { exact false.elim ∘ sum.lex_inr_inl }, { exact false.elim ∘ sum.lex_inr_inr.1 } }, { rcases a with a|_, { intro h, have := @principal_seg.init _ _ _ _ _ ⟨f, t, hf⟩ _ _ h, cases this with w h, exact ⟨sum.inl w, h⟩ }, { intro h, cases (hf b).1 h with w h, exact ⟨sum.inl w, h⟩ } } end⟩ instance : no_max_order ordinal := ⟨λ a, ⟨_, succ_le_iff'.1 le_rfl⟩⟩ instance : succ_order ordinal.{u} := succ_order.of_succ_le_iff (λ o, o + 1) (λ a b, succ_le_iff') @[simp] theorem add_one_eq_succ (o : ordinal) : o + 1 = succ o := rfl @[simp] theorem succ_zero : succ (0 : ordinal) = 1 := zero_add 1 @[simp] theorem succ_one : succ (1 : ordinal) = 2 := rfl theorem add_succ (o₁ o₂ : ordinal) : o₁ + succ o₂ = succ (o₁ + o₂) := (add_assoc _ _ _).symm theorem one_le_iff_pos {o : ordinal} : 1 ≤ o ↔ 0 < o := by rw [← succ_zero, succ_le_iff] theorem one_le_iff_ne_zero {o : ordinal} : 1 ≤ o ↔ o ≠ 0 := by rw [one_le_iff_pos, ordinal.pos_iff_ne_zero] theorem succ_pos (o : ordinal) : 0 < succ o := bot_lt_succ o theorem succ_ne_zero (o : ordinal) : succ o ≠ 0 := ne_of_gt $ succ_pos o theorem lt_one_iff_zero {a : ordinal} : a < 1 ↔ a = 0 := by simpa using @lt_succ_bot_iff _ _ _ a _ _ theorem le_one_iff {a : ordinal} : a ≤ 1 ↔ a = 0 ∨ a = 1 := by simpa using @le_succ_bot_iff _ _ _ a _ @[simp] theorem card_succ (o : ordinal) : card (succ o) = card o + 1 := by simp only [←add_one_eq_succ, card_add, card_one] theorem nat_cast_succ (n : ℕ) : ↑n.succ = succ (n : ordinal) := rfl instance unique_Iio_one : unique (Iio (1 : ordinal)) := { default := ⟨0, zero_lt_one⟩, uniq := λ a, subtype.ext $ lt_one_iff_zero.1 a.prop } instance unique_out_one : unique (1 : ordinal).out.α := { default := enum (<) 0 (by simp), uniq := λ a, begin rw ←enum_typein (<) a, unfold default, congr, rw ←lt_one_iff_zero, apply typein_lt_self end } theorem one_out_eq (x : (1 : ordinal).out.α) : x = enum (<) 0 (by simp) := unique.eq_default x /-! ### Extra properties of typein and enum -/ @[simp] theorem typein_one_out (x : (1 : ordinal).out.α) : typein (<) x = 0 := by rw [one_out_eq x, typein_enum] @[simp] lemma typein_le_typein (r : α → α → Prop) [is_well_order α r] {x x' : α} : typein r x ≤ typein r x' ↔ ¬r x' x := by rw [←not_lt, typein_lt_typein] @[simp] lemma typein_le_typein' (o : ordinal) {x x' : o.out.α} : typein (<) x ≤ typein (<) x' ↔ x ≤ x' := by { rw typein_le_typein, exact not_lt } @[simp] lemma enum_le_enum (r : α → α → Prop) [is_well_order α r] {o o' : ordinal} (ho : o < type r) (ho' : o' < type r) : ¬r (enum r o' ho') (enum r o ho) ↔ o ≤ o' := by rw [←@not_lt _ _ o' o, enum_lt_enum ho'] @[simp] lemma enum_le_enum' (a : ordinal) {o o' : ordinal} (ho : o < type (<)) (ho' : o' < type (<)) : enum (<) o ho ≤ @enum a.out.α (<) _ o' ho' ↔ o ≤ o' := by rw [←enum_le_enum (<), ←not_lt] theorem enum_zero_le {r : α → α → Prop} [is_well_order α r] (h0 : 0 < type r) (a : α) : ¬ r a (enum r 0 h0) := by { rw [←enum_typein r a, enum_le_enum r], apply ordinal.zero_le } theorem enum_zero_le' {o : ordinal} (h0 : 0 < o) (a : o.out.α) : @enum o.out.α (<) _ 0 (by rwa type_lt) ≤ a := by { rw ←not_lt, apply enum_zero_le } theorem le_enum_succ {o : ordinal} (a : (succ o).out.α) : a ≤ @enum (succ o).out.α (<) _ o (by { rw type_lt, exact lt_succ o }) := by { rw [←enum_typein (<) a, enum_le_enum', ←lt_succ_iff], apply typein_lt_self } @[simp] theorem enum_inj {r : α → α → Prop} [is_well_order α r] {o₁ o₂ : ordinal} (h₁ : o₁ < type r) (h₂ : o₂ < type r) : enum r o₁ h₁ = enum r o₂ h₂ ↔ o₁ = o₂ := (typein.principal_seg r).subrel_iso.injective.eq_iff.trans subtype.mk_eq_mk /-- A well order `r` is order isomorphic to the set of ordinals smaller than `type r`. -/ @[simps] def enum_iso (r : α → α → Prop) [is_well_order α r] : subrel (<) (< type r) ≃r r := { to_fun := λ x, enum r x.1 x.2, inv_fun := λ x, ⟨typein r x, typein_lt_type r x⟩, ..(typein.principal_seg r).subrel_iso } /-- The order isomorphism between ordinals less than `o` and `o.out.α`. -/ @[simps] noncomputable def enum_iso_out (o : ordinal) : set.Iio o ≃o o.out.α := { to_fun := λ x, enum (<) x.1 $ by { rw type_lt, exact x.2 }, inv_fun := λ x, ⟨typein (<) x, typein_lt_self x⟩, left_inv := λ ⟨o', h⟩, subtype.ext_val (typein_enum _ _), right_inv := λ h, enum_typein _ _, map_rel_iff' := by { rintros ⟨a, _⟩ ⟨b, _⟩, apply enum_le_enum' } } /-- `o.out.α` is an `order_bot` whenever `0 < o`. -/ def out_order_bot_of_pos {o : ordinal} (ho : 0 < o) : order_bot o.out.α := ⟨_, enum_zero_le' ho⟩ theorem enum_zero_eq_bot {o : ordinal} (ho : 0 < o) : enum (<) 0 (by rwa type_lt) = by { haveI H := out_order_bot_of_pos ho, exact ⊥ } := rfl /-! ### Universal ordinal -/ /-- `univ.{u v}` is the order type of the ordinals of `Type u` as a member of `ordinal.{v}` (when `u < v`). It is an inaccessible cardinal. -/ @[nolint check_univs] -- intended to be used with explicit universe parameters def univ : ordinal.{max (u + 1) v} := lift.{v (u+1)} (@type ordinal (<) _) theorem univ_id : univ.{u (u+1)} = @type ordinal (<) _ := lift_id _ @[simp] theorem lift_univ : lift.{w} univ.{u v} = univ.{u (max v w)} := lift_lift _ theorem univ_umax : univ.{u (max (u+1) v)} = univ.{u v} := congr_fun lift_umax _ /-- Principal segment version of the lift operation on ordinals, embedding `ordinal.{u}` in `ordinal.{v}` as a principal segment when `u < v`. -/ def lift.principal_seg : @principal_seg ordinal.{u} ordinal.{max (u+1) v} (<) (<) := ⟨↑lift.initial_seg.{u (max (u+1) v)}, univ.{u v}, begin refine λ b, induction_on b _, introsI β s _, rw [univ, ← lift_umax], split; intro h, { rw ← lift_id (type s) at h ⊢, cases lift_type_lt.1 h with f, cases f with f a hf, existsi a, revert hf, apply induction_on a, introsI α r _ hf, refine lift_type_eq.{u (max (u+1) v) (max (u+1) v)}.2 ⟨(rel_iso.of_surjective (rel_embedding.of_monotone _ _) _).symm⟩, { exact λ b, enum r (f b) ((hf _).2 ⟨_, rfl⟩) }, { refine λ a b h, (typein_lt_typein r).1 _, rw [typein_enum, typein_enum], exact f.map_rel_iff.2 h }, { intro a', cases (hf _).1 (typein_lt_type _ a') with b e, existsi b, simp, simp [e] } }, { cases h with a e, rw [← e], apply induction_on a, introsI α r _, exact lift_type_lt.{u (u+1) (max (u+1) v)}.2 ⟨typein.principal_seg r⟩ } end⟩ @[simp] theorem lift.principal_seg_coe : (lift.principal_seg.{u v} : ordinal → ordinal) = lift.{max (u+1) v} := rfl @[simp] theorem lift.principal_seg_top : lift.principal_seg.top = univ := rfl theorem lift.principal_seg_top' : lift.principal_seg.{u (u+1)}.top = @type ordinal (<) _ := by simp only [lift.principal_seg_top, univ_id] end ordinal /-! ### Representing a cardinal with an ordinal -/ namespace cardinal open ordinal @[simp] theorem mk_ordinal_out (o : ordinal) : #(o.out.α) = o.card := (ordinal.card_type _).symm.trans $ by rw ordinal.type_lt /-- The ordinal corresponding to a cardinal `c` is the least ordinal whose cardinal is `c`. For the order-embedding version, see `ord.order_embedding`. -/ def ord (c : cardinal) : ordinal := let F := λ α : Type u, ⨅ r : {r // is_well_order α r}, @type α r.1 r.2 in quot.lift_on c F begin suffices : ∀ {α β}, α ≈ β → F α ≤ F β, from λ α β h, (this h).antisymm (this (setoid.symm h)), rintros α β ⟨f⟩, refine le_cinfi_iff'.2 (λ i, _), haveI := @rel_embedding.is_well_order _ _ (f ⁻¹'o i.1) _ ↑(rel_iso.preimage f i.1) i.2, exact (cinfi_le' _ (subtype.mk (⇑f ⁻¹'o i.val) (@rel_embedding.is_well_order _ _ _ _ ↑(rel_iso.preimage f i.1) i.2))).trans_eq (quot.sound ⟨rel_iso.preimage f i.1⟩) end lemma ord_eq_Inf (α : Type u) : ord (#α) = ⨅ r : {r // is_well_order α r}, @type α r.1 r.2 := rfl theorem ord_eq (α) : ∃ (r : α → α → Prop) [wo : is_well_order α r], ord (#α) = @type α r wo := let ⟨r, wo⟩ := infi_mem (λ r : {r // is_well_order α r}, @type α r.1 r.2) in ⟨r.1, r.2, wo.symm⟩ theorem ord_le_type (r : α → α → Prop) [h : is_well_order α r] : ord (#α) ≤ type r := cinfi_le' _ (subtype.mk r h) theorem ord_le {c o} : ord c ≤ o ↔ c ≤ o.card := induction_on c $ λ α, ordinal.induction_on o $ λ β s _, let ⟨r, _, e⟩ := ord_eq α in begin resetI, simp only [card_type], split; intro h, { rw e at h, exact let ⟨f⟩ := h in ⟨f.to_embedding⟩ }, { cases h with f, have g := rel_embedding.preimage f s, haveI := rel_embedding.is_well_order g, exact le_trans (ord_le_type _) g.ordinal_type_le } end theorem gc_ord_card : galois_connection ord card := λ _ _, ord_le theorem lt_ord {c o} : o < ord c ↔ o.card < c := gc_ord_card.lt_iff_lt @[simp] theorem card_ord (c) : (ord c).card = c := quotient.induction_on c $ λ α, let ⟨r, _, e⟩ := ord_eq α in by simp only [mk_def, e, card_type] /-- Galois coinsertion between `cardinal.ord` and `ordinal.card`. -/ def gci_ord_card : galois_coinsertion ord card := gc_ord_card.to_galois_coinsertion $ λ c, c.card_ord.le theorem ord_card_le (o : ordinal) : o.card.ord ≤ o := gc_ord_card.l_u_le _ lemma lt_ord_succ_card (o : ordinal) : o < (succ o.card).ord := lt_ord.2 $ lt_succ _ @[mono] theorem ord_strict_mono : strict_mono ord := gci_ord_card.strict_mono_l @[mono] theorem ord_mono : monotone ord := gc_ord_card.monotone_l @[simp] theorem ord_le_ord {c₁ c₂} : ord c₁ ≤ ord c₂ ↔ c₁ ≤ c₂ := gci_ord_card.l_le_l_iff @[simp] theorem ord_lt_ord {c₁ c₂} : ord c₁ < ord c₂ ↔ c₁ < c₂ := ord_strict_mono.lt_iff_lt @[simp] theorem ord_zero : ord 0 = 0 := gc_ord_card.l_bot @[simp] theorem ord_nat (n : ℕ) : ord n = n := (ord_le.2 (card_nat n).ge).antisymm begin induction n with n IH, { apply ordinal.zero_le }, { exact succ_le_of_lt (IH.trans_lt $ ord_lt_ord.2 $ nat_cast_lt.2 (nat.lt_succ_self n)) } end @[simp] theorem ord_one : ord 1 = 1 := by simpa using ord_nat 1 @[simp] theorem lift_ord (c) : (ord c).lift = ord (lift c) := begin refine le_antisymm (le_of_forall_lt (λ a ha, _)) _, { rcases ordinal.lt_lift_iff.1 ha with ⟨a, rfl, h⟩, rwa [lt_ord, ← lift_card, lift_lt, ← lt_ord, ← ordinal.lift_lt] }, { rw [ord_le, ← lift_card, card_ord] } end lemma mk_ord_out (c : cardinal) : #c.ord.out.α = c := by simp lemma card_typein_lt (r : α → α → Prop) [is_well_order α r] (x : α) (h : ord (#α) = type r) : card (typein r x) < #α := by { rw [←lt_ord, h], apply typein_lt_type } lemma card_typein_out_lt (c : cardinal) (x : c.ord.out.α) : card (typein (<) x) < c := by { rw ←lt_ord, apply typein_lt_self } lemma ord_injective : injective ord := by { intros c c' h, rw [←card_ord c, ←card_ord c', h] } /-- The ordinal corresponding to a cardinal `c` is the least ordinal whose cardinal is `c`. This is the order-embedding version. For the regular function, see `ord`. -/ def ord.order_embedding : cardinal ↪o ordinal := rel_embedding.order_embedding_of_lt_embedding (rel_embedding.of_monotone cardinal.ord $ λ a b, cardinal.ord_lt_ord.2) @[simp] theorem ord.order_embedding_coe : (ord.order_embedding : cardinal → ordinal) = ord := rfl /-- The cardinal `univ` is the cardinality of ordinal `univ`, or equivalently the cardinal of `ordinal.{u}`, or `cardinal.{u}`, as an element of `cardinal.{v}` (when `u < v`). -/ @[nolint check_univs] -- intended to be used with explicit universe parameters def univ := lift.{v (u+1)} (#ordinal) theorem univ_id : univ.{u (u+1)} = #ordinal := lift_id _ @[simp] theorem lift_univ : lift.{w} univ.{u v} = univ.{u (max v w)} := lift_lift _ theorem univ_umax : univ.{u (max (u+1) v)} = univ.{u v} := congr_fun lift_umax _ theorem lift_lt_univ (c : cardinal) : lift.{(u+1) u} c < univ.{u (u+1)} := by simpa only [lift.principal_seg_coe, lift_ord, lift_succ, ord_le, succ_le_iff] using le_of_lt (lift.principal_seg.{u (u+1)}.lt_top (succ c).ord) theorem lift_lt_univ' (c : cardinal) : lift.{(max (u+1) v) u} c < univ.{u v} := by simpa only [lift_lift, lift_univ, univ_umax] using lift_lt.{_ (max (u+1) v)}.2 (lift_lt_univ c) @[simp] theorem ord_univ : ord univ.{u v} = ordinal.univ.{u v} := le_antisymm (ord_card_le _) $ le_of_forall_lt $ λ o h, lt_ord.2 begin rcases lift.principal_seg.{u v}.down.1 (by simpa only [lift.principal_seg_coe] using h) with ⟨o', rfl⟩, simp only [lift.principal_seg_coe], rw [← lift_card], apply lift_lt_univ' end theorem lt_univ {c} : c < univ.{u (u+1)} ↔ ∃ c', c = lift.{(u+1) u} c' := ⟨λ h, begin have := ord_lt_ord.2 h, rw ord_univ at this, cases lift.principal_seg.{u (u+1)}.down.1 (by simpa only [lift.principal_seg_top]) with o e, have := card_ord c, rw [← e, lift.principal_seg_coe, ← lift_card] at this, exact ⟨_, this.symm⟩ end, λ ⟨c', e⟩, e.symm ▸ lift_lt_univ _⟩ theorem lt_univ' {c} : c < univ.{u v} ↔ ∃ c', c = lift.{(max (u+1) v) u} c' := ⟨λ h, let ⟨a, e, h'⟩ := lt_lift_iff.1 h in begin rw [← univ_id] at h', rcases lt_univ.{u}.1 h' with ⟨c', rfl⟩, exact ⟨c', by simp only [e.symm, lift_lift]⟩ end, λ ⟨c', e⟩, e.symm ▸ lift_lt_univ' _⟩ theorem small_iff_lift_mk_lt_univ {α : Type u} : small.{v} α ↔ cardinal.lift (#α) < univ.{v (max u (v + 1))} := begin rw lt_univ', split, { rintro ⟨β, e⟩, exact ⟨#β, lift_mk_eq.{u _ (v + 1)}.2 e⟩ }, { rintro ⟨c, hc⟩, exact ⟨⟨c.out, lift_mk_eq.{u _ (v + 1)}.1 (hc.trans (congr rfl c.mk_out.symm))⟩⟩ } end end cardinal namespace ordinal @[simp] theorem card_univ : card univ = cardinal.univ := rfl @[simp] theorem nat_le_card {o} {n : ℕ} : (n : cardinal) ≤ card o ↔ (n : ordinal) ≤ o := by rw [← cardinal.ord_le, cardinal.ord_nat] @[simp] theorem nat_lt_card {o} {n : ℕ} : (n : cardinal) < card o ↔ (n : ordinal) < o := by { rw [←succ_le_iff, ←succ_le_iff, ←nat_succ, nat_le_card], refl } @[simp] theorem card_lt_nat {o} {n : ℕ} : card o < n ↔ o < n := lt_iff_lt_of_le_iff_le nat_le_card @[simp] theorem card_le_nat {o} {n : ℕ} : card o ≤ n ↔ o ≤ n := le_iff_le_iff_lt_iff_lt.2 nat_lt_card @[simp] theorem card_eq_nat {o} {n : ℕ} : card o = n ↔ o = n := by simp only [le_antisymm_iff, card_le_nat, nat_le_card] @[simp] theorem type_fintype (r : α → α → Prop) [is_well_order α r] [fintype α] : type r = fintype.card α := by rw [←card_eq_nat, card_type, mk_fintype] theorem type_fin (n : ℕ) : @type (fin n) (<) _ = n := by simp end ordinal
7d9168c355258a1407cb4683f1ed6773c25513a6
4727251e0cd73359b15b664c3170e5d754078599
/src/ring_theory/subring/basic.lean
57554df96a9d1f2a7d09270dde0f28b75e5e6d11
[ "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
46,820
lean
/- Copyright (c) 2020 Ashvni Narayanan. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Ashvni Narayanan -/ import group_theory.subgroup.basic import ring_theory.subsemiring.basic /-! # Subrings Let `R` be a ring. This file defines the "bundled" subring type `subring R`, a type whose terms correspond to subrings of `R`. This is the preferred way to talk about subrings in mathlib. Unbundled subrings (`s : set R` and `is_subring s`) are not in this file, and they will ultimately be deprecated. We prove that subrings are a complete lattice, and that you can `map` (pushforward) and `comap` (pull back) them along ring homomorphisms. We define the `closure` construction from `set R` to `subring R`, sending a subset of `R` to the subring it generates, and prove that it is a Galois insertion. ## Main definitions Notation used here: `(R : Type u) [ring R] (S : Type u) [ring S] (f g : R →+* S)` `(A : subring R) (B : subring S) (s : set R)` * `subring R` : the type of subrings of a ring `R`. * `instance : complete_lattice (subring R)` : the complete lattice structure on the subrings. * `subring.center` : the center of a ring `R`. * `subring.closure` : subring closure of a set, i.e., the smallest subring that includes the set. * `subring.gi` : `closure : set M → subring M` and coercion `coe : subring M → set M` form a `galois_insertion`. * `comap f B : subring A` : the preimage of a subring `B` along the ring homomorphism `f` * `map f A : subring B` : the image of a subring `A` along the ring homomorphism `f`. * `prod A B : subring (R × S)` : the product of subrings * `f.range : subring B` : the range of the ring homomorphism `f`. * `eq_locus f g : subring R` : given ring homomorphisms `f g : R →+* S`, the subring of `R` where `f x = g x` ## Implementation notes A subring is implemented as a subsemiring which is also an additive subgroup. The initial PR was as a submonoid which is also an additive subgroup. Lattice inclusion (e.g. `≤` and `⊓`) is used rather than set notation (`⊆` and `∩`), although `∈` is defined as membership of a subring's underlying set. ## Tags subring, subrings -/ open_locale big_operators universes u v w variables {R : Type u} {S : Type v} {T : Type w} [ring R] section subring_class /-- `subring_class S R` states that `S` is a type of subsets `s ⊆ R` that are both a multiplicative submonoid and an additive subgroup. -/ class subring_class (S : Type*) (R : out_param $ Type u) [ring R] [set_like S R] extends subsemiring_class S R := (neg_mem : ∀ {s : S} {a : R}, a ∈ s → -a ∈ s) @[priority 100] -- See note [lower instance priority] instance subring_class.add_subgroup_class (S : Type*) (R : out_param $ Type u) [set_like S R] [ring R] [h : subring_class S R] : add_subgroup_class S R := { .. h } variables [set_like S R] [hSR : subring_class S R] (s : S) include hSR lemma coe_int_mem (n : ℤ) : (n : R) ∈ s := by simp only [← zsmul_one, zsmul_mem, one_mem] namespace subring_class /-- A subring of a ring inherits a ring structure -/ @[priority 75] -- Prefer subclasses of `ring` over subclasses of `subring_class`. instance to_ring : ring s := { right_distrib := λ x y z, subtype.eq $ right_distrib x y z, left_distrib := λ x y z, subtype.eq $ left_distrib x y z, .. submonoid_class.to_monoid s, .. add_subgroup_class.to_add_comm_group s } omit hSR /-- A subring of a `comm_ring` is a `comm_ring`. -/ @[priority 75] -- Prefer subclasses of `ring` over subclasses of `subring_class`. instance to_comm_ring {R} [comm_ring R] [set_like S R] [subring_class S R] : comm_ring s := subtype.coe_injective.comm_ring coe rfl rfl (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) /-- A subring of a domain is a domain. -/ @[priority 75] -- Prefer subclasses of `ring` over subclasses of `subring_class`. instance {R} [ring R] [is_domain R] [set_like S R] [subring_class S R] : is_domain s := { .. subsemiring_class.nontrivial s, .. subsemiring_class.no_zero_divisors s } /-- A subring of an `ordered_ring` is an `ordered_ring`. -/ @[priority 75] -- Prefer subclasses of `ring` over subclasses of `subring_class`. instance to_ordered_ring {R} [ordered_ring R] [set_like S R] [subring_class S R] : ordered_ring s := subtype.coe_injective.ordered_ring coe rfl rfl (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) /-- A subring of an `ordered_comm_ring` is an `ordered_comm_ring`. -/ @[priority 75] -- Prefer subclasses of `ring` over subclasses of `subring_class`. instance to_ordered_comm_ring {R} [ordered_comm_ring R] [set_like S R] [subring_class S R] : ordered_comm_ring s := subtype.coe_injective.ordered_comm_ring coe rfl rfl (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) /-- A subring of a `linear_ordered_ring` is a `linear_ordered_ring`. -/ @[priority 75] -- Prefer subclasses of `ring` over subclasses of `subring_class`. instance to_linear_ordered_ring {R} [linear_ordered_ring R] [set_like S R] [subring_class S R] : linear_ordered_ring s := subtype.coe_injective.linear_ordered_ring coe rfl rfl (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) /-- A subring of a `linear_ordered_comm_ring` is a `linear_ordered_comm_ring`. -/ @[priority 75] -- Prefer subclasses of `ring` over subclasses of `subring_class`. instance to_linear_ordered_comm_ring {R} [linear_ordered_comm_ring R] [set_like S R] [subring_class S R] : linear_ordered_comm_ring s := subtype.coe_injective.linear_ordered_comm_ring coe rfl rfl (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) include hSR /-- The natural ring hom from a subring of ring `R` to `R`. -/ def subtype (s : S) : s →+* R := { to_fun := coe, .. submonoid_class.subtype s, .. add_subgroup_class.subtype s } @[simp] theorem coe_subtype : (subtype s : s → R) = coe := rfl @[simp, norm_cast] lemma coe_nat_cast (n : ℕ) : ((n : s) : R) = n := map_nat_cast (subtype s) n @[simp, norm_cast] lemma coe_int_cast (n : ℤ) : ((n : s) : R) = n := (subtype s : s →+* R).map_int_cast n end subring_class end subring_class variables [ring S] [ring T] set_option old_structure_cmd true /-- `subring R` is the type of subrings of `R`. A subring of `R` is a subset `s` that is a multiplicative submonoid and an additive subgroup. Note in particular that it shares the same 0 and 1 as R. -/ structure subring (R : Type u) [ring R] extends subsemiring R, add_subgroup R /-- Reinterpret a `subring` as a `subsemiring`. -/ add_decl_doc subring.to_subsemiring /-- Reinterpret a `subring` as an `add_subgroup`. -/ add_decl_doc subring.to_add_subgroup namespace subring /-- The underlying submonoid of a subring. -/ def to_submonoid (s : subring R) : submonoid R := { carrier := s.carrier, ..s.to_subsemiring.to_submonoid } instance : set_like (subring R) R := { coe := subring.carrier, coe_injective' := λ p q h, by cases p; cases q; congr' } instance : subring_class (subring R) R := { zero_mem := zero_mem', add_mem := add_mem', one_mem := one_mem', mul_mem := mul_mem', neg_mem := neg_mem' } @[simp] lemma mem_carrier {s : subring R} {x : R} : x ∈ s.carrier ↔ x ∈ s := iff.rfl @[simp] lemma mem_mk {S : set R} {x : R} (h₁ h₂ h₃ h₄ h₅) : x ∈ (⟨S, h₁, h₂, h₃, h₄, h₅⟩ : subring R) ↔ x ∈ S := iff.rfl @[simp] lemma coe_set_mk (S : set R) (h₁ h₂ h₃ h₄ h₅) : ((⟨S, h₁, h₂, h₃, h₄, h₅⟩ : subring R) : set R) = S := rfl @[simp] lemma mk_le_mk {S S' : set R} (h₁ h₂ h₃ h₄ h₅ h₁' h₂' h₃' h₄' h₅') : (⟨S, h₁, h₂, h₃, h₄, h₅⟩ : subring R) ≤ (⟨S', h₁', h₂', h₃', h₄', h₅'⟩ : subring R) ↔ S ⊆ S' := iff.rfl /-- Two subrings are equal if they have the same elements. -/ @[ext] theorem ext {S T : subring R} (h : ∀ x, x ∈ S ↔ x ∈ T) : S = T := set_like.ext h /-- Copy of a subring with a new `carrier` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (S : subring R) (s : set R) (hs : s = ↑S) : subring R := { carrier := s, neg_mem' := hs.symm ▸ S.neg_mem', ..S.to_subsemiring.copy s hs } @[simp] lemma coe_copy (S : subring R) (s : set R) (hs : s = ↑S) : (S.copy s hs : set R) = s := rfl lemma copy_eq (S : subring R) (s : set R) (hs : s = ↑S) : S.copy s hs = S := set_like.coe_injective hs lemma to_subsemiring_injective : function.injective (to_subsemiring : subring R → subsemiring R) | r s h := ext (set_like.ext_iff.mp h : _) @[mono] lemma to_subsemiring_strict_mono : strict_mono (to_subsemiring : subring R → subsemiring R) := λ _ _, id @[mono] lemma to_subsemiring_mono : monotone (to_subsemiring : subring R → subsemiring R) := to_subsemiring_strict_mono.monotone lemma to_add_subgroup_injective : function.injective (to_add_subgroup : subring R → add_subgroup R) | r s h := ext (set_like.ext_iff.mp h : _) @[mono] lemma to_add_subgroup_strict_mono : strict_mono (to_add_subgroup : subring R → add_subgroup R) := λ _ _, id @[mono] lemma to_add_subgroup_mono : monotone (to_add_subgroup : subring R → add_subgroup R) := to_add_subgroup_strict_mono.monotone lemma to_submonoid_injective : function.injective (to_submonoid : subring R → submonoid R) | r s h := ext (set_like.ext_iff.mp h : _) @[mono] lemma to_submonoid_strict_mono : strict_mono (to_submonoid : subring R → submonoid R) := λ _ _, id @[mono] lemma to_submonoid_mono : monotone (to_submonoid : subring R → submonoid R) := to_submonoid_strict_mono.monotone /-- Construct a `subring R` from a set `s`, a submonoid `sm`, and an additive subgroup `sa` such that `x ∈ s ↔ x ∈ sm ↔ x ∈ sa`. -/ protected def mk' (s : set R) (sm : submonoid R) (sa : add_subgroup R) (hm : ↑sm = s) (ha : ↑sa = s) : subring R := { carrier := s, zero_mem' := ha ▸ sa.zero_mem, one_mem' := hm ▸ sm.one_mem, add_mem' := λ x y, by simpa only [← ha] using sa.add_mem, mul_mem' := λ x y, by simpa only [← hm] using sm.mul_mem, neg_mem' := λ x, by simpa only [← ha] using sa.neg_mem, } @[simp] lemma coe_mk' {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_subgroup R} (ha : ↑sa = s) : (subring.mk' s sm sa hm ha : set R) = s := rfl @[simp] lemma mem_mk' {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_subgroup R} (ha : ↑sa = s) {x : R} : x ∈ subring.mk' s sm sa hm ha ↔ x ∈ s := iff.rfl @[simp] lemma mk'_to_submonoid {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_subgroup R} (ha : ↑sa = s) : (subring.mk' s sm sa hm ha).to_submonoid = sm := set_like.coe_injective hm.symm @[simp] lemma mk'_to_add_subgroup {s : set R} {sm : submonoid R} (hm : ↑sm = s) {sa : add_subgroup R} (ha : ↑sa =s) : (subring.mk' s sm sa hm ha).to_add_subgroup = sa := set_like.coe_injective ha.symm end subring /-- A `subsemiring` containing -1 is a `subring`. -/ def subsemiring.to_subring (s : subsemiring R) (hneg : (-1 : R) ∈ s) : subring R := { neg_mem' := by { rintros x, rw <-neg_one_mul, apply subsemiring.mul_mem, exact hneg, } ..s.to_submonoid, ..s.to_add_submonoid } namespace subring variables (s : subring R) /-- A subring contains the ring's 1. -/ protected theorem one_mem : (1 : R) ∈ s := one_mem _ /-- A subring contains the ring's 0. -/ protected theorem zero_mem : (0 : R) ∈ s := zero_mem _ /-- A subring is closed under multiplication. -/ protected theorem mul_mem {x y : R} : x ∈ s → y ∈ s → x * y ∈ s := mul_mem /-- A subring is closed under addition. -/ protected theorem add_mem {x y : R} : x ∈ s → y ∈ s → x + y ∈ s := add_mem /-- A subring is closed under negation. -/ protected theorem neg_mem {x : R} : x ∈ s → -x ∈ s := neg_mem /-- A subring is closed under subtraction -/ protected theorem sub_mem {x y : R} (hx : x ∈ s) (hy : y ∈ s) : x - y ∈ s := sub_mem hx hy /-- Product of a list of elements in a subring is in the subring. -/ protected lemma list_prod_mem {l : list R} : (∀x ∈ l, x ∈ s) → l.prod ∈ s := list_prod_mem /-- Sum of a list of elements in a subring is in the subring. -/ protected lemma list_sum_mem {l : list R} : (∀x ∈ l, x ∈ s) → l.sum ∈ s := list_sum_mem /-- Product of a multiset of elements in a subring of a `comm_ring` is in the subring. -/ protected lemma multiset_prod_mem {R} [comm_ring R] (s : subring R) (m : multiset R) : (∀a ∈ m, a ∈ s) → m.prod ∈ s := multiset_prod_mem _ /-- Sum of a multiset of elements in an `subring` of a `ring` is in the `subring`. -/ protected lemma multiset_sum_mem {R} [ring R] (s : subring R) (m : multiset R) : (∀a ∈ m, a ∈ s) → m.sum ∈ s := multiset_sum_mem _ /-- Product of elements of a subring of a `comm_ring` indexed by a `finset` is in the subring. -/ protected lemma prod_mem {R : Type*} [comm_ring R] (s : subring R) {ι : Type*} {t : finset ι} {f : ι → R} (h : ∀c ∈ t, f c ∈ s) : ∏ i in t, f i ∈ s := prod_mem h /-- Sum of elements in a `subring` of a `ring` indexed by a `finset` is in the `subring`. -/ protected lemma sum_mem {R : Type*} [ring R] (s : subring R) {ι : Type*} {t : finset ι} {f : ι → R} (h : ∀c ∈ t, f c ∈ s) : ∑ i in t, f i ∈ s := sum_mem h /-- A subring of a ring inherits a ring structure -/ instance to_ring : ring s := { right_distrib := λ x y z, subtype.eq $ right_distrib x y z, left_distrib := λ x y z, subtype.eq $ left_distrib x y z, .. s.to_submonoid.to_monoid, .. s.to_add_subgroup.to_add_comm_group } protected lemma zsmul_mem {x : R} (hx : x ∈ s) (n : ℤ) : n • x ∈ s := zsmul_mem hx n protected lemma pow_mem {x : R} (hx : x ∈ s) (n : ℕ) : x^n ∈ s := pow_mem hx n @[simp, norm_cast] lemma coe_add (x y : s) : (↑(x + y) : R) = ↑x + ↑y := rfl @[simp, norm_cast] lemma coe_neg (x : s) : (↑(-x) : R) = -↑x := rfl @[simp, norm_cast] lemma coe_mul (x y : s) : (↑(x * y) : R) = ↑x * ↑y := rfl @[simp, norm_cast] lemma coe_zero : ((0 : s) : R) = 0 := rfl @[simp, norm_cast] lemma coe_one : ((1 : s) : R) = 1 := rfl @[simp, norm_cast] lemma coe_pow (x : s) (n : ℕ) : (↑(x ^ n) : R) = x ^ n := submonoid_class.coe_pow x n -- TODO: can be generalized to `add_submonoid_class` @[simp] lemma coe_eq_zero_iff {x : s} : (x : R) = 0 ↔ x = 0 := ⟨λ h, subtype.ext (trans h s.coe_zero.symm), λ h, h.symm ▸ s.coe_zero⟩ /-- A subring of a `comm_ring` is a `comm_ring`. -/ instance to_comm_ring {R} [comm_ring R] (s : subring R) : comm_ring s := { mul_comm := λ _ _, subtype.eq $ mul_comm _ _, ..subring.to_ring s} /-- A subring of a non-trivial ring is non-trivial. -/ instance {R} [ring R] [nontrivial R] (s : subring R) : nontrivial s := s.to_subsemiring.nontrivial /-- A subring of a ring with no zero divisors has no zero divisors. -/ instance {R} [ring R] [no_zero_divisors R] (s : subring R) : no_zero_divisors s := s.to_subsemiring.no_zero_divisors /-- A subring of a domain is a domain. -/ instance {R} [ring R] [is_domain R] (s : subring R) : is_domain s := { .. s.nontrivial, .. s.no_zero_divisors, .. s.to_ring } /-- A subring of an `ordered_ring` is an `ordered_ring`. -/ instance to_ordered_ring {R} [ordered_ring R] (s : subring R) : ordered_ring s := subtype.coe_injective.ordered_ring coe rfl rfl (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) /-- A subring of an `ordered_comm_ring` is an `ordered_comm_ring`. -/ instance to_ordered_comm_ring {R} [ordered_comm_ring R] (s : subring R) : ordered_comm_ring s := subtype.coe_injective.ordered_comm_ring coe rfl rfl (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) /-- A subring of a `linear_ordered_ring` is a `linear_ordered_ring`. -/ instance to_linear_ordered_ring {R} [linear_ordered_ring R] (s : subring R) : linear_ordered_ring s := subtype.coe_injective.linear_ordered_ring coe rfl rfl (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) /-- A subring of a `linear_ordered_comm_ring` is a `linear_ordered_comm_ring`. -/ instance to_linear_ordered_comm_ring {R} [linear_ordered_comm_ring R] (s : subring R) : linear_ordered_comm_ring s := subtype.coe_injective.linear_ordered_comm_ring coe rfl rfl (λ _ _, rfl) (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) /-- The natural ring hom from a subring of ring `R` to `R`. -/ def subtype (s : subring R) : s →+* R := { to_fun := coe, .. s.to_submonoid.subtype, .. s.to_add_subgroup.subtype } @[simp] theorem coe_subtype : ⇑s.subtype = coe := rfl @[simp, norm_cast] lemma coe_nat_cast : ∀ (n : ℕ), ((n : s) : R) = n := map_nat_cast s.subtype @[simp, norm_cast] lemma coe_int_cast (n : ℤ) : ((n : s) : R) = n := s.subtype.map_int_cast n /-! ## Partial order -/ @[simp] lemma mem_to_submonoid {s : subring R} {x : R} : x ∈ s.to_submonoid ↔ x ∈ s := iff.rfl @[simp] lemma coe_to_submonoid (s : subring R) : (s.to_submonoid : set R) = s := rfl @[simp] lemma mem_to_add_subgroup {s : subring R} {x : R} : x ∈ s.to_add_subgroup ↔ x ∈ s := iff.rfl @[simp] lemma coe_to_add_subgroup (s : subring R) : (s.to_add_subgroup : set R) = s := rfl /-! ## top -/ /-- The subring `R` of the ring `R`. -/ instance : has_top (subring R) := ⟨{ .. (⊤ : submonoid R), .. (⊤ : add_subgroup R) }⟩ @[simp] lemma mem_top (x : R) : x ∈ (⊤ : subring R) := set.mem_univ x @[simp] lemma coe_top : ((⊤ : subring R) : set R) = set.univ := rfl /-! ## comap -/ /-- The preimage of a subring along a ring homomorphism is a subring. -/ def comap {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : subring S) : subring R := { carrier := f ⁻¹' s.carrier, .. s.to_submonoid.comap (f : R →* S), .. s.to_add_subgroup.comap (f : R →+ S) } @[simp] lemma coe_comap (s : subring S) (f : R →+* S) : (s.comap f : set R) = f ⁻¹' s := rfl @[simp] lemma mem_comap {s : subring S} {f : R →+* S} {x : R} : x ∈ s.comap f ↔ f x ∈ s := iff.rfl lemma comap_comap (s : subring T) (g : S →+* T) (f : R →+* S) : (s.comap g).comap f = s.comap (g.comp f) := rfl /-! ## map -/ /-- The image of a subring along a ring homomorphism is a subring. -/ def map {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : subring R) : subring S := { carrier := f '' s.carrier, .. s.to_submonoid.map (f : R →* S), .. s.to_add_subgroup.map (f : R →+ S) } @[simp] lemma coe_map (f : R →+* S) (s : subring R) : (s.map f : set S) = f '' s := rfl @[simp] lemma mem_map {f : R →+* S} {s : subring R} {y : S} : y ∈ s.map f ↔ ∃ x ∈ s, f x = y := set.mem_image_iff_bex @[simp] lemma map_id : s.map (ring_hom.id R) = s := set_like.coe_injective $ set.image_id _ lemma map_map (g : S →+* T) (f : R →+* S) : (s.map f).map g = s.map (g.comp f) := set_like.coe_injective $ set.image_image _ _ _ lemma map_le_iff_le_comap {f : R →+* S} {s : subring R} {t : subring S} : s.map f ≤ t ↔ s ≤ t.comap f := set.image_subset_iff lemma gc_map_comap (f : R →+* S) : galois_connection (map f) (comap f) := λ S T, map_le_iff_le_comap /-- A subring is isomorphic to its image under an injective function -/ noncomputable def equiv_map_of_injective (f : R →+* S) (hf : function.injective f) : s ≃+* s.map f := { map_mul' := λ _ _, subtype.ext (f.map_mul _ _), map_add' := λ _ _, subtype.ext (f.map_add _ _), ..equiv.set.image f s hf } @[simp] lemma coe_equiv_map_of_injective_apply (f : R →+* S) (hf : function.injective f) (x : s) : (equiv_map_of_injective s f hf x : S) = f x := rfl end subring namespace ring_hom variables (g : S →+* T) (f : R →+* S) /-! ## range -/ /-- The range of a ring homomorphism, as a subring of the target. See Note [range copy pattern]. -/ def range {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) : subring S := ((⊤ : subring R).map f).copy (set.range f) set.image_univ.symm @[simp] lemma coe_range : (f.range : set S) = set.range f := rfl @[simp] lemma mem_range {f : R →+* S} {y : S} : y ∈ f.range ↔ ∃ x, f x = y := iff.rfl lemma range_eq_map (f : R →+* S) : f.range = subring.map f ⊤ := by { ext, simp } lemma mem_range_self (f : R →+* S) (x : R) : f x ∈ f.range := mem_range.mpr ⟨x, rfl⟩ lemma map_range : f.range.map g = (g.comp f).range := by simpa only [range_eq_map] using (⊤ : subring R).map_map g f -- TODO -- rename to `cod_restrict` when is_ring_hom is deprecated /-- Restrict the codomain of a ring homomorphism to a subring that includes the range. -/ def cod_restrict' {R : Type u} {S : Type v} [ring R] [ring S] (f : R →+* S) (s : subring S) (h : ∀ x, f x ∈ s) : R →+* s := { to_fun := λ x, ⟨f x, h x⟩, map_add' := λ x y, subtype.eq $ f.map_add x y, map_zero' := subtype.eq f.map_zero, map_mul' := λ x y, subtype.eq $ f.map_mul x y, map_one' := subtype.eq f.map_one } /-- The range of a ring homomorphism is a fintype, if the domain is a fintype. Note: this instance can form a diamond with `subtype.fintype` in the presence of `fintype S`. -/ instance fintype_range [fintype R] [decidable_eq S] (f : R →+* S) : fintype (range f) := set.fintype_range f end ring_hom namespace subring /-! ## bot -/ instance : has_bot (subring R) := ⟨(int.cast_ring_hom R).range⟩ instance : inhabited (subring R) := ⟨⊥⟩ lemma coe_bot : ((⊥ : subring R) : set R) = set.range (coe : ℤ → R) := ring_hom.coe_range (int.cast_ring_hom R) lemma mem_bot {x : R} : x ∈ (⊥ : subring R) ↔ ∃ (n : ℤ), ↑n = x := ring_hom.mem_range /-! ## inf -/ /-- The inf of two subrings is their intersection. -/ instance : has_inf (subring R) := ⟨λ s t, { carrier := s ∩ t, .. s.to_submonoid ⊓ t.to_submonoid, .. s.to_add_subgroup ⊓ t.to_add_subgroup }⟩ @[simp] lemma coe_inf (p p' : subring R) : ((p ⊓ p' : subring R) : set R) = p ∩ p' := rfl @[simp] lemma mem_inf {p p' : subring R} {x : R} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl instance : has_Inf (subring R) := ⟨λ s, subring.mk' (⋂ t ∈ s, ↑t) (⨅ t ∈ s, subring.to_submonoid t ) (⨅ t ∈ s, subring.to_add_subgroup t) (by simp) (by simp)⟩ @[simp, norm_cast] lemma coe_Inf (S : set (subring R)) : ((Inf S : subring R) : set R) = ⋂ s ∈ S, ↑s := rfl lemma mem_Inf {S : set (subring R)} {x : R} : x ∈ Inf S ↔ ∀ p ∈ S, x ∈ p := set.mem_Inter₂ @[simp] lemma Inf_to_submonoid (s : set (subring R)) : (Inf s).to_submonoid = ⨅ t ∈ s, subring.to_submonoid t := mk'_to_submonoid _ _ @[simp] lemma Inf_to_add_subgroup (s : set (subring R)) : (Inf s).to_add_subgroup = ⨅ t ∈ s, subring.to_add_subgroup t := mk'_to_add_subgroup _ _ /-- Subrings of a ring form a complete lattice. -/ instance : complete_lattice (subring R) := { bot := (⊥), bot_le := λ s x hx, let ⟨n, hn⟩ := mem_bot.1 hx in hn ▸ coe_int_mem s n, top := (⊤), le_top := λ s x hx, trivial, inf := (⊓), inf_le_left := λ s t x, and.left, inf_le_right := λ s t x, and.right, le_inf := λ s t₁ t₂ h₁ h₂ x hx, ⟨h₁ hx, h₂ hx⟩, .. complete_lattice_of_Inf (subring R) (λ s, is_glb.of_image (λ s t, show (s : set R) ≤ t ↔ s ≤ t, from set_like.coe_subset_coe) is_glb_binfi)} lemma eq_top_iff' (A : subring R) : A = ⊤ ↔ ∀ x : R, x ∈ A := eq_top_iff.trans ⟨λ h m, h $ mem_top m, λ h m _, h m⟩ /-! ## Center of a ring -/ section variables (R) /-- The center of a ring `R` is the set of elements that commute with everything in `R` -/ def center : subring R := { carrier := set.center R, neg_mem' := λ a, set.neg_mem_center, .. subsemiring.center R } lemma coe_center : ↑(center R) = set.center R := rfl @[simp] lemma center_to_subsemiring : (center R).to_subsemiring = subsemiring.center R := rfl variables {R} lemma mem_center_iff {z : R} : z ∈ center R ↔ ∀ g, g * z = z * g := iff.rfl instance decidable_mem_center [decidable_eq R] [fintype R] : decidable_pred (∈ center R) := λ _, decidable_of_iff' _ mem_center_iff @[simp] lemma center_eq_top (R) [comm_ring R] : center R = ⊤ := set_like.coe_injective (set.center_eq_univ R) /-- The center is commutative. -/ instance : comm_ring (center R) := { ..subsemiring.center.comm_semiring, ..(center R).to_ring} end section division_ring variables {K : Type u} [division_ring K] instance : field (center K) := { inv := λ a, ⟨a⁻¹, set.inv_mem_center₀ a.prop⟩, mul_inv_cancel := λ ⟨a, ha⟩ h, subtype.ext $ mul_inv_cancel $ subtype.coe_injective.ne h, div := λ a b, ⟨a / b, set.div_mem_center₀ a.prop b.prop⟩, div_eq_mul_inv := λ a b, subtype.ext $ div_eq_mul_inv _ _, inv_zero := subtype.ext inv_zero, ..(center K).nontrivial, ..center.comm_ring } @[simp] lemma center.coe_inv (a : center K) : ((a⁻¹ : center K) : K) = (a : K)⁻¹ := rfl @[simp] lemma center.coe_div (a b : center K) : ((a / b : center K) : K) = (a : K) / (b : K) := rfl end division_ring /-! ## subring closure of a subset -/ /-- The `subring` generated by a set. -/ def closure (s : set R) : subring R := Inf {S | s ⊆ S} lemma mem_closure {x : R} {s : set R} : x ∈ closure s ↔ ∀ S : subring R, s ⊆ S → x ∈ S := mem_Inf /-- The subring generated by a set includes the set. -/ @[simp] lemma subset_closure {s : set R} : s ⊆ closure s := λ x hx, mem_closure.2 $ λ S hS, hS hx lemma not_mem_of_not_mem_closure {s : set R} {P : R} (hP : P ∉ closure s) : P ∉ s := λ h, hP (subset_closure h) /-- A subring `t` includes `closure s` if and only if it includes `s`. -/ @[simp] lemma closure_le {s : set R} {t : subring R} : closure s ≤ t ↔ s ⊆ t := ⟨set.subset.trans subset_closure, λ h, Inf_le h⟩ /-- Subring closure of a set is monotone in its argument: if `s ⊆ t`, then `closure s ≤ closure t`. -/ lemma closure_mono ⦃s t : set R⦄ (h : s ⊆ t) : closure s ≤ closure t := closure_le.2 $ set.subset.trans h subset_closure lemma closure_eq_of_le {s : set R} {t : subring R} (h₁ : s ⊆ t) (h₂ : t ≤ closure s) : closure s = t := le_antisymm (closure_le.2 h₁) h₂ /-- An induction principle for closure membership. If `p` holds for `0`, `1`, and all elements of `s`, and is preserved under addition, negation, and multiplication, then `p` holds for all elements of the closure of `s`. -/ @[elab_as_eliminator] lemma closure_induction {s : set R} {p : R → Prop} {x} (h : x ∈ closure s) (Hs : ∀ x ∈ s, p x) (H0 : p 0) (H1 : p 1) (Hadd : ∀ x y, p x → p y → p (x + y)) (Hneg : ∀ (x : R), p x → p (-x)) (Hmul : ∀ x y, p x → p y → p (x * y)) : p x := (@closure_le _ _ _ ⟨p, Hmul, H1, Hadd, H0, Hneg⟩).2 Hs h /-- An induction principle for closure membership, for predicates with two arguments. -/ @[elab_as_eliminator] lemma closure_induction₂ {s : set R} {p : R → R → Prop} {a b : R} (ha : a ∈ closure s) (hb : b ∈ closure s) (Hs : ∀ (x ∈ s) (y ∈ s), p x y) (H0_left : ∀ x, p 0 x) (H0_right : ∀ x, p x 0) (H1_left : ∀ x, p 1 x) (H1_right : ∀ x, p x 1) (Hneg_left : ∀ x y, p x y → p (-x) y) (Hneg_right : ∀ x y, p x y → p x (-y)) (Hadd_left : ∀ x₁ x₂ y, p x₁ y → p x₂ y → p (x₁ + x₂) y) (Hadd_right : ∀ x y₁ y₂, p x y₁ → p x y₂ → p x (y₁ + y₂)) (Hmul_left : ∀ x₁ x₂ y, p x₁ y → p x₂ y → p (x₁ * x₂) y) (Hmul_right : ∀ x y₁ y₂, p x y₁ → p x y₂ → p x (y₁ * y₂)) : p a b := begin refine closure_induction hb _ (H0_right _) (H1_right _) (Hadd_right a) (Hneg_right a) (Hmul_right a), refine closure_induction ha Hs (λ x _, H0_left x) (λ x _, H1_left x) _ _ _, { exact (λ x y H₁ H₂ z zs, Hadd_left x y z (H₁ z zs) (H₂ z zs)) }, { exact (λ x hx z zs, Hneg_left x z (hx z zs)) }, { exact (λ x y H₁ H₂ z zs, Hmul_left x y z (H₁ z zs) (H₂ z zs)) } end lemma mem_closure_iff {s : set R} {x} : x ∈ closure s ↔ x ∈ add_subgroup.closure (submonoid.closure s : set R) := ⟨λ h, closure_induction h (λ x hx, add_subgroup.subset_closure $ submonoid.subset_closure hx) (add_subgroup.zero_mem _) (add_subgroup.subset_closure ( submonoid.one_mem (submonoid.closure s)) ) (λ x y hx hy, add_subgroup.add_mem _ hx hy ) (λ x hx, add_subgroup.neg_mem _ hx ) (λ x y hx hy, add_subgroup.closure_induction hy (λ q hq, add_subgroup.closure_induction hx (λ p hp, add_subgroup.subset_closure ((submonoid.closure s).mul_mem hp hq)) (begin rw zero_mul q, apply add_subgroup.zero_mem _, end) (λ p₁ p₂ ihp₁ ihp₂, begin rw add_mul p₁ p₂ q, apply add_subgroup.add_mem _ ihp₁ ihp₂, end) (λ x hx, begin have f : -x * q = -(x*q) := by simp, rw f, apply add_subgroup.neg_mem _ hx, end)) (begin rw mul_zero x, apply add_subgroup.zero_mem _, end) (λ q₁ q₂ ihq₁ ihq₂, begin rw mul_add x q₁ q₂, apply add_subgroup.add_mem _ ihq₁ ihq₂ end) (λ z hz, begin have f : x * -z = -(x*z) := by simp, rw f, apply add_subgroup.neg_mem _ hz, end)), λ h, add_subgroup.closure_induction h (λ x hx, submonoid.closure_induction hx (λ x hx, subset_closure hx) (one_mem _) (λ x y hx hy, mul_mem hx hy)) (zero_mem _) (λ x y hx hy, add_mem hx hy) (λ x hx, neg_mem hx)⟩ /-- If all elements of `s : set A` commute pairwise, then `closure s` is a commutative ring. -/ def closure_comm_ring_of_comm {s : set R} (hcomm : ∀ (a ∈ s) (b ∈ s), a * b = b * a) : comm_ring (closure s) := { mul_comm := λ x y, begin ext, simp only [subring.coe_mul], refine closure_induction₂ x.prop y.prop hcomm (λ x, by simp only [mul_zero, zero_mul]) (λ x, by simp only [mul_zero, zero_mul]) (λ x, by simp only [mul_one, one_mul]) (λ x, by simp only [mul_one, one_mul]) (λ x y hxy, by simp only [mul_neg, neg_mul, hxy]) (λ x y hxy, by simp only [mul_neg, neg_mul, hxy]) (λ x₁ x₂ y h₁ h₂, by simp only [add_mul, mul_add, h₁, h₂]) (λ x₁ x₂ y h₁ h₂, by simp only [add_mul, mul_add, h₁, h₂]) (λ x₁ x₂ y h₁ h₂, by rw [←mul_assoc, ←h₁, mul_assoc x₁ y x₂, ←h₂, mul_assoc]) (λ x₁ x₂ y h₁ h₂, by rw [←mul_assoc, h₁, mul_assoc, h₂, ←mul_assoc]) end, ..(closure s).to_ring } theorem exists_list_of_mem_closure {s : set R} {x : R} (h : x ∈ closure s) : (∃ L : list (list R), (∀ t ∈ L, ∀ y ∈ t, y ∈ s ∨ y = (-1:R)) ∧ (L.map list.prod).sum = x) := add_subgroup.closure_induction (mem_closure_iff.1 h) (λ x hx, let ⟨l, hl, h⟩ :=submonoid.exists_list_of_mem_closure hx in ⟨[l], by simp [h]; clear_aux_decl; tauto!⟩) ⟨[], by simp⟩ (λ x y ⟨l, hl1, hl2⟩ ⟨m, hm1, hm2⟩, ⟨l ++ m, λ t ht, (list.mem_append.1 ht).elim (hl1 t) (hm1 t), by simp [hl2, hm2]⟩) (λ x ⟨L, hL⟩, ⟨L.map (list.cons (-1)), list.forall_mem_map_iff.2 $ λ j hj, list.forall_mem_cons.2 ⟨or.inr rfl, hL.1 j hj⟩, hL.2 ▸ list.rec_on L (by simp) (by simp [list.map_cons, add_comm] {contextual := tt})⟩) variable (R) /-- `closure` forms a Galois insertion with the coercion to set. -/ protected def gi : galois_insertion (@closure R _) coe := { choice := λ s _, closure s, gc := λ s t, closure_le, le_l_u := λ s, subset_closure, choice_eq := λ s h, rfl } variable {R} /-- Closure of a subring `S` equals `S`. -/ lemma closure_eq (s : subring R) : closure (s : set R) = s := (subring.gi R).l_u_eq s @[simp] lemma closure_empty : closure (∅ : set R) = ⊥ := (subring.gi R).gc.l_bot @[simp] lemma closure_univ : closure (set.univ : set R) = ⊤ := @coe_top R _ ▸ closure_eq ⊤ lemma closure_union (s t : set R) : closure (s ∪ t) = closure s ⊔ closure t := (subring.gi R).gc.l_sup lemma closure_Union {ι} (s : ι → set R) : closure (⋃ i, s i) = ⨆ i, closure (s i) := (subring.gi R).gc.l_supr lemma closure_sUnion (s : set (set R)) : closure (⋃₀ s) = ⨆ t ∈ s, closure t := (subring.gi R).gc.l_Sup lemma map_sup (s t : subring R) (f : R →+* S) : (s ⊔ t).map f = s.map f ⊔ t.map f := (gc_map_comap f).l_sup lemma map_supr {ι : Sort*} (f : R →+* S) (s : ι → subring R) : (supr s).map f = ⨆ i, (s i).map f := (gc_map_comap f).l_supr lemma comap_inf (s t : subring S) (f : R →+* S) : (s ⊓ t).comap f = s.comap f ⊓ t.comap f := (gc_map_comap f).u_inf lemma comap_infi {ι : Sort*} (f : R →+* S) (s : ι → subring S) : (infi s).comap f = ⨅ i, (s i).comap f := (gc_map_comap f).u_infi @[simp] lemma map_bot (f : R →+* S) : (⊥ : subring R).map f = ⊥ := (gc_map_comap f).l_bot @[simp] lemma comap_top (f : R →+* S) : (⊤ : subring S).comap f = ⊤ := (gc_map_comap f).u_top /-- Given `subring`s `s`, `t` of rings `R`, `S` respectively, `s.prod t` is `s ×̂ t` as a subring of `R × S`. -/ def prod (s : subring R) (t : subring S) : subring (R × S) := { carrier := (s : set R) ×ˢ (t : set S), .. s.to_submonoid.prod t.to_submonoid, .. s.to_add_subgroup.prod t.to_add_subgroup} @[norm_cast] lemma coe_prod (s : subring R) (t : subring S) : (s.prod t : set (R × S)) = (s : set R) ×ˢ (t : set S) := rfl lemma mem_prod {s : subring R} {t : subring S} {p : R × S} : p ∈ s.prod t ↔ p.1 ∈ s ∧ p.2 ∈ t := iff.rfl @[mono] lemma prod_mono ⦃s₁ s₂ : subring R⦄ (hs : s₁ ≤ s₂) ⦃t₁ t₂ : subring S⦄ (ht : t₁ ≤ t₂) : s₁.prod t₁ ≤ s₂.prod t₂ := set.prod_mono hs ht lemma prod_mono_right (s : subring R) : monotone (λ t : subring S, s.prod t) := prod_mono (le_refl s) lemma prod_mono_left (t : subring S) : monotone (λ s : subring R, s.prod t) := λ s₁ s₂ hs, prod_mono hs (le_refl t) lemma prod_top (s : subring R) : s.prod (⊤ : subring S) = s.comap (ring_hom.fst R S) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_fst] lemma top_prod (s : subring S) : (⊤ : subring R).prod s = s.comap (ring_hom.snd R S) := ext $ λ x, by simp [mem_prod, monoid_hom.coe_snd] @[simp] lemma top_prod_top : (⊤ : subring R).prod (⊤ : subring S) = ⊤ := (top_prod _).trans $ comap_top _ /-- Product of subrings is isomorphic to their product as rings. -/ def prod_equiv (s : subring R) (t : subring S) : s.prod t ≃+* s × t := { map_mul' := λ x y, rfl, map_add' := λ x y, rfl, .. equiv.set.prod ↑s ↑t } /-- The underlying set of a non-empty directed Sup of subrings is just a union of the subrings. Note that this fails without the directedness assumption (the union of two subrings is typically not a subring) -/ lemma mem_supr_of_directed {ι} [hι : nonempty ι] {S : ι → subring R} (hS : directed (≤) S) {x : R} : x ∈ (⨆ i, S i) ↔ ∃ i, x ∈ S i := begin refine ⟨_, λ ⟨i, hi⟩, (set_like.le_def.1 $ le_supr S i) hi⟩, let U : subring R := subring.mk' (⋃ i, (S i : set R)) (⨆ i, (S i).to_submonoid) (⨆ i, (S i).to_add_subgroup) (submonoid.coe_supr_of_directed $ hS.mono_comp _ (λ _ _, id)) (add_subgroup.coe_supr_of_directed $ hS.mono_comp _ (λ _ _, id)), suffices : (⨆ i, S i) ≤ U, by simpa using @this x, exact supr_le (λ i x hx, set.mem_Union.2 ⟨i, hx⟩), end lemma coe_supr_of_directed {ι} [hι : nonempty ι] {S : ι → subring R} (hS : directed (≤) S) : ((⨆ i, S i : subring R) : set R) = ⋃ i, ↑(S i) := set.ext $ λ x, by simp [mem_supr_of_directed hS] lemma mem_Sup_of_directed_on {S : set (subring R)} (Sne : S.nonempty) (hS : directed_on (≤) S) {x : R} : x ∈ Sup S ↔ ∃ s ∈ S, x ∈ s := begin haveI : nonempty S := Sne.to_subtype, simp only [Sup_eq_supr', mem_supr_of_directed hS.directed_coe, set_coe.exists, subtype.coe_mk] end lemma coe_Sup_of_directed_on {S : set (subring R)} (Sne : S.nonempty) (hS : directed_on (≤) S) : (↑(Sup S) : set R) = ⋃ s ∈ S, ↑s := set.ext $ λ x, by simp [mem_Sup_of_directed_on Sne hS] lemma mem_map_equiv {f : R ≃+* S} {K : subring R} {x : S} : x ∈ K.map (f : R →+* S) ↔ f.symm x ∈ K := @set.mem_image_equiv _ _ ↑K f.to_equiv x lemma map_equiv_eq_comap_symm (f : R ≃+* S) (K : subring R) : K.map (f : R →+* S) = K.comap f.symm := set_like.coe_injective (f.to_equiv.image_eq_preimage K) lemma comap_equiv_eq_map_symm (f : R ≃+* S) (K : subring S) : K.comap (f : R →+* S) = K.map f.symm := (map_equiv_eq_comap_symm f.symm K).symm end subring namespace ring_hom variables {s : subring R} open subring /-- Restriction of a ring homomorphism to a subring of the domain. -/ def restrict (f : R →+* S) (s : subring R) : s →+* S := f.comp s.subtype @[simp] lemma restrict_apply (f : R →+* S) (x : s) : f.restrict s x = f x := rfl /-- Restriction of a ring homomorphism to its range interpreted as a subsemiring. This is the bundled version of `set.range_factorization`. -/ def range_restrict (f : R →+* S) : R →+* f.range := f.cod_restrict' f.range $ λ x, ⟨x, rfl⟩ @[simp] lemma coe_range_restrict (f : R →+* S) (x : R) : (f.range_restrict x : S) = f x := rfl lemma range_restrict_surjective (f : R →+* S) : function.surjective f.range_restrict := λ ⟨y, hy⟩, let ⟨x, hx⟩ := mem_range.mp hy in ⟨x, subtype.ext hx⟩ lemma range_top_iff_surjective {f : R →+* S} : f.range = (⊤ : subring S) ↔ function.surjective f := set_like.ext'_iff.trans $ iff.trans (by rw [coe_range, coe_top]) set.range_iff_surjective /-- The range of a surjective ring homomorphism is the whole of the codomain. -/ lemma range_top_of_surjective (f : R →+* S) (hf : function.surjective f) : f.range = (⊤ : subring S) := range_top_iff_surjective.2 hf /-- The subring of elements `x : R` such that `f x = g x`, i.e., the equalizer of f and g as a subring of R -/ def eq_locus (f g : R →+* S) : subring R := { carrier := {x | f x = g x}, .. (f : R →* S).eq_mlocus g, .. (f : R →+ S).eq_locus g } /-- If two ring homomorphisms are equal on a set, then they are equal on its subring closure. -/ lemma eq_on_set_closure {f g : R →+* S} {s : set R} (h : set.eq_on f g s) : set.eq_on f g (closure s) := show closure s ≤ f.eq_locus g, from closure_le.2 h lemma eq_of_eq_on_set_top {f g : R →+* S} (h : set.eq_on f g (⊤ : subring R)) : f = g := ext $ λ x, h trivial lemma eq_of_eq_on_set_dense {s : set R} (hs : closure s = ⊤) {f g : R →+* S} (h : s.eq_on f g) : f = g := eq_of_eq_on_set_top $ hs ▸ eq_on_set_closure h lemma closure_preimage_le (f : R →+* S) (s : set S) : closure (f ⁻¹' s) ≤ (closure s).comap f := closure_le.2 $ λ x hx, set_like.mem_coe.2 $ mem_comap.2 $ subset_closure hx /-- The image under a ring homomorphism of the subring generated by a set equals the subring generated by the image of the set. -/ lemma map_closure (f : R →+* S) (s : set R) : (closure s).map f = closure (f '' s) := le_antisymm (map_le_iff_le_comap.2 $ le_trans (closure_mono $ set.subset_preimage_image _ _) (closure_preimage_le _ _)) (closure_le.2 $ set.image_subset _ subset_closure) end ring_hom namespace subring open ring_hom /-- The ring homomorphism associated to an inclusion of subrings. -/ def inclusion {S T : subring R} (h : S ≤ T) : S →+* T := S.subtype.cod_restrict' _ (λ x, h x.2) @[simp] lemma range_subtype (s : subring R) : s.subtype.range = s := set_like.coe_injective $ (coe_srange _).trans subtype.range_coe @[simp] lemma range_fst : (fst R S).srange = ⊤ := (fst R S).srange_top_of_surjective $ prod.fst_surjective @[simp] lemma range_snd : (snd R S).srange = ⊤ := (snd R S).srange_top_of_surjective $ prod.snd_surjective @[simp] lemma prod_bot_sup_bot_prod (s : subring R) (t : subring S) : (s.prod ⊥) ⊔ (prod ⊥ t) = s.prod t := le_antisymm (sup_le (prod_mono_right s bot_le) (prod_mono_left t bot_le)) $ assume p hp, prod.fst_mul_snd p ▸ mul_mem ((le_sup_left : s.prod ⊥ ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨hp.1, set_like.mem_coe.2 $ one_mem ⊥⟩) ((le_sup_right : prod ⊥ t ≤ s.prod ⊥ ⊔ prod ⊥ t) ⟨set_like.mem_coe.2 $ one_mem ⊥, hp.2⟩) end subring namespace ring_equiv variables {s t : subring R} /-- Makes the identity isomorphism from a proof two subrings of a multiplicative monoid are equal. -/ def subring_congr (h : s = t) : s ≃+* t := { map_mul' := λ _ _, rfl, map_add' := λ _ _, rfl, ..equiv.set_congr $ congr_arg _ h } /-- Restrict a ring homomorphism with a left inverse to a ring isomorphism to its `ring_hom.range`. -/ def of_left_inverse {g : S → R} {f : R →+* S} (h : function.left_inverse g f) : R ≃+* f.range := { to_fun := λ x, f.range_restrict x, inv_fun := λ x, (g ∘ f.range.subtype) x, left_inv := h, right_inv := λ x, subtype.ext $ let ⟨x', hx'⟩ := ring_hom.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 : S → R} {f : R →+* S} (h : function.left_inverse g f) (x : R) : ↑(of_left_inverse h x) = f x := rfl @[simp] lemma of_left_inverse_symm_apply {g : S → R} {f : R →+* S} (h : function.left_inverse g f) (x : f.range) : (of_left_inverse h).symm x = g x := rfl /-- Given an equivalence `e : R ≃+* S` of rings and a subring `s` of `R`, `subring_equiv_map e s` is the induced equivalence between `s` and `s.map e` -/ @[simps] def subring_map (e : R ≃+* S) : s ≃+* s.map e.to_ring_hom := e.subsemiring_map s.to_subsemiring end ring_equiv namespace subring variables {s : set R} local attribute [reducible] closure @[elab_as_eliminator] protected theorem in_closure.rec_on {C : R → Prop} {x : R} (hx : x ∈ closure s) (h1 : C 1) (hneg1 : C (-1)) (hs : ∀ z ∈ s, ∀ n, C n → C (z * n)) (ha : ∀ {x y}, C x → C y → C (x + y)) : C x := begin have h0 : C 0 := add_neg_self (1:R) ▸ ha h1 hneg1, rcases exists_list_of_mem_closure hx with ⟨L, HL, rfl⟩, clear hx, induction L with hd tl ih, { exact h0 }, rw list.forall_mem_cons at HL, suffices : C (list.prod hd), { rw [list.map_cons, list.sum_cons], exact ha this (ih HL.2) }, replace HL := HL.1, clear ih tl, suffices : ∃ L : list R, (∀ x ∈ L, x ∈ s) ∧ (list.prod hd = list.prod L ∨ list.prod hd = -list.prod L), { rcases this with ⟨L, HL', HP | HP⟩, { rw HP, clear HP HL hd, induction L with hd tl ih, { exact h1 }, rw list.forall_mem_cons at HL', rw list.prod_cons, exact hs _ HL'.1 _ (ih HL'.2) }, rw HP, clear HP HL hd, induction L with hd tl ih, { exact hneg1 }, rw [list.prod_cons, neg_mul_eq_mul_neg], rw list.forall_mem_cons at HL', exact hs _ HL'.1 _ (ih HL'.2) }, induction hd with hd tl ih, { exact ⟨[], list.forall_mem_nil _, or.inl rfl⟩ }, rw list.forall_mem_cons at HL, rcases ih HL.2 with ⟨L, HL', HP | HP⟩; cases HL.1 with hhd hhd, { exact ⟨hd :: L, list.forall_mem_cons.2 ⟨hhd, HL'⟩, or.inl $ by rw [list.prod_cons, list.prod_cons, HP]⟩ }, { exact ⟨L, HL', or.inr $ by rw [list.prod_cons, hhd, neg_one_mul, HP]⟩ }, { exact ⟨hd :: L, list.forall_mem_cons.2 ⟨hhd, HL'⟩, or.inr $ by rw [list.prod_cons, list.prod_cons, HP, neg_mul_eq_mul_neg]⟩ }, { exact ⟨L, HL', or.inl $ by rw [list.prod_cons, hhd, HP, neg_one_mul, neg_neg]⟩ } end lemma closure_preimage_le (f : R →+* S) (s : set S) : closure (f ⁻¹' s) ≤ (closure s).comap f := closure_le.2 $ λ x hx, set_like.mem_coe.2 $ mem_comap.2 $ subset_closure hx end subring lemma add_subgroup.int_mul_mem {G : add_subgroup R} (k : ℤ) {g : R} (h : g ∈ G) : (k : R) * g ∈ G := by { convert add_subgroup.zsmul_mem G h k, simp } /-! ## Actions by `subring`s These are just copies of the definitions about `subsemiring` starting from `subsemiring.mul_action`. When `R` is commutative, `algebra.of_subring` provides a stronger result than those found in this file, which uses the same scalar action. -/ section actions namespace subring variables {α β : Type*} /-- The action by a subring is the action by the underlying ring. -/ instance [has_scalar R α] (S : subring R) : has_scalar S α := S.to_subsemiring.has_scalar lemma smul_def [has_scalar R α] {S : subring R} (g : S) (m : α) : g • m = (g : R) • m := rfl instance smul_comm_class_left [has_scalar R β] [has_scalar α β] [smul_comm_class R α β] (S : subring R) : smul_comm_class S α β := S.to_subsemiring.smul_comm_class_left instance smul_comm_class_right [has_scalar α β] [has_scalar R β] [smul_comm_class α R β] (S : subring R) : smul_comm_class α S β := S.to_subsemiring.smul_comm_class_right /-- Note that this provides `is_scalar_tower S R R` which is needed by `smul_mul_assoc`. -/ instance [has_scalar α β] [has_scalar R α] [has_scalar R β] [is_scalar_tower R α β] (S : subring R) : is_scalar_tower S α β := S.to_subsemiring.is_scalar_tower instance [has_scalar R α] [has_faithful_scalar R α] (S : subring R) : has_faithful_scalar S α := S.to_subsemiring.has_faithful_scalar /-- The action by a subring is the action by the underlying ring. -/ instance [mul_action R α] (S : subring R) : mul_action S α := S.to_subsemiring.mul_action /-- The action by a subring is the action by the underlying ring. -/ instance [add_monoid α] [distrib_mul_action R α] (S : subring R) : distrib_mul_action S α := S.to_subsemiring.distrib_mul_action /-- The action by a subring is the action by the underlying ring. -/ instance [monoid α] [mul_distrib_mul_action R α] (S : subring R) : mul_distrib_mul_action S α := S.to_subsemiring.mul_distrib_mul_action /-- The action by a subring is the action by the underlying ring. -/ instance [has_zero α] [smul_with_zero R α] (S : subring R) : smul_with_zero S α := S.to_subsemiring.smul_with_zero /-- The action by a subring is the action by the underlying ring. -/ instance [has_zero α] [mul_action_with_zero R α] (S : subring R) : mul_action_with_zero S α := S.to_subsemiring.mul_action_with_zero /-- The action by a subring is the action by the underlying ring. -/ instance [add_comm_monoid α] [module R α] (S : subring R) : module S α := S.to_subsemiring.module end subring end actions -- while this definition is not about subrings, this is the earliest we have -- both ordered ring structures and submonoids available /-- The subgroup of positive units of a linear ordered semiring. -/ def units.pos_subgroup (R : Type*) [linear_ordered_semiring R] : subgroup Rˣ := { carrier := {x | (0 : R) < x}, inv_mem' := λ x, units.inv_pos.mpr, ..(pos_submonoid R).comap (units.coe_hom R)} @[simp] lemma units.mem_pos_subgroup {R : Type*} [linear_ordered_semiring R] (u : Rˣ) : u ∈ units.pos_subgroup R ↔ (0 : R) < u := iff.rfl
0ae690ec47ae38eef5469c9f4867e9425ff44f03
437dc96105f48409c3981d46fb48e57c9ac3a3e4
/src/category_theory/limits/shapes/kernels.lean
3f78c56eaf5a27ff25592646d61c2522e2745716
[ "Apache-2.0" ]
permissive
dan-c-k/mathlib
08efec79bd7481ee6da9cc44c24a653bff4fbe0d
96efc220f6225bc7a5ed8349900391a33a38cc56
refs/heads/master
1,658,082,847,093
1,589,013,201,000
1,589,013,201,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
11,320
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Markus Himmel -/ import category_theory.limits.shapes.zero import category_theory.limits.shapes.equalizers /-! # Kernels and cokernels In a category with zero morphisms, the kernel of a morphism `f : X ⟶ Y` is just the equalizer of `f` and `0 : X ⟶ Y`. (Similarly the cokernel is the coequalizer.) We don't yet prove much here, just provide * `kernel : (X ⟶ Y) → C` * `kernel.ι : kernel f ⟶ X` * `kernel.condition : kernel.ι f ≫ f = 0` and * `kernel.lift (k : W ⟶ X) (h : k ≫ f = 0) : W ⟶ kernel f` (as well as the dual versions) ## Main statements Besides the definition and lifts, * `kernel.ι_zero_is_iso`: a kernel map of a zero morphism is an isomorphism * `kernel.is_limit_cone_zero_cone`: if our category has a zero object, then the map from the zero obect is a kernel map of any monomorphism ## Future work * TODO: images and coimages, and then abelian categories. * TODO: connect this with existing working in the group theory and ring theory libraries. ## Implementation notes As with the other special shapes in the limits library, all the definitions here are given as `abbreviation`s of the general statements for limits, so all the `simp` lemmas and theorems about general limits can be used. ## References * [F. Borceux, *Handbook of Categorical Algebra 2*][borceux-vol2] -/ universes v u open category_theory open category_theory.limits.walking_parallel_pair namespace category_theory.limits variables {C : Type u} [category.{v} C] variables {X Y : C} (f : X ⟶ Y) section variables [has_zero_morphisms.{v} C] /-- A kernel fork is just a fork where the second morphism is a zero morphism. -/ abbreviation kernel_fork := fork f 0 variables {f} @[simp, reassoc] lemma kernel_fork.condition (s : kernel_fork f) : fork.ι s ≫ f = 0 := by erw [fork.condition, has_zero_morphisms.comp_zero] @[simp] lemma kernel_fork.app_one (s : kernel_fork f) : s.π.app one = 0 := by rw [←fork.app_zero_left, kernel_fork.condition] /-- A morphism `ι` satisfying `ι ≫ f = 0` determines a kernel fork over `f`. -/ abbreviation kernel_fork.of_ι {Z : C} (ι : Z ⟶ X) (w : ι ≫ f = 0) : kernel_fork f := fork.of_ι ι $ by rw [w, has_zero_morphisms.comp_zero] /-- If `s` is a limit kernel fork and `k : W ⟶ X` satisfies ``k ≫ f = 0`, then there is some `l : W ⟶ s.X` sich that `l ≫ fork.ι s = k`. -/ def kernel_fork.is_limit.lift' {s : kernel_fork f} (hs : is_limit s) {W : C} (k : W ⟶ X) (h : k ≫ f = 0) : {l : W ⟶ s.X // l ≫ fork.ι s = k} := ⟨hs.lift $ kernel_fork.of_ι _ h, hs.fac _ _⟩ end section variables [has_zero_morphisms.{v} C] [has_limit (parallel_pair f 0)] /-- The kernel of a morphism, expressed as the equalizer with the 0 morphism. -/ abbreviation kernel : C := equalizer f 0 /-- The map from `kernel f` into the source of `f`. -/ abbreviation kernel.ι : kernel f ⟶ X := equalizer.ι f 0 @[simp, reassoc] lemma kernel.condition : kernel.ι f ≫ f = 0 := kernel_fork.condition _ /-- Given any morphism `k : W ⟶ X` satisfying `k ≫ f = 0`, `k` factors through `kernel.ι f` via `kernel.lift : W ⟶ kernel f`. -/ abbreviation kernel.lift {W : C} (k : W ⟶ X) (h : k ≫ f = 0) : W ⟶ kernel f := limit.lift (parallel_pair f 0) (kernel_fork.of_ι k h) @[simp, reassoc] lemma kernel.lift_ι {W : C} (k : W ⟶ X) (h : k ≫ f = 0) : kernel.lift f k h ≫ kernel.ι f = k := limit.lift_π _ _ /-- Any morphism `k : W ⟶ X` satisfying `k ≫ f = 0` induces a morphism `l : W ⟶ kernel f` such that `l ≫ kernel.ι f = k`. -/ def kernel.lift' {W : C} (k : W ⟶ X) (h : k ≫ f = 0) : {l : W ⟶ kernel f // l ≫ kernel.ι f = k} := ⟨kernel.lift f k h, kernel.lift_ι _ _ _⟩ /-- Every kernel of the zero morphism is an isomorphism -/ def kernel.ι_zero_is_iso [has_limit (parallel_pair (0 : X ⟶ Y) 0)] : is_iso (kernel.ι (0 : X ⟶ Y)) := equalizer.ι_of_self _ end section has_zero_object variables [has_zero_object.{v} C] local attribute [instance] has_zero_object.has_zero variables [has_zero_morphisms.{v} C] /-- The morphism from the zero object determines a cone on a kernel diagram -/ def kernel.zero_cone : cone (parallel_pair f 0) := { X := 0, π := { app := λ j, 0 }} /-- The map from the zero object is a kernel of a monomorphism -/ def kernel.is_limit_cone_zero_cone [mono f] : is_limit (kernel.zero_cone f) := fork.is_limit.mk _ (λ s, 0) (λ s, by { erw has_zero_morphisms.zero_comp, convert (@zero_of_comp_mono _ _ _ _ _ _ _ f _ _).symm, exact kernel_fork.condition _ }) (λ _ _ _, has_zero_object.zero_of_to_zero _) /-- The kernel of a monomorphism is isomorphic to the zero object -/ def kernel.of_mono [has_limit (parallel_pair f 0)] [mono f] : kernel f ≅ 0 := functor.map_iso (cones.forget _) $ is_limit.unique_up_to_iso (limit.is_limit (parallel_pair f 0)) (kernel.is_limit_cone_zero_cone f) /-- The kernel morphism of a monomorphism is a zero morphism -/ lemma kernel.ι_of_mono [has_limit (parallel_pair f 0)] [mono f] : kernel.ι f = 0 := by rw [←category.id_comp (kernel.ι f), ←iso.hom_inv_id (kernel.of_mono f), category.assoc, has_zero_object.zero_of_to_zero (kernel.of_mono f).hom, has_zero_morphisms.zero_comp] end has_zero_object section variables (X) (Y) [has_zero_morphisms.{v} C] /-- The kernel morphism of a zero morphism is an isomorphism -/ def kernel.ι_of_zero [has_limit (parallel_pair (0 : X ⟶ Y) 0)] : is_iso (kernel.ι (0 : X ⟶ Y)) := equalizer.ι_of_self _ end section variables [has_zero_morphisms.{v} C] /-- A cokernel cofork is just a cofork where the second morphism is a zero morphism. -/ abbreviation cokernel_cofork := cofork f 0 variables {f} @[simp, reassoc] lemma cokernel_cofork.condition (s : cokernel_cofork f) : f ≫ cofork.π s = 0 := by rw [cofork.condition, has_zero_morphisms.zero_comp] @[simp] lemma cokernel_cofork.app_zero (s : cokernel_cofork f) : s.ι.app zero = 0 := by rw [←cofork.left_app_one, cokernel_cofork.condition] /-- A morphism `π` satisfying `f ≫ π = 0` determines a cokernel cofork on `f`. -/ abbreviation cokernel_cofork.of_π {Z : C} (π : Y ⟶ Z) (w : f ≫ π = 0) : cokernel_cofork f := cofork.of_π π $ by rw [w, has_zero_morphisms.zero_comp] /-- If `s` is a colimit cokernel cofork, then every `k : Y ⟶ W` satisfying `f ≫ k = 0` induces `l : s.X ⟶ W` such that `cofork.π s ≫ l = k`. -/ def cokernel_cofork.is_colimit.desc' {s : cokernel_cofork f} (hs : is_colimit s) {W : C} (k : Y ⟶ W) (h : f ≫ k = 0) : {l : s.X ⟶ W // cofork.π s ≫ l = k} := ⟨hs.desc $ cokernel_cofork.of_π _ h, hs.fac _ _⟩ end section variables [has_zero_morphisms.{v} C] [has_colimit (parallel_pair f 0)] /-- The cokernel of a morphism, expressed as the coequalizer with the 0 morphism. -/ abbreviation cokernel : C := coequalizer f 0 /-- The map from the target of `f` to `cokernel f`. -/ abbreviation cokernel.π : Y ⟶ cokernel f := coequalizer.π f 0 @[simp, reassoc] lemma cokernel.condition : f ≫ cokernel.π f = 0 := cokernel_cofork.condition _ /-- Given any morphism `k : Y ⟶ W` such that `f ≫ k = 0`, `k` factors through `cokernel.π f` via `cokernel.desc : cokernel f ⟶ W`. -/ abbreviation cokernel.desc {W : C} (k : Y ⟶ W) (h : f ≫ k = 0) : cokernel f ⟶ W := colimit.desc (parallel_pair f 0) (cokernel_cofork.of_π k h) @[simp, reassoc] lemma cokernel.π_desc {W : C} (k : Y ⟶ W) (h : f ≫ k = 0) : cokernel.π f ≫ cokernel.desc f k h = k := colimit.ι_desc _ _ /-- Any morphism `k : Y ⟶ W` satisfying `f ≫ k = 0` induces `l : cokernel f ⟶ W` such that `cokernel.π f ≫ l = k`. -/ def cokernel.desc' {W : C} (k : Y ⟶ W) (h : f ≫ k = 0) : {l : cokernel f ⟶ W // cokernel.π f ≫ l = k} := ⟨cokernel.desc f k h, cokernel.π_desc _ _ _⟩ end section has_zero_object variables [has_zero_object.{v} C] local attribute [instance] has_zero_object.has_zero variable [has_zero_morphisms.{v} C] /-- The morphism to the zero object determines a cocone on a cokernel diagram -/ def cokernel.zero_cocone : cocone (parallel_pair f 0) := { X := 0, ι := { app := λ j, 0 } } /-- The morphism to the zero object is a cokernel of an epimorphism -/ def cokernel.is_colimit_cocone_zero_cocone [epi f] : is_colimit (cokernel.zero_cocone f) := cofork.is_colimit.mk _ (λ s, 0) (λ s, by { erw has_zero_morphisms.zero_comp, convert (@zero_of_comp_epi _ _ _ _ _ _ f _ _ _).symm, exact cokernel_cofork.condition _ }) (λ _ _ _, has_zero_object.zero_of_from_zero _) /-- The cokernel of an epimorphism is isomorphic to the zero object -/ def cokernel.of_epi [has_colimit (parallel_pair f 0)] [epi f] : cokernel f ≅ 0 := functor.map_iso (cocones.forget _) $ is_colimit.unique_up_to_iso (colimit.is_colimit (parallel_pair f 0)) (cokernel.is_colimit_cocone_zero_cocone f) /-- The cokernel morphism if an epimorphism is a zero morphism -/ lemma cokernel.π_of_epi [has_colimit (parallel_pair f 0)] [epi f] : cokernel.π f = 0 := by rw [←category.comp_id (cokernel.π f), ←iso.hom_inv_id (cokernel.of_epi f), ←category.assoc, has_zero_object.zero_of_from_zero (cokernel.of_epi f).inv, has_zero_morphisms.comp_zero] end has_zero_object section variables (X) (Y) [has_zero_morphisms.{v} C] /-- The cokernel of a zero morphism is an isomorphism -/ def cokernel.π_of_zero [has_colimit (parallel_pair (0 : X ⟶ Y) 0)] : is_iso (cokernel.π (0 : X ⟶ Y)) := coequalizer.π_of_self _ end section has_zero_object variables [has_zero_object.{v} C] local attribute [instance] has_zero_object.has_zero variables [has_zero_morphisms.{v} C] /-- The kernel of the cokernel of an epimorphism is an isomorphism -/ instance kernel.of_cokernel_of_epi [has_colimit (parallel_pair f 0)] [has_limit (parallel_pair (cokernel.π f) 0)] [epi f] : is_iso (kernel.ι (cokernel.π f)) := equalizer.ι_of_eq $ cokernel.π_of_epi f /-- The cokernel of the kernel of a monomorphism is an isomorphism -/ instance cokernel.of_kernel_of_mono [has_limit (parallel_pair f 0)] [has_colimit (parallel_pair (kernel.ι f) 0)] [mono f] : is_iso (cokernel.π (kernel.ι f)) := coequalizer.π_of_eq $ kernel.ι_of_mono f end has_zero_object end category_theory.limits namespace category_theory.limits variables (C : Type u) [category.{v} C] variables [has_zero_morphisms.{v} C] /-- `has_kernels` represents a choice of kernel for every morphism -/ class has_kernels := (has_limit : Π {X Y : C} (f : X ⟶ Y), has_limit (parallel_pair f 0)) /-- `has_cokernels` represents a choice of cokernel for every morphism -/ class has_cokernels := (has_colimit : Π {X Y : C} (f : X ⟶ Y), has_colimit (parallel_pair f 0)) attribute [instance] has_kernels.has_limit has_cokernels.has_colimit /-- Kernels are finite limits, so if `C` has all finite limits, it also has all kernels -/ def has_kernels_of_has_finite_limits [has_finite_limits.{v} C] : has_kernels.{v} C := { has_limit := infer_instance } /-- Cokernels are finite limits, so if `C` has all finite colimits, it also has all cokernels -/ def has_cokernels_of_has_finite_colimits [has_finite_colimits.{v} C] : has_cokernels.{v} C := { has_colimit := infer_instance } end category_theory.limits
6811f579e2c0b552b773b0e7be6675e81843cd15
fe84e287c662151bb313504482b218a503b972f3
/src/poset/category.lean
a2266d19393fcbab4f537cad265a81ddc1bf36ec
[]
no_license
NeilStrickland/lean_lib
91e163f514b829c42fe75636407138b5c75cba83
6a9563de93748ace509d9db4302db6cd77d8f92c
refs/heads/master
1,653,408,198,261
1,652,996,419,000
1,652,996,419,000
181,006,067
4
1
null
null
null
null
UTF-8
Lean
false
false
551
lean
import category_theory.concrete_category universes u v open category_theory @[reducible] def POSet : Type (u+1) := bundled partial_order namespace POSet instance (P : POSet) : partial_order P := P.str /- instance concrete_is_poset_hom : concrete_category @is_group_hom := ⟨by introsI α ia; apply_instance, by introsI α β γ ia ib ic f g hf hg; apply_instance⟩ def of (X : Type u) [group X] : Group := ⟨X⟩ instance hom_is_group_hom {G₁ G₂ : Group} (f : G₁ ⟶ G₂) : is_group_hom (f : G₁ → G₂) := f.2 -/ end POSet
561651477164e51a7dcaff455d32b58f6dd4b490
5ca87fb2890eabae94422af0565520bfafd6c4ae
/lean/common.lean
9a0a661d51a669c59bd4b594720c562f1fef196f
[ "Apache-2.0" ]
permissive
dagit/reopt-vcg
5b7e1a0eceaac4736fd8ab8985d861c86ac5cff2
a80da1c1ff8dbf28d864b30999b166f76e66e231
refs/heads/master
1,585,418,433,534
1,541,806,437,000
1,541,806,437,000
148,844,174
0
0
null
1,536,960,966,000
1,536,960,966,000
null
UTF-8
Lean
false
false
25,526
lean
import .coe1 -- This tries to prove a property by just running the evaluator. meta def dec_trivial_tac : tactic unit := do tgt ← tactic.target, tactic.apply $ (`(@of_as_true) : expr) tgt, tactic.triv def unlines : list string → string := list.foldr (λx r, x ++ "\n" ++ r) "" -- Library for buidling s expressions namespace sexp def app (s:string) (l:list string) := "(" ++ s ++ list.foldr (λx r, " " ++ x ++ r) ")" l -- Pretty print def from_list : list string → string | [] := "()" | (s::l) := app s l def indent : string → string | s := " " ++ s end sexp def paren_if : bool → string → string | tt s := "(" ++ s ++ ")" | ff s := s ------------------------------------------------------------------------ -- Coercisions namespace mc_semantics ------------------------------------------------------------------------ -- arg_index @[reducible] def arg_index := nat def arg_index.pp (idx:arg_index) : string := sexp.app "arg" [idx.repr] ------------------------------------------------------------------------ -- nat_expr inductive nat_expr : Type | lit : nat → nat_expr | var : arg_index → nat_expr | add : nat_expr → nat_expr → nat_expr | sub : nat_expr → nat_expr → nat_expr | mul : nat_expr → nat_expr → nat_expr -- div x y is floor (x / y) | div : nat_expr → nat_expr → nat_expr namespace nat_expr protected def zero : nat_expr := lit 0 protected def one : nat_expr := lit 1 protected def do_add : nat_expr → nat_expr → nat_expr | (lit x) (lit y) := lit (x+y) | x y := add x y protected def do_sub : nat_expr → nat_expr → nat_expr | (lit x) (lit y) := lit (x-y) | x y := sub x y protected def do_mul : nat_expr → nat_expr → nat_expr | (lit x) (lit y) := lit (x*y) | x y := mul x y protected def do_div : nat_expr → nat_expr → nat_expr | (lit x) (lit y) := lit (x/y) | x y := div x y instance : has_zero nat_expr := ⟨nat_expr.zero⟩ instance : has_one nat_expr := ⟨nat_expr.one⟩ instance : has_add nat_expr := ⟨nat_expr.do_add⟩ instance : has_sub nat_expr := ⟨nat_expr.do_sub⟩ instance : has_mul nat_expr := ⟨nat_expr.do_mul⟩ instance : has_div nat_expr := ⟨nat_expr.do_div⟩ protected def pp : nat_expr → string | (lit x) := x.repr | (var x) := x.pp | (add x y) := sexp.app "addNat" [x.pp, y.pp] | (sub x y) := sexp.app "subNat" [x.pp, y.pp] | (mul x y) := sexp.app "mulNat" [x.pp, y.pp] | (div x y) := sexp.app "divNat" [x.pp, y.pp] instance : has_repr nat_expr := ⟨nat_expr.pp⟩ instance nat_coe_nat_expr : has_coe ℕ nat_expr := ⟨λx, lit x⟩ end nat_expr ------------------------------------------------------------------------ -- one_of inductive one_of (l:list ℕ) : Type | var{} : arg_index → one_of namespace one_of def to_nat_expr {l:list ℕ} : one_of l → nat_expr | (one_of.var i) := nat_expr.var i protected def pp {l:list ℕ} (x:one_of l) := x.to_nat_expr.pp instance (l:list ℕ) : has_coe (one_of l) nat_expr := ⟨ one_of.to_nat_expr ⟩ end one_of local notation ℕ := nat_expr inductive type | bv (w:ℕ) : type | bit : type | float : type | double : type | x86_80 : type -- A function from arg to res | fn (arg:type) (res:type) : type namespace type protected def pp' : Π(in_fun:bool), type → string | _ (bv w) := sexp.app "bv" [w.pp] | _ bit := "bit" | _ float := "float" | _ double := "double" | _ x86_80 := "x86_80" | in_fun (fn a r) := if in_fun then a.pp' ff ++ " " ++ r.pp' tt else sexp.app "fun" [a.pp' ff, r.pp' tt] protected def pp : type → string := type.pp' ff end type end mc_semantics ------------------------------------------------------------------------ -- X86 namespace x86 open mc_semantics open mc_semantics.type ------------------------------------------------------------------------ -- type local notation ℕ := nat_expr -- Denotes the type of a register. inductive gpreg_type : Type | reg8l : gpreg_type --| reg8h : gpreg_type | reg16 : gpreg_type | reg32 : gpreg_type | reg64 : gpreg_type namespace gpreg_type @[reducible] def width : gpreg_type → ℕ | reg8l := 8 --| reg8h := 8 | reg16 := 16 | reg32 := 32 | reg64 := 64 end gpreg_type -- Type for x86 registers inductive reg : type → Type | concrete_gpreg (idx:fin 16) (tp:gpreg_type) : reg (bv (tp.width)) | concrete_flagreg (idx:fin 32) : reg bit namespace reg protected def gpreg_prefix (x:fin 16) : string := match x.val with | 0 := "a" | v := "r" ++ v.repr end protected def r8l_names : list string := [ "al", "cl", "dl", "bl" , "spl", "bpl", "sil", "dil" , "r8b" , "r9b" , "r10b", "r11b" , "r12b", "r13b", "r14b", "r15b" ] protected def r16_names : list string := [ "ax", "cx", "dx", "bx" , "sp", "bp", "si", "di" , "r8w" , "r9w" , "r10w", "r11w" , "r12w", "r13w", "r14w", "r15w" ] protected def r32_names : list string := [ "eax", "ecx", "edx", "ebx" , "esp", "ebp", "esi", "edi" , "r8d" , "r9d" , "r10d", "r11d" , "r12d", "r13d", "r14d", "r15d" ] protected def r64_names : list string := [ "rax", "rcx", "rdx", "rbx" , "rsp", "rbp", "rsi", "rdi" , "r8" , "r9" , "r10", "r11" , "r12", "r13", "r14", "r15" ] protected def flag_names : list string := [ "cf", "RESERVED_1", "pf", "RESERVED_3", "af", "RESERVED_5", "zf", "sf" , "tf", "if", "df", "of", "iopl1", "iopl2", "nt", "RESERVED_15" , "rf", "vm", "ac", "vif", "vip", "id" ] protected def repr : Π{tp:type}, reg tp → string | ._ (concrete_gpreg idx tp) := "$" ++ match tp with | gpreg_type.reg8l := list.nth_le reg.r8l_names idx.val idx.is_lt | gpreg_type.reg16 := list.nth_le reg.r16_names idx.val idx.is_lt | gpreg_type.reg32 := list.nth_le reg.r32_names idx.val idx.is_lt | gpreg_type.reg64 := list.nth_le reg.r64_names idx.val idx.is_lt end | ._ (concrete_flagreg idx) := "$" ++ match list.nth reg.flag_names idx.val with | (option.some nm) := nm | option.none := "REVERSED_" ++ idx.val.repr end end reg -- Denotes an address. inductive addr (tp:type) : Type | arg {} (idx: arg_index) : addr namespace addr protected def repr {tp:type} : addr tp → string | (arg idx) := idx.pp end addr --- Expressions that may appear on the left-hand side of an assignment. inductive lhs : type → Type | reg {tp:type} (r:reg tp) : lhs tp -- A value that must be an address. | addr {tp:type} (a:addr tp) : lhs tp -- An argument that may be either a register or address. | arg (idx:arg_index) (tp:type) : lhs tp -- ST reg with the offset relative to the current stack top value. | streg (idx : fin 8) : lhs x86_80 namespace lhs -- Pretty printer for lhs protected def repr : Π {tp:type}, lhs tp → string | _ (reg r) := r.repr | ._ (addr a) := a.repr | _ (arg idx tp) := idx.pp | ._ (streg idx) := "st" ++ idx.val.repr end lhs section def reg8l (i:fin 16) := lhs.reg $ reg.concrete_gpreg i gpreg_type.reg8l def reg8h (i:fin 16) := lhs.reg $ reg.concrete_gpreg (16+i) gpreg_type.reg8l def al := reg8l 0 def cl := reg8l 1 def dl := reg8l 2 def bl := reg8l 3 def spl := reg8l 4 def bpl := reg8l 5 def sil := reg8l 6 def dil := reg8l 7 def ah := reg8h 0 def reg16 (i:fin 16) := lhs.reg $ reg.concrete_gpreg i gpreg_type.reg16 def ax := reg16 0 def cx := reg16 1 def dx := reg16 2 def bx := reg16 3 def reg32 (i:fin 16) := lhs.reg $ reg.concrete_gpreg i gpreg_type.reg32 def eax := reg32 0 def ecx := reg32 1 def edx := reg32 2 def ebx := reg32 3 def reg64 (i:fin 16) := lhs.reg $ reg.concrete_gpreg i gpreg_type.reg64 def rax := reg64 0 def rcx := reg64 1 def rdx := reg64 2 def rbx := reg64 3 def rsp := reg64 4 def rbp := reg64 5 def rsi := reg64 6 def rdi := reg64 7 def r8 := reg64 8 def r9 := reg64 9 def r10 := reg64 10 def r11 := reg64 11 def r12 := reg64 12 def r13 := reg64 13 def r14 := reg64 14 def r15 := reg64 15 def flagreg (i:fin 32) := lhs.reg $ reg.concrete_flagreg i def cf := flagreg 0 def pf := flagreg 2 def af := flagreg 4 def zf := flagreg 6 def sf := flagreg 7 def tf := flagreg 8 def if' := flagreg 9 def df := flagreg 10 def of := flagreg 11 def st0 : lhs x86_80 := lhs.streg 0 end local infixr `.→`:30 := fn -- This denotes primitive operations that are part of the semantics. inductive prim : type → Type -- `(add i)` returns the sum of two i-bit numbers. | add (i:ℕ) : prim (bv i .→ bv i .→ bv i) -- `(adc i)` returns the sum of two i-bit numbers and a carry bit. | adc (i:ℕ) : prim (bv i .→ bv i .→ bit .→ bv i) -- `(mul i)` returns the product of two i-bit numbers. | mul (i:ℕ) : prim (bv i .→ bv i .→ bv i) -- `(quot i)` returns the quotient of two i-bit numbers. | quot (i:ℕ) : prim (bv i .→ bv i .→ bv i) -- `(rem i)` returns the remainder of two i-bit numbers. | rem (i:ℕ) : prim (bv i .→ bv i .→ bv i) -- `(squot i)` returns the signed quotient of two i-bit numbers. | squot (i:ℕ) : prim (bv i .→ bv i .→ bv i) -- `(srem i)` returns the signed remainder of two i-bit numbers. | srem (i:ℕ) : prim (bv i .→ bv i .→ bv i) -- `(slice w u l)` takes bits `u` through `l` out of a `w`-bit number. | slice (w:ℕ) (u:ℕ) (l:ℕ) : prim (bv w .→ bv (u+1-l)) -- `(sext i o)` sign extends an `i`-bit number to a `o`-bit number. | sext (i:ℕ) (o:ℕ) : prim (bv i .→ bv o) -- `(uext i o)` unsigned extension of an `i`-bit number to a `o`-bit number. | uext (i:ℕ) (o:ℕ) : prim (bv i .→ bv o) -- `(trunc i o)` truncates an `i`-bit number to a `o`-bit number. | trunc (i:ℕ) (o:ℕ) : prim (bv i .→ bv o) -- `(bsf i)` returns the index of least-siginifant bit that is 1. | bsf (i:ℕ) : prim (bv i .→ bv i) -- `(bsr i)` returns the index of most-siginifant bit that is 1. | bsr (i:ℕ) : prim (bv i .→ bv i) -- `(bswap i)` reverses the bytes in the bitvector. | bswap (i:ℕ) : prim (bv i .→ bv i) -- `zero` is the zero bit | zero : prim bit -- `one` is the one bit | one : prim bit -- `(eq tp)` returns `true` if two values are equal. | eq (tp:type) : prim (tp .→ tp .→ bit) -- `(neq tp)` returns `true` if two values are not equal. | neq (tp:type) : prim (tp .→ tp .→ bit) -- `(neg tp)` Two's Complement negation. | neg (i:ℕ) : prim (bv i .→ bv i) -- `x87_fadd` adds two extended precision values using the flags in the x87 register. | x87_fadd : prim (x86_80 .→ x86_80 .→ x86_80) -- `float_to_x86_80` converts a float to an extended precision number (lossless) | float_to_x86_80 : prim (float .→ x86_80) -- `double_to_x86_80` converts a double to an extended precision number (lossless) | double_to_x86_80 : prim (double .→ x86_80) -- `bv_to_x86_80` converts a bitvector to an extended precision number (lossless) | bv_to_x86_80 (w : one_of [16,32]) : prim (bv w .→ x86_80) -- `bvnat` constructs a bit vector from a natural number. | bvnat (w:ℕ) : ℕ → prim (bv w) -- `(bvadd i)` adds two i-bit bitvectors. | bvadd (i:ℕ) : prim (bv i .→ bv i .→ bv i) -- `(bvsub i)` substracts two i-bit bitvectors. | bvsub (i:ℕ) : prim (bv i .→ bv i .→ bv i) -- `(ssbb_overflows i)` true if signed sub overflows, the bit -- is a borrow bit. | ssbb_overflows (i:ℕ) : prim (bv i .→ bv i .→ bit .→ bit) -- `(usbb_overflows i)` true if unsigned sub overflows, -- the bit is a borrow bit. | usbb_overflows (i:ℕ) : prim (bv i .→ bv i .→ bit .→ bit) | uadc_overflows (i:ℕ) : prim (bv i .→ bv i .→ bit .→ bit) | sadc_overflows (i:ℕ) : prim (bv i .→ bv i .→ bit .→ bit) | and (i:ℕ) : prim (bv i .→ bv i .→ bv i) | or (i:ℕ) : prim (bv i .→ bv i .→ bv i) | xor (i:ℕ) : prim (bv i .→ bv i .→ bv i) | shl (i:ℕ) : prim (bv i .→ bv i .→ bv i) -- `(bvbit i)` interprets the second argument as a bit index and returns -- that bit from the first argument. | bvbit (i:ℕ) : prim (bv i .→ bv i .→ bit) | complement (i:ℕ) : prim (bv i .→ bv i) | bvcat (i:ℕ) : prim (bv i .→ bv i .→ bv (2*i)) | bv_least_nibble (i:ℕ) : prim (bv i .→ bv 4) | msb (i:ℕ) : prim (bv i .→ bit) | least_byte (i:ℕ) : prim (bv i .→ bv 8) | even_parity (i:ℕ) : prim (bv i .→ bit) namespace prim def pp : Π{tp:type}, prim tp → string | ._ (add i) := "add " ++ i.pp | ._ (adc i) := "adc " ++ i.pp | ._ (mul i) := "mul " ++ i.pp | ._ (quot i) := "quot " ++ i.pp | ._ (rem i) := "rem " ++ i.pp | ._ (squot i) := "squot " ++ i.pp | ._ (srem i) := "srem " ++ i.pp | ._ (slice w u l) := "slice " ++ w.pp ++ " " ++ u.pp ++ " " ++ l.pp | ._ (sext i o) := "sext " ++ i.pp ++ " " ++ o.pp | ._ (uext i o) := "uext " ++ i.pp ++ " " ++ o.pp | ._ (trunc i o) := "trunc " ++ i.pp ++ " " ++ o.pp | ._ (bsf i) := "bsf " ++ i.pp | ._ (bsr i) := "bsr " ++ i.pp | ._ (bswap i) := "bswap " ++ i.pp | ._ zero := sexp.app "bit" ["0"] | ._ one := sexp.app "bit" ["1"] | ._ (eq tp) := "eq " ++ tp.pp | ._ (neq tp) := "neq " ++ tp.pp | ._ (neg tp) := "neg " ++ tp.pp | ._ x87_fadd := "x87_fadd" | ._ float_to_x86_80 := "float_to_x86_80" | ._ double_to_x86_80 := "double_to_X86_80" | ._ (bv_to_x86_80 w) := "sext " ++ w.pp | ._ (bvnat w n) := sexp.app "bvnat" [w.pp, n.pp] | ._ (bvadd i) := "bvadd " ++ i.pp | ._ (bvsub i) := "bvsub " ++ i.pp | ._ (ssbb_overflows i) := "ssbb_overflows " ++ i.pp | ._ (usbb_overflows i) := "usbb_overflows " ++ i.pp | ._ (uadc_overflows i) := "uadc_overflows " ++ i.pp | ._ (sadc_overflows i) := "sadc_overflows " ++ i.pp | ._ (and i) := "and " ++ i.pp | ._ (or i) := "or " ++ i.pp | ._ (xor i) := "xor " ++ i.pp | ._ (shl i) := "shl " ++ i.pp | ._ (bvbit i) := "bvbit " ++ i.pp | ._ (complement i) := "complement " ++ i.pp | ._ (bvcat i) := "bvcat " ++ i.pp | ._ (bv_least_nibble i) := "bv_least_nibble" ++ i.pp | ._ (msb i) := "msb " ++ i.pp | ._ (least_byte i) := "least_byte " ++ i.pp | ._ (even_parity i) := "even_parity " ++ i.pp end prim -- Type for expressions. inductive expression : type → Type -- Create a expression our of a primitive | primitive {rtp:type} (o:prim rtp) : expression rtp -- Apply a function to an argument. | app {rtp:type} {tp:type} (f : expression (type.fn tp rtp)) (a : expression tp) : expression rtp -- Get the expression associated with the assignable expression. | get {tp:type} (l:lhs tp) : expression tp -- Return the expression in the local variable at the given index. | get_local (idx:ℕ) (tp:type) : expression tp namespace expression instance (rtp:type) : has_coe (prim rtp) (expression rtp) := ⟨expression.primitive⟩ instance (a:type) (f:type) : has_coe_to_fun (expression (type.fn a f)) := { F := λ_, Π(y:expression a), expression f , coe := app } def bvadd : Π{w:ℕ}, expression (bv w) → expression (bv w) → expression (bv w) | ._ (primitive (prim.bvnat ._ n)) (primitive (prim.bvnat w m)) := prim.bvnat w (n + m) | i x y := prim.bvadd i x y def bvsub : Π{w:ℕ}, expression (bv w) → expression (bv w) → expression (bv w) | ._ (primitive (prim.bvnat ._ n)) (primitive (prim.bvnat w m)) := prim.bvnat w (n - m) | i x y := prim.bvsub i x y def bvneg : Π{w:ℕ}, expression (bv w) → expression (bv w) | _ x := app (primitive (prim.neg _)) x instance (w:ℕ) : has_zero (expression (bv w)) := ⟨prim.bvnat w 0⟩ instance (w:ℕ) : has_one (expression (bv w)) := ⟨prim.bvnat w 1⟩ instance (w:ℕ) : has_add (expression (bv w)) := ⟨bvadd⟩ instance (w:ℕ) : has_sub (expression (bv w)) := ⟨bvsub⟩ instance (w:ℕ) : has_neg (expression (bv w)) := ⟨bvneg⟩ def adc {w:ℕ} (x : expression (bv w)) (y : expression (bv w)) (b : expression bit) : expression (bv w) := prim.adc w x y b def bswap {w:ℕ} (v : expression (bv w)) : expression (bv w) := prim.bswap w v def quot {w:ℕ} (x : expression (bv w)) (y : expression (bv w)) : expression (bv w) := prim.quot w x y def rem {w:ℕ} (x : expression (bv w)) (y : expression (bv w)) : expression (bv w) := prim.rem w x y def signed_quot {w:ℕ} (x : expression (bv w)) (y : expression (bv w)) : expression (bv w) := prim.squot w x y def signed_rem {w:ℕ} (x : expression (bv w)) (y : expression (bv w)) : expression (bv w) := prim.srem w x y protected def is_app : Π{tp:type}, expression tp → bool | ._ (app _ _) := tt | _ _ := ff protected def pp_args : Π{tp:type}, expression tp → string | ._ (primitive o) := o.pp | ._ (app f a) := f.pp_args ++ " " ++ paren_if a.is_app a.pp_args | ._ (get lhs) := lhs.repr | ._ (get_local idx tp) := sexp.app "local" [idx.pp] protected def pp {tp:type} (v:expression tp) := paren_if v.is_app v.pp_args instance (tp:type) : has_repr (expression tp) := ⟨expression.pp⟩ instance addr_is_expression (tp:type) : has_coe (addr tp) (expression tp) := ⟨ expression.get ∘ lhs.addr ⟩ instance type_is_sort : has_coe_to_sort type := ⟨Type, expression⟩ instance all_lhs_is_expression : has_coe1 lhs expression := ⟨λ_, expression.get⟩ instance lhs_is_expression (tp:type) : has_coe (lhs tp) (expression tp) := ⟨expression.get⟩ end expression -- Operations on expressions def slice {w:nat_expr} (x:expression (bv w)) (u:nat_expr) (l:nat_expr) : expression (bv (u+1-l)) := prim.slice w u l x def trunc {w:nat_expr} (x: bv w) (o:nat_expr) : bv o := prim.trunc w o x def bsf {w:nat_expr} (x: bv w) : bv w := prim.bsf w x def bsr {w:nat_expr} (x: bv w) : bv w := prim.bsr w x def sext {w:nat_expr} (x: bv w) (o:nat_expr) : bv o := prim.sext w o x def uext {w:nat_expr} (x: bv w) (o:nat_expr) : bv o := prim.uext w o x def neq {tp:type} (x y : tp) : bit := prim.neq tp x y def eq {tp:type} (x y : tp) : bit := prim.eq tp x y def one : bit := prim.one def zero : bit := prim.zero instance bv_has_mul (w:nat_expr) : has_mul (bv w) := ⟨λx y, prim.mul w x y⟩ -- Add two 80-bit numbers using the current x87 floating point control. def x87_fadd (x y : x86_80) : x86_80 := prim.x87_fadd x y instance float_extends_to_80 : has_coe float x86_80 := ⟨prim.float_to_x86_80⟩ instance double_extends_to_80 : has_coe double x86_80 := ⟨prim.double_to_x86_80⟩ -- These are lossless conversions. instance bv_to_x86_80 (w:one_of [16,32]) : has_coe (bv w) x86_80 := ⟨prim.bv_to_x86_80 w⟩ ------------------------------------------------------------------------ -- event -- These are a type of action that may have side effects, but do -- not return values. inductive event | syscall : event | unsupported (msg:string) : event | pop_x87_register_stack : event | call (addr: bv 64) : event | jmp (addr: bv 64) : event | ret : event | hlt : event | xchg {w : nat_expr} (addr1: bv w) (addr2: bv w) : event namespace event protected def pp : event → string | syscall := "(syscall)" | (unsupported msg) := "(unsupported " ++ msg ++ ")" | pop_x87_register_stack := "(pop_x87_register_stack)" | (call addr) := "(call " ++ addr.pp ++ ")" | (jmp addr) := "(jmp " ++ addr.pp ++ ")" | ret := "(ret)" | hlt := "(hlt)" | (xchg addr1 addr2) := "(xchg " ++ addr1.pp ++ " " ++ addr2.pp ++ ")" end event ------------------------------------------------------------------------ -- action -- Denotes updates to program state from register. inductive action | set {tp:type} (l:lhs tp) (v:expression tp) : action | local_def {tp:type} (idx:ℕ) (v:expression tp) : action | event (e:event) : action | mk_undef {tp:type} (l:lhs tp) : action namespace action protected def repr : action → string | (set l r) := sexp.app "set" [l.repr, r.pp] | (local_def idx v) := sexp.app "var" [idx.pp, v.pp] | (event e) := e.pp | (mk_undef v) := sexp.app "mk_undef" [v.repr] end action ------------------------------------------------------------------------ -- binding inductive binding | one_of : list nat → binding | lhs : type → binding | expression : type → binding namespace binding def pp : binding → string | (one_of l) := sexp.app "one_of" (nat.repr <$> l) | (lhs tp) := sexp.app "lhs" [tp.pp] | (expression tp) := sexp.app "expression" [tp.pp] end binding ------------------------------------------------------------------------ -- context structure context := (bindings : list binding) def context.length (c:context) : arg_index := c.bindings.length def context.add (b:binding) (ctx:context) : context := { bindings := b :: ctx.bindings } instance : has_insert binding context := ⟨context.add⟩ instance : has_emptyc context := ⟨{bindings := []}⟩ ------------------------------------------------------------------------ -- Patterns structure pattern := (context : context) (actions : list action) namespace pattern private def pp_bindings : nat → list binding → string | i [] := "" | i (b::r) := sexp.indent (sexp.indent (sexp.app "arg" [i.repr, b.pp] ++ "\n")) ++ pp_bindings (i+1) r private def pp_action (m:action) : string := sexp.indent (sexp.indent m.repr) protected def pp (p:pattern) : string := "(pattern\n" ++ pp_bindings 0 p.context.bindings.reverse ++ unlines (pp_action <$> p.actions) ++ sexp.indent ")" end pattern ------------------------------------------------------------------------ -- instruction structure instruction := (mnemonic:string) (patterns:list pattern) namespace instruction def repr (i:instruction) : string := "(instruction " ++ i.mnemonic ++ "\n" ++ unlines (sexp.indent <$> pattern.pp <$> i.patterns) ++ ")" instance : has_repr instruction := ⟨instruction.repr⟩ end instruction ------------------------------------------------------------------------ -- is_bound_var -- Class for types that may be used as arguments in defining semantics. class is_bound_var (tp:Type) := (to_binding{} : binding) (mk_arg{} : arg_index → tp) instance one_of_is_bound_var (range:list nat) : is_bound_var (one_of range) := { to_binding := binding.one_of range , mk_arg := one_of.var } instance lhs_is_bound_var (tp:type) : is_bound_var (lhs tp) := { to_binding := binding.lhs tp , mk_arg := λi, lhs.arg i tp } instance expression_is_bound_var (tp:type) : is_bound_var (expression tp) := { to_binding := binding.expression tp , mk_arg := λi, expression.get (lhs.arg i tp) } ------------------------------------------------------------------------ -- semantics structure semantics_state : Type := -- Actions seen so far in reverse order. (actions : list action) -- Number of local constants to use. (local_variable_count : ℕ) namespace semantics_state def init : semantics_state := { actions := [] , local_variable_count := 0 } end semantics_state structure semantics (α:Type) := (monad : state semantics_state α) instance : monad semantics := { pure := λ_ x, { monad := pure x } , bind := λ_ _ m h, { monad := m.monad >>= λv, (h v).monad } , map := λ_ _ f m, { monad := f <$> m.monad } } namespace semantics --- Get the index to use for the next local variable. protected def next_local_index : semantics ℕ := { monad := do s ← state_t.get, state_t.put {s with local_variable_count := s.local_variable_count + 1 }, return s.local_variable_count } --- Add an action to the list of actions. protected def add_action (e:action) : semantics unit := { monad := state_t.modify (λs, { s with actions := e :: s.actions}) } def record_event (e:event) : semantics unit := semantics.add_action (action.event e) -- Record that some code path is unsupported. def unsupported (msg:string) := record_event (event.unsupported msg) --- Set the expression of the left-hand side to the expression. def set {tp:type} (l:lhs tp) (v:expression tp) : semantics unit := semantics.add_action (action.set l v) --- Evaluate the given expression and return a local expression that will not mutate. def eval {tp : type} (v:expression tp) : semantics (expression tp) := do idx ← semantics.next_local_index, semantics.add_action (action.local_def idx v), return (expression.get_local idx tp) protected def run (m:semantics unit) : list action := do (m.monad.run semantics_state.init).snd.actions.reverse end semantics ------------------------------------------------------------------------ -- pattern_def -- Class for functions of form λ... -> semantics unit -- -- This is used to define patterns with lambdas to bind arguments. The context variable -- is needed so that we can infer how many variables have been bound outside of the -- current context. class pattern_def (ctx : context) (tp:Type) := { define{} : tp → pattern } instance semantics_is_pattern_def (ctx : context) : pattern_def ctx (semantics unit) := { define := λm, { context := ctx , actions := semantics.run m } } instance pi_is_pattern_def (tp:Type) [is_bound_var tp] (ctx:context) (β:tp → Type) [pattern_def (insert (is_bound_var.to_binding tp) ctx) (β (is_bound_var.mk_arg ctx.length))] : pattern_def ctx (Π(w: tp), β w) := { define := λf, do pattern_def.define (insert (is_bound_var.to_binding tp) ctx) (f (is_bound_var.mk_arg ctx.length)) } -- Contains a list of patten matches defined using a monadic syntax. def pattern_list : Type → Type := state (list pattern) instance pattern_list_is_monad : monad pattern_list := begin simp [pattern_list], apply_instance, end -- Record pattern in current instruction def mk_pattern {α:Type} [h : pattern_def ∅ α] (x:α) : pattern_list unit := do state_t.modify (list.cons (pattern_def.define ∅ x)) ------------------------------------------------------------------------ -- definst def definst (mnem:string) (pat: pattern_list unit) : instruction := { mnemonic := mnem , patterns := (pat.run []).snd.reverse } end x86
c9cc47ccbfa9247ec8592dbac9483706e86eb91f
1abd1ed12aa68b375cdef28959f39531c6e95b84
/src/ring_theory/ideal/operations.lean
b3d1b9ef509f15baeedb8025c3ce5f00b331cd80
[ "Apache-2.0" ]
permissive
jumpy4/mathlib
d3829e75173012833e9f15ac16e481e17596de0f
af36f1a35f279f0e5b3c2a77647c6bf2cfd51a13
refs/heads/master
1,693,508,842,818
1,636,203,271,000
1,636,203,271,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
73,153
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import algebra.algebra.operations import algebra.algebra.tower import data.equiv.ring import data.nat.choose.sum import ring_theory.ideal.quotient import ring_theory.non_zero_divisors /-! # More operations on modules and ideals -/ universes u v w x open_locale big_operators pointwise namespace submodule variables {R : Type u} {M : Type v} section comm_semiring variables [comm_semiring R] [add_comm_monoid M] [module R M] open_locale pointwise instance has_scalar' : has_scalar (ideal R) (submodule R M) := ⟨λ I N, ⨆ r : I, (r : R) • N⟩ /-- `N.annihilator` is the ideal of all elements `r : R` such that `r • N = 0`. -/ def annihilator (N : submodule R M) : ideal R := (linear_map.lsmul R N).ker variables {I J : ideal R} {N P : submodule R M} theorem mem_annihilator {r} : r ∈ N.annihilator ↔ ∀ n ∈ N, r • n = (0:M) := ⟨λ hr n hn, congr_arg subtype.val (linear_map.ext_iff.1 (linear_map.mem_ker.1 hr) ⟨n, hn⟩), λ h, linear_map.mem_ker.2 $ linear_map.ext $ λ n, subtype.eq $ h n.1 n.2⟩ theorem mem_annihilator' {r} : r ∈ N.annihilator ↔ N ≤ comap (r • linear_map.id) ⊥ := mem_annihilator.trans ⟨λ H n hn, (mem_bot R).2 $ H n hn, λ H n hn, (mem_bot R).1 $ H hn⟩ theorem annihilator_bot : (⊥ : submodule R M).annihilator = ⊤ := (ideal.eq_top_iff_one _).2 $ mem_annihilator'.2 bot_le theorem annihilator_eq_top_iff : N.annihilator = ⊤ ↔ N = ⊥ := ⟨λ H, eq_bot_iff.2 $ λ (n:M) hn, (mem_bot R).2 $ one_smul R n ▸ mem_annihilator.1 ((ideal.eq_top_iff_one _).1 H) n hn, λ H, H.symm ▸ annihilator_bot⟩ theorem annihilator_mono (h : N ≤ P) : P.annihilator ≤ N.annihilator := λ r hrp, mem_annihilator.2 $ λ n hn, mem_annihilator.1 hrp n $ h hn theorem annihilator_supr (ι : Sort w) (f : ι → submodule R M) : (annihilator ⨆ i, f i) = ⨅ i, annihilator (f i) := le_antisymm (le_infi $ λ i, annihilator_mono $ le_supr _ _) (λ r H, mem_annihilator'.2 $ supr_le $ λ i, have _ := (mem_infi _).1 H i, mem_annihilator'.1 this) theorem smul_mem_smul {r} {n} (hr : r ∈ I) (hn : n ∈ N) : r • n ∈ I • N := (le_supr _ ⟨r, hr⟩ : _ ≤ I • N) ⟨n, hn, rfl⟩ theorem smul_le {P : submodule R M} : I • N ≤ P ↔ ∀ (r ∈ I) (n ∈ N), r • n ∈ P := ⟨λ H r hr n hn, H $ smul_mem_smul hr hn, λ H, supr_le $ λ r, map_le_iff_le_comap.2 $ λ n hn, H r.1 r.2 n hn⟩ @[elab_as_eliminator] theorem smul_induction_on {p : M → Prop} {x} (H : x ∈ I • N) (Hb : ∀ (r ∈ I) (n ∈ N), p (r • n)) (H0 : p 0) (H1 : ∀ x y, p x → p y → p (x + y)) (H2 : ∀ (c:R) n, p n → p (c • n)) : p x := (@smul_le _ _ _ _ _ _ _ ⟨p, H0, H1, H2⟩).2 Hb H theorem mem_smul_span_singleton {I : ideal R} {m : M} {x : M} : x ∈ I • span R ({m} : set M) ↔ ∃ y ∈ I, y • m = x := ⟨λ hx, smul_induction_on hx (λ r hri n hnm, let ⟨s, hs⟩ := mem_span_singleton.1 hnm in ⟨r * s, I.mul_mem_right _ hri, hs ▸ mul_smul r s m⟩) ⟨0, I.zero_mem, by rw [zero_smul]⟩ (λ m1 m2 ⟨y1, hyi1, hy1⟩ ⟨y2, hyi2, hy2⟩, ⟨y1 + y2, I.add_mem hyi1 hyi2, by rw [add_smul, hy1, hy2]⟩) (λ c r ⟨y, hyi, hy⟩, ⟨c * y, I.mul_mem_left _ hyi, by rw [mul_smul, hy]⟩), λ ⟨y, hyi, hy⟩, hy ▸ smul_mem_smul hyi (subset_span $ set.mem_singleton m)⟩ theorem smul_le_right : I • N ≤ N := smul_le.2 $ λ r hr n, N.smul_mem r theorem smul_mono (hij : I ≤ J) (hnp : N ≤ P) : I • N ≤ J • P := smul_le.2 $ λ r hr n hn, smul_mem_smul (hij hr) (hnp hn) theorem smul_mono_left (h : I ≤ J) : I • N ≤ J • N := smul_mono h (le_refl N) theorem smul_mono_right (h : N ≤ P) : I • N ≤ I • P := smul_mono (le_refl I) h @[simp] theorem annihilator_smul (N : submodule R M) : annihilator N • N = ⊥ := eq_bot_iff.2 (smul_le.2 (λ r, mem_annihilator.1)) @[simp] theorem annihilator_mul (I : ideal R) : annihilator I * I = ⊥ := annihilator_smul I @[simp] theorem mul_annihilator (I : ideal R) : I * annihilator I = ⊥ := by rw [mul_comm, annihilator_mul] variables (I J N P) @[simp] theorem smul_bot : I • (⊥ : submodule R M) = ⊥ := eq_bot_iff.2 $ smul_le.2 $ λ r hri s hsb, (submodule.mem_bot R).2 $ ((submodule.mem_bot R).1 hsb).symm ▸ smul_zero r @[simp] theorem bot_smul : (⊥ : ideal R) • N = ⊥ := eq_bot_iff.2 $ smul_le.2 $ λ r hrb s hsi, (submodule.mem_bot R).2 $ ((submodule.mem_bot R).1 hrb).symm ▸ zero_smul _ s @[simp] theorem top_smul : (⊤ : ideal R) • N = N := le_antisymm smul_le_right $ λ r hri, one_smul R r ▸ smul_mem_smul mem_top hri theorem smul_sup : I • (N ⊔ P) = I • N ⊔ I • P := le_antisymm (smul_le.2 $ λ r hri m hmnp, let ⟨n, hn, p, hp, hnpm⟩ := mem_sup.1 hmnp in mem_sup.2 ⟨_, smul_mem_smul hri hn, _, smul_mem_smul hri hp, hnpm ▸ (smul_add _ _ _).symm⟩) (sup_le (smul_mono_right le_sup_left) (smul_mono_right le_sup_right)) theorem sup_smul : (I ⊔ J) • N = I • N ⊔ J • N := le_antisymm (smul_le.2 $ λ r hrij n hn, let ⟨ri, hri, rj, hrj, hrijr⟩ := mem_sup.1 hrij in mem_sup.2 ⟨_, smul_mem_smul hri hn, _, smul_mem_smul hrj hn, hrijr ▸ (add_smul _ _ _).symm⟩) (sup_le (smul_mono_left le_sup_left) (smul_mono_left le_sup_right)) protected theorem smul_assoc : (I • J) • N = I • (J • N) := le_antisymm (smul_le.2 $ λ rs hrsij t htn, smul_induction_on hrsij (λ r hr s hs, (@smul_eq_mul R _ r s).symm ▸ smul_smul r s t ▸ smul_mem_smul hr (smul_mem_smul hs htn)) ((zero_smul R t).symm ▸ submodule.zero_mem _) (λ x y, (add_smul x y t).symm ▸ submodule.add_mem _) (λ r s h, (@smul_eq_mul R _ r s).symm ▸ smul_smul r s t ▸ submodule.smul_mem _ _ h)) (smul_le.2 $ λ r hr sn hsn, suffices J • N ≤ submodule.comap (r • linear_map.id) ((I • J) • N), from this hsn, smul_le.2 $ λ s hs n hn, show r • (s • n) ∈ (I • J) • N, from mul_smul r s n ▸ smul_mem_smul (smul_mem_smul hr hs) hn) variables (S : set R) (T : set M) theorem span_smul_span : (ideal.span S) • (span R T) = span R (⋃ (s ∈ S) (t ∈ T), {s • t}) := le_antisymm (smul_le.2 $ λ r hrS n hnT, span_induction hrS (λ r hrS, span_induction hnT (λ n hnT, subset_span $ set.mem_bUnion hrS $ set.mem_bUnion hnT $ set.mem_singleton _) ((smul_zero r : r • 0 = (0:M)).symm ▸ submodule.zero_mem _) (λ x y, (smul_add r x y).symm ▸ submodule.add_mem _) (λ c m, by rw [smul_smul, mul_comm, mul_smul]; exact submodule.smul_mem _ _)) ((zero_smul R n).symm ▸ submodule.zero_mem _) (λ r s, (add_smul r s n).symm ▸ submodule.add_mem _) (λ c r, by rw [smul_eq_mul, mul_smul]; exact submodule.smul_mem _ _)) $ span_le.2 $ set.bUnion_subset $ λ r hrS, set.bUnion_subset $ λ n hnT, set.singleton_subset_iff.2 $ smul_mem_smul (subset_span hrS) (subset_span hnT) variables {M' : Type w} [add_comm_monoid M'] [module R M'] theorem map_smul'' (f : M →ₗ[R] M') : (I • N).map f = I • N.map f := le_antisymm (map_le_iff_le_comap.2 $ smul_le.2 $ λ r hr n hn, show f (r • n) ∈ I • N.map f, from (f.map_smul r n).symm ▸ smul_mem_smul hr (mem_map_of_mem hn)) $ smul_le.2 $ λ r hr n hn, let ⟨p, hp, hfp⟩ := mem_map.1 hn in hfp ▸ f.map_smul r p ▸ mem_map_of_mem (smul_mem_smul hr hp) end comm_semiring section comm_ring variables [comm_ring R] [add_comm_group M] [module R M] variables {N N₁ N₂ P P₁ P₂ : submodule R M} /-- `N.colon P` is the ideal of all elements `r : R` such that `r • P ⊆ N`. -/ def colon (N P : submodule R M) : ideal R := annihilator (P.map N.mkq) theorem mem_colon {r} : r ∈ N.colon P ↔ ∀ p ∈ P, r • p ∈ N := mem_annihilator.trans ⟨λ H p hp, (quotient.mk_eq_zero N).1 (H (quotient.mk p) (mem_map_of_mem hp)), λ H m ⟨p, hp, hpm⟩, hpm ▸ (N.mkq).map_smul r p ▸ (quotient.mk_eq_zero N).2 $ H p hp⟩ theorem mem_colon' {r} : r ∈ N.colon P ↔ P ≤ comap (r • linear_map.id) N := mem_colon theorem colon_mono (hn : N₁ ≤ N₂) (hp : P₁ ≤ P₂) : N₁.colon P₂ ≤ N₂.colon P₁ := λ r hrnp, mem_colon.2 $ λ p₁ hp₁, hn $ mem_colon.1 hrnp p₁ $ hp hp₁ theorem infi_colon_supr (ι₁ : Sort w) (f : ι₁ → submodule R M) (ι₂ : Sort x) (g : ι₂ → submodule R M) : (⨅ i, f i).colon (⨆ j, g j) = ⨅ i j, (f i).colon (g j) := le_antisymm (le_infi $ λ i, le_infi $ λ j, colon_mono (infi_le _ _) (le_supr _ _)) (λ r H, mem_colon'.2 $ supr_le $ λ j, map_le_iff_le_comap.1 $ le_infi $ λ i, map_le_iff_le_comap.2 $ mem_colon'.1 $ have _ := ((mem_infi _).1 H i), have _ := ((mem_infi _).1 this j), this) end comm_ring end submodule namespace ideal section mul_and_radical variables {R : Type u} {ι : Type*} [comm_semiring R] variables {I J K L : ideal R} instance : has_mul (ideal R) := ⟨(•)⟩ @[simp] lemma add_eq_sup : I + J = I ⊔ J := rfl @[simp] lemma zero_eq_bot : (0 : ideal R) = ⊥ := rfl @[simp] lemma one_eq_top : (1 : ideal R) = ⊤ := by erw [submodule.one_eq_range, linear_map.range_id] theorem mul_mem_mul {r s} (hr : r ∈ I) (hs : s ∈ J) : r * s ∈ I * J := submodule.smul_mem_smul hr hs theorem mul_mem_mul_rev {r s} (hr : r ∈ I) (hs : s ∈ J) : s * r ∈ I * J := mul_comm r s ▸ mul_mem_mul hr hs lemma pow_mem_pow {x : R} (hx : x ∈ I) (n : ℕ) : x ^ n ∈ I ^ n := begin induction n with n ih, { simp only [pow_zero, ideal.one_eq_top], }, simpa only [pow_succ] using mul_mem_mul hx ih, end theorem mul_le : I * J ≤ K ↔ ∀ (r ∈ I) (s ∈ J), r * s ∈ K := submodule.smul_le lemma mul_le_left : I * J ≤ J := ideal.mul_le.2 (λ r hr s, J.mul_mem_left _) lemma mul_le_right : I * J ≤ I := ideal.mul_le.2 (λ r hr s hs, I.mul_mem_right _ hr) @[simp] lemma sup_mul_right_self : I ⊔ (I * J) = I := sup_eq_left.2 ideal.mul_le_right @[simp] lemma sup_mul_left_self : I ⊔ (J * I) = I := sup_eq_left.2 ideal.mul_le_left @[simp] lemma mul_right_self_sup : (I * J) ⊔ I = I := sup_eq_right.2 ideal.mul_le_right @[simp] lemma mul_left_self_sup : (J * I) ⊔ I = I := sup_eq_right.2 ideal.mul_le_left variables (I J K) protected theorem mul_comm : I * J = J * I := le_antisymm (mul_le.2 $ λ r hrI s hsJ, mul_mem_mul_rev hsJ hrI) (mul_le.2 $ λ r hrJ s hsI, mul_mem_mul_rev hsI hrJ) protected theorem mul_assoc : (I * J) * K = I * (J * K) := submodule.smul_assoc I J K theorem span_mul_span (S T : set R) : span S * span T = span ⋃ (s ∈ S) (t ∈ T), {s * t} := submodule.span_smul_span S T variables {I J K} lemma span_mul_span' (S T : set R) : span S * span T = span (S*T) := by { unfold span, rw submodule.span_mul_span, } lemma span_singleton_mul_span_singleton (r s : R) : span {r} * span {s} = (span {r * s} : ideal R) := by { unfold span, rw [submodule.span_mul_span, set.singleton_mul_singleton], } lemma span_singleton_pow (s : R) (n : ℕ): span {s} ^ n = (span {s ^ n} : ideal R) := begin induction n with n ih, { simp [set.singleton_one], }, simp only [pow_succ, ih, span_singleton_mul_span_singleton], end lemma mem_mul_span_singleton {x y : R} {I : ideal R} : x ∈ I * span {y} ↔ ∃ z ∈ I, z * y = x := submodule.mem_smul_span_singleton lemma mem_span_singleton_mul {x y : R} {I : ideal R} : x ∈ span {y} * I ↔ ∃ z ∈ I, y * z = x := by simp only [mul_comm, mem_mul_span_singleton] lemma le_span_singleton_mul_iff {x : R} {I J : ideal R} : I ≤ span {x} * J ↔ ∀ zI ∈ I, ∃ zJ ∈ J, x * zJ = zI := show (∀ {zI} (hzI : zI ∈ I), zI ∈ span {x} * J) ↔ ∀ zI ∈ I, ∃ zJ ∈ J, x * zJ = zI, by simp only [mem_span_singleton_mul] lemma span_singleton_mul_le_iff {x : R} {I J : ideal R} : span {x} * I ≤ J ↔ ∀ z ∈ I, x * z ∈ J := begin simp only [mul_le, mem_span_singleton_mul, mem_span_singleton], split, { intros h zI hzI, exact h x (dvd_refl x) zI hzI }, { rintros h _ ⟨z, rfl⟩ zI hzI, rw [mul_comm x z, mul_assoc], exact J.mul_mem_left _ (h zI hzI) }, end lemma span_singleton_mul_le_span_singleton_mul {x y : R} {I J : ideal R} : span {x} * I ≤ span {y} * J ↔ ∀ zI ∈ I, ∃ zJ ∈ J, x * zI = y * zJ := by simp only [span_singleton_mul_le_iff, mem_span_singleton_mul, eq_comm] lemma eq_span_singleton_mul {x : R} (I J : ideal R) : I = span {x} * J ↔ ((∀ zI ∈ I, ∃ zJ ∈ J, x * zJ = zI) ∧ (∀ z ∈ J, x * z ∈ I)) := by simp only [le_antisymm_iff, le_span_singleton_mul_iff, span_singleton_mul_le_iff] lemma span_singleton_mul_eq_span_singleton_mul {x y : R} (I J : ideal R) : span {x} * I = span {y} * J ↔ ((∀ zI ∈ I, ∃ zJ ∈ J, x * zI = y * zJ) ∧ (∀ zJ ∈ J, ∃ zI ∈ I, x * zI = y * zJ)) := by simp only [le_antisymm_iff, span_singleton_mul_le_span_singleton_mul, eq_comm] theorem mul_le_inf : I * J ≤ I ⊓ J := mul_le.2 $ λ r hri s hsj, ⟨I.mul_mem_right s hri, J.mul_mem_left r hsj⟩ theorem multiset_prod_le_inf {s : multiset (ideal R)} : s.prod ≤ s.inf := begin classical, refine s.induction_on _ _, { rw [multiset.inf_zero], exact le_top }, intros a s ih, rw [multiset.prod_cons, multiset.inf_cons], exact le_trans mul_le_inf (inf_le_inf (le_refl _) ih) end theorem prod_le_inf {s : finset ι} {f : ι → ideal R} : s.prod f ≤ s.inf f := multiset_prod_le_inf theorem mul_eq_inf_of_coprime (h : I ⊔ J = ⊤) : I * J = I ⊓ J := le_antisymm mul_le_inf $ λ r ⟨hri, hrj⟩, let ⟨s, hsi, t, htj, hst⟩ := submodule.mem_sup.1 ((eq_top_iff_one _).1 h) in mul_one r ▸ hst ▸ (mul_add r s t).symm ▸ ideal.add_mem (I * J) (mul_mem_mul_rev hsi hrj) (mul_mem_mul hri htj) variables (I) @[simp] theorem mul_bot : I * ⊥ = ⊥ := submodule.smul_bot I @[simp] theorem bot_mul : ⊥ * I = ⊥ := submodule.bot_smul I @[simp] theorem mul_top : I * ⊤ = I := ideal.mul_comm ⊤ I ▸ submodule.top_smul I @[simp] theorem top_mul : ⊤ * I = I := submodule.top_smul I variables {I} theorem mul_mono (hik : I ≤ K) (hjl : J ≤ L) : I * J ≤ K * L := submodule.smul_mono hik hjl theorem mul_mono_left (h : I ≤ J) : I * K ≤ J * K := submodule.smul_mono_left h theorem mul_mono_right (h : J ≤ K) : I * J ≤ I * K := submodule.smul_mono_right h variables (I J K) theorem mul_sup : I * (J ⊔ K) = I * J ⊔ I * K := submodule.smul_sup I J K theorem sup_mul : (I ⊔ J) * K = I * K ⊔ J * K := submodule.sup_smul I J K variables {I J K} lemma pow_le_pow {m n : ℕ} (h : m ≤ n) : I^n ≤ I^m := begin cases nat.exists_eq_add_of_le h with k hk, rw [hk, pow_add], exact le_trans (mul_le_inf) (inf_le_left) end lemma mul_eq_bot {R : Type*} [comm_ring R] [is_domain R] {I J : ideal R} : I * J = ⊥ ↔ I = ⊥ ∨ J = ⊥ := ⟨λ hij, or_iff_not_imp_left.mpr (λ I_ne_bot, J.eq_bot_iff.mpr (λ j hj, let ⟨i, hi, ne0⟩ := I.ne_bot_iff.mp I_ne_bot in or.resolve_left (mul_eq_zero.mp ((I * J).eq_bot_iff.mp hij _ (mul_mem_mul hi hj))) ne0)), λ h, by cases h; rw [← ideal.mul_bot, h, ideal.mul_comm]⟩ instance {R : Type*} [comm_ring R] [is_domain R] : no_zero_divisors (ideal R) := { eq_zero_or_eq_zero_of_mul_eq_zero := λ I J, mul_eq_bot.1 } /-- A product of ideals in an integral domain is zero if and only if one of the terms is zero. -/ lemma prod_eq_bot {R : Type*} [comm_ring R] [is_domain R] {s : multiset (ideal R)} : s.prod = ⊥ ↔ ∃ I ∈ s, I = ⊥ := prod_zero_iff_exists_zero /-- The radical of an ideal `I` consists of the elements `r` such that `r^n ∈ I` for some `n`. -/ def radical (I : ideal R) : ideal R := { carrier := { r | ∃ n : ℕ, r ^ n ∈ I }, zero_mem' := ⟨1, (pow_one (0:R)).symm ▸ I.zero_mem⟩, add_mem' := λ x y ⟨m, hxmi⟩ ⟨n, hyni⟩, ⟨m + n, (add_pow x y (m + n)).symm ▸ I.sum_mem $ show ∀ c ∈ finset.range (nat.succ (m + n)), x ^ c * y ^ (m + n - c) * (nat.choose (m + n) c) ∈ I, from λ c hc, or.cases_on (le_total c m) (λ hcm, I.mul_mem_right _ $ I.mul_mem_left _ $ nat.add_comm n m ▸ (add_tsub_assoc_of_le hcm n).symm ▸ (pow_add y n (m-c)).symm ▸ I.mul_mem_right _ hyni) (λ hmc, I.mul_mem_right _ $ I.mul_mem_right _ $ add_tsub_cancel_of_le hmc ▸ (pow_add x m (c-m)).symm ▸ I.mul_mem_right _ hxmi)⟩, smul_mem' := λ r s ⟨n, hsni⟩, ⟨n, (mul_pow r s n).symm ▸ I.mul_mem_left (r^n) hsni⟩ } theorem le_radical : I ≤ radical I := λ r hri, ⟨1, (pow_one r).symm ▸ hri⟩ variables (R) theorem radical_top : (radical ⊤ : ideal R) = ⊤ := (eq_top_iff_one _).2 ⟨0, submodule.mem_top⟩ variables {R} theorem radical_mono (H : I ≤ J) : radical I ≤ radical J := λ r ⟨n, hrni⟩, ⟨n, H hrni⟩ variables (I) @[simp] theorem radical_idem : radical (radical I) = radical I := le_antisymm (λ r ⟨n, k, hrnki⟩, ⟨n * k, (pow_mul r n k).symm ▸ hrnki⟩) le_radical variables {I} theorem radical_le_radical_iff : radical I ≤ radical J ↔ I ≤ radical J := ⟨λ h, le_trans le_radical h, λ h, radical_idem J ▸ radical_mono h⟩ theorem radical_eq_top : radical I = ⊤ ↔ I = ⊤ := ⟨λ h, (eq_top_iff_one _).2 $ let ⟨n, hn⟩ := (eq_top_iff_one _).1 h in @one_pow R _ n ▸ hn, λ h, h.symm ▸ radical_top R⟩ theorem is_prime.radical (H : is_prime I) : radical I = I := le_antisymm (λ r ⟨n, hrni⟩, H.mem_of_pow_mem n hrni) le_radical variables (I J) theorem radical_sup : radical (I ⊔ J) = radical (radical I ⊔ radical J) := le_antisymm (radical_mono $ sup_le_sup le_radical le_radical) $ λ r ⟨n, hrnij⟩, let ⟨s, hs, t, ht, hst⟩ := submodule.mem_sup.1 hrnij in @radical_idem _ _ (I ⊔ J) ▸ ⟨n, hst ▸ ideal.add_mem _ (radical_mono le_sup_left hs) (radical_mono le_sup_right ht)⟩ theorem radical_inf : radical (I ⊓ J) = radical I ⊓ radical J := le_antisymm (le_inf (radical_mono inf_le_left) (radical_mono inf_le_right)) (λ r ⟨⟨m, hrm⟩, ⟨n, hrn⟩⟩, ⟨m + n, (pow_add r m n).symm ▸ I.mul_mem_right _ hrm, (pow_add r m n).symm ▸ J.mul_mem_left _ hrn⟩) theorem radical_mul : radical (I * J) = radical I ⊓ radical J := le_antisymm (radical_inf I J ▸ radical_mono $ @mul_le_inf _ _ I J) (λ r ⟨⟨m, hrm⟩, ⟨n, hrn⟩⟩, ⟨m + n, (pow_add r m n).symm ▸ mul_mem_mul hrm hrn⟩) variables {I J} theorem is_prime.radical_le_iff (hj : is_prime J) : radical I ≤ J ↔ I ≤ J := ⟨le_trans le_radical, λ hij r ⟨n, hrni⟩, hj.mem_of_pow_mem n $ hij hrni⟩ theorem radical_eq_Inf (I : ideal R) : radical I = Inf { J : ideal R | I ≤ J ∧ is_prime J } := le_antisymm (le_Inf $ λ J hJ, hJ.2.radical_le_iff.2 hJ.1) $ λ r hr, classical.by_contradiction $ λ hri, let ⟨m, (hrm : r ∉ radical m), him, hm⟩ := zorn.zorn_nonempty_partial_order₀ {K : ideal R | r ∉ radical K} (λ c hc hcc y hyc, ⟨Sup c, λ ⟨n, hrnc⟩, let ⟨y, hyc, hrny⟩ := (submodule.mem_Sup_of_directed ⟨y, hyc⟩ hcc.directed_on).1 hrnc in hc hyc ⟨n, hrny⟩, λ z, le_Sup⟩) I hri in have ∀ x ∉ m, r ∈ radical (m ⊔ span {x}) := λ x hxm, classical.by_contradiction $ λ hrmx, hxm $ hm (m ⊔ span {x}) hrmx le_sup_left ▸ (le_sup_right : _ ≤ m ⊔ span {x}) (subset_span $ set.mem_singleton _), have is_prime m, from ⟨by rintro rfl; rw radical_top at hrm; exact hrm trivial, λ x y hxym, or_iff_not_imp_left.2 $ λ hxm, classical.by_contradiction $ λ hym, let ⟨n, hrn⟩ := this _ hxm, ⟨p, hpm, q, hq, hpqrn⟩ := submodule.mem_sup.1 hrn, ⟨c, hcxq⟩ := mem_span_singleton'.1 hq in let ⟨k, hrk⟩ := this _ hym, ⟨f, hfm, g, hg, hfgrk⟩ := submodule.mem_sup.1 hrk, ⟨d, hdyg⟩ := mem_span_singleton'.1 hg in hrm ⟨n + k, by rw [pow_add, ← hpqrn, ← hcxq, ← hfgrk, ← hdyg, add_mul, mul_add (c*x), mul_assoc c x (d*y), mul_left_comm x, ← mul_assoc]; refine m.add_mem (m.mul_mem_right _ hpm) (m.add_mem (m.mul_mem_left _ hfm) (m.mul_mem_left _ hxym))⟩⟩, hrm $ this.radical.symm ▸ (Inf_le ⟨him, this⟩ : Inf {J : ideal R | I ≤ J ∧ is_prime J} ≤ m) hr @[simp] lemma radical_bot_of_is_domain {R : Type u} [comm_ring R] [is_domain R] : radical (⊥ : ideal R) = ⊥ := eq_bot_iff.2 (λ x hx, hx.rec_on (λ n hn, pow_eq_zero hn)) instance : comm_semiring (ideal R) := submodule.comm_semiring variables (R) theorem top_pow (n : ℕ) : (⊤ ^ n : ideal R) = ⊤ := nat.rec_on n one_eq_top $ λ n ih, by rw [pow_succ, ih, top_mul] variables {R} variables (I) theorem radical_pow (n : ℕ) (H : n > 0) : radical (I^n) = radical I := nat.rec_on n (not.elim dec_trivial) (λ n ih H, or.cases_on (lt_or_eq_of_le $ nat.le_of_lt_succ H) (λ H, calc radical (I^(n+1)) = radical I ⊓ radical (I^n) : by { rw pow_succ, exact radical_mul _ _ } ... = radical I ⊓ radical I : by rw ih H ... = radical I : inf_idem) (λ H, H ▸ (pow_one I).symm ▸ rfl)) H theorem is_prime.mul_le {I J P : ideal R} (hp : is_prime P) : I * J ≤ P ↔ I ≤ P ∨ J ≤ P := ⟨λ h, or_iff_not_imp_left.2 $ λ hip j hj, let ⟨i, hi, hip⟩ := set.not_subset.1 hip in (hp.mem_or_mem $ h $ mul_mem_mul hi hj).resolve_left hip, λ h, or.cases_on h (le_trans $ le_trans mul_le_inf inf_le_left) (le_trans $ le_trans mul_le_inf inf_le_right)⟩ theorem is_prime.inf_le {I J P : ideal R} (hp : is_prime P) : I ⊓ J ≤ P ↔ I ≤ P ∨ J ≤ P := ⟨λ h, hp.mul_le.1 $ le_trans mul_le_inf h, λ h, or.cases_on h (le_trans inf_le_left) (le_trans inf_le_right)⟩ theorem is_prime.multiset_prod_le {s : multiset (ideal R)} {P : ideal R} (hp : is_prime P) (hne : s ≠ 0) : s.prod ≤ P ↔ ∃ I ∈ s, I ≤ P := suffices s.prod ≤ P → ∃ I ∈ s, I ≤ P, from ⟨this, λ ⟨i, his, hip⟩, le_trans multiset_prod_le_inf $ le_trans (multiset.inf_le his) hip⟩, begin classical, obtain ⟨b, hb⟩ : ∃ b, b ∈ s := multiset.exists_mem_of_ne_zero hne, obtain ⟨t, rfl⟩ : ∃ t, s = b ::ₘ t, from ⟨s.erase b, (multiset.cons_erase hb).symm⟩, refine t.induction_on _ _, { simp only [exists_prop, ←multiset.singleton_eq_cons, multiset.prod_singleton, multiset.mem_singleton, exists_eq_left, imp_self] }, intros a s ih h, rw [multiset.cons_swap, multiset.prod_cons, hp.mul_le] at h, rw multiset.cons_swap, cases h, { exact ⟨a, multiset.mem_cons_self a _, h⟩ }, obtain ⟨I, hI, ih⟩ : ∃ I ∈ b ::ₘ s, I ≤ P := ih h, exact ⟨I, multiset.mem_cons_of_mem hI, ih⟩ end theorem is_prime.multiset_prod_map_le {s : multiset ι} (f : ι → ideal R) {P : ideal R} (hp : is_prime P) (hne : s ≠ 0) : (s.map f).prod ≤ P ↔ ∃ i ∈ s, f i ≤ P := begin rw hp.multiset_prod_le (mt multiset.map_eq_zero.mp hne), simp_rw [exists_prop, multiset.mem_map, exists_exists_and_eq_and], end theorem is_prime.prod_le {s : finset ι} {f : ι → ideal R} {P : ideal R} (hp : is_prime P) (hne : s.nonempty) : s.prod f ≤ P ↔ ∃ i ∈ s, f i ≤ P := hp.multiset_prod_map_le f (mt finset.val_eq_zero.mp hne.ne_empty) theorem is_prime.inf_le' {s : finset ι} {f : ι → ideal R} {P : ideal R} (hp : is_prime P) (hsne: s.nonempty) : s.inf f ≤ P ↔ ∃ i ∈ s, f i ≤ P := ⟨λ h, (hp.prod_le hsne).1 $ le_trans prod_le_inf h, λ ⟨i, his, hip⟩, le_trans (finset.inf_le his) hip⟩ theorem subset_union {R : Type u} [comm_ring R] {I J K : ideal R} : (I : set R) ⊆ J ∪ K ↔ I ≤ J ∨ I ≤ K := ⟨λ h, or_iff_not_imp_left.2 $ λ hij s hsi, let ⟨r, hri, hrj⟩ := set.not_subset.1 hij in classical.by_contradiction $ λ hsk, or.cases_on (h $ I.add_mem hri hsi) (λ hj, hrj $ add_sub_cancel r s ▸ J.sub_mem hj ((h hsi).resolve_right hsk)) (λ hk, hsk $ add_sub_cancel' r s ▸ K.sub_mem hk ((h hri).resolve_left hrj)), λ h, or.cases_on h (λ h, set.subset.trans h $ set.subset_union_left J K) (λ h, set.subset.trans h $ set.subset_union_right J K)⟩ theorem subset_union_prime' {R : Type u} [comm_ring R] {s : finset ι} {f : ι → ideal R} {a b : ι} (hp : ∀ i ∈ s, is_prime (f i)) {I : ideal R} : (I : set R) ⊆ f a ∪ f b ∪ (⋃ i ∈ (↑s : set ι), f i) ↔ I ≤ f a ∨ I ≤ f b ∨ ∃ i ∈ s, I ≤ f i := suffices (I : set R) ⊆ f a ∪ f b ∪ (⋃ i ∈ (↑s : set ι), f i) → I ≤ f a ∨ I ≤ f b ∨ ∃ i ∈ s, I ≤ f i, from ⟨this, λ h, or.cases_on h (λ h, set.subset.trans h $ set.subset.trans (set.subset_union_left _ _) (set.subset_union_left _ _)) $ λ h, or.cases_on h (λ h, set.subset.trans h $ set.subset.trans (set.subset_union_right _ _) (set.subset_union_left _ _)) $ λ ⟨i, his, hi⟩, by refine (set.subset.trans hi $ set.subset.trans _ $ set.subset_union_right _ _); exact set.subset_bUnion_of_mem (finset.mem_coe.2 his)⟩, begin generalize hn : s.card = n, intros h, unfreezingI { induction n with n ih generalizing a b s }, { clear hp, rw finset.card_eq_zero at hn, subst hn, rw [finset.coe_empty, set.bUnion_empty, set.union_empty, subset_union] at h, simpa only [exists_prop, finset.not_mem_empty, false_and, exists_false, or_false] }, classical, replace hn : ∃ (i : ι) (t : finset ι), i ∉ t ∧ insert i t = s ∧ t.card = n := finset.card_eq_succ.1 hn, unfreezingI { rcases hn with ⟨i, t, hit, rfl, hn⟩ }, replace hp : is_prime (f i) ∧ ∀ x ∈ t, is_prime (f x) := (t.forall_mem_insert _ _).1 hp, by_cases Ht : ∃ j ∈ t, f j ≤ f i, { obtain ⟨j, hjt, hfji⟩ : ∃ j ∈ t, f j ≤ f i := Ht, obtain ⟨u, hju, rfl⟩ : ∃ u, j ∉ u ∧ insert j u = t, { exact ⟨t.erase j, t.not_mem_erase j, finset.insert_erase hjt⟩ }, have hp' : ∀ k ∈ insert i u, is_prime (f k), { rw finset.forall_mem_insert at hp ⊢, exact ⟨hp.1, hp.2.2⟩ }, have hiu : i ∉ u := mt finset.mem_insert_of_mem hit, have hn' : (insert i u).card = n, { rwa finset.card_insert_of_not_mem at hn ⊢, exacts [hiu, hju] }, have h' : (I : set R) ⊆ f a ∪ f b ∪ (⋃ k ∈ (↑(insert i u) : set ι), f k), { rw finset.coe_insert at h ⊢, rw finset.coe_insert at h, simp only [set.bUnion_insert] at h ⊢, rw [← set.union_assoc ↑(f i)] at h, erw [set.union_eq_self_of_subset_right hfji] at h, exact h }, specialize @ih a b (insert i u) hp' hn' h', refine ih.imp id (or.imp id (exists_imp_exists $ λ k, _)), simp only [exists_prop], exact and.imp (λ hk, finset.insert_subset_insert i (finset.subset_insert j u) hk) id }, by_cases Ha : f a ≤ f i, { have h' : (I : set R) ⊆ f i ∪ f b ∪ (⋃ j ∈ (↑t : set ι), f j), { rw [finset.coe_insert, set.bUnion_insert, ← set.union_assoc, set.union_right_comm ↑(f a)] at h, erw [set.union_eq_self_of_subset_left Ha] at h, exact h }, specialize @ih i b t hp.2 hn h', right, rcases ih with ih | ih | ⟨k, hkt, ih⟩, { exact or.inr ⟨i, finset.mem_insert_self i t, ih⟩ }, { exact or.inl ih }, { exact or.inr ⟨k, finset.mem_insert_of_mem hkt, ih⟩ } }, by_cases Hb : f b ≤ f i, { have h' : (I : set R) ⊆ f a ∪ f i ∪ (⋃ j ∈ (↑t : set ι), f j), { rw [finset.coe_insert, set.bUnion_insert, ← set.union_assoc, set.union_assoc ↑(f a)] at h, erw [set.union_eq_self_of_subset_left Hb] at h, exact h }, specialize @ih a i t hp.2 hn h', rcases ih with ih | ih | ⟨k, hkt, ih⟩, { exact or.inl ih }, { exact or.inr (or.inr ⟨i, finset.mem_insert_self i t, ih⟩) }, { exact or.inr (or.inr ⟨k, finset.mem_insert_of_mem hkt, ih⟩) } }, by_cases Hi : I ≤ f i, { exact or.inr (or.inr ⟨i, finset.mem_insert_self i t, Hi⟩) }, have : ¬I ⊓ f a ⊓ f b ⊓ t.inf f ≤ f i, { rcases t.eq_empty_or_nonempty with (rfl | hsne), { rw [finset.inf_empty, inf_top_eq, hp.1.inf_le, hp.1.inf_le, not_or_distrib, not_or_distrib], exact ⟨⟨Hi, Ha⟩, Hb⟩ }, simp only [hp.1.inf_le, hp.1.inf_le' hsne, not_or_distrib], exact ⟨⟨⟨Hi, Ha⟩, Hb⟩, Ht⟩ }, rcases set.not_subset.1 this with ⟨r, ⟨⟨⟨hrI, hra⟩, hrb⟩, hr⟩, hri⟩, by_cases HI : (I : set R) ⊆ f a ∪ f b ∪ ⋃ j ∈ (↑t : set ι), f j, { specialize ih hp.2 hn HI, rcases ih with ih | ih | ⟨k, hkt, ih⟩, { left, exact ih }, { right, left, exact ih }, { right, right, exact ⟨k, finset.mem_insert_of_mem hkt, ih⟩ } }, exfalso, rcases set.not_subset.1 HI with ⟨s, hsI, hs⟩, rw [finset.coe_insert, set.bUnion_insert] at h, have hsi : s ∈ f i := ((h hsI).resolve_left (mt or.inl hs)).resolve_right (mt or.inr hs), rcases h (I.add_mem hrI hsI) with ⟨ha | hb⟩ | hi | ht, { exact hs (or.inl $ or.inl $ add_sub_cancel' r s ▸ (f a).sub_mem ha hra) }, { exact hs (or.inl $ or.inr $ add_sub_cancel' r s ▸ (f b).sub_mem hb hrb) }, { exact hri (add_sub_cancel r s ▸ (f i).sub_mem hi hsi) }, { rw set.mem_bUnion_iff at ht, rcases ht with ⟨j, hjt, hj⟩, simp only [finset.inf_eq_infi, set_like.mem_coe, submodule.mem_infi] at hr, exact hs (or.inr $ set.mem_bUnion hjt $ add_sub_cancel' r s ▸ (f j).sub_mem hj $ hr j hjt) } end /-- Prime avoidance. Atiyah-Macdonald 1.11, Eisenbud 3.3, Stacks 00DS, Matsumura Ex.1.6. -/ theorem subset_union_prime {R : Type u} [comm_ring R] {s : finset ι} {f : ι → ideal R} (a b : ι) (hp : ∀ i ∈ s, i ≠ a → i ≠ b → is_prime (f i)) {I : ideal R} : (I : set R) ⊆ (⋃ i ∈ (↑s : set ι), f i) ↔ ∃ i ∈ s, I ≤ f i := suffices (I : set R) ⊆ (⋃ i ∈ (↑s : set ι), f i) → ∃ i, i ∈ s ∧ I ≤ f i, from ⟨λ h, bex_def.2 $ this h, λ ⟨i, his, hi⟩, set.subset.trans hi $ set.subset_bUnion_of_mem $ show i ∈ (↑s : set ι), from his⟩, assume h : (I : set R) ⊆ (⋃ i ∈ (↑s : set ι), f i), begin classical, tactic.unfreeze_local_instances, by_cases has : a ∈ s, { obtain ⟨t, hat, rfl⟩ : ∃ t, a ∉ t ∧ insert a t = s := ⟨s.erase a, finset.not_mem_erase a s, finset.insert_erase has⟩, by_cases hbt : b ∈ t, { obtain ⟨u, hbu, rfl⟩ : ∃ u, b ∉ u ∧ insert b u = t := ⟨t.erase b, finset.not_mem_erase b t, finset.insert_erase hbt⟩, have hp' : ∀ i ∈ u, is_prime (f i), { intros i hiu, refine hp i (finset.mem_insert_of_mem (finset.mem_insert_of_mem hiu)) _ _; rintro rfl; solve_by_elim only [finset.mem_insert_of_mem, *], }, rw [finset.coe_insert, finset.coe_insert, set.bUnion_insert, set.bUnion_insert, ← set.union_assoc, subset_union_prime' hp', bex_def] at h, rwa [finset.exists_mem_insert, finset.exists_mem_insert] }, { have hp' : ∀ j ∈ t, is_prime (f j), { intros j hj, refine hp j (finset.mem_insert_of_mem hj) _ _; rintro rfl; solve_by_elim only [finset.mem_insert_of_mem, *], }, rw [finset.coe_insert, set.bUnion_insert, ← set.union_self (f a : set R), subset_union_prime' hp', ← or_assoc, or_self, bex_def] at h, rwa finset.exists_mem_insert } }, { by_cases hbs : b ∈ s, { obtain ⟨t, hbt, rfl⟩ : ∃ t, b ∉ t ∧ insert b t = s := ⟨s.erase b, finset.not_mem_erase b s, finset.insert_erase hbs⟩, have hp' : ∀ j ∈ t, is_prime (f j), { intros j hj, refine hp j (finset.mem_insert_of_mem hj) _ _; rintro rfl; solve_by_elim only [finset.mem_insert_of_mem, *], }, rw [finset.coe_insert, set.bUnion_insert, ← set.union_self (f b : set R), subset_union_prime' hp', ← or_assoc, or_self, bex_def] at h, rwa finset.exists_mem_insert }, cases s.eq_empty_or_nonempty with hse hsne, { subst hse, rw [finset.coe_empty, set.bUnion_empty, set.subset_empty_iff] at h, have : (I : set R) ≠ ∅ := set.nonempty.ne_empty (set.nonempty_of_mem I.zero_mem), exact absurd h this }, { cases hsne.bex with i his, obtain ⟨t, hit, rfl⟩ : ∃ t, i ∉ t ∧ insert i t = s := ⟨s.erase i, finset.not_mem_erase i s, finset.insert_erase his⟩, have hp' : ∀ j ∈ t, is_prime (f j), { intros j hj, refine hp j (finset.mem_insert_of_mem hj) _ _; rintro rfl; solve_by_elim only [finset.mem_insert_of_mem, *], }, rw [finset.coe_insert, set.bUnion_insert, ← set.union_self (f i : set R), subset_union_prime' hp', ← or_assoc, or_self, bex_def] at h, rwa finset.exists_mem_insert } } end section dvd /-- If `I` divides `J`, then `I` contains `J`. In a Dedekind domain, to divide and contain are equivalent, see `ideal.dvd_iff_le`. -/ lemma le_of_dvd {I J : ideal R} : I ∣ J → J ≤ I | ⟨K, h⟩ := h.symm ▸ le_trans mul_le_inf inf_le_left lemma is_unit_iff {I : ideal R} : is_unit I ↔ I = ⊤ := is_unit_iff_dvd_one.trans ((@one_eq_top R _).symm ▸ ⟨λ h, eq_top_iff.mpr (ideal.le_of_dvd h), λ h, ⟨⊤, by rw [mul_top, h]⟩⟩) instance unique_units : unique (units (ideal R)) := { default := 1, uniq := λ u, units.ext (show (u : ideal R) = 1, by rw [is_unit_iff.mp u.is_unit, one_eq_top]) } end dvd end mul_and_radical section map_and_comap variables {R : Type u} {S : Type v} section semiring variables [semiring R] [semiring S] variables (f : R →+* S) variables {I J : ideal R} {K L : ideal S} /-- `I.map f` is the span of the image of the ideal `I` under `f`, which may be bigger than the image itself. -/ def map (I : ideal R) : ideal S := span (f '' I) /-- `I.comap f` is the preimage of `I` under `f`. -/ def comap (I : ideal S) : ideal R := { carrier := f ⁻¹' I, smul_mem' := λ c x hx, show f (c * x) ∈ I, by { rw f.map_mul, exact I.mul_mem_left _ hx }, .. I.to_add_submonoid.comap (f : R →+ S) } variables {f} theorem map_mono (h : I ≤ J) : map f I ≤ map f J := span_mono $ set.image_subset _ h theorem mem_map_of_mem (f : R →+* S) {I : ideal R} {x : R} (h : x ∈ I) : f x ∈ map f I := subset_span ⟨x, h, rfl⟩ lemma apply_coe_mem_map (f : R →+* S) (I : ideal R) (x : I) : f x ∈ I.map f := mem_map_of_mem f x.prop theorem map_le_iff_le_comap : map f I ≤ K ↔ I ≤ comap f K := span_le.trans set.image_subset_iff @[simp] theorem mem_comap {x} : x ∈ comap f K ↔ f x ∈ K := iff.rfl theorem comap_mono (h : K ≤ L) : comap f K ≤ comap f L := set.preimage_mono (λ x hx, h hx) variables (f) theorem comap_ne_top (hK : K ≠ ⊤) : comap f K ≠ ⊤ := (ne_top_iff_one _).2 $ by rw [mem_comap, f.map_one]; exact (ne_top_iff_one _).1 hK instance is_prime.comap [hK : K.is_prime] : (comap f K).is_prime := ⟨comap_ne_top _ hK.1, λ x y, by simp only [mem_comap, f.map_mul]; apply hK.2⟩ variables (I J K L) theorem map_top : map f ⊤ = ⊤ := (eq_top_iff_one _).2 $ subset_span ⟨1, trivial, f.map_one⟩ variable (f) lemma gc_map_comap : galois_connection (ideal.map f) (ideal.comap f) := λ I J, ideal.map_le_iff_le_comap @[simp] lemma comap_id : I.comap (ring_hom.id R) = I := ideal.ext $ λ _, iff.rfl @[simp] lemma map_id : I.map (ring_hom.id R) = I := (gc_map_comap (ring_hom.id R)).l_unique galois_connection.id comap_id lemma comap_comap {T : Type*} [semiring T] {I : ideal T} (f : R →+* S) (g : S →+* T) : (I.comap g).comap f = I.comap (g.comp f) := rfl lemma map_map {T : Type*} [semiring T] {I : ideal R} (f : R →+* S) (g : S →+* T) : (I.map f).map g = I.map (g.comp f) := ((gc_map_comap f).compose (gc_map_comap g)).l_unique (gc_map_comap (g.comp f)) (λ _, comap_comap _ _) lemma map_span (f : R →+* S) (s : set R) : map f (span s) = span (f '' s) := symm $ submodule.span_eq_of_le _ (λ y ⟨x, hy, x_eq⟩, x_eq ▸ mem_map_of_mem f (subset_span hy)) (map_le_iff_le_comap.2 $ span_le.2 $ set.image_subset_iff.1 subset_span) variables {f I J K L} lemma map_le_of_le_comap : I ≤ K.comap f → I.map f ≤ K := (gc_map_comap f).l_le lemma le_comap_of_map_le : I.map f ≤ K → I ≤ K.comap f := (gc_map_comap f).le_u lemma le_comap_map : I ≤ (I.map f).comap f := (gc_map_comap f).le_u_l _ lemma map_comap_le : (K.comap f).map f ≤ K := (gc_map_comap f).l_u_le _ @[simp] lemma comap_top : (⊤ : ideal S).comap f = ⊤ := (gc_map_comap f).u_top @[simp] lemma comap_eq_top_iff {I : ideal S} : I.comap f = ⊤ ↔ I = ⊤ := ⟨ λ h, I.eq_top_iff_one.mpr (f.map_one ▸ mem_comap.mp ((I.comap f).eq_top_iff_one.mp h)), λ h, by rw [h, comap_top] ⟩ @[simp] lemma map_bot : (⊥ : ideal R).map f = ⊥ := (gc_map_comap f).l_bot variables (f I J K L) @[simp] lemma map_comap_map : ((I.map f).comap f).map f = I.map f := (gc_map_comap f).l_u_l_eq_l I @[simp] lemma comap_map_comap : ((K.comap f).map f).comap f = K.comap f := (gc_map_comap f).u_l_u_eq_u K lemma map_sup : (I ⊔ J).map f = I.map f ⊔ J.map f := (gc_map_comap f).l_sup theorem comap_inf : comap f (K ⊓ L) = comap f K ⊓ comap f L := rfl variables {ι : Sort*} lemma map_supr (K : ι → ideal R) : (supr K).map f = ⨆ i, (K i).map f := (gc_map_comap f).l_supr lemma comap_infi (K : ι → ideal S) : (infi K).comap f = ⨅ i, (K i).comap f := (gc_map_comap f).u_infi lemma map_Sup (s : set (ideal R)): (Sup s).map f = ⨆ I ∈ s, (I : ideal R).map f := (gc_map_comap f).l_Sup lemma comap_Inf (s : set (ideal S)): (Inf s).comap f = ⨅ I ∈ s, (I : ideal S).comap f := (gc_map_comap f).u_Inf lemma comap_Inf' (s : set (ideal S)) : (Inf s).comap f = ⨅ I ∈ (comap f '' s), I := trans (comap_Inf f s) (by rw infi_image) theorem comap_is_prime [H : is_prime K] : is_prime (comap f K) := ⟨comap_ne_top f H.ne_top, λ x y h, H.mem_or_mem $ by rwa [mem_comap, ring_hom.map_mul] at h⟩ variables {I J K L} theorem map_inf_le : map f (I ⊓ J) ≤ map f I ⊓ map f J := (gc_map_comap f).monotone_l.map_inf_le _ _ theorem le_comap_sup : comap f K ⊔ comap f L ≤ comap f (K ⊔ L) := (gc_map_comap f).monotone_u.le_map_sup _ _ section surjective variables (hf : function.surjective f) include hf open function theorem map_comap_of_surjective (I : ideal S) : map f (comap f I) = I := le_antisymm (map_le_iff_le_comap.2 (le_refl _)) (λ s hsi, let ⟨r, hfrs⟩ := hf s in hfrs ▸ (mem_map_of_mem f $ show f r ∈ I, from hfrs.symm ▸ hsi)) /-- `map` and `comap` are adjoint, and the composition `map f ∘ comap f` is the identity -/ def gi_map_comap : galois_insertion (map f) (comap f) := galois_insertion.monotone_intro ((gc_map_comap f).monotone_u) ((gc_map_comap f).monotone_l) (λ _, le_comap_map) (map_comap_of_surjective _ hf) lemma map_surjective_of_surjective : surjective (map f) := (gi_map_comap f hf).l_surjective lemma comap_injective_of_surjective : injective (comap f) := (gi_map_comap f hf).u_injective lemma map_sup_comap_of_surjective (I J : ideal S) : (I.comap f ⊔ J.comap f).map f = I ⊔ J := (gi_map_comap f hf).l_sup_u _ _ lemma map_supr_comap_of_surjective (K : ι → ideal S) : (⨆i, (K i).comap f).map f = supr K := (gi_map_comap f hf).l_supr_u _ lemma map_inf_comap_of_surjective (I J : ideal S) : (I.comap f ⊓ J.comap f).map f = I ⊓ J := (gi_map_comap f hf).l_inf_u _ _ lemma map_infi_comap_of_surjective (K : ι → ideal S) : (⨅i, (K i).comap f).map f = infi K := (gi_map_comap f hf).l_infi_u _ theorem mem_image_of_mem_map_of_surjective {I : ideal R} {y} (H : y ∈ map f I) : y ∈ f '' I := submodule.span_induction H (λ _, id) ⟨0, I.zero_mem, f.map_zero⟩ (λ y1 y2 ⟨x1, hx1i, hxy1⟩ ⟨x2, hx2i, hxy2⟩, ⟨x1 + x2, I.add_mem hx1i hx2i, hxy1 ▸ hxy2 ▸ f.map_add _ _⟩) (λ c y ⟨x, hxi, hxy⟩, let ⟨d, hdc⟩ := hf c in ⟨d • x, I.smul_mem _ hxi, hdc ▸ hxy ▸ f.map_mul _ _⟩) lemma mem_map_iff_of_surjective {I : ideal R} {y} : y ∈ map f I ↔ ∃ x, x ∈ I ∧ f x = y := ⟨λ h, (set.mem_image _ _ _).2 (mem_image_of_mem_map_of_surjective f hf h), λ ⟨x, hx⟩, hx.right ▸ (mem_map_of_mem f hx.left)⟩ lemma le_map_of_comap_le_of_surjective : comap f K ≤ I → K ≤ map f I := λ h, (map_comap_of_surjective f hf K) ▸ map_mono h end surjective section injective variables (hf : function.injective f) include hf lemma comap_bot_le_of_injective : comap f ⊥ ≤ I := begin refine le_trans (λ x hx, _) bot_le, rw [mem_comap, submodule.mem_bot, ← ring_hom.map_zero f] at hx, exact eq.symm (hf hx) ▸ (submodule.zero_mem ⊥) end end injective end semiring section ring variables [ring R] [ring S] (f : R →+* S) {I : ideal R} section surjective variables (hf : function.surjective f) include hf theorem comap_map_of_surjective (I : ideal R) : comap f (map f I) = I ⊔ comap f ⊥ := le_antisymm (assume r h, let ⟨s, hsi, hfsr⟩ := mem_image_of_mem_map_of_surjective f hf h in submodule.mem_sup.2 ⟨s, hsi, r - s, (submodule.mem_bot S).2 $ by rw [f.map_sub, hfsr, sub_self], add_sub_cancel'_right s r⟩) (sup_le (map_le_iff_le_comap.1 (le_refl _)) (comap_mono bot_le)) /-- Correspondence theorem -/ def rel_iso_of_surjective : ideal S ≃o { p : ideal R // comap f ⊥ ≤ p } := { to_fun := λ J, ⟨comap f J, comap_mono bot_le⟩, inv_fun := λ I, map f I.1, left_inv := λ J, map_comap_of_surjective f hf J, right_inv := λ I, subtype.eq $ show comap f (map f I.1) = I.1, from (comap_map_of_surjective f hf I).symm ▸ le_antisymm (sup_le (le_refl _) I.2) le_sup_left, map_rel_iff' := λ I1 I2, ⟨λ H, map_comap_of_surjective f hf I1 ▸ map_comap_of_surjective f hf I2 ▸ map_mono H, comap_mono⟩ } /-- The map on ideals induced by a surjective map preserves inclusion. -/ def order_embedding_of_surjective : ideal S ↪o ideal R := (rel_iso_of_surjective f hf).to_rel_embedding.trans (subtype.rel_embedding _ _) theorem map_eq_top_or_is_maximal_of_surjective {I : ideal R} (H : is_maximal I) : (map f I) = ⊤ ∨ is_maximal (map f I) := begin refine or_iff_not_imp_left.2 (λ ne_top, ⟨⟨λ h, ne_top h, λ J hJ, _⟩⟩), { refine (rel_iso_of_surjective f hf).injective (subtype.ext_iff.2 (eq.trans (H.1.2 (comap f J) (lt_of_le_of_ne _ _)) comap_top.symm)), { exact (map_le_iff_le_comap).1 (le_of_lt hJ) }, { exact λ h, hJ.right (le_map_of_comap_le_of_surjective f hf (le_of_eq h.symm)) } } end theorem comap_is_maximal_of_surjective {K : ideal S} [H : is_maximal K] : is_maximal (comap f K) := begin refine ⟨⟨comap_ne_top _ H.1.1, λ J hJ, _⟩⟩, suffices : map f J = ⊤, { replace this := congr_arg (comap f) this, rw [comap_top, comap_map_of_surjective _ hf, eq_top_iff] at this, rw eq_top_iff, exact le_trans this (sup_le (le_of_eq rfl) (le_trans (comap_mono (bot_le)) (le_of_lt hJ))) }, refine H.1.2 (map f J) (lt_of_le_of_ne (le_map_of_comap_le_of_surjective _ hf (le_of_lt hJ)) (λ h, ne_of_lt hJ (trans (congr_arg (comap f) h) _))), rw [comap_map_of_surjective _ hf, sup_eq_left], exact le_trans (comap_mono bot_le) (le_of_lt hJ) end end surjective /-- If `f : R ≃+* S` is a ring isomorphism and `I : ideal R`, then `map f (map f.symm) = I`. -/ @[simp] lemma map_of_equiv (I : ideal R) (f : R ≃+* S) : (I.map (f : R →+* S)).map (f.symm : S →+* R) = I := by simp [← ring_equiv.to_ring_hom_eq_coe, map_map] /-- If `f : R ≃+* S` is a ring isomorphism and `I : ideal R`, then `comap f.symm (comap f) = I`. -/ @[simp] lemma comap_of_equiv (I : ideal R) (f : R ≃+* S) : (I.comap (f.symm : S →+* R)).comap (f : R →+* S) = I := by simp [← ring_equiv.to_ring_hom_eq_coe, comap_comap] /-- If `f : R ≃+* S` is a ring isomorphism and `I : ideal R`, then `map f I = comap f.symm I`. -/ lemma map_comap_of_equiv (I : ideal R) (f : R ≃+* S) : I.map (f : R →+* S) = I.comap f.symm := le_antisymm (le_comap_of_map_le (map_of_equiv I f).le) (le_map_of_comap_le_of_surjective _ f.surjective (comap_of_equiv I f).le) section bijective variables (hf : function.bijective f) include hf /-- Special case of the correspondence theorem for isomorphic rings -/ def rel_iso_of_bijective : ideal S ≃o ideal R := { to_fun := comap f, inv_fun := map f, left_inv := (rel_iso_of_surjective f hf.right).left_inv, right_inv := λ J, subtype.ext_iff.1 ((rel_iso_of_surjective f hf.right).right_inv ⟨J, comap_bot_le_of_injective f hf.left⟩), map_rel_iff' := (rel_iso_of_surjective f hf.right).map_rel_iff' } lemma comap_le_iff_le_map {I : ideal R} {K : ideal S} : comap f K ≤ I ↔ K ≤ map f I := ⟨λ h, le_map_of_comap_le_of_surjective f hf.right h, λ h, ((rel_iso_of_bijective f hf).right_inv I) ▸ comap_mono h⟩ theorem map.is_maximal {I : ideal R} (H : is_maximal I) : is_maximal (map f I) := by refine or_iff_not_imp_left.1 (map_eq_top_or_is_maximal_of_surjective f hf.right H) (λ h, H.1.1 _); calc I = comap f (map f I) : ((rel_iso_of_bijective f hf).right_inv I).symm ... = comap f ⊤ : by rw h ... = ⊤ : by rw comap_top end bijective lemma ring_equiv.bot_maximal_iff (e : R ≃+* S) : (⊥ : ideal R).is_maximal ↔ (⊥ : ideal S).is_maximal := ⟨λ h, (@map_bot _ _ _ _ e.to_ring_hom) ▸ map.is_maximal e.to_ring_hom e.bijective h, λ h, (@map_bot _ _ _ _ e.symm.to_ring_hom) ▸ map.is_maximal e.symm.to_ring_hom e.symm.bijective h⟩ end ring section comm_ring variables [comm_ring R] [comm_ring S] variables (f : R →+* S) variables {I J : ideal R} {K L : ideal S} lemma mem_quotient_iff_mem (hIJ : I ≤ J) {x : R} : quotient.mk I x ∈ J.map (quotient.mk I) ↔ x ∈ J := begin refine iff.trans (mem_map_iff_of_surjective _ quotient.mk_surjective) _, split, { rintros ⟨x, x_mem, x_eq⟩, simpa using J.add_mem (hIJ (quotient.eq.mp x_eq.symm)) x_mem }, { intro x_mem, exact ⟨x, x_mem, rfl⟩ } end variables (I J K L) theorem map_mul : map f (I * J) = map f I * map f J := le_antisymm (map_le_iff_le_comap.2 $ mul_le.2 $ λ r hri s hsj, show f (r * s) ∈ _, by rw f.map_mul; exact mul_mem_mul (mem_map_of_mem f hri) (mem_map_of_mem f hsj)) (trans_rel_right _ (span_mul_span _ _) $ span_le.2 $ set.bUnion_subset $ λ i ⟨r, hri, hfri⟩, set.bUnion_subset $ λ j ⟨s, hsj, hfsj⟩, set.singleton_subset_iff.2 $ hfri ▸ hfsj ▸ by rw [← f.map_mul]; exact mem_map_of_mem f (mul_mem_mul hri hsj)) theorem comap_radical : comap f (radical K) = radical (comap f K) := le_antisymm (λ r ⟨n, hfrnk⟩, ⟨n, show f (r ^ n) ∈ K, from (f.map_pow r n).symm ▸ hfrnk⟩) (λ r ⟨n, hfrnk⟩, ⟨n, f.map_pow r n ▸ hfrnk⟩) @[simp] lemma map_quotient_self : map (quotient.mk I) I = ⊥ := eq_bot_iff.2 $ ideal.map_le_iff_le_comap.2 $ λ x hx, (submodule.mem_bot I.quotient).2 $ ideal.quotient.eq_zero_iff_mem.2 hx variables {I J K L} theorem map_radical_le : map f (radical I) ≤ radical (map f I) := map_le_iff_le_comap.2 $ λ r ⟨n, hrni⟩, ⟨n, f.map_pow r n ▸ mem_map_of_mem f hrni⟩ theorem le_comap_mul : comap f K * comap f L ≤ comap f (K * L) := map_le_iff_le_comap.1 $ (map_mul f (comap f K) (comap f L)).symm ▸ mul_mono (map_le_iff_le_comap.2 $ le_refl _) (map_le_iff_le_comap.2 $ le_refl _) end comm_ring end map_and_comap section is_primary variables {R : Type u} [comm_semiring R] /-- A proper ideal `I` is primary iff `xy ∈ I` implies `x ∈ I` or `y ∈ radical I`. -/ def is_primary (I : ideal R) : Prop := I ≠ ⊤ ∧ ∀ {x y : R}, x * y ∈ I → x ∈ I ∨ y ∈ radical I theorem is_primary.to_is_prime (I : ideal R) (hi : is_prime I) : is_primary I := ⟨hi.1, λ x y hxy, (hi.mem_or_mem hxy).imp id $ λ hyi, le_radical hyi⟩ theorem mem_radical_of_pow_mem {I : ideal R} {x : R} {m : ℕ} (hx : x ^ m ∈ radical I) : x ∈ radical I := radical_idem I ▸ ⟨m, hx⟩ theorem is_prime_radical {I : ideal R} (hi : is_primary I) : is_prime (radical I) := ⟨mt radical_eq_top.1 hi.1, λ x y ⟨m, hxy⟩, begin rw mul_pow at hxy, cases hi.2 hxy, { exact or.inl ⟨m, h⟩ }, { exact or.inr (mem_radical_of_pow_mem h) } end⟩ theorem is_primary_inf {I J : ideal R} (hi : is_primary I) (hj : is_primary J) (hij : radical I = radical J) : is_primary (I ⊓ J) := ⟨ne_of_lt $ lt_of_le_of_lt inf_le_left (lt_top_iff_ne_top.2 hi.1), λ x y ⟨hxyi, hxyj⟩, begin rw [radical_inf, hij, inf_idem], cases hi.2 hxyi with hxi hyi, cases hj.2 hxyj with hxj hyj, { exact or.inl ⟨hxi, hxj⟩ }, { exact or.inr hyj }, { rw hij at hyi, exact or.inr hyi } end⟩ end is_primary end ideal namespace ring_hom variables {R : Type u} {S : Type v} section semiring variables [semiring R] [semiring S] (f : R →+* S) /-- Kernel of a ring homomorphism as an ideal of the domain. -/ def ker : ideal R := ideal.comap f ⊥ /-- An element is in the kernel if and only if it maps to zero.-/ lemma mem_ker {r} : r ∈ ker f ↔ f r = 0 := by rw [ker, ideal.mem_comap, submodule.mem_bot] lemma ker_eq : ((ker f) : set R) = set.preimage f {0} := rfl lemma ker_eq_comap_bot (f : R →+* S) : f.ker = ideal.comap f ⊥ := rfl /-- If the target is not the zero ring, then one is not in the kernel.-/ lemma not_one_mem_ker [nontrivial S] (f : R →+* S) : (1:R) ∉ ker f := by { rw [mem_ker, f.map_one], exact one_ne_zero } end semiring section ring variables [ring R] [semiring S] (f : R →+* S) lemma injective_iff_ker_eq_bot : function.injective f ↔ ker f = ⊥ := by { rw [set_like.ext'_iff, ker_eq, set.ext_iff], exact f.injective_iff' } lemma ker_eq_bot_iff_eq_zero : ker f = ⊥ ↔ ∀ x, f x = 0 → x = 0 := by { rw [← f.injective_iff, injective_iff_ker_eq_bot] } @[simp] lemma ker_coe_equiv (f : R ≃+* S) : ker (f : R →+* S) = ⊥ := by simpa only [←injective_iff_ker_eq_bot] using f.injective end ring section comm_ring variables [comm_ring R] [comm_ring S] (f : R →+* S) /-- The induced map from the quotient by the kernel to the codomain. This is an isomorphism if `f` has a right inverse (`quotient_ker_equiv_of_right_inverse`) / is surjective (`quotient_ker_equiv_of_surjective`). -/ def ker_lift (f : R →+* S) : f.ker.quotient →+* S := ideal.quotient.lift _ f $ λ r, f.mem_ker.mp @[simp] lemma ker_lift_mk (f : R →+* S) (r : R) : ker_lift f (ideal.quotient.mk f.ker r) = f r := ideal.quotient.lift_mk _ _ _ /-- The induced map from the quotient by the kernel is injective. -/ lemma ker_lift_injective (f : R →+* S) : function.injective (ker_lift f) := assume a b, quotient.induction_on₂' a b $ assume a b (h : f a = f b), quotient.sound' $ show a - b ∈ ker f, by rw [mem_ker, map_sub, h, sub_self] variable {f} /-- The **first isomorphism theorem** for commutative rings, computable version. -/ def quotient_ker_equiv_of_right_inverse {g : S → R} (hf : function.right_inverse g f) : f.ker.quotient ≃+* S := { to_fun := ker_lift f, inv_fun := (ideal.quotient.mk f.ker) ∘ g, left_inv := begin rintro ⟨x⟩, apply ker_lift_injective, simp [hf (f x)], end, right_inv := hf, ..ker_lift f} @[simp] lemma quotient_ker_equiv_of_right_inverse.apply {g : S → R} (hf : function.right_inverse g f) (x : f.ker.quotient) : quotient_ker_equiv_of_right_inverse hf x = ker_lift f x := rfl @[simp] lemma quotient_ker_equiv_of_right_inverse.symm.apply {g : S → R} (hf : function.right_inverse g f) (x : S) : (quotient_ker_equiv_of_right_inverse hf).symm x = ideal.quotient.mk f.ker (g x) := rfl /-- The **first isomorphism theorem** for commutative rings. -/ noncomputable def quotient_ker_equiv_of_surjective (hf : function.surjective f) : f.ker.quotient ≃+* S := quotient_ker_equiv_of_right_inverse (classical.some_spec hf.has_right_inverse) end comm_ring /-- The kernel of a homomorphism to a domain is a prime ideal. -/ lemma ker_is_prime [ring R] [ring S] [is_domain S] (f : R →+* S) : (ker f).is_prime := ⟨by { rw [ne.def, ideal.eq_top_iff_one], exact not_one_mem_ker f }, λ x y, by simpa only [mem_ker, f.map_mul] using @eq_zero_or_eq_zero_of_mul_eq_zero S _ _ _ _ _⟩ /-- The kernel of a homomorphism to a field is a maximal ideal. -/ lemma ker_is_maximal_of_surjective {R K : Type*} [ring R] [field K] (f : R →+* K) (hf : function.surjective f) : f.ker.is_maximal := begin refine ideal.is_maximal_iff.mpr ⟨λ h1, @one_ne_zero K _ _ $ f.map_one ▸ f.mem_ker.mp h1, λ J x hJ hxf hxJ, _⟩, obtain ⟨y, hy⟩ := hf (f x)⁻¹, have H : 1 = y * x - (y * x - 1) := (sub_sub_cancel _ _).symm, rw H, refine J.sub_mem (J.mul_mem_left _ hxJ) (hJ _), rw f.mem_ker, simp only [hy, ring_hom.map_sub, ring_hom.map_one, ring_hom.map_mul, inv_mul_cancel (mt f.mem_ker.mpr hxf), sub_self], end end ring_hom namespace ideal variables {R : Type*} {S : Type*} section semiring variables [semiring R] [semiring S] lemma map_eq_bot_iff_le_ker {I : ideal R} (f : R →+* S) : I.map f = ⊥ ↔ I ≤ f.ker := by rw [ring_hom.ker, eq_bot_iff, map_le_iff_le_comap] lemma ker_le_comap {K : ideal S} (f : R →+* S) : f.ker ≤ comap f K := λ x hx, mem_comap.2 (((ring_hom.mem_ker f).1 hx).symm ▸ K.zero_mem) end semiring section ring variables [ring R] [ring S] lemma map_Inf {A : set (ideal R)} {f : R →+* S} (hf : function.surjective f) : (∀ J ∈ A, ring_hom.ker f ≤ J) → map f (Inf A) = Inf (map f '' A) := begin refine λ h, le_antisymm (le_Inf _) _, { intros j hj y hy, cases (mem_map_iff_of_surjective f hf).1 hy with x hx, cases (set.mem_image _ _ _).mp hj with J hJ, rw [← hJ.right, ← hx.right], exact mem_map_of_mem f (Inf_le_of_le hJ.left (le_of_eq rfl) hx.left) }, { intros y hy, cases hf y with x hx, refine hx ▸ (mem_map_of_mem f _), have : ∀ I ∈ A, y ∈ map f I, by simpa using hy, rw [submodule.mem_Inf], intros J hJ, rcases (mem_map_iff_of_surjective f hf).1 (this J hJ) with ⟨x', hx', rfl⟩, have : x - x' ∈ J, { apply h J hJ, rw [ring_hom.mem_ker, ring_hom.map_sub, hx, sub_self] }, simpa only [sub_add_cancel] using J.add_mem this hx' } end theorem map_is_prime_of_surjective {f : R →+* S} (hf : function.surjective f) {I : ideal R} [H : is_prime I] (hk : ring_hom.ker f ≤ I) : is_prime (map f I) := begin refine ⟨λ h, H.ne_top (eq_top_iff.2 _), λ x y, _⟩, { replace h := congr_arg (comap f) h, rw [comap_map_of_surjective _ hf, comap_top] at h, exact h ▸ sup_le (le_of_eq rfl) hk }, { refine λ hxy, (hf x).rec_on (λ a ha, (hf y).rec_on (λ b hb, _)), rw [← ha, ← hb, ← ring_hom.map_mul, mem_map_iff_of_surjective _ hf] at hxy, rcases hxy with ⟨c, hc, hc'⟩, rw [← sub_eq_zero, ← ring_hom.map_sub] at hc', have : a * b ∈ I, { convert I.sub_mem hc (hk (hc' : c - a * b ∈ f.ker)), abel }, exact (H.mem_or_mem this).imp (λ h, ha ▸ mem_map_of_mem f h) (λ h, hb ▸ mem_map_of_mem f h) } end theorem map_is_prime_of_equiv (f : R ≃+* S) {I : ideal R} [is_prime I] : is_prime (map (f : R →+* S) I) := map_is_prime_of_surjective f.surjective $ by simp end ring section comm_ring variables [comm_ring R] [comm_ring S] @[simp] lemma mk_ker {I : ideal R} : (quotient.mk I).ker = I := by ext; rw [ring_hom.ker, mem_comap, submodule.mem_bot, quotient.eq_zero_iff_mem] lemma map_mk_eq_bot_of_le {I J : ideal R} (h : I ≤ J) : I.map (J^.quotient.mk) = ⊥ := by { rw [map_eq_bot_iff_le_ker, mk_ker], exact h } lemma ker_quotient_lift {S : Type v} [comm_ring S] {I : ideal R} (f : R →+* S) (H : I ≤ f.ker) : (ideal.quotient.lift I f H).ker = (f.ker).map I^.quotient.mk := begin ext x, split, { intro hx, obtain ⟨y, hy⟩ := quotient.mk_surjective x, rw [ring_hom.mem_ker, ← hy, ideal.quotient.lift_mk, ← ring_hom.mem_ker] at hx, rw [← hy, mem_map_iff_of_surjective I^.quotient.mk quotient.mk_surjective], exact ⟨y, hx, rfl⟩ }, { intro hx, rw mem_map_iff_of_surjective I^.quotient.mk quotient.mk_surjective at hx, obtain ⟨y, hy⟩ := hx, rw [ring_hom.mem_ker, ← hy.right, ideal.quotient.lift_mk, ← (ring_hom.mem_ker f)], exact hy.left }, end theorem map_eq_iff_sup_ker_eq_of_surjective {I J : ideal R} (f : R →+* S) (hf : function.surjective f) : map f I = map f J ↔ I ⊔ f.ker = J ⊔ f.ker := by rw [← (comap_injective_of_surjective f hf).eq_iff, comap_map_of_surjective f hf, comap_map_of_surjective f hf, ring_hom.ker_eq_comap_bot] theorem map_radical_of_surjective {f : R →+* S} (hf : function.surjective f) {I : ideal R} (h : ring_hom.ker f ≤ I) : map f (I.radical) = (map f I).radical := begin rw [radical_eq_Inf, radical_eq_Inf], have : ∀ J ∈ {J : ideal R | I ≤ J ∧ J.is_prime}, f.ker ≤ J := λ J hJ, le_trans h hJ.left, convert map_Inf hf this, refine funext (λ j, propext ⟨_, _⟩), { rintros ⟨hj, hj'⟩, haveI : j.is_prime := hj', exact ⟨comap f j, ⟨⟨map_le_iff_le_comap.1 hj, comap_is_prime f j⟩, map_comap_of_surjective f hf j⟩⟩ }, { rintro ⟨J, ⟨hJ, hJ'⟩⟩, haveI : J.is_prime := hJ.right, refine ⟨hJ' ▸ map_mono hJ.left, hJ' ▸ map_is_prime_of_surjective hf (le_trans h hJ.left)⟩ }, end @[simp] lemma bot_quotient_is_maximal_iff (I : ideal R) : (⊥ : ideal I.quotient).is_maximal ↔ I.is_maximal := ⟨λ hI, (@mk_ker _ _ I) ▸ @comap_is_maximal_of_surjective _ _ _ _ (quotient.mk I) quotient.mk_surjective ⊥ hI, λ hI, @bot_is_maximal _ (@field.to_division_ring _ (@quotient.field _ _ I hI)) ⟩ section quotient_algebra variables (R) {A : Type*} [comm_ring A] [algebra R A] /-- The `R`-algebra structure on `A/I` for an `R`-algebra `A` -/ instance {I : ideal A} : algebra R (ideal.quotient I) := { to_fun := λ x, ideal.quotient.mk I (algebra_map R A x), smul := (•), smul_def' := λ r x, quotient.induction_on' x $ λ x, ((quotient.mk I).congr_arg $ algebra.smul_def _ _).trans (ring_hom.map_mul _ _ _), commutes' := λ _ _, mul_comm _ _, .. ring_hom.comp (ideal.quotient.mk I) (algebra_map R A) } /-- The canonical morphism `A →ₐ[R] I.quotient` as morphism of `R`-algebras, for `I` an ideal of `A`, where `A` is an `R`-algebra. -/ def quotient.mkₐ (I : ideal A) : A →ₐ[R] I.quotient := ⟨λ a, submodule.quotient.mk a, rfl, λ _ _, rfl, rfl, λ _ _, rfl, λ _, rfl⟩ lemma quotient.alg_map_eq (I : ideal A) : algebra_map R I.quotient = (algebra_map A I.quotient).comp (algebra_map R A) := rfl instance [algebra S A] [algebra S R] [is_scalar_tower S R A] {I : ideal A} : is_scalar_tower S R (ideal.quotient I) := is_scalar_tower.of_algebra_map_eq' $ by rw [quotient.alg_map_eq R, quotient.alg_map_eq S, ring_hom.comp_assoc, is_scalar_tower.algebra_map_eq S R A] lemma quotient.mkₐ_to_ring_hom (I : ideal A) : (quotient.mkₐ R I).to_ring_hom = ideal.quotient.mk I := rfl @[simp] lemma quotient.mkₐ_eq_mk (I : ideal A) : ⇑(quotient.mkₐ R I) = ideal.quotient.mk I := rfl @[simp] lemma quotient.algebra_map_eq (I : ideal R) : algebra_map R I.quotient = I^.quotient.mk := rfl @[simp] lemma quotient.mk_comp_algebra_map (I : ideal A) : (quotient.mk I).comp (algebra_map R A) = algebra_map R I.quotient := rfl @[simp] lemma quotient.mk_algebra_map (I : ideal A) (x : R) : quotient.mk I (algebra_map R A x) = algebra_map R I.quotient x := rfl /-- The canonical morphism `A →ₐ[R] I.quotient` is surjective. -/ lemma quotient.mkₐ_surjective (I : ideal A) : function.surjective (quotient.mkₐ R I) := surjective_quot_mk _ /-- The kernel of `A →ₐ[R] I.quotient` is `I`. -/ @[simp] lemma quotient.mkₐ_ker (I : ideal A) : (quotient.mkₐ R I : A →+* I.quotient).ker = I := ideal.mk_ker variables {R} {B : Type*} [comm_ring B] [algebra R B] lemma ker_lift.map_smul (f : A →ₐ[R] B) (r : R) (x : f.to_ring_hom.ker.quotient) : f.to_ring_hom.ker_lift (r • x) = r • f.to_ring_hom.ker_lift x := begin obtain ⟨a, rfl⟩ := quotient.mkₐ_surjective R _ x, rw [← alg_hom.map_smul, quotient.mkₐ_eq_mk, ring_hom.ker_lift_mk], exact f.map_smul _ _ end /-- The induced algebras morphism from the quotient by the kernel to the codomain. This is an isomorphism if `f` has a right inverse (`quotient_ker_alg_equiv_of_right_inverse`) / is surjective (`quotient_ker_alg_equiv_of_surjective`). -/ def ker_lift_alg (f : A →ₐ[R] B) : f.to_ring_hom.ker.quotient →ₐ[R] B := alg_hom.mk' f.to_ring_hom.ker_lift (λ _ _, ker_lift.map_smul f _ _) @[simp] lemma ker_lift_alg_mk (f : A →ₐ[R] B) (a : A) : ker_lift_alg f (quotient.mk f.to_ring_hom.ker a) = f a := rfl @[simp] lemma ker_lift_alg_to_ring_hom (f : A →ₐ[R] B) : (ker_lift_alg f).to_ring_hom = ring_hom.ker_lift f := rfl /-- The induced algebra morphism from the quotient by the kernel is injective. -/ lemma ker_lift_alg_injective (f : A →ₐ[R] B) : function.injective (ker_lift_alg f) := ring_hom.ker_lift_injective f /-- The **first isomorphism** theorem for algebras, computable version. -/ def quotient_ker_alg_equiv_of_right_inverse {f : A →ₐ[R] B} {g : B → A} (hf : function.right_inverse g f) : f.to_ring_hom.ker.quotient ≃ₐ[R] B := { ..ring_hom.quotient_ker_equiv_of_right_inverse (λ x, show f.to_ring_hom (g x) = x, from hf x), ..ker_lift_alg f} @[simp] lemma quotient_ker_alg_equiv_of_right_inverse.apply {f : A →ₐ[R] B} {g : B → A} (hf : function.right_inverse g f) (x : f.to_ring_hom.ker.quotient) : quotient_ker_alg_equiv_of_right_inverse hf x = ker_lift_alg f x := rfl @[simp] lemma quotient_ker_alg_equiv_of_right_inverse_symm.apply {f : A →ₐ[R] B} {g : B → A} (hf : function.right_inverse g f) (x : B) : (quotient_ker_alg_equiv_of_right_inverse hf).symm x = quotient.mkₐ R f.to_ring_hom.ker (g x) := rfl /-- The **first isomorphism theorem** for algebras. -/ noncomputable def quotient_ker_alg_equiv_of_surjective {f : A →ₐ[R] B} (hf : function.surjective f) : f.to_ring_hom.ker.quotient ≃ₐ[R] B := quotient_ker_alg_equiv_of_right_inverse (classical.some_spec hf.has_right_inverse) /-- The ring hom `R/I →+* S/J` induced by a ring hom `f : R →+* S` with `I ≤ f⁻¹(J)` -/ def quotient_map {I : ideal R} (J : ideal S) (f : R →+* S) (hIJ : I ≤ J.comap f) : I.quotient →+* J.quotient := (quotient.lift I ((quotient.mk J).comp f) (λ _ ha, by simpa [function.comp_app, ring_hom.coe_comp, quotient.eq_zero_iff_mem] using hIJ ha)) @[simp] lemma quotient_map_mk {J : ideal R} {I : ideal S} {f : R →+* S} {H : J ≤ I.comap f} {x : R} : quotient_map I f H (quotient.mk J x) = quotient.mk I (f x) := quotient.lift_mk J _ _ @[simp] lemma quotient_map_algebra_map {J : ideal A} {I : ideal S} {f : A →+* S} {H : J ≤ I.comap f} {x : R} : quotient_map I f H (algebra_map R J.quotient x) = quotient.mk I (f (algebra_map _ _ x)) := quotient.lift_mk J _ _ lemma quotient_map_comp_mk {J : ideal R} {I : ideal S} {f : R →+* S} (H : J ≤ I.comap f) : (quotient_map I f H).comp (quotient.mk J) = (quotient.mk I).comp f := ring_hom.ext (λ x, by simp only [function.comp_app, ring_hom.coe_comp, ideal.quotient_map_mk]) /-- The ring equiv `R/I ≃+* S/J` induced by a ring equiv `f : R ≃+** S`, where `J = f(I)`. -/ @[simps] def quotient_equiv (I : ideal R) (J : ideal S) (f : R ≃+* S) (hIJ : J = I.map (f : R →+* S)) : I.quotient ≃+* J.quotient := { inv_fun := quotient_map I ↑f.symm (by {rw hIJ, exact le_of_eq (map_comap_of_equiv I f)}), left_inv := by {rintro ⟨r⟩, simp }, right_inv := by {rintro ⟨s⟩, simp }, ..quotient_map J ↑f (by {rw hIJ, exact @le_comap_map _ S _ _ _ _}) } /-- `H` and `h` are kept as separate hypothesis since H is used in constructing the quotient map. -/ lemma quotient_map_injective' {J : ideal R} {I : ideal S} {f : R →+* S} {H : J ≤ I.comap f} (h : I.comap f ≤ J) : function.injective (quotient_map I f H) := begin refine (quotient_map I f H).injective_iff.2 (λ a ha, _), obtain ⟨r, rfl⟩ := quotient.mk_surjective a, rw [quotient_map_mk, quotient.eq_zero_iff_mem] at ha, exact (quotient.eq_zero_iff_mem).mpr (h ha), end /-- If we take `J = I.comap f` then `quotient_map` is injective automatically. -/ lemma quotient_map_injective {I : ideal S} {f : R →+* S} : function.injective (quotient_map I f le_rfl) := quotient_map_injective' le_rfl lemma quotient_map_surjective {J : ideal R} {I : ideal S} {f : R →+* S} {H : J ≤ I.comap f} (hf : function.surjective f) : function.surjective (quotient_map I f H) := λ x, let ⟨x, hx⟩ := quotient.mk_surjective x in let ⟨y, hy⟩ := hf x in ⟨(quotient.mk J) y, by simp [hx, hy]⟩ /-- Commutativity of a square is preserved when taking quotients by an ideal. -/ lemma comp_quotient_map_eq_of_comp_eq {R' S' : Type*} [comm_ring R'] [comm_ring S'] {f : R →+* S} {f' : R' →+* S'} {g : R →+* R'} {g' : S →+* S'} (hfg : f'.comp g = g'.comp f) (I : ideal S') : (quotient_map I g' le_rfl).comp (quotient_map (I.comap g') f le_rfl) = (quotient_map I f' le_rfl).comp (quotient_map (I.comap f') g (le_of_eq (trans (comap_comap f g') (hfg ▸ (comap_comap g f'))))) := begin refine ring_hom.ext (λ a, _), obtain ⟨r, rfl⟩ := quotient.mk_surjective a, simp only [ring_hom.comp_apply, quotient_map_mk], exact congr_arg (quotient.mk I) (trans (g'.comp_apply f r).symm (hfg ▸ (f'.comp_apply g r))), end variables {I : ideal R} {J : ideal S} [algebra R S] /-- The algebra hom `A/I →+* S/J` induced by an algebra hom `f : A →ₐ[R] S` with `I ≤ f⁻¹(J)`. -/ def quotient_mapₐ {I : ideal A} (J : ideal S) (f : A →ₐ[R] S) (hIJ : I ≤ J.comap f) : I.quotient →ₐ[R] J.quotient := { commutes' := λ r, by simp, ..quotient_map J ↑f hIJ } @[simp] lemma quotient_map_mkₐ {I : ideal A} (J : ideal S) (f : A →ₐ[R] S) (H : I ≤ J.comap f) {x : A} : quotient_mapₐ J f H (quotient.mk I x) = quotient.mkₐ R J (f x) := rfl lemma quotient_map_comp_mkₐ {I : ideal A} (J : ideal S) (f : A →ₐ[R] S) (H : I ≤ J.comap f) : (quotient_mapₐ J f H).comp (quotient.mkₐ R I) = (quotient.mkₐ R J).comp f := alg_hom.ext (λ x, by simp only [quotient_map_mkₐ, quotient.mkₐ_eq_mk, alg_hom.comp_apply]) /-- The algebra equiv `A/I ≃ₐ[R] S/J` induced by an algebra equiv `f : A ≃ₐ[R] S`, where`J = f(I)`. -/ def quotient_equiv_alg (I : ideal A) (J : ideal S) (f : A ≃ₐ[R] S) (hIJ : J = I.map (f : A →+* S)) : I.quotient ≃ₐ[R] J.quotient := { commutes' := λ r, by simp, ..quotient_equiv I J (f : A ≃+* S) hIJ } @[priority 100] instance quotient_algebra : algebra (J.comap (algebra_map R S)).quotient J.quotient := (quotient_map J (algebra_map R S) (le_of_eq rfl)).to_algebra lemma algebra_map_quotient_injective : function.injective (algebra_map (J.comap (algebra_map R S)).quotient J.quotient) := begin rintros ⟨a⟩ ⟨b⟩ hab, replace hab := quotient.eq.mp hab, rw ← ring_hom.map_sub at hab, exact quotient.eq.mpr hab end end quotient_algebra end comm_ring end ideal namespace submodule variables {R : Type u} {M : Type v} variables [comm_semiring R] [add_comm_monoid M] [module R M] -- TODO: show `[algebra R A] : algebra (ideal R) A` too instance module_submodule : module (ideal R) (submodule R M) := { smul_add := smul_sup, add_smul := sup_smul, mul_smul := submodule.smul_assoc, one_smul := by simp, zero_smul := bot_smul, smul_zero := smul_bot } end submodule namespace ring_hom variables {A B C : Type*} [ring A] [ring B] [ring C] variables (f : A →+* B) (f_inv : B → A) /-- Auxiliary definition used to define `lift_of_right_inverse` -/ def lift_of_right_inverse_aux (hf : function.right_inverse f_inv f) (g : A →+* C) (hg : f.ker ≤ g.ker) : B →+* C := { to_fun := λ b, g (f_inv b), map_one' := begin rw [← g.map_one, ← sub_eq_zero, ← g.map_sub, ← g.mem_ker], apply hg, rw [f.mem_ker, f.map_sub, sub_eq_zero, f.map_one], exact hf 1 end, map_mul' := begin intros x y, rw [← g.map_mul, ← sub_eq_zero, ← g.map_sub, ← g.mem_ker], apply hg, rw [f.mem_ker, f.map_sub, sub_eq_zero, f.map_mul], simp only [hf _], end, .. add_monoid_hom.lift_of_right_inverse f.to_add_monoid_hom f_inv hf ⟨g.to_add_monoid_hom, hg⟩ } @[simp] lemma lift_of_right_inverse_aux_comp_apply (hf : function.right_inverse f_inv f) (g : A →+* C) (hg : f.ker ≤ g.ker) (a : A) : (f.lift_of_right_inverse_aux f_inv hf g hg) (f a) = g a := f.to_add_monoid_hom.lift_of_right_inverse_comp_apply f_inv hf ⟨g.to_add_monoid_hom, hg⟩ a /-- `lift_of_right_inverse f hf g hg` is the unique ring homomorphism `φ` * such that `φ.comp f = g` (`ring_hom.lift_of_right_inverse_comp`), * where `f : A →+* B` is has a right_inverse `f_inv` (`hf`), * and `g : B →+* C` satisfies `hg : f.ker ≤ g.ker`. See `ring_hom.eq_lift_of_right_inverse` for the uniqueness lemma. ``` A . | \ f | \ g | \ v \⌟ B ----> C ∃!φ ``` -/ def lift_of_right_inverse (hf : function.right_inverse f_inv f) : {g : A →+* C // f.ker ≤ g.ker} ≃ (B →+* C) := { to_fun := λ g, f.lift_of_right_inverse_aux f_inv hf g.1 g.2, inv_fun := λ φ, ⟨φ.comp f, λ x hx, (mem_ker _).mpr $ by simp [(mem_ker _).mp hx]⟩, left_inv := λ g, by { ext, simp only [comp_apply, lift_of_right_inverse_aux_comp_apply, subtype.coe_mk, subtype.val_eq_coe], }, right_inv := λ φ, by { ext b, simp [lift_of_right_inverse_aux, hf b], } } /-- A non-computable version of `ring_hom.lift_of_right_inverse` for when no computable right inverse is available, that uses `function.surj_inv`. -/ @[simp] noncomputable abbreviation lift_of_surjective (hf : function.surjective f) : {g : A →+* C // f.ker ≤ g.ker} ≃ (B →+* C) := f.lift_of_right_inverse (function.surj_inv hf) (function.right_inverse_surj_inv hf) lemma lift_of_right_inverse_comp_apply (hf : function.right_inverse f_inv f) (g : {g : A →+* C // f.ker ≤ g.ker}) (x : A) : (f.lift_of_right_inverse f_inv hf g) (f x) = g x := f.lift_of_right_inverse_aux_comp_apply f_inv hf g.1 g.2 x lemma lift_of_right_inverse_comp (hf : function.right_inverse f_inv f) (g : {g : A →+* C // f.ker ≤ g.ker}) : (f.lift_of_right_inverse f_inv hf g).comp f = g := ring_hom.ext $ f.lift_of_right_inverse_comp_apply f_inv hf g lemma eq_lift_of_right_inverse (hf : function.right_inverse f_inv f) (g : A →+* C) (hg : f.ker ≤ g.ker) (h : B →+* C) (hh : h.comp f = g) : h = (f.lift_of_right_inverse f_inv hf ⟨g, hg⟩) := begin simp_rw ←hh, exact ((f.lift_of_right_inverse f_inv hf).apply_symm_apply _).symm, end end ring_hom namespace double_quot open ideal variables {R : Type u} [comm_ring R] (I J : ideal R) /-- The obvious ring hom `R/I → R/(I ⊔ J)` -/ def quot_left_to_quot_sup : I.quotient →+* (I ⊔ J).quotient := ideal.quotient.factor I (I ⊔ J) le_sup_left /-- The kernel of `quot_left_to_quot_sup` -/ lemma ker_quot_left_to_quot_sup : (quot_left_to_quot_sup I J).ker = J.map (ideal.quotient.mk I) := by simp only [mk_ker, sup_idem, sup_comm, quot_left_to_quot_sup, quotient.factor, ker_quotient_lift, map_eq_iff_sup_ker_eq_of_surjective I^.quotient.mk quotient.mk_surjective, ← sup_assoc] /-- The ring homomorphism `(R/I)/J' -> R/(I ⊔ J)` induced by `quot_left_to_quot_sup` where `J'` is the image of `J` in `R/I`-/ def quot_quot_to_quot_sup : (J.map (ideal.quotient.mk I)).quotient →+* (I ⊔ J).quotient := ideal.quotient.lift (ideal.map (ideal.quotient.mk I) J) (quot_left_to_quot_sup I J) (ker_quot_left_to_quot_sup I J).symm.le /-- The composite of the maps `R → (R/I)` and `(R/I) → (R/I)/J'` -/ def quot_quot_mk : R →+* (J.map I^.quotient.mk).quotient := ((J.map I^.quotient.mk)^.quotient.mk).comp I^.quotient.mk /-- The kernel of `quot_quot_mk` -/ lemma ker_quot_quot_mk : (quot_quot_mk I J).ker = I ⊔ J := by rw [ring_hom.ker_eq_comap_bot, quot_quot_mk, ← comap_comap, ← ring_hom.ker, mk_ker, comap_map_of_surjective (ideal.quotient.mk I) (quotient.mk_surjective), ← ring_hom.ker, mk_ker, sup_comm] /-- The ring homomorphism `R/(I ⊔ J) → (R/I)/J' `induced by `quot_quot_mk` -/ def lift_sup_quot_quot_mk (I J : ideal R) : (I ⊔ J).quotient →+* (J.map (ideal.quotient.mk I)).quotient := ideal.quotient.lift (I ⊔ J) (quot_quot_mk I J) (ker_quot_quot_mk I J).symm.le /-- `quot_quot_to_quot_add` and `lift_sup_double_qot_mk` are inverse isomorphisms -/ def quot_quot_equiv_quot_sup : (J.map (ideal.quotient.mk I)).quotient ≃+* (I ⊔ J).quotient := ring_equiv.of_hom_inv (quot_quot_to_quot_sup I J) (lift_sup_quot_quot_mk I J) (by { ext z, refl }) (by { ext z, refl }) @[simp] lemma quot_quot_equiv_quot_sup_quot_quot_mk (x : R) : quot_quot_equiv_quot_sup I J (quot_quot_mk I J x) = ideal.quotient.mk (I ⊔ J) x := rfl @[simp] lemma quot_quot_equiv_quot_sup_symm_quot_quot_mk (x : R) : (quot_quot_equiv_quot_sup I J).symm (ideal.quotient.mk (I ⊔ J) x) = quot_quot_mk I J x := rfl /-- The obvious isomorphism `(R/I)/J' → (R/J)/I' ` -/ def quot_quot_equiv_comm : (J.map I^.quotient.mk).quotient ≃+* (I.map J^.quotient.mk).quotient := ((quot_quot_equiv_quot_sup I J).trans (quot_equiv_of_eq sup_comm)).trans (quot_quot_equiv_quot_sup J I).symm @[simp] lemma quot_quot_equiv_comm_quot_quot_mk (x : R) : quot_quot_equiv_comm I J (quot_quot_mk I J x) = quot_quot_mk J I x := rfl @[simp] lemma quot_quot_equiv_comm_comp_quot_quot_mk : ring_hom.comp ↑(quot_quot_equiv_comm I J) (quot_quot_mk I J) = quot_quot_mk J I := ring_hom.ext $ quot_quot_equiv_comm_quot_quot_mk I J @[simp] lemma quot_quot_equiv_comm_symm : (quot_quot_equiv_comm I J).symm = quot_quot_equiv_comm J I := rfl end double_quot
8f23ab71723360e59711253eecc34dbcdd7a6352
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/measure_theory/l1_space.lean
395d0d153a528bb96e8b17dc9dc987ba48bb8f91
[ "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
36,481
lean
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou -/ import measure_theory.lp_space /-! # Integrable functions and `L¹` space In the first part of this file, the predicate `integrable` is defined and basic properties of integrable functions are proved. Such a predicate is already available under the name `mem_ℒp 1`. We give a direct definition which is easier to use, and show that it is equivalent to `mem_ℒp 1` In the second part, we establish an API between `integrable` and the space `L¹` of equivalence classes of integrable functions, already defined as a special case of `L^p` spaces for `p = 1`. ## Notation * `α →₁[μ] β` is the type of `L¹` space, where `α` is a `measure_space` and `β` is a `normed_group` with a `second_countable_topology`. `f : α →ₘ β` is a "function" in `L¹`. In comments, `[f]` is also used to denote an `L¹` function. `₁` can be typed as `\1`. ## Main definitions * Let `f : α → β` be a function, where `α` is a `measure_space` and `β` a `normed_group`. Then `has_finite_integral f` means `(∫⁻ a, nnnorm (f a)) < ∞`. * If `β` is moreover a `measurable_space` then `f` is called `integrable` if `f` is `measurable` and `has_finite_integral f` holds. ## Implementation notes To prove something for an arbitrary integrable function, a useful theorem is `integrable.induction` in the file `set_integral`. ## Tags integrable, function space, l1 -/ noncomputable theory open_locale classical topological_space big_operators ennreal measure_theory nnreal open set filter topological_space ennreal emetric measure_theory variables {α β γ δ : Type*} [measurable_space α] {μ ν : measure α} variables [normed_group β] variables [normed_group γ] namespace measure_theory /-! ### Some results about the Lebesgue integral involving a normed group -/ lemma lintegral_nnnorm_eq_lintegral_edist (f : α → β) : ∫⁻ a, nnnorm (f a) ∂μ = ∫⁻ a, edist (f a) 0 ∂μ := by simp only [edist_eq_coe_nnnorm] lemma lintegral_norm_eq_lintegral_edist (f : α → β) : ∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ = ∫⁻ a, edist (f a) 0 ∂μ := by simp only [of_real_norm_eq_coe_nnnorm, edist_eq_coe_nnnorm] lemma lintegral_edist_triangle [second_countable_topology β] [measurable_space β] [opens_measurable_space β] {f g h : α → β} (hf : ae_measurable f μ) (hg : ae_measurable g μ) (hh : ae_measurable h μ) : ∫⁻ a, edist (f a) (g a) ∂μ ≤ ∫⁻ a, edist (f a) (h a) ∂μ + ∫⁻ a, edist (g a) (h a) ∂μ := begin rw ← lintegral_add' (hf.edist hh) (hg.edist hh), refine lintegral_mono (λ a, _), apply edist_triangle_right end lemma lintegral_nnnorm_zero : ∫⁻ a : α, nnnorm (0 : β) ∂μ = 0 := by simp lemma lintegral_nnnorm_add [measurable_space β] [opens_measurable_space β] [measurable_space γ] [opens_measurable_space γ] {f : α → β} {g : α → γ} (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ∫⁻ a, nnnorm (f a) + nnnorm (g a) ∂μ = ∫⁻ a, nnnorm (f a) ∂μ + ∫⁻ a, nnnorm (g a) ∂μ := lintegral_add' hf.ennnorm hg.ennnorm lemma lintegral_nnnorm_neg {f : α → β} : ∫⁻ a, nnnorm ((-f) a) ∂μ = ∫⁻ a, nnnorm (f a) ∂μ := by simp only [pi.neg_apply, nnnorm_neg] /-! ### The predicate `has_finite_integral` -/ /-- `has_finite_integral f μ` means that the integral `∫⁻ a, ∥f a∥ ∂μ` is finite. `has_finite_integral f` means `has_finite_integral f volume`. -/ def has_finite_integral (f : α → β) (μ : measure α . volume_tac) : Prop := ∫⁻ a, nnnorm (f a) ∂μ < ∞ lemma has_finite_integral_iff_norm (f : α → β) : has_finite_integral f μ ↔ ∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ < ∞ := by simp only [has_finite_integral, of_real_norm_eq_coe_nnnorm] lemma has_finite_integral_iff_edist (f : α → β) : has_finite_integral f μ ↔ ∫⁻ a, edist (f a) 0 ∂μ < ∞ := by simp only [has_finite_integral_iff_norm, edist_dist, dist_zero_right] lemma has_finite_integral_iff_of_real {f : α → ℝ} (h : 0 ≤ᵐ[μ] f) : has_finite_integral f μ ↔ ∫⁻ a, ennreal.of_real (f a) ∂μ < ∞ := have lintegral_eq : ∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ = ∫⁻ a, ennreal.of_real (f a) ∂μ := begin refine lintegral_congr_ae (h.mono $ λ a h, _), rwa [real.norm_eq_abs, abs_of_nonneg] end, by rw [has_finite_integral_iff_norm, lintegral_eq] lemma has_finite_integral_iff_of_nnreal {f : α → ℝ≥0} : has_finite_integral (λ x, (f x : ℝ)) μ ↔ ∫⁻ a, f a ∂μ < ∞ := by simp [has_finite_integral_iff_norm] lemma has_finite_integral.mono {f : α → β} {g : α → γ} (hg : has_finite_integral g μ) (h : ∀ᵐ a ∂μ, ∥f a∥ ≤ ∥g a∥) : has_finite_integral f μ := begin simp only [has_finite_integral_iff_norm] at *, calc ∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ ≤ ∫⁻ (a : α), (ennreal.of_real ∥g a∥) ∂μ : lintegral_mono_ae (h.mono $ assume a h, of_real_le_of_real h) ... < ∞ : hg end lemma has_finite_integral.mono' {f : α → β} {g : α → ℝ} (hg : has_finite_integral g μ) (h : ∀ᵐ a ∂μ, ∥f a∥ ≤ g a) : has_finite_integral f μ := hg.mono $ h.mono $ λ x hx, le_trans hx (le_abs_self _) lemma has_finite_integral.congr' {f : α → β} {g : α → γ} (hf : has_finite_integral f μ) (h : ∀ᵐ a ∂μ, ∥f a∥ = ∥g a∥) : has_finite_integral g μ := hf.mono $ eventually_eq.le $ eventually_eq.symm h lemma has_finite_integral_congr' {f : α → β} {g : α → γ} (h : ∀ᵐ a ∂μ, ∥f a∥ = ∥g a∥) : has_finite_integral f μ ↔ has_finite_integral g μ := ⟨λ hf, hf.congr' h, λ hg, hg.congr' $ eventually_eq.symm h⟩ lemma has_finite_integral.congr {f g : α → β} (hf : has_finite_integral f μ) (h : f =ᵐ[μ] g) : has_finite_integral g μ := hf.congr' $ h.fun_comp norm lemma has_finite_integral_congr {f g : α → β} (h : f =ᵐ[μ] g) : has_finite_integral f μ ↔ has_finite_integral g μ := has_finite_integral_congr' $ h.fun_comp norm lemma has_finite_integral_const_iff {c : β} : has_finite_integral (λ x : α, c) μ ↔ c = 0 ∨ μ univ < ∞ := begin simp only [has_finite_integral, lintegral_const], by_cases hc : c = 0, { simp [hc] }, { simp only [hc, false_or], refine ⟨λ h, _, λ h, mul_lt_top coe_lt_top h⟩, replace h := mul_lt_top (@coe_lt_top $ (nnnorm c)⁻¹) h, rwa [← mul_assoc, ← coe_mul, _root_.inv_mul_cancel, coe_one, one_mul] at h, rwa [ne.def, nnnorm_eq_zero] } end lemma has_finite_integral_const [finite_measure μ] (c : β) : has_finite_integral (λ x : α, c) μ := has_finite_integral_const_iff.2 (or.inr $ measure_lt_top _ _) lemma has_finite_integral_of_bounded [finite_measure μ] {f : α → β} {C : ℝ} (hC : ∀ᵐ a ∂μ, ∥f a∥ ≤ C) : has_finite_integral f μ := (has_finite_integral_const C).mono' hC lemma has_finite_integral.mono_measure {f : α → β} (h : has_finite_integral f ν) (hμ : μ ≤ ν) : has_finite_integral f μ := lt_of_le_of_lt (lintegral_mono' hμ (le_refl _)) h lemma has_finite_integral.add_measure {f : α → β} (hμ : has_finite_integral f μ) (hν : has_finite_integral f ν) : has_finite_integral f (μ + ν) := begin simp only [has_finite_integral, lintegral_add_measure] at *, exact add_lt_top.2 ⟨hμ, hν⟩ end lemma has_finite_integral.left_of_add_measure {f : α → β} (h : has_finite_integral f (μ + ν)) : has_finite_integral f μ := h.mono_measure $ measure.le_add_right $ le_refl _ lemma has_finite_integral.right_of_add_measure {f : α → β} (h : has_finite_integral f (μ + ν)) : has_finite_integral f ν := h.mono_measure $ measure.le_add_left $ le_refl _ @[simp] lemma has_finite_integral_add_measure {f : α → β} : has_finite_integral f (μ + ν) ↔ has_finite_integral f μ ∧ has_finite_integral f ν := ⟨λ h, ⟨h.left_of_add_measure, h.right_of_add_measure⟩, λ h, h.1.add_measure h.2⟩ lemma has_finite_integral.smul_measure {f : α → β} (h : has_finite_integral f μ) {c : ℝ≥0∞} (hc : c < ∞) : has_finite_integral f (c • μ) := begin simp only [has_finite_integral, lintegral_smul_measure] at *, exact mul_lt_top hc h end @[simp] lemma has_finite_integral_zero_measure (f : α → β) : has_finite_integral f 0 := by simp only [has_finite_integral, lintegral_zero_measure, with_top.zero_lt_top] variables (α β μ) @[simp] lemma has_finite_integral_zero : has_finite_integral (λa:α, (0:β)) μ := by simp [has_finite_integral] variables {α β μ} lemma has_finite_integral.neg {f : α → β} (hfi : has_finite_integral f μ) : has_finite_integral (-f) μ := by simpa [has_finite_integral] using hfi @[simp] lemma has_finite_integral_neg_iff {f : α → β} : has_finite_integral (-f) μ ↔ has_finite_integral f μ := ⟨λ h, neg_neg f ▸ h.neg, has_finite_integral.neg⟩ lemma has_finite_integral.norm {f : α → β} (hfi : has_finite_integral f μ) : has_finite_integral (λa, ∥f a∥) μ := have eq : (λa, (nnnorm ∥f a∥ : ℝ≥0∞)) = λa, (nnnorm (f a) : ℝ≥0∞), by { funext, rw nnnorm_norm }, by { rwa [has_finite_integral, eq] } lemma has_finite_integral_norm_iff (f : α → β) : has_finite_integral (λa, ∥f a∥) μ ↔ has_finite_integral f μ := has_finite_integral_congr' $ eventually_of_forall $ λ x, norm_norm (f x) section dominated_convergence variables {F : ℕ → α → β} {f : α → β} {bound : α → ℝ} lemma all_ae_of_real_F_le_bound (h : ∀ n, ∀ᵐ a ∂μ, ∥F n a∥ ≤ bound a) : ∀ n, ∀ᵐ a ∂μ, ennreal.of_real ∥F n a∥ ≤ ennreal.of_real (bound a) := λn, (h n).mono $ λ a h, ennreal.of_real_le_of_real h lemma all_ae_tendsto_of_real_norm (h : ∀ᵐ a ∂μ, tendsto (λ n, F n a) at_top $ 𝓝 $ f a) : ∀ᵐ a ∂μ, tendsto (λn, ennreal.of_real ∥F n a∥) at_top $ 𝓝 $ ennreal.of_real ∥f a∥ := h.mono $ λ a h, tendsto_of_real $ tendsto.comp (continuous.tendsto continuous_norm _) h lemma all_ae_of_real_f_le_bound (h_bound : ∀ n, ∀ᵐ a ∂μ, ∥F n a∥ ≤ bound a) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) at_top (𝓝 (f a))) : ∀ᵐ a ∂μ, ennreal.of_real ∥f a∥ ≤ ennreal.of_real (bound a) := begin have F_le_bound := all_ae_of_real_F_le_bound h_bound, rw ← ae_all_iff at F_le_bound, apply F_le_bound.mp ((all_ae_tendsto_of_real_norm h_lim).mono _), assume a tendsto_norm F_le_bound, exact le_of_tendsto' tendsto_norm (F_le_bound) end lemma has_finite_integral_of_dominated_convergence {F : ℕ → α → β} {f : α → β} {bound : α → ℝ} (bound_has_finite_integral : has_finite_integral bound μ) (h_bound : ∀ n, ∀ᵐ a ∂μ, ∥F n a∥ ≤ bound a) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) at_top (𝓝 (f a))) : has_finite_integral f μ := /- `∥F n a∥ ≤ bound a` and `∥F n a∥ --> ∥f a∥` implies `∥f a∥ ≤ bound a`, and so `∫ ∥f∥ ≤ ∫ bound < ∞` since `bound` is has_finite_integral -/ begin rw has_finite_integral_iff_norm, calc ∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ ≤ ∫⁻ a, ennreal.of_real (bound a) ∂μ : lintegral_mono_ae $ all_ae_of_real_f_le_bound h_bound h_lim ... < ∞ : begin rw ← has_finite_integral_iff_of_real, { exact bound_has_finite_integral }, exact (h_bound 0).mono (λ a h, le_trans (norm_nonneg _) h) end end lemma tendsto_lintegral_norm_of_dominated_convergence [measurable_space β] [borel_space β] [second_countable_topology β] {F : ℕ → α → β} {f : α → β} {bound : α → ℝ} (F_measurable : ∀ n, ae_measurable (F n) μ) (f_measurable : ae_measurable f μ) (bound_has_finite_integral : has_finite_integral bound μ) (h_bound : ∀ n, ∀ᵐ a ∂μ, ∥F n a∥ ≤ bound a) (h_lim : ∀ᵐ a ∂μ, tendsto (λ n, F n a) at_top (𝓝 (f a))) : tendsto (λn, ∫⁻ a, (ennreal.of_real ∥F n a - f a∥) ∂μ) at_top (𝓝 0) := let b := λa, 2 * ennreal.of_real (bound a) in /- `∥F n a∥ ≤ bound a` and `F n a --> f a` implies `∥f a∥ ≤ bound a`, and thus by the triangle inequality, have `∥F n a - f a∥ ≤ 2 * (bound a). -/ have hb : ∀ n, ∀ᵐ a ∂μ, ennreal.of_real ∥F n a - f a∥ ≤ b a, begin assume n, filter_upwards [all_ae_of_real_F_le_bound h_bound n, all_ae_of_real_f_le_bound h_bound h_lim], assume a h₁ h₂, calc ennreal.of_real ∥F n a - f a∥ ≤ (ennreal.of_real ∥F n a∥) + (ennreal.of_real ∥f a∥) : begin rw [← ennreal.of_real_add], apply of_real_le_of_real, { apply norm_sub_le }, { exact norm_nonneg _ }, { exact norm_nonneg _ } end ... ≤ (ennreal.of_real (bound a)) + (ennreal.of_real (bound a)) : add_le_add h₁ h₂ ... = b a : by rw ← two_mul end, /- On the other hand, `F n a --> f a` implies that `∥F n a - f a∥ --> 0` -/ have h : ∀ᵐ a ∂μ, tendsto (λ n, ennreal.of_real ∥F n a - f a∥) at_top (𝓝 0), begin rw ← ennreal.of_real_zero, refine h_lim.mono (λ a h, (continuous_of_real.tendsto _).comp _), rwa ← tendsto_iff_norm_tendsto_zero end, /- Therefore, by the dominated convergence theorem for nonnegative integration, have ` ∫ ∥f a - F n a∥ --> 0 ` -/ begin suffices h : tendsto (λn, ∫⁻ a, (ennreal.of_real ∥F n a - f a∥) ∂μ) at_top (𝓝 (∫⁻ (a:α), 0 ∂μ)), { rwa lintegral_zero at h }, -- Using the dominated convergence theorem. refine tendsto_lintegral_of_dominated_convergence' _ _ hb _ _, -- Show `λa, ∥f a - F n a∥` is almost everywhere measurable for all `n` { exact λn, measurable_of_real.comp_ae_measurable ((F_measurable n).sub f_measurable).norm }, -- Show `2 * bound` is has_finite_integral { rw has_finite_integral_iff_of_real at bound_has_finite_integral, { calc ∫⁻ a, b a ∂μ = 2 * ∫⁻ a, ennreal.of_real (bound a) ∂μ : by { rw lintegral_const_mul', exact coe_ne_top } ... < ∞ : mul_lt_top (coe_lt_top) bound_has_finite_integral }, filter_upwards [h_bound 0] λ a h, le_trans (norm_nonneg _) h }, -- Show `∥f a - F n a∥ --> 0` { exact h } end end dominated_convergence section pos_part /-! Lemmas used for defining the positive part of a `L¹` function -/ lemma has_finite_integral.max_zero {f : α → ℝ} (hf : has_finite_integral f μ) : has_finite_integral (λa, max (f a) 0) μ := hf.mono $ eventually_of_forall $ λ x, by simp [real.norm_eq_abs, abs_le, abs_nonneg, le_abs_self] lemma has_finite_integral.min_zero {f : α → ℝ} (hf : has_finite_integral f μ) : has_finite_integral (λa, min (f a) 0) μ := hf.mono $ eventually_of_forall $ λ x, by simp [real.norm_eq_abs, abs_le, abs_nonneg, neg_le, neg_le_abs_self] end pos_part section normed_space variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 β] lemma has_finite_integral.smul (c : 𝕜) {f : α → β} : has_finite_integral f μ → has_finite_integral (c • f) μ := begin simp only [has_finite_integral], assume hfi, calc ∫⁻ (a : α), nnnorm (c • f a) ∂μ = ∫⁻ (a : α), (nnnorm c) * nnnorm (f a) ∂μ : by simp only [nnnorm_smul, ennreal.coe_mul] ... < ∞ : begin rw lintegral_const_mul', exacts [mul_lt_top coe_lt_top hfi, coe_ne_top] end end lemma has_finite_integral_smul_iff {c : 𝕜} (hc : c ≠ 0) (f : α → β) : has_finite_integral (c • f) μ ↔ has_finite_integral f μ := begin split, { assume h, simpa only [smul_smul, inv_mul_cancel hc, one_smul] using h.smul c⁻¹ }, exact has_finite_integral.smul _ end lemma has_finite_integral.const_mul {f : α → ℝ} (h : has_finite_integral f μ) (c : ℝ) : has_finite_integral (λ x, c * f x) μ := (has_finite_integral.smul c h : _) lemma has_finite_integral.mul_const {f : α → ℝ} (h : has_finite_integral f μ) (c : ℝ) : has_finite_integral (λ x, f x * c) μ := by simp_rw [mul_comm, h.const_mul _] end normed_space /-! ### The predicate `integrable` -/ variables [measurable_space β] [measurable_space γ] [measurable_space δ] /-- `integrable f μ` means that `f` is measurable and that the integral `∫⁻ a, ∥f a∥ ∂μ` is finite. `integrable f` means `integrable f volume`. -/ def integrable (f : α → β) (μ : measure α . volume_tac) : Prop := ae_measurable f μ ∧ has_finite_integral f μ lemma integrable.ae_measurable {f : α → β} (hf : integrable f μ) : ae_measurable f μ := hf.1 lemma integrable.has_finite_integral {f : α → β} (hf : integrable f μ) : has_finite_integral f μ := hf.2 lemma integrable.mono {f : α → β} {g : α → γ} (hg : integrable g μ) (hf : ae_measurable f μ) (h : ∀ᵐ a ∂μ, ∥f a∥ ≤ ∥g a∥) : integrable f μ := ⟨hf, hg.has_finite_integral.mono h⟩ lemma integrable.mono' {f : α → β} {g : α → ℝ} (hg : integrable g μ) (hf : ae_measurable f μ) (h : ∀ᵐ a ∂μ, ∥f a∥ ≤ g a) : integrable f μ := ⟨hf, hg.has_finite_integral.mono' h⟩ lemma integrable.congr' {f : α → β} {g : α → γ} (hf : integrable f μ) (hg : ae_measurable g μ) (h : ∀ᵐ a ∂μ, ∥f a∥ = ∥g a∥) : integrable g μ := ⟨hg, hf.has_finite_integral.congr' h⟩ lemma integrable_congr' {f : α → β} {g : α → γ} (hf : ae_measurable f μ) (hg : ae_measurable g μ) (h : ∀ᵐ a ∂μ, ∥f a∥ = ∥g a∥) : integrable f μ ↔ integrable g μ := ⟨λ h2f, h2f.congr' hg h, λ h2g, h2g.congr' hf $ eventually_eq.symm h⟩ lemma integrable.congr {f g : α → β} (hf : integrable f μ) (h : f =ᵐ[μ] g) : integrable g μ := ⟨hf.1.congr h, hf.2.congr h⟩ lemma integrable_congr {f g : α → β} (h : f =ᵐ[μ] g) : integrable f μ ↔ integrable g μ := ⟨λ hf, hf.congr h, λ hg, hg.congr h.symm⟩ lemma integrable_const_iff {c : β} : integrable (λ x : α, c) μ ↔ c = 0 ∨ μ univ < ∞ := begin have : ae_measurable (λ (x : α), c) μ := measurable_const.ae_measurable, rw [integrable, and_iff_right this, has_finite_integral_const_iff] end lemma integrable_const [finite_measure μ] (c : β) : integrable (λ x : α, c) μ := integrable_const_iff.2 $ or.inr $ measure_lt_top _ _ lemma integrable.mono_measure {f : α → β} (h : integrable f ν) (hμ : μ ≤ ν) : integrable f μ := ⟨h.ae_measurable.mono_measure hμ, h.has_finite_integral.mono_measure hμ⟩ lemma integrable.add_measure {f : α → β} (hμ : integrable f μ) (hν : integrable f ν) : integrable f (μ + ν) := ⟨hμ.ae_measurable.add_measure hν.ae_measurable, hμ.has_finite_integral.add_measure hν.has_finite_integral⟩ lemma integrable.left_of_add_measure {f : α → β} (h : integrable f (μ + ν)) : integrable f μ := h.mono_measure $ measure.le_add_right $ le_refl _ lemma integrable.right_of_add_measure {f : α → β} (h : integrable f (μ + ν)) : integrable f ν := h.mono_measure $ measure.le_add_left $ le_refl _ @[simp] lemma integrable_add_measure {f : α → β} : integrable f (μ + ν) ↔ integrable f μ ∧ integrable f ν := ⟨λ h, ⟨h.left_of_add_measure, h.right_of_add_measure⟩, λ h, h.1.add_measure h.2⟩ lemma integrable.smul_measure {f : α → β} (h : integrable f μ) {c : ℝ≥0∞} (hc : c < ∞) : integrable f (c • μ) := ⟨h.ae_measurable.smul_measure c, h.has_finite_integral.smul_measure hc⟩ lemma integrable_map_measure [opens_measurable_space β] {f : α → δ} {g : δ → β} (hg : ae_measurable g (measure.map f μ)) (hf : measurable f) : integrable g (measure.map f μ) ↔ integrable (g ∘ f) μ := by simp [integrable, hg, hg.comp_measurable hf, has_finite_integral, lintegral_map' hg.ennnorm hf] lemma lintegral_edist_lt_top [second_countable_topology β] [opens_measurable_space β] {f g : α → β} (hf : integrable f μ) (hg : integrable g μ) : ∫⁻ a, edist (f a) (g a) ∂μ < ∞ := lt_of_le_of_lt (lintegral_edist_triangle hf.ae_measurable hg.ae_measurable (measurable_const.ae_measurable : ae_measurable (λa, (0 : β)) μ)) (ennreal.add_lt_top.2 $ by { simp_rw ← has_finite_integral_iff_edist, exact ⟨hf.has_finite_integral, hg.has_finite_integral⟩ }) variables (α β μ) @[simp] lemma integrable_zero : integrable (λ _, (0 : β)) μ := by simp [integrable, measurable_const.ae_measurable] variables {α β μ} lemma integrable.add' [opens_measurable_space β] {f g : α → β} (hf : integrable f μ) (hg : integrable g μ) : has_finite_integral (f + g) μ := calc ∫⁻ a, nnnorm (f a + g a) ∂μ ≤ ∫⁻ a, nnnorm (f a) + nnnorm (g a) ∂μ : lintegral_mono (λ a, by exact_mod_cast nnnorm_add_le _ _) ... = _ : lintegral_nnnorm_add hf.ae_measurable hg.ae_measurable ... < ∞ : add_lt_top.2 ⟨hf.has_finite_integral, hg.has_finite_integral⟩ lemma integrable.add [borel_space β] [second_countable_topology β] {f g : α → β} (hf : integrable f μ) (hg : integrable g μ) : integrable (f + g) μ := ⟨hf.ae_measurable.add hg.ae_measurable, hf.add' hg⟩ lemma integrable_finset_sum {ι} [borel_space β] [second_countable_topology β] (s : finset ι) {f : ι → α → β} (hf : ∀ i, integrable (f i) μ) : integrable (λ a, ∑ i in s, f i a) μ := begin refine finset.induction_on s _ _, { simp only [finset.sum_empty, integrable_zero] }, { assume i s his ih, simp only [his, finset.sum_insert, not_false_iff], exact (hf _).add ih } end lemma integrable.neg [borel_space β] {f : α → β} (hf : integrable f μ) : integrable (-f) μ := ⟨hf.ae_measurable.neg, hf.has_finite_integral.neg⟩ @[simp] lemma integrable_neg_iff [borel_space β] {f : α → β} : integrable (-f) μ ↔ integrable f μ := ⟨λ h, neg_neg f ▸ h.neg, integrable.neg⟩ lemma integrable.sub' [opens_measurable_space β] {f g : α → β} (hf : integrable f μ) (hg : integrable g μ) : has_finite_integral (f - g) μ := calc ∫⁻ a, nnnorm (f a - g a) ∂μ ≤ ∫⁻ a, nnnorm (f a) + nnnorm (-g a) ∂μ : lintegral_mono (assume a, by { simp only [sub_eq_add_neg], exact_mod_cast nnnorm_add_le _ _ } ) ... = _ : by { simp only [nnnorm_neg], exact lintegral_nnnorm_add hf.ae_measurable hg.ae_measurable } ... < ∞ : add_lt_top.2 ⟨hf.has_finite_integral, hg.has_finite_integral⟩ lemma integrable.sub [borel_space β] [second_countable_topology β] {f g : α → β} (hf : integrable f μ) (hg : integrable g μ) : integrable (f - g) μ := by simpa only [sub_eq_add_neg] using hf.add hg.neg lemma integrable.norm [opens_measurable_space β] {f : α → β} (hf : integrable f μ) : integrable (λa, ∥f a∥) μ := ⟨hf.ae_measurable.norm, hf.has_finite_integral.norm⟩ lemma integrable_norm_iff [opens_measurable_space β] {f : α → β} (hf : ae_measurable f μ) : integrable (λa, ∥f a∥) μ ↔ integrable f μ := by simp_rw [integrable, and_iff_right hf, and_iff_right hf.norm, has_finite_integral_norm_iff] lemma integrable_of_norm_sub_le [opens_measurable_space β] {f₀ f₁ : α → β} {g : α → ℝ} (hf₁_m : ae_measurable f₁ μ) (hf₀_i : integrable f₀ μ) (hg_i : integrable g μ) (h : ∀ᵐ a ∂μ, ∥f₀ a - f₁ a∥ ≤ g a) : integrable f₁ μ := begin have : ∀ᵐ a ∂μ, ∥f₁ a∥ ≤ ∥f₀ a∥ + g a, { apply h.mono, intros a ha, calc ∥f₁ a∥ ≤ ∥f₀ a∥ + ∥f₀ a - f₁ a∥ : norm_le_insert _ _ ... ≤ ∥f₀ a∥ + g a : add_le_add_left ha _ }, exact integrable.mono' (hf₀_i.norm.add hg_i) hf₁_m this end lemma integrable.prod_mk [opens_measurable_space β] [opens_measurable_space γ] {f : α → β} {g : α → γ} (hf : integrable f μ) (hg : integrable g μ) : integrable (λ x, (f x, g x)) μ := ⟨hf.ae_measurable.prod_mk hg.ae_measurable, (hf.norm.add' hg.norm).mono $ eventually_of_forall $ λ x, calc max ∥f x∥ ∥g x∥ ≤ ∥f x∥ + ∥g x∥ : max_le_add_of_nonneg (norm_nonneg _) (norm_nonneg _) ... ≤ ∥(∥f x∥ + ∥g x∥)∥ : le_abs_self _⟩ lemma mem_ℒp_one_iff_integrable {f : α → β} : mem_ℒp f 1 μ ↔ integrable f μ := by simp_rw [integrable, has_finite_integral, mem_ℒp, snorm_one_eq_lintegral_nnnorm] lemma mem_ℒp.integrable [borel_space β] {q : ℝ≥0∞} (hq1 : 1 ≤ q) {f : α → β} [finite_measure μ] (hfq : mem_ℒp f q μ) : integrable f μ := mem_ℒp_one_iff_integrable.mp (hfq.mem_ℒp_of_exponent_le hq1) lemma lipschitz_with.integrable_comp_iff_of_antilipschitz [complete_space β] [borel_space β] [borel_space γ] {K K'} {f : α → β} {g : β → γ} (hg : lipschitz_with K g) (hg' : antilipschitz_with K' g) (g0 : g 0 = 0) : integrable (g ∘ f) μ ↔ integrable f μ := by simp [← mem_ℒp_one_iff_integrable, hg.mem_ℒp_comp_iff_of_antilipschitz hg' g0] lemma integrable.real_to_nnreal {f : α → ℝ} (hf : integrable f μ) : integrable (λ x, ((f x).to_nnreal : ℝ)) μ := begin refine ⟨hf.ae_measurable.real_to_nnreal.coe_nnreal_real, _⟩, rw has_finite_integral_iff_norm, refine lt_of_le_of_lt _ ((has_finite_integral_iff_norm _).1 hf.has_finite_integral), apply lintegral_mono, assume x, simp [real.norm_eq_abs, ennreal.of_real_le_of_real, abs_le, abs_nonneg, le_abs_self], end section pos_part /-! ### Lemmas used for defining the positive part of a `L¹` function -/ lemma integrable.max_zero {f : α → ℝ} (hf : integrable f μ) : integrable (λa, max (f a) 0) μ := ⟨hf.ae_measurable.max measurable_const.ae_measurable, hf.has_finite_integral.max_zero⟩ lemma integrable.min_zero {f : α → ℝ} (hf : integrable f μ) : integrable (λa, min (f a) 0) μ := ⟨hf.ae_measurable.min measurable_const.ae_measurable, hf.has_finite_integral.min_zero⟩ end pos_part section normed_space variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 β] [measurable_space 𝕜] [opens_measurable_space 𝕜] lemma integrable.smul [borel_space β] (c : 𝕜) {f : α → β} (hf : integrable f μ) : integrable (c • f) μ := ⟨hf.ae_measurable.const_smul c, hf.has_finite_integral.smul c⟩ lemma integrable_smul_iff [borel_space β] {c : 𝕜} (hc : c ≠ 0) (f : α → β) : integrable (c • f) μ ↔ integrable f μ := and_congr (ae_measurable_const_smul_iff' hc) (has_finite_integral_smul_iff hc f) lemma integrable.const_mul {f : α → ℝ} (h : integrable f μ) (c : ℝ) : integrable (λ x, c * f x) μ := integrable.smul c h lemma integrable.mul_const {f : α → ℝ} (h : integrable f μ) (c : ℝ) : integrable (λ x, f x * c) μ := by simp_rw [mul_comm, h.const_mul _] end normed_space section normed_space_over_complete_field variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] [complete_space 𝕜] [measurable_space 𝕜] variables [borel_space 𝕜] variables {E : Type*} [normed_group E] [normed_space 𝕜 E] [measurable_space E] [borel_space E] lemma integrable_smul_const {f : α → 𝕜} {c : E} (hc : c ≠ 0) : integrable (λ x, f x • c) μ ↔ integrable f μ := begin simp_rw [integrable, ae_measurable_smul_const hc, and.congr_right_iff, has_finite_integral, nnnorm_smul, ennreal.coe_mul], intro hf, rw [lintegral_mul_const' _ _ ennreal.coe_ne_top, ennreal.mul_lt_top_iff], have : ∀ x : ℝ≥0∞, x = 0 → x < ∞ := by simp, simp [hc, or_iff_left_of_imp (this _)] end end normed_space_over_complete_field section trim variables {H α' : Type*} [normed_group H] [measurable_space H] {m m0 : measurable_space α'} {μ' : measure α'} lemma integrable.trim (hm : m ≤ m0) [opens_measurable_space H] {f : α' → H} (hf_int : integrable f μ') (hf : @measurable _ _ m _ f) : @integrable _ _ m _ _ f (μ'.trim hm) := begin refine ⟨@measurable.ae_measurable α' _ m _ f (μ'.trim hm) hf, _⟩, rw [has_finite_integral, lintegral_trim hm _], { exact hf_int.2, }, { exact @measurable.coe_nnreal_ennreal α' m _ (@measurable.nnnorm _ α' _ _ _ m _ hf), }, end lemma integrable_of_integrable_trim (hm : m ≤ m0) [opens_measurable_space H] {f : α' → H} (hf_int : @integrable α' H m _ _ f (μ'.trim hm)) : integrable f μ' := begin obtain ⟨hf_meas_ae, hf⟩ := hf_int, refine ⟨ae_measurable_of_ae_measurable_trim hm hf_meas_ae, _⟩, rw has_finite_integral at hf ⊢, rwa lintegral_trim_ae hm _ at hf, exact @ae_measurable.coe_nnreal_ennreal α' m _ _ (@ae_measurable.nnnorm H α' _ _ _ m _ _ hf_meas_ae), end end trim /-! ### The predicate `integrable` on measurable functions modulo a.e.-equality -/ namespace ae_eq_fun section /-- A class of almost everywhere equal functions is `integrable` if its function representative is integrable. -/ def integrable (f : α →ₘ[μ] β) : Prop := integrable f μ lemma integrable_mk {f : α → β} (hf : ae_measurable f μ ) : (integrable (mk f hf : α →ₘ[μ] β)) ↔ measure_theory.integrable f μ := begin simp [integrable], apply integrable_congr, exact coe_fn_mk f hf end lemma integrable_coe_fn {f : α →ₘ[μ] β} : (measure_theory.integrable f μ) ↔ integrable f := by rw [← integrable_mk, mk_coe_fn] lemma integrable_zero : integrable (0 : α →ₘ[μ] β) := (integrable_zero α β μ).congr (coe_fn_mk _ _).symm end section variables [borel_space β] lemma integrable.neg {f : α →ₘ[μ] β} : integrable f → integrable (-f) := induction_on f $ λ f hfm hfi, (integrable_mk _).2 ((integrable_mk hfm).1 hfi).neg section variable [second_countable_topology β] lemma integrable_iff_mem_L1 {f : α →ₘ[μ] β} : integrable f ↔ f ∈ (α →₁[μ] β) := by rw [← integrable_coe_fn, ← mem_ℒp_one_iff_integrable, Lp.mem_Lp_iff_mem_ℒp] lemma integrable.add {f g : α →ₘ[μ] β} : integrable f → integrable g → integrable (f + g) := begin refine induction_on₂ f g (λ f hf g hg hfi hgi, _), simp only [integrable_mk, mk_add_mk] at hfi hgi ⊢, exact hfi.add hgi end lemma integrable.sub {f g : α →ₘ[μ] β} (hf : integrable f) (hg : integrable g) : integrable (f - g) := (sub_eq_add_neg f g).symm ▸ hf.add hg.neg end section normed_space variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 β] [measurable_space 𝕜] [opens_measurable_space 𝕜] lemma integrable.smul {c : 𝕜} {f : α →ₘ[μ] β} : integrable f → integrable (c • f) := induction_on f $ λ f hfm hfi, (integrable_mk _).2 $ ((integrable_mk hfm).1 hfi).smul _ end normed_space end end ae_eq_fun namespace L1 variables [second_countable_topology β] [borel_space β] lemma integrable_coe_fn (f : α →₁[μ] β) : integrable f μ := by { rw ← mem_ℒp_one_iff_integrable, exact Lp.mem_ℒp f } lemma has_finite_integral_coe_fn (f : α →₁[μ] β) : has_finite_integral f μ := (integrable_coe_fn f).has_finite_integral lemma measurable_coe_fn (f : α →₁[μ] β) : measurable f := Lp.measurable f lemma ae_measurable_coe_fn (f : α →₁[μ] β) : ae_measurable f μ := Lp.ae_measurable f lemma edist_def (f g : α →₁[μ] β) : edist f g = ∫⁻ a, edist (f a) (g a) ∂μ := by { simp [Lp.edist_def, snorm, snorm'], simp [edist_eq_coe_nnnorm_sub] } lemma dist_def (f g : α →₁[μ] β) : dist f g = (∫⁻ a, edist (f a) (g a) ∂μ).to_real := by { simp [Lp.dist_def, snorm, snorm'], simp [edist_eq_coe_nnnorm_sub] } lemma norm_def (f : α →₁[μ] β) : ∥f∥ = (∫⁻ a, nnnorm (f a) ∂μ).to_real := by { simp [Lp.norm_def, snorm, snorm'] } /-- Computing the norm of a difference between two L¹-functions. Note that this is not a special case of `norm_def` since `(f - g) x` and `f x - g x` are not equal (but only a.e.-equal). -/ lemma norm_sub_eq_lintegral (f g : α →₁[μ] β) : ∥f - g∥ = (∫⁻ x, (nnnorm (f x - g x) : ℝ≥0∞) ∂μ).to_real := begin rw [norm_def], congr' 1, rw lintegral_congr_ae, filter_upwards [Lp.coe_fn_sub f g], assume a ha, simp only [ha, pi.sub_apply], end lemma of_real_norm_eq_lintegral (f : α →₁[μ] β) : ennreal.of_real ∥f∥ = ∫⁻ x, (nnnorm (f x) : ℝ≥0∞) ∂μ := by { rw [norm_def, ennreal.of_real_to_real], rw [← ennreal.lt_top_iff_ne_top], exact has_finite_integral_coe_fn f } /-- Computing the norm of a difference between two L¹-functions. Note that this is not a special case of `of_real_norm_eq_lintegral` since `(f - g) x` and `f x - g x` are not equal (but only a.e.-equal). -/ lemma of_real_norm_sub_eq_lintegral (f g : α →₁[μ] β) : ennreal.of_real ∥f - g∥ = ∫⁻ x, (nnnorm (f x - g x) : ℝ≥0∞) ∂μ := begin simp_rw [of_real_norm_eq_lintegral, ← edist_eq_coe_nnnorm], apply lintegral_congr_ae, filter_upwards [Lp.coe_fn_sub f g], assume a ha, simp only [ha, pi.sub_apply], end end L1 namespace integrable variables [second_countable_topology β] [borel_space β] /-- Construct the equivalence class `[f]` of an integrable function `f`, as a member of the space `L1 β 1 μ`. -/ def to_L1 (f : α → β) (hf : integrable f μ) : α →₁[μ] β := (mem_ℒp_one_iff_integrable.2 hf).to_Lp f @[simp] lemma to_L1_coe_fn (f : α →₁[μ] β) (hf : integrable f μ) : hf.to_L1 f = f := by simp [integrable.to_L1] lemma coe_fn_to_L1 {f : α → β} (hf : integrable f μ) : hf.to_L1 f =ᵐ[μ] f := ae_eq_fun.coe_fn_mk _ _ @[simp] lemma to_L1_zero (h : integrable (0 : α → β) μ) : h.to_L1 0 = 0 := rfl @[simp] lemma to_L1_eq_mk (f : α → β) (hf : integrable f μ) : (hf.to_L1 f : α →ₘ[μ] β) = ae_eq_fun.mk f hf.ae_measurable := rfl @[simp] lemma to_L1_eq_to_L1_iff (f g : α → β) (hf : integrable f μ) (hg : integrable g μ) : to_L1 f hf = to_L1 g hg ↔ f =ᵐ[μ] g := mem_ℒp.to_Lp_eq_to_Lp_iff _ _ lemma to_L1_add (f g : α → β) (hf : integrable f μ) (hg : integrable g μ) : to_L1 (f + g) (hf.add hg) = to_L1 f hf + to_L1 g hg := rfl lemma to_L1_neg (f : α → β) (hf : integrable f μ) : to_L1 (- f) (integrable.neg hf) = - to_L1 f hf := rfl lemma to_L1_sub (f g : α → β) (hf : integrable f μ) (hg : integrable g μ) : to_L1 (f - g) (hf.sub hg) = to_L1 f hf - to_L1 g hg := rfl lemma norm_to_L1 (f : α → β) (hf : integrable f μ) : ∥hf.to_L1 f∥ = ennreal.to_real (∫⁻ a, edist (f a) 0 ∂μ) := by { simp [to_L1, snorm, snorm'], simp [edist_eq_coe_nnnorm] } lemma norm_to_L1_eq_lintegral_norm (f : α → β) (hf : integrable f μ) : ∥hf.to_L1 f∥ = ennreal.to_real (∫⁻ a, (ennreal.of_real ∥f a∥) ∂μ) := by { rw [norm_to_L1, lintegral_norm_eq_lintegral_edist] } @[simp] lemma edist_to_L1_to_L1 (f g : α → β) (hf : integrable f μ) (hg : integrable g μ) : edist (hf.to_L1 f) (hg.to_L1 g) = ∫⁻ a, edist (f a) (g a) ∂μ := by { simp [integrable.to_L1, snorm, snorm'], simp [edist_eq_coe_nnnorm_sub] } @[simp] lemma edist_to_L1_zero (f : α → β) (hf : integrable f μ) : edist (hf.to_L1 f) 0 = ∫⁻ a, edist (f a) 0 ∂μ := by { simp [integrable.to_L1, snorm, snorm'], simp [edist_eq_coe_nnnorm] } variables {𝕜 : Type*} [normed_field 𝕜] [normed_space 𝕜 β] [measurable_space 𝕜] [opens_measurable_space 𝕜] lemma to_L1_smul (f : α → β) (hf : integrable f μ) (k : 𝕜) : to_L1 (λa, k • f a) (hf.smul k) = k • to_L1 f hf := rfl end integrable end measure_theory open measure_theory lemma integrable_zero_measure [measurable_space β] {f : α → β} : integrable f 0 := begin apply (integrable_zero _ _ _).congr, change (0 : measure α) {x | 0 ≠ f x} = 0, refl, end variables {E : Type*} [normed_group E] [measurable_space E] [borel_space E] [normed_space ℝ E] {H : Type*} [normed_group H] [normed_space ℝ H] lemma measure_theory.integrable.apply_continuous_linear_map {φ : α → H →L[ℝ] E} (φ_int : integrable φ μ) (v : H) : integrable (λ a, φ a v) μ := (φ_int.norm.mul_const ∥v∥).mono' (φ_int.ae_measurable.apply_continuous_linear_map v) (eventually_of_forall $ λ a, (φ a).le_op_norm v) variables {𝕜 : Type*} [is_R_or_C 𝕜] {G : Type*} [normed_group G] [normed_space 𝕜 G] [measurable_space G] [borel_space G] {F : Type*} [normed_group F] [normed_space 𝕜 F] [measurable_space F] [opens_measurable_space F] lemma continuous_linear_map.integrable_comp {φ : α → F} (L : F →L[𝕜] G) (φ_int : integrable φ μ) : integrable (λ (a : α), L (φ a)) μ := ((integrable.norm φ_int).const_mul ∥L∥).mono' (L.measurable.comp_ae_measurable φ_int.ae_measurable) (eventually_of_forall $ λ a, L.le_op_norm (φ a))
16462af4131507c90d3504d7f0a25664fd7697c9
37683ecbb27d7c2037bfd9ad7e06d662f460a005
/homotopy/susp.hlean
285653a32a4da545d8451128a4540a33c243bfc4
[ "Apache-2.0" ]
permissive
GRSEB9S/Spectral-1
b2443b09cae7aac1247b1f88c846c532ac802b8e
dd14277e0bfc6270a488eb3b9ec231484065b9d8
refs/heads/master
1,631,315,269,407
1,522,048,315,000
1,522,799,803,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,495
hlean
import .pushout types.pointed2 ..move_to_lib open susp eq pointed function is_equiv lift equiv is_trunc nat namespace susp variables {X X' Y Y' Z : Type*} definition iterate_susp_iterate_susp_rev (n m : ℕ) (A : Type*) : iterate_susp n (iterate_susp m A) ≃* iterate_susp (m + n) A := begin induction n with n e, { reflexivity }, { exact susp_pequiv e } end definition iterate_susp_pequiv [constructor] (n : ℕ) {X Y : Type*} (f : X ≃* Y) : iterate_susp n X ≃* iterate_susp n Y := begin induction n with n e, { exact f }, { exact susp_pequiv e } end open algebra nat definition iterate_susp_iterate_susp (n m : ℕ) (A : Type*) : iterate_susp n (iterate_susp m A) ≃* iterate_susp (n + m) A := iterate_susp_iterate_susp_rev n m A ⬝e* pequiv_of_eq (ap (λk, iterate_susp k A) (add.comm m n)) definition plift_susp.{u v} : Π(A : Type*), plift.{u v} (susp A) ≃* susp (plift.{u v} A) := begin intro A, calc plift.{u v} (susp A) ≃* susp A : by exact (pequiv_plift (susp A))⁻¹ᵉ* ... ≃* susp (plift.{u v} A) : by exact susp_pequiv (pequiv_plift.{u v} A) end definition is_contr_susp [instance] (A : Type) [H : is_contr A] : is_contr (susp A) := begin apply is_contr.mk north, intro x, induction x, reflexivity, exact merid !center, apply eq_pathover_constant_left_id_right, apply square_of_eq, exact whisker_left idp (ap merid !eq_of_is_contr) end definition loop_susp_pintro_phomotopy {X Y : Type*} {f g : ⅀ X →* Y} (p : f ~* g) : loop_susp_pintro X Y f ~* loop_susp_pintro X Y g := pwhisker_right (loop_susp_unit X) (Ω⇒ p) variables {A₀₀ A₂₀ A₀₂ A₂₂ : Type*} {f₁₀ : A₀₀ →* A₂₀} {f₁₂ : A₀₂ →* A₂₂} {f₀₁ : A₀₀ →* A₀₂} {f₂₁ : A₂₀ →* A₂₂} -- rename: susp_functor_psquare definition suspend_psquare (p : psquare f₁₀ f₁₂ f₀₁ f₂₁) : psquare (⅀→ f₁₀) (⅀→ f₁₂) (⅀→ f₀₁) (⅀→ f₂₁) := sorry definition susp_to_loop_psquare (f₁₀ : A₀₀ →* A₂₀) (f₁₂ : A₀₂ →* A₂₂) (f₀₁ : susp A₀₀ →* A₀₂) (f₂₁ : susp A₂₀ →* A₂₂) : (psquare (⅀→ f₁₀) f₁₂ f₀₁ f₂₁) → (psquare f₁₀ (Ω→ f₁₂) ((loop_susp_pintro A₀₀ A₀₂) f₀₁) ((loop_susp_pintro A₂₀ A₂₂) f₂₁)) := begin intro p, refine pvconcat _ (ap1_psquare p), exact loop_susp_unit_natural f₁₀ end definition loop_to_susp_square (f₁₀ : A₀₀ →* A₂₀) (f₁₂ : A₀₂ →* A₂₂) (f₀₁ : A₀₀ →* Ω A₀₂) (f₂₁ : A₂₀ →* Ω A₂₂) : (psquare f₁₀ (Ω→ f₁₂) f₀₁ f₂₁) → (psquare (⅀→ f₁₀) f₁₂ ((susp_pelim A₀₀ A₀₂) f₀₁) ((susp_pelim A₂₀ A₂₂) f₂₁)) := begin intro p, refine pvconcat (suspend_psquare p) _, exact psquare_transpose (loop_susp_counit_natural f₁₂) end open pushout unit prod sigma sigma.ops section parameters {A : Type*} {n : ℕ} [HA : is_conn n A] -- we end up not using this, because to prove that the -- composition with the first projection is loop_susp_counit A -- is hideous without HIT computations on path constructors parameter (A) definition pullback_diagonal_prod_of_wedge : susp (Ω A) ≃ Σ (a : A) (w : wedge A A), prod_of_wedge w = (a, a) := begin refine equiv.trans _ (comm_equiv_unc (λ z, prod_of_wedge (prod.pr1 z) = (prod.pr2 z, prod.pr2 z))), apply equiv.symm, apply equiv.trans (sigma_equiv_sigma_right (λ w, sigma_equiv_sigma_right (λ a, prod_eq_equiv (prod_of_wedge w) (a, a)))), apply equiv.trans !pushout.flattening', esimp, fapply pushout.equiv (λ z, ⟨pt, z.2⟩) (λ z, ⟨pt, glue z.1 ▸ z.2⟩) (λ p, star) (λ p, star), { apply equiv.trans !sigma_unit_left, fapply equiv.MK, { intro z, induction z with a w, induction w with p q, exact p ⬝ q⁻¹ }, { intro p, exact ⟨pt, (p, idp)⟩ }, { intro p, reflexivity }, { intro z, induction z with a w, induction w with p q, induction q, reflexivity } }, { fapply equiv.MK, { intro z, exact star }, { intro u, exact ⟨pt, ⟨pt, (idp, idp)⟩ ⟩ }, { intro u, induction u, reflexivity }, { intro z, induction z with a w, induction w with b z, induction z with p q, induction p, esimp at q, induction q, reflexivity } }, { fapply equiv.MK, { intro z, exact star }, { intro u, exact ⟨pt, ⟨pt, (idp, idp)⟩ ⟩ }, { intro u, induction u, reflexivity }, { intro z, induction z with a w, induction w with b z, induction z with p q, induction q, esimp at p, induction p, reflexivity } }, { intro z, induction z with u w, induction u, induction w with a z, induction z with p q, reflexivity }, { intro z, induction z with u w, induction u, induction w with a z, induction z with p q, reflexivity } end parameter {A} -- instead we directly compare the fibers, using flattening twice definition fiber_loop_susp_counit_equiv (a : A) : fiber (loop_susp_counit A) a ≃ fiber prod_of_wedge (a, a) := begin apply equiv.trans !fiber.sigma_char, apply equiv.trans !pushout.flattening', apply equiv.symm, apply equiv.trans !fiber.sigma_char, apply equiv.trans (sigma_equiv_sigma_right (λ w, prod_eq_equiv (prod_of_wedge w) (a, a))), esimp, apply equiv.trans !pushout.flattening', esimp, fapply pushout.equiv (λ z, ⟨pt, z.2⟩) (λ z, ⟨pt, glue z.1 ▸ z.2⟩) (λ z, ⟨star, z.2⟩) (λ z, ⟨star, glue z.1 ▸ z.2⟩), { fapply equiv.MK, { intro w, induction w with u z, induction z with p q, exact ⟨q ⬝ p⁻¹, q⟩ }, { intro z, induction z with p q, apply dpair star, exact (p⁻¹ ⬝ q, q) }, { intro z, induction z with p q, esimp, induction q, esimp, rewrite [idp_con,inv_inv] }, { intro w, induction w with u z, induction u, induction z with p q, esimp, induction q, rewrite [idp_con,inv_inv] } }, { fapply equiv.MK, { intro w, induction w with b z, induction z with p q, exact ⟨star, q⟩ }, { intro z, induction z with u p, induction u, esimp at p, esimp, apply dpair a, esimp, exact (idp, p) }, { intro z, induction z with u p, induction u, reflexivity }, { intro w, induction w with b z, induction z with p q, esimp, induction p, reflexivity } }, { fapply equiv.MK, { intro w, induction w with b z, induction z with p q, exact ⟨star, p⟩ }, { intro z, induction z with u p, induction u, esimp at p, esimp, apply dpair a, esimp, exact (p, idp) }, { intro z, induction z with u p, induction u, reflexivity }, { intro w, induction w with b z, induction z with p q, esimp, induction q, reflexivity } }, { intro w, induction w with u z, induction u, induction z with p q, reflexivity }, { intro w, induction w with u z, induction u, induction z with p q, esimp, induction q, esimp, krewrite prod_transport, fapply sigma_eq, { exact idp }, { esimp, rewrite eq_transport_Fl, rewrite eq_transport_Fl, krewrite elim_glue, krewrite (ap_compose' pr1 prod_of_wedge (glue star)), krewrite elim_glue, esimp, apply eq_pathover, rewrite idp_con, esimp, apply square_of_eq, rewrite [idp_con,idp_con,inv_inv] } } end include HA open is_conn trunc_index parameter (A) -- connectivity of loop_susp_counit definition is_conn_fun_loop_susp_counit {k : ℕ} (H : k ≤ 2 * n) : is_conn_fun k (loop_susp_counit A) := begin intro a, apply is_conn.is_conn_equiv_closed_rev k (fiber_loop_susp_counit_equiv a), fapply @is_conn.is_conn_of_le (fiber prod_of_wedge (a, a)) k (2 * n) (of_nat_le_of_nat H), assert H : of_nat (2 * n) = of_nat n + of_nat n, { rewrite (of_nat_add_of_nat n n), apply ap of_nat, apply trans (nat.mul_comm 2 n), apply ap (λ k, k + n), exact nat.zero_add n }, rewrite H, exact is_conn_fun_prod_of_wedge n n A A (a, a) end end end susp
014e8c283b105e97de118ec534ebe6a80d8b0008
4727251e0cd73359b15b664c3170e5d754078599
/src/order/prime_ideal.lean
09e5457b2ea8054d71bb992b2b50e30f0f807119
[ "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
6,176
lean
/- Copyright (c) 2021 Noam Atar. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Noam Atar -/ import order.ideal import order.pfilter /-! # Prime ideals ## Main definitions Throughout this file, `P` is at least a preorder, but some sections require more structure, such as a bottom element, a top element, or a join-semilattice structure. - `order.ideal.prime_pair`: A pair of an `ideal` and a `pfilter` which form a partition of `P`. This is useful as giving the data of a prime ideal is the same as giving the data of a prime filter. - `order.ideal.is_prime`: a predicate for prime ideals. Dual to the notion of a prime filter. - `order.pfilter.is_prime`: a predicate for prime filters. Dual to the notion of a prime ideal. ## References - <https://en.wikipedia.org/wiki/Ideal_(order_theory)> ## Tags ideal, prime -/ open order.pfilter namespace order variables {P : Type*} namespace ideal /-- A pair of an `ideal` and a `pfilter` which form a partition of `P`. -/ @[nolint has_inhabited_instance] structure prime_pair (P : Type*) [preorder P] := (I : ideal P) (F : pfilter P) (is_compl_I_F : is_compl (I : set P) F) namespace prime_pair variables [preorder P] (IF : prime_pair P) lemma compl_I_eq_F : (IF.I : set P)ᶜ = IF.F := IF.is_compl_I_F.compl_eq lemma compl_F_eq_I : (IF.F : set P)ᶜ = IF.I := IF.is_compl_I_F.eq_compl.symm lemma I_is_proper : is_proper IF.I := begin cases IF.F.nonempty, apply is_proper_of_not_mem (_ : w ∉ IF.I), rwa ← IF.compl_I_eq_F at h, end lemma disjoint : disjoint (IF.I : set P) IF.F := IF.is_compl_I_F.disjoint lemma I_union_F : (IF.I : set P) ∪ IF.F = set.univ := IF.is_compl_I_F.sup_eq_top lemma F_union_I : (IF.F : set P) ∪ IF.I = set.univ := IF.is_compl_I_F.symm.sup_eq_top end prime_pair /-- An ideal `I` is prime if its complement is a filter. -/ @[mk_iff] class is_prime [preorder P] (I : ideal P) extends is_proper I : Prop := (compl_filter : is_pfilter (I : set P)ᶜ) section preorder variable [preorder P] /-- Create an element of type `order.ideal.prime_pair` from an ideal satisfying the predicate `order.ideal.is_prime`. -/ def is_prime.to_prime_pair {I : ideal P} (h : is_prime I) : prime_pair P := { I := I, F := h.compl_filter.to_pfilter, is_compl_I_F := is_compl_compl } lemma prime_pair.I_is_prime (IF : prime_pair P) : is_prime IF.I := { compl_filter := by { rw IF.compl_I_eq_F, exact IF.F.is_pfilter }, ..IF.I_is_proper } end preorder section semilattice_inf variables [semilattice_inf P] {x y : P} {I : ideal P} lemma is_prime.mem_or_mem (hI : is_prime I) {x y : P} : x ⊓ y ∈ I → x ∈ I ∨ y ∈ I := begin contrapose!, let F := hI.compl_filter.to_pfilter, show x ∈ F ∧ y ∈ F → x ⊓ y ∈ F, exact λ h, inf_mem h.1 h.2, end lemma is_prime.of_mem_or_mem [is_proper I] (hI : ∀ {x y : P}, x ⊓ y ∈ I → x ∈ I ∨ y ∈ I) : is_prime I := begin rw is_prime_iff, use ‹_›, apply is_pfilter.of_def, { exact set.nonempty_compl.2 (I.is_proper_iff.1 ‹_›) }, { intros x _ y _, refine ⟨x ⊓ y, _, inf_le_left, inf_le_right⟩, have := mt hI, tauto! }, { exact @mem_compl_of_ge _ _ _ } end lemma is_prime_iff_mem_or_mem [is_proper I] : is_prime I ↔ ∀ {x y : P}, x ⊓ y ∈ I → x ∈ I ∨ y ∈ I := ⟨is_prime.mem_or_mem, is_prime.of_mem_or_mem⟩ end semilattice_inf section distrib_lattice variables [distrib_lattice P] {I : ideal P} @[priority 100] instance is_maximal.is_prime [is_maximal I] : is_prime I := begin rw is_prime_iff_mem_or_mem, intros x y, contrapose!, rintro ⟨hx, hynI⟩ hxy, apply hynI, let J := I ⊔ principal x, have hJuniv : (J : set P) = set.univ := is_maximal.maximal_proper (lt_sup_principal_of_not_mem ‹_›), have hyJ : y ∈ ↑J := set.eq_univ_iff_forall.mp hJuniv y, rw coe_sup_eq at hyJ, rcases hyJ with ⟨a, ha, b, hb, hy⟩, rw hy, refine sup_mem ha (I.lower (le_inf hb _) hxy), rw hy, exact le_sup_right end end distrib_lattice section boolean_algebra variables [boolean_algebra P] {x : P} {I : ideal P} lemma is_prime.mem_or_compl_mem (hI : is_prime I) : x ∈ I ∨ xᶜ ∈ I := begin apply hI.mem_or_mem, rw inf_compl_eq_bot, exact I.bot_mem, end lemma is_prime.mem_compl_of_not_mem (hI : is_prime I) (hxnI : x ∉ I) : xᶜ ∈ I := hI.mem_or_compl_mem.resolve_left hxnI lemma is_prime_of_mem_or_compl_mem [is_proper I] (h : ∀ {x : P}, x ∈ I ∨ xᶜ ∈ I) : is_prime I := begin simp only [is_prime_iff_mem_or_mem, or_iff_not_imp_left], intros x y hxy hxI, have hxcI : xᶜ ∈ I := h.resolve_left hxI, have ass : (x ⊓ y) ⊔ (y ⊓ xᶜ) ∈ I := sup_mem hxy (I.lower inf_le_right hxcI), rwa [inf_comm, sup_inf_inf_compl] at ass end lemma is_prime_iff_mem_or_compl_mem [is_proper I] : is_prime I ↔ ∀ {x : P}, x ∈ I ∨ xᶜ ∈ I := ⟨λ h _, h.mem_or_compl_mem, is_prime_of_mem_or_compl_mem⟩ @[priority 100] instance is_prime.is_maximal [is_prime I] : is_maximal I := begin simp only [is_maximal_iff, set.eq_univ_iff_forall, is_prime.to_is_proper, true_and], intros J hIJ x, rcases set.exists_of_ssubset hIJ with ⟨y, hyJ, hyI⟩, suffices ass : (x ⊓ y) ⊔ (x ⊓ yᶜ) ∈ J, { rwa sup_inf_inf_compl at ass }, exact sup_mem (J.lower inf_le_right hyJ) (hIJ.le $ I.lower inf_le_right $ is_prime.mem_compl_of_not_mem ‹_› hyI), end end boolean_algebra end ideal namespace pfilter variable [preorder P] /-- A filter `F` is prime if its complement is an ideal. -/ @[mk_iff] class is_prime (F : pfilter P) : Prop := (compl_ideal : is_ideal (F : set P)ᶜ) /-- Create an element of type `order.ideal.prime_pair` from a filter satisfying the predicate `order.pfilter.is_prime`. -/ def is_prime.to_prime_pair {F : pfilter P} (h : is_prime F) : ideal.prime_pair P := { I := h.compl_ideal.to_ideal, F := F, is_compl_I_F := is_compl_compl.symm } lemma _root_.order.ideal.prime_pair.F_is_prime (IF : ideal.prime_pair P) : is_prime IF.F := { compl_ideal := by { rw IF.compl_F_eq_I, exact IF.I.is_ideal } } end pfilter end order