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
e13e5f2b90c8f31dbb54aa49d9604f16e261ed4c
367134ba5a65885e863bdc4507601606690974c1
/src/category_theory/elements.lean
b73e5d4ef434a195bb0cac04f12ff85f1a9d0f5d
[ "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
4,898
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.comma import category_theory.groupoid import category_theory.punit /-! # The category of elements This file defines the category of elements, also known as (a special case of) the Grothendieck construction. Given a functor `F : C ⥤ Type`, an object of `F.elements` is a pair `(X : C, x : F.obj X)`. A morphism `(X, x) ⟶ (Y, y)` is a morphism `f : X ⟶ Y` in `C`, so `F.map f` takes `x` to `y`. ## Implementation notes This construction is equivalent to a special case of a comma construction, so this is mostly just a more convenient API. We prove the equivalence in `category_theory.category_of_elements.comma_equivalence`. ## References * [Emily Riehl, *Category Theory in Context*, Section 2.4][riehl2017] * <https://en.wikipedia.org/wiki/Category_of_elements> * <https://ncatlab.org/nlab/show/category+of+elements> ## Tags category of elements, Grothendieck construction, comma category -/ namespace category_theory universes w v u variables {C : Type u} [category.{v} C] /-- The type of objects for the category of elements of a functor `F : C ⥤ Type` is a pair `(X : C, x : F.obj X)`. -/ @[nolint has_inhabited_instance] def functor.elements (F : C ⥤ Type w) := (Σ c : C, F.obj c) /-- The category structure on `F.elements`, for `F : C ⥤ Type`. A morphism `(X, x) ⟶ (Y, y)` is a morphism `f : X ⟶ Y` in `C`, so `F.map f` takes `x` to `y`. -/ instance category_of_elements (F : C ⥤ Type w) : category.{v} F.elements := { hom := λ p q, { f : p.1 ⟶ q.1 // (F.map f) p.2 = q.2 }, id := λ p, ⟨𝟙 p.1, by obviously⟩, comp := λ p q r f g, ⟨f.val ≫ g.val, by obviously⟩ } namespace category_of_elements @[ext] lemma ext (F : C ⥤ Type w) {x y : F.elements} (f g : x ⟶ y) (w : f.val = g.val) : f = g := subtype.ext_val w @[simp] lemma comp_val {F : C ⥤ Type w} {p q r : F.elements} {f : p ⟶ q} {g : q ⟶ r} : (f ≫ g).val = f.val ≫ g.val := rfl @[simp] lemma id_val {F : C ⥤ Type w} {p : F.elements} : (𝟙 p : p ⟶ p).val = 𝟙 p.1 := rfl end category_of_elements instance groupoid_of_elements {G : Type u} [groupoid.{v} G] (F : G ⥤ Type w) : groupoid F.elements := { inv := λ p q f, ⟨inv f.val, calc F.map (inv f.val) q.2 = F.map (inv f.val) (F.map f.val p.2) : by rw f.2 ... = (F.map f.val ≫ F.map (inv f.val)) p.2 : by simp ... = p.2 : by {rw ←functor.map_comp, simp}⟩, } namespace category_of_elements variable (F : C ⥤ Type w) /-- The functor out of the category of elements which forgets the element. -/ @[simps] def π : F.elements ⥤ C := { obj := λ X, X.1, map := λ X Y f, f.val } /-- A natural transformation between functors induces a functor between the categories of elements. -/ @[simps] def map {F₁ F₂ : C ⥤ Type w} (α : F₁ ⟶ F₂) : F₁.elements ⥤ F₂.elements := { obj := λ t, ⟨t.1, α.app t.1 t.2⟩, map := λ t₁ t₂ k, ⟨k.1, by simpa [←k.2] using (functor_to_types.naturality _ _ α k.1 t₁.2).symm⟩ } @[simp] lemma map_π {F₁ F₂ : C ⥤ Type w} (α : F₁ ⟶ F₂) : map α ⋙ π F₂ = π F₁ := rfl /-- The forward direction of the equivalence `F.elements ≅ (*, F)`. -/ def to_comma : F.elements ⥤ comma (functor.from_punit punit) F := { obj := λ X, { left := punit.star, right := X.1, hom := λ _, X.2 }, map := λ X Y f, { right := f.val } } @[simp] lemma to_comma_obj (X) : (to_comma F).obj X = { left := punit.star, right := X.1, hom := λ _, X.2 } := rfl @[simp] lemma to_comma_map {X Y} (f : X ⟶ Y) : (to_comma F).map f = { right := f.val } := rfl /-- The reverse direction of the equivalence `F.elements ≅ (*, F)`. -/ def from_comma : comma (functor.from_punit punit) F ⥤ F.elements := { obj := λ X, ⟨X.right, X.hom (punit.star)⟩, map := λ X Y f, ⟨f.right, congr_fun f.w'.symm punit.star⟩ } @[simp] lemma from_comma_obj (X) : (from_comma F).obj X = ⟨X.right, X.hom (punit.star)⟩ := rfl @[simp] lemma from_comma_map {X Y} (f : X ⟶ Y) : (from_comma F).map f = ⟨f.right, congr_fun f.w'.symm punit.star⟩ := rfl /-- The equivalence between the category of elements `F.elements` and the comma category `(*, F)`. -/ def comma_equivalence : F.elements ≌ comma (functor.from_punit punit) F := equivalence.mk (to_comma F) (from_comma F) (nat_iso.of_components (λ X, eq_to_iso (by tidy)) (by tidy)) (nat_iso.of_components (λ X, { hom := { right := 𝟙 _ }, inv := { right := 𝟙 _ } }) (by tidy)) @[simp] lemma comma_equivalence_functor : (comma_equivalence F).functor = to_comma F := rfl @[simp] lemma comma_equivalence_inverse : (comma_equivalence F).inverse = from_comma F := rfl end category_of_elements end category_theory
4dd488e7d104eee70ae293259f7a2154267c6c8f
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/ring_theory/ideal/prod.lean
9b20409db40e9b635f219db880b0e904fe433cd2
[]
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
5,402
lean
/- Copyright (c) 2020 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.ring_theory.ideal.operations import Mathlib.PostPort universes u v namespace Mathlib /-! # Ideals in product rings For commutative rings `R` and `S` and ideals `I ≤ R`, `J ≤ S`, we define `ideal.prod I J` as the product `I × J`, viewed as an ideal of `R × S`. In `ideal_prod_eq` we show that every ideal of `R × S` is of this form. Furthermore, we show that every prime ideal of `R × S` is of the form `p × S` or `R × p`, where `p` is a prime ideal. -/ namespace ideal /-- `I × J` as an ideal of `R × S`. -/ def prod {R : Type u} {S : Type v} [comm_ring R] [comm_ring S] (I : ideal R) (J : ideal S) : ideal (R × S) := submodule.mk (set_of fun (x : R × S) => prod.fst x ∈ I ∧ prod.snd x ∈ J) sorry sorry sorry @[simp] theorem mem_prod {R : Type u} {S : Type v} [comm_ring R] [comm_ring S] (I : ideal R) (J : ideal S) {r : R} {s : S} : (r, s) ∈ prod I J ↔ r ∈ I ∧ s ∈ J := iff.rfl @[simp] theorem prod_top_top {R : Type u} {S : Type v} [comm_ring R] [comm_ring S] : prod ⊤ ⊤ = ⊤ := sorry /-- Every ideal of the product ring is of the form `I × J`, where `I` and `J` can be explicitly given as the image under the projection maps. -/ theorem ideal_prod_eq {R : Type u} {S : Type v} [comm_ring R] [comm_ring S] (I : ideal (R × S)) : I = prod (map (ring_hom.fst R S) I) (map (ring_hom.snd R S) I) := sorry @[simp] theorem map_fst_prod {R : Type u} {S : Type v} [comm_ring R] [comm_ring S] (I : ideal R) (J : ideal S) : map (ring_hom.fst R S) (prod I J) = I := sorry @[simp] theorem map_snd_prod {R : Type u} {S : Type v} [comm_ring R] [comm_ring S] (I : ideal R) (J : ideal S) : map (ring_hom.snd R S) (prod I J) = J := sorry @[simp] theorem map_prod_comm_prod {R : Type u} {S : Type v} [comm_ring R] [comm_ring S] (I : ideal R) (J : ideal S) : map (↑ring_equiv.prod_comm) (prod I J) = prod J I := sorry /-- Ideals of `R × S` are in one-to-one correspondence with pairs of ideals of `R` and ideals of `S`. -/ def ideal_prod_equiv {R : Type u} {S : Type v} [comm_ring R] [comm_ring S] : ideal (R × S) ≃ ideal R × ideal S := equiv.mk (fun (I : ideal (R × S)) => (map (ring_hom.fst R S) I, map (ring_hom.snd R S) I)) (fun (I : ideal R × ideal S) => prod (prod.fst I) (prod.snd I)) sorry sorry @[simp] theorem ideal_prod_equiv_symm_apply {R : Type u} {S : Type v} [comm_ring R] [comm_ring S] (I : ideal R) (J : ideal S) : coe_fn (equiv.symm ideal_prod_equiv) (I, J) = prod I J := rfl theorem prod.ext_iff {R : Type u} {S : Type v} [comm_ring R] [comm_ring S] {I : ideal R} {I' : ideal R} {J : ideal S} {J' : ideal S} : prod I J = prod I' J' ↔ I = I' ∧ J = J' := sorry theorem is_prime_of_is_prime_prod_top {R : Type u} {S : Type v} [comm_ring R] [comm_ring S] {I : ideal R} (h : is_prime (prod I ⊤)) : is_prime I := sorry theorem is_prime_of_is_prime_prod_top' {R : Type u} {S : Type v} [comm_ring R] [comm_ring S] {I : ideal S} (h : is_prime (prod ⊤ I)) : is_prime I := is_prime_of_is_prime_prod_top (eq.mpr (id (Eq._oldrec (Eq.refl (is_prime (prod I ⊤))) (Eq.symm (map_prod_comm_prod ⊤ I)))) (map_is_prime_of_equiv ring_equiv.prod_comm)) theorem is_prime_ideal_prod_top {R : Type u} {S : Type v} [comm_ring R] [comm_ring S] {I : ideal R} [h : is_prime I] : is_prime (prod I ⊤) := sorry theorem is_prime_ideal_prod_top' {R : Type u} {S : Type v} [comm_ring R] [comm_ring S] {I : ideal S} [h : is_prime I] : is_prime (prod ⊤ I) := eq.mpr (id (Eq._oldrec (Eq.refl (is_prime (prod ⊤ I))) (Eq.symm (map_prod_comm_prod I ⊤)))) (map_is_prime_of_equiv ring_equiv.prod_comm) theorem ideal_prod_prime_aux {R : Type u} {S : Type v} [comm_ring R] [comm_ring S] {I : ideal R} {J : ideal S} : is_prime (prod I J) → I = ⊤ ∨ J = ⊤ := sorry /-- Classification of prime ideals in product rings: the prime ideals of `R × S` are precisely the ideals of the form `p × S` or `R × p`, where `p` is a prime ideal of `R` or `S`. -/ theorem ideal_prod_prime {R : Type u} {S : Type v} [comm_ring R] [comm_ring S] (I : ideal (R × S)) : is_prime I ↔ (∃ (p : ideal R), is_prime p ∧ I = prod p ⊤) ∨ ∃ (p : ideal S), is_prime p ∧ I = prod ⊤ p := sorry /-- The prime ideals of `R × S` are in bijection with the disjoint union of the prime ideals of `R` and the prime ideals of `S`. -/ def prime_ideals_equiv (R : Type u) (S : Type v) [comm_ring R] [comm_ring S] : (Subtype fun (K : ideal (R × S)) => is_prime K) ≃ (Subtype fun (I : ideal R) => is_prime I) ⊕ Subtype fun (J : ideal S) => is_prime J := equiv.symm (equiv.of_bijective prime_ideals_equiv_impl sorry) @[simp] theorem prime_ideals_equiv_symm_inl {R : Type u} {S : Type v} [comm_ring R] [comm_ring S] (I : ideal R) (h : is_prime I) : coe_fn (equiv.symm (prime_ideals_equiv R S)) (sum.inl { val := I, property := h }) = { val := prod I ⊤, property := is_prime_ideal_prod_top } := rfl @[simp] theorem prime_ideals_equiv_symm_inr {R : Type u} {S : Type v} [comm_ring R] [comm_ring S] (J : ideal S) (h : is_prime J) : coe_fn (equiv.symm (prime_ideals_equiv R S)) (sum.inr { val := J, property := h }) = { val := prod ⊤ J, property := is_prime_ideal_prod_top' } := rfl
52d5438b3265fc2827ca5cad666e0e6bc10fc332
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/run/ind_cmd_bug.lean
ddaf7f5792630b14d6f98368b6716484a2dec283
[ "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
97
lean
new_frontend structure D (α : Type) := (a : α) inductive S | mk₁ (v : S) | mk₂ (v : D S)
650ab34538bafa86f91e7b13cc68d2219530fe19
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/run/let1.lean
3c7d3868e018f7155a2c6f4a0275fd9a53d948b6
[ "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
160
lean
check let f x y := x ∧ y, g x := f x x, a := g true in λ (x : a), let h x y := f x (g y), b := h in b
1df49b2dab1277c02547a1990cba6b2d02851534
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/field_theory/finite/basic.lean
08fed47f4e355f13a8d57af0b8e28e69078bd47a
[ "Apache-2.0" ]
permissive
troyjlee/mathlib
e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5
45e7eb8447555247246e3fe91c87066506c14875
refs/heads/master
1,689,248,035,046
1,629,470,528,000
1,629,470,528,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
12,510
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Joey van Langen, Casper Putz -/ import tactic.apply_fun import data.equiv.ring import data.zmod.basic import linear_algebra.basis import ring_theory.integral_domain import field_theory.separable /-! # Finite fields This file contains basic results about finite fields. Throughout most of this file, `K` denotes a finite field and `q` is notation for the cardinality of `K`. See `ring_theory.integral_domain` for the fact that the unit group of a finite field is a cyclic group, as well as the fact that every finite integral domain is a field (`field_of_integral_domain`). ## Main results 1. `card_units`: The unit group of a finite field is has cardinality `q - 1`. 2. `sum_pow_units`: The sum of `x^i`, where `x` ranges over the units of `K`, is - `q-1` if `q-1 ∣ i` - `0` otherwise 3. `finite_field.card`: The cardinality `q` is a power of the characteristic of `K`. See `card'` for a variant. ## Notation Throughout most of this file, `K` denotes a finite field and `q` is notation for the cardinality of `K`. -/ variables {K : Type*} [field K] [fintype K] variables {R : Type*} [integral_domain R] local notation `q` := fintype.card K open_locale big_operators namespace finite_field open finset function section polynomial open polynomial /-- The cardinality of a field is at most `n` times the cardinality of the image of a degree `n` polynomial -/ lemma card_image_polynomial_eval [decidable_eq R] [fintype R] {p : polynomial R} (hp : 0 < p.degree) : fintype.card R ≤ nat_degree p * (univ.image (λ x, eval x p)).card := finset.card_le_mul_card_image _ _ (λ a _, calc _ = (p - C a).roots.to_finset.card : congr_arg card (by simp [finset.ext_iff, mem_roots_sub_C hp]) ... ≤ (p - C a).roots.card : multiset.to_finset_card_le _ ... ≤ _ : card_roots_sub_C' hp) /-- If `f` and `g` are quadratic polynomials, then the `f.eval a + g.eval b = 0` has a solution. -/ lemma exists_root_sum_quadratic [fintype R] {f g : polynomial R} (hf2 : degree f = 2) (hg2 : degree g = 2) (hR : fintype.card R % 2 = 1) : ∃ a b, f.eval a + g.eval b = 0 := by letI := classical.dec_eq R; exact suffices ¬ disjoint (univ.image (λ x : R, eval x f)) (univ.image (λ x : R, eval x (-g))), begin simp only [disjoint_left, mem_image] at this, push_neg at this, rcases this with ⟨x, ⟨a, _, ha⟩, ⟨b, _, hb⟩⟩, exact ⟨a, b, by rw [ha, ← hb, eval_neg, neg_add_self]⟩ end, assume hd : disjoint _ _, lt_irrefl (2 * ((univ.image (λ x : R, eval x f)) ∪ (univ.image (λ x : R, eval x (-g)))).card) $ calc 2 * ((univ.image (λ x : R, eval x f)) ∪ (univ.image (λ x : R, eval x (-g)))).card ≤ 2 * fintype.card R : nat.mul_le_mul_left _ (finset.card_le_univ _) ... = fintype.card R + fintype.card R : two_mul _ ... < nat_degree f * (univ.image (λ x : R, eval x f)).card + nat_degree (-g) * (univ.image (λ x : R, eval x (-g))).card : add_lt_add_of_lt_of_le (lt_of_le_of_ne (card_image_polynomial_eval (by rw hf2; exact dec_trivial)) (mt (congr_arg (%2)) (by simp [nat_degree_eq_of_degree_eq_some hf2, hR]))) (card_image_polynomial_eval (by rw [degree_neg, hg2]; exact dec_trivial)) ... = 2 * (univ.image (λ x : R, eval x f) ∪ univ.image (λ x : R, eval x (-g))).card : by rw [card_disjoint_union hd]; simp [nat_degree_eq_of_degree_eq_some hf2, nat_degree_eq_of_degree_eq_some hg2, bit0, mul_add] end polynomial lemma card_units : fintype.card (units K) = fintype.card K - 1 := begin classical, rw [eq_comm, nat.sub_eq_iff_eq_add (fintype.card_pos_iff.2 ⟨(0 : K)⟩)], haveI := set_fintype {a : K | a ≠ 0}, haveI := set_fintype (@set.univ K), rw [fintype.card_congr (equiv.units_equiv_ne_zero _), ← @set.card_insert _ _ {a : K | a ≠ 0} _ (not_not.2 (eq.refl (0 : K))) (set.fintype_insert _ _), fintype.card_congr (equiv.set.univ K).symm], congr; simp [set.ext_iff, classical.em] end lemma prod_univ_units_id_eq_neg_one : (∏ x : units K, x) = (-1 : units K) := begin classical, have : (∏ x in (@univ (units K) _).erase (-1), x) = 1, from prod_involution (λ x _, x⁻¹) (by simp) (λ a, by simp [units.inv_eq_self_iff] {contextual := tt}) (λ a, by simp [@inv_eq_iff_inv_eq _ _ a, eq_comm] {contextual := tt}) (by simp), rw [← insert_erase (mem_univ (-1 : units K)), prod_insert (not_mem_erase _ _), this, mul_one] end lemma pow_card_sub_one_eq_one (a : K) (ha : a ≠ 0) : a ^ (q - 1) = 1 := calc a ^ (fintype.card K - 1) = (units.mk0 a ha ^ (fintype.card K - 1) : units K) : by rw [units.coe_pow, units.coe_mk0] ... = 1 : by { classical, rw [← card_units, pow_card_eq_one], refl } lemma pow_card (a : K) : a ^ q = a := begin have hp : fintype.card K > 0 := fintype.card_pos_iff.2 (by apply_instance), by_cases h : a = 0, { rw h, apply zero_pow hp }, rw [← nat.succ_pred_eq_of_pos hp, pow_succ, nat.pred_eq_sub_one, pow_card_sub_one_eq_one a h, mul_one], end variable (K) theorem card (p : ℕ) [char_p K p] : ∃ (n : ℕ+), nat.prime p ∧ q = p^(n : ℕ) := begin haveI hp : fact p.prime := ⟨char_p.char_is_prime K p⟩, letI : module (zmod p) K := { .. (zmod.cast_hom (dvd_refl _) K).to_module }, obtain ⟨n, h⟩ := vector_space.card_fintype (zmod p) K, rw zmod.card at h, refine ⟨⟨n, _⟩, hp.1, h⟩, apply or.resolve_left (nat.eq_zero_or_pos n), rintro rfl, rw pow_zero at h, have : (0 : K) = 1, { apply fintype.card_le_one_iff.mp (le_of_eq h) }, exact absurd this zero_ne_one, end theorem card' : ∃ (p : ℕ) (n : ℕ+), nat.prime p ∧ q = p^(n : ℕ) := let ⟨p, hc⟩ := char_p.exists K in ⟨p, @finite_field.card K _ _ p hc⟩ @[simp] lemma cast_card_eq_zero : (q : K) = 0 := begin rcases char_p.exists K with ⟨p, _char_p⟩, resetI, rcases card K p with ⟨n, hp, hn⟩, simp only [char_p.cast_eq_zero_iff K p, hn], conv { congr, rw [← pow_one p] }, exact pow_dvd_pow _ n.2, end lemma forall_pow_eq_one_iff (i : ℕ) : (∀ x : units K, x ^ i = 1) ↔ q - 1 ∣ i := begin obtain ⟨x, hx⟩ := is_cyclic.exists_generator (units K), classical, rw [← card_units, ← order_of_eq_card_of_forall_mem_gpowers hx, order_of_dvd_iff_pow_eq_one], split, { intro h, apply h }, { intros h y, simp_rw ← mem_powers_iff_mem_gpowers at hx, rcases hx y with ⟨j, rfl⟩, rw [← pow_mul, mul_comm, pow_mul, h, one_pow], } end /-- The sum of `x ^ i` as `x` ranges over the units of a finite field of cardinality `q` is equal to `0` unless `(q - 1) ∣ i`, in which case the sum is `q - 1`. -/ lemma sum_pow_units (i : ℕ) : ∑ x : units K, (x ^ i : K) = if (q - 1) ∣ i then -1 else 0 := begin let φ : units K →* K := { to_fun := λ x, x ^ i, map_one' := by rw [units.coe_one, one_pow], map_mul' := by { intros, rw [units.coe_mul, mul_pow] } }, haveI : decidable (φ = 1), { classical, apply_instance }, calc ∑ x : units K, φ x = if φ = 1 then fintype.card (units K) else 0 : sum_hom_units φ ... = if (q - 1) ∣ i then -1 else 0 : _, suffices : (q - 1) ∣ i ↔ φ = 1, { simp only [this], split_ifs with h h, swap, refl, rw [card_units, nat.cast_sub, cast_card_eq_zero, nat.cast_one, zero_sub], show 1 ≤ q, from fintype.card_pos_iff.mpr ⟨0⟩ }, rw [← forall_pow_eq_one_iff, monoid_hom.ext_iff], apply forall_congr, intro x, rw [units.ext_iff, units.coe_pow, units.coe_one, monoid_hom.one_apply], refl, end /-- The sum of `x ^ i` as `x` ranges over a finite field of cardinality `q` is equal to `0` if `i < q - 1`. -/ lemma sum_pow_lt_card_sub_one (i : ℕ) (h : i < q - 1) : ∑ x : K, x ^ i = 0 := begin by_cases hi : i = 0, { simp only [hi, nsmul_one, sum_const, pow_zero, card_univ, cast_card_eq_zero], }, classical, have hiq : ¬ (q - 1) ∣ i, { contrapose! h, exact nat.le_of_dvd (nat.pos_of_ne_zero hi) h }, let φ : units K ↪ K := ⟨coe, units.ext⟩, have : univ.map φ = univ \ {0}, { ext x, simp only [true_and, embedding.coe_fn_mk, mem_sdiff, units.exists_iff_ne_zero, mem_univ, mem_map, exists_prop_of_true, mem_singleton] }, calc ∑ x : K, x ^ i = ∑ x in univ \ {(0 : K)}, x ^ i : by rw [← sum_sdiff ({0} : finset K).subset_univ, sum_singleton, zero_pow (nat.pos_of_ne_zero hi), add_zero] ... = ∑ x : units K, x ^ i : by { rw [← this, univ.sum_map φ], refl } ... = 0 : by { rw [sum_pow_units K i, if_neg], exact hiq, } end variables {K} theorem frobenius_pow {p : ℕ} [fact p.prime] [char_p K p] {n : ℕ} (hcard : q = p^n) : (frobenius K p) ^ n = 1 := begin ext, conv_rhs { rw [ring_hom.one_def, ring_hom.id_apply, ← pow_card x, hcard], }, clear hcard, induction n, {simp}, rw [pow_succ, pow_succ', pow_mul, ring_hom.mul_def, ring_hom.comp_apply, frobenius_def, n_ih] end open polynomial lemma expand_card (f : polynomial K) : expand K q f = f ^ q := begin cases char_p.exists K with p hp, letI := hp, rcases finite_field.card K p with ⟨⟨n, npos⟩, ⟨hp, hn⟩⟩, haveI : fact p.prime := ⟨hp⟩, dsimp at hn, rw hn at *, rw ← map_expand_pow_char, rw [frobenius_pow hn, ring_hom.one_def, map_id], end end finite_field namespace zmod open finite_field polynomial lemma sq_add_sq (p : ℕ) [hp : fact p.prime] (x : zmod p) : ∃ a b : zmod p, a^2 + b^2 = x := begin cases hp.1.eq_two_or_odd with hp2 hp_odd, { substI p, change fin 2 at x, fin_cases x, { use 0, simp }, { use [0, 1], simp } }, let f : polynomial (zmod p) := X^2, let g : polynomial (zmod p) := X^2 - C x, obtain ⟨a, b, hab⟩ : ∃ a b, f.eval a + g.eval b = 0 := @exists_root_sum_quadratic _ _ _ f g (degree_X_pow 2) (degree_X_pow_sub_C dec_trivial _) (by rw [zmod.card, hp_odd]), refine ⟨a, b, _⟩, rw ← sub_eq_zero, simpa only [eval_C, eval_X, eval_pow, eval_sub, ← add_sub_assoc] using hab, end end zmod namespace char_p lemma sq_add_sq (R : Type*) [integral_domain R] (p : ℕ) [fact (0 < p)] [char_p R p] (x : ℤ) : ∃ a b : ℕ, (a^2 + b^2 : R) = x := begin haveI := char_is_prime_of_pos R p, obtain ⟨a, b, hab⟩ := zmod.sq_add_sq p x, refine ⟨a.val, b.val, _⟩, simpa using congr_arg (zmod.cast_hom (dvd_refl _) R) hab end end char_p open_locale nat open zmod /-- The **Fermat-Euler totient theorem**. `nat.modeq.pow_totient` is an alternative statement of the same theorem. -/ @[simp] lemma zmod.pow_totient {n : ℕ} [fact (0 < n)] (x : units (zmod n)) : x ^ φ n = 1 := by rw [← card_units_eq_totient, pow_card_eq_one] /-- The **Fermat-Euler totient theorem**. `zmod.pow_totient` is an alternative statement of the same theorem. -/ lemma nat.modeq.pow_totient {x n : ℕ} (h : nat.coprime x n) : x ^ φ n ≡ 1 [MOD n] := begin cases n, {simp}, rw ← zmod.eq_iff_modeq_nat, let x' : units (zmod (n+1)) := zmod.unit_of_coprime _ h, have := zmod.pow_totient x', apply_fun (coe : units (zmod (n+1)) → zmod (n+1)) at this, simpa only [-zmod.pow_totient, nat.succ_eq_add_one, nat.cast_pow, units.coe_one, nat.cast_one, coe_unit_of_coprime, units.coe_pow], end open finite_field namespace zmod /-- A variation on Fermat's little theorem. See `zmod.pow_card_sub_one_eq_one` -/ @[simp] lemma pow_card {p : ℕ} [fact p.prime] (x : zmod p) : x ^ p = x := by { have h := finite_field.pow_card x, rwa zmod.card p at h } @[simp] lemma frobenius_zmod (p : ℕ) [fact p.prime] : frobenius (zmod p) p = ring_hom.id _ := by { ext a, rw [frobenius_def, zmod.pow_card, ring_hom.id_apply] } @[simp] lemma card_units (p : ℕ) [fact p.prime] : fintype.card (units (zmod p)) = p - 1 := by rw [card_units, card] /-- **Fermat's Little Theorem**: for every unit `a` of `zmod p`, we have `a ^ (p - 1) = 1`. -/ theorem units_pow_card_sub_one_eq_one (p : ℕ) [fact p.prime] (a : units (zmod p)) : a ^ (p - 1) = 1 := by rw [← card_units p, pow_card_eq_one] /-- **Fermat's Little Theorem**: for all nonzero `a : zmod p`, we have `a ^ (p - 1) = 1`. -/ theorem pow_card_sub_one_eq_one {p : ℕ} [fact p.prime] {a : zmod p} (ha : a ≠ 0) : a ^ (p - 1) = 1 := by { have h := pow_card_sub_one_eq_one a ha, rwa zmod.card p at h } open polynomial lemma expand_card {p : ℕ} [fact p.prime] (f : polynomial (zmod p)) : expand (zmod p) p f = f ^ p := by { have h := finite_field.expand_card f, rwa zmod.card p at h } end zmod
a1c8f0c843212d6c56c7e6ccab945be6120bf2e5
ebf7140a9ea507409ff4c994124fa36e79b4ae35
/src/exercises_sources/friday/topology.lean
ed8be0e82e94b802552444c2654befca9d434c0e
[]
no_license
fundou/lftcm2020
3e88d58a92755ea5dd49f19c36239c35286ecf5e
99d11bf3bcd71ffeaef0250caa08ecc46e69b55b
refs/heads/master
1,685,610,799,304
1,624,070,416,000
1,624,070,416,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
12,143
lean
import topology.metric_space.basic open_locale classical filter topological_space namespace lftcm open filter set /-! # Filters ## Definition of filters -/ def principal {α : Type*} (s : set α) : filter α := { sets := {t | s ⊆ t}, univ_sets := begin sorry end, sets_of_superset := begin sorry end, inter_sets := begin sorry end} def at_top : filter ℕ := { sets := {s | ∃ a, ∀ b, a ≤ b → b ∈ s}, univ_sets := begin sorry end, sets_of_superset := begin sorry end, inter_sets := begin sorry end} -- The next exercise is slightly more tricky, you should probably keep it for later def nhds (x : ℝ) : filter ℝ := { sets := {s | ∃ ε > 0, Ioo (x - ε) (x + ε) ⊆ s}, univ_sets := begin sorry end, sets_of_superset := begin sorry end, inter_sets := begin sorry end} /- The filter axiom are also available as standalone lemmas where the filter argument is implicit Compare -/ #check @filter.sets_of_superset #check @mem_sets_of_superset -- And analogously: #check @inter_mem_sets /-! ## Definition of "tends to" -/ -- We'll practive using tendsto by reproving the composition lemma `tendsto.comp` from mathlib -- Let's first use the concrete definition recorded by `tendsto_def` #check @tendsto_def #check @preimage_comp example {α β γ : Type*} {A : filter α} {B : filter β} {C : filter γ} {f : α → β} {g : β → γ} (hf : tendsto f A B) (hg : tendsto g B C) : tendsto (g ∘ f) A C := begin sorry end -- Now let's get functorial (same statement as above, different proof packaging). example {α β γ : Type*} {A : filter α} {B : filter β} {C : filter γ} {f : α → β} {g : β → γ} (hf : tendsto f A B) (hg : tendsto g B C) : tendsto (g ∘ f) A C := begin calc map (g ∘ f) A = map g (map f A) : sorry ... ≤ map g B : sorry ... ≤ C : sorry, end /- Let's now focus on the pull-back operation `filter.comap` which takes `f : X → Y` and a filter `G` on `Y` and returns a filter on `X`. -/ #check @mem_comap_sets -- this is by definition, the proof is `iff.rfl` -- It also help to record a special case of one implication: #check @preimage_mem_comap -- The following exercise, which reproves `comap_ne_bot_iff` can start using #check @forall_sets_nonempty_iff_ne_bot example {α β : Type*} {f : filter β} {m : α → β} : (comap m f).ne_bot ↔ ∀ t ∈ f, ∃ a, m a ∈ t := begin sorry end /-! ## Properties holding eventually -/ /-- The next exercise only needs the definition of filters and the fact that `∀ᶠ x in f, p x` is a notation for `{x | p x} ∈ f`. It is called `eventually_and` in mathlib, and won't be needed below. For instance, applied to `α = ℕ` and the `at_top` filter above, it says that, given two predicates `p` and `q` on natural numbers, p n and q n for n large enough if and only if p n holds for n large enough and q n holds for n large enough. -/ example {α : Type*} {p q : α → Prop} {f : filter α} : (∀ᶠ x in f, p x ∧ q x) ↔ (∀ᶠ x in f, p x) ∧ (∀ᶠ x in f, q x) := begin sorry end /-! ## Topological spaces -/ section -- This is how we can talk about two topological spaces X and Y variables {X Y : Type*} [topological_space X] [topological_space Y] /- Given a topological space `X` and some `A : set X`, we have the usual zoo of predicates `is_open A`, `is_closed A`, `is_connected A`, `is_compact A` (and some more) There are also additional type classes referring to properties of `X` itself, like `compact_space X` or `connected_space X` -/ /-- We can talk about continuous functions from `X` to `Y` -/ example (f : X → Y) : continuous f ↔ ∀ V, is_open V → is_open (f ⁻¹' V) := continuous_def /- Each point `x` of a topological space has a neighborhood filter `𝓝 x` made of sets containing an open set containing `x`. It is always a proper filter, as recorded by `nhds_ne_bot` Asking for continuity is the same as asking for continuity at each point the right-hand side below is known as `continuous_at f x` -/ example (f : X → Y) : continuous f ↔ ∀ x, tendsto f (𝓝 x) (𝓝 (f x)) := continuous_iff_continuous_at /- The topological structure also brings operations on sets. To each `A : set X`, we can associate `closure A`, `interior A` and `frontier A`. We'll focus on `closure A`. It is defined as the intersection of closed sets containing `A` but we can characterize it in terms of neighborhoods. The most concrete version is `mem_closure_iff_nhds : a ∈ closure A ↔ ∀ B ∈ 𝓝 a, (B ∩ A).nonempty` We'll pratice by reproving the slightly more abstract `mem_closure_iff_comap_ne_bot`. First let's review sets and subtypes. Fix a type `X` and recall that `A : set X` is not a type a priori, but Lean coerces automatically when needed to the type `↥A` whose terms are build of a term `x : X` and a proof of `x ∈ A`. In the other direction, inhabitants of `↥A` can be coerced to `X` automatically. This inclusion coercion map is called `coe : A → X` and `coe a` is also denoted by `↑a`. Now assume `X` is a topological space, and let's understand the closure of A in terms of `coe` and the neighborhood filter. In the next exercise, you can use `simp_rw` instead of `rw` to rewrite inside a quantifier -/ #check nonempty_inter_iff_exists_right example {A : set X} {x : X} : x ∈ closure A ↔ (comap (coe : A → X) (𝓝 x)).ne_bot := begin sorry end /- In elementary contexts, the main property of `closure A` is that a converging sequence `u : ℕ → X` such that `∀ n, u n ∈ A` has its limit in `closure A`. Note we don't need all the full sequence to be in `A`, it's enough to ask it for `n` large enough, ie. `∀ᶠ n in at_top, u n ∈ A`. Also there is no reason to use sequences only, we can use any map and any source filter. We hence have the important `mem_closure_of_tendsto` : ∀ {f : β → X} {F : filter β} {a : X} {A : set X}, F ≠ ⊥ → tendsto f F (𝓝 a) → (∀ᶠ x in F, f x ∈ A) → a ∈ closure A If `A` is known to be closed then we can replace `closure A` by `A`, this is `is_closed.mem_of_tendsto`. -/ /- We need one last piece of filter technology: bases. By definition, each neighborhood of a point `x` contains an *open* neighborhood of `x`. Hence we can often restrict our attention to such neighborhoods. The general definition recording such a situation is: `has_basis` (l : filter α) (p : ι → Prop) (s : ι → set α) : Prop := (mem_iff' : ∀ t, t ∈ l ↔ ∃ i (hi : p i), s i ⊆ t) You can now inspect three examples of how bases allow to restrict attention to certain elements of a filter. -/ #check @has_basis.mem_iff #check @has_basis.tendsto_left_iff #check @has_basis.tendsto_right_iff -- We'll use the following bases: #check @nhds_basis_opens' #check @closed_nhds_basis /-- Our main goal is now to prove the basic theorem which allows extension by continuity. From Bourbaki's general topology book, I.8.5, Theorem 1 (taking only the non-trivial implication): Let `X` be a topological space, `A` a dense subset of `X`, `f : A → Y` a mapping of `A` into a regular space `Y`. If, for each `x` in `X`, `f(y)` tends to a limit in `Y` when `y` tends to `x` while remaining in `A` then there exists a continuous extension `φ` of `f` to `X`. The regularity assumption on `Y` ensures that each point of `Y` has a basis of *closed* neighborhoods, this is `closed_nhds_basis`. It also ensures that `Y` is Hausdorff so limits in `Y` are unique, this is `tendsto_nhds_unique`. mathlib contains a refinement of the above lemma, `dense_inducing.continuous_at_extend`, but we'll stick to Bourbaki's version here. Remember that, given `A : set X`, `↥A` is the subtype associated to `A`, and Lean will automatically insert that funny up arrow when needed. And the (inclusion) coercion map is `coe : A → X`. The assumption "tends to `x` while remaining in `A`" corresponds to the pull-back filter `comap coe (𝓝 x)`. Let's prove first an auxilliary lemma, extracted to simplify the context (in particular we don't need Y to be a topological space here). -/ lemma aux {X Y A : Type*} [topological_space X] {c : A → X} {f : A → Y} {x : X} {F : filter Y} (h : tendsto f (comap c (𝓝 x)) F) {V' : set Y} (V'_in : V' ∈ F) : ∃ V ∈ 𝓝 x, is_open V ∧ c ⁻¹' V ⊆ f ⁻¹' V' := begin sorry end /-- Let's now turn to the main proof of the extension by continuity theorem. When Lean needs a topology on `↥A` it will use the induced topology, thanks to the instance `subtype.topological_space`. This all happens automatically. The only relevant lemma is `nhds_induced coe : ∀ a : ↥A, 𝓝 a = comap coe (𝓝 ↑a)` (this is actually a general lemma about induced topologies). The proof outline is: The main assumption and the axiom of choice give a function `φ` such that `∀ x, tendsto f (comap coe $ 𝓝 x) (𝓝 (φ x))` (because `Y` is Hausdorff, `φ` is entirely determined, but we won't need that until we try to prove that `φ` indeed extends `f`). Let's first prove `φ` is continuous. Fix any `x : X`. Since `Y` is regular, it suffices to check that for every *closed* neighborhood `V'` of `φ x`, `φ ⁻¹' V' ∈ 𝓝 x`. The limit assumption gives (through the auxilliary lemma above) some `V ∈ 𝓝 x` such `is_open V ∧ coe ⁻¹' V ⊆ f ⁻¹' V'`. Since `V ∈ 𝓝 x`, it suffices to prove `V ⊆ φ ⁻¹' V'`, ie `∀ y ∈ V, φ y ∈ V'`. Let's fix `y` in `V`. Because `V` is *open*, it is a neighborhood of `y`. In particular `coe ⁻¹' V ∈ comap coe (𝓝 y)` and a fortiori `f ⁻¹' V' ∈ comap coe (𝓝 y)`. In addition `comap coe $ 𝓝 y ≠ ⊥` because `A` is dense. Because we know `tendsto f (comap coe $ 𝓝 y) (𝓝 (φ y))` this implies `φ y ∈ closure V'` and, since `V'` is closed, we have proved `φ y ∈ V'`. It remains to prove that `φ` extends `f`. This is were continuity of `f` enters the discussion, together with the fact that `Y` is Hausdorff. -/ example [regular_space Y] {A : set X} (hA : ∀ x, x ∈ closure A) {f : A → Y} (f_cont : continuous f) (hf : ∀ x : X, ∃ c : Y, tendsto f (comap coe $ 𝓝 x) $ 𝓝 c) : ∃ φ : X → Y, continuous φ ∧ ∀ a : A, φ a = f a := begin sorry end end /-! ## Metric spaces -/ /-- We now leave general topology and turn to metric spaces. The distance function is denoted by `dist`. A slight difficulty here is that, as in Bourbaki, many results you may expect to see stated for metric spaces are stated for uniform spaces, a more general notion that also includes topological groups. In this tutorial we will avoid uniform spaces for simplicity. We will prove that continuous functions from a compact metric space to a metric space are uniformly continuous. mathlib has a much more general version (about functions between uniform spaces...). The lemma `metric.uniform_continuous_iff` allows to translate the general definition of uniform continuity to the ε-δ definition that works for metric spaces only. So let's fix `ε > 0` and start looking for `δ`. We will deduce Heine-Cantor from the fact that a real value continuous function on a nonempty compact set reaches its infimum. There are several ways to state that, but here we recommend `is_compact.exists_forall_le`. Let `φ : X × X → ℝ := λ p, dist (f p.1) (f p.2)` and let `K := { p : X × X | ε ≤ φ p }`. Observe `φ` is continuous by assumption on `f` and using `continuous_dist`. And `K` is closed using `is_closed_le` hence compact since `X` is compact. Then we discuss two possibilities using `eq_empty_or_nonempty`. If `K` is empty then we are clearly done (we can set `δ = 1` for instance). So let's assume `K` is not empty, and choose `(x₀, x₁)` attaining the infimum of `φ` on `K`. We can then set `δ = dist x₀ x₁` and check everything works. -/ example {X : Type*} [metric_space X] [compact_space X] {Y : Type*} [metric_space Y] {f : X → Y} (hf : continuous f) : uniform_continuous f := begin sorry end end lftcm
50961a77e22de04c1431cb037f33078895dfd272
46125763b4dbf50619e8846a1371029346f4c3db
/src/data/equiv/local_equiv.lean
28b604cf89ac04a1ba89be70192648950bc3fa07
[ "Apache-2.0" ]
permissive
thjread/mathlib
a9d97612cedc2c3101060737233df15abcdb9eb1
7cffe2520a5518bba19227a107078d83fa725ddc
refs/heads/master
1,615,637,696,376
1,583,953,063,000
1,583,953,063,000
246,680,271
0
0
Apache-2.0
1,583,960,875,000
1,583,960,875,000
null
UTF-8
Lean
false
false
21,759
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import data.equiv.basic /-! # Local equivalences This files defines equivalences between subsets of given types. An element `e` of `local_equiv α β` is made of two maps `e.to_fun` and `e.inv_fun` respectively from α to β and from β to α (just like equivs), which are inverse to each other on the subsets `e.source` and `e.target` of respectively α and β. They are designed in particular to define charts on manifolds. The main functionality is `e.trans f`, which composes the two local equivalences by restricting the source and target to the maximal set where the composition makes sense. Contrary to equivs, we do not register the coercion to functions and we use explicitly to_fun and inv_fun: coercions create numerous unification problems for manifolds. ## Main definitions `equiv.to_local_equiv`: associating a local equiv to an equiv, with source = target = univ `local_equiv.symm` : the inverse of a local equiv `local_equiv.trans` : the composition of two local equivs `local_equiv.refl` : the identity local equiv `local_equiv.of_set` : the identity on a set `s` `eq_on_source` : equivalence relation describing the "right" notion of equality for local equivs (see below in implementation notes) ## Implementation notes There are at least three possible implementations of local equivalences: * equivs on subtypes * pairs of functions taking values in `option α` and `option β`, equal to none where the local equivalence is not defined * pairs of functions defined everywhere, keeping the source and target as additional data Each of these implementations has pros and cons. * When dealing with subtypes, one still need to define additional API for composition and restriction of domains. Checking that one always belongs to the right subtype makes things very tedious, and leads quickly to DTT hell (as the subtype `u ∩ v` is not the "same" as `v ∩ u`, for instance). * With option-valued functions, the composition is very neat (it is just the usual composition, and the domain is restricted automatically). These are implemented in `pequiv.lean`. For manifolds, where one wants to discuss thoroughly the smoothness of the maps, this creates however a lot of overhead as one would need to extend all classes of smoothness to option-valued maps. * The local_equiv version as explained above is easier to use for manifolds. The drawback is that there is extra useless data (the values of `to_fun` and `inv_fun` outside of `source` and `target`). In particular, the equality notion between local equivs is not "the right one", i.e., coinciding source and target and equality there. Moreover, there are no local equivs in this sense between an empty type and a nonempty type. Since empty types are not that useful, and since one almost never needs to talk about equal local equivs, this is not an issue in practice. Still, we introduce an equivalence relation `eq_on_source` that captures this right notion of equality, and show that many properties are invariant under this equivalence relation. -/ open function set variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} /-- Local equivalence between subsets `source` and `target` of α and β respectively. The (global) maps `to_fun : α → β` and `inv_fun : β → α` map `source` to `target` and conversely, and are inverse to each other there. The values of `to_fun` outside of `source` and of `inv_fun` outside of `target` are irrelevant. -/ structure local_equiv (α : Type*) (β : Type*) := (to_fun : α → β) (inv_fun : β → α) (source : set α) (target : set β) (map_source : ∀{x}, x ∈ source → to_fun x ∈ target) (map_target : ∀{x}, x ∈ target → inv_fun x ∈ source) (left_inv : ∀{x}, x ∈ source → inv_fun (to_fun x) = x) (right_inv : ∀{x}, x ∈ target → to_fun (inv_fun x) = x) attribute [simp] local_equiv.left_inv local_equiv.right_inv local_equiv.map_source local_equiv.map_target /-- Associating a local_equiv to an equiv-/ def equiv.to_local_equiv (e : equiv α β) : local_equiv α β := { to_fun := e.to_fun, inv_fun := e.inv_fun, source := univ, target := univ, map_source := λx hx, mem_univ _, map_target := λy hy, mem_univ _, left_inv := λx hx, e.left_inv x, right_inv := λx hx, e.right_inv x } namespace local_equiv variables (e : local_equiv α β) (e' : local_equiv β γ) /-- Associating to a local_equiv an equiv between the source and the target -/ protected def to_equiv : equiv (e.source) (e.target) := { to_fun := λ⟨x, hx⟩, ⟨e.to_fun x, e.map_source hx⟩, inv_fun := λ⟨y, hy⟩, ⟨e.inv_fun y, e.map_target hy⟩, left_inv := λ⟨x, hx⟩, subtype.eq $ e.left_inv hx, right_inv := λ⟨y, hy⟩, subtype.eq $ e.right_inv hy } /-- The inverse of a local equiv -/ protected def symm : local_equiv β α := { to_fun := e.inv_fun, inv_fun := e.to_fun, source := e.target, target := e.source, map_source := e.map_target, map_target := e.map_source, left_inv := e.right_inv, right_inv := e.left_inv } @[simp] lemma symm_to_fun : e.symm.to_fun = e.inv_fun := rfl @[simp] lemma symm_inv_fun : e.symm.inv_fun = e.to_fun := rfl @[simp] lemma symm_source : e.symm.source = e.target := rfl @[simp] lemma symm_target : e.symm.target = e.source := rfl @[simp] lemma symm_symm : e.symm.symm = e := by { cases e, refl } /-- A local equiv induces a bijection between its source and target -/ lemma bij_on_source : bij_on e.to_fun e.source e.target := inv_on.bij_on ⟨e.left_inv, e.right_inv⟩ e.map_source e.map_target lemma image_eq_target_inter_inv_preimage {s : set α} (h : s ⊆ e.source) : e.to_fun '' s = e.target ∩ e.inv_fun ⁻¹' s := begin refine subset.antisymm (λx hx, _) (λx hx, _), { rcases (mem_image _ _ _).1 hx with ⟨y, ys, hy⟩, rw ← hy, split, { apply e.map_source, exact h ys }, { rwa [mem_preimage, e.left_inv (h ys)] } }, { rw ← e.right_inv hx.1, exact mem_image_of_mem _ hx.2 } end lemma inv_image_eq_source_inter_preimage {s : set β} (h : s ⊆ e.target) : e.inv_fun '' s = e.source ∩ e.to_fun ⁻¹' s := e.symm.image_eq_target_inter_inv_preimage h lemma source_inter_preimage_inv_preimage (s : set α) : e.source ∩ e.to_fun ⁻¹' (e.inv_fun ⁻¹' s) = e.source ∩ s := begin ext, split, { rintros ⟨hx, xs⟩, simp only [mem_preimage, hx, e.left_inv, mem_preimage] at xs, exact ⟨hx, xs⟩ }, { rintros ⟨hx, xs⟩, simp [hx, xs] } end lemma target_inter_inv_preimage_preimage (s : set β) : e.target ∩ e.inv_fun ⁻¹' (e.to_fun ⁻¹' s) = e.target ∩ s := e.symm.source_inter_preimage_inv_preimage _ lemma image_source_eq_target : e.to_fun '' e.source = e.target := e.bij_on_source.image_eq lemma source_subset_preimage_target : e.source ⊆ e.to_fun ⁻¹' e.target := λx hx, e.map_source hx lemma inv_image_target_eq_source : e.inv_fun '' e.target = e.source := e.symm.bij_on_source.image_eq lemma target_subset_preimage_source : e.target ⊆ e.inv_fun ⁻¹' e.source := λx hx, e.map_target hx /-- Two local equivs that have the same source, same to_fun and same inv_fun, coincide. -/ @[ext] protected lemma ext (e' : local_equiv α β) (h : ∀x, e.to_fun x = e'.to_fun x) (hsymm : ∀x, e.inv_fun x = e'.inv_fun x) (hs : e.source = e'.source) : e = e' := begin have A : e.to_fun = e'.to_fun, by { ext x, exact h x }, have B : e.inv_fun = e'.inv_fun, by { ext x, exact hsymm x }, have I : e.to_fun '' e.source = e.target := e.image_source_eq_target, have I' : e'.to_fun '' e'.source = e'.target := e'.image_source_eq_target, rw [A, hs, I'] at I, cases e; cases e', simp * at * end /-- Restricting a local equivalence to e.source ∩ s -/ protected def restr (s : set α) : local_equiv α β := { to_fun := e.to_fun, inv_fun := e.inv_fun, source := e.source ∩ s, target := e.target ∩ e.inv_fun⁻¹' s, map_source := λx hx, begin apply mem_inter, { apply e.map_source, exact hx.1 }, { rw [mem_preimage, e.left_inv], exact hx.2, exact hx.1 }, end, map_target := λy hy, begin apply mem_inter, { apply e.map_target, exact hy.1 }, { exact hy.2 }, end, left_inv := λx hx, e.left_inv hx.1, right_inv := λy hy, e.right_inv hy.1 } @[simp] lemma restr_to_fun (s : set α) : (e.restr s).to_fun = e.to_fun := rfl @[simp] lemma restr_inv_fun (s : set α) : (e.restr s).inv_fun = e.inv_fun := rfl @[simp] lemma restr_source (s : set α) : (e.restr s).source = e.source ∩ s := rfl @[simp] lemma restr_target (s : set α) : (e.restr s).target = e.target ∩ e.inv_fun ⁻¹' s := rfl lemma restr_eq_of_source_subset {e : local_equiv α β} {s : set α} (h : e.source ⊆ s) : e.restr s = e := local_equiv.ext _ _ (λ_, rfl) (λ_, rfl) (by simp [inter_eq_self_of_subset_left h]) @[simp] lemma restr_univ {e : local_equiv α β} : e.restr univ = e := restr_eq_of_source_subset (subset_univ _) /-- The identity local equiv -/ protected def refl (α : Type*) : local_equiv α α := (equiv.refl α).to_local_equiv @[simp] lemma refl_source : (local_equiv.refl α).source = univ := rfl @[simp] lemma refl_target : (local_equiv.refl α).target = univ := rfl @[simp] lemma refl_to_fun : (local_equiv.refl α).to_fun = id := rfl @[simp] lemma refl_inv_fun : (local_equiv.refl α).inv_fun = id := rfl @[simp] lemma refl_symm : (local_equiv.refl α).symm = local_equiv.refl α := rfl @[simp] lemma refl_restr_source (s : set α) : ((local_equiv.refl α).restr s).source = s := by simp @[simp] lemma refl_restr_target (s : set α) : ((local_equiv.refl α).restr s).target = s := by { change univ ∩ id⁻¹' s = s, simp } /-- The identity local equiv on a set `s` -/ def of_set (s : set α) : local_equiv α α := { to_fun := id, inv_fun := id, source := s, target := s, map_source := λx hx, hx, map_target := λx hx, hx, left_inv := λx hx, rfl, right_inv := λx hx, rfl } @[simp] lemma of_set_source (s : set α) : (local_equiv.of_set s).source = s := rfl @[simp] lemma of_set_target (s : set α) : (local_equiv.of_set s).target = s := rfl @[simp] lemma of_set_to_fun (s : set α) : (local_equiv.of_set s).to_fun = id := rfl @[simp] lemma of_set_inv_fun {s : set α} : (local_equiv.of_set s).inv_fun = id := rfl @[simp] lemma of_set_symm (s : set α) : (local_equiv.of_set s).symm = local_equiv.of_set s := rfl /-- Composing two local equivs if the target of the first coincides with the source of the second. -/ protected def trans' (e' : local_equiv β γ) (h : e.target = e'.source) : local_equiv α γ := { to_fun := e'.to_fun ∘ e.to_fun, inv_fun := e.inv_fun ∘ e'.inv_fun, source := e.source, target := e'.target, map_source := λx hx, begin apply e'.map_source, rw ← h, apply e.map_source hx end, map_target := λy hy, begin apply e.map_target, rw h, apply e'.map_target hy end, left_inv := λx hx, begin change e.inv_fun (e'.inv_fun (e'.to_fun (e.to_fun x))) = x, rw e'.left_inv, { exact e.left_inv hx }, { rw ← h, exact e.map_source hx } end, right_inv := λy hy, begin change e'.to_fun (e.to_fun (e.inv_fun (e'.inv_fun y))) = y, rw e.right_inv, { exact e'.right_inv hy }, { rw h, exact e'.map_target hy } end } /-- Composing two local equivs, by restricting to the maximal domain where their composition is well defined. -/ protected def trans : local_equiv α γ := local_equiv.trans' (e.symm.restr (e'.source)).symm (e'.restr (e.target)) (inter_comm _ _) @[simp] lemma trans_to_fun : (e.trans e').to_fun = e'.to_fun ∘ e.to_fun := rfl @[simp] lemma trans_apply (x : α) : (e.trans e').to_fun x = e'.to_fun (e.to_fun x) := rfl @[simp] lemma trans_inv_fun : (e.trans e').inv_fun = e.inv_fun ∘ e'.inv_fun := rfl @[simp] lemma trans_inv_apply (x : γ) : (e.trans e').inv_fun x = e.inv_fun (e'.inv_fun x) := rfl lemma trans_symm_eq_symm_trans_symm : (e.trans e').symm = e'.symm.trans e.symm := by cases e; cases e'; refl /- This could be considered as a simp lemma, but there are many situations where it makes something simple into something more complicated. -/ lemma trans_source : (e.trans e').source = e.source ∩ e.to_fun ⁻¹' e'.source := rfl lemma trans_source' : (e.trans e').source = e.source ∩ e.to_fun ⁻¹' (e.target ∩ e'.source) := begin symmetry, calc e.source ∩ e.to_fun ⁻¹' (e.target ∩ e'.source) = (e.source ∩ e.to_fun ⁻¹' (e.target)) ∩ e.to_fun ⁻¹' (e'.source) : by rw [preimage_inter, inter_assoc] ... = e.source ∩ e.to_fun ⁻¹' (e'.source) : by { congr' 1, apply inter_eq_self_of_subset_left e.source_subset_preimage_target } ... = (e.trans e').source : rfl end lemma trans_source'' : (e.trans e').source = e.inv_fun '' (e.target ∩ e'.source) := begin rw [e.trans_source', e.inv_image_eq_source_inter_preimage, inter_comm], exact inter_subset_left _ _, end lemma image_trans_source : e.to_fun '' (e.trans e').source = e.target ∩ e'.source := image_source_eq_target (local_equiv.symm (local_equiv.restr (local_equiv.symm e) (e'.source))) lemma trans_target : (e.trans e').target = e'.target ∩ e'.inv_fun ⁻¹' e.target := rfl lemma trans_target' : (e.trans e').target = e'.target ∩ e'.inv_fun ⁻¹' (e'.source ∩ e.target) := trans_source' e'.symm e.symm lemma trans_target'' : (e.trans e').target = e'.to_fun '' (e'.source ∩ e.target) := trans_source'' e'.symm e.symm lemma inv_image_trans_target : e'.inv_fun '' (e.trans e').target = e'.source ∩ e.target := image_trans_source e'.symm e.symm lemma trans_assoc (e'' : local_equiv γ δ) : (e.trans e').trans e'' = e.trans (e'.trans e'') := local_equiv.ext _ _ (λx, rfl) (λx, rfl) (by simp [trans_source, @preimage_comp α β γ, inter_assoc]) @[simp] lemma trans_refl : e.trans (local_equiv.refl β) = e := local_equiv.ext _ _ (λx, rfl) (λx, rfl) (by simp [trans_source]) @[simp] lemma refl_trans : (local_equiv.refl α).trans e = e := local_equiv.ext _ _ (λx, rfl) (λx, rfl) (by simp [trans_source, preimage_id]) lemma trans_refl_restr (s : set β) : e.trans ((local_equiv.refl β).restr s) = e.restr (e.to_fun ⁻¹' s) := local_equiv.ext _ _ (λx, rfl) (λx, rfl) (by simp [trans_source]) lemma trans_refl_restr' (s : set β) : e.trans ((local_equiv.refl β).restr s) = e.restr (e.source ∩ e.to_fun ⁻¹' s) := local_equiv.ext _ _ (λx, rfl) (λx, rfl) $ by { simp [trans_source], rw [← inter_assoc, inter_self] } lemma restr_trans (s : set α) : (e.restr s).trans e' = (e.trans e').restr s := local_equiv.ext _ _ (λx, rfl) (λx, rfl) $ by { simp [trans_source, inter_comm], rwa inter_assoc } /-- `eq_on_source e e'` means that `e` and `e'` have the same source, and coincide there. Then `e` and `e'` should really be considered the same local equiv. -/ def eq_on_source (e e' : local_equiv α β) : Prop := e.source = e'.source ∧ (∀x ∈ e.source, e.to_fun x = e'.to_fun x) /-- `eq_on_source` is an equivalence relation -/ instance eq_on_source_setoid : setoid (local_equiv α β) := { r := eq_on_source, iseqv := ⟨ λe, by simp [eq_on_source], λe e' h, by { simp [eq_on_source, h.1.symm], exact λx hx, (h.2 x hx).symm }, λe e' e'' h h', ⟨by rwa [← h'.1, ← h.1], λx hx, by { rw [← h'.2 x, h.2 x hx], rwa ← h.1 }⟩⟩ } lemma eq_on_source_refl : e ≈ e := setoid.refl _ /-- If two local equivs are equivalent, so are their inverses -/ lemma eq_on_source_symm {e e' : local_equiv α β} (h : e ≈ e') : e.symm ≈ e'.symm := begin have T : e.target = e'.target, { have : set.bij_on e'.to_fun e.source e.target := e.bij_on_source.congr h.2, have A : e'.to_fun '' e.source = e.target := this.image_eq, rw [h.1, e'.bij_on_source.image_eq] at A, exact A.symm }, refine ⟨T, λx hx, _⟩, have xt : x ∈ e.target := hx, rw T at xt, have e's : e'.inv_fun x ∈ e.source, by { rw h.1, apply e'.map_target xt }, have A : e.to_fun (e.inv_fun x) = x := e.right_inv hx, have B : e.to_fun (e'.inv_fun x) = x, by { rw h.2, exact e'.right_inv xt, exact e's }, apply e.bij_on_source.inj_on (e.map_target hx) e's, rw [A, B] end /-- Two equivalent local equivs have the same source -/ lemma source_eq_of_eq_on_source {e e' : local_equiv α β} (h : e ≈ e') : e.source = e'.source := h.1 /-- Two equivalent local equivs have the same target -/ lemma target_eq_of_eq_on_source {e e' : local_equiv α β} (h : e ≈ e') : e.target = e'.target := (eq_on_source_symm h).1 /-- Two equivalent local equivs coincide on the source -/ lemma apply_eq_of_eq_on_source {e e' : local_equiv α β} (h : e ≈ e') {x : α} (hx : x ∈ e.source) : e.to_fun x = e'.to_fun x := h.2 x hx /-- Two equivalent local equivs have coinciding inverses on the target -/ lemma inv_apply_eq_of_eq_on_source {e e' : local_equiv α β} (h : e ≈ e') {x : β} (hx : x ∈ e.target) : e.inv_fun x = e'.inv_fun x := (eq_on_source_symm h).2 x hx /-- Composition of local equivs respects equivalence -/ lemma eq_on_source_trans {e e' : local_equiv α β} {f f' : local_equiv β γ} (he : e ≈ e') (hf : f ≈ f') : e.trans f ≈ e'.trans f' := begin split, { have : e.target = e'.target := (eq_on_source_symm he).1, rw [trans_source'', trans_source'', ← this, ← hf.1], exact eq_on.image_eq (λx hx, (eq_on_source_symm he).2 x hx.1) }, { assume x hx, rw trans_source at hx, simp [(he.2 x hx.1).symm, hf.2 _ hx.2] } end /-- Restriction of local equivs respects equivalence -/ lemma eq_on_source_restr {e e' : local_equiv α β} (he : e ≈ e') (s : set α) : e.restr s ≈ e'.restr s := begin split, { simp [he.1] }, { assume x hx, simp only [mem_inter_eq, restr_source] at hx, exact he.2 x hx.1 } end /-- Preimages are respected by equivalence -/ lemma eq_on_source_preimage {e e' : local_equiv α β} (he : e ≈ e') (s : set β) : e.source ∩ e.to_fun ⁻¹' s = e'.source ∩ e'.to_fun ⁻¹' s := begin ext x, simp only [mem_inter_eq, mem_preimage], split, { assume hx, rwa [apply_eq_of_eq_on_source (setoid.symm he), source_eq_of_eq_on_source (setoid.symm he)], rw source_eq_of_eq_on_source he at hx, exact hx.1 }, { assume hx, rwa [apply_eq_of_eq_on_source he, source_eq_of_eq_on_source he], rw source_eq_of_eq_on_source (setoid.symm he) at hx, exact hx.1 }, end /-- Composition of a local equiv and its inverse is equivalent to the restriction of the identity to the source -/ lemma trans_self_symm : e.trans e.symm ≈ local_equiv.of_set e.source := begin have A : (e.trans e.symm).source = e.source, by simp [trans_source, inter_eq_self_of_subset_left (source_subset_preimage_target _)], refine ⟨by simp [A], λx hx, _⟩, rw A at hx, simp [hx] end /-- Composition of the inverse of a local equiv and this local equiv is equivalent to the restriction of the identity to the target -/ lemma trans_symm_self : e.symm.trans e ≈ local_equiv.of_set e.target := trans_self_symm (e.symm) /-- Two equivalent local equivs are equal when the source and target are univ -/ lemma eq_of_eq_on_source_univ (e e' : local_equiv α β) (h : e ≈ e') (s : e.source = univ) (t : e.target = univ) : e = e' := begin apply local_equiv.ext _ _ (λx, _) (λx, _) h.1, { apply h.2 x, rw s, exact mem_univ _ }, { apply (eq_on_source_symm h).2 x, rw [symm_source, t], exact mem_univ _ } end section prod /-- The product of two local equivs, as a local equiv on the product. -/ def prod (e : local_equiv α β) (e' : local_equiv γ δ) : local_equiv (α × γ) (β × δ) := { source := set.prod e.source e'.source, target := set.prod e.target e'.target, to_fun := λp, (e.to_fun p.1, e'.to_fun p.2), inv_fun := λp, (e.inv_fun p.1, e'.inv_fun p.2), map_source := λp hp, by { simp at hp, simp [map_source, hp] }, map_target := λp hp, by { simp at hp, simp [map_target, hp] }, left_inv := λp hp, by { simp at hp, simp [hp] }, right_inv := λp hp, by { simp at hp, simp [hp] } } @[simp] lemma prod_source (e : local_equiv α β) (e' : local_equiv γ δ) : (e.prod e').source = set.prod e.source e'.source := rfl @[simp] lemma prod_target (e : local_equiv α β) (e' : local_equiv γ δ) : (e.prod e').target = set.prod e.target e'.target := rfl @[simp] lemma prod_to_fun (e : local_equiv α β) (e' : local_equiv γ δ) : (e.prod e').to_fun = (λp, (e.to_fun p.1, e'.to_fun p.2)) := rfl @[simp] lemma prod_inv_fun (e : local_equiv α β) (e' : local_equiv γ δ) : (e.prod e').inv_fun = (λp, (e.inv_fun p.1, e'.inv_fun p.2)) := rfl end prod end local_equiv namespace equiv /- equivs give rise to local_equiv. We set up simp lemmas to reduce most properties of the local equiv to that of the equiv. -/ variables (e : equiv α β) (e' : equiv β γ) @[simp] lemma to_local_equiv_to_fun : e.to_local_equiv.to_fun = e.to_fun := rfl @[simp] lemma to_local_equiv_inv_fun : e.to_local_equiv.inv_fun = e.inv_fun := rfl @[simp] lemma to_local_equiv_source : e.to_local_equiv.source = univ := rfl @[simp] lemma to_local_equiv_target : e.to_local_equiv.target = univ := rfl @[simp] lemma refl_to_local_equiv : (equiv.refl α).to_local_equiv = local_equiv.refl α := rfl @[simp] lemma symm_to_local_equiv : e.symm.to_local_equiv = e.to_local_equiv.symm := rfl @[simp] lemma trans_to_local_equiv : (e.trans e').to_local_equiv = e.to_local_equiv.trans e'.to_local_equiv := local_equiv.ext _ _ (λx, rfl) (λx, rfl) (by simp [local_equiv.trans_source, equiv.to_local_equiv]) end equiv
bae6d74100b641e78da5e0b1edd226dacb87a348
159fed64bfae88f3b6a6166836d6278f953bcbf9
/Structure/Generic/Lemmas/TypeRelations.lean
e01547ba1b33211b0cfa79ce850097e32db3fcfc
[ "MIT" ]
permissive
SReichelt/lean4-experiments
3e56830c8b2fbe3814eda071c48e3c8810d254a8
ff55357a01a34a91bf670d712637480089085ee4
refs/heads/main
1,683,977,454,907
1,622,991,121,000
1,622,991,121,000
340,765,677
2
0
null
null
null
null
UTF-8
Lean
false
false
2,546
lean
-- An abstract formalization of "isomorphism is equality up to relabeling" -- ------------------------------------------------------------------------- -- -- See `README.md` for more info. -- -- This file contains collections of category-theoretic axioms that are parameterized in such a way that -- they are suitable for all kinds of categories, groupoids, and higher groupoids. -- -- Several instances of these axioms can be constructed from basic structures provided by Lean (i.e. Core -- and a tiny bit of mathlib). These can be found in `Instances.lean`. import Structure.Generic.Axioms.Universes import Structure.Generic.Axioms.AbstractFunctors import Structure.Generic.Axioms.AbstractEquivalences import Structure.Generic.Axioms.GeneralizedProperties import Structure.Generic.Axioms.DependentGeneralizedProperties import Structure.Generic.Axioms.InstanceEquivalences open GeneralizedRelation open GeneralizedDependentRelation set_option autoBoundImplicitLocal false --set_option pp.universes true -- TODO: Add equivalences. -- TODO: Add (iso)morphisms as an additional type class. namespace HasLinearFunOp variable (U : Universe) [h : HasInternalFunctors U] [HasLinearFunOp U] instance : HasRefl h.Fun := ⟨idFun⟩ instance : HasTrans h.Fun := ⟨compFunFunFun _ _ _⟩ instance : IsPreorder h.Fun := ⟨⟩ instance hasArrows : HasArrows ⌈U⌉ U := ⟨h.Fun⟩ @[simp] theorem transDef {α β γ : U} {F : α ⟶ β} {G : β ⟶ γ} : G • F = G ⊙ F := compFunFunFun.effEff α β γ F G variable [hInst : HasNaturalEquivalences U] [HasLinearFunOp hInst.equivUniverse] def DependentArrow {α β : U} (F : α ⟶ β) (a : α) (b : β) := F a ≃ b namespace DependentArrow def refl {α : U} (a : α) : (idFun α) a ≃ a := Eq.ndrec (motive := λ x : α => ⌈x ≃ a⌉) (ident (hInst.Equiv α) a) (Eq.symm (idFun.eff α a)) def trans {α β γ : U} {F : α ⟶ β} {G : β ⟶ γ} {a : α} {b : β} {c : γ} : F a ≃ b ⟶ G b ≃ c ⟶ (G • F) a ≃ c := let f₁ : F a ≃ b ⟶ (G • F) a ≃ G b := compFunFunFun.effEffEff α β γ F G a ▸ HasEquivCongrArg.equiv_congrArg U G; HasTrans.trans ⊙ f₁ instance : HasDependentRefl (DependentArrow U) := ⟨refl U⟩ instance : HasDependentTrans (DependentArrow U) := ⟨trans U⟩ instance : IsDependentPreorder (DependentArrow U) := ⟨⟩ end DependentArrow instance hasDependentArrows : HasDependentArrows U U hInst.equivUniverse := ⟨DependentArrow U⟩ end HasLinearFunOp
5dd710bb19396511b1fb2faa53bb8a6e0ae7411e
63abd62053d479eae5abf4951554e1064a4c45b4
/src/algebra/continued_fractions/basic.lean
df1cc8ef1bb947f1bac511d6a1670bab64becadd
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
13,273
lean
/- Copyright (c) 2019 Kevin Kappelmann. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Kappelmann -/ import data.seq.seq import algebra.field /-! # Basic Definitions/Theorems for Continued Fractions ## Summary We define generalised, simple, and regular continued fractions and functions to evaluate their convergents. We follow the naming conventions from Wikipedia and [wall2018analytic], Chapter 1. ## Main definitions 1. Generalised continued fractions (gcfs) 2. Simple continued fractions (scfs) 3. (Regular) continued fractions ((r)cfs) 4. Computation of convergents using the recurrence relation in `convergents`. 5. Computation of convergents by directly evaluating the fraction described by the gcf in `convergents'`. ## Implementation notes 1. The most commonly used kind of continued fractions in the literature are regular continued fractions. We hence just call them `continued_fractions` in the library. 2. We use sequences from `data.seq` to encode potentially infinite sequences. ## References - <https://en.wikipedia.org/wiki/Generalized_continued_fraction> - [Wall, H.S., *Analytic Theory of Continued Fractions*][wall2018analytic] ## Tags numerics, number theory, approximations, fractions -/ -- Fix a carrier `α`. variable (α : Type*) /-- We collect a partial numerator `aᵢ` and partial denominator `bᵢ` in a pair `⟨aᵢ,bᵢ⟩`. -/ @[derive inhabited] protected structure generalized_continued_fraction.pair := (a : α) (b : α) /- Interlude: define some expected coercions and instances. -/ namespace generalized_continued_fraction.pair open generalized_continued_fraction as gcf /-- Make a gcf.pair printable. -/ instance [has_repr α] : has_repr (gcf.pair α) := ⟨λ p, "(a : " ++ (repr p.a) ++ ", b : " ++ (repr p.b) ++ ")"⟩ section coe /-! Interlude: define some expected coercions. -/ /- Fix another type `β` and assume `α` can be converted to `β`. -/ variables {α} {β : Type*} [has_coe α β] /-- Coerce a pair by elementwise coercion. -/ instance has_coe_to_generalized_continued_fraction_pair : has_coe (gcf.pair α) (gcf.pair β) := ⟨λ ⟨a, b⟩, ⟨(a : β), (b : β)⟩⟩ @[simp, norm_cast] lemma coe_to_generalized_continued_fraction_pair {a b : α} : (↑(gcf.pair.mk a b) : gcf.pair β) = gcf.pair.mk (a : β) (b : β) := rfl end coe end generalized_continued_fraction.pair /-- A *generalised continued fraction* (gcf) is a potentially infinite expression of the form a₀ h + --------------------------- a₁ b₀ + -------------------- a₂ b₁ + -------------- a₃ b₂ + -------- b₃ + ... where `h` is called the *head term* or *integer part*, the `aᵢ` are called the *partial numerators* and the `bᵢ` the *partial denominators* of the gcf. We store the sequence of partial numerators and denominators in a sequence of generalized_continued_fraction.pairs `s`. For convenience, one often writes `[h; (a₀, b₀), (a₁, b₁), (a₂, b₂),...]`. -/ structure generalized_continued_fraction := (h : α) (s : seq $ generalized_continued_fraction.pair α) variable {α} namespace generalized_continued_fraction open generalized_continued_fraction as gcf /-- Constructs a generalized continued fraction without fractional part. -/ def of_integer (a : α) : gcf α := ⟨a, seq.nil⟩ instance [inhabited α] : inhabited (gcf α) := ⟨of_integer (default _)⟩ /-- Returns the sequence of partial numerators `aᵢ` of `g`. -/ def partial_numerators (g : gcf α) : seq α := g.s.map gcf.pair.a /-- Returns the sequence of partial denominators `bᵢ` of `g`. -/ def partial_denominators (g : gcf α) : seq α := g.s.map gcf.pair.b /-- A gcf terminated at position `n` if its sequence terminates at position `n`. -/ def terminated_at (g : gcf α) (n : ℕ) : Prop := g.s.terminated_at n /-- It is decidable whether a gcf terminated at a given position. -/ instance terminated_at_decidable (g : gcf α) (n : ℕ) : decidable (g.terminated_at n) := by { unfold terminated_at, apply_instance } /-- A gcf terminates if its sequence terminates. -/ def terminates (g : gcf α) : Prop := g.s.terminates section coe /-! Interlude: define some expected coercions. -/ -- Fix another type `β` and assume `α` can be converted to `β`. variables {β : Type*} [has_coe α β] /-- Coerce a sequence by elementwise coercion. -/ def seq.coe_to_seq : has_coe (seq α) (seq β) := ⟨seq.map (λ a, (a : β))⟩ local attribute [instance] seq.coe_to_seq /-- Coerce a gcf by elementwise coercion. -/ instance has_coe_to_generalized_continued_fraction : has_coe (gcf α) (gcf β) := ⟨λ ⟨h, s⟩, ⟨(h : β), (s : seq $ gcf.pair β)⟩⟩ @[simp, norm_cast] lemma coe_to_generalized_continued_fraction {g : gcf α} : (↑(g : gcf α) : gcf β) = ⟨(g.h : β), (g.s : seq $ gcf.pair β)⟩ := by { cases g, refl } end coe end generalized_continued_fraction /-- A generalized continued fraction is a *simple continued fraction* if all partial numerators are equal to one. 1 h + --------------------------- 1 b₀ + -------------------- 1 b₁ + -------------- 1 b₂ + -------- b₃ + ... -/ def generalized_continued_fraction.is_simple_continued_fraction (g : generalized_continued_fraction α) [has_one α] : Prop := ∀ (n : ℕ) (aₙ : α), g.partial_numerators.nth n = some aₙ → aₙ = 1 variable (α) /-- A *simple continued fraction* (scf) is a generalized continued fraction (gcf) whose partial numerators are equal to one. 1 h + --------------------------- 1 b₀ + -------------------- 1 b₁ + -------------- 1 b₂ + -------- b₃ + ... For convenience, one often writes `[h; b₀, b₁, b₂,...]`. It is encoded as the subtype of gcfs that satisfy `generalized_continued_fraction.is_simple_continued_fraction`. -/ def simple_continued_fraction [has_one α] := {g : generalized_continued_fraction α // g.is_simple_continued_fraction} variable {α} /- Interlude: define some expected coercions. -/ namespace simple_continued_fraction open generalized_continued_fraction as gcf open simple_continued_fraction as scf variable [has_one α] /-- Constructs a simple continued fraction without fractional part. -/ def of_integer (a : α) : scf α := ⟨gcf.of_integer a, λ n aₙ h, by cases h⟩ instance : inhabited (scf α) := ⟨of_integer 1⟩ /-- Lift a scf to a gcf using the inclusion map. -/ instance has_coe_to_generalized_continued_fraction : has_coe (scf α) (gcf α) := by {unfold scf, apply_instance} lemma coe_to_generalized_continued_fraction {s : scf α} : (↑s : gcf α) = s.val := rfl end simple_continued_fraction /-- A simple continued fraction is a *(regular) continued fraction* ((r)cf) if all partial denominators `bᵢ` are positive, i.e. `0 < bᵢ`. -/ def simple_continued_fraction.is_regular_continued_fraction [has_one α] [has_zero α] [has_lt α] (s : simple_continued_fraction α) : Prop := ∀ (n : ℕ) (bₙ : α), (↑s : generalized_continued_fraction α).partial_denominators.nth n = some bₙ → 0 < bₙ variable (α) /-- A *(regular) continued fraction* ((r)cf) is a simple continued fraction (scf) whose partial denominators are all positive. It is the subtype of scfs that satisfy `simple_continued_fraction.is_regular_continued_fraction`. -/ def continued_fraction [has_one α] [has_zero α] [has_lt α] := {s : simple_continued_fraction α // s.is_regular_continued_fraction} variable {α} /- Interlude: define some expected coercions. -/ namespace continued_fraction open generalized_continued_fraction as gcf open simple_continued_fraction as scf open continued_fraction as cf variables [has_one α] [has_zero α] [has_lt α] /-- Constructs a continued fraction without fractional part. -/ def of_integer (a : α) : cf α := ⟨scf.of_integer a, λ n bₙ h, by cases h⟩ instance : inhabited (cf α) := ⟨of_integer 0⟩ /-- Lift a cf to a scf using the inclusion map. -/ instance has_coe_to_simple_continued_fraction : has_coe (cf α) (scf α) := by {unfold cf, apply_instance} lemma coe_to_simple_continued_fraction {c : cf α} : (↑c : scf α) = c.val := rfl /-- Lift a cf to a scf using the inclusion map. -/ instance has_coe_to_generalized_continued_fraction : has_coe (cf α) (gcf α) := ⟨λ c, ↑(↑c : scf α)⟩ lemma coe_to_generalized_continued_fraction {c : cf α} : (↑c : gcf α) = c.val := rfl end continued_fraction /- We now define how to compute the convergents of a gcf. There are two standard ways to do this: directly evaluating the (infinite) fraction described by the gcf or using a recurrence relation. For (r)cfs, these computations are equivalent as shown in `algebra.continued_fractions.convergents_equiv`. -/ namespace generalized_continued_fraction open generalized_continued_fraction as gcf -- Fix a division ring for the computations. variables {K : Type*} [division_ring K] /- We start with the definition of the recurrence relation. Given a gcf `g`, for all `n ≥ 1`, we define - `A₋₁ = 1, A₀ = h, Aₙ = bₙ₋₁ * Aₙ₋₁ + aₙ₋₁ * Aₙ₋₂`, and - `B₋₁ = 0, B₀ = 1, Bₙ = bₙ₋₁ * Bₙ₋₁ + aₙ₋₁ * Bₙ₋₂`. `Aₙ, `Bₙ` are called the *nth continuants*, Aₙ the *nth numerator*, and `Bₙ` the *nth denominator* of `g`. The *nth convergent* of `g` is given by `Aₙ / Bₙ`. -/ /-- Returns the next numerator `Aₙ = bₙ₋₁ * Aₙ₋₁ + aₙ₋₁ * Aₙ₋₂`, where `predA` is `Aₙ₋₁`, `ppredA` is `Aₙ₋₂`, `a` is `aₙ₋₁`, and `b` is `bₙ₋₁`. -/ def next_numerator (a b ppredA predA : K) : K := b * predA + a * ppredA /-- Returns the next denominator `Bₙ = bₙ₋₁ * Bₙ₋₁ + aₙ₋₁ * Bₙ₋₂``, where `predB` is `Bₙ₋₁` and `ppredB` is `Bₙ₋₂`, `a` is `aₙ₋₁`, and `b` is `bₙ₋₁`. -/ def next_denominator (aₙ bₙ ppredB predB : K) : K := bₙ * predB + aₙ * ppredB /-- Returns the next continuants `⟨Aₙ, Bₙ⟩` using `next_numerator` and `next_denominator`, where `pred` is `⟨Aₙ₋₁, Bₙ₋₁⟩`, `ppred` is `⟨Aₙ₋₂, Bₙ₋₂⟩`, `a` is `aₙ₋₁`, and `b` is `bₙ₋₁`. -/ def next_continuants (a b : K) (ppred pred : gcf.pair K) : gcf.pair K := ⟨next_numerator a b ppred.a pred.a, next_denominator a b ppred.b pred.b⟩ /-- Returns the continuants `⟨Aₙ₋₁, Bₙ₋₁⟩` of `g`. -/ def continuants_aux (g : gcf K) : stream (gcf.pair K) | 0 := ⟨1, 0⟩ | 1 := ⟨g.h, 1⟩ | (n + 2) := match g.s.nth n with | none := continuants_aux (n + 1) | some gp := next_continuants gp.a gp.b (continuants_aux n) (continuants_aux $ n + 1) end /-- Returns the continuants `⟨Aₙ, Bₙ⟩` of `g`. -/ def continuants (g : gcf K) : stream (gcf.pair K) := g.continuants_aux.tail /-- Returns the numerators `Aₙ` of `g`. -/ def numerators (g : gcf K) : stream K := g.continuants.map gcf.pair.a /-- Returns the denominators `Bₙ` of `g`. -/ def denominators (g : gcf K) : stream K := g.continuants.map gcf.pair.b /-- Returns the convergents `Aₙ / Bₙ` of `g`, where `Aₙ, Bₙ` are the nth continuants of `g`. -/ def convergents (g : gcf K) : stream K := λ (n : ℕ), (g.numerators n) / (g.denominators n) /-- Returns the approximation of the fraction described by the given sequence up to a given position n. For example, `convergents'_aux [(1, 2), (3, 4), (5, 6)] 2 = 1 / (2 + 3 / 4)` and `convergents'_aux [(1, 2), (3, 4), (5, 6)] 0 = 0`. -/ def convergents'_aux : seq (gcf.pair K) → ℕ → K | s 0 := 0 | s (n + 1) := match s.head with | none := 0 | some gp := gp.a / (gp.b + convergents'_aux s.tail n) end /-- Returns the convergents of `g` by evaluating the fraction described by `g` up to a given position `n`. For example, `convergents' [9; (1, 2), (3, 4), (5, 6)] 2 = 9 + 1 / (2 + 3 / 4)` and `convergents' [9; (1, 2), (3, 4), (5, 6)] 0 = 9` -/ def convergents' (g : gcf K) (n : ℕ) : K := g.h + convergents'_aux g.s n end generalized_continued_fraction -- Now, some basic, general theorems namespace generalized_continued_fraction open generalized_continued_fraction as gcf /-- Two gcfs `g` and `g'` are equal if and only if their components are equal. -/ protected lemma ext_iff {g g' : gcf α} : g = g' ↔ g.h = g'.h ∧ g.s = g'.s := by { cases g, cases g', simp } @[ext] protected lemma ext {g g' : gcf α} (hyp : g.h = g'.h ∧ g.s = g'.s) : g = g' := generalized_continued_fraction.ext_iff.elim_right hyp end generalized_continued_fraction
49067636edc5a109ace6e537cb813d37b462516c
947b78d97130d56365ae2ec264df196ce769371a
/src/Lean/Util/Sorry.lean
33b52086374fb17421376fd89fed6a122b3c3403
[ "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
2,545
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Message import Lean.Exception namespace Lean def Expr.isSorry : Expr → Bool | Expr.app (Expr.app (Expr.const `sorryAx _ _) _ _) _ _ => true | _ => false def Expr.isSyntheticSorry : Expr → Bool | Expr.app (Expr.app (Expr.const `sorryAx _ _) _ _) (Expr.const `Bool.true _ _) _ => true | _ => false def Expr.hasSorry : Expr → Bool | Expr.const c _ _ => c == `sorryAx | Expr.app f a _ => f.hasSorry || a.hasSorry | Expr.letE _ t v b _ => t.hasSorry || v.hasSorry || b.hasSorry | Expr.forallE _ d b _ => d.hasSorry || b.hasSorry | Expr.lam _ d b _ => d.hasSorry || b.hasSorry | Expr.mdata _ e _ => e.hasSorry | Expr.proj _ _ e _ => e.hasSorry | _ => false def Expr.hasSyntheticSorry : Expr → Bool | e@(Expr.app f a _) => e.isSyntheticSorry || f.hasSyntheticSorry || a.hasSyntheticSorry | Expr.letE _ t v b _ => t.hasSyntheticSorry || v.hasSyntheticSorry || b.hasSyntheticSorry | Expr.forallE _ d b _ => d.hasSyntheticSorry || b.hasSyntheticSorry | Expr.lam _ d b _ => d.hasSyntheticSorry || b.hasSyntheticSorry | Expr.mdata _ e _ => e.hasSyntheticSorry | Expr.proj _ _ e _ => e.hasSyntheticSorry | _ => false partial def MessageData.hasSorry : MessageData → Bool | MessageData.ofExpr e => e.hasSorry | MessageData.withContext _ msg => msg.hasSorry | MessageData.nest _ msg => msg.hasSorry | MessageData.group msg => msg.hasSorry | MessageData.compose msg₁ msg₂ => msg₁.hasSorry || msg₂.hasSorry | MessageData.tagged _ msg => msg.hasSorry | MessageData.node msgs => msgs.any MessageData.hasSorry | _ => false partial def MessageData.hasSyntheticSorry : MessageData → Bool | MessageData.ofExpr e => e.hasSyntheticSorry | MessageData.withContext _ msg => msg.hasSyntheticSorry | MessageData.nest _ msg => msg.hasSyntheticSorry | MessageData.group msg => msg.hasSyntheticSorry | MessageData.compose msg₁ msg₂ => msg₁.hasSyntheticSorry || msg₂.hasSyntheticSorry | MessageData.tagged _ msg => msg.hasSyntheticSorry | MessageData.node msgs => msgs.any MessageData.hasSyntheticSorry | _ => false def Exception.hasSyntheticSorry : Exception → Bool | Exception.error _ msg => msg.hasSyntheticSorry | _ => false end Lean
d57836852a69a951add045dd3595443d6f8590d6
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/tactic/default.lean
873da8c3400a1c4349105f25ea7899dcde985e23
[ "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
970
lean
/- This file imports many useful tactics ("the kitchen sink"). You can use `import tactic` at the beginning of your file to get everything. (Although you may want to strip things down when you're polishing.) Because this file imports some complicated tactics, it has many transitive dependencies (which of course may not use `import tactic`, and must import selectively). As (non-exhaustive) examples, these includes things like: * algebra.group_power * algebra.ordered_ring * data.rat * data.nat.prime * data.list.perm * data.set.lattice * data.equiv.encodable * order.complete_lattice -/ import tactic.basic tactic.monotonicity.interactive tactic.finish tactic.tauto tactic.tidy tactic.abel tactic.ring tactic.ring_exp tactic.linarith tactic.omega tactic.wlog tactic.tfae tactic.apply_fun tactic.apply tactic.pi_instances tactic.fin_cases tactic.interval_cases tactic.reassoc_axiom -- most likely useful only for category_theory
01a16e01aea135510baefea82e6e06369526ca8f
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebraic_topology/simplicial_set.lean
6727591420e4e7134e4225491bb18cc4709a425e
[ "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
5,937
lean
/- Copyright (c) 2021 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Scott Morrison, Adam Topaz -/ import algebraic_topology.simplicial_object import algebraic_topology.topological_simplex import category_theory.limits.presheaf import category_theory.limits.types import category_theory.yoneda import topology.category.Top.limits /-! A simplicial set is just a simplicial object in `Type`, i.e. a `Type`-valued presheaf on the simplex category. (One might be tempted to call these "simplicial types" when working in type-theoretic foundations, but this would be unnecessarily confusing given the existing notion of a simplicial type in homotopy type theory.) We define the standard simplices `Δ[n]` as simplicial sets, and their boundaries `∂Δ[n]` and horns `Λ[n, i]`. (The notations are available via `open_locale simplicial`.) ## Future work There isn't yet a complete API for simplices, boundaries, and horns. As an example, we should have a function that constructs from a non-surjective order preserving function `fin n → fin n` a morphism `Δ[n] ⟶ ∂Δ[n]`. -/ universes v u open category_theory category_theory.limits open_locale simplicial /-- The category of simplicial sets. This is the category of contravariant functors from `simplex_category` to `Type u`. -/ @[derive [large_category, limits.has_limits, limits.has_colimits]] def sSet : Type (u+1) := simplicial_object (Type u) namespace sSet /-- The `n`-th standard simplex `Δ[n]` associated with a nonempty finite linear order `n` is the Yoneda embedding of `n`. -/ def standard_simplex : simplex_category ⥤ sSet := yoneda localized "notation (name := standard_simplex) `Δ[`n`]` := sSet.standard_simplex.obj (simplex_category.mk n)" in simplicial instance : inhabited sSet := ⟨Δ[0]⟩ section /-- The `m`-simplices of the `n`-th standard simplex are the monotone maps from `fin (m+1)` to `fin (n+1)`. -/ def as_order_hom {n} {m} (α : Δ[n].obj m) : order_hom (fin (m.unop.len+1)) (fin (n+1)) := α.to_order_hom end /-- The boundary `∂Δ[n]` of the `n`-th standard simplex consists of all `m`-simplices of `standard_simplex n` that are not surjective (when viewed as monotone function `m → n`). -/ def boundary (n : ℕ) : sSet := { obj := λ m, {α : Δ[n].obj m // ¬ function.surjective (as_order_hom α)}, map := λ m₁ m₂ f α, ⟨f.unop ≫ (α : Δ[n].obj m₁), by { intro h, apply α.property, exact function.surjective.of_comp h }⟩ } localized "notation (name := sSet.boundary) `∂Δ[`n`]` := sSet.boundary n" in simplicial /-- The inclusion of the boundary of the `n`-th standard simplex into that standard simplex. -/ def boundary_inclusion (n : ℕ) : ∂Δ[n] ⟶ Δ[n] := { app := λ m (α : {α : Δ[n].obj m // _}), α } /-- `horn n i` (or `Λ[n, i]`) is the `i`-th horn of the `n`-th standard simplex, where `i : n`. It consists of all `m`-simplices `α` of `Δ[n]` for which the union of `{i}` and the range of `α` is not all of `n` (when viewing `α` as monotone function `m → n`). -/ def horn (n : ℕ) (i : fin (n+1)) : sSet := { obj := λ m, { α : Δ[n].obj m // set.range (as_order_hom α) ∪ {i} ≠ set.univ }, map := λ m₁ m₂ f α, ⟨f.unop ≫ (α : Δ[n].obj m₁), begin intro h, apply α.property, rw set.eq_univ_iff_forall at h ⊢, intro j, apply or.imp _ id (h j), intro hj, exact set.range_comp_subset_range _ _ hj, end⟩ } localized "notation (name := sSet.horn) `Λ[`n`, `i`]` := sSet.horn (n : ℕ) i" in simplicial /-- The inclusion of the `i`-th horn of the `n`-th standard simplex into that standard simplex. -/ def horn_inclusion (n : ℕ) (i : fin (n+1)) : Λ[n, i] ⟶ Δ[n] := { app := λ m (α : {α : Δ[n].obj m // _}), α } section examples open_locale simplicial /-- The simplicial circle. -/ noncomputable def S1 : sSet := limits.colimit $ limits.parallel_pair ((standard_simplex.map $ simplex_category.δ 0) : Δ[0] ⟶ Δ[1]) (standard_simplex.map $ simplex_category.δ 1) end examples /-- Truncated simplicial sets. -/ @[derive [large_category, limits.has_limits, limits.has_colimits]] def truncated (n : ℕ) := simplicial_object.truncated (Type u) n /-- The skeleton functor on simplicial sets. -/ def sk (n : ℕ) : sSet ⥤ sSet.truncated n := simplicial_object.sk n instance {n} : inhabited (sSet.truncated n) := ⟨(sk n).obj $ Δ[0]⟩ /-- The category of augmented simplicial sets, as a particular case of augmented simplicial objects. -/ abbreviation augmented := simplicial_object.augmented (Type u) namespace augmented /-- The functor which sends `[n]` to the simplicial set `Δ[n]` equipped by the obvious augmentation towards the terminal object of the category of sets. -/ @[simps] noncomputable def standard_simplex : simplex_category ⥤ sSet.augmented := { obj := λ Δ, { left := sSet.standard_simplex.obj Δ, right := terminal _, hom := { app := λ Δ', terminal.from _, }, }, map := λ Δ₁ Δ₂ θ, { left := sSet.standard_simplex.map θ, right := terminal.from _, }, } end augmented end sSet /-- The functor associating the singular simplicial set to a topological space. -/ def Top.to_sSet : Top ⥤ sSet := colimit_adj.restricted_yoneda simplex_category.to_Top /-- The geometric realization functor. -/ noncomputable def sSet.to_Top : sSet ⥤ Top := colimit_adj.extend_along_yoneda simplex_category.to_Top /-- Geometric realization is left adjoint to the singular simplicial set construction. -/ noncomputable def sSet_Top_adj : sSet.to_Top ⊣ Top.to_sSet := colimit_adj.yoneda_adjunction _ /-- The geometric realization of the representable simplicial sets agree with the usual topological simplices. -/ noncomputable def sSet.to_Top_simplex : (yoneda : simplex_category ⥤ _) ⋙ sSet.to_Top ≅ simplex_category.to_Top := colimit_adj.is_extension_along_yoneda _
2afc531f0e6fe9fee9ad3e51f41df95811d291a4
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/tactic/abel.lean
373396f74648619298b5fc0b8fc54c3833718481
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
13,121
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 tactic.norm_num /-! # The `abel` tactic Evaluate expressions in the language of additive, commutative monoids and groups. -/ namespace tactic namespace abel meta structure cache := (α : expr) (univ : level) (α0 : expr) (is_group : bool) (inst : expr) meta def mk_cache (e : expr) : tactic cache := do α ← infer_type e, c ← mk_app ``add_comm_monoid [α] >>= mk_instance, cg ← try_core (mk_app ``add_comm_group [α] >>= mk_instance), u ← mk_meta_univ, infer_type α >>= unify (expr.sort (level.succ u)), u ← get_univ_assignment u, α0 ← expr.of_nat α 0, match cg with | (some cg) := return ⟨α, u, α0, tt, cg⟩ | _ := return ⟨α, u, α0, ff, c⟩ end meta def cache.app (c : cache) (n : name) (inst : expr) : list expr → expr := (@expr.const tt n [c.univ] c.α inst).mk_app meta def cache.mk_app (c : cache) (n inst : name) (l : list expr) : tactic expr := do m ← mk_instance ((expr.const inst [c.univ] : expr) c.α), return $ c.app n m l meta def add_g : name → name | (name.mk_string s p) := name.mk_string (s ++ "g") p | n := n meta def cache.iapp (c : cache) (n : name) : list expr → expr := c.app (if c.is_group then add_g n else n) c.inst def term {α} [add_comm_monoid α] (n : ℕ) (x a : α) : α := n •ℕ x + a def termg {α} [add_comm_group α] (n : ℤ) (x a : α) : α := n •ℤ x + a meta def cache.mk_term (c : cache) (n x a : expr) : expr := c.iapp ``term [n, x, a] meta def cache.int_to_expr (c : cache) (n : ℤ) : tactic expr := expr.of_int (if c.is_group then `(ℤ) else `(ℕ)) n meta inductive normal_expr : Type | zero (e : expr) : normal_expr | nterm (e : expr) (n : expr × ℤ) (x : expr) (a : normal_expr) : normal_expr meta def normal_expr.e : normal_expr → expr | (normal_expr.zero e) := e | (normal_expr.nterm e _ _ _) := e meta instance : has_coe normal_expr expr := ⟨normal_expr.e⟩ meta instance : has_coe_to_fun normal_expr := ⟨_, λ e, ((e : expr) : expr → expr)⟩ meta def normal_expr.term' (c : cache) (n : expr × ℤ) (x : expr) (a : normal_expr) : normal_expr := normal_expr.nterm (c.mk_term n.1 x a) n x a meta def normal_expr.zero' (c : cache) : normal_expr := normal_expr.zero c.α0 meta def normal_expr.to_list : normal_expr → list (ℤ × expr) | (normal_expr.zero _) := [] | (normal_expr.nterm _ (_, n) x a) := (n, x) :: a.to_list open normal_expr meta def normal_expr.to_string (e : normal_expr) : string := " + ".intercalate $ (to_list e).map $ λ ⟨n, e⟩, to_string n ++ " • (" ++ to_string e ++ ")" meta def normal_expr.pp (e : normal_expr) : tactic format := do l ← (to_list e).mmap (λ ⟨n, e⟩, do pe ← pp e, return (to_fmt n ++ " • (" ++ pe ++ ")")), return $ format.join $ l.intersperse ↑" + " meta instance : has_to_tactic_format normal_expr := ⟨normal_expr.pp⟩ meta def normal_expr.refl_conv (e : normal_expr) : tactic (normal_expr × expr) := do p ← mk_eq_refl e, return (e, p) theorem const_add_term {α} [add_comm_monoid α] (k n x a a') (h : k + a = a') : k + @term α _ n x a = term n x a' := by simp [h.symm, term]; ac_refl theorem const_add_termg {α} [add_comm_group α] (k n x a a') (h : k + a = a') : k + @termg α _ n x a = termg n x a' := by simp [h.symm, termg]; ac_refl theorem term_add_const {α} [add_comm_monoid α] (n x a k a') (h : a + k = a') : @term α _ n x a + k = term n x a' := by simp [h.symm, term, add_assoc] theorem term_add_constg {α} [add_comm_group α] (n x a k a') (h : a + k = a') : @termg α _ n x a + k = termg n x a' := by simp [h.symm, termg, add_assoc] theorem term_add_term {α} [add_comm_monoid α] (n₁ x a₁ n₂ a₂ n' a') (h₁ : n₁ + n₂ = n') (h₂ : a₁ + a₂ = a') : @term α _ n₁ x a₁ + @term α _ n₂ x a₂ = term n' x a' := by simp [h₁.symm, h₂.symm, term, add_nsmul]; ac_refl theorem term_add_termg {α} [add_comm_group α] (n₁ x a₁ n₂ a₂ n' a') (h₁ : n₁ + n₂ = n') (h₂ : a₁ + a₂ = a') : @termg α _ n₁ x a₁ + @termg α _ n₂ x a₂ = termg n' x a' := by simp [h₁.symm, h₂.symm, termg, add_gsmul]; ac_refl theorem zero_term {α} [add_comm_monoid α] (x a) : @term α _ 0 x a = a := by simp [term] theorem zero_termg {α} [add_comm_group α] (x a) : @termg α _ 0 x a = a := by simp [termg] meta def eval_add (c : cache) : normal_expr → normal_expr → tactic (normal_expr × expr) | (zero _) e₂ := do p ← mk_app ``zero_add [e₂], return (e₂, p) | e₁ (zero _) := do p ← mk_app ``add_zero [e₁], return (e₁, p) | he₁@(nterm e₁ n₁ x₁ a₁) he₂@(nterm e₂ n₂ x₂ a₂) := if expr.lex_lt x₁ x₂ then do (a', h) ← eval_add a₁ he₂, return (term' c n₁ x₁ a', c.iapp ``term_add_const [n₁.1, x₁, a₁, e₂, a', h]) else if x₁ ≠ x₂ then do (a', h) ← eval_add he₁ a₂, return (term' c n₂ x₂ a', c.iapp ``const_add_term [e₁, n₂.1, x₂, a₂, a', h]) else do (n', h₁) ← mk_app ``has_add.add [n₁.1, n₂.1] >>= norm_num.derive', (a', h₂) ← eval_add a₁ a₂, let k := n₁.2 + n₂.2, let p₁ := c.iapp ``term_add_term [n₁.1, x₁, a₁, n₂.1, a₂, n', a', h₁, h₂], if k = 0 then do p ← mk_eq_trans p₁ (c.iapp ``zero_term [x₁, a']), return (a', p) else return (term' c (n', k) x₁ a', p₁) theorem term_neg {α} [add_comm_group α] (n x a n' a') (h₁ : -n = n') (h₂ : -a = a') : -@termg α _ n x a = termg n' x a' := by simp [h₂.symm, h₁.symm, termg]; ac_refl meta def eval_neg (c : cache) : normal_expr → tactic (normal_expr × expr) | (zero e) := do p ← c.mk_app ``neg_zero ``add_group [], return (zero' c, p) | (nterm e n x a) := do (n', h₁) ← mk_app ``has_neg.neg [n.1] >>= norm_num.derive', (a', h₂) ← eval_neg a, return (term' c (n', -n.2) x a', c.app ``term_neg c.inst [n.1, x, a, n', a', h₁, h₂]) def smul {α} [add_comm_monoid α] (n : ℕ) (x : α) : α := n •ℕ x def smulg {α} [add_comm_group α] (n : ℤ) (x : α) : α := n •ℤ x theorem zero_smul {α} [add_comm_monoid α] (c) : smul c (0 : α) = 0 := by simp [smul] theorem zero_smulg {α} [add_comm_group α] (c) : smulg c (0 : α) = 0 := by simp [smulg] theorem term_smul {α} [add_comm_monoid α] (c n x a n' a') (h₁ : c * n = n') (h₂ : smul c a = a') : smul c (@term α _ n x a) = term n' x a' := by simp [h₂.symm, h₁.symm, term, smul, nsmul_add, mul_nsmul] theorem term_smulg {α} [add_comm_group α] (c n x a n' a') (h₁ : c * n = n') (h₂ : smulg c a = a') : smulg c (@termg α _ n x a) = termg n' x a' := by simp [h₂.symm, h₁.symm, termg, smulg, gsmul_add, gsmul_mul] meta def eval_smul (c : cache) (k : expr × ℤ) : normal_expr → tactic (normal_expr × expr) | (zero _) := return (zero' c, c.iapp ``zero_smul [k.1]) | (nterm e n x a) := do (n', h₁) ← mk_app ``has_mul.mul [k.1, n.1] >>= norm_num.derive', (a', h₂) ← eval_smul a, return (term' c (n', k.2 * n.2) x a', c.iapp ``term_smul [k.1, n.1, x, a, n', a', h₁, h₂]) theorem term_atom {α} [add_comm_monoid α] (x : α) : x = term 1 x 0 := by simp [term] theorem term_atomg {α} [add_comm_group α] (x : α) : x = termg 1 x 0 := by simp [termg] meta def eval_atom (c : cache) (e : expr) : tactic (normal_expr × expr) := do n1 ← c.int_to_expr 1, return (term' c (n1, 1) e (zero' c), c.iapp ``term_atom [e]) lemma unfold_sub {α} [add_group α] (a b c : α) (h : a + -b = c) : a - b = c := h theorem unfold_smul {α} [add_comm_monoid α] (n) (x y : α) (h : smul n x = y) : n •ℕ x = y := h theorem unfold_smulg {α} [add_comm_group α] (n : ℕ) (x y : α) (h : smulg (int.of_nat n) x = y) : n •ℕ x = y := h theorem unfold_gsmul {α} [add_comm_group α] (n : ℤ) (x y : α) (h : smulg n x = y) : gsmul n x = y := h lemma subst_into_smul {α} [add_comm_monoid α] (l r tl tr t) (prl : l = tl) (prr : r = tr) (prt : @smul α _ tl tr = t) : smul l r = t := by simp [prl, prr, prt] lemma subst_into_smulg {α} [add_comm_group α] (l r tl tr t) (prl : l = tl) (prr : r = tr) (prt : @smulg α _ tl tr = t) : smulg l r = t := by simp [prl, prr, prt] meta def eval (c : cache) : expr → tactic (normal_expr × expr) | `(%%e₁ + %%e₂) := do (e₁', p₁) ← eval e₁, (e₂', p₂) ← eval e₂, (e', p') ← eval_add c e₁' e₂', p ← c.mk_app ``norm_num.subst_into_add ``has_add [e₁, e₂, e₁', e₂', e', p₁, p₂, p'], return (e', p) | `(%%e₁ - %%e₂) := do e₂' ← mk_app ``has_neg.neg [e₂], e ← mk_app ``has_add.add [e₁, e₂'], (e', p) ← eval e, p' ← c.mk_app ``unfold_sub ``add_group [e₁, e₂, e', p], return (e', p') | `(- %%e) := do (e₁, p₁) ← eval e, (e₂, p₂) ← eval_neg c e₁, p ← c.mk_app ``norm_num.subst_into_neg ``has_neg [e, e₁, e₂, p₁, p₂], return (e₂, p) | `(nsmul %%e₁ %%e₂) := do n ← if c.is_group then mk_app ``int.of_nat [e₁] else return e₁, (e', p) ← eval $ c.iapp ``smul [n, e₂], return (e', c.iapp ``unfold_smul [e₁, e₂, e', p]) | `(gsmul %%e₁ %%e₂) := do guardb c.is_group, (e', p) ← eval $ c.iapp ``smul [e₁, e₂], return (e', c.app ``unfold_gsmul c.inst [e₁, e₂, e', p]) | `(smul %%e₁ %%e₂) := do guard (¬ c.is_group), (e₁', p₁) ← norm_num.derive e₁ <|> refl_conv e₁, n ← e₁'.to_nat, (e₂', p₂) ← eval e₂, (e', p) ← eval_smul c (e₁', n) e₂', return (e', c.iapp ``subst_into_smul [e₁, e₂, e₁', e₂', e', p₁, p₂, p]) | `(smulg %%e₁ %%e₂) := do guardb c.is_group, (e₁', p₁) ← norm_num.derive e₁ <|> refl_conv e₁, n ← e₁'.to_int, (e₂', p₂) ← eval e₂, (e', p) ← eval_smul c (e₁', n) e₂', return (e', c.iapp ``subst_into_smul [e₁, e₂, e₁', e₂', e', p₁, p₂, p]) | e := eval_atom c e meta def eval' (c : cache) (e : expr) : tactic (expr × expr) := do (e', p) ← eval c e, return (e', p) @[derive has_reflect] inductive normalize_mode | raw | term instance : inhabited normalize_mode := ⟨normalize_mode.term⟩ meta def normalize (mode := normalize_mode.term) (e : expr) : tactic (expr × expr) := do pow_lemma ← simp_lemmas.mk.add_simp ``pow_one, let lemmas := match mode with | normalize_mode.term := [``term.equations._eqn_1, ``termg.equations._eqn_1, ``add_zero, ``one_nsmul, ``one_gsmul, ``gsmul_zero] | _ := [] end, lemmas ← lemmas.mfoldl simp_lemmas.add_simp simp_lemmas.mk, (_, e', pr) ← ext_simplify_core () {} simp_lemmas.mk (λ _, failed) (λ _ _ _ _ e, do c ← mk_cache e, (new_e, pr) ← match mode with | normalize_mode.raw := eval' c | normalize_mode.term := trans_conv (eval' c) (simplify lemmas []) end e, guard (¬ new_e =ₐ e), return ((), new_e, some pr, ff)) (λ _ _ _ _ _, failed) `eq e, return (e', pr) end abel namespace interactive open interactive interactive.types lean.parser open tactic.abel local postfix `?`:9001 := optional /-- Tactic for solving equations in the language of *additive*, commutative monoids and groups. This version of `abel` fails if the target is not an equality that is provable by the axioms of commutative monoids/groups. -/ meta def abel1 : tactic unit := do `(%%e₁ = %%e₂) ← target, c ← mk_cache e₁, (e₁', p₁) ← eval c e₁, (e₂', p₂) ← eval c e₂, is_def_eq e₁' e₂', p ← mk_eq_symm p₂ >>= mk_eq_trans p₁, tactic.exact p meta def abel.mode : lean.parser abel.normalize_mode := with_desc "(raw|term)?" $ do mode ← ident?, match mode with | none := return abel.normalize_mode.term | some `term := return abel.normalize_mode.term | some `raw := return abel.normalize_mode.raw | _ := failed end /-- Evaluate expressions in the language of *additive*, commutative monoids and groups. It attempts to prove the goal outright if there is no `at` specifier and the target is an equality, but if this fails, it falls back to rewriting all monoid expressions into a normal form. If there is an `at` specifier, it rewrites the given target into a normal form. ```lean example {α : Type*} {a b : α} [add_comm_monoid α] : a + (b + a) = a + a + b := by abel example {α : Type*} {a b : α} [add_comm_group α] : (a + b) - ((b + a) + a) = -a := by abel example {α : Type*} {a b : α} [add_comm_group α] (hyp : a + a - a = b - b) : a = 0 := by { abel at hyp, exact hyp } ``` -/ meta def abel (SOP : parse abel.mode) (loc : parse location) : tactic unit := match loc with | interactive.loc.ns [none] := abel1 | _ := failed end <|> do ns ← loc.get_locals, tt ← tactic.replace_at (normalize SOP) ns loc.include_goal | fail "abel failed to simplify", when loc.include_goal $ try tactic.reflexivity add_tactic_doc { name := "abel", category := doc_category.tactic, decl_names := [`tactic.interactive.abel], tags := ["arithmetic", "decision procedure"] } end interactive end tactic
d48290864cbd997ce427d278fcf962fc5ac8b6c8
4727251e0cd73359b15b664c3170e5d754078599
/src/logic/relation.lean
588181c272806abfb4a4a3676aec0cb409ddb5e9
[ "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
21,464
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import tactic.basic import logic.relator /-! # Relation closures This file defines the reflexive, transitive, and reflexive transitive closures of relations. It also proves some basic results on definitions in core, such as `eqv_gen`. Note that this is about unbundled relations, that is terms of types of the form `α → β → Prop`. For the bundled version, see `rel`. ## Definitions * `relation.refl_gen`: Reflexive closure. `refl_gen r` relates everything `r` related, plus for all `a` it relates `a` with itself. So `refl_gen r a b ↔ r a b ∨ a = b`. * `relation.trans_gen`: Transitive closure. `trans_gen r` relates everything `r` related transitively. So `trans_gen r a b ↔ ∃ x₀ ... xₙ, r a x₀ ∧ r x₀ x₁ ∧ ... ∧ r xₙ b`. * `relation.refl_trans_gen`: Reflexive transitive closure. `refl_trans_gen r` relates everything `r` related transitively, plus for all `a` it relates `a` with itself. So `refl_trans_gen r a b ↔ (∃ x₀ ... xₙ, r a x₀ ∧ r x₀ x₁ ∧ ... ∧ r xₙ b) ∨ a = b`. It is the same as the reflexive closure of the transitive closure, or the transitive closure of the reflexive closure. In terms of rewriting systems, this means that `a` can be rewritten to `b` in a number of rewrites. * `relation.comp`: Relation composition. We provide notation `∘r`. For `r : α → β → Prop` and `s : β → γ → Prop`, `r ∘r s`relates `a : α` and `c : γ` iff there exists `b : β` that's related to both. * `relation.map`: Image of a relation under a pair of maps. For `r : α → β → Prop`, `f : α → γ`, `g : β → δ`, `map r f g` is the relation `γ → δ → Prop` relating `f a` and `g b` for all `a`, `b` related by `r`. * `relation.join`: Join of a relation. For `r : α → α → Prop`, `join r a b ↔ ∃ c, r a c ∧ r b c`. In terms of rewriting systems, this means that `a` and `b` can be rewritten to the same term. -/ open function variables {α β γ δ : Type*} section ne_imp variable {r : α → α → Prop} lemma is_refl.reflexive [is_refl α r] : reflexive r := λ x, is_refl.refl x /-- To show a reflexive relation `r : α → α → Prop` holds over `x y : α`, it suffices to show it holds when `x ≠ y`. -/ lemma reflexive.rel_of_ne_imp (h : reflexive r) {x y : α} (hr : x ≠ y → r x y) : r x y := begin by_cases hxy : x = y, { exact hxy ▸ h x }, { exact hr hxy } end /-- If a reflexive relation `r : α → α → Prop` holds over `x y : α`, then it holds whether or not `x ≠ y`. -/ lemma reflexive.ne_imp_iff (h : reflexive r) {x y : α} : (x ≠ y → r x y) ↔ r x y := ⟨h.rel_of_ne_imp, λ hr _, hr⟩ /-- If a reflexive relation `r : α → α → Prop` holds over `x y : α`, then it holds whether or not `x ≠ y`. Unlike `reflexive.ne_imp_iff`, this uses `[is_refl α r]`. -/ lemma reflexive_ne_imp_iff [is_refl α r] {x y : α} : (x ≠ y → r x y) ↔ r x y := is_refl.reflexive.ne_imp_iff protected lemma symmetric.iff (H : symmetric r) (x y : α) : r x y ↔ r y x := ⟨λ h, H h, λ h, H h⟩ lemma symmetric.flip_eq (h : symmetric r) : flip r = r := funext₂ $ λ _ _, propext $ h.iff _ _ lemma symmetric.swap_eq : symmetric r → swap r = r := symmetric.flip_eq lemma flip_eq_iff : flip r = r ↔ symmetric r := ⟨λ h x y, (congr_fun₂ h _ _).mp, symmetric.flip_eq⟩ lemma swap_eq_iff : swap r = r ↔ symmetric r := flip_eq_iff end ne_imp section comap variables {r : β → β → Prop} lemma reflexive.comap (h : reflexive r) (f : α → β) : reflexive (r on f) := λ a, h (f a) lemma symmetric.comap (h : symmetric r) (f : α → β) : symmetric (r on f) := λ a b hab, h hab lemma transitive.comap (h : transitive r) (f : α → β) : transitive (r on f) := λ a b c hab hbc, h hab hbc lemma equivalence.comap (h : equivalence r) (f : α → β) : equivalence (r on f) := ⟨h.1.comap f, h.2.1.comap f, h.2.2.comap f⟩ end comap namespace relation section comp variables {r : α → β → Prop} {p : β → γ → Prop} {q : γ → δ → Prop} /-- The composition of two relations, yielding a new relation. The result relates a term of `α` and a term of `γ` if there is an intermediate term of `β` related to both. -/ def comp (r : α → β → Prop) (p : β → γ → Prop) (a : α) (c : γ) : Prop := ∃ b, r a b ∧ p b c local infixr ` ∘r ` : 80 := relation.comp lemma comp_eq : r ∘r (=) = r := funext $ λ a, funext $ λ b, propext $ iff.intro (λ ⟨c, h, eq⟩, eq ▸ h) (λ h, ⟨b, h, rfl⟩) lemma eq_comp : (=) ∘r r = r := funext $ λ a, funext $ λ b, propext $ iff.intro (λ ⟨c, eq, h⟩, eq.symm ▸ h) (λ h, ⟨a, rfl, h⟩) lemma iff_comp {r : Prop → α → Prop} : (↔) ∘r r = r := have (↔) = (=), by funext a b; exact iff_eq_eq, by rw [this, eq_comp] lemma comp_iff {r : α → Prop → Prop} : r ∘r (↔) = r := have (↔) = (=), by funext a b; exact iff_eq_eq, by rw [this, comp_eq] lemma comp_assoc : (r ∘r p) ∘r q = r ∘r p ∘r q := begin funext a d, apply propext, split, exact λ ⟨c, ⟨b, hab, hbc⟩, hcd⟩, ⟨b, hab, c, hbc, hcd⟩, exact λ ⟨b, hab, c, hbc, hcd⟩, ⟨c, ⟨b, hab, hbc⟩, hcd⟩ end lemma flip_comp : flip (r ∘r p) = (flip p) ∘r (flip r) := begin funext c a, apply propext, split, exact λ ⟨b, hab, hbc⟩, ⟨b, hbc, hab⟩, exact λ ⟨b, hbc, hab⟩, ⟨b, hab, hbc⟩ end end comp /-- The map of a relation `r` through a pair of functions pushes the relation to the codomains of the functions. The resulting relation is defined by having pairs of terms related if they have preimages related by `r`. -/ protected def map (r : α → β → Prop) (f : α → γ) (g : β → δ) : γ → δ → Prop := λ c d, ∃ a b, r a b ∧ f a = c ∧ g b = d variables {r : α → α → Prop} {a b c d : α} /-- `refl_trans_gen r`: reflexive transitive closure of `r` -/ @[mk_iff relation.refl_trans_gen.cases_tail_iff] inductive refl_trans_gen (r : α → α → Prop) (a : α) : α → Prop | refl : refl_trans_gen a | tail {b c} : refl_trans_gen b → r b c → refl_trans_gen c attribute [refl] refl_trans_gen.refl /-- `refl_gen r`: reflexive closure of `r` -/ @[mk_iff] inductive refl_gen (r : α → α → Prop) (a : α) : α → Prop | refl : refl_gen a | single {b} : r a b → refl_gen b /-- `trans_gen r`: transitive closure of `r` -/ @[mk_iff] inductive trans_gen (r : α → α → Prop) (a : α) : α → Prop | single {b} : r a b → trans_gen b | tail {b c} : trans_gen b → r b c → trans_gen c attribute [refl] refl_gen.refl namespace refl_gen lemma to_refl_trans_gen : ∀ {a b}, refl_gen r a b → refl_trans_gen r a b | a _ refl := by refl | a b (single h) := refl_trans_gen.tail refl_trans_gen.refl h lemma mono {p : α → α → Prop} (hp : ∀ a b, r a b → p a b) : ∀ {a b}, refl_gen r a b → refl_gen p a b | a _ refl_gen.refl := by refl | a b (single h) := single (hp a b h) instance : is_refl α (refl_gen r) := ⟨@refl α r⟩ end refl_gen namespace refl_trans_gen @[trans] lemma trans (hab : refl_trans_gen r a b) (hbc : refl_trans_gen r b c) : refl_trans_gen r a c := begin induction hbc, case refl_trans_gen.refl { assumption }, case refl_trans_gen.tail : c d hbc hcd hac { exact hac.tail hcd } end lemma single (hab : r a b) : refl_trans_gen r a b := refl.tail hab lemma head (hab : r a b) (hbc : refl_trans_gen r b c) : refl_trans_gen r a c := begin induction hbc, case refl_trans_gen.refl { exact refl.tail hab }, case refl_trans_gen.tail : c d hbc hcd hac { exact hac.tail hcd } end lemma symmetric (h : symmetric r) : symmetric (refl_trans_gen r) := begin intros x y h, induction h with z w a b c, { refl }, { apply relation.refl_trans_gen.head (h b) c } end lemma cases_tail : refl_trans_gen r a b → b = a ∨ (∃ c, refl_trans_gen r a c ∧ r c b) := (cases_tail_iff r a b).1 @[elab_as_eliminator] lemma head_induction_on {P : ∀ (a:α), refl_trans_gen r a b → Prop} {a : α} (h : refl_trans_gen r a b) (refl : P b refl) (head : ∀ {a c} (h' : r a c) (h : refl_trans_gen r c b), P c h → P a (h.head h')) : P a h := begin induction h generalizing P, case refl_trans_gen.refl { exact refl }, case refl_trans_gen.tail : b c hab hbc ih { apply ih, show P b _, from head hbc _ refl, show ∀ a a', r a a' → refl_trans_gen r a' b → P a' _ → P a _, from λ a a' hab hbc, head hab _ } end @[elab_as_eliminator] lemma trans_induction_on {P : ∀ {a b : α}, refl_trans_gen r a b → Prop} {a b : α} (h : refl_trans_gen r a b) (ih₁ : ∀ a, @P a a refl) (ih₂ : ∀ {a b} (h : r a b), P (single h)) (ih₃ : ∀ {a b c} (h₁ : refl_trans_gen r a b) (h₂ : refl_trans_gen r b c), P h₁ → P h₂ → P (h₁.trans h₂)) : P h := begin induction h, case refl_trans_gen.refl { exact ih₁ a }, case refl_trans_gen.tail : b c hab hbc ih { exact ih₃ hab (single hbc) ih (ih₂ hbc) } end lemma cases_head (h : refl_trans_gen r a b) : a = b ∨ (∃ c, r a c ∧ refl_trans_gen r c b) := begin induction h using relation.refl_trans_gen.head_induction_on, { left, refl }, { right, existsi _, split; assumption } end lemma cases_head_iff : refl_trans_gen r a b ↔ a = b ∨ (∃ c, r a c ∧ refl_trans_gen r c b) := begin use cases_head, rintro (rfl | ⟨c, hac, hcb⟩), { refl }, { exact head hac hcb } end lemma total_of_right_unique (U : relator.right_unique r) (ab : refl_trans_gen r a b) (ac : refl_trans_gen r a c) : refl_trans_gen r b c ∨ refl_trans_gen r c b := begin induction ab with b d ab bd IH, { exact or.inl ac }, { rcases IH with IH | IH, { rcases cases_head IH with rfl | ⟨e, be, ec⟩, { exact or.inr (single bd) }, { cases U bd be, exact or.inl ec } }, { exact or.inr (IH.tail bd) } } end end refl_trans_gen namespace trans_gen lemma to_refl {a b} (h : trans_gen r a b) : refl_trans_gen r a b := begin induction h with b h b c _ bc ab, exact refl_trans_gen.single h, exact refl_trans_gen.tail ab bc end @[trans] lemma trans_left (hab : trans_gen r a b) (hbc : refl_trans_gen r b c) : trans_gen r a c := begin induction hbc, case refl_trans_gen.refl : { assumption }, case refl_trans_gen.tail : c d hbc hcd hac { exact hac.tail hcd } end @[trans] lemma trans (hab : trans_gen r a b) (hbc : trans_gen r b c) : trans_gen r a c := trans_left hab hbc.to_refl lemma head' (hab : r a b) (hbc : refl_trans_gen r b c) : trans_gen r a c := trans_left (single hab) hbc lemma tail' (hab : refl_trans_gen r a b) (hbc : r b c) : trans_gen r a c := begin induction hab generalizing c, case refl_trans_gen.refl : c hac { exact single hac }, case refl_trans_gen.tail : d b hab hdb IH { exact tail (IH hdb) hbc } end lemma head (hab : r a b) (hbc : trans_gen r b c) : trans_gen r a c := head' hab hbc.to_refl @[elab_as_eliminator] lemma head_induction_on {P : ∀ (a:α), trans_gen r a b → Prop} {a : α} (h : trans_gen r a b) (base : ∀ {a} (h : r a b), P a (single h)) (ih : ∀ {a c} (h' : r a c) (h : trans_gen r c b), P c h → P a (h.head h')) : P a h := begin induction h generalizing P, case single : a h { exact base h }, case tail : b c hab hbc h_ih { apply h_ih, show ∀ a, r a b → P a _, from λ a h, ih h (single hbc) (base hbc), show ∀ a a', r a a' → trans_gen r a' b → P a' _ → P a _, from λ a a' hab hbc, ih hab _ } end @[elab_as_eliminator] lemma trans_induction_on {P : ∀ {a b : α}, trans_gen r a b → Prop} {a b : α} (h : trans_gen r a b) (base : ∀ {a b} (h : r a b), P (single h)) (ih : ∀ {a b c} (h₁ : trans_gen r a b) (h₂ : trans_gen r b c), P h₁ → P h₂ → P (h₁.trans h₂)) : P h := begin induction h, case single : a h { exact base h }, case tail : b c hab hbc h_ih { exact ih hab (single hbc) h_ih (base hbc) } end @[trans] lemma trans_right (hab : refl_trans_gen r a b) (hbc : trans_gen r b c) : trans_gen r a c := begin induction hbc, case trans_gen.single : c hbc { exact tail' hab hbc }, case trans_gen.tail : c d hbc hcd hac { exact hac.tail hcd } end lemma tail'_iff : trans_gen r a c ↔ ∃ b, refl_trans_gen r a b ∧ r b c := begin refine ⟨λ h, _, λ ⟨b, hab, hbc⟩, tail' hab hbc⟩, cases h with _ hac b _ hab hbc, { exact ⟨_, by refl, hac⟩ }, { exact ⟨_, hab.to_refl, hbc⟩ } end lemma head'_iff : trans_gen r a c ↔ ∃ b, r a b ∧ refl_trans_gen r b c := begin refine ⟨λ h, _, λ ⟨b, hab, hbc⟩, head' hab hbc⟩, induction h, case trans_gen.single : c hac { exact ⟨_, hac, by refl⟩ }, case trans_gen.tail : b c hab hbc IH { rcases IH with ⟨d, had, hdb⟩, exact ⟨_, had, hdb.tail hbc⟩ } end end trans_gen lemma _root_.well_founded.trans_gen {α} {r : α → α → Prop} (h : well_founded r) : well_founded (trans_gen r) := ⟨λ a, h.induction a (λ x H, acc.intro x (λ y hy, begin cases hy with _ hyx z _ hyz hzx, { exact H y hyx }, { exact acc.inv (H z hzx) hyz } end))⟩ section trans_gen lemma trans_gen_eq_self (trans : transitive r) : trans_gen r = r := funext $ λ a, funext $ λ b, propext $ ⟨λ h, begin induction h, case trans_gen.single : c hc { exact hc }, case trans_gen.tail : c d hac hcd hac { exact trans hac hcd } end, trans_gen.single⟩ lemma transitive_trans_gen : transitive (trans_gen r) := λ a b c, trans_gen.trans instance : is_trans α (trans_gen r) := ⟨@trans_gen.trans α r⟩ lemma trans_gen_idem : trans_gen (trans_gen r) = trans_gen r := trans_gen_eq_self transitive_trans_gen lemma trans_gen.lift {p : β → β → Prop} {a b : α} (f : α → β) (h : ∀ a b, r a b → p (f a) (f b)) (hab : trans_gen r a b) : trans_gen p (f a) (f b) := begin induction hab, case trans_gen.single : c hac { exact trans_gen.single (h a c hac) }, case trans_gen.tail : c d hac hcd hac { exact trans_gen.tail hac (h c d hcd) } end lemma trans_gen.lift' {p : β → β → Prop} {a b : α} (f : α → β) (h : ∀ a b, r a b → trans_gen p (f a) (f b)) (hab : trans_gen r a b) : trans_gen p (f a) (f b) := by simpa [trans_gen_idem] using hab.lift f h lemma trans_gen.closed {p : α → α → Prop} : (∀ a b, r a b → trans_gen p a b) → trans_gen r a b → trans_gen p a b := trans_gen.lift' id lemma trans_gen.mono {p : α → α → Prop} : (∀ a b, r a b → p a b) → trans_gen r a b → trans_gen p a b := trans_gen.lift id lemma trans_gen.swap (h : trans_gen r b a) : trans_gen (swap r) a b := by { induction h with b h b c hab hbc ih, { exact trans_gen.single h }, exact ih.head hbc } lemma trans_gen_swap : trans_gen (swap r) a b ↔ trans_gen r b a := ⟨trans_gen.swap, trans_gen.swap⟩ end trans_gen section refl_trans_gen open refl_trans_gen lemma refl_trans_gen_iff_eq (h : ∀ b, ¬ r a b) : refl_trans_gen r a b ↔ b = a := by rw [cases_head_iff]; simp [h, eq_comm] lemma refl_trans_gen_iff_eq_or_trans_gen : refl_trans_gen r a b ↔ b = a ∨ trans_gen r a b := begin refine ⟨λ h, _, λ h, _⟩, { cases h with c _ hac hcb, { exact or.inl rfl }, { exact or.inr (trans_gen.tail' hac hcb) } }, { rcases h with rfl | h, {refl}, {exact h.to_refl} } end lemma refl_trans_gen.lift {p : β → β → Prop} {a b : α} (f : α → β) (h : ∀ a b, r a b → p (f a) (f b)) (hab : refl_trans_gen r a b) : refl_trans_gen p (f a) (f b) := refl_trans_gen.trans_induction_on hab (λ a, refl) (λ a b, refl_trans_gen.single ∘ h _ _) (λ a b c _ _, trans) lemma refl_trans_gen.mono {p : α → α → Prop} : (∀ a b, r a b → p a b) → refl_trans_gen r a b → refl_trans_gen p a b := refl_trans_gen.lift id lemma refl_trans_gen_eq_self (refl : reflexive r) (trans : transitive r) : refl_trans_gen r = r := funext $ λ a, funext $ λ b, propext $ ⟨λ h, begin induction h with b c h₁ h₂ IH, {apply refl}, exact trans IH h₂, end, single⟩ lemma reflexive_refl_trans_gen : reflexive (refl_trans_gen r) := λ a, refl lemma transitive_refl_trans_gen : transitive (refl_trans_gen r) := λ a b c, trans instance : is_refl α (refl_trans_gen r) := ⟨@refl_trans_gen.refl α r⟩ instance : is_trans α (refl_trans_gen r) := ⟨@refl_trans_gen.trans α r⟩ lemma refl_trans_gen_idem : refl_trans_gen (refl_trans_gen r) = refl_trans_gen r := refl_trans_gen_eq_self reflexive_refl_trans_gen transitive_refl_trans_gen lemma refl_trans_gen.lift' {p : β → β → Prop} {a b : α} (f : α → β) (h : ∀ a b, r a b → refl_trans_gen p (f a) (f b)) (hab : refl_trans_gen r a b) : refl_trans_gen p (f a) (f b) := by simpa [refl_trans_gen_idem] using hab.lift f h lemma refl_trans_gen_closed {p : α → α → Prop} : (∀ a b, r a b → refl_trans_gen p a b) → refl_trans_gen r a b → refl_trans_gen p a b := refl_trans_gen.lift' id lemma refl_trans_gen.swap (h : refl_trans_gen r b a) : refl_trans_gen (swap r) a b := by { induction h with b c hab hbc ih, { refl }, exact ih.head hbc } lemma refl_trans_gen_swap : refl_trans_gen (swap r) a b ↔ refl_trans_gen r b a := ⟨refl_trans_gen.swap, refl_trans_gen.swap⟩ end refl_trans_gen /-- The join of a relation on a single type is a new relation for which pairs of terms are related if there is a third term they are both related to. For example, if `r` is a relation representing rewrites in a term rewriting system, then *confluence* is the property that if `a` rewrites to both `b` and `c`, then `join r` relates `b` and `c` (see `relation.church_rosser`). -/ def join (r : α → α → Prop) : α → α → Prop := λ a b, ∃ c, r a c ∧ r b c section join open refl_trans_gen refl_gen /-- A sufficient condition for the Church-Rosser property. -/ lemma church_rosser (h : ∀ a b c, r a b → r a c → ∃ d, refl_gen r b d ∧ refl_trans_gen r c d) (hab : refl_trans_gen r a b) (hac : refl_trans_gen r a c) : join (refl_trans_gen r) b c := begin induction hab, case refl_trans_gen.refl { exact ⟨c, hac, refl⟩ }, case refl_trans_gen.tail : d e had hde ih { clear hac had a, rcases ih with ⟨b, hdb, hcb⟩, have : ∃ a, refl_trans_gen r e a ∧ refl_gen r b a, { clear hcb, induction hdb, case refl_trans_gen.refl { exact ⟨e, refl, refl_gen.single hde⟩ }, case refl_trans_gen.tail : f b hdf hfb ih { rcases ih with ⟨a, hea, hfa⟩, cases hfa with _ hfa, { exact ⟨b, hea.tail hfb, refl_gen.refl⟩ }, { rcases h _ _ _ hfb hfa with ⟨c, hbc, hac⟩, exact ⟨c, hea.trans hac, hbc⟩ } } }, rcases this with ⟨a, hea, hba⟩, cases hba with _ hba, { exact ⟨b, hea, hcb⟩ }, { exact ⟨a, hea, hcb.tail hba⟩ } } end lemma join_of_single (h : reflexive r) (hab : r a b) : join r a b := ⟨b, hab, h b⟩ lemma symmetric_join : symmetric (join r) := λ a b ⟨c, hac, hcb⟩, ⟨c, hcb, hac⟩ lemma reflexive_join (h : reflexive r) : reflexive (join r) := λ a, ⟨a, h a, h a⟩ lemma transitive_join (ht : transitive r) (h : ∀ a b c, r a b → r a c → join r b c) : transitive (join r) := λ a b c ⟨x, hax, hbx⟩ ⟨y, hby, hcy⟩, let ⟨z, hxz, hyz⟩ := h b x y hbx hby in ⟨z, ht hax hxz, ht hcy hyz⟩ lemma equivalence_join (hr : reflexive r) (ht : transitive r) (h : ∀ a b c, r a b → r a c → join r b c) : equivalence (join r) := ⟨reflexive_join hr, symmetric_join, transitive_join ht h⟩ lemma equivalence_join_refl_trans_gen (h : ∀ a b c, r a b → r a c → ∃ d, refl_gen r b d ∧ refl_trans_gen r c d) : equivalence (join (refl_trans_gen r)) := equivalence_join reflexive_refl_trans_gen transitive_refl_trans_gen (λ a b c, church_rosser h) lemma join_of_equivalence {r' : α → α → Prop} (hr : equivalence r) (h : ∀ a b, r' a b → r a b) : join r' a b → r a b | ⟨c, hac, hbc⟩ := hr.2.2 (h _ _ hac) (hr.2.1 $ h _ _ hbc) lemma refl_trans_gen_of_transitive_reflexive {r' : α → α → Prop} (hr : reflexive r) (ht : transitive r) (h : ∀ a b, r' a b → r a b) (h' : refl_trans_gen r' a b) : r a b := begin induction h' with b c hab hbc ih, { exact hr _ }, { exact ht ih (h _ _ hbc) } end lemma refl_trans_gen_of_equivalence {r' : α → α → Prop} (hr : equivalence r) : (∀ a b, r' a b → r a b) → refl_trans_gen r' a b → r a b := refl_trans_gen_of_transitive_reflexive hr.1 hr.2.2 end join end relation section eqv_gen variables {r : α → α → Prop} {a b : α} lemma equivalence.eqv_gen_iff (h : equivalence r) : eqv_gen r a b ↔ r a b := iff.intro begin intro h, induction h, case eqv_gen.rel { assumption }, case eqv_gen.refl { exact h.1 _ }, case eqv_gen.symm { apply h.2.1, assumption }, case eqv_gen.trans : a b c _ _ hab hbc { exact h.2.2 hab hbc } end (eqv_gen.rel a b) lemma equivalence.eqv_gen_eq (h : equivalence r) : eqv_gen r = r := funext $ λ _, funext $ λ _, propext $ h.eqv_gen_iff lemma eqv_gen.mono {r p : α → α → Prop} (hrp : ∀ a b, r a b → p a b) (h : eqv_gen r a b) : eqv_gen p a b := begin induction h, case eqv_gen.rel : a b h { exact eqv_gen.rel _ _ (hrp _ _ h) }, case eqv_gen.refl : { exact eqv_gen.refl _ }, case eqv_gen.symm : a b h ih { exact eqv_gen.symm _ _ ih }, case eqv_gen.trans : a b c ih1 ih2 hab hbc { exact eqv_gen.trans _ _ _ hab hbc } end end eqv_gen
9deced013928c64e5f05e55d9721e80d4ca473bf
ae1e94c332e17c7dc7051ce976d5a9eebe7ab8a5
/tests/lean/run/explicitMotive.lean
0ac84c2e099ad8b8b411fb01806eadf0fe957728
[ "Apache-2.0" ]
permissive
dupuisf/lean4
d082d13b01243e1de29ae680eefb476961221eef
6a39c65bd28eb0e28c3870188f348c8914502718
refs/heads/master
1,676,948,755,391
1,610,665,114,000
1,610,665,114,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
330
lean
#lang lean4 def f1 (ex : Empty) {α : Type} : α := Empty.rec (motive := fun _ => α) ex def f2 (a b : Nat) (h₁ : b = a) (h₂ : a + b = b) : a + a = b := Eq.rec (motive := fun x _ => a + x = b) h₂ h₁ def f3 (a b : Nat) (h₁ : b = a) (h₂ : a + b = b) : a + a = b := Eq.recOn (motive := fun x _ => a + x = b) h₁ h₂
7e7289af570e0747b377a72adda2c4e3c61831ec
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/analysis/normed/group/SemiNormedGroup/completion.lean
5b16a708e9841435bf8aff8068c61cb4b3137519
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,545
lean
/- Copyright (c) 2021 Riccardo Brasca. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Riccardo Brasca, Johan Commelin -/ import analysis.normed.group.SemiNormedGroup import category_theory.preadditive.additive_functor import analysis.normed.group.hom_completion /-! # Completions of normed groups This file contains an API for completions of seminormed groups (basic facts about objects and morphisms). ## Main definitions - `SemiNormedGroup.Completion : SemiNormedGroup ⥤ SemiNormedGroup` : the completion of a seminormed group (defined as a functor on `SemiNormedGroup` to itself). - `SemiNormedGroup.Completion.lift (f : V ⟶ W) : (Completion.obj V ⟶ W)` : a normed group hom from `V` to complete `W` extends ("lifts") to a seminormed group hom from the completion of `V` to `W`. ## Projects 1. Construct the category of complete seminormed groups, say `CompleteSemiNormedGroup` and promote the `Completion` functor below to a functor landing in this category. 2. Prove that the functor `Completion : SemiNormedGroup ⥤ CompleteSemiNormedGroup` is left adjoint to the forgetful functor. -/ noncomputable theory universe u open uniform_space opposite category_theory normed_group_hom namespace SemiNormedGroup /-- The completion of a seminormed group, as an endofunctor on `SemiNormedGroup`. -/ @[simps] def Completion : SemiNormedGroup.{u} ⥤ SemiNormedGroup.{u} := { obj := λ V, SemiNormedGroup.of (completion V), map := λ V W f, f.completion, map_id' := λ V, completion_id, map_comp' := λ U V W f g, (completion_comp f g).symm } instance Completion_complete_space {V : SemiNormedGroup} : complete_space (Completion.obj V) := completion.complete_space _ /-- The canonical morphism from a seminormed group `V` to its completion. -/ @[simps] def Completion.incl {V : SemiNormedGroup} : V ⟶ Completion.obj V := { to_fun := λ v, (v : completion V), map_add' := completion.coe_add, bound' := ⟨1, λ v, by simp⟩ } lemma Completion.norm_incl_eq {V : SemiNormedGroup} {v : V} : ∥Completion.incl v∥ = ∥v∥ := by simp lemma Completion.map_norm_noninc {V W : SemiNormedGroup} {f : V ⟶ W} (hf : f.norm_noninc) : (Completion.map f).norm_noninc := normed_group_hom.norm_noninc.norm_noninc_iff_norm_le_one.2 $ (normed_group_hom.norm_completion f).le.trans $ normed_group_hom.norm_noninc.norm_noninc_iff_norm_le_one.1 hf /-- Given a normed group hom `V ⟶ W`, this defines the associated morphism from the completion of `V` to the completion of `W`. The difference from the definition obtained from the functoriality of completion is in that the map sending a morphism `f` to the associated morphism of completions is itself additive. -/ def Completion.map_hom (V W : SemiNormedGroup.{u}) : (V ⟶ W) →+ (Completion.obj V ⟶ Completion.obj W) := add_monoid_hom.mk' (category_theory.functor.map Completion) $ λ f g, f.completion_add g @[simp] lemma Completion.map_zero (V W : SemiNormedGroup) : Completion.map (0 : V ⟶ W) = 0 := (Completion.map_hom V W).map_zero instance : preadditive SemiNormedGroup.{u} := { hom_group := λ P Q, infer_instance, add_comp' := by { intros, ext, simp only [normed_group_hom.add_apply, category_theory.comp_apply, normed_group_hom.map_add] }, comp_add' := by { intros, ext, simp only [normed_group_hom.add_apply, category_theory.comp_apply, normed_group_hom.map_add] } } instance : functor.additive Completion := { map_zero' := Completion.map_zero, map_add' := λ X Y, (Completion.map_hom _ _).map_add } /-- Given a normed group hom `f : V → W` with `W` complete, this provides a lift of `f` to the completion of `V`. The lemmas `lift_unique` and `lift_comp_incl` provide the api for the universal property of the completion. -/ def Completion.lift {V W : SemiNormedGroup} [complete_space W] [separated_space W] (f : V ⟶ W) : Completion.obj V ⟶ W := { to_fun := f.extension, map_add' := f.extension.to_add_monoid_hom.map_add', bound' := f.extension.bound' } lemma Completion.lift_comp_incl {V W : SemiNormedGroup} [complete_space W] [separated_space W] (f : V ⟶ W) : Completion.incl ≫ (Completion.lift f) = f := by { ext, apply normed_group_hom.extension_coe } lemma Completion.lift_unique {V W : SemiNormedGroup} [complete_space W] [separated_space W] (f : V ⟶ W) (g : Completion.obj V ⟶ W) : Completion.incl ≫ g = f → g = Completion.lift f := λ h, (normed_group_hom.extension_unique _ (λ v, ((ext_iff.1 h) v).symm)).symm end SemiNormedGroup
c2d23debdaf31bc3aed9684ca8cf246726608c36
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/linear_algebra/prod.lean
68a944de4c467af787808db61ecdfea1a36cbe93
[ "Apache-2.0" ]
permissive
ilitzroth/mathlib
ea647e67f1fdfd19a0f7bdc5504e8acec6180011
5254ef14e3465f6504306132fe3ba9cec9ffff16
refs/heads/master
1,680,086,661,182
1,617,715,647,000
1,617,715,647,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
15,138
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Kevin Buzzard, Yury Kudryashov, Eric Wieser -/ import linear_algebra.basic /-! ### Products of semimodules This file defines constructors for linear maps whose domains or codomains are products. It contains theorems relating these to each other, as well as to `submodule.prod`, `submodule.map`, `submodule.comap`, `linear_map.range`, and `linear_map.ker`. ## Main definitions - products in the domain: - `linear_map.fst` - `linear_map.snd` - `linear_map.coprod` - `linear_map.prod_ext` - products in the codomain: - `linear_map.inl` - `linear_map.inr` - `linear_map.prod` - products in both domain and codomain: - `linear_map.prod_map` - `linear_equiv.prod_map` - `linear_equiv.skew_prod` -/ universes u v w x y z u' v' w' y' variables {R : Type u} {K : Type u'} {M : Type v} {V : Type v'} {M₂ : Type w} {V₂ : Type w'} variables {M₃ : Type y} {V₃ : Type y'} {M₄ : Type z} {ι : Type x} section prod namespace linear_map variables (S : Type*) [semiring R] [semiring S] variables [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M₄] variables [semimodule R M] [semimodule R M₂] [semimodule R M₃] [semimodule R M₄] variables (f : M →ₗ[R] M₂) section variables (R M M₂) /-- The first projection of a product is a linear map. -/ def fst : M × M₂ →ₗ[R] M := ⟨prod.fst, λ x y, rfl, λ x y, rfl⟩ /-- The second projection of a product is a linear map. -/ def snd : M × M₂ →ₗ[R] M₂ := ⟨prod.snd, λ x y, rfl, λ x y, rfl⟩ end @[simp] theorem fst_apply (x : M × M₂) : fst R M M₂ x = x.1 := rfl @[simp] theorem snd_apply (x : M × M₂) : snd R M M₂ x = x.2 := rfl /-- The prod of two linear maps is a linear map. -/ @[simps] def prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : (M →ₗ[R] M₂ × M₃) := { to_fun := λ x, (f x, g x), map_add' := λ x y, by simp only [prod.mk_add_mk, map_add], map_smul' := λ c x, by simp only [prod.smul_mk, map_smul] } @[simp] theorem fst_prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : (fst R M₂ M₃).comp (prod f g) = f := by ext; refl @[simp] theorem snd_prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : (snd R M₂ M₃).comp (prod f g) = g := by ext; refl @[simp] theorem pair_fst_snd : prod (fst R M M₂) (snd R M M₂) = linear_map.id := by ext; refl /-- Taking the product of two maps with the same domain is equivalent to taking the product of their codomains. See note [bundled maps over different rings] for why separate `R` and `S` semirings are used. -/ @[simps] def prod_equiv [semimodule S M₂] [semimodule S M₃] [smul_comm_class R S M₂] [smul_comm_class R S M₃] : ((M →ₗ[R] M₂) × (M →ₗ[R] M₃)) ≃ₗ[S] (M →ₗ[R] M₂ × M₃) := { to_fun := λ f, f.1.prod f.2, inv_fun := λ f, ((fst _ _ _).comp f, (snd _ _ _).comp f), left_inv := λ f, by ext; refl, right_inv := λ f, by ext; refl, map_add' := λ a b, rfl, map_smul' := λ r a, rfl } section variables (R M M₂) /-- The left injection into a product is a linear map. -/ def inl : M →ₗ[R] M × M₂ := prod linear_map.id 0 /-- The right injection into a product is a linear map. -/ def inr : M₂ →ₗ[R] M × M₂ := prod 0 linear_map.id end @[simp] theorem inl_apply (x : M) : inl R M M₂ x = (x, 0) := rfl @[simp] theorem inr_apply (x : M₂) : inr R M M₂ x = (0, x) := rfl theorem inl_eq_prod : inl R M M₂ = prod linear_map.id 0 := rfl theorem inr_eq_prod : inr R M M₂ = prod 0 linear_map.id := rfl theorem inl_injective : function.injective (inl R M M₂) := λ _, by simp theorem inr_injective : function.injective (inr R M M₂) := λ _, by simp /-- The coprod function `λ x : M × M₂, f.1 x.1 + f.2 x.2` is a linear map. -/ def coprod (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) : M × M₂ →ₗ[R] M₃ := f.comp (fst _ _ _) + g.comp (snd _ _ _) @[simp] theorem coprod_apply (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) (x : M × M₂) : coprod f g x = f x.1 + g x.2 := rfl @[simp] theorem coprod_inl (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) : (coprod f g).comp (inl R M M₂) = f := by ext; simp only [map_zero, add_zero, coprod_apply, inl_apply, comp_apply] @[simp] theorem coprod_inr (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) : (coprod f g).comp (inr R M M₂) = g := by ext; simp only [map_zero, coprod_apply, inr_apply, zero_add, comp_apply] @[simp] theorem coprod_inl_inr : coprod (inl R M M₂) (inr R M M₂) = linear_map.id := by ext; simp only [prod.mk_add_mk, add_zero, id_apply, coprod_apply, inl_apply, inr_apply, zero_add] theorem comp_coprod (f : M₃ →ₗ[R] M₄) (g₁ : M →ₗ[R] M₃) (g₂ : M₂ →ₗ[R] M₃) : f.comp (g₁.coprod g₂) = (f.comp g₁).coprod (f.comp g₂) := ext $ λ x, f.map_add (g₁ x.1) (g₂ x.2) theorem fst_eq_coprod : fst R M M₂ = coprod linear_map.id 0 := by ext; simp theorem snd_eq_coprod : snd R M M₂ = coprod 0 linear_map.id := by ext; simp @[simp] theorem coprod_comp_prod (f : M₂ →ₗ[R] M₄) (g : M₃ →ₗ[R] M₄) (f' : M →ₗ[R] M₂) (g' : M →ₗ[R] M₃) : (f.coprod g).comp (f'.prod g') = f.comp f' + g.comp g' := rfl /-- Taking the product of two maps with the same codomain is equivalent to taking the product of their domains. See note [bundled maps over different rings] for why separate `R` and `S` semirings are used. -/ @[simps] def coprod_equiv [semimodule S M₃] [smul_comm_class R S M₃] : ((M →ₗ[R] M₃) × (M₂ →ₗ[R] M₃)) ≃ₗ[S] (M × M₂ →ₗ[R] M₃) := { to_fun := λ f, f.1.coprod f.2, inv_fun := λ f, (f.comp (inl _ _ _), f.comp (inr _ _ _)), left_inv := λ f, by simp only [prod.mk.eta, coprod_inl, coprod_inr], right_inv := λ f, by simp only [←comp_coprod, comp_id, coprod_inl_inr], map_add' := λ a b, by { ext, simp only [prod.snd_add, add_apply, coprod_apply, prod.fst_add], ac_refl }, map_smul' := λ r a, by { ext, simp only [smul_add, smul_apply, prod.smul_snd, prod.smul_fst, coprod_apply] } } theorem prod_ext_iff {f g : M × M₂ →ₗ[R] M₃} : f = g ↔ f.comp (inl _ _ _) = g.comp (inl _ _ _) ∧ f.comp (inr _ _ _) = g.comp (inr _ _ _) := (coprod_equiv ℕ).symm.injective.eq_iff.symm.trans prod.ext_iff /-- Split equality of linear maps from a product into linear maps over each component, to allow `ext` to apply lemmas specific to `M →ₗ M₃` and `M₂ →ₗ M₃`. See note [partially-applied ext lemmas]. -/ @[ext] theorem prod_ext {f g : M × M₂ →ₗ[R] M₃} (hl : f.comp (inl _ _ _) = g.comp (inl _ _ _)) (hr : f.comp (inr _ _ _) = g.comp (inr _ _ _)) : f = g := prod_ext_iff.2 ⟨hl, hr⟩ /-- `prod.map` of two linear maps. -/ def prod_map (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₄) : (M × M₂) →ₗ[R] (M₃ × M₄) := (f.comp (fst R M M₂)).prod (g.comp (snd R M M₂)) @[simp] theorem prod_map_apply (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₄) (x) : f.prod_map g x = (f x.1, g x.2) := rfl end linear_map end prod namespace linear_map open submodule variables [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M₄] [semimodule R M] [semimodule R M₂] [semimodule R M₃] [semimodule R M₄] lemma range_coprod (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) : (f.coprod g).range = f.range ⊔ g.range := submodule.ext $ λ x, by simp [mem_sup] lemma is_compl_range_inl_inr : is_compl (inl R M M₂).range (inr R M M₂).range := begin split, { rintros ⟨_, _⟩ ⟨⟨x, -, hx⟩, ⟨y, -, hy⟩⟩, simp only [prod.ext_iff, inl_apply, inr_apply, mem_bot] at hx hy ⊢, exact ⟨hy.1.symm, hx.2.symm⟩ }, { rintros ⟨x, y⟩ -, simp only [mem_sup, mem_range, exists_prop], refine ⟨(x, 0), ⟨x, rfl⟩, (0, y), ⟨y, rfl⟩, _⟩, simp } end lemma sup_range_inl_inr : (inl R M M₂).range ⊔ (inr R M M₂).range = ⊤ := is_compl_range_inl_inr.sup_eq_top lemma disjoint_inl_inr : disjoint (inl R M M₂).range (inr R M M₂).range := by simp [disjoint_def, @eq_comm M 0, @eq_comm M₂ 0] {contextual := tt}; intros; refl theorem map_coprod_prod (f : M →ₗ[R] M₃) (g : M₂ →ₗ[R] M₃) (p : submodule R M) (q : submodule R M₂) : map (coprod f g) (p.prod q) = map f p ⊔ map g q := begin refine le_antisymm _ (sup_le (map_le_iff_le_comap.2 _) (map_le_iff_le_comap.2 _)), { rw set_like.le_def, rintro _ ⟨x, ⟨h₁, h₂⟩, rfl⟩, exact mem_sup.2 ⟨_, ⟨_, h₁, rfl⟩, _, ⟨_, h₂, rfl⟩, rfl⟩ }, { exact λ x hx, ⟨(x, 0), by simp [hx]⟩ }, { exact λ x hx, ⟨(0, x), by simp [hx]⟩ } end theorem comap_prod_prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) (p : submodule R M₂) (q : submodule R M₃) : comap (prod f g) (p.prod q) = comap f p ⊓ comap g q := submodule.ext $ λ x, iff.rfl theorem prod_eq_inf_comap (p : submodule R M) (q : submodule R M₂) : p.prod q = p.comap (linear_map.fst R M M₂) ⊓ q.comap (linear_map.snd R M M₂) := submodule.ext $ λ x, iff.rfl theorem prod_eq_sup_map (p : submodule R M) (q : submodule R M₂) : p.prod q = p.map (linear_map.inl R M M₂) ⊔ q.map (linear_map.inr R M M₂) := by rw [← map_coprod_prod, coprod_inl_inr, map_id] lemma span_inl_union_inr {s : set M} {t : set M₂} : span R (inl R M M₂ '' s ∪ inr R M M₂ '' t) = (span R s).prod (span R t) := by rw [span_union, prod_eq_sup_map, ← span_image, ← span_image] @[simp] lemma ker_prod (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : ker (prod f g) = ker f ⊓ ker g := by rw [ker, ← prod_bot, comap_prod_prod]; refl lemma range_prod_le (f : M →ₗ[R] M₂) (g : M →ₗ[R] M₃) : range (prod f g) ≤ (range f).prod (range g) := begin simp only [set_like.le_def, prod_apply, mem_range, set_like.mem_coe, mem_prod, exists_imp_distrib], rintro _ x rfl, exact ⟨⟨x, rfl⟩, ⟨x, rfl⟩⟩ end end linear_map namespace submodule open linear_map variables [semiring R] variables [add_comm_monoid M] [add_comm_monoid M₂] variables [semimodule R M] [semimodule R M₂] lemma sup_eq_range (p q : submodule R M) : p ⊔ q = (p.subtype.coprod q.subtype).range := submodule.ext $ λ x, by simp [submodule.mem_sup, set_like.exists] variables (p : submodule R M) (q : submodule R M₂) @[simp] theorem map_inl : p.map (inl R M M₂) = prod p ⊥ := by { ext ⟨x, y⟩, simp only [and.left_comm, eq_comm, mem_map, prod.mk.inj_iff, inl_apply, mem_bot, exists_eq_left', mem_prod] } @[simp] theorem map_inr : q.map (inr R M M₂) = prod ⊥ q := by ext ⟨x, y⟩; simp [and.left_comm, eq_comm] @[simp] theorem comap_fst : p.comap (fst R M M₂) = prod p ⊤ := by ext ⟨x, y⟩; simp @[simp] theorem comap_snd : q.comap (snd R M M₂) = prod ⊤ q := by ext ⟨x, y⟩; simp @[simp] theorem prod_comap_inl : (prod p q).comap (inl R M M₂) = p := by ext; simp @[simp] theorem prod_comap_inr : (prod p q).comap (inr R M M₂) = q := by ext; simp @[simp] theorem prod_map_fst : (prod p q).map (fst R M M₂) = p := by ext x; simp [(⟨0, zero_mem _⟩ : ∃ x, x ∈ q)] @[simp] theorem prod_map_snd : (prod p q).map (snd R M M₂) = q := by ext x; simp [(⟨0, zero_mem _⟩ : ∃ x, x ∈ p)] @[simp] theorem ker_inl : (inl R M M₂).ker = ⊥ := by rw [ker, ← prod_bot, prod_comap_inl] @[simp] theorem ker_inr : (inr R M M₂).ker = ⊥ := by rw [ker, ← prod_bot, prod_comap_inr] @[simp] theorem range_fst : (fst R M M₂).range = ⊤ := by rw [range, ← prod_top, prod_map_fst] @[simp] theorem range_snd : (snd R M M₂).range = ⊤ := by rw [range, ← prod_top, prod_map_snd] end submodule namespace linear_equiv section variables [semiring R] variables [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_monoid M₄] variables {semimodule_M : semimodule R M} {semimodule_M₂ : semimodule R M₂} variables {semimodule_M₃ : semimodule R M₃} {semimodule_M₄ : semimodule R M₄} variables (e₁ : M ≃ₗ[R] M₂) (e₂ : M₃ ≃ₗ[R] M₄) /-- Product of linear equivalences; the maps come from `equiv.prod_congr`. -/ protected def prod : (M × M₃) ≃ₗ[R] (M₂ × M₄) := { map_add' := λ x y, prod.ext (e₁.map_add _ _) (e₂.map_add _ _), map_smul' := λ c x, prod.ext (e₁.map_smul c _) (e₂.map_smul c _), .. equiv.prod_congr e₁.to_equiv e₂.to_equiv } lemma prod_symm : (e₁.prod e₂).symm = e₁.symm.prod e₂.symm := rfl @[simp] lemma prod_apply (p) : e₁.prod e₂ p = (e₁ p.1, e₂ p.2) := rfl @[simp, norm_cast] lemma coe_prod : (e₁.prod e₂ : (M × M₃) →ₗ[R] (M₂ × M₄)) = (e₁ : M →ₗ[R] M₂).prod_map (e₂ : M₃ →ₗ[R] M₄) := rfl end section variables [semiring R] variables [add_comm_monoid M] [add_comm_monoid M₂] [add_comm_monoid M₃] [add_comm_group M₄] variables {semimodule_M : semimodule R M} {semimodule_M₂ : semimodule R M₂} variables {semimodule_M₃ : semimodule R M₃} {semimodule_M₄ : semimodule R M₄} variables (e₁ : M ≃ₗ[R] M₂) (e₂ : M₃ ≃ₗ[R] M₄) /-- Equivalence given by a block lower diagonal matrix. `e₁` and `e₂` are diagonal square blocks, and `f` is a rectangular block below the diagonal. -/ protected def skew_prod (f : M →ₗ[R] M₄) : (M × M₃) ≃ₗ[R] M₂ × M₄ := { inv_fun := λ p : M₂ × M₄, (e₁.symm p.1, e₂.symm (p.2 - f (e₁.symm p.1))), left_inv := λ p, by simp, right_inv := λ p, by simp, .. ((e₁ : M →ₗ[R] M₂).comp (linear_map.fst R M M₃)).prod ((e₂ : M₃ →ₗ[R] M₄).comp (linear_map.snd R M M₃) + f.comp (linear_map.fst R M M₃)) } @[simp] lemma skew_prod_apply (f : M →ₗ[R] M₄) (x) : e₁.skew_prod e₂ f x = (e₁ x.1, e₂ x.2 + f x.1) := rfl @[simp] lemma skew_prod_symm_apply (f : M →ₗ[R] M₄) (x) : (e₁.skew_prod e₂ f).symm x = (e₁.symm x.1, e₂.symm (x.2 - f (e₁.symm x.1))) := rfl end end linear_equiv namespace linear_map open submodule variables [ring R] variables [add_comm_group M] [add_comm_group M₂] [add_comm_group M₃] variables [semimodule R M] [semimodule R M₂] [semimodule R M₃] /-- If the union of the kernels `ker f` and `ker g` spans the domain, then the range of `prod f g` is equal to the product of `range f` and `range g`. -/ lemma range_prod_eq {f : M →ₗ[R] M₂} {g : M →ₗ[R] M₃} (h : ker f ⊔ ker g = ⊤) : range (prod f g) = (range f).prod (range g) := begin refine le_antisymm (f.range_prod_le g) _, simp only [set_like.le_def, prod_apply, mem_range, set_like.mem_coe, mem_prod, exists_imp_distrib, and_imp, prod.forall], rintros _ _ x rfl y rfl, simp only [prod.mk.inj_iff, ← sub_mem_ker_iff], have : y - x ∈ ker f ⊔ ker g, { simp only [h, mem_top] }, rcases mem_sup.1 this with ⟨x', hx', y', hy', H⟩, refine ⟨x' + x, _, _⟩, { rwa add_sub_cancel }, { rwa [← eq_sub_iff_add_eq.1 H, add_sub_add_right_eq_sub, ← neg_mem_iff, neg_sub, add_sub_cancel'] } end end linear_map
25695049dc17d969da202245c8d3da584dfed2e5
c777c32c8e484e195053731103c5e52af26a25d1
/src/group_theory/subsemigroup/center.lean
0ab9e2901c059519201e29a812431eaa3e3c9a31
[ "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
5,163
lean
/- Copyright (c) 2021 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser, Jireh Loreaux -/ import algebra.ring.defs import group_theory.subsemigroup.operations /-! # Centers of magmas and semigroups > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. ## Main definitions * `set.center`: the center of a magma * `subsemigroup.center`: the center of a semigroup * `set.add_center`: the center of an additive magma * `add_subsemigroup.center`: the center of an additive semigroup We provide `submonoid.center`, `add_submonoid.center`, `subgroup.center`, `add_subgroup.center`, `subsemiring.center`, and `subring.center` in other files. -/ variables {M : Type*} namespace set variables (M) /-- The center of a magma. -/ @[to_additive add_center /-" The center of an additive magma. "-/] def center [has_mul M] : set M := {z | ∀ m, m * z = z * m} @[to_additive mem_add_center] lemma mem_center_iff [has_mul M] {z : M} : z ∈ center M ↔ ∀ g, g * z = z * g := iff.rfl instance decidable_mem_center [has_mul M] [∀ a : M, decidable $ ∀ b : M, b * a = a * b] : decidable_pred (∈ center M) := λ _, decidable_of_iff' _ (mem_center_iff M) @[simp, to_additive zero_mem_add_center] lemma one_mem_center [mul_one_class M] : (1 : M) ∈ set.center M := by simp [mem_center_iff] @[simp] lemma zero_mem_center [mul_zero_class M] : (0 : M) ∈ set.center M := by simp [mem_center_iff] variables {M} @[simp, to_additive add_mem_add_center] lemma mul_mem_center [semigroup M] {a b : M} (ha : a ∈ set.center M) (hb : b ∈ set.center M) : a * b ∈ set.center M := λ g, by rw [mul_assoc, ←hb g, ← mul_assoc, ha g, mul_assoc] @[simp, to_additive neg_mem_add_center] lemma inv_mem_center [group M] {a : M} (ha : a ∈ set.center M) : a⁻¹ ∈ set.center M := λ g, by rw [← inv_inj, mul_inv_rev, inv_inv, ← ha, mul_inv_rev, inv_inv] @[simp] lemma add_mem_center [distrib M] {a b : M} (ha : a ∈ set.center M) (hb : b ∈ set.center M) : a + b ∈ set.center M := λ c, by rw [add_mul, mul_add, ha c, hb c] @[simp] lemma neg_mem_center [ring M] {a : M} (ha : a ∈ set.center M) : -a ∈ set.center M := λ c, by rw [←neg_mul_comm, ha (-c), neg_mul_comm] @[to_additive subset_add_center_add_units] lemma subset_center_units [monoid M] : (coe : Mˣ → M) ⁻¹' center M ⊆ set.center Mˣ := λ a ha b, units.ext $ ha _ lemma center_units_subset [group_with_zero M] : set.center Mˣ ⊆ (coe : Mˣ → M) ⁻¹' center M := λ a ha b, begin obtain rfl | hb := eq_or_ne b 0, { rw [zero_mul, mul_zero], }, { exact units.ext_iff.mp (ha (units.mk0 _ hb)) } end /-- In a group with zero, the center of the units is the preimage of the center. -/ lemma center_units_eq [group_with_zero M] : set.center Mˣ = (coe : Mˣ → M) ⁻¹' center M := subset.antisymm center_units_subset subset_center_units @[simp] lemma inv_mem_center₀ [group_with_zero M] {a : M} (ha : a ∈ set.center M) : a⁻¹ ∈ set.center M := begin obtain rfl | ha0 := eq_or_ne a 0, { rw inv_zero, exact zero_mem_center M }, rcases is_unit.mk0 _ ha0 with ⟨a, rfl⟩, rw ←units.coe_inv, exact center_units_subset (inv_mem_center (subset_center_units ha)), end @[simp, to_additive sub_mem_add_center] lemma div_mem_center [group M] {a b : M} (ha : a ∈ set.center M) (hb : b ∈ set.center M) : a / b ∈ set.center M := begin rw [div_eq_mul_inv], exact mul_mem_center ha (inv_mem_center hb), end @[simp] lemma div_mem_center₀ [group_with_zero M] {a b : M} (ha : a ∈ set.center M) (hb : b ∈ set.center M) : a / b ∈ set.center M := begin rw div_eq_mul_inv, exact mul_mem_center ha (inv_mem_center₀ hb), end variables (M) @[simp, to_additive add_center_eq_univ] lemma center_eq_univ [comm_semigroup M] : center M = set.univ := subset.antisymm (subset_univ _) $ λ x _ y, mul_comm y x end set namespace subsemigroup section variables (M) [semigroup M] /-- The center of a semigroup `M` is the set of elements that commute with everything in `M` -/ @[to_additive "The center of a semigroup `M` is the set of elements that commute with everything in `M`"] def center : subsemigroup M := { carrier := set.center M, mul_mem' := λ a b, set.mul_mem_center } @[to_additive] lemma coe_center : ↑(center M) = set.center M := rfl variables {M} @[to_additive] lemma mem_center_iff {z : M} : z ∈ center M ↔ ∀ g, g * z = z * g := iff.rfl @[to_additive] instance decidable_mem_center (a) [decidable $ ∀ b : M, b * a = a * b] : decidable (a ∈ center M) := decidable_of_iff' _ mem_center_iff /-- The center of a semigroup is commutative. -/ @[to_additive "The center of an additive semigroup is commutative."] instance : comm_semigroup (center M) := { mul_comm := λ a b, subtype.ext $ b.prop _, .. mul_mem_class.to_semigroup (center M) } end section variables (M) [comm_semigroup M] @[to_additive, simp] lemma center_eq_top : center M = ⊤ := set_like.coe_injective (set.center_eq_univ M) end end subsemigroup -- Guard against import creep assert_not_exists finset
0acc242047fc75a7343199925f3d9f737b9510cc
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/category_theory/grothendieck.lean
8320cb4343efba49ca4acf16325edfb09ed72b6b
[ "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
5,009
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.category.Cat import category_theory.elements /-! # The Grothendieck construction Given a functor `F : C ⥤ Cat`, the objects of `grothendieck F` consist of dependent pairs `(b, f)`, where `b : C` and `f : F.obj c`, and a morphism `(b, f) ⟶ (b', f')` is a pair `β : b ⟶ b'` in `C`, and `φ : (F.map β).obj f ⟶ f'` Categories such as `PresheafedSpace` are in fact examples of this construction, and it may be interesting to try to generalize some of the development there. ## Implementation notes Really we should treat `Cat` as a 2-category, and allow `F` to be a 2-functor. There is also a closely related construction starting with `G : Cᵒᵖ ⥤ Cat`, where morphisms consists again of `β : b ⟶ b'` and `φ : f ⟶ (F.map (op β)).obj f'`. ## References See also `category_theory.functor.elements` for the category of elements of functor `F : C ⥤ Type`. * https://stacks.math.columbia.edu/tag/02XV * https://ncatlab.org/nlab/show/Grothendieck+construction -/ universe u namespace category_theory variables {C D : Type*} [category C] [category D] variables (F : C ⥤ Cat) /-- The Grothendieck construction (often written as `∫ F` in mathematics) for a functor `F : C ⥤ Cat` gives a category whose * objects `X` consist of `X.base : C` and `X.fiber : F.obj base` * morphisms `f : X ⟶ Y` consist of `base : X.base ⟶ Y.base` and `f.fiber : (F.map base).obj X.fiber ⟶ Y.fiber` -/ @[nolint has_inhabited_instance] structure grothendieck := (base : C) (fiber : F.obj base) namespace grothendieck variables {F} /-- A morphism in the Grothendieck category `F : C ⥤ Cat` consists of `base : X.base ⟶ Y.base` and `f.fiber : (F.map base).obj X.fiber ⟶ Y.fiber`. -/ structure hom (X Y : grothendieck F) := (base : X.base ⟶ Y.base) (fiber : (F.map base).obj X.fiber ⟶ Y.fiber) @[ext] lemma ext {X Y : grothendieck F} (f g : hom X Y) (w_base : f.base = g.base) (w_fiber : eq_to_hom (by rw w_base) ≫ f.fiber = g.fiber) : f = g := begin cases f; cases g, congr, dsimp at w_base, induction w_base, refl, dsimp at w_base, induction w_base, simpa using w_fiber, end /-- The identity morphism in the Grothendieck category. -/ @[simps] def id (X : grothendieck F) : hom X X := { base := 𝟙 X.base, fiber := eq_to_hom (by erw [category_theory.functor.map_id, functor.id_obj X.fiber]), } instance (X : grothendieck F) : inhabited (hom X X) := ⟨id X⟩ /-- Composition of morphisms in the Grothendieck category. -/ @[simps] def comp {X Y Z : grothendieck F} (f : hom X Y) (g : hom Y Z) : hom X Z := { base := f.base ≫ g.base, fiber := eq_to_hom (by erw [functor.map_comp, functor.comp_obj]) ≫ (F.map g.base).map f.fiber ≫ g.fiber, } instance : category (grothendieck F) := { hom := λ X Y, grothendieck.hom X Y, id := λ X, grothendieck.id X, comp := λ X Y Z f g, grothendieck.comp f g, comp_id' := λ X Y f, begin ext, { dsimp, -- We need to turn `F.map_id` (which is an equation between functors) -- into a natural isomorphism. rw ← nat_iso.naturality_2 (eq_to_iso (F.map_id Y.base)) f.fiber, simp, refl, }, { simp, }, end, id_comp' := λ X Y f, by ext; simp, assoc' := λ W X Y Z f g h, begin ext, swap, { simp, }, { dsimp, rw ← nat_iso.naturality_2 (eq_to_iso (F.map_comp _ _)) f.fiber, simp, refl, }, end, } @[simp] lemma id_fiber' (X : grothendieck F) : hom.fiber (𝟙 X) = eq_to_hom (by erw [category_theory.functor.map_id, functor.id_obj X.fiber]) := id_fiber X lemma congr {X Y : grothendieck F} {f g : X ⟶ Y} (h : f = g) : f.fiber = eq_to_hom (by subst h) ≫ g.fiber := by { subst h, dsimp, simp, } section variables (F) /-- The forgetful functor from `grothendieck F` to the source category. -/ @[simps] def forget : grothendieck F ⥤ C := { obj := λ X, X.1, map := λ X Y f, f.1, } end universe w variables (G : C ⥤ Type w) /-- The Grothendieck construction applied to a functor to `Type` (thought of as a functor to `Cat` by realising a type as a discrete category) is the same as the 'category of elements' construction. -/ def grothendieck_Type_to_Cat : grothendieck (G ⋙ Type_to_Cat) ≌ G.elements := { functor := { obj := λ X, ⟨X.1, X.2⟩, map := λ X Y f, ⟨f.1, f.2.1.1⟩ }, inverse := { obj := λ X, ⟨X.1, X.2⟩, map := λ X Y f, ⟨f.1, ⟨⟨f.2⟩⟩⟩ }, unit_iso := nat_iso.of_components (λ X, by { cases X, exact iso.refl _, }) (by { rintro ⟨⟩ ⟨⟩ ⟨base, ⟨⟨f⟩⟩⟩, dsimp at *, subst f, simp, }), counit_iso := nat_iso.of_components (λ X, by { cases X, exact iso.refl _, }) (by { rintro ⟨⟩ ⟨⟩ ⟨f, e⟩, dsimp at *, subst e, simp }), functor_unit_iso_comp' := by { rintro ⟨⟩, dsimp, simp, refl, } } end grothendieck end category_theory
90dc0cbcf6a201583c1b72e9a8bca7bbd2aac2ab
ec62863c729b7eedee77b86d974f2c529fa79d25
/23/a.lean
4a0b9b104a4fbcef238fdd2b50e1adde5ce79cac
[]
no_license
rwbarton/advent-of-lean-4
2ac9b17ba708f66051e3d8cd694b0249bc433b65
417c7e2718253ba7148c0279fcb251b6fc291477
refs/heads/main
1,675,917,092,057
1,609,864,581,000
1,609,864,581,000
317,700,289
24
0
null
null
null
null
UTF-8
Lean
false
false
670
lean
partial def resolve (bad : List Nat) (x : Nat) : Nat := let x' := if x = 0 then 9 else x if bad.any (· = x') then resolve bad (x'-1) else x' def step : List Nat → List Nat | a :: b₁ :: b₂ :: b₃ :: rest => let tgt := resolve [b₁, b₂, b₃] (a-1) let ⟨pre, post⟩ := rest.span (· ≠ tgt) pre ++ tgt :: b₁ :: b₂ :: b₃ :: post.drop 1 ++ [a] | _ => panic! "too short" def main : IO Unit := do let input ← IO.FS.lines "a.in" let init := input[0].toList.map (String.toNat! ∘ String.singleton) let after := Nat.repeat step 100 init let ⟨pre, post⟩ := after.span (· ≠ 1) IO.print s!"{String.join ((post.drop 1 ++ pre).map toString)}\n"
b364732b08023d26b1f6077cf97fbaa30f2323d9
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/tactic/norm_cast.lean
dd64e045976226756093f220ab5e8cbe5fda23aa
[ "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
25,974
lean
/- Copyright (c) 2019 Paul-Nicolas Madelaine. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Paul-Nicolas Madelaine, Robert Y. Lewis -/ import tactic.converter.interactive import tactic.hint /-! # A tactic for normalizing casts inside expressions This tactic normalizes casts inside expressions. It can be thought of as a call to the simplifier with a specific set of lemmas to move casts upwards in the expression. It has special handling of numerals and a simple heuristic to help moving casts "past" binary operators. Contrary to simp, it should be safe to use as a non-terminating tactic. The algorithm implemented here is described in the paper <https://lean-forward.github.io/norm_cast/norm_cast.pdf>. ## Important definitions * `tactic.interactive.norm_cast` * `tactic.interactive.push_cast` * `tactic.interactive.exact_mod_cast` * `tactic.interactive.apply_mod_cast` * `tactic.interactive.rw_mod_cast` * `tactic.interactive.assumption_mod_cast` -/ setup_tactic_parser namespace tactic /-- Runs `mk_instance` with a time limit. This is a work around to the fact that in some cases mk_instance times out instead of failing, for example: `has_lift_t ℤ ℕ` `mk_instance_fast` is used when we assume the type class search should end instantly. -/ meta def mk_instance_fast (e : expr) (timeout := 1000) : tactic expr := try_for timeout (mk_instance e) end tactic namespace norm_cast open tactic expr declare_trace norm_cast /-- Output a trace message if `trace.norm_cast` is enabled. -/ meta def trace_norm_cast {α} [has_to_tactic_format α] (msg : string) (a : α) : tactic unit := when_tracing `norm_cast $ do a ← pp a, trace ("[norm_cast] " ++ msg ++ a : format) mk_simp_attribute push_cast "The `push_cast` simp attribute uses `norm_cast` lemmas to move casts toward the leaf nodes of the expression." /-- `label` is a type used to classify `norm_cast` lemmas. * elim lemma: LHS has 0 head coes and ≥ 1 internal coe * move lemma: LHS has 1 head coe and 0 internal coes, RHS has 0 head coes and ≥ 1 internal coes * squash lemma: LHS has ≥ 1 head coes and 0 internal coes, RHS has fewer head coes -/ @[derive [decidable_eq, has_reflect, inhabited]] inductive label | elim : label | move : label | squash : label namespace label /-- Convert `label` into `string`. -/ protected def to_string : label → string | elim := "elim" | move := "move" | squash := "squash" instance : has_to_string label := ⟨label.to_string⟩ instance : has_repr label := ⟨label.to_string⟩ meta instance : has_to_format label := ⟨λ l, l.to_string⟩ /-- Convert `string` into `label`. -/ def of_string : string -> option label | "elim" := some elim | "move" := some move | "squash" := some squash | _ := none end label open label /-- Count how many coercions are at the top of the expression. -/ meta def count_head_coes : expr → ℕ | `(coe %%e) := count_head_coes e + 1 | `(coe_sort %%e) := count_head_coes e + 1 | `(coe_fn %%e) := count_head_coes e + 1 | _ := 0 /-- Count how many coercions are inside the expression, including the top ones. -/ meta def count_coes : expr → tactic ℕ | `(coe %%e) := (+1) <$> count_coes e | `(coe_sort %%e) := (+1) <$> count_coes e | `(coe_fn %%e) := (+1) <$> count_coes e | (app `(coe_fn %%e) x) := (+) <$> count_coes x <*> (+1) <$> count_coes e | (expr.lam n bi t e) := do l ← mk_local' n bi t, count_coes $ e.instantiate_var l | e := do as ← e.get_simp_args, list.sum <$> as.mmap count_coes /-- Count how many coercions are inside the expression, excluding the top ones. -/ private meta def count_internal_coes (e : expr) : tactic ℕ := do ncoes ← count_coes e, pure $ ncoes - count_head_coes e /-- Classifies a declaration of type `ty` as a `norm_cast` rule. -/ meta def classify_type (ty : expr) : tactic label := do (_, ty) ← open_pis ty, (lhs, rhs) ← match ty with | `(%%lhs = %%rhs) := pure (lhs, rhs) | `(%%lhs ↔ %%rhs) := pure (lhs, rhs) | _ := fail "norm_cast: lemma must be = or ↔" end, lhs_coes ← count_coes lhs, when (lhs_coes = 0) $ fail "norm_cast: badly shaped lemma, lhs must contain at least one coe", let lhs_head_coes := count_head_coes lhs, lhs_internal_coes ← count_internal_coes lhs, let rhs_head_coes := count_head_coes rhs, rhs_internal_coes ← count_internal_coes rhs, if lhs_head_coes = 0 then return elim else if lhs_head_coes = 1 then do when (rhs_head_coes ≠ 0) $ fail "norm_cast: badly shaped lemma, rhs can't start with coe", if rhs_internal_coes = 0 then return squash else return move else if rhs_head_coes < lhs_head_coes then do return squash else do fail "norm_cast: badly shaped shaped squash lemma, rhs must have fewer head coes than lhs" /-- The cache for `norm_cast` attribute stores three `simp_lemma` objects. -/ meta structure norm_cast_cache := (up : simp_lemmas) (down : simp_lemmas) (squash : simp_lemmas) /-- Empty `norm_cast_cache`. -/ meta def empty_cache : norm_cast_cache := { up := simp_lemmas.mk, down := simp_lemmas.mk, squash := simp_lemmas.mk, } meta instance : inhabited norm_cast_cache := ⟨empty_cache⟩ /-- `add_elim cache e` adds `e` as an `elim` lemma to `cache`. -/ meta def add_elim (cache : norm_cast_cache) (e : expr) : tactic norm_cast_cache := do new_up ← cache.up.add e, return { up := new_up, down := cache.down, squash := cache.squash, } /-- `add_move cache e` adds `e` as a `move` lemma to `cache`. -/ meta def add_move (cache : norm_cast_cache) (e : expr) : tactic norm_cast_cache := do new_up ← cache.up.add e tt, new_down ← cache.down.add e, return { up := new_up, down := new_down, squash := cache.squash, } /-- `add_squash cache e` adds `e` as an `squash` lemma to `cache`. -/ meta def add_squash (cache : norm_cast_cache) (e : expr) : tactic norm_cast_cache := do new_squash ← cache.squash.add e, new_down ← cache.down.add e, return { up := cache.up, down := new_down, squash := new_squash, } /-- The type of the `norm_cast` attribute. The optional label is used to overwrite the classifier. -/ meta def norm_cast_attr_ty : Type := user_attribute norm_cast_cache (option label) /-- Efficient getter for the `@[norm_cast]` attribute parameter that does not call `eval_expr`. See Note [user attribute parameters]. -/ meta def get_label_param (attr : norm_cast_attr_ty) (decl : name) : tactic (option label) := do p ← attr.get_param_untyped decl, match p with | `(none) := pure none | `(some label.elim) := pure label.elim | `(some label.move) := pure label.move | `(some label.squash) := pure label.squash | _ := fail p end /-- `add_lemma cache decl` infers the proper `norm_cast` attribute for `decl` and adds it to `cache`. -/ meta def add_lemma (attr : norm_cast_attr_ty) (cache : norm_cast_cache) (decl : name) : tactic norm_cast_cache := do e ← mk_const decl, param ← get_label_param attr decl, l ← param <|> (infer_type e >>= classify_type), match l with | elim := add_elim cache e | move := add_move cache e | squash := add_squash cache e end -- special lemmas to handle the ≥, > and ≠ operators private lemma ge_from_le {α} [has_le α] : ∀ (x y : α), x ≥ y ↔ y ≤ x := λ _ _, iff.rfl private lemma gt_from_lt {α} [has_lt α] : ∀ (x y : α), x > y ↔ y < x := λ _ _, iff.rfl private lemma ne_from_not_eq {α} : ∀ (x y : α), x ≠ y ↔ ¬(x = y) := λ _ _, iff.rfl /-- `mk_cache names` creates a `norm_cast_cache`. It infers the proper `norm_cast` attributes for names in `names`, and collects the lemmas attributed with specific `norm_cast` attributes. -/ meta def mk_cache (attr : thunk norm_cast_attr_ty) (names : list name) : tactic norm_cast_cache := do -- names has the declarations in reverse order cache ← names.mfoldr (λ name cache, add_lemma (attr ()) cache name) empty_cache, --some special lemmas to handle binary relations let up := cache.up, up ← up.add_simp ``ge_from_le, up ← up.add_simp ``gt_from_lt, up ← up.add_simp ``ne_from_not_eq, let down := cache.down, down ← down.add_simp ``coe_coe, pure { up := up, down := down, squash := cache.squash } /-- The `norm_cast` attribute. -/ @[user_attribute] meta def norm_cast_attr : user_attribute norm_cast_cache (option label) := { name := `norm_cast, descr := "attribute for norm_cast", parser := (do some l ← (label.of_string ∘ to_string) <$> ident, return l) <|> return none, after_set := some (λ decl prio persistent, do param ← get_label_param norm_cast_attr decl, match param with | some l := when (l ≠ elim) $ simp_attr.push_cast.set decl () tt prio | none := do e ← mk_const decl, ty ← infer_type e, l ← classify_type ty, norm_cast_attr.set decl l persistent prio end), before_unset := some $ λ _ _, tactic.skip, cache_cfg := { mk_cache := mk_cache norm_cast_attr, dependencies := [] } } /-- Classify a declaration as a `norm_cast` rule. -/ meta def make_guess (decl : name) : tactic label := do e ← mk_const decl, ty ← infer_type e, classify_type ty /-- Gets the `norm_cast` classification label for a declaration. Applies the override specified on the attribute, if necessary. -/ meta def get_label (decl : name) : tactic label := do param ← get_label_param norm_cast_attr decl, param <|> make_guess decl end norm_cast namespace tactic.interactive open norm_cast /-- `push_cast` rewrites the expression to move casts toward the leaf nodes. For example, `↑(a + b)` will be written to `↑a + ↑b`. Equivalent to `simp only with push_cast`. Can also be used at hypotheses. `push_cast` can also be used at hypotheses and with extra simp rules. ```lean example (a b : ℕ) (h1 : ((a + b : ℕ) : ℤ) = 10) (h2 : ((a + b + 0 : ℕ) : ℤ) = 10) : ((a + b : ℕ) : ℤ) = 10 := begin push_cast, push_cast at h1, push_cast [int.add_zero] at h2, end ``` -/ meta def push_cast (hs : parse tactic.simp_arg_list) (l : parse location) : tactic unit := tactic.interactive.simp none none tt hs [`push_cast] l {discharger := tactic.assumption} end tactic.interactive namespace norm_cast open tactic expr /-- Prove `a = b` using the given simp set. -/ meta def prove_eq_using (s : simp_lemmas) (a b : expr) : tactic expr := do (a', a_a', _) ← simplify s [] a {fail_if_unchanged := ff}, (b', b_b', _) ← simplify s [] b {fail_if_unchanged := ff}, on_exception (trace_norm_cast "failed: " (to_expr ``(%%a' = %%b') >>= pp)) $ is_def_eq a' b' reducible, b'_b ← mk_eq_symm b_b', mk_eq_trans a_a' b'_b /-- Prove `a = b` by simplifying using move and squash lemmas. -/ meta def prove_eq_using_down (a b : expr) : tactic expr := do cache ← norm_cast_attr.get_cache, trace_norm_cast "proving: " (to_expr ``(%%a = %%b) >>= pp), prove_eq_using cache.down a b /-- This is the main heuristic used alongside the elim and move lemmas. The goal is to help casts move past operators by adding intermediate casts. An expression of the shape: op (↑(x : α) : γ) (↑(y : β) : γ) is rewritten to: op (↑(↑(x : α) : β) : γ) (↑(y : β) : γ) when (↑(↑(x : α) : β) : γ) = (↑(x : α) : γ) can be proven with a squash lemma -/ meta def splitting_procedure : expr → tactic (expr × expr) | (app (app op x) y) := (do `(@coe %%α %%δ %%coe1 %%xx) ← return x, `(@coe %%β %%γ %%coe2 %%yy) ← return y, success_if_fail $ is_def_eq α β, is_def_eq δ γ, (do coe3 ← mk_app `has_lift_t [α, β] >>= mk_instance_fast, new_x ← to_expr ``(@coe %%β %%δ %%coe2 (@coe %%α %%β %%coe3 %%xx)), let new_e := app (app op new_x) y, eq_x ← prove_eq_using_down x new_x, pr ← mk_congr_arg op eq_x, pr ← mk_congr_fun pr y, return (new_e, pr) ) <|> (do coe3 ← mk_app `has_lift_t [β, α] >>= mk_instance_fast, new_y ← to_expr ``(@coe %%α %%δ %%coe1 (@coe %%β %%α %%coe3 %%yy)), let new_e := app (app op x) new_y, eq_y ← prove_eq_using_down y new_y, pr ← mk_congr_arg (app op x) eq_y, return (new_e, pr) ) ) <|> (do `(@coe %%α %%β %%coe1 %%xx) ← return x, `(@has_one.one %%β %%h1) ← return y, h2 ← to_expr ``(has_one %%α) >>= mk_instance_fast, new_y ← to_expr ``(@coe %%α %%β %%coe1 (@has_one.one %%α %%h2)), eq_y ← prove_eq_using_down y new_y, let new_e := app (app op x) new_y, pr ← mk_congr_arg (app op x) eq_y, return (new_e, pr) ) <|> (do `(@coe %%α %%β %%coe1 %%xx) ← return x, `(@has_zero.zero %%β %%h1) ← return y, h2 ← to_expr ``(has_zero %%α) >>= mk_instance_fast, new_y ← to_expr ``(@coe %%α %%β %%coe1 (@has_zero.zero %%α %%h2)), eq_y ← prove_eq_using_down y new_y, let new_e := app (app op x) new_y, pr ← mk_congr_arg (app op x) eq_y, return (new_e, pr) ) <|> (do `(@has_one.one %%β %%h1) ← return x, `(@coe %%α %%β %%coe1 %%xx) ← return y, h1 ← to_expr ``(has_one %%α) >>= mk_instance_fast, new_x ← to_expr ``(@coe %%α %%β %%coe1 (@has_one.one %%α %%h1)), eq_x ← prove_eq_using_down x new_x, let new_e := app (app op new_x) y, pr ← mk_congr_arg (lam `x binder_info.default β (app (app op (var 0)) y)) eq_x, return (new_e, pr) ) <|> (do `(@has_zero.zero %%β %%h1) ← return x, `(@coe %%α %%β %%coe1 %%xx) ← return y, h1 ← to_expr ``(has_zero %%α) >>= mk_instance_fast, new_x ← to_expr ``(@coe %%α %%β %%coe1 (@has_zero.zero %%α %%h1)), eq_x ← prove_eq_using_down x new_x, let new_e := app (app op new_x) y, pr ← mk_congr_arg (lam `x binder_info.default β (app (app op (var 0)) y)) eq_x, return (new_e, pr) ) | _ := failed /-- Discharging function used during simplification in the "squash" step. TODO: norm_cast takes a list of expressions to use as lemmas for the discharger TODO: a tactic to print the results the discharger fails to proove -/ private meta def prove : tactic unit := assumption /-- Core rewriting function used in the "squash" step, which moves casts upwards and eliminates them. It tries to rewrite an expression using the elim and move lemmas. On failure, it calls the splitting procedure heuristic. -/ meta def upward_and_elim (s : simp_lemmas) (e : expr) : tactic (expr × expr) := (do r ← mcond (is_prop e) (return `iff) (return `eq), (new_e, pr) ← s.rewrite e prove r, pr ← match r with | `iff := mk_app `propext [pr] | _ := return pr end, return (new_e, pr) ) <|> splitting_procedure e /-! The following auxiliary functions are used to handle numerals. -/ /-- If possible, rewrite `(n : α)` to `((n : ℕ) : α)` where `n` is a numeral and `α ≠ ℕ`. Returns a pair of the new expression and proof that they are equal. -/ meta def numeral_to_coe (e : expr) : tactic (expr × expr) := do α ← infer_type e, success_if_fail $ is_def_eq α `(ℕ), n ← e.to_nat, h1 ← mk_app `has_lift_t [`(ℕ), α] >>= mk_instance_fast, let new_e : expr := reflect n, new_e ← to_expr ``(@coe ℕ %%α %%h1 %%new_e), pr ← prove_eq_using_down e new_e, return (new_e, pr) /-- If possible, rewrite `((n : ℕ) : α)` to `(n : α)` where `n` is a numeral. Returns a pair of the new expression and proof that they are equal. -/ meta def coe_to_numeral (e : expr) : tactic (expr × expr) := do `(@coe ℕ %%α %%h1 %%e') ← return e, n ← e'.to_nat, -- replace e' by normalized numeral is_def_eq (reflect n) e' reducible, let e := e.app_fn (reflect n), new_e ← expr.of_nat α n, pr ← prove_eq_using_down e new_e, return (new_e, pr) /-- A local variant on `simplify_top_down`. -/ private meta def simplify_top_down' {α} (a : α) (pre : α → expr → tactic (α × expr × expr)) (e : expr) (cfg : simp_config := {}) : tactic (α × expr × expr) := ext_simplify_core a cfg simp_lemmas.mk (λ _, failed) (λ a _ _ _ e, do (new_a, new_e, pr) ← pre a e, guard (¬ new_e =ₐ e), return (new_a, new_e, some pr, ff)) (λ _ _ _ _ _, failed) `eq e /-- The core simplification routine of `norm_cast`. -/ meta def derive (e : expr) : tactic (expr × expr) := do cache ← norm_cast_attr.get_cache, e ← instantiate_mvars e, let cfg : simp_config := { zeta := ff, beta := ff, eta := ff, proj := ff, iota := ff, iota_eqn := ff, fail_if_unchanged := ff }, let e0 := e, -- step 1: pre-processing of numerals ((), e1, pr1) ← simplify_top_down' () (λ _ e, prod.mk () <$> numeral_to_coe e) e0 cfg, trace_norm_cast "after numeral_to_coe: " e1, -- step 2: casts are moved upwards and eliminated ((), e2, pr2) ← simplify_bottom_up () (λ _ e, prod.mk () <$> upward_and_elim cache.up e) e1 cfg, trace_norm_cast "after upward_and_elim: " e2, -- step 3: casts are squashed (e3, pr3, _) ← simplify cache.squash [] e2 cfg, trace_norm_cast "after squashing: " e3, -- step 4: post-processing of numerals ((), e4, pr4) ← simplify_top_down' () (λ _ e, prod.mk () <$> coe_to_numeral e) e3 cfg, trace_norm_cast "after coe_to_numeral: " e4, let new_e := e4, guard (¬ new_e =ₐ e), pr ← mk_eq_trans pr1 pr2, pr ← mk_eq_trans pr pr3, pr ← mk_eq_trans pr pr4, return (new_e, pr) /-- A small variant of `push_cast` suited for non-interactive use. `derive_push_cast extra_lems e` returns an expression `e'` and a proof that `e = e'`. -/ meta def derive_push_cast (extra_lems : list simp_arg_type) (e : expr) : tactic (expr × expr) := do (s, _) ← mk_simp_set tt [`push_cast] extra_lems, (e, prf, _) ← simplify (s.erase [`nat.cast_succ]) [] e {fail_if_unchanged := ff} `eq tactic.assumption, return (e, prf) end norm_cast namespace tactic open expr norm_cast /-- `aux_mod_cast e` runs `norm_cast` on `e` and returns the result. If `include_goal` is true, it also normalizes the goal. -/ meta def aux_mod_cast (e : expr) (include_goal : bool := tt) : tactic expr := match e with | local_const _ lc _ _ := do e ← get_local lc, replace_at derive [e] include_goal, get_local lc | e := do t ← infer_type e, e ← assertv `this t e, replace_at derive [e] include_goal, get_local `this end /-- `exact_mod_cast e` runs `norm_cast` on the goal and `e`, and tries to use `e` to close the goal. -/ meta def exact_mod_cast (e : expr) : tactic unit := decorate_error "exact_mod_cast failed:" $ do new_e ← aux_mod_cast e, exact new_e /-- `apply_mod_cast e` runs `norm_cast` on the goal and `e`, and tries to apply `e`. -/ meta def apply_mod_cast (e : expr) : tactic (list (name × expr)) := decorate_error "apply_mod_cast failed:" $ do new_e ← aux_mod_cast e, apply new_e /-- `assumption_mod_cast` runs `norm_cast` on the goal. For each local hypothesis `h`, it also normalizes `h` and tries to use that to close the goal. -/ meta def assumption_mod_cast : tactic unit := decorate_error "assumption_mod_cast failed:" $ do let cfg : simp_config := { fail_if_unchanged := ff, canonize_instances := ff, canonize_proofs := ff, proj := ff }, replace_at derive [] tt, ctx ← local_context, ctx.mfirst (λ h, aux_mod_cast h ff >>= tactic.exact) end tactic namespace tactic.interactive open tactic norm_cast /-- Normalize casts at the given locations by moving them "upwards". As opposed to simp, norm_cast can be used without necessarily closing the goal. -/ meta def norm_cast (loc : parse location) : tactic unit := do ns ← loc.get_locals, tt ← replace_at derive ns loc.include_goal | fail "norm_cast failed to simplify", when loc.include_goal $ try tactic.reflexivity, when loc.include_goal $ try tactic.triv, when (¬ ns.empty) $ try tactic.contradiction /-- Rewrite with the given rules and normalize casts between steps. -/ meta def rw_mod_cast (rs : parse rw_rules) (loc : parse location) : tactic unit := decorate_error "rw_mod_cast failed:" $ do let cfg_norm : simp_config := {}, let cfg_rw : rewrite_cfg := {}, ns ← loc.get_locals, monad.mapm' (λ r : rw_rule, do save_info r.pos, replace_at derive ns loc.include_goal, rw ⟨[r], none⟩ loc {} ) rs.rules, replace_at derive ns loc.include_goal, skip /-- Normalize the goal and the given expression, then close the goal with exact. -/ meta def exact_mod_cast (e : parse texpr) : tactic unit := do e ← i_to_expr e <|> do { ty ← target, e ← i_to_expr_strict ``(%%e : %%ty), pty ← pp ty, ptgt ← pp e, fail ("exact_mod_cast failed, expression type not directly " ++ "inferrable. Try:\n\nexact_mod_cast ...\nshow " ++ to_fmt pty ++ ",\nfrom " ++ ptgt : format) }, tactic.exact_mod_cast e /-- Normalize the goal and the given expression, then apply the expression to the goal. -/ meta def apply_mod_cast (e : parse texpr) : tactic unit := do e ← i_to_expr_for_apply e, concat_tags $ tactic.apply_mod_cast e /-- Normalize the goal and every expression in the local context, then close the goal with assumption. -/ meta def assumption_mod_cast : tactic unit := tactic.assumption_mod_cast end tactic.interactive namespace conv.interactive open conv open norm_cast (derive) /-- the converter version of `norm_cast' -/ meta def norm_cast : conv unit := replace_lhs derive end conv.interactive -- TODO: move this elsewhere? @[norm_cast] lemma ite_cast {α β} [has_lift_t α β] {c : Prop} [decidable c] {a b : α} : ↑(ite c a b) = ite c (↑a : β) (↑b : β) := by by_cases h : c; simp [h] @[norm_cast] lemma dite_cast {α β} [has_lift_t α β] {c : Prop} [decidable c] {a : c → α} {b : ¬ c → α} : ↑(dite c a b) = dite c (λ h, (↑(a h) : β)) (λ h, (↑(b h) : β)) := by by_cases h : c; simp [h] add_hint_tactic "norm_cast at *" /-- The `norm_cast` family of tactics is used to normalize casts inside expressions. It is basically a simp tactic with a specific set of lemmas to move casts upwards in the expression. Therefore it can be used more safely as a non-terminating tactic. It also has special handling of numerals. For instance, given an assumption ```lean a b : ℤ h : ↑a + ↑b < (10 : ℚ) ``` writing `norm_cast at h` will turn `h` into ```lean h : a + b < 10 ``` You can also use `exact_mod_cast`, `apply_mod_cast`, `rw_mod_cast` or `assumption_mod_cast`. Writing `exact_mod_cast h` and `apply_mod_cast h` will normalize the goal and `h` before using `exact h` or `apply h`. Writing `assumption_mod_cast` will normalize the goal and for every expression `h` in the context it will try to normalize `h` and use `exact h`. `rw_mod_cast` acts like the `rw` tactic but it applies `norm_cast` between steps. `push_cast` rewrites the expression to move casts toward the leaf nodes. This uses `norm_cast` lemmas in the forward direction. For example, `↑(a + b)` will be written to `↑a + ↑b`. It is equivalent to `simp only with push_cast`. It can also be used at hypotheses with `push_cast at h` and with extra simp lemmas with `push_cast [int.add_zero]`. ```lean example (a b : ℕ) (h1 : ((a + b : ℕ) : ℤ) = 10) (h2 : ((a + b + 0 : ℕ) : ℤ) = 10) : ((a + b : ℕ) : ℤ) = 10 := begin push_cast, push_cast at h1, push_cast [int.add_zero] at h2, end ``` The implementation and behavior of the `norm_cast` family is described in detail at <https://lean-forward.github.io/norm_cast/norm_cast.pdf>. -/ add_tactic_doc { name := "norm_cast", category := doc_category.tactic, decl_names := [``tactic.interactive.norm_cast, ``tactic.interactive.rw_mod_cast, ``tactic.interactive.apply_mod_cast, ``tactic.interactive.assumption_mod_cast, ``tactic.interactive.exact_mod_cast, ``tactic.interactive.push_cast], tags := ["coercions", "simplification"] } /-- The `norm_cast` attribute should be given to lemmas that describe the behaviour of a coercion in regard to an operator, a relation, or a particular function. It only concerns equality or iff lemmas involving `↑`, `⇑` and `↥`, describing the behavior of the coercion functions. It does not apply to the explicit functions that define the coercions. Examples: ```lean @[norm_cast] theorem coe_nat_inj' {m n : ℕ} : (↑m : ℤ) = ↑n ↔ m = n @[norm_cast] theorem coe_int_denom (n : ℤ) : (n : ℚ).denom = 1 @[norm_cast] theorem cast_id : ∀ n : ℚ, ↑n = n @[norm_cast] theorem coe_nat_add (m n : ℕ) : (↑(m + n) : ℤ) = ↑m + ↑n @[norm_cast] theorem cast_sub [add_group α] [has_one α] {m n} (h : m ≤ n) : ((n - m : ℕ) : α) = n - m @[norm_cast] theorem coe_nat_bit0 (n : ℕ) : (↑(bit0 n) : ℤ) = bit0 ↑n @[norm_cast] theorem cast_coe_nat (n : ℕ) : ((n : ℤ) : α) = n @[norm_cast] theorem cast_one : ((1 : ℚ) : α) = 1 ``` Lemmas tagged with `@[norm_cast]` are classified into three categories: `move`, `elim`, and `squash`. They are classified roughly as follows: * elim lemma: LHS has 0 head coes and ≥ 1 internal coe * move lemma: LHS has 1 head coe and 0 internal coes, RHS has 0 head coes and ≥ 1 internal coes * squash lemma: LHS has ≥ 1 head coes and 0 internal coes, RHS has fewer head coes `norm_cast` uses `move` and `elim` lemmas to factor coercions toward the root of an expression and to cancel them from both sides of an equation or relation. It uses `squash` lemmas to clean up the result. Occasionally you may want to override the automatic classification. You can do this by giving an optional `elim`, `move`, or `squash` parameter to the attribute. ```lean @[simp, norm_cast elim] lemma nat_cast_re (n : ℕ) : (n : ℂ).re = n := by rw [← of_real_nat_cast, of_real_re] ``` Don't do this unless you understand what you are doing. A full description of the tactic, and the use of each lemma category, can be found at <https://lean-forward.github.io/norm_cast/norm_cast.pdf>. -/ add_tactic_doc { name := "norm_cast attributes", category := doc_category.attr, decl_names := [``norm_cast.norm_cast_attr], tags := ["coercions", "simplification"] }
e329593c7c90e21a00b92dc6a011b6597982f493
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/data/nat/cast.lean
a303b44fccd17b933ed5086c8080a557305c73fc
[ "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
11,371
lean
/- Copyright (c) 2014 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro Natural homomorphism from the natural numbers into a monoid with one. -/ import algebra.ordered_field import data.nat.basic namespace nat variables {α : Type*} section variables [has_zero α] [has_one α] [has_add α] /-- Canonical homomorphism from `ℕ` to a type `α` with `0`, `1` and `+`. -/ protected def cast : ℕ → α | 0 := 0 | (n+1) := cast n + 1 /-- Computationally friendlier cast than `nat.cast`, using binary representation. -/ protected def bin_cast (n : ℕ) : α := @nat.binary_rec (λ _, α) 0 (λ odd k a, cond odd (a + a + 1) (a + a)) n /-- Coercions such as `nat.cast_coe` that go from a concrete structure such as `ℕ` to an arbitrary ring `α` should be set up as follows: ```lean @[priority 900] instance : has_coe_t ℕ α := ⟨...⟩ ``` It needs to be `has_coe_t` instead of `has_coe` because otherwise type-class inference would loop when constructing the transitive coercion `ℕ → ℕ → ℕ → ...`. The reduced priority is necessary so that it doesn't conflict with instances such as `has_coe_t α (option α)`. For this to work, we reduce the priority of the `coe_base` and `coe_trans` instances because we want the instances for `has_coe_t` to be tried in the following order: 1. `has_coe_t` instances declared in mathlib (such as `has_coe_t α (with_top α)`, etc.) 2. `coe_base`, which contains instances such as `has_coe (fin n) n` 3. `nat.cast_coe : has_coe_t ℕ α` etc. 4. `coe_trans` If `coe_trans` is tried first, then `nat.cast_coe` doesn't get a chance to apply. -/ library_note "coercion into rings" attribute [instance, priority 950] coe_base attribute [instance, priority 500] coe_trans -- see note [coercion into rings] @[priority 900] instance cast_coe : has_coe_t ℕ α := ⟨nat.cast⟩ @[simp, norm_cast] theorem cast_zero : ((0 : ℕ) : α) = 0 := rfl theorem cast_add_one (n : ℕ) : ((n + 1 : ℕ) : α) = n + 1 := rfl @[simp, norm_cast, priority 500] theorem cast_succ (n : ℕ) : ((succ n : ℕ) : α) = n + 1 := rfl @[simp, norm_cast] theorem cast_ite (P : Prop) [decidable P] (m n : ℕ) : (((ite P m n) : ℕ) : α) = ite P (m : α) (n : α) := by { split_ifs; refl, } end @[simp, norm_cast] theorem cast_one [add_monoid α] [has_one α] : ((1 : ℕ) : α) = 1 := zero_add _ @[simp, norm_cast] theorem cast_add [add_monoid α] [has_one α] (m) : ∀ n, ((m + n : ℕ) : α) = m + n | 0 := (add_zero _).symm | (n+1) := show ((m + n : ℕ) : α) + 1 = m + (n + 1), by rw [cast_add n, add_assoc] @[simp] lemma bin_cast_eq [add_monoid α] [has_one α] (n : ℕ) : (nat.bin_cast n : α) = ((n : ℕ) : α) := begin rw nat.bin_cast, apply binary_rec _ _ n, { rw [binary_rec_zero, cast_zero] }, { intros b k h, rw [binary_rec_eq, h], { cases b; simp [bit, bit0, bit1] }, { simp } }, end /-- `coe : ℕ → α` as an `add_monoid_hom`. -/ def cast_add_monoid_hom (α : Type*) [add_monoid α] [has_one α] : ℕ →+ α := { to_fun := coe, map_add' := cast_add, map_zero' := cast_zero } @[simp] lemma coe_cast_add_monoid_hom [add_monoid α] [has_one α] : (cast_add_monoid_hom α : ℕ → α) = coe := rfl @[simp, norm_cast] theorem cast_bit0 [add_monoid α] [has_one α] (n : ℕ) : ((bit0 n : ℕ) : α) = bit0 n := cast_add _ _ @[simp, norm_cast] theorem cast_bit1 [add_monoid α] [has_one α] (n : ℕ) : ((bit1 n : ℕ) : α) = bit1 n := by rw [bit1, cast_add_one, cast_bit0]; refl lemma cast_two {α : Type*} [add_monoid α] [has_one α] : ((2 : ℕ) : α) = 2 := by simp @[simp, norm_cast] theorem cast_pred [add_group α] [has_one α] : ∀ {n}, 0 < n → ((n - 1 : ℕ) : α) = n - 1 | (n+1) h := (add_sub_cancel (n:α) 1).symm @[simp, norm_cast] theorem cast_sub [add_group α] [has_one α] {m n} (h : m ≤ n) : ((n - m : ℕ) : α) = n - m := eq_sub_of_add_eq $ by rw [← cast_add, nat.sub_add_cancel h] @[simp, norm_cast] theorem cast_mul [semiring α] (m) : ∀ n, ((m * n : ℕ) : α) = m * n | 0 := (mul_zero _).symm | (n+1) := (cast_add _ _).trans $ show ((m * n : ℕ) : α) + m = m * (n + 1), by rw [cast_mul n, left_distrib, mul_one] @[simp] theorem cast_dvd {α : Type*} [field α] {m n : ℕ} (n_dvd : n ∣ m) (n_nonzero : (n:α) ≠ 0) : ((m / n : ℕ) : α) = m / n := begin rcases n_dvd with ⟨k, rfl⟩, have : n ≠ 0, {rintro rfl, simpa using n_nonzero}, rw nat.mul_div_cancel_left _ (pos_iff_ne_zero.2 this), rw [nat.cast_mul, mul_div_cancel_left _ n_nonzero], end /-- `coe : ℕ → α` as a `ring_hom` -/ def cast_ring_hom (α : Type*) [semiring α] : ℕ →+* α := { to_fun := coe, map_one' := cast_one, map_mul' := cast_mul, .. cast_add_monoid_hom α } @[simp] lemma coe_cast_ring_hom [semiring α] : (cast_ring_hom α : ℕ → α) = coe := rfl lemma cast_commute [semiring α] (n : ℕ) (x : α) : commute ↑n x := nat.rec_on n (commute.zero_left x) $ λ n ihn, ihn.add_left $ commute.one_left x lemma cast_comm [semiring α] (n : ℕ) (x : α) : (n : α) * x = x * n := (cast_commute n x).eq lemma commute_cast [semiring α] (x : α) (n : ℕ) : commute x n := (n.cast_commute x).symm section variables [ordered_semiring α] @[simp] theorem cast_nonneg : ∀ n : ℕ, 0 ≤ (n : α) | 0 := le_refl _ | (n+1) := add_nonneg (cast_nonneg n) zero_le_one theorem mono_cast : monotone (coe : ℕ → α) := λ m n h, let ⟨k, hk⟩ := le_iff_exists_add.1 h in by simp [hk] variable [nontrivial α] theorem strict_mono_cast : strict_mono (coe : ℕ → α) := λ m n h, nat.le_induction (lt_add_of_pos_right _ zero_lt_one) (λ n _ h, lt_add_of_lt_of_pos h zero_lt_one) _ h @[simp, norm_cast] theorem cast_le {m n : ℕ} : (m : α) ≤ n ↔ m ≤ n := strict_mono_cast.le_iff_le @[simp, norm_cast] theorem cast_lt {m n : ℕ} : (m : α) < n ↔ m < n := strict_mono_cast.lt_iff_lt @[simp] theorem cast_pos {n : ℕ} : (0 : α) < n ↔ 0 < n := by rw [← cast_zero, cast_lt] lemma cast_add_one_pos (n : ℕ) : 0 < (n : α) + 1 := add_pos_of_nonneg_of_pos n.cast_nonneg zero_lt_one @[simp, norm_cast] theorem one_lt_cast {n : ℕ} : 1 < (n : α) ↔ 1 < n := by rw [← cast_one, cast_lt] @[simp, norm_cast] theorem one_le_cast {n : ℕ} : 1 ≤ (n : α) ↔ 1 ≤ n := by rw [← cast_one, cast_le] @[simp, norm_cast] theorem cast_lt_one {n : ℕ} : (n : α) < 1 ↔ n = 0 := by rw [← cast_one, cast_lt, lt_succ_iff, le_zero_iff] @[simp, norm_cast] theorem cast_le_one {n : ℕ} : (n : α) ≤ 1 ↔ n ≤ 1 := by rw [← cast_one, cast_le] end @[simp, norm_cast] theorem cast_min [linear_ordered_semiring α] {a b : ℕ} : (↑(min a b) : α) = min a b := by by_cases a ≤ b; simp [h, min] @[simp, norm_cast] theorem cast_max [linear_ordered_semiring α] {a b : ℕ} : (↑(max a b) : α) = max a b := by by_cases a ≤ b; simp [h, max] @[simp, norm_cast] theorem abs_cast [linear_ordered_ring α] (a : ℕ) : abs (a : α) = a := abs_of_nonneg (cast_nonneg a) lemma coe_nat_dvd [comm_semiring α] {m n : ℕ} (h : m ∣ n) : (m : α) ∣ (n : α) := ring_hom.map_dvd (nat.cast_ring_hom α) h alias coe_nat_dvd ← has_dvd.dvd.nat_cast section linear_ordered_field variables [linear_ordered_field α] lemma inv_pos_of_nat {n : ℕ} : 0 < ((n : α) + 1)⁻¹ := inv_pos.2 $ add_pos_of_nonneg_of_pos n.cast_nonneg zero_lt_one lemma one_div_pos_of_nat {n : ℕ} : 0 < 1 / ((n : α) + 1) := by { rw one_div, exact inv_pos_of_nat } lemma one_div_le_one_div {n m : ℕ} (h : n ≤ m) : 1 / ((m : α) + 1) ≤ 1 / ((n : α) + 1) := by { refine one_div_le_one_div_of_le _ _, exact nat.cast_add_one_pos _, simpa } lemma one_div_lt_one_div {n m : ℕ} (h : n < m) : 1 / ((m : α) + 1) < 1 / ((n : α) + 1) := by { refine one_div_lt_one_div_of_lt _ _, exact nat.cast_add_one_pos _, simpa } end linear_ordered_field end nat namespace add_monoid_hom variables {A B : Type*} [add_monoid A] @[ext] lemma ext_nat {f g : ℕ →+ A} (h : f 1 = g 1) : f = g := ext $ λ n, nat.rec_on n (f.map_zero.trans g.map_zero.symm) $ λ n ihn, by simp only [nat.succ_eq_add_one, *, map_add] variables [has_one A] [add_monoid B] [has_one B] lemma eq_nat_cast (f : ℕ →+ A) (h1 : f 1 = 1) : ∀ n : ℕ, f n = n := congr_fun $ show f = nat.cast_add_monoid_hom A, from ext_nat (h1.trans nat.cast_one.symm) lemma map_nat_cast (f : A →+ B) (h1 : f 1 = 1) (n : ℕ) : f n = n := (f.comp (nat.cast_add_monoid_hom A)).eq_nat_cast (by simp [h1]) _ end add_monoid_hom namespace monoid_with_zero_hom variables {A : Type*} [monoid_with_zero A] /-- If two `monoid_with_zero_hom`s agree on the positive naturals they are equal. -/ @[ext] theorem ext_nat {f g : monoid_with_zero_hom ℕ A} (h_pos : ∀ {n : ℕ}, 0 < n → f n = g n) : f = g := begin ext (_ | n), { rw [f.map_zero, g.map_zero] }, { exact h_pos n.zero_lt_succ, }, end end monoid_with_zero_hom namespace ring_hom variables {R : Type*} {S : Type*} [semiring R] [semiring S] @[simp] lemma eq_nat_cast (f : ℕ →+* R) (n : ℕ) : f n = n := f.to_add_monoid_hom.eq_nat_cast f.map_one n @[simp] lemma map_nat_cast (f : R →+* S) (n : ℕ) : f n = n := (f.comp (nat.cast_ring_hom R)).eq_nat_cast n lemma ext_nat (f g : ℕ →+* R) : f = g := coe_add_monoid_hom_injective $ add_monoid_hom.ext_nat $ f.map_one.trans g.map_one.symm end ring_hom @[simp, norm_cast] theorem nat.cast_id (n : ℕ) : ↑n = n := ((ring_hom.id ℕ).eq_nat_cast n).symm @[simp] theorem nat.cast_with_bot : ∀ (n : ℕ), @coe ℕ (with_bot ℕ) (@coe_to_lift _ _ nat.cast_coe) n = n | 0 := rfl | (n+1) := by rw [with_bot.coe_add, nat.cast_add, nat.cast_with_bot n]; refl instance nat.subsingleton_ring_hom {R : Type*} [semiring R] : subsingleton (ℕ →+* R) := ⟨ring_hom.ext_nat⟩ namespace with_top variables {α : Type*} variables [has_zero α] [has_one α] [has_add α] @[simp, norm_cast] lemma coe_nat : ∀(n : nat), ((n : α) : with_top α) = n | 0 := rfl | (n+1) := by { push_cast, rw [coe_nat n] } @[simp] lemma nat_ne_top (n : nat) : (n : with_top α) ≠ ⊤ := by { rw [←coe_nat n], apply coe_ne_top } @[simp] lemma top_ne_nat (n : nat) : (⊤ : with_top α) ≠ n := by { rw [←coe_nat n], apply top_ne_coe } lemma add_one_le_of_lt {i n : with_top ℕ} (h : i < n) : i + 1 ≤ n := begin cases n, { exact le_top }, cases i, { exact (not_le_of_lt h le_top).elim }, exact with_top.coe_le_coe.2 (with_top.coe_lt_coe.1 h) end lemma one_le_iff_pos {n : with_top ℕ} : 1 ≤ n ↔ 0 < n := ⟨λ h, (coe_lt_coe.2 zero_lt_one).trans_le h, λ h, by simpa only [zero_add] using add_one_le_of_lt h⟩ @[elab_as_eliminator] lemma nat_induction {P : with_top ℕ → Prop} (a : with_top ℕ) (h0 : P 0) (hsuc : ∀n:ℕ, P n → P n.succ) (htop : (∀n : ℕ, P n) → P ⊤) : P a := begin have A : ∀n:ℕ, P n := λ n, nat.rec_on n h0 hsuc, cases a, { exact htop A }, { exact A a } end end with_top namespace pi variables {α β : Type*} lemma nat_apply [has_zero β] [has_one β] [has_add β] : ∀ (n : ℕ) (a : α), (n : α → β) a = n | 0 a := rfl | (n+1) a := by rw [nat.cast_succ, nat.cast_succ, add_apply, nat_apply, one_apply] @[simp] lemma coe_nat [has_zero β] [has_one β] [has_add β] (n : ℕ) : (n : α → β) = λ _, n := by { ext, rw pi.nat_apply } end pi
f4941243e773d621dda39e3f53d7893cf40e17eb
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/test_perm_ac1.lean
135fc17d7e3d22d77481556b19252d5c193cde55
[ "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,654
lean
#exit open expr decidable tactic nat meta definition is_poly_bin_app : expr → option name | (app (app (app (app (const op ls) A) s) lhs) rhs) := some op | _ := none meta definition is_add (e : expr) : bool := match (is_poly_bin_app e) with | (some op) := to_bool (op = `add) | none := ff end meta definition perm_add (e1 e2 : expr) : tactic expr := do when (is_add e1 = ff) (fail "given expression is not an addition"), add_fn : expr ← return $ app_fn (app_fn e1), A : expr ← return $ app_arg (app_fn add_fn), s1 : expr ← mk_app `add_semigroup [A] >>= mk_instance, assoc : expr ← mk_mapp `add.assoc [some A, some s1], s2 : expr ← mk_app `add_comm_semigroup [A] >>= mk_instance, comm : expr ← mk_mapp `add.comm [some A, some s2], perm_ac add_fn assoc comm e1 e2 meta definition tst_perm : tactic unit := do trace "--------", (lhs, rhs) ← target >>= match_eq, H ← perm_add lhs rhs, trace H, exact H set_option trace.tactic.perm_ac true example (a b c d : nat) : d + b + c + a = a + b + c + d := by tst_perm example (a b c d : nat) : a + b + c + d = d + c + b + a := by tst_perm example (a b c d : nat) : ((b + a) + (d + c)) = (c + d) + (a + b) := by tst_perm example (a b c d e f : nat) : (e + d) + (c + (b + (a + f))) = f + (b + (c + (d + (e + a)))) := by tst_perm example (a b c d e f : nat) : (c + b + a) + (f + (e + d)) = a + b + c + d + e + f := by tst_perm example (a b c d e f : nat) : a + (c + b) + (e + d) + f = f + (b + (c + (d + (e + a)))) := by tst_perm example (a b c d e f : nat) : a + (d + b + c) + (f + e) = a + (b + (c + (d + (e + f)))) := by tst_perm
62c12f839d2cfc28d1dc0c4b1128f969a42a5e3a
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/08_Building_Theories_and_Proofs.org.12.lean
8bd660026b0017c9beb84d9c093a01ec8984fde3
[]
no_license
cjmazey/lean-tutorial
ba559a49f82aa6c5848b9bf17b7389bf7f4ba645
381f61c9fcac56d01d959ae0fa6e376f2c4e3b34
refs/heads/master
1,610,286,098,832
1,447,124,923,000
1,447,124,923,000
43,082,433
0
0
null
null
null
null
UTF-8
Lean
false
false
921
lean
import standard namespace hide -- BEGIN variables {A : Type} (R : A → A → Prop) definition reflexive : Prop := ∀ (a : A), R a a definition symmetric : Prop := ∀ {a b : A}, R a b → R b a definition transitive : Prop := ∀ {a b c : A}, R a b → R b c → R a c definition euclidean : Prop := ∀ {a b c : A}, R a b → R a c → R b c variable {R} theorem th1 (refl : reflexive R) (eucl : euclidean R) : symmetric R := take a b : A, assume (H : R a b), show R b a, from eucl H !refl theorem th2 (symm : symmetric R) (eucl : euclidean R) : transitive R := take (a b c : A), assume (H : R a b) (K : R b c), have H' : R b a, from symm H, show R a c, from eucl H' K -- ERROR: /- theorem th3 (refl : reflexive R) (eucl : euclidean R) : transitive R := th2 (th1 refl eucl) eucl -/ theorem th3 (refl : reflexive R) (eucl : euclidean R) : transitive R := @th2 _ _ (@th1 _ _ @refl @eucl) @eucl -- END end hide
f54a0bbab1ad3f3ecde546bc27ebb6feb33ecd08
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/list/sublists.lean
d70adc5c082ee8a12b21b8e53643fa8295ea0861
[ "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
17,067
lean
/- Copyright (c) 2019 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.nat.choose.basic import data.list.perm /-! # sublists > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. `list.sublists` gives a list of all (not necessarily contiguous) sublists of a list. This file contains basic results on this function. -/ universes u v w variables {α : Type u} {β : Type v} {γ : Type w} open nat namespace list /-! ### sublists -/ @[simp] theorem sublists'_nil : sublists' (@nil α) = [[]] := rfl @[simp, priority 1100] theorem sublists'_singleton (a : α) : sublists' [a] = [[], [a]] := rfl theorem map_sublists'_aux (g : list β → list γ) (l : list α) (f r) : map g (sublists'_aux l f r) = sublists'_aux l (g ∘ f) (map g r) := by induction l generalizing f r; [refl, simp only [*, sublists'_aux]] theorem sublists'_aux_append (r' : list (list β)) (l : list α) (f r) : sublists'_aux l f (r ++ r') = sublists'_aux l f r ++ r' := by induction l generalizing f r; [refl, simp only [*, sublists'_aux]] theorem sublists'_aux_eq_sublists' (l f r) : @sublists'_aux α β l f r = map f (sublists' l) ++ r := by rw [sublists', map_sublists'_aux, ← sublists'_aux_append]; refl @[simp] theorem sublists'_cons (a : α) (l : list α) : sublists' (a :: l) = sublists' l ++ map (cons a) (sublists' l) := by rw [sublists', sublists'_aux]; simp only [sublists'_aux_eq_sublists', map_id, append_nil]; refl @[simp] theorem mem_sublists' {s t : list α} : s ∈ sublists' t ↔ s <+ t := begin induction t with a t IH generalizing s, { simp only [sublists'_nil, mem_singleton], exact ⟨λ h, by rw h, eq_nil_of_sublist_nil⟩ }, simp only [sublists'_cons, mem_append, IH, mem_map], split; intro h, rcases h with h | ⟨s, h, rfl⟩, { exact sublist_cons_of_sublist _ h }, { exact h.cons_cons _ }, { cases h with _ _ _ h s _ _ h, { exact or.inl h }, { exact or.inr ⟨s, h, rfl⟩ } } end @[simp] theorem length_sublists' : ∀ l : list α, length (sublists' l) = 2 ^ length l | [] := rfl | (a::l) := by simp only [sublists'_cons, length_append, length_sublists' l, length_map, length, pow_succ', mul_succ, mul_zero, zero_add] @[simp] theorem sublists_nil : sublists (@nil α) = [[]] := rfl @[simp] theorem sublists_singleton (a : α) : sublists [a] = [[], [a]] := rfl theorem sublists_aux₁_eq_sublists_aux : ∀ l (f : list α → list β), sublists_aux₁ l f = sublists_aux l (λ ys r, f ys ++ r) | [] f := rfl | (a::l) f := by rw [sublists_aux₁, sublists_aux]; simp only [*, append_assoc] theorem sublists_aux_cons_eq_sublists_aux₁ (l : list α) : sublists_aux l cons = sublists_aux₁ l (λ x, [x]) := by rw [sublists_aux₁_eq_sublists_aux]; refl theorem sublists_aux_eq_foldr.aux {a : α} {l : list α} (IH₁ : ∀ (f : list α → list β → list β), sublists_aux l f = foldr f [] (sublists_aux l cons)) (IH₂ : ∀ (f : list α → list (list α) → list (list α)), sublists_aux l f = foldr f [] (sublists_aux l cons)) (f : list α → list β → list β) : sublists_aux (a::l) f = foldr f [] (sublists_aux (a::l) cons) := begin simp only [sublists_aux, foldr_cons], rw [IH₂, IH₁], congr' 1, induction sublists_aux l cons with _ _ ih, {refl}, simp only [ih, foldr_cons] end theorem sublists_aux_eq_foldr (l : list α) : ∀ (f : list α → list β → list β), sublists_aux l f = foldr f [] (sublists_aux l cons) := suffices _ ∧ ∀ f : list α → list (list α) → list (list α), sublists_aux l f = foldr f [] (sublists_aux l cons), from this.1, begin induction l with a l IH, {split; intro; refl}, exact ⟨sublists_aux_eq_foldr.aux IH.1 IH.2, sublists_aux_eq_foldr.aux IH.2 IH.2⟩ end theorem sublists_aux_cons_cons (l : list α) (a : α) : sublists_aux (a::l) cons = [a] :: foldr (λys r, ys :: (a :: ys) :: r) [] (sublists_aux l cons) := by rw [← sublists_aux_eq_foldr]; refl theorem sublists_aux₁_append : ∀ (l₁ l₂ : list α) (f : list α → list β), sublists_aux₁ (l₁ ++ l₂) f = sublists_aux₁ l₁ f ++ sublists_aux₁ l₂ (λ x, f x ++ sublists_aux₁ l₁ (f ∘ (++ x))) | [] l₂ f := by simp only [sublists_aux₁, nil_append, append_nil] | (a::l₁) l₂ f := by simp only [sublists_aux₁, cons_append, sublists_aux₁_append l₁, append_assoc]; refl theorem sublists_aux₁_concat (l : list α) (a : α) (f : list α → list β) : sublists_aux₁ (l ++ [a]) f = sublists_aux₁ l f ++ f [a] ++ sublists_aux₁ l (λ x, f (x ++ [a])) := by simp only [sublists_aux₁_append, sublists_aux₁, append_assoc, append_nil] theorem sublists_aux₁_bind : ∀ (l : list α) (f : list α → list β) (g : β → list γ), (sublists_aux₁ l f).bind g = sublists_aux₁ l (λ x, (f x).bind g) | [] f g := rfl | (a::l) f g := by simp only [sublists_aux₁, bind_append, sublists_aux₁_bind l] theorem sublists_aux_cons_append (l₁ l₂ : list α) : sublists_aux (l₁ ++ l₂) cons = sublists_aux l₁ cons ++ (do x ← sublists_aux l₂ cons, (++ x) <$> sublists l₁) := begin simp only [sublists, sublists_aux_cons_eq_sublists_aux₁, sublists_aux₁_append, bind_eq_bind, sublists_aux₁_bind], congr, funext x, apply congr_arg _, rw [← bind_ret_eq_map, sublists_aux₁_bind], exact (append_nil _).symm end theorem sublists_append (l₁ l₂ : list α) : sublists (l₁ ++ l₂) = (do x ← sublists l₂, (++ x) <$> sublists l₁) := by simp only [map, sublists, sublists_aux_cons_append, map_eq_map, bind_eq_bind, cons_bind, map_id', append_nil, cons_append, map_id' (λ _, rfl)]; split; refl @[simp] theorem sublists_concat (l : list α) (a : α) : sublists (l ++ [a]) = sublists l ++ map (λ x, x ++ [a]) (sublists l) := by rw [sublists_append, sublists_singleton, bind_eq_bind, cons_bind, cons_bind, nil_bind, map_eq_map, map_eq_map, map_id' (append_nil), append_nil] theorem sublists_reverse (l : list α) : sublists (reverse l) = map reverse (sublists' l) := by induction l with hd tl ih; [refl, simp only [reverse_cons, sublists_append, sublists'_cons, map_append, ih, sublists_singleton, map_eq_map, bind_eq_bind, map_map, cons_bind, append_nil, nil_bind, (∘)]] theorem sublists_eq_sublists' (l : list α) : sublists l = map reverse (sublists' (reverse l)) := by rw [← sublists_reverse, reverse_reverse] theorem sublists'_reverse (l : list α) : sublists' (reverse l) = map reverse (sublists l) := by simp only [sublists_eq_sublists', map_map, map_id' (reverse_reverse)] theorem sublists'_eq_sublists (l : list α) : sublists' l = map reverse (sublists (reverse l)) := by rw [← sublists'_reverse, reverse_reverse] theorem sublists_aux_ne_nil : ∀ (l : list α), [] ∉ sublists_aux l cons | [] := id | (a::l) := begin rw [sublists_aux_cons_cons], refine not_mem_cons_of_ne_of_not_mem (cons_ne_nil _ _).symm _, have := sublists_aux_ne_nil l, revert this, induction sublists_aux l cons; intro, {rwa foldr}, simp only [foldr, mem_cons_iff, false_or, not_or_distrib], exact ⟨ne_of_not_mem_cons this, ih (not_mem_of_not_mem_cons this)⟩ end @[simp] theorem mem_sublists {s t : list α} : s ∈ sublists t ↔ s <+ t := by rw [← reverse_sublist_iff, ← mem_sublists', sublists'_reverse, mem_map_of_injective reverse_injective] @[simp] theorem length_sublists (l : list α) : length (sublists l) = 2 ^ length l := by simp only [sublists_eq_sublists', length_map, length_sublists', length_reverse] theorem map_ret_sublist_sublists (l : list α) : map list.ret l <+ sublists l := reverse_rec_on l (nil_sublist _) $ λ l a IH, by simp only [map, map_append, sublists_concat]; exact ((append_sublist_append_left _).2 $ singleton_sublist.2 $ mem_map.2 ⟨[], mem_sublists.2 (nil_sublist _), by refl⟩).trans ((append_sublist_append_right _).2 IH) /-! ### sublists_len -/ /-- Auxiliary function to construct the list of all sublists of a given length. Given an integer `n`, a list `l`, a function `f` and an auxiliary list `L`, it returns the list made of of `f` applied to all sublists of `l` of length `n`, concatenated with `L`. -/ def sublists_len_aux {α β : Type*} : ℕ → list α → (list α → β) → list β → list β | 0 l f r := f [] :: r | (n+1) [] f r := r | (n+1) (a::l) f r := sublists_len_aux (n + 1) l f (sublists_len_aux n l (f ∘ list.cons a) r) /-- The list of all sublists of a list `l` that are of length `n`. For instance, for `l = [0, 1, 2, 3]` and `n = 2`, one gets `[[2, 3], [1, 3], [1, 2], [0, 3], [0, 2], [0, 1]]`. -/ def sublists_len {α : Type*} (n : ℕ) (l : list α) : list (list α) := sublists_len_aux n l id [] lemma sublists_len_aux_append {α β γ : Type*} : ∀ (n : ℕ) (l : list α) (f : list α → β) (g : β → γ) (r : list β) (s : list γ), sublists_len_aux n l (g ∘ f) (r.map g ++ s) = (sublists_len_aux n l f r).map g ++ s | 0 l f g r s := rfl | (n+1) [] f g r s := rfl | (n+1) (a::l) f g r s := begin unfold sublists_len_aux, rw [show ((g ∘ f) ∘ list.cons a) = (g ∘ f ∘ list.cons a), by refl, sublists_len_aux_append, sublists_len_aux_append] end lemma sublists_len_aux_eq {α β : Type*} (l : list α) (n) (f : list α → β) (r) : sublists_len_aux n l f r = (sublists_len n l).map f ++ r := by rw [sublists_len, ← sublists_len_aux_append]; refl lemma sublists_len_aux_zero {α : Type*} (l : list α) (f : list α → β) (r) : sublists_len_aux 0 l f r = f [] :: r := by cases l; refl @[simp] lemma sublists_len_zero {α : Type*} (l : list α) : sublists_len 0 l = [[]] := sublists_len_aux_zero _ _ _ @[simp] lemma sublists_len_succ_nil {α : Type*} (n) : sublists_len (n+1) (@nil α) = [] := rfl @[simp] lemma sublists_len_succ_cons {α : Type*} (n) (a : α) (l) : sublists_len (n + 1) (a::l) = sublists_len (n + 1) l ++ (sublists_len n l).map (cons a) := by rw [sublists_len, sublists_len_aux, sublists_len_aux_eq, sublists_len_aux_eq, map_id, append_nil]; refl @[simp] lemma length_sublists_len {α : Type*} : ∀ n (l : list α), length (sublists_len n l) = nat.choose (length l) n | 0 l := by simp | (n+1) [] := by simp | (n+1) (a::l) := by simp [-add_comm, nat.choose, *]; apply add_comm lemma sublists_len_sublist_sublists' {α : Type*} : ∀ n (l : list α), sublists_len n l <+ sublists' l | 0 l := singleton_sublist.2 (mem_sublists'.2 (nil_sublist _)) | (n+1) [] := nil_sublist _ | (n+1) (a::l) := begin rw [sublists_len_succ_cons, sublists'_cons], exact (sublists_len_sublist_sublists' _ _).append ((sublists_len_sublist_sublists' _ _).map _) end lemma sublists_len_sublist_of_sublist {α : Type*} (n) {l₁ l₂ : list α} (h : l₁ <+ l₂) : sublists_len n l₁ <+ sublists_len n l₂ := begin induction n with n IHn generalizing l₁ l₂, {simp}, induction h with l₁ l₂ a s IH l₁ l₂ a s IH, {refl}, { refine IH.trans _, rw sublists_len_succ_cons, apply sublist_append_left }, { simp [sublists_len_succ_cons], exact IH.append ((IHn s).map _) } end lemma length_of_sublists_len {α : Type*} : ∀ {n} {l l' : list α}, l' ∈ sublists_len n l → length l' = n | 0 l l' (or.inl rfl) := rfl | (n+1) (a::l) l' h := begin rw [sublists_len_succ_cons, mem_append, mem_map] at h, rcases h with h | ⟨l', h, rfl⟩, { exact length_of_sublists_len h }, { exact congr_arg (+1) (length_of_sublists_len h) }, end lemma mem_sublists_len_self {α : Type*} {l l' : list α} (h : l' <+ l) : l' ∈ sublists_len (length l') l := begin induction h with l₁ l₂ a s IH l₁ l₂ a s IH, { exact or.inl rfl }, { cases l₁ with b l₁, { exact or.inl rfl }, { rw [length, sublists_len_succ_cons], exact mem_append_left _ IH } }, { rw [length, sublists_len_succ_cons], exact mem_append_right _ (mem_map.2 ⟨_, IH, rfl⟩) } end @[simp] lemma mem_sublists_len {α : Type*} {n} {l l' : list α} : l' ∈ sublists_len n l ↔ l' <+ l ∧ length l' = n := ⟨λ h, ⟨mem_sublists'.1 ((sublists_len_sublist_sublists' _ _).subset h), length_of_sublists_len h⟩, λ ⟨h₁, h₂⟩, h₂ ▸ mem_sublists_len_self h₁⟩ lemma sublists_len_of_length_lt {n} {l : list α} (h : l.length < n) : sublists_len n l = [] := eq_nil_iff_forall_not_mem.mpr $ λ x, mem_sublists_len.not.mpr $ λ ⟨hs, hl⟩, (h.trans_eq hl.symm).not_le (sublist.length_le hs) @[simp] lemma sublists_len_length : ∀ (l : list α), sublists_len l.length l = [l] | [] := rfl | (a::l) := by rw [length, sublists_len_succ_cons, sublists_len_length, map_singleton, sublists_len_of_length_lt (lt_succ_self _), nil_append] open function theorem pairwise.sublists' {R} : ∀ {l : list α}, pairwise R l → pairwise (lex (swap R)) (sublists' l) | _ pairwise.nil := pairwise_singleton _ _ | _ (@pairwise.cons _ _ a l H₁ H₂) := begin simp only [sublists'_cons, pairwise_append, pairwise_map, mem_sublists', mem_map, exists_imp_distrib, and_imp], refine ⟨H₂.sublists', H₂.sublists'.imp (λ l₁ l₂, lex.cons), _⟩, rintro l₁ sl₁ x l₂ sl₂ rfl, cases l₁ with b l₁, {constructor}, exact lex.rel (H₁ _ $ sl₁.subset $ mem_cons_self _ _) end theorem pairwise_sublists {R} {l : list α} (H : pairwise R l) : pairwise (λ l₁ l₂, lex R (reverse l₁) (reverse l₂)) (sublists l) := by { have := (pairwise_reverse.2 H).sublists', rwa [sublists'_reverse, pairwise_map] at this } @[simp] theorem nodup_sublists {l : list α} : nodup (sublists l) ↔ nodup l := ⟨λ h, (h.sublist (map_ret_sublist_sublists _)).of_map _, λ h, (pairwise_sublists h).imp (λ _ _ h, mt reverse_inj.2 h.to_ne)⟩ @[simp] theorem nodup_sublists' {l : list α} : nodup (sublists' l) ↔ nodup l := by rw [sublists'_eq_sublists, nodup_map_iff reverse_injective, nodup_sublists, nodup_reverse] alias nodup_sublists ↔ nodup.of_sublists nodup.sublists alias nodup_sublists' ↔ nodup.of_sublists' nodup.sublists' attribute [protected] nodup.sublists nodup.sublists' lemma nodup_sublists_len (n : ℕ) {l : list α} (h : nodup l) : (sublists_len n l).nodup := h.sublists'.sublist $ sublists_len_sublist_sublists' _ _ theorem sublists_cons_perm_append (a : α) (l : list α) : sublists (a :: l) ~ sublists l ++ map (cons a) (sublists l) := begin simp only [sublists, sublists_aux_cons_cons, cons_append, perm_cons], refine (perm.cons _ _).trans perm_middle.symm, induction sublists_aux l cons with b l IH; simp, exact (IH.cons _).trans perm_middle.symm end theorem sublists_perm_sublists' : ∀ l : list α, sublists l ~ sublists' l | [] := perm.refl _ | (a::l) := let IH := sublists_perm_sublists' l in by rw sublists'_cons; exact (sublists_cons_perm_append _ _).trans (IH.append (IH.map _)) theorem revzip_sublists (l : list α) : ∀ l₁ l₂, (l₁, l₂) ∈ revzip l.sublists → l₁ ++ l₂ ~ l := begin rw revzip, apply list.reverse_rec_on l, { intros l₁ l₂ h, simp at h, simp [h] }, { intros l a IH l₁ l₂ h, rw [sublists_concat, reverse_append, zip_append, ← map_reverse, zip_map_right, zip_map_left] at h; [skip, {simp}], simp only [prod.mk.inj_iff, mem_map, mem_append, prod.map_mk, prod.exists] at h, rcases h with ⟨l₁, l₂', h, rfl, rfl⟩ | ⟨l₁', l₂, h, rfl, rfl⟩, { rw ← append_assoc, exact (IH _ _ h).append_right _ }, { rw append_assoc, apply (perm_append_comm.append_left _).trans, rw ← append_assoc, exact (IH _ _ h).append_right _ } } end theorem revzip_sublists' (l : list α) : ∀ l₁ l₂, (l₁, l₂) ∈ revzip l.sublists' → l₁ ++ l₂ ~ l := begin rw revzip, induction l with a l IH; intros l₁ l₂ h, { simp at h, simp [h] }, { rw [sublists'_cons, reverse_append, zip_append, ← map_reverse, zip_map_right, zip_map_left] at h; [simp at h, simp], rcases h with ⟨l₁, l₂', h, rfl, rfl⟩ | ⟨l₁', h, rfl⟩, { exact perm_middle.trans ((IH _ _ h).cons _) }, { exact (IH _ _ h).cons _ } } end lemma range_bind_sublists_len_perm {α : Type*} (l : list α) : (list.range (l.length + 1)).bind (λ n, sublists_len n l) ~ sublists' l := begin induction l with h tl, { simp [range_succ] }, { simp_rw [range_succ_eq_map, length, cons_bind, map_bind, sublists_len_succ_cons, sublists'_cons, list.sublists_len_zero, list.singleton_append], refine ((bind_append_perm (range (tl.length + 1)) _ _).symm.cons _).trans _, simp_rw [←list.bind_map, ←cons_append], rw [←list.singleton_append, ←list.sublists_len_zero tl], refine perm.append _ (l_ih.map _), rw [list.range_succ, append_bind, bind_singleton, sublists_len_of_length_lt (nat.lt_succ_self _), append_nil, ←list.map_bind (λ n, sublists_len n tl) nat.succ, ←cons_bind 0 _ (λ n, sublists_len n tl), ←range_succ_eq_map], exact l_ih } end end list
11f87aacd155c41f1da70c42331f69c8aa8ee5ce
4fa118f6209450d4e8d058790e2967337811b2b5
/src/for_mathlib/topology.lean
8d7d3ed697a6cd6319a032bbdcf316b9a54e388f
[ "Apache-2.0" ]
permissive
leanprover-community/lean-perfectoid-spaces
16ab697a220ed3669bf76311daa8c466382207f7
95a6520ce578b30a80b4c36e36ab2d559a842690
refs/heads/master
1,639,557,829,139
1,638,797,866,000
1,638,797,866,000
135,769,296
96
10
Apache-2.0
1,638,797,866,000
1,527,892,754,000
Lean
UTF-8
Lean
false
false
14,976
lean
import topology.opens import topology.algebra.continuous_functions import for_mathlib.filter import for_mathlib.data.set.basic open topological_space function local notation `𝓝` x:70 := nhds x local notation f `∘₂` g := function.bicompr f g -- We need to think whether we could directly use the class t2_space (which is not using opens though) definition is_hausdorff (α : Type*) [topological_space α] : Prop := ∀ x y, x ≠ y → ∃ u v : opens α, x ∈ u ∧ y ∈ v ∧ u ∩ v = ∅ open set filter instance regular_of_discrete {α : Type*} [topological_space α] [discrete_topology α] : regular_space α := { t1 := λ x, is_open_discrete _, regular := begin intros s a s_closed a_not, refine ⟨s, is_open_discrete s, subset.refl s, _⟩, erw [← empty_in_sets_eq_bot, mem_inf_sets], use {a}, rw nhds_discrete α, simp, refine ⟨s, subset.refl s, _ ⟩, rintro x ⟨xa, xs⟩, rw ← mem_singleton_iff.1 xa at a_not, exact a_not xs end } lemma continuous_of_const {α : Type*} {β : Type*} [topological_space α] [topological_space β] {f : α → β} (h : ∀a b, f a = f b) : continuous f := λ s _, by convert @is_open_const _ _ (∃ a, f a ∈ s); exact set.ext (λ a, ⟨λ fa, ⟨_, fa⟩, λ ⟨b, fb⟩, show f a ∈ s, from h b a ▸ fb⟩) section variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} variables [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] def continuous₂ (f : α → β → γ) := continuous (function.uncurry' f) lemma continuous₂_def (f : α → β → γ) : continuous₂ f ↔ continuous (function.uncurry' f) := iff.rfl lemma continuous₂_curry (f : α × β → γ) : continuous₂ (function.curry f) ↔ continuous f := by rw [←function.uncurry'_curry f] {occs := occurrences.pos [2]} ; refl lemma continuous₂.comp {f : α → β → γ} {g : γ → δ} (hf : continuous₂ f)(hg : continuous g) : continuous₂ (g ∘₂ f) := hg.comp hf section open set filter lattice function /- f α → β g ↓ ↓ h γ → δ i -/ variables {f : α → β} {g : α → γ} {i : γ → δ} {h : β → δ} lemma continuous_of_continuous_on_of_induced (H : h ∘ f = i ∘ g) (hi : continuous_on i $ range g) (hg : ‹topological_space α› = induced g ‹topological_space γ›) (hh : ‹topological_space β› = induced h ‹topological_space δ›) : continuous f := begin rw continuous_iff_continuous_at, intro x, dsimp [continuous_at, tendsto], rw [hg, hh, nhds_induced, nhds_induced, ← map_le_iff_le_comap, map_comm H], specialize hi (g x) ⟨x, rfl⟩, have := calc nhds_within (g x) (range g) = 𝓝 g x ⊓ principal (range g) : rfl ... = 𝓝 g x ⊓ map g (principal univ) : by rw [← image_univ, ← map_principal] ... = 𝓝 g x ⊓ map g ⊤ : by rw principal_univ, rw [continuous_within_at, this, ← comp_app i g, ← congr_fun H x] at hi, clear this, have := calc map g (comap g 𝓝 g x) = map g (comap g 𝓝 g x ⊓ ⊤) : by rw inf_top_eq ... ≤ map g (comap g 𝓝 g x) ⊓ map g ⊤ : map_inf_le ... ≤ 𝓝 g x ⊓ map g ⊤ : inf_le_inf map_comap_le (le_refl _), exact le_trans (map_mono this) hi, end variables (eg : embedding g) (eh : embedding h) include eg lemma embedding.nhds_eq_comap (a : α) : nhds a = comap g (nhds $ g a) := by rw [eg.induced, nhds_induced] include eh lemma embedding.tendsto_iff (H : h ∘ f = i ∘ g) (a : α) : continuous_at i (g a) → continuous_at f a:= begin let N := nhds a, let Nf := nhds (f a), let Nhf := nhds (h $ f a), let Ng := nhds (g a), have Neq1 : Nf = comap h Nhf, from eh.nhds_eq_comap (f a), have Neq2 : N = comap g Ng, from eg.nhds_eq_comap a, intro hyp, replace hyp : Ng ≤ comap i Nhf, { unfold continuous_at at hyp, rw ← show h (f a) = i (g a), from congr_fun H a at hyp, rwa tendsto_iff_comap at hyp }, rw calc continuous_at f a ↔ tendsto f N Nf : iff.rfl ... ↔ N ≤ comap f Nf : tendsto_iff_comap ... ↔ comap g Ng ≤ comap f (comap h Nhf) : by rw [Neq1, Neq2] ... ↔ comap g Ng ≤ comap g (comap i Nhf) : by rw comap_comm H, exact comap_mono hyp end end end namespace dense_inducing open set function filter variables {α : Type*} {β : Type*} {δ : Type*} {γ : Type*} variables [topological_space α] [topological_space β] [topological_space δ] [topological_space γ] /- f α → β g ↓ ↓ h γ → δ i -/ variables {f : α → β} {g : α → γ} {i : γ → δ} {h : β → δ} lemma comp (dh : dense_inducing h) (df : dense_inducing f) : dense_inducing (h ∘ f) := { dense := dense_range.comp _ dh.dense df.dense dh.continuous, induced := (dh.to_inducing.comp df.to_inducing).induced } lemma of_comm_square (dg : dense_inducing g) (di : dense_inducing i) (dh : dense_inducing h) (H : h ∘ f = i ∘ g) : dense_inducing f := have dhf : dense_inducing (h ∘ f), by {rw H, exact di.comp dg }, { dense := begin intro x, have H := dhf.dense (h x), rw mem_closure_iff_nhds at H ⊢, intros t ht, rw [dh.nhds_eq_comap x, mem_comap_sets] at ht, rcases ht with ⟨u, hu, hinc⟩, rcases H u hu with ⟨v, hv1, a, rfl⟩, use f a, split, swap, apply mem_range_self, apply mem_of_mem_of_subset _ hinc, rwa mem_preimage, end , -- inj := λ a b H, dhf.inj (by {show h (f a) = _, rw H}), induced := by rw [dg.induced, di.induced, induced_compose, ← H, ← induced_compose, dh.induced] } end dense_inducing namespace dense_embedding open set function filter variables {α : Type*} {β : Type*} {δ : Type*} {γ : Type*} variables [topological_space α] [topological_space β] [topological_space δ] [topological_space γ] /- f α → β g ↓ ↓ h γ → δ i -/ variables {f : α → β} {g : α → γ} {i : γ → δ} {h : β → δ} -- TODO: fix implicit argument in dense_range.comp before PRing lemma comp (dh : dense_embedding h) (df : dense_embedding f) : dense_embedding (h ∘ f) := { dense := dense_range.comp _ dh.dense df.dense dh.to_dense_inducing.continuous, inj := function.injective_comp dh.inj df.inj, induced := (dh.to_inducing.comp df.to_inducing).induced } lemma of_homeo (h : α ≃ₜ β) : dense_embedding h := { dense := dense_range_iff_closure_range.mpr $ (range_iff_surjective.mpr h.to_equiv.surjective).symm ▸ closure_univ, inj := h.to_equiv.injective, induced := h.induced_eq.symm, } lemma of_comm_square (dg : dense_embedding g) (di : dense_embedding i) (dh : dense_embedding h) (H : h ∘ f = i ∘ g) : dense_embedding f := { inj := begin intros a b hab, have : (h ∘ f) a = (h ∘ f) b := by convert congr_arg h hab, rw H at this, exact dg.inj (di.inj this), end, ..dense_inducing.of_comm_square dg.to_dense_inducing di.to_dense_inducing dh.to_dense_inducing H } end dense_embedding section open filter variables {α : Type*} [topological_space α] {β : Type*} [topological_space β] [discrete_topology β] lemma continuous_into_discrete_iff (f : α → β) : continuous f ↔ ∀ b : β, is_open (f ⁻¹' {b}) := begin split, { intros hf b, exact hf _ (is_open_discrete _) }, { intro h, rw continuous_iff_continuous_at, intro x, have key : f ⁻¹' {f x} ∈ nhds x, from mem_nhds_sets (h $ f x) (set.mem_insert (f x) ∅), calc map f (nhds x) ≤ pure (f x) : le_pure_iff.mpr key ... ≤ nhds (f x) : pure_le_nhds _ } end lemma discrete_iff_open_singletons : discrete_topology α ↔ ∀ x, is_open ({x} : set α) := ⟨by introsI ; exact is_open_discrete _, λ h, ⟨eq_bot_of_singletons_open h⟩⟩ lemma discrete_iff_nhds_eq_pure {X : Type*} [topological_space X] : discrete_topology X ↔ ∀ x : X, nhds x = pure x := begin split, { introsI h, exact congr_fun (nhds_discrete X) }, { intro h, constructor, apply eq_bot_of_singletons_open, intro x, change _root_.is_open {x}, rw is_open_iff_nhds, simp [h] }, end lemma discrete_of_embedding_discrete {X : Type*} {Y : Type*} [topological_space X] [topological_space Y] {f : X → Y} (hf : embedding f) [discrete_topology Y] : discrete_topology X := begin rw discrete_iff_nhds_eq_pure, intro x, rw [hf.to_inducing.nhds_eq_comap, nhds_discrete, comap_pure hf.inj] end lemma is_open_singleton_iff {X : Type*} [topological_space X] {x : X} : is_open ({x} : set X) ↔ {x} ∈ nhds x := begin rw is_open_iff_nhds, split ; intro h, { apply h x (mem_singleton _), simp }, { intros y y_in, rw mem_singleton_iff at y_in, simp [*] }, end end -- tools for proving that a product of top rings is a top ring def continuous_pi₁ {I : Type*} {R : I → Type*} {S : I → Type*} [∀ i, topological_space (R i)] [∀ i, topological_space (S i)] {f : Π (i : I), (R i) → (S i)} (Hfi : ∀ i, continuous (f i)) : continuous (λ rs i, f i (rs i) : (Π (i : I), R i) → Π (i : I), S i) := continuous_pi (λ i, (Hfi i).comp (continuous_apply i)) def continuous_pi₂ {I : Type*} {R : I → Type*} {S : I → Type*} {T : I → Type*} [∀ i, topological_space (R i)] [∀ i, topological_space (S i)] [∀ i, topological_space (T i)] {f : Π (i : I), (R i) × (S i) → (T i)} (Hfi : ∀ i, continuous (f i)) : continuous (λ rs i, f i ⟨rs.1 i, rs.2 i⟩ : (Π (i : I), R i) × (Π (i : I), S i) → Π (i : I), T i) := continuous_pi (λ i, (Hfi i).comp (continuous.prod_mk ((continuous_apply i).comp continuous_fst) $ (continuous_apply i).comp continuous_snd)) /- The following class probably won't have global instances, but is meant to model proofs where we implictly fix a neighborhood filter basis. -/ class nhds_basis (α : Type*) [topological_space α] := (B : α → filter_basis α) (is_nhds : ∀ x, 𝓝 x = (B x).filter) namespace nhds_basis open filter set variables {α : Type*} {ι : Type*} [topological_space α] [nhds_basis α] variables {β : Type*} [topological_space β] {δ : Type*} lemma mem_nhds_iff (x : α) (U : set α) : U ∈ 𝓝 x ↔ ∃ V ∈ B x, V ⊆ U := by rw [is_nhds x, filter_basis.mem_filter] lemma mem_nhds_of_basis {x : α} {U : set α} (U_in : U ∈ B x) : U ∈ 𝓝 x := (is_nhds x).symm ▸ filter_basis.mem_filter_of_mem U_in lemma tendsto_from {f : α → δ} {x : α} {y : filter δ} : tendsto f (𝓝 x) y ↔ ∀ {V}, V ∈ y → ∃ U ∈ B x, U ⊆ f ⁻¹' V := by split ; intros h V V_in ; specialize h V_in ; rwa [← mem_nhds_iff x] at * lemma continuous_from {f : α → β} : continuous f ↔ ∀ x, ∀ {V}, V ∈ 𝓝 f x → ∃ U ∈ B x, U ⊆ f ⁻¹' V := by simp [continuous_iff_continuous_at, continuous_at, tendsto_from] lemma tendsto_into {f : δ → α} {x : filter δ} {y : α} : tendsto f x 𝓝 y ↔ ∀ U ∈ B y, f ⁻¹' U ∈ x := begin split ; intros h, { rintro U U_in, exact h (mem_nhds_of_basis U_in) }, { intros V V_in, rcases (mem_nhds_iff _ _).1 V_in with ⟨W, W_in, hW⟩, filter_upwards [h W W_in], exact preimage_mono hW } end lemma continuous_into {f : β → α} : continuous f ↔ ∀ x, ∀ U ∈ B (f x), f ⁻¹' U ∈ 𝓝 x := by simp [continuous_iff_continuous_at, continuous_at, tendsto_into] lemma tendsto_both [nhds_basis β] {f : α → β} {x : α} {y : β} : tendsto f (𝓝 x) 𝓝 y ↔ ∀ U ∈ B y, ∃ V ∈ B x, V ⊆ f ⁻¹' U := begin rw tendsto_into, split ; introv h U_in ; specialize h U U_in ; rwa mem_nhds_iff x at * end lemma continuous_both [nhds_basis β] {f : α → β} : continuous f ↔ ∀ x, ∀ U ∈ B (f x), ∃ V ∈ B x, V ⊆ f ⁻¹' U := by simp [continuous_iff_continuous_at, continuous_at, tendsto_both] end nhds_basis lemma dense_range.mem_nhds {α : Type*} [topological_space α] {β : Type*} [topological_space β] {f : α → β} (h : dense_range f) {b : β} {U : set β} (U_in : U ∈ nhds b) : ∃ a : α, f a ∈ U := begin rcases (mem_closure_iff_nhds.mp ((dense_range_iff_closure_range.mp h).symm ▸ mem_univ b : b ∈ closure (range f)) U U_in) with ⟨_, h, a, rfl⟩, exact ⟨a, h⟩ end lemma mem_closure_union {α : Type*} [topological_space α] {s₁ s₂ : set α} {x : α} (h : x ∈ closure (s₁ ∪ s₂)) (h₁ : -s₁ ∈ 𝓝 x) : x ∈ closure s₂ := begin rw closure_eq_nhds at *, have := calc 𝓝 x ⊓ principal (s₁ ∪ s₂) = 𝓝 x ⊓ (principal s₁ ⊔ principal s₂) : by rw sup_principal ... = (𝓝 x ⊓ principal s₁) ⊔ (𝓝 x ⊓ principal s₂) : by rw lattice.inf_sup_left ... = ⊥ ⊔ 𝓝 x ⊓ principal s₂ : by rw inf_principal_eq_bot h₁ ... = 𝓝 x ⊓ principal s₂ : by rw lattice.bot_sup_eq, dsimp, rwa ← this end open lattice lemma mem_closure_image {α : Type*} {β : Type*} [topological_space α] [topological_space β] {f : α → β} {x : α} {s : set α} (hf : continuous_at f x) (hx : x ∈ closure s) : f x ∈ closure (f '' s) := begin rw [closure_eq_nhds, mem_set_of_eq] at *, rw ← bot_lt_iff_ne_bot, calc ⊥ < map f (𝓝 x ⊓ principal s) : bot_lt_iff_ne_bot.mpr (map_ne_bot hx) ... ≤ (map f 𝓝 x) ⊓ (map f $ principal s) : map_inf_le ... = (map f 𝓝 x) ⊓ (principal $ f '' s) : by rw map_principal ... ≤ 𝓝 (f x) ⊓ (principal $ f '' s) : inf_le_inf hf (le_refl _) end lemma continuous_at.prod_mk {α : Type*} {β : Type*} {γ : Type*} [topological_space α] [topological_space β] [topological_space γ] {f : γ → α} {g : γ → β} {x : γ} (hf : continuous_at f x) (hg : continuous_at g x) : continuous_at (λ x, prod.mk (f x) $ g x) x := calc map (λ (x : γ), (f x, g x)) (𝓝 x) ≤ (map f 𝓝 x).prod (map g 𝓝 x) : filter.map_prod_mk _ _ _ ... ≤ (𝓝 f x).prod (𝓝 g x) : filter.prod_mono hf hg ... = 𝓝 (f x, g x) : by rw nhds_prod_eq lemma continuous_at.congr_aux {α : Type*} {β : Type*} [topological_space α] [topological_space β] {f g : α → β} {a : α} (h : {x | f x = g x } ∈ 𝓝 a) (hf : continuous_at f a) : continuous_at g a := begin intros U U_in, rw show g a = f a, from (mem_of_nhds h).symm at U_in, let V := {x : α | g x ∈ U} ∩ {x | f x = g x}, suffices : V ∈ 𝓝 a, { rw mem_map, exact mem_sets_of_superset this (inter_subset_left _ _) }, have : V = {x : α | f x ∈ U} ∩ {x | f x = g x}, { ext x, split ; rintros ⟨hl, hr⟩ ; rw mem_set_of_eq at hr hl ; [ rw ← hr at hl, rw hr at hl ] ; exact ⟨hl, hr⟩ }, rw this, exact filter.inter_mem_sets (hf U_in) ‹_› end lemma continuous_at.congr {α : Type*} {β : Type*} [topological_space α] [topological_space β] {f g : α → β} {a : α} (h : {x | f x = g x } ∈ 𝓝 a) : continuous_at f a ↔ continuous_at g a := begin split ; intro h', { exact continuous_at.congr_aux h h' }, { apply continuous_at.congr_aux _ h', convert h, ext x, rw eq_comm } end
7c64b42df8c7bf2ec0e0d721b225419f22ae5066
3f7026ea8bef0825ca0339a275c03b911baef64d
/src/topology/Top/opens.lean
604f614957e78d2a7aa0efcb8d8053832f30475d
[ "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
3,938
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.basic import category_theory.natural_isomorphism import category_theory.opposites import category_theory.eq_to_hom import topology.opens open category_theory open topological_space open opposite universe u namespace topological_space.opens variables {X Y Z : Top.{u}} instance opens_category : category.{u+1} (opens X) := { hom := λ U V, ulift (plift (U ≤ V)), id := λ X, ⟨ ⟨ le_refl X ⟩ ⟩, comp := λ X Y Z f g, ⟨ ⟨ le_trans f.down.down g.down.down ⟩ ⟩ } def to_Top (X : Top.{u}) : opens X ⥤ Top := { obj := λ U, ⟨U.val, infer_instance⟩, map := λ U V i, ⟨λ x, ⟨x.1, i.down.down x.2⟩, (embedding.continuous_iff embedding_subtype_val).2 continuous_induced_dom⟩ } /-- `opens.map f` gives the functor from open sets in Y to open set in X, given by taking preimages under f. -/ def map (f : X ⟶ Y) : opens Y ⥤ opens X := { obj := λ U, ⟨ f.val ⁻¹' U.val, f.property _ U.property ⟩, map := λ U V i, ⟨ ⟨ λ a b, i.down.down b ⟩ ⟩ }. @[simp] lemma map_obj (f : X ⟶ Y) (U) (p) : (map f).obj ⟨U, p⟩ = ⟨ f.val ⁻¹' U, f.property _ p ⟩ := rfl @[simp] lemma map_id_obj' (U) (p) : (map (𝟙 X)).obj ⟨U, p⟩ = ⟨U, p⟩ := rfl @[simp] lemma map_id_obj (U : opens X) : (map (𝟙 X)).obj U = U := by { ext, refl } -- not quite `rfl`, since we don't have eta for records @[simp] lemma map_id_obj_unop (U : (opens X)ᵒᵖ) : (map (𝟙 X)).obj (unop U) = unop U := by simp @[simp] lemma op_map_id_obj (U : (opens X)ᵒᵖ) : (map (𝟙 X)).op.obj U = U := by simp section variable (X) def map_id : map (𝟙 X) ≅ 𝟭 (opens X) := { hom := { app := λ U, eq_to_hom (map_id_obj U) }, inv := { app := λ U, eq_to_hom (map_id_obj U).symm } } @[simp] lemma map_id_hom_app (U) : (map_id X).hom.app U = eq_to_hom (map_id_obj U) := rfl @[simp] lemma map_id_inv_app (U) : (map_id X).inv.app U = eq_to_hom (map_id_obj U).symm := rfl end @[simp] lemma map_comp_obj' (f : X ⟶ Y) (g : Y ⟶ Z) (U) (p) : (map (f ≫ g)).obj ⟨U, p⟩ = (map f).obj ((map g).obj ⟨U, p⟩) := rfl @[simp] lemma map_comp_obj (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (map (f ≫ g)).obj U = (map f).obj ((map g).obj U) := by { ext, refl } -- not quite `rfl`, since we don't have eta for records @[simp] lemma map_comp_obj_unop (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (map (f ≫ g)).obj (unop U) = (map f).obj ((map g).obj (unop U)) := by simp @[simp] lemma op_map_comp_obj (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (map (f ≫ g)).op.obj U = (map f).op.obj ((map g).op.obj U) := by simp def map_comp (f : X ⟶ Y) (g : Y ⟶ Z) : map (f ≫ g) ≅ map g ⋙ map f := { hom := { app := λ U, eq_to_hom (map_comp_obj f g U) }, inv := { app := λ U, eq_to_hom (map_comp_obj f g U).symm } } @[simp] lemma map_comp_hom_app (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (map_comp f g).hom.app U = eq_to_hom (map_comp_obj f g U) := rfl @[simp] lemma map_comp_inv_app (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (map_comp f g).inv.app U = eq_to_hom (map_comp_obj f g U).symm := rfl -- We could make f g implicit here, but it's nice to be able to see when -- they are the identity (often!) def map_iso (f g : X ⟶ Y) (h : f = g) : map f ≅ map g := nat_iso.of_components (λ U, eq_to_iso (congr_fun (congr_arg functor.obj (congr_arg map h)) U) ) (by obviously) @[simp] lemma map_iso_refl (f : X ⟶ Y) (h) : map_iso f f h = iso.refl (map _) := rfl @[simp] lemma map_iso_hom_app (f g : X ⟶ Y) (h : f = g) (U : opens Y) : (map_iso f g h).hom.app U = eq_to_hom (congr_fun (congr_arg functor.obj (congr_arg map h)) U) := rfl @[simp] lemma map_iso_inv_app (f g : X ⟶ Y) (h : f = g) (U : opens Y) : (map_iso f g h).inv.app U = eq_to_hom (congr_fun (congr_arg functor.obj (congr_arg map h.symm)) U) := rfl end topological_space.opens
36feef13c26bed8234d5907dd9b8d155337e23bc
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebraic_geometry/structure_sheaf.lean
624f2f5b7d64faac0f101e83f14ee13a9fbd1630
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
4,118
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Scott Morrison -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebraic_geometry.prime_spectrum import Mathlib.algebra.category.CommRing.colimits import Mathlib.algebra.category.CommRing.limits import Mathlib.topology.sheaves.local_predicate import Mathlib.topology.sheaves.forget import Mathlib.ring_theory.localization import Mathlib.ring_theory.subring import Mathlib.PostPort universes u namespace Mathlib /-! # The structure sheaf on `prime_spectrum R`. We define the structure sheaf on `Top.of (prime_spectrum R)`, for a commutative ring `R`. We define this as a subsheaf of the sheaf of dependent functions into the localizations, cut out by the condition that the function must be locally equal to a ratio of elements of `R`. Because the condition "is equal to a fraction" passes to smaller open subsets, the subset of functions satisfying this condition is automatically a subpresheaf. Because the condition "is locally equal to a fraction" is local, it is also a subsheaf. (It may be helpful to refer back to `topology.sheaves.sheaf_of_functions`, where we show that dependent functions into any type family form a sheaf, and also `topology.sheaves.local_predicate`, where we characterise the predicates which pick out sub-presheaves and sub-sheaves of these sheaves.) We also set up the ring structure, obtaining `structure_sheaf R : sheaf CommRing (Top.of (prime_spectrum R))`. -/ namespace algebraic_geometry /-- $Spec R$, just as a topological space. -/ def Spec.Top (R : Type u) [comm_ring R] : Top := Top.of (prime_spectrum R) namespace structure_sheaf /-- The type family over `prime_spectrum R` consisting of the localization over each point. -/ def localizations (R : Type u) [comm_ring R] (P : ↥(Spec.Top R)) := localization.at_prime (prime_spectrum.as_ideal P) protected instance localizations.inhabited (R : Type u) [comm_ring R] (P : ↥(Spec.Top R)) : Inhabited (localizations R P) := { default := coe_fn (localization_map.to_map (localization.of (ideal.prime_compl (prime_spectrum.as_ideal P)))) 1 } /-- The predicate saying that a dependent function on an open `U` is realised as a fixed fraction `r / s` in each of the stalks (which are localizations at various prime ideals). -/ def is_fraction {R : Type u} [comm_ring R] {U : topological_space.opens ↥(Spec.Top R)} (f : (x : ↥U) → localizations R ↑x) := ∃ (r : R), ∃ (s : R), ∀ (x : ↥U), ¬s ∈ prime_spectrum.as_ideal (subtype.val x) ∧ f x * coe_fn (localization_map.to_map (localization.of (ideal.prime_compl (prime_spectrum.as_ideal ↑x)))) s = coe_fn (localization_map.to_map (localization.of (ideal.prime_compl (prime_spectrum.as_ideal ↑x)))) r /-- The predicate `is_fraction` is "prelocal", in the sense that if it holds on `U` it holds on any open subset `V` of `U`. -/ def is_fraction_prelocal (R : Type u) [comm_ring R] : Top.prelocal_predicate (localizations R) := Top.prelocal_predicate.mk (fun (U : topological_space.opens ↥(Spec.Top R)) (f : (x : ↥U) → localizations R ↑x) => is_fraction f) sorry /-- We will define the structure sheaf as the subsheaf of all dependent functions in `Π x : U, localizations R x` consisting of those functions which can locally be expressed as a ratio of (the images in the localization of) elements of `R`. Quoting Hartshorne: For an open set $$U ⊆ Spec A$$, we define $$𝒪(U)$$ to be the set of functions $$s : U → ⨆_{𝔭 ∈ U} A_𝔭$$, such that $s(𝔭) ∈ A_𝔭$$ for each $$𝔭$$, and such that $$s$$ is locally a quotient of elements of $$A$$: to be precise, we require that for each $$𝔭 ∈ U$$, there is a neighborhood $$V$$ of $$𝔭$$, contained in $$U$$, and elements $$a, f ∈ A$$, such that for each $$𝔮 ∈ V, f ∉ 𝔮$$, and $$s(𝔮) = a/f$$ in $$A_𝔮$$. Now Hartshorne had the disadvantage of not knowing about dependent functions, so we replace his circumlocut
40e53d680366441eebb2633de528bddc2511a0b3
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/linear_algebra/affine_space/midpoint.lean
e1ac95e66a40c5164c7bc1e5a82d0151c4492143
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
7,828
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 algebra.char_p.invertible import linear_algebra.affine_space.affine_equiv /-! # Midpoint of a segment ## Main definitions * `midpoint R x y`: midpoint of the segment `[x, y]`. We define it for `x` and `y` in a module over a ring `R` with invertible `2`. * `add_monoid_hom.of_map_midpoint`: construct an `add_monoid_hom` given a map `f` such that `f` sends zero to zero and midpoints to midpoints. ## Main theorems * `midpoint_eq_iff`: `z` is the midpoint of `[x, y]` if and only if `x + y = z + z`, * `midpoint_unique`: `midpoint R x y` does not depend on `R`; * `midpoint x y` is linear both in `x` and `y`; * `point_reflection_midpoint_left`, `point_reflection_midpoint_right`: `equiv.point_reflection (midpoint R x y)` swaps `x` and `y`. We do not mark most lemmas as `@[simp]` because it is hard to tell which side is simpler. ## Tags midpoint, add_monoid_hom -/ open affine_map affine_equiv section variables (R : Type*) {V V' P P' : Type*} [ring R] [invertible (2:R)] [add_comm_group V] [module R V] [add_torsor V P] [add_comm_group V'] [module R V'] [add_torsor V' P'] include V /-- `midpoint x y` is the midpoint of the segment `[x, y]`. -/ def midpoint (x y : P) : P := line_map x y (⅟2:R) variables {R} {x y z : P} include V' @[simp] lemma affine_map.map_midpoint (f : P →ᵃ[R] P') (a b : P) : f (midpoint R a b) = midpoint R (f a) (f b) := f.apply_line_map a b _ @[simp] lemma affine_equiv.map_midpoint (f : P ≃ᵃ[R] P') (a b : P) : f (midpoint R a b) = midpoint R (f a) (f b) := f.apply_line_map a b _ omit V' @[simp] lemma affine_equiv.point_reflection_midpoint_left (x y : P) : point_reflection R (midpoint R x y) x = y := by rw [midpoint, point_reflection_apply, line_map_apply, vadd_vsub, vadd_vadd, ← add_smul, ← two_mul, mul_inv_of_self, one_smul, vsub_vadd] lemma midpoint_comm (x y : P) : midpoint R x y = midpoint R y x := by rw [midpoint, ← line_map_apply_one_sub, one_sub_inv_of_two, midpoint] @[simp] lemma affine_equiv.point_reflection_midpoint_right (x y : P) : point_reflection R (midpoint R x y) y = x := by rw [midpoint_comm, affine_equiv.point_reflection_midpoint_left] lemma midpoint_vsub_midpoint (p₁ p₂ p₃ p₄ : P) : midpoint R p₁ p₂ -ᵥ midpoint R p₃ p₄ = midpoint R (p₁ -ᵥ p₃) (p₂ -ᵥ p₄) := line_map_vsub_line_map _ _ _ _ _ lemma midpoint_vadd_midpoint (v v' : V) (p p' : P) : midpoint R v v' +ᵥ midpoint R p p' = midpoint R (v +ᵥ p) (v' +ᵥ p') := line_map_vadd_line_map _ _ _ _ _ lemma midpoint_eq_iff {x y z : P} : midpoint R x y = z ↔ point_reflection R z x = y := eq_comm.trans ((injective_point_reflection_left_of_module R x).eq_iff' (affine_equiv.point_reflection_midpoint_left x y)).symm @[simp] lemma midpoint_vsub_left (p₁ p₂ : P) : midpoint R p₁ p₂ -ᵥ p₁ = (⅟2:R) • (p₂ -ᵥ p₁) := line_map_vsub_left _ _ _ @[simp] lemma midpoint_vsub_right (p₁ p₂ : P) : midpoint R p₁ p₂ -ᵥ p₂ = (⅟2:R) • (p₁ -ᵥ p₂) := by rw [midpoint_comm, midpoint_vsub_left] @[simp] lemma left_vsub_midpoint (p₁ p₂ : P) : p₁ -ᵥ midpoint R p₁ p₂ = (⅟2:R) • (p₁ -ᵥ p₂) := left_vsub_line_map _ _ _ @[simp] lemma right_vsub_midpoint (p₁ p₂ : P) : p₂ -ᵥ midpoint R p₁ p₂ = (⅟2:R) • (p₂ -ᵥ p₁) := by rw [midpoint_comm, left_vsub_midpoint] @[simp] lemma midpoint_sub_left (v₁ v₂ : V) : midpoint R v₁ v₂ - v₁ = (⅟2:R) • (v₂ - v₁) := midpoint_vsub_left v₁ v₂ @[simp] lemma midpoint_sub_right (v₁ v₂ : V) : midpoint R v₁ v₂ - v₂ = (⅟2:R) • (v₁ - v₂) := midpoint_vsub_right v₁ v₂ @[simp] lemma left_sub_midpoint (v₁ v₂ : V) : v₁ - midpoint R v₁ v₂ = (⅟2:R) • (v₁ - v₂) := left_vsub_midpoint v₁ v₂ @[simp] lemma right_sub_midpoint (v₁ v₂ : V) : v₂ - midpoint R v₁ v₂ = (⅟2:R) • (v₂ - v₁) := right_vsub_midpoint v₁ v₂ variable (R) lemma midpoint_eq_midpoint_iff_vsub_eq_vsub {x x' y y' : P} : midpoint R x y = midpoint R x' y' ↔ x -ᵥ x' = y' -ᵥ y := by rw [← @vsub_eq_zero_iff_eq V, midpoint_vsub_midpoint, midpoint_eq_iff, point_reflection_apply, vsub_eq_sub, zero_sub, vadd_eq_add, add_zero, neg_eq_iff_neg_eq, neg_vsub_eq_vsub_rev, eq_comm] lemma midpoint_eq_iff' {x y z : P} : midpoint R x y = z ↔ equiv.point_reflection z x = y := midpoint_eq_iff /-- `midpoint` does not depend on the ring `R`. -/ lemma midpoint_unique (R' : Type*) [ring R'] [invertible (2:R')] [module R' V] (x y : P) : midpoint R x y = midpoint R' x y := (midpoint_eq_iff' R).2 $ (midpoint_eq_iff' R').1 rfl @[simp] lemma midpoint_self (x : P) : midpoint R x x = x := line_map_same_apply _ _ @[simp] lemma midpoint_add_self (x y : V) : midpoint R x y + midpoint R x y = x + y := calc midpoint R x y +ᵥ midpoint R x y = midpoint R x y +ᵥ midpoint R y x : by rw midpoint_comm ... = x + y : by rw [midpoint_vadd_midpoint, vadd_eq_add, vadd_eq_add, add_comm, midpoint_self] lemma midpoint_zero_add (x y : V) : midpoint R 0 (x + y) = midpoint R x y := (midpoint_eq_midpoint_iff_vsub_eq_vsub R).2 $ by simp [sub_add_eq_sub_sub_swap] lemma midpoint_eq_smul_add (x y : V) : midpoint R x y = (⅟2 : R) • (x + y) := by rw [midpoint_eq_iff, point_reflection_apply, vsub_eq_sub, vadd_eq_add, sub_add_eq_add_sub, ← two_smul R, smul_smul, mul_inv_of_self, one_smul, add_sub_cancel'] end lemma line_map_inv_two {R : Type*} {V P : Type*} [division_ring R] [char_zero R] [add_comm_group V] [module R V] [add_torsor V P] (a b : P) : line_map a b (2⁻¹:R) = midpoint R a b := rfl lemma line_map_one_half {R : Type*} {V P : Type*} [division_ring R] [char_zero R] [add_comm_group V] [module R V] [add_torsor V P] (a b : P) : line_map a b (1/2:R) = midpoint R a b := by rw [one_div, line_map_inv_two] lemma homothety_inv_of_two {R : Type*} {V P : Type*} [comm_ring R] [invertible (2:R)] [add_comm_group V] [module R V] [add_torsor V P] (a b : P) : homothety a (⅟2:R) b = midpoint R a b := rfl lemma homothety_inv_two {k : Type*} {V P : Type*} [field k] [char_zero k] [add_comm_group V] [module k V] [add_torsor V P] (a b : P) : homothety a (2⁻¹:k) b = midpoint k a b := rfl lemma homothety_one_half {k : Type*} {V P : Type*} [field k] [char_zero k] [add_comm_group V] [module k V] [add_torsor V P] (a b : P) : homothety a (1/2:k) b = midpoint k a b := by rw [one_div, homothety_inv_two] @[simp] lemma pi_midpoint_apply {k ι : Type*} {V : Π i : ι, Type*} {P : Π i : ι, Type*} [field k] [invertible (2:k)] [Π i, add_comm_group (V i)] [Π i, module k (V i)] [Π i, add_torsor (V i) (P i)] (f g : Π i, P i) (i : ι) : midpoint k f g i = midpoint k (f i) (g i) := rfl namespace add_monoid_hom variables (R R' : Type*) {E F : Type*} [ring R] [invertible (2:R)] [add_comm_group E] [module R E] [ring R'] [invertible (2:R')] [add_comm_group F] [module R' F] /-- A map `f : E → F` sending zero to zero and midpoints to midpoints is an `add_monoid_hom`. -/ def of_map_midpoint (f : E → F) (h0 : f 0 = 0) (hm : ∀ x y, f (midpoint R x y) = midpoint R' (f x) (f y)) : E →+ F := { to_fun := f, map_zero' := h0, map_add' := λ x y, calc f (x + y) = f 0 + f (x + y) : by rw [h0, zero_add] ... = midpoint R' (f 0) (f (x + y)) + midpoint R' (f 0) (f (x + y)) : (midpoint_add_self _ _ _).symm ... = f (midpoint R x y) + f (midpoint R x y) : by rw [← hm, midpoint_zero_add] ... = f x + f y : by rw [hm, midpoint_add_self] } @[simp] lemma coe_of_map_midpoint (f : E → F) (h0 : f 0 = 0) (hm : ∀ x y, f (midpoint R x y) = midpoint R' (f x) (f y)) : ⇑(of_map_midpoint R R' f h0 hm) = f := rfl end add_monoid_hom
52e4bbc8dc432c7167d9534d2147e4d70d78b732
4727251e0cd73359b15b664c3170e5d754078599
/src/analysis/convex/quasiconvex.lean
a89dceac86ea5a9fe108bb755f12d56e79e82c9a
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
7,823
lean
/- Copyright (c) 2021 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import analysis.convex.function /-! # Quasiconvex and quasiconcave functions This file defines quasiconvexity, quasiconcavity and quasilinearity of functions, which are generalizations of unimodality and monotonicity. Convexity implies quasiconvexity, concavity implies quasiconcavity, and monotonicity implies quasilinearity. ## Main declarations * `quasiconvex_on 𝕜 s f`: Quasiconvexity of the function `f` on the set `s` with scalars `𝕜`. This means that, for all `r`, `{x ∈ s | f x ≤ r}` is `𝕜`-convex. * `quasiconcave_on 𝕜 s f`: Quasiconcavity of the function `f` on the set `s` with scalars `𝕜`. This means that, for all `r`, `{x ∈ s | r ≤ f x}` is `𝕜`-convex. * `quasilinear_on 𝕜 s f`: Quasilinearity of the function `f` on the set `s` with scalars `𝕜`. This means that `f` is both quasiconvex and quasiconcave. ## TODO Prove that a quasilinear function between two linear orders is either monotone or antitone. This is not hard but quite a pain to go about as there are many cases to consider. ## References * https://en.wikipedia.org/wiki/Quasiconvex_function -/ open function order_dual set variables {𝕜 E F β : Type*} section ordered_semiring variables [ordered_semiring 𝕜] section add_comm_monoid variables [add_comm_monoid E] [add_comm_monoid F] section ordered_add_comm_monoid variables (𝕜) [ordered_add_comm_monoid β] [has_scalar 𝕜 E] (s : set E) (f : E → β) /-- A function is quasiconvex if all its sublevels are convex. This means that, for all `r`, `{x ∈ s | f x ≤ r}` is `𝕜`-convex. -/ def quasiconvex_on : Prop := ∀ r, convex 𝕜 {x ∈ s | f x ≤ r} /-- A function is quasiconcave if all its superlevels are convex. This means that, for all `r`, `{x ∈ s | r ≤ f x}` is `𝕜`-convex. -/ def quasiconcave_on : Prop := ∀ r, convex 𝕜 {x ∈ s | r ≤ f x} /-- A function is quasilinear if it is both quasiconvex and quasiconcave. This means that, for all `r`, the sets `{x ∈ s | f x ≤ r}` and `{x ∈ s | r ≤ f x}` are `𝕜`-convex. -/ def quasilinear_on : Prop := quasiconvex_on 𝕜 s f ∧ quasiconcave_on 𝕜 s f variables {𝕜 s f} lemma quasiconvex_on.dual : quasiconvex_on 𝕜 s f → quasiconcave_on 𝕜 s (to_dual ∘ f) := id lemma quasiconcave_on.dual : quasiconcave_on 𝕜 s f → quasiconvex_on 𝕜 s (to_dual ∘ f) := id lemma quasilinear_on.dual : quasilinear_on 𝕜 s f → quasilinear_on 𝕜 s (to_dual ∘ f) := and.swap lemma convex.quasiconvex_on_of_convex_le (hs : convex 𝕜 s) (h : ∀ r, convex 𝕜 {x | f x ≤ r}) : quasiconvex_on 𝕜 s f := λ r, hs.inter (h r) lemma convex.quasiconcave_on_of_convex_ge (hs : convex 𝕜 s) (h : ∀ r, convex 𝕜 {x | r ≤ f x}) : quasiconcave_on 𝕜 s f := @convex.quasiconvex_on_of_convex_le 𝕜 E βᵒᵈ _ _ _ _ _ _ hs h lemma quasiconvex_on.convex [is_directed β (≤)] (hf : quasiconvex_on 𝕜 s f) : convex 𝕜 s := λ x y hx hy a b ha hb hab, let ⟨z, hxz, hyz⟩ := exists_ge_ge (f x) (f y) in (hf _ ⟨hx, hxz⟩ ⟨hy, hyz⟩ ha hb hab).1 lemma quasiconcave_on.convex [is_directed β (swap (≤))] (hf : quasiconcave_on 𝕜 s f) : convex 𝕜 s := hf.dual.convex end ordered_add_comm_monoid section linear_ordered_add_comm_monoid variables [linear_ordered_add_comm_monoid β] section has_scalar variables [has_scalar 𝕜 E] {s : set E} {f g : E → β} lemma quasiconvex_on.sup (hf : quasiconvex_on 𝕜 s f) (hg : quasiconvex_on 𝕜 s g) : quasiconvex_on 𝕜 s (f ⊔ g) := begin intro r, simp_rw [pi.sup_def, sup_le_iff, ←set.sep_inter_sep], exact (hf r).inter (hg r), end lemma quasiconcave_on.inf (hf : quasiconcave_on 𝕜 s f) (hg : quasiconcave_on 𝕜 s g) : quasiconcave_on 𝕜 s (f ⊓ g) := hf.dual.sup hg lemma quasiconvex_on_iff_le_max : quasiconvex_on 𝕜 s f ↔ convex 𝕜 s ∧ ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → f (a • x + b • y) ≤ max (f x) (f y) := ⟨λ hf, ⟨hf.convex, λ x y hx hy a b ha hb hab, (hf _ ⟨hx, le_max_left _ _⟩ ⟨hy, le_max_right _ _⟩ ha hb hab).2⟩, λ hf r x y hx hy a b ha hb hab, ⟨hf.1 hx.1 hy.1 ha hb hab, (hf.2 hx.1 hy.1 ha hb hab).trans $ max_le hx.2 hy.2⟩⟩ lemma quasiconcave_on_iff_min_le : quasiconcave_on 𝕜 s f ↔ convex 𝕜 s ∧ ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → min (f x) (f y) ≤ f (a • x + b • y) := @quasiconvex_on_iff_le_max 𝕜 E βᵒᵈ _ _ _ _ _ _ lemma quasilinear_on_iff_mem_interval : quasilinear_on 𝕜 s f ↔ convex 𝕜 s ∧ ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → f (a • x + b • y) ∈ interval (f x) (f y) := begin rw [quasilinear_on, quasiconvex_on_iff_le_max, quasiconcave_on_iff_min_le, and_and_and_comm, and_self], apply and_congr_right', simp_rw [←forall_and_distrib, interval, mem_Icc, and_comm], end lemma quasiconvex_on.convex_lt (hf : quasiconvex_on 𝕜 s f) (r : β) : convex 𝕜 {x ∈ s | f x < r} := begin refine λ x y hx hy a b ha hb hab, _, have h := hf _ ⟨hx.1, le_max_left _ _⟩ ⟨hy.1, le_max_right _ _⟩ ha hb hab, exact ⟨h.1, h.2.trans_lt $ max_lt hx.2 hy.2⟩, end lemma quasiconcave_on.convex_gt (hf : quasiconcave_on 𝕜 s f) (r : β) : convex 𝕜 {x ∈ s | r < f x} := hf.dual.convex_lt r end has_scalar section ordered_smul variables [has_scalar 𝕜 E] [module 𝕜 β] [ordered_smul 𝕜 β] {s : set E} {f : E → β} lemma convex_on.quasiconvex_on (hf : convex_on 𝕜 s f) : quasiconvex_on 𝕜 s f := hf.convex_le lemma concave_on.quasiconcave_on (hf : concave_on 𝕜 s f) : quasiconcave_on 𝕜 s f := hf.convex_ge end ordered_smul end linear_ordered_add_comm_monoid end add_comm_monoid section linear_ordered_add_comm_monoid variables [linear_ordered_add_comm_monoid E] [ordered_add_comm_monoid β] [module 𝕜 E] [ordered_smul 𝕜 E] {s : set E} {f : E → β} lemma monotone_on.quasiconvex_on (hf : monotone_on f s) (hs : convex 𝕜 s) : quasiconvex_on 𝕜 s f := hf.convex_le hs lemma monotone_on.quasiconcave_on (hf : monotone_on f s) (hs : convex 𝕜 s) : quasiconcave_on 𝕜 s f := hf.convex_ge hs lemma monotone_on.quasilinear_on (hf : monotone_on f s) (hs : convex 𝕜 s) : quasilinear_on 𝕜 s f := ⟨hf.quasiconvex_on hs, hf.quasiconcave_on hs⟩ lemma antitone_on.quasiconvex_on (hf : antitone_on f s) (hs : convex 𝕜 s) : quasiconvex_on 𝕜 s f := hf.convex_le hs lemma antitone_on.quasiconcave_on (hf : antitone_on f s) (hs : convex 𝕜 s) : quasiconcave_on 𝕜 s f := hf.convex_ge hs lemma antitone_on.quasilinear_on (hf : antitone_on f s) (hs : convex 𝕜 s) : quasilinear_on 𝕜 s f := ⟨hf.quasiconvex_on hs, hf.quasiconcave_on hs⟩ lemma monotone.quasiconvex_on (hf : monotone f) : quasiconvex_on 𝕜 univ f := (hf.monotone_on _).quasiconvex_on convex_univ lemma monotone.quasiconcave_on (hf : monotone f) : quasiconcave_on 𝕜 univ f := (hf.monotone_on _).quasiconcave_on convex_univ lemma monotone.quasilinear_on (hf : monotone f) : quasilinear_on 𝕜 univ f := ⟨hf.quasiconvex_on, hf.quasiconcave_on⟩ lemma antitone.quasiconvex_on (hf : antitone f) : quasiconvex_on 𝕜 univ f := (hf.antitone_on _).quasiconvex_on convex_univ lemma antitone.quasiconcave_on (hf : antitone f) : quasiconcave_on 𝕜 univ f := (hf.antitone_on _).quasiconcave_on convex_univ lemma antitone.quasilinear_on (hf : antitone f) : quasilinear_on 𝕜 univ f := ⟨hf.quasiconvex_on, hf.quasiconcave_on⟩ end linear_ordered_add_comm_monoid end ordered_semiring
bfaa47a3153195150878d4e74e2f9e284b294598
92b50235facfbc08dfe7f334827d47281471333b
/hott/types/pi.hlean
2026cd0ddafbcedc0b8160f2d050a6599fe29cbc
[ "Apache-2.0" ]
permissive
htzh/lean
24f6ed7510ab637379ec31af406d12584d31792c
d70c79f4e30aafecdfc4a60b5d3512199200ab6e
refs/heads/master
1,607,677,731,270
1,437,089,952,000
1,437,089,952,000
37,078,816
0
0
null
1,433,780,956,000
1,433,780,955,000
null
UTF-8
Lean
false
false
9,386
hlean
/- Copyright (c) 2014-15 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Floris van Doorn Partially ported from Coq HoTT Theorems about pi-types (dependent function spaces) -/ import types.sigma arity open eq equiv is_equiv funext sigma namespace pi variables {A A' : Type} {B : A → Type} {B' : A' → Type} {C : Πa, B a → Type} {D : Πa b, C a b → Type} {a a' a'' : A} {b b₁ b₂ : B a} {b' : B a'} {b'' : B a''} {f g : Πa, B a} /- Paths -/ /- Paths [p : f ≈ g] in a function type [Πx:X, P x] are equivalent to functions taking values in path types, [H : Πx:X, f x ≈ g x], or concisely, [H : f ~ g]. This equivalence, however, is just the combination of [apd10] and function extensionality [funext], and as such, [path_forall], et seq. are given in axioms.funext and path: -/ /- Now we show how these things compute. -/ definition apd10_eq_of_homotopy (h : f ~ g) : apd10 (eq_of_homotopy h) ~ h := apd10 (right_inv apd10 h) definition eq_of_homotopy_eta (p : f = g) : eq_of_homotopy (apd10 p) = p := left_inv apd10 p definition eq_of_homotopy_idp (f : Πa, B a) : eq_of_homotopy (λx : A, refl (f x)) = refl f := !eq_of_homotopy_eta /- The identification of the path space of a dependent function space, up to equivalence, is of course just funext. -/ definition eq_equiv_homotopy (f g : Πx, B x) : (f = g) ≃ (f ~ g) := equiv.mk _ !is_equiv_apd definition is_equiv_eq_of_homotopy [instance] (f g : Πx, B x) : is_equiv (@eq_of_homotopy _ _ f g) := is_equiv_inv apd10 definition homotopy_equiv_eq (f g : Πx, B x) : (f ~ g) ≃ (f = g) := equiv.mk _ !is_equiv_eq_of_homotopy /- Transport -/ definition pi_transport (p : a = a') (f : Π(b : B a), C a b) : (transport (λa, Π(b : B a), C a b) p f) ~ (λb, transport (C a') !tr_inv_tr (transportD _ p _ (f (p⁻¹ ▸ b)))) := eq.rec_on p (λx, idp) /- A special case of [transport_pi] where the type [B] does not depend on [A], and so it is just a fixed type [B]. -/ definition pi_transport_constant {C : A → A' → Type} (p : a = a') (f : Π(b : A'), C a b) (b : A') : (transport (λa, Π(b : A'), C a b) p f) b = transport (λa, C a b) p (f b) := eq.rec_on p idp /- Pathovers -/ definition pi_pathover {f : Πb, C a b} {g : Πb', C a' b'} {p : a = a'} (r : Π(b : B a) (b' : B a') (q : b =[p] b'), f b =[apo011 C p q] g b') : f =[p] g := begin cases p, apply pathover_idp_of_eq, apply eq_of_homotopy, intro b, apply eq_of_pathover_idp, apply r end -- a version where C is uncurried, but where the conclusion of r is still a proper pathover -- instead of a heterogenous equality definition pi_pathover' {C : (Σa, B a) → Type} {f : Πb, C ⟨a, b⟩} {g : Πb', C ⟨a', b'⟩} {p : a = a'} (r : Π(b : B a) (b' : B a') (q : b =[p] b'), f b =[dpair_eq_dpair p q] g b') : f =[p] g := begin cases p, apply pathover_idp_of_eq, apply eq_of_homotopy, intro b, apply (@eq_of_pathover_idp _ C), exact (r b b (pathover.idpatho b)), end definition pi_pathover_left {f : Πb, C a b} {g : Πb', C a' b'} {p : a = a'} (r : Π(b : B a), f b =[apo011 C p !pathover_tr] g (p ▸ b)) : f =[p] g := begin cases p, apply pathover_idp_of_eq, apply eq_of_homotopy, intro b, apply eq_of_pathover_idp, apply r end definition pi_pathover_right {f : Πb, C a b} {g : Πb', C a' b'} {p : a = a'} (r : Π(b' : B a'), f (p⁻¹ ▸ b') =[apo011 C p !tr_pathover] g b') : f =[p] g := begin cases p, apply pathover_idp_of_eq, apply eq_of_homotopy, intro b, apply eq_of_pathover_idp, apply r end definition pi_pathover_constant {C : A → A' → Type} {f : Π(b : A'), C a b} {g : Π(b : A'), C a' b} {p : a = a'} (r : Π(b : A'), f b =[p] g b) : f =[p] g := begin cases p, apply pathover_idp_of_eq, apply eq_of_homotopy, intro b, exact eq_of_pathover_idp (r b), end /- Maps on paths -/ /- The action of maps given by lambda. -/ definition ap_lambdaD {C : A' → Type} (p : a = a') (f : Πa b, C b) : ap (λa b, f a b) p = eq_of_homotopy (λb, ap (λa, f a b) p) := begin apply (eq.rec_on p), apply inverse, apply eq_of_homotopy_idp end /- Dependent paths -/ /- with more implicit arguments the conclusion of the following theorem is (Π(b : B a), transportD B C p b (f b) = g (transport B p b)) ≃ (transport (λa, Π(b : B a), C a b) p f = g) -/ definition heq_piD (p : a = a') (f : Π(b : B a), C a b) (g : Π(b' : B a'), C a' b') : (Π(b : B a), p ▸D (f b) = g (p ▸ b)) ≃ (p ▸ f = g) := eq.rec_on p (λg, !homotopy_equiv_eq) g definition heq_pi {C : A → Type} (p : a = a') (f : Π(b : B a), C a) (g : Π(b' : B a'), C a') : (Π(b : B a), p ▸ (f b) = g (p ▸ b)) ≃ (p ▸ f = g) := eq.rec_on p (λg, !homotopy_equiv_eq) g section open sigma sigma.ops /- more implicit arguments: (Π(b : B a), transport C (sigma_eq p idp) (f b) = g (p ▸ b)) ≃ (Π(b : B a), transportD B (λ(a : A) (b : B a), C ⟨a, b⟩) p b (f b) = g (transport B p b)) -/ definition heq_pi_sigma {C : (Σa, B a) → Type} (p : a = a') (f : Π(b : B a), C ⟨a, b⟩) (g : Π(b' : B a'), C ⟨a', b'⟩) : (Π(b : B a), (sigma_eq p !pathover_tr) ▸ (f b) = g (p ▸ b)) ≃ (Π(b : B a), p ▸D (f b) = g (p ▸ b)) := eq.rec_on p (λg, !equiv.refl) g end /- Functorial action -/ variables (f0 : A' → A) (f1 : Π(a':A'), B (f0 a') → B' a') /- The functoriality of [forall] is slightly subtle: it is contravariant in the domain type and covariant in the codomain, but the codomain is dependent on the domain. -/ definition pi_functor : (Π(a:A), B a) → (Π(a':A'), B' a') := (λg a', f1 a' (g (f0 a'))) definition ap_pi_functor {g g' : Π(a:A), B a} (h : g ~ g') : ap (pi_functor f0 f1) (eq_of_homotopy h) = eq_of_homotopy (λa':A', (ap (f1 a') (h (f0 a')))) := begin apply (equiv_rect (@apd10 A B g g')), intro p, clear h, cases p, apply concat, exact (ap (ap (pi_functor f0 f1)) (eq_of_homotopy_idp g)), apply symm, apply eq_of_homotopy_idp end /- Equivalences -/ definition is_equiv_pi_functor [instance] [H0 : is_equiv f0] [H1 : Πa', @is_equiv (B (f0 a')) (B' a') (f1 a')] : is_equiv (pi_functor f0 f1) := begin apply (adjointify (pi_functor f0 f1) (pi_functor f0⁻¹ (λ(a : A) (b' : B' (f0⁻¹ a)), transport B (right_inv f0 a) ((f1 (f0⁻¹ a))⁻¹ b')))), intro h, apply eq_of_homotopy, unfold pi_functor, unfold function.compose, unfold function.id, begin intro a', apply (tr_rev _ (adj f0 a')), apply (transport (λx, f1 a' x = h a') (transport_compose B f0 (left_inv f0 a') _)), apply (tr_rev (λx, x = h a') (fn_tr_eq_tr_fn _ f1 _)), unfold function.compose, apply (tr_rev (λx, left_inv f0 a' ▸ x = h a') (right_inv (f1 _) _)), unfold function.id, apply apd end, begin intro h, apply eq_of_homotopy, intro a, apply (tr_rev (λx, right_inv f0 a ▸ x = h a) (left_inv (f1 _) _)), unfold function.id, apply apd end end definition pi_equiv_pi_of_is_equiv [H : is_equiv f0] [H1 : Πa', @is_equiv (B (f0 a')) (B' a') (f1 a')] : (Πa, B a) ≃ (Πa', B' a') := equiv.mk (pi_functor f0 f1) _ definition pi_equiv_pi (f0 : A' ≃ A) (f1 : Πa', (B (to_fun f0 a') ≃ B' a')) : (Πa, B a) ≃ (Πa', B' a') := pi_equiv_pi_of_is_equiv (to_fun f0) (λa', to_fun (f1 a')) definition pi_equiv_pi_id {P Q : A → Type} (g : Πa, P a ≃ Q a) : (Πa, P a) ≃ (Πa, Q a) := pi_equiv_pi equiv.refl g /- Truncatedness: any dependent product of n-types is an n-type -/ open is_trunc definition is_trunc_pi (B : A → Type) (n : trunc_index) [H : ∀a, is_trunc n (B a)] : is_trunc n (Πa, B a) := begin revert B H, eapply (trunc_index.rec_on n), {intro B H, fapply is_contr.mk, intro a, apply center, intro f, apply eq_of_homotopy, intro x, apply (center_eq (f x))}, {intro n IH B H, fapply is_trunc_succ_intro, intro f g, fapply is_trunc_equiv_closed, apply equiv.symm, apply eq_equiv_homotopy, apply IH, intro a, show is_trunc n (f a = g a), from is_trunc_eq n (f a) (g a)} end local attribute is_trunc_pi [instance] definition is_trunc_eq_pi [instance] [priority 500] (n : trunc_index) (f g : Πa, B a) [H : ∀a, is_trunc n (f a = g a)] : is_trunc n (f = g) := begin apply is_trunc_equiv_closed_rev, apply eq_equiv_homotopy end definition is_hprop_pi_eq [instance] [priority 490] (a : A) : is_hprop (Π(a' : A), a = a') := is_hprop_of_imp_is_contr ( assume (f : Πa', a = a'), assert H : is_contr A, from is_contr.mk a f, _) /- Symmetry of Π -/ definition is_equiv_flip [instance] {P : A → A' → Type} : is_equiv (@function.flip A A' P) := begin fapply is_equiv.mk, exact (@function.flip _ _ (function.flip P)), repeat (intro f; apply idp) end definition pi_comm_equiv {P : A → A' → Type} : (Πa b, P a b) ≃ (Πb a, P a b) := equiv.mk (@function.flip _ _ P) _ end pi attribute pi.is_trunc_pi [instance] [priority 1510]
eb123ebf3bb67eaef4574fb6951322993f1e55e7
98beff2e97d91a54bdcee52f922c4e1866a6c9b9
/src/sub.lean
ca4e7c42e47a9e31b15aaa83bc2d4980e1432e1a
[]
no_license
b-mehta/topos
c3fc43fb04ba16bae1965ce5c26c6461172e5bc6
c9032b11789e36038bc841a1e2b486972421b983
refs/heads/master
1,629,609,492,867
1,609,907,263,000
1,609,907,263,000
240,943,034
43
3
null
1,598,210,062,000
1,581,877,668,000
Lean
UTF-8
Lean
false
false
31,957
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.opposites import category_theory.limits.lattice import category_theory.limits.shapes.finite_products import category_theory.limits.shapes.terminal import category_theory.full_subcategory import category_theory.limits.shapes.regular_mono import category_theory.closed.cartesian import category_theory.limits.shapes.pullbacks import category_theory.limits.over import category_theory.monad.adjunction import category_theory.currying import category_theory.adjunction.fully_faithful import category_theory.skeletal import over universes v v₂ u u₂ noncomputable theory namespace category_theory open category_theory category_theory.category category_theory.limits variables {C : Type u} [category.{v} C] {X Y Z : C} variables {D : Type u₂} [category.{v₂} D] /-- The subobject category as a full subcategory of the over category. In particular this isn't skeletal, so it's not a partial order. The quotient is taken in `subq` instead, it's useful to be able to work with both. It's reducible for now to get instances to happen quickly, marked semireducible again later. -/ @[derive [category, λ t, has_coe t (over X)]] def sub (X : C) := {f : over X // mono f.hom} /-- The inclusion arrow from subobjects to the over category. -/ def forget_sub (X : C) : sub X ⥤ over X := full_subcategory_inclusion _ @[simp] lemma forget_sub_obj_left {f} : ((forget_sub X).obj f).left = f.val.left := rfl /-- Convenience notation for the underlying arrow of a subobject. -/ abbreviation sub.arrow (f : sub X) : _ ⟶ X := ((forget_sub X).obj f).hom @[simp] lemma forget_sub_obj_hom {f} : ((forget_sub X).obj f).hom = f.arrow := rfl instance : full (forget_sub X) := full_subcategory.full _ instance : faithful (forget_sub X) := full_subcategory.faithful _ instance sub_mono (f : sub X) : mono f.arrow := f.property /-- The subobject category is a thin category, which makes defining its skeleton easy. -/ instance is_thin {X : C} (f g : sub X) : subsingleton (f ⟶ g) := ⟨begin intros h₁ h₂, ext1, erw [← cancel_mono g.arrow, over.w h₁, over.w h₂], end⟩ @[reassoc] lemma sub.w {f g : sub X} (k : f ⟶ g) : k.left ≫ g.arrow = f.arrow := over.w _ /-- Convience constructor for a morphism in the subobject category. -/ abbreviation sub.hom_mk {f g : sub X} (h : f.val.left ⟶ g.val.left) (w : h ≫ g.arrow = f.arrow) : f ⟶ g := over.hom_mk h w def sub.iso_mk {f g : sub X} (h : f.val.left ≅ g.val.left) (w : h.hom ≫ g.arrow = f.arrow) : f ≅ g := { hom := sub.hom_mk h.hom w, inv := sub.hom_mk h.inv (by rw [h.inv_comp_eq, w]) } @[derive [partial_order, category]] def subq (X : C) := thin_skeleton (sub X) @[simps] def sub.mk' {X A : C} (f : A ⟶ X) [hf : mono f] : sub X := { val := over.mk f, property := hf } @[simp] lemma sub_mk'_arrow {X A : C} (f : A ⟶ X) [hf : mono f] : (sub.mk' f).arrow = f := rfl abbreviation subq.mk {X A : C} (f : A ⟶ X) [mono f] : subq X := (to_thin_skeleton _).obj (sub.mk' f) @[simps] def restrict_to_sub {Y : D} (F : over Y ⥤ over X) (h : ∀ (f : sub Y), mono (F.obj ((forget_sub Y).obj f)).hom) : sub Y ⥤ sub X := { obj := λ f, ⟨_, h f⟩, map := λ _ _ k, (forget_sub X).preimage ((forget_sub Y ⋙ F).map k), } def restrict_to_sub_iso {Y : D} {F₁ F₂ : over Y ⥤ over X} (h₁ h₂) (i : F₁ ≅ F₂) : restrict_to_sub F₁ h₁ ≅ restrict_to_sub F₂ h₂ := fully_faithful_cancel_right (forget_sub X) (iso_whisker_left (forget_sub Y) i) def restrict_to_sub_comp {X Z : C} {Y : D} (F : over X ⥤ over Y) (G : over Y ⥤ over Z) (h₁ h₂) : restrict_to_sub F h₁ ⋙ restrict_to_sub G h₂ ≅ restrict_to_sub (F ⋙ G) (λ f, h₂ ⟨_, h₁ f⟩) := fully_faithful_cancel_right (forget_sub _) (iso.refl _) def restrict_to_sub_id : restrict_to_sub (𝟭 (over X)) (λ f, f.2) ≅ 𝟭 _ := fully_faithful_cancel_right (forget_sub _) (iso.refl _) @[simp] lemma restrict_comm (F : over Y ⥤ over X) (h : ∀ (f : sub Y), mono (F.obj ((forget_sub Y).obj f)).hom) : restrict_to_sub F h ⋙ forget_sub X = forget_sub Y ⋙ F := rfl def lower_sub {Y : D} (F : sub Y ⥤ sub X) : subq Y ⥤ subq X := thin_skeleton.map F lemma lower_sub_iso (F₁ F₂ : sub X ⥤ sub Y) (h : F₁ ≅ F₂) : lower_sub F₁ = lower_sub F₂ := thin_skeleton.map_iso_eq h def lower_sub₂ (F : sub X ⥤ sub Y ⥤ sub Z) : subq X ⥤ subq Y ⥤ subq Z := thin_skeleton.map₂ F @[simp] lemma lower_comm (F : sub Y ⥤ sub X) : to_thin_skeleton _ ⋙ lower_sub F = F ⋙ to_thin_skeleton _ := rfl def sub.pullback [has_pullbacks.{v} C] (f : X ⟶ Y) : sub Y ⥤ sub X := restrict_to_sub (real_pullback f) begin intro g, apply @pullback.snd_of_mono _ _ _ _ _ _ _ _ _, change mono g.arrow, apply_instance, end def sub.pullback_comp [has_pullbacks.{v} C] (f : X ⟶ Y) (g : Y ⟶ Z) : sub.pullback (f ≫ g) ≅ sub.pullback g ⋙ sub.pullback f := restrict_to_sub_iso _ _ (pullback_comp _ _) ≪≫ (restrict_to_sub_comp _ _ _ _).symm def sub.pullback_id [has_pullbacks.{v} C] : sub.pullback (𝟙 X) ≅ 𝟭 _ := restrict_to_sub_iso _ _ pullback_id ≪≫ restrict_to_sub_id @[simp] lemma sub.pullback_obj_left [has_pullbacks.{v} C] (f : X ⟶ Y) (g : sub Y) : (↑((sub.pullback f).obj g) : over X).left = pullback g.arrow f := rfl @[simp] lemma sub.pullback_obj_arrow [has_pullbacks.{v} C] (f : X ⟶ Y) (g : sub Y) : ((sub.pullback f).obj g).arrow = pullback.snd := rfl def subq.pullback [has_pullbacks.{v} C] (f : X ⟶ Y) : subq Y ⥤ subq X := lower_sub (sub.pullback f) lemma subq.pullback_id [has_pullbacks.{v} C] (x : subq X) : (subq.pullback (𝟙 X)).obj x = x := begin apply quotient.induction_on' x, intro f, apply quotient.sound, exact ⟨sub.pullback_id.app f⟩, end lemma subq.pullback_comp [has_pullbacks.{v} C] (f : X ⟶ Y) (g : Y ⟶ Z) (x : subq Z) : (subq.pullback (f ≫ g)).obj x = (subq.pullback f).obj ((subq.pullback g).obj x) := begin apply quotient.induction_on' x, intro t, apply quotient.sound, refine ⟨(sub.pullback_comp _ _).app t⟩, end attribute [instance] mono_comp def sub.post (f : X ⟶ Y) [mono f] : sub X ⥤ sub Y := restrict_to_sub (over.map f) (λ g, by apply mono_comp g.arrow f) def sub.post_comp (f : X ⟶ Y) (g : Y ⟶ Z) [mono f] [mono g] : sub.post (f ≫ g) ≅ sub.post f ⋙ sub.post g := restrict_to_sub_iso _ _ (over_map_comp _ _) ≪≫ (restrict_to_sub_comp _ _ _ _).symm def sub.post_id : sub.post (𝟙 X) ≅ 𝟭 _ := restrict_to_sub_iso _ _ over_map_id ≪≫ restrict_to_sub_id @[simp] lemma sub.post_obj_left (f : X ⟶ Y) [mono f] (g : sub X) : (↑((sub.post f).obj g) : over Y).left = g.val.left := rfl @[simp] lemma sub.post_obj_arrow (f : X ⟶ Y) [mono f] (g : sub X) : ((sub.post f).obj g).arrow = g.arrow ≫ f := rfl instance sub.full_post (f : X ⟶ Y) [mono f] : full (sub.post f) := { preimage := λ g h e, begin refine sub.hom_mk e.left _, rw [← cancel_mono f, assoc], apply sub.w e, end } instance sub.faithful_post (f : X ⟶ Y) [mono f] : faithful (sub.post f) := {}. def subq.post (f : X ⟶ Y) [mono f] : subq X ⥤ subq Y := lower_sub (sub.post f) lemma subq.post_id (x : subq X) : (subq.post (𝟙 X)).obj x = x := begin apply quotient.induction_on' x, intro f, apply quotient.sound, exact ⟨sub.post_id.app f⟩, end lemma subq.post_comp (f : X ⟶ Y) (g : Y ⟶ Z) [mono f] [mono g] (x : subq X) : (subq.post (f ≫ g)).obj x = (subq.post g).obj ((subq.post f).obj x) := begin apply quotient.induction_on' x, intro t, apply quotient.sound, refine ⟨(sub.post_comp _ _).app t⟩, end @[simps] def sub.image [has_images C] : over X ⥤ sub X := { obj := λ f, sub.mk' (image.ι f.hom), map := λ f g k, begin apply (forget_sub X).preimage _, apply over.hom_mk _ _, refine image.lift {I := image _, m := image.ι g.hom, e := k.left ≫ factor_thru_image g.hom}, apply image.lift_fac, end } def image_forget_adj [has_images C] : sub.image ⊣ forget_sub X := adjunction.mk_of_hom_equiv { hom_equiv := λ f g, { to_fun := λ k, begin apply over.hom_mk (factor_thru_image f.hom ≫ k.left) _, change (factor_thru_image f.hom ≫ k.left) ≫ _ = f.hom, rw [assoc, over.w k], apply image.fac end, inv_fun := λ k, begin refine over.hom_mk _ _, refine image.lift {I := g.val.left, m := g.arrow, e := k.left, fac' := over.w k}, apply image.lift_fac, end, left_inv := λ k, subsingleton.elim _ _, right_inv := λ k, begin ext1, change factor_thru_image _ ≫ image.lift _ = _, rw [← cancel_mono g.arrow, assoc, image.lift_fac, image.fac f.hom], exact (over.w k).symm, end } } instance [has_images C] : is_right_adjoint (forget_sub X) := { left := sub.image, adj := image_forget_adj } instance sub.reflective [has_images C] : reflective (forget_sub X) := {}. def forget_image [has_images C] : forget_sub X ⋙ sub.image ≅ 𝟭 (sub X) := as_iso (adjunction.counit image_forget_adj) def sub.exists [has_images C] (f : X ⟶ Y) : sub X ⥤ sub Y := forget_sub _ ⋙ over.map f ⋙ sub.image def subq.exists [has_images C] (f : X ⟶ Y) : subq X ⥤ subq Y := lower_sub (sub.exists f) instance sub.faithful_pullback (f : X ⟶ Y) [has_pullbacks C] : faithful (sub.pullback f) := {}. instance sub.faithful_exists (f : X ⟶ Y) [has_images C] : faithful (sub.exists f) := {}. def exists_iso_post [has_images C] (f : X ⟶ Y) [mono f] : sub.exists f ≅ sub.post f := nat_iso.of_components begin intro Z, suffices : (forget_sub _).obj ((sub.exists f).obj Z) ≅ (forget_sub _).obj ((sub.post f).obj Z), apply preimage_iso this, apply over_iso _ _, apply image_mono_iso_source (Z.arrow ≫ f), apply image_mono_iso_source_hom_self, end begin intros Z₁ Z₂ g, ext1, change image.lift ⟨_, _, _, _⟩ ≫ (image_mono_iso_source (Z₂.arrow ≫ f)).hom = (image_mono_iso_source (Z₁.arrow ≫ f)).hom ≫ g.left, rw [← cancel_mono (Z₂.arrow ≫ f), assoc, assoc, sub.w_assoc g, image_mono_iso_source_hom_self, image_mono_iso_source_hom_self], apply image.lift_fac, end /-- post is adjoint to pullback for monos -/ def sub.pull_post_adj (f : X ⟶ Y) [mono f] [has_pullbacks C] : sub.post f ⊣ sub.pullback f := adjunction.restrict_fully_faithful (forget_sub X) (forget_sub Y) (radj f) (iso.refl _) (iso.refl _) def thin_skeleton.lower_adjunction [∀ (X Y : C), subsingleton (X ⟶ Y)] [∀ (X Y : D), subsingleton (X ⟶ Y)] (R : D ⥤ C) (L : C ⥤ D) (h : L ⊣ R) : thin_skeleton.map L ⊣ thin_skeleton.map R := adjunction.mk_of_unit_counit { unit := { app := λ X, begin letI := is_isomorphic_setoid C, refine quotient.rec_on_subsingleton X (λ x, hom_of_le ⟨h.unit.app x⟩), -- TODO: make quotient.rec_on_subsingleton' so the letI isn't needed end }, counit := { app := λ X, begin letI := is_isomorphic_setoid D, refine quotient.rec_on_subsingleton X (λ x, hom_of_le ⟨h.counit.app x⟩), end } } def subq.lower_adjunction {A : C} {B : D} {R : sub B ⥤ sub A} {L : sub A ⥤ sub B} (h : L ⊣ R) : lower_sub L ⊣ lower_sub R := thin_skeleton.lower_adjunction _ _ h def subq.pull_post_adj (f : X ⟶ Y) [mono f] [has_pullbacks C] : subq.post f ⊣ subq.pullback f := subq.lower_adjunction (sub.pull_post_adj f) /-- image is adjoint to pullback if images exist -/ -- I really think there should be a high-level proof of this but not sure what it is... def sub.exists_pull_adj (f : X ⟶ Y) [has_images C] [has_pullbacks C] : sub.exists f ⊣ sub.pullback f := adjunction.mk_of_hom_equiv { hom_equiv := λ g h, { to_fun := λ k, sub.hom_mk (begin refine pullback.lift (factor_thru_image _ ≫ k.1) g.arrow _, rw [assoc, sub.w k], apply image.fac, end) (pullback.lift_snd _ _ _), inv_fun := λ k, sub.hom_mk (image.lift ⟨_, h.arrow, k.left ≫ pullback.fst, by { rw [assoc, pullback.condition], apply sub.w_assoc }⟩) (image.lift_fac _), left_inv := λ k, subsingleton.elim _ _, right_inv := λ k, subsingleton.elim _ _ } } def subq.exists_pull_adj (f : X ⟶ Y) [has_pullbacks C] [has_images C] : subq.exists f ⊣ subq.pullback f := subq.lower_adjunction (sub.exists_pull_adj f) -- Is this actually necessary? def factors_through {X Y Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) : Prop := nonempty (over.mk f ⟶ over.mk g) lemma factors_through_iff_le {X Y Z : C} (f : X ⟶ Z) (g : Y ⟶ Z) [mono f] [mono g] : factors_through f g ↔ subq.mk f ≤ subq.mk g := iff.rfl instance {X : C} : has_top (sub X) := { top := sub.mk' (𝟙 _) } def to_top (f : sub X) : f ⟶ ⊤ := sub.hom_mk f.arrow (comp_id _) instance subq.order_top {X : C} : order_top (subq X) := { top := quotient.mk' ⊤, le_top := begin refine quotient.ind' (λ f, _), exact ⟨to_top f⟩, end, ..category_theory.subq.partial_order X} @[simp] lemma top_left (X : C) : (⊤ : sub X).val.left = X := rfl @[simp] lemma top_arrow (X : C) : (⊤ : sub X).arrow = 𝟙 X := rfl def sub.post_top (f : X ⟶ Y) [mono f] : (sub.post f).obj ⊤ ≅ sub.mk' f := iso_of_both_ways (sub.hom_mk (𝟙 _) rfl) (sub.hom_mk (𝟙 _) (by simp [id_comp f])) def subq.post_top (f : X ⟶ Y) [mono f] : (subq.post f).obj ⊤ = quotient.mk' (sub.mk' f) := quotient.sound' ⟨sub.post_top f⟩ def sub.pullback_top (f : X ⟶ Y) [has_pullbacks C] : (sub.pullback f).obj ⊤ ≅ ⊤ := iso_of_both_ways (to_top _) (sub.hom_mk (pullback.lift f (𝟙 _) (by tidy)) (pullback.lift_snd _ _ _)) def subq.pullback_top (f : X ⟶ Y) [has_pullbacks C] : (subq.pullback f).obj ⊤ = ⊤ := quotient.sound' ⟨sub.pullback_top f⟩ variable (C) @[simps] def subq.functor [has_pullbacks.{v} C] : Cᵒᵖ ⥤ Type (max u v) := { obj := λ X, subq X.unop, map := λ X Y f, (subq.pullback f.unop).obj, map_id' := λ X, funext subq.pullback_id, map_comp' := λ X Y Z f g, funext (subq.pullback_comp _ _) } variable {C} @[simps] def postcompose_sub_equiv_of_iso (e : X ≅ Y) : subq X ≃ subq Y := { to_fun := (subq.post e.hom).obj, inv_fun := (subq.post e.inv).obj, left_inv := λ g, by simp_rw [← subq.post_comp, e.hom_inv_id, subq.post_id], right_inv := λ g, by simp_rw [← subq.post_comp, e.inv_hom_id, subq.post_id] } -- lemma postcompose_pullback_comm' [has_pullbacks.{v} C] {X Y Z W : C} {f : X ⟶ Y} {g : X ⟶ Z} {h : Y ⟶ W} {k : Z ⟶ W} [mono h] [mono g] -- {comm : f ≫ h = g ≫ k} (t : is_limit (pullback_cone.mk f g comm)) (a) : -- (sub.post g).obj ((sub.pullback f).obj a) ≈ (sub.pullback k).obj ((sub.post h).obj a) := -- begin -- apply equiv_of_both_ways, -- { refine sub.hom_mk (pullback.lift pullback.fst _ _) (pullback.lift_snd _ _ _), -- change _ ≫ a.arrow ≫ h = (pullback.snd ≫ g) ≫ _, -- rw [assoc, ← comm, pullback.condition_assoc] }, -- { refine sub.hom_mk (pullback.lift pullback.fst -- (pullback_cone.is_limit.lift' t (pullback.fst ≫ a.arrow) pullback.snd _).1 -- (pullback_cone.is_limit.lift' _ _ _ _).2.1.symm) _, -- { rw [← pullback.condition, assoc], refl }, -- { erw [pullback.lift_snd_assoc], apply (pullback_cone.is_limit.lift' _ _ _ _).2.2 } } -- end lemma postcompose_pullback_comm [has_pullbacks.{v} C] {X Y Z W : C} {f : X ⟶ Y} {g : X ⟶ Z} {h : Y ⟶ W} {k : Z ⟶ W} [mono h] [mono g] (comm : f ≫ h = g ≫ k) (t : is_limit (pullback_cone.mk f g comm)) : ∀ p, (subq.post g).obj ((subq.pullback f).obj p) = (subq.pullback k).obj ((subq.post h).obj p) := begin apply quotient.ind', intro a, apply quotient.sound, apply thin_skeleton.equiv_of_both_ways, { refine sub.hom_mk (pullback.lift pullback.fst _ _) (pullback.lift_snd _ _ _), change _ ≫ a.arrow ≫ h = (pullback.snd ≫ g) ≫ _, rw [assoc, ← comm, pullback.condition_assoc] }, { refine sub.hom_mk (pullback.lift pullback.fst (pullback_cone.is_limit.lift' t (pullback.fst ≫ a.arrow) pullback.snd _).1 (pullback_cone.is_limit.lift' _ _ _ _).2.1.symm) _, { rw [← pullback.condition, assoc], refl }, { dsimp, rw [pullback.lift_snd_assoc], apply (pullback_cone.is_limit.lift' _ _ _ _).2.2 } } end lemma sub.pull_post_self [has_pullbacks.{v} C] (f : X ⟶ Y) [mono f] (g₁ : sub X) : sub.post f ⋙ sub.pullback f ≅ 𝟭 _ := (as_iso (sub.pull_post_adj f).unit).symm lemma subq.pull_post_self [has_pullbacks.{v} C] (f : X ⟶ Y) [mono f] : ∀ g₁, (subq.pullback f).obj ((subq.post f).obj g₁) = g₁ := begin apply quotient.ind, intro g, apply quotient.sound, exact ⟨(sub.pull_post_self f g).app _⟩, end instance over_mono {B : C} {f g : over B} (m : f ⟶ g) [mono m] : mono m.left := ⟨λ A h k e, begin let A' : over B := over.mk (k ≫ f.hom), have: h ≫ f.hom = k ≫ f.hom, rw ← over.w m, rw reassoc_of e, let h' : A' ⟶ f := over.hom_mk h, let k' : A' ⟶ f := over.hom_mk k, have : h' ≫ m = k' ≫ m := over.over_morphism.ext e, rw cancel_mono m at this, injection this end⟩ def over_mono' {B : C} {f g : over B} (m : f ⟶ g) [mono m.left] : mono m := {right_cancellation := λ A h k e, over.over_morphism.ext ((cancel_mono m.left).1 (congr_arg comma_morphism.left e))} @[simps] def preorder_functor {α β : Type*} [preorder α] [preorder β] (f : α → β) (hf : monotone f) : α ⥤ β := { obj := f, map := λ X Y ⟨⟨h⟩⟩, ⟨⟨hf h⟩⟩ } @[simps] def preorder_equivalence {α β : Type*} [preorder α] [preorder β] (f : α ≃o β) : α ≌ β := { functor := preorder_functor f (λ x y h, by rwa [← rel_iso.map_rel_iff f]), inverse := preorder_functor f.symm (λ x y h, by rwa [← rel_iso.map_rel_iff f.symm]), unit_iso := nat_iso.of_components (λ X, eq_to_iso (f.left_inv _).symm) (λ X Y f, rfl), counit_iso := nat_iso.of_components (λ X, eq_to_iso (f.right_inv _)) (λ X Y f, rfl) } instance iso_term (A : C) [has_terminal (over A)] : is_iso (⊤_ over A).hom := begin let := (⊤_ over A).hom, dsimp at this, let ident : over A := over.mk (𝟙 A), let k : ident ⟶ (⊤_ over A) := default _, haveI : split_epi (⊤_ over A).hom := ⟨k.left, over.w k⟩, let l : (⊤_ over A) ⟶ ident := over.hom_mk (⊤_ over A).hom (comp_id _), haveI : mono l := ⟨λ _ _ _ _, subsingleton.elim _ _⟩, haveI : mono (⊤_ over A).hom := category_theory.over_mono l, apply is_iso_of_mono_of_split_epi, end def sub_iso {A B : C} (e : A ≅ B) : sub A ≌ sub B := { functor := sub.post e.hom, inverse := sub.post e.inv, unit_iso := ((sub.post_comp _ _).symm ≪≫ eq_to_iso (by simp) ≪≫ sub.post_id).symm, counit_iso := ((sub.post_comp _ _).symm ≪≫ eq_to_iso (by simp) ≪≫ sub.post_id) } def sub_slice {A : C} {f : over A} (h₁ h₂) : sub f ≌ sub f.left := { functor := restrict_to_sub f.iterated_slice_equiv.functor h₁, inverse := restrict_to_sub f.iterated_slice_equiv.inverse h₂, unit_iso := restrict_to_sub_id.symm ≪≫ restrict_to_sub_iso _ _ f.iterated_slice_equiv.unit_iso ≪≫ (restrict_to_sub_comp _ _ _ _).symm, counit_iso := restrict_to_sub_comp _ _ _ _ ≪≫ restrict_to_sub_iso _ _ f.iterated_slice_equiv.counit_iso ≪≫ restrict_to_sub_id } @[simps] def subq.equiv {A : C} {B : D} (e : sub A ≌ sub B) : subq A ≌ subq B := { functor := lower_sub e.functor, inverse := lower_sub e.inverse, unit_iso := begin apply eq_to_iso, convert thin_skeleton.map_iso_eq e.unit_iso, { exact thin_skeleton.map_id_eq.symm }, { exact (thin_skeleton.map_comp_eq _ _).symm }, end, counit_iso := begin apply eq_to_iso, convert thin_skeleton.map_iso_eq e.counit_iso, { exact (thin_skeleton.map_comp_eq _ _).symm }, { exact thin_skeleton.map_id_eq.symm }, end } def sub_one_over (A : C) [has_terminal (over A)] : subq A ≌ subq (⊤_ (over A)) := begin refine subq.equiv ((sub_iso (as_iso (⊤_ over A).hom).symm).trans (sub_slice _ _).symm), intro f, dsimp, apply_instance, intro f, apply over_mono' _, dsimp, apply_instance, end @[simps] def sub.intersection [has_pullbacks.{v} C] {A : C} : sub A ⥤ sub A ⥤ sub A := { obj := λ f, sub.pullback f.arrow ⋙ sub.post f.arrow, map := λ f₁ f₂ k, { app := λ g, begin apply sub.hom_mk _ _, apply pullback.lift pullback.fst (pullback.snd ≫ k.left) _, rw [pullback.condition, assoc, sub.w k], dsimp, rw [pullback.lift_snd_assoc, assoc, sub.w k], end } }. def sub.inter_le_left [has_pullbacks.{v} C] {A : C} (f g : sub A) : (sub.intersection.obj f).obj g ⟶ f := sub.hom_mk _ rfl def sub.inter_le_right [has_pullbacks.{v} C] {A : C} (f g : sub A) : (sub.intersection.obj f).obj g ⟶ g := sub.hom_mk _ pullback.condition def sub.le_inter [has_pullbacks.{v} C] {A : C} (f g h : sub A) : (h ⟶ f) → (h ⟶ g) → (h ⟶ (sub.intersection.obj f).obj g) := begin intros k₁ k₂, refine sub.hom_mk (pullback.lift k₂.left k₁.left _) _, rw [sub.w k₁, sub.w k₂], erw [pullback.lift_snd_assoc, sub.w k₁], end def subq.intersection [has_pullbacks.{v} C] {A : C} : subq A ⥤ subq A ⥤ subq A := thin_skeleton.map₂ sub.intersection lemma subq.inf_le_left [has_pullbacks.{v} C] {A : C} (f g : subq A) : (subq.intersection.obj f).obj g ≤ f := quotient.induction_on₂' f g (λ a b, ⟨sub.inter_le_left _ _⟩) lemma subq.inf_le_right [has_pullbacks.{v} C] {A : C} (f g : subq A) : (subq.intersection.obj f).obj g ≤ g := quotient.induction_on₂' f g (λ a b, ⟨sub.inter_le_right _ _⟩) lemma subq.le_inf [has_pullbacks.{v} C] {A : C} (h f g : subq A) : h ≤ f → h ≤ g → h ≤ (subq.intersection.obj f).obj g := quotient.induction_on₃' h f g begin rintros f g h ⟨k⟩ ⟨l⟩, exact ⟨sub.le_inter _ _ _ k l⟩, end @[simps] def over.coprod' [has_finite_coproducts.{v} C] {A : C} : over A → over A ⥤ over A := λ f, { obj := λ g, over.mk (coprod.desc f.hom g.hom), map := λ g₁ g₂ k, over.hom_mk (coprod.map (𝟙 _) k.left) } @[simps] def over.coprod [has_finite_coproducts.{v} C] {A : C} : over A ⥤ over A ⥤ over A := { obj := λ f, over.coprod' f, map := λ f₁ f₂ k, { app := λ g, over.hom_mk (coprod.map k.left (𝟙 _)) (by { dsimp, rw [coprod.map_desc, id_comp, over.w k] }), naturality' := λ f g k, -- tidy can do this but it takes ages begin ext1, dsimp, simp, end }, map_id' := λ X, begin ext; dsimp; simp, end, map_comp' := λ X Y Z f g, begin ext; dsimp; simp end }. def sub.union [has_images.{v} C] [has_finite_coproducts.{v} C] {A : C} : sub A ⥤ sub A ⥤ sub A := curry_obj ((forget_sub A).prod (forget_sub A) ⋙ uncurry.obj over.coprod ⋙ sub.image) def sub.le_union_left [has_images.{v} C] [has_finite_coproducts.{v} C] {A : C} (f g : sub A) : f ⟶ (sub.union.obj f).obj g := begin refine sub.hom_mk (coprod.inl ≫ factor_thru_image _) _, erw [assoc, image.fac, coprod.inl_desc], refl, end def sub.le_union_right [has_images.{v} C] [has_finite_coproducts.{v} C] {A : C} (f g : sub A) : g ⟶ (sub.union.obj f).obj g := begin refine sub.hom_mk (coprod.inr ≫ factor_thru_image _) _, erw [assoc, image.fac, coprod.inr_desc], refl, end def sub.union_le [has_images.{v} C] [has_finite_coproducts.{v} C] {A : C} (f g h : sub A) : (f ⟶ h) → (g ⟶ h) → ((sub.union.obj f).obj g ⟶ h) := begin intros k₁ k₂, refine sub.hom_mk _ _, apply image.lift ⟨_, h.arrow, coprod.desc k₁.left k₂.left, _⟩, { dsimp, ext1, { simp [sub.w k₁] }, { simp [sub.w k₂] } }, { apply image.lift_fac } end def subq.union [has_images.{v} C] [has_finite_coproducts.{v} C] {A : C} : subq A ⥤ subq A ⥤ subq A := thin_skeleton.map₂ sub.union lemma sub.intersection_eq_post_pull [has_pullbacks.{v} C] {A : C} (f₁ f₂ : sub A) : (sub.intersection.obj f₁).obj f₂ = (sub.post f₁.arrow).obj ((sub.pullback f₁.arrow).obj f₂) := rfl lemma subq.intersection_eq_post_pull [has_pullbacks.{v} C] {A : C} (f₁ : sub A) (f₂ : subq A) : (subq.intersection.obj (quotient.mk' f₁)).obj f₂ = (subq.post f₁.arrow).obj ((subq.pullback f₁.arrow).obj f₂) := begin apply quotient.induction_on' f₂, intro f₂, refl, end instance [has_pullbacks.{v} C] {B : C} : semilattice_inf_top (subq B) := { inf := λ m n, (subq.intersection.obj m).obj n, inf_le_left := subq.inf_le_left, inf_le_right := subq.inf_le_right, le_inf := subq.le_inf, ..category_theory.subq.order_top } lemma subq.inf_eq_post_pull [has_pullbacks.{v} C] {A : C} (f₁ : sub A) (f₂ : subq A) : (quotient.mk' f₁ ⊓ f₂ : subq A) = (subq.post f₁.arrow).obj ((subq.pullback f₁.arrow).obj f₂) := begin apply quotient.induction_on' f₂, intro f₂, refl, end instance [has_finite_coproducts.{v} C] [has_images.{v} C] {B : C} : semilattice_sup (subq B) := { sup := λ m n, (subq.union.obj m).obj n, le_sup_left := λ m n, quotient.induction_on₂' m n (λ a b, ⟨sub.le_union_left _ _⟩), le_sup_right := λ m n, quotient.induction_on₂' m n (λ a b, ⟨sub.le_union_right _ _⟩), sup_le := λ m n k, quotient.induction_on₃' m n k (λ a b c ⟨i⟩ ⟨j⟩, ⟨sub.union_le _ _ _ i j⟩), ..category_theory.subq.partial_order B } lemma prod_eq_inter {A : C} [has_pullbacks.{v} C] {f₁ f₂ : subq A} : (f₁ ⨯ f₂) = f₁ ⊓ f₂ := le_antisymm (le_inf (le_of_hom limits.prod.fst) (le_of_hom limits.prod.snd)) (le_of_hom (prod.lift (hom_of_le inf_le_left) (hom_of_le inf_le_right))) lemma inf_eq_intersection {B : C} (m m' : subq B) [has_pullbacks.{v} C] : m ⊓ m' = (subq.intersection.obj m).obj m' := rfl lemma top_eq_id {B : C} : (⊤ : subq B) = subq.mk (𝟙 B) := rfl /-- Intersection plays well with pullback. -/ lemma inf_pullback [has_pullbacks.{v} C] {X Y : C} (g : X ⟶ Y) (f₂) : ∀ f₁, (subq.pullback g).obj (f₁ ⊓ f₂) = (subq.pullback g).obj f₁ ⊓ (subq.pullback g).obj f₂ := quotient.ind' begin intro f₁, erw [inf_eq_intersection, inf_eq_intersection, subq.intersection_eq_post_pull, subq.intersection_eq_post_pull, ← subq.pullback_comp, ← postcompose_pullback_comm pullback.condition (cone_is_pullback f₁.arrow g), ← subq.pullback_comp, pullback.condition], refl, end lemma inf_post [has_pullbacks.{v} C] {X Y : C} (g : Y ⟶ X) [mono g] (f₂) : ∀ f₁, (subq.post g).obj (f₁ ⊓ f₂) = (subq.post g).obj f₁ ⊓ (subq.post g).obj f₂ := quotient.ind' begin intro f₁, erw [inf_eq_intersection, inf_eq_intersection, subq.intersection_eq_post_pull, subq.intersection_eq_post_pull, ← subq.post_comp], dsimp, rw [subq.pullback_comp, subq.pull_post_self], end def sub.top_le_pullback_self {A B : C} (f : A ⟶ B) [mono f] [has_pullbacks.{v} C] : (⊤ : sub A) ⟶ (sub.pullback f).obj (sub.mk' f) := sub.hom_mk _ (pullback.lift_snd _ _ rfl) def sub.pullback_self {A B : C} (f : A ⟶ B) [mono f] [has_pullbacks.{v} C] : (sub.pullback f).obj (sub.mk' f) ≅ ⊤ := iso_of_both_ways (to_top _) (sub.top_le_pullback_self _) lemma subq.pullback_self {A B : C} (f : A ⟶ B) [mono f] [has_pullbacks.{v} C] : (subq.pullback f).obj (subq.mk f) = ⊤ := quotient.sound' ⟨sub.pullback_self f⟩ section variable [has_binary_products.{v} C] instance mono_prod_lift_of_left {X Y Z : C} (f : X ⟶ Y) (g : X ⟶ Z) [mono f] : mono (limits.prod.lift f g) := begin split, intros W h k l, have := l =≫ limits.prod.fst, simp at this, rwa cancel_mono at this, end instance mono_prod_lift_of_right {X Y Z : C} (f : X ⟶ Y) (g : X ⟶ Z) [mono g] : mono (limits.prod.lift f g) := begin split, intros W h k l, have := l =≫ limits.prod.snd, simp at this, rwa cancel_mono at this, end end section variable [has_finite_products.{v} C] instance subterminal_ideal {A B : C} [exponentiable B] [mono (default (A ⟶ ⊤_ C))] : mono (default (A^^B ⟶ ⊤_ C)) := ⟨λ Z f g eq, begin apply uncurry_injective, rw ← cancel_mono (default (A ⟶ ⊤_ C)), apply subsingleton.elim, end⟩ /-- Auxiliary def for the exponential in the subobject category `sub 1`. -/ def sub.exp_aux (A : C) [exponentiable A] : sub (⊤_ C) ⥤ sub (⊤_ C) := { obj := λ f, { val := over.mk (default (f.val.left^^A ⟶ ⊤_ C)), property := ⟨λ Z g h eq, uncurry_injective (by { rw ← cancel_mono f.arrow, apply subsingleton.elim })⟩ }, map := λ f₁ f₂ h, sub.hom_mk ((exp A).map h.left) (subsingleton.elim _ _) } @[simps] def sub.exp_aux_left {A₁ A₂ : C} [exponentiable A₁] [exponentiable A₂] (f : A₁ ⟶ A₂) : sub.exp_aux A₂ ⟶ sub.exp_aux A₁ := { app := λ g, sub.hom_mk (pre _ f) (subsingleton.elim _ _) } lemma sub_exp_aux_left_comp {A₁ A₂ A₃ : C} [cartesian_closed C] (f : A₁ ⟶ A₂) (g : A₂ ⟶ A₃) : sub.exp_aux_left (f ≫ g) = sub.exp_aux_left g ≫ sub.exp_aux_left f := begin ext : 3, apply pre_map, end lemma sub_exp_aux_left_id {A₁ : C} [cartesian_closed C] : sub.exp_aux_left (𝟙 A₁) = 𝟙 _ := begin ext : 3, apply pre_id, end /-- Candidate for the exponential functor in sub 1. -/ def sub.exp (f : sub (⊤_ C)) [cartesian_closed C] : sub (⊤_ C) ⥤ sub (⊤_ C) := sub.exp_aux f.val.left end variable [has_finite_limits.{v} C] local attribute [instance] has_finite_products_of_has_finite_limits def sub.exp_equiv [cartesian_closed C] (f₁ f₂ f₃ : sub (⊤_ C)) : ((sub.intersection.obj f₂).obj f₁ ⟶ f₃) ≃ (f₁ ⟶ (sub.exp f₂).obj f₃) := { to_fun := λ k, begin refine sub.hom_mk (cartesian_closed.curry _) (subsingleton.elim _ _), apply (pullback.lift limits.prod.snd limits.prod.fst _) ≫ k.left, dsimp, apply subsingleton.elim, end, inv_fun := λ k, sub.hom_mk (prod.lift pullback.snd pullback.fst ≫ cartesian_closed.uncurry k.left) (subsingleton.elim _ _), left_inv := λ x, subsingleton.elim _ _, right_inv := λ x, subsingleton.elim _ _ } def subq.exp_aux [cartesian_closed C] (f : sub (⊤_ C)) : subq (⊤_ C) ⥤ subq (⊤_ C) := lower_sub (sub.exp f) def subq.exp (f : subq (⊤_ C)) [cartesian_closed C] : subq (⊤_ C) ⥤ subq (⊤_ C) := begin apply quotient.lift_on' f subq.exp_aux _, rintros f₁ f₂ ⟨h⟩, apply lower_sub_iso, have hi : h.hom.left ≫ h.inv.left = 𝟙 _, change (h.hom ≫ h.inv).left = _, rw h.hom_inv_id, refl, have ih : h.inv.left ≫ h.hom.left = 𝟙 _, change (h.inv ≫ h.hom).left = _, rw h.inv_hom_id, refl, refine ⟨sub.exp_aux_left h.inv.left, sub.exp_aux_left h.hom.left, _, _⟩, rw [← sub_exp_aux_left_comp, hi, sub_exp_aux_left_id], exact rfl, rw [← sub_exp_aux_left_comp, ih, sub_exp_aux_left_id], exact rfl, end variable (C) def top_cc [cartesian_closed C] : cartesian_closed (subq (⊤_ C)) := { closed := λ f₁, { is_adj := { right := subq.exp f₁, adj := adjunction.mk_of_hom_equiv { hom_equiv := λ f₂ f₃, begin change (_ ⨯ _ ⟶ _) ≃ (_ ⟶ _), rw prod_eq_inter, apply @@quotient.rec_on_subsingleton₂ (is_isomorphic_setoid _) (is_isomorphic_setoid _) _ _ f₁ f₂, intros f₁ f₂, apply @@quotient.rec_on_subsingleton (is_isomorphic_setoid _) _ _ f₃, intro f₃, refine ⟨_, _, _, _⟩, { rintro k, refine ⟨⟨_⟩⟩, rcases k with ⟨⟨⟨k⟩⟩⟩, refine ⟨sub.exp_equiv _ _ _ k⟩ }, { rintro k, refine ⟨⟨_⟩⟩, rcases k with ⟨⟨⟨k⟩⟩⟩, refine ⟨(sub.exp_equiv _ _ _).symm k⟩ }, { tidy }, { tidy }, { tidy }, { tidy } end } } } } end category_theory
cc0a9c705377f4b2c44094b038d65fab205671dd
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/tactic/suggest.lean
b7f975f0263d04089875f497c8056a50edba191c
[ "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
15,556
lean
/- Copyright (c) 2019 Lucas Allen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Lucas Allen and Scott Morrison -/ import data.mllist import tactic.solve_by_elim /-! # `suggest` and `library_search` `suggest` and `library_search` are a pair of tactics for applying lemmas from the library to the current goal. * `suggest` prints a list of `exact ...` or `refine ...` statements, which may produce new goals * `library_search` prints a single `exact ...` which closes the goal, or fails -/ namespace tactic open native namespace suggest /-- compute the head symbol of an expression, but normalise `>` to `<` and `≥` to `≤` -/ -- We may want to tweak this further? meta def head_symbol : expr → name | (expr.pi _ _ _ t) := head_symbol t | (expr.app f _) := head_symbol f | (expr.const n _) := -- TODO this is a hack; if you suspect more cases here would help, please report them match n with | `gt := `has_lt.lt | `ge := `has_le.le | _ := n end | _ := `_ /-- A declaration can match the head symbol of the current goal in four possible ways: * `ex` : an exact match * `mp` : the declaration returns an `iff`, and the right hand side matches the goal * `mpr` : the declaration returns an `iff`, and the left hand side matches the goal * `both`: the declaration returns an `iff`, and the both sides match the goal -/ @[derive decidable_eq, derive inhabited] inductive head_symbol_match | ex | mp | mpr | both open head_symbol_match /-- a textual representation of a `head_symbol_match`, for trace debugging. -/ def head_symbol_match.to_string : head_symbol_match → string | ex := "exact" | mp := "iff.mp" | mpr := "iff.mpr" | both := "iff.mp and iff.mpr" /-- When we are determining if a given declaration is potentially relevant for the current goal, we compute `unfold_head_symbol` on the head symbol of the declaration, producing a list of names. We consider the declaration potentially relevant if the head symbol of the goal appears in this list. -/ -- This is a hack. meta def unfold_head_symbol : name → list name | `false := [`not, `false] | n := [n] /-- Determine if, and in which way, a given expression matches the specified head symbol. -/ meta def match_head_symbol (hs : name) : expr → option head_symbol_match | (expr.pi _ _ _ t) := match_head_symbol t | `(%%a ↔ %%b) := if `iff = hs then some ex else match (match_head_symbol a, match_head_symbol b) with | (some ex, some ex) := some both | (some ex, _) := some mpr | (_, some ex) := some mp | _ := none end | (expr.app f _) := match_head_symbol f | (expr.const n _) := if list.mem hs (unfold_head_symbol n) then some ex else none | _ := none meta structure decl_data := (d : declaration) (n : name) (m : head_symbol_match) (l : ℕ) -- cached length of name /-- Generate a `decl_data` from the given declaration if it matches the head symbol `hs` for the current goal. -/ -- We used to check here for private declarations, or declarations with certain suffixes. -- It turns out `apply` is so fast, it's better to just try them all. meta def process_declaration (hs : name) (d : declaration) : option decl_data := let n := d.to_name in if ¬ d.is_trusted ∨ n.is_internal then none else (λ m, ⟨d, n, m, n.length⟩) <$> match_head_symbol hs d.type /-- Retrieve all library definitions with a given head symbol. -/ meta def library_defs (hs : name) : tactic (list decl_data) := do env ← get_env, return $ env.decl_filter_map (process_declaration hs) /-- Apply the lemma `e`, then attempt to close all goals using `solve_by_elim { discharger := discharger }`, failing if `close_goals = tt` and there are any goals remaining. -/ -- Implementation note: as this is used by both `library_search` and `suggest`, -- we first run `solve_by_elim` separately on a subset of the goals, -- whether or not `close_goals` is set, -- and then if `close_goals = tt`, require that `solve_by_elim { all_goals := tt }` succeeds -- on the remaining goals. meta def apply_and_solve (close_goals : bool) (discharger : tactic unit) (e : expr) : tactic unit := apply e >> -- Phase 1 -- Run `solve_by_elim` on each "safe" goal separately, not worrying about failures. -- (We only attempt the "safe" goals in this way in Phase 1. In Phase 2 we will do -- backtracking search across all goals, allowing us to guess solutions that involve data, or -- unify metavariables, but only as long as we can finish all goals.) try (any_goals (independent_goal >> solve_by_elim { discharger := discharger })) >> -- Phase 2 (done <|> -- If there were any goals that we did not attempt solving in the first phase -- (because they weren't propositional, or contained a metavariable) -- as a second phase we attempt to solve all remaining goals at once -- (with backtracking across goals). any_goals (success_if_fail independent_goal) >> solve_by_elim { backtrack_all_goals := tt, discharger := discharger } <|> -- and fail unless `close_goals = ff` guard ¬ close_goals) /-- Apply the declaration `d` (or the forward and backward implications separately, if it is an `iff`), and then attempt to solve the goal using `apply_and_solve`. -/ meta def apply_declaration (close_goals : bool) (discharger : tactic unit) (d : decl_data) : tactic unit := let tac := apply_and_solve close_goals discharger in do (e, t) ← decl_mk_const d.d, match d.m with | ex := tac e | mp := do l ← iff_mp_core e t, tac l | mpr := do l ← iff_mpr_core e t, tac l | both := (do l ← iff_mp_core e t, tac l) <|> (do l ← iff_mpr_core e t, tac l) end /-- Replace any metavariables in the expression with underscores, in preparation for printing `refine ...` statements. -/ meta def replace_mvars (e : expr) : expr := e.replace (λ e' _, if e'.is_mvar then some (unchecked_cast pexpr.mk_placeholder) else none) /-- Construct a `refine ...` or `exact ...` string which would construct `g`. -/ meta def tactic_statement (g : expr) : tactic string := do g ← instantiate_mvars g, g ← head_beta g, r ← pp (replace_mvars g), if g.has_meta_var then return (sformat!"Try this: refine {r}") else return (sformat!"Try this: exact {r}") /-- Assuming that a goal `g` has been (partially) solved in the tactic_state `l`, this function prepares a string of the form `exact ...` or `refine ...` (possibly with underscores) that will reproduce that result. -/ meta def message (g : expr) (l : tactic_state) : tactic string := do s ← read, write l, r ← tactic_statement g, write s, return r /-- An `application` records the result of a successful application of a library lemma. -/ meta structure application := (state : tactic_state) (script : string) (decl : option declaration) (num_goals : ℕ) (hyps_used : ℕ) end suggest open suggest declare_trace suggest -- Trace a list of all relevant lemmas -- implementation note: we produce a `tactic (mllist tactic application)` first, -- because it's easier to work in the tactic monad, but in a moment we squash this -- down to an `mllist tactic application`. private meta def suggest_core' (discharger : tactic unit := done) : tactic (mllist tactic application) := do g :: _ ← get_goals, hyps ← local_context, -- Make sure that `solve_by_elim` doesn't just solve the goal immediately: (lock_tactic_state (do focus1 $ solve_by_elim { discharger := discharger }, s ← read, m ← tactic_statement g, g ← instantiate_mvars g, return $ mllist.of_list [⟨s, m, none, 0, hyps.countp(λ h, h.occurs g)⟩])) <|> -- Otherwise, let's actually try applying library lemmas. (do -- Collect all definitions with the correct head symbol t ← infer_type g, defs ← library_defs (head_symbol t), -- Sort by length; people like short proofs let defs := defs.qsort(λ d₁ d₂, d₁.l ≤ d₂.l), trace_if_enabled `suggest format!"Found {defs.length} relevant lemmas:", trace_if_enabled `suggest $ defs.map (λ ⟨d, n, m, l⟩, (n, m.to_string)), -- Try applying each lemma against the goal, -- then record the number of remaining goals, and number of local hypotheses used. return $ (mllist.of_list defs).mfilter_map -- (This tactic block is only executed when we evaluate the mllist, -- so we need to do the `focus1` here.) (λ d, lock_tactic_state $ focus1 $ do apply_declaration ff discharger d, ng ← num_goals, g ← instantiate_mvars g, state ← read, m ← message g state, return { application . state := state, decl := d.d, script := m, num_goals := ng, hyps_used := hyps.countp(λ h, h.occurs g) })) /-- The core `suggest` tactic. It attempts to apply a declaration from the library, then solve new goals using `solve_by_elim`. It returns a list of `application`s consisting of fields: * `state`, a tactic state resulting from the successful application of a declaration from the library, * `script`, a string of the form `refine ...` or `exact ...` which will reproduce that tactic state, * `decl`, an `option declaration` indicating the declaration that was applied (or none, if `solve_by_elim` succeeded), * `num_goals`, the number of remaining goals, and * `hyps_used`, the number of local hypotheses used in the solution. -/ meta def suggest_core (discharger : tactic unit := done) : mllist tactic application := (mllist.monad_lift (suggest_core' discharger)).join /-- See `suggest_core`. Returns a list of at most `limit` `application`s, sorted by number of goals, and then (reverse) number of hypotheses used. -/ meta def suggest (limit : option ℕ := none) (discharger : tactic unit := done) : tactic (list application) := do let results := suggest_core discharger, -- Get the first n elements of the successful lemmas L ← if h : limit.is_some then results.take (option.get h) else results.force, -- Sort by number of remaining goals, then by number of hypotheses used. return $ L.qsort (λ d₁ d₂, d₁.num_goals < d₂.num_goals ∨ (d₁.num_goals = d₂.num_goals ∧ d₁.hyps_used ≥ d₂.hyps_used)) /-- Returns a list of at most `limit` strings, of the form `exact ...` or `refine ...`, which make progress on the current goal using a declaration from the library. -/ meta def suggest_scripts (limit : option ℕ := none) (discharger : tactic unit := done) : tactic (list string) := do L ← suggest limit discharger, return $ L.map application.script /-- Returns a string of the form `exact ...`, which closes the current goal. -/ meta def library_search (discharger : tactic unit := done) : tactic string := (suggest_core discharger).mfirst (λ a, do guard (a.num_goals = 0), write a.state, return a.script) namespace interactive open tactic open interactive open lean.parser local postfix `?`:9001 := optional declare_trace silence_suggest -- Turn off `exact/refine ...` trace messages for `suggest` /-- `suggest` tries to apply suitable theorems/defs from the library, and generates a list of `exact ...` or `refine ...` scripts that could be used at this step. It leaves the tactic state unchanged. It is intended as a complement of the search function in your editor, the `#find` tactic, and `library_search`. `suggest` takes an optional natural number `num` as input and returns the first `num` (or less, if all possibilities are exhausted) possibilities ordered by length of lemma names. The default for `num` is `50`. For performance reasons `suggest` uses monadic lazy lists (`mllist`). This means that `suggest` might miss some results if `num` is not large enough. However, because `suggest` uses monadic lazy lists, smaller values of `num` run faster than larger values. -/ meta def suggest (n : parse (with_desc "n" small_nat)?) : tactic unit := do L ← tactic.suggest_scripts (n.get_or_else 50), when (¬ is_trace_enabled_for `silence_suggest) (if L.length = 0 then fail "There are no applicable declarations" else L.mmap trace >> skip) /-- `suggest` lists possible usages of the `refine` tactic and leaves the tactic state unchanged. It is intended as a complement of the search function in your editor, the `#find` tactic, and `library_search`. `suggest` takes an optional natural number `num` as input and returns the first `num` (or less, if all possibilities are exhausted) possibilities ordered by length of lemma names. The default for `num` is `50`. For performance reasons `suggest` uses monadic lazy lists (`mllist`). This means that `suggest` might miss some results if `num` is not large enough. However, because `suggest` uses monadic lazy lists, smaller values of `num` run faster than larger values. An example of `suggest` in action, ```lean example (n : nat) : n < n + 1 := begin suggest, sorry end ``` prints the list, ```lean Try this: exact nat.lt.base n Try this: exact nat.lt_succ_self n Try this: refine not_le.mp _ Try this: refine gt_iff_lt.mp _ Try this: refine nat.lt.step _ Try this: refine lt_of_not_ge _ ... ``` -/ add_tactic_doc { name := "suggest", category := doc_category.tactic, decl_names := [`tactic.interactive.suggest], tags := ["search", "Try this"] } declare_trace silence_library_search -- Turn off `exact ...` trace message for `library_search /-- `library_search` attempts to apply every definition in the library whose head symbol matches the goal, and then discharge any new goals using `solve_by_elim`. If it succeeds, it prints a trace message `exact ...` which can replace the invocation of `library_search`. -/ meta def library_search : tactic unit := tactic.library_search tactic.done >>= if is_trace_enabled_for `silence_library_search then (λ _, skip) else trace /-- `library_search` is a tactic to identify existing lemmas in the library. It tries to close the current goal by applying a lemma from the library, then discharging any new goals using `solve_by_elim`. Typical usage is: ```lean example (n m k : ℕ) : n * (m - k) = n * m - n * k := by library_search -- Try this: exact nat.mul_sub_left_distrib n m k ``` `library_search` prints a trace message showing the proof it found, shown above as a comment. Typically you will then copy and paste this proof, replacing the call to `library_search`. -/ add_tactic_doc { name := "library_search", category := doc_category.tactic, decl_names := [`tactic.interactive.library_search], tags := ["search", "Try this"] } end interactive /-- Invoking the hole command `library_search` ("Use `library_search` to complete the goal") calls the tactic `library_search` to produce a proof term with the type of the hole. Running it on ```lean example : 0 < 1 := {!!} ``` produces ```lean example : 0 < 1 := nat.one_pos ``` -/ @[hole_command] meta def library_search_hole_cmd : hole_command := { name := "library_search", descr := "Use `library_search` to complete the goal.", action := λ _, do script ← library_search, -- Is there a better API for dropping the 'exact ' prefix on this string? return [((script.mk_iterator.remove 6).to_string, "by library_search")] } add_tactic_doc { name := "library_search", category := doc_category.hole_cmd, decl_names := [`tactic.library_search_hole_cmd], tags := ["search", "Try this"] } end tactic
b6d4d20d1886d40b017b8bbfdb8041edae79aca1
36c7a18fd72e5b57229bd8ba36493daf536a19ce
/library/init/setoid.lean
dc138dc77964a91e1a952ef09eda1ead153b283b
[ "Apache-2.0" ]
permissive
YHVHvx/lean
732bf0fb7a298cd7fe0f15d82f8e248c11db49e9
038369533e0136dd395dc252084d3c1853accbf2
refs/heads/master
1,610,701,080,210
1,449,128,595,000
1,449,128,595,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
740
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.relation structure setoid [class] (A : Type) := (r : A → A → Prop) (iseqv : equivalence r) namespace setoid infix ` ≈ ` := setoid.r variable {A : Type} variable [s : setoid A] include s theorem refl (a : A) : a ≈ a := and.elim_left (@setoid.iseqv A s) a theorem symm {a b : A} : a ≈ b → b ≈ a := λ H, and.elim_left (and.elim_right (@setoid.iseqv A s)) a b H theorem trans {a b c : A} : a ≈ b → b ≈ c → a ≈ c := λ H₁ H₂, and.elim_right (and.elim_right (@setoid.iseqv A s)) a b c H₁ H₂ end setoid
bebe17f94d01ec2c5d50055292696748f6212596
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/src/Init/Lean/Parser/Syntax.lean
ef0462c719ac64c0adc20a9e1876b3fa36a8938b
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
4,328
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 -/ prelude import Init.Lean.Parser.Command import Init.Lean.Parser.Tactic namespace Lean namespace Parser @[init] def regBuiltinSyntaxParserAttr : IO Unit := let leadingIdentAsSymbol := true; registerBuiltinParserAttribute `builtinSyntaxParser `syntax leadingIdentAsSymbol @[inline] def syntaxParser (rbp : Nat := 0) : Parser := categoryParser `syntax rbp def maxPrec := parser! nonReservedSymbol "max" true def precedenceLit : Parser := numLit <|> maxPrec def «precedence» := parser! ":" >> precedenceLit def optPrecedence := optional (try «precedence») namespace Syntax @[builtinSyntaxParser] def paren := parser! "(" >> many1 syntaxParser >> ")" @[builtinSyntaxParser] def cat := parser! ident >> optPrecedence @[builtinSyntaxParser] def atom := parser! strLit >> optPrecedence @[builtinSyntaxParser] def num := parser! nonReservedSymbol "num" @[builtinSyntaxParser] def str := parser! nonReservedSymbol "str" @[builtinSyntaxParser] def char := parser! nonReservedSymbol "char" @[builtinSyntaxParser] def ident := parser! nonReservedSymbol "ident" @[builtinSyntaxParser] def try := parser! nonReservedSymbol "try " >> syntaxParser appPrec @[builtinSyntaxParser] def lookahead := parser! nonReservedSymbol "lookahead " >> syntaxParser appPrec @[builtinSyntaxParser] def sepBy := parser! nonReservedSymbol "sepBy " >> syntaxParser appPrec >> syntaxParser appPrec @[builtinSyntaxParser] def sepBy1 := parser! nonReservedSymbol "sepBy1 " >> syntaxParser appPrec >> syntaxParser appPrec @[builtinSyntaxParser] def optional := tparser! symbolAux "?" none @[builtinSyntaxParser] def many := tparser! symbolAux "*" none @[builtinSyntaxParser] def many1 := tparser! symbolAux "+" none @[builtinSyntaxParser] def orelse := tparser! " <|> " >> syntaxParser 1 end Syntax namespace Command def quotedSymbolPrec := parser! quotedSymbol >> optPrecedence def «prefix» := parser! "prefix" def «infix» := parser! "infix" def «infixl» := parser! "infixl" def «infixr» := parser! "infixr" def «postfix» := parser! "postfix" def mixfixKind := «prefix» <|> «infix» <|> «infixl» <|> «infixr» <|> «postfix» -- TODO delete reserve @[builtinCommandParser] def «reserve» := parser! "reserve " >> mixfixKind >> quotedSymbolPrec def mixfixSymbol := quotedSymbolPrec <|> unquotedSymbol -- TODO: remove " := " after old frontend is gone @[builtinCommandParser] def «mixfix» := parser! mixfixKind >> mixfixSymbol >> (" := " <|> darrow) >> termParser def strLitPrec := parser! strLit >> optPrecedence def identPrec := parser! ident >> optPrecedence def optKind : Parser := optional ("[" >> ident >> "]") -- TODO: remove " := " after old frontend is gone @[builtinCommandParser] def «notation» := parser! "notation" >> many (strLitPrec <|> quotedSymbolPrec <|> identPrec) >> (" := " <|> darrow) >> termParser @[builtinCommandParser] def «macro_rules» := parser! "macro_rules" >> optKind >> Term.matchAlts @[builtinCommandParser] def «syntax» := parser! "syntax " >> optKind >> many1 syntaxParser >> " : " >> ident @[builtinCommandParser] def syntaxCat := parser! "declare_syntax_cat " >> ident def macroArgType := nonReservedSymbol "ident" <|> nonReservedSymbol "num" <|> nonReservedSymbol "str" <|> nonReservedSymbol "char" <|> (ident >> optPrecedence) def macroArgSimple := parser! ident >> checkNoWsBefore "no space before ':'" >> ":" >> macroArgType def macroArg := try strLitPrec <|> try macroArgSimple def macroHead := macroArg <|> try identPrec def macroTailTactic : Parser := try (" : " >> identEq "tactic") >> darrow >> "`(" >> sepBy1 tacticParser "; " true true >> ")" def macroTailCommand : Parser := try (" : " >> identEq "command") >> darrow >> "`(" >> many1 commandParser true >> ")" def macroTailDefault : Parser := try (" : " >> ident) >> darrow >> (("`(" >> categoryParserOfStack 2 >> ")") <|> termParser) def macroTail := macroTailTactic <|> macroTailCommand <|> macroTailDefault @[builtinCommandParser] def «macro» := parser! "macro " >> macroHead >> many macroArg >> macroTail end Command end Parser end Lean
472253899f3d45516e8d18ec10dc812aede89445
097294e9b80f0d9893ac160b9c7219aa135b51b9
/instructor/functions/polymomrphic/intro.lean
471345475b258eb5d165e323554718c45da0a892
[]
no_license
AbigailCastro17/CS2102-Discrete-Math
cf296251be9418ce90206f5e66bde9163e21abf9
d741e4d2d6a9b2e0c8380e51706218b8f608cee4
refs/heads/main
1,682,891,087,358
1,621,401,341,000
1,621,401,341,000
368,749,959
0
0
null
null
null
null
UTF-8
Lean
false
false
2,900
lean
/- We've seen identity functions for specific types. For example, here are identity functions for the types, ℕ, bool, and string. -/ def id_nat (a : ℕ) : ℕ := a def id_bool (a : bool) : bool := a def id_string (a : string) : string := a /- These functions differ only in the types of values they take and return. We don't want to have to repeat ourselves in this way. What we can do is to "factor out" the variation in the types of values into a parameter, namely into a *type parameter. The goal is to write a single paramterized function definition rather than many slightly varying copies of the essentially the same definition. Adding a type parameter is the solution. Our function will now take two arguments: - a type, α - a value, (a : α), of that type -/ def dm_id' (α : Type) (a : α): α := a #check dm_id' #eval dm_id' bool tt #eval dm_id' ℕ 3 #eval dm_id' string "Hello, Logic!" def fun_id_bool := dm_id' bool def fun_id_nat := dm_id' nat def fun_id_string := dm_id' string #check fun_id_bool #check fun_id_nat #check fun_id_string /- A little thought leads us to see that if we supply the second argument, such as 3, to this function, Lean should be able to infer the value of the first argument. In this case it would be ℕ, for example. Indeed, using underscore instead of an explicit type name will work: Lean will infer and fill in the missing value! -/ #eval dm_id' _ ff #eval dm_id' _ 3 #eval dm_id' _ "Hello, Logic!" /- Even better though would be to have a way to tell Lean to infer the missing value without our having the explicitly include those underscores in function application expressions. We Lean calls "implicit arguments" is the solution. We tell Lean to silently infer argument values by surrounding the argument declarations not with parentheses but with curly braces. -/ def dm_id {α : Type} (a : α) : α := a #eval dm_id tt #eval dm_id 3 #eval dm_id "Hello, Logic!" /- We can now write "clean" polymorphic function application expressions! -/ /- Note that giving explicit type arguments is not an option here. Try it and you'll see that Lean gives a type error. It is expecting that you will NOT give explicit arguments where implicit arguments are specified. -/ -- Try it /- In some cases, as we'll see later, Lean is unable to infer the value of an implicit argument. In such cases, you have to turn off implicit arguments and given arguments explicitly within a given expression. In these cases, you can turn off implicitl arguments on a case-by-case basis by adding an at sign (@) to the beginning of such an expression. -/ #eval @dm_id bool tt #eval @dm_id ℕ 3 #eval @dm_id string "Hello, Logic!" /- Now you understand exactly how Lean's polymorphic id function works! It is defined in Lean's standard libraries. -/ #eval id tt #eval id 3 #eval id "I love this stuff!" /- Yay! -/
39f5258193362b8758cbdcfbab27fd7a5859c4fe
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/limits/constructions/finite_products_of_binary_products.lean
ff8793b35fff61afb613c23daacb270b5d7b44ca
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
12,176
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.shapes.binary_products import category_theory.limits.preserves.shapes.products import category_theory.limits.shapes.binary_products import category_theory.limits.shapes.finite_products import logic.equiv.fin /-! # Constructing finite products from binary products and terminal. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. If a category has binary products and a terminal object then it has finite products. If a functor preserves binary products and the terminal object then it preserves finite products. # TODO Provide the dual results. Show the analogous results for functors which reflect or create (co)limits. -/ universes v v' u u' noncomputable theory open category_theory category_theory.category category_theory.limits namespace category_theory variables {J : Type v} [small_category J] variables {C : Type u} [category.{v} C] variables {D : Type u'} [category.{v'} D] /-- Given `n+1` objects of `C`, a fan for the last `n` with point `c₁.X` and a binary fan on `c₁.X` and `f 0`, we can build a fan for all `n+1`. In `extend_fan_is_limit` we show that if the two given fans are limits, then this fan is also a limit. -/ @[simps {rhs_md := semireducible}] def extend_fan {n : ℕ} {f : fin (n+1) → C} (c₁ : fan (λ (i : fin n), f i.succ)) (c₂ : binary_fan (f 0) c₁.X) : fan f := fan.mk c₂.X begin refine fin.cases _ _, { apply c₂.fst }, { intro i, apply c₂.snd ≫ c₁.π.app ⟨i⟩ }, end /-- Show that if the two given fans in `extend_fan` are limits, then the constructed fan is also a limit. -/ def extend_fan_is_limit {n : ℕ} (f : fin (n+1) → C) {c₁ : fan (λ (i : fin n), f i.succ)} {c₂ : binary_fan (f 0) c₁.X} (t₁ : is_limit c₁) (t₂ : is_limit c₂) : is_limit (extend_fan c₁ c₂) := { lift := λ s, begin apply (binary_fan.is_limit.lift' t₂ (s.π.app ⟨0⟩) _).1, apply t₁.lift ⟨_, discrete.nat_trans (λ ⟨i⟩, s.π.app ⟨i.succ⟩)⟩ end, fac' := λ s ⟨j⟩, begin apply fin.induction_on j, { apply (binary_fan.is_limit.lift' t₂ _ _).2.1 }, { rintro i -, dsimp only [extend_fan_π_app], rw [fin.cases_succ, ← assoc, (binary_fan.is_limit.lift' t₂ _ _).2.2, t₁.fac], refl } end, uniq' := λ s m w, begin apply binary_fan.is_limit.hom_ext t₂, { rw (binary_fan.is_limit.lift' t₂ _ _).2.1, apply w ⟨0⟩ }, { rw (binary_fan.is_limit.lift' t₂ _ _).2.2, apply t₁.uniq ⟨_, _⟩, rintro ⟨j⟩, rw assoc, dsimp only [discrete.nat_trans_app, extend_fan_is_limit._match_1], rw ← w ⟨j.succ⟩, dsimp only [extend_fan_π_app], rw fin.cases_succ } end } section variables [has_binary_products C] [has_terminal C] /-- If `C` has a terminal object and binary products, then it has a product for objects indexed by `fin n`. This is a helper lemma for `has_finite_products_of_has_binary_and_terminal`, which is more general than this. -/ private lemma has_product_fin : Π (n : ℕ) (f : fin n → C), has_product f | 0 := λ f, begin letI : has_limits_of_shape (discrete (fin 0)) C := has_limits_of_shape_of_equivalence (discrete.equivalence.{0} fin_zero_equiv'.symm), apply_instance, end | (n+1) := λ f, begin haveI := has_product_fin n, apply has_limit.mk ⟨_, extend_fan_is_limit f (limit.is_limit _) (limit.is_limit _)⟩, end /-- If `C` has a terminal object and binary products, then it has finite products. -/ lemma has_finite_products_of_has_binary_and_terminal : has_finite_products C := begin refine ⟨λ n, ⟨λ K, _⟩⟩, letI := has_product_fin n (λ n, K.obj ⟨n⟩), let : discrete.functor (λ n, K.obj ⟨n⟩) ≅ K := discrete.nat_iso (λ ⟨i⟩, iso.refl _), apply has_limit_of_iso this, end end section preserves variables (F : C ⥤ D) variables [preserves_limits_of_shape (discrete walking_pair) F] variables [preserves_limits_of_shape (discrete.{0} pempty) F] variables [has_finite_products.{v} C] /-- If `F` preserves the terminal object and binary products, then it preserves products indexed by `fin n` for any `n`. -/ noncomputable def preserves_fin_of_preserves_binary_and_terminal : Π (n : ℕ) (f : fin n → C), preserves_limit (discrete.functor f) F | 0 := λ f, begin letI : preserves_limits_of_shape (discrete (fin 0)) F := preserves_limits_of_shape_of_equiv.{0 0} (discrete.equivalence fin_zero_equiv'.symm) _, apply_instance, end | (n+1) := begin haveI := preserves_fin_of_preserves_binary_and_terminal n, intro f, refine preserves_limit_of_preserves_limit_cone (extend_fan_is_limit f (limit.is_limit _) (limit.is_limit _)) _, apply (is_limit_map_cone_fan_mk_equiv _ _ _).symm _, let := extend_fan_is_limit (λ i, F.obj (f i)) (is_limit_of_has_product_of_preserves_limit F _) (is_limit_of_has_binary_product_of_preserves_limit F _ _), refine is_limit.of_iso_limit this _, apply cones.ext _ _, apply iso.refl _, rintro ⟨j⟩, apply fin.induction_on j, { apply (category.id_comp _).symm }, { rintro i -, dsimp only [extend_fan_π_app, iso.refl_hom, fan.mk_π_app], rw [fin.cases_succ, fin.cases_succ], change F.map _ ≫ _ = 𝟙 _ ≫ _, rw [id_comp, ←F.map_comp], refl } end /-- If `F` preserves the terminal object and binary products, then it preserves limits of shape `discrete (fin n)`. -/ def preserves_shape_fin_of_preserves_binary_and_terminal (n : ℕ) : preserves_limits_of_shape (discrete (fin n)) F := { preserves_limit := λ K, begin let : discrete.functor (λ n, K.obj ⟨n⟩) ≅ K := discrete.nat_iso (λ ⟨i⟩, iso.refl _), haveI := preserves_fin_of_preserves_binary_and_terminal F n (λ n, K.obj ⟨n⟩), apply preserves_limit_of_iso_diagram F this, end } /-- If `F` preserves the terminal object and binary products then it preserves finite products. -/ def preserves_finite_products_of_preserves_binary_and_terminal (J : Type) [fintype J] : preserves_limits_of_shape (discrete J) F := begin classical, let e := fintype.equiv_fin J, haveI := preserves_shape_fin_of_preserves_binary_and_terminal F (fintype.card J), apply preserves_limits_of_shape_of_equiv.{0 0} (discrete.equivalence e).symm, end end preserves /-- Given `n+1` objects of `C`, a cofan for the last `n` with point `c₁.X` and a binary cofan on `c₁.X` and `f 0`, we can build a cofan for all `n+1`. In `extend_cofan_is_colimit` we show that if the two given cofans are colimits, then this cofan is also a colimit. -/ @[simps {rhs_md := semireducible}] def extend_cofan {n : ℕ} {f : fin (n+1) → C} (c₁ : cofan (λ (i : fin n), f i.succ)) (c₂ : binary_cofan (f 0) c₁.X) : cofan f := cofan.mk c₂.X begin refine fin.cases _ _, { apply c₂.inl }, { intro i, apply c₁.ι.app ⟨i⟩ ≫ c₂.inr }, end /-- Show that if the two given cofans in `extend_cofan` are colimits, then the constructed cofan is also a colimit. -/ def extend_cofan_is_colimit {n : ℕ} (f : fin (n+1) → C) {c₁ : cofan (λ (i : fin n), f i.succ)} {c₂ : binary_cofan (f 0) c₁.X} (t₁ : is_colimit c₁) (t₂ : is_colimit c₂) : is_colimit (extend_cofan c₁ c₂) := { desc := λ s, begin apply (binary_cofan.is_colimit.desc' t₂ (s.ι.app ⟨0⟩) _).1, apply t₁.desc ⟨_, discrete.nat_trans (λ i, s.ι.app ⟨i.as.succ⟩)⟩ end, fac' := λ s, begin rintro ⟨j⟩, apply fin.induction_on j, { apply (binary_cofan.is_colimit.desc' t₂ _ _).2.1 }, { rintro i -, dsimp only [extend_cofan_ι_app], rw [fin.cases_succ, assoc, (binary_cofan.is_colimit.desc' t₂ _ _).2.2, t₁.fac], refl } end, uniq' := λ s m w, begin apply binary_cofan.is_colimit.hom_ext t₂, { rw (binary_cofan.is_colimit.desc' t₂ _ _).2.1, apply w ⟨0⟩ }, { rw (binary_cofan.is_colimit.desc' t₂ _ _).2.2, apply t₁.uniq ⟨_, _⟩, rintro ⟨j⟩, dsimp only [discrete.nat_trans_app], rw ← w ⟨j.succ⟩, dsimp only [extend_cofan_ι_app], rw [fin.cases_succ, assoc], } end } section variables [has_binary_coproducts C] [has_initial C] /-- If `C` has an initial object and binary coproducts, then it has a coproduct for objects indexed by `fin n`. This is a helper lemma for `has_cofinite_products_of_has_binary_and_terminal`, which is more general than this. -/ private lemma has_coproduct_fin : Π (n : ℕ) (f : fin n → C), has_coproduct f | 0 := λ f, begin letI : has_colimits_of_shape (discrete (fin 0)) C := has_colimits_of_shape_of_equivalence (discrete.equivalence.{0} fin_zero_equiv'.symm), apply_instance, end | (n+1) := λ f, begin haveI := has_coproduct_fin n, apply has_colimit.mk ⟨_, extend_cofan_is_colimit f (colimit.is_colimit _) (colimit.is_colimit _)⟩, end /-- If `C` has an initial object and binary coproducts, then it has finite coproducts. -/ lemma has_finite_coproducts_of_has_binary_and_initial : has_finite_coproducts C := begin refine ⟨λ n, ⟨λ K, _⟩⟩, letI := has_coproduct_fin n (λ n, K.obj ⟨n⟩), let : K ≅ discrete.functor (λ n, K.obj ⟨n⟩) := discrete.nat_iso (λ ⟨i⟩, iso.refl _), apply has_colimit_of_iso this, end end section preserves variables (F : C ⥤ D) variables [preserves_colimits_of_shape (discrete walking_pair) F] variables [preserves_colimits_of_shape (discrete.{0} pempty) F] variables [has_finite_coproducts.{v} C] /-- If `F` preserves the initial object and binary coproducts, then it preserves products indexed by `fin n` for any `n`. -/ noncomputable def preserves_fin_of_preserves_binary_and_initial : Π (n : ℕ) (f : fin n → C), preserves_colimit (discrete.functor f) F | 0 := λ f, begin letI : preserves_colimits_of_shape (discrete (fin 0)) F := preserves_colimits_of_shape_of_equiv.{0 0} (discrete.equivalence fin_zero_equiv'.symm) _, apply_instance, end | (n+1) := begin haveI := preserves_fin_of_preserves_binary_and_initial n, intro f, refine preserves_colimit_of_preserves_colimit_cocone (extend_cofan_is_colimit f (colimit.is_colimit _) (colimit.is_colimit _)) _, apply (is_colimit_map_cocone_cofan_mk_equiv _ _ _).symm _, let := extend_cofan_is_colimit (λ i, F.obj (f i)) (is_colimit_of_has_coproduct_of_preserves_colimit F _) (is_colimit_of_has_binary_coproduct_of_preserves_colimit F _ _), refine is_colimit.of_iso_colimit this _, apply cocones.ext _ _, apply iso.refl _, rintro ⟨j⟩, apply fin.induction_on j, { apply category.comp_id }, { rintro i -, dsimp only [extend_cofan_ι_app, iso.refl_hom, cofan.mk_ι_app], rw [fin.cases_succ, fin.cases_succ], erw [comp_id, ←F.map_comp], refl, } end /-- If `F` preserves the initial object and binary coproducts, then it preserves colimits of shape `discrete (fin n)`. -/ def preserves_shape_fin_of_preserves_binary_and_initial (n : ℕ) : preserves_colimits_of_shape (discrete (fin n)) F := { preserves_colimit := λ K, begin let : discrete.functor (λ n, K.obj ⟨n⟩) ≅ K := discrete.nat_iso (λ ⟨i⟩, iso.refl _), haveI := preserves_fin_of_preserves_binary_and_initial F n (λ n, K.obj ⟨n⟩), apply preserves_colimit_of_iso_diagram F this, end } /-- If `F` preserves the initial object and binary coproducts then it preserves finite products. -/ def preserves_finite_coproducts_of_preserves_binary_and_initial (J : Type) [fintype J] : preserves_colimits_of_shape (discrete J) F := begin classical, let e := fintype.equiv_fin J, haveI := preserves_shape_fin_of_preserves_binary_and_initial F (fintype.card J), apply preserves_colimits_of_shape_of_equiv.{0 0} (discrete.equivalence e).symm, end end preserves end category_theory
fff7f425fb027d9a220b7cd02739d668e84ae8cb
297c4ceafbbaed2a59b6215504d09e6bf201a2ee
/kruskal_final/temp.lean
df15cbb61387cda2a4eecca6083ba7f84820d6b4
[]
no_license
minchaowu/Kruskal.lean3
559c91b82033ce44ea61593adcec9cfff725c88d
a14516f47b21e636e9df914fc6ebe64cbe5cd38d
refs/heads/master
1,611,010,001,429
1,497,935,421,000
1,497,935,421,000
82,000,982
1
0
null
null
null
null
UTF-8
Lean
false
false
182
lean
open set function theorem least_seq_at_n {S : set (ℕ → ℕ)} (H : S ≠ ∅) (n : ℕ) : ∃ f : ℕ → ℕ, f ∈ S ∧ ∀ g : ℕ → ℕ, g ∈ S → f n ≤ g n := sorry
13c6e7f74f6d0f2b8fd1b7efd3cbc0df0081d1cc
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/group_theory/specific_groups/cyclic.lean
58f20ec31754288237e759fc97c06c0b02fd30df
[ "Apache-2.0" ]
permissive
troyjlee/mathlib
e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5
45e7eb8447555247246e3fe91c87066506c14875
refs/heads/master
1,689,248,035,046
1,629,470,528,000
1,629,470,528,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
19,225
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import algebra.big_operators.order import data.nat.totient import group_theory.order_of_element import tactic.group /-! # Cyclic groups A group `G` is called cyclic if there exists an element `g : G` such that every element of `G` is of the form `g ^ n` for some `n : ℕ`. This file only deals with the predicate on a group to be cyclic. For the concrete cyclic group of order `n`, see `data.zmod.basic`. ## Main definitions * `is_cyclic` is a predicate on a group stating that the group is cyclic. ## Main statements * `is_cyclic_of_prime_card` proves that a finite group of prime order is cyclic. * `is_simple_group_of_prime_card`, `is_simple_group.is_cyclic`, and `is_simple_group.prime_card` classify finite simple abelian groups. ## Implementation details This file is currently only available for multiplicatively written groups. ## Tags cyclic group ## TODO * Add the attribute `@[to_additive]` to the declarations about `is_cyclic`, so that they work for additive groups. -/ universe u variables {α : Type u} {a : α} section cyclic open_locale big_operators local attribute [instance] set_fintype open subgroup /-- A group is called *cyclic* if it is generated by a single element. -/ class is_cyclic (α : Type u) [group α] : Prop := (exists_generator [] : ∃ g : α, ∀ x, x ∈ gpowers g) @[priority 100] instance is_cyclic_of_subsingleton [group α] [subsingleton α] : is_cyclic α := ⟨⟨1, λ x, by { rw subsingleton.elim x 1, exact mem_gpowers 1 }⟩⟩ /-- A cyclic group is always commutative. This is not an `instance` because often we have a better proof of `comm_group`. -/ def is_cyclic.comm_group [hg : group α] [is_cyclic α] : comm_group α := { mul_comm := λ x y, show x * y = y * x, from let ⟨g, hg⟩ := is_cyclic.exists_generator α in let ⟨n, hn⟩ := hg x in let ⟨m, hm⟩ := hg y in hm ▸ hn ▸ gpow_mul_comm _ _ _, ..hg } variables [group α] lemma monoid_hom.map_cyclic {G : Type*} [group G] [h : is_cyclic G] (σ : G →* G) : ∃ m : ℤ, ∀ g : G, σ g = g ^ m := begin obtain ⟨h, hG⟩ := is_cyclic.exists_generator G, obtain ⟨m, hm⟩ := hG (σ h), use m, intro g, obtain ⟨n, rfl⟩ := hG g, rw [monoid_hom.map_gpow, ←hm, ←gpow_mul, ←gpow_mul'], end lemma is_cyclic_of_order_of_eq_card [fintype α] (x : α) (hx : order_of x = fintype.card α) : is_cyclic α := begin classical, use x, simp_rw ← set_like.mem_coe, rw ← set.eq_univ_iff_forall, apply set.eq_of_subset_of_card_le (set.subset_univ _), rw [fintype.card_congr (equiv.set.univ α), ← hx, order_eq_card_gpowers], end /-- A finite group of prime order is cyclic. -/ lemma is_cyclic_of_prime_card {α : Type u} [group α] [fintype α] {p : ℕ} [hp : fact p.prime] (h : fintype.card α = p) : is_cyclic α := ⟨begin obtain ⟨g, hg⟩ : ∃ g : α, g ≠ 1, from fintype.exists_ne_of_one_lt_card (by { rw h, exact hp.1.one_lt }) 1, classical, -- for fintype (subgroup.gpowers g) have : fintype.card (subgroup.gpowers g) ∣ p, { rw ←h, apply card_subgroup_dvd_card }, rw nat.dvd_prime hp.1 at this, cases this, { rw fintype.card_eq_one_iff at this, cases this with t ht, suffices : g = 1, { contradiction }, have hgt := ht ⟨g, by { change g ∈ subgroup.gpowers g, exact subgroup.mem_gpowers g }⟩, rw [←ht 1] at hgt, change (⟨_, _⟩ : subgroup.gpowers g) = ⟨_, _⟩ at hgt, simpa using hgt }, { use g, intro x, rw [←h] at this, rw subgroup.eq_top_of_card_eq _ this, exact subgroup.mem_top _ } end⟩ lemma order_of_eq_card_of_forall_mem_gpowers [fintype α] {g : α} (hx : ∀ x, x ∈ gpowers g) : order_of g = fintype.card α := by { classical, rw [← fintype.card_congr (equiv.set.univ α), order_eq_card_gpowers], simp [hx], apply fintype.card_of_finset', simp, intro x, exact hx x} instance bot.is_cyclic {α : Type u} [group α] : is_cyclic (⊥ : subgroup α) := ⟨⟨1, λ x, ⟨0, subtype.eq $ eq.symm (subgroup.mem_bot.1 x.2)⟩⟩⟩ instance subgroup.is_cyclic {α : Type u} [group α] [is_cyclic α] (H : subgroup α) : is_cyclic H := by haveI := classical.prop_decidable; exact let ⟨g, hg⟩ := is_cyclic.exists_generator α in if hx : ∃ (x : α), x ∈ H ∧ x ≠ (1 : α) then let ⟨x, hx₁, hx₂⟩ := hx in let ⟨k, hk⟩ := hg x in have hex : ∃ n : ℕ, 0 < n ∧ g ^ n ∈ H, from ⟨k.nat_abs, nat.pos_of_ne_zero (λ h, hx₂ $ by rw [← hk, int.eq_zero_of_nat_abs_eq_zero h, gpow_zero]), match k, hk with | (k : ℕ), hk := by rw [int.nat_abs_of_nat, ← gpow_coe_nat, hk]; exact hx₁ | -[1+ k], hk := by rw [int.nat_abs_of_neg_succ_of_nat, ← subgroup.inv_mem_iff H]; simp * at * end⟩, ⟨⟨⟨g ^ nat.find hex, (nat.find_spec hex).2⟩, λ ⟨x, hx⟩, let ⟨k, hk⟩ := hg x in have hk₁ : g ^ ((nat.find hex : ℤ) * (k / nat.find hex)) ∈ gpowers (g ^ nat.find hex), from ⟨k / nat.find hex, by rw [← gpow_coe_nat, gpow_mul]⟩, have hk₂ : g ^ ((nat.find hex : ℤ) * (k / nat.find hex)) ∈ H, by { rw gpow_mul, apply H.gpow_mem, exact_mod_cast (nat.find_spec hex).2 }, have hk₃ : g ^ (k % nat.find hex) ∈ H, from (subgroup.mul_mem_cancel_right H hk₂).1 $ by rw [← gpow_add, int.mod_add_div, hk]; exact hx, have hk₄ : k % nat.find hex = (k % nat.find hex).nat_abs, by rw int.nat_abs_of_nonneg (int.mod_nonneg _ (int.coe_nat_ne_zero_iff_pos.2 (nat.find_spec hex).1)), have hk₅ : g ^ (k % nat.find hex ).nat_abs ∈ H, by rwa [← gpow_coe_nat, ← hk₄], have hk₆ : (k % (nat.find hex : ℤ)).nat_abs = 0, from by_contradiction (λ h, nat.find_min hex (int.coe_nat_lt.1 $ by rw [← hk₄]; exact int.mod_lt_of_pos _ (int.coe_nat_pos.2 (nat.find_spec hex).1)) ⟨nat.pos_of_ne_zero h, hk₅⟩), ⟨k / (nat.find hex : ℤ), subtype.ext_iff_val.2 begin suffices : g ^ ((nat.find hex : ℤ) * (k / nat.find hex)) = x, { simpa [gpow_mul] }, rw [int.mul_div_cancel' (int.dvd_of_mod_eq_zero (int.eq_zero_of_nat_abs_eq_zero hk₆)), hk] end⟩⟩⟩ else have H = (⊥ : subgroup α), from subgroup.ext $ λ x, ⟨λ h, by simp at *; tauto, λ h, by rw [subgroup.mem_bot.1 h]; exact H.one_mem⟩, by clear _let_match; substI this; apply_instance open finset nat section classical open_locale classical lemma is_cyclic.card_pow_eq_one_le [decidable_eq α] [fintype α] [is_cyclic α] {n : ℕ} (hn0 : 0 < n) : (univ.filter (λ a : α, a ^ n = 1)).card ≤ n := let ⟨g, hg⟩ := is_cyclic.exists_generator α in calc (univ.filter (λ a : α, a ^ n = 1)).card ≤ ((gpowers (g ^ (fintype.card α / (gcd n (fintype.card α))))) : set α).to_finset.card : card_le_of_subset (λ x hx, let ⟨m, hm⟩ := show x ∈ submonoid.powers g, from mem_powers_iff_mem_gpowers.2 $ hg x in set.mem_to_finset.2 ⟨(m / (fintype.card α / (gcd n (fintype.card α))) : ℕ), have hgmn : g ^ (m * gcd n (fintype.card α)) = 1, by rw [pow_mul, hm, ← pow_gcd_card_eq_one_iff]; exact (mem_filter.1 hx).2, begin rw [gpow_coe_nat, ← pow_mul, nat.mul_div_cancel_left', hm], refine dvd_of_mul_dvd_mul_right (gcd_pos_of_pos_left (fintype.card α) hn0) _, conv {to_lhs, rw [nat.div_mul_cancel (gcd_dvd_right _ _), ← order_of_eq_card_of_forall_mem_gpowers hg]}, exact order_of_dvd_of_pow_eq_one hgmn end⟩) ... ≤ n : let ⟨m, hm⟩ := gcd_dvd_right n (fintype.card α) in have hm0 : 0 < m, from nat.pos_of_ne_zero $ λ hm0, by { rw [hm0, mul_zero, fintype.card_eq_zero_iff] at hm, exact hm.elim' 1 }, begin rw [← fintype.card_of_finset' _ (λ _, set.mem_to_finset), ← order_eq_card_gpowers, order_of_pow g, order_of_eq_card_of_forall_mem_gpowers hg], rw [hm] {occs := occurrences.pos [2,3]}, rw [nat.mul_div_cancel_left _ (gcd_pos_of_pos_left _ hn0), gcd_mul_left_left, hm, nat.mul_div_cancel _ hm0], exact le_of_dvd hn0 (gcd_dvd_left _ _) end end classical lemma is_cyclic.exists_monoid_generator [fintype α] [is_cyclic α] : ∃ x : α, ∀ y : α, y ∈ submonoid.powers x := by { simp_rw [mem_powers_iff_mem_gpowers], exact is_cyclic.exists_generator α } section variables [decidable_eq α] [fintype α] lemma is_cyclic.image_range_order_of (ha : ∀ x : α, x ∈ gpowers a) : finset.image (λ i, a ^ i) (range (order_of a)) = univ := begin simp_rw [←set_like.mem_coe] at ha, simp only [image_range_order_of, set.eq_univ_iff_forall.mpr ha], convert set.to_finset_univ end lemma is_cyclic.image_range_card (ha : ∀ x : α, x ∈ gpowers a) : finset.image (λ i, a ^ i) (range (fintype.card α)) = univ := by rw [← order_of_eq_card_of_forall_mem_gpowers ha, is_cyclic.image_range_order_of ha] end section totient variables [decidable_eq α] [fintype α] (hn : ∀ n : ℕ, 0 < n → (univ.filter (λ a : α, a ^ n = 1)).card ≤ n) include hn lemma card_pow_eq_one_eq_order_of_aux (a : α) : (finset.univ.filter (λ b : α, b ^ order_of a = 1)).card = order_of a := le_antisymm (hn _ (order_of_pos a)) (calc order_of a = @fintype.card (gpowers a) (id _) : order_eq_card_gpowers ... ≤ @fintype.card (↑(univ.filter (λ b : α, b ^ order_of a = 1)) : set α) (fintype.of_finset _ (λ _, iff.rfl)) : @fintype.card_le_of_injective (gpowers a) (↑(univ.filter (λ b : α, b ^ order_of a = 1)) : set α) (id _) (id _) (λ b, ⟨b.1, mem_filter.2 ⟨mem_univ _, let ⟨i, hi⟩ := b.2 in by rw [← hi, ← gpow_coe_nat, ← gpow_mul, mul_comm, gpow_mul, gpow_coe_nat, pow_order_of_eq_one, one_gpow]⟩⟩) (λ _ _ h, subtype.eq (subtype.mk.inj h)) ... = (univ.filter (λ b : α, b ^ order_of a = 1)).card : fintype.card_of_finset _ _) open_locale nat -- use φ for nat.totient private lemma card_order_of_eq_totient_aux₁ : ∀ {d : ℕ}, d ∣ fintype.card α → 0 < (univ.filter (λ a : α, order_of a = d)).card → (univ.filter (λ a : α, order_of a = d)).card = φ d | 0 := λ hd hd0, let ⟨a, ha⟩ := card_pos.1 hd0 in absurd (mem_filter.1 ha).2 $ ne_of_gt $ order_of_pos a | (d+1) := λ hd hd0, let ⟨a, ha⟩ := card_pos.1 hd0 in have ha : order_of a = d.succ, from (mem_filter.1 ha).2, have h : ∑ m in (range d.succ).filter (∣ d.succ), (univ.filter (λ a : α, order_of a = m)).card = ∑ m in (range d.succ).filter (∣ d.succ), φ m, from finset.sum_congr rfl (λ m hm, have hmd : m < d.succ, from mem_range.1 (mem_filter.1 hm).1, have hm : m ∣ d.succ, from (mem_filter.1 hm).2, card_order_of_eq_totient_aux₁ (hm.trans hd) (finset.card_pos.2 ⟨a ^ (d.succ / m), mem_filter.2 ⟨mem_univ _, by { rw [order_of_pow a, ha, gcd_eq_right (div_dvd_of_dvd hm), nat.div_div_self hm (succ_pos _)] }⟩⟩)), have hinsert : insert d.succ ((range d.succ).filter (∣ d.succ)) = (range d.succ.succ).filter (∣ d.succ), from (finset.ext $ λ x, ⟨λ h, (mem_insert.1 h).elim (λ h, by simp [h, range_succ]) (by clear _let_match; simp [range_succ]; tauto), by clear _let_match; simp [range_succ] {contextual := tt}; tauto⟩), have hinsert₁ : d.succ ∉ (range d.succ).filter (∣ d.succ), by simp [mem_range, zero_le_one, le_succ], (add_left_inj (∑ m in (range d.succ).filter (∣ d.succ), (univ.filter (λ a : α, order_of a = m)).card)).1 (calc _ = ∑ m in insert d.succ (filter (∣ d.succ) (range d.succ)), (univ.filter (λ a : α, order_of a = m)).card : eq.symm (finset.sum_insert (by simp [mem_range, zero_le_one, le_succ])) ... = ∑ m in (range d.succ.succ).filter (∣ d.succ), (univ.filter (λ a : α, order_of a = m)).card : sum_congr hinsert (λ _ _, rfl) ... = (univ.filter (λ a : α, a ^ d.succ = 1)).card : sum_card_order_of_eq_card_pow_eq_one (succ_pos d) ... = ∑ m in (range d.succ.succ).filter (∣ d.succ), φ m : ha ▸ (card_pow_eq_one_eq_order_of_aux hn a).symm ▸ (sum_totient _).symm ... = _ : by rw [h, ← sum_insert hinsert₁]; exact finset.sum_congr hinsert.symm (λ _ _, rfl)) lemma card_order_of_eq_totient_aux₂ {d : ℕ} (hd : d ∣ fintype.card α) : (univ.filter (λ a : α, order_of a = d)).card = φ d := by_contradiction $ λ h, have h0 : (univ.filter (λ a : α , order_of a = d)).card = 0 := not_not.1 (mt pos_iff_ne_zero.2 (mt (card_order_of_eq_totient_aux₁ hn hd) h)), let c := fintype.card α in have hc0 : 0 < c, from fintype.card_pos_iff.2 ⟨1⟩, lt_irrefl c $ calc c = (univ.filter (λ a : α, a ^ c = 1)).card : congr_arg card $ by simp [finset.ext_iff, c] ... = ∑ m in (range c.succ).filter (∣ c), (univ.filter (λ a : α, order_of a = m)).card : (sum_card_order_of_eq_card_pow_eq_one hc0).symm ... = ∑ m in ((range c.succ).filter (∣ c)).erase d, (univ.filter (λ a : α, order_of a = m)).card : eq.symm (sum_subset (erase_subset _ _) (λ m hm₁ hm₂, have m = d, by simp at *; cc, by simp [*, finset.ext_iff] at *; exact h0)) ... ≤ ∑ m in ((range c.succ).filter (∣ c)).erase d, φ m : sum_le_sum (λ m hm, have hmc : m ∣ c, by simp at hm; tauto, (imp_iff_not_or.1 (card_order_of_eq_totient_aux₁ hn hmc)).elim (λ h, by simp [nat.le_zero_iff.1 (le_of_not_gt h), nat.zero_le]) (λ h, by rw h)) ... < φ d + ∑ m in ((range c.succ).filter (∣ c)).erase d, φ m : lt_add_of_pos_left _ (totient_pos (nat.pos_of_ne_zero (λ h, pos_iff_ne_zero.1 hc0 (eq_zero_of_zero_dvd $ h ▸ hd)))) ... = ∑ m in insert d (((range c.succ).filter (∣ c)).erase d), φ m : eq.symm (sum_insert (by simp)) ... = ∑ m in (range c.succ).filter (∣ c), φ m : finset.sum_congr (finset.insert_erase (mem_filter.2 ⟨mem_range.2 (lt_succ_of_le (le_of_dvd hc0 hd)), hd⟩)) (λ _ _, rfl) ... = c : sum_totient _ lemma is_cyclic_of_card_pow_eq_one_le : is_cyclic α := have (univ.filter (λ a : α, order_of a = fintype.card α)).nonempty, from (card_pos.1 $ by rw [card_order_of_eq_totient_aux₂ hn (dvd_refl _)]; exact totient_pos (fintype.card_pos_iff.2 ⟨1⟩)), let ⟨x, hx⟩ := this in is_cyclic_of_order_of_eq_card x (finset.mem_filter.1 hx).2 end totient lemma is_cyclic.card_order_of_eq_totient [is_cyclic α] [fintype α] {d : ℕ} (hd : d ∣ fintype.card α) : (univ.filter (λ a : α, order_of a = d)).card = totient d := begin classical, apply card_order_of_eq_totient_aux₂ (λ n, is_cyclic.card_pow_eq_one_le) hd end /-- A finite group of prime order is simple. -/ lemma is_simple_group_of_prime_card {α : Type u} [group α] [fintype α] {p : ℕ} [hp : fact p.prime] (h : fintype.card α = p) : is_simple_group α := ⟨begin have h' := nat.prime.one_lt (fact.out p.prime), rw ← h at h', haveI := fintype.one_lt_card_iff_nontrivial.1 h', apply exists_pair_ne α, end, λ H Hn, begin classical, have hcard := card_subgroup_dvd_card H, rw [h, dvd_prime (fact.out p.prime)] at hcard, refine hcard.imp (λ h1, _) (λ hp, _), { haveI := fintype.card_le_one_iff_subsingleton.1 (le_of_eq h1), apply eq_bot_of_subsingleton }, { exact eq_top_of_card_eq _ (hp.trans h.symm) } end⟩ end cyclic section quotient_center open subgroup variables {G : Type*} {H : Type*} [group G] [group H] /-- A group is commutative if the quotient by the center is cyclic. Also see `comm_group_of_cycle_center_quotient` for the `comm_group` instance -/ lemma commutative_of_cyclic_center_quotient [is_cyclic H] (f : G →* H) (hf : f.ker ≤ center G) (a b : G) : a * b = b * a := let ⟨⟨x, y, (hxy : f y = x)⟩, (hx : ∀ a : f.range, a ∈ gpowers _)⟩ := is_cyclic.exists_generator f.range in let ⟨m, hm⟩ := hx ⟨f a, a, rfl⟩ in let ⟨n, hn⟩ := hx ⟨f b, b, rfl⟩ in have hm : x ^ m = f a, by simpa [subtype.ext_iff] using hm, have hn : x ^ n = f b, by simpa [subtype.ext_iff] using hn, have ha : y ^ (-m) * a ∈ center G, from hf (by rw [f.mem_ker, f.map_mul, f.map_gpow, hxy, gpow_neg, hm, inv_mul_self]), have hb : y ^ (-n) * b ∈ center G, from hf (by rw [f.mem_ker, f.map_mul, f.map_gpow, hxy, gpow_neg, hn, inv_mul_self]), calc a * b = y ^ m * ((y ^ (-m) * a) * y ^ n) * (y ^ (-n) * b) : by simp [mul_assoc] ... = y ^ m * (y ^ n * (y ^ (-m) * a)) * (y ^ (-n) * b) : by rw [mem_center_iff.1 ha] ... = y ^ m * y ^ n * y ^ (-m) * (a * (y ^ (-n) * b)) : by simp [mul_assoc] ... = y ^ m * y ^ n * y ^ (-m) * ((y ^ (-n) * b) * a) : by rw [mem_center_iff.1 hb] ... = b * a : by group /-- A group is commutative if the quotient by the center is cyclic. -/ def comm_group_of_cycle_center_quotient [is_cyclic H] (f : G →* H) (hf : f.ker ≤ center G) : comm_group G := { mul_comm := commutative_of_cyclic_center_quotient f hf, ..show group G, by apply_instance } end quotient_center namespace is_simple_group section comm_group variables [comm_group α] [is_simple_group α] @[priority 100] instance : is_cyclic α := begin cases subsingleton_or_nontrivial α with hi hi; haveI := hi, { apply is_cyclic_of_subsingleton }, { obtain ⟨g, hg⟩ := exists_ne (1 : α), refine ⟨⟨g, λ x, _⟩⟩, cases is_simple_lattice.eq_bot_or_eq_top (subgroup.gpowers g) with hb ht, { exfalso, apply hg, rw [← subgroup.mem_bot, ← hb], apply subgroup.mem_gpowers }, { rw ht, apply subgroup.mem_top } } end theorem prime_card [fintype α] : (fintype.card α).prime := begin have h0 : 0 < fintype.card α := fintype.card_pos_iff.2 (by apply_instance), obtain ⟨g, hg⟩ := is_cyclic.exists_generator α, refine ⟨fintype.one_lt_card_iff_nontrivial.2 infer_instance, λ n hn, _⟩, refine (is_simple_lattice.eq_bot_or_eq_top (subgroup.gpowers (g ^ n))).symm.imp _ _, { intro h, have hgo := order_of_pow g, rw [order_of_eq_card_of_forall_mem_gpowers hg, nat.gcd_eq_right_iff_dvd.1 hn, order_of_eq_card_of_forall_mem_gpowers, eq_comm, nat.div_eq_iff_eq_mul_left (nat.pos_of_dvd_of_pos hn h0) hn] at hgo, { exact (mul_left_cancel' (ne_of_gt h0) ((mul_one (fintype.card α)).trans hgo)).symm }, { intro x, rw h, exact subgroup.mem_top _ } }, { intro h, apply le_antisymm (nat.le_of_dvd h0 hn), rw ← order_of_eq_card_of_forall_mem_gpowers hg, apply order_of_le_of_pow_eq_one (nat.pos_of_dvd_of_pos hn h0), rw [← subgroup.mem_bot, ← h], exact subgroup.mem_gpowers _ } end end comm_group end is_simple_group theorem comm_group.is_simple_iff_is_cyclic_and_prime_card [fintype α] [comm_group α] : is_simple_group α ↔ is_cyclic α ∧ (fintype.card α).prime := begin split, { introI h, exact ⟨is_simple_group.is_cyclic, is_simple_group.prime_card⟩ }, { rintro ⟨hc, hp⟩, haveI : fact (fintype.card α).prime := ⟨hp⟩, exact is_simple_group_of_prime_card rfl } end
da7d7deb9eab3f58d6a9b48dd1ad64f1d70a5055
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/rewrite.lean
fb695b9c2377a43e294d57835cca709632c75eb0
[ "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
218
lean
class is_one_class (n : ℕ) := (one : n = 1) lemma one_eq (n : ℕ) [is_one_class n] : 1 = n := is_one_class.one.symm -- error message should say which typeclass is missing example (n : ℕ) : n = 1 := by rw one_eq
e74152bf8bfdbae9c62fa39eeb7ff2cb5fbca531
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/data/nat/enat.lean
4f2c32e45b6d5816ece88ecfe44dc9849a964968
[ "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
19,797
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import data.equiv.mul_add import tactic.norm_num import data.part /-! # Natural numbers with infinity The natural numbers and an extra `top` element `⊤`. ## Main definitions The following instances are defined: * `ordered_add_comm_monoid enat` * `canonically_ordered_add_monoid enat` There is no additive analogue of `monoid_with_zero`; if there were then `enat` could be an `add_monoid_with_top`. * `to_with_top` : the map from `enat` to `with_top ℕ`, with theorems that it plays well with `+` and `≤`. * `with_top_add_equiv : enat ≃+ with_top ℕ` * `with_top_order_iso : enat ≃o with_top ℕ` ## Implementation details `enat` is defined to be `part ℕ`. `+` and `≤` are defined on `enat`, but there is an issue with `*` because it's not clear what `0 * ⊤` should be. `mul` is hence left undefined. Similarly `⊤ - ⊤` is ambiguous so there is no `-` defined on `enat`. Before the `open_locale classical` line, various proofs are made with decidability assumptions. This can cause issues -- see for example the non-simp lemma `to_with_top_zero` proved by `rfl`, followed by `@[simp] lemma to_with_top_zero'` whose proof uses `convert`. ## Tags enat, with_top ℕ -/ open part (hiding some) /-- Type of natural numbers with infinity (`⊤`) -/ def enat : Type := part ℕ namespace enat /-- The computable embedding `ℕ → enat`. This coincides with the coercion `coe : ℕ → enat`, see `enat.some_eq_coe`. However, `coe` is noncomputable so `some` is preferable when computability is a concern. -/ def some : ℕ → enat := part.some instance : has_zero enat := ⟨some 0⟩ instance : inhabited enat := ⟨0⟩ instance : has_one enat := ⟨some 1⟩ instance : has_add enat := ⟨λ x y, ⟨x.dom ∧ y.dom, λ h, get x h.1 + get y h.2⟩⟩ instance (n : ℕ) : decidable (some n).dom := is_true trivial lemma some_eq_coe (n : ℕ) : some n = n := begin induction n with n ih, { refl }, apply part.ext', { show true ↔ ((n : enat).dom ∧ true), rw [← ih, and_true], exact iff.rfl }, { intros h H, show n.succ = (n : enat).get H.1 + 1, rw [nat.cast_succ] at H, revert H, simp only [← ih], intro, refl }, end @[simp] lemma coe_inj {x y : ℕ} : (x : enat) = y ↔ x = y := by simpa only [← some_eq_coe] using part.some_inj @[simp] lemma dom_some (x : ℕ) : (some x).dom := trivial @[simp] lemma dom_coe (x : ℕ) : (x : enat).dom := by rw [← some_eq_coe]; trivial instance : add_comm_monoid enat := { add := (+), zero := (0), add_comm := λ x y, part.ext' and.comm (λ _ _, add_comm _ _), zero_add := λ x, part.ext' (true_and _) (λ _ _, zero_add _), add_zero := λ x, part.ext' (and_true _) (λ _ _, add_zero _), add_assoc := λ x y z, part.ext' and.assoc (λ _ _, add_assoc _ _ _) } instance : has_le enat := ⟨λ x y, ∃ h : y.dom → x.dom, ∀ hy : y.dom, x.get (h hy) ≤ y.get hy⟩ instance : has_top enat := ⟨none⟩ instance : has_bot enat := ⟨0⟩ instance : has_sup enat := ⟨λ x y, ⟨x.dom ∧ y.dom, λ h, x.get h.1 ⊔ y.get h.2⟩⟩ lemma le_def (x y : enat) : x ≤ y ↔ ∃ h : y.dom → x.dom, ∀ hy : y.dom, x.get (h hy) ≤ y.get hy := iff.rfl @[elab_as_eliminator] protected lemma cases_on' {P : enat → Prop} : ∀ a : enat, P ⊤ → (∀ n : ℕ, P (some n)) → P a := part.induction_on @[elab_as_eliminator] protected lemma cases_on {P : enat → Prop} : ∀ a : enat, P ⊤ → (∀ n : ℕ, P n) → P a := by { simp only [← some_eq_coe], exact enat.cases_on' } @[simp] lemma top_add (x : enat) : ⊤ + x = ⊤ := part.ext' (false_and _) (λ h, h.left.elim) @[simp] lemma add_top (x : enat) : x + ⊤ = ⊤ := by rw [add_comm, top_add] @[simp] lemma coe_get {x : enat} (h : x.dom) : (x.get h : enat) = x := by { rw [← some_eq_coe], exact part.ext' (iff_of_true trivial h) (λ _ _, rfl) } @[simp, norm_cast] lemma get_coe' (x : ℕ) (h : (x : enat).dom) : get (x : enat) h = x := by rw [← coe_inj, coe_get] lemma get_coe {x : ℕ} : get (x : enat) (dom_coe x) = x := get_coe' _ _ lemma coe_add_get {x : ℕ} {y : enat} (h : ((x : enat) + y).dom) : get ((x : enat) + y) h = x + get y h.2 := by { simp only [← some_eq_coe] at h ⊢, refl } @[simp] lemma get_add {x y : enat} (h : (x + y).dom) : get (x + y) h = x.get h.1 + y.get h.2 := rfl @[simp] lemma get_zero (h : (0 : enat).dom) : (0 : enat).get h = 0 := rfl @[simp] lemma get_one (h : (1 : enat).dom) : (1 : enat).get h = 1 := rfl lemma get_eq_iff_eq_some {a : enat} {ha : a.dom} {b : ℕ} : a.get ha = b ↔ a = some b := get_eq_iff_eq_some lemma get_eq_iff_eq_coe {a : enat} {ha : a.dom} {b : ℕ} : a.get ha = b ↔ a = b := by rw [get_eq_iff_eq_some, some_eq_coe] lemma dom_of_le_of_dom {x y : enat} : x ≤ y → y.dom → x.dom := λ ⟨h, _⟩, h lemma dom_of_le_some {x : enat} {y : ℕ} (h : x ≤ some y) : x.dom := dom_of_le_of_dom h trivial lemma dom_of_le_coe {x : enat} {y : ℕ} (h : x ≤ y) : x.dom := by { rw [← some_eq_coe] at h, exact dom_of_le_some h } instance decidable_le (x y : enat) [decidable x.dom] [decidable y.dom] : decidable (x ≤ y) := if hx : x.dom then decidable_of_decidable_of_iff (show decidable (∀ (hy : (y : enat).dom), x.get hx ≤ (y : enat).get hy), from forall_prop_decidable _) $ by { dsimp [(≤)], simp only [hx, exists_prop_of_true, forall_true_iff] } else if hy : y.dom then is_false $ λ h, hx $ dom_of_le_of_dom h hy else is_true ⟨λ h, (hy h).elim, λ h, (hy h).elim⟩ /-- The coercion `ℕ → enat` preserves `0` and addition. -/ def coe_hom : ℕ →+ enat := ⟨coe, nat.cast_zero, nat.cast_add⟩ @[simp] lemma coe_coe_hom : ⇑coe_hom = coe := rfl instance : partial_order enat := { le := (≤), le_refl := λ x, ⟨id, λ _, le_rfl⟩, le_trans := λ x y z ⟨hxy₁, hxy₂⟩ ⟨hyz₁, hyz₂⟩, ⟨hxy₁ ∘ hyz₁, λ _, le_trans (hxy₂ _) (hyz₂ _)⟩, le_antisymm := λ x y ⟨hxy₁, hxy₂⟩ ⟨hyx₁, hyx₂⟩, part.ext' ⟨hyx₁, hxy₁⟩ (λ _ _, le_antisymm (hxy₂ _) (hyx₂ _)) } lemma lt_def (x y : enat) : x < y ↔ ∃ (hx : x.dom), ∀ (hy : y.dom), x.get hx < y.get hy := begin rw [lt_iff_le_not_le, le_def, le_def, not_exists], split, { rintro ⟨⟨hyx, H⟩, h⟩, by_cases hx : x.dom, { use hx, intro hy, specialize H hy, specialize h (λ _, hy), rw not_forall at h, cases h with hx' h, rw not_le at h, exact h }, { specialize h (λ hx', (hx hx').elim), rw not_forall at h, cases h with hx' h, exact (hx hx').elim } }, { rintro ⟨hx, H⟩, exact ⟨⟨λ _, hx, λ hy, (H hy).le⟩, λ hxy h, not_lt_of_le (h _) (H _)⟩ } end @[simp, norm_cast] lemma coe_le_coe {x y : ℕ} : (x : enat) ≤ y ↔ x ≤ y := by { rw [← some_eq_coe, ← some_eq_coe], exact ⟨λ ⟨_, h⟩, h trivial, λ h, ⟨λ _, trivial, λ _, h⟩⟩ } @[simp, norm_cast] lemma coe_lt_coe {x y : ℕ} : (x : enat) < y ↔ x < y := by rw [lt_iff_le_not_le, lt_iff_le_not_le, coe_le_coe, coe_le_coe] @[simp] lemma get_le_get {x y : enat} {hx : x.dom} {hy : y.dom} : x.get hx ≤ y.get hy ↔ x ≤ y := by conv { to_lhs, rw [← coe_le_coe, coe_get, coe_get]} lemma le_coe_iff (x : enat) (n : ℕ) : x ≤ n ↔ ∃ h : x.dom, x.get h ≤ n := begin rw [← some_eq_coe], show (∃ (h : true → x.dom), _) ↔ ∃ h : x.dom, x.get h ≤ n, simp only [forall_prop_of_true, some_eq_coe, dom_coe, get_coe'] end lemma lt_coe_iff (x : enat) (n : ℕ) : x < n ↔ ∃ h : x.dom, x.get h < n := by simp only [lt_def, forall_prop_of_true, get_coe', dom_coe] lemma coe_le_iff (n : ℕ) (x : enat) : (n : enat) ≤ x ↔ ∀ h : x.dom, n ≤ x.get h := begin rw [← some_eq_coe], simp only [le_def, exists_prop_of_true, dom_some, forall_true_iff], refl, end lemma coe_lt_iff (n : ℕ) (x : enat) : (n : enat) < x ↔ ∀ h : x.dom, n < x.get h := begin rw [← some_eq_coe], simp only [lt_def, exists_prop_of_true, dom_some, forall_true_iff], refl, end protected lemma zero_lt_one : (0 : enat) < 1 := by { norm_cast, norm_num } instance semilattice_sup : semilattice_sup enat := { sup := (⊔), le_sup_left := λ _ _, ⟨and.left, λ _, le_sup_left⟩, le_sup_right := λ _ _, ⟨and.right, λ _, le_sup_right⟩, sup_le := λ x y z ⟨hx₁, hx₂⟩ ⟨hy₁, hy₂⟩, ⟨λ hz, ⟨hx₁ hz, hy₁ hz⟩, λ _, sup_le (hx₂ _) (hy₂ _)⟩, ..enat.partial_order } instance order_bot : order_bot enat := { bot := (⊥), bot_le := λ _, ⟨λ _, trivial, λ _, nat.zero_le _⟩ } instance order_top : order_top enat := { top := (⊤), le_top := λ x, ⟨λ h, false.elim h, λ hy, false.elim hy⟩ } lemma dom_of_lt {x y : enat} : x < y → x.dom := enat.cases_on x not_top_lt $ λ _ _, dom_coe _ lemma top_eq_none : (⊤ : enat) = none := rfl @[simp] lemma coe_lt_top (x : ℕ) : (x : enat) < ⊤ := ne.lt_top (λ h, absurd (congr_arg dom h) $ by simpa only [dom_coe] using true_ne_false) @[simp] lemma coe_ne_top (x : ℕ) : (x : enat) ≠ ⊤ := ne_of_lt (coe_lt_top x) lemma ne_top_iff {x : enat} : x ≠ ⊤ ↔ ∃ (n : ℕ), x = n := by simpa only [← some_eq_coe] using part.ne_none_iff lemma ne_top_iff_dom {x : enat} : x ≠ ⊤ ↔ x.dom := by classical; exact not_iff_comm.1 part.eq_none_iff'.symm lemma ne_top_of_lt {x y : enat} (h : x < y) : x ≠ ⊤ := ne_of_lt $ lt_of_lt_of_le h le_top lemma eq_top_iff_forall_lt (x : enat) : x = ⊤ ↔ ∀ n : ℕ, (n : enat) < x := begin split, { rintro rfl n, exact coe_lt_top _ }, { contrapose!, rw ne_top_iff, rintro ⟨n, rfl⟩, exact ⟨n, irrefl _⟩ } end lemma eq_top_iff_forall_le (x : enat) : x = ⊤ ↔ ∀ n : ℕ, (n : enat) ≤ x := (eq_top_iff_forall_lt x).trans ⟨λ h n, (h n).le, λ h n, lt_of_lt_of_le (coe_lt_coe.mpr n.lt_succ_self) (h (n + 1))⟩ lemma pos_iff_one_le {x : enat} : 0 < x ↔ 1 ≤ x := enat.cases_on x (by simp only [iff_true, le_top, coe_lt_top, ← @nat.cast_zero enat]) $ λ n, by { rw [← nat.cast_zero, ← nat.cast_one, enat.coe_lt_coe, enat.coe_le_coe], refl } instance : is_total enat (≤) := { total := λ x y, enat.cases_on x (or.inr le_top) (enat.cases_on y (λ _, or.inl le_top) (λ x y, (le_total x y).elim (or.inr ∘ coe_le_coe.2) (or.inl ∘ coe_le_coe.2))) } noncomputable instance : linear_order enat := { le_total := is_total.total, decidable_le := classical.dec_rel _, max := (⊔), max_def := @sup_eq_max_default _ _ (id _) _, ..enat.partial_order } instance : bounded_order enat := { ..enat.order_top, ..enat.order_bot } noncomputable instance : lattice enat := { inf := min, inf_le_left := min_le_left, inf_le_right := min_le_right, le_inf := λ _ _ _, le_min, ..enat.semilattice_sup } instance : ordered_add_comm_monoid enat := { add_le_add_left := λ a b ⟨h₁, h₂⟩ c, enat.cases_on c (by simp) (λ c, ⟨λ h, and.intro (dom_coe _) (h₁ h.2), λ h, by simpa only [coe_add_get] using add_le_add_left (h₂ _) c⟩), ..enat.linear_order, ..enat.add_comm_monoid } instance : canonically_ordered_add_monoid enat := { le_iff_exists_add := λ a b, enat.cases_on b (iff_of_true le_top ⟨⊤, (add_top _).symm⟩) (λ b, enat.cases_on a (iff_of_false (not_le_of_gt (coe_lt_top _)) (not_exists.2 (λ x, ne_of_lt (by rw [top_add]; exact coe_lt_top _)))) (λ a, ⟨λ h, ⟨(b - a : ℕ), by rw [← nat.cast_add, coe_inj, add_comm, tsub_add_cancel_of_le (coe_le_coe.1 h)]⟩, (λ ⟨c, hc⟩, enat.cases_on c (λ hc, hc.symm ▸ show (a : enat) ≤ a + ⊤, by rw [add_top]; exact le_top) (λ c (hc : (b : enat) = a + c), coe_le_coe.2 (by rw [← nat.cast_add, coe_inj] at hc; rw hc; exact nat.le_add_right _ _)) hc)⟩)), ..enat.semilattice_sup, ..enat.order_bot, ..enat.ordered_add_comm_monoid } protected lemma add_lt_add_right {x y z : enat} (h : x < y) (hz : z ≠ ⊤) : x + z < y + z := begin rcases ne_top_iff.mp (ne_top_of_lt h) with ⟨m, rfl⟩, rcases ne_top_iff.mp hz with ⟨k, rfl⟩, induction y using enat.cases_on with n, { rw [top_add], apply_mod_cast coe_lt_top }, norm_cast at h, apply_mod_cast add_lt_add_right h end protected lemma add_lt_add_iff_right {x y z : enat} (hz : z ≠ ⊤) : x + z < y + z ↔ x < y := ⟨lt_of_add_lt_add_right, λ h, enat.add_lt_add_right h hz⟩ protected lemma add_lt_add_iff_left {x y z : enat} (hz : z ≠ ⊤) : z + x < z + y ↔ x < y := by rw [add_comm z, add_comm z, enat.add_lt_add_iff_right hz] protected lemma lt_add_iff_pos_right {x y : enat} (hx : x ≠ ⊤) : x < x + y ↔ 0 < y := by { conv_rhs { rw [← enat.add_lt_add_iff_left hx] }, rw [add_zero] } lemma lt_add_one {x : enat} (hx : x ≠ ⊤) : x < x + 1 := by { rw [enat.lt_add_iff_pos_right hx], norm_cast, norm_num } lemma le_of_lt_add_one {x y : enat} (h : x < y + 1) : x ≤ y := begin induction y using enat.cases_on with n, apply le_top, rcases ne_top_iff.mp (ne_top_of_lt h) with ⟨m, rfl⟩, apply_mod_cast nat.le_of_lt_succ, apply_mod_cast h end lemma add_one_le_of_lt {x y : enat} (h : x < y) : x + 1 ≤ y := begin induction y using enat.cases_on with n, apply le_top, rcases ne_top_iff.mp (ne_top_of_lt h) with ⟨m, rfl⟩, apply_mod_cast nat.succ_le_of_lt, apply_mod_cast h end lemma add_one_le_iff_lt {x y : enat} (hx : x ≠ ⊤) : x + 1 ≤ y ↔ x < y := begin split, swap, exact add_one_le_of_lt, intro h, rcases ne_top_iff.mp hx with ⟨m, rfl⟩, induction y using enat.cases_on with n, apply coe_lt_top, apply_mod_cast nat.lt_of_succ_le, apply_mod_cast h end lemma lt_add_one_iff_lt {x y : enat} (hx : x ≠ ⊤) : x < y + 1 ↔ x ≤ y := begin split, exact le_of_lt_add_one, intro h, rcases ne_top_iff.mp hx with ⟨m, rfl⟩, induction y using enat.cases_on with n, { rw [top_add], apply coe_lt_top }, apply_mod_cast nat.lt_succ_of_le, apply_mod_cast h end lemma add_eq_top_iff {a b : enat} : a + b = ⊤ ↔ a = ⊤ ∨ b = ⊤ := by apply enat.cases_on a; apply enat.cases_on b; simp; simp only [(nat.cast_add _ _).symm, enat.coe_ne_top]; simp protected lemma add_right_cancel_iff {a b c : enat} (hc : c ≠ ⊤) : a + c = b + c ↔ a = b := begin rcases ne_top_iff.1 hc with ⟨c, rfl⟩, apply enat.cases_on a; apply enat.cases_on b; simp [add_eq_top_iff, coe_ne_top, @eq_comm _ (⊤ : enat)]; simp only [(nat.cast_add _ _).symm, add_left_cancel_iff, enat.coe_inj, add_comm]; tauto end protected lemma add_left_cancel_iff {a b c : enat} (ha : a ≠ ⊤) : a + b = a + c ↔ b = c := by rw [add_comm a, add_comm a, enat.add_right_cancel_iff ha] section with_top /-- Computably converts an `enat` to a `with_top ℕ`. -/ def to_with_top (x : enat) [decidable x.dom] : with_top ℕ := x.to_option lemma to_with_top_top : to_with_top ⊤ = ⊤ := rfl @[simp] lemma to_with_top_top' {h : decidable (⊤ : enat).dom} : to_with_top ⊤ = ⊤ := by convert to_with_top_top lemma to_with_top_zero : to_with_top 0 = 0 := rfl @[simp] lemma to_with_top_zero' {h : decidable (0 : enat).dom} : to_with_top 0 = 0 := by convert to_with_top_zero lemma to_with_top_some (n : ℕ) : to_with_top (some n) = n := rfl lemma to_with_top_coe (n : ℕ) {_ : decidable (n : enat).dom} : to_with_top n = n := by simp only [← some_eq_coe, ← to_with_top_some] @[simp] lemma to_with_top_coe' (n : ℕ) {h : decidable (n : enat).dom} : to_with_top (n : enat) = n := by convert to_with_top_coe n @[simp] lemma to_with_top_le {x y : enat} : Π [decidable x.dom] [decidable y.dom], by exactI to_with_top x ≤ to_with_top y ↔ x ≤ y := enat.cases_on y (by simp) (enat.cases_on x (by simp) (by intros; simp)) @[simp] lemma to_with_top_lt {x y : enat} [decidable x.dom] [decidable y.dom] : to_with_top x < to_with_top y ↔ x < y := lt_iff_lt_of_le_iff_le to_with_top_le end with_top section with_top_equiv open_locale classical @[simp] lemma to_with_top_add {x y : enat} : to_with_top (x + y) = to_with_top x + to_with_top y := by apply enat.cases_on y; apply enat.cases_on x; simp [← nat.cast_add, ← with_top.coe_add] /-- `equiv` between `enat` and `with_top ℕ` (for the order isomorphism see `with_top_order_iso`). -/ noncomputable def with_top_equiv : enat ≃ with_top ℕ := { to_fun := λ x, to_with_top x, inv_fun := λ x, match x with (option.some n) := coe n | none := ⊤ end, left_inv := λ x, by apply enat.cases_on x; intros; simp; refl, right_inv := λ x, by cases x; simp [with_top_equiv._match_1]; refl } @[simp] lemma with_top_equiv_top : with_top_equiv ⊤ = ⊤ := to_with_top_top' @[simp] lemma with_top_equiv_coe (n : nat) : with_top_equiv n = n := to_with_top_coe' _ @[simp] lemma with_top_equiv_zero : with_top_equiv 0 = 0 := by simpa only [nat.cast_zero] using with_top_equiv_coe 0 @[simp] lemma with_top_equiv_le {x y : enat} : with_top_equiv x ≤ with_top_equiv y ↔ x ≤ y := to_with_top_le @[simp] lemma with_top_equiv_lt {x y : enat} : with_top_equiv x < with_top_equiv y ↔ x < y := to_with_top_lt /-- `to_with_top` induces an order isomorphism between `enat` and `with_top ℕ`. -/ noncomputable def with_top_order_iso : enat ≃o with_top ℕ := { map_rel_iff' := λ _ _, with_top_equiv_le, .. with_top_equiv} @[simp] lemma with_top_equiv_symm_top : with_top_equiv.symm ⊤ = ⊤ := rfl @[simp] lemma with_top_equiv_symm_coe (n : nat) : with_top_equiv.symm n = n := rfl @[simp] lemma with_top_equiv_symm_zero : with_top_equiv.symm 0 = 0 := rfl @[simp] lemma with_top_equiv_symm_le {x y : with_top ℕ} : with_top_equiv.symm x ≤ with_top_equiv.symm y ↔ x ≤ y := by rw ← with_top_equiv_le; simp @[simp] lemma with_top_equiv_symm_lt {x y : with_top ℕ} : with_top_equiv.symm x < with_top_equiv.symm y ↔ x < y := by rw ← with_top_equiv_lt; simp /-- `to_with_top` induces an additive monoid isomorphism between `enat` and `with_top ℕ`. -/ noncomputable def with_top_add_equiv : enat ≃+ with_top ℕ := { map_add' := λ x y, by simp only [with_top_equiv]; convert to_with_top_add, ..with_top_equiv} end with_top_equiv lemma lt_wf : well_founded ((<) : enat → enat → Prop) := show well_founded (λ a b : enat, a < b), by haveI := classical.dec; simp only [to_with_top_lt.symm] {eta := ff}; exact inv_image.wf _ (with_top.well_founded_lt nat.lt_wf) instance : has_well_founded enat := ⟨(<), lt_wf⟩ section find variables (P : ℕ → Prop) [decidable_pred P] /-- The smallest `enat` satisfying a (decidable) predicate `P : ℕ → Prop` -/ def find : enat := ⟨∃ n, P n, nat.find⟩ @[simp] lemma find_get (h : (find P).dom) : (find P).get h = nat.find h := rfl lemma find_dom (h : ∃ n, P n) : (find P).dom := h lemma lt_find (n : ℕ) (h : ∀ m ≤ n, ¬P m) : (n : enat) < find P := begin rw coe_lt_iff, intro h', rw find_get, have := @nat.find_spec P _ h', contrapose! this, exact h _ this end lemma lt_find_iff (n : ℕ) : (n : enat) < find P ↔ (∀ m ≤ n, ¬P m) := begin refine ⟨_, lt_find P n⟩, intros h m hm, by_cases H : (find P).dom, { apply nat.find_min H, rw coe_lt_iff at h, specialize h H, exact lt_of_le_of_lt hm h }, { exact not_exists.mp H m } end lemma find_le (n : ℕ) (h : P n) : find P ≤ n := by { rw le_coe_iff, refine ⟨⟨_, h⟩, @nat.find_min' P _ _ _ h⟩ } lemma find_eq_top_iff : find P = ⊤ ↔ ∀ n, ¬P n := (eq_top_iff_forall_lt _).trans ⟨λ h n, (lt_find_iff P n).mp (h n) _ le_rfl, λ h n, lt_find P n $ λ _ _, h _⟩ end find noncomputable instance : linear_ordered_add_comm_monoid_with_top enat := { top_add' := top_add, .. enat.linear_order, .. enat.ordered_add_comm_monoid, .. enat.order_top } end enat
9a5964415e36d0152f2b33f02b9ce397123d1e86
367134ba5a65885e863bdc4507601606690974c1
/src/data/set/intervals/pi.lean
a273017aa549aca6e81ada4cfa9a70c5e6f22039
[ "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
5,432
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 data.set.intervals.basic import data.set.lattice /-! # Intervals in `pi`-space In this we prove various simple lemmas about intervals in `Π i, α i`. Closed intervals (`Ici x`, `Iic x`, `Icc x y`) are equal to products of their projections to `α i`, while (semi-)open intervals usually include the corresponding products as proper subsets. -/ variables {ι : Type*} {α : ι → Type*} namespace set section pi_preorder variables [Π i, preorder (α i)] (x y : Π i, α i) @[simp] lemma pi_univ_Ici : pi univ (λ i, Ici (x i)) = Ici x := ext $ λ y, by simp [pi.le_def] @[simp] lemma pi_univ_Iic : pi univ (λ i, Iic (x i)) = Iic x := ext $ λ y, by simp [pi.le_def] @[simp] lemma pi_univ_Icc : pi univ (λ i, Icc (x i) (y i)) = Icc x y := ext $ λ y, by simp [pi.le_def, forall_and_distrib] section nonempty variable [nonempty ι] lemma pi_univ_Ioi_subset : pi univ (λ i, Ioi (x i)) ⊆ Ioi x := λ z hz, ⟨λ i, le_of_lt $ hz i trivial, λ h, nonempty.elim ‹nonempty ι› $ λ i, (h i).not_lt (hz i trivial)⟩ lemma pi_univ_Iio_subset : pi univ (λ i, Iio (x i)) ⊆ Iio x := @pi_univ_Ioi_subset ι (λ i, order_dual (α i)) _ x _ lemma pi_univ_Ioo_subset : pi univ (λ i, Ioo (x i) (y i)) ⊆ Ioo x y := λ x hx, ⟨pi_univ_Ioi_subset _ $ λ i hi, (hx i hi).1, pi_univ_Iio_subset _ $ λ i hi, (hx i hi).2⟩ lemma pi_univ_Ioc_subset : pi univ (λ i, Ioc (x i) (y i)) ⊆ Ioc x y := λ x hx, ⟨pi_univ_Ioi_subset _ $ λ i hi, (hx i hi).1, λ i, (hx i trivial).2⟩ lemma pi_univ_Ico_subset : pi univ (λ i, Ico (x i) (y i)) ⊆ Ico x y := λ x hx, ⟨λ i, (hx i trivial).1, pi_univ_Iio_subset _ $ λ i hi, (hx i hi).2⟩ end nonempty variable [decidable_eq ι] open function (update) lemma pi_univ_Ioc_update_left {x y : Π i, α i} {i₀ : ι} {m : α i₀} (hm : x i₀ ≤ m) : pi univ (λ i, Ioc (update x i₀ m i) (y i)) = {z | m < z i₀} ∩ pi univ (λ i, Ioc (x i) (y i)) := begin have : Ioc m (y i₀) = Ioi m ∩ Ioc (x i₀) (y i₀), by rw [← Ioi_inter_Iic, ← Ioi_inter_Iic, ← inter_assoc, inter_eq_self_of_subset_left (Ioi_subset_Ioi hm)], simp_rw [univ_pi_update i₀ _ _ (λ i z, Ioc z (y i)), ← pi_inter_compl ({i₀} : set ι), singleton_pi', ← inter_assoc, this], refl end lemma pi_univ_Ioc_update_right {x y : Π i, α i} {i₀ : ι} {m : α i₀} (hm : m ≤ y i₀) : pi univ (λ i, Ioc (x i) (update y i₀ m i)) = {z | z i₀ ≤ m} ∩ pi univ (λ i, Ioc (x i) (y i)) := begin have : Ioc (x i₀) m = Iic m ∩ Ioc (x i₀) (y i₀), by rw [← Ioi_inter_Iic, ← Ioi_inter_Iic, inter_left_comm, inter_eq_self_of_subset_left (Iic_subset_Iic.2 hm)], simp_rw [univ_pi_update i₀ y m (λ i z, Ioc (x i) z), ← pi_inter_compl ({i₀} : set ι), singleton_pi', ← inter_assoc, this], refl end lemma disjoint_pi_univ_Ioc_update_left_right {x y : Π i, α i} {i₀ : ι} {m : α i₀} : disjoint (pi univ (λ i, Ioc (x i) (update y i₀ m i))) (pi univ (λ i, Ioc (update x i₀ m i) (y i))) := begin rintro z ⟨h₁, h₂⟩, refine (h₁ i₀ (mem_univ _)).2.not_lt _, simpa only [function.update_same] using (h₂ i₀ (mem_univ _)).1 end end pi_preorder variables [decidable_eq ι] [Π i, linear_order (α i)] open function (update) lemma pi_univ_Ioc_update_union (x y : Π i, α i) (i₀ : ι) (m : α i₀) (hm : m ∈ Icc (x i₀) (y i₀)) : pi univ (λ i, Ioc (x i) (update y i₀ m i)) ∪ pi univ (λ i, Ioc (update x i₀ m i) (y i)) = pi univ (λ i, Ioc (x i) (y i)) := by simp_rw [pi_univ_Ioc_update_left hm.1, pi_univ_Ioc_update_right hm.2, ← union_inter_distrib_right, ← set_of_or, le_or_lt, set_of_true, univ_inter] /-- If `x`, `y`, `x'`, and `y'` are functions `Π i : ι, α i`, then the set difference between the box `[x, y]` and the product of the open intervals `(x' i, y' i)` is covered by the union of the following boxes: for each `i : ι`, we take `[x, update y i (x' i)]` and `[update x i (y' i), y]`. E.g., if `x' = x` and `y' = y`, then this lemma states that the difference between a closed box `[x, y]` and the corresponding open box `{z | ∀ i, x i < z i < y i}` is covered by the union of the faces of `[x, y]`. -/ lemma Icc_diff_pi_univ_Ioo_subset (x y x' y' : Π i, α i) : Icc x y \ pi univ (λ i, Ioo (x' i) (y' i)) ⊆ (⋃ i : ι, Icc x (update y i (x' i))) ∪ ⋃ i : ι, Icc (update x i (y' i)) y := begin rintros a ⟨⟨hxa, hay⟩, ha'⟩, simpa [le_update_iff, update_le_iff, hxa, hay, hxa _, hay _, ← exists_or_distrib, not_and_distrib] using ha' end /-- If `x`, `y`, `z` are functions `Π i : ι, α i`, then the set difference between the box `[x, z]` and the product of the intervals `(y i, z i]` is covered by the union of the boxes `[x, update z i (y i)]`. E.g., if `x = y`, then this lemma states that the difference between a closed box `[x, y]` and the product of half-open intervals `{z | ∀ i, x i < z i ≤ y i}` is covered by the union of the faces of `[x, y]` adjacent to `x`. -/ lemma Icc_diff_pi_univ_Ioc_subset (x y z : Π i, α i) : Icc x z \ pi univ (λ i, Ioc (y i) (z i)) ⊆ ⋃ i : ι, Icc x (update z i (y i)) := begin rintros a ⟨⟨hax, haz⟩, hay⟩, simpa [not_and_distrib, hax, le_update_iff, haz _] using hay end end set
6a83b418f7dcd230f34496a06a85bd4224c2cce1
7cef822f3b952965621309e88eadf618da0c8ae9
/src/topology/metric_space/lipschitz.lean
489094067786af1b84f63eb5a3bbc5e99a80aa13
[ "Apache-2.0" ]
permissive
rmitta/mathlib
8d90aee30b4db2b013e01f62c33f297d7e64a43d
883d974b608845bad30ae19e27e33c285200bf84
refs/heads/master
1,585,776,832,544
1,576,874,096,000
1,576,874,096,000
153,663,165
0
2
Apache-2.0
1,544,806,490,000
1,539,884,365,000
Lean
UTF-8
Lean
false
false
9,423
lean
/- Copyright (c) 2018 Rohan Mitta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rohan Mitta, Kevin Buzzard, Alistair Tucker, Johannes Hölzl, Yury Kudryashov -/ import topology.metric_space.basic import category_theory.endomorphism category_theory.types /-! # Lipschitz continuous functions A map `f : α → β` between two metric spaces is called *Lipschitz continuous* with constant `K ≥ 0` if for all `x, y` we have `dist (f x) (f y) ≤ K * dist x y`. In this file we provide various ways to prove that various combinations of Lipschitz continuous functions are Lipschitz continuous. We also prove that Lipschitz continuous functions are uniformly continuous. ## Implementation notes The parameter `K` has type `nnreal`; this way we avoid conjuction in the definition. Some constructors (`of_dist_le` and those ending with `'`) take `K : ℝ` as an argument, and return `lipschitz_with (nnreal.of_real K) f`. -/ universes u v w x open filter open_locale topological_space nnreal variables {α : Type u} {β : Type v} {γ : Type w} {ι : Type x} /-- A function `f` is Lipschitz continuous with constant `K ≥ 0` if for all `x, y` we have `dist (f x) (f y) ≤ K * dist x y` -/ def lipschitz_with [metric_space α] [metric_space β] (K : ℝ≥0) (f : α → β) := ∀x y, dist (f x) (f y) ≤ K * dist x y namespace lipschitz_with variables [metric_space α] [metric_space β] [metric_space γ] {K : ℝ≥0} protected lemma of_dist_le {f : α → β} {K : ℝ} (h : ∀x y, dist (f x) (f y) ≤ K * dist x y) : lipschitz_with (nnreal.of_real K) f := λ x y, le_trans (h x y) (mul_le_mul_of_nonneg_right (nnreal.le_coe_of_real K) dist_nonneg) protected lemma one_mk {f : α → β} (h : ∀ x y, dist (f x) (f y) ≤ dist x y) : lipschitz_with 1 f := λ x y, by simp only [nnreal.coe_one, one_mul, h] /-- For functions to `ℝ`, it suffices to prove one of the two inequalities; this version doesn't assume `0≤K`. -/ protected lemma of_le_add' {f : α → ℝ} (K : ℝ) (h : ∀x y, f x ≤ f y + K * dist x y) : lipschitz_with (nnreal.of_real K) f := have I : ∀ x y, f x - f y ≤ K * dist x y, from assume x y, sub_le_iff_le_add'.2 (h x y), lipschitz_with.of_dist_le $ assume x y, abs_sub_le_iff.2 ⟨I x y, dist_comm y x ▸ I y x⟩ /-- For functions to `ℝ`, it suffices to prove one of the two inequalities; this version assumes `0≤K`. -/ protected lemma of_le_add {f : α → ℝ} (K : ℝ≥0) (h : ∀x y, f x ≤ f y + K * dist x y) : lipschitz_with K f := by simpa only [nnreal.of_real_coe] using lipschitz_with.of_le_add' K h protected lemma one_of_le_add {f : α → ℝ} (h : ∀ x y, f x ≤ f y + dist x y) : lipschitz_with 1 f := lipschitz_with.of_le_add 1 $ by simpa only [nnreal.coe_one, one_mul] protected lemma le_add {f : α → ℝ} {K : ℝ≥0} (h : lipschitz_with K f) (x y) : f x ≤ f y + K * dist x y := sub_le_iff_le_add'.1 $ le_trans (le_abs_self _) $ h x y protected lemma iff_le_add {f : α → ℝ} {K : ℝ≥0} : lipschitz_with K f ↔ ∀ x y, f x ≤ f y + K * dist x y := ⟨lipschitz_with.le_add, lipschitz_with.of_le_add K⟩ section variables {f : α → β} (hf : lipschitz_with K f) include hf lemma nndist_map_le (x y : α) : nndist (f x) (f y) ≤ K * nndist x y := hf x y lemma edist_map_le (x y : α) : edist (f x) (f y) ≤ K * edist x y := by simp only [edist_nndist, ennreal.coe_le_coe, ennreal.coe_mul.symm, hf.nndist_map_le] lemma ediam_image_le (s : set α) : emetric.diam (f '' s) ≤ K * emetric.diam s := begin apply emetric.diam_le_of_forall_edist_le, rintros _ _ ⟨x, hx, rfl⟩ ⟨y, hy, rfl⟩, calc edist (f x) (f y) ≤ ↑K * edist x y : hf.edist_map_le x y ... ≤ ↑K * emetric.diam s : canonically_ordered_semiring.mul_le_mul (le_refl _) (emetric.edist_le_diam_of_mem hx hy) end lemma diam_image_le (s : set α) (hs : metric.bounded s) : metric.diam (f '' s) ≤ K * metric.diam s := begin apply metric.diam_le_of_forall_dist_le (mul_nonneg K.2 metric.diam_nonneg), rintros _ _ ⟨x, hx, rfl⟩ ⟨y, hy, rfl⟩, calc dist (f x) (f y) ≤ ↑K * dist x y : hf x y ... ≤ ↑K * metric.diam s : mul_le_mul_of_nonneg_left (metric.dist_le_diam_of_mem hs hx hy) K.2 end protected lemma weaken {K' : ℝ≥0} (h : K ≤ K') : lipschitz_with K' f := assume x y, le_trans (hf x y) $ mul_le_mul_of_nonneg_right h dist_nonneg /-- A Lipschitz function is uniformly continuous -/ protected lemma to_uniform_continuous : uniform_continuous f := begin have : (0:ℝ) < max K 1 := lt_of_lt_of_le zero_lt_one (le_max_right K 1), refine metric.uniform_continuous_iff.2 (λε εpos, _), exact ⟨ε/max K 1, div_pos εpos this, assume y x Dyx, calc dist (f y) (f x) ≤ K * dist y x : hf y x ... ≤ max K 1 * dist y x : mul_le_mul_of_nonneg_right (le_max_left K 1) (dist_nonneg) ... < max K 1 * (ε/max K 1) : mul_lt_mul_of_pos_left Dyx this ... = ε : mul_div_cancel' _ (ne_of_gt this)⟩ end /-- A Lipschitz function is continuous -/ protected lemma to_continuous {f : α → β} (hf : lipschitz_with K f) : continuous f := hf.to_uniform_continuous.continuous end protected lemma const (b : β) : lipschitz_with 0 (λa:α, b) := assume x y, by simp only [zero_mul, dist_self, nnreal.coe_zero] protected lemma id : lipschitz_with 1 (@id α) := lipschitz_with.one_mk $ assume x y, le_refl _ protected lemma subtype_val (s : set α) : lipschitz_with 1 (subtype.val : s → α) := lipschitz_with.one_mk $ assume x y, le_refl _ protected lemma subtype_coe (s : set α) : lipschitz_with 1 (coe : s → α) := lipschitz_with.subtype_val s protected lemma comp {Kf Kg : ℝ≥0} {f : β → γ} {g : α → β} (hf : lipschitz_with Kf f) (hg : lipschitz_with Kg g) : lipschitz_with (Kf * Kg) (f ∘ g) := assume x y, calc dist (f (g x)) (f (g y)) ≤ Kf * dist (g x) (g y) : hf _ _ ... ≤ Kf * (Kg * dist x y) : mul_le_mul_of_nonneg_left (hg _ _) Kf.2 ... = (Kf * Kg) * dist x y : by rw mul_assoc protected lemma prod_fst : lipschitz_with 1 (@prod.fst α β) := lipschitz_with.one_mk $ assume x y, le_max_left _ _ protected lemma prod_snd : lipschitz_with 1 (@prod.snd α β) := lipschitz_with.one_mk $ assume x y, le_max_right _ _ protected lemma prod {f : α → β} {Kf : ℝ≥0} (hf : lipschitz_with Kf f) {g : α → γ} {Kg : ℝ≥0} (hg : lipschitz_with Kg g) : lipschitz_with (max Kf Kg) (λ x, (f x, g x)) := begin assume x y, simp only [nnreal.coe_mono.map_max, prod.dist_eq, max_mul_of_nonneg _ _ dist_nonneg], exact max_le_max (hf x y) (hg x y) end protected lemma uncurry' {f : α → β → γ} {Kα Kβ : ℝ≥0} (hα : ∀ b, lipschitz_with Kα (λ a, f a b)) (hβ : ∀ a, lipschitz_with Kβ (f a)) : lipschitz_with (Kα + Kβ) (function.uncurry' f) := begin rintros ⟨a₁, b₁⟩ ⟨a₂, b₂⟩, simp only [function.uncurry', nnreal.coe_add, add_mul], refine le_trans (dist_triangle _ (f a₂ b₁) _) (add_le_add _ _), { calc dist (f a₁ b₁) (f a₂ b₁) ≤ Kα * dist a₁ a₂ : hα _ _ _ ... ≤ Kα * dist (a₁, b₁) (a₂, b₂) : mul_le_mul_of_nonneg_left (le_max_left _ _) Kα.2 }, { calc dist (f a₂ b₁) (f a₂ b₂) ≤ Kβ * dist b₁ b₂ : hβ _ _ _ ... ≤ Kβ * dist (a₁, b₁) (a₂, b₂) : mul_le_mul_of_nonneg_left (le_max_right _ _) Kβ.2 } end protected lemma uncurry {f : α → β → γ} {Kα Kβ : ℝ≥0} (hα : ∀ b, lipschitz_with Kα (λ a, f a b)) (hβ : ∀ a, lipschitz_with Kβ (f a)) : lipschitz_with (Kα + Kβ) (function.uncurry f) := by { rw function.uncurry_def, apply lipschitz_with.uncurry'; assumption } protected lemma dist_left (y : α) : lipschitz_with 1 (λ x, dist x y) := lipschitz_with.one_of_le_add $ assume x z, by { rw [add_comm, dist_comm z], apply dist_triangle_right } protected lemma dist_right (x : α) : lipschitz_with 1 (dist x) := by { convert lipschitz_with.dist_left x, funext y, apply dist_comm } protected lemma dist : lipschitz_with 2 (function.uncurry' $ @dist α _) := lipschitz_with.uncurry' lipschitz_with.dist_left lipschitz_with.dist_right protected lemma iterate {f : α → α} (hf : lipschitz_with K f) : ∀n, lipschitz_with (K ^ n) (f^[n]) | 0 := lipschitz_with.id | (n + 1) := by rw [pow_succ']; exact (iterate n).comp hf lemma dist_iterate_succ_le_geometric {f : α → α} (hf : lipschitz_with K f) (x n) : dist (f^[n] x) (f^[n + 1] x) ≤ dist x (f x) * K ^ n := begin rw [nat.iterate_succ, mul_comm], simpa only [is_monoid_hom.map_pow (coe : ℝ≥0 → ℝ)] using (hf.iterate n) x (f x) end open category_theory protected lemma mul {f g : End α} {Kf Kg} (hf : lipschitz_with Kf f) (hg : lipschitz_with Kg g) : lipschitz_with (Kf * Kg) (f * g : End α) := hf.comp hg /-- The product of a list of Lipschitz continuous endomorphisms is a Lipschitz continuous endomorphism. -/ protected lemma list_prod (f : ι → End α) (K : ι → ℝ≥0) (h : ∀ i, lipschitz_with (K i) (f i)) : ∀ l : list ι, lipschitz_with (l.map K).prod (l.map f).prod | [] := by simp [lipschitz_with.id] | (i :: l) := by { simp only [list.map_cons, list.prod_cons], exact (h i).mul (list_prod l) } protected lemma pow {f : End α} {K} (h : lipschitz_with K f) : ∀ n : ℕ, lipschitz_with (K^n) (f^n : End α) | 0 := lipschitz_with.id | (n + 1) := h.mul (pow n) end lipschitz_with
624fca7131466a69fa1f02a7ea7bb347b143d5eb
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/topology/opens.lean
64212f12edf510cb08898a36e2adfbdec8bb1333
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,178
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, Floris van Doorn -/ import topology.bases import topology.homeomorph /-! # Open sets ## Summary We define the subtype of open sets in a topological space. ## Main Definitions - `opens α` is the type of open subsets of a topological space `α`. - `open_nhds_of x` is the type of open subsets of a topological space `α` containing `x : α`. - -/ open filter set variables {α : Type*} {β : Type*} {γ : Type*} [topological_space α] [topological_space β] [topological_space γ] namespace topological_space variable (α) /-- The type of open subsets of a topological space. -/ def opens := {s : set α // is_open s} variable {α} namespace opens instance : has_coe (opens α) (set α) := { coe := subtype.val } lemma val_eq_coe (U : opens α) : U.1 = ↑U := rfl /-- the coercion `opens α → set α` applied to a pair is the same as taking the first component -/ lemma coe_mk {α : Type*} [topological_space α] {U : set α} {hU : is_open U} : ↑(⟨U, hU⟩ : opens α) = U := rfl instance : has_subset (opens α) := { subset := λ U V, (U : set α) ⊆ V } instance : has_mem α (opens α) := { mem := λ a U, a ∈ (U : set α) } @[simp] lemma subset_coe {U V : opens α} : ((U : set α) ⊆ (V : set α)) = (U ⊆ V) := rfl @[simp] lemma mem_coe {x : α} {U : opens α} : (x ∈ (U : set α)) = (x ∈ U) := rfl @[ext] lemma ext {U V : opens α} (h : (U : set α) = V) : U = V := subtype.ext_iff.mpr h @[ext] lemma ext_iff {U V : opens α} : (U : set α) = V ↔ U = V := ⟨opens.ext, congr_arg coe⟩ instance : partial_order (opens α) := subtype.partial_order _ /-- The interior of a set, as an element of `opens`. -/ def interior (s : set α) : opens α := ⟨interior s, is_open_interior⟩ lemma gc : galois_connection (coe : opens α → set α) interior := λ U s, ⟨λ h, interior_maximal h U.property, λ h, le_trans h interior_subset⟩ open order_dual (of_dual to_dual) /-- The galois insertion between sets and opens, but ordered by reverse inclusion. -/ def gi : galois_insertion (to_dual ∘ @interior α _ ∘ of_dual) (to_dual ∘ subtype.val ∘ of_dual) := { choice := λ s hs, ⟨s, interior_eq_iff_open.mp $ le_antisymm interior_subset hs⟩, gc := gc.dual, le_l_u := λ _, interior_subset, choice_eq := λ s hs, le_antisymm interior_subset hs } @[simp] lemma gi_choice_val {s : order_dual (set α)} {hs} : (gi.choice s hs).val = s := rfl instance : complete_lattice (opens α) := complete_lattice.copy (@order_dual.complete_lattice _ (galois_insertion.lift_complete_lattice (@gi α _))) /- le -/ (λ U V, U ⊆ V) rfl /- top -/ ⟨set.univ, is_open_univ⟩ (subtype.ext_iff_val.mpr interior_univ.symm) /- bot -/ ⟨∅, is_open_empty⟩ rfl /- sup -/ (λ U V, ⟨↑U ∪ ↑V, is_open.union U.2 V.2⟩) rfl /- inf -/ (λ U V, ⟨↑U ∩ ↑V, is_open.inter U.2 V.2⟩) begin funext, apply subtype.ext_iff_val.mpr, exact (is_open.inter U.2 V.2).interior_eq.symm, end /- Sup -/ _ rfl /- Inf -/ _ rfl lemma le_def {U V : opens α} : U ≤ V ↔ (U : set α) ≤ (V : set α) := by refl @[simp] lemma mk_inf_mk {U V : set α} {hU : is_open U} {hV : is_open V} : (⟨U, hU⟩ ⊓ ⟨V, hV⟩ : opens α) = ⟨U ⊓ V, is_open.inter hU hV⟩ := rfl @[simp,norm_cast] lemma coe_inf {U V : opens α} : ((U ⊓ V : opens α) : set α) = (U : set α) ⊓ (V : set α) := rfl instance : has_inter (opens α) := ⟨λ U V, U ⊓ V⟩ instance : has_union (opens α) := ⟨λ U V, U ⊔ V⟩ instance : has_emptyc (opens α) := ⟨⊥⟩ instance : inhabited (opens α) := ⟨∅⟩ @[simp] lemma inter_eq (U V : opens α) : U ∩ V = U ⊓ V := rfl @[simp] lemma union_eq (U V : opens α) : U ∪ V = U ⊔ V := rfl @[simp] lemma empty_eq : (∅ : opens α) = ⊥ := rfl @[simp] lemma Sup_s {Us : set (opens α)} : ↑(Sup Us) = ⋃₀ ((coe : _ → set α) '' Us) := by { rw [(@gc α _).l_Sup, set.sUnion_image], refl } lemma supr_def {ι} (s : ι → opens α) : (⨆ i, s i) = ⟨⋃ i, s i, is_open_Union $ λ i, (s i).2⟩ := by { ext, simp only [supr, opens.Sup_s, sUnion_image, bUnion_range], refl } @[simp] lemma supr_mk {ι} (s : ι → set α) (h : Π i, is_open (s i)) : (⨆ i, ⟨s i, h i⟩ : opens α) = ⟨⋃ i, s i, is_open_Union h⟩ := by { rw supr_def, simp } @[simp] lemma supr_s {ι} (s : ι → opens α) : ((⨆ i, s i : opens α) : set α) = ⋃ i, s i := by simp [supr_def] @[simp] theorem mem_supr {ι} {x : α} {s : ι → opens α} : x ∈ supr s ↔ ∃ i, x ∈ s i := by { rw [←mem_coe], simp, } @[simp] lemma mem_Sup {Us : set (opens α)} {x : α} : x ∈ Sup Us ↔ ∃ u ∈ Us, x ∈ u := by simp_rw [Sup_eq_supr, mem_supr] lemma open_embedding_of_le {U V : opens α} (i : U ≤ V) : open_embedding (set.inclusion i) := { inj := set.inclusion_injective i, induced := (@induced_compose _ _ _ _ (set.inclusion i) coe).symm, open_range := begin rw set.range_inclusion i, exact U.property.preimage continuous_subtype_val end, } /-- A set of `opens α` is a basis if the set of corresponding sets is a topological basis. -/ def is_basis (B : set (opens α)) : Prop := is_topological_basis ((coe : _ → set α) '' B) lemma is_basis_iff_nbhd {B : set (opens α)} : is_basis B ↔ ∀ {U : opens α} {x}, x ∈ U → ∃ U' ∈ B, x ∈ U' ∧ U' ⊆ U := begin split; intro h, { rintros ⟨sU, hU⟩ x hx, rcases h.mem_nhds_iff.mp (is_open.mem_nhds hU hx) with ⟨sV, ⟨⟨V, H₁, H₂⟩, hsV⟩⟩, refine ⟨V, H₁, _⟩, cases V, dsimp at H₂, subst H₂, exact hsV }, { refine is_topological_basis_of_open_of_nhds _ _, { rintros sU ⟨U, ⟨H₁, H₂⟩⟩, subst H₂, exact U.property }, { intros x sU hx hsU, rcases @h (⟨sU, hsU⟩ : opens α) x hx with ⟨V, hV, H⟩, exact ⟨V, ⟨V, hV, rfl⟩, H⟩ } } end lemma is_basis_iff_cover {B : set (opens α)} : is_basis B ↔ ∀ U : opens α, ∃ Us ⊆ B, U = Sup Us := begin split, { intros hB U, refine ⟨{V : opens α | V ∈ B ∧ V ⊆ U}, λ U hU, hU.left, _⟩, apply ext, rw [Sup_s, hB.open_eq_sUnion' U.prop], simp_rw [sUnion_image, sUnion_eq_bUnion, Union, supr_and, supr_image], refl }, { intro h, rw is_basis_iff_nbhd, intros U x hx, rcases h U with ⟨Us, hUs, rfl⟩, rcases mem_Sup.1 hx with ⟨U, Us, xU⟩, exact ⟨U, hUs Us, xU, le_Sup Us⟩ } end /-- The preimage of an open set, as an open set. -/ def comap {f : α → β} (hf : continuous f) (V : opens β) : opens α := ⟨f ⁻¹' V.1, V.2.preimage hf⟩ @[simp] lemma comap_id (U : opens α) : U.comap continuous_id = U := by { ext, refl } lemma comap_mono {f : α → β} (hf : continuous f) {V W : opens β} (hVW : V ⊆ W) : V.comap hf ⊆ W.comap hf := λ _ h, hVW h @[simp] lemma coe_comap {f : α → β} (hf : continuous f) (U : opens β) : ↑(U.comap hf) = f ⁻¹' U := rfl @[simp] lemma comap_val {f : α → β} (hf : continuous f) (U : opens β) : (U.comap hf).1 = f ⁻¹' U := rfl protected lemma comap_comp {g : β → γ} {f : α → β} (hg : continuous g) (hf : continuous f) (U : opens γ) : U.comap (hg.comp hf) = (U.comap hg).comap hf := by { ext1, simp only [coe_comap, preimage_preimage] } /-- A homeomorphism induces an equivalence on open sets, by taking comaps. -/ @[simp] protected def equiv (f : α ≃ₜ β) : opens α ≃ opens β := { to_fun := opens.comap f.symm.continuous, inv_fun := opens.comap f.continuous, left_inv := by { intro U, ext1, simp only [coe_comap, ← preimage_comp, f.symm_comp_self, preimage_id] }, right_inv := by { intro U, ext1, simp only [coe_comap, ← preimage_comp, f.self_comp_symm, preimage_id] } } end opens /-- The open neighborhoods of a point. See also `opens` or `nhds`. -/ def open_nhds_of (x : α) : Type* := { s : set α // is_open s ∧ x ∈ s } instance open_nhds_of.inhabited {α : Type*} [topological_space α] (x : α) : inhabited (open_nhds_of x) := ⟨⟨set.univ, is_open_univ, set.mem_univ _⟩⟩ end topological_space
4b8e2a33dcffa4bfb4f69048f530be1444ac24ac
f5f7e6fae601a5fe3cac7cc3ed353ed781d62419
/src/data/finset.lean
9fc168969abaa0ed958c19cd19bbc7306700d223
[ "Apache-2.0" ]
permissive
EdAyers/mathlib
9ecfb2f14bd6caad748b64c9c131befbff0fb4e0
ca5d4c1f16f9c451cf7170b10105d0051db79e1b
refs/heads/master
1,626,189,395,845
1,555,284,396,000
1,555,284,396,000
144,004,030
0
0
Apache-2.0
1,533,727,664,000
1,533,727,663,000
null
UTF-8
Lean
false
false
81,231
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 logic.embedding order.boolean_algebra algebra.order_functions data.multiset data.sigma.basic data.set.lattice open multiset subtype nat lattice 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. -/ def to_set (s : finset α) : set α := {x | x ∈ s} instance : has_lift (finset α) (set α) := ⟨to_set⟩ @[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 /- extensionality -/ theorem ext {s₁ s₂ : finset α} : s₁ = s₂ ↔ ∀ a, a ∈ s₁ ↔ a ∈ s₂ := val_inj.symm.trans $ nodup_ext s₁.2 s₂.2 @[extensionality] 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 /- 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 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_iff_subset_not_subset, 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 /- empty -/ 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 @[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 exists_mem_of_ne_empty {s : finset α} (h : s ≠ ∅) : ∃ a : α, a ∈ s := exists_mem_of_ne_zero (mt val_eq_zero.1 h) @[simp] lemma coe_empty : ↑(∅ : finset α) = (∅ : set α) := rfl /-- `singleton a` is the set `{a}` containing `a` and nothing else. -/ def singleton (a : α) : finset α := ⟨_, nodup_singleton a⟩ local prefix `ι`:90 := singleton @[simp] theorem singleton_val (a : α) : (ι a).1 = a :: 0 := rfl @[simp] theorem mem_singleton {a b : α} : b ∈ ι a ↔ b = a := mem_singleton theorem not_mem_singleton {a b : α} : a ∉ ι b ↔ a ≠ b := not_iff_not_of_iff mem_singleton theorem mem_singleton_self (a : α) : a ∈ ι a := or.inl rfl theorem singleton_inj {a b : α} : ι a = ι b ↔ a = b := ⟨λ h, mem_singleton.1 (h ▸ mem_singleton_self _), congr_arg _⟩ @[simp] theorem singleton_ne_empty (a : α) : ι a ≠ ∅ := ne_empty_of_mem (mem_singleton_self _) @[simp] lemma coe_singleton (a : α) : ↑(ι a) = ({a} : set α) := rfl /- 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⟩⟩ @[simp] theorem has_insert_eq_insert (a : α) (s : finset α) : has_insert.insert a s = insert a s := rfl 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] @[simp] theorem insert_eq_of_mem {a : α} {s : finset α} (h : a ∈ s) : insert a s = s := eq_of_veq $ ndinsert_of_mem h 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) 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 [h : decidable_eq α] (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 @[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 @[simp] theorem singleton_eq_singleton (a : α) : _root_.singleton a = ι a := rfl @[simp] theorem insert_empty_eq_singleton (a : α) : {a} = ι a := rfl @[simp] theorem insert_singleton_self_eq (a : α) : ({a, a} : finset α) = ι a := insert_eq_of_mem $ mem_singleton_self _ /- 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 _ @[simp] 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₂)] @[simp] 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] /- 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 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] @[simp] theorem inter_left_comm (s₁ s₂ s₃ : finset α) : s₁ ∩ (s₂ ∩ s₃) = s₂ ∩ (s₁ ∩ s₃) := ext.2 $ λ _, by simp only [mem_inter, and.left_comm] @[simp] 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] 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] /- 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.lattice } instance {α : Type*} [decidable_eq α] : semilattice_sup_bot (finset α) := { ..finset.lattice.semilattice_inf_bot, ..finset.lattice.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.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 /- 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 @[simp] 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) @[simp] theorem union_sdiff_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁ ∪ (s₂ \ s₁) = s₂ := (union_comm _ _).trans (sdiff_union_of_subset h) @[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 _ _) 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₂) @[simp] lemma coe_sdiff (s₁ s₂ : finset α) : ↑(s₁ \ s₂) = (↑s₁ \ ↑s₂ : set α) := set.ext $ λ _, mem_sdiff 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 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_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⟩ 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_or (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 (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 (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 filter_union_filter_neg_eq (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] @[simp] lemma coe_filter (s : finset α) : ↑(s.filter p) = ({x ∈ ↑s | p x} : set α) := set.ext $ λ _, mem_filter 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 range_subset {n m} : range n ⊆ range m ↔ n ≤ m := range_subset theorem exists_nat_subset_range (s : finset ℕ) : ∃n : ℕ, s ⊆ range n := finset.induction_on s ⟨0, empty_subset _⟩ $ λ a s ha ⟨n, hn⟩, ⟨max (a + 1) n, insert_subset.2 ⟨by simpa only [mem_range] using le_max_left (a+1) n, subset.trans hn (by simpa only [range_subset] using le_max_right (a+1) n)⟩⟩ 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 namespace option /-- Construct an empty or singleton finset from an `option` -/ def to_finset (o : option α) : finset α := match o with | none := ∅ | some a := finset.singleton a end @[simp] theorem to_finset_none : none.to_finset = (∅ : finset α) := rfl @[simp] theorem to_finset_some {a : α} : (some a).to_finset = finset.singleton 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_smul (s : multiset α) : ∀(n : ℕ) (hn : n ≠ 0), (add_monoid.smul n s).to_finset = s.to_finset | 0 h := by contradiction | (n+1) h := begin by_cases n = 0, { rw [h, zero_add, add_monoid.one_smul] }, { rw [add_monoid.add_smul, to_finset_add, add_monoid.one_smul, to_finset_smul 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 section map open function 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 : α ↪ β) (s : finset α) : (∅ : 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 @[simp] theorem mem_map_of_mem (f : α ↪ β) {a} {s : finset α} : a ∈ s → f a ∈ s.map f := (mem_map' _).2 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] 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 : α) : (singleton a).map f = singleton (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, insert_empty_eq_singleton, 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] 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] @[simp] 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 $ by simp only [exists_prop]; refl 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] @[simp] 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_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 [decidable_eq α] (f : α → β) (a : α) : (singleton a).image f = singleton (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, insert_empty_eq_singleton, 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 [decidable_eq β] {s : finset α} (h : s ≠ ∅) (b : β) : s.image (λa, b) = singleton b := ext.2 $ assume b', by simp only [mem_image, exists_prop, exists_and_distrib_right, exists_mem_of_ne_empty h, true_and, mem_singleton, eq_comm] 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] 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 ≠ ∅ := pos_iff_ne_zero.trans $ not_congr card_eq_zero theorem card_eq_one {s : finset α} : s.card = 1 ↔ ∃ a, s = finset.singleton a := by cases s; simp [multiset.card_eq_one, finset.singleton, finset.card] @[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 (singleton a) = 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 @[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 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 lemma card_eq_of_bijective [decidable_eq α] {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 := 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 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 card s > 0, from eq.symm ▸ nat.zero_lt_succ _, let ⟨a, has⟩ := finset.exists_mem_of_ne_empty $ 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 [decidable_eq α] {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 [decidable_eq α] [decidable_eq β] {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 := 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 lemma card_le_of_inj_on [decidable_eq α] {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₂) @[elab_as_eliminator] lemma 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, by by_cases a ∈ s; simp * {contextual := tt}) 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 end card 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 [decidable_eq α] {a : α} : (singleton a).bind t = t a := show (insert a ∅ : finset α).bind t = t a, from bind_insert.trans $ union_empty _ 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_singleton {f : α → β} : s.bind (λa, {f a}) = s.image f := ext.2 $ λ x, by simp only [mem_bind, mem_image, insert_empty_eq_singleton, 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, simp, split, { rintros ⟨a, ⟨b', _, _⟩, hb, _⟩, exact hb }, { rintros hb, exact ⟨g b, ⟨b, hb, rfl⟩, hb, rfl⟩ } end end bind 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 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 section pi variables {δ : α → Type*} [decidable_eq α] 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 _ _ _ def pi.empty (β : α → Sort*) [decidable_eq α] (a : α) (h : a ∈ (∅ : finset α)) : β a := multiset.pi.empty β a h 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), by 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 end pi section powerset 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] 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) end powerset 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 : (singleton a).fold op b f = f a * b := rfl @[simp] theorem fold_map [decidable_eq α] {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 := by haveI := classical.prop_decidable; rw [fold, insert_val', ← fold_erase_dup_idem op, erase_dup_map_erase_dup_eq, fold_erase_dup_idem op]; simp only [map_cons, fold_cons_left, fold] end fold 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 [decidable_eq β] {b : β} : ({b} : finset β).sup f = f b := calc _ = f b ⊔ (∅:finset β).sup f : sup_insert ... = f b : sup_bot_eq 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 lemma sup_mono_fun {g : β → α} : (∀b∈s, f b ≤ g b) → s.sup f ≤ s.sup g := by letI := classical.dec_eq β; from finset.induction_on s (λ _, le_refl _) (λ a s has ih H, by simp only [mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] at H; simp only [sup_insert]; exact sup_le_sup H.1 (ih H.2)) lemma le_sup {b : β} (hb : b ∈ s) : f b ≤ s.sup f := by letI := classical.dec_eq β; from calc f b ≤ f b ⊔ s.sup f : le_sup_left ... = (insert b s).sup f : sup_insert.symm ... = s.sup f : by rw [insert_eq_of_mem hb] lemma sup_le {a : α} : (∀b ∈ s, f b ≤ a) → s.sup f ≤ a := by letI := classical.dec_eq β; from finset.induction_on s (λ _, bot_le) (λ n s hns ih H, by simp only [mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] at H; simp only [sup_insert]; exact sup_le H.1 (ih H.2)) lemma sup_le_iff {a : α} : s.sup f ≤ a ↔ (∀b ∈ s, f b ≤ a) := iff.intro (assume h b hb, le_trans (le_sup hb) h) sup_le lemma sup_mono (h : s₁ ⊆ s₂) : s₁.sup f ≤ s₂.sup f := sup_le $ assume b hb, le_sup (h hb) lemma sup_lt [is_total α (≤)] {a : α} : (⊥ < a) → (∀b ∈ s, f b < a) → s.sup f < a := by letI := classical.dec_eq β; from finset.induction_on s (by simp) (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}) 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) 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 @[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 [decidable_eq β] {b : β} : ({b} : finset β).inf f = f b := calc _ = f b ⊓ (∅:finset β).inf f : inf_insert ... = f b : inf_top_eq 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_mono_fun {g : β → α} : (∀b∈s, f b ≤ g b) → s.inf f ≤ s.inf g := by letI := classical.dec_eq β; from finset.induction_on s (λ _, le_refl _) (λ a s has ih H, by simp only [mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] at H; simp only [inf_insert]; exact inf_le_inf H.1 (ih H.2)) lemma inf_le {b : β} (hb : b ∈ s) : s.inf f ≤ f b := by letI := classical.dec_eq β; from calc f b ≥ f b ⊓ s.inf f : inf_le_left ... = (insert b s).inf f : inf_insert.symm ... = s.inf f : by rw [insert_eq_of_mem hb] lemma le_inf {a : α} : (∀b ∈ s, a ≤ f b) → a ≤ s.inf f := by letI := classical.dec_eq β; from finset.induction_on s (λ _, le_top) (λ n s hns ih H, by simp only [mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] at H; simp only [inf_insert]; exact le_inf H.1 (ih H.2)) lemma le_inf_iff {a : α} : a ≤ s.inf f ↔ (∀b ∈ s, a ≤ f b) := iff.intro (assume h b hb, le_trans h (inf_le hb)) le_inf 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 α] 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 := max_insert @[simp] theorem max_singleton' {a : α} : finset.max (singleton a) = some a := max_singleton 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_ne_empty {s : finset α} (h : s ≠ ∅) : ∃ a, a ∈ s.max := let ⟨a, ha⟩ := exists_mem_of_ne_empty h in max_of_mem ha theorem max_eq_none {s : finset α} : s.max = none ↔ s = ∅ := ⟨λ h, by_contradiction $ λ hs, let ⟨a, ha⟩ := max_of_ne_empty hs 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 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 := 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_ne_empty {s : finset α} (h : s ≠ ∅) : ∃ a, a ∈ s.min := let ⟨a, ha⟩ := exists_mem_of_ne_empty h in min_of_mem ha theorem min_eq_none {s : finset α} : s.min = none ↔ s = ∅ := ⟨λ h, by_contradiction $ λ hs, let ⟨a, ha⟩ := min_of_ne_empty hs 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 le_min_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 end max_min 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 _ end sort 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 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_bind_left {ι : Type*} [decidable_eq ι] (s : finset ι) (f : ι → finset α) (t : finset α) : disjoint (s.bind f) t ↔ (∀i∈s, disjoint (f i) t) := begin 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*} [decidable_eq ι] (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] end disjoint theorem sort_sorted_lt [decidable_linear_order α] (s : finset α) : list.sorted (<) (sort (≤) s) := (sort_sorted _ _).imp₂ (@lt_of_le_of_ne _ _) (sort_nodup _ _) instance [has_repr α] : has_repr (finset α) := ⟨λ s, repr s.1⟩ 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 _ _ _ section choose variables (p : α → Prop) [decidable_pred p] (l : finset α) def choose_x (hp : (∃! a, a ∈ l ∧ p a)) : { a // a ∈ l ∧ p a } := multiset.choose_x p l.val hp 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 {α} [decidable_eq α] : 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 section decidable_linear_order variables {α} [decidable_linear_order α] def min' (S : finset α) (H : S ≠ ∅) : α := @option.get _ S.min $ let ⟨k, hk⟩ := exists_mem_of_ne_empty H in let ⟨b, hb⟩ := min_of_mem hk in by simp at hb; simp [hb] def max' (S : finset α) (H : S ≠ ∅) : α := @option.get _ S.max $ let ⟨k, hk⟩ := exists_mem_of_ne_empty H in let ⟨b, hb⟩ := max_of_mem hk in by simp at hb; simp [hb] variables (S : finset α) (H : S ≠ ∅) theorem min'_mem : S.min' H ∈ S := mem_of_min $ by simp [min'] theorem min'_le (x) (H2 : x ∈ S) : S.min' H ≤ x := le_min_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 end decidable_linear_order /- Ico (a closed openinterval) -/ 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 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 @[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 eq_cons {n m : ℕ} (h : n < m) : Ico n m = insert n (Ico (n + 1) m) := by rw [← to_finset, multiset.Ico.eq_cons h, multiset.to_finset_cons, to_finset] @[simp] theorem pred_singleton {m : ℕ} (h : m > 0) : 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_ge_of_le_bot {n m l : ℕ} (hln : l ≤ n) : (Ico n m).filter (λ x, x ≥ l) = Ico n m := eq_of_veq $ multiset.Ico.filter_ge_of_le_bot hln lemma filter_ge_of_top_le {n m l : ℕ} (hml : m ≤ l) : (Ico n m).filter (λ x, x ≥ l) = ∅ := eq_of_veq $ multiset.Ico.filter_ge_of_top_le hml lemma filter_ge_of_ge {n m l : ℕ} (hnl : n ≤ l) : (Ico n m).filter (λ x, x ≥ l) = Ico l m := eq_of_veq $ multiset.Ico.filter_ge_of_ge hnl @[simp] lemma filter_ge (n m l : ℕ) : (Ico n m).filter (λ x, x ≥ l) = Ico (max n l) m := eq_of_veq $ multiset.Ico.filter_ge 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] end Ico 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 namespace lattice variables {ι : Sort*} [complete_lattice α] [decidable_eq ι] lemma supr_eq_supr_finset (s : ι → α) : (⨆i, s i) = (⨆t:finset (plift ι), ⨆i∈t, s (plift.down i)) := 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 _ _) lemma infi_eq_infi_finset (s : ι → α) : (⨅i, s i) = (⨅t:finset (plift ι), ⨅i∈t, s (plift.down i)) := 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 lattice namespace set variables {ι : Sort*} [decidable_eq ι] lemma Union_eq_Union_finset (s : ι → set α) : (⋃i, s i) = (⋃t:finset (plift ι), ⋃i∈t, s (plift.down i)) := lattice.supr_eq_supr_finset s lemma Inter_eq_Inter_finset (s : ι → set α) : (⋂i, s i) = (⋂t:finset (plift ι), ⋂i∈t, s (plift.down i)) := lattice.infi_eq_infi_finset s end set
93e146babc7e3afdc15a065f2dcd923353739535
9a208b2f36ebdb9b100b123c6aa00063414860a7
/src/basic.lean
45ad7fe4b6ce28bff3cb8d62f10e8d0d6a919f55
[ "Apache-2.0" ]
permissive
SnobbyDragon/sudoku
67b0dc46ff3484e29b0d99e1b82d66896ce13ff0
3aa976818a7c950c85d6e7f1a70d329125fcea9b
refs/heads/master
1,675,441,677,467
1,609,123,194,000
1,609,123,194,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
10,250
lean
/- Copyright (c) 2020 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel -/ import data.set.function import tactic open set def row (i : fin 9) : set (fin 9 × fin 9) := { p | p.1 = i } def col (i : fin 9) : set (fin 9 × fin 9) := { p | p.2 = i } def box (i j : fin 3) : set (fin 9 × fin 9) := { p | p.1.1 / 3 = i.1 ∧ p.2.1 / 3 = j.1 } lemma mem_row (i j k : fin 9) : (j, k) ∈ row i ↔ j = i := iff.rfl lemma mem_col (i j k : fin 9) : (j, k) ∈ col i ↔ k = i := iff.rfl lemma mem_box (i j : fin 9) (k l : fin 3) : (i, j) ∈ box k l ↔ i.1 / 3 = k.1 ∧ j.1 / 3 = l.1 := iff.rfl structure sudoku := (f : fin 9 × fin 9 → fin 9) (h_row : ∀ i : fin 9, set.bij_on f (row i) set.univ) (h_col : ∀ i : fin 9, set.bij_on f (col i) set.univ) (h_box : ∀ i j : fin 3, set.bij_on f (box i j) set.univ) namespace sudoku lemma cell_cases (s : sudoku) (i j : fin 9) : s.f (i, j) = 9 ∨ s.f (i, j) = 1 ∨ s.f (i, j) = 2 ∨ s.f (i, j) = 3 ∨ s.f (i, j) = 4 ∨ s.f (i, j) = 5 ∨ s.f (i, j) = 6 ∨ s.f (i, j) = 7 ∨ s.f (i, j) = 8 := begin cases s.f (i, j) with v hv, interval_cases v; tauto end lemma row_cases (s : sudoku) (i j : fin 9) : s.f (i, 0) = j ∨ s.f (i, 1) = j ∨ s.f (i, 2) = j ∨ s.f (i, 3) = j ∨ s.f (i, 4) = j ∨ s.f (i, 5) = j ∨ s.f (i, 6) = j ∨ s.f (i, 7) = j ∨ s.f (i, 8) = j := begin obtain ⟨⟨x, ⟨y, hy⟩⟩, ⟨h, h'⟩⟩ : j ∈ s.f '' row i := (s.h_row i).surj_on trivial, rw mem_row at h, subst h, interval_cases y; tauto end lemma col_cases (s : sudoku) (i j : fin 9) : s.f (0, i) = j ∨ s.f (1, i) = j ∨ s.f (2, i) = j ∨ s.f (3, i) = j ∨ s.f (4, i) = j ∨ s.f (5, i) = j ∨ s.f (6, i) = j ∨ s.f (7, i) = j ∨ s.f (8, i) = j := begin obtain ⟨⟨⟨x, hx⟩, y⟩, ⟨h, h'⟩⟩ : j ∈ s.f '' col i := (s.h_col i).surj_on trivial, rw mem_col at h, subst h, interval_cases x; tauto end lemma box_cases (s : sudoku) (i j : fin 3) (k : fin 9) : s.f ((3 * i.1 : ℕ), (3 * j.1 : ℕ)) = k ∨ s.f ((3 * i.1 : ℕ), (3 * j.1 + 1 : ℕ)) = k ∨ s.f ((3 * i.1 : ℕ), (3 * j.1 + 2 : ℕ)) = k ∨ s.f ((3 * i.1 + 1 : ℕ), (3 * j.1 : ℕ)) = k ∨ s.f ((3 * i.1 + 1 : ℕ), (3 * j.1 + 1 : ℕ)) = k ∨ s.f ((3 * i.1 + 1 : ℕ), (3 * j.1 + 2 : ℕ)) = k ∨ s.f ((3 * i.1 + 2 : ℕ), (3 * j.1 : ℕ)) = k ∨ s.f ((3 * i.1 + 2 : ℕ), (3 * j.1 + 1 : ℕ)) = k ∨ s.f ((3 * i.1 + 2 : ℕ), (3 * j.1 + 2 : ℕ)) = k := begin obtain ⟨⟨x, y⟩, ⟨h, h'⟩⟩ : k ∈ s.f '' box i j := (s.h_box i j).surj_on trivial, rw mem_box at h, cases h with h₀ h₁, have hx : x.1 = 3 * i.val + (x.1 % 3), { rw [add_comm, ←h₀, nat.mod_add_div] }, have hy : y.1 = 3 * j.val + (y.1 % 3), { rw [add_comm, ←h₁, nat.mod_add_div] }, have := nat.mod_lt x.val (show 3 > 0, from dec_trivial), have := nat.mod_lt y.val (show 3 > 0, from dec_trivial), interval_cases (x.val % 3); rw h at hx; try { rw add_zero at hx }; rw ←hx; interval_cases (y.val % 3); rw h_1 at hy; try { rw add_zero at hy }; simp only [←hy, fin.coe_val_eq_self]; tauto end lemma cell_conflict (s : sudoku) {i j k l : fin 9} (h₀ : s.f (i, j) = k) (h₁ : s.f (i, j) = l) (h₂ : k ≠ l) : false := begin apply h₂, rw [←h₀, ←h₁] end lemma row_conflict (s : sudoku) {i j k l : fin 9} (h₀ : s.f (i, j) = l) (h₁ : s.f (i, k) = l) (h₂ : j ≠ k) : false := begin apply h₂, suffices : (i, j) = (i, k), { cases this, refl }, refine (s.h_row i).inj_on _ _ (h₀.trans h₁.symm); rw mem_row end lemma col_conflict (s : sudoku) {i j k l : fin 9} (h₀ : s.f (i, k) = l) (h₁ : s.f (j, k) = l) (h₂ : i ≠ j) : false := begin apply h₂, suffices : (i, k) = (j, k), { cases this, refl }, refine (s.h_col k).inj_on _ _ (h₀.trans h₁.symm); rw mem_col end lemma bloop {i : ℕ} (hi : i < 9) : i / 3 < 3 := by interval_cases i; exact dec_trivial lemma box_conflict (s : sudoku) {i j k l m : fin 9} (h₀ : s.f (i, j) = m) (h₁ : s.f (k, l) = m) (h₂ : i.1 / 3 = k.1 / 3) (h₃ : j.1 / 3 = l.1 / 3) (h₄ : i ≠ k ∨ j ≠ l) : false := begin contrapose h₄, push_neg, clear h₄, suffices : (i, j) = (k, l), { cases this, exact ⟨rfl, rfl⟩ }, refine (s.h_box (i.1 / 3 : ℕ) (j.1 / 3 : ℕ)).inj_on _ _ (h₀.trans h₁.symm), { rw mem_box, split, { rw fin.coe_val_of_lt, exact bloop i.2 }, { rw fin.coe_val_of_lt, exact bloop j.2 } }, { rw mem_box, split, { rw fin.coe_val_of_lt, { exact h₂.symm }, { exact bloop i.2 } }, { rw fin.coe_val_of_lt, { exact h₃.symm }, { exact bloop j.2 } } } end /-- Outer pencil marks capture the fact that a certain number appears in one of two places. -/ def snyder (s : sudoku) (i j k l m : fin 9) : Prop := s.f (i, j) = m ∨ s.f (k, l) = m def snyder₃ (s : sudoku) (i j k l m n o : fin 9) : Prop := s.f (i, j) = o ∨ s.f (k, l) = o ∨ s.f (m, n) = o /-- Inner pencil marks capture the fact that a certain cell contains one of two numbers. -/ def double (s : sudoku) (i j k l : fin 9) : Prop := s.f (i, j) = k ∨ s.f (i, j) = l /-- Inner pencil marks capture the fact that a certain cell contains one of three numbers. -/ def triple (s : sudoku) (i j k l m : fin 9) : Prop := s.f (i, j) = k ∨ s.f (i, j) = l ∨ s.f (i, j) = m lemma triple_perm₁ {s : sudoku} {i j k l m : fin 9} : s.triple i j k l m → s.triple i j l k m := by { unfold triple, tauto } lemma triple_perm₂ {s : sudoku} {i j k l m : fin 9} : s.triple i j k l m → s.triple i j m l k := by { unfold triple, tauto } /-- The first (trivial) piece of sudoku theory: If there are two outer pencil marks relating two cells, then we get an inner pencil mark for those two numbers in both cells. -/ lemma double_left_of_snyder {s : sudoku} {i j k l m n : fin 9} (h₀ : snyder s i j k l m) (h₁ : snyder s i j k l n) (h₂ : m ≠ n) : double s i j m n := by { unfold double, cases h₀; cases h₁; try { tauto }, exact absurd (h₀.symm.trans h₁) h₂ } /-- The first (trivial) piece of sudoku theory: If there are two outer pencil marks relating two cells, then we get an inner pencil mark for those two numbers in both cells. -/ lemma double_right_of_snyder {s : sudoku} {i j k l m n : fin 9} (h₀ : snyder s i j k l m) (h₁ : snyder s i j k l n) (h₂ : m ≠ n) : double s k l m n := by { unfold double, cases h₀; cases h₁; try { tauto }, exact absurd (h₀.symm.trans h₁) h₂ } lemma triple_of_double₁ {s : sudoku} {i j k l m : fin 9} : s.double i j k l → s.triple i j m k l := by { unfold triple, tidy, tauto } lemma triple_of_double₂ {s : sudoku} {i j k l m : fin 9} : s.double i j k l → s.triple i j k m l := by { unfold triple, tidy, tauto } lemma triple_of_double₃ {s : sudoku} {i j k l m : fin 9} : s.double i j k l → s.triple i j k l m := by { unfold triple, tidy, tauto } /-- Two cells are in contention if they "see each other", i.e., cannot contain the same number. -/ def contention (s : sudoku) (i j k l : fin 9) : Prop := ∀ (m : fin 9), s.f (i, j) = m → s.f (k, l) = m → false lemma row_contention {s : sudoku} {i j k : fin 9} (h : j ≠ k) : s.contention i j i k := λ m h₀ h₁, s.row_conflict h₀ h₁ h lemma col_contention {s : sudoku} {i j k : fin 9} (h : i ≠ j) : s.contention i k j k := λ m h₀ h₁, s.col_conflict h₀ h₁ h lemma box_contention {s : sudoku} {i j k l : fin 9} (h : i.1 / 3 = k.1 / 3) (h' : j.1 / 3 = l.1 / 3) (h'' : i ≠ k ∨ j ≠ l) : s.contention i j k l := λ m h₀ h₁, s.box_conflict h₀ h₁ h h' h'' lemma snyder₃_of_triple₁ {s : sudoku} {i j k l m n o p q : fin 9} (h₀ : s.triple i j o p q) (h₁ : s.triple k l o p q) (h₂ : s.triple m n o p q) (h : s.contention i j k l) (h' : s.contention i j m n) (h'' : s.contention k l m n) : s.snyder₃ i j k l m n o := begin unfold snyder₃, rcases h₀ with _|_|_, { left, exact h₀ }, { rcases h₁ with _|_|_, { right, left, exact h₁ }, { exfalso, exact h _ h₀ h₁ }, rcases h₂ with _|_|_, { right, right, exact h₂ }, { exfalso, exact h' _ h₀ h₂ }, { exfalso, exact h'' _ h₁ h₂ } }, { rcases h₁ with _|_|_, { right, left, exact h₁ }, swap, { exfalso, exact h _ h₀ h₁ }, rcases h₂ with _|_|_, { right, right, exact h₂ }, { exfalso, exact h'' _ h₁ h₂ }, { exfalso, exact h' _ h₀ h₂ } } end lemma snyder₃_of_triple₂ {s : sudoku} {i j k l m n o p q : fin 9} (h₀ : s.triple i j o p q) (h₁ : s.triple k l o p q) (h₂ : s.triple m n o p q) (h : s.contention i j k l) (h' : s.contention i j m n) (h'' : s.contention k l m n) : s.snyder₃ i j k l m n p := snyder₃_of_triple₁ (triple_perm₁ h₀) (triple_perm₁ h₁) (triple_perm₁ h₂) h h' h'' lemma snyder₃_of_triple₃ {s : sudoku} {i j k l m n o p q : fin 9} (h₀ : s.triple i j o p q) (h₁ : s.triple k l o p q) (h₂ : s.triple m n o p q) (h : s.contention i j k l) (h' : s.contention i j m n) (h'' : s.contention k l m n) : s.snyder₃ i j k l m n q := snyder₃_of_triple₁ (triple_perm₂ h₀) (triple_perm₂ h₁) (triple_perm₂ h₂) h h' h'' lemma snyder₃_of_triple_row₁ {s : sudoku} {i j k l m n o : fin 9} (hj : s.triple i j m n o) (hk : s.triple i k m n o) (hl : s.triple i l m n o) (hjk : j ≠ k) (hkl : k ≠ l) (hjl : j ≠ l) : s.snyder₃ i j i k i l m := snyder₃_of_triple₁ hj hk hl (row_contention hjk) (row_contention hjl) (row_contention hkl) lemma snyder₃_of_triple_row₂ {s : sudoku} {i j k l m n o : fin 9} (hj : s.triple i j m n o) (hk : s.triple i k m n o) (hl : s.triple i l m n o) (hjk : j ≠ k) (hkl : k ≠ l) (hjl : j ≠ l) : s.snyder₃ i j i k i l n := snyder₃_of_triple₂ hj hk hl (row_contention hjk) (row_contention hjl) (row_contention hkl) lemma snyder₃_of_triple_row₃ {s : sudoku} {i j k l m n o : fin 9} (hj : s.triple i j m n o) (hk : s.triple i k m n o) (hl : s.triple i l m n o) (hjk : j ≠ k) (hkl : k ≠ l) (hjl : j ≠ l) : s.snyder₃ i j i k i l o := snyder₃_of_triple₃ hj hk hl (row_contention hjk) (row_contention hjl) (row_contention hkl) end sudoku
266bb1a8f8c6122471c387daeb5bbd2fbbb390d0
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/library/algebra/complete_lattice.lean
065970c8cf97908b7ddb099724dd5295e5b221c6
[ "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
14,985
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 Complete lattices TODO: define dual complete lattice and simplify proof of dual theorems. -/ import algebra.lattice data.set.basic algebra.monotone open set variable {A : Type} structure complete_lattice [class] (A : Type) extends lattice A := (Inf : set A → A) (Sup : set A → A) (Inf_le : ∀ {a : A} {s : set A}, a ∈ s → le (Inf s) a) (le_Inf : ∀ {b : A} {s : set A}, (∀ (a : A), a ∈ s → le b a) → le b (Inf s)) (le_Sup : ∀ {a : A} {s : set A}, a ∈ s → le a (Sup s)) (Sup_le : ∀ {b : A} {s : set A} (h : ∀ (a : A), a ∈ s → le a b), le (Sup s) b) section variable [complete_lattice A] definition Inf (S : set A) : A := complete_lattice.Inf S prefix `⨅ `:70 := Inf definition Sup (S : set A) : A := complete_lattice.Sup S prefix `⨆ `:65 := Sup theorem Inf_le {a : A} {s : set A} (H : a ∈ s) : (Inf s) ≤ a := complete_lattice.Inf_le H theorem le_Inf {b : A} {s : set A} (H : ∀ (a : A), a ∈ s → b ≤ a) : b ≤ Inf s := complete_lattice.le_Inf H theorem le_Sup {a : A} {s : set A} (H : a ∈ s) : a ≤ Sup s := complete_lattice.le_Sup H theorem Sup_le {b : A} {s : set A} (H : ∀ (a : A), a ∈ s → a ≤ b) : Sup s ≤ b := complete_lattice.Sup_le H end -- Minimal complete_lattice definition based just on Inf. -- We later show that complete_lattice_Inf is a complete_lattice. structure complete_lattice_Inf [class] (A : Type) extends weak_order A := (Inf : set A → A) (Inf_le : ∀ {a : A} {s : set A}, a ∈ s → le (Inf s) a) (le_Inf : ∀ {b : A} {s : set A}, (∀ (a : A), a ∈ s → le b a) → le b (Inf s)) -- Minimal complete_lattice definition based just on Sup. -- We later show that complete_lattice_Sup is a complete_lattice. structure complete_lattice_Sup [class] (A : Type) extends weak_order A := (Sup : set A → A) (le_Sup : ∀ {a : A} {s : set A}, a ∈ s → le a (Sup s)) (Sup_le : ∀ {b : A} {s : set A} (h : ∀ (a : A), a ∈ s → le a b), le (Sup s) b) namespace complete_lattice_Inf variable [C : complete_lattice_Inf A] include C definition Sup (s : set A) : A := Inf {b | ∀ a, a ∈ s → a ≤ b} local prefix `⨅`:70 := Inf local prefix `⨆`:65 := Sup lemma le_Sup {a : A} {s : set A} : a ∈ s → a ≤ ⨆ s := suppose a ∈ s, le_Inf (show ∀ (b : A), (∀ (a : A), a ∈ s → a ≤ b) → a ≤ b, from take b, assume h, h a `a ∈ s`) lemma Sup_le {b : A} {s : set A} (h : ∀ (a : A), a ∈ s → a ≤ b) : ⨆ s ≤ b := Inf_le h definition inf (a b : A) := ⨅ '{a, b} definition sup (a b : A) := ⨆ '{a, b} local infix `⊓` := inf local infix `⊔` := sup lemma inf_le_left (a b : A) : a ⊓ b ≤ a := Inf_le !mem_insert lemma inf_le_right (a b : A) : a ⊓ b ≤ b := Inf_le (!mem_insert_of_mem !mem_insert) lemma le_inf {a b c : A} : c ≤ a → c ≤ b → c ≤ a ⊓ b := assume h₁ h₂, le_Inf (take x, suppose x ∈ '{a, b}, or.elim (eq_or_mem_of_mem_insert this) (suppose x = a, begin subst x, exact h₁ end) (suppose x ∈ '{b}, have x = b, from !eq_of_mem_singleton this, begin subst x, exact h₂ end)) lemma le_sup_left (a b : A) : a ≤ a ⊔ b := le_Sup !mem_insert lemma le_sup_right (a b : A) : b ≤ a ⊔ b := le_Sup (!mem_insert_of_mem !mem_insert) lemma sup_le {a b c : A} : a ≤ c → b ≤ c → a ⊔ b ≤ c := assume h₁ h₂, Sup_le (take x, suppose x ∈ '{a, b}, or.elim (eq_or_mem_of_mem_insert this) (suppose x = a, by subst x; assumption) (suppose x ∈ '{b}, have x = b, from !eq_of_mem_singleton this, by subst x; assumption)) end complete_lattice_Inf -- Every complete_lattice_Inf is a complete_lattice_Sup definition complete_lattice_Inf_to_complete_lattice_Sup [C : complete_lattice_Inf A] : complete_lattice_Sup A := ⦃ complete_lattice_Sup, C ⦄ -- Every complete_lattice_Inf is a complete_lattice attribute [trans_instance] definition complete_lattice_Inf_to_complete_lattice [C : complete_lattice_Inf A] : complete_lattice A := ⦃ complete_lattice, C ⦄ namespace complete_lattice_Sup variable [C : complete_lattice_Sup A] include C definition Inf (s : set A) : A := Sup {b | ∀ a, a ∈ s → b ≤ a} lemma Inf_le {a : A} {s : set A} : a ∈ s → Inf s ≤ a := suppose a ∈ s, Sup_le (show ∀ (b : A), (∀ (a : A), a ∈ s → b ≤ a) → b ≤ a, from take b, assume h, h a `a ∈ s`) lemma le_Inf {b : A} {s : set A} (h : ∀ (a : A), a ∈ s → b ≤ a) : b ≤ Inf s := le_Sup h local prefix `⨅`:70 := Inf local prefix `⨆`:65 := Sup definition inf (a b : A) := ⨅ '{a, b} definition sup (a b : A) := ⨆ '{a, b} local infix `⊓` := inf local infix `⊔` := sup lemma inf_le_left (a b : A) : a ⊓ b ≤ a := Inf_le !mem_insert lemma inf_le_right (a b : A) : a ⊓ b ≤ b := Inf_le (!mem_insert_of_mem !mem_insert) lemma le_inf {a b c : A} : c ≤ a → c ≤ b → c ≤ a ⊓ b := assume h₁ h₂, le_Inf (take x, suppose x ∈ '{a, b}, or.elim (eq_or_mem_of_mem_insert this) (suppose x = a, begin subst x, exact h₁ end) (suppose x ∈ '{b}, have x = b, from !eq_of_mem_singleton this, begin subst x, exact h₂ end)) lemma le_sup_left (a b : A) : a ≤ a ⊔ b := le_Sup !mem_insert lemma le_sup_right (a b : A) : b ≤ a ⊔ b := le_Sup (!mem_insert_of_mem !mem_insert) lemma sup_le {a b c : A} : a ≤ c → b ≤ c → a ⊔ b ≤ c := assume h₁ h₂, Sup_le (take x, suppose x ∈ '{a, b}, or.elim (eq_or_mem_of_mem_insert this) (assume H : x = a, by subst x; exact h₁) (suppose x ∈ '{b}, have x = b, from !eq_of_mem_singleton this, by subst x; exact h₂)) end complete_lattice_Sup -- Every complete_lattice_Sup is a complete_lattice_Inf definition complete_lattice_Sup_to_complete_lattice_Inf [C : complete_lattice_Sup A] : complete_lattice_Inf A := ⦃ complete_lattice_Inf, C ⦄ -- Every complete_lattice_Sup is a complete_lattice section attribute [trans_instance] definition complete_lattice_Sup_to_complete_lattice [C : complete_lattice_Sup A] : complete_lattice A := ⦃ complete_lattice, C ⦄ end section complete_lattice variable [C : complete_lattice A] include C variable {f : A → A} premise (mono : nondecreasing f) theorem knaster_tarski : ∃ a, f a = a ∧ ∀ b, f b = b → a ≤ b := let a := ⨅ {u | f u ≤ u} in have h₁ : f a = a, from have ge : f a ≤ a, from have ∀ b, b ∈ {u | f u ≤ u} → f a ≤ b, from take b, suppose f b ≤ b, have a ≤ b, from Inf_le this, have f a ≤ f b, from mono this, le.trans `f a ≤ f b` `f b ≤ b`, le_Inf this, have le : a ≤ f a, from have f (f a) ≤ f a, from !mono ge, have f a ∈ {u | f u ≤ u}, from this, Inf_le this, le.antisymm ge le, have h₂ : ∀ b, f b = b → a ≤ b, from take b, suppose f b = b, have b ∈ {u | f u ≤ u}, from show f b ≤ b, by rewrite this, Inf_le this, exists.intro a (and.intro h₁ h₂) theorem knaster_tarski_dual : ∃ a, f a = a ∧ ∀ b, f b = b → b ≤ a := let a := ⨆ {u | u ≤ f u} in have h₁ : f a = a, from have le : a ≤ f a, from have ∀ b, b ∈ {u | u ≤ f u} → b ≤ f a, from take b, suppose b ≤ f b, have b ≤ a, from le_Sup this, have f b ≤ f a, from mono this, le.trans `b ≤ f b` `f b ≤ f a`, Sup_le this, have ge : f a ≤ a, from have f a ≤ f (f a), from !mono le, have f a ∈ {u | u ≤ f u}, from this, le_Sup this, le.antisymm ge le, have h₂ : ∀ b, f b = b → b ≤ a, from take b, suppose f b = b, have b ≤ f b, by rewrite this, le_Sup this, exists.intro a (and.intro h₁ h₂) /- top and bot -/ definition bot : A := ⨅ univ definition top : A := ⨆ univ notation `⊥` := bot notation `⊤` := top lemma bot_le (a : A) : ⊥ ≤ a := Inf_le !mem_univ lemma eq_bot {a : A} : (∀ b, a ≤ b) → a = ⊥ := assume h, have a ≤ ⊥, from le_Inf (take b bin, h b), le.antisymm this !bot_le lemma le_top (a : A) : a ≤ ⊤ := le_Sup !mem_univ lemma eq_top {a : A} : (∀ b, b ≤ a) → a = ⊤ := assume h, have ⊤ ≤ a, from Sup_le (take b bin, h b), le.antisymm !le_top this /- general facts about complete lattices -/ lemma Inf_singleton {a : A} : ⨅'{a} = a := have ⨅'{a} ≤ a, from Inf_le !mem_insert, have a ≤ ⨅'{a}, from le_Inf (take b, suppose b ∈ '{a}, have b = a, from eq_of_mem_singleton this, by rewrite this), le.antisymm `⨅'{a} ≤ a` `a ≤ ⨅'{a}` lemma Sup_singleton {a : A} : ⨆'{a} = a := have ⨆'{a} ≤ a, from Sup_le (take b, suppose b ∈ '{a}, have b = a, from eq_of_mem_singleton this, by rewrite this), have a ≤ ⨆'{a}, from le_Sup !mem_insert, le.antisymm `⨆'{a} ≤ a` `a ≤ ⨆'{a}` lemma Inf_antimono {s₁ s₂ : set A} : s₁ ⊆ s₂ → ⨅ s₂ ≤ ⨅ s₁ := suppose s₁ ⊆ s₂, le_Inf (take a : A, suppose a ∈ s₁, Inf_le (mem_of_subset_of_mem `s₁ ⊆ s₂` `a ∈ s₁`)) lemma Sup_mono {s₁ s₂ : set A} : s₁ ⊆ s₂ → ⨆ s₁ ≤ ⨆ s₂ := suppose s₁ ⊆ s₂, Sup_le (take a : A, suppose a ∈ s₁, le_Sup (mem_of_subset_of_mem `s₁ ⊆ s₂` `a ∈ s₁`)) lemma Inf_union (s₁ s₂ : set A) : ⨅ (s₁ ∪ s₂) = (⨅s₁) ⊓ (⨅s₂) := have le₁ : ⨅ (s₁ ∪ s₂) ≤ (⨅s₁) ⊓ (⨅s₂), from !le_inf (le_Inf (take a : A, suppose a ∈ s₁, Inf_le (mem_unionl `a ∈ s₁`))) (le_Inf (take a : A, suppose a ∈ s₂, Inf_le (mem_unionr `a ∈ s₂`))), have le₂ : (⨅s₁) ⊓ (⨅s₂) ≤ ⨅ (s₁ ∪ s₂), from le_Inf (take a : A, suppose a ∈ s₁ ∪ s₂, or.elim this (suppose a ∈ s₁, have (⨅s₁) ⊓ (⨅s₂) ≤ ⨅s₁, from !inf_le_left, have ⨅s₁ ≤ a, from Inf_le `a ∈ s₁`, le.trans `(⨅s₁) ⊓ (⨅s₂) ≤ ⨅s₁` `⨅s₁ ≤ a`) (suppose a ∈ s₂, have (⨅s₁) ⊓ (⨅s₂) ≤ ⨅s₂, from !inf_le_right, have ⨅s₂ ≤ a, from Inf_le `a ∈ s₂`, le.trans `(⨅s₁) ⊓ (⨅s₂) ≤ ⨅s₂` `⨅s₂ ≤ a`)), le.antisymm le₁ le₂ lemma Sup_union (s₁ s₂ : set A) : ⨆ (s₁ ∪ s₂) = (⨆s₁) ⊔ (⨆s₂) := have le₁ : ⨆ (s₁ ∪ s₂) ≤ (⨆s₁) ⊔ (⨆s₂), from Sup_le (take a : A, suppose a ∈ s₁ ∪ s₂, or.elim this (suppose a ∈ s₁, have a ≤ ⨆s₁, from le_Sup `a ∈ s₁`, have ⨆s₁ ≤ (⨆s₁) ⊔ (⨆s₂), from !le_sup_left, le.trans `a ≤ ⨆s₁` `⨆s₁ ≤ (⨆s₁) ⊔ (⨆s₂)`) (suppose a ∈ s₂, have a ≤ ⨆s₂, from le_Sup `a ∈ s₂`, have ⨆s₂ ≤ (⨆s₁) ⊔ (⨆s₂), from !le_sup_right, le.trans `a ≤ ⨆s₂` `⨆s₂ ≤ (⨆s₁) ⊔ (⨆s₂)`)), have le₂ : (⨆s₁) ⊔ (⨆s₂) ≤ ⨆ (s₁ ∪ s₂), from !sup_le (Sup_le (take a : A, suppose a ∈ s₁, le_Sup (mem_unionl `a ∈ s₁`))) (Sup_le (take a : A, suppose a ∈ s₂, le_Sup (mem_unionr `a ∈ s₂`))), le.antisymm le₁ le₂ lemma Inf_empty_eq_Sup_univ : ⨅ (∅ : set A) = ⨆ univ := have le₁ : ⨅ (∅ : set A) ≤ ⨆ univ, from le_Sup !mem_univ, have le₂ : ⨆ univ ≤ ⨅ ∅, from le_Inf (take a : A, suppose a ∈ ∅, absurd this !not_mem_empty), le.antisymm le₁ le₂ lemma Sup_empty_eq_Inf_univ : ⨆ (∅ : set A) = ⨅ univ := have le₁ : ⨆ (∅ : set A) ≤ ⨅ univ, from Sup_le (take a, suppose a ∈ ∅, absurd this !not_mem_empty), have le₂ : ⨅ univ ≤ ⨆ (∅ : set A), from Inf_le !mem_univ, le.antisymm le₁ le₂ lemma Sup_pair (a b : A) : Sup '{a, b} = sup a b := by rewrite [insert_eq, Sup_union, *Sup_singleton] lemma Inf_pair (a b : A) : Inf '{a, b} = inf a b := by rewrite [insert_eq, Inf_union, *Inf_singleton] end complete_lattice /- complete lattice instances -/ section open eq.ops complete_lattice attribute [instance] definition complete_lattice_fun (A B : Type) [complete_lattice B] : complete_lattice (A → B) := ⦃ complete_lattice, lattice_fun A B, Inf := λS x, Inf ((λf, f x) ' S), le_Inf := take f S H x, le_Inf (take y Hy, obtain g `g ∈ S` `g x = y`, from Hy, `g x = y` ▸ H g `g ∈ S` x), Inf_le := take f S `f ∈ S` x, Inf_le (exists.intro f (and.intro `f ∈ S` rfl)), Sup := λS x, Sup ((λf, f x) ' S), le_Sup := take f S `f ∈ S` x, le_Sup (exists.intro f (and.intro `f ∈ S` rfl)), Sup_le := take f S H x, Sup_le (take y Hy, obtain g `g ∈ S` `g x = y`, from Hy, `g x = y` ▸ H g `g ∈ S` x) ⦄ section local attribute classical.prop_decidable [instance] -- Prop and set are only in the classical setting a complete lattice attribute [instance] definition complete_lattice_Prop : complete_lattice Prop := ⦃ complete_lattice, lattice_Prop, Inf := λS, false ∉ S, le_Inf := take x S H Hx Hf, H _ Hf Hx, Inf_le := take x S Hx Hf, (classical.cases_on x (take x, true.intro) Hf) Hx, Sup := λS, true ∈ S, le_Sup := take x S Hx H, iff_subst (iff.intro (take H, true.intro) (take H', H)) Hx, Sup_le := take x S H Ht, H _ Ht true.intro ⦄ lemma sInter_eq_Inf_fun {A : Type} (S : set (set A)) : ⋂₀ S = @Inf (A → Prop) _ S := funext (take x, calc (⋂₀ S) x = ∀₀ P ∈ S, P x : rfl ... = ¬ (∃₀ P ∈ S, P x = false) : begin rewrite not_bounded_exists, apply bounded_forall_congr, intros, rewrite eq_false, rewrite not_not_iff end ... = @Inf (A → Prop) _ S x : rfl) lemma sUnion_eq_Sup_fun {A : Type} (S : set (set A)) : ⋃₀ S = @Sup (A → Prop) _ S := funext (take x, calc (⋃₀ S) x = ∃₀ P ∈ S, P x : rfl ... = (∃₀ P ∈ S, P x = true) : begin apply bounded_exists_congr, intros, rewrite eq_true end ... = @Sup (A → Prop) _ S x : rfl) attribute [instance] definition complete_lattice_set (A : Type) : complete_lattice (set A) := ⦃ complete_lattice, le := subset, le_refl := @le_refl (A → Prop) _, le_trans := @le_trans (A → Prop) _, le_antisymm := @le_antisymm (A → Prop) _, inf := inter, sup := union, inf_le_left := @inf_le_left (A → Prop) _, inf_le_right := @inf_le_right (A → Prop) _, le_inf := @le_inf (A → Prop) _, le_sup_left := @le_sup_left (A → Prop) _, le_sup_right := @le_sup_right (A → Prop) _, sup_le := @sup_le (A → Prop) _, Inf := sInter, Sup := sUnion, le_Inf := begin intros X S H, rewrite sInter_eq_Inf_fun, apply (@le_Inf (A → Prop) _), exact H end, Inf_le := begin intros X S H, rewrite sInter_eq_Inf_fun, apply (@Inf_le (A → Prop) _), exact H end, le_Sup := begin intros X S H, rewrite sUnion_eq_Sup_fun, apply (@le_Sup (A → Prop) _), exact H end, Sup_le := begin intros X S H, rewrite sUnion_eq_Sup_fun, apply (@Sup_le (A → Prop) _), exact H end ⦄ end end
ba99ceae375ba381b4162feecc5e0c8973a365b7
da3a76c514d38801bae19e8a9e496dc31f8e5866
/tests/lean/run/my_tac_class.lean
222bb1eaccc2173cd3aa4d7465ca62084ade42d8
[ "Apache-2.0" ]
permissive
cipher1024/lean
270c1ac5781e6aee12f5c8d720d267563a164beb
f5cbdff8932dd30c6dd8eec68f3059393b4f8b3a
refs/heads/master
1,611,223,459,029
1,487,566,573,000
1,487,566,573,000
83,356,543
0
0
null
1,488,229,336,000
1,488,229,336,000
null
UTF-8
Lean
false
false
1,753
lean
meta def mytac := state_t nat tactic meta instance : monad mytac := state_t.monad _ _ meta instance : monad.has_monad_lift tactic mytac := monad.monad_transformer_lift (state_t nat) tactic meta instance (α : Type) : has_coe (tactic α) (mytac α) := ⟨monad.monad_lift⟩ namespace mytac meta def step {α : Type} (t : mytac α) : mytac unit := t >> return () meta def rstep {α : Type} (line : nat) (col : nat) (t : mytac α) : mytac unit := λ v s, result.cases_on (@scope_trace _ line col (t v s)) (λ ⟨a, v⟩ new_s, result.success ((), v) new_s) (λ opt_msg_thunk e new_s, match opt_msg_thunk with | some msg_thunk := let msg := msg_thunk () ++ format.line ++ to_fmt "value: " ++ to_fmt v ++ format.line ++ to_fmt "state:" ++ format.line ++ new_s^.to_format in (tactic.report_error line col msg >> interaction_monad.silent_fail) new_s | none := interaction_monad.silent_fail new_s end) meta def execute (tac : mytac unit) : tactic unit := tac 0 >> return () meta def save_info (line col : nat) : mytac unit := do v ← state_t.read, s ← tactic.read, tactic.save_info_thunk line col (λ _, to_fmt "Custom state: " ++ to_fmt v ++ format.line ++ tactic_state.to_format s) namespace interactive meta def intros : mytac unit := tactic.intros >> return () meta def constructor : mytac unit := tactic.constructor meta def trace (s : string) : mytac unit := tactic.trace s meta def assumption : mytac unit := tactic.assumption meta def inc : mytac unit := do v ← state_t.read, state_t.write (v+1) end interactive end mytac example (p q : Prop) : p → q → p ∧ q := begin [mytac] intros, inc, trace "test", constructor, inc, assumption, assumption end
a34d80fe7070cd34fa25181e2e7dcbeecbbb6339
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/interactive/coe.lean
d65992caf5cb2ffb47184e44a7d494ec041b8398
[ "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
164
lean
import data.int open int protected theorem has_decidable_eq₁ [instance] : decidable_eq ℤ := take (a b : ℤ), _ constant n : nat constant i : int check n + i
e1d0ea95e60bdc34c755bf1e469a53bda923f882
1dd482be3f611941db7801003235dc84147ec60a
/src/algebra/ring.lean
c2e72751b9641aa8bd1a9a2e6adc75ce770f1ebc
[ "Apache-2.0" ]
permissive
sanderdahmen/mathlib
479039302bd66434bb5672c2a4cecf8d69981458
8f0eae75cd2d8b7a083cf935666fcce4565df076
refs/heads/master
1,587,491,322,775
1,549,672,060,000
1,549,672,060,000
169,748,224
0
0
Apache-2.0
1,549,636,694,000
1,549,636,694,000
null
UTF-8
Lean
false
false
10,685
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Floris van Doorn -/ import algebra.group data.set.basic universes u v variable {α : Type u} section variable [semiring α] theorem mul_two (n : α) : n * 2 = n + n := (left_distrib n 1 1).trans (by simp) theorem bit0_eq_two_mul (n : α) : bit0 n = 2 * n := (two_mul _).symm variable (α) lemma zero_ne_one_or_forall_eq_0 : (0 : α) ≠ 1 ∨ (∀a:α, a = 0) := by haveI := classical.dec; refine not_or_of_imp (λ h a, _); simpa using congr_arg ((*) a) h.symm lemma eq_zero_of_zero_eq_one (h : (0 : α) = 1) : (∀a:α, a = 0) := (zero_ne_one_or_forall_eq_0 α).neg_resolve_left h theorem subsingleton_of_zero_eq_one (h : (0 : α) = 1) : subsingleton α := ⟨λa b, by rw [eq_zero_of_zero_eq_one α h a, eq_zero_of_zero_eq_one α h b]⟩ end namespace units variables [ring α] {a b : α} instance : has_neg (units α) := ⟨λu, ⟨-↑u, -↑u⁻¹, by simp, by simp⟩ ⟩ @[simp] protected theorem coe_neg (u : units α) : (↑-u : α) = -u := rfl @[simp] protected theorem neg_inv (u : units α) : (-u)⁻¹ = -u⁻¹ := rfl @[simp] protected theorem neg_neg (u : units α) : - -u = u := units.ext $ neg_neg _ @[simp] protected theorem neg_mul (u₁ u₂ : units α) : -u₁ * u₂ = -(u₁ * u₂) := units.ext $ neg_mul_eq_neg_mul_symm _ _ @[simp] protected theorem mul_neg (u₁ u₂ : units α) : u₁ * -u₂ = -(u₁ * u₂) := units.ext $ (neg_mul_eq_mul_neg _ _).symm @[simp] protected theorem neg_mul_neg (u₁ u₂ : units α) : -u₁ * -u₂ = u₁ * u₂ := by simp protected theorem neg_eq_neg_one_mul (u : units α) : -u = -1 * u := by simp end units instance [semiring α] : semiring (with_zero α) := { left_distrib := λ a b c, begin cases a with a, {refl}, cases b with b; cases c with c; try {refl}, exact congr_arg some (left_distrib _ _ _) end, right_distrib := λ a b c, begin cases c with c, { change (a + b) * 0 = a * 0 + b * 0, simp }, cases a with a; cases b with b; try {refl}, exact congr_arg some (right_distrib _ _ _) end, ..with_zero.add_comm_monoid, ..with_zero.mul_zero_class, ..with_zero.monoid } attribute [refl] dvd_refl attribute [trans] dvd.trans class is_semiring_hom {α : Type u} {β : Type v} [semiring α] [semiring β] (f : α → β) : Prop := (map_zero : f 0 = 0) (map_one : f 1 = 1) (map_add : ∀ {x y}, f (x + y) = f x + f y) (map_mul : ∀ {x y}, f (x * y) = f x * f y) namespace is_semiring_hom variables {β : Type v} [semiring α] [semiring β] variables (f : α → β) [is_semiring_hom f] {x y : α} instance id : is_semiring_hom (@id α) := by refine {..}; intros; refl instance comp {γ} [semiring γ] (g : β → γ) [is_semiring_hom g] : is_semiring_hom (g ∘ f) := { map_zero := by simp [map_zero f]; exact map_zero g, map_one := by simp [map_one f]; exact map_one g, map_add := λ x y, by simp [map_add f]; rw map_add g; refl, map_mul := λ x y, by simp [map_mul f]; rw map_mul g; refl } instance : is_add_monoid_hom f := { ..‹is_semiring_hom f› } instance : is_monoid_hom f := { ..‹is_semiring_hom f› } end is_semiring_hom section variables [ring α] (a b c d e : α) lemma mul_neg_one (a : α) : a * -1 = -a := by simp lemma neg_one_mul (a : α) : -1 * a = -a := by simp theorem mul_add_eq_mul_add_iff_sub_mul_add_eq : a * e + c = b * e + d ↔ (a - b) * e + c = d := calc a * e + c = b * e + d ↔ a * e + c = d + b * e : by simp ... ↔ a * e + c - b * e = d : iff.intro (λ h, begin simp [h] end) (λ h, begin simp [h.symm] end) ... ↔ (a - b) * e + c = d : begin simp [@sub_eq_add_neg α, @right_distrib α] end theorem sub_mul_add_eq_of_mul_add_eq_mul_add : a * e + c = b * e + d → (a - b) * e + c = d := assume h, calc (a - b) * e + c = (a * e + c) - b * e : begin simp [@sub_eq_add_neg α, @right_distrib α] end ... = d : begin rw h, simp [@add_sub_cancel α] end theorem ne_zero_and_ne_zero_of_mul_ne_zero {a b : α} (h : a * b ≠ 0) : a ≠ 0 ∧ b ≠ 0 := begin split, { intro ha, apply h, simp [ha] }, { intro hb, apply h, simp [hb] } end end @[simp] lemma zero_dvd_iff [comm_semiring α] {a : α} : 0 ∣ a ↔ a = 0 := ⟨eq_zero_of_zero_dvd, λ h, by rw h⟩ section comm_ring variable [comm_ring α] @[simp] lemma dvd_neg (a b : α) : (a ∣ -b) ↔ (a ∣ b) := ⟨dvd_of_dvd_neg, dvd_neg_of_dvd⟩ @[simp] lemma neg_dvd (a b : α) : (-a ∣ b) ↔ (a ∣ b) := ⟨dvd_of_neg_dvd, neg_dvd_of_dvd⟩ theorem dvd_add_left {a b c : α} (h : a ∣ c) : a ∣ b + c ↔ a ∣ b := (dvd_add_iff_left h).symm theorem dvd_add_right {a b c : α} (h : a ∣ b) : a ∣ b + c ↔ a ∣ c := (dvd_add_iff_right h).symm end comm_ring class is_ring_hom {α : Type u} {β : Type v} [ring α] [ring β] (f : α → β) : Prop := (map_one : f 1 = 1) (map_mul : ∀ {x y}, f (x * y) = f x * f y) (map_add : ∀ {x y}, f (x + y) = f x + f y) namespace is_ring_hom variables {β : Type v} [ring α] [ring β] def of_semiring (f : α → β) [H : is_semiring_hom f] : is_ring_hom f := {..H} variables (f : α → β) [is_ring_hom f] {x y : α} lemma map_zero : f 0 = 0 := calc f 0 = f (0 + 0) - f 0 : by rw [map_add f]; simp ... = 0 : by simp lemma map_neg : f (-x) = -f x := calc f (-x) = f (-x + x) - f x : by rw [map_add f]; simp ... = -f x : by simp [map_zero f] lemma map_sub : f (x - y) = f x - f y := by simp [map_add f, map_neg f] instance id : is_ring_hom (@id α) := by refine {..}; intros; refl instance comp {γ} [ring γ] (g : β → γ) [is_ring_hom g] : is_ring_hom (g ∘ f) := { map_add := λ x y, by simp [map_add f]; rw map_add g; refl, map_mul := λ x y, by simp [map_mul f]; rw map_mul g; refl, map_one := by simp [map_one f]; exact map_one g } instance : is_semiring_hom f := { map_zero := map_zero f, ..‹is_ring_hom f› } instance : is_add_group_hom f := ⟨λ _ _, map_add f⟩ end is_ring_hom set_option old_structure_cmd true class nonzero_comm_ring (α : Type*) extends zero_ne_one_class α, comm_ring α instance integral_domain.to_nonzero_comm_ring (α : Type*) [id : integral_domain α] : nonzero_comm_ring α := { ..id } lemma units.coe_ne_zero [nonzero_comm_ring α] (u : units α) : (u : α) ≠ 0 := λ h : u.1 = 0, by simpa [h, zero_ne_one] using u.3 /-- A domain is a ring with no zero divisors, i.e. satisfying the condition `a * b = 0 ↔ a = 0 ∨ b = 0`. Alternatively, a domain is an integral domain without assuming commutativity of multiplication. -/ class domain (α : Type u) extends ring α, no_zero_divisors α, zero_ne_one_class α section domain variable [domain α] @[simp] theorem mul_eq_zero {a b : α} : a * b = 0 ↔ a = 0 ∨ b = 0 := ⟨eq_zero_or_eq_zero_of_mul_eq_zero, λo, or.elim o (λh, by rw h; apply zero_mul) (λh, by rw h; apply mul_zero)⟩ @[simp] theorem zero_eq_mul {a b : α} : 0 = a * b ↔ a = 0 ∨ b = 0 := by rw [eq_comm, mul_eq_zero] theorem mul_ne_zero' {a b : α} (h₁ : a ≠ 0) (h₂ : b ≠ 0) : a * b ≠ 0 := λ h, or.elim (eq_zero_or_eq_zero_of_mul_eq_zero h) h₁ h₂ theorem domain.mul_right_inj {a b c : α} (ha : a ≠ 0) : b * a = c * a ↔ b = c := by rw [← sub_eq_zero, ← mul_sub_right_distrib, mul_eq_zero]; simp [ha]; exact sub_eq_zero theorem domain.mul_left_inj {a b c : α} (ha : a ≠ 0) : a * b = a * c ↔ b = c := by rw [← sub_eq_zero, ← mul_sub_left_distrib, mul_eq_zero]; simp [ha]; exact sub_eq_zero theorem eq_zero_of_mul_eq_self_right' {a b : α} (h₁ : b ≠ 1) (h₂ : a * b = a) : a = 0 := by apply (mul_eq_zero.1 _).resolve_right (sub_ne_zero.2 h₁); rw [mul_sub_left_distrib, mul_one, sub_eq_zero, h₂] theorem eq_zero_of_mul_eq_self_left' {a b : α} (h₁ : b ≠ 1) (h₂ : b * a = a) : a = 0 := by apply (mul_eq_zero.1 _).resolve_left (sub_ne_zero.2 h₁); rw [mul_sub_right_distrib, one_mul, sub_eq_zero, h₂] theorem mul_ne_zero_comm' {a b : α} (h : a * b ≠ 0) : b * a ≠ 0 := mul_ne_zero' (ne_zero_of_mul_ne_zero_left h) (ne_zero_of_mul_ne_zero_right h) end domain /- integral domains -/ section variables [s : integral_domain α] (a b c d e : α) include s instance integral_domain.to_domain : domain α := {..s} theorem eq_of_mul_eq_mul_right_of_ne_zero {a b c : α} (ha : a ≠ 0) (h : b * a = c * a) : b = c := have b * a - c * a = 0, by simp [h], have (b - c) * a = 0, by rw [mul_sub_right_distrib, this], have b - c = 0, from (eq_zero_or_eq_zero_of_mul_eq_zero this).resolve_right ha, eq_of_sub_eq_zero this theorem eq_of_mul_eq_mul_left_of_ne_zero {a b c : α} (ha : a ≠ 0) (h : a * b = a * c) : b = c := have a * b - a * c = 0, by simp [h], have a * (b - c) = 0, by rw [mul_sub_left_distrib, this], have b - c = 0, from (eq_zero_or_eq_zero_of_mul_eq_zero this).resolve_left ha, eq_of_sub_eq_zero this theorem mul_dvd_mul_iff_left {a b c : α} (ha : a ≠ 0) : a * b ∣ a * c ↔ b ∣ c := exists_congr $ λ d, by rw [mul_assoc, domain.mul_left_inj ha] theorem mul_dvd_mul_iff_right {a b c : α} (hc : c ≠ 0) : a * c ∣ b * c ↔ a ∣ b := exists_congr $ λ d, by rw [mul_right_comm, domain.mul_right_inj hc] lemma units.inv_eq_self_iff (u : units α) : u⁻¹ = u ↔ u = 1 ∨ u = -1 := by conv {to_lhs, rw [inv_eq_iff_mul_eq_one, ← mul_one (1 : units α), units.ext_iff, units.coe_mul, units.coe_mul, mul_self_eq_mul_self_iff, ← units.ext_iff, ← units.coe_neg, ← units.ext_iff] } end /- units in various rings -/ namespace units section comm_semiring variables [comm_semiring α] (a b : α) (u : units α) @[simp] lemma coe_dvd : ↑u ∣ a := ⟨↑u⁻¹ * a, by simp⟩ @[simp] lemma dvd_coe_mul : a ∣ b * u ↔ a ∣ b := iff.intro (assume ⟨c, eq⟩, ⟨c * ↑u⁻¹, by rw [← mul_assoc, ← eq, units.mul_inv_cancel_right]⟩) (assume ⟨c, eq⟩, eq.symm ▸ dvd_mul_of_dvd_left (dvd_mul_right _ _) _) @[simp] lemma dvd_coe : a ∣ ↑u ↔ a ∣ 1 := suffices a ∣ 1 * ↑u ↔ a ∣ 1, by simpa, dvd_coe_mul _ _ _ @[simp] lemma coe_mul_dvd : a * u ∣ b ↔ a ∣ b := iff.intro (assume ⟨c, eq⟩, ⟨c * ↑u, eq.symm ▸ by ac_refl⟩) (assume h, suffices a * ↑u ∣ b * 1, by simpa, mul_dvd_mul h (coe_dvd _ _)) end comm_semiring section domain variables [domain α] @[simp] theorem ne_zero : ∀(u : units α), (↑u : α) ≠ 0 | ⟨u, v, (huv : 0 * v = 1), hvu⟩ rfl := by simpa using huv end domain end units
00ca9ae8659e7efbdc89b971c5c34b6dd5d03b5e
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/nested_begin_end.lean
470c7969272a76ff41c7dc98c3efc2a65c6bc556
[ "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
543
lean
example (p q : Prop) : p ∧ q ↔ q ∧ p := begin apply iff.intro, begin intro H, apply and.intro, apply (and.elim_right H), apply (and.elim_left H) end, begin intro H, apply and.intro, apply (and.elim_right H), apply (and.elim_left H) end end example (p q : Prop) : p ∧ q ↔ q ∧ p := begin apply iff.intro, {intro H, apply and.intro, apply (and.elim_right H), apply (and.elim_left H)}, {intro H, apply and.intro, apply (and.elim_right H), apply (and.elim_left H)} end
9ea350ad2e86cfcfca6fb41d8d4742df8d62c1b7
4dbc106f944ae08d9082a937156fe53f8241336c
/src/data/sigma/on_fst.lean
ae8bbf8bf76fe4a6c49cb561ddca11d4d4efa1c9
[]
no_license
spl/lean-finmap
feff7ee53811b172531f84b20c02e50c787fe6fc
936d9caeb27631e3c6cf20e972de4837c9fe98fa
refs/heads/master
1,584,501,090,642
1,537,511,660,000
1,537,515,626,000
134,227,269
0
0
null
null
null
null
UTF-8
Lean
false
false
4,375
lean
import data.sigma.basic namespace sigma universes u v section variables {α : Type u} {β : α → Type v} theorem eq_fst {s₁ s₂ : sigma β} : s₁ = s₂ → s₁.1 = s₂.1 := by cases s₁; cases s₂; cc theorem eq_snd {s₁ s₂ : sigma β} : s₁ = s₂ → s₁.2 == s₂.2 := by cases s₁; cases s₂; cc end section variables {α₁ α₂ : Type u} {β₁ : α₁ → Type v} {β₂ : α₂ → Type v} /-- A function on `sigma`s that is functional on `fst`s (preserves equality from argument to result). -/ def fst_functional (f : sigma β₁ → sigma β₂) : Prop := ∀ ⦃s t : sigma β₁⦄, s.1 = t.1 → (f s).1 = (f t).1 /-- A function on `sigma`s that is injective on `fst`s (preserves equality from result to argument). -/ def fst_injective (f : sigma β₁ → sigma β₂) : Prop := ∀ ⦃s t : sigma β₁⦄, (f s).1 = (f t).1 → s.1 = t.1 end /-- A function on `sigma`s bundled with its `fst`-injectivity property. -/ structure embedding {α₁ α₂ : Type u} (β₁ : α₁ → Type v) (β₂ : α₂ → Type v) := (to_fun : sigma β₁ → sigma β₂) (fst_inj : fst_injective to_fun) infixr ` s↪ `:25 := embedding namespace embedding variables {α₁ α₂ : Type u} {β₁ : α₁ → Type v} {β₂ : α₂ → Type v} instance : has_coe_to_fun (β₁ s↪ β₂) := ⟨_, embedding.to_fun⟩ @[simp] theorem to_fun_eq_coe (f : β₁ s↪ β₂) : f.to_fun = f := rfl @[simp] theorem coe_fn_mk (f : sigma β₁ → sigma β₂) (i : fst_injective f) : (mk f i : sigma β₁ → sigma β₂) = f := rfl theorem fst_inj' : ∀ (f : β₁ s↪ β₂), fst_injective f | ⟨_, h⟩ := h end embedding section map_id variables {α : Type u} {β₁ β₂ : α → Type v} @[simp] theorem map_id_eq_fst {s : sigma β₁} (f : ∀ a, β₁ a → β₂ a) : (s.map id f).1 = s.1 := by cases s; refl theorem map_id_fst_functional (f : ∀ a, β₁ a → β₂ a) : fst_functional (map id f) := λ _ _, by simp only [map_id_eq_fst]; exact id theorem map_id_fst_injective (f : ∀ a, β₁ a → β₂ a) : fst_injective (map id f) := λ _ _, by simp only [map_id_eq_fst]; exact id /-- Construct an `embedding` with `id` on `fst`. -/ def embedding.mk₂ (f : ∀ a, β₁ a → β₂ a) : embedding β₁ β₂ := ⟨_, map_id_fst_injective f⟩ end map_id section variables {α : Type u} {β : α → Type v} {R : α → α → Prop} /-- A relation `R` on `fst` values lifted to the `sigma`. This is useful where you might otherwise use the term `λ s₁ s₂, R s₁.1 s₂.1`. -/ def fst_rel (R : α → α → Prop) (s₁ s₂ : sigma β) : Prop := R s₁.1 s₂.1 @[simp] theorem fst_rel_def {s₁ s₂ : sigma β} : fst_rel R s₁ s₂ = R s₁.1 s₂.1 := rfl instance fst_rel_decidable [d : decidable_rel R] : decidable_rel (@fst_rel _ β R) | s₁ s₂ := @d s₁.1 s₂.1 theorem fst_rel.refl (h : reflexive R) : reflexive (@fst_rel _ β R) := λ s, h s.1 theorem fst_rel.symm (h : symmetric R) : symmetric (@fst_rel _ β R) := λ s₁ s₂ (p : R s₁.1 s₂.1), h p theorem fst_rel.trans (h : transitive R) : transitive (@fst_rel _ β R) := λ s₁ s₂ s₃ (p : R s₁.1 s₂.1) (q : R s₂.1 s₃.1), h p q end section variables {α : Type u} {β : α → Type v} theorem fst_functional_id : fst_functional (@id (sigma β)) := λ s t h, h theorem fst_injective_id : fst_injective (@id (sigma β)) := λ s t h, h @[refl] protected def embedding.refl (β : α → Type v) : β s↪ β := ⟨_, fst_injective_id⟩ @[simp] theorem embedding.refl_apply (s : sigma β) : embedding.refl β s = s := rfl end section variables {α₁ α₂ α₃ : Type u} variables {β₁ : α₁ → Type v} {β₂ : α₂ → Type v} {β₃ : α₃ → Type v} variables {g : sigma β₂ → sigma β₃} {f : sigma β₁ → sigma β₂} theorem fst_functional_comp (gf : fst_functional g) (ff : fst_functional f) : fst_functional (g ∘ f) := λ s t h, gf (ff h) theorem fst_injective_comp (gi : fst_injective g) (fi : fst_injective f) : fst_injective (g ∘ f) := λ s t h, fi (gi h) @[trans] protected def embedding.trans (f : β₁ s↪ β₂) (g : β₂ s↪ β₃) : β₁ s↪ β₃ := ⟨_, fst_injective_comp g.fst_inj f.fst_inj⟩ @[simp] theorem embedding.trans_apply (f : β₁ s↪ β₂) (g : β₂ s↪ β₃) (s : sigma β₁) : (f.trans g) s = g (f s) := rfl end end sigma
b13d26ed1a10035162f1eefa31c5236843e758d1
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/combinatorics/simple_graph/triangle/basic.lean
ed1a2512fedaf666e60d700f0ffb62e94dd3aa48
[ "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
3,052
lean
/- Copyright (c) 2022 Yaël Dillies, Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies, Bhavik Mehta -/ import combinatorics.simple_graph.clique /-! # Triangles in graphs A *triangle* in a simple graph is a `3`-clique, namely a set of three vertices that are pairwise adjacent. This module defines and proves properties about triangles in simple graphs. ## Main declarations * `simple_graph.far_from_triangle_free`: Predicate for a graph to have enough triangles that, to remove all of them, one must one must remove a lot of edges. This is the crux of the Triangle Removal lemma. ## TODO * Generalise `far_from_triangle_free` to other graphs, to state and prove the Graph Removal Lemma. * Find a better name for `far_from_triangle_free`. Added 4/26/2022. Remove this TODO if it gets old. -/ open finset fintype nat open_locale classical namespace simple_graph variables {α 𝕜 : Type*} [fintype α] [linear_ordered_field 𝕜] {G H : simple_graph α} {ε δ : 𝕜} {n : ℕ} {s : finset α} /-- A simple graph is *`ε`-triangle-free far* if one must remove at least `ε * (card α)^2` edges to make it triangle-free. -/ def far_from_triangle_free (G : simple_graph α) (ε : 𝕜) : Prop := G.delete_far (λ H, H.clique_free 3) $ ε * (card α^2 : ℕ) lemma far_from_triangle_free_iff : G.far_from_triangle_free ε ↔ ∀ ⦃H⦄, H ≤ G → H.clique_free 3 → ε * (card α^2 : ℕ) ≤ G.edge_finset.card - H.edge_finset.card := delete_far_iff alias far_from_triangle_free_iff ↔ far_from_triangle_free.le_card_sub_card _ lemma far_from_triangle_free.mono (hε : G.far_from_triangle_free ε) (h : δ ≤ ε) : G.far_from_triangle_free δ := hε.mono $ mul_le_mul_of_nonneg_right h $ cast_nonneg _ lemma far_from_triangle_free.clique_finset_nonempty' (hH : H ≤ G) (hG : G.far_from_triangle_free ε) (hcard : (G.edge_finset.card - H.edge_finset.card : 𝕜) < ε * (card α ^ 2 : ℕ)) : (H.clique_finset 3).nonempty := nonempty_of_ne_empty $ H.clique_finset_eq_empty_iff.not.2 $ λ hH', (hG.le_card_sub_card hH hH').not_lt hcard variables [nonempty α] lemma far_from_triangle_free.nonpos (h₀ : G.far_from_triangle_free ε) (h₁ : G.clique_free 3) : ε ≤ 0 := begin have := h₀ (empty_subset _), rw [coe_empty, finset.card_empty, cast_zero, delete_edges_empty_eq] at this, exact nonpos_of_mul_nonpos_left (this h₁) (cast_pos.2 $ sq_pos_of_pos fintype.card_pos), end lemma clique_free.not_far_from_triangle_free (hG : G.clique_free 3) (hε : 0 < ε) : ¬ G.far_from_triangle_free ε := λ h, (h.nonpos hG).not_lt hε lemma far_from_triangle_free.not_clique_free (hG : G.far_from_triangle_free ε) (hε : 0 < ε) : ¬ G.clique_free 3 := λ h, (hG.nonpos h).not_lt hε lemma far_from_triangle_free.clique_finset_nonempty (hG : G.far_from_triangle_free ε) (hε : 0 < ε) : (G.clique_finset 3).nonempty := nonempty_of_ne_empty $ G.clique_finset_eq_empty_iff.not.2 $ hG.not_clique_free hε end simple_graph
1a24c013dfdec40b72595b39358c88152e658efe
367134ba5a65885e863bdc4507601606690974c1
/src/order/liminf_limsup.lean
645bfc9c7bfb5a10c885401117cdd3e61f6ad15e
[ "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
22,096
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, Johannes Hölzl, Rémy Degenne -/ import order.filter.partial import order.filter.at_top_bot /-! # liminfs and limsups of functions and filters Defines the Liminf/Limsup of a function taking values in a conditionally complete lattice, with respect to an arbitrary filter. We define `f.Limsup` (`f.Liminf`) where `f` is a filter taking values in a conditionally complete lattice. `f.Limsup` is the smallest element `a` such that, eventually, `u ≤ a` (and vice versa for `f.Liminf`). To work with the Limsup along a function `u` use `(f.map u).Limsup`. Usually, one defines the Limsup as `Inf (Sup s)` where the Inf is taken over all sets in the filter. For instance, in ℕ along a function `u`, this is `Inf_n (Sup_{k ≥ n} u k)` (and the latter quantity decreases with `n`, so this is in fact a limit.). There is however a difficulty: it is well possible that `u` is not bounded on the whole space, only eventually (think of `Limsup (λx, 1/x)` on ℝ. Then there is no guarantee that the quantity above really decreases (the value of the `Sup` beforehand is not really well defined, as one can not use ∞), so that the Inf could be anything. So one can not use this `Inf Sup ...` definition in conditionally complete lattices, and one has to use a less tractable definition. In conditionally complete lattices, the definition is only useful for filters which are eventually bounded above (otherwise, the Limsup would morally be +∞, which does not belong to the space) and which are frequently bounded below (otherwise, the Limsup would morally be -∞, which is not in the space either). We start with definitions of these concepts for arbitrary filters, before turning to the definitions of Limsup and Liminf. In complete lattices, however, it coincides with the `Inf Sup` definition. -/ open filter set open_locale filter variables {α β ι : Type*} namespace filter section relation /-- `f.is_bounded (≺)`: the filter `f` is eventually bounded w.r.t. the relation `≺`, i.e. eventually, it is bounded by some uniform bound. `r` will be usually instantiated with `≤` or `≥`. -/ def is_bounded (r : α → α → Prop) (f : filter α) := ∃ b, ∀ᶠ x in f, r x b /-- `f.is_bounded_under (≺) u`: the image of the filter `f` under `u` is eventually bounded w.r.t. the relation `≺`, i.e. eventually, it is bounded by some uniform bound. -/ def is_bounded_under (r : α → α → Prop) (f : filter β) (u : β → α) := (f.map u).is_bounded r variables {r : α → α → Prop} {f g : filter α} /-- `f` is eventually bounded if and only if, there exists an admissible set on which it is bounded. -/ lemma is_bounded_iff : f.is_bounded r ↔ (∃s∈f.sets, ∃b, s ⊆ {x | r x b}) := iff.intro (assume ⟨b, hb⟩, ⟨{a | r a b}, hb, b, subset.refl _⟩) (assume ⟨s, hs, b, hb⟩, ⟨b, mem_sets_of_superset hs hb⟩) /-- A bounded function `u` is in particular eventually bounded. -/ lemma is_bounded_under_of {f : filter β} {u : β → α} : (∃b, ∀x, r (u x) b) → f.is_bounded_under r u | ⟨b, hb⟩ := ⟨b, show ∀ᶠ x in f, r (u x) b, from eventually_of_forall hb⟩ lemma is_bounded_bot : is_bounded r ⊥ ↔ nonempty α := by simp [is_bounded, exists_true_iff_nonempty] lemma is_bounded_top : is_bounded r ⊤ ↔ (∃t, ∀x, r x t) := by simp [is_bounded, eq_univ_iff_forall] lemma is_bounded_principal (s : set α) : is_bounded r (𝓟 s) ↔ (∃t, ∀x∈s, r x t) := by simp [is_bounded, subset_def] lemma is_bounded_sup [is_trans α r] (hr : ∀b₁ b₂, ∃b, r b₁ b ∧ r b₂ b) : is_bounded r f → is_bounded r g → is_bounded r (f ⊔ g) | ⟨b₁, h₁⟩ ⟨b₂, h₂⟩ := let ⟨b, rb₁b, rb₂b⟩ := hr b₁ b₂ in ⟨b, eventually_sup.mpr ⟨h₁.mono (λ x h, trans h rb₁b), h₂.mono (λ x h, trans h rb₂b)⟩⟩ lemma is_bounded.mono (h : f ≤ g) : is_bounded r g → is_bounded r f | ⟨b, hb⟩ := ⟨b, h hb⟩ lemma is_bounded_under.mono {f g : filter β} {u : β → α} (h : f ≤ g) : g.is_bounded_under r u → f.is_bounded_under r u := λ hg, hg.mono (map_mono h) lemma is_bounded.is_bounded_under {q : β → β → Prop} {u : α → β} (hf : ∀a₀ a₁, r a₀ a₁ → q (u a₀) (u a₁)) : f.is_bounded r → f.is_bounded_under q u | ⟨b, h⟩ := ⟨u b, show ∀ᶠ x in f, q (u x) (u b), from h.mono (λ x, hf x b)⟩ /-- `is_cobounded (≺) f` states that the filter `f` does not tend to infinity w.r.t. `≺`. This is also called frequently bounded. Will be usually instantiated with `≤` or `≥`. There is a subtlety in this definition: we want `f.is_cobounded` to hold for any `f` in the case of complete lattices. This will be relevant to deduce theorems on complete lattices from their versions on conditionally complete lattices with additional assumptions. We have to be careful in the edge case of the trivial filter containing the empty set: the other natural definition `¬ ∀ a, ∀ᶠ n in f, a ≤ n` would not work as well in this case. -/ def is_cobounded (r : α → α → Prop) (f : filter α) := ∃b, ∀a, (∀ᶠ x in f, r x a) → r b a /-- `is_cobounded_under (≺) f u` states that the image of the filter `f` under the map `u` does not tend to infinity w.r.t. `≺`. This is also called frequently bounded. Will be usually instantiated with `≤` or `≥`. -/ def is_cobounded_under (r : α → α → Prop) (f : filter β) (u : β → α) := (f.map u).is_cobounded r /-- To check that a filter is frequently bounded, it suffices to have a witness which bounds `f` at some point for every admissible set. This is only an implication, as the other direction is wrong for the trivial filter.-/ lemma is_cobounded.mk [is_trans α r] (a : α) (h : ∀s∈f, ∃x∈s, r a x) : f.is_cobounded r := ⟨a, assume y s, let ⟨x, h₁, h₂⟩ := h _ s in trans h₂ h₁⟩ /-- A filter which is eventually bounded is in particular frequently bounded (in the opposite direction). At least if the filter is not trivial. -/ lemma is_bounded.is_cobounded_flip [is_trans α r] [ne_bot f] : f.is_bounded r → f.is_cobounded (flip r) | ⟨a, ha⟩ := ⟨a, assume b hb, let ⟨x, rxa, rbx⟩ := (ha.and hb).exists in show r b a, from trans rbx rxa⟩ lemma is_cobounded_bot : is_cobounded r ⊥ ↔ (∃b, ∀x, r b x) := by simp [is_cobounded] lemma is_cobounded_top : is_cobounded r ⊤ ↔ nonempty α := by simp [is_cobounded, eq_univ_iff_forall, exists_true_iff_nonempty] {contextual := tt} lemma is_cobounded_principal (s : set α) : (𝓟 s).is_cobounded r ↔ (∃b, ∀a, (∀x∈s, r x a) → r b a) := by simp [is_cobounded, subset_def] lemma is_cobounded.mono (h : f ≤ g) : f.is_cobounded r → g.is_cobounded r | ⟨b, hb⟩ := ⟨b, assume a ha, hb a (h ha)⟩ end relation lemma is_cobounded_le_of_bot [order_bot α] {f : filter α} : f.is_cobounded (≤) := ⟨⊥, assume a h, bot_le⟩ lemma is_cobounded_ge_of_top [order_top α] {f : filter α} : f.is_cobounded (≥) := ⟨⊤, assume a h, le_top⟩ lemma is_bounded_le_of_top [order_top α] {f : filter α} : f.is_bounded (≤) := ⟨⊤, eventually_of_forall $ λ _, le_top⟩ lemma is_bounded_ge_of_bot [order_bot α] {f : filter α} : f.is_bounded (≥) := ⟨⊥, eventually_of_forall $ λ _, bot_le⟩ lemma is_bounded_under_sup [semilattice_sup α] {f : filter β} {u v : β → α} : f.is_bounded_under (≤) u → f.is_bounded_under (≤) v → f.is_bounded_under (≤) (λa, u a ⊔ v a) | ⟨bu, (hu : ∀ᶠ x in f, u x ≤ bu)⟩ ⟨bv, (hv : ∀ᶠ x in f, v x ≤ bv)⟩ := ⟨bu ⊔ bv, show ∀ᶠ x in f, u x ⊔ v x ≤ bu ⊔ bv, by filter_upwards [hu, hv] assume x, sup_le_sup⟩ lemma is_bounded_under_inf [semilattice_inf α] {f : filter β} {u v : β → α} : f.is_bounded_under (≥) u → f.is_bounded_under (≥) v → f.is_bounded_under (≥) (λa, u a ⊓ v a) | ⟨bu, (hu : ∀ᶠ x in f, u x ≥ bu)⟩ ⟨bv, (hv : ∀ᶠ x in f, v x ≥ bv)⟩ := ⟨bu ⊓ bv, show ∀ᶠ x in f, u x ⊓ v x ≥ bu ⊓ bv, by filter_upwards [hu, hv] assume x, inf_le_inf⟩ /-- Filters are automatically bounded or cobounded in complete lattices. To use the same statements in complete and conditionally complete lattices but let automation fill automatically the boundedness proofs in complete lattices, we use the tactic `is_bounded_default` in the statements, in the form `(hf : f.is_bounded (≥) . is_bounded_default)`. -/ meta def is_bounded_default : tactic unit := tactic.applyc ``is_cobounded_le_of_bot <|> tactic.applyc ``is_cobounded_ge_of_top <|> tactic.applyc ``is_bounded_le_of_top <|> tactic.applyc ``is_bounded_ge_of_bot section conditionally_complete_lattice variables [conditionally_complete_lattice α] /-- The `Limsup` of a filter `f` is the infimum of the `a` such that, eventually for `f`, holds `x ≤ a`. -/ def Limsup (f : filter α) : α := Inf { a | ∀ᶠ n in f, n ≤ a } /-- The `Liminf` of a filter `f` is the supremum of the `a` such that, eventually for `f`, holds `x ≥ a`. -/ def Liminf (f : filter α) : α := Sup { a | ∀ᶠ n in f, a ≤ n } /-- The `limsup` of a function `u` along a filter `f` is the infimum of the `a` such that, eventually for `f`, holds `u x ≤ a`. -/ def limsup (f : filter β) (u : β → α) : α := (f.map u).Limsup /-- The `liminf` of a function `u` along a filter `f` is the supremum of the `a` such that, eventually for `f`, holds `u x ≥ a`. -/ def liminf (f : filter β) (u : β → α) : α := (f.map u).Liminf section variables {f : filter β} {u : β → α} theorem limsup_eq : f.limsup u = Inf { a | ∀ᶠ n in f, u n ≤ a } := rfl theorem liminf_eq : f.liminf u = Sup { a | ∀ᶠ n in f, a ≤ u n } := rfl end theorem Limsup_le_of_le {f : filter α} {a} (hf : f.is_cobounded (≤) . is_bounded_default) (h : ∀ᶠ n in f, n ≤ a) : f.Limsup ≤ a := cInf_le hf h theorem le_Liminf_of_le {f : filter α} {a} (hf : f.is_cobounded (≥) . is_bounded_default) (h : ∀ᶠ n in f, a ≤ n) : a ≤ f.Liminf := le_cSup hf h theorem le_Limsup_of_le {f : filter α} {a} (hf : f.is_bounded (≤) . is_bounded_default) (h : ∀ b, (∀ᶠ n in f, n ≤ b) → a ≤ b) : a ≤ f.Limsup := le_cInf hf h theorem Liminf_le_of_le {f : filter α} {a} (hf : f.is_bounded (≥) . is_bounded_default) (h : ∀ b, (∀ᶠ n in f, b ≤ n) → b ≤ a) : f.Liminf ≤ a := cSup_le hf h theorem Liminf_le_Limsup {f : filter α} [ne_bot f] (h₁ : f.is_bounded (≤) . is_bounded_default) (h₂ : f.is_bounded (≥) . is_bounded_default) : f.Liminf ≤ f.Limsup := Liminf_le_of_le h₂ $ assume a₀ ha₀, le_Limsup_of_le h₁ $ assume a₁ ha₁, show a₀ ≤ a₁, from let ⟨b, hb₀, hb₁⟩ := (ha₀.and ha₁).exists in le_trans hb₀ hb₁ lemma Liminf_le_Liminf {f g : filter α} (hf : f.is_bounded (≥) . is_bounded_default) (hg : g.is_cobounded (≥) . is_bounded_default) (h : ∀ a, (∀ᶠ n in f, a ≤ n) → ∀ᶠ n in g, a ≤ n) : f.Liminf ≤ g.Liminf := cSup_le_cSup hg hf h lemma Limsup_le_Limsup {f g : filter α} (hf : f.is_cobounded (≤) . is_bounded_default) (hg : g.is_bounded (≤) . is_bounded_default) (h : ∀ a, (∀ᶠ n in g, n ≤ a) → ∀ᶠ n in f, n ≤ a) : f.Limsup ≤ g.Limsup := cInf_le_cInf hf hg h lemma Limsup_le_Limsup_of_le {f g : filter α} (h : f ≤ g) (hf : f.is_cobounded (≤) . is_bounded_default) (hg : g.is_bounded (≤) . is_bounded_default) : f.Limsup ≤ g.Limsup := Limsup_le_Limsup hf hg (assume a ha, h ha) lemma Liminf_le_Liminf_of_le {f g : filter α} (h : g ≤ f) (hf : f.is_bounded (≥) . is_bounded_default) (hg : g.is_cobounded (≥) . is_bounded_default) : f.Liminf ≤ g.Liminf := Liminf_le_Liminf hf hg (assume a ha, h ha) lemma limsup_le_limsup {α : Type*} [conditionally_complete_lattice β] {f : filter α} {u v : α → β} (h : u ≤ᶠ[f] v) (hu : f.is_cobounded_under (≤) u . is_bounded_default) (hv : f.is_bounded_under (≤) v . is_bounded_default) : f.limsup u ≤ f.limsup v := Limsup_le_Limsup hu hv $ assume b, h.trans lemma liminf_le_liminf {α : Type*} [conditionally_complete_lattice β] {f : filter α} {u v : α → β} (h : ∀ᶠ a in f, u a ≤ v a) (hu : f.is_bounded_under (≥) u . is_bounded_default) (hv : f.is_cobounded_under (≥) v . is_bounded_default) : f.liminf u ≤ f.liminf v := @limsup_le_limsup (order_dual β) α _ _ _ _ h hv hu theorem Limsup_principal {s : set α} (h : bdd_above s) (hs : s.nonempty) : (𝓟 s).Limsup = Sup s := by simp [Limsup]; exact cInf_upper_bounds_eq_cSup h hs theorem Liminf_principal {s : set α} (h : bdd_below s) (hs : s.nonempty) : (𝓟 s).Liminf = Inf s := @Limsup_principal (order_dual α) _ s h hs lemma limsup_congr {α : Type*} [conditionally_complete_lattice β] {f : filter α} {u v : α → β} (h : ∀ᶠ a in f, u a = v a) : limsup f u = limsup f v := begin rw limsup_eq, congr' with b, exact eventually_congr (h.mono $ λ x hx, by simp [hx]) end lemma liminf_congr {α : Type*} [conditionally_complete_lattice β] {f : filter α} {u v : α → β} (h : ∀ᶠ a in f, u a = v a) : liminf f u = liminf f v := @limsup_congr (order_dual β) _ _ _ _ _ h lemma limsup_const {α : Type*} [conditionally_complete_lattice β] {f : filter α} [ne_bot f] (b : β) : limsup f (λ x, b) = b := by simpa only [limsup_eq, eventually_const] using cInf_Ici lemma liminf_const {α : Type*} [conditionally_complete_lattice β] {f : filter α} [ne_bot f] (b : β) : liminf f (λ x, b) = b := @limsup_const (order_dual β) α _ f _ b end conditionally_complete_lattice section complete_lattice variables [complete_lattice α] @[simp] theorem Limsup_bot : (⊥ : filter α).Limsup = ⊥ := bot_unique $ Inf_le $ by simp @[simp] theorem Liminf_bot : (⊥ : filter α).Liminf = ⊤ := top_unique $ le_Sup $ by simp @[simp] theorem Limsup_top : (⊤ : filter α).Limsup = ⊤ := top_unique $ le_Inf $ by simp [eq_univ_iff_forall]; exact assume b hb, (top_unique $ hb _) @[simp] theorem Liminf_top : (⊤ : filter α).Liminf = ⊥ := bot_unique $ Sup_le $ by simp [eq_univ_iff_forall]; exact assume b hb, (bot_unique $ hb _) /-- Same as limsup_const applied to `⊥` but without the `ne_bot f` assumption -/ lemma limsup_const_bot {f : filter β} : limsup f (λ x : β, (⊥ : α)) = (⊥ : α) := begin rw [limsup_eq, eq_bot_iff], exact Inf_le (eventually_of_forall (λ x, le_refl _)), end /-- Same as limsup_const applied to `⊤` but without the `ne_bot f` assumption -/ lemma liminf_const_top {f : filter β} : liminf f (λ x : β, (⊤ : α)) = (⊤ : α) := @limsup_const_bot (order_dual α) β _ _ lemma liminf_le_limsup {f : filter β} [ne_bot f] {u : β → α} : liminf f u ≤ limsup f u := Liminf_le_Limsup is_bounded_le_of_top is_bounded_ge_of_bot theorem has_basis.Limsup_eq_infi_Sup {ι} {p : ι → Prop} {s} {f : filter α} (h : f.has_basis p s) : f.Limsup = ⨅ i (hi : p i), Sup (s i) := le_antisymm (le_binfi $ λ i hi, Inf_le $ h.eventually_iff.2 ⟨i, hi, λ x, le_Sup⟩) (le_Inf $ assume a ha, let ⟨i, hi, ha⟩ := h.eventually_iff.1 ha in infi_le_of_le _ $ infi_le_of_le hi $ Sup_le ha) theorem has_basis.Liminf_eq_supr_Inf {p : ι → Prop} {s : ι → set α} {f : filter α} (h : f.has_basis p s) : f.Liminf = ⨆ i (hi : p i), Inf (s i) := @has_basis.Limsup_eq_infi_Sup (order_dual α) _ _ _ _ _ h theorem Limsup_eq_infi_Sup {f : filter α} : f.Limsup = ⨅ s ∈ f, Sup s := f.basis_sets.Limsup_eq_infi_Sup theorem Liminf_eq_supr_Inf {f : filter α} : f.Liminf = ⨆ s ∈ f, Inf s := @Limsup_eq_infi_Sup (order_dual α) _ _ /-- In a complete lattice, the limsup of a function is the infimum over sets `s` in the filter of the supremum of the function over `s` -/ theorem limsup_eq_infi_supr {f : filter β} {u : β → α} : f.limsup u = ⨅ s ∈ f, ⨆ a ∈ s, u a := (f.basis_sets.map u).Limsup_eq_infi_Sup.trans $ by simp only [Sup_image, id] lemma limsup_eq_infi_supr_of_nat {u : ℕ → α} : limsup at_top u = ⨅ n : ℕ, ⨆ i ≥ n, u i := (at_top_basis.map u).Limsup_eq_infi_Sup.trans $ by simp only [Sup_image, infi_const]; refl lemma limsup_eq_infi_supr_of_nat' {u : ℕ → α} : limsup at_top u = ⨅ n : ℕ, ⨆ i : ℕ, u (i + n) := by simp only [limsup_eq_infi_supr_of_nat, supr_ge_eq_supr_nat_add] theorem has_basis.limsup_eq_infi_supr {p : ι → Prop} {s : ι → set β} {f : filter β} {u : β → α} (h : f.has_basis p s) : f.limsup u = ⨅ i (hi : p i), ⨆ a ∈ s i, u a := (h.map u).Limsup_eq_infi_Sup.trans $ by simp only [Sup_image, id] /-- In a complete lattice, the liminf of a function is the infimum over sets `s` in the filter of the supremum of the function over `s` -/ theorem liminf_eq_supr_infi {f : filter β} {u : β → α} : f.liminf u = ⨆ s ∈ f, ⨅ a ∈ s, u a := @limsup_eq_infi_supr (order_dual α) β _ _ _ lemma liminf_eq_supr_infi_of_nat {u : ℕ → α} : liminf at_top u = ⨆ n : ℕ, ⨅ i ≥ n, u i := @limsup_eq_infi_supr_of_nat (order_dual α) _ u lemma liminf_eq_supr_infi_of_nat' {u : ℕ → α} : liminf at_top u = ⨆ n : ℕ, ⨅ i : ℕ, u (i + n) := @limsup_eq_infi_supr_of_nat' (order_dual α) _ _ theorem has_basis.liminf_eq_supr_infi {p : ι → Prop} {s : ι → set β} {f : filter β} {u : β → α} (h : f.has_basis p s) : f.liminf u = ⨆ i (hi : p i), ⨅ a ∈ s i, u a := @has_basis.limsup_eq_infi_supr (order_dual α) _ _ _ _ _ _ _ h @[simp] lemma liminf_nat_add (f : ℕ → α) (k : ℕ) : at_top.liminf (λ i, f (i + k)) = at_top.liminf f := by { simp_rw liminf_eq_supr_infi_of_nat, exact supr_infi_ge_nat_add f k } @[simp] lemma limsup_nat_add (f : ℕ → α) (k : ℕ) : at_top.limsup (λ i, f (i + k)) = at_top.limsup f := @liminf_nat_add (order_dual α) _ f k lemma liminf_le_of_frequently_le' {α β} [complete_lattice β] {f : filter α} {u : α → β} {x : β} (h : ∃ᶠ a in f, u a ≤ x) : f.liminf u ≤ x := begin rw liminf_eq, refine Sup_le (λ b hb, _), have hbx : ∃ᶠ a in f, b ≤ x, { revert h, rw [←not_imp_not, not_frequently, not_frequently], exact λ h, hb.mp (h.mono (λ a hbx hba hax, hbx (hba.trans hax))), }, exact hbx.exists.some_spec, end lemma le_limsup_of_frequently_le' {α β} [complete_lattice β] {f : filter α} {u : α → β} {x : β} (h : ∃ᶠ a in f, x ≤ u a) : x ≤ f.limsup u := @liminf_le_of_frequently_le' _ (order_dual β) _ _ _ _ h end complete_lattice section conditionally_complete_linear_order lemma eventually_lt_of_lt_liminf {f : filter α} [conditionally_complete_linear_order β] {u : α → β} {b : β} (h : b < liminf f u) (hu : f.is_bounded_under (≥) u . is_bounded_default) : ∀ᶠ a in f, b < u a := begin obtain ⟨c, hc, hbc⟩ : ∃ (c : β) (hc : c ∈ {c : β | ∀ᶠ (n : α) in f, c ≤ u n}), b < c := exists_lt_of_lt_cSup hu h, exact hc.mono (λ x hx, lt_of_lt_of_le hbc hx) end lemma eventually_lt_of_limsup_lt {f : filter α} [conditionally_complete_linear_order β] {u : α → β} {b : β} (h : limsup f u < b) (hu : f.is_bounded_under (≤) u . is_bounded_default) : ∀ᶠ a in f, u a < b := @eventually_lt_of_lt_liminf _ (order_dual β) _ _ _ _ h hu lemma le_limsup_of_frequently_le {α β} [conditionally_complete_linear_order β] {f : filter α} {u : α → β} {b : β} (hu_le : ∃ᶠ x in f, b ≤ u x) (hu : f.is_bounded_under (≤) u . is_bounded_default) : b ≤ f.limsup u := begin revert hu_le, rw [←not_imp_not, not_frequently], simp_rw ←lt_iff_not_ge, exact λ h, eventually_lt_of_limsup_lt h hu, end lemma liminf_le_of_frequently_le {α β} [conditionally_complete_linear_order β] {f : filter α} {u : α → β} {b : β} (hu_le : ∃ᶠ x in f, u x ≤ b) (hu : f.is_bounded_under (≥) u . is_bounded_default) : f.liminf u ≤ b := @le_limsup_of_frequently_le _ (order_dual β) _ f u b hu_le hu end conditionally_complete_linear_order end filter section order open filter lemma galois_connection.l_limsup_le {α β γ} [conditionally_complete_lattice β] [conditionally_complete_lattice γ] {f : filter α} {v : α → β} {l : β → γ} {u : γ → β} (gc : galois_connection l u) (hlv : f.is_bounded_under (≤) (λ x, l (v x)) . is_bounded_default) (hv_co : f.is_cobounded_under (≤) v . is_bounded_default) : l (f.limsup v) ≤ f.limsup (λ x, l (v x)) := begin refine le_Limsup_of_le hlv (λ c hc, _), rw filter.eventually_map at hc, simp_rw (gc _ _) at hc ⊢, exact Limsup_le_of_le hv_co hc, end lemma order_iso.limsup_apply {γ} [conditionally_complete_lattice β] [conditionally_complete_lattice γ] {f : filter α} {u : α → β} (g : β ≃o γ) (hu : f.is_bounded_under (≤) u . is_bounded_default) (hu_co : f.is_cobounded_under (≤) u . is_bounded_default) (hgu : f.is_bounded_under (≤) (λ x, g (u x)) . is_bounded_default) (hgu_co : f.is_cobounded_under (≤) (λ x, g (u x)) . is_bounded_default) : g (f.limsup u) = f.limsup (λ x, g (u x)) := begin refine le_antisymm (g.to_galois_connection.l_limsup_le hgu hu_co) _, rw [←(g.symm.symm_apply_apply (f.limsup (λ (x : α), g (u x)))), g.symm_symm], refine g.monotone _, have hf : u = λ i, g.symm (g (u i)), from funext (λ i, (g.symm_apply_apply (u i)).symm), nth_rewrite 0 hf, refine g.symm.to_galois_connection.l_limsup_le _ hgu_co, simp_rw g.symm_apply_apply, exact hu, end lemma order_iso.liminf_apply {γ} [conditionally_complete_lattice β] [conditionally_complete_lattice γ] {f : filter α} {u : α → β} (g : β ≃o γ) (hu : f.is_bounded_under (≥) u . is_bounded_default) (hu_co : f.is_cobounded_under (≥) u . is_bounded_default) (hgu : f.is_bounded_under (≥) (λ x, g (u x)) . is_bounded_default) (hgu_co : f.is_cobounded_under (≥) (λ x, g (u x)) . is_bounded_default) : g (f.liminf u) = f.liminf (λ x, g (u x)) := @order_iso.limsup_apply α (order_dual β) (order_dual γ) _ _ f u g.dual hu hu_co hgu hgu_co end order
83923d7c0a4da4591d7a133f049aa6990d5d4b69
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/special_functions/trigonometric/inverse.lean
0d6c733ba75514c0a3bdbd324f70051aa21af647
[ "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,503
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne, Benjamin Davidson -/ import analysis.special_functions.trigonometric.basic import topology.algebra.order.proj_Icc /-! # Inverse trigonometric functions. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. See also `analysis.special_functions.trigonometric.arctan` for the inverse tan function. (This is delayed as it is easier to set up after developing complex trigonometric functions.) Basic inequalities on trigonometric functions. -/ noncomputable theory open_locale classical topology filter open set filter open_locale real namespace real /-- Inverse of the `sin` function, returns values in the range `-π / 2 ≤ arcsin x ≤ π / 2`. It defaults to `-π / 2` on `(-∞, -1)` and to `π / 2` to `(1, ∞)`. -/ @[pp_nodot] noncomputable def arcsin : ℝ → ℝ := coe ∘ Icc_extend (neg_le_self zero_le_one) sin_order_iso.symm lemma arcsin_mem_Icc (x : ℝ) : arcsin x ∈ Icc (-(π / 2)) (π / 2) := subtype.coe_prop _ @[simp] lemma range_arcsin : range arcsin = Icc (-(π / 2)) (π / 2) := by { rw [arcsin, range_comp coe], simp [Icc] } lemma arcsin_le_pi_div_two (x : ℝ) : arcsin x ≤ π / 2 := (arcsin_mem_Icc x).2 lemma neg_pi_div_two_le_arcsin (x : ℝ) : -(π / 2) ≤ arcsin x := (arcsin_mem_Icc x).1 lemma arcsin_proj_Icc (x : ℝ) : arcsin (proj_Icc (-1) 1 (neg_le_self zero_le_one) x) = arcsin x := by rw [arcsin, function.comp_app, Icc_extend_coe, function.comp_app, Icc_extend] lemma sin_arcsin' {x : ℝ} (hx : x ∈ Icc (-1 : ℝ) 1) : sin (arcsin x) = x := by simpa [arcsin, Icc_extend_of_mem _ _ hx, -order_iso.apply_symm_apply] using subtype.ext_iff.1 (sin_order_iso.apply_symm_apply ⟨x, hx⟩) lemma sin_arcsin {x : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) : sin (arcsin x) = x := sin_arcsin' ⟨hx₁, hx₂⟩ lemma arcsin_sin' {x : ℝ} (hx : x ∈ Icc (-(π / 2)) (π / 2)) : arcsin (sin x) = x := inj_on_sin (arcsin_mem_Icc _) hx $ by rw [sin_arcsin (neg_one_le_sin _) (sin_le_one _)] lemma arcsin_sin {x : ℝ} (hx₁ : -(π / 2) ≤ x) (hx₂ : x ≤ π / 2) : arcsin (sin x) = x := arcsin_sin' ⟨hx₁, hx₂⟩ lemma strict_mono_on_arcsin : strict_mono_on arcsin (Icc (-1) 1) := (subtype.strict_mono_coe _).comp_strict_mono_on $ sin_order_iso.symm.strict_mono.strict_mono_on_Icc_extend _ lemma monotone_arcsin : monotone arcsin := (subtype.mono_coe _).comp $ sin_order_iso.symm.monotone.Icc_extend _ lemma inj_on_arcsin : inj_on arcsin (Icc (-1) 1) := strict_mono_on_arcsin.inj_on lemma arcsin_inj {x y : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) (hy₁ : -1 ≤ y) (hy₂ : y ≤ 1) : arcsin x = arcsin y ↔ x = y := inj_on_arcsin.eq_iff ⟨hx₁, hx₂⟩ ⟨hy₁, hy₂⟩ @[continuity] lemma continuous_arcsin : continuous arcsin := continuous_subtype_coe.comp sin_order_iso.symm.continuous.Icc_extend' lemma continuous_at_arcsin {x : ℝ} : continuous_at arcsin x := continuous_arcsin.continuous_at lemma arcsin_eq_of_sin_eq {x y : ℝ} (h₁ : sin x = y) (h₂ : x ∈ Icc (-(π / 2)) (π / 2)) : arcsin y = x := begin subst y, exact inj_on_sin (arcsin_mem_Icc _) h₂ (sin_arcsin' (sin_mem_Icc x)) end @[simp] lemma arcsin_zero : arcsin 0 = 0 := arcsin_eq_of_sin_eq sin_zero ⟨neg_nonpos.2 pi_div_two_pos.le, pi_div_two_pos.le⟩ @[simp] lemma arcsin_one : arcsin 1 = π / 2 := arcsin_eq_of_sin_eq sin_pi_div_two $ right_mem_Icc.2 (neg_le_self pi_div_two_pos.le) lemma arcsin_of_one_le {x : ℝ} (hx : 1 ≤ x) : arcsin x = π / 2 := by rw [← arcsin_proj_Icc, proj_Icc_of_right_le _ hx, subtype.coe_mk, arcsin_one] lemma arcsin_neg_one : arcsin (-1) = -(π / 2) := arcsin_eq_of_sin_eq (by rw [sin_neg, sin_pi_div_two]) $ left_mem_Icc.2 (neg_le_self pi_div_two_pos.le) lemma arcsin_of_le_neg_one {x : ℝ} (hx : x ≤ -1) : arcsin x = -(π / 2) := by rw [← arcsin_proj_Icc, proj_Icc_of_le_left _ hx, subtype.coe_mk, arcsin_neg_one] @[simp] lemma arcsin_neg (x : ℝ) : arcsin (-x) = -arcsin x := begin cases le_total x (-1) with hx₁ hx₁, { rw [arcsin_of_le_neg_one hx₁, neg_neg, arcsin_of_one_le (le_neg.2 hx₁)] }, cases le_total 1 x with hx₂ hx₂, { rw [arcsin_of_one_le hx₂, arcsin_of_le_neg_one (neg_le_neg hx₂)] }, refine arcsin_eq_of_sin_eq _ _, { rw [sin_neg, sin_arcsin hx₁ hx₂] }, { exact ⟨neg_le_neg (arcsin_le_pi_div_two _), neg_le.2 (neg_pi_div_two_le_arcsin _)⟩ } end lemma arcsin_le_iff_le_sin {x y : ℝ} (hx : x ∈ Icc (-1 : ℝ) 1) (hy : y ∈ Icc (-(π / 2)) (π / 2)) : arcsin x ≤ y ↔ x ≤ sin y := by rw [← arcsin_sin' hy, strict_mono_on_arcsin.le_iff_le hx (sin_mem_Icc _), arcsin_sin' hy] lemma arcsin_le_iff_le_sin' {x y : ℝ} (hy : y ∈ Ico (-(π / 2)) (π / 2)) : arcsin x ≤ y ↔ x ≤ sin y := begin cases le_total x (-1) with hx₁ hx₁, { simp [arcsin_of_le_neg_one hx₁, hy.1, hx₁.trans (neg_one_le_sin _)] }, cases lt_or_le 1 x with hx₂ hx₂, { simp [arcsin_of_one_le hx₂.le, hy.2.not_le, (sin_le_one y).trans_lt hx₂] }, exact arcsin_le_iff_le_sin ⟨hx₁, hx₂⟩ (mem_Icc_of_Ico hy) end lemma le_arcsin_iff_sin_le {x y : ℝ} (hx : x ∈ Icc (-(π / 2)) (π / 2)) (hy : y ∈ Icc (-1 : ℝ) 1) : x ≤ arcsin y ↔ sin x ≤ y := by rw [← neg_le_neg_iff, ← arcsin_neg, arcsin_le_iff_le_sin ⟨neg_le_neg hy.2, neg_le.2 hy.1⟩ ⟨neg_le_neg hx.2, neg_le.2 hx.1⟩, sin_neg, neg_le_neg_iff] lemma le_arcsin_iff_sin_le' {x y : ℝ} (hx : x ∈ Ioc (-(π / 2)) (π / 2)) : x ≤ arcsin y ↔ sin x ≤ y := by rw [← neg_le_neg_iff, ← arcsin_neg, arcsin_le_iff_le_sin' ⟨neg_le_neg hx.2, neg_lt.2 hx.1⟩, sin_neg, neg_le_neg_iff] lemma arcsin_lt_iff_lt_sin {x y : ℝ} (hx : x ∈ Icc (-1 : ℝ) 1) (hy : y ∈ Icc (-(π / 2)) (π / 2)) : arcsin x < y ↔ x < sin y := not_le.symm.trans $ (not_congr $ le_arcsin_iff_sin_le hy hx).trans not_le lemma arcsin_lt_iff_lt_sin' {x y : ℝ} (hy : y ∈ Ioc (-(π / 2)) (π / 2)) : arcsin x < y ↔ x < sin y := not_le.symm.trans $ (not_congr $ le_arcsin_iff_sin_le' hy).trans not_le lemma lt_arcsin_iff_sin_lt {x y : ℝ} (hx : x ∈ Icc (-(π / 2)) (π / 2)) (hy : y ∈ Icc (-1 : ℝ) 1) : x < arcsin y ↔ sin x < y := not_le.symm.trans $ (not_congr $ arcsin_le_iff_le_sin hy hx).trans not_le lemma lt_arcsin_iff_sin_lt' {x y : ℝ} (hx : x ∈ Ico (-(π / 2)) (π / 2)) : x < arcsin y ↔ sin x < y := not_le.symm.trans $ (not_congr $ arcsin_le_iff_le_sin' hx).trans not_le lemma arcsin_eq_iff_eq_sin {x y : ℝ} (hy : y ∈ Ioo (-(π / 2)) (π / 2)) : arcsin x = y ↔ x = sin y := by simp only [le_antisymm_iff, arcsin_le_iff_le_sin' (mem_Ico_of_Ioo hy), le_arcsin_iff_sin_le' (mem_Ioc_of_Ioo hy)] @[simp] lemma arcsin_nonneg {x : ℝ} : 0 ≤ arcsin x ↔ 0 ≤ x := (le_arcsin_iff_sin_le' ⟨neg_lt_zero.2 pi_div_two_pos, pi_div_two_pos.le⟩).trans $ by rw [sin_zero] @[simp] lemma arcsin_nonpos {x : ℝ} : arcsin x ≤ 0 ↔ x ≤ 0 := neg_nonneg.symm.trans $ arcsin_neg x ▸ arcsin_nonneg.trans neg_nonneg @[simp] lemma arcsin_eq_zero_iff {x : ℝ} : arcsin x = 0 ↔ x = 0 := by simp [le_antisymm_iff] @[simp] lemma zero_eq_arcsin_iff {x} : 0 = arcsin x ↔ x = 0 := eq_comm.trans arcsin_eq_zero_iff @[simp] lemma arcsin_pos {x : ℝ} : 0 < arcsin x ↔ 0 < x := lt_iff_lt_of_le_iff_le arcsin_nonpos @[simp] lemma arcsin_lt_zero {x : ℝ} : arcsin x < 0 ↔ x < 0 := lt_iff_lt_of_le_iff_le arcsin_nonneg @[simp] lemma arcsin_lt_pi_div_two {x : ℝ} : arcsin x < π / 2 ↔ x < 1 := (arcsin_lt_iff_lt_sin' (right_mem_Ioc.2 $ neg_lt_self pi_div_two_pos)).trans $ by rw sin_pi_div_two @[simp] lemma neg_pi_div_two_lt_arcsin {x : ℝ} : -(π / 2) < arcsin x ↔ -1 < x := (lt_arcsin_iff_sin_lt' $ left_mem_Ico.2 $ neg_lt_self pi_div_two_pos).trans $ by rw [sin_neg, sin_pi_div_two] @[simp] lemma arcsin_eq_pi_div_two {x : ℝ} : arcsin x = π / 2 ↔ 1 ≤ x := ⟨λ h, not_lt.1 $ λ h', (arcsin_lt_pi_div_two.2 h').ne h, arcsin_of_one_le⟩ @[simp] lemma pi_div_two_eq_arcsin {x} : π / 2 = arcsin x ↔ 1 ≤ x := eq_comm.trans arcsin_eq_pi_div_two @[simp] lemma pi_div_two_le_arcsin {x} : π / 2 ≤ arcsin x ↔ 1 ≤ x := (arcsin_le_pi_div_two x).le_iff_eq.trans pi_div_two_eq_arcsin @[simp] lemma arcsin_eq_neg_pi_div_two {x : ℝ} : arcsin x = -(π / 2) ↔ x ≤ -1 := ⟨λ h, not_lt.1 $ λ h', (neg_pi_div_two_lt_arcsin.2 h').ne' h, arcsin_of_le_neg_one⟩ @[simp] lemma neg_pi_div_two_eq_arcsin {x} : -(π / 2) = arcsin x ↔ x ≤ -1 := eq_comm.trans arcsin_eq_neg_pi_div_two @[simp] lemma arcsin_le_neg_pi_div_two {x} : arcsin x ≤ -(π / 2) ↔ x ≤ -1 := (neg_pi_div_two_le_arcsin x).le_iff_eq.trans arcsin_eq_neg_pi_div_two @[simp] lemma pi_div_four_le_arcsin {x} : π / 4 ≤ arcsin x ↔ sqrt 2 / 2 ≤ x := by { rw [← sin_pi_div_four, le_arcsin_iff_sin_le'], have := pi_pos, split; linarith } lemma maps_to_sin_Ioo : maps_to sin (Ioo (-(π / 2)) (π / 2)) (Ioo (-1) 1) := λ x h, by rwa [mem_Ioo, ← arcsin_lt_pi_div_two, ← neg_pi_div_two_lt_arcsin, arcsin_sin h.1.le h.2.le] /-- `real.sin` as a `local_homeomorph` between `(-π / 2, π / 2)` and `(-1, 1)`. -/ @[simp] def sin_local_homeomorph : local_homeomorph ℝ ℝ := { to_fun := sin, inv_fun := arcsin, source := Ioo (-(π / 2)) (π / 2), target := Ioo (-1) 1, map_source' := maps_to_sin_Ioo, map_target' := λ y hy, ⟨neg_pi_div_two_lt_arcsin.2 hy.1, arcsin_lt_pi_div_two.2 hy.2⟩, left_inv' := λ x hx, arcsin_sin hx.1.le hx.2.le, right_inv' := λ y hy, sin_arcsin hy.1.le hy.2.le, open_source := is_open_Ioo, open_target := is_open_Ioo, continuous_to_fun := continuous_sin.continuous_on, continuous_inv_fun := continuous_arcsin.continuous_on } lemma cos_arcsin_nonneg (x : ℝ) : 0 ≤ cos (arcsin x) := cos_nonneg_of_mem_Icc ⟨neg_pi_div_two_le_arcsin _, arcsin_le_pi_div_two _⟩ -- The junk values for `arcsin` and `sqrt` make this true even outside `[-1, 1]`. lemma cos_arcsin (x : ℝ) : cos (arcsin x) = sqrt (1 - x ^ 2) := begin by_cases hx₁ : -1 ≤ x, swap, { rw not_le at hx₁, rw [arcsin_of_le_neg_one hx₁.le, cos_neg, cos_pi_div_two, sqrt_eq_zero_of_nonpos], nlinarith }, by_cases hx₂ : x ≤ 1, swap, { rw not_le at hx₂, rw [arcsin_of_one_le hx₂.le, cos_pi_div_two, sqrt_eq_zero_of_nonpos], nlinarith }, have : sin (arcsin x) ^ 2 + cos (arcsin x) ^ 2 = 1 := sin_sq_add_cos_sq (arcsin x), rw [← eq_sub_iff_add_eq', ← sqrt_inj (sq_nonneg _) (sub_nonneg.2 (sin_sq_le_one (arcsin x))), sq, sqrt_mul_self (cos_arcsin_nonneg _)] at this, rw [this, sin_arcsin hx₁ hx₂], end -- The junk values for `arcsin` and `sqrt` make this true even outside `[-1, 1]`. lemma tan_arcsin (x : ℝ) : tan (arcsin x) = x / sqrt (1 - x ^ 2) := begin rw [tan_eq_sin_div_cos, cos_arcsin], by_cases hx₁ : -1 ≤ x, swap, { have h : sqrt (1 - x ^ 2) = 0, { exact sqrt_eq_zero_of_nonpos (by nlinarith) }, rw h, simp }, by_cases hx₂ : x ≤ 1, swap, { have h : sqrt (1 - x ^ 2) = 0, { exact sqrt_eq_zero_of_nonpos (by nlinarith) }, rw h, simp }, rw sin_arcsin hx₁ hx₂ end /-- Inverse of the `cos` function, returns values in the range `0 ≤ arccos x` and `arccos x ≤ π`. It defaults to `π` on `(-∞, -1)` and to `0` to `(1, ∞)`. -/ @[pp_nodot] noncomputable def arccos (x : ℝ) : ℝ := π / 2 - arcsin x lemma arccos_eq_pi_div_two_sub_arcsin (x : ℝ) : arccos x = π / 2 - arcsin x := rfl lemma arcsin_eq_pi_div_two_sub_arccos (x : ℝ) : arcsin x = π / 2 - arccos x := by simp [arccos] lemma arccos_le_pi (x : ℝ) : arccos x ≤ π := by unfold arccos; linarith [neg_pi_div_two_le_arcsin x] lemma arccos_nonneg (x : ℝ) : 0 ≤ arccos x := by unfold arccos; linarith [arcsin_le_pi_div_two x] @[simp] lemma arccos_pos {x : ℝ} : 0 < arccos x ↔ x < 1 := by simp [arccos] lemma cos_arccos {x : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) : cos (arccos x) = x := by rw [arccos, cos_pi_div_two_sub, sin_arcsin hx₁ hx₂] lemma arccos_cos {x : ℝ} (hx₁ : 0 ≤ x) (hx₂ : x ≤ π) : arccos (cos x) = x := by rw [arccos, ← sin_pi_div_two_sub, arcsin_sin]; simp [sub_eq_add_neg]; linarith lemma strict_anti_on_arccos : strict_anti_on arccos (Icc (-1) 1) := λ x hx y hy h, sub_lt_sub_left (strict_mono_on_arcsin hx hy h) _ lemma arccos_inj_on : inj_on arccos (Icc (-1) 1) := strict_anti_on_arccos.inj_on lemma arccos_inj {x y : ℝ} (hx₁ : -1 ≤ x) (hx₂ : x ≤ 1) (hy₁ : -1 ≤ y) (hy₂ : y ≤ 1) : arccos x = arccos y ↔ x = y := arccos_inj_on.eq_iff ⟨hx₁, hx₂⟩ ⟨hy₁, hy₂⟩ @[simp] lemma arccos_zero : arccos 0 = π / 2 := by simp [arccos] @[simp] lemma arccos_one : arccos 1 = 0 := by simp [arccos] @[simp] lemma arccos_neg_one : arccos (-1) = π := by simp [arccos, add_halves] @[simp] lemma arccos_eq_zero {x} : arccos x = 0 ↔ 1 ≤ x := by simp [arccos, sub_eq_zero] @[simp] lemma arccos_eq_pi_div_two {x} : arccos x = π / 2 ↔ x = 0 := by simp [arccos] @[simp] lemma arccos_eq_pi {x} : arccos x = π ↔ x ≤ -1 := by rw [arccos, sub_eq_iff_eq_add, ← sub_eq_iff_eq_add', div_two_sub_self, neg_pi_div_two_eq_arcsin] lemma arccos_neg (x : ℝ) : arccos (-x) = π - arccos x := by rw [← add_halves π, arccos, arcsin_neg, arccos, add_sub_assoc, sub_sub_self, sub_neg_eq_add] lemma arccos_of_one_le {x : ℝ} (hx : 1 ≤ x) : arccos x = 0 := by rw [arccos, arcsin_of_one_le hx, sub_self] lemma arccos_of_le_neg_one {x : ℝ} (hx : x ≤ -1) : arccos x = π := by rw [arccos, arcsin_of_le_neg_one hx, sub_neg_eq_add, add_halves'] -- The junk values for `arccos` and `sqrt` make this true even outside `[-1, 1]`. lemma sin_arccos (x : ℝ) : sin (arccos x) = sqrt (1 - x ^ 2) := begin by_cases hx₁ : -1 ≤ x, swap, { rw not_le at hx₁, rw [arccos_of_le_neg_one hx₁.le, sin_pi, sqrt_eq_zero_of_nonpos], nlinarith }, by_cases hx₂ : x ≤ 1, swap, { rw not_le at hx₂, rw [arccos_of_one_le hx₂.le, sin_zero, sqrt_eq_zero_of_nonpos], nlinarith }, rw [arccos_eq_pi_div_two_sub_arcsin, sin_pi_div_two_sub, cos_arcsin] end @[simp] lemma arccos_le_pi_div_two {x} : arccos x ≤ π / 2 ↔ 0 ≤ x := by simp [arccos] @[simp] lemma arccos_lt_pi_div_two {x : ℝ} : arccos x < π / 2 ↔ 0 < x := by simp [arccos] @[simp] lemma arccos_le_pi_div_four {x} : arccos x ≤ π / 4 ↔ sqrt 2 / 2 ≤ x := by { rw [arccos, ← pi_div_four_le_arcsin], split; { intro, linarith } } @[continuity] lemma continuous_arccos : continuous arccos := continuous_const.sub continuous_arcsin -- The junk values for `arccos` and `sqrt` make this true even outside `[-1, 1]`. lemma tan_arccos (x : ℝ) : tan (arccos x) = sqrt (1 - x ^ 2) / x := by rw [arccos, tan_pi_div_two_sub, tan_arcsin, inv_div] -- The junk values for `arccos` and `sqrt` make this true even for `1 < x`. lemma arccos_eq_arcsin {x : ℝ} (h : 0 ≤ x) : arccos x = arcsin (sqrt (1 - x ^ 2)) := (arcsin_eq_of_sin_eq (sin_arccos _) ⟨(left.neg_nonpos_iff.2 (div_nonneg pi_pos.le (by norm_num))).trans (arccos_nonneg _), arccos_le_pi_div_two.2 h⟩).symm -- The junk values for `arcsin` and `sqrt` make this true even for `1 < x`. lemma arcsin_eq_arccos {x : ℝ} (h : 0 ≤ x) : arcsin x = arccos (sqrt (1 - x ^ 2)) := begin rw [eq_comm, ← cos_arcsin], exact arccos_cos (arcsin_nonneg.2 h) ((arcsin_le_pi_div_two _).trans (div_le_self pi_pos.le one_le_two)) end end real
ce33c69761c3e8b32789c8f66fb7dada91a7fb21
9dc8cecdf3c4634764a18254e94d43da07142918
/src/data/finset/card.lean
fb8634fecac807aa2bbe10de14be50d1e186f420
[ "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
24,034
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jeremy Avigad -/ import data.finset.basic import tactic.by_contra /-! # Cardinality of a finite set This defines the cardinality of a `finset` and provides induction principles for finsets. ## Main declarations * `finset.card`: `s.card : ℕ` returns the cardinality of `s : finset α`. ### Induction principles * `finset.strong_induction`: Strong induction * `finset.strong_induction_on` * `finset.strong_downward_induction` * `finset.strong_downward_induction_on` * `finset.case_strong_induction_on` ## TODO Should we add a noncomputable version? -/ open function multiset nat variables {α β : Type*} namespace finset variables {s t : finset α} {a b : α} /-- `s.card` is the number of elements of `s`, aka its cardinality. -/ def card (s : finset α) : ℕ := s.1.card lemma card_def (s : finset α) : s.card = s.1.card := rfl @[simp] lemma card_mk {m nodup} : (⟨m, nodup⟩ : finset α).card = m.card := rfl @[simp] lemma card_empty : card (∅ : finset α) = 0 := rfl lemma card_le_of_subset : s ⊆ t → s.card ≤ t.card := multiset.card_le_of_le ∘ val_le_iff.mpr @[mono] lemma card_mono : monotone (@card α) := by apply card_le_of_subset @[simp] lemma card_eq_zero : s.card = 0 ↔ s = ∅ := card_eq_zero.trans val_eq_zero lemma card_pos : 0 < s.card ↔ s.nonempty := pos_iff_ne_zero.trans $ (not_congr card_eq_zero).trans nonempty_iff_ne_empty.symm alias card_pos ↔ _ nonempty.card_pos lemma card_ne_zero_of_mem (h : a ∈ s) : s.card ≠ 0 := (not_congr card_eq_zero).2 $ ne_empty_of_mem h @[simp] lemma card_singleton (a : α) : card ({a} : finset α) = 1 := card_singleton _ lemma card_singleton_inter [decidable_eq α] : ({a} ∩ s).card ≤ 1 := begin cases (finset.decidable_mem a s), { simp [finset.singleton_inter_of_not_mem h] }, { simp [finset.singleton_inter_of_mem h] } end @[simp] lemma card_cons (h : a ∉ s) : (s.cons a h).card = s.card + 1 := card_cons _ _ section insert_erase variables [decidable_eq α] @[simp] lemma card_insert_of_not_mem (h : a ∉ s) : (insert a s).card = s.card + 1 := by rw [←cons_eq_insert _ _ h, card_cons] lemma card_insert_of_mem (h : a ∈ s) : card (insert a s) = s.card := by rw insert_eq_of_mem h lemma card_insert_le (a : α) (s : finset α) : card (insert a s) ≤ s.card + 1 := by by_cases a ∈ s; [{rw insert_eq_of_mem h, exact nat.le_succ _ }, rw card_insert_of_not_mem h] /-- If `a ∈ s` is known, see also `finset.card_insert_of_mem` and `finset.card_insert_of_not_mem`. -/ lemma card_insert_eq_ite : card (insert a s) = if a ∈ s then s.card else s.card + 1 := begin by_cases h : a ∈ s, { rw [card_insert_of_mem h, if_pos h] }, { rw [card_insert_of_not_mem h, if_neg h] } end @[simp] lemma card_doubleton (h : a ≠ b) : ({a, b} : finset α).card = 2 := by rw [card_insert_of_not_mem (not_mem_singleton.2 h), card_singleton] @[simp] lemma card_erase_of_mem : a ∈ s → (s.erase a).card = s.card - 1 := card_erase_of_mem @[simp] lemma card_erase_add_one : a ∈ s → (s.erase a).card + 1 = s.card := card_erase_add_one lemma card_erase_lt_of_mem : a ∈ s → (s.erase a).card < s.card := card_erase_lt_of_mem lemma card_erase_le : (s.erase a).card ≤ s.card := card_erase_le lemma pred_card_le_card_erase : s.card - 1 ≤ (s.erase a).card := begin by_cases h : a ∈ s, { exact (card_erase_of_mem h).ge }, { rw erase_eq_of_not_mem h, exact nat.sub_le _ _ } end /-- If `a ∈ s` is known, see also `finset.card_erase_of_mem` and `finset.erase_eq_of_not_mem`. -/ lemma card_erase_eq_ite : (s.erase a).card = if a ∈ s then s.card - 1 else s.card := card_erase_eq_ite end insert_erase @[simp] lemma card_range (n : ℕ) : (range n).card = n := card_range n @[simp] lemma card_attach : s.attach.card = s.card := multiset.card_attach end finset section to_list_multiset variables [decidable_eq α] (m : multiset α) (l : list α) lemma multiset.card_to_finset : m.to_finset.card = m.dedup.card := rfl lemma multiset.to_finset_card_le : m.to_finset.card ≤ m.card := card_le_of_le $ dedup_le _ lemma multiset.to_finset_card_of_nodup {m : multiset α} (h : m.nodup) : m.to_finset.card = m.card := congr_arg card $ multiset.dedup_eq_self.mpr h lemma list.card_to_finset : l.to_finset.card = l.dedup.length := rfl lemma list.to_finset_card_le : l.to_finset.card ≤ l.length := multiset.to_finset_card_le ⟦l⟧ lemma list.to_finset_card_of_nodup {l : list α} (h : l.nodup) : l.to_finset.card = l.length := multiset.to_finset_card_of_nodup h end to_list_multiset namespace finset variables {s t : finset α} {f : α → β} {n : ℕ} @[simp] lemma length_to_list (s : finset α) : s.to_list.length = s.card := by { rw [to_list, ←multiset.coe_card, multiset.coe_to_list], refl } lemma card_image_le [decidable_eq β] : (s.image f).card ≤ s.card := by simpa only [card_map] using (s.1.map f).to_finset_card_le lemma card_image_of_inj_on [decidable_eq β] (H : set.inj_on f s) : (s.image f).card = s.card := by simp only [card, image_val_of_inj_on H, card_map] lemma inj_on_of_card_image_eq [decidable_eq β] (H : (s.image f).card = s.card) : set.inj_on f s := begin change (s.1.map f).dedup.card = s.1.card at H, have : (s.1.map f).dedup = s.1.map f, { refine multiset.eq_of_le_of_card_le (multiset.dedup_le _) _, rw H, simp only [multiset.card_map] }, rw multiset.dedup_eq_self at this, exact inj_on_of_nodup_map this, end lemma card_image_iff [decidable_eq β] : (s.image f).card = s.card ↔ set.inj_on f s := ⟨inj_on_of_card_image_eq, card_image_of_inj_on⟩ lemma card_image_of_injective [decidable_eq β] (s : finset α) (H : injective f) : (s.image f).card = s.card := card_image_of_inj_on $ λ x _ y _ h, H h lemma fiber_card_ne_zero_iff_mem_image (s : finset α) (f : α → β) [decidable_eq β] (y : β) : (s.filter (λ x, f x = y)).card ≠ 0 ↔ y ∈ s.image f := by { rw [←pos_iff_ne_zero, card_pos, fiber_nonempty_iff_mem_image] } @[simp] lemma card_map (f : α ↪ β) : (s.map f).card = s.card := multiset.card_map _ _ @[simp] lemma card_subtype (p : α → Prop) [decidable_pred p] (s : finset α) : (s.subtype p).card = (s.filter p).card := by simp [finset.subtype] lemma card_filter_le (s : finset α) (p : α → Prop) [decidable_pred p] : (s.filter p).card ≤ s.card := card_le_of_subset $ filter_subset _ _ lemma eq_of_subset_of_card_le {s t : finset α} (h : s ⊆ t) (h₂ : t.card ≤ s.card) : s = t := eq_of_veq $ multiset.eq_of_le_of_card_le (val_le_iff.mpr h) h₂ lemma map_eq_of_subset {f : α ↪ α} (hs : s.map f ⊆ s) : s.map f = s := eq_of_subset_of_card_le hs (card_map _).ge lemma filter_card_eq {p : α → Prop} [decidable_pred p] (h : (s.filter p).card = s.card) (x : α) (hx : x ∈ s) : p x := begin rw [←eq_of_subset_of_card_le (s.filter_subset p) h.ge, mem_filter] at hx, exact hx.2, end lemma card_lt_card (h : s ⊂ t) : s.card < t.card := card_lt_of_lt $ val_lt_iff.2 h lemma card_eq_of_bijective (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) : s.card = n := begin classical, have : ∀ (a : α), a ∈ s ↔ ∃ i (hi : i ∈ range n), f i (mem_range.1 hi) = a, from λ a, ⟨λ ha, let ⟨i, hi, eq⟩ := hf a ha in ⟨i, mem_range.2 hi, eq⟩, λ ⟨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_iff, mem_image, exists_prop, subtype.exists, mem_attach, true_and], calc s.card = 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 _ $ λ ⟨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_congr {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 classical; 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 $ λ 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_le_card_of_inj_on {t : finset β} (f : α → β) (hf : ∀ a ∈ s, f a ∈ t) (f_inj : ∀ a₁ ∈ s, ∀ a₂ ∈ s, f a₁ = f a₂ → a₁ = a₂) : s.card ≤ t.card := by classical; calc s.card = (s.image f).card : (card_image_of_inj_on f_inj).symm ... ≤ t.card : card_le_of_subset $ image_subset_iff.2 hf /-- If there are more pigeons than pigeonholes, then there are two pigeons in the same pigeonhole. -/ lemma exists_ne_map_eq_of_card_lt_of_maps_to {t : finset β} (hc : t.card < s.card) {f : α → β} (hf : ∀ a ∈ s, f a ∈ t) : ∃ (x ∈ s) (y ∈ s), x ≠ y ∧ f x = f y := begin classical, by_contra' hz, refine hc.not_le (card_le_card_of_inj_on f hf _), intros x hx y hy, contrapose, exact hz x hx y hy, end lemma le_card_of_inj_on_range (f : ℕ → α) (hf : ∀ i < n, f i ∈ s) (f_inj : ∀ (i < n) (j < n), f i = f j → i = j) : n ≤ s.card := calc n = card (range n) : (card_range n).symm ... ≤ s.card : card_le_card_of_inj_on f (by simpa only [mem_range]) (by simpa only [mem_range]) lemma surj_on_of_inj_on_of_card_le {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 : t.card ≤ s.card) : ∀ b ∈ t, ∃ a ha, b = f a ha := begin classical, intros b hb, have h : (s.attach.image $ λ (a : {a // a ∈ s}), f a a.prop).card = s.card, { exact @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 a.prop) s.attach = t, { exact eq_of_subset_of_card_le (λ b h, let ⟨a, ha₁, ha₂⟩ := mem_image.1 h in ha₂ ▸ hf _ _) (by simp [hst, h]) }, rw ←h' at hb, obtain ⟨a, ha₁, ha₂⟩ := mem_image.1 hb, exact ⟨a, a.2, ha₂.symm⟩, end lemma inj_on_of_surj_on_of_card_le {t : finset β} (f : Π a ∈ s, β) (hf : ∀ a ha, f a ha ∈ t) (hsurj : ∀ b ∈ t, ∃ a ha, b = f a ha) (hst : s.card ≤ t.card) ⦃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 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_iff_val.1 (@hif ⟨a₁, ha₁⟩ ⟨a₂, ha₂⟩ (subtype.eq ha₁a₂)) @[simp] lemma card_disj_union (s t : finset α) (h) : (s.disj_union t h).card = s.card + t.card := multiset.card_add _ _ /-! ### Lattice structure -/ section lattice variables [decidable_eq α] lemma card_union_add_card_inter (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 (s t : finset α) : (s ∪ t).card ≤ s.card + t.card := card_union_add_card_inter s t ▸ nat.le_add_right _ _ lemma card_union_eq (h : disjoint s t) : (s ∪ t).card = s.card + t.card := by rw [←disj_union_eq_union s t $ disjoint_left.mp h, card_disj_union _ _ _] @[simp] lemma card_disjoint_union (h : disjoint s t) : card (s ∪ t) = s.card + t.card := card_union_eq h lemma card_sdiff (h : s ⊆ t) : card (t \ s) = t.card - s.card := suffices card (t \ s) = card ((t \ s) ∪ s) - s.card, by rwa sdiff_union_of_subset h at this, by rw [card_disjoint_union sdiff_disjoint, add_tsub_cancel_right] lemma card_sdiff_add_card_eq_card {s t : finset α} (h : s ⊆ t) : card (t \ s) + card s = card t := ((nat.sub_eq_iff_eq_add (card_le_of_subset h)).mp (card_sdiff h).symm).symm lemma le_card_sdiff (s t : finset α) : t.card - s.card ≤ card (t \ s) := calc card t - card s ≤ card t - card (s ∩ t) : tsub_le_tsub_left (card_le_of_subset (inter_subset_left s t)) _ ... = card (t \ (s ∩ t)) : (card_sdiff (inter_subset_right s t)).symm ... ≤ card (t \ s) : by rw sdiff_inter_self_right t s lemma card_sdiff_add_card : (s \ t).card + t.card = (s ∪ t).card := by rw [←card_disjoint_union sdiff_disjoint, sdiff_union_self_eq_union] end lattice lemma filter_card_add_filter_neg_card_eq_card (p : α → Prop) [decidable_pred p] : (s.filter p).card + (s.filter (not ∘ p)).card = s.card := by { classical, simp [←card_union_eq, filter_union_filter_neg_eq, disjoint_filter] } /-- 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, add_tsub_cancel_right, 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 (mem_sdiff.1 ha).1, ←h], refl }, rcases ih _ z with ⟨B', hB', B'subA', cards⟩, { exact ⟨B', hB', trans B'subA' (erase_subset _ _), cards⟩ }, { rintro 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₂⟩ lemma exists_subset_or_subset_of_two_mul_lt_card [decidable_eq α] {X Y : finset α} {n : ℕ} (hXY : 2 * n < (X ∪ Y).card) : ∃ C : finset α, n < C.card ∧ (C ⊆ X ∨ C ⊆ Y) := begin have h₁ : (X ∩ (Y \ X)).card = 0 := finset.card_eq_zero.mpr (finset.inter_sdiff_self X Y), have h₂ : (X ∪ Y).card = X.card + (Y \ X).card, { rw [←card_union_add_card_inter X (Y \ X), finset.union_sdiff_self_eq_union, h₁, add_zero] }, rw [h₂, two_mul] at hXY, rcases lt_or_lt_of_add_lt_add hXY with h|h, { exact ⟨X, h, or.inl (finset.subset.refl X)⟩ }, { exact ⟨Y \ X, h, or.inr (finset.sdiff_subset Y X)⟩ } end /-! ### Explicit description of a finset from its card -/ lemma card_eq_one : s.card = 1 ↔ ∃ a, s = {a} := by cases s; simp only [multiset.card_eq_one, finset.card, ←val_inj, singleton_val] lemma exists_eq_insert_iff [decidable_eq α] {s t : finset α} : (∃ a ∉ s, insert a s = t) ↔ s ⊆ t ∧ s.card + 1 = t.card := begin split, { rintro ⟨a, ha, rfl⟩, exact ⟨subset_insert _ _, (card_insert_of_not_mem ha).symm⟩ }, { rintro ⟨hst, h⟩, obtain ⟨a, ha⟩ : ∃ a, t \ s = {a}, { exact card_eq_one.1 (by rw [card_sdiff hst, ←h, add_tsub_cancel_left]) }, refine ⟨a, λ hs, (_ : a ∉ {a}) $ mem_singleton_self _, by rw [insert_eq, ←ha, sdiff_union_of_subset hst]⟩, rw ←ha, exact not_mem_sdiff_of_mem_right hs } end lemma card_le_one : s.card ≤ 1 ↔ ∀ (a ∈ s) (b ∈ s), a = b := begin obtain rfl | ⟨x, hx⟩ := s.eq_empty_or_nonempty, { simp }, refine (nat.succ_le_of_lt (card_pos.2 ⟨x, hx⟩)).le_iff_eq.trans (card_eq_one.trans ⟨_, _⟩), { rintro ⟨y, rfl⟩, simp }, { exact λ h, ⟨x, eq_singleton_iff_unique_mem.2 ⟨hx, λ y hy, h _ hy _ hx⟩⟩ } end lemma card_le_one_iff : s.card ≤ 1 ↔ ∀ {a b}, a ∈ s → b ∈ s → a = b := by { rw card_le_one, tauto } lemma card_le_one_iff_subset_singleton [nonempty α] : s.card ≤ 1 ↔ ∃ (x : α), s ⊆ {x} := begin refine ⟨λ H, _, _⟩, { obtain rfl | ⟨x, hx⟩ := s.eq_empty_or_nonempty, { exact ⟨classical.arbitrary α, empty_subset _⟩ }, { exact ⟨x, λ y hy, by rw [card_le_one.1 H y hy x hx, mem_singleton]⟩ } }, { rintro ⟨x, hx⟩, rw ←card_singleton x, exact card_le_of_subset hx } end /-- A `finset` of a subsingleton type has cardinality at most one. -/ lemma card_le_one_of_subsingleton [subsingleton α] (s : finset α) : s.card ≤ 1 := finset.card_le_one_iff.2 $ λ _ _ _ _, subsingleton.elim _ _ lemma one_lt_card : 1 < s.card ↔ ∃ (a ∈ s) (b ∈ s), a ≠ b := by { rw ←not_iff_not, push_neg, exact card_le_one } lemma one_lt_card_iff : 1 < s.card ↔ ∃ a b, a ∈ s ∧ b ∈ s ∧ a ≠ b := by { rw one_lt_card, simp only [exists_prop, exists_and_distrib_left] } lemma two_lt_card_iff : 2 < s.card ↔ ∃ a b c, a ∈ s ∧ b ∈ s ∧ c ∈ s ∧ a ≠ b ∧ a ≠ c ∧ b ≠ c := begin classical, refine ⟨λ h, _, _⟩, { obtain ⟨c, hc⟩ := card_pos.mp (zero_lt_two.trans h), have : 1 < (s.erase c).card := by rwa [←add_lt_add_iff_right 1, card_erase_add_one hc], obtain ⟨a, b, ha, hb, hab⟩ := one_lt_card_iff.mp this, exact ⟨a, b, c, mem_of_mem_erase ha, mem_of_mem_erase hb, hc, hab, ne_of_mem_erase ha, ne_of_mem_erase hb⟩ }, { rintros ⟨a, b, c, ha, hb, hc, hab, hac, hbc⟩, rw [←card_erase_add_one hc, ←card_erase_add_one (mem_erase_of_ne_of_mem hbc hb), ←card_erase_add_one (mem_erase_of_ne_of_mem hab (mem_erase_of_ne_of_mem hac ha))], apply nat.le_add_left }, end lemma two_lt_card : 2 < s.card ↔ ∃ (a ∈ s) (b ∈ s) (c ∈ s), a ≠ b ∧ a ≠ c ∧ b ≠ c := by simp_rw [two_lt_card_iff, exists_prop, exists_and_distrib_left] lemma exists_ne_of_one_lt_card (hs : 1 < s.card) (a : α) : ∃ b, b ∈ s ∧ b ≠ a := begin obtain ⟨x, hx, y, hy, hxy⟩ := finset.one_lt_card.mp hs, by_cases ha : y = a, { exact ⟨x, hx, ne_of_ne_of_eq hxy ha⟩ }, { exact ⟨y, hy, ha⟩ } end lemma card_eq_succ [decidable_eq α] : s.card = n + 1 ↔ ∃ a t, a ∉ t ∧ insert a t = s ∧ t.card = n := ⟨λ h, let ⟨a, has⟩ := card_pos.mp (h.symm ▸ nat.zero_lt_succ _ : 0 < s.card) in ⟨a, s.erase a, s.not_mem_erase a, insert_erase has, by simp only [h, card_erase_of_mem has, add_tsub_cancel_right]⟩, λ ⟨a, t, hat, s_eq, n_eq⟩, s_eq ▸ n_eq ▸ card_insert_of_not_mem hat⟩ lemma card_eq_two [decidable_eq α] : s.card = 2 ↔ ∃ x y, x ≠ y ∧ s = {x, y} := begin split, { rw card_eq_succ, simp_rw [card_eq_one], rintro ⟨a, _, hab, rfl, b, rfl⟩, exact ⟨a, b, not_mem_singleton.1 hab, rfl⟩ }, { rintro ⟨x, y, h, rfl⟩, exact card_doubleton h } end lemma card_eq_three [decidable_eq α] : s.card = 3 ↔ ∃ x y z, x ≠ y ∧ x ≠ z ∧ y ≠ z ∧ s = {x, y, z} := begin split, { rw card_eq_succ, simp_rw [card_eq_two], rintro ⟨a, _, abc, rfl, b, c, bc, rfl⟩, rw [mem_insert, mem_singleton, not_or_distrib] at abc, exact ⟨a, b, c, abc.1, abc.2, bc, rfl⟩ }, { rintro ⟨x, y, z, xy, xz, yz, rfl⟩, simp only [xy, xz, yz, mem_insert, card_insert_of_not_mem, not_false_iff, mem_singleton, or_self, card_singleton] } end /-! ### Inductions -/ /-- 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. -/ def strong_induction {p : finset α → Sort*} (H : ∀ s, (∀ t ⊂ s, p t) → p s) : ∀ (s : finset α), p s | s := H s (λ t h, have t.card < s.card, from card_lt_card h, strong_induction t) using_well_founded {rel_tac := λ _ _, `[exact ⟨_, measure_wf card⟩]} lemma strong_induction_eq {p : finset α → Sort*} (H : ∀ s, (∀ t ⊂ s, p t) → p s) (s : finset α) : strong_induction H s = H s (λ t h, strong_induction H t) := by rw strong_induction /-- Analogue of `strong_induction` with order of arguments swapped. -/ @[elab_as_eliminator] def strong_induction_on {p : finset α → Sort*} (s : finset α) : (∀ s, (∀ t ⊂ s, p t) → p s) → p s := λ H, strong_induction H s lemma strong_induction_on_eq {p : finset α → Sort*} (s : finset α) (H : ∀ s, (∀ t ⊂ s, p t) → p s) : s.strong_induction_on H = H s (λ t h, t.strong_induction_on H) := by { dunfold strong_induction_on, rw strong_induction } @[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 < _) /-- Suppose that, given that `p t` can be defined on all supersets of `s` of cardinality less than `n`, one knows how to define `p s`. Then one can inductively define `p s` for all finsets `s` of cardinality less than `n`, starting from finsets of card `n` and iterating. This can be used either to define data, or to prove properties. -/ def strong_downward_induction {p : finset α → Sort*} {n : ℕ} (H : ∀ t₁, (∀ {t₂ : finset α}, t₂.card ≤ n → t₁ ⊂ t₂ → p t₂) → t₁.card ≤ n → p t₁) : ∀ (s : finset α), s.card ≤ n → p s | s := H s (λ t ht h, have n - t.card < n - s.card, from (tsub_lt_tsub_iff_left_of_le ht).2 (finset.card_lt_card h), strong_downward_induction t ht) using_well_founded {rel_tac := λ _ _, `[exact ⟨_, measure_wf (λ (t : finset α), n - t.card)⟩]} lemma strong_downward_induction_eq {p : finset α → Sort*} (H : ∀ t₁, (∀ {t₂ : finset α}, t₂.card ≤ n → t₁ ⊂ t₂ → p t₂) → t₁.card ≤ n → p t₁) (s : finset α) : strong_downward_induction H s = H s (λ t ht hst, strong_downward_induction H t ht) := by rw strong_downward_induction /-- Analogue of `strong_downward_induction` with order of arguments swapped. -/ @[elab_as_eliminator] def strong_downward_induction_on {p : finset α → Sort*} (s : finset α) (H : ∀ t₁, (∀ {t₂ : finset α}, t₂.card ≤ n → t₁ ⊂ t₂ → p t₂) → t₁.card ≤ n → p t₁) : s.card ≤ n → p s := strong_downward_induction H s lemma strong_downward_induction_on_eq {p : finset α → Sort*} (s : finset α) (H : ∀ t₁, (∀ {t₂ : finset α}, t₂.card ≤ n → t₁ ⊂ t₂ → p t₂) → t₁.card ≤ n → p t₁) : s.strong_downward_induction_on H = H s (λ t ht h, t.strong_downward_induction_on H ht) := by { dunfold strong_downward_induction_on, rw strong_downward_induction } lemma 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 end finset
ababd6463add2ca66eec7d46d369b10a595e6201
0851884047bb567d19e188e8f1ad959c5ae9c5ce
/src/M3P14/sheet2.lean
abba64219857cad183827c3f3f1801897b67bd77
[ "Apache-2.0" ]
permissive
yz5216/xena-UROP-2018
00d3f71a831324966d40d302544ed2cbbec3fd0f
5e9da9dc64dc51897677f8b73ab9f94061a8d977
refs/heads/master
1,584,922,436,989
1,531,487,250,000
1,531,487,250,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,121
lean
import analysis.real -- Questions: -- Compute 210/449 and 605/617 using quadratic reciprocity. -- (449 and 617 are both prime). -- TODO: how to prove it using quadratic reciprocity? theorem q1 : ∃ x y : ℝ, -- Find all 6 primitive roots modulo 19. theorem q2a : -- Show that if n is odd and a is a primitive root mod n, then a is aprimitive root mod 2n if a is odd, and a + n is a primitive root mod 2n if a is even. -- [HINT: Φ(2n) = Φ(n) when n is odd.] theorem q2b : -- Let p be a prime and let a be a primitive root mod p. -- Show that a is also a primitive root mod p² if, and only if, a^p−1 is not congruent to 1 mod p². -- [HINT: what is the order of a mod p? What does this say about the order of a mod p²?] theorem q3 : -- Let p be a prime, and let a be an integer not divisible by p. -- Show that the equation x^d ≡ a (mod p) has a solution if, and only if, a^(p−1/(d,p−1)) ≡ 1 (mod p). -- Show further that if this is the case then this equation has (d, p − 1) solutions mod p. -- [HINT: what happens when you fix a primitive root g mod p, and take the discrete log of the equation x^d ≡ a (mod p)?] theorem q4 : -- Let p be an odd prime different from 7. -- Show that 7 is a square mod p if, and only if, p is congruent to 1, 3, 9, 19, 25 or 27 modulo 28. -- [HINT: use quadratic reciprocity to relate 7/p to p/7.] theorem q5 : -- Let n and m be relatively prime. Show that every element of (ℤ/nmℤ)^x has order dividing the least common multiple of Φ(n) and Φ(m). theorem q6a : -- Show that if n and m are relatively prime, then ℤ/nmℤ has a primitive root if, and only if, both ℤ/nℤ and ℤ/mℤ have primitive roots, and (Φ(n), Φ(m)) = 1. -- When can this happen? theorem q6b : -- Suppose a is a primitive root modulo n. Show that a^d is also a primitive root modulo n for all d such that (d, Φ(n)) = 1. -- [Hint: show that there exists k such that (a^d)^k is equal to a.] theorem q7 : -- Show that if p is a prime congruent to ±1 mod 24 then none of 2, 3, 4, 6is a primitive root modulo p. -- [Hint: show that 2 and 3 are squares mod p.] theorem q8 :
a166bcab431bb3f8d9c601b134fa3feeef334159
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/free.lean
943813c801394d5a7aece1a11925f77fd4364c45
[ "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
23,950
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 algebra.hom.group import algebra.hom.equiv.basic import control.applicative import control.traversable.basic import logic.equiv.defs import data.list.basic /-! # Free constructions > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. ## Main definitions * `free_magma α`: free magma (structure with binary operation without any axioms) over alphabet `α`, defined inductively, with traversable instance and decidable equality. * `magma.assoc_quotient α`: quotient of a magma `α` by the associativity equivalence relation. * `free_semigroup α`: free semigroup over alphabet `α`, defined as a structure with two fields `head : α` and `tail : list α` (i.e. nonempty lists), with traversable instance and decidable equality. * `free_magma_assoc_quotient_equiv α`: isomorphism between `magma.assoc_quotient (free_magma α)` and `free_semigroup α`. * `free_magma.lift`: the universal property of the free magma, expressing its adjointness. -/ universes u v l /-- Free magma over a given alphabet. -/ @[derive decidable_eq] inductive free_magma (α : Type u) : Type u | of : α → free_magma | mul : free_magma → free_magma → free_magma /-- Free nonabelian additive magma over a given alphabet. -/ @[derive decidable_eq] inductive free_add_magma (α : Type u) : Type u | of : α → free_add_magma | add : free_add_magma → free_add_magma → free_add_magma attribute [to_additive] free_magma namespace free_magma variables {α : Type u} @[to_additive] instance [inhabited α] : inhabited (free_magma α) := ⟨of default⟩ @[to_additive] instance : has_mul (free_magma α) := ⟨free_magma.mul⟩ attribute [pattern] has_mul.mul @[simp, to_additive] theorem mul_eq (x y : free_magma α) : mul x y = x * y := rfl /-- Recursor for `free_magma` using `x * y` instead of `free_magma.mul x y`. -/ @[elab_as_eliminator, to_additive "Recursor for `free_add_magma` using `x + y` instead of `free_add_magma.add x y`."] def rec_on_mul {C : free_magma α → Sort l} (x) (ih1 : ∀ x, C (of x)) (ih2 : ∀ x y, C x → C y → C (x * y)) : C x := free_magma.rec_on x ih1 ih2 @[ext, to_additive] lemma hom_ext {β : Type v} [has_mul β] {f g : free_magma α →ₙ* β} (h : f ∘ of = g ∘ of) : f = g := fun_like.ext _ _ $ λ x, rec_on_mul x (congr_fun h) $ by { intros, simp only [map_mul, *] } end free_magma /-- Lifts a function `α → β` to a magma homomorphism `free_magma α → β` given a magma `β`. -/ def free_magma.lift_aux {α : Type u} {β : Type v} [has_mul β] (f : α → β) : free_magma α → β | (free_magma.of x) := f x | (x * y) := x.lift_aux * y.lift_aux /-- Lifts a function `α → β` to an additive magma homomorphism `free_add_magma α → β` given an additive magma `β`. -/ def free_add_magma.lift_aux {α : Type u} {β : Type v} [has_add β] (f : α → β) : free_add_magma α → β | (free_add_magma.of x) := f x | (x + y) := x.lift_aux + y.lift_aux attribute [to_additive free_add_magma.lift_aux] free_magma.lift_aux namespace free_magma section lift variables {α : Type u} {β : Type v} [has_mul β] (f : α → β) /-- The universal property of the free magma expressing its adjointness. -/ @[to_additive "The universal property of the free additive magma expressing its adjointness.", simps symm_apply] def lift : (α → β) ≃ (free_magma α →ₙ* β) := { to_fun := λ f, { to_fun := lift_aux f, map_mul' := λ x y, rfl, }, inv_fun := λ F, F ∘ of, left_inv := λ f, by { ext, refl }, right_inv := λ F, by { ext, refl } } @[simp, to_additive] lemma lift_of (x) : lift f (of x) = f x := rfl @[simp, to_additive] lemma lift_comp_of : lift f ∘ of = f := rfl @[simp, to_additive] lemma lift_comp_of' (f : free_magma α →ₙ* β) : lift (f ∘ of) = f := lift.apply_symm_apply f end lift section map variables {α : Type u} {β : Type v} (f : α → β) /-- The unique magma homomorphism `free_magma α →ₙ* free_magma β` that sends each `of x` to `of (f x)`. -/ @[to_additive "The unique additive magma homomorphism `free_add_magma α → free_add_magma β` that sends each `of x` to `of (f x)`."] def map (f : α → β) : free_magma α →ₙ* free_magma β := lift (of ∘ f) @[simp, to_additive] lemma map_of (x) : map f (of x) = of (f x) := rfl end map section category variables {α β : Type u} @[to_additive] instance : monad free_magma := { pure := λ _, of, bind := λ _ _ x f, lift f x } /-- Recursor on `free_magma` using `pure` instead of `of`. -/ @[elab_as_eliminator, to_additive "Recursor on `free_add_magma` using `pure` instead of `of`."] protected def rec_on_pure {C : free_magma α → Sort l} (x) (ih1 : ∀ x, C (pure x)) (ih2 : ∀ x y, C x → C y → C (x * y)) : C x := free_magma.rec_on_mul x ih1 ih2 @[simp, to_additive] lemma map_pure (f : α → β) (x) : (f <$> pure x : free_magma β) = pure (f x) := rfl @[simp, to_additive] lemma map_mul' (f : α → β) (x y : free_magma α) : (f <$> (x * y)) = (f <$> x * f <$> y) := rfl @[simp, to_additive] lemma pure_bind (f : α → free_magma β) (x) : (pure x >>= f) = f x := rfl @[simp, to_additive] lemma mul_bind (f : α → free_magma β) (x y : free_magma α) : (x * y >>= f) = ((x >>= f) * (y >>= f)) := rfl @[simp, to_additive] lemma pure_seq {α β : Type u} {f : α → β} {x : free_magma α} : pure f <*> x = f <$> x := rfl @[simp, to_additive] lemma mul_seq {α β : Type u} {f g : free_magma (α → β)} {x : free_magma α} : (f * g) <*> x = (f <*> x) * (g <*> x) := rfl @[to_additive] instance : is_lawful_monad free_magma.{u} := { pure_bind := λ _ _ _ _, rfl, bind_assoc := λ α β γ x f g, free_magma.rec_on_pure x (λ x, rfl) (λ x y ih1 ih2, by rw [mul_bind, mul_bind, mul_bind, ih1, ih2]), id_map := λ α x, free_magma.rec_on_pure x (λ _, rfl) (λ x y ih1 ih2, by rw [map_mul', ih1, ih2]) } end category end free_magma /-- `free_magma` is traversable. -/ protected def free_magma.traverse {m : Type u → Type u} [applicative m] {α β : Type u} (F : α → m β) : free_magma α → m (free_magma β) | (free_magma.of x) := free_magma.of <$> F x | (x * y) := (*) <$> x.traverse <*> y.traverse /-- `free_add_magma` is traversable. -/ protected def free_add_magma.traverse {m : Type u → Type u} [applicative m] {α β : Type u} (F : α → m β) : free_add_magma α → m (free_add_magma β) | (free_add_magma.of x) := free_add_magma.of <$> F x | (x + y) := (+) <$> x.traverse <*> y.traverse attribute [to_additive free_add_magma.traverse] free_magma.traverse namespace free_magma variables {α : Type u} section category variables {β : Type u} @[to_additive] instance : traversable free_magma := ⟨@free_magma.traverse⟩ variables {m : Type u → Type u} [applicative m] (F : α → m β) @[simp, to_additive] lemma traverse_pure (x) : traverse F (pure x : free_magma α) = pure <$> F x := rfl @[simp, to_additive] lemma traverse_pure' : traverse F ∘ pure = λ x, (pure <$> F x : m (free_magma β)) := rfl @[simp, to_additive] lemma traverse_mul (x y : free_magma α) : traverse F (x * y) = (*) <$> traverse F x <*> traverse F y := rfl @[simp, to_additive] lemma traverse_mul' : function.comp (traverse F) ∘ @has_mul.mul (free_magma α) _ = λ x y, (*) <$> traverse F x <*> traverse F y := rfl @[simp, to_additive] lemma traverse_eq (x) : free_magma.traverse F x = traverse F x := rfl @[simp, to_additive] lemma mul_map_seq (x y : free_magma α) : ((*) <$> x <*> y : id (free_magma α)) = (x * y : free_magma α) := rfl @[to_additive] instance : is_lawful_traversable free_magma.{u} := { id_traverse := λ α x, free_magma.rec_on_pure x (λ x, rfl) (λ x y ih1 ih2, by rw [traverse_mul, ih1, ih2, mul_map_seq]), comp_traverse := λ F G hf1 hg1 hf2 hg2 α β γ f g x, free_magma.rec_on_pure x (λ x, by resetI; simp only [traverse_pure, traverse_pure'] with functor_norm) (λ x y ih1 ih2, by resetI; rw [traverse_mul, ih1, ih2, traverse_mul]; simp only [traverse_mul'] with functor_norm), naturality := λ F G hf1 hg1 hf2 hg2 η α β f x, free_magma.rec_on_pure x (λ x, by simp only [traverse_pure] with functor_norm) (λ x y ih1 ih2, by simp only [traverse_mul] with functor_norm; rw [ih1, ih2]), traverse_eq_map_id := λ α β f x, free_magma.rec_on_pure x (λ _, rfl) (λ x y ih1 ih2, by rw [traverse_mul, ih1, ih2, map_mul', mul_map_seq]; refl), .. free_magma.is_lawful_monad } end category end free_magma /-- Representation of an element of a free magma. -/ protected def free_magma.repr {α : Type u} [has_repr α] : free_magma α → string | (free_magma.of x) := repr x | (x * y) := "( " ++ x.repr ++ " * " ++ y.repr ++ " )" /-- Representation of an element of a free additive magma. -/ protected def free_add_magma.repr {α : Type u} [has_repr α] : free_add_magma α → string | (free_add_magma.of x) := repr x | (x + y) := "( " ++ x.repr ++ " + " ++ y.repr ++ " )" attribute [to_additive free_add_magma.repr] free_magma.repr @[to_additive] instance {α : Type u} [has_repr α] : has_repr (free_magma α) := ⟨free_magma.repr⟩ /-- Length of an element of a free magma. -/ @[simp] def free_magma.length {α : Type u} : free_magma α → ℕ | (free_magma.of x) := 1 | (x * y) := x.length + y.length /-- Length of an element of a free additive magma. -/ @[simp] def free_add_magma.length {α : Type u} : free_add_magma α → ℕ | (free_add_magma.of x) := 1 | (x + y) := x.length + y.length attribute [to_additive free_add_magma.length] free_magma.length /-- Associativity relations for an additive magma. -/ inductive add_magma.assoc_rel (α : Type u) [has_add α] : α → α → Prop | intro : ∀ x y z, add_magma.assoc_rel ((x + y) + z) (x + (y + z)) | left : ∀ w x y z, add_magma.assoc_rel (w + ((x + y) + z)) (w + (x + (y + z))) /-- Associativity relations for a magma. -/ @[to_additive add_magma.assoc_rel "Associativity relations for an additive magma."] inductive magma.assoc_rel (α : Type u) [has_mul α] : α → α → Prop | intro : ∀ x y z, magma.assoc_rel ((x * y) * z) (x * (y * z)) | left : ∀ w x y z, magma.assoc_rel (w * ((x * y) * z)) (w * (x * (y * z))) namespace magma /-- Semigroup quotient of a magma. -/ @[to_additive add_magma.free_add_semigroup "Additive semigroup quotient of an additive magma."] def assoc_quotient (α : Type u) [has_mul α] : Type u := quot $ assoc_rel α namespace assoc_quotient variables {α : Type u} [has_mul α] @[to_additive] lemma quot_mk_assoc (x y z : α) : quot.mk (assoc_rel α) (x * y * z) = quot.mk _ (x * (y * z)) := quot.sound (assoc_rel.intro _ _ _) @[to_additive] lemma quot_mk_assoc_left (x y z w : α) : quot.mk (assoc_rel α) (x * (y * z * w)) = quot.mk _ (x * (y * (z * w))) := quot.sound (assoc_rel.left _ _ _ _) @[to_additive] instance : semigroup (assoc_quotient α) := { mul := λ x y, begin refine quot.lift_on₂ x y (λ x y, quot.mk _ (x * y)) _ _, { rintro a b₁ b₂ (⟨c, d, e⟩ | ⟨c, d, e, f⟩); simp only, { exact quot_mk_assoc_left _ _ _ _ }, { rw [← quot_mk_assoc, quot_mk_assoc_left, quot_mk_assoc] } }, { rintro a₁ a₂ b (⟨c, d, e⟩ | ⟨c, d, e, f⟩); simp only, { simp only [quot_mk_assoc, quot_mk_assoc_left] }, { rw [quot_mk_assoc, quot_mk_assoc, quot_mk_assoc_left, quot_mk_assoc_left, quot_mk_assoc_left, ← quot_mk_assoc c d, ← quot_mk_assoc c d, quot_mk_assoc_left] } } end, mul_assoc := λ x y z, quot.induction_on₃ x y z $ λ p q r, quot_mk_assoc p q r } /-- Embedding from magma to its free semigroup. -/ @[to_additive "Embedding from additive magma to its free additive semigroup."] def of : α →ₙ* assoc_quotient α := ⟨quot.mk _, λ x y, rfl⟩ @[to_additive] instance [inhabited α] : inhabited (assoc_quotient α) := ⟨of default⟩ @[elab_as_eliminator, to_additive] protected lemma induction_on {C : assoc_quotient α → Prop} (x : assoc_quotient α) (ih : ∀ x, C (of x)) : C x := quot.induction_on x ih section lift variables {β : Type v} [semigroup β] (f : α →ₙ* β) @[ext, to_additive] lemma hom_ext {f g : assoc_quotient α →ₙ* β} (h : f.comp of = g.comp of) : f = g := fun_like.ext _ _ $ λ x, assoc_quotient.induction_on x $ fun_like.congr_fun h /-- Lifts a magma homomorphism `α → β` to a semigroup homomorphism `magma.assoc_quotient α → β` given a semigroup `β`. -/ @[to_additive "Lifts an additive magma homomorphism `α → β` to an additive semigroup homomorphism `add_magma.assoc_quotient α → β` given an additive semigroup `β`.", simps symm_apply] def lift : (α →ₙ* β) ≃ (assoc_quotient α →ₙ* β) := { to_fun := λ f, { to_fun := λ x, quot.lift_on x f $ by rintros a b (⟨c, d, e⟩ | ⟨c, d, e, f⟩); simp only [map_mul, mul_assoc], map_mul' := λ x y, quot.induction_on₂ x y (map_mul f) }, inv_fun := λ f, f.comp of, left_inv := λ f, fun_like.ext _ _ $ λ x, rfl, right_inv := λ f, hom_ext $ fun_like.ext _ _ $ λ x, rfl } @[simp, to_additive] lemma lift_of (x : α) : lift f (of x) = f x := rfl @[simp, to_additive] lemma lift_comp_of : (lift f).comp of = f := lift.symm_apply_apply f @[simp, to_additive] lemma lift_comp_of' (f : assoc_quotient α →ₙ* β) : lift (f.comp of) = f := lift.apply_symm_apply f end lift variables {β : Type v} [has_mul β] (f : α →ₙ* β) /-- From a magma homomorphism `α →ₙ* β` to a semigroup homomorphism `magma.assoc_quotient α →ₙ* magma.assoc_quotient β`. -/ @[to_additive "From an additive magma homomorphism `α → β` to an additive semigroup homomorphism `add_magma.assoc_quotient α → add_magma.assoc_quotient β`."] def map : assoc_quotient α →ₙ* assoc_quotient β := lift (of.comp f) @[simp, to_additive] lemma map_of (x) : map f (of x) = of (f x) := rfl end assoc_quotient end magma /-- Free additive semigroup over a given alphabet. -/ @[ext] structure free_add_semigroup (α : Type u) := (head : α) (tail : list α) /-- Free semigroup over a given alphabet. -/ @[ext, to_additive] structure free_semigroup (α : Type u) := (head : α) (tail : list α) namespace free_semigroup variables {α : Type u} @[to_additive] instance : semigroup (free_semigroup α) := { mul := λ L1 L2, ⟨L1.1, L1.2 ++ L2.1 :: L2.2⟩, mul_assoc := λ L1 L2 L3, ext _ _ rfl $ list.append_assoc _ _ _ } @[simp, to_additive] lemma head_mul (x y : free_semigroup α) : (x * y).1 = x.1 := rfl @[simp, to_additive] lemma tail_mul (x y : free_semigroup α) : (x * y).2 = x.2 ++ (y.1 :: y.2) := rfl @[simp, to_additive] lemma mk_mul_mk (x y : α) (L1 L2 : list α) : mk x L1 * mk y L2 = mk x (L1 ++ y :: L2) := rfl /-- The embedding `α → free_semigroup α`. -/ @[to_additive "The embedding `α → free_add_semigroup α`.", simps] def of (x : α) : free_semigroup α := ⟨x, []⟩ /-- Length of an element of free semigroup. -/ @[to_additive "Length of an element of free additive semigroup"] def length (x : free_semigroup α) : ℕ := x.tail.length + 1 @[simp, to_additive] lemma length_mul (x y : free_semigroup α) : (x * y).length = x.length + y.length := by simp [length, ← add_assoc, add_right_comm] @[simp, to_additive] lemma length_of (x : α) : (of x).length = 1 := rfl @[to_additive] instance [inhabited α] : inhabited (free_semigroup α) := ⟨of default⟩ /-- Recursor for free semigroup using `of` and `*`. -/ @[elab_as_eliminator, to_additive "Recursor for free additive semigroup using `of` and `+`."] protected def rec_on_mul {C : free_semigroup α → Sort l} (x) (ih1 : ∀ x, C (of x)) (ih2 : ∀ x y, C (of x) → C y → C (of x * y)) : C x := free_semigroup.rec_on x $ λ f s, list.rec_on s ih1 (λ hd tl ih f, ih2 f ⟨hd, tl⟩ (ih1 f) (ih hd)) f @[ext, to_additive] lemma hom_ext {β : Type v} [has_mul β] {f g : free_semigroup α →ₙ* β} (h : f ∘ of = g ∘ of) : f = g := fun_like.ext _ _ $ λ x, free_semigroup.rec_on_mul x (congr_fun h) $ λ x y hx hy, by simp only [map_mul, *] section lift variables {β : Type v} [semigroup β] (f : α → β) /-- Lifts a function `α → β` to a semigroup homomorphism `free_semigroup α → β` given a semigroup `β`. -/ @[to_additive "Lifts a function `α → β` to an additive semigroup homomorphism `free_add_semigroup α → β` given an additive semigroup `β`.", simps symm_apply] def lift : (α → β) ≃ (free_semigroup α →ₙ* β) := { to_fun := λ f, { to_fun := λ x, x.2.foldl (λ a b, a * f b) (f x.1), map_mul' := λ x y, by simp only [head_mul, tail_mul, ← list.foldl_map f, list.foldl_append, list.foldl_cons, list.foldl_assoc] }, inv_fun := λ f, f ∘ of, left_inv := λ f, rfl, right_inv := λ f, hom_ext rfl } @[simp, to_additive] lemma lift_of (x : α) : lift f (of x) = f x := rfl @[simp, to_additive] lemma lift_comp_of : lift f ∘ of = f := rfl @[simp, to_additive] lemma lift_comp_of' (f : free_semigroup α →ₙ* β) : lift (f ∘ of) = f := hom_ext rfl @[to_additive] lemma lift_of_mul (x y) : lift f (of x * y) = f x * lift f y := by rw [map_mul, lift_of] end lift section map variables {β : Type v} (f : α → β) /-- The unique semigroup homomorphism that sends `of x` to `of (f x)`. -/ @[to_additive "The unique additive semigroup homomorphism that sends `of x` to `of (f x)`."] def map : free_semigroup α →ₙ* free_semigroup β := lift $ of ∘ f @[simp, to_additive] lemma map_of (x) : map f (of x) = of (f x) := rfl @[simp, to_additive] lemma length_map (x) : (map f x).length = x.length := free_semigroup.rec_on_mul x (λ x, rfl) $ λ x y hx hy, by simp only [map_mul, length_mul, *] end map section category variables {β : Type u} @[to_additive] instance : monad free_semigroup := { pure := λ _, of, bind := λ _ _ x f, lift f x } /-- Recursor that uses `pure` instead of `of`. -/ @[elab_as_eliminator, to_additive "Recursor that uses `pure` instead of `of`."] def rec_on_pure {C : free_semigroup α → Sort l} (x) (ih1 : ∀ x, C (pure x)) (ih2 : ∀ x y, C (pure x) → C y → C (pure x * y)) : C x := free_semigroup.rec_on_mul x ih1 ih2 @[simp, to_additive] lemma map_pure (f : α → β) (x) : (f <$> pure x : free_semigroup β) = pure (f x) := rfl @[simp, to_additive] lemma map_mul' (f : α → β) (x y : free_semigroup α) : (f <$> (x * y)) = (f <$> x * f <$> y) := map_mul (map f) _ _ @[simp, to_additive] lemma pure_bind (f : α → free_semigroup β) (x) : (pure x >>= f) = f x := rfl @[simp, to_additive] lemma mul_bind (f : α → free_semigroup β) (x y : free_semigroup α) : (x * y >>= f) = ((x >>= f) * (y >>= f)) := map_mul (lift f) _ _ @[simp, to_additive] lemma pure_seq {f : α → β} {x : free_semigroup α} : pure f <*> x = f <$> x := rfl @[simp, to_additive] lemma mul_seq {f g : free_semigroup (α → β)} {x : free_semigroup α} : (f * g) <*> x = (f <*> x) * (g <*> x) := mul_bind _ _ _ @[to_additive] instance : is_lawful_monad free_semigroup.{u} := { pure_bind := λ _ _ _ _, rfl, bind_assoc := λ α β γ x f g, rec_on_pure x (λ x, rfl) (λ x y ih1 ih2, by rw [mul_bind, mul_bind, mul_bind, ih1, ih2]), id_map := λ α x, rec_on_pure x (λ _, rfl) (λ x y ih1 ih2, by rw [map_mul', ih1, ih2]) } /-- `free_semigroup` is traversable. -/ @[to_additive "`free_add_semigroup` is traversable."] protected def traverse {m : Type u → Type u} [applicative m] {α β : Type u} (F : α → m β) (x : free_semigroup α) : m (free_semigroup β) := rec_on_pure x (λ x, pure <$> F x) (λ x y ihx ihy, (*) <$> ihx <*> ihy) @[to_additive] instance : traversable free_semigroup := ⟨@free_semigroup.traverse⟩ variables {m : Type u → Type u} [applicative m] (F : α → m β) @[simp, to_additive] lemma traverse_pure (x) :traverse F (pure x : free_semigroup α) = pure <$> F x := rfl @[simp, to_additive] lemma traverse_pure' : traverse F ∘ pure = λ x, (pure <$> F x : m (free_semigroup β)) := rfl section variables [is_lawful_applicative m] @[simp, to_additive] lemma traverse_mul (x y : free_semigroup α) : traverse F (x * y) = (*) <$> traverse F x <*> traverse F y := let ⟨x, L1⟩ := x, ⟨y, L2⟩ := y in list.rec_on L1 (λ x, rfl) (λ hd tl ih x, show (*) <$> pure <$> F x <*> traverse F ((mk hd tl) * (mk y L2)) = (*) <$> ((*) <$> pure <$> F x <*> traverse F (mk hd tl)) <*> traverse F (mk y L2), by rw ih; simp only [(∘), (mul_assoc _ _ _).symm] with functor_norm) x @[simp, to_additive] lemma traverse_mul' : function.comp (traverse F) ∘ @has_mul.mul (free_semigroup α) _ = λ x y, (*) <$> traverse F x <*> traverse F y := funext $ λ x, funext $ λ y, traverse_mul F x y end @[simp, to_additive] lemma traverse_eq (x) : free_semigroup.traverse F x = traverse F x := rfl @[simp, to_additive] lemma mul_map_seq (x y : free_semigroup α) : ((*) <$> x <*> y : id (free_semigroup α)) = (x * y : free_semigroup α) := rfl @[to_additive] instance : is_lawful_traversable free_semigroup.{u} := { id_traverse := λ α x, free_semigroup.rec_on_mul x (λ x, rfl) (λ x y ih1 ih2, by rw [traverse_mul, ih1, ih2, mul_map_seq]), comp_traverse := λ F G hf1 hg1 hf2 hg2 α β γ f g x, rec_on_pure x (λ x, by resetI; simp only [traverse_pure, traverse_pure'] with functor_norm) (λ x y ih1 ih2, by resetI; rw [traverse_mul, ih1, ih2, traverse_mul]; simp only [traverse_mul'] with functor_norm), naturality := λ F G hf1 hg1 hf2 hg2 η α β f x, rec_on_pure x (λ x, by simp only [traverse_pure] with functor_norm) (λ x y ih1 ih2, by resetI; simp only [traverse_mul] with functor_norm; rw [ih1, ih2]), traverse_eq_map_id := λ α β f x, free_semigroup.rec_on_mul x (λ _, rfl) (λ x y ih1 ih2, by rw [traverse_mul, ih1, ih2, map_mul', mul_map_seq]; refl), .. free_semigroup.is_lawful_monad } end category @[to_additive] instance [decidable_eq α] : decidable_eq (free_semigroup α) := λ x y, decidable_of_iff' _ (ext_iff _ _) end free_semigroup namespace free_magma variables {α : Type u} {β : Type v} /-- The canonical multiplicative morphism from `free_magma α` to `free_semigroup α`. -/ @[to_additive "The canonical additive morphism from `free_add_magma α` to `free_add_semigroup α`."] def to_free_semigroup : free_magma α →ₙ* free_semigroup α := free_magma.lift free_semigroup.of @[simp, to_additive] lemma to_free_semigroup_of (x : α) : to_free_semigroup (of x) = free_semigroup.of x := rfl @[simp, to_additive] lemma to_free_semigroup_comp_of : @to_free_semigroup α ∘ of = free_semigroup.of := rfl @[to_additive] lemma to_free_semigroup_comp_map (f : α → β) : to_free_semigroup.comp (map f) = (free_semigroup.map f).comp to_free_semigroup := by { ext1, refl } @[to_additive] lemma to_free_semigroup_map (f : α → β) (x : free_magma α) : (map f x).to_free_semigroup = free_semigroup.map f x.to_free_semigroup := fun_like.congr_fun (to_free_semigroup_comp_map f) x @[simp, to_additive] lemma length_to_free_semigroup (x : free_magma α) : x.to_free_semigroup.length = x.length := free_magma.rec_on_mul x (λ x, rfl) $ λ x y hx hy, by rw [map_mul, free_semigroup.length_mul, length, hx, hy] end free_magma /-- Isomorphism between `magma.assoc_quotient (free_magma α)` and `free_semigroup α`. -/ @[to_additive "Isomorphism between `add_magma.assoc_quotient (free_add_magma α)` and `free_add_semigroup α`."] def free_magma_assoc_quotient_equiv (α : Type u) : magma.assoc_quotient (free_magma α) ≃* free_semigroup α := (magma.assoc_quotient.lift free_magma.to_free_semigroup).to_mul_equiv (free_semigroup.lift (magma.assoc_quotient.of ∘ free_magma.of)) (by { ext, refl }) (by { ext1, refl })
248d11f6c4c583e19b89e3ac7e0a5ce4f29ac773
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/eval_expr_error.lean
a77150fe21ee9ecaeda907a597c1753053732b94
[ "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
347
lean
open tactic meta def tst1 (A : Type) : tactic unit := do a ← to_expr ``(0), v ← eval_expr A a, return () run_cmd do a ← to_expr ``(nat.add), v ← eval_expr nat a, trace v, return () run_cmd do a ← to_expr ``(λ x : nat, x + 1), v ← (eval_expr nat a <|> trace "tactic failed" >> return 1), trace v, return ()
bd49fb7e8bbaf3e60854f9379fa8c33afd24b2bb
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/nat/dist.lean
7b809ee1d61c17ef3df16226f3dc4e7fca1f9523
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
3,888
lean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Jeremy Avigad Distance function on the natural numbers. -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.nat.basic import Mathlib.PostPort namespace Mathlib namespace nat /- distance -/ /-- Distance (absolute value of difference) between natural numbers. -/ def dist (n : ℕ) (m : ℕ) : ℕ := n - m + (m - n) theorem dist.def (n : ℕ) (m : ℕ) : dist n m = n - m + (m - n) := rfl theorem dist_comm (n : ℕ) (m : ℕ) : dist n m = dist m n := sorry @[simp] theorem dist_self (n : ℕ) : dist n n = 0 := sorry theorem eq_of_dist_eq_zero {n : ℕ} {m : ℕ} (h : dist n m = 0) : n = m := sorry theorem dist_eq_zero {n : ℕ} {m : ℕ} (h : n = m) : dist n m = 0 := eq.mpr (id (Eq._oldrec (Eq.refl (dist n m = 0)) h)) (eq.mpr (id (Eq._oldrec (Eq.refl (dist m m = 0)) (dist_self m))) (Eq.refl 0)) theorem dist_eq_sub_of_le {n : ℕ} {m : ℕ} (h : n ≤ m) : dist n m = m - n := eq.mpr (id (Eq._oldrec (Eq.refl (dist n m = m - n)) (dist.def n m))) (eq.mpr (id (Eq._oldrec (Eq.refl (n - m + (m - n) = m - n)) (sub_eq_zero_of_le h))) (eq.mpr (id (Eq._oldrec (Eq.refl (0 + (m - n) = m - n)) (zero_add (m - n)))) (Eq.refl (m - n)))) theorem dist_eq_sub_of_le_right {n : ℕ} {m : ℕ} (h : m ≤ n) : dist n m = n - m := eq.mpr (id (Eq._oldrec (Eq.refl (dist n m = n - m)) (dist_comm n m))) (dist_eq_sub_of_le h) theorem dist_tri_left (n : ℕ) (m : ℕ) : m ≤ dist n m + n := le_trans (nat.le_sub_add m n) (add_le_add_right (le_add_left (m - n) (n - m)) n) theorem dist_tri_right (n : ℕ) (m : ℕ) : m ≤ n + dist n m := eq.mpr (id (Eq._oldrec (Eq.refl (m ≤ n + dist n m)) (add_comm n (dist n m)))) (dist_tri_left n m) theorem dist_tri_left' (n : ℕ) (m : ℕ) : n ≤ dist n m + m := eq.mpr (id (Eq._oldrec (Eq.refl (n ≤ dist n m + m)) (dist_comm n m))) (dist_tri_left m n) theorem dist_tri_right' (n : ℕ) (m : ℕ) : n ≤ m + dist n m := eq.mpr (id (Eq._oldrec (Eq.refl (n ≤ m + dist n m)) (dist_comm n m))) (dist_tri_right m n) theorem dist_zero_right (n : ℕ) : dist n 0 = n := Eq.trans (dist_eq_sub_of_le_right (zero_le n)) (nat.sub_zero n) theorem dist_zero_left (n : ℕ) : dist 0 n = n := Eq.trans (dist_eq_sub_of_le (zero_le n)) (nat.sub_zero n) theorem dist_add_add_right (n : ℕ) (k : ℕ) (m : ℕ) : dist (n + k) (m + k) = dist n m := sorry theorem dist_add_add_left (k : ℕ) (n : ℕ) (m : ℕ) : dist (k + n) (k + m) = dist n m := eq.mpr (id (Eq._oldrec (Eq.refl (dist (k + n) (k + m) = dist n m)) (add_comm k n))) (eq.mpr (id (Eq._oldrec (Eq.refl (dist (n + k) (k + m) = dist n m)) (add_comm k m))) (dist_add_add_right n k m)) theorem dist_eq_intro {n : ℕ} {m : ℕ} {k : ℕ} {l : ℕ} (h : n + m = k + l) : dist n k = dist l m := sorry protected theorem sub_lt_sub_add_sub (n : ℕ) (m : ℕ) (k : ℕ) : n - k ≤ n - m + (m - k) := sorry theorem dist.triangle_inequality (n : ℕ) (m : ℕ) (k : ℕ) : dist n k ≤ dist n m + dist m k := sorry theorem dist_mul_right (n : ℕ) (k : ℕ) (m : ℕ) : dist (n * k) (m * k) = dist n m * k := sorry theorem dist_mul_left (k : ℕ) (n : ℕ) (m : ℕ) : dist (k * n) (k * m) = k * dist n m := sorry -- TODO(Jeremy): do when we have max and minx --theorem dist_eq_max_sub_min {i j : nat} : dist i j = (max i j) - min i j := --sorry /- or.elim (lt_or_ge i j) (assume : i < j, by rw [max_eq_right_of_lt this, min_eq_left_of_lt this, dist_eq_sub_of_lt this]) (assume : i ≥ j, by rw [max_eq_left this , min_eq_right this, dist_eq_sub_of_le_right this]) -/ theorem dist_succ_succ {i : ℕ} {j : ℕ} : dist (Nat.succ i) (Nat.succ j) = dist i j := sorry theorem dist_pos_of_ne {i : ℕ} {j : ℕ} : i ≠ j → 0 < dist i j := sorry
3056e179da82554406ff1428164e6a40188ca434
8c9f90127b78cbeb5bb17fd6b5db1db2ffa3cbc4
/Kevins_blog_post_2.lean
fe0b2b91a3cbaa7c18fea0bf9c14bd1fbc6b7aca
[]
no_license
picrin/lean
420f4d08bb3796b911d56d0938e4410e1da0e072
3d10c509c79704aa3a88ebfb24d08b30ce1137cc
refs/heads/master
1,611,166,610,726
1,536,671,438,000
1,536,671,438,000
60,029,899
0
0
null
null
null
null
UTF-8
Lean
false
false
2,850
lean
inductive xnat : Type | zero : xnat | succ : xnat → xnat inductive equality (a : xnat): xnat → Prop | refl : equality a definition equality.trans: Π (a b c : xnat), Π eq1: (equality a b), Π eq2 : (equality b c), equality a c := λ a b c : xnat, λ eq1 : (equality a b), λ eq2 : (equality b c), @equality.rec_on b (λ x, equality a x) c eq2 eq1 definition equality.symm: Π (a b : xnat), Π eq1 : (equality a b), equality b a := λ a b : xnat, λ eq1 : (equality a b), -- {a : xnat} {C : xnat → Sort l} {a_1 : xnat}, equality a a_1 → C a → C a_1 --@equality.rec_on b (equality b) b eq1 (equality.refl a) @equality.rec_on a (λ b, equality b a) b eq1 (equality.refl a) definition add : xnat → xnat → xnat := λ a : xnat, λ b : xnat, xnat.rec_on b a (λ prev_b IH : xnat, xnat.succ IH) def succ_over_equality : Π (a b : xnat) (H : equality a b), equality (xnat.succ a) (xnat.succ b) := λ a b : xnat, λ H : equality a b, equality.rec_on H (equality.refl (xnat.succ a)) def xnat.add_zero_base_case : Π a: xnat, equality (add xnat.zero (xnat.succ a)) (xnat.succ (add xnat.zero a)) := λ a: xnat, equality.refl (add xnat.zero (xnat.succ a)) def xnat.add_zero_inductive_step : Π a : xnat, Π IH : (equality (add xnat.zero a) a), equality (xnat.succ (add xnat.zero a)) (xnat.succ a) := λ a: xnat, λ IH, succ_over_equality (add xnat.zero a) a IH def xnat.add_zero: Π n : xnat, equality (add xnat.zero n) n := λ n, xnat.rec_on n (equality.refl xnat.zero) (λ a : xnat, λ IH, equality.trans (add xnat.zero (xnat.succ a)) (xnat.succ (add xnat.zero a)) (xnat.succ a) (xnat.add_zero_base_case a) (xnat.add_zero_inductive_step a IH)) def xnat.add_succ (a b : xnat) : equality (add a (xnat.succ b)) (xnat.succ (add a b)) := equality.refl (add a (xnat.succ b)) -- This is getting out of hand, we really need tactics. def xnat.add_assoc (a b c : xnat) : equality (add (add a b) c) (add a (add b c)) := xnat.rec_on c (equality.refl (add a b)) (λ prev_c : xnat, λ IH : equality (add (add a b) prev_c) (add a (add b prev_c)), show equality (add (add a b) (xnat.succ prev_c)) (add a (add b (xnat.succ prev_c))), from have H1 : equality (add b (xnat.succ prev_c)) (xnat.succ (add b prev_c)), from xnat.add_succ b prev_c, have H2 : equality (xnat.succ (add b prev_c)) (add b (xnat.succ prev_c)), from equality.symm (add b (xnat.succ prev_c)) (xnat.succ (add b prev_c)) H1, have IH_up : equality (xnat.succ (add (add a b) prev_c)) (xnat.succ (add a (add b prev_c))), from succ_over_equality (add (add a b) prev_c) (add a (add b prev_c)) IH, sorry) --variables a b : xnat --variable IH : equality (add xnat.zero a) a --#check (equality a) b --#check equality.refl xnat.zero --#check xnat.zero
d4663aea49aaabb52b4641ce2541f3627c79b77d
aa3f8992ef7806974bc1ffd468baa0c79f4d6643
/library/data/nat/decl.lean
e0a73bc78ce44460e952886998655969ffb56847
[ "Apache-2.0" ]
permissive
codyroux/lean
7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3
0cca265db19f7296531e339192e9b9bae4a31f8b
refs/heads/master
1,610,909,964,159
1,407,084,399,000
1,416,857,075,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
10,790
lean
--- Copyright (c) 2014 Floris van Doorn. All rights reserved. --- Released under Apache 2.0 license as described in the file LICENSE. --- Author: Floris van Doorn, Leonardo de Moura import logic.eq logic.heq logic.wf logic.decidable logic.if data.prod open eq.ops decidable inductive nat := zero : nat, succ : nat → nat namespace nat notation `ℕ` := nat inductive lt (a : nat) : nat → Prop := base : lt a (succ a), step : Π {b}, lt a b → lt a (succ b) notation a < b := lt a b inductive le (a : nat) : nat → Prop := refl : le a a, of_lt : ∀ {b}, lt a b → le a b notation a ≤ b := le a b definition pred (a : nat) : nat := cases_on a zero (λ a₁, a₁) protected definition is_inhabited [instance] : inhabited nat := inhabited.mk zero protected definition has_decidable_eq [instance] : decidable_eq nat := λn m : nat, have general : ∀n, decidable (n = m), from rec_on m (λ n, cases_on n (inl rfl) (λ m, inr (λ (e : succ m = zero), no_confusion e))) (λ (m' : nat) (ih : ∀n, decidable (n = m')) (n : nat), cases_on n (inr (λ h, no_confusion h)) (λ (n' : nat), decidable.rec_on (ih n') (assume Heq : n' = m', inl (eq.rec_on Heq rfl)) (assume Hne : n' ≠ m', have H1 : succ n' ≠ succ m', from assume Heq, no_confusion Heq (λ e : n' = m', Hne e), inr H1))), general n -- less-than is well-founded definition lt.wf [instance] : well_founded lt := well_founded.intro (λn, rec_on n (acc.intro zero (λ (y : nat) (hlt : y < zero), have aux : ∀ {n₁}, y < n₁ → zero = n₁ → acc lt y, from λ n₁ hlt, lt.cases_on hlt (λ heq, no_confusion heq) (λ b hlt heq, no_confusion heq), aux hlt rfl)) (λ (n : nat) (ih : acc lt n), acc.intro (succ n) (λ (m : nat) (hlt : m < succ n), have aux : ∀ {n₁} (hlt : m < n₁), succ n = n₁ → acc lt m, from λ n₁ hlt, lt.cases_on hlt (λ (heq : succ n = succ m), nat.no_confusion heq (λ (e : n = m), eq.rec_on e ih)) (λ b (hlt : m < b) (heq : succ n = succ b), nat.no_confusion heq (λ (e : n = b), acc.inv (eq.rec_on e ih) hlt)), aux hlt rfl))) definition measure {A : Type} (f : A → nat) : A → A → Prop := inv_image lt f definition measure.wf {A : Type} (f : A → nat) : well_founded (measure f) := inv_image.wf f lt.wf definition not_lt_zero (a : nat) : ¬ a < zero := have aux : ∀ {b}, a < b → b = zero → false, from λ b H, lt.cases_on H (λ heq, nat.no_confusion heq) (λ b h₁ heq, nat.no_confusion heq), λ H, aux H rfl definition zero_lt_succ (a : nat) : zero < succ a := rec_on a (lt.base zero) (λ a (hlt : zero < succ a), lt.step hlt) definition lt.trans {a b c : nat} (H₁ : a < b) (H₂ : b < c) : a < c := have aux : ∀ {d}, d < c → b = d → a < b → a < c, from (λ d H, lt.rec_on H (λ h₁ h₂, lt.step (eq.rec_on h₁ h₂)) (λ b hl ih h₁ h₂, lt.step (ih h₁ h₂))), aux H₂ rfl H₁ definition lt.imp_succ {a b : nat} (H : a < b) : succ a < succ b := lt.rec_on H (lt.base (succ a)) (λ b hlt ih, lt.trans ih (lt.base (succ b))) definition lt.cancel_succ_left {a b : nat} (H : succ a < b) : a < b := have aux : ∀ {a₁}, a₁ < b → succ a = a₁ → a < b, from λ a₁ H, lt.rec_on H (λ e₁, eq.rec_on e₁ (lt.step (lt.base a))) (λ d hlt ih e₁, lt.step (ih e₁)), aux H rfl definition lt.cancel_succ_left_right {a b : nat} (H : succ a < succ b) : a < b := have aux : pred (succ a) < pred (succ b), from lt.rec_on H (lt.base a) (λ (b : nat) (hlt : succ a < b) ih, show pred (succ a) < pred (succ b), from lt.cancel_succ_left hlt), aux definition lt.is_decidable_rel [instance] : decidable_rel lt := λ a b, rec_on b (λ (a : nat), inr (not_lt_zero a)) (λ (b₁ : nat) (ih : ∀ a, decidable (a < b₁)) (a : nat), cases_on a (inl !zero_lt_succ) (λ a, decidable.rec_on (ih a) (λ h_pos : a < b₁, inl (lt.imp_succ h_pos)) (λ h_neg : ¬ a < b₁, have aux : ¬ succ a < succ b₁, from λ h : succ a < succ b₁, h_neg (lt.cancel_succ_left_right h), inr aux))) a definition le_def_right {a b : nat} (H : a ≤ b) : a = b ∨ a < b := le.cases_on H (or.inl rfl) (λ b hlt, or.inr hlt) definition le_def_left {a b : nat} (H : a = b ∨ a < b) : a ≤ b := or.rec_on H (λ hl, eq.rec_on hl !le.refl) (λ hr, le.of_lt hr) definition le.is_decidable_rel [instance] : decidable_rel le := λ a b, decidable_iff_equiv _ (iff.intro le_def_left le_def_right) definition lt.irrefl (a : nat) : ¬ a < a := rec_on a !not_lt_zero (λ (a : nat) (ih : ¬ a < a) (h : succ a < succ a), ih (lt.cancel_succ_left_right h)) definition lt.asymm {a b : nat} (H : a < b) : ¬ b < a := lt.rec_on H (λ h : succ a < a, !lt.irrefl (lt.cancel_succ_left h)) (λ b hlt (ih : ¬ b < a) (h : succ b < a), ih (lt.cancel_succ_left h)) definition lt.trichotomy (a b : nat) : a < b ∨ a = b ∨ b < a := rec_on b (λa, cases_on a (or.inr (or.inl rfl)) (λ a₁, or.inr (or.inr !zero_lt_succ))) (λ b₁ (ih : ∀a, a < b₁ ∨ a = b₁ ∨ b₁ < a) (a : nat), cases_on a (or.inl !zero_lt_succ) (λ a, or.rec_on (ih a) (λ h : a < b₁, or.inl (lt.imp_succ h)) (λ h, or.rec_on h (λ h : a = b₁, or.inr (or.inl (eq.rec_on h rfl))) (λ h : b₁ < a, or.inr (or.inr (lt.imp_succ h)))))) a definition not_lt {a b : nat} (hnlt : ¬ a < b) : a = b ∨ b < a := or.rec_on (lt.trichotomy a b) (λ hlt, absurd hlt hnlt) (λ h, h) definition le_imp_lt_succ {a b : nat} (h : a ≤ b) : a < succ b := le.rec_on h (lt.base a) (λ b h, lt.step h) definition le_succ_imp_lt {a b : nat} (h : succ a ≤ b) : a < b := le.rec_on h (lt.base a) (λ b (h : succ a < b), lt.cancel_succ_left_right (lt.step h)) definition le.step {a b : nat} (h : a ≤ b) : a ≤ succ b := le.rec_on h (le.of_lt (lt.base a)) (λ b (h : a < b), le.of_lt (lt.step h)) definition lt_imp_le_succ {a b : nat} (h : a < b) : succ a ≤ b := lt.rec_on h (le.refl (succ a)) (λ b hlt (ih : succ a ≤ b), le.step ih) definition le.trans {a b c : nat} (h₁ : a ≤ b) : b ≤ c → a ≤ c := le.rec_on h₁ (λ h, h) (λ b (h₁ : a < b) (h₂ : b ≤ c), le.rec_on h₂ (le.of_lt h₁) (λ c (h₂ : b < c), le.of_lt (lt.trans h₁ h₂))) definition le_lt.trans {a b c : nat} (h₁ : a ≤ b) : b < c → a < c := le.rec_on h₁ (λ h, h) (λ b (h₁ : a < b) (h₂ : b < c), lt.trans h₁ h₂) definition lt_le.trans {a b c : nat} (h₁ : a < b) (h₂ : b ≤ c) : a < c := le.rec_on h₂ h₁ (λ c (h₂ : b < c), lt.trans h₁ h₂) definition lt_eq.trans {a b c : nat} (h₁ : a < b) (h₂ : b = c) : a < c := eq.rec_on h₂ h₁ definition le_eq.trans {a b c : nat} (h₁ : a ≤ b) (h₂ : b = c) : a ≤ c := eq.rec_on h₂ h₁ definition eq_lt.trans {a b c : nat} (h₁ : a = b) (h₂ : b < c) : a < c := eq.rec_on (eq.rec_on h₁ rfl) h₂ definition eq_le.trans {a b c : nat} (h₁ : a = b) (h₂ : b ≤ c) : a ≤ c := eq.rec_on (eq.rec_on h₁ rfl) h₂ calc_trans lt.trans calc_trans le.trans calc_trans le_lt.trans calc_trans lt_le.trans calc_trans lt_eq.trans calc_trans le_eq.trans calc_trans eq_lt.trans calc_trans eq_le.trans definition max (a b : nat) : nat := if a < b then b else a definition min (a b : nat) : nat := if a < b then a else b definition max_a_a (a : nat) : a = max a a := eq.rec_on !if_t_t rfl definition max.eq_right {a b : nat} (H : a < b) : max a b = b := if_pos H definition max.eq_left {a b : nat} (H : ¬ a < b) : max a b = a := if_neg H definition max.eq_right_symm {a b : nat} (H : a < b) : b = max a b := eq.rec_on (max.eq_right H) rfl definition max.eq_left_symm {a b : nat} (H : ¬ a < b) : a = max a b := eq.rec_on (max.eq_left H) rfl definition max.left (a b : nat) : a ≤ max a b := by_cases (λ h : a < b, le.of_lt (eq.rec_on (max.eq_right_symm h) h)) (λ h : ¬ a < b, eq.rec_on (max.eq_left h) !le.refl) definition max.right (a b : nat) : b ≤ max a b := by_cases (λ h : a < b, eq.rec_on (max.eq_right h) !le.refl) (λ h : ¬ a < b, or.rec_on (not_lt h) (λ heq, eq.rec_on heq (eq.rec_on (max_a_a a) !le.refl)) (λ h : b < a, have aux : a = max a b, from max.eq_left_symm (lt.asymm h), eq.rec_on aux (le.of_lt h))) definition gt a b := lt b a notation a > b := gt a b definition ge a b := le b a notation a ≥ b := ge a b definition add (a b : nat) : nat := rec_on b a (λ b₁ r, succ r) notation a + b := add a b definition sub (a b : nat) : nat := rec_on b a (λ b₁ r, pred r) notation a - b := sub a b definition mul (a b : nat) : nat := rec_on b zero (λ b₁ r, r + a) notation a * b := mul a b definition sub.succ_succ (a b : nat) : succ a - succ b = a - b := induction_on b rfl (λ b₁ (ih : succ a - succ b₁ = a - b₁), eq.rec_on ih (eq.refl (pred (succ a - succ b₁)))) definition sub.succ_succ_symm (a b : nat) : a - b = succ a - succ b := eq.rec_on (sub.succ_succ a b) rfl definition sub.zero_left (a : nat) : zero - a = zero := induction_on a rfl (λ a₁ (ih : zero - a₁ = zero), eq.rec_on ih (eq.refl (pred (zero - a₁)))) definition sub.zero_left_symm (a : nat) : zero = zero - a := eq.rec_on (sub.zero_left a) rfl definition sub.lt {a b : nat} : zero < a → zero < b → a - b < a := have aux : Π {a}, zero < a → Π {b}, zero < b → a - b < a, from λa h₁, lt.rec_on h₁ (λb h₂, lt.cases_on h₂ (lt.base zero) (λ b₁ bpos, eq.rec_on (sub.succ_succ_symm zero b₁) (eq.rec_on (sub.zero_left_symm b₁) (lt.base zero)))) (λa₁ apos ih b h₂, lt.cases_on h₂ (lt.base a₁) (λ b₁ bpos, eq.rec_on (sub.succ_succ_symm a₁ b₁) (lt.trans (@ih b₁ bpos) (lt.base a₁)))), λ h₁ h₂, aux h₁ h₂ definition sub_pred (a : nat) : pred a ≤ a := cases_on a (le.refl zero) (λ a₁, le.of_lt (lt.base a₁)) definition sub_le_self (a b : nat) : a - b ≤ a := induction_on b (le.refl a) (λ b₁ ih, le.trans !sub_pred ih) end nat
63b277ab2a77bb6dfea48e5c9ab7ee079c57ca30
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/measure_theory/measurable_space.lean
8298b8bdd1157dcbdb62ce284257a9b04bc4cd55
[ "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
44,676
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import measure_theory.measurable_space_def import measure_theory.tactic /-! # Measurable spaces and measurable functions This file provides properties of measurable spaces and the functions and isomorphisms between them. The definition of a measurable space is in `measure_theory.measurable_space_def`. A measurable space is a set equipped with a σ-algebra, a collection of subsets closed under complementation and countable union. A function between measurable spaces is measurable if the preimage of each measurable subset is measurable. σ-algebras on a fixed set `α` form a complete lattice. Here we order σ-algebras by writing `m₁ ≤ m₂` if every set which is `m₁`-measurable is also `m₂`-measurable (that is, `m₁` is a subset of `m₂`). In particular, any collection of subsets of `α` generates a smallest σ-algebra which contains all of them. A function `f : α → β` induces a Galois connection between the lattices of σ-algebras on `α` and `β`. A measurable equivalence between measurable spaces is an equivalence which respects the σ-algebras, that is, for which both directions of the equivalence are measurable functions. We say that a filter `f` is measurably generated if every set `s ∈ f` includes a measurable set `t ∈ f`. This property is useful, e.g., to extract a measurable witness of `filter.eventually`. ## Notation * We write `α ≃ᵐ β` for measurable equivalences between the measurable spaces `α` and `β`. This should not be confused with `≃ₘ` which is used for diffeomorphisms between manifolds. ## Implementation notes Measurability of a function `f : α → β` between measurable spaces is defined in terms of the Galois connection induced by f. ## References * <https://en.wikipedia.org/wiki/Measurable_space> * <https://en.wikipedia.org/wiki/Sigma-algebra> * <https://en.wikipedia.org/wiki/Dynkin_system> ## Tags measurable space, σ-algebra, measurable function, measurable equivalence, dynkin system, π-λ theorem, π-system -/ open set encodable function equiv open_locale classical filter variables {α β γ δ δ' : Type*} {ι : Sort*} {s t u : set α} namespace measurable_space section functors variables {m m₁ m₂ : measurable_space α} {m' : measurable_space β} {f : α → β} {g : β → α} /-- The forward image of a measure space under a function. `map f m` contains the sets `s : set β` whose preimage under `f` is measurable. -/ protected def map (f : α → β) (m : measurable_space α) : measurable_space β := { measurable_set' := λ s, m.measurable_set' $ f ⁻¹' s, measurable_set_empty := m.measurable_set_empty, measurable_set_compl := assume s hs, m.measurable_set_compl _ hs, measurable_set_Union := assume f hf, by { rw preimage_Union, exact m.measurable_set_Union _ hf }} @[simp] lemma map_id : m.map id = m := measurable_space.ext $ assume s, iff.rfl @[simp] lemma map_comp {f : α → β} {g : β → γ} : (m.map f).map g = m.map (g ∘ f) := measurable_space.ext $ assume s, iff.rfl /-- The reverse image of a measure space under a function. `comap f m` contains the sets `s : set α` such that `s` is the `f`-preimage of a measurable set in `β`. -/ protected def comap (f : α → β) (m : measurable_space β) : measurable_space α := { measurable_set' := λ s, ∃s', m.measurable_set' s' ∧ f ⁻¹' s' = s, measurable_set_empty := ⟨∅, m.measurable_set_empty, rfl⟩, measurable_set_compl := assume s ⟨s', h₁, h₂⟩, ⟨s'ᶜ, m.measurable_set_compl _ h₁, h₂ ▸ rfl⟩, measurable_set_Union := assume s hs, let ⟨s', hs'⟩ := classical.axiom_of_choice hs in ⟨⋃ i, s' i, m.measurable_set_Union _ (λ i, (hs' i).left), by simp [hs'] ⟩ } @[simp] lemma comap_id : m.comap id = m := measurable_space.ext $ assume s, ⟨assume ⟨s', hs', h⟩, h ▸ hs', assume h, ⟨s, h, rfl⟩⟩ @[simp] lemma comap_comp {f : β → α} {g : γ → β} : (m.comap f).comap g = m.comap (f ∘ g) := measurable_space.ext $ assume s, ⟨assume ⟨t, ⟨u, h, hu⟩, ht⟩, ⟨u, h, ht ▸ hu ▸ rfl⟩, assume ⟨t, h, ht⟩, ⟨f ⁻¹' t, ⟨_, h, rfl⟩, ht⟩⟩ lemma comap_le_iff_le_map {f : α → β} : m'.comap f ≤ m ↔ m' ≤ m.map f := ⟨assume h s hs, h _ ⟨_, hs, rfl⟩, assume h s ⟨t, ht, heq⟩, heq ▸ h _ ht⟩ lemma gc_comap_map (f : α → β) : galois_connection (measurable_space.comap f) (measurable_space.map f) := assume f g, comap_le_iff_le_map lemma map_mono (h : m₁ ≤ m₂) : m₁.map f ≤ m₂.map f := (gc_comap_map f).monotone_u h lemma monotone_map : monotone (measurable_space.map f) := assume a b h, map_mono h lemma comap_mono (h : m₁ ≤ m₂) : m₁.comap g ≤ m₂.comap g := (gc_comap_map g).monotone_l h lemma monotone_comap : monotone (measurable_space.comap g) := assume a b h, comap_mono h @[simp] lemma comap_bot : (⊥ : measurable_space α).comap g = ⊥ := (gc_comap_map g).l_bot @[simp] lemma comap_sup : (m₁ ⊔ m₂).comap g = m₁.comap g ⊔ m₂.comap g := (gc_comap_map g).l_sup @[simp] lemma comap_supr {m : ι → measurable_space α} : (⨆i, m i).comap g = (⨆i, (m i).comap g) := (gc_comap_map g).l_supr @[simp] lemma map_top : (⊤ : measurable_space α).map f = ⊤ := (gc_comap_map f).u_top @[simp] lemma map_inf : (m₁ ⊓ m₂).map f = m₁.map f ⊓ m₂.map f := (gc_comap_map f).u_inf @[simp] lemma map_infi {m : ι → measurable_space α} : (⨅i, m i).map f = (⨅i, (m i).map f) := (gc_comap_map f).u_infi lemma comap_map_le : (m.map f).comap f ≤ m := (gc_comap_map f).l_u_le _ lemma le_map_comap : m ≤ (m.comap g).map g := (gc_comap_map g).le_u_l _ end functors lemma generate_from_le_generate_from {s t : set (set α)} (h : s ⊆ t) : generate_from s ≤ generate_from t := gi_generate_from.gc.monotone_l h lemma generate_from_sup_generate_from {s t : set (set α)} : generate_from s ⊔ generate_from t = generate_from (s ∪ t) := (@gi_generate_from α).gc.l_sup.symm lemma comap_generate_from {f : α → β} {s : set (set β)} : (generate_from s).comap f = generate_from (preimage f '' s) := le_antisymm (comap_le_iff_le_map.2 $ generate_from_le $ assume t hts, generate_measurable.basic _ $ mem_image_of_mem _ $ hts) (generate_from_le $ assume t ⟨u, hu, eq⟩, eq ▸ ⟨u, generate_measurable.basic _ hu, rfl⟩) end measurable_space section measurable_functions open measurable_space lemma measurable_iff_le_map {m₁ : measurable_space α} {m₂ : measurable_space β} {f : α → β} : measurable f ↔ m₂ ≤ m₁.map f := iff.rfl alias measurable_iff_le_map ↔ measurable.le_map measurable.of_le_map lemma measurable_iff_comap_le {m₁ : measurable_space α} {m₂ : measurable_space β} {f : α → β} : measurable f ↔ m₂.comap f ≤ m₁ := comap_le_iff_le_map.symm alias measurable_iff_comap_le ↔ measurable.comap_le measurable.of_comap_le lemma measurable.mono {ma ma' : measurable_space α} {mb mb' : measurable_space β} {f : α → β} (hf : @measurable α β ma mb f) (ha : ma ≤ ma') (hb : mb' ≤ mb) : @measurable α β ma' mb' f := λ t ht, ha _ $ hf $ hb _ ht @[measurability] lemma measurable_from_top [measurable_space β] {f : α → β} : @measurable _ _ ⊤ _ f := λ s hs, trivial lemma measurable_generate_from [measurable_space α] {s : set (set β)} {f : α → β} (h : ∀ t ∈ s, measurable_set (f ⁻¹' t)) : @measurable _ _ _ (generate_from s) f := measurable.of_le_map $ generate_from_le h variables [measurable_space α] [measurable_space β] [measurable_space γ] @[measurability] lemma measurable_set_preimage {f : α → β} {t : set β} (hf : measurable f) (ht : measurable_set t) : measurable_set (f ⁻¹' t) := hf ht @[measurability] lemma measurable.iterate {f : α → α} (hf : measurable f) : ∀ n, measurable (f^[n]) | 0 := measurable_id | (n+1) := (measurable.iterate n).comp hf @[nontriviality, measurability] lemma subsingleton.measurable [subsingleton α] {f : α → β} : measurable f := λ s hs, @subsingleton.measurable_set α _ _ _ @[measurability] lemma measurable.piecewise {s : set α} {_ : decidable_pred s} {f g : α → β} (hs : measurable_set s) (hf : measurable f) (hg : measurable g) : measurable (piecewise s f g) := begin intros t ht, rw piecewise_preimage, exact hs.ite (hf ht) (hg ht) end /-- this is slightly different from `measurable.piecewise`. It can be used to show `measurable (ite (x=0) 0 1)` by `exact measurable.ite (measurable_set_singleton 0) measurable_const measurable_const`, but replacing `measurable.ite` by `measurable.piecewise` in that example proof does not work. -/ lemma measurable.ite {p : α → Prop} {_ : decidable_pred p} {f g : α → β} (hp : measurable_set {a : α | p a}) (hf : measurable f) (hg : measurable g) : measurable (λ x, ite (p x) (f x) (g x)) := measurable.piecewise hp hf hg @[measurability] lemma measurable.indicator [has_zero β] {s : set α} {f : α → β} (hf : measurable f) (hs : measurable_set s) : measurable (s.indicator f) := hf.piecewise hs measurable_const @[to_additive] lemma measurable_one [has_one α] : measurable (1 : β → α) := @measurable_const _ _ _ _ 1 lemma measurable_of_not_nonempty (h : ¬ nonempty α) (f : α → β) : measurable f := begin assume s hs, convert measurable_set.empty, exact eq_empty_of_not_nonempty h _, end @[to_additive, measurability] lemma measurable_set_mul_support [has_one β] [measurable_singleton_class β] {f : α → β} (hf : measurable f) : measurable_set (mul_support f) := hf (measurable_set_singleton 1).compl attribute [measurability] measurable_set_support /-- If a function coincides with a measurable function outside of a countable set, it is measurable. -/ lemma measurable.measurable_of_countable_ne [measurable_singleton_class α] {f g : α → β} (hf : measurable f) (h : countable {x | f x ≠ g x}) : measurable g := begin assume t ht, have : g ⁻¹' t = (g ⁻¹' t ∩ {x | f x = g x}ᶜ) ∪ (g ⁻¹' t ∩ {x | f x = g x}), by simp [← inter_union_distrib_left], rw this, apply measurable_set.union (h.mono (inter_subset_right _ _)).measurable_set, have : g ⁻¹' t ∩ {x : α | f x = g x} = f ⁻¹' t ∩ {x : α | f x = g x}, by { ext x, simp {contextual := tt} }, rw this, exact (hf ht).inter h.measurable_set.of_compl, end lemma measurable_of_fintype [fintype α] [measurable_singleton_class α] (f : α → β) : measurable f := λ s hs, (finite.of_fintype (f ⁻¹' s)).measurable_set end measurable_functions section constructions variables [measurable_space α] [measurable_space β] [measurable_space γ] instance : measurable_space empty := ⊤ instance : measurable_space punit := ⊤ -- this also works for `unit` instance : measurable_space bool := ⊤ instance : measurable_space ℕ := ⊤ instance : measurable_space ℤ := ⊤ instance : measurable_space ℚ := ⊤ lemma measurable_to_encodable [encodable α] {f : β → α} (h : ∀ y, measurable_set (f ⁻¹' {f y})) : measurable f := begin assume s hs, rw [← bUnion_preimage_singleton], refine measurable_set.Union (λ y, measurable_set.Union_Prop $ λ hy, _), by_cases hyf : y ∈ range f, { rcases hyf with ⟨y, rfl⟩, apply h }, { simp only [preimage_singleton_eq_empty.2 hyf, measurable_set.empty] } end @[measurability] lemma measurable_unit (f : unit → α) : measurable f := measurable_from_top section nat @[measurability] lemma measurable_from_nat {f : ℕ → α} : measurable f := measurable_from_top lemma measurable_to_nat {f : α → ℕ} : (∀ y, measurable_set (f ⁻¹' {f y})) → measurable f := measurable_to_encodable lemma measurable_find_greatest' {p : α → ℕ → Prop} {N} (hN : ∀ k ≤ N, measurable_set {x | nat.find_greatest (p x) N = k}) : measurable (λ x, nat.find_greatest (p x) N) := measurable_to_nat $ λ x, hN _ nat.find_greatest_le lemma measurable_find_greatest {p : α → ℕ → Prop} {N} (hN : ∀ k ≤ N, measurable_set {x | p x k}) : measurable (λ x, nat.find_greatest (p x) N) := begin refine measurable_find_greatest' (λ k hk, _), simp only [nat.find_greatest_eq_iff, set_of_and, set_of_forall, ← compl_set_of], repeat { apply_rules [measurable_set.inter, measurable_set.const, measurable_set.Inter, measurable_set.Inter_Prop, measurable_set.compl, hN]; try { intros } } end lemma measurable_find {p : α → ℕ → Prop} (hp : ∀ x, ∃ N, p x N) (hm : ∀ k, measurable_set {x | p x k}) : measurable (λ x, nat.find (hp x)) := begin refine measurable_to_nat (λ x, _), simp only [set.preimage, mem_singleton_iff, nat.find_eq_iff, set_of_and, set_of_forall, ← compl_set_of], repeat { apply_rules [measurable_set.inter, hm, measurable_set.Inter, measurable_set.Inter_Prop, measurable_set.compl]; try { intros } } end end nat section subtype instance {α} {p : α → Prop} [m : measurable_space α] : measurable_space (subtype p) := m.comap (coe : _ → α) @[measurability] lemma measurable_subtype_coe {p : α → Prop} : measurable (coe : subtype p → α) := measurable_space.le_map_comap @[measurability] lemma measurable.subtype_coe {p : β → Prop} {f : α → subtype p} (hf : measurable f) : measurable (λ a : α, (f a : β)) := measurable_subtype_coe.comp hf @[measurability] lemma measurable.subtype_mk {p : β → Prop} {f : α → β} (hf : measurable f) {h : ∀ x, p (f x)} : measurable (λ x, (⟨f x, h x⟩ : subtype p)) := λ t ⟨s, hs⟩, hs.2 ▸ by simp only [← preimage_comp, (∘), subtype.coe_mk, hf hs.1] lemma measurable_set.subtype_image {s : set α} {t : set s} (hs : measurable_set s) : measurable_set t → measurable_set ((coe : s → α) '' t) | ⟨u, (hu : measurable_set u), (eq : coe ⁻¹' u = t)⟩ := begin rw [← eq, subtype.image_preimage_coe], exact hu.inter hs end lemma measurable_of_measurable_union_cover {f : α → β} (s t : set α) (hs : measurable_set s) (ht : measurable_set t) (h : univ ⊆ s ∪ t) (hc : measurable (λ a : s, f a)) (hd : measurable (λ a : t, f a)) : measurable f := begin intros u hu, convert (hs.subtype_image (hc hu)).union (ht.subtype_image (hd hu)), change f ⁻¹' u = coe '' (coe ⁻¹' (f ⁻¹' u) : set s) ∪ coe '' (coe ⁻¹' (f ⁻¹' u) : set t), rw [image_preimage_eq_inter_range, image_preimage_eq_inter_range, subtype.range_coe, subtype.range_coe, ← inter_distrib_left, univ_subset_iff.1 h, inter_univ], end instance {α} {p : α → Prop} [measurable_space α] [measurable_singleton_class α] : measurable_singleton_class (subtype p) := { measurable_set_singleton := λ x, begin have : measurable_set {(x : α)} := measurable_set_singleton _, convert @measurable_subtype_coe α _ p _ this, ext y, simp [subtype.ext_iff], end } lemma measurable_of_measurable_on_compl_finite [measurable_singleton_class α] {f : α → β} (s : set α) (hs : finite s) (hf : measurable (set.restrict f sᶜ)) : measurable f := begin letI : fintype s := finite.fintype hs, exact measurable_of_measurable_union_cover s sᶜ hs.measurable_set hs.measurable_set.compl (by simp only [union_compl_self]) (measurable_of_fintype _) hf end lemma measurable_of_measurable_on_compl_singleton [measurable_singleton_class α] {f : α → β} (a : α) (hf : measurable (set.restrict f {x | x ≠ a})) : measurable f := measurable_of_measurable_on_compl_finite {a} (finite_singleton a) hf end subtype section prod instance {α β} [m₁ : measurable_space α] [m₂ : measurable_space β] : measurable_space (α × β) := m₁.comap prod.fst ⊔ m₂.comap prod.snd @[measurability] lemma measurable_fst : measurable (prod.fst : α × β → α) := measurable.of_comap_le le_sup_left lemma measurable.fst {f : α → β × γ} (hf : measurable f) : measurable (λ a : α, (f a).1) := measurable_fst.comp hf @[measurability] lemma measurable_snd : measurable (prod.snd : α × β → β) := measurable.of_comap_le le_sup_right lemma measurable.snd {f : α → β × γ} (hf : measurable f) : measurable (λ a : α, (f a).2) := measurable_snd.comp hf @[measurability] lemma measurable.prod {f : α → β × γ} (hf₁ : measurable (λ a, (f a).1)) (hf₂ : measurable (λ a, (f a).2)) : measurable f := measurable.of_le_map $ sup_le (by { rw [measurable_space.comap_le_iff_le_map, measurable_space.map_comp], exact hf₁ }) (by { rw [measurable_space.comap_le_iff_le_map, measurable_space.map_comp], exact hf₂ }) lemma measurable_prod {f : α → β × γ} : measurable f ↔ measurable (λ a, (f a).1) ∧ measurable (λ a, (f a).2) := ⟨λ hf, ⟨measurable_fst.comp hf, measurable_snd.comp hf⟩, λ h, measurable.prod h.1 h.2⟩ lemma measurable.prod_mk {f : α → β} {g : α → γ} (hf : measurable f) (hg : measurable g) : measurable (λ a : α, (f a, g a)) := measurable.prod hf hg lemma measurable_prod_mk_left {x : α} : measurable (@prod.mk _ β x) := measurable_const.prod_mk measurable_id lemma measurable_prod_mk_right {y : β} : measurable (λ x : α, (x, y)) := measurable_id.prod_mk measurable_const lemma measurable.prod_map [measurable_space δ] {f : α → β} {g : γ → δ} (hf : measurable f) (hg : measurable g) : measurable (prod.map f g) := (hf.comp measurable_fst).prod_mk (hg.comp measurable_snd) lemma measurable.of_uncurry_left {f : α → β → γ} (hf : measurable (uncurry f)) {x : α} : measurable (f x) := hf.comp measurable_prod_mk_left lemma measurable.of_uncurry_right {f : α → β → γ} (hf : measurable (uncurry f)) {y : β} : measurable (λ x, f x y) := hf.comp measurable_prod_mk_right @[measurability] lemma measurable_swap : measurable (prod.swap : α × β → β × α) := measurable.prod measurable_snd measurable_fst lemma measurable_swap_iff {f : α × β → γ} : measurable (f ∘ prod.swap) ↔ measurable f := ⟨λ hf, by { convert hf.comp measurable_swap, ext ⟨x, y⟩, refl }, λ hf, hf.comp measurable_swap⟩ @[measurability] lemma measurable_set.prod {s : set α} {t : set β} (hs : measurable_set s) (ht : measurable_set t) : measurable_set (s.prod t) := measurable_set.inter (measurable_fst hs) (measurable_snd ht) lemma measurable_set_prod_of_nonempty {s : set α} {t : set β} (h : (s.prod t).nonempty) : measurable_set (s.prod t) ↔ measurable_set s ∧ measurable_set t := begin rcases h with ⟨⟨x, y⟩, hx, hy⟩, refine ⟨λ hst, _, λ h, h.1.prod h.2⟩, have : measurable_set ((λ x, (x, y)) ⁻¹' s.prod t) := measurable_id.prod_mk measurable_const hst, have : measurable_set (prod.mk x ⁻¹' s.prod t) := measurable_const.prod_mk measurable_id hst, simp * at * end lemma measurable_set_prod {s : set α} {t : set β} : measurable_set (s.prod t) ↔ (measurable_set s ∧ measurable_set t) ∨ s = ∅ ∨ t = ∅ := begin cases (s.prod t).eq_empty_or_nonempty with h h, { simp [h, prod_eq_empty_iff.mp h] }, { simp [←not_nonempty_iff_eq_empty, prod_nonempty_iff.mp h, measurable_set_prod_of_nonempty h] } end lemma measurable_set_swap_iff {s : set (α × β)} : measurable_set (prod.swap ⁻¹' s) ↔ measurable_set s := ⟨λ hs, by { convert measurable_swap hs, ext ⟨x, y⟩, refl }, λ hs, measurable_swap hs⟩ lemma measurable_from_prod_encodable [encodable β] [measurable_singleton_class β] {f : α × β → γ} (hf : ∀ y, measurable (λ x, f (x, y))) : measurable f := begin intros s hs, have : f ⁻¹' s = ⋃ y, ((λ x, f (x, y)) ⁻¹' s).prod {y}, { ext1 ⟨x, y⟩, simp [and_assoc, and.left_comm] }, rw this, exact measurable_set.Union (λ y, (hf y hs).prod (measurable_set_singleton y)) end end prod section pi variables {π : δ → Type*} instance measurable_space.pi [m : Π a, measurable_space (π a)] : measurable_space (Π a, π a) := ⨆ a, (m a).comap (λ b, b a) variables [Π a, measurable_space (π a)] [measurable_space γ] lemma measurable_pi_iff {g : α → Π a, π a} : measurable g ↔ ∀ a, measurable (λ x, g x a) := by simp_rw [measurable_iff_comap_le, measurable_space.pi, measurable_space.comap_supr, measurable_space.comap_comp, function.comp, supr_le_iff] @[measurability] lemma measurable_pi_apply (a : δ) : measurable (λ f : Π a, π a, f a) := measurable.of_comap_le $ le_supr _ a @[measurability] lemma measurable.eval {a : δ} {g : α → Π a, π a} (hg : measurable g) : measurable (λ x, g x a) := (measurable_pi_apply a).comp hg @[measurability] lemma measurable_pi_lambda (f : α → Π a, π a) (hf : ∀ a, measurable (λ c, f c a)) : measurable f := measurable_pi_iff.mpr hf /-- The function `update f a : π a → Π a, π a` is always measurable. This doesn't require `f` to be measurable. This should not be confused with the statement that `update f a x` is measurable. -/ @[measurability] lemma measurable_update (f : Π (a : δ), π a) {a : δ} : measurable (update f a) := begin apply measurable_pi_lambda, intro x, by_cases hx : x = a, { cases hx, convert measurable_id, ext, simp }, simp_rw [update_noteq hx], apply measurable_const, end /- Even though we cannot use projection notation, we still keep a dot to be consistent with similar lemmas, like `measurable_set.prod`. -/ @[measurability] lemma measurable_set.pi {s : set δ} {t : Π i : δ, set (π i)} (hs : countable s) (ht : ∀ i ∈ s, measurable_set (t i)) : measurable_set (s.pi t) := by { rw [pi_def], exact measurable_set.bInter hs (λ i hi, measurable_pi_apply _ (ht i hi)) } lemma measurable_set.univ_pi [encodable δ] {t : Π i : δ, set (π i)} (ht : ∀ i, measurable_set (t i)) : measurable_set (pi univ t) := measurable_set.pi (countable_encodable _) (λ i _, ht i) lemma measurable_set_pi_of_nonempty {s : set δ} {t : Π i, set (π i)} (hs : countable s) (h : (pi s t).nonempty) : measurable_set (pi s t) ↔ ∀ i ∈ s, measurable_set (t i) := begin rcases h with ⟨f, hf⟩, refine ⟨λ hst i hi, _, measurable_set.pi hs⟩, convert measurable_update f hst, rw [update_preimage_pi hi], exact λ j hj _, hf j hj end lemma measurable_set_pi {s : set δ} {t : Π i, set (π i)} (hs : countable s) : measurable_set (pi s t) ↔ (∀ i ∈ s, measurable_set (t i)) ∨ pi s t = ∅ := begin cases (pi s t).eq_empty_or_nonempty with h h, { simp [h] }, { simp [measurable_set_pi_of_nonempty hs, h, ← not_nonempty_iff_eq_empty] } end section fintype local attribute [instance] fintype.encodable lemma measurable_set.pi_fintype [fintype δ] {s : set δ} {t : Π i, set (π i)} (ht : ∀ i ∈ s, measurable_set (t i)) : measurable_set (pi s t) := measurable_set.pi (countable_encodable _) ht lemma measurable_set.univ_pi_fintype [fintype δ] {t : Π i, set (π i)} (ht : ∀ i, measurable_set (t i)) : measurable_set (pi univ t) := measurable_set.pi_fintype (λ i _, ht i) end fintype end pi instance tprod.measurable_space (π : δ → Type*) [∀ x, measurable_space (π x)] : ∀ (l : list δ), measurable_space (list.tprod π l) | [] := punit.measurable_space | (i :: is) := @prod.measurable_space _ _ _ (tprod.measurable_space is) section tprod open list variables {π : δ → Type*} [∀ x, measurable_space (π x)] lemma measurable_tprod_mk (l : list δ) : measurable (@tprod.mk δ π l) := begin induction l with i l ih, { exact measurable_const }, { exact (measurable_pi_apply i).prod_mk ih } end lemma measurable_tprod_elim : ∀ {l : list δ} {i : δ} (hi : i ∈ l), measurable (λ (v : tprod π l), v.elim hi) | (i :: is) j hj := begin by_cases hji : j = i, { subst hji, simp [measurable_fst] }, { rw [funext $ tprod.elim_of_ne _ hji], exact (measurable_tprod_elim (hj.resolve_left hji)).comp measurable_snd } end lemma measurable_tprod_elim' {l : list δ} (h : ∀ i, i ∈ l) : measurable (tprod.elim' h : tprod π l → Π i, π i) := measurable_pi_lambda _ (λ i, measurable_tprod_elim (h i)) lemma measurable_set.tprod (l : list δ) {s : ∀ i, set (π i)} (hs : ∀ i, measurable_set (s i)) : measurable_set (set.tprod l s) := by { induction l with i l ih, exact measurable_set.univ, exact (hs i).prod ih } end tprod instance {α β} [m₁ : measurable_space α] [m₂ : measurable_space β] : measurable_space (α ⊕ β) := m₁.map sum.inl ⊓ m₂.map sum.inr section sum @[measurability] lemma measurable_inl : measurable (@sum.inl α β) := measurable.of_le_map inf_le_left @[measurability] lemma measurable_inr : measurable (@sum.inr α β) := measurable.of_le_map inf_le_right lemma measurable_sum {f : α ⊕ β → γ} (hl : measurable (f ∘ sum.inl)) (hr : measurable (f ∘ sum.inr)) : measurable f := measurable.of_comap_le $ le_inf (measurable_space.comap_le_iff_le_map.2 $ hl) (measurable_space.comap_le_iff_le_map.2 $ hr) @[measurability] lemma measurable.sum_elim {f : α → γ} {g : β → γ} (hf : measurable f) (hg : measurable g) : measurable (sum.elim f g) := measurable_sum hf hg lemma measurable_set.inl_image {s : set α} (hs : measurable_set s) : measurable_set (sum.inl '' s : set (α ⊕ β)) := ⟨show measurable_set (sum.inl ⁻¹' _), by { rwa [preimage_image_eq], exact (λ a b, sum.inl.inj) }, have sum.inr ⁻¹' (sum.inl '' s : set (α ⊕ β)) = ∅ := eq_empty_of_subset_empty $ assume x ⟨y, hy, eq⟩, by contradiction, show measurable_set (sum.inr ⁻¹' _), by { rw [this], exact measurable_set.empty }⟩ lemma measurable_set_range_inl : measurable_set (range sum.inl : set (α ⊕ β)) := by { rw [← image_univ], exact measurable_set.univ.inl_image } lemma measurable_set_inr_image {s : set β} (hs : measurable_set s) : measurable_set (sum.inr '' s : set (α ⊕ β)) := ⟨ have sum.inl ⁻¹' (sum.inr '' s : set (α ⊕ β)) = ∅ := eq_empty_of_subset_empty $ assume x ⟨y, hy, eq⟩, by contradiction, show measurable_set (sum.inl ⁻¹' _), by { rw [this], exact measurable_set.empty }, show measurable_set (sum.inr ⁻¹' _), by { rwa [preimage_image_eq], exact λ a b, sum.inr.inj }⟩ lemma measurable_set_range_inr : measurable_set (range sum.inr : set (α ⊕ β)) := by { rw [← image_univ], exact measurable_set_inr_image measurable_set.univ } end sum instance {α} {β : α → Type*} [m : Πa, measurable_space (β a)] : measurable_space (sigma β) := ⨅a, (m a).map (sigma.mk a) end constructions /-- Equivalences between measurable spaces. Main application is the simplification of measurability statements along measurable equivalences. -/ structure measurable_equiv (α β : Type*) [measurable_space α] [measurable_space β] extends α ≃ β := (measurable_to_fun : measurable to_fun) (measurable_inv_fun : measurable inv_fun) infix ` ≃ᵐ `:25 := measurable_equiv namespace measurable_equiv variables (α β) [measurable_space α] [measurable_space β] [measurable_space γ] [measurable_space δ] instance : has_coe_to_fun (α ≃ᵐ β) := ⟨λ _, α → β, λ e, e.to_equiv⟩ variables {α β} lemma coe_eq (e : α ≃ᵐ β) : (e : α → β) = e.to_equiv := rfl @[measurability] protected lemma measurable (e : α ≃ᵐ β) : measurable (e : α → β) := e.measurable_to_fun @[simp] lemma coe_mk (e : α ≃ β) (h1 : measurable e) (h2 : measurable e.symm) : ((⟨e, h1, h2⟩ : α ≃ᵐ β) : α → β) = e := rfl /-- Any measurable space is equivalent to itself. -/ def refl (α : Type*) [measurable_space α] : α ≃ᵐ α := { to_equiv := equiv.refl α, measurable_to_fun := measurable_id, measurable_inv_fun := measurable_id } instance : inhabited (α ≃ᵐ α) := ⟨refl α⟩ /-- The composition of equivalences between measurable spaces. -/ @[simps] def trans (ab : α ≃ᵐ β) (bc : β ≃ᵐ γ) : α ≃ᵐ γ := { to_equiv := ab.to_equiv.trans bc.to_equiv, measurable_to_fun := bc.measurable_to_fun.comp ab.measurable_to_fun, measurable_inv_fun := ab.measurable_inv_fun.comp bc.measurable_inv_fun } /-- The inverse of an equivalence between measurable spaces. -/ @[simps] def symm (ab : α ≃ᵐ β) : β ≃ᵐ α := { to_equiv := ab.to_equiv.symm, measurable_to_fun := ab.measurable_inv_fun, measurable_inv_fun := ab.measurable_to_fun } @[simp] lemma coe_symm_mk (e : α ≃ β) (h1 : measurable e) (h2 : measurable e.symm) : ((⟨e, h1, h2⟩ : α ≃ᵐ β).symm : β → α) = e.symm := rfl @[simp] theorem symm_comp_self (e : α ≃ᵐ β) : e.symm ∘ e = id := funext e.left_inv @[simp] theorem self_comp_symm (e : α ≃ᵐ β) : e ∘ e.symm = id := funext e.right_inv /-- Equal measurable spaces are equivalent. -/ protected def cast {α β} [i₁ : measurable_space α] [i₂ : measurable_space β] (h : α = β) (hi : i₁ == i₂) : α ≃ᵐ β := { to_equiv := equiv.cast h, measurable_to_fun := by { substI h, substI hi, exact measurable_id }, measurable_inv_fun := by { substI h, substI hi, exact measurable_id }} protected lemma measurable_coe_iff {f : β → γ} (e : α ≃ᵐ β) : measurable (f ∘ e) ↔ measurable f := iff.intro (assume hfe, have measurable (f ∘ (e.symm.trans e).to_equiv) := hfe.comp e.symm.measurable, by rwa [trans_to_equiv, symm_to_equiv, equiv.symm_trans] at this) (λ h, h.comp e.measurable) /-- Products of equivalent measurable spaces are equivalent. -/ def prod_congr (ab : α ≃ᵐ β) (cd : γ ≃ᵐ δ) : α × γ ≃ᵐ β × δ := { to_equiv := prod_congr ab.to_equiv cd.to_equiv, measurable_to_fun := (ab.measurable_to_fun.comp measurable_id.fst).prod_mk (cd.measurable_to_fun.comp measurable_id.snd), measurable_inv_fun := (ab.measurable_inv_fun.comp measurable_id.fst).prod_mk (cd.measurable_inv_fun.comp measurable_id.snd) } /-- Products of measurable spaces are symmetric. -/ def prod_comm : α × β ≃ᵐ β × α := { to_equiv := prod_comm α β, measurable_to_fun := measurable_id.snd.prod_mk measurable_id.fst, measurable_inv_fun := measurable_id.snd.prod_mk measurable_id.fst } /-- Products of measurable spaces are associative. -/ def prod_assoc : (α × β) × γ ≃ᵐ α × (β × γ) := { to_equiv := prod_assoc α β γ, measurable_to_fun := measurable_fst.fst.prod_mk $ measurable_fst.snd.prod_mk measurable_snd, measurable_inv_fun := (measurable_fst.prod_mk measurable_snd.fst).prod_mk measurable_snd.snd } /-- Sums of measurable spaces are symmetric. -/ def sum_congr (ab : α ≃ᵐ β) (cd : γ ≃ᵐ δ) : α ⊕ γ ≃ᵐ β ⊕ δ := { to_equiv := sum_congr ab.to_equiv cd.to_equiv, measurable_to_fun := begin cases ab with ab' abm, cases ab', cases cd with cd' cdm, cases cd', refine measurable_sum (measurable_inl.comp abm) (measurable_inr.comp cdm) end, measurable_inv_fun := begin cases ab with ab' _ abm, cases ab', cases cd with cd' _ cdm, cases cd', refine measurable_sum (measurable_inl.comp abm) (measurable_inr.comp cdm) end } /-- `set.prod s t ≃ (s × t)` as measurable spaces. -/ def set.prod (s : set α) (t : set β) : s.prod t ≃ᵐ s × t := { to_equiv := equiv.set.prod s t, measurable_to_fun := measurable_id.subtype_coe.fst.subtype_mk.prod_mk measurable_id.subtype_coe.snd.subtype_mk, measurable_inv_fun := measurable.subtype_mk $ measurable_id.fst.subtype_coe.prod_mk measurable_id.snd.subtype_coe } /-- `univ α ≃ α` as measurable spaces. -/ def set.univ (α : Type*) [measurable_space α] : (univ : set α) ≃ᵐ α := { to_equiv := equiv.set.univ α, measurable_to_fun := measurable_id.subtype_coe, measurable_inv_fun := measurable_id.subtype_mk } /-- `{a} ≃ unit` as measurable spaces. -/ def set.singleton (a : α) : ({a} : set α) ≃ᵐ unit := { to_equiv := equiv.set.singleton a, measurable_to_fun := measurable_const, measurable_inv_fun := measurable_const } /-- A set is equivalent to its image under a function `f` as measurable spaces, if `f` is an injective measurable function that sends measurable sets to measurable sets. -/ noncomputable def set.image (f : α → β) (s : set α) (hf : injective f) (hfm : measurable f) (hfi : ∀ s, measurable_set s → measurable_set (f '' s)) : s ≃ᵐ (f '' s) := { to_equiv := equiv.set.image f s hf, measurable_to_fun := (hfm.comp measurable_id.subtype_coe).subtype_mk, measurable_inv_fun := begin rintro t ⟨u, hu, rfl⟩, simp [preimage_preimage, set.image_symm_preimage hf], exact measurable_subtype_coe (hfi u hu) end } /-- The domain of `f` is equivalent to its range as measurable spaces, if `f` is an injective measurable function that sends measurable sets to measurable sets. -/ noncomputable def set.range (f : α → β) (hf : injective f) (hfm : measurable f) (hfi : ∀ s, measurable_set s → measurable_set (f '' s)) : α ≃ᵐ (range f) := (measurable_equiv.set.univ _).symm.trans $ (measurable_equiv.set.image f univ hf hfm hfi).trans $ measurable_equiv.cast (by rw image_univ) (by rw image_univ) /-- `α` is equivalent to its image in `α ⊕ β` as measurable spaces. -/ def set.range_inl : (range sum.inl : set (α ⊕ β)) ≃ᵐ α := { to_fun := λ ab, match ab with | ⟨sum.inl a, _⟩ := a | ⟨sum.inr b, p⟩ := have false, by { cases p, contradiction }, this.elim end, inv_fun := λ a, ⟨sum.inl a, a, rfl⟩, left_inv := by { rintro ⟨ab, a, rfl⟩, refl }, right_inv := assume a, rfl, measurable_to_fun := assume s (hs : measurable_set s), begin refine ⟨_, hs.inl_image, set.ext _⟩, rintros ⟨ab, a, rfl⟩, simp [set.range_inl._match_1] end, measurable_inv_fun := measurable.subtype_mk measurable_inl } /-- `β` is equivalent to its image in `α ⊕ β` as measurable spaces. -/ def set.range_inr : (range sum.inr : set (α ⊕ β)) ≃ᵐ β := { to_fun := λ ab, match ab with | ⟨sum.inr b, _⟩ := b | ⟨sum.inl a, p⟩ := have false, by { cases p, contradiction }, this.elim end, inv_fun := λ b, ⟨sum.inr b, b, rfl⟩, left_inv := by { rintro ⟨ab, b, rfl⟩, refl }, right_inv := assume b, rfl, measurable_to_fun := assume s (hs : measurable_set s), begin refine ⟨_, measurable_set_inr_image hs, set.ext _⟩, rintros ⟨ab, b, rfl⟩, simp [set.range_inr._match_1] end, measurable_inv_fun := measurable.subtype_mk measurable_inr } /-- Products distribute over sums (on the right) as measurable spaces. -/ def sum_prod_distrib (α β γ) [measurable_space α] [measurable_space β] [measurable_space γ] : (α ⊕ β) × γ ≃ᵐ (α × γ) ⊕ (β × γ) := { to_equiv := sum_prod_distrib α β γ, measurable_to_fun := begin refine measurable_of_measurable_union_cover ((range sum.inl).prod univ) ((range sum.inr).prod univ) (measurable_set_range_inl.prod measurable_set.univ) (measurable_set_range_inr.prod measurable_set.univ) (by { rintro ⟨a|b, c⟩; simp [set.prod_eq] }) _ _, { refine (set.prod (range sum.inl) univ).symm.measurable_coe_iff.1 _, refine (prod_congr set.range_inl (set.univ _)).symm.measurable_coe_iff.1 _, dsimp [(∘)], convert measurable_inl, ext ⟨a, c⟩, refl }, { refine (set.prod (range sum.inr) univ).symm.measurable_coe_iff.1 _, refine (prod_congr set.range_inr (set.univ _)).symm.measurable_coe_iff.1 _, dsimp [(∘)], convert measurable_inr, ext ⟨b, c⟩, refl } end, measurable_inv_fun := measurable_sum ((measurable_inl.comp measurable_fst).prod_mk measurable_snd) ((measurable_inr.comp measurable_fst).prod_mk measurable_snd) } /-- Products distribute over sums (on the left) as measurable spaces. -/ def prod_sum_distrib (α β γ) [measurable_space α] [measurable_space β] [measurable_space γ] : α × (β ⊕ γ) ≃ᵐ (α × β) ⊕ (α × γ) := prod_comm.trans $ (sum_prod_distrib _ _ _).trans $ sum_congr prod_comm prod_comm /-- Products distribute over sums as measurable spaces. -/ def sum_prod_sum (α β γ δ) [measurable_space α] [measurable_space β] [measurable_space γ] [measurable_space δ] : (α ⊕ β) × (γ ⊕ δ) ≃ᵐ ((α × γ) ⊕ (α × δ)) ⊕ ((β × γ) ⊕ (β × δ)) := (sum_prod_distrib _ _ _).trans $ sum_congr (prod_sum_distrib _ _ _) (prod_sum_distrib _ _ _) variables {π π' : δ' → Type*} [∀ x, measurable_space (π x)] [∀ x, measurable_space (π' x)] /-- A family of measurable equivalences `Π a, β₁ a ≃ᵐ β₂ a` generates a measurable equivalence between `Π a, β₁ a` and `Π a, β₂ a`. -/ def Pi_congr_right (e : Π a, π a ≃ᵐ π' a) : (Π a, π a) ≃ᵐ (Π a, π' a) := { to_equiv := Pi_congr_right (λ a, (e a).to_equiv), measurable_to_fun := measurable_pi_lambda _ (λ i, (e i).measurable_to_fun.comp (measurable_pi_apply i)), measurable_inv_fun := measurable_pi_lambda _ (λ i, (e i).measurable_inv_fun.comp (measurable_pi_apply i)) } /-- Pi-types are measurably equivalent to iterated products. -/ noncomputable def pi_measurable_equiv_tprod {l : list δ'} (hnd : l.nodup) (h : ∀ i, i ∈ l) : (Π i, π i) ≃ᵐ list.tprod π l := { to_equiv := list.tprod.pi_equiv_tprod hnd h, measurable_to_fun := measurable_tprod_mk l, measurable_inv_fun := measurable_tprod_elim' h } end measurable_equiv namespace filter variables [measurable_space α] /-- A filter `f` is measurably generates if each `s ∈ f` includes a measurable `t ∈ f`. -/ class is_measurably_generated (f : filter α) : Prop := (exists_measurable_subset : ∀ ⦃s⦄, s ∈ f → ∃ t ∈ f, measurable_set t ∧ t ⊆ s) instance is_measurably_generated_bot : is_measurably_generated (⊥ : filter α) := ⟨λ _ _, ⟨∅, mem_bot_sets, measurable_set.empty, empty_subset _⟩⟩ instance is_measurably_generated_top : is_measurably_generated (⊤ : filter α) := ⟨λ s hs, ⟨univ, univ_mem_sets, measurable_set.univ, λ x _, hs x⟩⟩ lemma eventually.exists_measurable_mem {f : filter α} [is_measurably_generated f] {p : α → Prop} (h : ∀ᶠ x in f, p x) : ∃ s ∈ f, measurable_set s ∧ ∀ x ∈ s, p x := is_measurably_generated.exists_measurable_subset h lemma eventually.exists_measurable_mem_of_lift' {f : filter α} [is_measurably_generated f] {p : set α → Prop} (h : ∀ᶠ s in f.lift' powerset, p s) : ∃ s ∈ f, measurable_set s ∧ p s := let ⟨s, hsf, hs⟩ := eventually_lift'_powerset.1 h, ⟨t, htf, htm, hts⟩ := is_measurably_generated.exists_measurable_subset hsf in ⟨t, htf, htm, hs t hts⟩ instance inf_is_measurably_generated (f g : filter α) [is_measurably_generated f] [is_measurably_generated g] : is_measurably_generated (f ⊓ g) := begin refine ⟨_⟩, rintros t ⟨sf, hsf, sg, hsg, ht⟩, rcases is_measurably_generated.exists_measurable_subset hsf with ⟨s'f, hs'f, hmf, hs'sf⟩, rcases is_measurably_generated.exists_measurable_subset hsg with ⟨s'g, hs'g, hmg, hs'sg⟩, refine ⟨s'f ∩ s'g, inter_mem_inf_sets hs'f hs'g, hmf.inter hmg, _⟩, exact subset.trans (inter_subset_inter hs'sf hs'sg) ht end lemma principal_is_measurably_generated_iff {s : set α} : is_measurably_generated (𝓟 s) ↔ measurable_set s := begin refine ⟨_, λ hs, ⟨λ t ht, ⟨s, mem_principal_self s, hs, ht⟩⟩⟩, rintros ⟨hs⟩, rcases hs (mem_principal_self s) with ⟨t, ht, htm, hts⟩, have : t = s := subset.antisymm hts ht, rwa ← this end alias principal_is_measurably_generated_iff ↔ _ measurable_set.principal_is_measurably_generated instance infi_is_measurably_generated {f : ι → filter α} [∀ i, is_measurably_generated (f i)] : is_measurably_generated (⨅ i, f i) := begin refine ⟨λ s hs, _⟩, rw [← equiv.plift.surjective.infi_comp, mem_infi_iff] at hs, rcases hs with ⟨t, ht, ⟨V, hVf, hVs⟩⟩, choose U hUf hU using λ i, is_measurably_generated.exists_measurable_subset (hVf i), refine ⟨⋂ i : t, U i, _, _, _⟩, { rw [← equiv.plift.surjective.infi_comp, mem_infi_iff], refine ⟨t, ht, U, hUf, subset.refl _⟩ }, { haveI := ht.countable.to_encodable, refine measurable_set.Inter (λ i, (hU i).1) }, { exact subset.trans (Inter_subset_Inter $ λ i, (hU i).2) hVs } end end filter /-- We say that a collection of sets is countably spanning if a countable subset spans the whole type. This is a useful condition in various parts of measure theory. For example, it is a needed condition to show that the product of two collections generate the product sigma algebra, see `generate_from_prod_eq`. -/ def is_countably_spanning (C : set (set α)) : Prop := ∃ (s : ℕ → set α), (∀ n, s n ∈ C) ∧ (⋃ n, s n) = univ lemma is_countably_spanning_measurable_set [measurable_space α] : is_countably_spanning {s : set α | measurable_set s} := ⟨λ _, univ, λ _, measurable_set.univ, Union_const _⟩ namespace measurable_set /-! ### Typeclasses on `subtype measurable_set` -/ variables [measurable_space α] instance : has_mem α (subtype (measurable_set : set α → Prop)) := ⟨λ a s, a ∈ (s : set α)⟩ @[simp] lemma mem_coe (a : α) (s : subtype (measurable_set : set α → Prop)) : a ∈ (s : set α) ↔ a ∈ s := iff.rfl instance : has_emptyc (subtype (measurable_set : set α → Prop)) := ⟨⟨∅, measurable_set.empty⟩⟩ @[simp] lemma coe_empty : ↑(∅ : subtype (measurable_set : set α → Prop)) = (∅ : set α) := rfl instance [measurable_singleton_class α] : has_insert α (subtype (measurable_set : set α → Prop)) := ⟨λ a s, ⟨has_insert.insert a s, s.prop.insert a⟩⟩ @[simp] lemma coe_insert [measurable_singleton_class α] (a : α) (s : subtype (measurable_set : set α → Prop)) : ↑(has_insert.insert a s) = (has_insert.insert a s : set α) := rfl instance : has_compl (subtype (measurable_set : set α → Prop)) := ⟨λ x, ⟨xᶜ, x.prop.compl⟩⟩ @[simp] lemma coe_compl (s : subtype (measurable_set : set α → Prop)) : ↑(sᶜ) = (sᶜ : set α) := rfl instance : has_union (subtype (measurable_set : set α → Prop)) := ⟨λ x y, ⟨x ∪ y, x.prop.union y.prop⟩⟩ @[simp] lemma coe_union (s t : subtype (measurable_set : set α → Prop)) : ↑(s ∪ t) = (s ∪ t : set α) := rfl instance : has_inter (subtype (measurable_set : set α → Prop)) := ⟨λ x y, ⟨x ∩ y, x.prop.inter y.prop⟩⟩ @[simp] lemma coe_inter (s t : subtype (measurable_set : set α → Prop)) : ↑(s ∩ t) = (s ∩ t : set α) := rfl instance : has_sdiff (subtype (measurable_set : set α → Prop)) := ⟨λ x y, ⟨x \ y, x.prop.diff y.prop⟩⟩ @[simp] lemma coe_sdiff (s t : subtype (measurable_set : set α → Prop)) : ↑(s \ t) = (s \ t : set α) := rfl instance : has_bot (subtype (measurable_set : set α → Prop)) := ⟨⟨⊥, measurable_set.empty⟩⟩ @[simp] lemma coe_bot : ↑(⊥ : subtype (measurable_set : set α → Prop)) = (⊥ : set α) := rfl instance : has_top (subtype (measurable_set : set α → Prop)) := ⟨⟨⊤, measurable_set.univ⟩⟩ @[simp] lemma coe_top : ↑(⊤ : subtype (measurable_set : set α → Prop)) = (⊤ : set α) := rfl instance : partial_order (subtype (measurable_set : set α → Prop)) := partial_order.lift _ subtype.coe_injective instance : bounded_distrib_lattice (subtype (measurable_set : set α → Prop)) := { sup := (∪), le_sup_left := λ a b, show (a : set α) ≤ a ⊔ b, from le_sup_left, le_sup_right := λ a b, show (b : set α) ≤ a ⊔ b, from le_sup_right, sup_le := λ a b c ha hb, show (a ⊔ b : set α) ≤ c, from sup_le ha hb, inf := (∩), inf_le_left := λ a b, show (a ⊓ b : set α) ≤ a, from inf_le_left, inf_le_right := λ a b, show (a ⊓ b : set α) ≤ b, from inf_le_right, le_inf := λ a b c ha hb, show (a : set α) ≤ b ⊓ c, from le_inf ha hb, top := ⊤, le_top := λ a, show (a : set α) ≤ ⊤, from le_top, bot := ⊥, bot_le := λ a, show (⊥ : set α) ≤ a, from bot_le, le_sup_inf := λ x y z, show ((x ⊔ y) ⊓ (x ⊔ z) : set α) ≤ x ⊔ y ⊓ z, from le_sup_inf, .. measurable_set.subtype.partial_order } instance : boolean_algebra (subtype (measurable_set : set α → Prop)) := { sdiff := (\), sup_inf_sdiff := λ a b, subtype.eq $ sup_inf_sdiff a b, inf_inf_sdiff := λ a b, subtype.eq $ inf_inf_sdiff a b, compl := has_compl.compl, inf_compl_le_bot := λ a, boolean_algebra.inf_compl_le_bot (a : set α), top_le_sup_compl := λ a, boolean_algebra.top_le_sup_compl (a : set α), sdiff_eq := λ a b, subtype.eq $ sdiff_eq, .. measurable_set.subtype.bounded_distrib_lattice } end measurable_set
d8c02d68683bbcb24005e2774fec1f99b69aa5da
947b78d97130d56365ae2ec264df196ce769371a
/stage0/src/Lean/Data/Lsp/Capabilities.lean
584599ab0f59e5bb20043630da8ce3a6701f318a
[ "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
991
lean
/- Copyright (c) 2020 Marc Huisinga. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Marc Huisinga, Wojciech Nawrocki -/ import Lean.Data.JsonRpc import Lean.Data.Lsp.TextSync /-! Minimal LSP servers/clients do not have to implement a lot of functionality. Most useful additional behaviour is instead opted into via capabilities. -/ namespace Lean namespace Lsp open Json -- TODO: right now we ignore the client's capabilities inductive ClientCapabilities | mk instance ClientCapabilities.hasFromJson : HasFromJson ClientCapabilities := ⟨fun j => ClientCapabilities.mk⟩ -- TODO largely unimplemented structure ServerCapabilities := (textDocumentSync? : Option TextDocumentSyncOptions := none) (hoverProvider : Bool := false) instance ServerCapabilities.hasToJson : HasToJson ServerCapabilities := ⟨fun o => mkObj $ opt "textDocumentSync" o.textDocumentSync? ++ [⟨"hoverProvider", o.hoverProvider⟩]⟩ end Lsp end Lean
675e276b7653878d156732e61c7c4d67c850bba7
302b541ac2e998a523ae04da7673fd0932ded126
/tests/simple/higherorder.lean
612916a0c2ad6081e4ddf576a177d67842722c13
[]
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
115
lean
set_option trace.compiler.ir.init true def higherorder (f: Nat -> Nat) : Nat := f 0 def applied_higher_order()
d480c25ecdccec94e712aa4e5a7f7c7818d9a1a9
fecda8e6b848337561d6467a1e30cf23176d6ad0
/src/geometry/manifold/times_cont_mdiff.lean
1db5d3f0b1ee8bb554419a34b8d9de0e4e1c494c
[ "Apache-2.0" ]
permissive
spolu/mathlib
bacf18c3d2a561d00ecdc9413187729dd1f705ed
480c92cdfe1cf3c2d083abded87e82162e8814f4
refs/heads/master
1,671,684,094,325
1,600,736,045,000
1,600,736,045,000
297,564,749
1
0
null
1,600,758,368,000
1,600,758,367,000
null
UTF-8
Lean
false
false
69,198
lean
/- Copyright (c) 2020 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import geometry.manifold.mfderiv import geometry.manifold.local_invariant_properties /-! # Smooth functions between smooth manifolds We define `Cⁿ` functions between smooth manifolds, as functions which are `Cⁿ` in charts, and prove basic properties of these notions. ## Main definitions and statements Let `M ` and `M'` be two smooth manifolds, with respect to model with corners `I` and `I'`. Let `f : M → M'`. * `times_cont_mdiff_within_at I I' n f s x` states that the function `f` is `Cⁿ` within the set `s` around the point `x`. * `times_cont_mdiff_at I I' n f x` states that the function `f` is `Cⁿ` around `x`. * `times_cont_mdiff_on I I' n f s` states that the function `f` is `Cⁿ` on the set `s` * `times_cont_mdiff I I' n f` states that the function `f` is `Cⁿ`. * `times_cont_mdiff_on.comp` gives the invariance of the `Cⁿ` property under composition * `times_cont_mdiff_on.times_cont_mdiff_on_tangent_map_within` states that the bundled derivative of a `Cⁿ` function in a domain is `Cᵐ` when `m + 1 ≤ n`. * `times_cont_mdiff.times_cont_mdiff_tangent_map` states that the bundled derivative of a `Cⁿ` function is `Cᵐ` when `m + 1 ≤ n`. * `times_cont_mdiff_iff_times_cont_diff` states that, for functions between vector spaces, manifold-smoothness is equivalent to usual smoothness. We also give many basic properties of smooth functions between manifolds, following the API of smooth functions between vector spaces. ## Implementation details Many properties follow for free from the corresponding properties of functions in vector spaces, as being `Cⁿ` is a local property invariant under the smooth groupoid. We take advantage of the general machinery developed in `local_invariant_properties.lean` to get these properties automatically. For instance, the fact that being `Cⁿ` does not depend on the chart one considers is given by `lift_prop_within_at_indep_chart`. For this to work, the definition of `times_cont_mdiff_within_at` and friends has to follow definitionally the setup of local invariant properties. Still, we recast the definition in terms of extended charts in `times_cont_mdiff_on_iff` and `times_cont_mdiff_iff`. -/ open set charted_space smooth_manifold_with_corners open_locale topological_space manifold /-! ### Definition of smooth functions between manifolds -/ variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] -- declare a smooth manifold `M` over the pair `(E, H)`. {E : Type*} [normed_group E] [normed_space 𝕜 E] {H : Type*} [topological_space H] (I : model_with_corners 𝕜 E H) {M : Type*} [topological_space M] [charted_space H M] [Is : smooth_manifold_with_corners I M] -- declare a smooth manifold `M'` over the pair `(E', H')`. {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {H' : Type*} [topological_space H'] (I' : model_with_corners 𝕜 E' H') {M' : Type*} [topological_space M'] [charted_space H' M'] [I's : smooth_manifold_with_corners I' M'] -- declare a smooth manifold `N` over the pair `(F, G)`. {F : Type*} [normed_group F] [normed_space 𝕜 F] {G : Type*} [topological_space G] {J : model_with_corners 𝕜 F G} {N : Type*} [topological_space N] [charted_space G N] [Js : smooth_manifold_with_corners J N] -- declare a smooth manifold `N'` over the pair `(F', G')`. {F' : Type*} [normed_group F'] [normed_space 𝕜 F'] {G' : Type*} [topological_space G'] {J' : model_with_corners 𝕜 F' G'} {N' : Type*} [topological_space N'] [charted_space G' N'] [J's : smooth_manifold_with_corners J' N'] -- declare functions, sets, points and smoothness indices {f f₁ : M → M'} {s s₁ t : set M} {x : M} {m n : with_top ℕ} /-- Property in the model space of a model with corners of being `C^n` within at set at a point, when read in the model vector space. This property will be lifted to manifolds to define smooth functions between manifolds. -/ def times_cont_diff_within_at_prop (n : with_top ℕ) (f s x) : Prop := times_cont_diff_within_at 𝕜 n (I' ∘ f ∘ I.symm) (range I ∩ I.symm ⁻¹' s) (I x) /-- Being `Cⁿ` in the model space is a local property, invariant under smooth maps. Therefore, it will lift nicely to manifolds. -/ lemma times_cont_diff_within_at_local_invariant_prop (n : with_top ℕ) : (times_cont_diff_groupoid ∞ I).local_invariant_prop (times_cont_diff_groupoid ∞ I') (times_cont_diff_within_at_prop I I' n) := { is_local := begin assume s x u f u_open xu, have : range I ∩ I.symm ⁻¹' (s ∩ u) = (range I ∩ I.symm ⁻¹' s) ∩ I.symm ⁻¹' u, by simp only [inter_assoc, preimage_inter], rw [times_cont_diff_within_at_prop, times_cont_diff_within_at_prop, this], symmetry, apply times_cont_diff_within_at_inter, have : u ∈ 𝓝 (I.symm (I x)), by { rw [model_with_corners.left_inv], exact mem_nhds_sets u_open xu }, apply continuous_at.preimage_mem_nhds I.continuous_symm.continuous_at this, end, right_invariance := begin assume s x f e he hx h, rw times_cont_diff_within_at_prop at h ⊢, have : I x = (I ∘ e.symm ∘ I.symm) (I (e x)), by simp only [hx] with mfld_simps, rw this at h, have : I (e x) ∈ (I.symm) ⁻¹' e.target ∩ range ⇑I, by simp only [hx] with mfld_simps, have := ((mem_groupoid_of_pregroupoid.2 he).2.times_cont_diff_within_at this).of_le le_top, convert h.comp' _ this using 1, { ext y, simp only with mfld_simps }, { mfld_set_tac } end, congr := begin assume s x f g h hx hf, apply hf.congr, { assume y hy, simp only with mfld_simps at hy, simp only [h, hy] with mfld_simps }, { simp only [hx] with mfld_simps } end, left_invariance := begin assume s x f e' he' hs hx h, rw times_cont_diff_within_at_prop at h ⊢, have A : (I' ∘ f ∘ I.symm) (I x) ∈ (I'.symm ⁻¹' e'.source ∩ range I'), by simp only [hx] with mfld_simps, have := ((mem_groupoid_of_pregroupoid.2 he').1.times_cont_diff_within_at A).of_le le_top, convert this.comp _ h _, { ext y, simp only with mfld_simps }, { assume y hy, simp only with mfld_simps at hy, simpa only [hy] with mfld_simps using hs hy.2 } end } lemma times_cont_diff_within_at_local_invariant_prop_mono (n : with_top ℕ) ⦃s x t⦄ ⦃f : H → H'⦄ (hts : t ⊆ s) (h : times_cont_diff_within_at_prop I I' n f s x) : times_cont_diff_within_at_prop I I' n f t x := begin apply h.mono (λ y hy, _), simp only with mfld_simps at hy, simp only [hy, hts _] with mfld_simps end lemma times_cont_diff_within_at_local_invariant_prop_id (x : H) : times_cont_diff_within_at_prop I I ∞ id univ x := begin simp [times_cont_diff_within_at_prop], have : times_cont_diff_within_at 𝕜 ∞ id (range I) (I x) := times_cont_diff_id.times_cont_diff_at.times_cont_diff_within_at, apply this.congr (λ y hy, _), { simp only with mfld_simps }, { simp only [model_with_corners.right_inv I hy] with mfld_simps } end /-- A function is `n` times continuously differentiable within a set at a point in a manifold if it is continuous and it is `n` times continuously differentiable in this set around this point, when read in the preferred chart at this point. -/ def times_cont_mdiff_within_at (n : with_top ℕ) (f : M → M') (s : set M) (x : M) := lift_prop_within_at (times_cont_diff_within_at_prop I I' n) f s x /-- Abbreviation for `times_cont_mdiff_within_at I I' ⊤ f s x`. See also documentation for `smooth`. -/ @[reducible] def smooth_within_at (f : M → M') (s : set M) (x : M) := times_cont_mdiff_within_at I I' ⊤ f s x /-- A function is `n` times continuously differentiable at a point in a manifold if it is continuous and it is `n` times continuously differentiable around this point, when read in the preferred chart at this point. -/ def times_cont_mdiff_at (n : with_top ℕ) (f : M → M') (x : M) := times_cont_mdiff_within_at I I' n f univ x /-- Abbreviation for `times_cont_mdiff_at I I' ⊤ f x`. See also documentation for `smooth`. -/ @[reducible] def smooth_at (f : M → M') (x : M) := times_cont_mdiff_at I I' ⊤ f x /-- A function is `n` times continuously differentiable in a set of a manifold if it is continuous and, for any pair of points, it is `n` times continuously differentiable on this set in the charts around these points. -/ def times_cont_mdiff_on (n : with_top ℕ) (f : M → M') (s : set M) := ∀ x ∈ s, times_cont_mdiff_within_at I I' n f s x /-- Abbreviation for `times_cont_mdiff_on I I' ⊤ f s`. See also documentation for `smooth`. -/ @[reducible] def smooth_on (f : M → M') (s : set M) := times_cont_mdiff_on I I' ⊤ f s /-- A function is `n` times continuously differentiable in a manifold if it is continuous and, for any pair of points, it is `n` times continuously differentiable in the charts around these points. -/ def times_cont_mdiff (n : with_top ℕ) (f : M → M') := ∀ x, times_cont_mdiff_at I I' n f x /-- Abbreviation for `times_cont_mdiff I I' ⊤ f`. Short note to work with these abbreviations: a lemma of the form `times_cont_mdiff_foo.bar` will apply fine to an assumption `smooth_foo` using dot notation or normal notation. If the consequence `bar` of the lemma involves `times_cont_diff`, it is still better to restate the lemma replacing `times_cont_diff` with `smooth` both in the assumption and in the conclusion, to make it possible to use `smooth` consistently. This also applies to `smooth_at`, `smooth_on` and `smooth_within_at`.-/ @[reducible] def smooth (f : M → M') := times_cont_mdiff I I' ⊤ f /-! ### Basic properties of smooth functions between manifolds -/ variables {I I'} lemma times_cont_mdiff.smooth (h : times_cont_mdiff I I' ⊤ f) : smooth I I' f := h lemma smooth.times_cont_mdiff (h : smooth I I' f) : times_cont_mdiff I I' ⊤ f := h lemma times_cont_mdiff_on.smooth_on (h : times_cont_mdiff_on I I' ⊤ f s) : smooth_on I I' f s := h lemma smooth_on.times_cont_mdiff_on (h : smooth_on I I' f s) : times_cont_mdiff_on I I' ⊤ f s := h lemma times_cont_mdiff_at.smooth_at (h : times_cont_mdiff_at I I' ⊤ f x) : smooth_at I I' f x := h lemma smooth_at.times_cont_mdiff_at (h : smooth_at I I' f x) : times_cont_mdiff_at I I' ⊤ f x := h lemma times_cont_mdiff_within_at.smooth_within_at (h : times_cont_mdiff_within_at I I' ⊤ f s x) : smooth_within_at I I' f s x := h lemma smooth_within_at.times_cont_mdiff_within_at (h : smooth_within_at I I' f s x) : times_cont_mdiff_within_at I I' ⊤ f s x := h lemma times_cont_mdiff.times_cont_mdiff_at (h : times_cont_mdiff I I' n f) : times_cont_mdiff_at I I' n f x := h x lemma smooth.smooth_at (h : smooth I I' f) : smooth_at I I' f x := times_cont_mdiff.times_cont_mdiff_at h lemma times_cont_mdiff_within_at_univ : times_cont_mdiff_within_at I I' n f univ x ↔ times_cont_mdiff_at I I' n f x := iff.rfl lemma smooth_at_univ : smooth_within_at I I' f univ x ↔ smooth_at I I' f x := times_cont_mdiff_within_at_univ lemma times_cont_mdiff_on_univ : times_cont_mdiff_on I I' n f univ ↔ times_cont_mdiff I I' n f := by simp only [times_cont_mdiff_on, times_cont_mdiff, times_cont_mdiff_within_at_univ, forall_prop_of_true, mem_univ] lemma smooth_on_univ : smooth_on I I' f univ ↔ smooth I I' f := times_cont_mdiff_on_univ /-- One can reformulate smoothness within a set at a point as continuity within this set at this point, and smoothness in the corresponding extended chart. -/ lemma times_cont_mdiff_within_at_iff : times_cont_mdiff_within_at I I' n f s x ↔ continuous_within_at f s x ∧ times_cont_diff_within_at 𝕜 n ((ext_chart_at I' (f x)) ∘ f ∘ (ext_chart_at I x).symm) ((ext_chart_at I x).target ∩ (ext_chart_at I x).symm ⁻¹' (s ∩ f ⁻¹' (ext_chart_at I' (f x)).source)) (ext_chart_at I x x) := begin rw [times_cont_mdiff_within_at, lift_prop_within_at, times_cont_diff_within_at_prop], congr' 3, mfld_set_tac end lemma smooth_within_at_iff : smooth_within_at I I' f s x ↔ continuous_within_at f s x ∧ times_cont_diff_within_at 𝕜 ∞ ((ext_chart_at I' (f x)) ∘ f ∘ (ext_chart_at I x).symm) ((ext_chart_at I x).target ∩ (ext_chart_at I x).symm ⁻¹' (s ∩ f ⁻¹' (ext_chart_at I' (f x)).source)) (ext_chart_at I x x) := times_cont_mdiff_within_at_iff include Is I's /-- One can reformulate smoothness on a set as continuity on this set, and smoothness in any extended chart. -/ lemma times_cont_mdiff_on_iff : times_cont_mdiff_on I I' n f s ↔ continuous_on f s ∧ ∀ (x : M) (y : M'), times_cont_diff_on 𝕜 n ((ext_chart_at I' y) ∘ f ∘ (ext_chart_at I x).symm) ((ext_chart_at I x).target ∩ (ext_chart_at I x).symm ⁻¹' (s ∩ f ⁻¹' (ext_chart_at I' y).source)) := begin split, { assume h, refine ⟨λ x hx, (h x hx).1, λ x y z hz, _⟩, simp only with mfld_simps at hz, let w := (ext_chart_at I x).symm z, have : w ∈ s, by simp only [w, hz] with mfld_simps, specialize h w this, have w1 : w ∈ (chart_at H x).source, by simp only [w, hz] with mfld_simps, have w2 : f w ∈ (chart_at H' y).source, by simp only [w, hz] with mfld_simps, convert (((times_cont_diff_within_at_local_invariant_prop I I' n).lift_prop_within_at_indep_chart (structure_groupoid.chart_mem_maximal_atlas _ x) w1 (structure_groupoid.chart_mem_maximal_atlas _ y) w2).1 h).2 using 1, { mfld_set_tac }, { simp only [w, hz] with mfld_simps } }, { rintros ⟨hcont, hdiff⟩ x hx, refine ⟨hcont x hx, _⟩, have Z := hdiff x (f x) (ext_chart_at I x x) (by simp only [hx] with mfld_simps), dsimp [times_cont_diff_within_at_prop], convert Z using 1, mfld_set_tac } end lemma smooth_on_iff : smooth_on I I' f s ↔ continuous_on f s ∧ ∀ (x : M) (y : M'), times_cont_diff_on 𝕜 ⊤ ((ext_chart_at I' y) ∘ f ∘ (ext_chart_at I x).symm) ((ext_chart_at I x).target ∩ (ext_chart_at I x).symm ⁻¹' (s ∩ f ⁻¹' (ext_chart_at I' y).source)) := times_cont_mdiff_on_iff /-- One can reformulate smoothness as continuity and smoothness in any extended chart. -/ lemma times_cont_mdiff_iff : times_cont_mdiff I I' n f ↔ continuous f ∧ ∀ (x : M) (y : M'), times_cont_diff_on 𝕜 n ((ext_chart_at I' y) ∘ f ∘ (ext_chart_at I x).symm) ((ext_chart_at I x).target ∩ (ext_chart_at I x).symm ⁻¹' (f ⁻¹' (ext_chart_at I' y).source)) := by simp [← times_cont_mdiff_on_univ, times_cont_mdiff_on_iff, continuous_iff_continuous_on_univ] lemma smooth_iff : smooth I I' f ↔ continuous f ∧ ∀ (x : M) (y : M'), times_cont_diff_on 𝕜 ⊤ ((ext_chart_at I' y) ∘ f ∘ (ext_chart_at I x).symm) ((ext_chart_at I x).target ∩ (ext_chart_at I x).symm ⁻¹' (f ⁻¹' (ext_chart_at I' y).source)) := times_cont_mdiff_iff omit Is I's /-! ### Deducing smoothness from higher smoothness -/ lemma times_cont_mdiff_within_at.of_le (hf : times_cont_mdiff_within_at I I' n f s x) (le : m ≤ n) : times_cont_mdiff_within_at I I' m f s x := ⟨hf.1, hf.2.of_le le⟩ lemma times_cont_mdiff_at.of_le (hf : times_cont_mdiff_at I I' n f x) (le : m ≤ n) : times_cont_mdiff_at I I' m f x := times_cont_mdiff_within_at.of_le hf le lemma times_cont_mdiff_on.of_le (hf : times_cont_mdiff_on I I' n f s) (le : m ≤ n) : times_cont_mdiff_on I I' m f s := λ x hx, (hf x hx).of_le le lemma times_cont_mdiff.of_le (hf : times_cont_mdiff I I' n f) (le : m ≤ n) : times_cont_mdiff I I' m f := λ x, (hf x).of_le le /-! ### Deducing smoothness from smoothness one step beyond -/ lemma times_cont_mdiff_within_at.of_succ {n : ℕ} (h : times_cont_mdiff_within_at I I' n.succ f s x) : times_cont_mdiff_within_at I I' n f s x := h.of_le (with_top.coe_le_coe.2 (nat.le_succ n)) lemma times_cont_mdiff_at.of_succ {n : ℕ} (h : times_cont_mdiff_at I I' n.succ f x) : times_cont_mdiff_at I I' n f x := times_cont_mdiff_within_at.of_succ h lemma times_cont_mdiff_on.of_succ {n : ℕ} (h : times_cont_mdiff_on I I' n.succ f s) : times_cont_mdiff_on I I' n f s := λ x hx, (h x hx).of_succ lemma times_cont_mdiff.of_succ {n : ℕ} (h : times_cont_mdiff I I' n.succ f) : times_cont_mdiff I I' n f := λ x, (h x).of_succ /-! ### Deducing continuity from smoothness-/ lemma times_cont_mdiff_within_at.continuous_within_at (hf : times_cont_mdiff_within_at I I' n f s x) : continuous_within_at f s x := hf.1 lemma times_cont_mdiff_at.continuous_at (hf : times_cont_mdiff_at I I' n f x) : continuous_at f x := (continuous_within_at_univ _ _ ).1 $ times_cont_mdiff_within_at.continuous_within_at hf lemma times_cont_mdiff_on.continuous_on (hf : times_cont_mdiff_on I I' n f s) : continuous_on f s := λ x hx, (hf x hx).continuous_within_at lemma times_cont_mdiff.continuous (hf : times_cont_mdiff I I' n f) : continuous f := continuous_iff_continuous_at.2 $ λ x, (hf x).continuous_at /-! ### Deducing differentiability from smoothness -/ lemma times_cont_mdiff_within_at.mdifferentiable_within_at (hf : times_cont_mdiff_within_at I I' n f s x) (hn : 1 ≤ n) : mdifferentiable_within_at I I' f s x := begin suffices h : mdifferentiable_within_at I I' f (s ∩ (f ⁻¹' (ext_chart_at I' (f x)).source)) x, { rwa mdifferentiable_within_at_inter' at h, apply (hf.1).preimage_mem_nhds_within, exact mem_nhds_sets (ext_chart_at_open_source I' (f x)) (mem_ext_chart_source I' (f x)) }, rw mdifferentiable_within_at_iff, exact ⟨hf.1.mono (inter_subset_left _ _), (hf.2.differentiable_within_at hn).mono (by mfld_set_tac)⟩, end lemma times_cont_mdiff_at.mdifferentiable_at (hf : times_cont_mdiff_at I I' n f x) (hn : 1 ≤ n) : mdifferentiable_at I I' f x := mdifferentiable_within_at_univ.1 $ times_cont_mdiff_within_at.mdifferentiable_within_at hf hn lemma times_cont_mdiff_on.mdifferentiable_on (hf : times_cont_mdiff_on I I' n f s) (hn : 1 ≤ n) : mdifferentiable_on I I' f s := λ x hx, (hf x hx).mdifferentiable_within_at hn lemma times_cont_mdiff.mdifferentiable (hf : times_cont_mdiff I I' n f) (hn : 1 ≤ n) : mdifferentiable I I' f := λ x, (hf x).mdifferentiable_at hn /-! ### `C^∞` smoothness -/ lemma times_cont_mdiff_within_at_top : smooth_within_at I I' f s x ↔ (∀n:ℕ, times_cont_mdiff_within_at I I' n f s x) := ⟨λ h n, ⟨h.1, times_cont_diff_within_at_top.1 h.2 n⟩, λ H, ⟨(H 0).1, times_cont_diff_within_at_top.2 (λ n, (H n).2)⟩⟩ lemma times_cont_mdiff_at_top : smooth_at I I' f x ↔ (∀n:ℕ, times_cont_mdiff_at I I' n f x) := times_cont_mdiff_within_at_top lemma times_cont_mdiff_on_top : smooth_on I I' f s ↔ (∀n:ℕ, times_cont_mdiff_on I I' n f s) := ⟨λ h n, h.of_le le_top, λ h x hx, times_cont_mdiff_within_at_top.2 (λ n, h n x hx)⟩ lemma times_cont_mdiff_top : smooth I I' f ↔ (∀n:ℕ, times_cont_mdiff I I' n f) := ⟨λ h n, h.of_le le_top, λ h x, times_cont_mdiff_within_at_top.2 (λ n, h n x)⟩ lemma times_cont_mdiff_within_at_iff_nat : times_cont_mdiff_within_at I I' n f s x ↔ (∀m:ℕ, (m : with_top ℕ) ≤ n → times_cont_mdiff_within_at I I' m f s x) := begin refine ⟨λ h m hm, h.of_le hm, λ h, _⟩, cases n, { exact times_cont_mdiff_within_at_top.2 (λ n, h n le_top) }, { exact h n (le_refl _) } end /-! ### Restriction to a smaller set -/ lemma times_cont_mdiff_within_at.mono (hf : times_cont_mdiff_within_at I I' n f s x) (hts : t ⊆ s) : times_cont_mdiff_within_at I I' n f t x := structure_groupoid.local_invariant_prop.lift_prop_within_at_mono (times_cont_diff_within_at_local_invariant_prop_mono I I' n) hf hts lemma times_cont_mdiff_at.times_cont_mdiff_within_at (hf : times_cont_mdiff_at I I' n f x) : times_cont_mdiff_within_at I I' n f s x := times_cont_mdiff_within_at.mono hf (subset_univ _) lemma smooth_at.smooth_within_at (hf : smooth_at I I' f x) : smooth_within_at I I' f s x := times_cont_mdiff_at.times_cont_mdiff_within_at hf lemma times_cont_mdiff_on.mono (hf : times_cont_mdiff_on I I' n f s) (hts : t ⊆ s) : times_cont_mdiff_on I I' n f t := λ x hx, (hf x (hts hx)).mono hts lemma times_cont_mdiff.times_cont_mdiff_on (hf : times_cont_mdiff I I' n f) : times_cont_mdiff_on I I' n f s := λ x hx, (hf x).times_cont_mdiff_within_at lemma smooth.smooth_on (hf : smooth I I' f) : smooth_on I I' f s := times_cont_mdiff.times_cont_mdiff_on hf lemma times_cont_mdiff_within_at_inter' (ht : t ∈ 𝓝[s] x) : times_cont_mdiff_within_at I I' n f (s ∩ t) x ↔ times_cont_mdiff_within_at I I' n f s x := (times_cont_diff_within_at_local_invariant_prop I I' n).lift_prop_within_at_inter' ht lemma times_cont_mdiff_within_at_inter (ht : t ∈ 𝓝 x) : times_cont_mdiff_within_at I I' n f (s ∩ t) x ↔ times_cont_mdiff_within_at I I' n f s x := (times_cont_diff_within_at_local_invariant_prop I I' n).lift_prop_within_at_inter ht lemma times_cont_mdiff_within_at.times_cont_mdiff_at (h : times_cont_mdiff_within_at I I' n f s x) (ht : s ∈ 𝓝 x) : times_cont_mdiff_at I I' n f x := (times_cont_diff_within_at_local_invariant_prop I I' n).lift_prop_at_of_lift_prop_within_at h ht lemma smooth_within_at.smooth_at (h : smooth_within_at I I' f s x) (ht : s ∈ 𝓝 x) : smooth_at I I' f x := times_cont_mdiff_within_at.times_cont_mdiff_at h ht include Is I's /-- A function is `C^n` within a set at a point, for `n : ℕ`, if and only if it is `C^n` on a neighborhood of this point. -/ lemma times_cont_mdiff_within_at_iff_times_cont_mdiff_on_nhds {n : ℕ} : times_cont_mdiff_within_at I I' n f s x ↔ ∃ u ∈ 𝓝[insert x s] x, times_cont_mdiff_on I I' n f u := begin split, { assume h, -- the property is true in charts. We will pull such a good neighborhood in the chart to the -- manifold. For this, we need to restrict to a small enough set where everything makes sense obtain ⟨o, o_open, xo, ho, h'o⟩ : ∃ (o : set M), is_open o ∧ x ∈ o ∧ o ⊆ (chart_at H x).source ∧ o ∩ s ⊆ f ⁻¹' (chart_at H' (f x)).source, { have : (chart_at H' (f x)).source ∈ 𝓝 (f x) := mem_nhds_sets (local_homeomorph.open_source _) (mem_chart_source H' (f x)), rcases mem_nhds_within.1 (h.1.preimage_mem_nhds_within this) with ⟨u, u_open, xu, hu⟩, refine ⟨u ∩ (chart_at H x).source, _, ⟨xu, mem_chart_source _ _⟩, _, _⟩, { exact is_open_inter u_open (local_homeomorph.open_source _) }, { assume y hy, exact hy.2 }, { assume y hy, exact hu ⟨hy.1.1, hy.2⟩ } }, have h' : times_cont_mdiff_within_at I I' n f (s ∩ o) x := h.mono (inter_subset_left _ _), simp only [times_cont_mdiff_within_at, lift_prop_within_at, times_cont_diff_within_at_prop] at h', -- let `u` be a good neighborhood in the chart where the function is smooth rcases h.2.times_cont_diff_on (le_refl _) with ⟨u, u_nhds, u_subset, hu⟩, -- pull it back to the manifold, and intersect with a suitable neighborhood of `x`, to get the -- desired good neighborhood `v`. let v := ((insert x s) ∩ o) ∩ (ext_chart_at I x) ⁻¹' u, have v_incl : v ⊆ (chart_at H x).source := λ y hy, ho hy.1.2, have v_incl' : ∀ y ∈ v, f y ∈ (chart_at H' (f x)).source, { assume y hy, rcases hy.1.1 with rfl|h', { simp only with mfld_simps }, { apply h'o ⟨hy.1.2, h'⟩ } }, refine ⟨v, _, _⟩, show v ∈ 𝓝[insert x s] x, { rw nhds_within_restrict _ xo o_open, refine filter.inter_mem_sets self_mem_nhds_within _, suffices : u ∈ 𝓝[(ext_chart_at I x) '' (insert x s ∩ o)] (ext_chart_at I x x), from (ext_chart_at_continuous_at I x).continuous_within_at.preimage_mem_nhds_within' this, apply nhds_within_mono _ _ u_nhds, rw image_subset_iff, assume y hy, rcases hy.1 with rfl|h', { simp only [mem_insert_iff] with mfld_simps }, { simp only [mem_insert_iff, ho hy.2, h', h'o ⟨hy.2, h'⟩] with mfld_simps } }, show times_cont_mdiff_on I I' n f v, { assume y hy, apply (((times_cont_diff_within_at_local_invariant_prop I I' n).lift_prop_within_at_indep_chart (structure_groupoid.chart_mem_maximal_atlas _ x) (v_incl hy) (structure_groupoid.chart_mem_maximal_atlas _ (f x)) (v_incl' y hy))).2, split, { apply (((ext_chart_at_continuous_on_symm I' (f x) _ _).comp' (hu _ hy.2).continuous_within_at).comp' (ext_chart_at_continuous_on I x _ _)).congr_mono, { assume z hz, simp only [v_incl hz, v_incl' z hz] with mfld_simps }, { assume z hz, simp only [v_incl hz, v_incl' z hz] with mfld_simps, exact hz.2 }, { simp only [v_incl hy, v_incl' y hy] with mfld_simps }, { simp only [v_incl hy, v_incl' y hy] with mfld_simps }, { simp only [v_incl hy] with mfld_simps } }, { apply hu.mono, { assume z hz, simp only [v] with mfld_simps at hz, have : I ((chart_at H x) (((chart_at H x).symm) (I.symm z))) ∈ u, by simp only [hz], simpa only [hz] with mfld_simps using this }, { have exty : I (chart_at H x y) ∈ u := hy.2, simp only [v_incl hy, v_incl' y hy, exty, hy.1.1, hy.1.2] with mfld_simps } } } }, { rintros ⟨u, u_nhds, hu⟩, have : times_cont_mdiff_within_at I I' ↑n f (insert x s ∩ u) x, { have : x ∈ insert x s := mem_insert x s, exact hu.mono (inter_subset_right _ _) _ ⟨this, mem_of_mem_nhds_within this u_nhds⟩ }, rw times_cont_mdiff_within_at_inter' u_nhds at this, exact this.mono (subset_insert x s) } end /-- A function is `C^n` at a point, for `n : ℕ`, if and only if it is `C^n` on a neighborhood of this point. -/ lemma times_cont_mdiff_at_iff_times_cont_mdiff_on_nhds {n : ℕ} : times_cont_mdiff_at I I' n f x ↔ ∃ u ∈ 𝓝 x, times_cont_mdiff_on I I' n f u := by simp [← times_cont_mdiff_within_at_univ, times_cont_mdiff_within_at_iff_times_cont_mdiff_on_nhds, nhds_within_univ] omit Is I's /-! ### Congruence lemmas -/ lemma times_cont_mdiff_within_at.congr (h : times_cont_mdiff_within_at I I' n f s x) (h₁ : ∀ y ∈ s, f₁ y = f y) (hx : f₁ x = f x) : times_cont_mdiff_within_at I I' n f₁ s x := (times_cont_diff_within_at_local_invariant_prop I I' n).lift_prop_within_at_congr h h₁ hx lemma times_cont_mdiff_within_at_congr (h₁ : ∀ y ∈ s, f₁ y = f y) (hx : f₁ x = f x) : times_cont_mdiff_within_at I I' n f₁ s x ↔ times_cont_mdiff_within_at I I' n f s x := (times_cont_diff_within_at_local_invariant_prop I I' n).lift_prop_within_at_congr_iff h₁ hx lemma times_cont_mdiff_within_at.congr_of_eventually_eq (h : times_cont_mdiff_within_at I I' n f s x) (h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) : times_cont_mdiff_within_at I I' n f₁ s x := (times_cont_diff_within_at_local_invariant_prop I I' n).lift_prop_within_at_congr_of_eventually_eq h h₁ hx lemma filter.eventually_eq.times_cont_mdiff_within_at_iff (h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) : times_cont_mdiff_within_at I I' n f₁ s x ↔ times_cont_mdiff_within_at I I' n f s x := (times_cont_diff_within_at_local_invariant_prop I I' n) .lift_prop_within_at_congr_iff_of_eventually_eq h₁ hx lemma times_cont_mdiff_at.congr_of_eventually_eq (h : times_cont_mdiff_at I I' n f x) (h₁ : f₁ =ᶠ[𝓝 x] f) : times_cont_mdiff_at I I' n f₁ x := (times_cont_diff_within_at_local_invariant_prop I I' n).lift_prop_at_congr_of_eventually_eq h h₁ lemma filter.eventually_eq.times_cont_mdiff_at_iff (h₁ : f₁ =ᶠ[𝓝 x] f) : times_cont_mdiff_at I I' n f₁ x ↔ times_cont_mdiff_at I I' n f x := (times_cont_diff_within_at_local_invariant_prop I I' n).lift_prop_at_congr_iff_of_eventually_eq h₁ lemma times_cont_mdiff_on.congr (h : times_cont_mdiff_on I I' n f s) (h₁ : ∀ y ∈ s, f₁ y = f y) : times_cont_mdiff_on I I' n f₁ s := (times_cont_diff_within_at_local_invariant_prop I I' n).lift_prop_on_congr h h₁ lemma times_cont_mdiff_on_congr (h₁ : ∀ y ∈ s, f₁ y = f y) : times_cont_mdiff_on I I' n f₁ s ↔ times_cont_mdiff_on I I' n f s := (times_cont_diff_within_at_local_invariant_prop I I' n).lift_prop_on_congr_iff h₁ /-! ### Locality -/ /-- Being `C^n` is a local property. -/ lemma times_cont_mdiff_on_of_locally_times_cont_mdiff_on (h : ∀x∈s, ∃u, is_open u ∧ x ∈ u ∧ times_cont_mdiff_on I I' n f (s ∩ u)) : times_cont_mdiff_on I I' n f s := (times_cont_diff_within_at_local_invariant_prop I I' n).lift_prop_on_of_locally_lift_prop_on h lemma times_cont_mdiff_of_locally_times_cont_mdiff_on (h : ∀x, ∃u, is_open u ∧ x ∈ u ∧ times_cont_mdiff_on I I' n f u) : times_cont_mdiff I I' n f := (times_cont_diff_within_at_local_invariant_prop I I' n).lift_prop_of_locally_lift_prop_on h /-! ### Smoothness of the composition of smooth functions between manifolds -/ section composition variables {E'' : Type*} [normed_group E''] [normed_space 𝕜 E''] {H'' : Type*} [topological_space H''] {I'' : model_with_corners 𝕜 E'' H''} {M'' : Type*} [topological_space M''] [charted_space H'' M''] [smooth_manifold_with_corners I'' M''] include Is I's /-- The composition of `C^n` functions on domains is `C^n`. -/ lemma times_cont_mdiff_on.comp {t : set M'} {g : M' → M''} (hg : times_cont_mdiff_on I' I'' n g t) (hf : times_cont_mdiff_on I I' n f s) (st : s ⊆ f ⁻¹' t) : times_cont_mdiff_on I I'' n (g ∘ f) s := begin rw times_cont_mdiff_on_iff at hf hg ⊢, have cont_gf : continuous_on (g ∘ f) s := continuous_on.comp hg.1 hf.1 st, refine ⟨cont_gf, λx y, _⟩, apply times_cont_diff_on_of_locally_times_cont_diff_on, assume z hz, let x' := (ext_chart_at I x).symm z, have x'_source : x' ∈ (ext_chart_at I x).source := (ext_chart_at I x).map_target hz.1, obtain ⟨o, o_open, zo, o_subset⟩ : ∃ o, is_open o ∧ z ∈ o ∧ o ∩ (((ext_chart_at I x).symm ⁻¹' s ∩ range I)) ⊆ (ext_chart_at I x).symm ⁻¹' (f ⁻¹' (ext_chart_at I' (f x')).source), { have x'z : (ext_chart_at I x) x' = z, by simp only [x', hz.1, -ext_chart_at] with mfld_simps, have : continuous_within_at f s x' := hf.1 _ hz.2.1, have : f ⁻¹' (ext_chart_at I' (f x')).source ∈ 𝓝[s] x' := this.preimage_mem_nhds_within (mem_nhds_sets (ext_chart_at_open_source I' (f x')) (mem_ext_chart_source I' (f x'))), have : (ext_chart_at I x).symm ⁻¹' (f ⁻¹' (ext_chart_at I' (f x')).source) ∈ 𝓝[(ext_chart_at I x).symm ⁻¹' s ∩ range I] ((ext_chart_at I x) x') := ext_chart_preimage_mem_nhds_within' _ _ x'_source this, rw x'z at this, exact mem_nhds_within.1 this }, refine ⟨o, o_open, zo, _⟩, let u := ((ext_chart_at I x).target ∩ (ext_chart_at I x).symm ⁻¹' (s ∩ g ∘ f ⁻¹' (ext_chart_at I'' y).source) ∩ o), -- it remains to show that `g ∘ f` read in the charts is `C^n` on `u` have u_subset : u ⊆ (ext_chart_at I x).target ∩ (ext_chart_at I x).symm ⁻¹' (s ∩ f ⁻¹' (ext_chart_at I' (f x')).source), { rintros p ⟨⟨hp₁, ⟨hp₂, hp₃⟩⟩, hp₄⟩, refine ⟨hp₁, ⟨hp₂, o_subset ⟨hp₄, ⟨hp₂, _⟩⟩⟩⟩, have := hp₁.1, rwa model_with_corners.target at this }, have : times_cont_diff_on 𝕜 n (((ext_chart_at I'' y) ∘ g ∘ (ext_chart_at I' (f x')).symm) ∘ ((ext_chart_at I' (f x')) ∘ f ∘ (ext_chart_at I x).symm)) u, { refine times_cont_diff_on.comp (hg.2 (f x') y) ((hf.2 x (f x')).mono u_subset) (λp hp, _), simp only [local_equiv.map_source _ (u_subset hp).2.2, local_equiv.left_inv _ (u_subset hp).2.2, -ext_chart_at] with mfld_simps, exact ⟨st (u_subset hp).2.1, hp.1.2.2⟩ }, refine this.congr (λp hp, _), simp only [local_equiv.left_inv _ (u_subset hp).2.2, -ext_chart_at] with mfld_simps end /-- The composition of `C^n` functions on domains is `C^n`. -/ lemma times_cont_mdiff_on.comp' {t : set M'} {g : M' → M''} (hg : times_cont_mdiff_on I' I'' n g t) (hf : times_cont_mdiff_on I I' n f s) : times_cont_mdiff_on I I'' n (g ∘ f) (s ∩ f ⁻¹' t) := hg.comp (hf.mono (inter_subset_left _ _)) (inter_subset_right _ _) /-- The composition of `C^n` functions is `C^n`. -/ lemma times_cont_mdiff.comp {g : M' → M''} (hg : times_cont_mdiff I' I'' n g) (hf : times_cont_mdiff I I' n f) : times_cont_mdiff I I'' n (g ∘ f) := begin rw ← times_cont_mdiff_on_univ at hf hg ⊢, exact hg.comp hf subset_preimage_univ, end /-- The composition of `C^n` functions within domains at points is `C^n`. -/ lemma times_cont_mdiff_within_at.comp {t : set M'} {g : M' → M''} (x : M) (hg : times_cont_mdiff_within_at I' I'' n g t (f x)) (hf : times_cont_mdiff_within_at I I' n f s x) (st : s ⊆ f ⁻¹' t) : times_cont_mdiff_within_at I I'' n (g ∘ f) s x := begin apply times_cont_mdiff_within_at_iff_nat.2 (λ m hm, _), rcases times_cont_mdiff_within_at_iff_times_cont_mdiff_on_nhds.1 (hg.of_le hm) with ⟨v, v_nhds, hv⟩, rcases times_cont_mdiff_within_at_iff_times_cont_mdiff_on_nhds.1 (hf.of_le hm) with ⟨u, u_nhds, hu⟩, apply times_cont_mdiff_within_at_iff_times_cont_mdiff_on_nhds.2 ⟨_, _, hv.comp' hu⟩, apply filter.inter_mem_sets u_nhds, suffices h : v ∈ 𝓝[f '' s] (f x), { convert mem_nhds_within_insert (hf.continuous_within_at.preimage_mem_nhds_within' h), rw insert_eq_of_mem, apply mem_of_mem_nhds_within (mem_insert (f x) t) v_nhds }, apply nhds_within_mono _ _ v_nhds, rw image_subset_iff, exact subset.trans st (preimage_mono (subset_insert _ _)) end /-- The composition of `C^n` functions within domains at points is `C^n`. -/ lemma times_cont_mdiff_within_at.comp' {t : set M'} {g : M' → M''} (x : M) (hg : times_cont_mdiff_within_at I' I'' n g t (f x)) (hf : times_cont_mdiff_within_at I I' n f s x) : times_cont_mdiff_within_at I I'' n (g ∘ f) (s ∩ f⁻¹' t) x := hg.comp x (hf.mono (inter_subset_left _ _)) (inter_subset_right _ _) /-- The composition of `C^n` functions at points is `C^n`. -/ lemma times_cont_mdiff_at.comp {g : M' → M''} (x : M) (hg : times_cont_mdiff_at I' I'' n g (f x)) (hf : times_cont_mdiff_at I I' n f x) : times_cont_mdiff_at I I'' n (g ∘ f) x := hg.comp x hf subset_preimage_univ lemma times_cont_mdiff.comp_times_cont_mdiff_on {f : M → M'} {g : M' → M''} {s : set M} (hg : times_cont_mdiff I' I'' n g) (hf : times_cont_mdiff_on I I' n f s) : times_cont_mdiff_on I I'' n (g ∘ f) s := hg.times_cont_mdiff_on.comp hf set.subset_preimage_univ lemma smooth.comp_smooth_on {f : M → M'} {g : M' → M''} {s : set M} (hg : smooth I' I'' g) (hf : smooth_on I I' f s) : smooth_on I I'' (g ∘ f) s := hg.smooth_on.comp hf set.subset_preimage_univ end composition /-! ### Atlas members are smooth -/ section atlas variables {e : local_homeomorph M H} include Is /-- An atlas member is `C^n` for any `n`. -/ lemma times_cont_mdiff_on_of_mem_maximal_atlas (h : e ∈ maximal_atlas I M) : times_cont_mdiff_on I I n e e.source := times_cont_mdiff_on.of_le ((times_cont_diff_within_at_local_invariant_prop I I ∞).lift_prop_on_of_mem_maximal_atlas (times_cont_diff_within_at_local_invariant_prop_id I) h) le_top /-- The inverse of an atlas member is `C^n` for any `n`. -/ lemma times_cont_mdiff_on_symm_of_mem_maximal_atlas (h : e ∈ maximal_atlas I M) : times_cont_mdiff_on I I n e.symm e.target := times_cont_mdiff_on.of_le ((times_cont_diff_within_at_local_invariant_prop I I ∞).lift_prop_on_symm_of_mem_maximal_atlas (times_cont_diff_within_at_local_invariant_prop_id I) h) le_top lemma times_cont_mdiff_on_chart : times_cont_mdiff_on I I n (chart_at H x) (chart_at H x).source := times_cont_mdiff_on_of_mem_maximal_atlas ((times_cont_diff_groupoid ⊤ I).chart_mem_maximal_atlas x) lemma times_cont_mdiff_on_chart_symm : times_cont_mdiff_on I I n (chart_at H x).symm (chart_at H x).target := times_cont_mdiff_on_symm_of_mem_maximal_atlas ((times_cont_diff_groupoid ⊤ I).chart_mem_maximal_atlas x) end atlas /-! ### The identity is smooth -/ section id lemma times_cont_mdiff_id : times_cont_mdiff I I n (id : M → M) := times_cont_mdiff.of_le ((times_cont_diff_within_at_local_invariant_prop I I ∞).lift_prop_id (times_cont_diff_within_at_local_invariant_prop_id I)) le_top lemma smooth_id : smooth I I (id : M → M) := times_cont_mdiff_id lemma times_cont_mdiff_on_id : times_cont_mdiff_on I I n (id : M → M) s := times_cont_mdiff_id.times_cont_mdiff_on lemma smooth_on_id : smooth_on I I (id : M → M) s := times_cont_mdiff_on_id lemma times_cont_mdiff_at_id : times_cont_mdiff_at I I n (id : M → M) x := times_cont_mdiff_id.times_cont_mdiff_at lemma smooth_at_id : smooth_at I I (id : M → M) x := times_cont_mdiff_at_id lemma times_cont_mdiff_within_at_id : times_cont_mdiff_within_at I I n (id : M → M) s x := times_cont_mdiff_at_id.times_cont_mdiff_within_at lemma smooth_within_at_id : smooth_within_at I I (id : M → M) s x := times_cont_mdiff_within_at_id end id /-! ### Constants are smooth -/ section id variable {c : M'} lemma times_cont_mdiff_const : times_cont_mdiff I I' n (λ (x : M), c) := begin assume x, refine ⟨continuous_within_at_const, _⟩, simp only [times_cont_diff_within_at_prop, (∘)], exact times_cont_diff_within_at_const, end lemma smooth_const : smooth I I' (λ (x : M), c) := times_cont_mdiff_const lemma times_cont_mdiff_on_const : times_cont_mdiff_on I I' n (λ (x : M), c) s := times_cont_mdiff_const.times_cont_mdiff_on lemma smooth_on_const : smooth_on I I' (λ (x : M), c) s := times_cont_mdiff_on_const lemma times_cont_mdiff_at_const : times_cont_mdiff_at I I' n (λ (x : M), c) x := times_cont_mdiff_const.times_cont_mdiff_at lemma smooth_at_const : smooth_at I I' (λ (x : M), c) x := times_cont_mdiff_at_const lemma times_cont_mdiff_within_at_const : times_cont_mdiff_within_at I I' n (λ (x : M), c) s x := times_cont_mdiff_at_const.times_cont_mdiff_within_at lemma smooth_within_at_const : smooth_within_at I I' (λ (x : M), c) s x := times_cont_mdiff_within_at_const end id /-! ### Equivalence with the basic definition for functions between vector spaces -/ section vector_space lemma times_cont_mdiff_within_at_iff_times_cont_diff_within_at {f : E → E'} {s : set E} {x : E} : times_cont_mdiff_within_at (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') n f s x ↔ times_cont_diff_within_at 𝕜 n f s x := begin simp only [times_cont_mdiff_within_at, lift_prop_within_at, times_cont_diff_within_at_prop, iff_def] with mfld_simps {contextual := tt}, exact times_cont_diff_within_at.continuous_within_at end lemma times_cont_mdiff_at_iff_times_cont_diff_at {f : E → E'} {x : E} : times_cont_mdiff_at (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') n f x ↔ times_cont_diff_at 𝕜 n f x := by rw [← times_cont_mdiff_within_at_univ, times_cont_mdiff_within_at_iff_times_cont_diff_within_at, times_cont_diff_within_at_univ] lemma times_cont_mdiff_on_iff_times_cont_diff_on {f : E → E'} {s : set E} : times_cont_mdiff_on (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') n f s ↔ times_cont_diff_on 𝕜 n f s := forall_congr $ by simp [times_cont_mdiff_within_at_iff_times_cont_diff_within_at] lemma times_cont_mdiff_iff_times_cont_diff {f : E → E'} : times_cont_mdiff (model_with_corners_self 𝕜 E) (model_with_corners_self 𝕜 E') n f ↔ times_cont_diff 𝕜 n f := by rw [← times_cont_diff_on_univ, ← times_cont_mdiff_on_univ, times_cont_mdiff_on_iff_times_cont_diff_on] end vector_space /-! ### The tangent map of a smooth function is smooth -/ section tangent_map /-- If a function is `C^n` with `1 ≤ n` on a domain with unique derivatives, then its bundled derivative is continuous. In this auxiliary lemma, we prove this fact when the source and target space are model spaces in models with corners. The general fact is proved in `times_cont_mdiff_on.continuous_on_tangent_map_within`-/ lemma times_cont_mdiff_on.continuous_on_tangent_map_within_aux {f : H → H'} {s : set H} (hf : times_cont_mdiff_on I I' n f s) (hn : 1 ≤ n) (hs : unique_mdiff_on I s) : continuous_on (tangent_map_within I I' f s) ((tangent_bundle.proj I H) ⁻¹' s) := begin suffices h : continuous_on (λ (p : H × E), (f p.fst, (fderiv_within 𝕜 (written_in_ext_chart_at I I' p.fst f) (I.symm ⁻¹' s ∩ range I) ((ext_chart_at I p.fst) p.fst) : E →L[𝕜] E') p.snd)) (prod.fst ⁻¹' s), { have A := (tangent_bundle_model_space_homeomorph H I).continuous, rw continuous_iff_continuous_on_univ at A, have B := ((tangent_bundle_model_space_homeomorph H' I').symm.continuous.comp_continuous_on h) .comp' A, have : (univ ∩ ⇑(tangent_bundle_model_space_homeomorph H I) ⁻¹' (prod.fst ⁻¹' s)) = tangent_bundle.proj I H ⁻¹' s, by { ext ⟨x, v⟩, simp only with mfld_simps }, rw this at B, apply B.congr, rintros ⟨x, v⟩ hx, dsimp [tangent_map_within], ext, { refl }, simp only with mfld_simps, apply congr_fun, apply congr_arg, rw mdifferentiable_within_at.mfderiv_within (hf.mdifferentiable_on hn x hx), refl }, suffices h : continuous_on (λ (p : H × E), (fderiv_within 𝕜 (I' ∘ f ∘ I.symm) (I.symm ⁻¹' s ∩ range I) (I p.fst) : E →L[𝕜] E') p.snd) (prod.fst ⁻¹' s), { dsimp [written_in_ext_chart_at, ext_chart_at], apply continuous_on.prod (continuous_on.comp hf.continuous_on continuous_fst.continuous_on (subset.refl _)), apply h.congr, assume p hp, refl }, suffices h : continuous_on (fderiv_within 𝕜 (I' ∘ f ∘ I.symm) (I.symm ⁻¹' s ∩ range I)) (I '' s), { have C := continuous_on.comp h I.continuous_to_fun.continuous_on (subset.refl _), have A : continuous (λq : (E →L[𝕜] E') × E, q.1 q.2) := is_bounded_bilinear_map_apply.continuous, have B : continuous_on (λp : H × E, (fderiv_within 𝕜 (I' ∘ f ∘ I.symm) (I.symm ⁻¹' s ∩ range I) (I p.1), p.2)) (prod.fst ⁻¹' s), { apply continuous_on.prod _ continuous_snd.continuous_on, refine (continuous_on.comp C continuous_fst.continuous_on _ : _), exact preimage_mono (subset_preimage_image _ _) }, exact A.comp_continuous_on B }, rw times_cont_mdiff_on_iff at hf, let x : H := I.symm (0 : E), let y : H' := I'.symm (0 : E'), have A := hf.2 x y, simp only [I.image, inter_comm] with mfld_simps at A ⊢, apply A.continuous_on_fderiv_within _ hn, convert hs.unique_diff_on x using 1, simp only [inter_comm] with mfld_simps end /-- If a function is `C^n` on a domain with unique derivatives, then its bundled derivative is `C^m` when `m+1 ≤ n`. In this auxiliary lemma, we prove this fact when the source and target space are model spaces in models with corners. The general fact is proved in `times_cont_mdiff_on.times_cont_mdiff_on_tangent_map_within` -/ lemma times_cont_mdiff_on.times_cont_mdiff_on_tangent_map_within_aux {f : H → H'} {s : set H} (hf : times_cont_mdiff_on I I' n f s) (hmn : m + 1 ≤ n) (hs : unique_mdiff_on I s) : times_cont_mdiff_on I.tangent I'.tangent m (tangent_map_within I I' f s) ((tangent_bundle.proj I H) ⁻¹' s) := begin have m_le_n : m ≤ n, { apply le_trans _ hmn, have : m + 0 ≤ m + 1 := add_le_add_left (zero_le _) _, simpa only [add_zero] using this }, have one_le_n : 1 ≤ n, { apply le_trans _ hmn, change 0 + 1 ≤ m + 1, exact add_le_add_right (zero_le _) _ }, have U': unique_diff_on 𝕜 (range I ∩ I.symm ⁻¹' s), { assume y hy, simpa only [unique_mdiff_on, unique_mdiff_within_at, hy.1, inter_comm] with mfld_simps using hs (I.symm y) hy.2 }, have U : unique_diff_on 𝕜 (set.prod (range I ∩ I.symm ⁻¹' s) (univ : set E)) := U'.prod unique_diff_on_univ, rw times_cont_mdiff_on_iff, refine ⟨hf.continuous_on_tangent_map_within_aux one_le_n hs, λp q, _⟩, have A : (range I).prod univ ∩ ((equiv.sigma_equiv_prod H E).symm ∘ λ (p : E × E), ((I.symm) p.fst, p.snd)) ⁻¹' (tangent_bundle.proj I H ⁻¹' s) = set.prod (range I ∩ I.symm ⁻¹' s) univ, by { ext ⟨x, v⟩, simp only with mfld_simps }, suffices h : times_cont_diff_on 𝕜 m (((λ (p : H' × E'), (I' p.fst, p.snd)) ∘ (equiv.sigma_equiv_prod H' E')) ∘ tangent_map_within I I' f s ∘ ((equiv.sigma_equiv_prod H E).symm) ∘ λ (p : E × E), (I.symm p.fst, p.snd)) ((range ⇑I ∩ ⇑(I.symm) ⁻¹' s).prod univ), by simpa [A] using h, change times_cont_diff_on 𝕜 m (λ (p : E × E), ((I' (f (I.symm p.fst)), ((mfderiv_within I I' f s (I.symm p.fst)) : E → E') p.snd) : E' × E')) (set.prod (range I ∩ I.symm ⁻¹' s) univ), -- check that all bits in this formula are `C^n` have hf' := times_cont_mdiff_on_iff.1 hf, have A : times_cont_diff_on 𝕜 m (I' ∘ f ∘ I.symm) (range I ∩ I.symm ⁻¹' s) := by simpa only with mfld_simps using (hf'.2 (I.symm 0) (I'.symm 0)).of_le m_le_n, have B : times_cont_diff_on 𝕜 m ((I' ∘ f ∘ I.symm) ∘ prod.fst) (set.prod (range I ∩ I.symm ⁻¹' s) (univ : set E)) := A.comp (times_cont_diff_fst.times_cont_diff_on) (prod_subset_preimage_fst _ _), suffices C : times_cont_diff_on 𝕜 m (λ (p : E × E), ((fderiv_within 𝕜 (I' ∘ f ∘ I.symm) (I.symm ⁻¹' s ∩ range I) p.1 : _) p.2)) (set.prod (range I ∩ I.symm ⁻¹' s) univ), { apply times_cont_diff_on.prod B _, apply C.congr (λp hp, _), simp only with mfld_simps at hp, simp only [mfderiv_within, hf.mdifferentiable_on one_le_n _ hp.2, hp.1, dif_pos] with mfld_simps }, have D : times_cont_diff_on 𝕜 m (λ x, (fderiv_within 𝕜 (I' ∘ f ∘ I.symm) (I.symm ⁻¹' s ∩ range I) x)) (range I ∩ I.symm ⁻¹' s), { have : times_cont_diff_on 𝕜 n (I' ∘ f ∘ I.symm) (range I ∩ I.symm ⁻¹' s) := by simpa only with mfld_simps using (hf'.2 (I.symm 0) (I'.symm 0)), simpa only [inter_comm] using this.fderiv_within U' hmn }, have := D.comp (times_cont_diff_fst.times_cont_diff_on) (prod_subset_preimage_fst _ _), have := times_cont_diff_on.prod this (times_cont_diff_snd.times_cont_diff_on), exact is_bounded_bilinear_map_apply.times_cont_diff.comp_times_cont_diff_on this, end include Is I's /-- If a function is `C^n` on a domain with unique derivatives, then its bundled derivative is `C^m` when `m+1 ≤ n`. -/ theorem times_cont_mdiff_on.times_cont_mdiff_on_tangent_map_within (hf : times_cont_mdiff_on I I' n f s) (hmn : m + 1 ≤ n) (hs : unique_mdiff_on I s) : times_cont_mdiff_on I.tangent I'.tangent m (tangent_map_within I I' f s) ((tangent_bundle.proj I M) ⁻¹' s) := begin /- The strategy of the proof is to avoid unfolding the definitions, and reduce by functoriality to the case of functions on the model spaces, where we have already proved the result. Let `l` and `r` be the charts to the left and to the right, so that we have ``` l^{-1} f r H --------> M ---> M' ---> H' ``` Then the tangent map `T(r ∘ f ∘ l)` is smooth by a previous result. Consider the composition ``` Tl T(r ∘ f ∘ l^{-1}) Tr^{-1} TM -----> TH -------------------> TH' ---------> TM' ``` where `Tr^{-1}` and `Tl` are the tangent maps of `r^{-1}` and `l`. Writing `Tl` and `Tr^{-1}` as composition of charts (called `Dl` and `il` for `l` and `Dr` and `ir` in the proof below), it follows that they are smooth. The composition of all these maps is `Tf`, and is therefore smooth as a composition of smooth maps. -/ have m_le_n : m ≤ n, { apply le_trans _ hmn, have : m + 0 ≤ m + 1 := add_le_add_left (zero_le _) _, simpa only [add_zero] }, have one_le_n : 1 ≤ n, { apply le_trans _ hmn, change 0 + 1 ≤ m + 1, exact add_le_add_right (zero_le _) _ }, /- First step: local reduction on the space, to a set `s'` which is contained in chart domains. -/ refine times_cont_mdiff_on_of_locally_times_cont_mdiff_on (λp hp, _), have hf' := times_cont_mdiff_on_iff.1 hf, simp [tangent_bundle.proj] at hp, let l := chart_at H p.1, set Dl := chart_at (model_prod H E) p with hDl, let r := chart_at H' (f p.1), let Dr := chart_at (model_prod H' E') (tangent_map_within I I' f s p), let il := chart_at (model_prod H E) (tangent_map I I l p), let ir := chart_at (model_prod H' E') (tangent_map I I' (r ∘ f) p), let s' := f ⁻¹' r.source ∩ s ∩ l.source, let s'_lift := (tangent_bundle.proj I M)⁻¹' s', let s'l := l.target ∩ l.symm ⁻¹' s', let s'l_lift := (tangent_bundle.proj I H) ⁻¹' s'l, rcases continuous_on_iff'.1 hf'.1 r.source r.open_source with ⟨o, o_open, ho⟩, suffices h : times_cont_mdiff_on I.tangent I'.tangent m (tangent_map_within I I' f s) s'_lift, { refine ⟨(tangent_bundle.proj I M)⁻¹' (o ∩ l.source), _, _, _⟩, show is_open ((tangent_bundle.proj I M)⁻¹' (o ∩ l.source)), from tangent_bundle_proj_continuous _ _ _ (is_open_inter o_open l.open_source), show p ∈ tangent_bundle.proj I M ⁻¹' (o ∩ l.source), { simp [tangent_bundle.proj] at ⊢, have : p.1 ∈ f ⁻¹' r.source ∩ s, by simp [hp], rw ho at this, exact this.1 }, { have : tangent_bundle.proj I M ⁻¹' s ∩ tangent_bundle.proj I M ⁻¹' (o ∩ l.source) = s'_lift, { dsimp only [s'_lift, s'], rw [ho], mfld_set_tac }, rw this, exact h } }, /- Second step: check that all functions are smooth, and use the chain rule to write the bundled derivative as a composition of a function between model spaces and of charts. Convention: statements about the differentiability of `a ∘ b ∘ c` are named `diff_abc`. Statements about differentiability in the bundle have a `_lift` suffix. -/ have U' : unique_mdiff_on I s', { apply unique_mdiff_on.inter _ l.open_source, rw [ho, inter_comm], exact hs.inter o_open }, have U'l : unique_mdiff_on I s'l := U'.unique_mdiff_on_preimage (mdifferentiable_chart _ _), have diff_f : times_cont_mdiff_on I I' n f s' := hf.mono (by mfld_set_tac), have diff_r : times_cont_mdiff_on I' I' n r r.source := times_cont_mdiff_on_chart, have diff_rf : times_cont_mdiff_on I I' n (r ∘ f) s', { apply times_cont_mdiff_on.comp diff_r diff_f (λx hx, _), simp only [s'] with mfld_simps at hx, simp only [hx] with mfld_simps }, have diff_l : times_cont_mdiff_on I I n l.symm s'l, { have A : times_cont_mdiff_on I I n l.symm l.target := times_cont_mdiff_on_chart_symm, exact A.mono (by mfld_set_tac) }, have diff_rfl : times_cont_mdiff_on I I' n (r ∘ f ∘ l.symm) s'l, { apply times_cont_mdiff_on.comp diff_rf diff_l, mfld_set_tac }, have diff_rfl_lift : times_cont_mdiff_on I.tangent I'.tangent m (tangent_map_within I I' (r ∘ f ∘ l.symm) s'l) s'l_lift := diff_rfl.times_cont_mdiff_on_tangent_map_within_aux hmn U'l, have diff_irrfl_lift : times_cont_mdiff_on I.tangent I'.tangent m (ir ∘ (tangent_map_within I I' (r ∘ f ∘ l.symm) s'l)) s'l_lift, { have A : times_cont_mdiff_on I'.tangent I'.tangent m ir ir.source := times_cont_mdiff_on_chart, exact times_cont_mdiff_on.comp A diff_rfl_lift (λp hp, by simp only [ir] with mfld_simps) }, have diff_Drirrfl_lift : times_cont_mdiff_on I.tangent I'.tangent m (Dr.symm ∘ (ir ∘ (tangent_map_within I I' (r ∘ f ∘ l.symm) s'l))) s'l_lift, { have A : times_cont_mdiff_on I'.tangent I'.tangent m Dr.symm Dr.target := times_cont_mdiff_on_chart_symm, apply times_cont_mdiff_on.comp A diff_irrfl_lift (λp hp, _), simp only [s'l_lift, tangent_bundle.proj] with mfld_simps at hp, simp only [ir, @local_equiv.refl_coe (model_prod H' E'), hp] with mfld_simps }, -- conclusion of this step: the composition of all the maps above is smooth have diff_DrirrflilDl : times_cont_mdiff_on I.tangent I'.tangent m (Dr.symm ∘ (ir ∘ (tangent_map_within I I' (r ∘ f ∘ l.symm) s'l)) ∘ (il.symm ∘ Dl)) s'_lift, { have A : times_cont_mdiff_on I.tangent I.tangent m Dl Dl.source := times_cont_mdiff_on_chart, have A' : times_cont_mdiff_on I.tangent I.tangent m Dl s'_lift, { apply A.mono (λp hp, _), simp only [s'_lift, tangent_bundle.proj] with mfld_simps at hp, simp only [Dl, hp] with mfld_simps }, have B : times_cont_mdiff_on I.tangent I.tangent m il.symm il.target := times_cont_mdiff_on_chart_symm, have C : times_cont_mdiff_on I.tangent I.tangent m (il.symm ∘ Dl) s'_lift := times_cont_mdiff_on.comp B A' (λp hp, by simp only [il] with mfld_simps), apply times_cont_mdiff_on.comp diff_Drirrfl_lift C (λp hp, _), simp only [s'_lift, tangent_bundle.proj] with mfld_simps at hp, simp only [il, s'l_lift, hp, tangent_bundle.proj] with mfld_simps }, /- Third step: check that the composition of all the maps indeed coincides with the derivative we are looking for -/ have eq_comp : ∀q ∈ s'_lift, tangent_map_within I I' f s q = (Dr.symm ∘ ir ∘ (tangent_map_within I I' (r ∘ f ∘ l.symm) s'l) ∘ (il.symm ∘ Dl)) q, { assume q hq, simp only [s'_lift, tangent_bundle.proj] with mfld_simps at hq, have U'q : unique_mdiff_within_at I s' q.1, by { apply U', simp only [hq, s'] with mfld_simps }, have U'lq : unique_mdiff_within_at I s'l (Dl q).1, by { apply U'l, simp only [hq, s'l] with mfld_simps }, have A : tangent_map_within I I' ((r ∘ f) ∘ l.symm) s'l (il.symm (Dl q)) = tangent_map_within I I' (r ∘ f) s' (tangent_map_within I I l.symm s'l (il.symm (Dl q))), { refine tangent_map_within_comp_at (il.symm (Dl q)) _ _ (λp hp, _) U'lq, { apply diff_rf.mdifferentiable_on one_le_n, simp only [hq] with mfld_simps }, { apply diff_l.mdifferentiable_on one_le_n, simp only [s'l, hq] with mfld_simps }, { simp only with mfld_simps at hp, simp only [hp] with mfld_simps } }, have B : tangent_map_within I I l.symm s'l (il.symm (Dl q)) = q, { have : tangent_map_within I I l.symm s'l (il.symm (Dl q)) = tangent_map I I l.symm (il.symm (Dl q)), { refine tangent_map_within_eq_tangent_map U'lq _, refine mdifferentiable_at_atlas_symm _ (chart_mem_atlas _ _) _, simp only [hq] with mfld_simps }, rw [this, tangent_map_chart_symm, hDl], { simp only [hq] with mfld_simps, have : q ∈ (chart_at (model_prod H E) p).source, by simp only [hq] with mfld_simps, exact (chart_at (model_prod H E) p).left_inv this }, { simp only [hq] with mfld_simps } }, have C : tangent_map_within I I' (r ∘ f) s' q = tangent_map_within I' I' r r.source (tangent_map_within I I' f s' q), { refine tangent_map_within_comp_at q _ _ (λr hr, _) U'q, { apply diff_r.mdifferentiable_on one_le_n, simp only [hq] with mfld_simps }, { apply diff_f.mdifferentiable_on one_le_n, simp only [hq] with mfld_simps }, { simp only [s'] with mfld_simps at hr, simp only [hr] with mfld_simps } }, have D : Dr.symm (ir (tangent_map_within I' I' r r.source (tangent_map_within I I' f s' q))) = tangent_map_within I I' f s' q, { have A : tangent_map_within I' I' r r.source (tangent_map_within I I' f s' q) = tangent_map I' I' r (tangent_map_within I I' f s' q), { apply tangent_map_within_eq_tangent_map, { apply is_open.unique_mdiff_within_at _ r.open_source, simp [hq] }, { refine mdifferentiable_at_atlas _ (chart_mem_atlas _ _) _, simp only [hq] with mfld_simps } }, have : f p.1 = (tangent_map_within I I' f s p).1 := rfl, rw [A], dsimp [r, Dr], rw [this, tangent_map_chart], { simp only [hq] with mfld_simps, have : tangent_map_within I I' f s' q ∈ (chart_at (model_prod H' E') (tangent_map_within I I' f s p)).source, by simp only [hq] with mfld_simps, exact (chart_at (model_prod H' E') (tangent_map_within I I' f s p)).left_inv this }, { simp only [hq] with mfld_simps } }, have E : tangent_map_within I I' f s' q = tangent_map_within I I' f s q, { refine tangent_map_within_subset (by mfld_set_tac) U'q _, apply hf.mdifferentiable_on one_le_n, simp only [hq] with mfld_simps }, simp only [(∘), A, B, C, D, E.symm] }, exact diff_DrirrflilDl.congr eq_comp, end /-- If a function is `C^n` on a domain with unique derivatives, with `1 ≤ n`, then its bundled derivative is continuous there. -/ theorem times_cont_mdiff_on.continuous_on_tangent_map_within (hf : times_cont_mdiff_on I I' n f s) (hmn : 1 ≤ n) (hs : unique_mdiff_on I s) : continuous_on (tangent_map_within I I' f s) ((tangent_bundle.proj I M) ⁻¹' s) := begin have : times_cont_mdiff_on I.tangent I'.tangent 0 (tangent_map_within I I' f s) ((tangent_bundle.proj I M) ⁻¹' s) := hf.times_cont_mdiff_on_tangent_map_within hmn hs, exact this.continuous_on end /-- If a function is `C^n`, then its bundled derivative is `C^m` when `m+1 ≤ n`. -/ theorem times_cont_mdiff.times_cont_mdiff_tangent_map (hf : times_cont_mdiff I I' n f) (hmn : m + 1 ≤ n) : times_cont_mdiff I.tangent I'.tangent m (tangent_map I I' f) := begin rw ← times_cont_mdiff_on_univ at hf ⊢, convert hf.times_cont_mdiff_on_tangent_map_within hmn unique_mdiff_on_univ, rw tangent_map_within_univ end /-- If a function is `C^n`, with `1 ≤ n`, then its bundled derivative is continuous. -/ theorem times_cont_mdiff.continuous_tangent_map (hf : times_cont_mdiff I I' n f) (hmn : 1 ≤ n) : continuous (tangent_map I I' f) := begin rw ← times_cont_mdiff_on_univ at hf, rw continuous_iff_continuous_on_univ, convert hf.continuous_on_tangent_map_within hmn unique_mdiff_on_univ, rw tangent_map_within_univ end end tangent_map /-! ### Smoothness of the projection in a basic smooth bundle -/ namespace basic_smooth_bundle_core variables (Z : basic_smooth_bundle_core I M E') lemma times_cont_mdiff_proj : times_cont_mdiff ((I.prod (model_with_corners_self 𝕜 E'))) I n Z.to_topological_fiber_bundle_core.proj := begin assume x, rw [times_cont_mdiff_at, times_cont_mdiff_within_at_iff], refine ⟨Z.to_topological_fiber_bundle_core.continuous_proj.continuous_at.continuous_within_at, _⟩, simp only [(∘), chart_at, chart] with mfld_simps, apply times_cont_diff_within_at_fst.congr, { rintros ⟨a, b⟩ hab, simp only with mfld_simps at hab, simp only [hab] with mfld_simps }, { simp only with mfld_simps } end lemma smooth_proj : smooth ((I.prod (model_with_corners_self 𝕜 E'))) I Z.to_topological_fiber_bundle_core.proj := times_cont_mdiff_proj Z lemma times_cont_mdiff_on_proj {s : set (Z.to_topological_fiber_bundle_core.total_space)} : times_cont_mdiff_on ((I.prod (model_with_corners_self 𝕜 E'))) I n Z.to_topological_fiber_bundle_core.proj s := Z.times_cont_mdiff_proj.times_cont_mdiff_on lemma smooth_on_proj {s : set (Z.to_topological_fiber_bundle_core.total_space)} : smooth_on ((I.prod (model_with_corners_self 𝕜 E'))) I Z.to_topological_fiber_bundle_core.proj s := times_cont_mdiff_on_proj Z lemma times_cont_mdiff_at_proj {p : Z.to_topological_fiber_bundle_core.total_space} : times_cont_mdiff_at ((I.prod (model_with_corners_self 𝕜 E'))) I n Z.to_topological_fiber_bundle_core.proj p := Z.times_cont_mdiff_proj.times_cont_mdiff_at lemma smooth_at_proj {p : Z.to_topological_fiber_bundle_core.total_space} : smooth_at ((I.prod (model_with_corners_self 𝕜 E'))) I Z.to_topological_fiber_bundle_core.proj p := Z.times_cont_mdiff_at_proj lemma times_cont_mdiff_within_at_proj {s : set (Z.to_topological_fiber_bundle_core.total_space)} {p : Z.to_topological_fiber_bundle_core.total_space} : times_cont_mdiff_within_at ((I.prod (model_with_corners_self 𝕜 E'))) I n Z.to_topological_fiber_bundle_core.proj s p := Z.times_cont_mdiff_at_proj.times_cont_mdiff_within_at lemma smooth_within_at_proj {s : set (Z.to_topological_fiber_bundle_core.total_space)} {p : Z.to_topological_fiber_bundle_core.total_space} : smooth_within_at ((I.prod (model_with_corners_self 𝕜 E'))) I Z.to_topological_fiber_bundle_core.proj s p := Z.times_cont_mdiff_within_at_proj end basic_smooth_bundle_core /-! ### Smoothness of the tangent bundle projection -/ namespace tangent_bundle include Is lemma times_cont_mdiff_proj : times_cont_mdiff I.tangent I n (proj I M) := basic_smooth_bundle_core.times_cont_mdiff_proj _ lemma smooth_proj : smooth I.tangent I (proj I M) := basic_smooth_bundle_core.smooth_proj _ lemma times_cont_mdiff_on_proj {s : set (tangent_bundle I M)} : times_cont_mdiff_on I.tangent I n (proj I M) s := basic_smooth_bundle_core.times_cont_mdiff_on_proj _ lemma smooth_on_proj {s : set (tangent_bundle I M)} : smooth_on I.tangent I (proj I M) s := basic_smooth_bundle_core.smooth_on_proj _ lemma times_cont_mdiff_at_proj {p : tangent_bundle I M} : times_cont_mdiff_at I.tangent I n (proj I M) p := basic_smooth_bundle_core.times_cont_mdiff_at_proj _ lemma smooth_at_proj {p : tangent_bundle I M} : smooth_at I.tangent I (proj I M) p := basic_smooth_bundle_core.smooth_at_proj _ lemma times_cont_mdiff_within_at_proj {s : set (tangent_bundle I M)} {p : tangent_bundle I M} : times_cont_mdiff_within_at I.tangent I n (proj I M) s p := basic_smooth_bundle_core.times_cont_mdiff_within_at_proj _ lemma smooth_within_at_proj {s : set (tangent_bundle I M)} {p : tangent_bundle I M} : smooth_within_at I.tangent I (proj I M) s p := basic_smooth_bundle_core.smooth_within_at_proj _ end tangent_bundle /-! ### Smoothness of standard maps associated to the product of manifolds -/ section prod_mk lemma times_cont_mdiff_within_at.prod_mk {f : M → M'} {g : M → N'} (hf : times_cont_mdiff_within_at I I' n f s x) (hg : times_cont_mdiff_within_at I J' n g s x) : times_cont_mdiff_within_at I (I'.prod J') n (λ x, (f x, g x)) s x := begin rw times_cont_mdiff_within_at_iff at *, refine ⟨hf.1.prod hg.1, (hf.2.mono _).prod (hg.2.mono _)⟩; mfld_set_tac, end lemma times_cont_mdiff_at.prod_mk {f : M → M'} {g : M → N'} (hf : times_cont_mdiff_at I I' n f x) (hg : times_cont_mdiff_at I J' n g x) : times_cont_mdiff_at I (I'.prod J') n (λ x, (f x, g x)) x := hf.prod_mk hg lemma times_cont_mdiff_on.prod_mk {f : M → M'} {g : M → N'} (hf : times_cont_mdiff_on I I' n f s) (hg : times_cont_mdiff_on I J' n g s) : times_cont_mdiff_on I (I'.prod J') n (λ x, (f x, g x)) s := λ x hx, (hf x hx).prod_mk (hg x hx) lemma times_cont_mdiff.prod_mk {f : M → M'} {g : M → N'} (hf : times_cont_mdiff I I' n f) (hg : times_cont_mdiff I J' n g) : times_cont_mdiff I (I'.prod J') n (λ x, (f x, g x)) := λ x, (hf x).prod_mk (hg x) lemma smooth_within_at.prod_mk {f : M → M'} {g : M → N'} (hf : smooth_within_at I I' f s x) (hg : smooth_within_at I J' g s x) : smooth_within_at I (I'.prod J') (λ x, (f x, g x)) s x := hf.prod_mk hg lemma smooth_at.prod_mk {f : M → M'} {g : M → N'} (hf : smooth_at I I' f x) (hg : smooth_at I J' g x) : smooth_at I (I'.prod J') (λ x, (f x, g x)) x := hf.prod_mk hg lemma smooth_on.prod_mk {f : M → M'} {g : M → N'} (hf : smooth_on I I' f s) (hg : smooth_on I J' g s) : smooth_on I (I'.prod J') (λ x, (f x, g x)) s := hf.prod_mk hg lemma smooth.prod_mk {f : M → M'} {g : M → N'} (hf : smooth I I' f) (hg : smooth I J' g) : smooth I (I'.prod J') (λ x, (f x, g x)) := hf.prod_mk hg end prod_mk section projections lemma times_cont_mdiff_within_at_fst {s : set (M × N)} {p : M × N} : times_cont_mdiff_within_at (I.prod J) I n prod.fst s p := begin rw times_cont_mdiff_within_at_iff, refine ⟨continuous_within_at_fst, _⟩, refine times_cont_diff_within_at_fst.congr (λ y hy, _) _, { simp only with mfld_simps at hy, simp only [hy] with mfld_simps }, { simp only with mfld_simps } end lemma times_cont_mdiff_at_fst {p : M × N} : times_cont_mdiff_at (I.prod J) I n prod.fst p := times_cont_mdiff_within_at_fst lemma times_cont_mdiff_on_fst {s : set (M × N)} : times_cont_mdiff_on (I.prod J) I n prod.fst s := λ x hx, times_cont_mdiff_within_at_fst lemma times_cont_mdiff_fst : times_cont_mdiff (I.prod J) I n (@prod.fst M N) := λ x, times_cont_mdiff_at_fst lemma smooth_within_at_fst {s : set (M × N)} {p : M × N} : smooth_within_at (I.prod J) I prod.fst s p := times_cont_mdiff_within_at_fst lemma smooth_at_fst {p : M × N} : smooth_at (I.prod J) I prod.fst p := times_cont_mdiff_at_fst lemma smooth_on_fst {s : set (M × N)} : smooth_on (I.prod J) I prod.fst s := times_cont_mdiff_on_fst lemma smooth_fst : smooth (I.prod J) I (@prod.fst M N) := times_cont_mdiff_fst lemma times_cont_mdiff_within_at_snd {s : set (M × N)} {p : M × N} : times_cont_mdiff_within_at (I.prod J) J n prod.snd s p := begin rw times_cont_mdiff_within_at_iff, refine ⟨continuous_within_at_snd, _⟩, refine times_cont_diff_within_at_snd.congr (λ y hy, _) _, { simp only with mfld_simps at hy, simp only [hy] with mfld_simps }, { simp only with mfld_simps } end lemma times_cont_mdiff_at_snd {p : M × N} : times_cont_mdiff_at (I.prod J) J n prod.snd p := times_cont_mdiff_within_at_snd lemma times_cont_mdiff_on_snd {s : set (M × N)} : times_cont_mdiff_on (I.prod J) J n prod.snd s := λ x hx, times_cont_mdiff_within_at_snd lemma times_cont_mdiff_snd : times_cont_mdiff (I.prod J) J n (@prod.snd M N) := λ x, times_cont_mdiff_at_snd lemma smooth_within_at_snd {s : set (M × N)} {p : M × N} : smooth_within_at (I.prod J) J prod.snd s p := times_cont_mdiff_within_at_snd lemma smooth_at_snd {p : M × N} : smooth_at (I.prod J) J prod.snd p := times_cont_mdiff_at_snd lemma smooth_on_snd {s : set (M × N)} : smooth_on (I.prod J) J prod.snd s := times_cont_mdiff_on_snd lemma smooth_snd : smooth (I.prod J) J (@prod.snd M N) := times_cont_mdiff_snd include Is I's J's lemma smooth_iff_proj_smooth {f : M → M' × N'} : (smooth I (I'.prod J') f) ↔ (smooth I I' (prod.fst ∘ f)) ∧ (smooth I J' (prod.snd ∘ f)) := begin split, { intro h, exact ⟨smooth_fst.comp h, smooth_snd.comp h⟩ }, { rintro ⟨h_fst, h_snd⟩, simpa only [prod.mk.eta] using h_fst.prod_mk h_snd, } end end projections section prod_map variables {g : N → N'} {r : set N} {y : N} include Is I's Js J's /-- The product map of two `C^n` functions within a set at a point is `C^n` within the product set at the product point. -/ lemma times_cont_mdiff_within_at.prod_map' {p : M × N} (hf : times_cont_mdiff_within_at I I' n f s p.1) (hg : times_cont_mdiff_within_at J J' n g r p.2) : times_cont_mdiff_within_at (I.prod J) (I'.prod J') n (prod.map f g) (s.prod r) p := (hf.comp p times_cont_mdiff_within_at_fst (prod_subset_preimage_fst _ _)).prod_mk $ hg.comp p times_cont_mdiff_within_at_snd (prod_subset_preimage_snd _ _) lemma times_cont_mdiff_within_at.prod_map (hf : times_cont_mdiff_within_at I I' n f s x) (hg : times_cont_mdiff_within_at J J' n g r y) : times_cont_mdiff_within_at (I.prod J) (I'.prod J') n (prod.map f g) (s.prod r) (x, y) := times_cont_mdiff_within_at.prod_map' hf hg lemma times_cont_mdiff_at.prod_map (hf : times_cont_mdiff_at I I' n f x) (hg : times_cont_mdiff_at J J' n g y) : times_cont_mdiff_at (I.prod J) (I'.prod J') n (prod.map f g) (x, y) := begin rw ← times_cont_mdiff_within_at_univ at *, convert hf.prod_map hg, exact univ_prod_univ.symm end lemma times_cont_mdiff_at.prod_map' {p : M × N} (hf : times_cont_mdiff_at I I' n f p.1) (hg : times_cont_mdiff_at J J' n g p.2) : times_cont_mdiff_at (I.prod J) (I'.prod J') n (prod.map f g) p := begin rcases p, exact hf.prod_map hg end lemma times_cont_mdiff_on.prod_map (hf : times_cont_mdiff_on I I' n f s) (hg : times_cont_mdiff_on J J' n g r) : times_cont_mdiff_on (I.prod J) (I'.prod J') n (prod.map f g) (s.prod r) := (hf.comp times_cont_mdiff_on_fst (prod_subset_preimage_fst _ _)).prod_mk $ hg.comp (times_cont_mdiff_on_snd) (prod_subset_preimage_snd _ _) lemma times_cont_mdiff.prod_map (hf : times_cont_mdiff I I' n f) (hg : times_cont_mdiff J J' n g) : times_cont_mdiff (I.prod J) (I'.prod J') n (prod.map f g) := begin assume p, exact (hf p.1).prod_map' (hg p.2) end lemma smooth_within_at.prod_map (hf : smooth_within_at I I' f s x) (hg : smooth_within_at J J' g r y) : smooth_within_at (I.prod J) (I'.prod J') (prod.map f g) (s.prod r) (x, y) := hf.prod_map hg lemma smooth_at.prod_map (hf : smooth_at I I' f x) (hg : smooth_at J J' g y) : smooth_at (I.prod J) (I'.prod J') (prod.map f g) (x, y) := hf.prod_map hg lemma smooth_on.prod_map (hf : smooth_on I I' f s) (hg : smooth_on J J' g r) : smooth_on (I.prod J) (I'.prod J') (prod.map f g) (s.prod r) := hf.prod_map hg lemma smooth.prod_map (hf : smooth I I' f) (hg : smooth J J' g) : smooth (I.prod J) (I'.prod J') (prod.map f g) := hf.prod_map hg end prod_map
6150f0e7b1c2e38a77792fd27c2fe1b620605293
0c9c1ff8e5013c525bf1d72338b62db639374733
/library/data/rbtree/basic.lean
1b092d90ed2dbfedf291292e604ec0c011bf8de0
[ "Apache-2.0" ]
permissive
semorrison/lean
1f2bb450c3400098666ff6e43aa29b8e1e3cdc3a
85dcb385d5219f2fca8c73b2ebca270fe81337e0
refs/heads/master
1,638,526,143,586
1,634,825,588,000
1,634,825,588,000
258,650,844
0
0
Apache-2.0
1,587,772,955,000
1,587,772,954,000
null
UTF-8
Lean
false
false
8,767
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ universe u meta def tactic.interactive.blast_disjs : tactic unit := `[cases_type* or] namespace rbnode variables {α : Type u} open color nat inductive is_node_of : rbnode α → rbnode α → α → rbnode α → Prop | of_red (l v r) : is_node_of (red_node l v r) l v r | of_black (l v r) : is_node_of (black_node l v r) l v r def lift (lt : α → α → Prop) : option α → option α → Prop | (some a) (some b) := lt a b | _ _ := true inductive is_searchable (lt : α → α → Prop) : rbnode α → option α → option α → Prop | leaf_s {lo hi} (hlt : lift lt lo hi) : is_searchable leaf lo hi | red_s {l r v lo hi} (hs₁ : is_searchable l lo (some v)) (hs₂ : is_searchable r (some v) hi) : is_searchable (red_node l v r) lo hi | black_s {l r v lo hi} (hs₁ : is_searchable l lo (some v)) (hs₂ : is_searchable r (some v) hi) : is_searchable (black_node l v r) lo hi meta def is_searchable_tactic : tactic unit := `[ constructor_matching* [is_searchable _ leaf _ _, is_searchable _ (red_node _ _ _) _ _, is_searchable _ (black_node _ _ _) _ _]; cases_matching* [is_searchable _ leaf _ _, is_searchable _ (red_node _ _ _) _ _, is_searchable _ (black_node _ _ _) _ _]; try { assumption } ] open rbnode (mem) open is_searchable section is_searchable_lemmas variable {lt : α → α → Prop} lemma lo_lt_hi {t : rbnode α} {lt} [is_trans α lt] : ∀ {lo hi}, is_searchable lt t lo hi → lift lt lo hi := begin induction t; intros lo hi hs, case leaf { cases hs, assumption }, all_goals { cases hs, have h₁ := t_ih_lchild hs_hs₁, have h₂ := t_ih_rchild hs_hs₂, cases lo; cases hi; simp [lift] at *, apply trans_of lt h₁ h₂, } end variable [decidable_rel lt] lemma is_searchable_of_is_searchable_of_incomp [is_strict_weak_order α lt] {t} : ∀ {lo hi hi'} (hc : ¬ lt hi' hi ∧ ¬ lt hi hi') (hs : is_searchable lt t lo (some hi)), is_searchable lt t lo (some hi') := begin induction t; intros; is_searchable_tactic, { cases lo; simp [lift, *] at *, apply lt_of_lt_of_incomp, assumption, exact ⟨hc.2, hc.1⟩ }, all_goals { apply t_ih_rchild hc hs_hs₂ } end lemma is_searchable_of_incomp_of_is_searchable [is_strict_weak_order α lt] {t} : ∀ {lo lo' hi} (hc : ¬ lt lo' lo ∧ ¬ lt lo lo') (hs : is_searchable lt t (some lo) hi), is_searchable lt t (some lo') hi := begin induction t; intros; is_searchable_tactic, { cases hi; simp [lift, *] at *, apply lt_of_incomp_of_lt, assumption, assumption }, all_goals { apply t_ih_lchild hc hs_hs₁ } end lemma is_searchable_some_low_of_is_searchable_of_lt {t} [is_trans α lt] : ∀ {lo hi lo'} (hlt : lt lo' lo) (hs : is_searchable lt t (some lo) hi), is_searchable lt t (some lo') hi := begin induction t; intros; is_searchable_tactic, { cases hi; simp [lift, *] at *, apply trans_of lt hlt, assumption }, all_goals { apply t_ih_lchild hlt hs_hs₁ } end lemma is_searchable_none_low_of_is_searchable_some_low {t} : ∀ {y hi} (hlt : is_searchable lt t (some y) hi), is_searchable lt t none hi := begin induction t; intros; is_searchable_tactic, { simp [lift] }, all_goals { apply t_ih_lchild hlt_hs₁ } end lemma is_searchable_some_high_of_is_searchable_of_lt {t} [is_trans α lt] : ∀ {lo hi hi'} (hlt : lt hi hi') (hs : is_searchable lt t lo (some hi)), is_searchable lt t lo (some hi') := begin induction t; intros; is_searchable_tactic, { cases lo; simp [lift, *] at *, apply trans_of lt, assumption, assumption}, all_goals { apply t_ih_rchild hlt hs_hs₂ } end lemma is_searchable_none_high_of_is_searchable_some_high {t} : ∀ {lo y} (hlt : is_searchable lt t lo (some y)), is_searchable lt t lo none := begin induction t; intros; is_searchable_tactic, { cases lo; simp [lift] }, all_goals { apply t_ih_rchild hlt_hs₂ } end lemma range [is_strict_weak_order α lt] {t : rbnode α} {x} : ∀ {lo hi}, is_searchable lt t lo hi → mem lt x t → lift lt lo (some x) ∧ lift lt (some x) hi := begin induction t, case leaf { simp [mem], intros, trivial }, all_goals { -- red_node and black_node are identical intros lo hi h₁ h₂, cases h₁, simp only [mem] at h₂, have val_hi : lift lt (some t_val) hi, { apply lo_lt_hi, assumption }, have lo_val : lift lt lo (some t_val), { apply lo_lt_hi, assumption }, blast_disjs, { have h₃ : lift lt lo (some x) ∧ lift lt (some x) (some t_val), { apply t_ih_lchild, assumption, assumption }, cases h₃ with lo_x x_val, split, show lift lt lo (some x), { assumption }, show lift lt (some x ) hi, { cases hi with hi; simp [lift] at *, apply trans_of lt x_val val_hi } }, { cases h₂, cases lo with lo; cases hi with hi; simp [lift] at *, { apply lt_of_incomp_of_lt _ val_hi, simp [*] }, { apply lt_of_lt_of_incomp lo_val, simp [*] }, split, { apply lt_of_lt_of_incomp lo_val, simp [*] }, { apply lt_of_incomp_of_lt _ val_hi, simp [*] } }, { have h₃ : lift lt (some t_val) (some x) ∧ lift lt (some x) hi, { apply t_ih_rchild, assumption, assumption }, cases h₃ with val_x x_hi, cases lo with lo; cases hi with hi; simp [lift] at *, { assumption }, { apply trans_of lt lo_val val_x }, split, { apply trans_of lt lo_val val_x, }, { assumption } } } end lemma lt_of_mem_left [is_strict_weak_order α lt] {y : α} {t l r : rbnode α} : ∀ {lo hi}, is_searchable lt t lo hi → is_node_of t l y r → ∀ {x}, mem lt x l → lt x y := begin intros _ _ hs hn x hm, cases hn; cases hs, all_goals { exact (range hs_hs₁ hm).2 } end lemma lt_of_mem_right [is_strict_weak_order α lt] {y : α} {t l r : rbnode α} : ∀ {lo hi}, is_searchable lt t lo hi → is_node_of t l y r → ∀ {z}, mem lt z r → lt y z := begin intros _ _ hs hn z hm, cases hn; cases hs, all_goals { exact (range hs_hs₂ hm).1 } end lemma lt_of_mem_left_right [is_strict_weak_order α lt] {y : α} {t l r : rbnode α} : ∀ {lo hi}, is_searchable lt t lo hi → is_node_of t l y r → ∀ {x z}, mem lt x l → mem lt z r → lt x z := begin intros _ _ hs hn x z hm₁ hm₂, cases hn; cases hs, all_goals { have h₁ := range hs_hs₁ hm₁, have h₂ := range hs_hs₂ hm₂, exact trans_of lt h₁.2 h₂.1, } end end is_searchable_lemmas inductive is_red_black : rbnode α → color → nat → Prop | leaf_rb : is_red_black leaf black 0 | red_rb {v l r n} (rb_l : is_red_black l black n) (rb_r : is_red_black r black n) : is_red_black (red_node l v r) red n | black_rb {v l r n c₁ c₂} (rb_l : is_red_black l c₁ n) (rb_r : is_red_black r c₂ n) : is_red_black (black_node l v r) black (succ n) open is_red_black lemma depth_min : ∀ {c n} {t : rbnode α}, is_red_black t c n → depth min t ≥ n := begin intros c n' t h, induction h, case leaf_rb {apply le_refl}, case red_rb { simp [depth], have : min (depth min h_l) (depth min h_r) ≥ h_n, { apply le_min; assumption }, apply le_succ_of_le, assumption }, case black_rb { simp [depth], apply succ_le_succ, apply le_min; assumption } end private def upper : color → nat → nat | red n := 2*n + 1 | black n := 2*n private lemma upper_le : ∀ c n, upper c n ≤ 2 * n + 1 | red n := by apply le_refl | black n := by apply le_succ lemma depth_max' : ∀ {c n} {t : rbnode α}, is_red_black t c n → depth max t ≤ upper c n := begin intros c n' t h, induction h, case leaf_rb { simp [max, depth, upper, nat.mul_zero] }, case red_rb { suffices : succ (max (depth max h_l) (depth max h_r)) ≤ 2 * h_n + 1, { simp [depth, upper, *] at * }, apply succ_le_succ, apply max_le; assumption }, case black_rb { have : depth max h_l ≤ 2*h_n + 1, from le_trans h_ih_rb_l (upper_le _ _), have : depth max h_r ≤ 2*h_n + 1, from le_trans h_ih_rb_r (upper_le _ _), suffices new : max (depth max h_l) (depth max h_r) + 1 ≤ 2 * h_n + 2*1, { simp [depth, upper, succ_eq_add_one, nat.left_distrib, *] at * }, apply succ_le_succ, apply max_le; assumption } end lemma depth_max {c n} {t : rbnode α} (h : is_red_black t c n) : depth max t ≤ 2 * n + 1:= le_trans (depth_max' h) (upper_le _ _) lemma balanced {c n} {t : rbnode α} (h : is_red_black t c n) : 2 * depth min t + 1 ≥ depth max t := begin have : 2 * depth min t + 1 ≥ 2 * n + 1, { apply succ_le_succ, apply nat.mul_le_mul_left, apply depth_min h}, apply le_trans, apply depth_max h, apply this end end rbnode
62dff1a05dc74486b05e875da91a3bc091db38df
63abd62053d479eae5abf4951554e1064a4c45b4
/src/algebra/category/Group/images.lean
025223dcb2e20a685ce953a1ffccd349478406ba
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
3,299
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 algebra.category.Group.abelian import category_theory.limits.shapes.images import category_theory.limits.types /-! # The category of commutative additive groups has images. Note that we don't need to register any of the constructions here as instances, because we get them from the fact that `AddCommGroup` is an abelian category. -/ open category_theory open category_theory.limits universe u namespace AddCommGroup -- Note that because `injective_of_mono` is currently only proved in `Type 0`, -- we restrict to the lowest universe here for now. variables {G H : AddCommGroup.{0}} (f : G ⟶ H) local attribute [ext] subtype.ext_val section -- implementation details of `has_image` for AddCommGroup; use the API, not these /-- the image of a morphism in AddCommGroup is just the bundling of `add_monoid_hom.range f` -/ def image : AddCommGroup := AddCommGroup.of (add_monoid_hom.range f) /-- the inclusion of `image f` into the target -/ def image.ι : image f ⟶ H := f.range.subtype instance : mono (image.ι f) := concrete_category.mono_of_injective (image.ι f) subtype.val_injective /-- the corestriction map to the image -/ def factor_thru_image : G ⟶ image f := f.to_range lemma image.fac : factor_thru_image f ≫ image.ι f = f := by { ext, refl, } local attribute [simp] image.fac variables {f} /-- the universal property for the image factorisation -/ noncomputable def image.lift (F' : mono_factorisation f) : image f ⟶ F'.I := { to_fun := (λ x, F'.e (classical.indefinite_description _ x.2).1 : image f → F'.I), map_zero' := begin haveI := F'.m_mono, apply injective_of_mono F'.m, change (F'.e ≫ F'.m) _ = _, rw [F'.fac, add_monoid_hom.map_zero], convert (classical.indefinite_description (λ y, f y = 0) _).2, end, map_add' := begin intros x y, haveI := F'.m_mono, apply injective_of_mono F'.m, rw [add_monoid_hom.map_add], change (F'.e ≫ F'.m) _ = (F'.e ≫ F'.m) _ + (F'.e ≫ F'.m) _, rw [F'.fac], rw (classical.indefinite_description (λ z, f z = _) _).2, rw (classical.indefinite_description (λ z, f z = _) _).2, rw (classical.indefinite_description (λ z, f z = _) _).2, refl, end, } lemma image.lift_fac (F' : mono_factorisation f) : image.lift F' ≫ F'.m = image.ι f := begin ext x, change (F'.e ≫ F'.m) _ = _, rw [F'.fac, (classical.indefinite_description _ x.2).2], refl, end end /-- the factorisation of any morphism in AddCommGroup through a mono. -/ def mono_factorisation : mono_factorisation f := { I := image f, m := image.ι f, e := factor_thru_image f } /-- the factorisation of any morphism in AddCommGroup through a mono has the universal property of the image. -/ noncomputable def is_image : is_image (mono_factorisation f) := { lift := image.lift, lift_fac' := image.lift_fac } /-- The categorical image of a morphism in `AddCommGroup` agrees with the usual group-theoretical range. -/ noncomputable def image_iso_range {G H : AddCommGroup.{0}} (f : G ⟶ H) : limits.image f ≅ AddCommGroup.of f.range := is_image.iso_ext (image.is_image f) (is_image f) end AddCommGroup
456266622602040710ff5cdfb4cb3c9117bff297
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/run/cdotTests.lean
084a35cfacd9ee465d65cfe0cabcde760a06348d
[ "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
591
lean
new_frontend class Inc (α : Type) := (inc : α → α) export Inc (inc) instance {α} [Inc α] : Inc (List α) := { inc := (·.map inc) } instance : Inc Nat := { inc := Nat.succ } #eval inc 10 #eval inc [1, 2, 3] theorem ex1 : [(1, "hello"), (2, "world")].map (·.1) = [1, 2] := rfl theorem ex2 : [(1, "hello"), (2, "world")].map (·.snd) = ["hello", "world"] := rfl def sum (xs : List Nat) : Nat := (·.2) $ Id.run $ StateT.run (s:=0) do xs.forM fun x => modify (· + x) #eval sum [1, 2, 3, 4] theorem ex3 : sum [1, 2, 3] = 6 := rfl theorem ex4 : sum [1, 2, 3, 4] = 10 := rfl
90236170f9495d72a7f9d88194168288351d7610
97c8e5d8aca4afeebb5b335f26a492c53680efc8
/ground_zero/HITs/colimit.lean
7c93c83ec1d01846f1dabbf36747da9a7bc67bd6
[]
no_license
jfrancese/lean
cf32f0d8d5520b6f0e9d3987deb95841c553c53c
06e7efaecce4093d97fb5ecc75479df2ef1dbbdb
refs/heads/master
1,587,915,151,351
1,551,012,140,000
1,551,012,140,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,770
lean
import ground_zero.types.heq open ground_zero.types.dep_path (pathover_of_eq) /- Sequential colimit. * https://homotopytypetheory.org/2016/01/08/colimits-in-hott/ -/ namespace ground_zero.HITs universes u v inductive colimit.core (α : ℕ → Sort u) (f : Π (n : ℕ), α n → α (n + 1)) | incl {} {n : ℕ} : α n → colimit.core inductive colimit.rel (α : ℕ → Sort u) (f : Π (n : ℕ), α n → α (n + 1)) : colimit.core α f → colimit.core α f → Prop | glue (n : ℕ) (x : α n) : colimit.rel (core.incl (f n x)) (core.incl x) def colimit (α : ℕ → Sort u) (f : Π (n : ℕ), α n → α (n + 1)) := quot (colimit.rel α f) namespace colimit variables {α : ℕ → Sort u} {f : Π (n : ℕ), α n → α (n + 1)} def incl {n : ℕ} (x : α n) : colimit α f := quot.mk (rel α f) (core.incl x) abbreviation inclusion (n : ℕ) : α n → colimit α f := incl def glue {n : ℕ} (x : α n) : incl (f n x) = incl x :> colimit α f := ground_zero.support.inclusion (quot.sound (colimit.rel.glue f n x)) def ind {π : colimit α f → Sort v} (incl₁ : Π {n : ℕ} (x : α n), π (incl x)) (glue₁ : Π {n : ℕ} (x : α n), incl₁ (f n x) =[glue x] incl₁ x) : Π x, π x := begin intro x, fapply quot.hrec_on x, { intro a, cases a with n r, exact incl₁ r }, { intros, cases a with n a, cases b with m b, simp, cases p, fapply ground_zero.types.heq.from_pathover, apply glue, apply glue₁ } end def rec {π : Sort v} (incl₁ : Π {n : ℕ} (x : α n), π) (glue₁ : Π {n : ℕ} (x : α n), incl₁ (f n x) = incl₁ x :> π) : colimit α f → π := ind @incl₁ (λ n x, pathover_of_eq (glue x) (glue₁ x)) end colimit end ground_zero.HITs
30a5c8790bcf42838405a76ed90bdcebd4e0eb00
86f6f4f8d827a196a32bfc646234b73328aeb306
/examples/basics/unnamed_173.lean
7667fdf5c9affa24a2c100c70fcaddb1866177e3
[]
no_license
jamescheuk91/mathematics_in_lean
09f1f87d2b0dce53464ff0cbe592c568ff59cf5e
4452499264e2975bca2f42565c0925506ba5dda3
refs/heads/master
1,679,716,410,967
1,613,957,947,000
1,613,957,947,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
194
lean
import data.real.basic -- BEGIN example (a b c d e f : ℝ) (h : a * b = c * d) (h' : e = f) : a * (b * e) = c * (d * f) := begin rw h', rw ←mul_assoc, rw h, rw mul_assoc end -- END
f94c3333be2008e371161d8ff683f7f2b92309f4
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/measure_theory/ae_measurable_sequence.lean
71244d2b1485c61e47b93a28ae7e3eb91df2fb32
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
6,370
lean
/- Copyright (c) 2021 Rémy Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémy Degenne -/ import measure_theory.measure_space /-! # Sequence of measurable functions associated to a sequence of a.e.-measurable functions We define here tools to prove statements about limits (infi, supr...) of sequences of `ae_measurable` functions. Given a sequence of a.e.-measurable functions `f : ι → α → β` with hypothesis `hf : ∀ i, ae_measurable (f i) μ`, and a pointwise property `p : α → (ι → β) → Prop` such that we have `hp : ∀ᵐ x ∂μ, p x (λ n, f n x)`, we define a sequence of measurable functions `ae_seq hf p` and a measurable set `ae_seq_set hf p`, such that * `μ (ae_seq_set hf p)ᶜ = 0` * `x ∈ ae_seq_set hf p → ∀ i : ι, ae_seq hf hp i x = f i x` * `x ∈ ae_seq_set hf p → p x (λ n, f n x)` -/ open measure_theory open_locale classical variables {α β γ ι : Type*} [measurable_space α] [measurable_space β] {f : ι → α → β} {μ : measure α} {p : α → (ι → β) → Prop} /-- If we have the additional hypothesis `∀ᵐ x ∂μ, p x (λ n, f n x)`, this is a measurable set whose complement has measure 0 such that for all `x ∈ ae_seq_set`, `f i x` is equal to `(hf i).mk (f i) x` for all `i` and we have the pointwise property `p x (λ n, f n x)`. -/ def ae_seq_set (hf : ∀ i, ae_measurable (f i) μ) (p : α → (ι → β) → Prop) : set α := (to_measurable μ {x | (∀ i, f i x = (hf i).mk (f i) x) ∧ p x (λ n, f n x)}ᶜ)ᶜ /-- A sequence of measurable functions that are equal to `f` and verify property `p` on the measurable set `ae_seq_set hf p`. -/ noncomputable def ae_seq (hf : ∀ i, ae_measurable (f i) μ) (p : α → (ι → β) → Prop) : ι → α → β := λ i x, ite (x ∈ ae_seq_set hf p) ((hf i).mk (f i) x) (⟨f i x⟩ : nonempty β).some namespace ae_seq section mem_ae_seq_set lemma mk_eq_fun_of_mem_ae_seq_set (hf : ∀ i, ae_measurable (f i) μ) {x : α} (hx : x ∈ ae_seq_set hf p) (i : ι) : (hf i).mk (f i) x = f i x := begin have h_ss : ae_seq_set hf p ⊆ {x | ∀ i, f i x = (hf i).mk (f i) x}, { rw [ae_seq_set, ←compl_compl {x | ∀ i, f i x = (hf i).mk (f i) x}, set.compl_subset_compl], refine set.subset.trans (set.compl_subset_compl.mpr (λ x h, _)) (subset_to_measurable _ _), exact h.1, }, exact (h_ss hx i).symm, end lemma ae_seq_eq_mk_of_mem_ae_seq_set (hf : ∀ i, ae_measurable (f i) μ) {x : α} (hx : x ∈ ae_seq_set hf p) (i : ι) : ae_seq hf p i x = (hf i).mk (f i) x := by simp only [ae_seq, hx, if_true] lemma ae_seq_eq_fun_of_mem_ae_seq_set (hf : ∀ i, ae_measurable (f i) μ) {x : α} (hx : x ∈ ae_seq_set hf p) (i : ι) : ae_seq hf p i x = f i x := by simp only [ae_seq_eq_mk_of_mem_ae_seq_set hf hx i, mk_eq_fun_of_mem_ae_seq_set hf hx i] lemma prop_of_mem_ae_seq_set (hf : ∀ i, ae_measurable (f i) μ) {x : α} (hx : x ∈ ae_seq_set hf p) : p x (λ n, ae_seq hf p n x) := begin simp only [ae_seq, hx, if_true], rw funext (λ n, mk_eq_fun_of_mem_ae_seq_set hf hx n), have h_ss : ae_seq_set hf p ⊆ {x | p x (λ n, f n x)}, { rw [←compl_compl {x | p x (λ n, f n x)}, ae_seq_set, set.compl_subset_compl], refine set.subset.trans (set.compl_subset_compl.mpr _) (subset_to_measurable _ _), exact λ x hx, hx.2, }, have hx' := set.mem_of_subset_of_mem h_ss hx, exact hx', end lemma fun_prop_of_mem_ae_seq_set (hf : ∀ i, ae_measurable (f i) μ) {x : α} (hx : x ∈ ae_seq_set hf p) : p x (λ n, f n x) := begin have h_eq : (λ n, f n x) = λ n, ae_seq hf p n x, from funext (λ n, (ae_seq_eq_fun_of_mem_ae_seq_set hf hx n).symm), rw h_eq, exact prop_of_mem_ae_seq_set hf hx, end end mem_ae_seq_set lemma ae_seq_set_measurable_set {hf : ∀ i, ae_measurable (f i) μ} : measurable_set (ae_seq_set hf p) := (measurable_set_to_measurable _ _).compl lemma measurable (hf : ∀ i, ae_measurable (f i) μ) (p : α → (ι → β) → Prop) (i : ι) : measurable (ae_seq hf p i) := begin refine measurable.ite ae_seq_set_measurable_set (hf i).measurable_mk _, by_cases hα : nonempty α, { exact @measurable_const _ _ _ _ (⟨f i hα.some⟩ : nonempty β).some }, { exact measurable_of_not_nonempty hα _ } end lemma measure_compl_ae_seq_set_eq_zero [encodable ι] (hf : ∀ i, ae_measurable (f i) μ) (hp : ∀ᵐ x ∂μ, p x (λ n, f n x)) : μ (ae_seq_set hf p)ᶜ = 0 := begin rw [ae_seq_set, compl_compl, measure_to_measurable], have hf_eq := λ i, (hf i).ae_eq_mk, simp_rw [filter.eventually_eq, ←ae_all_iff] at hf_eq, exact filter.eventually.and hf_eq hp, end lemma ae_seq_eq_mk_ae [encodable ι] (hf : ∀ i, ae_measurable (f i) μ) (hp : ∀ᵐ x ∂μ, p x (λ n, f n x)) : ∀ᵐ (a : α) ∂μ, ∀ (i : ι), ae_seq hf p i a = (hf i).mk (f i) a := begin have h_ss : ae_seq_set hf p ⊆ {a : α | ∀ i, ae_seq hf p i a = (hf i).mk (f i) a}, from λ x hx i, by simp only [ae_seq, hx, if_true], exact le_antisymm (le_trans (measure_mono (set.compl_subset_compl.mpr h_ss)) (le_of_eq (measure_compl_ae_seq_set_eq_zero hf hp))) (zero_le _), end lemma ae_seq_eq_fun_ae [encodable ι] (hf : ∀ i, ae_measurable (f i) μ) (hp : ∀ᵐ x ∂μ, p x (λ n, f n x)) : ∀ᵐ (a : α) ∂μ, ∀ (i : ι), ae_seq hf p i a = f i a := begin have h_ss : {a : α | ¬∀ (i : ι), ae_seq hf p i a = f i a} ⊆ (ae_seq_set hf p)ᶜ, from λ x, mt (λ hx i, (ae_seq_eq_fun_of_mem_ae_seq_set hf hx i)), exact measure_mono_null h_ss (measure_compl_ae_seq_set_eq_zero hf hp), end lemma ae_seq_n_eq_fun_n_ae [encodable ι] (hf : ∀ i, ae_measurable (f i) μ) (hp : ∀ᵐ x ∂μ, p x (λ n, f n x)) (n : ι) : ae_seq hf p n =ᵐ[μ] f n:= ae_all_iff.mp (ae_seq_eq_fun_ae hf hp) n lemma supr [complete_lattice β] [encodable ι] (hf : ∀ i, ae_measurable (f i) μ) (hp : ∀ᵐ x ∂μ, p x (λ n, f n x)) : (⨆ n, ae_seq hf p n) =ᵐ[μ] ⨆ n, f n := begin simp_rw [filter.eventually_eq, ae_iff, supr_apply], have h_ss : ae_seq_set hf p ⊆ {a : α | (⨆ (i : ι), ae_seq hf p i a) = ⨆ (i : ι), f i a}, { intros x hx, congr, exact funext (λ i, ae_seq_eq_fun_of_mem_ae_seq_set hf hx i), }, exact measure_mono_null (set.compl_subset_compl.mpr h_ss) (measure_compl_ae_seq_set_eq_zero hf hp), end end ae_seq
562f673db4937e7da98fbe23b71b4c054e30157b
c53d5ba209b8af48cc7da493312ee546b152c1f2
/src/Lean/Server/FileWorker.lean
8055ab4a45581b4e2c41a336dbe55bae38cee1fe
[ "Apache-2.0" ]
permissive
bacaimano/lean4
2d3136a7a2cfebd757ae28b9df4102564b0221cc
6ca8389670bd7979172234c3c31b2b394fc69d06
refs/heads/master
1,686,768,155,370
1,625,522,310,000
1,625,556,563,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
17,841
lean
/- Copyright (c) 2020 Marc Huisinga. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Marc Huisinga, Wojciech Nawrocki -/ import Init.System.IO import Std.Data.RBMap import Lean.Environment import Lean.Data.Lsp import Lean.Data.Json.FromToJson import Lean.Server.Utils import Lean.Server.Snapshots import Lean.Server.AsyncList import Lean.Server.FileWorker.Utils import Lean.Server.FileWorker.RequestHandling /-! For general server architecture, see `README.md`. For details of IPC communication, see `Watchdog.lean`. This module implements per-file worker processes. File processing and requests+notifications against a file should be concurrent for two reasons: - By the LSP standard, requests should be cancellable. - Since Lean allows arbitrary user code to be executed during elaboration via the tactic framework, elaboration can be extremely slow and even not halt in some cases. Users should be able to work with the file while this is happening, e.g. make new changes to the file or send requests. To achieve these goals, elaboration is executed in a chain of tasks, where each task corresponds to the elaboration of one command. When the elaboration of one command is done, the next task is spawned. On didChange notifications, we search for the task in which the change occured. If we stumble across a task that has not yet finished before finding the task we're looking for, we terminate it and start the elaboration there, otherwise we start the elaboration at the task where the change occured. Requests iterate over tasks until they find the command that they need to answer the request. In order to not block the main thread, this is done in a request task. If a task that the request task waits for is terminated, a change occured somewhere before the command that the request is looking for and the request sends a "content changed" error. -/ namespace Lean.Server.FileWorker open Lsp open IO open Snapshots open Std (RBMap RBMap.empty) open JsonRpc /- Asynchronous snapshot elaboration. -/ section Elab /-- Elaborates the next command after `parentSnap` and emits diagnostics into `hOut`. -/ private def nextCmdSnap (m : DocumentMeta) (parentSnap : Snapshot) (cancelTk : CancelToken) (hOut : FS.Stream) : ExceptT ElabTaskError IO Snapshot := do cancelTk.check publishProgressAtPos m parentSnap.endPos hOut let maybeSnap ← compileNextCmd m.text.source parentSnap -- TODO(MH): check for interrupt with increased precision cancelTk.check match maybeSnap with | Sum.inl snap => /- NOTE(MH): This relies on the client discarding old diagnostics upon receiving new ones while prefering newer versions over old ones. The former is necessary because we do not explicitly clear older diagnostics, while the latter is necessary because we do not guarantee that diagnostics are emitted in order. Specifically, it may happen that we interrupted this elaboration task right at this point and a newer elaboration task emits diagnostics, after which we emit old diagnostics because we did not yet detect the interrupt. Explicitly clearing diagnostics is difficult for a similar reason, because we cannot guarantee that no further diagnostics are emitted after clearing them. -/ publishMessages m snap.msgLog hOut snap | Sum.inr msgLog => publishMessages m msgLog hOut publishProgressDone m hOut throw ElabTaskError.eof /-- Elaborates all commands after `initSnap`, emitting the diagnostics into `hOut`. -/ def unfoldCmdSnaps (m : DocumentMeta) (initSnap : Snapshot) (cancelTk : CancelToken) (hOut : FS.Stream) (initial : Bool) : IO (AsyncList ElabTaskError Snapshot) := do if initial && initSnap.msgLog.hasErrors then -- treat header processing errors as fatal so users aren't swamped with followup errors AsyncList.nil else AsyncList.unfoldAsync (nextCmdSnap m . cancelTk hOut) initSnap end Elab -- Pending requests are tracked so they can be cancelled abbrev PendingRequestMap := RBMap RequestID (Task (Except IO.Error Unit)) compare structure WorkerContext where hIn : FS.Stream hOut : FS.Stream hLog : FS.Stream srcSearchPath : SearchPath docRef : IO.Ref EditableDocument pendingRequestsRef : IO.Ref PendingRequestMap abbrev WorkerM := ReaderT WorkerContext IO /- Worker initialization sequence. -/ section Initialization /-- Use `leanpkg print-paths` to compile dependencies on the fly and add them to `LEAN_PATH`. Compilation progress is reported to `hOut` via LSP notifications. Return the search path for source files. -/ partial def leanpkgSetupSearchPath (leanpkgPath : System.FilePath) (m : DocumentMeta) (imports : Array Import) (hOut : FS.Stream) : IO SearchPath := do let leanpkgProc ← Process.spawn { stdin := Process.Stdio.null stdout := Process.Stdio.piped stderr := Process.Stdio.piped cmd := leanpkgPath.toString args := #["print-paths"] ++ imports.map (toString ·.module) } -- progress notification: report latest stderr line let rec processStderr (acc : String) : IO String := do let line ← leanpkgProc.stderr.getLine if line == "" then return acc else publishDiagnostics m #[{ range := ⟨⟨0, 0⟩, ⟨0, 0⟩⟩, severity? := DiagnosticSeverity.information, message := line }] hOut processStderr (acc ++ line) let stderr ← IO.asTask (processStderr "") Task.Priority.dedicated let stdout := String.trim (← leanpkgProc.stdout.readToEnd) let stderr ← IO.ofExcept stderr.get if (← leanpkgProc.wait) == 0 then let leanpkgLines := stdout.split (· == '\n') -- ignore any output up to the last two lines -- TODO: leanpkg should instead redirect nested stdout output to stderr let leanpkgLines := leanpkgLines.drop (leanpkgLines.length - 2) match leanpkgLines with | [""] => pure [] -- e.g. no leanpkg.toml | [leanPath, leanSrcPath] => let sp ← getBuiltinSearchPath let sp ← addSearchPathFromEnv sp let sp := System.SearchPath.parse leanPath ++ sp searchPathRef.set sp let srcPath := System.SearchPath.parse leanSrcPath srcPath.mapM realPathNormalized | _ => throwServerError s!"unexpected output from `leanpkg print-paths`:\n{stdout}\nstderr:\n{stderr}" else throwServerError s!"`leanpkg print-paths` failed:\n{stdout}\nstderr:\n{stderr}" def compileHeader (m : DocumentMeta) (hOut : FS.Stream) : IO (Snapshot × SearchPath) := do let opts := {} -- TODO let inputCtx := Parser.mkInputContext m.text.source "<input>" let (headerStx, headerParserState, msgLog) ← Parser.parseHeader inputCtx let leanpkgPath ← match (← IO.getEnv "LEAN_SYSROOT") with | some path => pure <| System.FilePath.mk path / "bin" / "leanpkg" | _ => pure <| (← appDir) / "leanpkg" let leanpkgPath := leanpkgPath.withExtension System.FilePath.exeExtension let mut srcSearchPath := [(← appDir) / ".." / "lib" / "lean" / "src"] if let some p := (← IO.getEnv "LEAN_SRC_PATH") then srcSearchPath := srcSearchPath ++ System.SearchPath.parse p let (headerEnv, msgLog) ← try -- NOTE: leanpkg does not exist in stage 0 (yet?) if (← System.FilePath.pathExists leanpkgPath) then let pkgSearchPath ← leanpkgSetupSearchPath leanpkgPath m (Lean.Elab.headerToImports headerStx).toArray hOut srcSearchPath := srcSearchPath ++ pkgSearchPath Elab.processHeader headerStx opts msgLog inputCtx catch e => -- should be from `leanpkg print-paths` let msgs := MessageLog.empty.add { fileName := "<ignored>", pos := ⟨0, 0⟩, data := e.toString } pure (← mkEmptyEnvironment, msgs) publishMessages m msgLog hOut let cmdState := Elab.Command.mkState headerEnv msgLog opts let cmdState := { cmdState with infoState.enabled := true, scopes := [{ header := "", opts := opts }] } let headerSnap := { beginPos := 0 stx := headerStx mpState := headerParserState cmdState := cmdState } return (headerSnap, srcSearchPath) def initializeWorker (meta : DocumentMeta) (i o e : FS.Stream) : IO WorkerContext := do /- NOTE(WN): `toFileMap` marks line beginnings as immediately following "\n", which should be enough to handle both LF and CRLF correctly. This is because LSP always refers to characters by (line, column), so if we get the line number correct it shouldn't matter that there is a CR there. -/ let (headerSnap, srcSearchPath) ← compileHeader meta o let cancelTk ← CancelToken.new let cmdSnaps ← unfoldCmdSnaps meta headerSnap cancelTk o (initial := true) let doc : EditableDocument := ⟨meta, headerSnap, cmdSnaps, cancelTk⟩ return { hIn := i hOut := o hLog := e srcSearchPath := srcSearchPath docRef := ←IO.mkRef doc pendingRequestsRef := ←IO.mkRef RBMap.empty } end Initialization section Updates def updatePendingRequests (map : PendingRequestMap → PendingRequestMap) : WorkerM Unit := do (←read).pendingRequestsRef.modify map /-- Given the new document and `changePos`, the UTF-8 offset of a change into the pre-change source, updates editable doc state. -/ def updateDocument (newMeta : DocumentMeta) (changePos : String.Pos) : WorkerM Unit := do -- The watchdog only restarts the file worker when the syntax tree of the header changes. -- If e.g. a newline is deleted, it will not restart this file worker, but we still -- need to reparse the header so that the offsets are correct. let st ← read let oldDoc ← st.docRef.get let newHeaderSnap ← reparseHeader newMeta.text.source oldDoc.headerSnap if newHeaderSnap.stx != oldDoc.headerSnap.stx then throwServerError "Internal server error: header changed but worker wasn't restarted." let ⟨cmdSnaps, e?⟩ ← oldDoc.cmdSnaps.updateFinishedPrefix match e? with -- This case should not be possible. only the main task aborts tasks and ensures that aborted tasks -- do not show up in `snapshots` of an EditableDocument. | some ElabTaskError.aborted => throwServerError "Internal server error: elab task was aborted while still in use." | some (ElabTaskError.ioError ioError) => throw ioError | _ => -- No error or EOF oldDoc.cancelTk.set -- NOTE(WN): we invalidate eagerly as `endPos` consumes input greedily. To re-elaborate only -- when really necessary, we could do a whitespace-aware `Syntax` comparison instead. let mut validSnaps := cmdSnaps.finishedPrefix.takeWhile (fun s => s.endPos < changePos) if validSnaps.length = 0 then let cancelTk ← CancelToken.new let newCmdSnaps ← unfoldCmdSnaps newMeta newHeaderSnap cancelTk st.hOut (initial := true) st.docRef.set ⟨newMeta, newHeaderSnap, newCmdSnaps, cancelTk⟩ else /- When at least one valid non-header snap exists, it may happen that a change does not fall within the syntactic range of that last snap but still modifies it by appending tokens. We check for this here. We do not currently handle crazy grammars in which an appended token can merge two or more previous commands into one. To do so would require reparsing the entire file. -/ let mut lastSnap := validSnaps.getLast! let preLastSnap := if validSnaps.length ≥ 2 then validSnaps.get! (validSnaps.length - 2) else newHeaderSnap let newLastStx ← parseNextCmd newMeta.text.source preLastSnap if newLastStx != lastSnap.stx then validSnaps ← validSnaps.dropLast lastSnap ← preLastSnap let cancelTk ← CancelToken.new let newSnaps ← unfoldCmdSnaps newMeta lastSnap cancelTk st.hOut (initial := false) let newCmdSnaps := AsyncList.ofList validSnaps ++ newSnaps st.docRef.set ⟨newMeta, newHeaderSnap, newCmdSnaps, cancelTk⟩ end Updates /- Notifications are handled in the main thread. They may change global worker state such as the current file contents. -/ section NotificationHandling def handleDidChange (p : DidChangeTextDocumentParams) : WorkerM Unit := do let docId := p.textDocument let changes := p.contentChanges let oldDoc ← (←read).docRef.get let some newVersion ← pure docId.version? | throwServerError "Expected version number" if newVersion ≤ oldDoc.meta.version then -- TODO(WN): This happens on restart sometimes. IO.eprintln s!"Got outdated version number: {newVersion} ≤ {oldDoc.meta.version}" else if ¬ changes.isEmpty then let (newDocText, minStartOff) := foldDocumentChanges changes oldDoc.meta.text updateDocument ⟨docId.uri, newVersion, newDocText⟩ minStartOff def handleCancelRequest (p : CancelParams) : WorkerM Unit := do updatePendingRequests (fun pendingRequests => pendingRequests.erase p.id) end NotificationHandling section MessageHandling def parseParams (paramType : Type) [FromJson paramType] (params : Json) : WorkerM paramType := match fromJson? params with | Except.ok parsed => pure parsed | Except.error inner => throwServerError s!"Got param with wrong structure: {params.compress}\n{inner}" def handleNotification (method : String) (params : Json) : WorkerM Unit := do let handle := fun paramType [FromJson paramType] (handler : paramType → WorkerM Unit) => parseParams paramType params >>= handler match method with | "textDocument/didChange" => handle DidChangeTextDocumentParams handleDidChange | "$/cancelRequest" => handle CancelParams handleCancelRequest | _ => throwServerError s!"Got unsupported notification method: {method}" def queueRequest (id : RequestID) (requestTask : Task (Except IO.Error Unit)) : WorkerM Unit := do updatePendingRequests (fun pendingRequests => pendingRequests.insert id requestTask) def handleRequest (id : RequestID) (method : String) (params : Json) : WorkerM Unit := do let st ← read let rc : Requests.RequestContext := { srcSearchPath := st.srcSearchPath docRef := st.docRef hLog := st.hLog } let t? ← (ExceptT.run <| Requests.handleLspRequest method params rc : IO _) let t₁ ← match t? with | Except.error e => IO.asTask do st.hOut.writeLspResponseError <| e.toLspResponseError id | Except.ok t => (IO.mapTask · t) fun | Except.ok resp => st.hOut.writeLspResponse ⟨id, resp⟩ | Except.error e => st.hOut.writeLspResponseError <| e.toLspResponseError id queueRequest id t₁ end MessageHandling section MainLoop partial def mainLoop : WorkerM Unit := do let st ← read let msg ← st.hIn.readLspMessage let pendingRequests ← st.pendingRequestsRef.get let filterFinishedTasks (acc : PendingRequestMap) (id : RequestID) (task : Task (Except IO.Error Unit)) : WorkerM PendingRequestMap := do if (←hasFinished task) then /- Handler tasks are constructed so that the only possible errors here are failures of writing a response into the stream. -/ if let Except.error e := task.get then throwServerError s!"Failed responding to request {id}: {e}" acc.erase id else acc let pendingRequests ← pendingRequests.foldM filterFinishedTasks pendingRequests st.pendingRequestsRef.set pendingRequests match msg with | Message.request id method (some params) => handleRequest id method (toJson params) mainLoop | Message.notification "exit" none => let doc ← st.docRef.get doc.cancelTk.set return () | Message.notification method (some params) => handleNotification method (toJson params) mainLoop | _ => throwServerError "Got invalid JSON-RPC message" end MainLoop def initAndRunWorker (i o e : FS.Stream) : IO UInt32 := do let i ← maybeTee "fwIn.txt" false i let o ← maybeTee "fwOut.txt" true o let _ ← i.readLspRequestAs "initialize" InitializeParams let ⟨_, param⟩ ← i.readLspNotificationAs "textDocument/didOpen" DidOpenTextDocumentParams let doc := param.textDocument let meta : DocumentMeta := ⟨doc.uri, doc.version, doc.text.toFileMap⟩ let e ← e.withPrefix s!"[{param.textDocument.uri}] " let _ ← IO.setStderr e try let ctx ← initializeWorker meta i o e ReaderT.run (r := ctx) mainLoop return 0 catch e => IO.eprintln e publishDiagnostics meta #[{ range := ⟨⟨0, 0⟩, ⟨0, 0⟩⟩, severity? := DiagnosticSeverity.error, message := e.toString }] o return 1 @[export lean_server_worker_main] def workerMain : IO UInt32 := do let i ← IO.getStdin let o ← IO.getStdout let e ← IO.getStderr try let exitCode ← initAndRunWorker i o e -- HACK: all `Task`s are currently "foreground", i.e. we join on them on main thread exit, but we definitely don't -- want to do that in the case of the worker processes, which can produce non-terminating tasks evaluating user code o.flush e.flush IO.Process.exit exitCode.toUInt8 catch err => e.putStrLn s!"worker initialization error: {err}" return (1 : UInt32) end Lean.Server.FileWorker
d8e60c4f49d356dde264e6add6c3cac65def8b61
ca1ad81c8733787aba30f7a8d63f418508e12812
/clfrags/src/hilbert/wr/neg.lean
414c13326019557d02516d596c08fcb72725a652
[]
no_license
greati/hilbert-classical-fragments
5cdbe07851e979c8a03c621a5efd4d24bbfa333a
18a21ac6b2e890060eb4ae65752fc0245394d226
refs/heads/master
1,591,973,117,184
1,573,822,710,000
1,573,822,710,000
194,334,439
2
0
null
null
null
null
UTF-8
Lean
false
false
363
lean
import core.connectives namespace clfrags namespace hilbert namespace wr namespace neg axiom n₁ : Π {a b : Prop}, a → neg a → b axiom n₂ : Π {a : Prop}, a → neg (neg a) axiom n₃ : Π {a : Prop}, neg (neg a) → a end neg end wr end hilbert end clfrags
1df1881728340e025e9b41f90f6c39d0f029160b
3446e92e64a5de7ed1f2109cfb024f83cd904c34
/src/game/world2/level7.lean
b03048730c13141ecb8b7d12159c4b5f2bbff4b2
[]
no_license
kckennylau/natural_number_game
019f4a5f419c9681e65234ecd124c564f9a0a246
ad8c0adaa725975be8a9f978c8494a39311029be
refs/heads/master
1,598,784,137,722
1,571,905,156,000
1,571,905,156,000
218,354,686
0
0
null
1,572,373,319,000
1,572,373,318,000
null
UTF-8
Lean
false
false
1,942
lean
import mynat.definition -- hide import mynat.add -- hide import game.world2.level6 -- hide namespace mynat -- hide /- # World 2 -- Addition World ## Level 7 -- `succ_ne_zero` You have these: * `zero_ne_succ : ∀ (a : mynat), zero ≠ succ(a)` * `succ_inj : ∀ a b : mynat, succ(a) = succ(b) → a = b` * `add_zero : ∀ a : mynat, a + 0 = a` * `add_succ : ∀ a b : mynat, a + succ(b) = succ(a + b)` * `zero_add : ∀ a : mynat, 0 + a = a` * `add_assoc : ∀ a b c : mynat, (a + b) + c = a + (b + c)` * `succ_add : ∀ a b : mynat, succ a + b = succ (a + b)` * `add_comm : ∀ a b : mynat, a + b = b + a` Levels 7 to 16 are some more advanced facts about addition. If you just want to skip these and move straight on to multiplication, click on "next world" on the top right. The four tactics `refl`, `exact`, `rw` and `induction` will get you through to the boss, `a * b = b * a`. If you want to stick with addition world and prove some trickier goals, you can, but you'll need to know some more tactics. For example the `symmetry` tactic can be used whenever the goal is a proposition defined by a symmetric binary relation, such as `=` or `≠`. Remember we already have `zero_ne_succ`, which here should be thought of as a *function* which takes as input a natural number `m` and outputs a proof that `zero ≠ succ(m)`. If you want to venture further into these bonus levels, you will almost certainly need the <a href="http://wwwf.imperial.ac.uk/~buzzard/xena/html/source/tactics/tacticindex.html" target="blank">tactic guide</a>, but I'll give you some hints along the way. If you are still totally stuck, ask at <a href="https://leanprover.zulipchat.com" target="blank">the Lean chat</a> in the new users stream. -/ /- Theorem Zero is not the successor of any natural number. -/ theorem succ_ne_zero {{a : mynat}} : succ a ≠ 0 := begin [less_leaky] symmetry, exact zero_ne_succ a, end end mynat
e5b0e66cbcd8b1c26fb45fc9a77992f9e71a9ae4
5c7fe6c4a9d4079b5457ffa5f061797d42a1cd65
/src/exercises/src_39_square_root_of_two.lean
890f0316fa7d9f280b06f4c662106d303b691904
[]
no_license
gihanmarasingha/mth1001_tutorial
8e0817feeb96e7c1bb3bac49b63e3c9a3a329061
bb277eebd5013766e1418365b91416b406275130
refs/heads/master
1,675,008,746,310
1,607,993,443,000
1,607,993,443,000
321,511,270
3
0
null
null
null
null
UTF-8
Lean
false
false
17,544
lean
import ..library.src_real_field_lemmas import ..library.src_ordered_field_lemmas import data.set.basic import tactic /- There are no explicit exercises in this file, though you may choose to prove examples yourself. The aim of this file is to prove that there is a real number whose square is 2. -/ namespace mth1001 namespace myreal section auxiliary open myreal_field classical myordered_field open_locale classical variables {R : Type} [myreal_field R] lemma difference_of_two_squares (x y : R) : x*x - y*y = (x - y)*(x+y) := begin rw mul_add, repeat { rw sub_eq_add_neg' <|> rw add_mul }, rw [←neg_neg (x*y), neg_mul_eq_mul_neg x y, mul_comm x (-y), add_assoc, ←add_assoc (-y*x) _ _], rw [add_neg', zero_add, neg_mul_eq_mul_neg, mul_comm y (-y)], end lemma square_le_square_iff_le_of_non_neg_of_non_neg (a b : R) (h₁ : 0 ≤ a) (h₂ : 0 ≤ b) : a*a ≤ b*b ↔ a ≤ b := begin split, { intro aalebb, have h₃ : 0 ≤ b*b - a*a, linarith, rw difference_of_two_squares b a at h₃, rw non_neg_mul_iff_non_neg_and_non_neg_or_non_pos_and_non_pos at h₃, cases h₃; linarith }, { intro aleb, exact mul_le_mul aleb aleb h₁ h₂, }, end lemma pos_mul_iff_pos_and_pos_or_neg_and_neg (a b : R) : 0 < a * b ↔ (0 < a ∧ 0 < b) ∨ (a < 0 ∧ b < 0) := begin split, { intro h, by_cases h₂ : 0 ≤ a, { by_cases h₃ : a = 0, { rw h₃ at h, linarith, }, { have h₄ : 0 < a, from or.elim h₂ id (λ aeq0, absurd aeq0.symm h₃), have h₅ : 0 < b, from (zero_lt_mul_left h₄).mp h, exact or.inl ⟨h₄, h₅⟩, }, }, { have h₄ : a < 0, linarith, have h₅ : b < 0, { by_contra p, rw not_lt_iff_le at p, rw ←neg_pos at h₄, have h₅ : 0 ≤ (-a) * b, from (zero_le_mul_left h₄).mpr p, have h₆ : -a * b = -(a*b), { rw [mul_comm (-a), ←neg_mul_eq_mul_neg, mul_comm], }, rw h₆ at h₅, linarith, }, exact or.inr ⟨h₄, h₅⟩, }, }, { rintro (⟨apos, bpos⟩ | ⟨aneg, bneg⟩), { exact gt_zero_mul_of_gt_zero_of_gt_zero apos bpos }, { rw ←neg_mul_neg a b, rw ←neg_pos at aneg bneg, exact gt_zero_mul_of_gt_zero_of_gt_zero aneg bneg, }, }, end lemma square_lt_square_iff_lt_of_pos_of_pos (a b : R) (h₁ : 0 < a) (h₂ : 0 < b) : a*a < b*b ↔ a < b := begin split, { intro aalebb, have h₃ : 0 < b*b - a*a, linarith, rw difference_of_two_squares b a at h₃, rw pos_mul_iff_pos_and_pos_or_neg_and_neg at h₃, rcases h₃ with ⟨h₃_left, h₃_right⟩ | ⟨h₃_left, h₃_right⟩, { linarith, }, { have h₄ : a < -b, linarith, have h₅ : -b < 0, linarith, have h₆ : a < 0, from lt_trans h₄ h₅, have : (0 : R) < 0, from lt_trans h₁ h₆, linarith, }, }, { intro aleb, apply mul_lt_mul, { exact aleb, }, { exact le_of_lt aleb, }, { exact h₁ }, { exact le_of_lt h₂ }, }, end lemma zero_eq_zero : (0 : R) = ↑0 := rfl lemma one_eq_one : ↑1 = (1 : R) := by rw [coe_nat_succ, ←zero_eq_zero, zero_add] lemma pos_iff_gt_zero (x : R) : pos x ↔ 0 < x := by rw [lt_iff_pos_sub, sub_zero] lemma non_zero_of_pos {x : R} (h : 0 < x) : x ≠ 0 := begin intro k, rw k at h, exact lt_irrefl h, end lemma coe_zero_inj {m : ℕ} (h : (m : R) = 0) : m = 0 := begin by_contra k, have h₂ : pos(m : R), from pos_nat m k, rw [h, pos_iff_gt_zero (0 : R)] at h₂, exact lt_irrefl h₂, end lemma coe_non_zero_of_non_zero {m : ℕ} (h : m ≠ 0) : (m : R) ≠ 0 := begin contrapose! h, exact coe_zero_inj h, end lemma gt_zero_of_ne_zero_nat (n : ℕ) (h : n ≠ 0) : (0 : R) < n := begin rw [←pos_iff_gt_zero], exact pos_nat n h, end lemma coe_pred (n : ℕ) (h : n ≠ 0) : (↑(n-1) : R) = ↑n - 1 := begin induction n with k hk, { exfalso, apply h, refl, }, { have : nat.succ k - 1 = k := rfl, rw [this, coe_nat_succ, sub_eq_add_neg', add_assoc, add_neg', add_zero ], }, end lemma coe_pred_eq_of_coe_succ_eq {x k : ℕ} (h : ↑x = ↑k + (1 : R)) : (↑(x-1) : R)= ↑k := begin by_cases xeq0 : x = 0, { exfalso, rw xeq0 at h, change 0 = ↑k + (1:R) at h, have h₂ : ↑k = -(1 : R), { rw [←zero_add (-1: R), h, add_assoc, add_neg', add_zero], }, have h₃ : (0 : R) ≤ ↑k, { rw le_iff_lt_or_eq, by_cases h₄ : k = 0, { rw h₄, right, refl, }, { left, rw ←pos_iff_gt_zero, exact pos_nat k h₄, }, }, rw h₂ at h₃, linarith, }, { change x ≠ 0 at xeq0, rw coe_pred _ xeq0, linarith, } end lemma coe_inj (m n : ℕ) (h : (m : R) = (n : R)) : m = n := begin revert m, induction n with k hk, { intro m, exact coe_zero_inj, }, { intros x hx, rw coe_nat_succ at hx, specialize hk (x-1), by_cases h₂ : x = 0, { rw [h₂, ←zero_eq_zero] at hx, have h₂ : ↑k = -(1 : R), { rw [←zero_add (-1: R), hx, add_assoc, add_neg', add_zero], }, have h₃ : (0 : R) ≤ ↑k, { rw le_iff_lt_or_eq, by_cases h₄ : k = 0, { rw h₄, right, refl, }, { left, rw ←pos_iff_gt_zero, exact pos_nat k h₄, }, }, rw h₂ at h₃, linarith, }, { rw coe_pred x h₂ at hk, have h₃ : ↑x - (1 : R) = ↑k, linarith, have h₄ : x - 1 = k, from hk h₃, rw ←h₄, cases nat.eq_zero_or_eq_succ_pred x with x0 xsp, { exact absurd x0 h₂, }, { assumption, }, }, }, end lemma coe_monotone (m n : ℕ) (h : m ≤ n) : (m : R) ≤ (n : R) := begin revert n, induction m with k hk, { intros n h, rw [←zero_eq_zero,le_iff_lt_or_eq], by_cases h₂ : n = 0, { right, rw [h₂, zero_eq_zero], }, { left, exact gt_zero_of_ne_zero_nat n h₂, }, }, { intros x hx, specialize hk (x-1), by_cases h₂ : x = 0, { exfalso, rw [h₂, ←not_lt] at hx, exact hx (nat.succ_pos k), }, { rcases nat.exists_eq_succ_of_ne_zero h₂ with ⟨w, h⟩, rw coe_nat_succ, rw coe_pred x h₂ at hk, rw h at hx hk, suffices h₃ : ↑k ≤ ↑(nat.succ w) - (1 : R), { rw h, linarith, }, have h₃ : nat.succ w - 1 = w := rfl, rw h₃ at hk, rw ←nat.pred_le_iff at hx, change k ≤ w at hx, exact hk hx, }, }, end lemma coe_monotone' (m n : ℕ) (h : m < n) : (m : R) < (n : R) := begin have h₂ : m ≤ n, linarith, have h₃ : (m : R) ≤ n, from coe_monotone _ _ h₂, rw le_iff_lt_or_eq at h₃, cases h₃ with mltn meqn, { exact mltn, }, { have h₃ : m = n, from coe_inj m n meqn, linarith, }, end lemma non_neg_of_non_neg (n : ℕ) (h : 0 ≤ n) : (0 : R) ≤ ↑n := begin rw le_iff_lt_or_eq, by_cases h₂ : n = 0, { right, rw [h₂, zero_eq_zero], }, { left, exact gt_zero_of_ne_zero_nat n h₂, } end lemma ge_one_of_non_zero (k : ℕ) (h : k ≠ 0): (1 : R) ≤ k := begin induction k with m hm, { exfalso, apply h, refl, }, { rw coe_nat_succ m, conv {to_lhs, rw ←add_zero (1 : R)}, by_cases h₂ : m = 1, { rw [h₂, add_zero, one_eq_one], change (1 : R) ≤ 2, rw le_iff_lt_or_eq, left, linarith, }, { rw add_comm, apply add_le_add, { rw le_iff_lt_or_eq, by_cases h₃ : m = 0, { right, rw h₃, refl, }, { left, exact gt_zero_of_ne_zero_nat m h₃, }, }, { exact le_refl 1, }, }, }, end lemma pos_inv_nat_of_non_zero {n : ℕ} (h : n ≠ 0) : (0 : R) < (↑n)⁻¹ := begin have h₂ : (0 : R) < ↑n, from gt_zero_of_ne_zero_nat n h, have h₃ : ↑n ≠ (0 : R), from non_zero_of_pos h₂, rwa inv_pos h₃, end theorem inv_le_inv {a b : R} (h₁ : 0 < a) (h₂ : 0 < b) : a⁻¹ ≤ b⁻¹ ↔ b ≤ a := begin have k₁ : a ≠ (0 : R), from non_zero_of_pos h₁, have k₂ : b ≠ 0, from non_zero_of_pos h₂, repeat {rw le_iff_lt_or_eq }, split, { rintro (hlt | heq), { left, rwa ←inv_lt_inv h₁ h₂, }, { right, rw [←inv_inv' a k₁, ←inv_inv' b k₂, heq], }, }, { rintro (hlt | heq), { left, rwa inv_lt_inv h₁ h₂, }, { rw heq, right, refl, }, }, end lemma pos_add_inv_nat_of_pos {u : R} {n : ℕ} (h : (0 : R) < u) (h₂ : n ≠ 0) : (0 : R) < u + (↑n)⁻¹ := begin rw ←add_zero (0 : R), apply add_lt_add h, rw inv_pos (coe_non_zero_of_non_zero h₂), exact gt_zero_of_ne_zero_nat n h₂, end lemma pos_sub_inv_nat_of_pos {u : R} {n : ℕ} (k₄ : (0 : R) < u) (ne0 : n ≠ 0) (k₅ : 2 < u*u) : (0 : R) < u - (↑n)⁻¹ := begin by_contra p, have : u ≤ (↑n)⁻¹, linarith, have h₂ : (1 : R) ≤ (↑n), from ge_one_of_non_zero n ne0, have h₃ : (0 : R) < ↑n, {rw ←pos_iff_gt_zero, exact pos_nat n ne0}, have h₄ : (↑n)⁻¹ ≤ (1 : R)⁻¹, { rwa inv_le_inv h₃ zero_lt_one, }, have h₅ : u ≤ (1 : R)⁻¹, linarith, rw one_inv at h₅, have : u * u ≤ 1 * 1, from mul_le_mul h₅ h₅ (le_of_lt k₄) zero_le_one, linarith, end lemma lt_sub_inv_nat {u : R} {n : ℕ} (h₂ : n ≠ 0) : u - (↑n)⁻¹ < u:= begin rw [sub_eq_add_neg', add_lt_iff_neg_left, neg_lt_zero, inv_pos (coe_non_zero_of_non_zero h₂)], exact gt_zero_of_ne_zero_nat n h₂, end end auxiliary namespace sqrt_two open myreal_field classical myordered_field open_locale classical variables {R : Type} [myreal_field R] lemma has_upper_bound_S : has_upper_bound ({x : R | (0 < x) ∧ (x*x < 2)} : set R) := begin use (2 : R), intros s hs, cases hs with h₁ h₂, -- `h₁ : 0 < s`, `h₂ : s * s < 2`, have h₄ : (2 : R) < 2 * 2, linarith, have h₅ : s * s < 2*2, { apply lt_trans h₂ h₄, }, have : s * s ≤ 2*2, from le_of_lt h₅, have h₆ : 0 < (2 : R), linarith, have h₇ : 0 ≤ (2 : R), from le_of_lt h₆, rwa ←square_le_square_iff_le_of_non_neg_of_non_neg _ _ (le_of_lt h₁) h₇, end lemma one_in_S : (1 : R) ∈ {x : R | (0 < x) ∧ (x*x < 2)} := ⟨by linarith, by linarith⟩ lemma non_empty_S : ({x : R | (0 < x) ∧ (x*x < 2)} : set R) ≠ ∅ := begin have h : (1 : R) ∈ {x : R | (0 < x) ∧ (x*x < 2)}, from one_in_S, intro h₂, rw h₂ at h, exact h, end lemma not_upper_bound_of_exists_nat {u : R} {S : set R} (h : ∃ n : ℕ, n ≠ 0 ∧ (u + (↑n)⁻¹ ∈ S)) : ¬(upper_bound u S) := begin rcases h with ⟨n, ne0, hn⟩, unfold upper_bound, push_neg, use (u + (↑n)⁻¹), apply and.intro hn, suffices h : (0 : R) + u< (↑n)⁻¹ + u, { rwa [zero_add, add_comm] at h,}, apply add_lt_add_iff_right_mpr, have h₂ : (0 : R) < n, from gt_zero_of_ne_zero_nat n ne0, rwa inv_pos (coe_non_zero_of_non_zero ne0), end lemma not_lub_of_exists_nat {u : R} {S : set R} (h : ∃ n : ℕ, n ≠ 0 ∧ (upper_bound (u - (↑n)⁻¹) S)) : ¬(∀ v : R, upper_bound v S → u ≤ v) := begin rcases h with ⟨n, ne0, hn⟩, push_neg, use (u - (↑n)⁻¹), exact and.intro hn (lt_sub_inv_nat ne0), end def S : set R := {x : R | (0 < x) ∧ (x*x < 2)} section lemmas_for_ub_contra lemma pos_sub_inv_nat_of_pos {u : R} {n : ℕ} (k₄ : (0 : R) < u) (ne0 : n ≠ 0) (k₅ : 2 < u*u) : (0 : R) < u - (↑n)⁻¹ := begin by_contra p, have : u ≤ (↑n)⁻¹, linarith, have h₂ : (1 : R) ≤ (↑n), from ge_one_of_non_zero n ne0, have h₃ : (0 : R) < ↑n, {rw ←pos_iff_gt_zero, exact pos_nat n ne0}, have h₄ : (↑n)⁻¹ ≤ (1 : R)⁻¹, { rwa inv_le_inv h₃ zero_lt_one, }, have h₅ : u ≤ (1 : R)⁻¹, linarith, rw one_inv at h₅, have : u * u ≤ 1 * 1, from mul_le_mul h₅ h₅ (le_of_lt k₄) zero_le_one, linarith, end lemma sq_add_inv_ub {u : R} {k : ℕ} (h : k ≠ 0) : (u + (↑k)⁻¹)*(u + (↑k)⁻¹) ≤ u*u + (2*u+1)*(↑k)⁻¹ := begin rw [add_mul, add_mul, mul_add, mul_add, mul_comm (↑k)⁻¹ u, one_mul, add_assoc], apply add_le_add (le_refl (u* u)), rw [←add_assoc, ←two_mul, ←mul_assoc], apply add_le_add (le_refl _), have h₂ : (k : R) ≠ 0, from coe_non_zero_of_non_zero h, suffices h : (↑k)⁻¹ * (↑k)⁻¹ ≤ (↑k)⁻¹ * (1 : R), { rwa mul_one at h, }, have h₃ : (1 : R) ≤ (↑k), from ge_one_of_non_zero k h, have h₄ : (↑k)⁻¹ ≤ (1 : R), { rwa [←one_inv, inv_le_inv (gt_zero_of_ne_zero_nat k h) (zero_lt_one : (0 : R) < 1)], }, have h₅ : (0 : R) < (↑k)⁻¹, from pos_inv_nat_of_non_zero h, exact mul_le_mul (le_refl (↑k)⁻¹) h₄ (le_of_lt h₅) (le_of_lt h₅), end lemma inequ1 {u : R} (h₁ : 0 < u) (h₂ : u*u < 2) : (0 : R) < (2*u + 1)⁻¹ * (2 - u*u) := begin have h₃ : 0 < 2 - u * u, linarith, have h₄ : 0 < 2 * u + 1, from add_pos (by linarith) (by linarith), have h₅ : 2*u+ 1 ≠ 0, linarith, have h₆ : 0 < (2*u + 1)⁻¹, from (inv_pos h₅).mpr h₄, exact mul_pos _ _ h₆ h₃, end lemma inequ2 {u : R} {n : ℕ} (h₁ : 0 < u) (h₂ : (↑n)⁻¹ < (2 * u + 1)⁻¹ * (2 - u * u)) : u * u + (2 * u + 1) * (↑n)⁻¹ < 2:= begin suffices h : ((2 : R)*u+1) * (↑n)⁻¹ < (2- u * u), linarith, have h₄ : 0 < 2 * u + 1, from add_pos (by linarith) (by linarith), have h₅ : 2*u+ 1 ≠ 0, linarith, suffices h : (2 * u + 1) * (↑n)⁻¹ < (2 * u + 1) * ((2*u + 1)⁻¹ * (2 - u * u)), { rwa [←mul_assoc, mul_inv _ h₅, one_mul] at h, }, exact mul_lt_mul_left_mpr h₄ h₂, end lemma ub_contra {u : R} (k₃ : upper_bound u S) (k₄ : (0 : R) < u) (k₅ : u * u < 2) : u * u = 2 := begin suffices h : ¬(upper_bound u S), from absurd k₃ h, suffices h : ∃ n : ℕ, (n ≠ 0) ∧ u + (↑n)⁻¹ ∈ S, from not_upper_bound_of_exists_nat h, suffices h : ∃ n : ℕ, (n ≠ 0) ∧ ((u + (n : R)⁻¹) * (u + (↑n)⁻¹) < 2), { rcases h with ⟨n, ne0, hn⟩, exact ⟨n, ne0, pos_add_inv_nat_of_pos k₄ ne0, hn⟩, }, suffices h : ∃ n : ℕ, n ≠ 0 ∧ (u*u + ((2 : R)*u + 1)*(↑n)⁻¹) < 2, { rcases h with ⟨n, ne0, hn⟩, exact ⟨n, ne0, (lt_of_le_of_lt) (sq_add_inv_ub ne0) hn⟩, }, have k₆ : (0 : R) < (2*u + 1)⁻¹ * (2 - u*u) := inequ1 k₄ k₅, rcases (inv_lt_of_pos _ k₆) with ⟨n, ne0, h₂⟩, exact ⟨n, ne0, (inequ2 k₄ h₂)⟩, end end lemmas_for_ub_contra section lemmas_for_lub_contra lemma sq_sub_inv_ub {u : R} {k : ℕ} (h₁ : 0 < u) (h₂ : k ≠ 0) : (u - (↑k)⁻¹) * (u - (↑k)⁻¹) > u*u - 2*u*(↑k)⁻¹ := begin repeat {rw sub_eq_add_neg}, rw [mul_add, add_mul, add_mul, mul_comm (-(↑k)⁻¹) u, add_assoc], apply add_lt_add_of_le_of_lt(le_refl (u* u)), rw [←add_assoc, ←two_mul, neg_mul_eq_mul_neg, mul_assoc, lt_add_iff_pos_right, neg_mul_neg_self], exact mul_pos _ _ (pos_inv_nat_of_non_zero h₂) (pos_inv_nat_of_non_zero h₂), end lemma lub_contra_subproof1 {u : R} (k₄ : (0 : R) < u) (k₅ : 2 < u * u) (h : ∃ n : ℕ, (n ≠ 0) ∧ (2 < (u - (n : R)⁻¹) * (u - (↑n)⁻¹))) : ∃ (n : ℕ), n ≠ 0 ∧ upper_bound (u - (↑n)⁻¹) S := begin rcases h with ⟨n, ne0, hn⟩, use n, apply and.intro ne0, intros x hx, cases hx with xpos xsqlt2, have h₃ : x * x < (u - (n : R)⁻¹) * (u - (↑n)⁻¹), from lt_trans xsqlt2 hn, rw le_iff_lt_or_eq, left, exact (square_lt_square_iff_lt_of_pos_of_pos _ _ xpos (pos_sub_inv_nat_of_pos k₄ ne0 k₅)).mp h₃, end lemma inequ3 {u : R} {n : ℕ} (h₁ : 0 < u) (h₂ : (↑n)⁻¹ < (2*u)⁻¹ * (u*u - 2)) : 2 < u * u - 2 * u * (↑n)⁻¹ := begin suffices h : 2 * u * (↑n)⁻¹ < u * u - 2, linarith, have h₄ : 0 < (2 * u), linarith, have h₅ : 2 * u ≠ 0, linarith, suffices h : (2 * u) * (↑n)⁻¹ < (2 * u) * ( (2*u)⁻¹ *(u*u -2)), { rwa [←mul_assoc, mul_inv _ h₅, one_mul] at h, }, exact mul_lt_mul_left_mpr h₄ h₂, end lemma inequ4 {u : R} (h₁ : 0 < u) (h₂ : 2 < u * u) : 0 < (2*u)⁻¹ * (u * u - 2):= begin have h₃ : 0 < u * u - 2, linarith, have h₄ : 0 < 2 * u, linarith, have h₅ : 2*u ≠ 0, linarith, have h₆ : 0 < (2*u)⁻¹, from (inv_pos h₅).mpr h₄, exact mul_pos _ _ h₆ h₃, end lemma lub_contra {u : R} (k₃ : ∀ v : R, upper_bound v S → u ≤ v) (k₄ : (0 : R) < u) (k₅ : 2 < u * u) : u * u = 2 := begin suffices h : ¬(∀ v : R, upper_bound v S → u ≤ v), from absurd k₃ h, suffices h : ∃ n : ℕ, n ≠ 0 ∧ (upper_bound (u - (↑n)⁻¹) S), from not_lub_of_exists_nat h, suffices h : ∃ n : ℕ, (n ≠ 0) ∧ (2 < (u - (n : R)⁻¹) * (u - (↑n)⁻¹)), from lub_contra_subproof1 k₄ k₅ h, suffices h : ∃ n : ℕ, n ≠ 0 ∧ 2 < u * u - 2 * u * (↑n)⁻¹, { rcases h with ⟨n, ne0, hn⟩, exact ⟨n, ne0, lt_trans hn (sq_sub_inv_ub k₄ ne0)⟩ }, suffices h : ∃ n : ℕ, n ≠ 0 ∧ (↑n)⁻¹ < (2*u)⁻¹ * (u*u - 2), { rcases h with ⟨n, ne0, hn⟩, exact ⟨n, ne0, inequ3 k₄ hn⟩, }, have k₆ : 0 < (2*u)⁻¹ * (u * u - 2):= inequ4 k₄ k₅, exact inv_lt_of_pos _ k₆, end end lemmas_for_lub_contra lemma sqrt_two_exists : (sup S)*(sup S) = (2 : R) := begin have k₂ : ({x : R | (0 < x) ∧ (x*x < 2)} : set R) ≠ ∅, from non_empty_S, have k₃ : is_sup (sup S) S, from sup_is_sup has_upper_bound_S k₂, have k₄ : ↑0 < sup S, { suffices h : (1 : R) ≤ sup S, { change (0 : R) < sup S, exact lt_of_lt_of_le zero_lt_one h, }, exact k₃.left (1 : R) one_in_S, }, rcases trichotomy' ((sup S)*(sup S)) (2 : R) with ub | sq_eq | lub, { exact ub_contra k₃.left k₄ ub.left, }, { exact sq_eq.right.left, }, { exact lub_contra k₃.right k₄ lub.right.right, }, end end sqrt_two end myreal end mth1001
8754143a3bb082cd721f97a5577bc38da47e8c79
8930e38ac0fae2e5e55c28d0577a8e44e2639a6d
/category/basic.lean
07c3fe2e130e6a98d982c3abbaab24e12d0af9f5
[ "Apache-2.0" ]
permissive
SG4316/mathlib
3d64035d02a97f8556ad9ff249a81a0a51a3321a
a7846022507b531a8ab53b8af8a91953fceafd3a
refs/heads/master
1,584,869,960,527
1,530,718,645,000
1,530,724,110,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,228
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 Extends the theory on functors, applicatives and monads. -/ universes u v w x y variables {α β γ : Type u} notation a ` $< `:1 f:1 := f a section functor variables {f : Type u → Type v} [functor f] [is_lawful_functor f] run_cmd mk_simp_attr `functor_norm @[functor_norm] theorem map_map (m : α → β) (g : β → γ) (x : f α) : g <$> (m <$> x) = (g ∘ m) <$> x := (comp_map _ _ _).symm @[simp] theorem id_map' (x : f α) : (λa, a) <$> x = x := id_map _ end functor section applicative variables {f : Type u → Type v} [applicative f] [is_lawful_applicative f] attribute [functor_norm] seq_assoc pure_seq_eq_map @[simp] theorem pure_id'_seq (x : f α) : pure (λx, x) <*> x = x := pure_id_seq x @[functor_norm] theorem seq_map_assoc (x : f (α → β)) (g : γ → α) (y : f γ) : (x <*> (g <$> y)) = (λ(m:α→β), m ∘ g) <$> x <*> y := begin simp [(pure_seq_eq_map _ _).symm], simp [seq_assoc, (comp_map _ _ _).symm, (∘)], simp [pure_seq_eq_map] end @[functor_norm] theorem map_seq (g : β → γ) (x : f (α → β)) (y : f α) : (g <$> (x <*> y)) = ((∘) g) <$> x <*> y := by simp [(pure_seq_eq_map _ _).symm]; simp [seq_assoc] end applicative -- TODO: setup `functor_norm` for `monad` laws section monad variables {m : Type u → Type v} [monad m] [is_lawful_monad m] lemma map_bind (x : m α) {g : α → m β} {f : β → γ} : f <$> (x >>= g) = (x >>= λa, f <$> g a) := by simp [bind_assoc, (∘), (bind_pure_comp_eq_map _ _ _).symm] lemma seq_bind_eq (x : m α) {g : β → m γ} {f : α → β} : (f <$> x) >>= g = (x >>= g ∘ f) := show bind (f <$> x) g = bind x (g ∘ f), by rw [← bind_pure_comp_eq_map, bind_assoc]; simp [pure_bind] lemma seq_eq_bind_map {x : m α} {f : m (α → β)} : f <*> x = (f >>= (<$> x)) := (bind_map_eq_seq m f x).symm end monad section alternative variables {f : Type → Type v} [alternative f] @[simp] theorem guard_true {h : decidable true} : @guard f _ true h = pure () := by simp [guard] @[simp] theorem guard_false {h : decidable false} : @guard f _ false h = failure := by simp [guard] end alternative class is_comm_applicative (m : Type* → Type*) [applicative m] extends is_lawful_applicative m : Prop := (commutative_prod : ∀{α β} (a : m α) (b : m β), prod.mk <$> a <*> b = (λb a, (a, b)) <$> b <*> a) lemma is_comm_applicative.commutative_map {m : Type* → Type*} [applicative m] [is_comm_applicative m] {α β γ} (a : m α) (b : m β) {f : α → β → γ} : f <$> a <*> b = flip f <$> b <*> a := calc f <$> a <*> b = (λp:α×β, f p.1 p.2) <$> (prod.mk <$> a <*> b) : by simp [seq_map_assoc, map_seq, seq_assoc, seq_pure, map_map] ... = (λb a, f a b) <$> b <*> a : by rw [is_comm_applicative.commutative_prod]; simp [seq_map_assoc, map_seq, seq_assoc, seq_pure, map_map] def mmap₂ {α₁ α₂ φ : Type u} {m : Type u → Type*} [applicative m] (f : α₁ → α₂ → m φ) : Π (ma₁ : list α₁) (ma₂: list α₂), m (list φ) | (x :: xs) (y :: ys) := (::) <$> f x y <*> mmap₂ xs ys | _ _ := pure []
bccfa17ca5403583abfe8e2dd0faaa26e0555c3a
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/run/expr1.lean
01f1d9596c3098c64fb64da437269e64e0b1e41f
[ "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
4,231
lean
import Lean.Expr new_frontend open Lean def tst1 : IO Unit := do let f := mkConst `f []; let a := mkConst `a []; let b := mkConst `b []; let t := mkAppN f #[a, b, b]; let as₁ := t.getAppArgs; let as₂ := t.getAppRevArgs; IO.println as₁; IO.println as₂; «unless» (as₁.reverse == as₂) $ throw $ IO.userError "failed"; pure () #eval tst1 def tst2 : IO Unit := do let l1 := mkLevelMax (mkLevelParam `a) (mkLevelParam `b); let l2 := mkLevelMax (mkLevelParam `b) (mkLevelParam `a); IO.println l1; IO.println l2; «unless» (Level.isEquiv l1 l2) $ throw $ IO.userError "not equiv"; pure () #eval tst2 def tst3 : IO Unit := do let f := mkConst `f []; let a := mkConst `a []; let b := mkConst `b []; let c := mkConst `c []; let t := mkAppN f #[a, b, c]; IO.println $ t.getArg! 0; IO.println $ t.getArg! 1; IO.println $ t.getArg! 2; pure () #eval tst3 def tst4 : IO Unit := do let f := mkConst `f []; let a := mkConst `a []; let b := mkConst `b []; let x0 := mkBVar 0; let x1 := mkBVar 1; let t1 := mkAppN f #[a, b]; let t2 := mkAppN f #[a, x0]; let t3 := mkLambda `x BinderInfo.default (mkSort levelZero) (mkAppN f #[a, x0]); let t4 := mkLambda `x BinderInfo.default (mkSort levelZero) (mkAppN f #[a, x1]); «unless» (!t1.hasLooseBVar 0) $ throw $ IO.userError "failed-1"; «unless» (t2.hasLooseBVar 0) $ throw $ IO.userError "failed-2"; «unless» (!t3.hasLooseBVar 0) $ throw $ IO.userError "failed-3"; «unless» (t4.hasLooseBVar 0) $ throw $ IO.userError "failed-4"; «unless» (!t4.hasLooseBVar 1) $ throw $ IO.userError "failed-5"; «unless» (!t2.hasLooseBVar 1) $ throw $ IO.userError "failed-6"; pure () #eval tst4 def tst5 : IO Unit := do let f := mkConst `f []; let a := mkConst `a []; let nat := mkConst `Nat []; let x0 := mkBVar 0; let x1 := mkBVar 1; let x2 := mkBVar 2; let t := mkLambda `x BinderInfo.default nat (mkApp f x0); IO.println t.etaExpanded?; «unless» (t.etaExpanded? == some f) $ throw $ IO.userError "failed-1"; let t := mkLambda `x BinderInfo.default nat (mkApp f x1); «unless» (t.etaExpanded? == none) $ throw $ IO.userError "failed-2"; let t := mkLambda `x BinderInfo.default nat (mkAppN f #[a, x0]); «unless» (t.etaExpanded? == some (mkApp f a)) $ throw $ IO.userError "failed-3"; let t := mkLambda `x BinderInfo.default nat (mkAppN f #[x0, x0]); «unless» (t.etaExpanded? == none) $ throw $ IO.userError "failed-4"; let t := mkLambda `x BinderInfo.default nat (mkLambda `y BinderInfo.default nat (mkApp f x0)); «unless» (t.etaExpanded? == none) $ throw $ IO.userError "failed-5"; let t := mkLambda `x BinderInfo.default nat (mkLambda `y BinderInfo.default nat (mkAppN f #[x1, x0])); IO.println t; «unless» (t.etaExpanded? == some f) $ throw $ IO.userError "failed-6"; let t := mkLambda `x BinderInfo.default nat (mkLambda `y BinderInfo.default nat (mkLambda `z BinderInfo.default nat (mkAppN f #[x2, x1, x0]))); IO.println t; «unless» (t.etaExpanded? == some f) $ throw $ IO.userError "failed-7"; let t := mkLambda `x BinderInfo.default nat (mkLambda `y BinderInfo.default nat (mkLambda `z BinderInfo.default nat (mkAppN f #[a, x2, x1, x0]))); IO.println t; «unless» (t.etaExpanded? == some (mkApp f a)) $ throw $ IO.userError "failed-8"; IO.println t.etaExpanded?; let t := mkApp f a; «unless» (t.etaExpanded? == some (mkApp f a)) $ throw $ IO.userError "failed-9"; pure () #eval tst5 def tst6 : IO Unit := do let x1 := mkBVar 0; let x2 := mkBVar 1; let t1 := mkApp2 (mkConst `f) x1 x2; let t2 := mkForall `x BinderInfo.default (mkConst `Nat) t1; IO.println (t1.liftLooseBVars 0 1); IO.println (t2.liftLooseBVars 0 1); let t3 := (t2.liftLooseBVars 0 1).lowerLooseBVars 1 1; IO.println $ t3; «unless» (t2 == t3) $ throw $ IO.userError "failed-1"; pure () #eval tst6 def tst7 : IO Unit := do let x := mkFVar `x; let y := mkFVar `y; let f := mkConst `f; let t := mkAppN f #[x, y, mkNatLit 2]; let t := t.abstract #[x, y]; let t := t.instantiateRev #[mkNatLit 0, mkNatLit 1]; IO.println t #eval tst7
891d6b7bbc50cd8faf9f5b37066575a1a5e991d7
31f556cdeb9239ffc2fad8f905e33987ff4feab9
/src/Lean/Elab/Tactic/Conv/Basic.lean
42ca3eb343c70c8127b7f15f6f953362a4f731a4
[ "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
7,391
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.Reduce import Lean.Meta.Tactic.Apply import Lean.Meta.Tactic.Replace import Lean.Elab.Tactic.Basic import Lean.Elab.Tactic.BuiltinTactic namespace Lean.Elab.Tactic.Conv open Meta /-- Annotate `e` with the LHS annotation. The delaborator displays expressions of the form `lhs = rhs` as `lhs` when they have this annotation. This is used to implement the infoview for the `conv` mode. -/ def mkLHSGoal (e : Expr) : MetaM Expr := if let some _ := Expr.eq? e then return mkLHSGoalRaw e else return mkLHSGoalRaw (← whnf e) /-- Given `lhs`, returns a pair of metavariables `(?rhs, ?newGoal)` where `?newGoal : lhs = ?rhs`. `tag` is the name of `newGoal`. -/ def mkConvGoalFor (lhs : Expr) (tag : Name := .anonymous) : MetaM (Expr × Expr) := do let lhsType ← inferType lhs let rhs ← mkFreshExprMVar lhsType let targetNew := mkLHSGoalRaw (← mkEq lhs rhs) let newGoal ← mkFreshExprSyntheticOpaqueMVar targetNew tag return (rhs, newGoal) def markAsConvGoal (mvarId : MVarId) : MetaM MVarId := do let target ← mvarId.getType if isLHSGoal? target |>.isSome then return mvarId -- it is already tagged as LHS goal mvarId.replaceTargetDefEq (← mkLHSGoal (← mvarId.getType)) /-- Given `lhs`, runs the `conv` tactic with the goal `⊢ lhs = ?rhs`. `conv` should produce no remaining goals that are not solvable with refl. Returns a pair of instantiated expressions `(?rhs, ?p)` where `?p : lhs = ?rhs`. -/ def convert (lhs : Expr) (conv : TacticM Unit) : TacticM (Expr × Expr) := do let (rhs, newGoal) ← mkConvGoalFor lhs let savedGoals ← getGoals try setGoals [newGoal.mvarId!] conv for mvarId in (← getGoals) do liftM <| mvarId.refl <|> mvarId.inferInstance <|> pure () pruneSolvedGoals unless (← getGoals).isEmpty do throwError "convert tactic failed, there are unsolved goals\n{goalsToMessageData (← getGoals)}" pure () finally setGoals savedGoals return (← instantiateMVars rhs, ← instantiateMVars newGoal) def getLhsRhsCore (mvarId : MVarId) : MetaM (Expr × Expr) := mvarId.withContext do let some (_, lhs, rhs) ← matchEq? (← mvarId.getType) | throwError "invalid 'conv' goal" return (lhs, rhs) def getLhsRhs : TacticM (Expr × Expr) := do getLhsRhsCore (← getMainGoal) def getLhs : TacticM Expr := return (← getLhsRhs).1 def getRhs : TacticM Expr := return (← getLhsRhs).2 /-- `⊢ lhs = rhs` ~~> `⊢ lhs' = rhs` using `h : lhs = lhs'`. -/ def updateLhs (lhs' : Expr) (h : Expr) : TacticM Unit := do let mvarId ← getMainGoal let rhs ← getRhs let newGoal ← mkFreshExprSyntheticOpaqueMVar (mkLHSGoalRaw (← mkEq lhs' rhs)) (← mvarId.getTag) mvarId.assign (← mkEqTrans h newGoal) replaceMainGoal [newGoal.mvarId!] /-- Replace `lhs` with the definitionally equal `lhs'`. -/ def changeLhs (lhs' : Expr) : TacticM Unit := do let rhs ← getRhs liftMetaTactic1 fun mvarId => do mvarId.replaceTargetDefEq (mkLHSGoalRaw (← mkEq lhs' rhs)) @[builtinTactic Lean.Parser.Tactic.Conv.whnf] def evalWhnf : Tactic := fun _ => withMainContext do changeLhs (← whnf (← getLhs)) @[builtinTactic Lean.Parser.Tactic.Conv.reduce] def evalReduce : Tactic := fun _ => withMainContext do changeLhs (← reduce (← getLhs)) @[builtinTactic Lean.Parser.Tactic.Conv.zeta] def evalZeta : Tactic := fun _ => withMainContext do changeLhs (← zetaReduce (← getLhs)) /-- Evaluate `sepByIndent conv "; " -/ def evalSepByIndentConv (stx : Syntax) : TacticM Unit := do for arg in stx.getArgs, i in [:stx.getArgs.size] do if i % 2 == 0 then evalTactic arg else saveTacticInfoForToken arg @[builtinTactic Lean.Parser.Tactic.Conv.convSeq1Indented] def evalConvSeq1Indented : Tactic := fun stx => do evalSepByIndentConv stx[0] @[builtinTactic Lean.Parser.Tactic.Conv.convSeqBracketed] def evalConvSeqBracketed : Tactic := fun stx => do let initInfo ← mkInitialTacticInfo stx[0] withRef stx[2] <| closeUsingOrAdmit do -- save state before/after entering focus on `{` withInfoContext (pure ()) initInfo evalSepByIndentConv stx[1] evalTactic (← `(tactic| all_goals (try rfl))) @[builtinTactic Lean.Parser.Tactic.Conv.nestedConv] def evalNestedConv : Tactic := fun stx => do evalConvSeqBracketed stx[0] @[builtinTactic Lean.Parser.Tactic.Conv.convSeq] def evalConvSeq : Tactic := fun stx => do evalTactic stx[0] @[builtinTactic Lean.Parser.Tactic.Conv.convConvSeq] def evalConvConvSeq : Tactic := fun stx => withMainContext do let (lhsNew, proof) ← convert (← getLhs) (evalTactic stx[2][0]) updateLhs lhsNew proof @[builtinTactic Lean.Parser.Tactic.Conv.paren] def evalParen : Tactic := fun stx => evalTactic stx[1] /-- Mark goals of the form `⊢ a = ?m ..` with the conv goal annotation -/ def remarkAsConvGoal : TacticM Unit := do let newGoals ← (← getUnsolvedGoals).mapM fun mvarId => mvarId.withContext do let target ← mvarId.getType if let some (_, _, rhs) ← matchEq? target then if rhs.getAppFn.isMVar then mvarId.replaceTargetDefEq (← mkLHSGoal target) else return mvarId else return mvarId setGoals newGoals @[builtinTactic Lean.Parser.Tactic.Conv.nestedTacticCore] def evalNestedTacticCore : Tactic := fun stx => do let seq := stx[2] evalTactic seq; remarkAsConvGoal @[builtinTactic Lean.Parser.Tactic.Conv.nestedTactic] def evalNestedTactic : Tactic := fun stx => do let seq := stx[2] let target ← getMainTarget if let some _ := isLHSGoal? target then liftMetaTactic1 fun mvarId => mvarId.replaceTargetDefEq target.mdataExpr! focus do evalTactic seq; remarkAsConvGoal @[builtinTactic Lean.Parser.Tactic.Conv.convTactic] def evalConvTactic : Tactic := fun stx => evalTactic stx[2] private def convTarget (conv : Syntax) : TacticM Unit := withMainContext do let target ← getMainTarget let (targetNew, proof) ← convert target (withTacticInfoContext (← getRef) (evalTactic conv)) liftMetaTactic1 fun mvarId => mvarId.replaceTargetEq targetNew proof evalTactic (← `(tactic| try rfl)) private def convLocalDecl (conv : Syntax) (hUserName : Name) : TacticM Unit := withMainContext do let localDecl ← getLocalDeclFromUserName hUserName let (typeNew, proof) ← convert localDecl.type (withTacticInfoContext (← getRef) (evalTactic conv)) liftMetaTactic1 fun mvarId => return some (← mvarId.replaceLocalDecl localDecl.fvarId typeNew proof).mvarId @[builtinTactic Lean.Parser.Tactic.Conv.conv] def evalConv : Tactic := fun stx => do match stx with | `(tactic| conv%$tk $[at $loc?]? in $(occs)? $p =>%$arr $code) => evalTactic (← `(tactic| conv%$tk $[at $loc?]? =>%$arr pattern $(occs)? $p; ($code:convSeq))) | `(tactic| conv%$tk $[at $loc?]? =>%$arr $code) => -- show initial conv goal state between `conv` and `=>` withRef (mkNullNode #[tk, arr]) do if let some loc := loc? then convLocalDecl code loc.getId else convTarget code | _ => throwUnsupportedSyntax @[builtinTactic Lean.Parser.Tactic.Conv.first] partial def evalFirst : Tactic := Tactic.evalFirst end Lean.Elab.Tactic.Conv
3cf311fb20822f0cd33fc307290e8a09893125fb
bdb33f8b7ea65f7705fc342a178508e2722eb851
/logic/basic.lean
6023505857e4e6278cceea4d9a6b21063ffadff7
[ "Apache-2.0" ]
permissive
rwbarton/mathlib
939ae09bf8d6eb1331fc2f7e067d39567e10e33d
c13c5ea701bb1eec057e0a242d9f480a079105e9
refs/heads/master
1,584,015,335,862
1,524,142,167,000
1,524,142,167,000
130,614,171
0
0
Apache-2.0
1,548,902,667,000
1,524,437,371,000
Lean
UTF-8
Lean
false
false
22,064
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura Theorems that require decidability hypotheses are in the namespace "decidable". Classical versions are in the namespace "classical". Note: in the presence of automation, this whole file may be unnecessary. On the other hand, maybe it is useful for writing automation. -/ import data.prod tactic.interactive /- miscellany TODO: move elsewhere -/ section miscellany variables {α : Type*} {β : Type*} def empty.elim {C : Sort*} : empty → C. instance : subsingleton empty := ⟨λa, a.elim⟩ instance : decidable_eq empty := λa, a.elim @[priority 0] instance decidable_eq_of_subsingleton {α} [subsingleton α] : decidable_eq α | a b := is_true (subsingleton.elim a b) /- Add an instance to "undo" coercion transitivity into a chain of coercions, because most simp lemmas are stated with respect to simple coercions and will not match when part of a chain. -/ @[simp] theorem coe_coe {α β γ} [has_coe α β] [has_coe_t β γ] (a : α) : (a : γ) = (a : β) := rfl end miscellany /- propositional connectives -/ @[simp] theorem false_ne_true : false ≠ true | h := h.symm ▸ trivial section propositional variables {a b c d : Prop} /- implies -/ theorem iff_of_eq (e : a = b) : a ↔ b := e ▸ iff.rfl theorem iff_iff_eq : (a ↔ b) ↔ a = b := ⟨propext, iff_of_eq⟩ @[simp] theorem imp_self : (a → a) ↔ true := iff_true_intro id theorem imp_intro {α β} (h : α) (h₂ : β) : α := h theorem imp_false : (a → false) ↔ ¬ a := iff.rfl theorem imp_and_distrib {α} : (α → b ∧ c) ↔ (α → b) ∧ (α → c) := ⟨λ h, ⟨λ ha, (h ha).left, λ ha, (h ha).right⟩, λ h ha, ⟨h.left ha, h.right ha⟩⟩ @[simp] theorem and_imp : (a ∧ b → c) ↔ (a → b → c) := iff.intro (λ h ha hb, h ⟨ha, hb⟩) (λ h ⟨ha, hb⟩, h ha hb) theorem iff_def : (a ↔ b) ↔ (a → b) ∧ (b → a) := iff_iff_implies_and_implies _ _ theorem iff_def' : (a ↔ b) ↔ (b → a) ∧ (a → b) := iff_def.trans and.comm @[simp] theorem imp_true_iff {α : Sort*} : (α → true) ↔ true := iff_true_intro $ λ_, trivial @[simp] theorem imp_iff_right (ha : a) : (a → b) ↔ b := ⟨λf, f ha, imp_intro⟩ /- not -/ theorem not.elim {α : Sort*} (H1 : ¬a) (H2 : a) : α := absurd H2 H1 @[reducible] theorem not.imp {a b : Prop} (H2 : ¬b) (H1 : a → b) : ¬a := mt H1 H2 theorem not_not_of_not_imp : ¬(a → b) → ¬¬a := mt not.elim theorem not_of_not_imp {α} : ¬(α → b) → ¬b := mt imp_intro theorem dec_em (p : Prop) [decidable p] : p ∨ ¬p := decidable.em p theorem by_contradiction {p} [decidable p] : (¬p → false) → p := decidable.by_contradiction @[simp] theorem not_not [decidable a] : ¬¬a ↔ a := iff.intro by_contradiction not_not_intro theorem of_not_not [decidable a] : ¬¬a → a := by_contradiction theorem of_not_imp [decidable a] (h : ¬ (a → b)) : a := by_contradiction (not_not_of_not_imp h) theorem not.imp_symm [decidable a] (h : ¬a → b) (hb : ¬b) : a := by_contradiction $ hb ∘ h theorem not_imp_comm [decidable a] [decidable b] : (¬a → b) ↔ (¬b → a) := ⟨not.imp_symm, not.imp_symm⟩ theorem imp.swap : (a → b → c) ↔ (b → a → c) := ⟨function.swap, function.swap⟩ theorem imp_not_comm : (a → ¬b) ↔ (b → ¬a) := imp.swap /- and -/ theorem not_and_of_not_left (b : Prop) : ¬a → ¬(a ∧ b) := mt and.left theorem not_and_of_not_right (a : Prop) {b : Prop} : ¬b → ¬(a ∧ b) := mt and.right theorem and.imp_left (h : a → b) : a ∧ c → b ∧ c := and.imp h id theorem and.imp_right (h : a → b) : c ∧ a → c ∧ b := and.imp id h theorem and_not_self_iff (a : Prop) : a ∧ ¬ a ↔ false := iff.intro (assume h, (h.right) (h.left)) (assume h, h.elim) theorem not_and_self_iff (a : Prop) : ¬ a ∧ a ↔ false := iff.intro (assume ⟨hna, ha⟩, hna ha) false.elim theorem and_iff_left_of_imp {a b : Prop} (h : a → b) : (a ∧ b) ↔ a := iff.intro and.left (λ ha, ⟨ha, h ha⟩) theorem and_iff_right_of_imp {a b : Prop} (h : b → a) : (a ∧ b) ↔ b := iff.intro and.right (λ hb, ⟨h hb, hb⟩) /- or -/ theorem or_of_or_of_imp_of_imp (h₁ : a ∨ b) (h₂ : a → c) (h₃ : b → d) : c ∨ d := or.imp h₂ h₃ h₁ theorem or_of_or_of_imp_left (h₁ : a ∨ c) (h : a → b) : b ∨ c := or.imp_left h h₁ theorem or_of_or_of_imp_right (h₁ : c ∨ a) (h : a → b) : c ∨ b := or.imp_right h h₁ theorem or.elim3 (h : a ∨ b ∨ c) (ha : a → d) (hb : b → d) (hc : c → d) : d := or.elim h ha (assume h₂, or.elim h₂ hb hc) theorem or_imp_distrib : (a ∨ b → c) ↔ (a → c) ∧ (b → c) := ⟨assume h, ⟨assume ha, h (or.inl ha), assume hb, h (or.inr hb)⟩, assume ⟨ha, hb⟩, or.rec ha hb⟩ theorem or_iff_not_imp_left [decidable a] : a ∨ b ↔ (¬ a → b) := ⟨or.resolve_left, λ h, dite _ or.inl (or.inr ∘ h)⟩ theorem or_iff_not_imp_right [decidable b] : a ∨ b ↔ (¬ b → a) := or.comm.trans or_iff_not_imp_left theorem not_imp_not [decidable a] : (¬ a → ¬ b) ↔ (b → a) := ⟨assume h hb, by_contradiction $ assume na, h na hb, mt⟩ /- distributivity -/ theorem and_or_distrib_left : a ∧ (b ∨ c) ↔ (a ∧ b) ∨ (a ∧ c) := ⟨λ ⟨ha, hbc⟩, hbc.imp (and.intro ha) (and.intro ha), or.rec (and.imp_right or.inl) (and.imp_right or.inr)⟩ theorem or_and_distrib_right : (a ∨ b) ∧ c ↔ (a ∧ c) ∨ (b ∧ c) := (and.comm.trans and_or_distrib_left).trans (or_congr and.comm and.comm) theorem or_and_distrib_left : a ∨ (b ∧ c) ↔ (a ∨ b) ∧ (a ∨ c) := ⟨or.rec (λha, and.intro (or.inl ha) (or.inl ha)) (and.imp or.inr or.inr), and.rec $ or.rec (imp_intro ∘ or.inl) (or.imp_right ∘ and.intro)⟩ theorem and_or_distrib_right : (a ∧ b) ∨ c ↔ (a ∨ c) ∧ (b ∨ c) := (or.comm.trans or_and_distrib_left).trans (and_congr or.comm or.comm) /- iff -/ theorem iff_of_true (ha : a) (hb : b) : a ↔ b := ⟨λ_, hb, λ _, ha⟩ theorem iff_of_false (ha : ¬a) (hb : ¬b) : a ↔ b := ⟨ha.elim, hb.elim⟩ theorem iff_true_left (ha : a) : (a ↔ b) ↔ b := ⟨λ h, h.1 ha, iff_of_true ha⟩ theorem iff_true_right (ha : a) : (b ↔ a) ↔ b := iff.comm.trans (iff_true_left ha) theorem iff_false_left (ha : ¬a) : (a ↔ b) ↔ ¬b := ⟨λ h, mt h.2 ha, iff_of_false ha⟩ theorem iff_false_right (ha : ¬a) : (b ↔ a) ↔ ¬b := iff.comm.trans (iff_false_left ha) theorem not_or_of_imp [decidable a] (h : a → b) : ¬ a ∨ b := if ha : a then or.inr (h ha) else or.inl ha theorem imp_iff_not_or [decidable a] : (a → b) ↔ (¬ a ∨ b) := ⟨not_or_of_imp, or.neg_resolve_left⟩ theorem imp_or_distrib [decidable a] : (a → b ∨ c) ↔ (a → b) ∨ (a → c) := by simp [imp_iff_not_or, or.comm, or.left_comm] theorem imp_or_distrib' [decidable b] : (a → b ∨ c) ↔ (a → b) ∨ (a → c) := by by_cases b; simp [h, or_iff_right_of_imp ((∘) false.elim)] theorem not_imp_of_and_not : a ∧ ¬ b → ¬ (a → b) | ⟨ha, hb⟩ h := hb $ h ha @[simp] theorem not_imp [decidable a] : ¬(a → b) ↔ a ∧ ¬b := ⟨λ h, ⟨of_not_imp h, not_of_not_imp h⟩, not_imp_of_and_not⟩ theorem peirce (a b : Prop) [decidable a] : ((a → b) → a) → a := if ha : a then λ h, ha else λ h, h ha.elim theorem peirce' {a : Prop} (H : ∀ b : Prop, (a → b) → a) : a := H _ id theorem not_iff_not [decidable a] [decidable b] : (¬ a ↔ ¬ b) ↔ (a ↔ b) := by rw [@iff_def (¬ a), @iff_def' a]; exact and_congr not_imp_not not_imp_not theorem not_iff_comm [decidable a] [decidable b] : (¬ a ↔ b) ↔ (¬ b ↔ a) := by rw [@iff_def (¬ a), @iff_def (¬ b)]; exact and_congr not_imp_comm imp_not_comm theorem iff_not_comm [decidable a] [decidable b] : (a ↔ ¬ b) ↔ (b ↔ ¬ a) := by rw [@iff_def a, @iff_def b]; exact and_congr imp_not_comm not_imp_comm @[simp] theorem not_and_not_right [decidable b] : ¬(a ∧ ¬b) ↔ (a → b) := ⟨λ h ha, h.imp_symm $ and.intro ha, λ h ⟨ha, hb⟩, hb $ h ha⟩ @[inline] def decidable_of_iff (a : Prop) (h : a ↔ b) [D : decidable a] : decidable b := decidable_of_decidable_of_iff D h @[inline] def decidable_of_iff' (b : Prop) (h : a ↔ b) [D : decidable b] : decidable a := decidable_of_decidable_of_iff D h.symm def decidable_of_bool : ∀ (b : bool) (h : b ↔ a), decidable a | tt h := is_true (h.1 rfl) | ff h := is_false (mt h.2 bool.ff_ne_tt) /- de morgan's laws -/ theorem not_and_of_not_or_not (h : ¬ a ∨ ¬ b) : ¬ (a ∧ b) | ⟨ha, hb⟩ := or.elim h (absurd ha) (absurd hb) theorem not_and_distrib [decidable a] : ¬ (a ∧ b) ↔ ¬a ∨ ¬b := ⟨λ h, if ha : a then or.inr (λ hb, h ⟨ha, hb⟩) else or.inl ha, not_and_of_not_or_not⟩ theorem not_and_distrib' [decidable b] : ¬ (a ∧ b) ↔ ¬a ∨ ¬b := ⟨λ h, if hb : b then or.inl (λ ha, h ⟨ha, hb⟩) else or.inr hb, not_and_of_not_or_not⟩ @[simp] theorem not_and : ¬ (a ∧ b) ↔ (a → ¬ b) := and_imp theorem not_and' : ¬ (a ∧ b) ↔ b → ¬a := not_and.trans imp_not_comm theorem not_or_distrib : ¬ (a ∨ b) ↔ ¬ a ∧ ¬ b := ⟨λ h, ⟨λ ha, h (or.inl ha), λ hb, h (or.inr hb)⟩, λ ⟨h₁, h₂⟩ h, or.elim h h₁ h₂⟩ theorem or_iff_not_and_not [decidable a] [decidable b] : a ∨ b ↔ ¬ (¬a ∧ ¬b) := by rw [← not_or_distrib, not_not] theorem and_iff_not_or_not [decidable a] [decidable b] : a ∧ b ↔ ¬ (¬ a ∨ ¬ b) := by rw [← not_and_distrib, not_not] end propositional /- equality -/ section equality variables {α : Sort*} {a b : α} @[simp] theorem heq_iff_eq : a == b ↔ a = b := ⟨eq_of_heq, heq_of_eq⟩ theorem ne_of_mem_of_not_mem {α β} [has_mem α β] {s : β} {a b : α} (h : a ∈ s) : b ∉ s → a ≠ b := mt $ λ e, e ▸ h end equality /- quantifiers -/ section quantifiers variables {α : Sort*} {p q : α → Prop} {b : Prop} def Exists.imp := @exists_imp_exists theorem forall_swap {α β} {p : α → β → Prop} : (∀ x y, p x y) ↔ ∀ y x, p x y := ⟨function.swap, function.swap⟩ theorem exists_swap {α β} {p : α → β → Prop} : (∃ x y, p x y) ↔ ∃ y x, p x y := ⟨λ ⟨x, y, h⟩, ⟨y, x, h⟩, λ ⟨y, x, h⟩, ⟨x, y, h⟩⟩ @[simp] theorem exists_imp_distrib : ((∃ x, p x) → b) ↔ ∀ x, p x → b := ⟨λ h x hpx, h ⟨x, hpx⟩, λ h ⟨x, hpx⟩, h x hpx⟩ --theorem forall_not_of_not_exists (h : ¬ ∃ x, p x) : ∀ x, ¬ p x := --forall_imp_of_exists_imp h theorem not_exists_of_forall_not (h : ∀ x, ¬ p x) : ¬ ∃ x, p x := exists_imp_distrib.2 h @[simp] theorem not_exists : (¬ ∃ x, p x) ↔ ∀ x, ¬ p x := exists_imp_distrib theorem not_forall_of_exists_not : (∃ x, ¬ p x) → ¬ ∀ x, p x | ⟨x, hn⟩ h := hn (h x) theorem not_forall {p : α → Prop} [decidable (∃ x, ¬ p x)] [∀ x, decidable (p x)] : (¬ ∀ x, p x) ↔ ∃ x, ¬ p x := ⟨not.imp_symm $ λ nx x, nx.imp_symm $ λ h, ⟨x, h⟩, not_forall_of_exists_not⟩ @[simp] theorem not_forall_not [decidable (∃ x, p x)] : (¬ ∀ x, ¬ p x) ↔ ∃ x, p x := by haveI := decidable_of_iff (¬ ∃ x, p x) not_exists; exact not_iff_comm.1 not_exists @[simp] theorem not_exists_not [∀ x, decidable (p x)] : (¬ ∃ x, ¬ p x) ↔ ∀ x, p x := by simp @[simp] theorem forall_true_iff : (α → true) ↔ true := iff_true_intro (λ _, trivial) -- Unfortunately this causes simp to loop sometimes, so we -- add the 2 and 3 cases as simp lemmas instead theorem forall_true_iff' (h : ∀ a, p a ↔ true) : (∀ a, p a) ↔ true := iff_true_intro (λ _, of_iff_true (h _)) @[simp] theorem forall_2_true_iff {β : α → Sort*} : (∀ a, β a → true) ↔ true := forall_true_iff' $ λ _, forall_true_iff @[simp] theorem forall_3_true_iff {β : α → Sort*} {γ : Π a, β a → Sort*} : (∀ a (b : β a), γ a b → true) ↔ true := forall_true_iff' $ λ _, forall_2_true_iff @[simp] theorem forall_const (α : Sort*) [inhabited α] : (α → b) ↔ b := ⟨λ h, h (arbitrary α), λ hb x, hb⟩ @[simp] theorem exists_const (α : Sort*) [inhabited α] : (∃ x : α, b) ↔ b := ⟨λ ⟨x, h⟩, h, λ h, ⟨arbitrary α, h⟩⟩ theorem forall_and_distrib : (∀ x, p x ∧ q x) ↔ (∀ x, p x) ∧ (∀ x, q x) := ⟨λ h, ⟨λ x, (h x).left, λ x, (h x).right⟩, λ ⟨h₁, h₂⟩ x, ⟨h₁ x, h₂ x⟩⟩ theorem exists_or_distrib : (∃ x, p x ∨ q x) ↔ (∃ x, p x) ∨ (∃ x, q x) := ⟨λ ⟨x, hpq⟩, hpq.elim (λ hpx, or.inl ⟨x, hpx⟩) (λ hqx, or.inr ⟨x, hqx⟩), λ hepq, hepq.elim (λ ⟨x, hpx⟩, ⟨x, or.inl hpx⟩) (λ ⟨x, hqx⟩, ⟨x, or.inr hqx⟩)⟩ @[simp] theorem exists_and_distrib_left {q : Prop} {p : α → Prop} : (∃x, q ∧ p x) ↔ q ∧ (∃x, p x) := ⟨λ ⟨x, hq, hp⟩, ⟨hq, x, hp⟩, λ ⟨hq, x, hp⟩, ⟨x, hq, hp⟩⟩ @[simp] theorem exists_and_distrib_right {q : Prop} {p : α → Prop} : (∃x, p x ∧ q) ↔ (∃x, p x) ∧ q := by simp [and_comm] @[simp] theorem forall_eq {a' : α} : (∀a, a = a' → p a) ↔ p a' := ⟨λ h, h a' rfl, λ h a e, e.symm ▸ h⟩ @[simp] theorem exists_eq {a' : α} : (∃ a, a = a' ∧ p a) ↔ p a' := ⟨λ ⟨a, e, h⟩, e ▸ h, λ h, ⟨_, rfl, h⟩⟩ @[simp] theorem exists_eq_right {a' : α} : (∃ a, p a ∧ a = a') ↔ p a' := (exists_congr $ by exact λ a, and.comm).trans exists_eq @[simp] theorem forall_eq' {a' : α} : (∀a, a' = a → p a) ↔ p a' := by simp [@eq_comm _ a'] @[simp] theorem exists_eq' {a' : α} : (∃ a, a' = a ∧ p a) ↔ p a' := by simp [@eq_comm _ a'] @[simp] theorem exists_eq_right' {a' : α} : (∃ a, p a ∧ a' = a) ↔ p a' := by simp [@eq_comm _ a'] theorem forall_or_of_or_forall (h : b ∨ ∀x, p x) (x) : b ∨ p x := h.imp_right $ λ h₂, h₂ x theorem forall_or_distrib_left {q : Prop} {p : α → Prop} [decidable q] : (∀x, q ∨ p x) ↔ q ∨ (∀x, p x) := ⟨λ h, if hq : q then or.inl hq else or.inr $ λ x, (h x).resolve_left hq, forall_or_of_or_forall⟩ @[simp] theorem exists_prop {p q : Prop} : (∃ h : p, q) ↔ p ∧ q := ⟨λ ⟨h₁, h₂⟩, ⟨h₁, h₂⟩, λ ⟨h₁, h₂⟩, ⟨h₁, h₂⟩⟩ @[simp] theorem exists_false : ¬ (∃a:α, false) := assume ⟨a, h⟩, h theorem Exists.fst {p : b → Prop} : Exists p → b | ⟨h, _⟩ := h theorem Exists.snd {p : b → Prop} : ∀ h : Exists p, p h.fst | ⟨_, h⟩ := h @[simp] theorem forall_prop_of_true {p : Prop} {q : p → Prop} (h : p) : (∀ h' : p, q h') ↔ q h := @forall_const (q h) p ⟨h⟩ @[simp] theorem exists_prop_of_true {p : Prop} {q : p → Prop} (h : p) : (∃ h' : p, q h') ↔ q h := @exists_const (q h) p ⟨h⟩ @[simp] theorem forall_prop_of_false {p : Prop} {q : p → Prop} (hn : ¬ p) : (∀ h' : p, q h') ↔ true := iff_true_intro $ λ h, hn.elim h @[simp] theorem exists_prop_of_false {p : Prop} {q : p → Prop} : ¬ p → ¬ (∃ h' : p, q h') := mt Exists.fst end quantifiers /- classical versions -/ namespace classical variables {α : Sort*} {p : α → Prop} local attribute [instance] prop_decidable protected theorem not_forall : (¬ ∀ x, p x) ↔ (∃ x, ¬ p x) := not_forall protected theorem forall_or_distrib_left {q : Prop} {p : α → Prop} : (∀x, q ∨ p x) ↔ q ∨ (∀x, p x) := forall_or_distrib_left theorem cases {p : Prop → Prop} (h1 : p true) (h2 : p false) : ∀a, p a := assume a, cases_on a h1 h2 theorem or_not {p : Prop} : p ∨ ¬ p := by_cases or.inl or.inr protected theorem or_iff_not_imp_left {p q : Prop} : p ∨ q ↔ (¬ p → q) := or_iff_not_imp_left protected theorem or_iff_not_imp_right {p q : Prop} : q ∨ p ↔ (¬ p → q) := or_iff_not_imp_right /- use shortened names to avoid conflict when classical namespace is open -/ noncomputable theorem dec (p : Prop) : decidable p := by apply_instance noncomputable theorem dec_pred (p : α → Prop) : decidable_pred p := by apply_instance noncomputable theorem dec_rel (p : α → α → Prop) : decidable_rel p := by apply_instance noncomputable theorem dec_eq (α : Sort*) : decidable_eq α := by apply_instance end classical /- bounded quantifiers -/ section bounded_quantifiers variables {α : Sort*} {r p q : α → Prop} {P Q : ∀ x, p x → Prop} {b : Prop} theorem bex_def : (∃ x (h : p x), q x) ↔ ∃ x, p x ∧ q x := ⟨λ ⟨x, px, qx⟩, ⟨x, px, qx⟩, λ ⟨x, px, qx⟩, ⟨x, px, qx⟩⟩ theorem bex.elim {b : Prop} : (∃ x h, P x h) → (∀ a h, P a h → b) → b | ⟨a, h₁, h₂⟩ h' := h' a h₁ h₂ theorem bex.intro (a : α) (h₁ : p a) (h₂ : P a h₁) : ∃ x (h : p x), P x h := ⟨a, h₁, h₂⟩ theorem ball_congr (H : ∀ x h, P x h ↔ Q x h) : (∀ x h, P x h) ↔ (∀ x h, Q x h) := forall_congr $ λ x, forall_congr (H x) theorem bex_congr (H : ∀ x h, P x h ↔ Q x h) : (∃ x h, P x h) ↔ (∃ x h, Q x h) := exists_congr $ λ x, exists_congr (H x) theorem ball.imp_right (H : ∀ x h, (P x h → Q x h)) (h₁ : ∀ x h, P x h) (x h) : Q x h := H _ _ $ h₁ _ _ theorem bex.imp_right (H : ∀ x h, (P x h → Q x h)) : (∃ x h, P x h) → ∃ x h, Q x h | ⟨x, h, h'⟩ := ⟨_, _, H _ _ h'⟩ theorem ball.imp_left (H : ∀ x, p x → q x) (h₁ : ∀ x, q x → r x) (x) (h : p x) : r x := h₁ _ $ H _ h theorem bex.imp_left (H : ∀ x, p x → q x) : (∃ x (_ : p x), r x) → ∃ x (_ : q x), r x | ⟨x, hp, hr⟩ := ⟨x, H _ hp, hr⟩ theorem ball_of_forall (h : ∀ x, p x) (x) (_ : q x) : p x := h x theorem forall_of_ball (H : ∀ x, p x) (h : ∀ x, p x → q x) (x) : q x := h x $ H x theorem bex_of_exists (H : ∀ x, p x) : (∃ x, q x) → ∃ x (_ : p x), q x | ⟨x, hq⟩ := ⟨x, H x, hq⟩ theorem exists_of_bex : (∃ x (_ : p x), q x) → ∃ x, q x | ⟨x, _, hq⟩ := ⟨x, hq⟩ @[simp] theorem bex_imp_distrib : ((∃ x h, P x h) → b) ↔ (∀ x h, P x h → b) := by simp theorem not_bex : (¬ ∃ x h, P x h) ↔ ∀ x h, ¬ P x h := bex_imp_distrib theorem not_ball_of_bex_not : (∃ x h, ¬ P x h) → ¬ ∀ x h, P x h | ⟨x, h, hp⟩ al := hp $ al x h theorem not_ball [decidable (∃ x h, ¬ P x h)] [∀ x h, decidable (P x h)] : (¬ ∀ x h, P x h) ↔ (∃ x h, ¬ P x h) := ⟨not.imp_symm $ λ nx x h, nx.imp_symm $ λ h', ⟨x, h, h'⟩, not_ball_of_bex_not⟩ theorem ball_true_iff (p : α → Prop) : (∀ x, p x → true) ↔ true := iff_true_intro (λ h hrx, trivial) theorem ball_and_distrib : (∀ x h, P x h ∧ Q x h) ↔ (∀ x h, P x h) ∧ (∀ x h, Q x h) := iff.trans (forall_congr $ λ x, forall_and_distrib) forall_and_distrib theorem bex_or_distrib : (∃ x h, P x h ∨ Q x h) ↔ (∃ x h, P x h) ∨ (∃ x h, Q x h) := iff.trans (exists_congr $ λ x, exists_or_distrib) exists_or_distrib end bounded_quantifiers namespace classical local attribute [instance] prop_decidable theorem not_ball {α : Sort*} {p : α → Prop} {P : Π (x : α), p x → Prop} : (¬ ∀ x h, P x h) ↔ (∃ x h, ¬ P x h) := _root_.not_ball end classical section nonempty universes u v w variables {α : Type u} {β : Type v} {γ : α → Type w} attribute [simp] nonempty_of_inhabited @[simp] lemma nonempty_Prop {p : Prop} : nonempty p ↔ p := iff.intro (assume ⟨h⟩, h) (assume h, ⟨h⟩) lemma not_nonempty_iff_imp_false {p : Prop} : ¬ nonempty α ↔ α → false := ⟨λ h a, h ⟨a⟩, λ h ⟨a⟩, h a⟩ @[simp] lemma nonempty_sigma : nonempty (Σa:α, γ a) ↔ (∃a:α, nonempty (γ a)) := iff.intro (assume ⟨⟨a, c⟩⟩, ⟨a, ⟨c⟩⟩) (assume ⟨a, ⟨c⟩⟩, ⟨⟨a, c⟩⟩) @[simp] lemma nonempty_subtype {α : Sort u} {p : α → Prop} : nonempty (subtype p) ↔ (∃a:α, p a) := iff.intro (assume ⟨⟨a, h⟩⟩, ⟨a, h⟩) (assume ⟨a, h⟩, ⟨⟨a, h⟩⟩) @[simp] lemma nonempty_prod : nonempty (α × β) ↔ (nonempty α ∧ nonempty β) := iff.intro (assume ⟨⟨a, b⟩⟩, ⟨⟨a⟩, ⟨b⟩⟩) (assume ⟨⟨a⟩, ⟨b⟩⟩, ⟨⟨a, b⟩⟩) @[simp] lemma nonempty_pprod {α : Sort u} {β : Sort v} : nonempty (pprod α β) ↔ (nonempty α ∧ nonempty β) := iff.intro (assume ⟨⟨a, b⟩⟩, ⟨⟨a⟩, ⟨b⟩⟩) (assume ⟨⟨a⟩, ⟨b⟩⟩, ⟨⟨a, b⟩⟩) @[simp] lemma nonempty_sum : nonempty (α ⊕ β) ↔ (nonempty α ∨ nonempty β) := iff.intro (assume ⟨h⟩, match h with sum.inl a := or.inl ⟨a⟩ | sum.inr b := or.inr ⟨b⟩ end) (assume h, match h with or.inl ⟨a⟩ := ⟨sum.inl a⟩ | or.inr ⟨b⟩ := ⟨sum.inr b⟩ end) @[simp] lemma nonempty_psum {α : Sort u} {β : Sort v} : nonempty (psum α β) ↔ (nonempty α ∨ nonempty β) := iff.intro (assume ⟨h⟩, match h with psum.inl a := or.inl ⟨a⟩ | psum.inr b := or.inr ⟨b⟩ end) (assume h, match h with or.inl ⟨a⟩ := ⟨psum.inl a⟩ | or.inr ⟨b⟩ := ⟨psum.inr b⟩ end) @[simp] lemma nonempty_psigma {α : Sort u} {β : α → Sort v} : nonempty (psigma β) ↔ (∃a:α, nonempty (β a)) := iff.intro (assume ⟨⟨a, c⟩⟩, ⟨a, ⟨c⟩⟩) (assume ⟨a, ⟨c⟩⟩, ⟨⟨a, c⟩⟩) @[simp] lemma nonempty_empty : ¬ nonempty empty := assume ⟨h⟩, h.elim @[simp] lemma nonempty_ulift : nonempty (ulift α) ↔ nonempty α := iff.intro (assume ⟨⟨a⟩⟩, ⟨a⟩) (assume ⟨a⟩, ⟨⟨a⟩⟩) @[simp] lemma nonempty_plift {α : Sort u} : nonempty (plift α) ↔ nonempty α := iff.intro (assume ⟨⟨a⟩⟩, ⟨a⟩) (assume ⟨a⟩, ⟨⟨a⟩⟩) @[simp] lemma nonempty.forall {α : Sort u} {p : nonempty α → Prop} : (∀h:nonempty α, p h) ↔ (∀a, p ⟨a⟩) := iff.intro (assume h a, h _) (assume h ⟨a⟩, h _) @[simp] lemma nonempty.exists {α : Sort u} {p : nonempty α → Prop} : (∃h:nonempty α, p h) ↔ (∃a, p ⟨a⟩) := iff.intro (assume ⟨⟨a⟩, h⟩, ⟨a, h⟩) (assume ⟨a, h⟩, ⟨⟨a⟩, h⟩) lemma classical.nonempty_pi {α : Sort u} {β : α → Sort v} : nonempty (Πa:α, β a) ↔ (∀a:α, nonempty (β a)) := iff.intro (assume ⟨f⟩ a, ⟨f a⟩) (assume f, ⟨assume a, classical.choice $ f a⟩) end nonempty
50937d5f23ab5866e387224fbdfa9d5ec6006c8c
54deab7025df5d2df4573383df7e1e5497b7a2c2
/algebra/group_power.lean
24b1bbf34508e90687bf44fc137a8e89c2d117b8
[ "Apache-2.0" ]
permissive
HGldJ1966/mathlib
f8daac93a5b4ae805cfb0ecebac21a9ce9469009
c5c5b504b918a6c5e91e372ee29ed754b0513e85
refs/heads/master
1,611,340,395,683
1,503,040,489,000
1,503,040,489,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,553
lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Robert Y. Lewis The power operation on monoids and groups. We separate this from group, because it depends on nat, which in turn depends on other parts of algebra. We have "pow a n" for natural number powers, and "gpow a i" for integer powers. The notation a^n is used for the first, but users can locally redefine it to gpow when needed. Note: power adopts the convention that 0^0=1. -/ import data.nat.basic data.int.basic tactic.finish universe u variable {α : Type u} class has_pow_nat (α : Type u) := (pow_nat : α → nat → α) def pow_nat {α : Type u} [has_pow_nat α] : α → nat → α := has_pow_nat.pow_nat infix ` ^ ` := pow_nat class has_pow_int (α : Type u) := (pow_int : α → int → α) def pow_int {α : Type u} [has_pow_int α] : α → int → α := has_pow_int.pow_int /- monoid -/ section monoid variable [monoid α] def monoid.pow (a : α) : ℕ → α | 0 := 1 | (n+1) := a * monoid.pow n instance monoid.has_pow_nat : has_pow_nat α := has_pow_nat.mk monoid.pow @[simp] theorem pow_zero (a : α) : a^0 = 1 := rfl theorem pow_succ (a : α) (n : ℕ) : a^(n+1) = a * a^n := rfl @[simp] theorem pow_one (a : α) : a^1 = a := mul_one _ theorem pow_mul_comm' (a : α) (n : ℕ) : a^n * a = a * a^n := by induction n with n ih; simp [*, pow_succ] theorem pow_succ' (a : α) (n : ℕ) : a^(n+1) = a^n * a := by simp [pow_succ, pow_mul_comm'] theorem pow_add (a : α) (m n : ℕ) : a^(m + n) = a^m * a^n := by induction n; simp [*, pow_succ', nat.add_succ] @[simp] theorem one_pow (n : ℕ) : (1 : α)^n = (1:α) := by induction n; simp [*, pow_succ] theorem pow_mul (a : α) (m : ℕ) : ∀ n, a^(m * n) = (a^m)^n | 0 := by simp | (n+1) := by rw [nat.mul_succ, pow_add, pow_succ', pow_mul] theorem pow_mul_comm (a : α) (m n : ℕ) : a^m * a^n = a^n * a^m := by rw [←pow_add, ←pow_add, add_comm] end monoid /- commutative monoid -/ section comm_monoid variable [comm_monoid α] theorem mul_pow (a b : α) : ∀ n, (a * b)^n = a^n * b^n | 0 := by simp | (n+1) := by simp [pow_succ]; rw mul_pow end comm_monoid section group variable [group α] section nat theorem inv_pow (a : α) : ∀n, (a⁻¹)^n = (a^n)⁻¹ | 0 := by simp | (n+1) := by rw [pow_succ', _root_.pow_succ, mul_inv_rev, inv_pow] theorem pow_sub (a : α) {m n : ℕ} (h : m ≥ n) : a^(m - n) = a^m * (a^n)⁻¹ := have h1 : m - n + n = m, from nat.sub_add_cancel h, have h2 : a^(m - n) * a^n = a^m, by rw [←pow_add, h1], eq_mul_inv_of_mul_eq h2 theorem pow_inv_comm (a : α) : ∀m n, (a⁻¹)^m * a^n = a^n * (a⁻¹)^m | 0 n := by simp | m 0 := by simp | (m+1) (n+1) := calc a⁻¹ ^ (m+1) * a ^ (n+1) = (a⁻¹ ^ m * a⁻¹) * (a * a^n) : by rw [pow_succ', _root_.pow_succ] ... = a⁻¹ ^ m * (a⁻¹ * a) * a^n : by simp [pow_succ, pow_succ'] ... = a⁻¹ ^ m * a^n : by simp ... = a ^ n * (a⁻¹)^m : by rw pow_inv_comm ... = a ^ n * (a * a⁻¹) * (a⁻¹)^m : by simp ... = (a^n * a) * (a⁻¹ * (a⁻¹)^m) : by simp only [mul_assoc] ... = a ^ (n+1) * a⁻¹ ^ (m+1) : by rw [pow_succ', _root_.pow_succ]; simp end nat open int def gpow (a : α) : ℤ → α | (of_nat n) := a^n | -[1+n] := (a^(nat.succ n))⁻¹ local attribute [ematch] le_of_lt open nat private lemma gpow_add_aux (a : α) (m n : nat) : gpow a ((of_nat m) + -[1+n]) = gpow a (of_nat m) * gpow a (-[1+n]) := or.elim (nat.lt_or_ge m (nat.succ n)) (assume : m < succ n, have m ≤ n, from le_of_lt_succ this, suffices gpow a -[1+ n-m] = (gpow a (of_nat m)) * (gpow a -[1+n]), by simp [*, of_nat_add_neg_succ_of_nat_of_lt], suffices (a^(nat.succ (n - m)))⁻¹ = (gpow a (of_nat m)) * (gpow a -[1+n]), from this, suffices (a^(nat.succ n - m))⁻¹ = (gpow a (of_nat m)) * (gpow a -[1+n]), by rw ←succ_sub; assumption, by rw pow_sub; finish [gpow]) (assume : m ≥ succ n, suffices gpow a (of_nat (m - succ n)) = (gpow a (of_nat m)) * (gpow a -[1+ n]), by rw [of_nat_add_neg_succ_of_nat_of_ge]; assumption, suffices a ^ (m - succ n) = a^m * (a^succ n)⁻¹, from this, by rw pow_sub; assumption) theorem gpow_add (a : α) : ∀i j : int, gpow a (i + j) = gpow a i * gpow a j | (of_nat m) (of_nat n) := pow_add _ _ _ | (of_nat m) -[1+n] := gpow_add_aux _ _ _ | -[1+m] (of_nat n) := begin rw [add_comm, gpow_add_aux], unfold gpow, rw [←inv_pow, pow_inv_comm] end | -[1+m] -[1+n] := suffices (a ^ (m + succ (succ n)))⁻¹ = (a ^ succ m)⁻¹ * (a ^ succ n)⁻¹, from this, by rw [←succ_add_eq_succ_add, add_comm, pow_add, mul_inv_rev] theorem gpow_mul_comm (a : α) (i j : ℤ) : gpow a i * gpow a j = gpow a j * gpow a i := by rw [←gpow_add, ←gpow_add, add_comm] end group section ordered_ring variable [linear_ordered_ring α] theorem pow_pos {a : α} (H : a > 0) : ∀ (n : ℕ), a ^ n > 0 | 0 := by simp; apply zero_lt_one | (n+1) := begin simp [_root_.pow_succ], apply mul_pos, assumption, apply pow_pos end theorem pow_ge_one_of_ge_one {a : α} (H : a ≥ 1) : ∀ (n : ℕ), a ^ n ≥ 1 | 0 := by simp; apply le_refl | (n+1) := begin simp [_root_.pow_succ], rw ←(one_mul (1 : α)), apply mul_le_mul, assumption, apply pow_ge_one_of_ge_one, apply zero_le_one, transitivity, apply zero_le_one, assumption end end ordered_ring
7ed11365d4caf411e6dbe258ed750aab843b9c32
4727251e0cd73359b15b664c3170e5d754078599
/src/analysis/analytic/basic.lean
02deec2c320b5728043983033e9b29272fe43777
[ "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
60,560
lean
/- Copyright (c) 2020 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel, Yury Kudryashov -/ import analysis.calculus.formal_multilinear_series import analysis.specific_limits.normed import logic.equiv.fin /-! # Analytic functions A function is analytic in one dimension around `0` if it can be written as a converging power series `Σ pₙ zⁿ`. This definition can be extended to any dimension (even in infinite dimension) by requiring that `pₙ` is a continuous `n`-multilinear map. In general, `pₙ` is not unique (in two dimensions, taking `p₂ (x, y) (x', y') = x y'` or `y x'` gives the same map when applied to a vector `(x, y) (x, y)`). A way to guarantee uniqueness is to take a symmetric `pₙ`, but this is not always possible in nonzero characteristic (in characteristic 2, the previous example has no symmetric representative). Therefore, we do not insist on symmetry or uniqueness in the definition, and we only require the existence of a converging series. The general framework is important to say that the exponential map on bounded operators on a Banach space is analytic, as well as the inverse on invertible operators. ## Main definitions Let `p` be a formal multilinear series from `E` to `F`, i.e., `p n` is a multilinear map on `E^n` for `n : ℕ`. * `p.radius`: the largest `r : ℝ≥0∞` such that `∥p n∥ * r^n` grows subexponentially. * `p.le_radius_of_bound`, `p.le_radius_of_bound_nnreal`, `p.le_radius_of_is_O`: if `∥p n∥ * r ^ n` is bounded above, then `r ≤ p.radius`; * `p.is_o_of_lt_radius`, `p.norm_mul_pow_le_mul_pow_of_lt_radius`, `p.is_o_one_of_lt_radius`, `p.norm_mul_pow_le_of_lt_radius`, `p.nnnorm_mul_pow_le_of_lt_radius`: if `r < p.radius`, then `∥p n∥ * r ^ n` tends to zero exponentially; * `p.lt_radius_of_is_O`: if `r ≠ 0` and `∥p n∥ * r ^ n = O(a ^ n)` for some `-1 < a < 1`, then `r < p.radius`; * `p.partial_sum n x`: the sum `∑_{i = 0}^{n-1} pᵢ xⁱ`. * `p.sum x`: the sum `∑'_{i = 0}^{∞} pᵢ xⁱ`. Additionally, let `f` be a function from `E` to `F`. * `has_fpower_series_on_ball f p x r`: on the ball of center `x` with radius `r`, `f (x + y) = ∑'_n pₙ yⁿ`. * `has_fpower_series_at f p x`: on some ball of center `x` with positive radius, holds `has_fpower_series_on_ball f p x r`. * `analytic_at 𝕜 f x`: there exists a power series `p` such that holds `has_fpower_series_at f p x`. * `analytic_on 𝕜 f s`: the function `f` is analytic at every point of `s`. We develop the basic properties of these notions, notably: * If a function admits a power series, it is continuous (see `has_fpower_series_on_ball.continuous_on` and `has_fpower_series_at.continuous_at` and `analytic_at.continuous_at`). * In a complete space, the sum of a formal power series with positive radius is well defined on the disk of convergence, see `formal_multilinear_series.has_fpower_series_on_ball`. * If a function admits a power series in a ball, then it is analytic at any point `y` of this ball, and the power series there can be expressed in terms of the initial power series `p` as `p.change_origin y`. See `has_fpower_series_on_ball.change_origin`. It follows in particular that the set of points at which a given function is analytic is open, see `is_open_analytic_at`. ## Implementation details We only introduce the radius of convergence of a power series, as `p.radius`. For a power series in finitely many dimensions, there is a finer (directional, coordinate-dependent) notion, describing the polydisk of convergence. This notion is more specific, and not necessary to build the general theory. We do not define it here. -/ noncomputable theory variables {𝕜 E F G : Type*} open_locale topological_space classical big_operators nnreal filter ennreal open set filter asymptotics namespace formal_multilinear_series variables [ring 𝕜] [add_comm_group E] [add_comm_group F] [module 𝕜 E] [module 𝕜 F] variables [topological_space E] [topological_space F] variables [topological_add_group E] [topological_add_group F] variables [has_continuous_const_smul 𝕜 E] [has_continuous_const_smul 𝕜 F] /-- Given a formal multilinear series `p` and a vector `x`, then `p.sum x` is the sum `Σ pₙ xⁿ`. A priori, it only behaves well when `∥x∥ < p.radius`. -/ protected def sum (p : formal_multilinear_series 𝕜 E F) (x : E) : F := ∑' n : ℕ , p n (λ i, x) /-- Given a formal multilinear series `p` and a vector `x`, then `p.partial_sum n x` is the sum `Σ pₖ xᵏ` for `k ∈ {0,..., n-1}`. -/ def partial_sum (p : formal_multilinear_series 𝕜 E F) (n : ℕ) (x : E) : F := ∑ k in finset.range n, p k (λ(i : fin k), x) /-- The partial sums of a formal multilinear series are continuous. -/ lemma partial_sum_continuous (p : formal_multilinear_series 𝕜 E F) (n : ℕ) : continuous (p.partial_sum n) := by continuity end formal_multilinear_series /-! ### The radius of a formal multilinear series -/ variables [nondiscrete_normed_field 𝕜] [normed_group E] [normed_space 𝕜 E] [normed_group F] [normed_space 𝕜 F] [normed_group G] [normed_space 𝕜 G] namespace formal_multilinear_series variables (p : formal_multilinear_series 𝕜 E F) {r : ℝ≥0} /-- The radius of a formal multilinear series is the largest `r` such that the sum `Σ ∥pₙ∥ ∥y∥ⁿ` converges for all `∥y∥ < r`. This implies that `Σ pₙ yⁿ` converges for all `∥y∥ < r`, but these definitions are *not* equivalent in general. -/ def radius (p : formal_multilinear_series 𝕜 E F) : ℝ≥0∞ := ⨆ (r : ℝ≥0) (C : ℝ) (hr : ∀ n, ∥p n∥ * r ^ n ≤ C), (r : ℝ≥0∞) /-- If `∥pₙ∥ rⁿ` is bounded in `n`, then the radius of `p` is at least `r`. -/ lemma le_radius_of_bound (C : ℝ) {r : ℝ≥0} (h : ∀ (n : ℕ), ∥p n∥ * r^n ≤ C) : (r : ℝ≥0∞) ≤ p.radius := le_supr_of_le r $ le_supr_of_le C $ (le_supr (λ _, (r : ℝ≥0∞)) h) /-- If `∥pₙ∥ rⁿ` is bounded in `n`, then the radius of `p` is at least `r`. -/ lemma le_radius_of_bound_nnreal (C : ℝ≥0) {r : ℝ≥0} (h : ∀ (n : ℕ), ∥p n∥₊ * r^n ≤ C) : (r : ℝ≥0∞) ≤ p.radius := p.le_radius_of_bound C $ λ n, by exact_mod_cast (h n) /-- If `∥pₙ∥ rⁿ = O(1)`, as `n → ∞`, then the radius of `p` is at least `r`. -/ lemma le_radius_of_is_O (h : is_O (λ n, ∥p n∥ * r^n) (λ n, (1 : ℝ)) at_top) : ↑r ≤ p.radius := exists.elim (is_O_one_nat_at_top_iff.1 h) $ λ C hC, p.le_radius_of_bound C $ λ n, (le_abs_self _).trans (hC n) lemma le_radius_of_eventually_le (C) (h : ∀ᶠ n in at_top, ∥p n∥ * r ^ n ≤ C) : ↑r ≤ p.radius := p.le_radius_of_is_O $ is_O.of_bound C $ h.mono $ λ n hn, by simpa lemma le_radius_of_summable_nnnorm (h : summable (λ n, ∥p n∥₊ * r ^ n)) : ↑r ≤ p.radius := p.le_radius_of_bound_nnreal (∑' n, ∥p n∥₊ * r ^ n) $ λ n, le_tsum' h _ lemma le_radius_of_summable (h : summable (λ n, ∥p n∥ * r ^ n)) : ↑r ≤ p.radius := p.le_radius_of_summable_nnnorm $ by { simp only [← coe_nnnorm] at h, exact_mod_cast h } lemma radius_eq_top_of_forall_nnreal_is_O (h : ∀ r : ℝ≥0, is_O (λ n, ∥p n∥ * r^n) (λ n, (1 : ℝ)) at_top) : p.radius = ∞ := ennreal.eq_top_of_forall_nnreal_le $ λ r, p.le_radius_of_is_O (h r) lemma radius_eq_top_of_eventually_eq_zero (h : ∀ᶠ n in at_top, p n = 0) : p.radius = ∞ := p.radius_eq_top_of_forall_nnreal_is_O $ λ r, (is_O_zero _ _).congr' (h.mono $ λ n hn, by simp [hn]) eventually_eq.rfl lemma radius_eq_top_of_forall_image_add_eq_zero (n : ℕ) (hn : ∀ m, p (m + n) = 0) : p.radius = ∞ := p.radius_eq_top_of_eventually_eq_zero $ mem_at_top_sets.2 ⟨n, λ k hk, tsub_add_cancel_of_le hk ▸ hn _⟩ /-- For `r` strictly smaller than the radius of `p`, then `∥pₙ∥ rⁿ` tends to zero exponentially: for some `0 < a < 1`, `∥p n∥ rⁿ = o(aⁿ)`. -/ lemma is_o_of_lt_radius (h : ↑r < p.radius) : ∃ a ∈ Ioo (0 : ℝ) 1, is_o (λ n, ∥p n∥ * r ^ n) (pow a) at_top := begin rw (tfae_exists_lt_is_o_pow (λ n, ∥p n∥ * r ^ n) 1).out 1 4, simp only [radius, lt_supr_iff] at h, rcases h with ⟨t, C, hC, rt⟩, rw [ennreal.coe_lt_coe, ← nnreal.coe_lt_coe] at rt, have : 0 < (t : ℝ), from r.coe_nonneg.trans_lt rt, rw [← div_lt_one this] at rt, refine ⟨_, rt, C, or.inr zero_lt_one, λ n, _⟩, calc |∥p n∥ * r ^ n| = (∥p n∥ * t ^ n) * (r / t) ^ n : by field_simp [mul_right_comm, abs_mul, this.ne'] ... ≤ C * (r / t) ^ n : mul_le_mul_of_nonneg_right (hC n) (pow_nonneg (div_nonneg r.2 t.2) _) end /-- For `r` strictly smaller than the radius of `p`, then `∥pₙ∥ rⁿ = o(1)`. -/ lemma is_o_one_of_lt_radius (h : ↑r < p.radius) : is_o (λ n, ∥p n∥ * r ^ n) (λ _, 1 : ℕ → ℝ) at_top := let ⟨a, ha, hp⟩ := p.is_o_of_lt_radius h in hp.trans $ (is_o_pow_pow_of_lt_left ha.1.le ha.2).congr (λ n, rfl) one_pow /-- For `r` strictly smaller than the radius of `p`, then `∥pₙ∥ rⁿ` tends to zero exponentially: for some `0 < a < 1` and `C > 0`, `∥p n∥ * r ^ n ≤ C * a ^ n`. -/ lemma norm_mul_pow_le_mul_pow_of_lt_radius (h : ↑r < p.radius) : ∃ (a ∈ Ioo (0 : ℝ) 1) (C > 0), ∀ n, ∥p n∥ * r^n ≤ C * a^n := begin rcases ((tfae_exists_lt_is_o_pow (λ n, ∥p n∥ * r ^ n) 1).out 1 5).mp (p.is_o_of_lt_radius h) with ⟨a, ha, C, hC, H⟩, exact ⟨a, ha, C, hC, λ n, (le_abs_self _).trans (H n)⟩ end /-- If `r ≠ 0` and `∥pₙ∥ rⁿ = O(aⁿ)` for some `-1 < a < 1`, then `r < p.radius`. -/ lemma lt_radius_of_is_O (h₀ : r ≠ 0) {a : ℝ} (ha : a ∈ Ioo (-1 : ℝ) 1) (hp : is_O (λ n, ∥p n∥ * r ^ n) (pow a) at_top) : ↑r < p.radius := begin rcases ((tfae_exists_lt_is_o_pow (λ n, ∥p n∥ * r ^ n) 1).out 2 5).mp ⟨a, ha, hp⟩ with ⟨a, ha, C, hC, hp⟩, rw [← pos_iff_ne_zero, ← nnreal.coe_pos] at h₀, lift a to ℝ≥0 using ha.1.le, have : (r : ℝ) < r / a := by simpa only [div_one] using (div_lt_div_left h₀ zero_lt_one ha.1).2 ha.2, norm_cast at this, rw [← ennreal.coe_lt_coe] at this, refine this.trans_le (p.le_radius_of_bound C $ λ n, _), rw [nnreal.coe_div, div_pow, ← mul_div_assoc, div_le_iff (pow_pos ha.1 n)], exact (le_abs_self _).trans (hp n) end /-- For `r` strictly smaller than the radius of `p`, then `∥pₙ∥ rⁿ` is bounded. -/ lemma norm_mul_pow_le_of_lt_radius (p : formal_multilinear_series 𝕜 E F) {r : ℝ≥0} (h : (r : ℝ≥0∞) < p.radius) : ∃ C > 0, ∀ n, ∥p n∥ * r^n ≤ C := let ⟨a, ha, C, hC, h⟩ := p.norm_mul_pow_le_mul_pow_of_lt_radius h in ⟨C, hC, λ n, (h n).trans $ mul_le_of_le_one_right hC.lt.le (pow_le_one _ ha.1.le ha.2.le)⟩ /-- For `r` strictly smaller than the radius of `p`, then `∥pₙ∥ rⁿ` is bounded. -/ lemma norm_le_div_pow_of_pos_of_lt_radius (p : formal_multilinear_series 𝕜 E F) {r : ℝ≥0} (h0 : 0 < r) (h : (r : ℝ≥0∞) < p.radius) : ∃ C > 0, ∀ n, ∥p n∥ ≤ C / r ^ n := let ⟨C, hC, hp⟩ := p.norm_mul_pow_le_of_lt_radius h in ⟨C, hC, λ n, iff.mpr (le_div_iff (pow_pos h0 _)) (hp n)⟩ /-- For `r` strictly smaller than the radius of `p`, then `∥pₙ∥ rⁿ` is bounded. -/ lemma nnnorm_mul_pow_le_of_lt_radius (p : formal_multilinear_series 𝕜 E F) {r : ℝ≥0} (h : (r : ℝ≥0∞) < p.radius) : ∃ C > 0, ∀ n, ∥p n∥₊ * r^n ≤ C := let ⟨C, hC, hp⟩ := p.norm_mul_pow_le_of_lt_radius h in ⟨⟨C, hC.lt.le⟩, hC, by exact_mod_cast hp⟩ lemma le_radius_of_tendsto (p : formal_multilinear_series 𝕜 E F) {l : ℝ} (h : tendsto (λ n, ∥p n∥ * r^n) at_top (𝓝 l)) : ↑r ≤ p.radius := p.le_radius_of_is_O (is_O_one_of_tendsto _ h) lemma le_radius_of_summable_norm (p : formal_multilinear_series 𝕜 E F) (hs : summable (λ n, ∥p n∥ * r^n)) : ↑r ≤ p.radius := p.le_radius_of_tendsto hs.tendsto_at_top_zero lemma not_summable_norm_of_radius_lt_nnnorm (p : formal_multilinear_series 𝕜 E F) {x : E} (h : p.radius < ∥x∥₊) : ¬ summable (λ n, ∥p n∥ * ∥x∥^n) := λ hs, not_le_of_lt h (p.le_radius_of_summable_norm hs) lemma summable_norm_mul_pow (p : formal_multilinear_series 𝕜 E F) {r : ℝ≥0} (h : ↑r < p.radius) : summable (λ n : ℕ, ∥p n∥ * r ^ n) := begin obtain ⟨a, ha : a ∈ Ioo (0 : ℝ) 1, C, hC : 0 < C, hp⟩ := p.norm_mul_pow_le_mul_pow_of_lt_radius h, exact summable_of_nonneg_of_le (λ n, mul_nonneg (norm_nonneg _) (pow_nonneg r.coe_nonneg _)) hp ((summable_geometric_of_lt_1 ha.1.le ha.2).mul_left _), end lemma summable_norm_apply (p : formal_multilinear_series 𝕜 E F) {x : E} (hx : x ∈ emetric.ball (0 : E) p.radius) : summable (λ n : ℕ, ∥p n (λ _, x)∥) := begin rw mem_emetric_ball_zero_iff at hx, refine summable_of_nonneg_of_le (λ _, norm_nonneg _) (λ n, ((p n).le_op_norm _).trans_eq _) (p.summable_norm_mul_pow hx), simp end lemma summable_nnnorm_mul_pow (p : formal_multilinear_series 𝕜 E F) {r : ℝ≥0} (h : ↑r < p.radius) : summable (λ n : ℕ, ∥p n∥₊ * r ^ n) := by { rw ← nnreal.summable_coe, push_cast, exact p.summable_norm_mul_pow h } protected lemma summable [complete_space F] (p : formal_multilinear_series 𝕜 E F) {x : E} (hx : x ∈ emetric.ball (0 : E) p.radius) : summable (λ n : ℕ, p n (λ _, x)) := summable_of_summable_norm (p.summable_norm_apply hx) lemma radius_eq_top_of_summable_norm (p : formal_multilinear_series 𝕜 E F) (hs : ∀ r : ℝ≥0, summable (λ n, ∥p n∥ * r^n)) : p.radius = ∞ := ennreal.eq_top_of_forall_nnreal_le (λ r, p.le_radius_of_summable_norm (hs r)) lemma radius_eq_top_iff_summable_norm (p : formal_multilinear_series 𝕜 E F) : p.radius = ∞ ↔ ∀ r : ℝ≥0, summable (λ n, ∥p n∥ * r^n) := begin split, { intros h r, obtain ⟨a, ha : a ∈ Ioo (0 : ℝ) 1, C, hC : 0 < C, hp⟩ := p.norm_mul_pow_le_mul_pow_of_lt_radius (show (r:ℝ≥0∞) < p.radius, from h.symm ▸ ennreal.coe_lt_top), refine (summable_of_norm_bounded (λ n, (C : ℝ) * a ^ n) ((summable_geometric_of_lt_1 ha.1.le ha.2).mul_left _) (λ n, _)), specialize hp n, rwa real.norm_of_nonneg (mul_nonneg (norm_nonneg _) (pow_nonneg r.coe_nonneg n)) }, { exact p.radius_eq_top_of_summable_norm } end /-- If the radius of `p` is positive, then `∥pₙ∥` grows at most geometrically. -/ lemma le_mul_pow_of_radius_pos (p : formal_multilinear_series 𝕜 E F) (h : 0 < p.radius) : ∃ C r (hC : 0 < C) (hr : 0 < r), ∀ n, ∥p n∥ ≤ C * r ^ n := begin rcases ennreal.lt_iff_exists_nnreal_btwn.1 h with ⟨r, r0, rlt⟩, have rpos : 0 < (r : ℝ), by simp [ennreal.coe_pos.1 r0], rcases norm_le_div_pow_of_pos_of_lt_radius p rpos rlt with ⟨C, Cpos, hCp⟩, refine ⟨C, r ⁻¹, Cpos, by simp [rpos], λ n, _⟩, convert hCp n, exact inv_pow₀ _ _, end /-- The radius of the sum of two formal series is at least the minimum of their two radii. -/ lemma min_radius_le_radius_add (p q : formal_multilinear_series 𝕜 E F) : min p.radius q.radius ≤ (p + q).radius := begin refine ennreal.le_of_forall_nnreal_lt (λ r hr, _), rw lt_min_iff at hr, have := ((p.is_o_one_of_lt_radius hr.1).add (q.is_o_one_of_lt_radius hr.2)).is_O, refine (p + q).le_radius_of_is_O ((is_O_of_le _ $ λ n, _).trans this), rw [← add_mul, norm_mul, norm_mul, norm_norm], exact mul_le_mul_of_nonneg_right ((norm_add_le _ _).trans (le_abs_self _)) (norm_nonneg _) end @[simp] lemma radius_neg (p : formal_multilinear_series 𝕜 E F) : (-p).radius = p.radius := by simp [radius] protected lemma has_sum [complete_space F] (p : formal_multilinear_series 𝕜 E F) {x : E} (hx : x ∈ emetric.ball (0 : E) p.radius) : has_sum (λ n : ℕ, p n (λ _, x)) (p.sum x) := (p.summable hx).has_sum lemma radius_le_radius_continuous_linear_map_comp (p : formal_multilinear_series 𝕜 E F) (f : F →L[𝕜] G) : p.radius ≤ (f.comp_formal_multilinear_series p).radius := begin refine ennreal.le_of_forall_nnreal_lt (λ r hr, _), apply le_radius_of_is_O, apply (is_O.trans_is_o _ (p.is_o_one_of_lt_radius hr)).is_O, refine is_O.mul (@is_O_with.is_O _ _ _ _ _ (∥f∥) _ _ _ _) (is_O_refl _ _), apply is_O_with.of_bound (eventually_of_forall (λ n, _)), simpa only [norm_norm] using f.norm_comp_continuous_multilinear_map_le (p n) end end formal_multilinear_series /-! ### Expanding a function as a power series -/ section variables {f g : E → F} {p pf pg : formal_multilinear_series 𝕜 E F} {x : E} {r r' : ℝ≥0∞} /-- Given a function `f : E → F` and a formal multilinear series `p`, we say that `f` has `p` as a power series on the ball of radius `r > 0` around `x` if `f (x + y) = ∑' pₙ yⁿ` for all `∥y∥ < r`. -/ structure has_fpower_series_on_ball (f : E → F) (p : formal_multilinear_series 𝕜 E F) (x : E) (r : ℝ≥0∞) : Prop := (r_le : r ≤ p.radius) (r_pos : 0 < r) (has_sum : ∀ {y}, y ∈ emetric.ball (0 : E) r → has_sum (λn:ℕ, p n (λ(i : fin n), y)) (f (x + y))) /-- Given a function `f : E → F` and a formal multilinear series `p`, we say that `f` has `p` as a power series around `x` if `f (x + y) = ∑' pₙ yⁿ` for all `y` in a neighborhood of `0`. -/ def has_fpower_series_at (f : E → F) (p : formal_multilinear_series 𝕜 E F) (x : E) := ∃ r, has_fpower_series_on_ball f p x r variable (𝕜) /-- Given a function `f : E → F`, we say that `f` is analytic at `x` if it admits a convergent power series expansion around `x`. -/ def analytic_at (f : E → F) (x : E) := ∃ (p : formal_multilinear_series 𝕜 E F), has_fpower_series_at f p x /-- Given a function `f : E → F`, we say that `f` is analytic on a set `s` if it is analytic around every point of `s`. -/ def analytic_on (f : E → F) (s : set E) := ∀ x, x ∈ s → analytic_at 𝕜 f x variable {𝕜} lemma has_fpower_series_on_ball.has_fpower_series_at (hf : has_fpower_series_on_ball f p x r) : has_fpower_series_at f p x := ⟨r, hf⟩ lemma has_fpower_series_at.analytic_at (hf : has_fpower_series_at f p x) : analytic_at 𝕜 f x := ⟨p, hf⟩ lemma has_fpower_series_on_ball.analytic_at (hf : has_fpower_series_on_ball f p x r) : analytic_at 𝕜 f x := hf.has_fpower_series_at.analytic_at lemma has_fpower_series_on_ball.congr (hf : has_fpower_series_on_ball f p x r) (hg : eq_on f g (emetric.ball x r)) : has_fpower_series_on_ball g p x r := { r_le := hf.r_le, r_pos := hf.r_pos, has_sum := λ y hy, begin convert hf.has_sum hy, apply hg.symm, simpa [edist_eq_coe_nnnorm_sub] using hy, end } /-- If a function `f` has a power series `p` around `x`, then the function `z ↦ f (z - y)` has the same power series around `x + y`. -/ lemma has_fpower_series_on_ball.comp_sub (hf : has_fpower_series_on_ball f p x r) (y : E) : has_fpower_series_on_ball (λ z, f (z - y)) p (x + y) r := { r_le := hf.r_le, r_pos := hf.r_pos, has_sum := λ z hz, by { convert hf.has_sum hz, abel } } lemma has_fpower_series_on_ball.has_sum_sub (hf : has_fpower_series_on_ball f p x r) {y : E} (hy : y ∈ emetric.ball x r) : has_sum (λ n : ℕ, p n (λ i, y - x)) (f y) := have y - x ∈ emetric.ball (0 : E) r, by simpa [edist_eq_coe_nnnorm_sub] using hy, by simpa only [add_sub_cancel'_right] using hf.has_sum this lemma has_fpower_series_on_ball.radius_pos (hf : has_fpower_series_on_ball f p x r) : 0 < p.radius := lt_of_lt_of_le hf.r_pos hf.r_le lemma has_fpower_series_at.radius_pos (hf : has_fpower_series_at f p x) : 0 < p.radius := let ⟨r, hr⟩ := hf in hr.radius_pos lemma has_fpower_series_on_ball.mono (hf : has_fpower_series_on_ball f p x r) (r'_pos : 0 < r') (hr : r' ≤ r) : has_fpower_series_on_ball f p x r' := ⟨le_trans hr hf.1, r'_pos, λ y hy, hf.has_sum (emetric.ball_subset_ball hr hy)⟩ protected lemma has_fpower_series_at.eventually (hf : has_fpower_series_at f p x) : ∀ᶠ r : ℝ≥0∞ in 𝓝[>] 0, has_fpower_series_on_ball f p x r := let ⟨r, hr⟩ := hf in mem_of_superset (Ioo_mem_nhds_within_Ioi (left_mem_Ico.2 hr.r_pos)) $ λ r' hr', hr.mono hr'.1 hr'.2.le lemma has_fpower_series_on_ball.add (hf : has_fpower_series_on_ball f pf x r) (hg : has_fpower_series_on_ball g pg x r) : has_fpower_series_on_ball (f + g) (pf + pg) x r := { r_le := le_trans (le_min_iff.2 ⟨hf.r_le, hg.r_le⟩) (pf.min_radius_le_radius_add pg), r_pos := hf.r_pos, has_sum := λ y hy, (hf.has_sum hy).add (hg.has_sum hy) } lemma has_fpower_series_at.add (hf : has_fpower_series_at f pf x) (hg : has_fpower_series_at g pg x) : has_fpower_series_at (f + g) (pf + pg) x := begin rcases (hf.eventually.and hg.eventually).exists with ⟨r, hr⟩, exact ⟨r, hr.1.add hr.2⟩ end lemma analytic_at.add (hf : analytic_at 𝕜 f x) (hg : analytic_at 𝕜 g x) : analytic_at 𝕜 (f + g) x := let ⟨pf, hpf⟩ := hf, ⟨qf, hqf⟩ := hg in (hpf.add hqf).analytic_at lemma has_fpower_series_on_ball.neg (hf : has_fpower_series_on_ball f pf x r) : has_fpower_series_on_ball (-f) (-pf) x r := { r_le := by { rw pf.radius_neg, exact hf.r_le }, r_pos := hf.r_pos, has_sum := λ y hy, (hf.has_sum hy).neg } lemma has_fpower_series_at.neg (hf : has_fpower_series_at f pf x) : has_fpower_series_at (-f) (-pf) x := let ⟨rf, hrf⟩ := hf in hrf.neg.has_fpower_series_at lemma analytic_at.neg (hf : analytic_at 𝕜 f x) : analytic_at 𝕜 (-f) x := let ⟨pf, hpf⟩ := hf in hpf.neg.analytic_at lemma has_fpower_series_on_ball.sub (hf : has_fpower_series_on_ball f pf x r) (hg : has_fpower_series_on_ball g pg x r) : has_fpower_series_on_ball (f - g) (pf - pg) x r := by simpa only [sub_eq_add_neg] using hf.add hg.neg lemma has_fpower_series_at.sub (hf : has_fpower_series_at f pf x) (hg : has_fpower_series_at g pg x) : has_fpower_series_at (f - g) (pf - pg) x := by simpa only [sub_eq_add_neg] using hf.add hg.neg lemma analytic_at.sub (hf : analytic_at 𝕜 f x) (hg : analytic_at 𝕜 g x) : analytic_at 𝕜 (f - g) x := by simpa only [sub_eq_add_neg] using hf.add hg.neg lemma has_fpower_series_on_ball.coeff_zero (hf : has_fpower_series_on_ball f pf x r) (v : fin 0 → E) : pf 0 v = f x := begin have v_eq : v = (λ i, 0) := subsingleton.elim _ _, have zero_mem : (0 : E) ∈ emetric.ball (0 : E) r, by simp [hf.r_pos], have : ∀ i ≠ 0, pf i (λ j, 0) = 0, { assume i hi, have : 0 < i := pos_iff_ne_zero.2 hi, exact continuous_multilinear_map.map_coord_zero _ (⟨0, this⟩ : fin i) rfl }, have A := (hf.has_sum zero_mem).unique (has_sum_single _ this), simpa [v_eq] using A.symm, end lemma has_fpower_series_at.coeff_zero (hf : has_fpower_series_at f pf x) (v : fin 0 → E) : pf 0 v = f x := let ⟨rf, hrf⟩ := hf in hrf.coeff_zero v /-- If a function `f` has a power series `p` on a ball and `g` is linear, then `g ∘ f` has the power series `g ∘ p` on the same ball. -/ lemma _root_.continuous_linear_map.comp_has_fpower_series_on_ball (g : F →L[𝕜] G) (h : has_fpower_series_on_ball f p x r) : has_fpower_series_on_ball (g ∘ f) (g.comp_formal_multilinear_series p) x r := { r_le := h.r_le.trans (p.radius_le_radius_continuous_linear_map_comp _), r_pos := h.r_pos, has_sum := λ y hy, by simpa only [continuous_linear_map.comp_formal_multilinear_series_apply, continuous_linear_map.comp_continuous_multilinear_map_coe, function.comp_app] using g.has_sum (h.has_sum hy) } /-- If a function `f` is analytic on a set `s` and `g` is linear, then `g ∘ f` is analytic on `s`. -/ lemma _root_.continuous_linear_map.comp_analytic_on {s : set E} (g : F →L[𝕜] G) (h : analytic_on 𝕜 f s) : analytic_on 𝕜 (g ∘ f) s := begin rintros x hx, rcases h x hx with ⟨p, r, hp⟩, exact ⟨g.comp_formal_multilinear_series p, r, g.comp_has_fpower_series_on_ball hp⟩, end /-- If a function admits a power series expansion, then it is exponentially close to the partial sums of this power series on strict subdisks of the disk of convergence. This version provides an upper estimate that decreases both in `∥y∥` and `n`. See also `has_fpower_series_on_ball.uniform_geometric_approx` for a weaker version. -/ lemma has_fpower_series_on_ball.uniform_geometric_approx' {r' : ℝ≥0} (hf : has_fpower_series_on_ball f p x r) (h : (r' : ℝ≥0∞) < r) : ∃ (a ∈ Ioo (0 : ℝ) 1) (C > 0), (∀ y ∈ metric.ball (0 : E) r', ∀ n, ∥f (x + y) - p.partial_sum n y∥ ≤ C * (a * (∥y∥ / r')) ^ n) := begin obtain ⟨a, ha, C, hC, hp⟩ : ∃ (a ∈ Ioo (0 : ℝ) 1) (C > 0), ∀ n, ∥p n∥ * r' ^n ≤ C * a^n := p.norm_mul_pow_le_mul_pow_of_lt_radius (h.trans_le hf.r_le), refine ⟨a, ha, C / (1 - a), div_pos hC (sub_pos.2 ha.2), λ y hy n, _⟩, have yr' : ∥y∥ < r', by { rw ball_zero_eq at hy, exact hy }, have hr'0 : 0 < (r' : ℝ), from (norm_nonneg _).trans_lt yr', have : y ∈ emetric.ball (0 : E) r, { refine mem_emetric_ball_zero_iff.2 (lt_trans _ h), exact_mod_cast yr' }, rw [norm_sub_rev, ← mul_div_right_comm], have ya : a * (∥y∥ / ↑r') ≤ a, from mul_le_of_le_one_right ha.1.le (div_le_one_of_le yr'.le r'.coe_nonneg), suffices : ∥p.partial_sum n y - f (x + y)∥ ≤ C * (a * (∥y∥ / r')) ^ n / (1 - a * (∥y∥ / r')), { refine this.trans _, apply_rules [div_le_div_of_le_left, sub_pos.2, div_nonneg, mul_nonneg, pow_nonneg, hC.lt.le, ha.1.le, norm_nonneg, nnreal.coe_nonneg, ha.2, (sub_le_sub_iff_left _).2]; apply_instance }, apply norm_sub_le_of_geometric_bound_of_has_sum (ya.trans_lt ha.2) _ (hf.has_sum this), assume n, calc ∥(p n) (λ (i : fin n), y)∥ ≤ ∥p n∥ * (∏ i : fin n, ∥y∥) : continuous_multilinear_map.le_op_norm _ _ ... = (∥p n∥ * r' ^ n) * (∥y∥ / r') ^ n : by field_simp [hr'0.ne', mul_right_comm] ... ≤ (C * a ^ n) * (∥y∥ / r') ^ n : mul_le_mul_of_nonneg_right (hp n) (pow_nonneg (div_nonneg (norm_nonneg _) r'.coe_nonneg) _) ... ≤ C * (a * (∥y∥ / r')) ^ n : by rw [mul_pow, mul_assoc] end /-- If a function admits a power series expansion, then it is exponentially close to the partial sums of this power series on strict subdisks of the disk of convergence. -/ lemma has_fpower_series_on_ball.uniform_geometric_approx {r' : ℝ≥0} (hf : has_fpower_series_on_ball f p x r) (h : (r' : ℝ≥0∞) < r) : ∃ (a ∈ Ioo (0 : ℝ) 1) (C > 0), (∀ y ∈ metric.ball (0 : E) r', ∀ n, ∥f (x + y) - p.partial_sum n y∥ ≤ C * a ^ n) := begin obtain ⟨a, ha, C, hC, hp⟩ : ∃ (a ∈ Ioo (0 : ℝ) 1) (C > 0), (∀ y ∈ metric.ball (0 : E) r', ∀ n, ∥f (x + y) - p.partial_sum n y∥ ≤ C * (a * (∥y∥ / r')) ^ n), from hf.uniform_geometric_approx' h, refine ⟨a, ha, C, hC, λ y hy n, (hp y hy n).trans _⟩, have yr' : ∥y∥ < r', by rwa ball_zero_eq at hy, refine mul_le_mul_of_nonneg_left (pow_le_pow_of_le_left _ _ _) hC.lt.le, exacts [mul_nonneg ha.1.le (div_nonneg (norm_nonneg y) r'.coe_nonneg), mul_le_of_le_one_right ha.1.le (div_le_one_of_le yr'.le r'.coe_nonneg)] end /-- Taylor formula for an analytic function, `is_O` version. -/ lemma has_fpower_series_at.is_O_sub_partial_sum_pow (hf : has_fpower_series_at f p x) (n : ℕ) : is_O (λ y : E, f (x + y) - p.partial_sum n y) (λ y, ∥y∥ ^ n) (𝓝 0) := begin rcases hf with ⟨r, hf⟩, rcases ennreal.lt_iff_exists_nnreal_btwn.1 hf.r_pos with ⟨r', r'0, h⟩, obtain ⟨a, ha, C, hC, hp⟩ : ∃ (a ∈ Ioo (0 : ℝ) 1) (C > 0), (∀ y ∈ metric.ball (0 : E) r', ∀ n, ∥f (x + y) - p.partial_sum n y∥ ≤ C * (a * (∥y∥ / r')) ^ n), from hf.uniform_geometric_approx' h, refine is_O_iff.2 ⟨C * (a / r') ^ n, _⟩, replace r'0 : 0 < (r' : ℝ), by exact_mod_cast r'0, filter_upwards [metric.ball_mem_nhds (0 : E) r'0] with y hy, simpa [mul_pow, mul_div_assoc, mul_assoc, div_mul_eq_mul_div] using hp y hy n, end /-- If `f` has formal power series `∑ n, pₙ` on a ball of radius `r`, then for `y, z` in any smaller ball, the norm of the difference `f y - f z - p 1 (λ _, y - z)` is bounded above by `C * (max ∥y - x∥ ∥z - x∥) * ∥y - z∥`. This lemma formulates this property using `is_O` and `filter.principal` on `E × E`. -/ lemma has_fpower_series_on_ball.is_O_image_sub_image_sub_deriv_principal (hf : has_fpower_series_on_ball f p x r) (hr : r' < r) : is_O (λ y : E × E, f y.1 - f y.2 - (p 1 (λ _, y.1 - y.2))) (λ y, ∥y - (x, x)∥ * ∥y.1 - y.2∥) (𝓟 $ emetric.ball (x, x) r') := begin lift r' to ℝ≥0 using ne_top_of_lt hr, rcases (zero_le r').eq_or_lt with rfl|hr'0, { simp only [is_O_bot, emetric.ball_zero, principal_empty, ennreal.coe_zero] }, obtain ⟨a, ha, C, hC : 0 < C, hp⟩ : ∃ (a ∈ Ioo (0 : ℝ) 1) (C > 0), ∀ (n : ℕ), ∥p n∥ * ↑r' ^ n ≤ C * a ^ n, from p.norm_mul_pow_le_mul_pow_of_lt_radius (hr.trans_le hf.r_le), simp only [← le_div_iff (pow_pos (nnreal.coe_pos.2 hr'0) _)] at hp, set L : E × E → ℝ := λ y, (C * (a / r') ^ 2) * (∥y - (x, x)∥ * ∥y.1 - y.2∥) * (a / (1 - a) ^ 2 + 2 / (1 - a)), have hL : ∀ y ∈ emetric.ball (x, x) r', ∥f y.1 - f y.2 - (p 1 (λ _, y.1 - y.2))∥ ≤ L y, { intros y hy', have hy : y ∈ emetric.ball x r ×ˢ emetric.ball x r, { rw [emetric.ball_prod_same], exact emetric.ball_subset_ball hr.le hy' }, set A : ℕ → F := λ n, p n (λ _, y.1 - x) - p n (λ _, y.2 - x), have hA : has_sum (λ n, A (n + 2)) (f y.1 - f y.2 - (p 1 (λ _, y.1 - y.2))), { convert (has_sum_nat_add_iff' 2).2 ((hf.has_sum_sub hy.1).sub (hf.has_sum_sub hy.2)) using 1, rw [finset.sum_range_succ, finset.sum_range_one, hf.coeff_zero, hf.coeff_zero, sub_self, zero_add, ← subsingleton.pi_single_eq (0 : fin 1) (y.1 - x), pi.single, ← subsingleton.pi_single_eq (0 : fin 1) (y.2 - x), pi.single, ← (p 1).map_sub, ← pi.single, subsingleton.pi_single_eq, sub_sub_sub_cancel_right] }, rw [emetric.mem_ball, edist_eq_coe_nnnorm_sub, ennreal.coe_lt_coe] at hy', set B : ℕ → ℝ := λ n, (C * (a / r') ^ 2) * (∥y - (x, x)∥ * ∥y.1 - y.2∥) * ((n + 2) * a ^ n), have hAB : ∀ n, ∥A (n + 2)∥ ≤ B n := λ n, calc ∥A (n + 2)∥ ≤ ∥p (n + 2)∥ * ↑(n + 2) * ∥y - (x, x)∥ ^ (n + 1) * ∥y.1 - y.2∥ : by simpa only [fintype.card_fin, pi_norm_const (_ : E), prod.norm_def, pi.sub_def, prod.fst_sub, prod.snd_sub, sub_sub_sub_cancel_right] using (p $ n + 2).norm_image_sub_le (λ _, y.1 - x) (λ _, y.2 - x) ... = ∥p (n + 2)∥ * ∥y - (x, x)∥ ^ n * (↑(n + 2) * ∥y - (x, x)∥ * ∥y.1 - y.2∥) : by { rw [pow_succ ∥y - (x, x)∥], ring } ... ≤ (C * a ^ (n + 2) / r' ^ (n + 2)) * r' ^ n * (↑(n + 2) * ∥y - (x, x)∥ * ∥y.1 - y.2∥) : by apply_rules [mul_le_mul_of_nonneg_right, mul_le_mul, hp, pow_le_pow_of_le_left, hy'.le, norm_nonneg, pow_nonneg, div_nonneg, mul_nonneg, nat.cast_nonneg, hC.le, r'.coe_nonneg, ha.1.le] ... = B n : by { field_simp [B, pow_succ, hr'0.ne'], simp only [mul_assoc, mul_comm, mul_left_comm] }, have hBL : has_sum B (L y), { apply has_sum.mul_left, simp only [add_mul], have : ∥a∥ < 1, by simp only [real.norm_eq_abs, abs_of_pos ha.1, ha.2], convert (has_sum_coe_mul_geometric_of_norm_lt_1 this).add ((has_sum_geometric_of_norm_lt_1 this).mul_left 2) }, exact hA.norm_le_of_bounded hBL hAB }, suffices : is_O L (λ y, ∥y - (x, x)∥ * ∥y.1 - y.2∥) (𝓟 (emetric.ball (x, x) r')), { refine (is_O.of_bound 1 (eventually_principal.2 $ λ y hy, _)).trans this, rw one_mul, exact (hL y hy).trans (le_abs_self _) }, simp_rw [L, mul_right_comm _ (_ * _)], exact (is_O_refl _ _).const_mul_left _, end /-- If `f` has formal power series `∑ n, pₙ` on a ball of radius `r`, then for `y, z` in any smaller ball, the norm of the difference `f y - f z - p 1 (λ _, y - z)` is bounded above by `C * (max ∥y - x∥ ∥z - x∥) * ∥y - z∥`. -/ lemma has_fpower_series_on_ball.image_sub_sub_deriv_le (hf : has_fpower_series_on_ball f p x r) (hr : r' < r) : ∃ C, ∀ (y z ∈ emetric.ball x r'), ∥f y - f z - (p 1 (λ _, y - z))∥ ≤ C * (max ∥y - x∥ ∥z - x∥) * ∥y - z∥ := by simpa only [is_O_principal, mul_assoc, norm_mul, norm_norm, prod.forall, emetric.mem_ball, prod.edist_eq, max_lt_iff, and_imp, @forall_swap (_ < _) E] using hf.is_O_image_sub_image_sub_deriv_principal hr /-- If `f` has formal power series `∑ n, pₙ` at `x`, then `f y - f z - p 1 (λ _, y - z) = O(∥(y, z) - (x, x)∥ * ∥y - z∥)` as `(y, z) → (x, x)`. In particular, `f` is strictly differentiable at `x`. -/ lemma has_fpower_series_at.is_O_image_sub_norm_mul_norm_sub (hf : has_fpower_series_at f p x) : is_O (λ y : E × E, f y.1 - f y.2 - (p 1 (λ _, y.1 - y.2))) (λ y, ∥y - (x, x)∥ * ∥y.1 - y.2∥) (𝓝 (x, x)) := begin rcases hf with ⟨r, hf⟩, rcases ennreal.lt_iff_exists_nnreal_btwn.1 hf.r_pos with ⟨r', r'0, h⟩, refine (hf.is_O_image_sub_image_sub_deriv_principal h).mono _, exact le_principal_iff.2 (emetric.ball_mem_nhds _ r'0) end /-- If a function admits a power series expansion at `x`, then it is the uniform limit of the partial sums of this power series on strict subdisks of the disk of convergence, i.e., `f (x + y)` is the uniform limit of `p.partial_sum n y` there. -/ lemma has_fpower_series_on_ball.tendsto_uniformly_on {r' : ℝ≥0} (hf : has_fpower_series_on_ball f p x r) (h : (r' : ℝ≥0∞) < r) : tendsto_uniformly_on (λ n y, p.partial_sum n y) (λ y, f (x + y)) at_top (metric.ball (0 : E) r') := begin obtain ⟨a, ha, C, hC, hp⟩ : ∃ (a ∈ Ioo (0 : ℝ) 1) (C > 0), (∀ y ∈ metric.ball (0 : E) r', ∀ n, ∥f (x + y) - p.partial_sum n y∥ ≤ C * a ^ n), from hf.uniform_geometric_approx h, refine metric.tendsto_uniformly_on_iff.2 (λ ε εpos, _), have L : tendsto (λ n, (C : ℝ) * a^n) at_top (𝓝 ((C : ℝ) * 0)) := tendsto_const_nhds.mul (tendsto_pow_at_top_nhds_0_of_lt_1 ha.1.le ha.2), rw mul_zero at L, refine (L.eventually (gt_mem_nhds εpos)).mono (λ n hn y hy, _), rw dist_eq_norm, exact (hp y hy n).trans_lt hn end /-- If a function admits a power series expansion at `x`, then it is the locally uniform limit of the partial sums of this power series on the disk of convergence, i.e., `f (x + y)` is the locally uniform limit of `p.partial_sum n y` there. -/ lemma has_fpower_series_on_ball.tendsto_locally_uniformly_on (hf : has_fpower_series_on_ball f p x r) : tendsto_locally_uniformly_on (λ n y, p.partial_sum n y) (λ y, f (x + y)) at_top (emetric.ball (0 : E) r) := begin assume u hu x hx, rcases ennreal.lt_iff_exists_nnreal_btwn.1 hx with ⟨r', xr', hr'⟩, have : emetric.ball (0 : E) r' ∈ 𝓝 x := is_open.mem_nhds emetric.is_open_ball xr', refine ⟨emetric.ball (0 : E) r', mem_nhds_within_of_mem_nhds this, _⟩, simpa [metric.emetric_ball_nnreal] using hf.tendsto_uniformly_on hr' u hu end /-- If a function admits a power series expansion at `x`, then it is the uniform limit of the partial sums of this power series on strict subdisks of the disk of convergence, i.e., `f y` is the uniform limit of `p.partial_sum n (y - x)` there. -/ lemma has_fpower_series_on_ball.tendsto_uniformly_on' {r' : ℝ≥0} (hf : has_fpower_series_on_ball f p x r) (h : (r' : ℝ≥0∞) < r) : tendsto_uniformly_on (λ n y, p.partial_sum n (y - x)) f at_top (metric.ball (x : E) r') := begin convert (hf.tendsto_uniformly_on h).comp (λ y, y - x), { simp [(∘)] }, { ext z, simp [dist_eq_norm] } end /-- If a function admits a power series expansion at `x`, then it is the locally uniform limit of the partial sums of this power series on the disk of convergence, i.e., `f y` is the locally uniform limit of `p.partial_sum n (y - x)` there. -/ lemma has_fpower_series_on_ball.tendsto_locally_uniformly_on' (hf : has_fpower_series_on_ball f p x r) : tendsto_locally_uniformly_on (λ n y, p.partial_sum n (y - x)) f at_top (emetric.ball (x : E) r) := begin have A : continuous_on (λ (y : E), y - x) (emetric.ball (x : E) r) := (continuous_id.sub continuous_const).continuous_on, convert (hf.tendsto_locally_uniformly_on).comp (λ (y : E), y - x) _ A, { ext z, simp }, { assume z, simp [edist_eq_coe_nnnorm, edist_eq_coe_nnnorm_sub] } end /-- If a function admits a power series expansion on a disk, then it is continuous there. -/ protected lemma has_fpower_series_on_ball.continuous_on (hf : has_fpower_series_on_ball f p x r) : continuous_on f (emetric.ball x r) := hf.tendsto_locally_uniformly_on'.continuous_on $ eventually_of_forall $ λ n, ((p.partial_sum_continuous n).comp (continuous_id.sub continuous_const)).continuous_on protected lemma has_fpower_series_at.continuous_at (hf : has_fpower_series_at f p x) : continuous_at f x := let ⟨r, hr⟩ := hf in hr.continuous_on.continuous_at (emetric.ball_mem_nhds x (hr.r_pos)) protected lemma analytic_at.continuous_at (hf : analytic_at 𝕜 f x) : continuous_at f x := let ⟨p, hp⟩ := hf in hp.continuous_at protected lemma analytic_on.continuous_on {s : set E} (hf : analytic_on 𝕜 f s) : continuous_on f s := λ x hx, (hf x hx).continuous_at.continuous_within_at /-- In a complete space, the sum of a converging power series `p` admits `p` as a power series. This is not totally obvious as we need to check the convergence of the series. -/ protected lemma formal_multilinear_series.has_fpower_series_on_ball [complete_space F] (p : formal_multilinear_series 𝕜 E F) (h : 0 < p.radius) : has_fpower_series_on_ball p.sum p 0 p.radius := { r_le := le_rfl, r_pos := h, has_sum := λ y hy, by { rw zero_add, exact p.has_sum hy } } lemma has_fpower_series_on_ball.sum (h : has_fpower_series_on_ball f p x r) {y : E} (hy : y ∈ emetric.ball (0 : E) r) : f (x + y) = p.sum y := (h.has_sum hy).tsum_eq.symm /-- The sum of a converging power series is continuous in its disk of convergence. -/ protected lemma formal_multilinear_series.continuous_on [complete_space F] : continuous_on p.sum (emetric.ball 0 p.radius) := begin cases (zero_le p.radius).eq_or_lt with h h, { simp [← h, continuous_on_empty] }, { exact (p.has_fpower_series_on_ball h).continuous_on } end end /-! ### Uniqueness of power series If a function `f : E → F` has two representations as power series at a point `x : E`, corresponding to formal multilinear series `p₁` and `p₂`, then these representations agree term-by-term. That is, for any `n : ℕ` and `y : E`, `p₁ n (λ i, y) = p₂ n (λ i, y)`. In the one-dimensional case, when `f : 𝕜 → E`, the continuous multilinear maps `p₁ n` and `p₂ n` are given by `formal_multilinear_series.mk_pi_field`, and hence are determined completely by the value of `p₁ n (λ i, 1)`, so `p₁ = p₂`. Consequently, the radius of convergence for one series can be transferred to the other. -/ section uniqueness open continuous_multilinear_map lemma asymptotics.is_O.continuous_multilinear_map_apply_eq_zero {n : ℕ} {p : E [×n]→L[𝕜] F} (h : is_O (λ y, p (λ i, y)) (λ y, ∥y∥ ^ (n + 1)) (𝓝 0)) (y : E) : p (λ i, y) = 0 := begin obtain ⟨c, c_pos, hc⟩ := h.exists_pos, obtain ⟨t, ht, t_open, z_mem⟩ := eventually_nhds_iff.mp (is_O_with_iff.mp hc), obtain ⟨δ, δ_pos, δε⟩ := (metric.is_open_iff.mp t_open) 0 z_mem, clear h hc z_mem, cases n, { exact norm_eq_zero.mp (by simpa only [fin0_apply_norm, norm_eq_zero, norm_zero, zero_pow', ne.def, nat.one_ne_zero, not_false_iff, mul_zero, norm_le_zero_iff] using ht 0 (δε (metric.mem_ball_self δ_pos))), }, { refine or.elim (em (y = 0)) (λ hy, by simpa only [hy] using p.map_zero) (λ hy, _), replace hy := norm_pos_iff.mpr hy, refine norm_eq_zero.mp (le_antisymm (le_of_forall_pos_le_add (λ ε ε_pos, _)) (norm_nonneg _)), have h₀ := mul_pos c_pos (pow_pos hy (n.succ + 1)), obtain ⟨k, k_pos, k_norm⟩ := normed_field.exists_norm_lt 𝕜 (lt_min (mul_pos δ_pos (inv_pos.mpr hy)) (mul_pos ε_pos (inv_pos.mpr h₀))), have h₁ : ∥k • y∥ < δ, { rw norm_smul, exact inv_mul_cancel_right₀ hy.ne.symm δ ▸ mul_lt_mul_of_pos_right (lt_of_lt_of_le k_norm (min_le_left _ _)) hy }, have h₂ := calc ∥p (λ i, k • y)∥ ≤ c * ∥k • y∥ ^ (n.succ + 1) : by simpa only [norm_pow, norm_norm] using ht (k • y) (δε (mem_ball_zero_iff.mpr h₁)) ... = ∥k∥ ^ n.succ * (∥k∥ * (c * ∥y∥ ^ (n.succ + 1))) : by { simp only [norm_smul, mul_pow], rw pow_succ, ring }, have h₃ : ∥k∥ * (c * ∥y∥ ^ (n.succ + 1)) < ε, from inv_mul_cancel_right₀ h₀.ne.symm ε ▸ mul_lt_mul_of_pos_right (lt_of_lt_of_le k_norm (min_le_right _ _)) h₀, calc ∥p (λ i, y)∥ = ∥(k⁻¹) ^ n.succ∥ * ∥p (λ i, k • y)∥ : by simpa only [inv_smul_smul₀ (norm_pos_iff.mp k_pos), norm_smul, finset.prod_const, finset.card_fin] using congr_arg norm (p.map_smul_univ (λ (i : fin n.succ), k⁻¹) (λ (i : fin n.succ), k • y)) ... ≤ ∥(k⁻¹) ^ n.succ∥ * (∥k∥ ^ n.succ * (∥k∥ * (c * ∥y∥ ^ (n.succ + 1)))) : mul_le_mul_of_nonneg_left h₂ (norm_nonneg _) ... = ∥(k⁻¹ * k) ^ n.succ∥ * (∥k∥ * (c * ∥y∥ ^ (n.succ + 1))) : by { rw ←mul_assoc, simp [norm_mul, mul_pow] } ... ≤ 0 + ε : by { rw inv_mul_cancel (norm_pos_iff.mp k_pos), simpa using h₃.le }, }, end /-- If a formal multilinear series `p` represents the zero function at `x : E`, then the terms `p n (λ i, y)` appearing the in sum are zero for any `n : ℕ`, `y : E`. -/ lemma has_fpower_series_at.apply_eq_zero {p : formal_multilinear_series 𝕜 E F} {x : E} (h : has_fpower_series_at 0 p x) (n : ℕ) : ∀ y : E, p n (λ i, y) = 0 := begin refine nat.strong_rec_on n (λ k hk, _), have psum_eq : p.partial_sum (k + 1) = (λ y, p k (λ i, y)), { funext z, refine finset.sum_eq_single _ (λ b hb hnb, _) (λ hn, _), { have := finset.mem_range_succ_iff.mp hb, simp only [hk b (this.lt_of_ne hnb), pi.zero_apply, zero_apply] }, { exact false.elim (hn (finset.mem_range.mpr (lt_add_one k))) } }, replace h := h.is_O_sub_partial_sum_pow k.succ, simp only [psum_eq, zero_sub, pi.zero_apply, asymptotics.is_O_neg_left] at h, exact h.continuous_multilinear_map_apply_eq_zero, end /-- A one-dimensional formal multilinear series representing the zero function is zero. -/ lemma has_fpower_series_at.eq_zero {p : formal_multilinear_series 𝕜 𝕜 E} {x : 𝕜} (h : has_fpower_series_at 0 p x) : p = 0 := by { ext n x, rw ←mk_pi_field_apply_one_eq_self (p n), simp [h.apply_eq_zero n 1] } /-- One-dimensional formal multilinear series representing the same function are equal. -/ theorem has_fpower_series_at.eq_formal_multilinear_series {p₁ p₂ : formal_multilinear_series 𝕜 𝕜 E} {f : 𝕜 → E} {x : 𝕜} (h₁ : has_fpower_series_at f p₁ x) (h₂ : has_fpower_series_at f p₂ x) : p₁ = p₂ := sub_eq_zero.mp (has_fpower_series_at.eq_zero (by simpa only [sub_self] using h₁.sub h₂)) /-- If a function `f : 𝕜 → E` has two power series representations at `x`, then the given radii in which convergence is guaranteed may be interchanged. This can be useful when the formal multilinear series in one representation has a particularly nice form, but the other has a larger radius. -/ theorem has_fpower_series_on_ball.exchange_radius {p₁ p₂ : formal_multilinear_series 𝕜 𝕜 E} {f : 𝕜 → E} {r₁ r₂ : ℝ≥0∞} {x : 𝕜} (h₁ : has_fpower_series_on_ball f p₁ x r₁) (h₂ : has_fpower_series_on_ball f p₂ x r₂) : has_fpower_series_on_ball f p₁ x r₂ := h₂.has_fpower_series_at.eq_formal_multilinear_series h₁.has_fpower_series_at ▸ h₂ /-- If a function `f : 𝕜 → E` has power series representation `p` on a ball of some radius and for each positive radius it has some power series representation, then `p` converges to `f` on the whole `𝕜`. -/ theorem has_fpower_series_on_ball.r_eq_top_of_exists {f : 𝕜 → E} {r : ℝ≥0∞} {x : 𝕜} {p : formal_multilinear_series 𝕜 𝕜 E} (h : has_fpower_series_on_ball f p x r) (h' : ∀ (r' : ℝ≥0) (hr : 0 < r'), ∃ p' : formal_multilinear_series 𝕜 𝕜 E, has_fpower_series_on_ball f p' x r') : has_fpower_series_on_ball f p x ∞ := { r_le := ennreal.le_of_forall_pos_nnreal_lt $ λ r hr hr', let ⟨p', hp'⟩ := h' r hr in (h.exchange_radius hp').r_le, r_pos := ennreal.coe_lt_top, has_sum := λ y hy, let ⟨r', hr'⟩ := exists_gt ∥y∥₊, ⟨p', hp'⟩ := h' r' hr'.ne_bot.bot_lt in (h.exchange_radius hp').has_sum $ mem_emetric_ball_zero_iff.mpr (ennreal.coe_lt_coe.2 hr') } end uniqueness /-! ### Changing origin in a power series If a function is analytic in a disk `D(x, R)`, then it is analytic in any disk contained in that one. Indeed, one can write $$ f (x + y + z) = \sum_{n} p_n (y + z)^n = \sum_{n, k} \binom{n}{k} p_n y^{n-k} z^k = \sum_{k} \Bigl(\sum_{n} \binom{n}{k} p_n y^{n-k}\Bigr) z^k. $$ The corresponding power series has thus a `k`-th coefficient equal to $\sum_{n} \binom{n}{k} p_n y^{n-k}$. In the general case where `pₙ` is a multilinear map, this has to be interpreted suitably: instead of having a binomial coefficient, one should sum over all possible subsets `s` of `fin n` of cardinal `k`, and attribute `z` to the indices in `s` and `y` to the indices outside of `s`. In this paragraph, we implement this. The new power series is called `p.change_origin y`. Then, we check its convergence and the fact that its sum coincides with the original sum. The outcome of this discussion is that the set of points where a function is analytic is open. -/ namespace formal_multilinear_series section variables (p : formal_multilinear_series 𝕜 E F) {x y : E} {r R : ℝ≥0} /-- A term of `formal_multilinear_series.change_origin_series`. Given a formal multilinear series `p` and a point `x` in its ball of convergence, `p.change_origin x` is a formal multilinear series such that `p.sum (x+y) = (p.change_origin x).sum y` when this makes sense. Each term of `p.change_origin x` is itself an analytic function of `x` given by the series `p.change_origin_series`. Each term in `change_origin_series` is the sum of `change_origin_series_term`'s over all `s` of cardinality `l`. The definition is such that `p.change_origin_series_term k l s hs (λ _, x) (λ _, y) = p (k + l) (s.piecewise (λ _, x) (λ _, y))` -/ def change_origin_series_term (k l : ℕ) (s : finset (fin (k + l))) (hs : s.card = l) : E [×l]→L[𝕜] E [×k]→L[𝕜] F := continuous_multilinear_map.curry_fin_finset 𝕜 E F hs (by erw [finset.card_compl, fintype.card_fin, hs, add_tsub_cancel_right]) (p $ k + l) lemma change_origin_series_term_apply (k l : ℕ) (s : finset (fin (k + l))) (hs : s.card = l) (x y : E) : p.change_origin_series_term k l s hs (λ _, x) (λ _, y) = p (k + l) (s.piecewise (λ _, x) (λ _, y)) := continuous_multilinear_map.curry_fin_finset_apply_const _ _ _ _ _ @[simp] lemma norm_change_origin_series_term (k l : ℕ) (s : finset (fin (k + l))) (hs : s.card = l) : ∥p.change_origin_series_term k l s hs∥ = ∥p (k + l)∥ := by simp only [change_origin_series_term, linear_isometry_equiv.norm_map] @[simp] lemma nnnorm_change_origin_series_term (k l : ℕ) (s : finset (fin (k + l))) (hs : s.card = l) : ∥p.change_origin_series_term k l s hs∥₊ = ∥p (k + l)∥₊ := by simp only [change_origin_series_term, linear_isometry_equiv.nnnorm_map] lemma nnnorm_change_origin_series_term_apply_le (k l : ℕ) (s : finset (fin (k + l))) (hs : s.card = l) (x y : E) : ∥p.change_origin_series_term k l s hs (λ _, x) (λ _, y)∥₊ ≤ ∥p (k + l)∥₊ * ∥x∥₊ ^ l * ∥y∥₊ ^ k := begin rw [← p.nnnorm_change_origin_series_term k l s hs, ← fin.prod_const, ← fin.prod_const], apply continuous_multilinear_map.le_of_op_nnnorm_le, apply continuous_multilinear_map.le_op_nnnorm end /-- The power series for `f.change_origin k`. Given a formal multilinear series `p` and a point `x` in its ball of convergence, `p.change_origin x` is a formal multilinear series such that `p.sum (x+y) = (p.change_origin x).sum y` when this makes sense. Its `k`-th term is the sum of the series `p.change_origin_series k`. -/ def change_origin_series (k : ℕ) : formal_multilinear_series 𝕜 E (E [×k]→L[𝕜] F) := λ l, ∑ s : {s : finset (fin (k + l)) // finset.card s = l}, p.change_origin_series_term k l s s.2 lemma nnnorm_change_origin_series_le_tsum (k l : ℕ) : ∥p.change_origin_series k l∥₊ ≤ ∑' (x : {s : finset (fin (k + l)) // s.card = l}), ∥p (k + l)∥₊ := (nnnorm_sum_le _ _).trans_eq $ by simp only [tsum_fintype, nnnorm_change_origin_series_term] lemma nnnorm_change_origin_series_apply_le_tsum (k l : ℕ) (x : E) : ∥p.change_origin_series k l (λ _, x)∥₊ ≤ ∑' s : {s : finset (fin (k + l)) // s.card = l}, ∥p (k + l)∥₊ * ∥x∥₊ ^ l := begin rw [nnreal.tsum_mul_right, ← fin.prod_const], exact (p.change_origin_series k l).le_of_op_nnnorm_le _ (p.nnnorm_change_origin_series_le_tsum _ _) end /-- Changing the origin of a formal multilinear series `p`, so that `p.sum (x+y) = (p.change_origin x).sum y` when this makes sense. -/ def change_origin (x : E) : formal_multilinear_series 𝕜 E F := λ k, (p.change_origin_series k).sum x /-- An auxiliary equivalence useful in the proofs about `formal_multilinear_series.change_origin_series`: the set of triples `(k, l, s)`, where `s` is a `finset (fin (k + l))` of cardinality `l` is equivalent to the set of pairs `(n, s)`, where `s` is a `finset (fin n)`. The forward map sends `(k, l, s)` to `(k + l, s)` and the inverse map sends `(n, s)` to `(n - finset.card s, finset.card s, s)`. The actual definition is less readable because of problems with non-definitional equalities. -/ @[simps] def change_origin_index_equiv : (Σ k l : ℕ, {s : finset (fin (k + l)) // s.card = l}) ≃ Σ n : ℕ, finset (fin n) := { to_fun := λ s, ⟨s.1 + s.2.1, s.2.2⟩, inv_fun := λ s, ⟨s.1 - s.2.card, s.2.card, ⟨s.2.map (fin.cast $ (tsub_add_cancel_of_le $ card_finset_fin_le s.2).symm).to_equiv.to_embedding, finset.card_map _⟩⟩, left_inv := begin rintro ⟨k, l, ⟨s : finset (fin $ k + l), hs : s.card = l⟩⟩, dsimp only [subtype.coe_mk], -- Lean can't automatically generalize `k' = k + l - s.card`, `l' = s.card`, so we explicitly -- formulate the generalized goal suffices : ∀ k' l', k' = k → l' = l → ∀ (hkl : k + l = k' + l') hs', (⟨k', l', ⟨finset.map (fin.cast hkl).to_equiv.to_embedding s, hs'⟩⟩ : (Σ k l : ℕ, {s : finset (fin (k + l)) // s.card = l})) = ⟨k, l, ⟨s, hs⟩⟩, { apply this; simp only [hs, add_tsub_cancel_right] }, rintro _ _ rfl rfl hkl hs', simp only [equiv.refl_to_embedding, fin.cast_refl, finset.map_refl, eq_self_iff_true, order_iso.refl_to_equiv, and_self, heq_iff_eq] end, right_inv := begin rintro ⟨n, s⟩, simp [tsub_add_cancel_of_le (card_finset_fin_le s), fin.cast_to_equiv] end } lemma change_origin_series_summable_aux₁ {r r' : ℝ≥0} (hr : (r + r' : ℝ≥0∞) < p.radius) : summable (λ s : Σ k l : ℕ, {s : finset (fin (k + l)) // s.card = l}, ∥p (s.1 + s.2.1)∥₊ * r ^ s.2.1 * r' ^ s.1) := begin rw ← change_origin_index_equiv.symm.summable_iff, dsimp only [(∘), change_origin_index_equiv_symm_apply_fst, change_origin_index_equiv_symm_apply_snd_fst], have : ∀ n : ℕ, has_sum (λ s : finset (fin n), ∥p (n - s.card + s.card)∥₊ * r ^ s.card * r' ^ (n - s.card)) (∥p n∥₊ * (r + r') ^ n), { intro n, -- TODO: why `simp only [tsub_add_cancel_of_le (card_finset_fin_le _)]` fails? convert_to has_sum (λ s : finset (fin n), ∥p n∥₊ * (r ^ s.card * r' ^ (n - s.card))) _, { ext1 s, rw [tsub_add_cancel_of_le (card_finset_fin_le _), mul_assoc] }, rw ← fin.sum_pow_mul_eq_add_pow, exact (has_sum_fintype _).mul_left _ }, refine nnreal.summable_sigma.2 ⟨λ n, (this n).summable, _⟩, simp only [(this _).tsum_eq], exact p.summable_nnnorm_mul_pow hr end lemma change_origin_series_summable_aux₂ (hr : (r : ℝ≥0∞) < p.radius) (k : ℕ) : summable (λ s : Σ l : ℕ, {s : finset (fin (k + l)) // s.card = l}, ∥p (k + s.1)∥₊ * r ^ s.1) := begin rcases ennreal.lt_iff_exists_add_pos_lt.1 hr with ⟨r', h0, hr'⟩, simpa only [mul_inv_cancel_right₀ (pow_pos h0 _).ne'] using ((nnreal.summable_sigma.1 (p.change_origin_series_summable_aux₁ hr')).1 k).mul_right (r' ^ k)⁻¹ end lemma change_origin_series_summable_aux₃ {r : ℝ≥0} (hr : ↑r < p.radius) (k : ℕ) : summable (λ l : ℕ, ∥p.change_origin_series k l∥₊ * r ^ l) := begin refine nnreal.summable_of_le (λ n, _) (nnreal.summable_sigma.1 $ p.change_origin_series_summable_aux₂ hr k).2, simp only [nnreal.tsum_mul_right], exact mul_le_mul' (p.nnnorm_change_origin_series_le_tsum _ _) le_rfl end lemma le_change_origin_series_radius (k : ℕ) : p.radius ≤ (p.change_origin_series k).radius := ennreal.le_of_forall_nnreal_lt $ λ r hr, le_radius_of_summable_nnnorm _ (p.change_origin_series_summable_aux₃ hr k) lemma nnnorm_change_origin_le (k : ℕ) (h : (∥x∥₊ : ℝ≥0∞) < p.radius) : ∥p.change_origin x k∥₊ ≤ ∑' s : Σ l : ℕ, {s : finset (fin (k + l)) // s.card = l}, ∥p (k + s.1)∥₊ * ∥x∥₊ ^ s.1 := begin refine tsum_of_nnnorm_bounded _ (λ l, p.nnnorm_change_origin_series_apply_le_tsum k l x), have := p.change_origin_series_summable_aux₂ h k, refine has_sum.sigma this.has_sum (λ l, _), exact ((nnreal.summable_sigma.1 this).1 l).has_sum end /-- The radius of convergence of `p.change_origin x` is at least `p.radius - ∥x∥`. In other words, `p.change_origin x` is well defined on the largest ball contained in the original ball of convergence.-/ lemma change_origin_radius : p.radius - ∥x∥₊ ≤ (p.change_origin x).radius := begin refine ennreal.le_of_forall_pos_nnreal_lt (λ r h0 hr, _), rw [lt_tsub_iff_right, add_comm] at hr, have hr' : (∥x∥₊ : ℝ≥0∞) < p.radius, from (le_add_right le_rfl).trans_lt hr, apply le_radius_of_summable_nnnorm, have : ∀ k : ℕ, ∥p.change_origin x k∥₊ * r ^ k ≤ (∑' s : Σ l : ℕ, {s : finset (fin (k + l)) // s.card = l}, ∥p (k + s.1)∥₊ * ∥x∥₊ ^ s.1) * r ^ k, from λ k, mul_le_mul_right' (p.nnnorm_change_origin_le k hr') (r ^ k), refine nnreal.summable_of_le this _, simpa only [← nnreal.tsum_mul_right] using (nnreal.summable_sigma.1 (p.change_origin_series_summable_aux₁ hr)).2 end end -- From this point on, assume that the space is complete, to make sure that series that converge -- in norm also converge in `F`. variables [complete_space F] (p : formal_multilinear_series 𝕜 E F) {x y : E} {r R : ℝ≥0} lemma has_fpower_series_on_ball_change_origin (k : ℕ) (hr : 0 < p.radius) : has_fpower_series_on_ball (λ x, p.change_origin x k) (p.change_origin_series k) 0 p.radius := have _ := p.le_change_origin_series_radius k, ((p.change_origin_series k).has_fpower_series_on_ball (hr.trans_le this)).mono hr this /-- Summing the series `p.change_origin x` at a point `y` gives back `p (x + y)`-/ theorem change_origin_eval (h : (∥x∥₊ + ∥y∥₊ : ℝ≥0∞) < p.radius) : (p.change_origin x).sum y = (p.sum (x + y)) := begin have radius_pos : 0 < p.radius := lt_of_le_of_lt (zero_le _) h, have x_mem_ball : x ∈ emetric.ball (0 : E) p.radius, from mem_emetric_ball_zero_iff.2 ((le_add_right le_rfl).trans_lt h), have y_mem_ball : y ∈ emetric.ball (0 : E) (p.change_origin x).radius, { refine mem_emetric_ball_zero_iff.2 (lt_of_lt_of_le _ p.change_origin_radius), rwa [lt_tsub_iff_right, add_comm] }, have x_add_y_mem_ball : x + y ∈ emetric.ball (0 : E) p.radius, { refine mem_emetric_ball_zero_iff.2 (lt_of_le_of_lt _ h), exact_mod_cast nnnorm_add_le x y }, set f : (Σ (k l : ℕ), {s : finset (fin (k + l)) // s.card = l}) → F := λ s, p.change_origin_series_term s.1 s.2.1 s.2.2 s.2.2.2 (λ _, x) (λ _, y), have hsf : summable f, { refine summable_of_nnnorm_bounded _ (p.change_origin_series_summable_aux₁ h) _, rintro ⟨k, l, s, hs⟩, dsimp only [subtype.coe_mk], exact p.nnnorm_change_origin_series_term_apply_le _ _ _ _ _ _ }, have hf : has_sum f ((p.change_origin x).sum y), { refine has_sum.sigma_of_has_sum ((p.change_origin x).summable y_mem_ball).has_sum (λ k, _) hsf, { dsimp only [f], refine continuous_multilinear_map.has_sum_eval _ _, have := (p.has_fpower_series_on_ball_change_origin k radius_pos).has_sum x_mem_ball, rw zero_add at this, refine has_sum.sigma_of_has_sum this (λ l, _) _, { simp only [change_origin_series, continuous_multilinear_map.sum_apply], apply has_sum_fintype }, { refine summable_of_nnnorm_bounded _ (p.change_origin_series_summable_aux₂ (mem_emetric_ball_zero_iff.1 x_mem_ball) k) (λ s, _), refine (continuous_multilinear_map.le_op_nnnorm _ _).trans_eq _, simp } } }, refine hf.unique (change_origin_index_equiv.symm.has_sum_iff.1 _), refine has_sum.sigma_of_has_sum (p.has_sum x_add_y_mem_ball) (λ n, _) (change_origin_index_equiv.symm.summable_iff.2 hsf), erw [(p n).map_add_univ (λ _, x) (λ _, y)], convert has_sum_fintype _, ext1 s, dsimp only [f, change_origin_series_term, (∘), change_origin_index_equiv_symm_apply_fst, change_origin_index_equiv_symm_apply_snd_fst, change_origin_index_equiv_symm_apply_snd_snd_coe], rw continuous_multilinear_map.curry_fin_finset_apply_const, have : ∀ m (hm : n = m), p n (s.piecewise (λ _, x) (λ _, y)) = p m ((s.map (fin.cast hm).to_equiv.to_embedding).piecewise (λ _, x) (λ _, y)), { rintro m rfl, simp }, apply this end end formal_multilinear_series section variables [complete_space F] {f : E → F} {p : formal_multilinear_series 𝕜 E F} {x y : E} {r : ℝ≥0∞} /-- If a function admits a power series expansion `p` on a ball `B (x, r)`, then it also admits a power series on any subball of this ball (even with a different center), given by `p.change_origin`. -/ theorem has_fpower_series_on_ball.change_origin (hf : has_fpower_series_on_ball f p x r) (h : (∥y∥₊ : ℝ≥0∞) < r) : has_fpower_series_on_ball f (p.change_origin y) (x + y) (r - ∥y∥₊) := { r_le := begin apply le_trans _ p.change_origin_radius, exact tsub_le_tsub hf.r_le le_rfl end, r_pos := by simp [h], has_sum := λ z hz, begin convert (p.change_origin y).has_sum _, { rw [mem_emetric_ball_zero_iff, lt_tsub_iff_right, add_comm] at hz, rw [p.change_origin_eval (hz.trans_le hf.r_le), add_assoc, hf.sum], refine mem_emetric_ball_zero_iff.2 (lt_of_le_of_lt _ hz), exact_mod_cast nnnorm_add_le y z }, { refine emetric.ball_subset_ball (le_trans _ p.change_origin_radius) hz, exact tsub_le_tsub hf.r_le le_rfl } end } /-- If a function admits a power series expansion `p` on an open ball `B (x, r)`, then it is analytic at every point of this ball. -/ lemma has_fpower_series_on_ball.analytic_at_of_mem (hf : has_fpower_series_on_ball f p x r) (h : y ∈ emetric.ball x r) : analytic_at 𝕜 f y := begin have : (∥y - x∥₊ : ℝ≥0∞) < r, by simpa [edist_eq_coe_nnnorm_sub] using h, have := hf.change_origin this, rw [add_sub_cancel'_right] at this, exact this.analytic_at end lemma has_fpower_series_on_ball.analytic_on (hf : has_fpower_series_on_ball f p x r) : analytic_on 𝕜 f (emetric.ball x r) := λ y hy, hf.analytic_at_of_mem hy variables (𝕜 f) /-- For any function `f` from a normed vector space to a Banach space, the set of points `x` such that `f` is analytic at `x` is open. -/ lemma is_open_analytic_at : is_open {x | analytic_at 𝕜 f x} := begin rw is_open_iff_mem_nhds, rintro x ⟨p, r, hr⟩, exact mem_of_superset (emetric.ball_mem_nhds _ hr.r_pos) (λ y hy, hr.analytic_at_of_mem hy) end end
9987d0a5b47d829dbe41790ee8c9edbfe5e7d942
a25cc44b447f2fbde61e4b848318762eb892f2bf
/src/basic.lean
aea950c2392b33db8df75d25abeb6c2910f13e6a
[]
no_license
NeilStrickland/itloc
20f9b9bcaa4d5840f7f32e84108174b294d84b1a
5b13b5b418766d10926b983eb3dd2ac42abf63d8
refs/heads/master
1,592,747,030,159
1,578,138,004,000
1,578,138,004,000
197,470,135
2
0
null
null
null
null
UTF-8
Lean
false
false
16,262
lean
/- Copyright (c) 2019 Neil Strickland. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Neil Strickland This file effectively deals with the cartesian-closed category of finite posets and the associated "strong homotopy category". However, we have taken an ad hoc approach rather than using the category theory library. -/ import order.basic import data.equiv.basic import data.fintype import logic.relation import algebra.punit_instances import sort_rank fin_extra universes uP uQ uR uS variables (P : Type uP) [partial_order P] variables (Q : Type uQ) [partial_order Q] variables (R : Type uR) [partial_order R] variables (S : Type uS) [partial_order S] namespace poset structure hom := (val : P → Q) (property : monotone val) instance : has_coe_to_fun (hom P Q) := { F := λ _, P → Q, coe := λ f, f.val } @[ext] lemma hom_ext (f g : hom P Q) : (∀ (p : P), f p = g p) → f = g := begin rcases f with ⟨f,hf⟩, rcases g with ⟨g,hg⟩, intro h, have h' : f = g := funext h, rcases h', refl, end def id : hom P P := ⟨_root_.id,monotone_id⟩ lemma id_val : (id P).val = _root_.id := rfl variables {P Q R} instance hom_order : partial_order (hom P Q) := { le := λ f g, ∀ p, (f p) ≤ (g p), le_refl := λ f p,le_refl (f p), le_antisymm := λ f g f_le_g g_le_f, begin ext p, exact le_antisymm (f_le_g p) (g_le_f p), end, le_trans := λ f g h f_le_g g_le_h p, le_trans (f_le_g p) (g_le_h p) } @[simp] lemma id_eval (p : P) : (id P) p = p := rfl variable (P) def const (q : Q) : hom P Q := ⟨λ p,q, λ p₀ p₁ hp, le_refl q⟩ def terminal : hom P punit.{uP + 1} := const P punit.star variable {P} lemma eq_terminal (f : hom P punit.{uP + 1}) : f = terminal P := begin ext p, cases f p, cases terminal P p, refl end @[irreducible] def adjoint (f : hom P Q) (g : hom Q P) : Prop := ∀ {p : P} {q : Q}, f p ≤ q ↔ p ≤ g q def adjoint.iff {f : hom P Q} {g : hom Q P} (h : adjoint f g) : ∀ {p : P} {q : Q}, f p ≤ q ↔ p ≤ g q := by { intros p q, unfold adjoint at h, exact h } def comp : (hom Q R) → (hom P Q) → (hom P R) := λ g f, ⟨g.val ∘ f.val, monotone.comp g.property f.property ⟩ lemma comp_val (g : hom Q R) (f : hom P Q) : (comp g f).val = g.val ∘ f.val := rfl lemma id_comp (f : hom P Q) : comp (id Q) f = f := by {ext, refl} lemma comp_id (f : hom P Q) : comp f (id P) = f := by {ext, refl} lemma comp_assoc (h : hom R S) (g : hom Q R) (f : hom P Q) : comp (comp h g) f = comp h (comp g f) := by {ext, refl} lemma const_comp (r : R) (f : hom P Q) : comp (const Q r) f = const P r := by {ext, refl} lemma comp_const (g : hom Q R) (q : Q) : comp g (const P q) = const P (g q) := by {ext, refl} lemma comp_mono₂ {g₀ g₁ : hom Q R} {f₀ f₁ : hom P Q} (eg : g₀ ≤ g₁) (ef : f₀ ≤ f₁) : comp g₀ f₀ ≤ comp g₁ f₁ := λ p, calc g₀.val (f₀.val p) ≤ g₀.val (f₁.val p) : g₀.property (ef p) ... ≤ g₁.val (f₁.val p) : eg (f₁.val p) @[simp] lemma comp_eval (g : hom Q R) (f : hom P Q) (p : P) : (comp g f) p = g (f p) := rfl def comp' : (hom Q R) × (hom P Q) → (hom P R) := λ ⟨g,f⟩, comp g f lemma comp'_mono : monotone (@comp' P _ Q _ R _) := λ ⟨g₀,f₀⟩ ⟨g₁,f₁⟩ ⟨eg,ef⟩, comp_mono₂ eg ef def eval : (hom P Q) → P → Q := λ f p, f.val p lemma eval_mono₂ {f₀ f₁ : hom P Q} {p₀ p₁ : P} (ef : f₀ ≤ f₁) (ep : p₀ ≤ p₁) : eval f₀ p₀ ≤ eval f₁ p₁ := calc f₀.val p₀ ≤ f₀.val p₁ : f₀.property ep ... ≤ f₁.val p₁ : ef p₁ def eval' : (hom P Q) × P → Q := λ ⟨f,p⟩, eval f p lemma eval'_mono : monotone (@eval' P _ Q _) := λ ⟨f₀,p₀⟩ ⟨f₁,p₁⟩ ⟨ef,ep⟩, eval_mono₂ ef ep def ins' : P → (hom Q (P × Q)) := λ p, ⟨λ q,⟨p,q⟩, λ q₀ q₁ eq, ⟨le_refl p,eq⟩⟩ lemma ins_mono : monotone (@ins' P _ Q _) := λ p₀ p₁ ep q, ⟨ep,le_refl q⟩ lemma adjoint.unit {f : hom P Q} {g : hom Q P} (h : adjoint f g) : id P ≤ comp g f := λ p, h.iff.mp (le_refl (f p)) lemma adjoint.counit {f : hom P Q} {g : hom Q P} (h : adjoint f g) : comp f g ≤ id Q := λ q, h.iff.mpr (le_refl (g q)) variable (P) def π₀ : Type* := quot (has_le.le : P → P → Prop) variable {P} def component (p : P) : π₀ P := quot.mk _ p def connected : P → P → Prop := λ p₀ p₁, component p₀ = component p₁ lemma π₀.sound {p₀ p₁ : P} (hp : p₀ ≤ p₁) : component p₀ = component p₁ := quot.sound hp lemma π₀.epi {X : Type*} (f₀ f₁ : π₀ P → X) (h : ∀ p, f₀ (component p) = f₁ (component p)) : f₀ = f₁ := by {apply funext, rintro ⟨p⟩, exact (h p),} def π₀.lift {X : Type*} (f : P → X) (h : ∀ p₀ p₁ : P, p₀ ≤ p₁ → f p₀ = f p₁) : (π₀ P) → X := @quot.lift P has_le.le X f h lemma π₀.lift_beta {X : Type*} (f : P → X) (h : ∀ p₀ p₁ : P, p₀ ≤ p₁ → f p₀ = f p₁) (p : P) : π₀.lift f h (component p) = f p := @quot.lift_beta P has_le.le X f h p def π₀.lift₂ {X : Type*} (f : P → Q → X) (h : ∀ p₀ p₁ q₀ q₁, p₀ ≤ p₁ → q₀ ≤ q₁ → f p₀ q₀ = f p₁ q₁) : (π₀ P) → (π₀ Q) → X := begin let h1 := λ p q₀ q₁ hq, h p p q₀ q₁ (le_refl p) hq, let f1 : P → (π₀ Q) → X := λ p, π₀.lift (f p) (h1 p), let hf1 : ∀ p q, f1 p (component q) = f p q := λ p, π₀.lift_beta (f p) (h1 p), let h2 : ∀ p₀ p₁, p₀ ≤ p₁ → f1 p₀ = f1 p₁ := λ p₀ p₁ hp, begin apply π₀.epi,intro q,rw[hf1,hf1], exact h p₀ p₁ q q hp (le_refl q), end, exact π₀.lift f1 h2 end lemma π₀.lift₂_beta {X : Type*} (f : P → Q → X) (h : ∀ p₀ p₁ q₀ q₁, p₀ ≤ p₁ → q₀ ≤ q₁ → f p₀ q₀ = f p₁ q₁) (p : P) (q : Q) : (π₀.lift₂ f h) (component p) (component q) = f p q := begin unfold π₀.lift₂,simp only [],rw[π₀.lift_beta,π₀.lift_beta], end lemma parity_induction (u : ℕ → Prop) (h_zero : u 0) (h_even : ∀ i, u (2 * i) → u (2 * i + 1)) (h_odd : ∀ i, u (2 * i + 1) → u (2 * i + 2)) : ∀ i, u i | 0 := h_zero | (i + 1) := begin have ih := parity_induction i, let k := i.div2, have hi : cond i.bodd 1 0 + 2 * k = i := nat.bodd_add_div2 i, rcases i.bodd ; intro hk; rw[cond] at hk, { rw [zero_add] at hk, rw [← hk] at ih ⊢, exact h_even k ih }, { rw [add_comm] at hk, rw [← hk] at ih ⊢, exact h_odd k ih } end lemma zigzag (u : ℕ → P) (h_even : ∀ i, u (2 * i) ≤ u (2 * i + 1)) (h_odd : ∀ i, u (2 * i + 2) ≤ u(2 * i + 1)) : ∀ i, component (u i) = component (u 0) := parity_induction (λ i, component (u i) = component (u 0)) rfl (λ i h, (π₀.sound (h_even i)).symm.trans h) (λ i h, (π₀.sound (h_odd i)).trans h) variables (P Q) def homₕ := π₀ (hom P Q) def idₕ : homₕ P P := component (id P) variables {P Q} def compₕ : (homₕ Q R) → (homₕ P Q) → (homₕ P R) := π₀.lift₂ (λ g f, component (comp g f)) (begin intros g₀ g₁ f₀ f₁ hg hf, let hgf := comp_mono₂ hg hf, let hgf' := π₀.sound hgf, exact (π₀.sound (comp_mono₂ hg hf)) end) lemma compₕ_def (g : hom Q R) (f : hom P Q) : compₕ (component g) (component f) = component (comp g f) := by {simp[compₕ,π₀.lift₂_beta]} lemma id_compₕ (f : homₕ P Q) : compₕ (idₕ Q) f = f := begin rcases f with f, change compₕ (component (id Q)) (component f) = component f, rw[compₕ_def,id_comp], end lemma comp_idₕ (f : homₕ P Q) : compₕ f (idₕ P) = f := begin rcases f with f, change compₕ (component f) (component (id P)) = component f, rw[compₕ_def,comp_id], end lemma comp_assocₕ (h : homₕ R S) (g : homₕ Q R) (f : homₕ P Q) : compₕ (compₕ h g) f = compₕ h (compₕ g f) := begin rcases h with h, rcases g with g, rcases f with f, change compₕ (compₕ (component h) (component g)) (component f) = compₕ (component h) (compₕ (component g) (component f)), repeat {rw[compₕ_def]},rw[comp_assoc], end variables (P Q) structure equivₕ := (to_fun : homₕ P Q) (inv_fun : homₕ Q P) (left_inv : compₕ inv_fun to_fun = idₕ P) (right_inv : compₕ to_fun inv_fun = idₕ Q) @[refl] def equivₕ.refl : equivₕ P P := { to_fun := idₕ P, inv_fun := idₕ P, left_inv := comp_idₕ _, right_inv := comp_idₕ _ } variables {P Q} @[symm] def equivₕ.symm (e : equivₕ P Q) : equivₕ Q P := { to_fun := e.inv_fun, inv_fun := e.to_fun, left_inv := e.right_inv, right_inv := e.left_inv } @[trans] def equivₕ.trans (e : equivₕ P Q) (f : equivₕ Q R) : (equivₕ P R) := { to_fun := compₕ f.to_fun e.to_fun, inv_fun := compₕ e.inv_fun f.inv_fun, left_inv := by rw [comp_assocₕ, ← comp_assocₕ _ f.inv_fun, f.left_inv, id_compₕ, e.left_inv], right_inv := by rw [comp_assocₕ, ← comp_assocₕ _ e.to_fun, e.right_inv, id_compₕ, f.right_inv] } lemma adjoint.unitₕ {f : hom P Q} {g : hom Q P} (h : adjoint f g) : compₕ (component g) (component f) = idₕ P := begin have : id P ≤ comp g f := by { apply adjoint.unit, assumption }, exact (π₀.sound this).symm end lemma adjoint.counitₕ {f : hom P Q} {g : hom Q P} (h : adjoint f g) : compₕ (component f) (component g) = idₕ Q := begin have : comp f g ≤ id Q := by { apply adjoint.counit, assumption }, exact (π₀.sound this) end /-- LaTeX: rem-adjoint-strong -/ def equivₕ_of_adjoint {f : hom P Q} {g : hom Q P} (h : adjoint f g) : equivₕ P Q := { to_fun := component f, inv_fun := component g, left_inv := adjoint.unitₕ h, right_inv := adjoint.counitₕ h } variable (P) /-- defn-strongly-contractible -/ def contractibleₕ := nonempty (equivₕ P punit.{uP + 1}) variable {P} lemma contractibleₕ_of_smallest {m : P} (h : ∀ p, m ≤ p) : contractibleₕ P := begin have : adjoint (const punit.{uP + 1} m) (terminal P) := begin unfold adjoint, rintro ⟨⟩ p, change m ≤ p ↔ punit.star ≤ punit.star, simp only [le_refl, h p], end, let hh := equivₕ_of_adjoint this, exact ⟨hh.symm⟩, end def π₀.map (f : hom P Q) : (π₀ P) → (π₀ Q) := π₀.lift (λ p, component (f p)) (λ p₀ p₁ ep, quot.sound (f.property ep)) lemma π₀.map_def (f : hom P Q) (p : P) : π₀.map f (component p) = component (f p) := by { simp [π₀.map, π₀.lift_beta] } lemma π₀.map_congr {f₀ f₁ : hom P Q} (ef : f₀ ≤ f₁) : π₀.map f₀ = π₀.map f₁ := begin apply π₀.epi, intro p, rw [π₀.map_def, π₀.map_def], exact π₀.sound (ef p) end variable (P) lemma π₀.map_id : π₀.map (id P) = _root_.id := by { apply π₀.epi, intro p, rw[π₀.map_def], refl } variable {P} lemma π₀.map_comp (g : hom Q R) (f : hom P Q) : π₀.map (comp g f) = (π₀.map g) ∘ (π₀.map f) := by { apply π₀.epi, intro p, rw[π₀.map_def], refl } def evalₕ : (homₕ P Q) → (π₀ P) → (π₀ Q) := π₀.lift π₀.map (@π₀.map_congr _ _ _ _) variables {P Q} def comma (f : hom P Q) (q : Q) := { p : P // f p ≤ q } instance comma_order (f : hom P Q) (q : Q) : partial_order (comma f q) := by { dsimp[comma], apply_instance } def cocomma (f : hom P Q) (q : Q) := { p : P // q ≤ f p } instance cocomma_order (f : hom P Q) (q : Q) : partial_order (cocomma f q) := by { dsimp[cocomma], apply_instance } /-- Here we define predicates finalₕ and cofinalₕ. If (finalₕ f) holds then f is homotopy cofinal, by prop-cofinal. The dual is also valid, but the converse is not. -/ def finalₕ (f : hom P Q) : Prop := ∀ q, contractibleₕ (cocomma f q) def cofinalₕ (f : hom P Q) : Prop := ∀ q, contractibleₕ (comma f q) variable (P) structure fin_ranking := (card : ℕ) (rank : P ≃ fin card) (rank_mono : monotone rank.to_fun) section sort variable {P} variable [decidable_rel (has_le.le : P → P → Prop)] def is_semisorted (l : list P) : Prop := l.pairwise (λ a b, ¬ b < a) lemma mem_ordered_insert (x p : P) (l : list P) : x ∈ (l.ordered_insert has_le.le p) ↔ x = p ∨ x ∈ l := begin rw [list.mem_of_perm (list.perm_ordered_insert _ _ _)], apply list.mem_cons_iff end lemma insert_semisorted (p : P) (l : list P) (h : is_semisorted l) : is_semisorted (l.ordered_insert has_le.le p) := begin induction h with q l hq hl ih, { apply list.pairwise_singleton }, { dsimp [list.ordered_insert], split_ifs with hpq, { apply list.pairwise.cons, { intros x x_in_ql, rcases (list.mem_cons_iff _ _ _).mp x_in_ql with ⟨⟨⟩⟩ | x_in_l, { exact not_lt_of_ge hpq }, { intro x_lt_p, exact hq x x_in_l (lt_of_lt_of_le x_lt_p hpq) } }, { exact list.pairwise.cons hq hl } }, { apply list.pairwise.cons, { intros x x_in_pl x_lt_q, rw [mem_ordered_insert] at x_in_pl, rcases x_in_pl with ⟨⟨⟩⟩ | x_in_l, { exact hpq (le_of_lt x_lt_q) }, { exact hq x x_in_l x_lt_q } }, { exact ih } } } end lemma insertion_sort_semisorted (l : list P) : is_semisorted (l.insertion_sort (has_le.le : P → P → Prop)) := begin induction l with p l ih, { apply list.pairwise.nil }, { dsimp [list.insertion_sort], apply insert_semisorted, exact ih } end variable (P) lemma exists_fin_ranking [fintype P] : nonempty (fin_ranking P) := begin rcases fintype.exists_equiv_fin P with ⟨n,⟨f⟩⟩, let l := (fin.elems_list n).map f.symm, have l_nodup : l.nodup := list.nodup_map f.symm.injective (fin.elems_list_nodup n), have l_univ : ∀ p, p ∈ l := λ p, begin apply list.mem_map.mpr, exact ⟨f.to_fun p, ⟨fin.elems_list_complete (f.to_fun p),f.left_inv p⟩⟩ end, have l_length : l.length = n := (list.length_map f.symm (fin.elems_list n)).trans (fin.elems_list_length n), let ls := l.insertion_sort has_le.le, let ls_perm := list.perm_insertion_sort has_le.le l, have ls_sorted : is_semisorted ls := insertion_sort_semisorted l, have ls_nodup : ls.nodup := (list.perm_nodup ls_perm).mpr l_nodup, have ls_univ : ∀ p, p ∈ ls := λ p, (list.mem_of_perm ls_perm).mpr (l_univ p), have ls_length : ls.length = n := (list.perm_length ls_perm).trans l_length, let inv_fun : (fin n) → P := λ i, ls.nth_le i.val (@eq.subst ℕ (nat.lt i.val) _ _ ls_length.symm i.is_lt), let to_fun_aux : P → {i : fin n // inv_fun i = a} := begin intro p, let i_val := ls.index_of p, let i_lt_l := list.index_of_lt_length.mpr (ls_univ p), let i_lt_n : i_val < n := @eq.subst ℕ (nat.lt i_val) _ _ ls_length i_lt_l, let i : fin n := ⟨i_val,i_lt_n⟩, have : inv_fun i = p := list.index_of_nth_le i_lt_l, exact ⟨i,this⟩ end, let to_fun : P → (fin n) := λ p, (to_fun_aux p).val, let left_inv : ∀ p : P, inv_fun (to_fun p) = p := λ p, (to_fun_aux p).property, let right_inv : ∀ i : (fin n), to_fun (inv_fun i) = i := begin intro i,cases i, apply fin.eq_of_veq, let i_lt_l : i_val < ls.length := @eq.subst ℕ (nat.lt i_val) _ _ ls_length.symm i_is_lt, exact list.nth_le_index_of ls_nodup i_val i_lt_l, end, let g : P ≃ (fin n) := ⟨to_fun,inv_fun,left_inv,right_inv⟩, have g_mono : monotone g.to_fun := λ p q hpq, begin let i := g.to_fun p, let j := g.to_fun q, have hp : g.inv_fun i = p := g.left_inv p, have hq : g.inv_fun j = q := g.left_inv q, have hi : i.val < ls.length := by { rw [ls_length], exact i.is_lt }, have hj : j.val < ls.length := by { rw [ls_length], exact j.is_lt }, by_cases h : i ≤ j, { exact h }, exfalso, replace h := lt_of_not_ge h, have hp' : ls.nth_le i.val hi = g.inv_fun i := rfl, have hq' : ls.nth_le j.val hj = g.inv_fun j := rfl, let h_ne := list.pairwise_nth_iff.mp ls_nodup h hi, let h_ngt := list.pairwise_nth_iff.mp ls_sorted h hi, rw [hp', hq', hp, hq] at h_ne h_ngt, exact h_ngt (lt_of_le_of_ne hpq h_ne.symm), end, exact ⟨⟨n,g,g_mono⟩⟩ end end sort end poset
98189d232af36ff33fefcfad7379cb8e0cab2cab
c45b34bfd44d8607a2e8762c926e3cfaa7436201
/uexp/src/uexp/rules/pushAggregateThroughJoin3.lean
c77b12259347fcebf09f99740de9df444e3399cd
[ "BSD-2-Clause" ]
permissive
Shamrock-Frost/Cosette
b477c442c07e45082348a145f19ebb35a7f29392
24cbc4adebf627f13f5eac878f04ffa20d1209af
refs/heads/master
1,619,721,304,969
1,526,082,841,000
1,526,082,841,000
121,695,605
1
0
null
1,518,737,210,000
1,518,737,210,000
null
UTF-8
Lean
false
false
2,166
lean
import ..sql import ..tactics import ..u_semiring import ..extra_constants import ..ucongr import ..TDP set_option profiler true open Expr open Proj open Pred open SQL open tree notation `int` := datatypes.int variable integer_10: const datatypes.int theorem rule: forall ( Γ scm_t scm_account scm_bonus scm_dept scm_emp: Schema) (rel_t: relation scm_t) (rel_account: relation scm_account) (rel_bonus: relation scm_bonus) (rel_dept: relation scm_dept) (rel_emp: relation scm_emp) (t_k0 : Column int scm_t) (t_c1 : Column int scm_t) (t_f1_a0 : Column int scm_t) (t_f2_a0 : Column int scm_t) (t_f0_c0 : Column int scm_t) (t_f1_c0 : Column int scm_t) (t_f0_c1 : Column int scm_t) (t_f1_c2 : Column int scm_t) (t_f2_c3 : Column int scm_t) (account_acctno : Column int scm_account) (account_type : Column int scm_account) (account_balance : Column int scm_account) (bonus_ename : Column int scm_bonus) (bonus_job : Column int scm_bonus) (bonus_sal : Column int scm_bonus) (bonus_comm : Column int scm_bonus) (dept_deptno : Column int scm_dept) (dept_name : Column int scm_dept) (emp_empno : Column int scm_emp) (emp_ename : Column int scm_emp) (emp_job : Column int scm_emp) (emp_mgr : Column int scm_emp) (emp_hiredate : Column int scm_emp) (emp_comm : Column int scm_emp) (emp_sal : Column int scm_emp) (emp_deptno : Column int scm_emp) (emp_slacker : Column int scm_emp), denoteSQL (DISTINCT (SELECT1 (combine (right⋅left⋅emp_empno) (right⋅right⋅dept_deptno)) FROM1 (product ((SELECT * FROM1 (table rel_emp) WHERE (equal (uvariable (right⋅emp_empno)) (constantExpr integer_10)))) (table rel_dept)) WHERE (castPred (combine (right⋅right⋅dept_deptno) (right⋅left⋅emp_empno) ) predicates.gt)) :SQL Γ _) = denoteSQL (DISTINCT (SELECT1 (combine (right⋅left⋅emp_empno) (right⋅right⋅dept_deptno)) FROM1 (product ((SELECT * FROM1 (table rel_emp) WHERE (equal (uvariable (right⋅emp_empno)) (constantExpr integer_10)))) (table rel_dept)) WHERE (castPred (combine (right⋅right⋅dept_deptno) (right⋅left⋅emp_empno) ) predicates.gt)) :SQL Γ _) := begin intros, unfold_all_denotations, funext, try {simp}, try {TDP' ucongr}, end
56207647e252167fa4c13a66bdb5a4430d29c774
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/limits/pi.lean
f332f4f13ec6ef54f5844ad0f2e466acd95ce611
[ "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
4,616
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.pi.basic import category_theory.limits.has_limits /-! # Limits in the category of indexed families of objects. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. Given a functor `F : J ⥤ Π i, C i` into a category of indexed families, 1. we can assemble a collection of cones over `F ⋙ pi.eval C i` into a cone over `F` 2. if all those cones are limit cones, the assembled cone is a limit cone, and 3. if we have limits for each of `F ⋙ pi.eval C i`, we can produce a `has_limit F` instance -/ open category_theory open category_theory.limits namespace category_theory.pi universes v₁ v₂ u₁ u₂ variables {I : Type v₁} {C : I → Type u₁} [Π i, category.{v₁} (C i)] variables {J : Type v₁} [small_category J] variables {F : J ⥤ Π i, C i} /-- A cone over `F : J ⥤ Π i, C i` has as its components cones over each of the `F ⋙ pi.eval C i`. -/ def cone_comp_eval (c : cone F) (i : I) : cone (F ⋙ pi.eval C i) := { X := c.X i, π := { app := λ j, c.π.app j i, naturality' := λ j j' f, congr_fun (c.π.naturality f) i, } } /-- A cocone over `F : J ⥤ Π i, C i` has as its components cocones over each of the `F ⋙ pi.eval C i`. -/ def cocone_comp_eval (c : cocone F) (i : I) : cocone (F ⋙ pi.eval C i) := { X := c.X i, ι := { app := λ j, c.ι.app j i, naturality' := λ j j' f, congr_fun (c.ι.naturality f) i, } } /-- Given a family of cones over the `F ⋙ pi.eval C i`, we can assemble these together as a `cone F`. -/ def cone_of_cone_comp_eval (c : Π i, cone (F ⋙ pi.eval C i)) : cone F := { X := λ i, (c i).X, π := { app := λ j i, (c i).π.app j, naturality' := λ j j' f, by { ext i, exact (c i).π.naturality f, } } } /-- Given a family of cocones over the `F ⋙ pi.eval C i`, we can assemble these together as a `cocone F`. -/ def cocone_of_cocone_comp_eval (c : Π i, cocone (F ⋙ pi.eval C i)) : cocone F := { X := λ i, (c i).X, ι := { app := λ j i, (c i).ι.app j, naturality' := λ j j' f, by { ext i, exact (c i).ι.naturality f, } } } /-- Given a family of limit cones over the `F ⋙ pi.eval C i`, assembling them together as a `cone F` produces a limit cone. -/ def cone_of_cone_eval_is_limit {c : Π i, cone (F ⋙ pi.eval C i)} (P : Π i, is_limit (c i)) : is_limit (cone_of_cone_comp_eval c) := { lift := λ s i, (P i).lift (cone_comp_eval s i), fac' := λ s j, begin ext i, exact (P i).fac (cone_comp_eval s i) j, end, uniq' := λ s m w, begin ext i, exact (P i).uniq (cone_comp_eval s i) (m i) (λ j, congr_fun (w j) i) end } /-- Given a family of colimit cocones over the `F ⋙ pi.eval C i`, assembling them together as a `cocone F` produces a colimit cocone. -/ def cocone_of_cocone_eval_is_colimit {c : Π i, cocone (F ⋙ pi.eval C i)} (P : Π i, is_colimit (c i)) : is_colimit (cocone_of_cocone_comp_eval c) := { desc := λ s i, (P i).desc (cocone_comp_eval s i), fac' := λ s j, begin ext i, exact (P i).fac (cocone_comp_eval s i) j, end, uniq' := λ s m w, begin ext i, exact (P i).uniq (cocone_comp_eval s i) (m i) (λ j, congr_fun (w j) i) end } section variables [∀ i, has_limit (F ⋙ pi.eval C i)] /-- If we have a functor `F : J ⥤ Π i, C i` into a category of indexed families, and we have limits for each of the `F ⋙ pi.eval C i`, then `F` has a limit. -/ lemma has_limit_of_has_limit_comp_eval : has_limit F := has_limit.mk { cone := cone_of_cone_comp_eval (λ i, limit.cone _), is_limit := cone_of_cone_eval_is_limit (λ i, limit.is_limit _), } end section variables [∀ i, has_colimit (F ⋙ pi.eval C i)] /-- If we have a functor `F : J ⥤ Π i, C i` into a category of indexed families, and colimits exist for each of the `F ⋙ pi.eval C i`, there is a colimit for `F`. -/ lemma has_colimit_of_has_colimit_comp_eval : has_colimit F := has_colimit.mk { cocone := cocone_of_cocone_comp_eval (λ i, colimit.cocone _), is_colimit := cocone_of_cocone_eval_is_colimit (λ i, colimit.is_colimit _), } end /-! As an example, we can use this to construct particular shapes of limits in a category of indexed families. With the addition of `import category_theory.limits.shapes.types` we can use: ``` local attribute [instance] has_limit_of_has_limit_comp_eval example : has_binary_products (I → Type v₁) := ⟨by apply_instance⟩ ``` -/ end category_theory.pi
2160387e81d2a233a403142649e7fac82acbfa85
b7f22e51856f4989b970961f794f1c435f9b8f78
/library/data/list/perm.lean
24be55f54c4d4f6d7d9ac6a2f8f890542e5d6d2e
[ "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
40,848
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 List permutations. -/ import data.list.basic data.list.set open list setoid nat binary variables {A B : Type} inductive perm : list A → list A → Prop := | nil : perm [] [] | skip : Π (x : A) {l₁ l₂ : list A}, perm l₁ l₂ → perm (x::l₁) (x::l₂) | swap : Π (x y : A) (l : list A), perm (y::x::l) (x::y::l) | trans : Π {l₁ l₂ l₃ : list A}, perm l₁ l₂ → perm l₂ l₃ → perm l₁ l₃ namespace perm infix ~ := perm theorem eq_nil_of_perm_nil {l₁ : list A} (p : [] ~ l₁) : l₁ = [] := have gen : ∀ (l₂ : list A) (p : l₂ ~ l₁), l₂ = [] → l₁ = [], from take l₂ p, perm.induction_on p (λ h, h) (by contradiction) (by contradiction) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂ e, r₂ (r₁ e)), gen [] p rfl theorem not_perm_nil_cons (x : A) (l : list A) : ¬ [] ~ (x::l) := have gen : ∀ (l₁ l₂ : list A) (p : l₁ ~ l₂), l₁ = [] → l₂ = (x::l) → false, from take l₁ l₂ p, perm.induction_on p (by contradiction) (by contradiction) (by contradiction) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂ e₁ e₂, begin rewrite [e₂ at *, e₁ at *], have e₃ : l₂ = [], from eq_nil_of_perm_nil p₁, exact (r₂ e₃ rfl) end), assume p, gen [] (x::l) p rfl rfl protected theorem refl [refl] : ∀ (l : list A), l ~ l | [] := nil | (x::xs) := skip x (refl xs) protected theorem symm [symm] : ∀ {l₁ l₂ : list A}, l₁ ~ l₂ → l₂ ~ l₁ := take l₁ l₂ p, perm.induction_on p nil (λ x l₁ l₂ p₁ r₁, skip x r₁) (λ x y l, swap y x l) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, trans r₂ r₁) attribute perm.trans [trans] theorem eqv (A : Type) : equivalence (@perm A) := mk_equivalence (@perm A) (@perm.refl A) (@perm.symm A) (@perm.trans A) protected definition is_setoid [instance] (A : Type) : setoid (list A) := setoid.mk (@perm A) (perm.eqv A) theorem mem_perm {a : A} {l₁ l₂ : list A} : l₁ ~ l₂ → a ∈ l₁ → a ∈ l₂ := assume p, perm.induction_on p (λ h, h) (λ x l₁ l₂ p₁ r₁ i, or.elim (eq_or_mem_of_mem_cons i) (suppose a = x, by rewrite this; apply !mem_cons) (suppose a ∈ l₁, or.inr (r₁ this))) (λ x y l ainyxl, or.elim (eq_or_mem_of_mem_cons ainyxl) (suppose a = y, by rewrite this; exact (or.inr !mem_cons)) (suppose a ∈ x::l, or.elim (eq_or_mem_of_mem_cons this) (suppose a = x, or.inl this) (suppose a ∈ l, or.inr (or.inr this)))) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂ ainl₁, r₂ (r₁ ainl₁)) theorem not_mem_perm {a : A} {l₁ l₂ : list A} : l₁ ~ l₂ → a ∉ l₁ → a ∉ l₂ := assume p nainl₁ ainl₂, absurd (mem_perm (perm.symm p) ainl₂) nainl₁ theorem perm_app_left {l₁ l₂ : list A} (t₁ : list A) : l₁ ~ l₂ → (l₁++t₁) ~ (l₂++t₁) := assume p, perm.induction_on p !perm.refl (λ x l₁ l₂ p₁ r₁, skip x r₁) (λ x y l, !swap) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, trans r₁ r₂) theorem perm_app_right (l : list A) {t₁ t₂ : list A} : t₁ ~ t₂ → (l++t₁) ~ (l++t₂) := list.induction_on l (λ p, p) (λ x xs r p, skip x (r p)) theorem perm_app [congr] {l₁ l₂ t₁ t₂ : list A} : l₁ ~ l₂ → t₁ ~ t₂ → (l₁++t₁) ~ (l₂++t₂) := assume p₁ p₂, trans (perm_app_left t₁ p₁) (perm_app_right l₂ p₂) theorem perm_app_cons (a : A) {h₁ h₂ t₁ t₂ : list A} : h₁ ~ h₂ → t₁ ~ t₂ → (h₁ ++ (a::t₁)) ~ (h₂ ++ (a::t₂)) := assume p₁ p₂, perm_app p₁ (skip a p₂) theorem perm_cons_app (a : A) : ∀ (l : list A), (a::l) ~ (l ++ [a]) | [] := !perm.refl | (x::xs) := calc a::x::xs ~ x::a::xs : swap x a xs ... ~ x::(xs++[a]) : skip x (perm_cons_app xs) theorem perm_cons_app_simp [simp] (a : A) : ∀ (l : list A), (l ++ [a]) ~ (a::l) := take l, perm.symm !perm_cons_app theorem perm_app_comm [simp] {l₁ l₂ : list A} : (l₁++l₂) ~ (l₂++l₁) := list.induction_on l₁ (by rewrite [append_nil_right, append_nil_left]) (λ a t r, calc a::(t++l₂) ~ a::(l₂++t) : skip a r ... ~ l₂++t++[a] : perm_cons_app ... = l₂++(t++[a]) : append.assoc ... ~ l₂++(a::t) : perm_app_right l₂ (perm.symm (perm_cons_app a t))) theorem length_eq_length_of_perm {l₁ l₂ : list A} : l₁ ~ l₂ → length l₁ = length l₂ := assume p, perm.induction_on p rfl (λ x l₁ l₂ p r, by rewrite [*length_cons, r]) (λ x y l, by rewrite *length_cons) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, eq.trans r₁ r₂) theorem eq_singleton_of_perm_inv (a : A) {l : list A} : [a] ~ l → l = [a] := have gen : ∀ l₂, perm l₂ l → l₂ = [a] → l = [a], from take l₂, assume p, perm.induction_on p (λ e, e) (λ x l₁ l₂ p r e, begin injection e with e₁ e₂, rewrite [e₁, e₂ at p], have h₁ : l₂ = [], from eq_nil_of_perm_nil p, substvars end) (λ x y l e, by injection e; contradiction) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂ e, r₂ (r₁ e)), assume p, gen [a] p rfl theorem eq_singleton_of_perm (a b : A) : [a] ~ [b] → a = b := assume p, begin injection eq_singleton_of_perm_inv a p with e₁, rewrite e₁ end theorem perm_rev : ∀ (l : list A), l ~ (reverse l) | [] := nil | (x::xs) := calc x::xs ~ xs++[x] : perm_cons_app x xs ... ~ reverse xs ++ [x] : perm_app_left [x] (perm_rev xs) ... = reverse (x::xs) : by rewrite [reverse_cons, concat_eq_append] theorem perm_rev_simp [simp] : ∀ (l : list A), (reverse l) ~ l := take l, perm.symm (perm_rev l) theorem perm_middle (a : A) (l₁ l₂ : list A) : (a::l₁)++l₂ ~ l₁++(a::l₂) := calc (a::l₁) ++ l₂ = a::(l₁++l₂) : rfl ... ~ l₁++l₂++[a] : perm_cons_app ... = l₁++(l₂++[a]) : append.assoc ... ~ l₁++(a::l₂) : perm_app_right l₁ (perm.symm (perm_cons_app a l₂)) theorem perm_middle_simp [simp] (a : A) (l₁ l₂ : list A) : l₁++(a::l₂) ~ (a::l₁)++l₂ := perm.symm !perm_middle theorem perm_cons_app_cons {l l₁ l₂ : list A} (a : A) : l ~ l₁++l₂ → a::l ~ l₁++(a::l₂) := assume p, calc a::l ~ l++[a] : perm_cons_app ... ~ l₁++l₂++[a] : perm_app_left [a] p ... = l₁++(l₂++[a]) : append.assoc ... ~ l₁++(a::l₂) : perm_app_right l₁ (perm.symm (perm_cons_app a l₂)) open decidable theorem perm_erase [decidable_eq A] {a : A} : ∀ {l : list A}, a ∈ l → l ~ a::(erase a l) | [] h := absurd h !not_mem_nil | (x::t) h := by_cases (assume aeqx : a = x, by rewrite [aeqx, erase_cons_head]) (assume naeqx : a ≠ x, have aint : a ∈ t, from mem_of_ne_of_mem naeqx h, have aux : t ~ a :: erase a t, from perm_erase aint, calc x::t ~ x::a::(erase a t) : skip x aux ... ~ a::x::(erase a t) : swap ... = a::(erase a (x::t)) : by rewrite [!erase_cons_tail naeqx]) theorem erase_perm_erase_of_perm [congr] [decidable_eq A] (a : A) {l₁ l₂ : list A} : l₁ ~ l₂ → erase a l₁ ~ erase a l₂ := assume p, perm.induction_on p nil (λ x t₁ t₂ p r, by_cases (assume aeqx : a = x, by rewrite [aeqx, *erase_cons_head]; exact p) (assume naeqx : a ≠ x, by rewrite [*erase_cons_tail _ naeqx]; exact (skip x r))) (λ x y l, by_cases (assume aeqx : a = x, by_cases (assume aeqy : a = y, by rewrite [-aeqx, -aeqy]) (assume naeqy : a ≠ y, by rewrite [-aeqx, erase_cons_tail _ naeqy, *erase_cons_head])) (assume naeqx : a ≠ x, by_cases (assume aeqy : a = y, by rewrite [-aeqy, erase_cons_tail _ naeqx, *erase_cons_head]) (assume naeqy : a ≠ y, by rewrite[erase_cons_tail _ naeqx, *erase_cons_tail _ naeqy, erase_cons_tail _ naeqx]; exact !swap))) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, trans r₁ r₂) theorem perm_induction_on {P : list A → list A → Prop} {l₁ l₂ : list A} (p : l₁ ~ l₂) (h₁ : P [] []) (h₂ : ∀ x l₁ l₂, l₁ ~ l₂ → P l₁ l₂ → P (x::l₁) (x::l₂)) (h₃ : ∀ x y l₁ l₂, l₁ ~ l₂ → P l₁ l₂ → P (y::x::l₁) (x::y::l₂)) (h₄ : ∀ l₁ l₂ l₃, l₁ ~ l₂ → l₂ ~ l₃ → P l₁ l₂ → P l₂ l₃ → P l₁ l₃) : P l₁ l₂ := have P_refl : ∀ l, P l l | [] := h₁ | (x::xs) := h₂ x xs xs !perm.refl (P_refl xs), perm.induction_on p h₁ h₂ (λ x y l, h₃ x y l l !perm.refl !P_refl) h₄ theorem xswap {l₁ l₂ : list A} (x y : A) : l₁ ~ l₂ → x::y::l₁ ~ y::x::l₂ := assume p, calc x::y::l₁ ~ y::x::l₁ : swap ... ~ y::x::l₂ : skip y (skip x p) theorem perm_map [congr] (f : A → B) {l₁ l₂ : list A} : l₁ ~ l₂ → map f l₁ ~ map f l₂ := assume p, perm_induction_on p nil (λ x l₁ l₂ p r, skip (f x) r) (λ x y l₁ l₂ p r, xswap (f y) (f x) r) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, trans r₁ r₂) lemma perm_of_qeq {a : A} {l₁ l₂ : list A} : l₁≈a|l₂ → l₁~a::l₂ := assume q, qeq.induction_on q (λ h, !perm.refl) (λ b t₁ t₂ q₁ r₁, calc b::t₂ ~ b::a::t₁ : skip b r₁ ... ~ a::b::t₁ : swap) /- permutation is decidable if A has decidable equality -/ section dec open decidable variable [Ha : decidable_eq A] include Ha definition decidable_perm_aux : ∀ (n : nat) (l₁ l₂ : list A), length l₁ = n → length l₂ = n → decidable (l₁ ~ l₂) | 0 l₁ l₂ H₁ H₂ := have l₁n : l₁ = [], from eq_nil_of_length_eq_zero H₁, have l₂n : l₂ = [], from eq_nil_of_length_eq_zero H₂, by rewrite [l₁n, l₂n]; exact (inl perm.nil) | (n+1) (x::t₁) l₂ H₁ H₂ := by_cases (assume xinl₂ : x ∈ l₂, let t₂ : list A := erase x l₂ in have len_t₁ : length t₁ = n, begin injection H₁ with e, exact e end, have length t₂ = pred (length l₂), from length_erase_of_mem xinl₂, have length t₂ = n, by rewrite [this, H₂], match decidable_perm_aux n t₁ t₂ len_t₁ this with | inl p := inl (calc x::t₁ ~ x::(erase x l₂) : skip x p ... ~ l₂ : perm_erase xinl₂) | inr np := inr (λ p : x::t₁ ~ l₂, have erase x (x::t₁) ~ erase x l₂, from erase_perm_erase_of_perm x p, have t₁ ~ erase x l₂, by rewrite [erase_cons_head at this]; exact this, absurd this np) end) (assume nxinl₂ : x ∉ l₂, inr (λ p : x::t₁ ~ l₂, absurd (mem_perm p !mem_cons) nxinl₂)) definition decidable_perm [instance] : ∀ (l₁ l₂ : list A), decidable (l₁ ~ l₂) := λ l₁ l₂, by_cases (assume eql : length l₁ = length l₂, decidable_perm_aux (length l₂) l₁ l₂ eql rfl) (assume neql : length l₁ ≠ length l₂, inr (λ p : l₁ ~ l₂, absurd (length_eq_length_of_perm p) neql)) end dec -- Auxiliary theorem for performing cases-analysis on l₂. -- We use it to prove perm_inv_core. private theorem discr {P : Prop} {a b : A} {l₁ l₂ l₃ : list A} : a::l₁ = l₂++(b::l₃) → (l₂ = [] → a = b → l₁ = l₃ → P) → (∀ t, l₂ = a::t → l₁ = t++(b::l₃) → P) → P := match l₂ with | [] := λ e h₁ h₂, by injection e with e₁ e₂; exact h₁ rfl e₁ e₂ | h::t := λ e h₁ h₂, begin injection e with e₁ e₂, rewrite e₁ at h₂, exact h₂ t rfl e₂ end end -- Auxiliary theorem for performing cases-analysis on l₂. -- We use it to prove perm_inv_core. private theorem discr₂ {P : Prop} {a b c : A} {l₁ l₂ l₃ : list A} : a::b::l₁ = l₂++(c::l₃) → (l₂ = [] → l₃ = b::l₁ → a = c → P) → (l₂ = [a] → b = c → l₁ = l₃ → P) → (∀ t, l₂ = a::b::t → l₁ = t++(c::l₃) → P) → P := match l₂ with | [] := λ e H₁ H₂ H₃, begin injection e with a_eq_c b_l₁_eq_l₃, exact H₁ rfl (eq.symm b_l₁_eq_l₃) a_eq_c end | [h₁] := λ e H₁ H₂ H₃, begin rewrite [append_cons at e, append_nil_left at e], injection e with a_eq_h₁ b_eq_c l₁_eq_l₃, rewrite [a_eq_h₁ at H₂, b_eq_c at H₂, l₁_eq_l₃ at H₂], exact H₂ rfl rfl rfl end | h₁::h₂::t₂ := λ e H₁ H₂ H₃, begin injection e with a_eq_h₁ b_eq_h₂ l₁_eq, rewrite [a_eq_h₁ at H₃, b_eq_h₂ at H₃], exact H₃ t₂ rfl l₁_eq end end /- permutation inversion -/ theorem perm_inv_core {l₁ l₂ : list A} (p' : l₁ ~ l₂) : ∀ {a s₁ s₂}, l₁≈a|s₁ → l₂≈a|s₂ → s₁ ~ s₂ := perm_induction_on p' (λ a s₁ s₂ e₁ e₂, have innil : a ∈ [], from mem_head_of_qeq e₁, absurd innil !not_mem_nil) (λ x t₁ t₂ p (r : ∀{a s₁ s₂}, t₁≈a|s₁ → t₂≈a|s₂ → s₁ ~ s₂) a s₁ s₂ e₁ e₂, obtain (s₁₁ s₁₂ : list A) (C₁₁ : s₁ = s₁₁ ++ s₁₂) (C₁₂ : x::t₁ = s₁₁++(a::s₁₂)), from qeq_split e₁, obtain (s₂₁ s₂₂ : list A) (C₂₁ : s₂ = s₂₁ ++ s₂₂) (C₂₂ : x::t₂ = s₂₁++(a::s₂₂)), from qeq_split e₂, discr C₁₂ (λ (s₁₁_eq : s₁₁ = []) (x_eq_a : x = a) (t₁_eq : t₁ = s₁₂), have s₁_p : s₁ ~ t₂, from calc s₁ = s₁₁ ++ s₁₂ : C₁₁ ... = t₁ : by rewrite [-t₁_eq, s₁₁_eq, append_nil_left] ... ~ t₂ : p, discr C₂₂ (λ (s₂₁_eq : s₂₁ = []) (x_eq_a : x = a) (t₂_eq: t₂ = s₂₂), proof calc s₁ ~ t₂ : s₁_p ... = s₂₁ ++ s₂₂ : by rewrite [-t₂_eq, s₂₁_eq, append_nil_left] ... = s₂ : by rewrite C₂₁ qed) (λ (ts₂₁ : list A) (s₂₁_eq : s₂₁ = x::ts₂₁) (t₂_eq : t₂ = ts₂₁++(a::s₂₂)), proof calc s₁ ~ t₂ : s₁_p ... = ts₂₁++(a::s₂₂) : t₂_eq ... ~ (a::ts₂₁)++s₂₂ : !perm_middle ... = s₂₁ ++ s₂₂ : by rewrite [-x_eq_a, -s₂₁_eq] ... = s₂ : by rewrite C₂₁ qed)) (λ (ts₁₁ : list A) (s₁₁_eq : s₁₁ = x::ts₁₁) (t₁_eq : t₁ = ts₁₁++(a::s₁₂)), have t₁_qeq : t₁ ≈ a|(ts₁₁++s₁₂), by rewrite t₁_eq; exact !qeq_app, have s₁_eq : s₁ = x::(ts₁₁++s₁₂), from calc s₁ = s₁₁ ++ s₁₂ : C₁₁ ... = x::(ts₁₁++ s₁₂) : by rewrite s₁₁_eq, discr C₂₂ (λ (s₂₁_eq : s₂₁ = []) (x_eq_a : x = a) (t₂_eq: t₂ = s₂₂), proof calc s₁ = a::(ts₁₁++s₁₂) : by rewrite [s₁_eq, x_eq_a] ... ~ ts₁₁++(a::s₁₂) : !perm_middle ... = t₁ : t₁_eq ... ~ t₂ : p ... = s₂ : by rewrite [t₂_eq, C₂₁, s₂₁_eq, append_nil_left] qed) (λ (ts₂₁ : list A) (s₂₁_eq : s₂₁ = x::ts₂₁) (t₂_eq : t₂ = ts₂₁++(a::s₂₂)), have t₂_qeq : t₂ ≈ a|(ts₂₁++s₂₂), by rewrite t₂_eq; exact !qeq_app, proof calc s₁ = x::(ts₁₁++s₁₂) : s₁_eq ... ~ x::(ts₂₁++s₂₂) : skip x (r t₁_qeq t₂_qeq) ... = s₂ : by rewrite [-append_cons, -s₂₁_eq, C₂₁] qed))) (λ x y t₁ t₂ p (r : ∀{a s₁ s₂}, t₁≈a|s₁ → t₂≈a|s₂ → s₁ ~ s₂) a s₁ s₂ e₁ e₂, obtain (s₁₁ s₁₂ : list A) (C₁₁ : s₁ = s₁₁ ++ s₁₂) (C₁₂ : y::x::t₁ = s₁₁++(a::s₁₂)), from qeq_split e₁, obtain (s₂₁ s₂₂ : list A) (C₂₁ : s₂ = s₂₁ ++ s₂₂) (C₂₂ : x::y::t₂ = s₂₁++(a::s₂₂)), from qeq_split e₂, discr₂ C₁₂ (λ (s₁₁_eq : s₁₁ = []) (s₁₂_eq : s₁₂ = x::t₁) (y_eq_a : y = a), have s₁_p : s₁ ~ x::t₂, from calc s₁ = s₁₁ ++ s₁₂ : C₁₁ ... = x::t₁ : by rewrite [s₁₂_eq, s₁₁_eq, append_nil_left] ... ~ x::t₂ : skip x p, discr₂ C₂₂ (λ (s₂₁_eq : s₂₁ = []) (s₂₂_eq : s₂₂ = y::t₂) (x_eq_a : x = a), proof calc s₁ ~ x::t₂ : s₁_p ... = s₂₁ ++ s₂₂ : by rewrite [x_eq_a, -y_eq_a, -s₂₂_eq, s₂₁_eq, append_nil_left] ... = s₂ : by rewrite C₂₁ qed) (λ (s₂₁_eq : s₂₁ = [x]) (y_eq_a : y = a) (t₂_eq : t₂ = s₂₂), proof calc s₁ ~ x::t₂ : s₁_p ... = s₂₁ ++ s₂₂ : by rewrite [t₂_eq, s₂₁_eq, append_cons] ... = s₂ : by rewrite C₂₁ qed) (λ (ts₂₁ : list A) (s₂₁_eq : s₂₁ = x::y::ts₂₁) (t₂_eq : t₂ = ts₂₁++(a::s₂₂)), proof calc s₁ ~ x::t₂ : s₁_p ... = x::(ts₂₁++(y::s₂₂)) : by rewrite [t₂_eq, -y_eq_a] ... ~ x::y::(ts₂₁++s₂₂) : skip x !perm_middle ... = s₂₁ ++ s₂₂ : by rewrite [s₂₁_eq, append_cons] ... = s₂ : by rewrite C₂₁ qed)) (λ (s₁₁_eq : s₁₁ = [y]) (x_eq_a : x = a) (t₁_eq : t₁ = s₁₂), have s₁_p : s₁ ~ y::t₂, from calc s₁ = y::t₁ : by rewrite [C₁₁, s₁₁_eq, t₁_eq] ... ~ y::t₂ : skip y p, discr₂ C₂₂ (λ (s₂₁_eq : s₂₁ = []) (s₂₂_eq : s₂₂ = y::t₂) (x_eq_a : x = a), proof calc s₁ ~ y::t₂ : s₁_p ... = s₂₁ ++ s₂₂ : by rewrite [s₂₁_eq, s₂₂_eq] ... = s₂ : by rewrite C₂₁ qed) (λ (s₂₁_eq : s₂₁ = [x]) (y_eq_a : y = a) (t₂_eq : t₂ = s₂₂), proof calc s₁ ~ y::t₂ : s₁_p ... = s₂₁ ++ s₂₂ : by rewrite [s₂₁_eq, t₂_eq, y_eq_a, -x_eq_a] ... = s₂ : by rewrite C₂₁ qed) (λ (ts₂₁ : list A) (s₂₁_eq : s₂₁ = x::y::ts₂₁) (t₂_eq : t₂ = ts₂₁++(a::s₂₂)), proof calc s₁ ~ y::t₂ : s₁_p ... = y::(ts₂₁++(x::s₂₂)) : by rewrite [t₂_eq, -x_eq_a] ... ~ y::x::(ts₂₁++s₂₂) : skip y !perm_middle ... ~ x::y::(ts₂₁++s₂₂) : swap ... = s₂₁ ++ s₂₂ : by rewrite [s₂₁_eq] ... = s₂ : by rewrite C₂₁ qed)) (λ (ts₁₁ : list A) (s₁₁_eq : s₁₁ = y::x::ts₁₁) (t₁_eq : t₁ = ts₁₁++(a::s₁₂)), have s₁_eq : s₁ = y::x::(ts₁₁++s₁₂), by rewrite [C₁₁, s₁₁_eq], discr₂ C₂₂ (λ (s₂₁_eq : s₂₁ = []) (s₂₂_eq : s₂₂ = y::t₂) (x_eq_a : x = a), proof calc s₁ = y::a::(ts₁₁++s₁₂) : by rewrite [s₁_eq, x_eq_a] ... ~ y::(ts₁₁++(a::s₁₂)) : skip y !perm_middle ... = y::t₁ : by rewrite t₁_eq ... ~ y::t₂ : skip y p ... = s₂₁ ++ s₂₂ : by rewrite [s₂₁_eq, s₂₂_eq] ... = s₂ : by rewrite C₂₁ qed) (λ (s₂₁_eq : s₂₁ = [x]) (y_eq_a : y = a) (t₂_eq : t₂ = s₂₂), proof calc s₁ = y::x::(ts₁₁++s₁₂) : by rewrite s₁_eq ... ~ x::y::(ts₁₁++s₁₂) : swap ... = x::a::(ts₁₁++s₁₂) : by rewrite y_eq_a ... ~ x::(ts₁₁++(a::s₁₂)) : skip x !perm_middle ... = x::t₁ : by rewrite t₁_eq ... ~ x::t₂ : skip x p ... = s₂₁ ++ s₂₂ : by rewrite [t₂_eq, s₂₁_eq] ... = s₂ : by rewrite C₂₁ qed) (λ (ts₂₁ : list A) (s₂₁_eq : s₂₁ = x::y::ts₂₁) (t₂_eq : t₂ = ts₂₁++(a::s₂₂)), have t₁_qeq : t₁ ≈ a|(ts₁₁++s₁₂), by rewrite t₁_eq; exact !qeq_app, have t₂_qeq : t₂ ≈ a|(ts₂₁++s₂₂), by rewrite t₂_eq; exact !qeq_app, have p_aux : ts₁₁++s₁₂ ~ ts₂₁++s₂₂, from r t₁_qeq t₂_qeq, proof calc s₁ = y::x::(ts₁₁++s₁₂) : by rewrite s₁_eq ... ~ y::x::(ts₂₁++s₂₂) : skip y (skip x p_aux) ... ~ x::y::(ts₂₁++s₂₂) : swap ... = s₂₁ ++ s₂₂ : by rewrite s₂₁_eq ... = s₂ : by rewrite C₂₁ qed))) (λ t₁ t₂ t₃ p₁ p₂ (r₁ : ∀{a s₁ s₂}, t₁ ≈ a|s₁ → t₂≈a|s₂ → s₁ ~ s₂) (r₂ : ∀{a s₁ s₂}, t₂ ≈ a|s₁ → t₃≈a|s₂ → s₁ ~ s₂) a s₁ s₂ e₁ e₂, have a ∈ t₁, from mem_head_of_qeq e₁, have a ∈ t₂, from mem_perm p₁ this, obtain (t₂' : list A) (e₂' : t₂≈a|t₂'), from qeq_of_mem this, calc s₁ ~ t₂' : r₁ e₁ e₂' ... ~ s₂ : r₂ e₂' e₂) theorem perm_cons_inv {a : A} {l₁ l₂ : list A} : a::l₁ ~ a::l₂ → l₁ ~ l₂ := assume p, perm_inv_core p (qeq.qhead a l₁) (qeq.qhead a l₂) theorem perm_app_inv {a : A} {l₁ l₂ l₃ l₄ : list A} : l₁++(a::l₂) ~ l₃++(a::l₄) → l₁++l₂ ~ l₃++l₄ := assume p : l₁++(a::l₂) ~ l₃++(a::l₄), have p' : a::(l₁++l₂) ~ a::(l₃++l₄), from calc a::(l₁++l₂) ~ l₁++(a::l₂) : perm_middle ... ~ l₃++(a::l₄) : p ... ~ a::(l₃++l₄) : perm.symm (!perm_middle), perm_cons_inv p' section foldl variables {f : B → A → B} {l₁ l₂ : list A} variable rcomm : right_commutative f include rcomm theorem foldl_eq_of_perm : l₁ ~ l₂ → ∀ b, foldl f b l₁ = foldl f b l₂ := assume p, perm_induction_on p (λ b, by rewrite *foldl_nil) (λ x t₁ t₂ p r b, calc foldl f b (x::t₁) = foldl f (f b x) t₁ : foldl_cons ... = foldl f (f b x) t₂ : r (f b x) ... = foldl f b (x::t₂) : foldl_cons) (λ x y t₁ t₂ p r b, calc foldl f b (y :: x :: t₁) = foldl f (f (f b y) x) t₁ : by rewrite foldl_cons ... = foldl f (f (f b x) y) t₁ : by rewrite rcomm ... = foldl f (f (f b x) y) t₂ : r (f (f b x) y) ... = foldl f b (x :: y :: t₂) : by rewrite foldl_cons) (λ t₁ t₂ t₃ p₁ p₂ r₁ r₂ b, eq.trans (r₁ b) (r₂ b)) end foldl section foldr variables {f : A → B → B} {l₁ l₂ : list A} variable lcomm : left_commutative f include lcomm theorem foldr_eq_of_perm : l₁ ~ l₂ → ∀ b, foldr f b l₁ = foldr f b l₂ := assume p, perm_induction_on p (λ b, by rewrite *foldl_nil) (λ x t₁ t₂ p r b, calc foldr f b (x::t₁) = f x (foldr f b t₁) : foldr_cons ... = f x (foldr f b t₂) : by rewrite [r b] ... = foldr f b (x::t₂) : foldr_cons) (λ x y t₁ t₂ p r b, calc foldr f b (y :: x :: t₁) = f y (f x (foldr f b t₁)) : by rewrite foldr_cons ... = f x (f y (foldr f b t₁)) : by rewrite lcomm ... = f x (f y (foldr f b t₂)) : by rewrite [r b] ... = foldr f b (x :: y :: t₂) : by rewrite foldr_cons) (λ t₁ t₂ t₃ p₁ p₂ r₁ r₂ a, eq.trans (r₁ a) (r₂ a)) end foldr theorem perm_erase_dup_of_perm [congr] [H : decidable_eq A] {l₁ l₂ : list A} : l₁ ~ l₂ → erase_dup l₁ ~ erase_dup l₂ := assume p, perm_induction_on p nil (λ x t₁ t₂ p r, by_cases (λ xint₁ : x ∈ t₁, have xint₂ : x ∈ t₂, from mem_of_mem_erase_dup (mem_perm r (mem_erase_dup xint₁)), by rewrite [erase_dup_cons_of_mem xint₁, erase_dup_cons_of_mem xint₂]; exact r) (λ nxint₁ : x ∉ t₁, have nxint₂ : x ∉ t₂, from assume xint₂ : x ∈ t₂, absurd (mem_of_mem_erase_dup (mem_perm (perm.symm r) (mem_erase_dup xint₂))) nxint₁, by rewrite [erase_dup_cons_of_not_mem nxint₂, erase_dup_cons_of_not_mem nxint₁]; exact (skip x r))) (λ y x t₁ t₂ p r, by_cases (λ xinyt₁ : x ∈ y::t₁, by_cases (λ yint₁ : y ∈ t₁, have yint₂ : y ∈ t₂, from mem_of_mem_erase_dup (mem_perm r (mem_erase_dup yint₁)), have yinxt₂ : y ∈ x::t₂, from or.inr (yint₂), or.elim (eq_or_mem_of_mem_cons xinyt₁) (λ xeqy : x = y, have xint₂ : x ∈ t₂, by rewrite [-xeqy at yint₂]; exact yint₂, begin rewrite [erase_dup_cons_of_mem xinyt₁, erase_dup_cons_of_mem yinxt₂, erase_dup_cons_of_mem yint₁, erase_dup_cons_of_mem xint₂], exact r end) (λ xint₁ : x ∈ t₁, have xint₂ : x ∈ t₂, from mem_of_mem_erase_dup (mem_perm r (mem_erase_dup xint₁)), begin rewrite [erase_dup_cons_of_mem xinyt₁, erase_dup_cons_of_mem yinxt₂, erase_dup_cons_of_mem yint₁, erase_dup_cons_of_mem xint₂], exact r end)) (λ nyint₁ : y ∉ t₁, have nyint₂ : y ∉ t₂, from assume yint₂ : y ∈ t₂, absurd (mem_of_mem_erase_dup (mem_perm (perm.symm r) (mem_erase_dup yint₂))) nyint₁, by_cases (λ xeqy : x = y, have nxint₂ : x ∉ t₂, by rewrite [-xeqy at nyint₂]; exact nyint₂, have yinxt₂ : y ∈ x::t₂, by rewrite [xeqy]; exact !mem_cons, begin rewrite [erase_dup_cons_of_mem xinyt₁, erase_dup_cons_of_mem yinxt₂, erase_dup_cons_of_not_mem nyint₁, erase_dup_cons_of_not_mem nxint₂, xeqy], exact skip y r end) (λ xney : x ≠ y, have x ∈ t₁, from or_resolve_right xinyt₁ xney, have x ∈ t₂, from mem_of_mem_erase_dup (mem_perm r (mem_erase_dup this)), have y ∉ x::t₂, from suppose y ∈ x::t₂, or.elim (eq_or_mem_of_mem_cons this) (λ h, absurd h (ne.symm xney)) (λ h, absurd h nyint₂), begin rewrite [erase_dup_cons_of_mem xinyt₁, erase_dup_cons_of_not_mem `y ∉ x::t₂`, erase_dup_cons_of_not_mem nyint₁, erase_dup_cons_of_mem `x ∈ t₂`], exact skip y r end))) (λ nxinyt₁ : x ∉ y::t₁, have xney : x ≠ y, from ne_of_not_mem_cons nxinyt₁, have nxint₁ : x ∉ t₁, from not_mem_of_not_mem_cons nxinyt₁, have nxint₂ : x ∉ t₂, from assume xint₂ : x ∈ t₂, absurd (mem_of_mem_erase_dup (mem_perm (perm.symm r) (mem_erase_dup xint₂))) nxint₁, by_cases (λ yint₁ : y ∈ t₁, have yinxt₂ : y ∈ x::t₂, from or.inr (mem_of_mem_erase_dup (mem_perm r (mem_erase_dup yint₁))), begin rewrite [erase_dup_cons_of_not_mem nxinyt₁, erase_dup_cons_of_mem yinxt₂, erase_dup_cons_of_mem yint₁, erase_dup_cons_of_not_mem nxint₂], exact skip x r end) (λ nyint₁ : y ∉ t₁, have nyinxt₂ : y ∉ x::t₂, from assume yinxt₂ : y ∈ x::t₂, or.elim (eq_or_mem_of_mem_cons yinxt₂) (λ h, absurd h (ne.symm xney)) (λ h, absurd (mem_of_mem_erase_dup (mem_perm (perm.symm r) (mem_erase_dup h))) nyint₁), begin rewrite [erase_dup_cons_of_not_mem nxinyt₁, erase_dup_cons_of_not_mem nyinxt₂, erase_dup_cons_of_not_mem nyint₁, erase_dup_cons_of_not_mem nxint₂], exact xswap x y r end))) (λ t₁ t₂ t₃ p₁ p₂ r₁ r₂, trans r₁ r₂) section perm_union variable [H : decidable_eq A] include H theorem perm_union_left {l₁ l₂ : list A} (t₁ : list A) : l₁ ~ l₂ → (union l₁ t₁) ~ (union l₂ t₁) := assume p, perm.induction_on p (by rewrite [nil_union]) (λ x l₁ l₂ p₁ r₁, by_cases (λ xint₁ : x ∈ t₁, by rewrite [*union_cons_of_mem _ xint₁]; exact r₁) (λ nxint₁ : x ∉ t₁, by rewrite [*union_cons_of_not_mem _ nxint₁]; exact (skip _ r₁))) (λ x y l, by_cases (λ yint : y ∈ t₁, by_cases (λ xint : x ∈ t₁, by rewrite [*union_cons_of_mem _ xint, *union_cons_of_mem _ yint, *union_cons_of_mem _ xint]) (λ nxint : x ∉ t₁, by rewrite [*union_cons_of_mem _ yint, *union_cons_of_not_mem _ nxint, union_cons_of_mem _ yint])) (λ nyint : y ∉ t₁, by_cases (λ xint : x ∈ t₁, by rewrite [*union_cons_of_mem _ xint, *union_cons_of_not_mem _ nyint, union_cons_of_mem _ xint]) (λ nxint : x ∉ t₁, by rewrite [*union_cons_of_not_mem _ nxint, *union_cons_of_not_mem _ nyint, union_cons_of_not_mem _ nxint]; exact !swap))) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, trans r₁ r₂) theorem perm_union_right (l : list A) {t₁ t₂ : list A} : t₁ ~ t₂ → (union l t₁) ~ (union l t₂) := list.induction_on l (λ p, by rewrite [*union_nil]; exact p) (λ x xs r p, by_cases (λ xint₁ : x ∈ t₁, have xint₂ : x ∈ t₂, from mem_perm p xint₁, by rewrite [union_cons_of_mem _ xint₁, union_cons_of_mem _ xint₂]; exact (r p)) (λ nxint₁ : x ∉ t₁, have nxint₂ : x ∉ t₂, from not_mem_perm p nxint₁, by rewrite [union_cons_of_not_mem _ nxint₁, union_cons_of_not_mem _ nxint₂]; exact (skip _ (r p)))) theorem perm_union [congr] {l₁ l₂ t₁ t₂ : list A} : l₁ ~ l₂ → t₁ ~ t₂ → (union l₁ t₁) ~ (union l₂ t₂) := assume p₁ p₂, trans (perm_union_left t₁ p₁) (perm_union_right l₂ p₂) end perm_union section perm_insert variable [H : decidable_eq A] include H theorem perm_insert [congr] (a : A) {l₁ l₂ : list A} : l₁ ~ l₂ → (insert a l₁) ~ (insert a l₂) := assume p, by_cases (λ ainl₁ : a ∈ l₁, have ainl₂ : a ∈ l₂, from mem_perm p ainl₁, by rewrite [insert_eq_of_mem ainl₁, insert_eq_of_mem ainl₂]; exact p) (λ nainl₁ : a ∉ l₁, have nainl₂ : a ∉ l₂, from not_mem_perm p nainl₁, by rewrite [insert_eq_of_not_mem nainl₁, insert_eq_of_not_mem nainl₂]; exact (skip _ p)) end perm_insert section perm_inter variable [H : decidable_eq A] include H theorem perm_inter_left {l₁ l₂ : list A} (t₁ : list A) : l₁ ~ l₂ → (inter l₁ t₁) ~ (inter l₂ t₁) := assume p, perm.induction_on p !perm.refl (λ x l₁ l₂ p₁ r₁, by_cases (λ xint₁ : x ∈ t₁, by rewrite [*inter_cons_of_mem _ xint₁]; exact (skip x r₁)) (λ nxint₁ : x ∉ t₁, by rewrite [*inter_cons_of_not_mem _ nxint₁]; exact r₁)) (λ x y l, by_cases (λ yint : y ∈ t₁, by_cases (λ xint : x ∈ t₁, by rewrite [*inter_cons_of_mem _ xint, *inter_cons_of_mem _ yint, *inter_cons_of_mem _ xint]; exact !swap) (λ nxint : x ∉ t₁, by rewrite [*inter_cons_of_mem _ yint, *inter_cons_of_not_mem _ nxint, inter_cons_of_mem _ yint])) (λ nyint : y ∉ t₁, by_cases (λ xint : x ∈ t₁, by rewrite [*inter_cons_of_mem _ xint, *inter_cons_of_not_mem _ nyint, inter_cons_of_mem _ xint]) (λ nxint : x ∉ t₁, by rewrite [*inter_cons_of_not_mem _ nxint, *inter_cons_of_not_mem _ nyint, inter_cons_of_not_mem _ nxint]))) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, trans r₁ r₂) theorem perm_inter_right (l : list A) {t₁ t₂ : list A} : t₁ ~ t₂ → (inter l t₁) ~ (inter l t₂) := list.induction_on l (λ p, by rewrite [*inter_nil]) (λ x xs r p, by_cases (λ xint₁ : x ∈ t₁, have xint₂ : x ∈ t₂, from mem_perm p xint₁, by rewrite [inter_cons_of_mem _ xint₁, inter_cons_of_mem _ xint₂]; exact (skip _ (r p))) (λ nxint₁ : x ∉ t₁, have nxint₂ : x ∉ t₂, from not_mem_perm p nxint₁, by rewrite [inter_cons_of_not_mem _ nxint₁, inter_cons_of_not_mem _ nxint₂]; exact (r p))) theorem perm_inter [congr] {l₁ l₂ t₁ t₂ : list A} : l₁ ~ l₂ → t₁ ~ t₂ → (inter l₁ t₁) ~ (inter l₂ t₂) := assume p₁ p₂, trans (perm_inter_left t₁ p₁) (perm_inter_right l₂ p₂) end perm_inter /- extensionality -/ section ext open eq.ops theorem perm_ext : ∀ {l₁ l₂ : list A}, nodup l₁ → nodup l₂ → (∀a, a ∈ l₁ ↔ a ∈ l₂) → l₁ ~ l₂ | [] [] d₁ d₂ e := !perm.nil | [] (a₂::t₂) d₁ d₂ e := absurd (iff.mpr (e a₂) !mem_cons) (not_mem_nil a₂) | (a₁::t₁) [] d₁ d₂ e := absurd (iff.mp (e a₁) !mem_cons) (not_mem_nil a₁) | (a₁::t₁) (a₂::t₂) d₁ d₂ e := have a₁ ∈ a₂::t₂, from iff.mp (e a₁) !mem_cons, have ∃ s₁ s₂, a₂::t₂ = s₁++(a₁::s₂), from mem_split this, obtain (s₁ s₂ : list A) (t₂_eq : a₂::t₂ = s₁++(a₁::s₂)), from this, have dt₂' : nodup (a₁::(s₁++s₂)), from nodup_head (by rewrite [t₂_eq at d₂]; exact d₂), have eqv : ∀a, a ∈ t₁ ↔ a ∈ s₁++s₂, from take a, iff.intro (suppose a ∈ t₁, have a ∈ a₂::t₂, from iff.mp (e a) (mem_cons_of_mem _ this), have a ∈ s₁++(a₁::s₂), by rewrite [t₂_eq at this]; exact this, or.elim (mem_or_mem_of_mem_append this) (suppose a ∈ s₁, mem_append_left s₂ this) (suppose a ∈ a₁::s₂, or.elim (eq_or_mem_of_mem_cons this) (suppose a = a₁, have a₁ ∉ t₁, from not_mem_of_nodup_cons d₁, by subst a; contradiction) (suppose a ∈ s₂, mem_append_right s₁ this))) (suppose a ∈ s₁ ++ s₂, or.elim (mem_or_mem_of_mem_append this) (suppose a ∈ s₁, have a ∈ a₂::t₂, from by rewrite [t₂_eq]; exact (mem_append_left _ this), have a ∈ a₁::t₁, from iff.mpr (e a) this, or.elim (eq_or_mem_of_mem_cons this) (suppose a = a₁, have a₁ ∉ s₁++s₂, from not_mem_of_nodup_cons dt₂', have a₁ ∉ s₁, from not_mem_of_not_mem_append_left this, by subst a; contradiction) (suppose a ∈ t₁, this)) (suppose a ∈ s₂, have a ∈ a₂::t₂, from by rewrite [t₂_eq]; exact (mem_append_right _ (mem_cons_of_mem _ this)), have a ∈ a₁::t₁, from iff.mpr (e a) this, or.elim (eq_or_mem_of_mem_cons this) (suppose a = a₁, have a₁ ∉ s₁++s₂, from not_mem_of_nodup_cons dt₂', have a₁ ∉ s₂, from not_mem_of_not_mem_append_right this, by subst a; contradiction) (suppose a ∈ t₁, this))), have ds₁s₂ : nodup (s₁++s₂), from nodup_of_nodup_cons dt₂', have nodup t₁, from nodup_of_nodup_cons d₁, calc a₁::t₁ ~ a₁::(s₁++s₂) : skip a₁ (perm_ext this ds₁s₂ eqv) ... ~ s₁++(a₁::s₂) : !perm_middle ... = a₂::t₂ : by rewrite t₂_eq end ext theorem nodup_of_perm_of_nodup {l₁ l₂ : list A} : l₁ ~ l₂ → nodup l₁ → nodup l₂ := assume h, perm.induction_on h (λ h, h) (λ a l₁ l₂ p ih nd, have nodup l₁, from nodup_of_nodup_cons nd, have nodup l₂, from ih this, have a ∉ l₁, from not_mem_of_nodup_cons nd, have a ∉ l₂, from suppose a ∈ l₂, absurd (mem_perm (perm.symm p) this) `a ∉ l₁`, nodup_cons `a ∉ l₂` `nodup l₂`) (λ x y l₁ nd, have nodup (x::l₁), from nodup_of_nodup_cons nd, have nodup l₁, from nodup_of_nodup_cons this, have x ∉ l₁, from not_mem_of_nodup_cons `nodup (x::l₁)`, have y ∉ x::l₁, from not_mem_of_nodup_cons nd, have x ≠ y, from suppose x = y, begin subst x, exact absurd !mem_cons `y ∉ y::l₁` end, have y ∉ l₁, from not_mem_of_not_mem_cons `y ∉ x::l₁`, have x ∉ y::l₁, from not_mem_cons_of_ne_of_not_mem `x ≠ y` `x ∉ l₁`, have nodup (y::l₁), from nodup_cons `y ∉ l₁` `nodup l₁`, show nodup (x::y::l₁), from nodup_cons `x ∉ y::l₁` `nodup (y::l₁)`) (λ l₁ l₂ l₃ p₁ p₂ ih₁ ih₂ nd, ih₂ (ih₁ nd)) /- product -/ section product theorem perm_product_left {l₁ l₂ : list A} (t₁ : list B) : l₁ ~ l₂ → (product l₁ t₁) ~ (product l₂ t₁) := assume p : l₁ ~ l₂, perm.induction_on p !perm.refl (λ x l₁ l₂ p r, perm_app (perm.refl (map _ t₁)) r) (λ x y l, let m₁ := map (λ b, (x, b)) t₁ in let m₂ := map (λ b, (y, b)) t₁ in let c := product l t₁ in calc m₂ ++ (m₁ ++ c) = (m₂ ++ m₁) ++ c : by rewrite append.assoc ... ~ (m₁ ++ m₂) ++ c : perm_app !perm_app_comm !perm.refl ... = m₁ ++ (m₂ ++ c) : by rewrite append.assoc) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, trans r₁ r₂) theorem perm_product_right (l : list A) {t₁ t₂ : list B} : t₁ ~ t₂ → (product l t₁) ~ (product l t₂) := list.induction_on l (λ p, by rewrite [*nil_product]) (λ a t r p, perm_app (perm_map _ p) (r p)) theorem perm_product [congr] {l₁ l₂ : list A} {t₁ t₂ : list B} : l₁ ~ l₂ → t₁ ~ t₂ → (product l₁ t₁) ~ (product l₂ t₂) := assume p₁ p₂, trans (perm_product_left t₁ p₁) (perm_product_right l₂ p₂) end product /- filter -/ theorem perm_filter [congr] {l₁ l₂ : list A} {p : A → Prop} [decidable_pred p] : l₁ ~ l₂ → (filter p l₁) ~ (filter p l₂) := assume u, perm.induction_on u perm.nil (take x l₁' l₂', assume u' : l₁' ~ l₂', assume u'' : filter p l₁' ~ filter p l₂', decidable.by_cases (suppose p x, by rewrite [*filter_cons_of_pos _ this]; apply perm.skip; apply u'') (suppose ¬ p x, by rewrite [*filter_cons_of_neg _ this]; apply u'')) (take x y l, decidable.by_cases (assume H1 : p x, decidable.by_cases (assume H2 : p y, begin rewrite [filter_cons_of_pos _ H1, *filter_cons_of_pos _ H2, filter_cons_of_pos _ H1], apply perm.swap end) (assume H2 : ¬ p y, by rewrite [filter_cons_of_pos _ H1, *filter_cons_of_neg _ H2, filter_cons_of_pos _ H1])) (assume H1 : ¬ p x, decidable.by_cases (assume H2 : p y, by rewrite [filter_cons_of_neg _ H1, *filter_cons_of_pos _ H2, filter_cons_of_neg _ H1]) (assume H2 : ¬ p y, by rewrite [filter_cons_of_neg _ H1, *filter_cons_of_neg _ H2, filter_cons_of_neg _ H1]))) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, trans r₁ r₂) section count variable [decA : decidable_eq A] include decA theorem count_eq_of_perm {l₁ l₂ : list A} : l₁ ~ l₂ → ∀ a, count a l₁ = count a l₂ := suppose l₁ ~ l₂, perm.induction_on this (λ a, rfl) (λ x l₁ l₂ p h a, by rewrite [*count_cons, *h a]) (λ x y l a, by_cases (suppose a = x, by_cases (suppose a = y, begin subst x, subst y end) (suppose a ≠ y, begin subst x, rewrite [count_cons_of_ne this, *count_cons_eq, count_cons_of_ne this] end)) (suppose a ≠ x, by_cases (suppose a = y, begin subst y, rewrite [count_cons_of_ne this, *count_cons_eq, count_cons_of_ne this] end) (suppose a ≠ y, begin rewrite [count_cons_of_ne `a≠x`, *count_cons_of_ne `a≠y`, count_cons_of_ne `a≠x`] end))) (λ l₁ l₂ l₃ p₁ p₂ h₁ h₂ a, eq.trans (h₁ a) (h₂ a)) end count end perm
6c4e5c784c55b336c80402fa446b61f67435fbc4
572fb32b6f5b7c2bf26921ffa2abea054cce881a
/src/week_1/Part_A_logic.lean
d987f38a7623ab932662788f588fd863c4bb8159
[ "Apache-2.0" ]
permissive
kgeorgiy/lean-formalising-mathematics
2deb30756d5a54bee1cfa64873e86f641c59c7dc
73429a8ded68f641c896b6ba9342450d4d3ae50f
refs/heads/master
1,683,029,640,682
1,621,403,041,000
1,621,403,041,000
367,790,347
0
0
null
null
null
null
UTF-8
Lean
false
false
11,626
lean
-- We import all of Lean's standard tactics import tactic /-! # Logic We will develop the basic theory of following five basic logical symbols * `→` ("implies" -- type with `\l`) * `¬` ("not" -- type with `\not` or `\n`) * `∧` ("and" -- type with `\and` or `\an`) * `↔` ("iff" -- type with `\iff` or `\lr`) * `∨` ("or" -- type with `\or` or `\v` # Tactics you will need to know * `intro` * `exact` * `apply` * `rw` * `cases` * `split` * `left` * `right` See `README.md` in `src/week_1` for an explanation of what these tactics do. Note that there are plenty of other tactics, and indeed once you've "got the hang of it" you might want to try tactics such as `cc`, `tauto` and its variant `tauto!`, `finish`, and `library_search`. # What to do The `example`s are to demonstrate things to you. They sometimes use tactics you don't know. You can look at them but you don't need to touch them. The `theorem`s and `lemma`s are things which have no proof. You need to change the `sorry`s into proofs which Lean will accept. This paragraph is a comment, by the way. One-line comments are preceded with `--`. -/ -- We work in a "namespace". All this means is that whenever it -- looks like we've defined a new theorem called `id`, its full -- name is `xena.id`. Which is good because `id` is already -- defined in Lean. namespace xena -- Throughout this namespace, P Q and R will be arbitrary (variable) -- true-false statements. variables (P Q R : Prop) /-! ## implies (→) To prove the theorems in this section, you will need to know about the tactics `intro`, `apply` and `exact`. You might also like the `assumption` tactic. -/ /-- Every proposition implies itself. -/ theorem id : P → P := λ P, P /- Note that → isn't associative! Try working out `false → (false → false) and (false → false) → false -/ example : (false → (false → false)) ↔ true := by simp example : ((false → false) → false) ↔ false := by simp -- TODO -- example : (false → (false → false)) ↔ true := begin -- split, -- { intro f, }, -- {sorry} -- end -- in Lean, `P → Q → R` is _defined_ to mean `P → (Q → R)` -- Here's a proof of what I just said. example : (P → Q → R) ↔ (P → (Q → R)) := by refl -- example : (P → Q → R) ↔ (P → (Q → R)) := rfl theorem imp_intro : P → Q → P := λ P Q, P /-- If we know `P`, and we also know `P → Q`, we can deduce `Q`. -/ lemma modus_ponens : P → (P → Q) → Q := λ P PQ, PQ P /-- implication is transitive -/ lemma imp_trans : (P → Q) → (Q → R) → (P → R) := λ PQ QR P, QR (PQ P) -- This one is a "relative modus ponens" -- in the -- presence of P, if Q -> R and Q then R. lemma forall_imp : (P → Q → R) → (P → Q) → (P → R) := λ PQR PQ P, PQR P (PQ P) /- ### not `not P`, with notation `¬ P`, is *defined* to mean `P → false` in Lean, i.e., the proposition that P implies false. You can easily check with a truth table that P → false and ¬ P are equivalent. We develop a basic interface for `¬`. -/ -- I'll prove this one for you theorem not_iff_imp_false : ¬ P ↔ (P → false) := by refl theorem not_not_intro : P → ¬ (¬ P) := λ hP hnP, hnP hP -- Here is a funny alternative proof! Can you work out how it works? example : P → ¬ (¬ P) := begin apply modus_ponens, end -- Here is a proof which does not use tactics at all, but uses lambda calculus. -- It is called a "term mode" proof. We will not be discussing term mode -- much in this course. It is a cool way to do basic logic proofs, but -- it does not scale well in practice. example : P → ¬ (¬ P) := λ hP hnP, hnP hP -- This is "modus tollens". Some mathematicians think of it as -- "proof by contradiction". theorem modus_tollens : (P → Q) → (¬ Q → ¬ P) := λ hPQ hnQ hP, hnQ (hPQ hP) -- This one cannot be proved using constructive mathematics! -- You _have_ to use a tactic like `by_contra` (or, if you're happy -- to cheat, the full "truth table" tactic `tauto!`. -- Try it without using these, and you'll get stuck! theorem double_negation_elimination : ¬ (¬ P) → P := begin intro hnnP, by_contra, exact hnnP h, end /-! ### and The hypothesis `hPaQ : P ∧ Q` in Lean, is equivalent to hypotheses `hP : P` and `hQ : Q`. If you have `hPaQ` as a hypothesis, and you want to get to `hP` and `hQ`, you can use the `cases` tactic. If you have `⊢ P ∧ Q` as a goal, and want to turn the goal into two goals `⊢ P` and `⊢ Q`, then use the `split` tactic. Note that after `split` it's good etiquette to use braces e.g. example (hP : P) (hQ : Q) : P ∧ Q := begin split, { exact hP }, { exact hQ } end but for this sort of stuff I think principled indentation is OK ``` example (hP : P) (hQ : Q) : P ∧ Q := begin split, exact hP, exact hQ end ``` -/ theorem and.elim_left : P ∧ Q → P := λ ⟨hP, _⟩, hP theorem and.elim_right : P ∧ Q → Q := λ ⟨_, hQ⟩, hQ theorem and.intro : P → Q → P ∧ Q := λ hP hQ, ⟨hP, hQ⟩ -- theorem flip : ∀ (P Q R : Prop), (P → Q → R) → (Q → P → R) := λ hPQR hQ hP, hPQR hP hQ /-- the eliminator for `∧` -/ theorem and.elim : P ∧ Q → (P → Q → R) → R := λ ⟨hP, hQ⟩ hPQR, hPQR hP hQ /-- The recursor for `∧` -/ theorem and.rec : (P → Q → R) → P ∧ Q → R := λ hPQR ⟨hP, hQ⟩, hPQR hP hQ /-- `∧` is symmetric -/ theorem and.symm : P ∧ Q → Q ∧ P := λ ⟨hP, hQ⟩, ⟨hQ, hP⟩ /-- `∧` is transitive -/ theorem and.trans : (P ∧ Q) → (Q ∧ R) → (P ∧ R) := λ ⟨hP, _⟩ ⟨_, hR⟩, ⟨hP, hR⟩ /- Recall that the convention for the implies sign → is that it is _right associative_, by which I mean that `P → Q → R` means `P → (Q → R)` by definition. Now note that if `P` implies `Q → R` then this means that `P` and `Q` together, imply `R`, so `P → Q → R` is logically equivalent to `(P ∧ Q) → R`. We proved that `P → Q → R` implied `(P ∧ Q) → R`; this was `and.rec`. Let's go the other way. -/ lemma imp_imp_of_and_imp : ((P ∧ Q) → R) → (P → Q → R) := λ hPaQiR hP hQ, hPaQiR ⟨hP, hQ⟩ /-! ### iff The basic theory of `iff`. In Lean, to prove `P ∧ Q` you have to prove `P` and `Q`. Similarly, to prove `P ↔ Q` in Lean, you have to prove `P → Q` and `Q → P`. Just like `∧`, you can uses `cases h` if you have a hypothesis `h : P ↔ Q`, and `split` if you have a goal `⊢ P ↔ Q`. -/ /-- `P ↔ P` is true for all propositions `P`, i.e. `↔` is reflexive. -/ theorem iff.refl : P ↔ P := ⟨(λ hP, hP), (λ hP, hP)⟩ -- refl tactic also works, it knows that `=` and `↔` are reflexive. example : P ↔ P := by refl /-- `↔` is symmetric -/ theorem iff.symm : (P ↔ Q) → (Q ↔ P) := λ ⟨hPQ, hQP⟩, ⟨hQP, hPQ⟩ -- NB there is quite a devious proof of this using `rw`. /-- `↔` is commutative -/ theorem iff.comm : (P ↔ Q) ↔ (Q ↔ P) := ⟨λ ⟨hPQ, hQP⟩ , ⟨hQP, hPQ⟩, λ ⟨hQP, hPQ⟩ , ⟨hPQ, hQP⟩ ⟩ -- without rw or cc this is painful! /-- `↔` is transitive -/ theorem iff.trans : (P ↔ Q) → (Q ↔ R) → (P ↔ R) := λ ⟨hPQ, hQP⟩ ⟨hQR, hRQ⟩, ⟨λ hP, hQR (hPQ hP), λ hR, hQP (hRQ hR)⟩ -- This can be done constructively, but it's hard. You'll need to know -- about the `have` tactic to do it. Alternatively the truth table -- tactic `tauto!` will do it. theorem iff.boss : ¬ (P ↔ ¬ P) := begin rintro ⟨hPimpnP, hnPimpP⟩, have hnP : ¬ P := λ hP, hPimpnP hP hP, exact hnP (hnPimpP hnP) end -- Now we have iff we can go back to and. /-! ### ↔ and ∧ -/ /-- `∧` is commutative -/ theorem and.comm : P ∧ Q ↔ Q ∧ P := ⟨and.symm P Q, and.symm Q P⟩ -- Note that ∧ is "right associative" in Lean, which means -- that `P ∧ Q ∧ R` is _defined to mean_ `P ∧ (Q ∧ R)`. -- Associativity can hence be written like this: /-- `∧` is associative -/ theorem and_assoc : ((P ∧ Q) ∧ R) ↔ (P ∧ Q ∧ R) := ⟨ λ ⟨⟨hP, hQ⟩, hR⟩, ⟨hP, ⟨hQ, hR⟩⟩, λ ⟨hP, ⟨hQ, hR⟩⟩, ⟨⟨hP, hQ⟩, hR⟩ ⟩ /-! ## Or `P ∨ Q` is true when at least one of `P` and `Q` are true. Here is how to work with `∨` in Lean. If you have a hypothesis `hPoQ : P ∨ Q` then you can break into the two cases `hP : P` and `hQ : Q` using `cases hPoQ with hP hQ` If you have a _goal_ of the form `⊢ P ∨ Q` then you need to decide whether you're going to prove `P` or `Q`. If you want to prove `P` then use the `left` tactic, and if you want to prove `Q` then use the `right` tactic. -/ -- recall that P, Q, R are Propositions. We'll need S for this one. variable (S : Prop) -- You will need to use the `left` tactic for this one. theorem or.intro_left : P → P ∨ Q := λ hP, or.inl hP theorem or.intro_right : Q → P ∨ Q := λ hP, or.inr hP /-- the eliminator for `∨`. -/ theorem or.elim : P ∨ Q → (P → R) → (Q → R) → R := λ hPoQ, or.dcases_on hPoQ (λ hP hPR hQR, hPR hP) (λ hQ hPR hQR, hQR hQ) /-- `∨` is symmetric -/ theorem or.symm : P ∨ Q → Q ∨ P := λ hPoQ, or.dcases_on hPoQ or.inr or.inl /-- `∨` is commutative -/ theorem or.comm : P ∨ Q ↔ Q ∨ P := ⟨or.symm P Q, or.symm Q P⟩ --variables {P Q R : Prop} theorem or.cases {P Q R : Prop} : (P → R) → (Q → R) → P ∨ Q → R := λ hPiR hQiR hPoQ, or.cases_on hPoQ hPiR hQiR /-- `∨` is associative -/ theorem or.assoc : (P ∨ Q) ∨ R ↔ P ∨ Q ∨ R := ⟨ or.cases (or.cases or.inl (or.inr ∘ or.inl)) (or.inr ∘ or.inr), or.cases (or.inl ∘ or.inl) (or.cases (or.inl ∘ or.inr) or.inr) ⟩ /-! ### More about → and ∨ -/ theorem or.imp : (P → R) → (Q → S) → P ∨ Q → R ∨ S := λ hPiR hQiS, or.cases (or.inl ∘ hPiR) (or.inr ∘ hQiS) theorem or.imp_left : (P → Q) → P ∨ R → Q ∨ R := λ hPiQ, or.cases (or.inl ∘ hPiQ) or.inr theorem or.imp_right : (P → Q) → R ∨ P → R ∨ Q := λ hPiQ, or.cases or.inl (or.inr ∘ hPiQ) theorem or.left_comm : P ∨ Q ∨ R ↔ Q ∨ P ∨ R := begin rewrite [← or.assoc, ← or.assoc, or.comm P Q], end /-- the recursor for `∨` -/ theorem or.rec : (P → R) → (Q → R) → P ∨ Q → R := λ hPiR hQiR hPoR, or.elim P Q R hPoR hPiR hQiR theorem or_congr : (P ↔ R) → (Q ↔ S) → (P ∨ Q ↔ R ∨ S) := begin intros hPiffR hQiffS, rewrite [hPiffR, hQiffS], end /-! ### true and false `true` is a true-false statement, which can be proved with the `trivial` tactic. `false` is a true-false statment which can only be proved if you manage to find a contradiction within your assumptions. If you manage to end up with a hypothesis `h : false` then there's quite a funny way to proceed, which we now explain. If you have `h : P ∧ Q` then you can uses `cases h with hP hQ` to split into two cases. If you have `h : false` then what do you think happens if we do `cases h`? Hint: how many cases are there? -/ /-- eliminator for `false` -/ theorem false.elim : false → P := false.dcases_on (λ _, P) theorem and_true_iff : P ∧ true ↔ P := ⟨λ ⟨hP, _⟩, hP, λ hP, ⟨hP, by trivial⟩⟩ theorem or_false_iff : P ∨ false ↔ P := ⟨ or.cases (λ hP, hP) (false.elim _), or.inl ⟩ -- false.elim is handy for this one theorem or.resolve_left : P ∨ Q → ¬P → Q := λ hPoQ hnP, or.dcases_on hPoQ (false.elim _ ∘ hnP) (λ hQ, hQ) -- this one you can't do constructively theorem or_iff_not_imp_left : P ∨ Q ↔ ¬P → Q := ⟨ or.resolve_left P Q, λ hnPiQ, begin by_cases hP : P, {exact or.inl hP}, {exact or.inr (hnPiQ hP)} end ⟩ end xena
655b366535ce4b33644ac2c1e984d688ac1805c4
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/480.hlean
fac44c49e04ca7b5e988efee64878b732de714c9
[ "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
hlean
open is_trunc check is_contr.mk
ec8483047be45600c1f7abda62e2e135fea1d932
63abd62053d479eae5abf4951554e1064a4c45b4
/src/data/list/range.lean
db1b84bbe0e187d04ab6f473ff3c3677ae49ac92
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
9,015
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 open nat namespace list /- iota and range(') -/ universe u 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 (le_add_right _ _), have l : m = s ∨ s + 1 ≤ m ↔ s ≤ m, by simpa only [eq_comm] using (@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 (by exact λ a b c, lt_trans)).1 (chain_lt_range' s n) theorem nodup_range' (s n : ℕ) : nodup (range' s n) := (pairwise_lt_range' s n).imp (λ a b, ne_of_lt) @[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 [← nat.sub_add_cancel h, ← range'_append]; apply sublist_append_left⟩ theorem range'_subset_right {s m n : ℕ} : range' s m ⊆ range' s n ↔ m ≤ n := ⟨λ h, le_of_not_lt $ λ hn, lt_irrefl (s+n) $ (mem_range'.1 $ h $ mem_range'.2 ⟨le_add_right _ _, nat.add_lt_add_left hn s⟩).2, λ h, (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 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_concat (n : ℕ) : range (succ n) = range n ++ [n] := by simp only [range_eq_range', range'_concat, zero_add] 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, nat.sub_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`. -/ 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_pmap (λ _ _ _ _, fin.veq_of_eq) (nodup_range _) @[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] @[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_concat, 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'] @[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)] 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; from nodup_pmap (λ _ _ _ _ H, fin.veq_of_eq $ hf H) (nodup_range n) end list
908f9cec8d8e47140003314abaa00077b518b837
54d7e71c3616d331b2ec3845d31deb08f3ff1dea
/library/init/algebra/order.lean
eabb7f6f815e06ff7356dbac815179a437448ce1
[ "Apache-2.0" ]
permissive
pachugupta/lean
6f3305c4292288311cc4ab4550060b17d49ffb1d
0d02136a09ac4cf27b5c88361750e38e1f485a1a
refs/heads/master
1,611,110,653,606
1,493,130,117,000
1,493,167,649,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,796
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.logic /- 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 variables {α : Type u} class weak_order (α : Type u) extends has_le α := (le_refl : ∀ a : α, a ≤ a) (le_trans : ∀ a b c : α, a ≤ b → b ≤ c → a ≤ c) (le_antisymm : ∀ a b : α, a ≤ b → b ≤ a → a = b) class linear_weak_order (α : Type u) extends weak_order α := (le_total : ∀ a b : α, a ≤ b ∨ b ≤ a) class strict_order (α : Type u) extends has_lt α := (lt_irrefl : ∀ a : α, ¬ a < a) (lt_trans : ∀ a b c : α, a < b → b < c → a < c) /- structures with a weak and a strict order -/ class order_pair (α : Type u) extends weak_order α, has_lt α := (le_of_lt : ∀ a b : α, a < b → a ≤ b) (lt_of_lt_of_le : ∀ a b c : α, a < b → b ≤ c → a < c) (lt_of_le_of_lt : ∀ a b c : α, a ≤ b → b < c → a < c) (lt_irrefl : ∀ a : α, ¬ a < a) class strong_order_pair (α : Type u) extends weak_order α, has_lt α := (le_iff_lt_or_eq : ∀ a b : α, a ≤ b ↔ a < b ∨ a = b) (lt_irrefl : ∀ a : α, ¬ a < a) class linear_order_pair (α : Type u) extends order_pair α, linear_weak_order α class linear_strong_order_pair (α : Type u) extends strong_order_pair α, linear_weak_order α @[refl] lemma le_refl [weak_order α] : ∀ a : α, a ≤ a := weak_order.le_refl @[trans] lemma le_trans [weak_order α] : ∀ {a b c : α}, a ≤ b → b ≤ c → a ≤ c := weak_order.le_trans lemma le_antisymm [weak_order α] : ∀ {a b : α}, a ≤ b → b ≤ a → a = b := weak_order.le_antisymm lemma le_of_eq [weak_order α] {a b : α} : a = b → a ≤ b := λ h, h ▸ le_refl a @[trans] lemma ge_trans [weak_order α] : ∀ {a b c : α}, a ≥ b → b ≥ c → a ≥ c := λ a b c h₁ h₂, le_trans h₂ h₁ lemma le_total [linear_weak_order α] : ∀ a b : α, a ≤ b ∨ b ≤ a := linear_weak_order.le_total lemma le_of_not_ge [linear_weak_order α] {a b : α} : ¬ a ≥ b → a ≤ b := or.resolve_left (le_total b a) lemma le_of_not_le [linear_weak_order α] {a b : α} : ¬ a ≤ b → b ≤ a := or.resolve_left (le_total a b) lemma lt_irrefl [strict_order α] : ∀ a : α, ¬ a < a := strict_order.lt_irrefl lemma gt_irrefl [strict_order α] : ∀ a : α, ¬ a > a := lt_irrefl @[trans] lemma lt_trans [strict_order α] : ∀ {a b c : α}, a < b → b < c → a < c := strict_order.lt_trans def lt.trans := @lt_trans @[trans] lemma gt_trans [strict_order α] : ∀ {a b c : α}, a > b → b > c → a > c := λ a b c h₁ h₂, lt_trans h₂ h₁ def gt.trans := @gt_trans lemma ne_of_lt [strict_order α] {a b : α} (h : a < b) : a ≠ b := λ he, absurd h (he ▸ lt_irrefl a) lemma ne_of_gt [strict_order α] {a b : α} (h : a > b) : a ≠ b := λ he, absurd h (he ▸ lt_irrefl a) lemma lt_asymm [strict_order α] {a b : α} (h : a < b) : ¬ b < a := λ h1 : b < a, lt_irrefl a (lt_trans h h1) lemma not_lt_of_gt [strict_order α] {a b : α} (h : a > b) : ¬ a < b := lt_asymm h lemma le_of_lt [order_pair α] : ∀ {a b : α}, a < b → a ≤ b := order_pair.le_of_lt @[trans] lemma lt_of_lt_of_le [order_pair α] : ∀ {a b c : α}, a < b → b ≤ c → a < c := order_pair.lt_of_lt_of_le @[trans] lemma lt_of_le_of_lt [order_pair α] : ∀ {a b c : α}, a ≤ b → b < c → a < c := order_pair.lt_of_le_of_lt @[trans] lemma gt_of_gt_of_ge [order_pair α] {a b c : α} (h₁ : a > b) (h₂ : b ≥ c) : a > c := lt_of_le_of_lt h₂ h₁ @[trans] lemma gt_of_ge_of_gt [order_pair α] {a b c : α} (h₁ : a ≥ b) (h₂ : b > c) : a > c := lt_of_lt_of_le h₂ h₁ instance order_pair.to_strict_order [s : order_pair α] : strict_order α := { s with lt_irrefl := order_pair.lt_irrefl, lt_trans := λ a b c h₁ h₂, lt_of_lt_of_le h₁ (le_of_lt h₂) } lemma not_le_of_gt [order_pair α] {a b : α} (h : a > b) : ¬ a ≤ b := λ h₁, lt_irrefl b (lt_of_lt_of_le h h₁) lemma not_lt_of_ge [order_pair α] {a b : α} (h : a ≥ b) : ¬ a < b := λ h₁, lt_irrefl b (lt_of_le_of_lt h h₁) lemma le_iff_lt_or_eq [strong_order_pair α] : ∀ {a b : α}, a ≤ b ↔ a < b ∨ a = b := strong_order_pair.le_iff_lt_or_eq lemma lt_or_eq_of_le [strong_order_pair α] : ∀ {a b : α}, a ≤ b → a < b ∨ a = b := λ a b h, iff.mp le_iff_lt_or_eq h lemma le_of_lt_or_eq [strong_order_pair α] : ∀ {a b : α}, (a < b ∨ a = b) → a ≤ b := λ a b h, iff.mpr le_iff_lt_or_eq h lemma lt_of_le_of_ne [strong_order_pair α] {a b : α} : a ≤ b → a ≠ b → a < b := λ h₁ h₂, or.resolve_right (lt_or_eq_of_le h₁) h₂ private lemma lt_irrefl' [strong_order_pair α] : ∀ a : α, ¬ a < a := strong_order_pair.lt_irrefl private lemma le_of_lt' [strong_order_pair α] ⦃a b : α⦄ (h : a < b) : a ≤ b := le_of_lt_or_eq (or.inl h) private lemma lt_of_lt_of_le' [strong_order_pair α] (a b c : α) (h₁ : a < b) (h₂ : b ≤ c) : a < c := have a ≤ c, from le_trans (le_of_lt' h₁) h₂, or.elim (lt_or_eq_of_le this) (λ h : a < c, h) (λ h : a = c, have b ≤ a, from h.symm ▸ h₂, have a = b, from le_antisymm (le_of_lt' h₁) this, absurd h₁ (this ▸ lt_irrefl' a)) private lemma lt_of_le_of_lt' [strong_order_pair α] (a b c : α) (h₁ : a ≤ b) (h₂ : b < c) : a < c := have a ≤ c, from le_trans h₁ (le_of_lt' h₂), or.elim (lt_or_eq_of_le this) (λ h : a < c, h) (λ h : a = c, have c ≤ b, from h ▸ h₁, have c = b, from le_antisymm this (le_of_lt' h₂), absurd h₂ (this ▸ lt_irrefl' c)) instance strong_order_pair.to_order_pair [s : strong_order_pair α] : order_pair α := { s with lt_irrefl := lt_irrefl', le_of_lt := le_of_lt', lt_of_le_of_lt := lt_of_le_of_lt', lt_of_lt_of_le := lt_of_lt_of_le'} instance linear_strong_order_pair.to_linear_order_pair [s : linear_strong_order_pair α] : linear_order_pair α := { s with lt_irrefl := lt_irrefl', le_of_lt := le_of_lt', lt_of_le_of_lt := lt_of_le_of_lt', lt_of_lt_of_le := lt_of_lt_of_le'} lemma lt_trichotomy [linear_strong_order_pair α] (a b : α) : a < b ∨ a = b ∨ b < a := or.elim (le_total a b) (λ h : a ≤ b, or.elim (lt_or_eq_of_le h) (λ h : a < b, or.inl h) (λ h : a = b, or.inr (or.inl h))) (λ h : b ≤ a, or.elim (lt_or_eq_of_le h) (λ h : b < a, or.inr (or.inr h)) (λ h : b = a, or.inr (or.inl h.symm))) lemma le_of_not_gt [linear_strong_order_pair α] {a b : α} (h : ¬ a > b) : a ≤ b := match lt_trichotomy a b with | or.inl hlt := le_of_lt hlt | or.inr (or.inl heq) := heq ▸ le_refl a | or.inr (or.inr hgt) := absurd hgt h end lemma lt_of_not_ge [linear_strong_order_pair α] {a b : α} (h : ¬ a ≥ b) : a < b := match lt_trichotomy a b with | or.inl hlt := hlt | or.inr (or.inl heq) := absurd (heq ▸ le_refl a : a ≥ b) h | or.inr (or.inr hgt) := absurd (le_of_lt hgt) h end lemma lt_or_ge [linear_strong_order_pair α] (a b : α) : a < b ∨ a ≥ b := match lt_trichotomy a b with | or.inl hlt := or.inl hlt | or.inr (or.inl heq) := or.inr (heq ▸ le_refl a) | or.inr (or.inr hgt) := or.inr (le_of_lt hgt) end lemma le_or_gt [linear_strong_order_pair α] (a b : α) : a ≤ b ∨ a > b := or.swap (lt_or_ge b a) lemma lt_or_gt_of_ne [linear_strong_order_pair α] {a b : α} (h : a ≠ b) : a < b ∨ a > b := match lt_trichotomy a b with | or.inl hlt := or.inl hlt | or.inr (or.inl heq) := absurd heq h | or.inr (or.inr hgt) := or.inr hgt end lemma lt_iff_not_ge [linear_strong_order_pair α] (x y : α) : x < y ↔ ¬ x ≥ y := ⟨not_le_of_gt, lt_of_not_ge⟩ /- The following lemma can be used when defining a decidable_linear_order instance, and the concrete structure does not have its own definition for decidable le -/ def decidable_le_of_decidable_lt [linear_strong_order_pair α] [∀ a b : α, decidable (a < b)] (a b : α) : decidable (a ≤ b) := if h₁ : a < b then is_true (le_of_lt h₁) else if h₂ : b < a then is_false (not_le_of_gt h₂) else is_true (le_of_not_gt h₂) /- The following lemma can be used when defining a decidable_linear_order instance, and the concrete structure does not have its own definition for decidable le -/ def decidable_eq_of_decidable_lt [linear_strong_order_pair α] [∀ a b : α, decidable (a < b)] (a b : α) : decidable (a = b) := match decidable_le_of_decidable_lt a b with | is_true h₁ := match decidable_le_of_decidable_lt b a with | is_true h₂ := is_true (le_antisymm h₁ h₂) | is_false h₂ := is_false (λ he : a = b, h₂ (he ▸ le_refl a)) end | is_false h₁ := is_false (λ he : a = b, h₁ (he ▸ le_refl a)) end class decidable_linear_order (α : Type u) extends linear_strong_order_pair α := (decidable_lt : decidable_rel lt) (decidable_le : decidable_rel le) -- TODO(Leo): add default value using decidable_le_of_decidable_lt (decidable_eq : decidable_eq α) -- TODO(Leo): add default value using decidable_eq_of_decidable_lt instance [decidable_linear_order α] (a b : α) : decidable (a < b) := decidable_linear_order.decidable_lt α a b instance [decidable_linear_order α] (a b : α) : decidable (a ≤ b) := decidable_linear_order.decidable_le α a b instance [decidable_linear_order α] (a b : α) : decidable (a = b) := decidable_linear_order.decidable_eq α a b lemma eq_or_lt_of_not_lt [decidable_linear_order α] {a b : α} (h : ¬ a < b) : a = b ∨ b < a := if h₁ : a = b then or.inl h₁ else or.inr (lt_of_not_ge (λ hge, h (lt_of_le_of_ne hge h₁)))
071d729799d5d48cb6403bb2391d883468c3bfa1
56762daf61566a2baf390b5d77988c29c75187e3
/src/diffeomorph.lean
329248d754a658d5ce05778a5837cbc9de390738
[]
no_license
Nicknamen/lie_group
de173ce5f1ffccb945ba05dca23ff27daef0e3b4
e0d5c4f859654e3dea092702f1320c3c72a49983
refs/heads/master
1,674,937,428,196
1,607,213,423,000
1,607,213,423,000
275,196,635
0
0
null
null
null
null
UTF-8
Lean
false
false
2,264
lean
import .preamble_results variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {E' : Type*} [normed_group E'] [normed_space 𝕜 E'] {F : Type*} [normed_group F] [normed_space 𝕜 F] {F' : Type*} [normed_group F'] [normed_space 𝕜 F'] {H : Type*} [topological_space H] {H' : Type*} [topological_space H'] {G : Type*} [topological_space G] {G' : Type*} [topological_space G'] (I : model_with_corners 𝕜 E H) (I' : model_with_corners 𝕜 E' H') (J : model_with_corners 𝕜 F G) (J' : model_with_corners 𝕜 F' G') section diffeomorph variables (M : Type*) [topological_space M] [charted_space H M] [smooth_manifold_with_corners I M] (M' : Type*) [topological_space M'] [charted_space H' M'] [smooth_manifold_with_corners I' M'] (N : Type*) [topological_space N] [charted_space G N] [smooth_manifold_with_corners J N] (N' : Type*) [topological_space N'] [charted_space G' N'] [smooth_manifold_with_corners J' N'] (n : with_top ℕ) /-- α and β are homeomorph, also called topological isomoph -/ structure diffeomorph extends M ≃ M' := (times_cont_mdiff_to_fun : smooth I I' to_fun) (times_cont_mdiff_inv_fun : smooth I' I inv_fun) infix ` ≃ₘ `:50 := diffeomorph _ _ notation M ` ≃ₘ[` I `;` J `]` N := diffeomorph I J M N namespace diffeomorph instance : has_coe_to_fun (diffeomorph I I' M M') := ⟨λ _, M → M', λe, e.to_equiv⟩ lemma coe_eq_to_equiv (h : diffeomorph I I' M M') (x : M) : h x = h.to_equiv x := rfl /-- Identity map is a diffeomorphism. -/ protected def refl : M ≃ₘ[I; I] M := { smooth_to_fun := smooth_in_charts_id, smooth_inv_fun := smooth_in_charts_id, .. homeomorph.refl M } /-- Composition of two diffeomorphisms. -/ protected def trans (h₁ : diffeomorph I I' M M') (h₂ : diffeomorph I' J M' N) : M ≃ₘ[I | J] N := { smooth_to_fun := h₂.smooth_to_fun.comp h₁.smooth_to_fun, smooth_inv_fun := h₁.smooth_inv_fun.comp h₂.smooth_inv_fun, .. homeomorph.trans h₁.to_homeomorph h₂.to_homeomorph } /-- Inverse of a diffeomorphism. -/ protected def symm (h : M ≃ₘ[I | J] N) : N ≃ₘ[J | I] M := { smooth_to_fun := h.smooth_inv_fun, smooth_inv_fun := h.smooth_to_fun, .. h.to_homeomorph.symm } end diffeomorph end diffeomorph
0c7501fcff0740feea2d7f0e98c374ba0696280a
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/test/qpf.lean
b015723e205d61dac33b78bf4bb4bc3223180fd5
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
6,214
lean
import data.qpf.univariate.basic import control.bifunctor universes u variables {F : Type u → Type u} [functor F] namespace qpf section box variables (F) /-- apply a functor to a set of values. taken from [Basil Fürer, Andreas Lochbihler, Joshua Schneider, Dmitriy Traytel *Quotients of Bounded Natural Functors*][fuerer-lochbihler-schneider-traytel2020] henceforth referred to as the QBNF paper -/ def box {α} (A : set α) : set (F α) := { x | ∀ β (f g : α → β), (∀ a ∈ A, f a = g a) → f <$> x = g <$> x } variables {F} /-- Alternate notion of support set based on `box`. Taken from the QBNF paper -/ def supp' {α} (x : F α) : set α := ⋂ A ∈ { A : set α | x ∈ box F A}, A /-- Alternate notion of predicate lifting based on `box`. Taken from the QBNF paper -/ def liftp' {α} (x : F α) (p : α → Prop) : Prop := ∀ a ∈ supp' x, p a end box end qpf namespace ex /-- polynomial functor isomorph to `α × _` for some `α` -/ def prod.pfunctor (α : Type) : pfunctor := ⟨ α, λ _, unit ⟩ instance {α} : qpf (prod α) := { P := prod.pfunctor α, abs := λ β ⟨a,f⟩, (a, f ()), repr := λ β ⟨x,y⟩, ⟨x, λ _, y⟩, abs_repr := λ β ⟨x,y⟩, rfl, abs_map := λ β γ f ⟨a,g⟩, rfl } /-- example relation for products -/ def foo.R (α : Type) (x y : bool × α) : Prop := x.1 = y.1 ∧ (x.1 → x.2 = y.2) lemma equivalence_foo.R (α) : equivalence (foo.R α) := begin refine ⟨_,_,_⟩, { intro, exact ⟨rfl,λ _, rfl⟩ }, { intros x y h, refine ⟨h.1.symm, λ _, (h.2 _).symm⟩, rwa h.1 }, { rintros x y z ⟨ha,ha'⟩ ⟨hb,hb'⟩, refine ⟨ha.trans hb, λ hh, _⟩, refine (ha' hh).trans (hb' _), rwa ← ha } end /-- example of a qpf -/ def foo (α : Type) := quot $ foo.R α instance {α} [inhabited α] : inhabited (foo α) := ⟨ quot.mk _ (default _) ⟩ /-- functor operation of `foo` -/ def foo.map {α β} (f : α → β) (x : foo α) : foo β := quot.lift_on x (λ x : bool × α, quot.mk (foo.R β) $ f <$> x) (λ ⟨a₀,a₁⟩ ⟨b₀,b₁⟩ h, quot.sound ⟨h.1,λ h', show f a₁ = f b₁, from congr_arg f (h.2 h')⟩) instance : functor foo := { map := @foo.map } @[simp] lemma foo.map_mk {α β : Type} (f : α → β) (x : bool × α) : (f <$> quot.mk _ x : foo β) = quot.mk _ (f <$> x) := by simp [(<$>),foo.map] noncomputable instance qpf.foo : qpf foo := @qpf.quotient_qpf (prod bool) _ ex.prod.qpf foo _ (λ α, quot.mk _) (λ α, quot.out) (by simp) (by intros; simp) /-- constructor for `foo` -/ def foo.mk {α} (b : bool) (x : α) : foo α := quot.mk _ (b, x) @[simp] lemma foo.map_mk' {α β : Type} (f : α → β) (b : bool) (x : α) : f <$> foo.mk b x = foo.mk b (f x) := by simp only [foo.mk, foo.map_mk]; refl @[simp] lemma foo.map_tt {α : Type} (x y : α) : foo.mk tt x = foo.mk tt y ↔ x = y := by simp [foo.mk]; split; intro h; [replace h := quot.exact _ h, rw h]; rw relation.eqv_gen_iff_of_equivalence at h; [exact h.2 rfl, apply equivalence_foo.R] /-- consequence of original definition of `supp`. If there exists more than one value of type `α`, then the support of `foo.mk ff x` is empty -/ lemma supp_mk_ff₀ {α} (x y : α) (h : ¬ x = y) : functor.supp (foo.mk ff x) = {} := begin dsimp [functor.supp], ext z, simp, -- split; intro h, classical, by_cases x = z, { use (λ a, ¬ z = a), subst z, dsimp [functor.liftp], simp, refine ⟨foo.mk ff ⟨y,h⟩,_⟩, simp, apply quot.sound, simp [foo.R] }, { use (λ a, x = a), dsimp [functor.liftp], simp [h], use foo.mk ff ⟨x,rfl⟩, simp } end /-- consequence of original definition of `supp`. If there exists only one value of type `α`, then the support of `foo.mk ff x` contains that value -/ lemma supp_mk_ff₁ {α} (x : α) (h : ∀ z, x = z) : functor.supp (foo.mk ff x) = {x} := begin dsimp [functor.supp], ext y, simp, split; intro h', { apply @h' (= x), dsimp [functor.liftp], use foo.mk ff ⟨x,rfl⟩, refl }, { introv hp, simp [functor.liftp] at hp, rcases hp with ⟨⟨z,z',hz⟩,hp⟩, simp at hp, convert hz, rw [h'], apply h }, end /-- Such a QPF is not uniform -/ lemma foo_not_uniform : ¬ @qpf.is_uniform foo _ qpf.foo := begin simp only [qpf.is_uniform, foo, qpf.foo, set.image_univ, not_forall, not_imp], existsi [bool,ff,ff,λ a : unit, tt,λ a : unit, ff], split, { apply quot.sound, simp [foo.R, qpf.abs, prod.qpf._match_1] }, { simp! only [set.range, set.ext_iff], simp only [not_exists, false_iff, bool.forall_bool, eq_self_iff_true, exists_false, not_true, and_self, set.mem_set_of_eq, iff_false], exact λ h, h () } end /-- intuitive consequence of original definition of `supp`. -/ lemma supp_mk_tt {α} (x : α) : functor.supp (foo.mk tt x) = {x} := begin dsimp [functor.supp], ext y, simp, split; intro h', { apply @h' (= x), dsimp [functor.liftp], use foo.mk tt ⟨x,rfl⟩, refl }, { introv hp, simp [functor.liftp] at hp, rcases hp with ⟨⟨z,z',hz⟩,hp⟩, simp at hp, replace hp := quot.exact _ hp, rw relation.eqv_gen_iff_of_equivalence (equivalence_foo.R _) at hp, rcases hp with ⟨⟨⟩,hp⟩, subst y, replace hp := hp rfl, cases hp, exact hz } end /-- simple consequence of the definition of `supp` from the QBNF paper -/ lemma supp_mk_ff' {α} (x : α) : qpf.supp' (foo.mk ff x) = {} := begin dsimp [qpf.supp'], ext, simp, dsimp [qpf.box], use ∅, simp [foo.mk], intros, apply quot.sound, dsimp [foo.R], split, refl, rintro ⟨ ⟩ end /-- simple consequence of the definition of `supp` from the QBNF paper -/ lemma supp_mk_tt' {α} (x : α) : qpf.supp' (foo.mk tt x) = {x} := begin dsimp [qpf.supp'], ext, simp, dsimp [qpf.box], split; intro h, { specialize h {x} _, { clear h, introv hfg, simp, rw hfg, simp }, { simp at h, assumption }, }, { introv hfg, subst x_1, classical, let f : α → α ⊕ bool := λ x, if x ∈ i then sum.inl x else sum.inr tt, let g : α → α ⊕ bool := λ x, if x ∈ i then sum.inl x else sum.inr ff, specialize hfg _ f g _, { intros, simp [*,f,g,if_pos] }, { simp [f,g] at hfg, split_ifs at hfg, assumption, cases hfg } } end end ex
f40a64ea3e5774f8cf7e4be01ad8420ec0b8f554
4727251e0cd73359b15b664c3170e5d754078599
/src/control/traversable/derive.lean
b15f65b7dc517e8b87f9a26512819e2663e9aedd
[ "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
17,031
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon Automation to construct `traversable` instances -/ import control.traversable.lemmas namespace tactic.interactive open tactic list monad functor meta def with_prefix : option name → name → name | none n := n | (some p) n := p ++ n /-- similar to `nested_traverse` but for `functor` -/ meta def nested_map (f v : expr) : expr → tactic expr | t := do t ← instantiate_mvars t, mcond (succeeds $ is_def_eq t v) (pure f) (if ¬ v.occurs (t.app_fn) then do cl ← mk_app ``functor [t.app_fn], _inst ← mk_instance cl, f' ← nested_map t.app_arg, mk_mapp ``functor.map [t.app_fn,_inst,none,none,f'] else fail format!"type {t} is not a functor with respect to variable {v}") /-- similar to `traverse_field` but for `functor` -/ meta def map_field (n : name) (cl f α β e : expr) : tactic expr := do t ← infer_type e >>= whnf, if t.get_app_fn.const_name = n then fail "recursive types not supported" else if α =ₐ e then pure β else if α.occurs t then do f' ← nested_map f α t, pure $ f' e else (is_def_eq t.app_fn cl >> mk_app ``comp.mk [e]) <|> pure e /-- similar to `traverse_constructor` but for `functor` -/ meta def map_constructor (c n : name) (f α β : expr) (args₀ : list expr) (args₁ : list (bool × expr)) (rec_call : list expr) : tactic expr := do g ← target, (_, args') ← mmap_accuml (λ (x : list expr) (y : bool × expr), if y.1 then pure (x.tail,x.head) else prod.mk rec_call <$> map_field n g.app_fn f α β y.2) rec_call args₁, constr ← mk_const c, let r := constr.mk_app (args₀ ++ args'), return r /-- derive the `map` definition of a `functor` -/ meta def mk_map (type : name) := do ls ← local_context, [α,β,f,x] ← tactic.intro_lst [`α,`β,`f,`x], et ← infer_type x, xs ← tactic.induction x, xs.mmap' (λ (x : name × list expr × list (name × expr)), do let (c,args,_) := x, (args,rec_call) ← args.mpartition $ λ e, (bnot ∘ β.occurs) <$> infer_type e, args₀ ← args.mmap $ λ a, do { b ← et.occurs <$> infer_type a, pure (b,a) }, map_constructor c type f α β (ls ++ [β]) args₀ rec_call >>= tactic.exact) meta def mk_mapp_aux' : expr → expr → list expr → tactic expr | fn (expr.pi n bi d b) (a::as) := do infer_type a >>= unify d, fn ← head_beta (fn a), t ← whnf (b.instantiate_var a), mk_mapp_aux' fn t as | fn _ _ := pure fn meta def mk_mapp' (fn : expr) (args : list expr) : tactic expr := do t ← infer_type fn >>= whnf, mk_mapp_aux' fn t args /-- derive the equations for a specific `map` definition -/ meta def derive_map_equations (pre : option name) (n : name) (vs : list expr) (tgt : expr) : tactic unit := do e ← get_env, ((e.constructors_of n).enum_from 1).mmap' $ λ ⟨i,c⟩, do { mk_meta_var tgt >>= set_goals ∘ pure, vs ← intro_lst $ vs.map expr.local_pp_name, [α,β,f] ← tactic.intro_lst [`α,`β,`f] >>= mmap instantiate_mvars, c' ← mk_mapp c $ vs.map some ++ [α], tgt' ← infer_type c' >>= pis vs, mk_meta_var tgt' >>= set_goals ∘ pure, vs ← tactic.intro_lst $ vs.map expr.local_pp_name, vs' ← tactic.intros, c' ← mk_mapp c $ vs.map some ++ [α], arg ← mk_mapp' c' vs', n_map ← mk_const (with_prefix pre n <.> "map"), let call_map := λ x, mk_mapp' n_map (vs ++ [α,β,f,x]), lhs ← call_map arg, args ← vs'.mmap $ λ a, do { t ← infer_type a, pure ((expr.const_name (expr.get_app_fn t) = n : bool),a) }, let rec_call := args.filter_map $ λ ⟨b, e⟩, guard b >> pure e, rec_call ← rec_call.mmap call_map, rhs ← map_constructor c n f α β (vs ++ [β]) args rec_call, monad.join $ unify <$> infer_type lhs <*> infer_type rhs, eqn ← mk_app ``eq [lhs,rhs], let ws := eqn.list_local_consts, eqn ← pis ws.reverse eqn, eqn ← instantiate_mvars eqn, (_,pr) ← solve_aux eqn (tactic.intros >> refine ``(rfl)), let eqn_n := (with_prefix pre n <.> "map" <.> "equations" <.> "_eqn").append_after i, pr ← instantiate_mvars pr, add_decl $ declaration.thm eqn_n eqn.collect_univ_params eqn (pure pr), return () }, set_goals [], return () meta def derive_functor (pre : option name) : tactic unit := do vs ← local_context, `(functor %%f) ← target, env ← get_env, let n := f.get_app_fn.const_name, d ← get_decl n, refine ``( { functor . map := _ , .. } ), tgt ← target, extract_def (with_prefix pre n <.> "map") d.is_trusted $ mk_map n, when (d.is_trusted) $ do tgt ← pis vs tgt, derive_map_equations pre n vs tgt /-- `seq_apply_constructor f [x,y,z]` synthesizes `f <*> x <*> y <*> z` -/ private meta def seq_apply_constructor : expr → list (expr ⊕ expr) → tactic (list (tactic expr) × expr) | e (sum.inr x :: xs) := prod.map (cons intro1) id <$> (to_expr ``(%%e <*> %%x) >>= flip seq_apply_constructor xs) | e (sum.inl x :: xs) := prod.map (cons $ pure x) id <$> seq_apply_constructor e xs | e [] := return ([],e) /-- ``nested_traverse f α (list (array n (list α)))`` synthesizes the expression `traverse (traverse (traverse f))`. `nested_traverse` assumes that `α` appears in `(list (array n (list α)))` -/ meta def nested_traverse (f v : expr) : expr → tactic expr | t := do t ← instantiate_mvars t, mcond (succeeds $ is_def_eq t v) (pure f) (if ¬ v.occurs (t.app_fn) then do cl ← mk_app ``traversable [t.app_fn], _inst ← mk_instance cl, f' ← nested_traverse t.app_arg, mk_mapp ``traversable.traverse [t.app_fn,_inst,none,none,none,none,f'] else fail format!"type {t} is not traversable with respect to variable {v}") /-- For a sum type `inductive foo (α : Type) | foo1 : list α → ℕ → foo | ...` ``traverse_field `foo appl_inst f `α `(x : list α)`` synthesizes `traverse f x` as part of traversing `foo1`. -/ meta def traverse_field (n : name) (appl_inst cl f v e : expr) : tactic (expr ⊕ expr) := do t ← infer_type e >>= whnf, if t.get_app_fn.const_name = n then fail "recursive types not supported" else if v.occurs t then do f' ← nested_traverse f v t, pure $ sum.inr $ f' e else (is_def_eq t.app_fn cl >> sum.inr <$> mk_app ``comp.mk [e]) <|> pure (sum.inl e) /-- For a sum type `inductive foo (α : Type) | foo1 : list α → ℕ → foo | ...` ``traverse_constructor `foo1 `foo appl_inst f `α `β [`(x : list α), `(y : ℕ)]`` synthesizes `foo1 <$> traverse f x <*> pure y.` -/ meta def traverse_constructor (c n : name) (appl_inst f α β : expr) (args₀ : list expr) (args₁ : list (bool × expr)) (rec_call : list expr) : tactic expr := do g ← target, args' ← mmap (traverse_field n appl_inst g.app_fn f α) args₀, (_, args') ← mmap_accuml (λ (x : list expr) (y : bool × _), if y.1 then pure (x.tail, sum.inr x.head) else prod.mk x <$> traverse_field n appl_inst g.app_fn f α y.2) rec_call args₁, constr ← mk_const c, v ← mk_mvar, constr' ← to_expr ``(@pure _ (%%appl_inst).to_has_pure _ %%v), (vars_intro,r) ← seq_apply_constructor constr' (args₀.map sum.inl ++ args'), gs ← get_goals, set_goals [v], vs ← vars_intro.mmap id, tactic.exact (constr.mk_app vs), done, set_goals gs, return r /-- derive the `traverse` definition of a `traversable` instance -/ meta def mk_traverse (type : name) := do do ls ← local_context, [m,appl_inst,α,β,f,x] ← tactic.intro_lst [`m,`appl_inst,`α,`β,`f,`x], et ← infer_type x, reset_instance_cache, xs ← tactic.induction x, xs.mmap' (λ (x : name × list expr × list (name × expr)), do let (c,args,_) := x, (args,rec_call) ← args.mpartition $ λ e, (bnot ∘ β.occurs) <$> infer_type e, args₀ ← args.mmap $ λ a, do { b ← et.occurs <$> infer_type a, pure (b,a) }, traverse_constructor c type appl_inst f α β (ls ++ [β]) args₀ rec_call >>= tactic.exact) open applicative /-- derive the equations for a specific `traverse` definition -/ meta def derive_traverse_equations (pre : option name) (n : name) (vs : list expr) (tgt : expr) : tactic unit := do e ← get_env, ((e.constructors_of n).enum_from 1).mmap' $ λ ⟨i,c⟩, do { mk_meta_var tgt >>= set_goals ∘ pure, vs ← intro_lst $ vs.map expr.local_pp_name, [m,appl_inst,α,β,f] ← tactic.intro_lst [`m,`appl_inst,`α,`β,`f] >>= mmap instantiate_mvars, c' ← mk_mapp c $ vs.map some ++ [α], tgt' ← infer_type c' >>= pis vs, mk_meta_var tgt' >>= set_goals ∘ pure, vs ← tactic.intro_lst $ vs.map expr.local_pp_name, c' ← mk_mapp c $ vs.map some ++ [α], vs' ← tactic.intros, arg ← mk_mapp' c' vs', n_map ← mk_const (with_prefix pre n <.> "traverse"), let call_traverse := λ x, mk_mapp' n_map (vs ++ [m,appl_inst,α,β,f,x]), lhs ← call_traverse arg, args ← vs'.mmap $ λ a, do { t ← infer_type a, pure ((expr.const_name (expr.get_app_fn t) = n : bool),a) }, let rec_call := args.filter_map $ λ ⟨b, e⟩, guard b >> pure e, rec_call ← rec_call.mmap call_traverse, rhs ← traverse_constructor c n appl_inst f α β (vs ++ [β]) args rec_call, monad.join $ unify <$> infer_type lhs <*> infer_type rhs, eqn ← mk_app ``eq [lhs,rhs], let ws := eqn.list_local_consts, eqn ← pis ws.reverse eqn, eqn ← instantiate_mvars eqn, (_,pr) ← solve_aux eqn (tactic.intros >> refine ``(rfl)), let eqn_n := (with_prefix pre n <.> "traverse" <.> "equations" <.> "_eqn").append_after i, pr ← instantiate_mvars pr, add_decl $ declaration.thm eqn_n eqn.collect_univ_params eqn (pure pr), return () }, set_goals [], return () meta def derive_traverse (pre : option name) : tactic unit := do vs ← local_context, `(traversable %%f) ← target, env ← get_env, let n := f.get_app_fn.const_name, d ← get_decl n, constructor, tgt ← target, extract_def (with_prefix pre n <.> "traverse") d.is_trusted $ mk_traverse n, when (d.is_trusted) $ do tgt ← pis vs tgt, derive_traverse_equations pre n vs tgt meta def mk_one_instance (n : name) (cls : name) (tac : tactic unit) (namesp : option name) (mk_inst : name → expr → tactic expr := λ n arg, mk_app n [arg]) : tactic unit := do decl ← get_decl n, cls_decl ← get_decl cls, env ← get_env, guard (env.is_inductive n) <|> fail format!"failed to derive '{cls}', '{n}' is not an inductive type", let ls := decl.univ_params.map $ λ n, level.param n, -- incrementally build up target expression `Π (hp : p) [cls hp] ..., cls (n.{ls} hp ...)` -- where `p ...` are the inductive parameter types of `n` let tgt : expr := expr.const n ls, ⟨params, _⟩ ← open_pis (decl.type.instantiate_univ_params (decl.univ_params.zip ls)), let params := params.init, let tgt := tgt.mk_app params, tgt ← mk_inst cls tgt, tgt ← params.enum.mfoldr (λ ⟨i, param⟩ tgt, do -- add typeclass hypothesis for each inductive parameter tgt ← do { guard $ i < env.inductive_num_params n, param_cls ← mk_app cls [param], pure $ expr.pi `a binder_info.inst_implicit param_cls tgt } <|> pure tgt, pure $ tgt.bind_pi param ) tgt, () <$ mk_instance tgt <|> do (_, val) ← tactic.solve_aux tgt (do tactic.intros >> tac), val ← instantiate_mvars val, let trusted := decl.is_trusted ∧ cls_decl.is_trusted, let inst_n := with_prefix namesp n ++ cls, add_decl (declaration.defn inst_n decl.univ_params tgt val reducibility_hints.abbrev trusted), set_basic_attribute `instance inst_n namesp.is_none open interactive meta def get_equations_of (n : name) : tactic (list pexpr) := do e ← get_env, let pre := n <.> "equations", let x := e.fold [] $ λ d xs, if pre.is_prefix_of d.to_name then d.to_name :: xs else xs, x.mmap resolve_name meta def derive_lawful_functor (pre : option name) : tactic unit := do `(@is_lawful_functor %%f %%d) ← target, refine ``( { .. } ), let n := f.get_app_fn.const_name, let rules := λ r, [simp_arg_type.expr r, simp_arg_type.all_hyps], let goal := loc.ns [none], solve1 (do vs ← tactic.intros, try $ dunfold [``functor.map] (loc.ns [none]), dunfold [with_prefix pre n <.> "map",``id] (loc.ns [none]), () <$ tactic.induction vs.ilast; simp none none ff (rules ``(functor.map_id)) [] goal), focus1 (do vs ← tactic.intros, try $ dunfold [``functor.map] (loc.ns [none]), dunfold [with_prefix pre n <.> "map",``id] (loc.ns [none]), () <$ tactic.induction vs.ilast; simp none none ff (rules ``(functor.map_comp_map)) [] goal), return () meta def simp_functor (rs : list simp_arg_type := []) : tactic unit := simp none none ff rs [`functor_norm] (loc.ns [none]) meta def traversable_law_starter (rs : list simp_arg_type) := do vs ← tactic.intros, resetI, dunfold [``traversable.traverse,``functor.map] (loc.ns [none]), () <$ tactic.induction vs.ilast; simp_functor rs meta def derive_lawful_traversable (pre : option name) : tactic unit := do `(@is_lawful_traversable %%f %%d) ← target, let n := f.get_app_fn.const_name, eqns ← get_equations_of (with_prefix pre n <.> "traverse"), eqns' ← get_equations_of (with_prefix pre n <.> "map"), let def_eqns := eqns.map simp_arg_type.expr ++ eqns'.map simp_arg_type.expr ++ [simp_arg_type.all_hyps], let comp_def := [ simp_arg_type.expr ``(function.comp) ], let tr_map := list.map simp_arg_type.expr [``(traversable.traverse_eq_map_id')], let natur := λ (η : expr), [simp_arg_type.expr ``(traversable.naturality_pf %%η)], let goal := loc.ns [none], constructor; [ traversable_law_starter def_eqns; refl, traversable_law_starter def_eqns; (refl <|> simp_functor (def_eqns ++ comp_def)), traversable_law_starter def_eqns; (refl <|> simp none none tt tr_map [] goal ), traversable_law_starter def_eqns; (refl <|> do η ← get_local `η <|> do { t ← mk_const ``is_lawful_traversable.naturality >>= infer_type >>= pp, fail format!"expecting an `applicative_transformation` called `η` in\nnaturality : {t}"}, simp none none tt (natur η) [] goal) ]; refl, return () open function meta def guard_class (cls : name) (hdl : derive_handler) : derive_handler := λ p n, if p.is_constant_of cls then hdl p n else pure ff meta def higher_order_derive_handler (cls : name) (tac : tactic unit) (deps : list derive_handler := []) (namesp : option name) (mk_inst : name → expr → tactic expr := λ n arg, mk_app n [arg]) : derive_handler := λ p n, do mmap' (λ f : derive_handler, f p n) deps, mk_one_instance n cls tac namesp mk_inst, pure tt meta def functor_derive_handler' (nspace : option name := none) : derive_handler := higher_order_derive_handler ``functor (derive_functor nspace) [] nspace @[derive_handler] meta def functor_derive_handler : derive_handler := guard_class ``functor functor_derive_handler' meta def traversable_derive_handler' (nspace : option name := none) : derive_handler := higher_order_derive_handler ``traversable (derive_traverse nspace) [functor_derive_handler' nspace] nspace @[derive_handler] meta def traversable_derive_handler : derive_handler := guard_class ``traversable traversable_derive_handler' meta def lawful_functor_derive_handler' (nspace : option name := none) : derive_handler := higher_order_derive_handler ``is_lawful_functor (derive_lawful_functor nspace) [traversable_derive_handler' nspace] nspace (λ n arg, mk_mapp n [arg,none]) @[derive_handler] meta def lawful_functor_derive_handler : derive_handler := guard_class ``is_lawful_functor lawful_functor_derive_handler' meta def lawful_traversable_derive_handler' (nspace : option name := none) : derive_handler := higher_order_derive_handler ``is_lawful_traversable (derive_lawful_traversable nspace) [traversable_derive_handler' nspace, lawful_functor_derive_handler' nspace] nspace (λ n arg, mk_mapp n [arg,none]) @[derive_handler] meta def lawful_traversable_derive_handler : derive_handler := guard_class ``is_lawful_traversable lawful_traversable_derive_handler' end tactic.interactive
362404595cb4d3dbfcc6612023f8431a60abaa77
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/topology/algebra/order/t5.lean
1faf616de80a7ea6c21708c156ddd30a84f54b9f
[ "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,379
lean
/- Copyright (c) 2022 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import topology.algebra.order.basic import data.set.intervals.ord_connected_component /-! # Linear order is a completely normal Hausdorff topological space In this file we prove that a linear order with order topology is a completely normal Hausdorff topological space. -/ open filter set function order_dual open_locale topological_space filter interval variables {X : Type*} [linear_order X] [topological_space X] [order_topology X] {a b c : X} {s t : set X} namespace set @[simp] lemma ord_connected_component_mem_nhds : ord_connected_component s a ∈ 𝓝 a ↔ s ∈ 𝓝 a := begin refine ⟨λ h, mem_of_superset h ord_connected_component_subset, λ h, _⟩, rcases exists_Icc_mem_subset_of_mem_nhds h with ⟨b, c, ha, ha', hs⟩, exact mem_of_superset ha' (subset_ord_connected_component ha hs) end lemma compl_section_ord_separating_set_mem_nhds_within_Ici (hd : disjoint s (closure t)) (ha : a ∈ s) : (ord_connected_section $ ord_separating_set s t)ᶜ ∈ 𝓝[≥] a := begin have hmem : tᶜ ∈ 𝓝[≥] a, { refine mem_nhds_within_of_mem_nhds _, rw [← mem_interior_iff_mem_nhds, interior_compl], exact disjoint_left.1 hd ha }, rcases exists_Icc_mem_subset_of_mem_nhds_within_Ici hmem with ⟨b, hab, hmem', hsub⟩, by_cases H : disjoint (Icc a b) (ord_connected_section $ ord_separating_set s t), { exact mem_of_superset hmem' (disjoint_left.1 H) }, { simp only [set.disjoint_left, not_forall, not_not] at H, rcases H with ⟨c, ⟨hac, hcb⟩, hc⟩, have hsub' : Icc a b ⊆ ord_connected_component tᶜ a, from subset_ord_connected_component (left_mem_Icc.2 hab) hsub, replace hac : a < c := hac.lt_of_ne (ne.symm $ ne_of_mem_of_not_mem hc $ disjoint_left.1 (disjoint_left_ord_separating_set.mono_right ord_connected_section_subset) ha), refine mem_of_superset (Ico_mem_nhds_within_Ici (left_mem_Ico.2 hac)) (λ x hx hx', _), refine hx.2.ne (eq_of_mem_ord_connected_section_of_interval_subset hx' hc _), refine subset_inter (subset_Union₂_of_subset a ha _) _, { exact ord_connected.interval_subset infer_instance (hsub' ⟨hx.1, hx.2.le.trans hcb⟩) (hsub' ⟨hac.le, hcb⟩) }, { rcases mem_Union₂.1 (ord_connected_section_subset hx').2 with ⟨y, hyt, hxy⟩, refine subset_Union₂_of_subset y hyt (ord_connected.interval_subset infer_instance hxy _), refine subset_ord_connected_component left_mem_interval hxy _, suffices : c < y, { rw [interval_of_ge (hx.2.trans this).le], exact ⟨hx.2.le, this.le⟩ }, refine lt_of_not_le (λ hyc, _), have hya : y < a, from not_le.1 (λ hay, hsub ⟨hay, hyc.trans hcb⟩ hyt), exact hxy (Icc_subset_interval ⟨hya.le, hx.1⟩) ha } } end lemma compl_section_ord_separating_set_mem_nhds_within_Iic (hd : disjoint s (closure t)) (ha : a ∈ s) : (ord_connected_section $ ord_separating_set s t)ᶜ ∈ 𝓝[≤] a := have hd' : disjoint (of_dual ⁻¹' s) (closure $ of_dual ⁻¹' t) := hd, have ha' : to_dual a ∈ of_dual ⁻¹' s := ha, by simpa only [dual_ord_separating_set, dual_ord_connected_section] using compl_section_ord_separating_set_mem_nhds_within_Ici hd' ha' lemma compl_section_ord_separating_set_mem_nhds (hd : disjoint s (closure t)) (ha : a ∈ s) : (ord_connected_section $ ord_separating_set s t)ᶜ ∈ 𝓝 a := begin rw [← nhds_left_sup_nhds_right, mem_sup], exact ⟨compl_section_ord_separating_set_mem_nhds_within_Iic hd ha, compl_section_ord_separating_set_mem_nhds_within_Ici hd ha⟩ end lemma ord_t5_nhd_mem_nhds_set (hd : disjoint s (closure t)) : ord_t5_nhd s t ∈ 𝓝ˢ s := bUnion_mem_nhds_set $ λ x hx, ord_connected_component_mem_nhds.2 $ inter_mem (by { rw [← mem_interior_iff_mem_nhds, interior_compl], exact disjoint_left.1 hd hx }) (compl_section_ord_separating_set_mem_nhds hd hx) end set open set /-- A linear order with order topology is a completely normal Hausdorff topological space. -/ @[priority 100] instance order_topology.t5_space : t5_space X := ⟨λ s t h₁ h₂, filter.disjoint_iff.2 ⟨ord_t5_nhd s t, ord_t5_nhd_mem_nhds_set h₂, ord_t5_nhd t s, ord_t5_nhd_mem_nhds_set h₁.symm, disjoint_ord_t5_nhd⟩⟩
b7d0706975aab83f837ac780fdb24ded475ea675
1a61aba1b67cddccce19532a9596efe44be4285f
/hott/algebra/field.hlean
39751c30242832afbb4d650702a103b927121adc
[ "Apache-2.0" ]
permissive
eigengrau/lean
07986a0f2548688c13ba36231f6cdbee82abf4c6
f8a773be1112015e2d232661ce616d23f12874d0
refs/heads/master
1,610,939,198,566
1,441,352,386,000
1,441,352,494,000
41,903,576
0
0
null
1,441,352,210,000
1,441,352,210,000
null
UTF-8
Lean
false
false
19,026
hlean
/- Copyright (c) 2014 Robert Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Lewis Structures with multiplicative and additive components, including division rings and fields. The development is modeled after Isabelle's library. Ported from the standard library -/ import algebra.ring open core namespace algebra variable {A : Type} -- in division rings, 1 / 0 = 0 structure division_ring [class] (A : Type) extends ring A, has_inv A, zero_ne_one_class A := (mul_inv_cancel : Π{a}, a ≠ zero → mul a (inv a) = one) (inv_mul_cancel : Π{a}, a ≠ zero → mul (inv a) a = one) --(inv_zero : inv zero = zero) section division_ring variables [s : division_ring A] {a b c : A} include s definition divide (a b : A) : A := a * b⁻¹ infix `/` := divide -- only in this file local attribute divide [reducible] definition mul_inv_cancel (H : a ≠ 0) : a * a⁻¹ = 1 := division_ring.mul_inv_cancel H definition inv_mul_cancel (H : a ≠ 0) : a⁻¹ * a = 1 := division_ring.inv_mul_cancel H definition inv_eq_one_div : a⁻¹ = 1 / a := !one_mul⁻¹ -- the following are only theorems if we assume inv_zero here /- definition inv_zero : 0⁻¹ = 0 := !division_ring.inv_zero definition one_div_zero : 1 / 0 = 0 := calc 1 / 0 = 1 * 0⁻¹ : refl ... = 1 * 0 : division_ring.inv_zero A ... = 0 : mul_zero -/ definition div_eq_mul_one_div : a / b = a * (1 / b) := by rewrite [↑divide, one_mul] -- definition div_zero : a / 0 = 0 := by rewrite [div_eq_mul_one_div, one_div_zero, mul_zero] definition mul_one_div_cancel (H : a ≠ 0) : a * (1 / a) = 1 := by rewrite [-inv_eq_one_div, (mul_inv_cancel H)] definition one_div_mul_cancel (H : a ≠ 0) : (1 / a) * a = 1 := by rewrite [-inv_eq_one_div, (inv_mul_cancel H)] definition div_self (H : a ≠ 0) : a / a = 1 := mul_inv_cancel H definition one_div_one : 1 / 1 = (1:A) := div_self (ne.symm zero_ne_one) definition mul_div_assoc : (a * b) / c = a * (b / c) := !mul.assoc definition one_div_ne_zero (H : a ≠ 0) : 1 / a ≠ 0 := assume H2 : 1 / a = 0, have C1 : 0 = (1:A), from inverse (by rewrite [-(mul_one_div_cancel H), H2, mul_zero]), absurd C1 zero_ne_one -- definition ne_zero_of_one_div_ne_zero (H : 1 / a ≠ 0) : a ≠ 0 := -- assume Ha : a = 0, absurd (Ha⁻¹ ▸ one_div_zero) H definition inv_one_eq : 1⁻¹ = (1:A) := by rewrite [-mul_one, (inv_mul_cancel (ne.symm (@zero_ne_one A _)))] definition div_one : a / 1 = a := by rewrite [↑divide, inv_one_eq, mul_one] definition zero_div : 0 / a = 0 := !zero_mul -- note: integral domain has a "mul_ne_zero". Discrete fields are int domains. definition mul_ne_zero' (Ha : a ≠ 0) (Hb : b ≠ 0) : a * b ≠ 0 := assume H : a * b = 0, have C1 : a = 0, by rewrite [-mul_one, -(mul_one_div_cancel Hb), -mul.assoc, H, zero_mul], absurd C1 Ha definition mul_ne_zero_comm (H : a * b ≠ 0) : b * a ≠ 0 := have H2 : a ≠ 0 × b ≠ 0, from ne_zero_and_ne_zero_of_mul_ne_zero H, mul_ne_zero' (prod.pr2 H2) (prod.pr1 H2) -- make "left" and "right" versions? definition eq_one_div_of_mul_eq_one (H : a * b = 1) : b = 1 / a := have H2 : a ≠ 0, from (assume aeq0 : a = 0, have B : 0 = (1:A), by rewrite [-(zero_mul b), -aeq0, H], absurd B zero_ne_one), show b = 1 / a, from inverse (calc 1 / a = (1 / a) * 1 : mul_one ... = (1 / a) * (a * b) : H ... = (1 / a) * a * b : mul.assoc ... = 1 * b : one_div_mul_cancel H2 ... = b : one_mul) -- which one is left and which is right? definition eq_one_div_of_mul_eq_one_left (H : b * a = 1) : b = 1 / a := have H2 : a ≠ 0, from (assume A : a = 0, have B : 0 = 1, from inverse (calc 1 = b * a : inverse H ... = b * 0 : A ... = 0 : mul_zero), absurd B zero_ne_one), show b = 1 / a, from inverse (calc 1 / a = 1 * (1 / a) : one_mul ... = b * a * (1 / a) : H ... = b * (a * (1 / a)) : mul.assoc ... = b * 1 : mul_one_div_cancel H2 ... = b : mul_one) definition one_div_mul_one_div (Ha : a ≠ 0) (Hb : b ≠ 0) : (1 / a) * (1 / b) = 1 / (b * a) := have H : (b * a) * ((1 / a) * (1 / b)) = 1, by rewrite [mul.assoc, -(mul.assoc a), (mul_one_div_cancel Ha), one_mul, (mul_one_div_cancel Hb)], eq_one_div_of_mul_eq_one H definition one_div_neg_one_eq_neg_one : (1:A) / (-1) = -1 := have H : (-1) * (-1) = 1, by rewrite [-neg_eq_neg_one_mul, neg_neg], inverse (eq_one_div_of_mul_eq_one H) definition one_div_neg_eq_neg_one_div (H : a ≠ 0) : 1 / (- a) = - (1 / a) := have H1 : -1 ≠ 0, from (assume H2 : -1 = 0, absurd (inverse (calc 1 = -(-1) : neg_neg ... = -0 : H2 ... = (0:A) : neg_zero)) zero_ne_one), calc 1 / (- a) = 1 / ((-1) * a) : neg_eq_neg_one_mul ... = (1 / a) * (1 / (- 1)) : one_div_mul_one_div H H1 ... = (1 / a) * (-1) : one_div_neg_one_eq_neg_one ... = - (1 / a) : mul_neg_one_eq_neg definition div_neg_eq_neg_div (Ha : a ≠ 0) : b / (- a) = - (b / a) := calc b / (- a) = b * (1 / (- a)) : inv_eq_one_div ... = b * -(1 / a) : one_div_neg_eq_neg_one_div Ha ... = -(b * (1 / a)) : neg_mul_eq_mul_neg ... = - (b * a⁻¹) : inv_eq_one_div definition neg_div (Ha : a ≠ 0) : (-b) / a = - (b / a) := by rewrite [neg_eq_neg_one_mul, mul_div_assoc, -neg_eq_neg_one_mul] definition neg_div_neg_eq_div (Hb : b ≠ 0) : (-a) / (-b) = a / b := by rewrite [(div_neg_eq_neg_div Hb), (neg_div Hb), neg_neg] definition div_div (H : a ≠ 0) : 1 / (1 / a) = a := inverse (eq_one_div_of_mul_eq_one_left (mul_one_div_cancel H)) definition eq_of_invs_eq (Ha : a ≠ 0) (Hb : b ≠ 0) (H : 1 / a = 1 / b) : a = b := by rewrite [-(div_div Ha), H, (div_div Hb)] -- oops, the analogous definition in group is called inv_mul, but it *should* be called -- mul_inv, in which case, we will have to rename this one definition mul_inv_eq (Ha : a ≠ 0) (Hb : b ≠ 0) : (b * a)⁻¹ = a⁻¹ * b⁻¹ := have H1 : b * a ≠ 0, from mul_ne_zero' Hb Ha, inverse (calc a⁻¹ * b⁻¹ = (1 / a) * b⁻¹ : inv_eq_one_div ... = (1 / a) * (1 / b) : inv_eq_one_div ... = (1 / (b * a)) : one_div_mul_one_div Ha Hb ... = (b * a)⁻¹ : inv_eq_one_div) definition mul_div_cancel (Hb : b ≠ 0) : a * b / b = a := by rewrite [↑divide, mul.assoc, (mul_inv_cancel Hb), mul_one] definition div_mul_cancel (Hb : b ≠ 0) : a / b * b = a := by rewrite [↑divide, mul.assoc, (inv_mul_cancel Hb), mul_one] definition div_add_div_same : a / c + b / c = (a + b) / c := !right_distrib⁻¹ definition inv_mul_add_mul_inv_eq_inv_add_inv (Ha : a ≠ 0) (Hb : b ≠ 0) : (1 / a) * (a + b) * (1 / b) = 1 / a + 1 / b := by rewrite [(left_distrib (1 / a)), (one_div_mul_cancel Ha), right_distrib, one_mul, mul.assoc, (mul_one_div_cancel Hb), mul_one, add.comm] definition inv_mul_sub_mul_inv_eq_inv_add_inv (Ha : a ≠ 0) (Hb : b ≠ 0) : (1 / a) * (b - a) * (1 / b) = 1 / a - 1 / b := by rewrite [(mul_sub_left_distrib (1 / a)), (one_div_mul_cancel Ha), mul_sub_right_distrib, one_mul, mul.assoc, (mul_one_div_cancel Hb), mul_one, one_mul] definition div_eq_one_iff_eq (Hb : b ≠ 0) : a / b = 1 ↔ a = b := iff.intro (assume H1 : a / b = 1, inverse (calc b = 1 * b : one_mul ... = a / b * b : H1 ... = a : div_mul_cancel Hb)) (assume H2 : a = b, calc a / b = b / b : H2 ... = 1 : div_self Hb) definition eq_div_iff_mul_eq (Hc : c ≠ 0) : a = b / c ↔ a * c = b := iff.intro (assume H : a = b / c, by rewrite [H, (div_mul_cancel Hc)]) (assume H : a * c = b, by rewrite [-(mul_div_cancel Hc), H]) definition add_div_eq_mul_add_div (Hc : c ≠ 0) : a + b / c = (a * c + b) / c := have H : (a + b / c) * c = a * c + b, by rewrite [right_distrib, (div_mul_cancel Hc)], (iff.elim_right (eq_div_iff_mul_eq Hc)) H definition mul_mul_div (Hc : c ≠ 0) : a = a * c * (1 / c) := calc a = a * 1 : mul_one ... = a * (c * (1 / c)) : mul_one_div_cancel Hc ... = a * c * (1 / c) : mul.assoc -- There are many similar rules to these last two in the Isabelle library -- that haven't been ported yet. Do as necessary. end division_ring structure field [class] (A : Type) extends division_ring A, comm_ring A section field variables [s : field A] {a b c d: A} include s local attribute divide [reducible] definition one_div_mul_one_div' (Ha : a ≠ 0) (Hb : b ≠ 0) : (1 / a) * (1 / b) = 1 / (a * b) := by rewrite [(one_div_mul_one_div Ha Hb), mul.comm b] definition div_mul_right (Hb : b ≠ 0) (H : a * b ≠ 0) : a / (a * b) = 1 / b := let Ha : a ≠ 0 := prod.pr1 (ne_zero_and_ne_zero_of_mul_ne_zero H) in inverse (calc 1 / b = 1 * (1 / b) : one_mul ... = (a * a⁻¹) * (1 / b) : mul_inv_cancel Ha ... = a * (a⁻¹ * (1 / b)) : mul.assoc ... = a * ((1 / a) * (1 / b)) :inv_eq_one_div ... = a * (1 / (b * a)) : one_div_mul_one_div Ha Hb ... = a * (1 / (a * b)) : mul.comm ... = a * (a * b)⁻¹ : inv_eq_one_div) definition div_mul_left (Ha : a ≠ 0) (H : a * b ≠ 0) : b / (a * b) = 1 / a := let H1 : b * a ≠ 0 := mul_ne_zero_comm H in by rewrite [mul.comm a, (div_mul_right Ha H1)] definition mul_div_cancel_left (Ha : a ≠ 0) : a * b / a = b := by rewrite [mul.comm a, (mul_div_cancel Ha)] definition mul_div_cancel' (Hb : b ≠ 0) : b * (a / b) = a := by rewrite [mul.comm, (div_mul_cancel Hb)] definition one_div_add_one_div (Ha : a ≠ 0) (Hb : b ≠ 0) : 1 / a + 1 / b = (a + b) / (a * b) := have H [visible] : a * b ≠ 0, from (mul_ne_zero' Ha Hb), by rewrite [add.comm, -(div_mul_left Ha H), -(div_mul_right Hb H), ↑divide, -right_distrib] definition div_mul_div (Hb : b ≠ 0) (Hd : d ≠ 0) : (a / b) * (c / d) = (a * c) / (b * d) := by rewrite [↑divide, 2 mul.assoc, (mul.comm b⁻¹), mul.assoc, (mul_inv_eq Hd Hb)] definition mul_div_mul_left (Hb : b ≠ 0) (Hc : c ≠ 0) : (c * a) / (c * b) = a / b := have H [visible] : c * b ≠ 0, from mul_ne_zero' Hc Hb, by rewrite [-(div_mul_div Hc Hb), (div_self Hc), one_mul] definition mul_div_mul_right (Hb : b ≠ 0) (Hc : c ≠ 0) : (a * c) / (b * c) = a / b := by rewrite [(mul.comm a), (mul.comm b), (mul_div_mul_left Hb Hc)] definition div_mul_eq_mul_div : (b / c) * a = (b * a) / c := by rewrite [↑divide, mul.assoc, (mul.comm c⁻¹), -mul.assoc] -- this one is odd -- I am not sure what to call it, but again, the prefix is right definition div_mul_eq_mul_div_comm (Hc : c ≠ 0) : (b / c) * a = b * (a / c) := by rewrite [(div_mul_eq_mul_div), -(one_mul c), -(div_mul_div (ne.symm zero_ne_one) Hc), div_one, one_mul] definition div_add_div (Hb : b ≠ 0) (Hd : d ≠ 0) : (a / b) + (c / d) = ((a * d) + (b * c)) / (b * d) := have H [visible] : b * d ≠ 0, from mul_ne_zero' Hb Hd, by rewrite [-(mul_div_mul_right Hb Hd), -(mul_div_mul_left Hd Hb), div_add_div_same] definition div_sub_div (Hb : b ≠ 0) (Hd : d ≠ 0) : (a / b) - (c / d) = ((a * d) - (b * c)) / (b * d) := by rewrite [↑sub, neg_eq_neg_one_mul, -mul_div_assoc, (div_add_div Hb Hd), -mul.assoc, (mul.comm b), mul.assoc, -neg_eq_neg_one_mul] definition mul_eq_mul_of_div_eq_div (Hb : b ≠ 0) (Hd : d ≠ 0) (H : a / b = c / d) : a * d = c * b := by rewrite [-mul_one, mul.assoc, (mul.comm d), -mul.assoc, -(div_self Hb), -(div_mul_eq_mul_div_comm Hb), H, (div_mul_eq_mul_div), (div_mul_cancel Hd)] definition one_div_div (Ha : a ≠ 0) (Hb : b ≠ 0) : 1 / (a / b) = b / a := have H : (a / b) * (b / a) = 1, from calc (a / b) * (b / a) = (a * b) / (b * a) : div_mul_div Hb Ha ... = (a * b) / (a * b) : mul.comm ... = 1 : div_self (mul_ne_zero' Ha Hb), inverse (eq_one_div_of_mul_eq_one H) definition div_div_eq_mul_div (Hb : b ≠ 0) (Hc : c ≠ 0) : a / (b / c) = (a * c) / b := by rewrite [div_eq_mul_one_div, (one_div_div Hb Hc), -mul_div_assoc] definition div_div_eq_div_mul (Hb : b ≠ 0) (Hc : c ≠ 0) : (a / b) / c = a / (b * c) := by rewrite [div_eq_mul_one_div, (div_mul_div Hb Hc), mul_one] definition div_div_div_div (Hb : b ≠ 0) (Hc : c ≠ 0) (Hd : d ≠ 0) : (a / b) / (c / d) = (a * d) / (b * c) := by rewrite [(div_div_eq_mul_div Hc Hd), (div_mul_eq_mul_div), (div_div_eq_div_mul Hb Hc)] -- remaining to transfer from Isabelle fields: ordered fields end field structure discrete_field [class] (A : Type) extends field A := (has_decidable_eq : decidable_eq A) (inv_zero : inv zero = zero) attribute discrete_field.has_decidable_eq [instance] section discrete_field variable [s : discrete_field A] include s variables {a b c d : A} -- many of the theorems in discrete_field are the same as theorems in field or division ring, -- but with fewer hypotheses since 0⁻¹ = 0 and equality is decidable. -- they are named with '. Is there a better convention? definition discrete_field.eq_zero_or_eq_zero_of_mul_eq_zero (x y : A) (H : x * y = 0) : x = 0 ⊎ y = 0 := decidable.by_cases (assume H : x = 0, sum.inl H) (assume H1 : x ≠ 0, sum.inr (by rewrite [-one_mul, -(inv_mul_cancel H1), mul.assoc, H, mul_zero])) definition discrete_field.to_integral_domain [instance] [reducible] [coercion] : integral_domain A := ⦃ integral_domain, s, eq_zero_or_eq_zero_of_mul_eq_zero := discrete_field.eq_zero_or_eq_zero_of_mul_eq_zero⦄ definition inv_zero : 0⁻¹ = (0 : A) := !discrete_field.inv_zero definition one_div_zero : 1 / 0 = (0:A) := calc 1 / 0 = 1 * 0⁻¹ : refl ... = 1 * 0 : discrete_field.inv_zero A ... = 0 : mul_zero definition div_zero : a / 0 = 0 := by rewrite [div_eq_mul_one_div, one_div_zero, mul_zero] definition ne_zero_of_one_div_ne_zero (H : 1 / a ≠ 0) : a ≠ 0 := assume Ha : a = 0, absurd (Ha⁻¹ ▸ one_div_zero) H definition inv_zero_imp_zero (H : 1 / a = 0) : a = 0 := decidable.by_cases (assume Ha, Ha) (assume Ha, empty.elim ((one_div_ne_zero Ha) H)) -- the following could all go in "discrete_division_ring" definition one_div_mul_one_div'' : (1 / a) * (1 / b) = 1 / (b * a) := decidable.by_cases (assume Ha : a = 0, by rewrite [Ha, div_zero, zero_mul, -(@div_zero A s 1), mul_zero b]) (assume Ha : a ≠ 0, decidable.by_cases (assume Hb : b = 0, by rewrite [Hb, div_zero, mul_zero, -(@div_zero A s 1), zero_mul a]) (assume Hb : b ≠ 0, one_div_mul_one_div Ha Hb)) definition one_div_neg_eq_neg_one_div' : 1 / (- a) = - (1 / a) := decidable.by_cases (assume Ha : a = 0, by rewrite [Ha, neg_zero, 2 div_zero, neg_zero]) (assume Ha : a ≠ 0, one_div_neg_eq_neg_one_div Ha) definition neg_div' : (-b) / a = - (b / a) := decidable.by_cases (assume Ha : a = 0, by rewrite [Ha, 2 div_zero, neg_zero]) (assume Ha : a ≠ 0, neg_div Ha) definition neg_div_neg_eq_div' : (-a) / (-b) = a / b := decidable.by_cases (assume Hb : b = 0, by rewrite [Hb, neg_zero, 2 div_zero]) (assume Hb : b ≠ 0, neg_div_neg_eq_div Hb) definition div_div' : 1 / (1 / a) = a := decidable.by_cases (assume Ha : a = 0, by rewrite [Ha, 2 div_zero]) (assume Ha : a ≠ 0, div_div Ha) definition eq_of_invs_eq' (H : 1 / a = 1 / b) : a = b := decidable.by_cases (assume Ha : a = 0, have Hb : b = 0, from inv_zero_imp_zero (by rewrite [-H, Ha, div_zero]), Hb⁻¹ ▸ Ha) (assume Ha : a ≠ 0, have Hb : b ≠ 0, from ne_zero_of_one_div_ne_zero (H ▸ (one_div_ne_zero Ha)), eq_of_invs_eq Ha Hb H) definition mul_inv' : (b * a)⁻¹ = a⁻¹ * b⁻¹ := decidable.by_cases (assume Ha : a = 0, by rewrite [Ha, mul_zero, 2 inv_zero, zero_mul]) (assume Ha : a ≠ 0, decidable.by_cases (assume Hb : b = 0, by rewrite [Hb, zero_mul, 2 inv_zero, mul_zero]) (assume Hb : b ≠ 0, mul_inv_eq Ha Hb)) -- the following are specifically for fields definition one_div_mul_one_div''' : (1 / a) * (1 / b) = 1 / (a * b) := by rewrite [(one_div_mul_one_div''), mul.comm b] definition div_mul_right' (Ha : a ≠ 0) : a / (a * b) = 1 / b := decidable.by_cases (assume Hb : b = 0, by rewrite [Hb, mul_zero, 2 div_zero]) (assume Hb : b ≠ 0, div_mul_right Hb (mul_ne_zero Ha Hb)) definition div_mul_left' (Hb : b ≠ 0) : b / (a * b) = 1 / a := by rewrite [mul.comm a, div_mul_right' Hb] definition div_mul_div' : (a / b) * (c / d) = (a * c) / (b * d) := decidable.by_cases (assume Hb : b = 0, by rewrite [Hb, div_zero, zero_mul, -(@div_zero A s (a * c)), zero_mul]) (assume Hb : b ≠ 0, decidable.by_cases (assume Hd : d = 0, by rewrite [Hd, div_zero, mul_zero, -(@div_zero A s (a * c)), mul_zero]) (assume Hd : d ≠ 0, div_mul_div Hb Hd)) definition mul_div_mul_left' (Hc : c ≠ 0) : (c * a) / (c * b) = a / b := decidable.by_cases (assume Hb : b = 0, by rewrite [Hb, mul_zero, 2 div_zero]) (assume Hb : b ≠ 0, mul_div_mul_left Hb Hc) definition mul_div_mul_right' (Hc : c ≠ 0) : (a * c) / (b * c) = a / b := by rewrite [(mul.comm a), (mul.comm b), (mul_div_mul_left' Hc)] -- this one is odd -- I am not sure what to call it, but again, the prefix is right definition div_mul_eq_mul_div_comm' : (b / c) * a = b * (a / c) := decidable.by_cases (assume Hc : c = 0, by rewrite [Hc, div_zero, zero_mul, -(mul_zero b), -(@div_zero A s a)]) (assume Hc : c ≠ 0, div_mul_eq_mul_div_comm Hc) definition one_div_div' : 1 / (a / b) = b / a := decidable.by_cases (assume Ha : a = 0, by rewrite [Ha, zero_div, 2 div_zero]) (assume Ha : a ≠ 0, decidable.by_cases (assume Hb : b = 0, by rewrite [Hb, 2 div_zero, zero_div]) (assume Hb : b ≠ 0, one_div_div Ha Hb)) definition div_div_eq_mul_div' : a / (b / c) = (a * c) / b := by rewrite [div_eq_mul_one_div, one_div_div', -mul_div_assoc] definition div_div_eq_div_mul' : (a / b) / c = a / (b * c) := by rewrite [div_eq_mul_one_div, div_mul_div', mul_one] definition div_div_div_div' : (a / b) / (c / d) = (a * d) / (b * c) := by rewrite [div_div_eq_mul_div', div_mul_eq_mul_div, div_div_eq_div_mul'] end discrete_field end algebra /- decidable.by_cases (assume Ha : a = 0, sorry) (assume Ha : a ≠ 0, sorry) -/
1e812edee0d3266d546d174bd9fc9f66f6d74d75
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/list/range.lean
94e3d326fd7bdc0c67bb7bbedd38c1880b33713f
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
10,935
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.zip /-! # Ranges of naturals as lists > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. 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 = [n, n - 1, ..., 1]` 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 h.length_le, λ 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 pairwise_le_range (n : ℕ) : pairwise (≤) (range n) := pairwise.imp (@le_of_lt ℕ _) (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) := (pairwise_gt_iota n).imp (λ a b, ne_of_gt) 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, by { cases a, refl, }⟩ lemma nodup_fin_range (n : ℕ) : (fin_range n).nodup := pairwise.pmap (nodup_range n) _ $ λ _ _ _ _, @fin.ne_of_vne _ ⟨_, _⟩ ⟨_, _⟩ @[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] @[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] end list
13380c73728d8e3b36f32ce3786b2607d5f2f0e7
09b3e1beaeff2641ac75019c9f735d79d508071d
/Mathlib/Tactic/NormNum.lean
d78a22b8e737f8790682e344aae713eaf5d251be
[ "Apache-2.0" ]
permissive
abentkamp/mathlib4
b819079cc46426b3c5c77413504b07541afacc19
f8294a67548f8f3d1f5913677b070a2ef5bcf120
refs/heads/master
1,685,309,252,764
1,623,232,534,000
1,623,232,534,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,594
lean
/- Copyright (c) 2021 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Lean.Elab.Tactic.Basic import Mathlib.Algebra.Ring.Basic namespace Lean namespace Meta instance Semiring.OfNat [Semiring α] : OfNat α n := Numeric.OfNat def mkOfNatLit (u : Level) (α sα n : Expr) : Expr := let inst := mkApp3 (mkConst ``Semiring.OfNat [u]) α sα n mkApp3 (mkConst ``OfNat.ofNat [u]) α n inst namespace NormNum theorem ofNat_add {α} [Semiring α] : (a b : α) → (a' b' c : Nat) → a = OfNat.ofNat a' → b = OfNat.ofNat b' → a' + b' = c → a + b = OfNat.ofNat c | _, _, _, _, _, rfl, rfl, rfl => (Semiring.ofNat_add _ _).symm theorem ofNat_mul {α} [Semiring α] : (a b : α) → (a' b' c : Nat) → a = OfNat.ofNat a' → b = OfNat.ofNat b' → a' * b' = c → a * b = OfNat.ofNat c | _, _, _, _, _, rfl, rfl, rfl => (Semiring.ofNat_mul _ _).symm partial def eval : Expr → MetaM (Expr × Expr) | e => e.withApp fun f args => do if f.isConstOf ``HAdd.hAdd then evalB ``NormNum.ofNat_add (·+·) args else if f.isConstOf ``HMul.hMul then evalB ``NormNum.ofNat_mul (·*·) args else if f.isConstOf ``OfNat.ofNat then pure (e, ← mkEqRefl e) else throwError "fail" where evalB (name : Name) (f : Nat → Nat → Nat) (args : Array Expr) : MetaM (Expr × Expr) := do if let #[_, _, α, _, a, b] ← args then let Level.succ u _ ← getLevel α | throwError "fail" let sα ← synthInstance (mkApp (mkConst ``Semiring [u]) α) let (a', pa) ← eval a let (b', pb) ← eval b let la := Expr.getRevArg! a' 1 let some na ← la.natLit? | throwError "fail" let lb := Expr.getRevArg! b' 1 let some nb ← lb.natLit? | throwError "fail" let lc := mkNatLit (f na nb) let c := mkOfNatLit u α sα lc pure (c, mkApp10 (mkConst name [u]) α sα a b la lb lc pa pb (← mkEqRefl lc)) else throwError "fail" end NormNum end Meta syntax (name := Parser.Tactic.normNum) "normNum" : tactic open Meta Elab Tactic @[tactic normNum] def Tactic.evalNormNum : Tactic := fun stx => liftMetaTactic fun g => do let some (α, lhs, rhs) ← matchEq? (← getMVarType g) | throwError "fail" let (lhs2, p) ← NormNum.eval lhs unless ← isDefEq lhs2 rhs do throwError "fail" assignExprMVar g p pure [] end Lean /- variable (α) [Semiring α] example : (2 + 2 + 2 : α) = 6 := by normNum example : (0 + (2 + 3) + 7 : α) = 12 := by normNum example : (70 * (33 + 2) : α) = 2450 := by normNum -/
1d8de5e6d2556a49f2ef56fff5915ce2d3040d2a
54ce0561cebde424526f41d45f490ed56be2bd0c
/src/game/ch2_Natural_Numbers/2_Addition.lean
838407d80cb35f6088fa00f91c5c2a16d49328cd
[]
no_license
melembroucarlitos/Tao_Analysis-LEAN
cf7b3298d317891a09e4bf21cfe7c7ffcb57b9a9
3f4fc7e090d96b6cef64896492fba4bef124794b
refs/heads/master
1,692,952,385,694
1,636,287,522,000
1,636,287,522,000
400,630,166
3
0
null
1,635,910,807,000
1,630,096,823,000
Lean
UTF-8
Lean
false
false
160
lean
-- Level name : Addition /- # Hey yall ## This is just to a placeholder to make sure all is working these are some words and these are some other words -/
83e5f2252c29552a2d95206737ff28fb0c5d6840
a721fe7446524f18ba361625fc01033d9c8b7a78
/src/principia/myring/quotients.lean
725a34aefeada762527dda3678e75ac7673e3fb1
[]
no_license
Sterrs/leaning
8fd80d1f0a6117a220bb2e57ece639b9a63deadc
3901cc953694b33adda86cb88ca30ba99594db31
refs/heads/master
1,627,023,822,744
1,616,515,221,000
1,616,515,221,000
245,512,190
2
0
null
1,616,429,050,000
1,583,527,118,000
Lean
UTF-8
Lean
false
false
5,350
lean
import .ideal namespace hidden namespace myring -- at some point this could be done as a very general construction -- of quotient groups, I suppose. variables {α : Type} [myring α] -- not exactly sure what the most principled type-classical approach is -- because really we do want to be explicit about which ideal we're -- quotienting by def setoid_from_ideal {I: myset α} (hIi: is_ideal I): setoid α := setoid.mk (λ a b, a - b ∈ I) begin split, { intro a, change a + -a ∈ I, rw add_neg, from hIi.contains_zero, }, split, { intros a b, assume habI, have := hIi.neg_closure _ habI, change -(a + -b) ∈ I at this, rw neg_distr at this, rw add_comm at this, rw neg_neg at this, from this, }, { intros a b c, assume habI hbcI, have := hIi.add_closure _ _ habI hbcI, change a + -b + (b + -c) ∈ I at this, rw add_assoc at this, rw ←add_assoc (-b) at this, rw neg_add at this, rw zero_add at this, from this, }, end def q_ideal {I: myset α} (hIi: is_ideal I) := quotient (setoid_from_ideal hIi) -- notational convenience -- could introduce proper symbolic notation? def coset {I: myset α} (hIi: is_ideal I): α → q_ideal hIi := (@quotient.mk α (setoid_from_ideal hIi)) def coset_exists_rep {I: myset α} (hIi: is_ideal I) := @quotient.exists_rep _ (setoid_from_ideal hIi) private def quotient_ring_add {I: myset α} (hIi: is_ideal I): q_ideal hIi → q_ideal hIi → q_ideal hIi := @quotient.lift₂ _ _ _ (setoid_from_ideal hIi) (setoid_from_ideal hIi) (λ a b, coset hIi (a + b)) begin intros a b a' b', assume haa' hbb', change a - a' ∈ I at haa', change b - b' ∈ I at hbb', have := hIi.add_closure _ _ haa' hbb', apply quotient.sound, change a + -a' + (b + -b') ∈ I at this, rw add_assoc at this, rw add_comm (-a') at this, rw add_assoc at this, rw ←add_assoc a at this, rw add_comm (-b') at this, rw ←neg_distr at this, from this, end private def quotient_ring_mul {I: myset α} (hIi: is_ideal I): q_ideal hIi → q_ideal hIi → q_ideal hIi := @quotient.lift₂ _ _ _ (setoid_from_ideal hIi) (setoid_from_ideal hIi) (λ a b, coset hIi (a * b)) begin intros a b a' b', assume haa' hbb', change a - a' ∈ I at haa', change b - b' ∈ I at hbb', apply quotient.sound, have this1 := hIi.mul_closure b (a - a') haa', have this2 := hIi.mul_closure a' (b - b') hbb', have := hIi.add_closure _ _ this1 this2, change b * (a + -a') + a' * (b + -b') ∈ I at this, repeat {rw mul_add at this}, repeat {rw mul_neg at this}, rw add_assoc at this, rw ←add_assoc (-(b * a')) at this, rw mul_comm b a' at this, rw neg_add at this, rw zero_add at this, rw mul_comm b a at this, from this, end private def quotient_ring_neg {I: myset α} (hIi: is_ideal I): q_ideal hIi → q_ideal hIi := @quotient.lift _ _ (setoid_from_ideal hIi) (λ a, coset hIi (-a)) begin intros a a', assume haa', change a - a' ∈ I at haa', apply quotient.sound, have := hIi.neg_closure _ haa', change -(a + -a') ∈ I at this, rw neg_distr at this, from this, end -- shrug instance quotient_ring_has_zero {I: myset α} [hIi: is_ideal I]: has_zero (q_ideal hIi) := ⟨@quotient.mk α (setoid_from_ideal hIi) 0⟩ instance quotient_ring_has_one {I: myset α} [hIi: is_ideal I]: has_one (q_ideal hIi) := ⟨@quotient.mk α (setoid_from_ideal hIi) 1⟩ instance quotient_ring_has_add {I: myset α} [hIi: is_ideal I]: has_add (q_ideal hIi) := ⟨quotient_ring_add hIi⟩ instance quotient_ring_has_mul {I: myset α} [hIi: is_ideal I]: has_mul (q_ideal hIi) := ⟨quotient_ring_mul hIi⟩ instance quotient_ring_has_neg {I: myset α} [hIi: is_ideal I]: has_neg (q_ideal hIi) := ⟨quotient_ring_neg hIi⟩ instance quotient_ring_is_ring {I: myset α} [hIi: is_ideal I]: myring (q_ideal hIi) := begin split, { intros a b c, cases coset_exists_rep hIi a with x hx, subst hx, cases coset_exists_rep hIi b with y hy, subst hy, cases coset_exists_rep hIi c with z hz, subst hz, apply congr_arg (coset hIi), apply add_assoc, }, { intro a, cases coset_exists_rep hIi a with x hx, subst hx, apply congr_arg (coset hIi), apply add_zero, }, { intro a, cases coset_exists_rep hIi a with x hx, subst hx, apply congr_arg (coset hIi), apply add_neg, }, { intros a b c, cases coset_exists_rep hIi a with x hx, subst hx, cases coset_exists_rep hIi b with y hy, subst hy, cases coset_exists_rep hIi c with z hz, subst hz, apply congr_arg (coset hIi), apply mul_assoc, }, { intros a b, cases coset_exists_rep hIi a with x hx, subst hx, cases coset_exists_rep hIi b with y hy, subst hy, apply congr_arg (coset hIi), apply mul_comm, }, { intro a, cases coset_exists_rep hIi a with x hx, subst hx, apply congr_arg (coset hIi), apply mul_one, }, { intros a b c, cases coset_exists_rep hIi a with x hx, subst hx, cases coset_exists_rep hIi b with y hy, subst hy, cases coset_exists_rep hIi c with z hz, subst hz, apply congr_arg (coset hIi), apply mul_add, }, end end myring end hidden
16fbb39e5296d089db9773e5aba32609adae0eea
e514e8b939af519a1d5e9b30a850769d058df4e9
/test/rewrite_search_discovery.lean
e86417d08401b53ef428ec3e6a0c598ac5ba9f66
[]
no_license
semorrison/lean-rewrite-search
dca317c5a52e170fb6ffc87c5ab767afb5e3e51a
e804b8f2753366b8957be839908230ee73f9e89f
refs/heads/master
1,624,051,754,485
1,614,160,817,000
1,614,160,817,000
162,660,605
0
1
null
null
null
null
UTF-8
Lean
false
false
4,532
lean
import tactic.rewrite_search open tactic.rewrite_search.discovery namespace tactic.rewrite_search.testing @[bundle] meta def algebraic_geometry : bundle := {} private axiom foo : [0] = [1] private axiom bar1 : [1] = [2] private axiom bar2 : [3] = [2] private axiom bar3 : [3] = [4] private def my_example (a : unit) : [[0],[0]] = [[4],[4]] := begin -- These don't work (because they don't know about the lemmas): success_if_fail { rewrite_search {} }, success_if_fail { rewrite_search_using [`search] {} }, -- But manually specifying them does: rewrite_search_with [foo, bar1, ← bar2, bar2, ← bar3] {}, end -- Let's add them to the `algebraic_geometry` bundle: attribute [search algebraic_geometry] foo bar1 bar2 bar3 -- Now because they are under the `search xxx` namespace whatever, -- the following "old" thing will succeed private example : [[0],[0]] = [[4],[4]] := begin rewrite_search_using [`search] {}, end -- And manually suggesting the `algebraic_geometry` bundle -- will work too: private example : [[0],[0]] = [[4],[4]] := begin rewrite_search {suggest := [`algebraic_geometry]} end -- Finally (and probably most commonly), you can suggest some number -- of bundles via: @[suggest] meta def my_suggestion := `algebraic_geometry -- or: @[suggest] meta def my_suggestion2 := [`algebraic_geometry, `default] -- The discovery code will accept both a name, or a list of names, -- as tagged with `[suggest]`. -- This is pretty cool, because any number of suggestions will be -- considered and are available to the `rewrite_search`er, not -- just the last one to be tagged `[suggest]` or something. -- -- Also, you can use `local attribute xxx` and imports to constrain -- the scope of where your suggestions apply, just like you were -- doing before with [search] in the category theory library. -- In terms of using `[search xxx]`, the attribute will accept any -- `xxx` which is a bundle which has already been declared as -- `@[bundle]`. You can also leave the `xxx` off and just annotate -- as `@[search]`, which will add the lemma to all of the default -- bundles. At the moment, the list consists of only one bundle, and -- it is called `default`. -- -- Lemmas can be part of multiple bundles too simultanously, either -- with annotations declared in separate places, or in the same place -- via the list syntax: @[search [algebraic_geometry, default]] private axiom bar4 : [3] = [4] -- (Here we add `bar4` to both `algebraic_geometry` and `default` -- at the same time.) -- When `rewrite_search` goes to run, it does not do several ugly -- attribute lookups over all bundles, and then all of their children. -- Instead, all of the membership is cached at *parse* time via -- some tricky (if I do say so myself ;)) hiding and updating of -- mutable state in annotations. In fact, we even cache the -- resolved names of which bundles you refer to when you annotate -- with `@[suggest]` (and then forget this state when you go out -- of scope). -- The idea is to have builtin bundles under -- -- tactic.rewrite_search.discovery.bundles -- -- which are imported everytime you include -- -- tactic.rewrite_search -- -- but as you can see, anyone can create one or modify an existing one -- on the fly. -- One more thing: the bundle names ARE NOT the names of objects -- declared of type bundle. They are anything you want, and can -- choosen as you like: @[bundle] meta def scotts_fave_bundle : bundle := {name := `the_real_name} -- And so this will work: @[search the_real_name] private axiom bar5 : [3] = [4] -- but this will not: -- UNCOMMENT ME -- @[search scotts_fave_bundle] private axiom bar4 : [3] = [4] -- This was intentional, because I didn't want the fully-scoped identifier -- to have to be available and `open`ed, or fully-qualified, when -- you go to write `@[search xxxx]`. -- -- I used some autoparams tricks to default the name of a bundle -- to its "lowest level" identifier (i.e. after the last dot). -- So in practise this means it always gets the name you expect, -- and you don't have to write it. -- We also fail gracefully if you try to break the rules: -- UNCOMMENT ME: -- @[suggest] meta def my_suggestion_rebel : ℕ := 0 -- UNCOMMENT ME: -- @[suggest] meta def my_suggestion_rebel2 : name := `fake_name -- Everything else gracefully handles errors, too: private example : tt := begin -- UNCOMMENT ME: -- rewrite_search {suggest := [`algssebraic_geometry]}, exact dec_trivial end end tactic.rewrite_search.testing