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
db765bb4a7737f42d1c40bd29c1d11e9dc3480ca
77b579b9a1295d88352dc4296573364c800e7441
/lean-tutorial/first.lean
5bc93d7ca61a1d09dc995147a25186b25f6d105f
[]
no_license
joelmccracken/playground
47a6286b9ab7227597841bec0a469fa5327ec8ba
6401d9a8eb67491ee589b85e0a84a0ff414938bd
refs/heads/master
1,685,073,507,705
1,684,476,111,000
1,684,476,111,000
7,369,222
0
0
null
null
null
null
UTF-8
Lean
false
false
80
lean
-- $ nix-shell https://github.com/leanprover/lean4/archive/master.tar.gz -A nix
5f461947dca0302a7bc063ae17b56b9e5fbc744f
7a51de2540dffbb2071672b4c6ace23ebf73b000
/src/matroidexamples.lean
7b7ae96ab42c7b91b5d07e0b4f73ada50fc4899e
[ "Apache-2.0" ]
permissive
bryangingechen/lean-matroids
112b3839ca8d067d13d2b9bc6c392b680968d5e8
37c2964208f5a0532ef0a3e525ace06d4d7bc156
refs/heads/master
1,624,161,348,754
1,575,521,642,000
1,575,521,642,000
149,715,612
4
1
Apache-2.0
1,608,525,997,000
1,537,508,725,000
Lean
UTF-8
Lean
false
false
4,422
lean
/- Examples of matroids. -/ import matroid data.equiv.list open finset variables {α : Type*} [decidable_eq α] {E : finset α} namespace matroid /-- the loopy matroid on `E : finset α` is the matroid where every element of `E` is a loop; equivalently, every subset of `E` is dependent -/ def loopy (E : finset α) : indep E := ⟨{∅}, powerset_mono.mpr $ empty_subset _, mem_singleton_self _, λ x y h1 h2, mem_singleton.mpr $ subset_empty.mp $ (mem_singleton.mp h1) ▸ h2, λ x y hx hy hcard, false.elim $ (nat.not_lt_zero $ card x) $ card_empty.subst $ (mem_singleton.mp hy).subst hcard⟩ /-- the free matroid is the matroid where every subset of the ground set is independent; sometimes called the trivial matroid -/ def free (E : finset α) : indep E := ⟨powerset E, subset.refl _, empty_mem_powerset _, λ x y h1 h2, mem_powerset.mpr $ subset.trans h2 $ mem_powerset.mp h1, λ x y hx hy hcard, exists.elim (exists_sdiff_of_card_lt hcard) $ λ e exy, ⟨e, exy, mem_powerset.mpr $ insert_subset.mpr ⟨mem_of_subset (mem_powerset.mp hy) (mem_sdiff.mp exy).1, mem_powerset.mp hx⟩⟩⟩ /-- the uniform matroid U_k on `E : finset α` is the matroid whose independent sets are all subsets of `E` of size `k` or less; Example 1.2.7 in Oxley -/ def uniform (k : ℕ) (E : finset α) : indep E := ⟨(powerset E).filter (λ x, card x ≤ k), filter_subset (powerset E), mem_filter.mpr ⟨empty_mem_powerset E, (@card_empty $ finset α).symm ▸ nat.zero_le k⟩, by { simp only [mem_powerset, and_imp, mem_filter], exact λ x y hx hcardx hy, ⟨subset.trans hy hx, le_trans (card_le_of_subset hy) hcardx⟩ }, by { simp only [mem_powerset, and_imp, mem_filter, mem_sdiff], exact λ x y hx hcardx hy hcardy hcard, exists.elim (exists_sdiff_of_card_lt hcard) $ λ e exy, ⟨e, ⟨mem_sdiff.mp exy, ⟨insert_subset.mpr ⟨mem_of_subset hy (mem_sdiff.mp exy).1, hx⟩, (card_insert_of_not_mem (mem_sdiff.mp exy).2).symm ▸ nat.succ_le_of_lt $ nat.lt_of_lt_of_le hcard hcardy⟩⟩⟩ }⟩ theorem loopy_eq_uniform_zero (E : finset α) : loopy E = uniform 0 E := suffices (loopy E).indep = (uniform 0 E).indep, from eq_of_indep_eq this, by { simp only [loopy, uniform, ext, mem_powerset, mem_filter, card_eq_zero, le_zero_iff_eq, iff_false, insert_empty_eq_singleton, mem_singleton, not_mem_empty], intro a, rw ←eq_empty_iff_forall_not_mem, exact ⟨λ ha, ⟨ha.symm ▸ empty_subset E, ha⟩, λ ha, ha.2⟩ } theorem free_eq_uniform_card (E : finset α) : free E = uniform (card E) E := suffices (free E).indep = (uniform (card E) E).indep, from eq_of_indep_eq this, by { simp only [free, uniform, ext, mem_powerset, mem_filter, empty_mem_powerset], exact λ a, ⟨λ ha, ⟨ha, card_le_of_subset ha⟩, λ ha, ha.1⟩ } #eval uniform 2 $ range 4 #eval (is_basis {1,3} $ uniform 2 $ range 4 : bool) #eval (is_basis {1,0,3} $ uniform 2 $ range 4 : bool) #eval bases_of_indep $ loopy $ range 5 #eval bases_of_indep $ uniform 3 $ range 5 #eval bases_of_indep $ free $ range 5 #eval (is_circuit {1,2} $ uniform 2 $ range 4 : bool) #eval (is_circuit {1,2,4} $ uniform 2 $ range 4 : bool) #eval (is_circuit {1,2,3,4} $ uniform 2 $ range 4 : bool) #eval circuits_of_indep $ loopy $ range 5 #eval circuits_of_indep $ uniform 3 $ range 5 #eval circuits_of_indep $ free $ range 5 #eval uniform 3 $ range 5 #eval indep_of_bases $ bases_of_indep $ uniform 3 $ range 5 #eval indep_of_circuits $ circuits_of_indep $ uniform 3 $ range 5 /- /- slow -/ #eval circuit_of_dep_of_insert_indep (dec_trivial : {0,2,3} ∈ (uniform 3 $ range 5).indep) (dec_trivial : 1 ∈ range 5) (dec_trivial : _ /-insert 3 {1,2} ∉ (uniform 2 $ range 4).indep -/) #eval fund_circ_of_basis (dec_trivial : is_basis {0,1,2} (uniform 3 $ range 5)) (dec_trivial : 4 ∈ range 5 \ {0,1,2}) -/ #eval fund_circ_of_basis (dec_trivial : is_basis ∅ (loopy $ range 5)) (dec_trivial : 4 ∈ range 5 \ ∅) #eval basis_containing_indep (dec_trivial : {0,2} ∈ (uniform 3 $ range 5).indep) #eval basis_of_subset (dec_trivial : {0,4,1,2,3} ⊆ range 5) (uniform 3 $ range 5) #eval rank_of_subset (dec_trivial : {0,4,1} ⊆ range 5) (uniform 3 $ range 5) #eval rank_of_subset (dec_trivial : {0,4,2,1} ⊆ range 5) (uniform 3 $ range 5) #eval rank_of_subset (dec_trivial : {0,4} ⊆ range 5) (loopy $ range 5) #eval rank_of_subset (dec_trivial : {0,4} ⊆ range 5) (free $ range 5) end matroid
8dfbdbeba81c34198f0ced9da0cd58302cd80e8c
7c4610454cf55b49f0c3cdaeb6b856eb3249cb2d
/src/torsion.lean
ec518375da77e485fa5126a5ea65f3b8d8b2087a
[]
no_license
101damnations/fg_over_pid
097be43e11c3680a3fd4b6de2265de393cf4d4ef
a1a587c455a54a802f6ff61b07bb033701e451a7
refs/heads/master
1,669,708,904,636
1,597,259,770,000
1,597,259,770,000
287,097,363
0
0
null
null
null
null
UTF-8
Lean
false
false
10,903
lean
import ring_theory.principal_ideal_domain linear_algebra.basic open unique_factorization_domain variables (R : Type*) (M : Type*) [add_comm_group M] open_locale classical section variables [integral_domain R] [add_comm_group M] [module R M] (a : M) /-- Given `a` in an R-module M, the ideal of `R` consisting of `r : R` such that `r • a = 0`. -/ @[reducible] def ord : ideal R := ⟨{r : R | r • a = 0}, zero_smul R a, λ x y (hx : _ = _) (hy : _ = _), show _ = _, by rw [add_smul x y a, hx, hy, zero_add], λ c x (h : _ = _), show _ = _, by rw [smul_eq_mul, ←smul_smul c x, h, smul_zero c]⟩ variables {R M} lemma mem_ord (b : R) : b ∈ ord R M a ↔ b • a = 0 := iff.rfl variables (R M) /-- Torsion submodule: elements `m` of M such that there exists a non-zero `r : R` such that `m • r = 0`. -/ def tors : submodule R M := ⟨{a₁ : M | ∃ r : R, r ≠ 0 ∧ r ∈ ord R M a₁}, ⟨1, ⟨ne.symm $ zero_ne_one, smul_zero 1⟩⟩, λ x y ⟨r, ⟨hr0, (hr : _ = _)⟩⟩ ⟨s, ⟨hs0, (hs : _ = _)⟩⟩, ⟨r * s, show _ ∧ (_ = _), from ⟨mul_ne_zero hr0 hs0, by rw [smul_add (r*s) x y, ←smul_smul r s y, hs, mul_comm, ←smul_smul s r]; norm_num [hr]⟩⟩, λ c x ⟨r, ⟨hr0, (hr : _ = _)⟩⟩, ⟨r, show _ ∧ (_ = _), from ⟨hr0, by rw [smul_smul r c, mul_comm, ←smul_smul c r, hr, smul_zero]⟩⟩⟩ end variables [integral_domain R] [is_principal_ideal_ring R] [decidable_eq (associates R)] [add_comm_group M] [module R M] (a : M) noncomputable instance ufd : unique_factorization_domain R := principal_ideal_ring.to_unique_factorization_domain variables {R M} --is private in mathlib /-- Given nonzero `x : R`, if `⟦a⟧` is an element of `R` modulo association and `a` is a factor of `x`, then `⟦a⟧` is irreducible. -/ theorem forall_map_mk_factors_irreducible (x : R) (hx : x ≠ 0) : ∀(a : associates R), a ∈ multiset.map associates.mk (factors x) → irreducible a := begin assume a ha, rcases multiset.mem_map.1 ha with ⟨c, hc, rfl⟩, exact (associates.irreducible_mk_iff c).2 (irreducible_factors hx _ hc) end /-- Given prime `p : R` and `⟦a⟧`, if `⟦a⟧` is in an i-tuple of `⟦p⟧`'s, `⟦a⟧` is irred. -/ lemma mem_repeat_prime_irred {p : R} (i : ℕ) (hp : prime p) (a : associates R) (h : a ∈ multiset.repeat (associates.mk p) i) : irreducible a := by rw multiset.eq_of_mem_repeat h; exact (associates.irreducible_mk_iff p).2 (irreducible_iff_prime.2 hp) /-- Given prime `p : R`, the tuple of irreducible factors of `p^i` (with multiplicity) modulo assocition is an i-tuple of `⟦p⟧`'s. -/ lemma factors_prime_pow {p : R} (i : ℕ) (hp : prime p) : (factors (p^i)).map associates.mk = multiset.repeat (associates.mk p) i := associates.unique' (forall_map_mk_factors_irreducible (p ^ i) $ pow_ne_zero i hp.1) (mem_repeat_prime_irred i hp) $ by rw [associates.prod_mk, associates.mk_eq_mk_iff_associated.2 $ factors_prod $ pow_ne_zero i hp.1, multiset.prod_repeat, associates.mk_pow] /-- Given prime `p : R` and the equivalence relation 'association', if `⟦r⟧` divides `⟦p ^ i⟧`, `r ≠ 0`. -/ lemma ne_zero_of_le_prime_pow {r p : R} {i : ℕ} (hp : prime p) (hr : associates.mk r ≤ associates.mk (p^i)) : r ≠ 0 := by intro h; rw h at hr; exact absurd (zero_dvd_iff.1 $ associates.dvd_of_mk_le_mk hr) (pow_ne_zero i hp.1) /-- Given prime `p : R` and the equivalence relation 'association', if `⟦r⟧` divides `⟦p ^ i⟧` then there exists `j ≤ i` such that `⟦r⟧ = ⟦p ^ j⟧`. -/ lemma eq_prime_pow_of_dvd_prime_pow (r p : R) (i : ℕ) (hp : prime p) (hr : associates.mk r ≤ associates.mk (p^i)) : ∃ j : ℕ, j ≤ i ∧ associates.mk r = associates.mk (p^j) := begin have : (factors r).map associates.mk ≤ multiset.repeat (associates.mk p) i, by rwa [←factors_prime_pow i hp, ←associates.prod_le_prod_iff_le (forall_map_mk_factors_irreducible r $ ne_zero_of_le_prime_pow hp hr) $ forall_map_mk_factors_irreducible (p^i) $ pow_ne_zero i hp.1, associates.prod_mk, associates.prod_mk, associates.mk_eq_mk_iff_associated.2 $ factors_prod $ ne_zero_of_le_prime_pow hp hr, associates.mk_eq_mk_iff_associated.2 $ factors_prod $ pow_ne_zero i hp.1], let j := multiset.card (multiset.map associates.mk $ factors r), exact ⟨j, ⟨by rw ←multiset.card_repeat (associates.mk p) i; exact multiset.card_le_of_le this, by { have H1 : multiset.map associates.mk (factors r) = multiset.repeat (associates.mk p) j, by rw multiset.eq_repeat'; intros; rw multiset.eq_of_mem_repeat (multiset.mem_of_le this H), rw [←associates.mk_eq_mk_iff_associated.2 $ factors_prod $ ne_zero_of_le_prime_pow hp hr, ←associates.prod_mk, H1, multiset.prod_repeat, associates.mk_pow]}⟩⟩ end /-- if `a b : R` are equal up to association, they generate the same ideal. -/ lemma eq_span_of_eq_mk {α : Type*} [comm_ring α] {a b : α} (H: associates.mk a = associates.mk b) : ideal.span ({a} : set α) = ideal.span ({b}) := begin apply ideal.ext, intro, split, intro h, cases associates.mk_eq_mk_iff_associated.1 H.symm with u hu, swap, intro h, cases associates.mk_eq_mk_iff_associated.1 H with u hu, all_goals { cases exists_eq_mul_right_of_dvd (ideal.mem_span_singleton.1 h) with w hw, apply ideal.mem_span_singleton.2, use (↑u*w), rw [←mul_assoc, hu, hw]}, end /-- Given `a : M` and prime `p : R` such that `p ^ i = 0`, there exists `j ≤ i` such that the ideal generated by `p ^ j` equals the ideal of elements `r : R` such that `r • a = 0`. -/ lemma prime_pow_le (a : M) (p : R) (i : ℕ) (hp : prime p) (Ha : p^i•a = 0) : ∃ j : ℕ, j ≤ i ∧ ord R M a = ideal.span ({p^j}: set R) := begin rw ←submodule.is_principal.span_singleton_generator (ord R M a), let r := submodule.is_principal.generator (ord R M a), have : associates.mk r ≤ associates.mk (p^i), from associates.mk_le_mk_of_dvd ((submodule.is_principal.mem_iff_generator_dvd $ ord R M a).mp Ha), cases (eq_prime_pow_of_dvd_prime_pow r p i hp this) with j hj, exact ⟨j, hj.1, eq_span_of_eq_mk hj.2⟩, end lemma ord_ideal_zero_eq_top : ord R M 0 = ⊤ := eq_top_iff.2 $ λ x h, smul_zero x /-- Given `x, y` in a torsion R-module `M`, and `n m : ℕ, p : R` such that the ideal generated by `p ^ n` is the set of `r : R` such that `r • x = 0` and the ideal generated by `p ^ m` is the set of `r : R` such that `r • y = 0`, we have `p ^ max{n, m} • x + p ^ max{n, m} • y = 0`. -/ lemma pow_aux {x y : tors R M} {n m : ℕ} {p : R} (hn : ord R (tors R M) x = ideal.span ({p ^ n} : set R)) (hm : ord R (tors R M) y = ideal.span ({p ^ m} : set R)) : p ^ max n m • x + p ^ max n m • y = 0 := begin conv in (p ^ max n m • x) { rw [←nat.sub_add_cancel (le_max_left n m), pow_add p (max n m - n) n, mul_smul]}, conv in (p ^ max n m • y) { rw [←nat.sub_add_cancel (le_max_right n m), pow_add p (max n m - m) m, mul_smul]}, suffices h : p ^ n ∈ ord R (tors R M) x ∧ p ^ m ∈ ord R (tors R M) y, by change _ = _ ∧ _ = _ at h; norm_num [h], rw [hn, hm], exact ⟨ideal.mem_span_singleton'.2 ⟨1, one_mul _⟩, ideal.mem_span_singleton'.2 ⟨1, one_mul _⟩⟩, end /-- Given `a : M` and prime `p : R`, if the ideal generated by `p ^ i` equals the set `r : R` such that `r • a = 0`, then `p ^ i • a = 0`. -/ lemma eq_zero_of_ord_pow {a : M} {p : R} (i : ℕ) (hp : prime p) (H : ord R M a = ideal.span ({p^i}: set R)) : p^i•a = 0 := show p ^ i ∈ ord R M a, by rw H; exact ideal.mem_span_singleton'.2 ⟨1, one_mul _⟩ /-- Given `a : M` and prime `p : R`, if `p ^ i • ≠ 0` and `p ^ (i + 1) • a = 0`, then the ideal generated by `p ^ (i + 1)` equals the set of `r : R` such that `r • a = 0`. -/ lemma ord_eq_of_pow {a : M} {p : R} {i : ℕ} (hp : prime p) (hn0 : p ^ i • a ≠ 0) (h0 : p ^ (i + 1) • a = 0) : ord R M a = ideal.span ({p^(i + 1)}: set R) := begin cases prime_pow_le _ _ _ hp h0 with j hj, have : i + 1 ≤ j, by {rw ←not_lt, intro hnot, have := nat.lt_succ_iff.1 hnot, cases nat.le.dest this with k hk, rw ←hk at hn0, rw pow_add at hn0, rw mul_comm at hn0, rw mul_smul at hn0, have hj0 := eq_zero_of_ord_pow j hp hj.2, rw hj0 at hn0, rw smul_zero at hn0, exact hn0 rfl, }, rw nat.le_antisymm this hj.1, exact hj.2, end variables (M) /-- Given a prime `p : R`, we define a submodule of a torsion R-module comprising `a` such that there exists `n : ℕ` for which the ideal generated by `p ^ n` equals the set of `r : R` such that `r • a = 0`. -/ def prime_pow_ord (p : {p // prime p}) : submodule R (tors R M) := ⟨ {a : tors R M | ∃ n : ℕ, ord R (tors R M) a = ideal.span ({p.1 ^ n} : set R)}, ⟨0, by erw [pow_zero _, ideal.span_singleton_one, ord_ideal_zero_eq_top]⟩, λ x y ⟨n, (hn : _ = _)⟩ ⟨m, (hm : _ = _)⟩, by { cases prime_pow_le (x + y) p.1 (max n m) p.2 (show _, by {rw smul_add (p.1 ^ (max n m)) x y, exact pow_aux hn hm}), exact ⟨w, h.2⟩}, λ c x ⟨n, (hn : _ = _)⟩, by { cases prime_pow_le (c•x) p.1 n p.2 (show _, by { rw [←mul_smul, mul_comm, mul_smul], suffices : p.1 ^ n • x = 0, by {rw this, rw smul_zero}, show p.1 ^ n ∈ ord R (tors R M) x, rw hn, exact ideal.mem_span_singleton'.2 ⟨1, one_mul _⟩}), exact ⟨w, h.2⟩}⟩ variables {M} lemma exists_rep (A : submodule R M) (x : submodule.quotient A) : ∃ a : M, submodule.quotient.mk a = x := @quotient.exists_rep M (submodule.quotient_rel A) x /-- The quotient of a module by its torsion submodule is torsion free. -/ variables (R M) lemma tors_free_of_quotient : tors R (submodule.quotient $ tors R M) = ⊥ := by apply submodule.ext; exact (λ x, iff.intro (λ h, by {rw [submodule.mem_bot], rcases h with ⟨r, ⟨hr0, hr⟩⟩, cases (exists_rep (tors R M) x) with a ha, have h : r • a ∈ tors R M, by {apply (submodule.quotient.mk_eq_zero (tors R M)).1, rw [submodule.quotient.mk_smul, ha], exact hr}, rcases h with ⟨s, ⟨hs0, hs⟩⟩, rw [ha.symm], apply (submodule.quotient.mk_eq_zero (tors R M)).2, exact ⟨s*r, ⟨mul_ne_zero hs0 hr0, by {rw mem_ord at hs ⊢, simp [←smul_smul, hs]}⟩⟩}) (λ h, by {rw [submodule.mem_bot] at h, rw h, exact ⟨1, ⟨ne.symm $ zero_ne_one, by {rw ord_ideal_zero_eq_top, trivial} ⟩⟩})) /-- If `p : R` is prime, it's not nilpotent. -/ lemma ne_zero_of_prime_pow {p : R} (n : ℕ) (hp : prime p) : p ^ n ≠ 0 := begin induction n with n hn, simp, intro hnot, rw pow_succ at hnot, cases mul_eq_zero.1 hnot with hl hr, refine @not_prime_zero R _ _, rw ←hl, exact hp, exact hn hr, end
c2e8a2582fcc2c7b3db326419bebf65cd843dfe2
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/analysis/convex/cone.lean
954fdf43def751f7a556c923b1ad544d075ee7e5
[ "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
25,259
lean
/- Copyright (c) 2020 Yury Kudryashov All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Frédéric Dupuis -/ import analysis.convex.hull import analysis.inner_product_space.basic /-! # Convex cones In a `𝕜`-module `E`, we define a convex cone as a set `s` such that `a • x + b • y ∈ s` whenever `x, y ∈ s` and `a, b > 0`. We prove that convex cones form a `complete_lattice`, and define their images (`convex_cone.map`) and preimages (`convex_cone.comap`) under linear maps. We define pointed, blunt, flat and salient cones, and prove the correspondence between convex cones and ordered modules. We also define `convex.to_cone` to be the minimal cone that includes a given convex set. We define `set.inner_dual_cone` to be the cone consisting of all points `y` such that for all points `x` in a given set `0 ≤ ⟪ x, y ⟫`. ## Main statements We prove two extension theorems: * `riesz_extension`: [M. Riesz extension theorem](https://en.wikipedia.org/wiki/M._Riesz_extension_theorem) says that if `s` is a convex cone in a real vector space `E`, `p` is a submodule of `E` such that `p + s = E`, and `f` is a linear function `p → ℝ` which is nonnegative on `p ∩ s`, then there exists a globally defined linear function `g : E → ℝ` that agrees with `f` on `p`, and is nonnegative on `s`. * `exists_extension_of_le_sublinear`: Hahn-Banach theorem: if `N : E → ℝ` is a sublinear map, `f` is a linear map defined on a subspace of `E`, and `f x ≤ N x` for all `x` in the domain of `f`, then `f` can be extended to the whole space to a linear map `g` such that `g x ≤ N x` for all `x` ## Implementation notes While `convex 𝕜` is a predicate on sets, `convex_cone 𝕜 E` is a bundled convex cone. ## References * https://en.wikipedia.org/wiki/Convex_cone -/ open set linear_map open_locale classical pointwise variables {𝕜 E F G : Type*} /-! ### Definition of `convex_cone` and basic properties -/ section definitions variables (𝕜 E) [ordered_semiring 𝕜] /-- A convex cone is a subset `s` of a `𝕜`-module such that `a • x + b • y ∈ s` whenever `a, b > 0` and `x, y ∈ s`. -/ structure convex_cone [add_comm_monoid E] [has_scalar 𝕜 E] := (carrier : set E) (smul_mem' : ∀ ⦃c : 𝕜⦄, 0 < c → ∀ ⦃x : E⦄, x ∈ carrier → c • x ∈ carrier) (add_mem' : ∀ ⦃x⦄ (hx : x ∈ carrier) ⦃y⦄ (hy : y ∈ carrier), x + y ∈ carrier) end definitions variables {𝕜 E} namespace convex_cone section ordered_semiring variables [ordered_semiring 𝕜] [add_comm_monoid E] section has_scalar variables [has_scalar 𝕜 E] (S T : convex_cone 𝕜 E) instance : has_coe (convex_cone 𝕜 E) (set E) := ⟨convex_cone.carrier⟩ instance : has_mem E (convex_cone 𝕜 E) := ⟨λ m S, m ∈ S.carrier⟩ instance : has_le (convex_cone 𝕜 E) := ⟨λ S T, S.carrier ⊆ T.carrier⟩ instance : has_lt (convex_cone 𝕜 E) := ⟨λ S T, S.carrier ⊂ T.carrier⟩ @[simp, norm_cast] lemma mem_coe {x : E} : x ∈ (S : set E) ↔ x ∈ S := iff.rfl @[simp] lemma mem_mk {s : set E} {h₁ h₂ x} : x ∈ @mk 𝕜 _ _ _ _ s h₁ h₂ ↔ x ∈ s := iff.rfl /-- Two `convex_cone`s are equal if the underlying sets are equal. -/ theorem ext' {S T : convex_cone 𝕜 E} (h : (S : set E) = T) : S = T := by cases S; cases T; congr' /-- Two `convex_cone`s are equal if and only if the underlying sets are equal. -/ protected theorem ext'_iff {S T : convex_cone 𝕜 E} : (S : set E) = T ↔ S = T := ⟨ext', λ h, h ▸ rfl⟩ /-- Two `convex_cone`s are equal if they have the same elements. -/ @[ext] theorem ext {S T : convex_cone 𝕜 E} (h : ∀ x, x ∈ S ↔ x ∈ T) : S = T := ext' $ set.ext h lemma smul_mem {c : 𝕜} {x : E} (hc : 0 < c) (hx : x ∈ S) : c • x ∈ S := S.smul_mem' hc hx lemma add_mem ⦃x⦄ (hx : x ∈ S) ⦃y⦄ (hy : y ∈ S) : x + y ∈ S := S.add_mem' hx hy instance : has_inf (convex_cone 𝕜 E) := ⟨λ S T, ⟨S ∩ T, λ c hc x hx, ⟨S.smul_mem hc hx.1, T.smul_mem hc hx.2⟩, λ x hx y hy, ⟨S.add_mem hx.1 hy.1, T.add_mem hx.2 hy.2⟩⟩⟩ lemma coe_inf : ((S ⊓ T : convex_cone 𝕜 E) : set E) = ↑S ∩ ↑T := rfl lemma mem_inf {x} : x ∈ S ⊓ T ↔ x ∈ S ∧ x ∈ T := iff.rfl instance : has_Inf (convex_cone 𝕜 E) := ⟨λ S, ⟨⋂ s ∈ S, ↑s, λ c hc x hx, mem_bInter $ λ s hs, s.smul_mem hc $ mem_Inter₂.1 hx s hs, λ x hx y hy, mem_bInter $ λ s hs, s.add_mem (mem_Inter₂.1 hx s hs) (mem_Inter₂.1 hy s hs)⟩⟩ lemma mem_Inf {x : E} {S : set (convex_cone 𝕜 E)} : x ∈ Inf S ↔ ∀ s ∈ S, x ∈ s := mem_Inter₂ variables (𝕜) instance : has_bot (convex_cone 𝕜 E) := ⟨⟨∅, λ c hc x, false.elim, λ x, false.elim⟩⟩ lemma mem_bot (x : E) : x ∈ (⊥ : convex_cone 𝕜 E) = false := rfl instance : has_top (convex_cone 𝕜 E) := ⟨⟨univ, λ c hc x hx, mem_univ _, λ x hx y hy, mem_univ _⟩⟩ lemma mem_top (x : E) : x ∈ (⊤ : convex_cone 𝕜 E) := mem_univ x instance : complete_lattice (convex_cone 𝕜 E) := { le := (≤), lt := (<), bot := (⊥), bot_le := λ S x, false.elim, top := (⊤), le_top := λ S x hx, mem_top 𝕜 x, inf := (⊓), Inf := has_Inf.Inf, sup := λ a b, Inf {x | a ≤ x ∧ b ≤ x}, Sup := λ s, Inf {T | ∀ S ∈ s, S ≤ T}, le_sup_left := λ a b, λ x hx, mem_Inf.2 $ λ s hs, hs.1 hx, le_sup_right := λ a b, λ x hx, mem_Inf.2 $ λ s hs, hs.2 hx, sup_le := λ a b c ha hb x hx, mem_Inf.1 hx c ⟨ha, hb⟩, le_inf := λ a b c ha hb x hx, ⟨ha hx, hb hx⟩, inf_le_left := λ a b x, and.left, inf_le_right := λ a b x, and.right, le_Sup := λ s p hs x hx, mem_Inf.2 $ λ t ht, ht p hs hx, Sup_le := λ s p hs x hx, mem_Inf.1 hx p hs, le_Inf := λ s a ha x hx, mem_Inf.2 $ λ t ht, ha t ht hx, Inf_le := λ s a ha x hx, mem_Inf.1 hx _ ha, .. partial_order.lift (coe : convex_cone 𝕜 E → set E) (λ a b, ext') } instance : inhabited (convex_cone 𝕜 E) := ⟨⊥⟩ end has_scalar section module variables [module 𝕜 E] (S : convex_cone 𝕜 E) protected lemma convex : convex 𝕜 (S : set E) := convex_iff_forall_pos.2 $ λ x y hx hy a b ha hb hab, S.add_mem (S.smul_mem ha hx) (S.smul_mem hb hy) end module end ordered_semiring section linear_ordered_field variables [linear_ordered_field 𝕜] section add_comm_monoid variables [add_comm_monoid E] [add_comm_monoid F] [add_comm_monoid G] section mul_action variables [mul_action 𝕜 E] (S : convex_cone 𝕜 E) lemma smul_mem_iff {c : 𝕜} (hc : 0 < c) {x : E} : c • x ∈ S ↔ x ∈ S := ⟨λ h, inv_smul_smul₀ hc.ne' x ▸ S.smul_mem (inv_pos.2 hc) h, S.smul_mem hc⟩ end mul_action section module variables [module 𝕜 E] [module 𝕜 F] [module 𝕜 G] /-- The image of a convex cone under a `𝕜`-linear map is a convex cone. -/ def map (f : E →ₗ[𝕜] F) (S : convex_cone 𝕜 E) : convex_cone 𝕜 F := { carrier := f '' S, smul_mem' := λ c hc y ⟨x, hx, hy⟩, hy ▸ f.map_smul c x ▸ mem_image_of_mem f (S.smul_mem hc hx), add_mem' := λ y₁ ⟨x₁, hx₁, hy₁⟩ y₂ ⟨x₂, hx₂, hy₂⟩, hy₁ ▸ hy₂ ▸ f.map_add x₁ x₂ ▸ mem_image_of_mem f (S.add_mem hx₁ hx₂) } lemma map_map (g : F →ₗ[𝕜] G) (f : E →ₗ[𝕜] F) (S : convex_cone 𝕜 E) : (S.map f).map g = S.map (g.comp f) := ext' $ image_image g f S @[simp] lemma map_id (S : convex_cone 𝕜 E) : S.map linear_map.id = S := ext' $ image_id _ /-- The preimage of a convex cone under a `𝕜`-linear map is a convex cone. -/ def comap (f : E →ₗ[𝕜] F) (S : convex_cone 𝕜 F) : convex_cone 𝕜 E := { carrier := f ⁻¹' S, smul_mem' := λ c hc x hx, by { rw [mem_preimage, f.map_smul c], exact S.smul_mem hc hx }, add_mem' := λ x hx y hy, by { rw [mem_preimage, f.map_add], exact S.add_mem hx hy } } @[simp] lemma comap_id (S : convex_cone 𝕜 E) : S.comap linear_map.id = S := ext' preimage_id lemma comap_comap (g : F →ₗ[𝕜] G) (f : E →ₗ[𝕜] F) (S : convex_cone 𝕜 G) : (S.comap g).comap f = S.comap (g.comp f) := ext' $ preimage_comp.symm @[simp] lemma mem_comap {f : E →ₗ[𝕜] F} {S : convex_cone 𝕜 F} {x : E} : x ∈ S.comap f ↔ f x ∈ S := iff.rfl end module end add_comm_monoid section ordered_add_comm_group variables [ordered_add_comm_group E] [module 𝕜 E] /-- Constructs an ordered module given an `ordered_add_comm_group`, a cone, and a proof that the order relation is the one defined by the cone. -/ lemma to_ordered_smul (S : convex_cone 𝕜 E) (h : ∀ x y : E, x ≤ y ↔ y - x ∈ S) : ordered_smul 𝕜 E := ordered_smul.mk' begin intros x y z xy hz, rw [h (z • x) (z • y), ←smul_sub z y x], exact smul_mem S hz ((h x y).mp xy.le), end end ordered_add_comm_group end linear_ordered_field /-! ### Convex cones with extra properties -/ section ordered_semiring variables [ordered_semiring 𝕜] section add_comm_monoid variables [add_comm_monoid E] [has_scalar 𝕜 E] (S : convex_cone 𝕜 E) /-- A convex cone is pointed if it includes `0`. -/ def pointed (S : convex_cone 𝕜 E) : Prop := (0 : E) ∈ S /-- A convex cone is blunt if it doesn't include `0`. -/ def blunt (S : convex_cone 𝕜 E) : Prop := (0 : E) ∉ S lemma pointed_iff_not_blunt (S : convex_cone 𝕜 E) : S.pointed ↔ ¬S.blunt := ⟨λ h₁ h₂, h₂ h₁, not_not.mp⟩ lemma blunt_iff_not_pointed (S : convex_cone 𝕜 E) : S.blunt ↔ ¬S.pointed := by rw [pointed_iff_not_blunt, not_not] end add_comm_monoid section add_comm_group variables [add_comm_group E] [has_scalar 𝕜 E] (S : convex_cone 𝕜 E) /-- A convex cone is flat if it contains some nonzero vector `x` and its opposite `-x`. -/ def flat : Prop := ∃ x ∈ S, x ≠ (0 : E) ∧ -x ∈ S /-- A convex cone is salient if it doesn't include `x` and `-x` for any nonzero `x`. -/ def salient : Prop := ∀ x ∈ S, x ≠ (0 : E) → -x ∉ S lemma salient_iff_not_flat (S : convex_cone 𝕜 E) : S.salient ↔ ¬S.flat := begin split, { rintros h₁ ⟨x, xs, H₁, H₂⟩, exact h₁ x xs H₁ H₂ }, { intro h, unfold flat at h, push_neg at h, exact h } end /-- A flat cone is always pointed (contains `0`). -/ lemma flat.pointed {S : convex_cone 𝕜 E} (hS : S.flat) : S.pointed := begin obtain ⟨x, hx, _, hxneg⟩ := hS, rw [pointed, ←add_neg_self x], exact add_mem S hx hxneg, end /-- A blunt cone (one not containing `0`) is always salient. -/ lemma blunt.salient {S : convex_cone 𝕜 E} : S.blunt → S.salient := begin rw [salient_iff_not_flat, blunt_iff_not_pointed], exact mt flat.pointed, end /-- A pointed convex cone defines a preorder. -/ def to_preorder (h₁ : S.pointed) : preorder E := { le := λ x y, y - x ∈ S, le_refl := λ x, by change x - x ∈ S; rw [sub_self x]; exact h₁, le_trans := λ x y z xy zy, by simpa using add_mem S zy xy } /-- A pointed and salient cone defines a partial order. -/ def to_partial_order (h₁ : S.pointed) (h₂ : S.salient) : partial_order E := { le_antisymm := begin intros a b ab ba, by_contradiction h, have h' : b - a ≠ 0 := λ h'', h (eq_of_sub_eq_zero h'').symm, have H := h₂ (b-a) ab h', rw neg_sub b a at H, exact H ba, end, ..to_preorder S h₁ } /-- A pointed and salient cone defines an `ordered_add_comm_group`. -/ def to_ordered_add_comm_group (h₁ : S.pointed) (h₂ : S.salient) : ordered_add_comm_group E := { add_le_add_left := begin intros a b hab c, change c + b - (c + a) ∈ S, rw add_sub_add_left_eq_sub, exact hab, end, ..to_partial_order S h₁ h₂, ..show add_comm_group E, by apply_instance } end add_comm_group end ordered_semiring /-! ### Positive cone of an ordered module -/ section positive_cone variables (𝕜 E) [ordered_semiring 𝕜] [ordered_add_comm_group E] [module 𝕜 E] [ordered_smul 𝕜 E] /-- The positive cone is the convex cone formed by the set of nonnegative elements in an ordered module. -/ def positive_cone : convex_cone 𝕜 E := { carrier := {x | 0 ≤ x}, smul_mem' := begin rintro c hc x (hx : _ ≤ _), rw ←smul_zero c, exact smul_le_smul_of_nonneg hx hc.le, end, add_mem' := λ x (hx : _ ≤ _) y (hy : _ ≤ _), add_nonneg hx hy } /-- The positive cone of an ordered module is always salient. -/ lemma salient_positive_cone : salient (positive_cone 𝕜 E) := λ x xs hx hx', lt_irrefl (0 : E) (calc 0 < x : lt_of_le_of_ne xs hx.symm ... ≤ x + (-x) : le_add_of_nonneg_right hx' ... = 0 : add_neg_self x) /-- The positive cone of an ordered module is always pointed. -/ lemma pointed_positive_cone : pointed (positive_cone 𝕜 E) := le_refl 0 end positive_cone end convex_cone /-! ### Cone over a convex set -/ section cone_from_convex variables [linear_ordered_field 𝕜] [ordered_add_comm_group E] [module 𝕜 E] namespace convex /-- The set of vectors proportional to those in a convex set forms a convex cone. -/ def to_cone (s : set E) (hs : convex 𝕜 s) : convex_cone 𝕜 E := begin apply convex_cone.mk (⋃ (c : 𝕜) (H : 0 < c), c • s); simp only [mem_Union, mem_smul_set], { rintros c c_pos _ ⟨c', c'_pos, x, hx, rfl⟩, exact ⟨c * c', mul_pos c_pos c'_pos, x, hx, (smul_smul _ _ _).symm⟩ }, { rintros _ ⟨cx, cx_pos, x, hx, rfl⟩ _ ⟨cy, cy_pos, y, hy, rfl⟩, have : 0 < cx + cy, from add_pos cx_pos cy_pos, refine ⟨_, this, _, convex_iff_div.1 hs hx hy cx_pos.le cy_pos.le this, _⟩, simp only [smul_add, smul_smul, mul_div_assoc', mul_div_cancel_left _ this.ne'] } end variables {s : set E} (hs : convex 𝕜 s) {x : E} lemma mem_to_cone : x ∈ hs.to_cone s ↔ ∃ (c : 𝕜), 0 < c ∧ ∃ y ∈ s, c • y = x := by simp only [to_cone, convex_cone.mem_mk, mem_Union, mem_smul_set, eq_comm, exists_prop] lemma mem_to_cone' : x ∈ hs.to_cone s ↔ ∃ (c : 𝕜), 0 < c ∧ c • x ∈ s := begin refine hs.mem_to_cone.trans ⟨_, _⟩, { rintros ⟨c, hc, y, hy, rfl⟩, exact ⟨c⁻¹, inv_pos.2 hc, by rwa [smul_smul, inv_mul_cancel hc.ne', one_smul]⟩ }, { rintros ⟨c, hc, hcx⟩, exact ⟨c⁻¹, inv_pos.2 hc, _, hcx, by rw [smul_smul, inv_mul_cancel hc.ne', one_smul]⟩ } end lemma subset_to_cone : s ⊆ hs.to_cone s := λ x hx, hs.mem_to_cone'.2 ⟨1, zero_lt_one, by rwa one_smul⟩ /-- `hs.to_cone s` is the least cone that includes `s`. -/ lemma to_cone_is_least : is_least { t : convex_cone 𝕜 E | s ⊆ t } (hs.to_cone s) := begin refine ⟨hs.subset_to_cone, λ t ht x hx, _⟩, rcases hs.mem_to_cone.1 hx with ⟨c, hc, y, hy, rfl⟩, exact t.smul_mem hc (ht hy) end lemma to_cone_eq_Inf : hs.to_cone s = Inf { t : convex_cone 𝕜 E | s ⊆ t } := hs.to_cone_is_least.is_glb.Inf_eq.symm end convex lemma convex_hull_to_cone_is_least (s : set E) : is_least {t : convex_cone 𝕜 E | s ⊆ t} ((convex_convex_hull 𝕜 s).to_cone _) := begin convert (convex_convex_hull 𝕜 s).to_cone_is_least, ext t, exact ⟨λ h, convex_hull_min h t.convex, (subset_convex_hull 𝕜 s).trans⟩, end lemma convex_hull_to_cone_eq_Inf (s : set E) : (convex_convex_hull 𝕜 s).to_cone _ = Inf {t : convex_cone 𝕜 E | s ⊆ t} := (convex_hull_to_cone_is_least s).is_glb.Inf_eq.symm end cone_from_convex /-! ### M. Riesz extension theorem Given a convex cone `s` in a vector space `E`, a submodule `p`, and a linear `f : p → ℝ`, assume that `f` is nonnegative on `p ∩ s` and `p + s = E`. Then there exists a globally defined linear function `g : E → ℝ` that agrees with `f` on `p`, and is nonnegative on `s`. We prove this theorem using Zorn's lemma. `riesz_extension.step` is the main part of the proof. It says that if the domain `p` of `f` is not the whole space, then `f` can be extended to a larger subspace `p ⊔ span ℝ {y}` without breaking the non-negativity condition. In `riesz_extension.exists_top` we use Zorn's lemma to prove that we can extend `f` to a linear map `g` on `⊤ : submodule E`. Mathematically this is the same as a linear map on `E` but in Lean `⊤ : submodule E` is isomorphic but is not equal to `E`. In `riesz_extension` we use this isomorphism to prove the theorem. -/ variables [add_comm_group E] [module ℝ E] namespace riesz_extension open submodule variables (s : convex_cone ℝ E) (f : linear_pmap ℝ E ℝ) /-- Induction step in M. Riesz extension theorem. Given a convex cone `s` in a vector space `E`, a partially defined linear map `f : f.domain → ℝ`, assume that `f` is nonnegative on `f.domain ∩ p` and `p + s = E`. If `f` is not defined on the whole `E`, then we can extend it to a larger submodule without breaking the non-negativity condition. -/ lemma step (nonneg : ∀ x : f.domain, (x : E) ∈ s → 0 ≤ f x) (dense : ∀ y, ∃ x : f.domain, (x : E) + y ∈ s) (hdom : f.domain ≠ ⊤) : ∃ g, f < g ∧ ∀ x : g.domain, (x : E) ∈ s → 0 ≤ g x := begin obtain ⟨y, -, hy⟩ : ∃ (y : E) (h : y ∈ ⊤), y ∉ f.domain, { exact @set_like.exists_of_lt (submodule ℝ E) _ _ _ _ (lt_top_iff_ne_top.2 hdom) }, obtain ⟨c, le_c, c_le⟩ : ∃ c, (∀ x : f.domain, -(x:E) - y ∈ s → f x ≤ c) ∧ (∀ x : f.domain, (x:E) + y ∈ s → c ≤ f x), { set Sp := f '' {x : f.domain | (x:E) + y ∈ s}, set Sn := f '' {x : f.domain | -(x:E) - y ∈ s}, suffices : (upper_bounds Sn ∩ lower_bounds Sp).nonempty, by simpa only [set.nonempty, upper_bounds, lower_bounds, ball_image_iff] using this, refine exists_between_of_forall_le (nonempty.image f _) (nonempty.image f (dense y)) _, { rcases (dense (-y)) with ⟨x, hx⟩, rw [← neg_neg x, coe_neg, ← sub_eq_add_neg] at hx, exact ⟨_, hx⟩ }, rintros a ⟨xn, hxn, rfl⟩ b ⟨xp, hxp, rfl⟩, have := s.add_mem hxp hxn, rw [add_assoc, add_sub_cancel'_right, ← sub_eq_add_neg, ← coe_sub] at this, replace := nonneg _ this, rwa [f.map_sub, sub_nonneg] at this }, have hy' : y ≠ 0, from λ hy₀, hy (hy₀.symm ▸ zero_mem _), refine ⟨f.sup_span_singleton y (-c) hy, _, _⟩, { refine lt_iff_le_not_le.2 ⟨f.left_le_sup _ _, λ H, _⟩, replace H := linear_pmap.domain_mono.monotone H, rw [linear_pmap.domain_sup_span_singleton, sup_le_iff, span_le, singleton_subset_iff] at H, exact hy H.2 }, { rintros ⟨z, hz⟩ hzs, rcases mem_sup.1 hz with ⟨x, hx, y', hy', rfl⟩, rcases mem_span_singleton.1 hy' with ⟨r, rfl⟩, simp only [subtype.coe_mk] at hzs, erw [linear_pmap.sup_span_singleton_apply_mk _ _ _ _ _ hx, smul_neg, ← sub_eq_add_neg, sub_nonneg], rcases lt_trichotomy r 0 with hr|hr|hr, { have : -(r⁻¹ • x) - y ∈ s, by rwa [← s.smul_mem_iff (neg_pos.2 hr), smul_sub, smul_neg, neg_smul, neg_neg, smul_smul, mul_inv_cancel hr.ne, one_smul, sub_eq_add_neg, neg_smul, neg_neg], replace := le_c (r⁻¹ • ⟨x, hx⟩) this, rwa [← mul_le_mul_left (neg_pos.2 hr), ← neg_mul_eq_neg_mul, ← neg_mul_eq_neg_mul, neg_le_neg_iff, f.map_smul, smul_eq_mul, ← mul_assoc, mul_inv_cancel hr.ne, one_mul] at this }, { subst r, simp only [zero_smul, add_zero] at hzs ⊢, apply nonneg, exact hzs }, { have : r⁻¹ • x + y ∈ s, by rwa [← s.smul_mem_iff hr, smul_add, smul_smul, mul_inv_cancel hr.ne', one_smul], replace := c_le (r⁻¹ • ⟨x, hx⟩) this, rwa [← mul_le_mul_left hr, f.map_smul, smul_eq_mul, ← mul_assoc, mul_inv_cancel hr.ne', one_mul] at this } } end theorem exists_top (p : linear_pmap ℝ E ℝ) (hp_nonneg : ∀ x : p.domain, (x : E) ∈ s → 0 ≤ p x) (hp_dense : ∀ y, ∃ x : p.domain, (x : E) + y ∈ s) : ∃ q ≥ p, q.domain = ⊤ ∧ ∀ x : q.domain, (x : E) ∈ s → 0 ≤ q x := begin replace hp_nonneg : p ∈ { p | _ }, by { rw mem_set_of_eq, exact hp_nonneg }, obtain ⟨q, hqs, hpq, hq⟩ := zorn.zorn_nonempty_partial_order₀ _ _ _ hp_nonneg, { refine ⟨q, hpq, _, hqs⟩, contrapose! hq, rcases step s q hqs _ hq with ⟨r, hqr, hr⟩, { exact ⟨r, hr, hqr.le, hqr.ne'⟩ }, { exact λ y, let ⟨x, hx⟩ := hp_dense y in ⟨of_le hpq.left x, hx⟩ } }, { intros c hcs c_chain y hy, clear hp_nonneg hp_dense p, have cne : c.nonempty := ⟨y, hy⟩, refine ⟨linear_pmap.Sup c c_chain.directed_on, _, λ _, linear_pmap.le_Sup c_chain.directed_on⟩, rintros ⟨x, hx⟩ hxs, have hdir : directed_on (≤) (linear_pmap.domain '' c), from directed_on_image.2 (c_chain.directed_on.mono linear_pmap.domain_mono.monotone), rcases (mem_Sup_of_directed (cne.image _) hdir).1 hx with ⟨_, ⟨f, hfc, rfl⟩, hfx⟩, have : f ≤ linear_pmap.Sup c c_chain.directed_on, from linear_pmap.le_Sup _ hfc, convert ← hcs hfc ⟨x, hfx⟩ hxs, apply this.2, refl } end end riesz_extension /-- M. **Riesz extension theorem**: given a convex cone `s` in a vector space `E`, a submodule `p`, and a linear `f : p → ℝ`, assume that `f` is nonnegative on `p ∩ s` and `p + s = E`. Then there exists a globally defined linear function `g : E → ℝ` that agrees with `f` on `p`, and is nonnegative on `s`. -/ theorem riesz_extension (s : convex_cone ℝ E) (f : linear_pmap ℝ E ℝ) (nonneg : ∀ x : f.domain, (x : E) ∈ s → 0 ≤ f x) (dense : ∀ y, ∃ x : f.domain, (x : E) + y ∈ s) : ∃ g : E →ₗ[ℝ] ℝ, (∀ x : f.domain, g x = f x) ∧ (∀ x ∈ s, 0 ≤ g x) := begin rcases riesz_extension.exists_top s f nonneg dense with ⟨⟨g_dom, g⟩, ⟨hpg, hfg⟩, htop, hgs⟩, clear hpg, refine ⟨g ∘ₗ ↑(linear_equiv.of_top _ htop).symm, _, _⟩; simp only [comp_apply, linear_equiv.coe_coe, linear_equiv.of_top_symm_apply], { exact λ x, (hfg (submodule.coe_mk _ _).symm).symm }, { exact λ x hx, hgs ⟨x, _⟩ hx } end /-- **Hahn-Banach theorem**: if `N : E → ℝ` is a sublinear map, `f` is a linear map defined on a subspace of `E`, and `f x ≤ N x` for all `x` in the domain of `f`, then `f` can be extended to the whole space to a linear map `g` such that `g x ≤ N x` for all `x`. -/ theorem exists_extension_of_le_sublinear (f : linear_pmap ℝ E ℝ) (N : E → ℝ) (N_hom : ∀ (c : ℝ), 0 < c → ∀ x, N (c • x) = c * N x) (N_add : ∀ x y, N (x + y) ≤ N x + N y) (hf : ∀ x : f.domain, f x ≤ N x) : ∃ g : E →ₗ[ℝ] ℝ, (∀ x : f.domain, g x = f x) ∧ (∀ x, g x ≤ N x) := begin let s : convex_cone ℝ (E × ℝ) := { carrier := {p : E × ℝ | N p.1 ≤ p.2 }, smul_mem' := λ c hc p hp, calc N (c • p.1) = c * N p.1 : N_hom c hc p.1 ... ≤ c * p.2 : mul_le_mul_of_nonneg_left hp hc.le, add_mem' := λ x hx y hy, (N_add _ _).trans (add_le_add hx hy) }, obtain ⟨g, g_eq, g_nonneg⟩ := riesz_extension s ((-f).coprod (linear_map.id.to_pmap ⊤)) _ _; try { simp only [linear_pmap.coprod_apply, to_pmap_apply, id_apply, linear_pmap.neg_apply, ← sub_eq_neg_add, sub_nonneg, subtype.coe_mk] at * }, replace g_eq : ∀ (x : f.domain) (y : ℝ), g (x, y) = y - f x, { intros x y, simpa only [subtype.coe_mk, subtype.coe_eta] using g_eq ⟨(x, y), ⟨x.2, trivial⟩⟩ }, { refine ⟨-g.comp (inl ℝ E ℝ), _, _⟩; simp only [neg_apply, inl_apply, comp_apply], { intro x, simp [g_eq x 0] }, { intro x, have A : (x, N x) = (x, 0) + (0, N x), by simp, have B := g_nonneg ⟨x, N x⟩ (le_refl (N x)), rw [A, map_add, ← neg_le_iff_add_nonneg'] at B, have C := g_eq 0 (N x), simp only [submodule.coe_zero, f.map_zero, sub_zero] at C, rwa ← C } }, { exact λ x hx, le_trans (hf _) hx }, { rintros ⟨x, y⟩, refine ⟨⟨(0, N x - y), ⟨f.domain.zero_mem, trivial⟩⟩, _⟩, simp only [convex_cone.mem_mk, mem_set_of_eq, subtype.coe_mk, prod.fst_add, prod.snd_add, zero_add, sub_add_cancel] } end /-! ### The dual cone -/ section dual variables {H : Type*} [inner_product_space ℝ H] (s t : set H) open_locale real_inner_product_space /-- The dual cone is the cone consisting of all points `y` such that for all points `x` in a given set `0 ≤ ⟪ x, y ⟫`. -/ noncomputable def set.inner_dual_cone (s : set H) : convex_cone ℝ H := { carrier := { y | ∀ x ∈ s, 0 ≤ ⟪ x, y ⟫ }, smul_mem' := λ c hc y hy x hx, begin rw real_inner_smul_right, exact mul_nonneg hc.le (hy x hx) end, add_mem' := λ u hu v hv x hx, begin rw inner_add_right, exact add_nonneg (hu x hx) (hv x hx) end } lemma mem_inner_dual_cone (y : H) (s : set H) : y ∈ s.inner_dual_cone ↔ ∀ x ∈ s, 0 ≤ ⟪ x, y ⟫ := by refl @[simp] lemma inner_dual_cone_empty : (∅ : set H).inner_dual_cone = ⊤ := convex_cone.ext' (eq_univ_of_forall (λ x y hy, false.elim (set.not_mem_empty _ hy))) lemma inner_dual_cone_le_inner_dual_cone (h : t ⊆ s) : s.inner_dual_cone ≤ t.inner_dual_cone := λ y hy x hx, hy x (h hx) lemma pointed_inner_dual_cone : s.inner_dual_cone.pointed := λ x hx, by rw inner_zero_right end dual
0bf15c9a5f8a2fb1fc059d9585d17ef51b68e647
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/number_theory/padics/padic_val.lean
e297e0f631f3d2d98d854dda9e9b24b97305b956
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
21,493
lean
/- Copyright (c) 2018 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis -/ import algebra.order.absolute_value import algebra.field_power import ring_theory.int.basic import tactic.basic import tactic.ring_exp import number_theory.divisors /-! # p-adic Valuation This file defines the p-adic valuation on ℕ, ℤ, and ℚ. The p-adic valuation on ℚ is the difference of the multiplicities of `p` in the numerator and denominator of `q`. This function obeys the standard properties of a valuation, with the appropriate assumptions on p. The p-adic valuations on ℕ and ℤ agree with that on ℚ. The valuation induces a norm on ℚ. This norm is defined in padic_norm.lean. ## Notations This file uses the local notation `/.` for `rat.mk`. ## Implementation notes Much, but not all, of this file assumes that `p` is prime. This assumption is inferred automatically by taking `[fact (prime p)]` as a type class argument. ## References * [F. Q. Gouvêa, *p-adic numbers*][gouvea1997] * [R. Y. Lewis, *A formal proof of Hensel's lemma over the p-adic integers*][lewis2019] * <https://en.wikipedia.org/wiki/P-adic_number> ## Tags p-adic, p adic, padic, norm, valuation -/ universe u open nat open_locale rat open multiplicity /-- For `p ≠ 1`, the p-adic valuation of a natural `n ≠ 0` is the largest natural number `k` such that p^k divides z. If `n = 0` or `p = 1`, then `padic_val_nat p q` defaults to 0. -/ def padic_val_nat (p : ℕ) (n : ℕ) : ℕ := if h : p ≠ 1 ∧ 0 < n then (multiplicity p n).get (multiplicity.finite_nat_iff.2 h) else 0 namespace padic_val_nat open multiplicity variables {p : ℕ} /-- `padic_val_nat p 0` is 0 for any `p`. -/ @[simp] protected lemma zero : padic_val_nat p 0 = 0 := by simp [padic_val_nat] /-- `padic_val_nat p 1` is 0 for any `p`. -/ @[simp] protected lemma one : padic_val_nat p 1 = 0 := by unfold padic_val_nat; split_ifs; simp * /-- For `p ≠ 0, p ≠ 1, `padic_val_rat p p` is 1. -/ @[simp] lemma self (hp : 1 < p) : padic_val_nat p p = 1 := begin have neq_one : (¬ p = 1) ↔ true, { exact iff_of_true ((ne_of_lt hp).symm) trivial, }, have eq_zero_false : (p = 0) ↔ false, { exact iff_false_intro ((ne_of_lt (trans zero_lt_one hp)).symm) }, simp [padic_val_nat, neq_one, eq_zero_false], end lemma eq_zero_of_not_dvd {n : ℕ} (h : ¬ p ∣ n) : padic_val_nat p n = 0 := begin rw padic_val_nat, split_ifs, { simp [multiplicity_eq_zero_of_not_dvd h], }, refl, end end padic_val_nat /-- For `p ≠ 1`, the p-adic valuation of an integer `z ≠ 0` is the largest natural number `k` such that p^k divides z. If `x = 0` or `p = 1`, then `padic_val_int p q` defaults to 0. -/ def padic_val_int (p : ℕ) (z : ℤ) : ℕ := padic_val_nat p (z.nat_abs) namespace padic_val_int open multiplicity variables {p : ℕ} lemma of_ne_one_ne_zero {z : ℤ} (hp : p ≠ 1) (hz : z ≠ 0) : padic_val_int p z = (multiplicity (p : ℤ) z).get (by {apply multiplicity.finite_int_iff.2, simp [hp, hz]}) := begin rw [padic_val_int, padic_val_nat, dif_pos (and.intro hp (int.nat_abs_pos_of_ne_zero hz))], simp_rw multiplicity.int.nat_abs p z, refl, end /-- `padic_val_int p 0` is 0 for any `p`. -/ @[simp] protected lemma zero : padic_val_int p 0 = 0 := by simp [padic_val_int] /-- `padic_val_int p 1` is 0 for any `p`. -/ @[simp] protected lemma one : padic_val_int p 1 = 0 := by simp [padic_val_int] /-- The p-adic value of an natural is its p-adic_value as an integer -/ @[simp] lemma of_nat {n : ℕ} : padic_val_int p (n : ℤ) = padic_val_nat p n := by simp [padic_val_int] /-- For `p ≠ 0, p ≠ 1, `padic_val_int p p` is 1. -/ lemma self (hp : 1 < p) : padic_val_int p p = 1 := by simp [padic_val_nat.self hp] lemma eq_zero_of_not_dvd {z : ℤ} (h : ¬ (p : ℤ) ∣ z) : padic_val_int p z = 0 := begin rw [padic_val_int, padic_val_nat], split_ifs, { simp_rw multiplicity.int.nat_abs, simp [multiplicity_eq_zero_of_not_dvd h], }, refl, end end padic_val_int /-- `padic_val_rat` defines the valuation of a rational `q` to be the valuation of `q.num` minus the valuation of `q.denom`. If `q = 0` or `p = 1`, then `padic_val_rat p q` defaults to 0. -/ def padic_val_rat (p : ℕ) (q : ℚ) : ℤ := padic_val_int p q.num - padic_val_nat p q.denom namespace padic_val_rat open multiplicity variables {p : ℕ} /-- `padic_val_rat p q` is symmetric in `q`. -/ @[simp] protected lemma neg (q : ℚ) : padic_val_rat p (-q) = padic_val_rat p q := by simp [padic_val_rat, padic_val_int] /-- `padic_val_rat p 0` is 0 for any `p`. -/ @[simp] protected lemma zero (m : nat) : padic_val_rat m 0 = 0 := by simp [padic_val_rat, padic_val_int] /-- `padic_val_rat p 1` is 0 for any `p`. -/ @[simp] protected lemma one : padic_val_rat p 1 = 0 := by simp [padic_val_rat, padic_val_int] /-- The p-adic value of an integer `z ≠ 0` is its p-adic_value as a rational -/ @[simp] lemma of_int {z : ℤ} : padic_val_rat p (z : ℚ) = padic_val_int p z := by simp [padic_val_rat] /-- The p-adic value of an integer `z ≠ 0` is the multiplicity of `p` in `z`. -/ lemma of_int_multiplicity (z : ℤ) (hp : p ≠ 1) (hz : z ≠ 0) : padic_val_rat p (z : ℚ) = (multiplicity (p : ℤ) z).get (finite_int_iff.2 ⟨hp, hz⟩) := by rw [of_int, padic_val_int.of_ne_one_ne_zero hp hz] lemma multiplicity_sub_multiplicity {q : ℚ} (hp : p ≠ 1) (hq : q ≠ 0) : padic_val_rat p q = (multiplicity (p : ℤ) q.num).get (finite_int_iff.2 ⟨hp, rat.num_ne_zero_of_ne_zero hq⟩) - (multiplicity p q.denom).get (by { rw [←finite_iff_dom, finite_nat_iff, and_iff_right hp], exact q.pos }) := begin rw [padic_val_rat, padic_val_int.of_ne_one_ne_zero hp, padic_val_nat, dif_pos], { refl }, { exact ⟨hp, q.pos⟩ }, { exact rat.num_ne_zero_of_ne_zero hq }, end /-- The p-adic value of an integer `z ≠ 0` is its p-adic_value as a rational -/ @[simp] lemma of_nat {n : ℕ} : padic_val_rat p (n : ℚ) = padic_val_nat p n := by simp [padic_val_rat, padic_val_int] /-- For `p ≠ 0, p ≠ 1, `padic_val_rat p p` is 1. -/ lemma self (hp : 1 < p) : padic_val_rat p p = 1 := by simp [of_nat, hp] end padic_val_rat section padic_val_nat lemma zero_le_padic_val_rat_of_nat (p n : ℕ) : 0 ≤ padic_val_rat p n := by simp -- /-- `padic_val_rat` coincides with `padic_val_nat`. -/ @[norm_cast] lemma padic_val_rat_of_nat (p n : ℕ) : ↑(padic_val_nat p n) = padic_val_rat p n := by simp [padic_val_rat, padic_val_int] /-- A simplification of `padic_val_nat` when one input is prime, by analogy with `padic_val_rat_def`. -/ lemma padic_val_nat_def {p : ℕ} [hp : fact p.prime] {n : ℕ} (hn : 0 < n) : padic_val_nat p n = (multiplicity p n).get (multiplicity.finite_nat_iff.2 ⟨nat.prime.ne_one hp.1, hn⟩) := begin simp [padic_val_nat], split_ifs, { refl, }, { exfalso, apply h ⟨(hp.out).ne_one, hn⟩, } end lemma padic_val_nat_def' {n p : ℕ} (hp : p ≠ 1) (hn : 0 < n) : ↑(padic_val_nat p n) = multiplicity p n := by simp [padic_val_nat, hp, hn] @[simp] lemma padic_val_nat_self (p : ℕ) [fact p.prime] : padic_val_nat p p = 1 := by simp [padic_val_nat_def (fact.out p.prime).pos] lemma one_le_padic_val_nat_of_dvd {n p : nat} [prime : fact p.prime] (n_pos : 0 < n) (div : p ∣ n) : 1 ≤ padic_val_nat p n := begin rw @padic_val_nat_def _ prime _ n_pos, let one_le_mul : _ ≤ multiplicity p n := @multiplicity.le_multiplicity_of_pow_dvd _ _ _ p n 1 (begin norm_num, exact div end), simp only [nat.cast_one] at one_le_mul, rcases one_le_mul with ⟨_, q⟩, dsimp at q, solve_by_elim, end lemma dvd_iff_padic_val_nat_ne_zero {p n : ℕ} [fact p.prime] (hn0 : n ≠ 0) : (p ∣ n) ↔ padic_val_nat p n ≠ 0 := ⟨λ h, one_le_iff_ne_zero.mp (one_le_padic_val_nat_of_dvd hn0.bot_lt h), λ h, not_not.1 (mt padic_val_nat.eq_zero_of_not_dvd h)⟩ end padic_val_nat namespace padic_val_rat open multiplicity variables (p : ℕ) [p_prime : fact p.prime] include p_prime /-- The multiplicity of `p : ℕ` in `a : ℤ` is finite exactly when `a ≠ 0`. -/ lemma finite_int_prime_iff {p : ℕ} [p_prime : fact p.prime] {a : ℤ} : finite (p : ℤ) a ↔ a ≠ 0 := by simp [finite_int_iff, ne.symm (ne_of_lt (p_prime.1.one_lt))] /-- A rewrite lemma for `padic_val_rat p q` when `q` is expressed in terms of `rat.mk`. -/ protected lemma defn {q : ℚ} {n d : ℤ} (hqz : q ≠ 0) (qdf : q = n /. d) : padic_val_rat p q = (multiplicity (p : ℤ) n).get (finite_int_iff.2 ⟨ne.symm $ ne_of_lt p_prime.1.one_lt, λ hn, by simp * at *⟩) - (multiplicity (p : ℤ) d).get (finite_int_iff.2 ⟨ne.symm $ ne_of_lt p_prime.1.one_lt, λ hd, by simp * at *⟩) := have hd : d ≠ 0, from rat.mk_denom_ne_zero_of_ne_zero hqz qdf, let ⟨c, hc1, hc2⟩ := rat.num_denom_mk hd qdf in begin rw [padic_val_rat.multiplicity_sub_multiplicity]; simp [hc1, hc2, multiplicity.mul' (nat.prime_iff_prime_int.1 p_prime.1), (ne.symm (ne_of_lt p_prime.1.one_lt)), hqz, pos_iff_ne_zero], simp_rw [int.coe_nat_multiplicity p q.denom], end /-- A rewrite lemma for `padic_val_rat p (q * r)` with conditions `q ≠ 0`, `r ≠ 0`. -/ protected lemma mul {q r : ℚ} (hq : q ≠ 0) (hr : r ≠ 0) : padic_val_rat p (q * r) = padic_val_rat p q + padic_val_rat p r := have q*r = (q.num * r.num) /. (↑q.denom * ↑r.denom), by rw_mod_cast rat.mul_num_denom, have hq' : q.num /. q.denom ≠ 0, by rw rat.num_denom; exact hq, have hr' : r.num /. r.denom ≠ 0, by rw rat.num_denom; exact hr, have hp' : _root_.prime (p : ℤ), from nat.prime_iff_prime_int.1 p_prime.1, begin rw [padic_val_rat.defn p (mul_ne_zero hq hr) this], conv_rhs { rw [←(@rat.num_denom q), padic_val_rat.defn p hq', ←(@rat.num_denom r), padic_val_rat.defn p hr'] }, rw [multiplicity.mul' hp', multiplicity.mul' hp']; simp [add_comm, add_left_comm, sub_eq_add_neg] end /-- A rewrite lemma for `padic_val_rat p (q^k)` with condition `q ≠ 0`. -/ protected lemma pow {q : ℚ} (hq : q ≠ 0) {k : ℕ} : padic_val_rat p (q ^ k) = k * padic_val_rat p q := by induction k; simp [*, padic_val_rat.mul _ hq (pow_ne_zero _ hq), pow_succ, add_mul, add_comm] /-- A rewrite lemma for `padic_val_rat p (q⁻¹)` with condition `q ≠ 0`. -/ protected lemma inv (q : ℚ) : padic_val_rat p (q⁻¹) = -padic_val_rat p q := begin by_cases hq : q = 0, { simp [hq], }, { rw [eq_neg_iff_add_eq_zero, ← padic_val_rat.mul p (inv_ne_zero hq) hq, inv_mul_cancel hq, padic_val_rat.one] }, end /-- A rewrite lemma for `padic_val_rat p (q / r)` with conditions `q ≠ 0`, `r ≠ 0`. -/ protected lemma div {q r : ℚ} (hq : q ≠ 0) (hr : r ≠ 0) : padic_val_rat p (q / r) = padic_val_rat p q - padic_val_rat p r := by rw [div_eq_mul_inv, padic_val_rat.mul p hq (inv_ne_zero hr), padic_val_rat.inv p r, sub_eq_add_neg] /-- A condition for `padic_val_rat p (n₁ / d₁) ≤ padic_val_rat p (n₂ / d₂), in terms of divisibility by `p^n`. -/ lemma padic_val_rat_le_padic_val_rat_iff {n₁ n₂ d₁ d₂ : ℤ} (hn₁ : n₁ ≠ 0) (hn₂ : n₂ ≠ 0) (hd₁ : d₁ ≠ 0) (hd₂ : d₂ ≠ 0) : padic_val_rat p (n₁ /. d₁) ≤ padic_val_rat p (n₂ /. d₂) ↔ ∀ (n : ℕ), ↑p ^ n ∣ n₁ * d₂ → ↑p ^ n ∣ n₂ * d₁ := have hf1 : finite (p : ℤ) (n₁ * d₂), from finite_int_prime_iff.2 (mul_ne_zero hn₁ hd₂), have hf2 : finite (p : ℤ) (n₂ * d₁), from finite_int_prime_iff.2 (mul_ne_zero hn₂ hd₁), by conv { to_lhs, rw [padic_val_rat.defn p (rat.mk_ne_zero_of_ne_zero hn₁ hd₁) rfl, padic_val_rat.defn p (rat.mk_ne_zero_of_ne_zero hn₂ hd₂) rfl, sub_le_iff_le_add', ← add_sub_assoc, le_sub_iff_add_le], norm_cast, rw [← multiplicity.mul' (nat.prime_iff_prime_int.1 p_prime.1) hf1, add_comm, ← multiplicity.mul' (nat.prime_iff_prime_int.1 p_prime.1) hf2, part_enat.get_le_get, multiplicity_le_multiplicity_iff] } /-- Sufficient conditions to show that the p-adic valuation of `q` is less than or equal to the p-adic vlauation of `q + r`. -/ theorem le_padic_val_rat_add_of_le {q r : ℚ} (hqr : q + r ≠ 0) (h : padic_val_rat p q ≤ padic_val_rat p r) : padic_val_rat p q ≤ padic_val_rat p (q + r) := if hq : q = 0 then by simpa [hq] using h else if hr : r = 0 then by simp [hr] else have hqn : q.num ≠ 0, from rat.num_ne_zero_of_ne_zero hq, have hqd : (q.denom : ℤ) ≠ 0, by exact_mod_cast rat.denom_ne_zero _, have hrn : r.num ≠ 0, from rat.num_ne_zero_of_ne_zero hr, have hrd : (r.denom : ℤ) ≠ 0, by exact_mod_cast rat.denom_ne_zero _, have hqreq : q + r = (((q.num * r.denom + q.denom * r.num : ℤ)) /. (↑q.denom * ↑r.denom : ℤ)), from rat.add_num_denom _ _, have hqrd : q.num * ↑(r.denom) + ↑(q.denom) * r.num ≠ 0, from rat.mk_num_ne_zero_of_ne_zero hqr hqreq, begin conv_lhs { rw ←(@rat.num_denom q) }, rw [hqreq, padic_val_rat_le_padic_val_rat_iff p hqn hqrd hqd (mul_ne_zero hqd hrd), ← multiplicity_le_multiplicity_iff, mul_left_comm, multiplicity.mul (nat.prime_iff_prime_int.1 p_prime.1), add_mul], rw [←(@rat.num_denom q), ←(@rat.num_denom r), padic_val_rat_le_padic_val_rat_iff p hqn hrn hqd hrd, ← multiplicity_le_multiplicity_iff] at h, calc _ ≤ min (multiplicity ↑p (q.num * ↑(r.denom) * ↑(q.denom))) (multiplicity ↑p (↑(q.denom) * r.num * ↑(q.denom))) : (le_min (by rw [@multiplicity.mul _ _ _ _ (_ * _) _ (nat.prime_iff_prime_int.1 p_prime.1), add_comm]) (by rw [mul_assoc, @multiplicity.mul _ _ _ _ (q.denom : ℤ) (_ * _) (nat.prime_iff_prime_int.1 p_prime.1)]; exact add_le_add_left h _)) ... ≤ _ : min_le_multiplicity_add end /-- The minimum of the valuations of `q` and `r` is less than or equal to the valuation of `q + r`. -/ theorem min_le_padic_val_rat_add {q r : ℚ} (hqr : q + r ≠ 0) : min (padic_val_rat p q) (padic_val_rat p r) ≤ padic_val_rat p (q + r) := (le_total (padic_val_rat p q) (padic_val_rat p r)).elim (λ h, by rw [min_eq_left h]; exact le_padic_val_rat_add_of_le _ hqr h) (λ h, by rw [min_eq_right h, add_comm]; exact le_padic_val_rat_add_of_le _ (by rwa add_comm) h) open_locale big_operators /-- A finite sum of rationals with positive p-adic valuation has positive p-adic valuation (if the sum is non-zero). -/ theorem sum_pos_of_pos {n : ℕ} {F : ℕ → ℚ} (hF : ∀ i, i < n → 0 < padic_val_rat p (F i)) (hn0 : ∑ i in finset.range n, F i ≠ 0) : 0 < padic_val_rat p (∑ i in finset.range n, F i) := begin induction n with d hd, { exact false.elim (hn0 rfl) }, { rw finset.sum_range_succ at hn0 ⊢, by_cases h : ∑ (x : ℕ) in finset.range d, F x = 0, { rw [h, zero_add], exact hF d (lt_add_one _) }, { refine lt_of_lt_of_le _ (min_le_padic_val_rat_add p hn0), { refine lt_min (hd (λ i hi, _) h) (hF d (lt_add_one _)), exact hF _ (lt_trans hi (lt_add_one _)) }, } } end end padic_val_rat namespace padic_val_nat /-- A rewrite lemma for `padic_val_nat p (q * r)` with conditions `q ≠ 0`, `r ≠ 0`. -/ protected lemma mul (p : ℕ) [p_prime : fact p.prime] {q r : ℕ} (hq : q ≠ 0) (hr : r ≠ 0) : padic_val_nat p (q * r) = padic_val_nat p q + padic_val_nat p r := begin apply int.coe_nat_inj, simp only [padic_val_rat_of_nat, nat.cast_mul], rw padic_val_rat.mul, norm_cast, exact cast_ne_zero.mpr hq, exact cast_ne_zero.mpr hr, end protected lemma div_of_dvd (p : ℕ) [hp : fact p.prime] {a b : ℕ} (h : b ∣ a) : padic_val_nat p (a / b) = padic_val_nat p a - padic_val_nat p b := begin rcases eq_or_ne a 0 with rfl | ha, { simp }, obtain ⟨k, rfl⟩ := h, obtain ⟨hb, hk⟩ := mul_ne_zero_iff.mp ha, rw [mul_comm, k.mul_div_cancel hb.bot_lt, padic_val_nat.mul p hk hb, nat.add_sub_cancel] end /-- Dividing out by a prime factor reduces the padic_val_nat by 1. -/ protected lemma div {p : ℕ} [p_prime : fact p.prime] {b : ℕ} (dvd : p ∣ b) : (padic_val_nat p (b / p)) = (padic_val_nat p b) - 1 := begin convert padic_val_nat.div_of_dvd p dvd, rw padic_val_nat_self p end /-- A version of `padic_val_rat.pow` for `padic_val_nat` -/ protected lemma pow (p q n : ℕ) [fact p.prime] (hq : q ≠ 0) : padic_val_nat p (q ^ n) = n * padic_val_nat p q := begin apply @nat.cast_injective ℤ, push_cast, exact padic_val_rat.pow _ (cast_ne_zero.mpr hq), end @[simp] protected lemma prime_pow (p n : ℕ) [fact p.prime] : padic_val_nat p (p ^ n) = n := by rw [padic_val_nat.pow p _ _ (fact.out p.prime).ne_zero, padic_val_nat_self p, mul_one] protected lemma div_pow {p : ℕ} [p_prime : fact p.prime] {b k : ℕ} (dvd : p ^ k ∣ b) : (padic_val_nat p (b / p ^ k)) = (padic_val_nat p b) - k := begin convert padic_val_nat.div_of_dvd p dvd, rw padic_val_nat.prime_pow end end padic_val_nat section padic_val_nat lemma dvd_of_one_le_padic_val_nat {n p : nat} (hp : 1 ≤ padic_val_nat p n) : p ∣ n := begin by_contra h, rw padic_val_nat.eq_zero_of_not_dvd h at hp, exact lt_irrefl 0 (lt_of_lt_of_le zero_lt_one hp), end lemma pow_padic_val_nat_dvd {p n : ℕ} : p ^ (padic_val_nat p n) ∣ n := begin rcases n.eq_zero_or_pos with rfl | hn, { simp }, rcases eq_or_ne p 1 with rfl | hp, { simp }, rw [multiplicity.pow_dvd_iff_le_multiplicity, padic_val_nat_def']; assumption, end lemma pow_succ_padic_val_nat_not_dvd {p n : ℕ} [hp : fact (nat.prime p)] (hn : 0 < n) : ¬ p ^ (padic_val_nat p n + 1) ∣ n := begin rw multiplicity.pow_dvd_iff_le_multiplicity, rw padic_val_nat_def hn, { rw [nat.cast_add, part_enat.coe_get], simp only [nat.cast_one, not_le], exact part_enat.lt_add_one (ne_top_iff_finite.mpr (finite_nat_iff.mpr ⟨(fact.elim hp).ne_one, hn⟩)), }, { apply_instance } end lemma padic_val_nat_dvd_iff (p : ℕ) [hp :fact p.prime] (n : ℕ) (a : ℕ) : p^n ∣ a ↔ a = 0 ∨ n ≤ padic_val_nat p a := begin split, { rw [pow_dvd_iff_le_multiplicity, padic_val_nat], split_ifs, { rw part_enat.coe_le_iff, exact λ hn, or.inr (hn _) }, { simp only [true_and, not_lt, ne.def, not_false_iff, nat.le_zero_iff, hp.out.ne_one] at h, exact λ hn, or.inl h } }, { rintro (rfl|h), { exact dvd_zero (p ^ n) }, { exact dvd_trans (pow_dvd_pow p h) pow_padic_val_nat_dvd } }, end lemma padic_val_nat_primes {p q : ℕ} [p_prime : fact p.prime] [q_prime : fact q.prime] (neq : p ≠ q) : padic_val_nat p q = 0 := @padic_val_nat.eq_zero_of_not_dvd p q $ (not_congr (iff.symm (prime_dvd_prime_iff_eq p_prime.1 q_prime.1))).mp neq protected lemma padic_val_nat.div' {p : ℕ} [p_prime : fact p.prime] : ∀ {m : ℕ} (cpm : coprime p m) {b : ℕ} (dvd : m ∣ b), padic_val_nat p (b / m) = padic_val_nat p b | 0 := λ cpm b dvd, by { rw zero_dvd_iff at dvd, rw [dvd, nat.zero_div], } | (n + 1) := λ cpm b dvd, begin rcases dvd with ⟨c, rfl⟩, rw [mul_div_right c (nat.succ_pos _)],by_cases hc : c = 0, { rw [hc, mul_zero] }, { rw padic_val_nat.mul, { suffices : ¬ p ∣ (n+1), { rw [padic_val_nat.eq_zero_of_not_dvd this, zero_add] }, contrapose! cpm, exact p_prime.1.dvd_iff_not_coprime.mp cpm }, { exact nat.succ_ne_zero _ }, { exact hc } }, end open_locale big_operators lemma range_pow_padic_val_nat_subset_divisors {n : ℕ} (p : ℕ) (hn : n ≠ 0) : (finset.range (padic_val_nat p n + 1)).image (pow p) ⊆ n.divisors := begin intros t ht, simp only [exists_prop, finset.mem_image, finset.mem_range] at ht, obtain ⟨k, hk, rfl⟩ := ht, rw nat.mem_divisors, exact ⟨(pow_dvd_pow p $ by linarith).trans pow_padic_val_nat_dvd, hn⟩ end lemma range_pow_padic_val_nat_subset_divisors' {n : ℕ} (p : ℕ) [h : fact p.prime] : (finset.range (padic_val_nat p n)).image (λ t, p ^ (t + 1)) ⊆ (n.divisors \ {1}) := begin rcases eq_or_ne n 0 with rfl | hn, { simp }, intros t ht, simp only [exists_prop, finset.mem_image, finset.mem_range] at ht, obtain ⟨k, hk, rfl⟩ := ht, rw [finset.mem_sdiff, nat.mem_divisors], refine ⟨⟨(pow_dvd_pow p $ by linarith).trans pow_padic_val_nat_dvd, hn⟩, _⟩, rw [finset.mem_singleton], nth_rewrite 1 ←one_pow (k + 1), exact (nat.pow_lt_pow_of_lt_left h.1.one_lt $ nat.succ_pos k).ne', end end padic_val_nat section padic_val_int variables (p : ℕ) [p_prime : fact p.prime] lemma padic_val_int_dvd_iff (p : ℕ) [fact p.prime] (n : ℕ) (a : ℤ) : ↑p^n ∣ a ↔ a = 0 ∨ n ≤ padic_val_int p a := by rw [padic_val_int, ←int.nat_abs_eq_zero, ←padic_val_nat_dvd_iff, ←int.coe_nat_dvd_left, int.coe_nat_pow] lemma padic_val_int_dvd (p : ℕ) [fact p.prime] (a : ℤ) : ↑p^(padic_val_int p a) ∣ a := begin rw padic_val_int_dvd_iff, exact or.inr le_rfl, end lemma padic_val_int_self (p : ℕ) [pp : fact p.prime] : padic_val_int p p = 1 := padic_val_int.self pp.out.one_lt lemma padic_val_int.mul (p : ℕ) [fact p.prime] {a b : ℤ} (ha : a ≠ 0) (hb : b ≠ 0) : padic_val_int p (a*b) = padic_val_int p a + padic_val_int p b := begin simp_rw padic_val_int, rw [int.nat_abs_mul, padic_val_nat.mul]; rwa int.nat_abs_ne_zero, end lemma padic_val_int_mul_eq_succ (p : ℕ) [pp : fact p.prime] (a : ℤ) (ha : a ≠ 0) : padic_val_int p (a * p) = (padic_val_int p a) + 1 := begin rw padic_val_int.mul p ha (int.coe_nat_ne_zero.mpr (pp.out).ne_zero), simp only [eq_self_iff_true, padic_val_int.of_nat, padic_val_nat_self], end end padic_val_int
72b5577eefb229568dff4d47126ee4eefe01cf52
217bb195841a8be2d1b4edd2084d6b69ccd62f50
/library/init/lean/position.lean
da39fed8335cd3a9ac43cb6bf051e97508338bfa
[ "Apache-2.0" ]
permissive
frank-lesser/lean4
717f56c9bacd5bf3a67542d2f5cea721d4743a30
79e2abe33f73162f773ea731265e456dbfe822f9
refs/heads/master
1,589,741,267,933
1,556,424,200,000
1,556,424,281,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,834
lean
/- Copyright (c) 2018 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ prelude import init.data.nat init.data.rbmap init.lean.format init.lean.parser.parsec namespace Lean structure Position := (line : Nat) (column : Nat) namespace Position instance : DecidableEq Position := {decEq := λ ⟨l₁, c₁⟩ ⟨l₂, c₂⟩, if h₁ : l₁ = l₂ then if h₂ : c₁ = c₂ then isTrue (Eq.recOn h₁ (Eq.recOn h₂ rfl)) else isFalse (λ contra, Position.noConfusion contra (λ e₁ e₂, absurd e₂ h₂)) else isFalse (λ contra, Position.noConfusion contra (λ e₁ e₂, absurd e₁ h₁))} protected def lt : Position → Position → Bool | ⟨l₁, c₁⟩ ⟨l₂, c₂⟩ := (l₁, c₁) < (l₂, c₂) instance : HasToFormat Position := ⟨λ ⟨l, c⟩, "⟨" ++ toFmt l ++ ", " ++ toFmt c ++ "⟩"⟩ instance : Inhabited Position := ⟨⟨1, 0⟩⟩ end Position /-- A precomputed cache for quickly mapping Char offsets to positionitions. -/ structure FileMap := -- A mapping from Char offset of line start to line index (lines : RBMap Nat Nat (λ a b, a < b)) namespace FileMap private def fromStringAux : Nat → String.OldIterator → Nat → List (Nat × Nat) | 0 it line := [] | (k+1) it line := if it.hasNext = false then [] else match it.curr with | '\n' := (it.next.offset, line+1) :: fromStringAux k it.next (line+1) | other := fromStringAux k it.next line def fromString (s : String) : FileMap := {lines := RBMap.ofList $ fromStringAux s.length s.mkOldIterator 1} def toPosition (m : FileMap) (off : Nat) : Position := match m.lines.lowerBound off with | some ⟨start, l⟩ := ⟨l, off - start⟩ | none := ⟨1, off⟩ end FileMap end Lean
153dd8d36b16e7126620e0c637d320cb6c67afe7
e030b0259b777fedcdf73dd966f3f1556d392178
/library/init/meta/task.lean
d2cad9694fc23b80c2dc3f8d21146c4101bfe5ad
[ "Apache-2.0" ]
permissive
fgdorais/lean
17b46a095b70b21fa0790ce74876658dc5faca06
c3b7c54d7cca7aaa25328f0a5660b6b75fe26055
refs/heads/master
1,611,523,590,686
1,484,412,902,000
1,484,412,902,000
38,489,734
0
0
null
1,435,923,380,000
1,435,923,379,000
null
UTF-8
Lean
false
false
197
lean
prelude meta constant {u} task : Type u → Type (max u 1) namespace task meta constant {u} get {α : Type u} (t : task α) : α meta constant {u} pure {α : Type u} (t : α) : task α end task
19e703a3577f600dbe5c78a9cdf262880ff4e9c6
8e50deafd5bb76463f835e09114910e9e33e0e63
/Tr.lean
1b6304aae842c9e5dce151ccd6c6cd531d2780e5
[ "MIT" ]
permissive
teodorov/lean-transition-systems
b174a09a168d27cde684cd5be5b93eaa9e144070
252ceb254d95ab167273049717609e2c2089688c
refs/heads/main
1,691,617,772,896
1,629,892,131,000
1,629,892,131,000
399,796,873
1
0
null
null
null
null
UTF-8
Lean
false
false
5,879
lean
notation:65 "ℕ" => Nat open Nat def fact : ℕ → ℕ | zero => 1 | succ n' => fact n' * succ n' inductive fact_state := | AnswerIs (answer : ℕ) | WithAccumulator (input accumulator : ℕ) inductive fact_init (original_input : ℕ) : fact_state → Prop := | FactInit : fact_init original_input (fact_state.WithAccumulator original_input 1) inductive fact_final : fact_state → Prop := | FactFinal : ∀ ans, fact_final (fact_state.AnswerIs ans) inductive fact_step : fact_state → fact_state → Prop := | FactDone : ∀ acc, fact_step (fact_state.WithAccumulator zero acc) (fact_state.AnswerIs acc) | FactStep : ∀ n acc, fact_step (fact_state.WithAccumulator (succ n) acc) (fact_state.WithAccumulator n (acc * succ n)) -- -------------- Transition Relation --------------------- inductive trc {A} (R : A → A → Prop) : A → A → Prop := | TrcRefl : ∀ x, trc R x x | TrcFront : ∀ x y z, R x y → trc R y z → trc R x z theorem trc_transitive : ∀ {A} (R : A → A → Prop) x y, trc R x y → ∀ z, trc R y z → trc R x z := by intros A R x y TR induction TR with | TrcRefl a => intros z TR assumption | TrcFront _ _ _ _ _ ih => intros apply trc.TrcFront assumption apply ih assumption postfix:max "^*" => trc -- -------------- Transition Relation --------------------- theorem succ12 : ∀ n, succ n = n + 1 := by intro simp theorem x : (succ 2 * succ 1) = 6 := by simp open trc open fact_step example : fact_step^* (fact_state.WithAccumulator 3 1) (fact_state.AnswerIs 6) := by apply TrcFront apply FactStep simp apply TrcFront apply FactStep simp [x] apply TrcFront apply FactStep simp apply TrcFront apply FactDone apply TrcRefl example : fact_step^* (fact_state.WithAccumulator 3 1) (fact_state.AnswerIs 6) := by repeat apply TrcFront apply FactStep simp [x] apply TrcFront apply FactDone apply TrcRefl done example : fact_step^* (fact_state.WithAccumulator 3 1) (fact_state.AnswerIs 6) := by repeat constructor done example : 2 + 3 = 5 := by simp -- -------------- Transition Relation --------------------- structure trsys (state) := (initial : state → Prop) (step : state → state → Prop) -- -------------- Transition Relation --------------------- def factorial_sys (original_input : ℕ) : trsys fact_state := { initial := fact_init original_input, step := fact_step } -- -------------- Transition Relation --------------------- inductive reachable {state} (sys : trsys state) (st : state) : Prop := | Reachable : ∀ st0, sys.initial st0 -> sys.step^* st0 st -> reachable sys st def invariantFor {state} (sys : trsys state) (invariant : state → Prop) := ∀ s, sys.initial s → ∀ s', sys.step^* s s' → invariant s' theorem use_invariant' : ∀ {state} (sys : trsys state) (invariant : state → Prop) s s', invariantFor sys invariant → sys.initial s → sys.step^* s s' → invariant s' := by -- intros state trsys inv s s' simp [invariantFor] intros _ _ _ _ _ H _ _ apply H assumption assumption theorem use_invariant : ∀ {state} (sys : trsys state) (invariant : state → Prop) s, invariantFor sys invariant → reachable sys s → invariant s := by intros _ _ _ _ _ H0 cases H0 apply use_invariant' repeat assumption theorem invariant_induction' : ∀ {state} (sys : trsys state) (invariant : state → Prop), (∀ s, invariant s → ∀ s', sys.step s s' → invariant s') → ∀ s s', sys.step^* s s' → invariant s → invariant s' := by intros _ _ _ H₀ _ _ H₁ induction H₁ with | TrcRefl a => intro assumption | TrcFront _ _ _ _ _ iH => intros apply iH apply H₀ repeat assumption done theorem invariant_induction : ∀ {state} (sys : trsys state) (invariant : state → Prop), (∀ s, sys.initial s → invariant s) → (∀ s, invariant s → ∀ s', sys.step s s' → invariant s') → invariantFor sys invariant := by simp [invariantFor]; intros _ _ _ H _ _ _ _ _ apply invariant_induction' assumption assumption apply H assumption -- -------------- Transition Relation --------------------- open fact_state def fact_invariant (original_input : ℕ) (st : fact_state) : Prop := match st with | AnswerIs ans => fact original_input = ans | WithAccumulator n acc => fact original_input = fact n * acc theorem fact_invariant_ok : ∀ original_input, invariantFor (factorial_sys original_input) (fact_invariant original_input) := by intros oi apply invariant_induction intros s simp [factorial_sys] intro H cases H simp [fact_invariant] intros s simp [factorial_sys] intros H _ H0 cases H0 simp [fact_invariant, fact] at * assumption simp [fact_invariant, fact] at * rw [H, Nat.mul_assoc] simp [Nat.mul_comm] done theorem fact_invariant_always : ∀ original_input s, reachable (factorial_sys original_input) s → fact_invariant original_input s := by intros apply use_invariant apply fact_invariant_ok assumption -- Therefore, any final state has the right answer! theorem fact_ok' : ∀ original_input s, fact_final s -> fact_invariant original_input s -> s = AnswerIs (fact original_input) := by intros oi s simp [fact_invariant] at * cases s with | AnswerIs x => simp intros a b rw [b] done | WithAccumulator => simp intros H cases H done theorem fact_ok : ∀ original_input s, reachable (factorial_sys original_input) s → fact_final s → s = AnswerIs (fact original_input) := by intros apply fact_ok' assumption apply fact_invariant_always assumption done def main : IO Unit := IO.println (fact 4)
7c8cdf3dc9535b9507db04cb158eb455308611ac
d1a52c3f208fa42c41df8278c3d280f075eb020c
/tests/lean/interactive/hoverException.lean
6e56570b65f6255e87cfdd5f7861bce31a97d854
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
cipher1024/lean4
6e1f98bb58e7a92b28f5364eb38a14c8d0aae393
69114d3b50806264ef35b57394391c3e738a9822
refs/heads/master
1,642,227,983,603
1,642,011,696,000
1,642,011,696,000
228,607,691
0
0
Apache-2.0
1,576,584,269,000
1,576,584,268,000
null
UTF-8
Lean
false
false
76
lean
inductive Foo | mk : (a b : Bar) → Foo --^ textDocument/hover
6aaf7e2871fdbea80600e3c8a9d90f6a0d8c5c51
df7bb3acd9623e489e95e85d0bc55590ab0bc393
/lean/love12_basic_mathematical_structures_exercise_solution.lean
a2c65b9dae2505e689ed280e149fe31cea5dc52f
[]
no_license
MaschavanderMarel/logical_verification_2020
a41c210b9237c56cb35f6cd399e3ac2fe42e775d
7d562ef174cc6578ca6013f74db336480470b708
refs/heads/master
1,692,144,223,196
1,634,661,675,000
1,634,661,675,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,965
lean
import .love12_basic_mathematical_structures_demo /- # LoVe Exercise 12: Basic Mathematical Structures -/ set_option pp.beta true set_option pp.generalized_field_notation false namespace LoVe /- ## Question 1: Type Classes Recall the inductive type `btree` we introduced earlier: -/ #check btree /- The following function takes two trees and attaches copies of the second tree to each leaf of the first tree. -/ def btree.graft {α : Type} : btree α → btree α → btree α | btree.empty u := u | (btree.node a l r) u := btree.node a (btree.graft l u) (btree.graft r u) #reduce btree.graft (btree.node 1 btree.empty btree.empty) (btree.node 2 btree.empty btree.empty) /- 1.1. Prove the following two lemmas by structural induction on `t`. -/ lemma btree.graft_assoc {α : Type} (t u v : btree α) : btree.graft (btree.graft t u) v = btree.graft t (btree.graft u v) := begin induction' t, case empty { refl }, case node : a l r ih_l ih_r { simp [btree.graft, ih_l, ih_r] } end lemma btree.graft_empty {α : Type} (t : btree α) : btree.graft t btree.empty = t := begin induction' t, case empty { refl }, case node : a l r ih_l ih_r { simp [btree.graft, ih_l, ih_r] } end /- 1.2. Declare btree an instance of `add_monoid` using `graft` as addition operator. -/ #print add_monoid @[instance] def btree.add_monid {α : Type} : add_monoid (btree α) := { add := btree.graft, add_assoc := btree.graft_assoc, zero := btree.empty, add_zero := btree.graft_empty, zero_add := by intro x; refl } /- 1.3. Explain why `btree` with `graft` as addition cannot be declared an instance of `add_group`. -/ #print add_group /- No matter how we define `neg` (`-`), we will not be able to prove `add_left_neg`, which amounts to the following property: `∀t, btree.graft (btree.neg t) t = btree.empty` However, if `t` is a nonempty tree, `btree.graft _ t` cannot possibly be equal to `btree.empty`, no matter what the first argument is. -/ /- 1.4 (**optional**). Prove the following lemma illustrating why `btree` with `graft` as addition does not constitute an `add_group`. -/ lemma btree.add_left_neg_counterexample : ∃x : btree ℕ, ∀ y : btree ℕ, btree.graft y x ≠ btree.empty := begin apply exists.intro (btree.node 0 btree.empty btree.empty), intros y hy, cases' y, { cases' hy }, { cases' hy } end /- ## Question 2: Multisets and Finsets Recall the following definitions from the lecture: -/ #check multiset.elems #check finset.elems #check list.elems /- 2.1. Prove that the multiset of nodes does not change when mirroring a tree. Hints: * Perform structural induction on `t`. * The `cc` tactic also works with set operations. -/ lemma multiset.elems_mirror (t : btree ℕ) : multiset.elems (mirror t) = multiset.elems t := begin induction' t, case empty { refl }, case node : a l r ih_l ih_r { rw multiset.elems, rw mirror, rw ←ih_l, rw ←ih_r, rw multiset.elems, cc } end /- 2.2. Prove that the finite set of nodes does not change when mirroring a tree. -/ lemma finset.elems_mirror (t : btree ℕ) : finset.elems (mirror t) = finset.elems t := begin induction' t, case empty { refl }, case node : a l r ih_l ih_r { rw finset.elems, rw mirror, rw ←ih_l, rw ←ih_r, rw finset.elems, cc } end /- 2.3. Show that this does not hold for the list of nodes by providing a tree `t` for which `nodes_list t ≠ nodes_list (mirror t)`. If you define a suitable counterexample, the proof below will succeed. -/ def rotten_tree : btree ℕ := btree.node 0 (btree.node 1 btree.empty btree.empty) (btree.node 2 btree.empty btree.empty) #eval list.elems rotten_tree #eval list.elems (mirror rotten_tree) lemma list.elems_mirror_counterexample : ∃t : btree ℕ, list.elems t ≠ list.elems (mirror t) := begin apply exists.intro rotten_tree, exact dec_trivial end end LoVe
7db613e30285a6cecedee9981e4f999118b3651c
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/order/filter/basic.lean
ac40539e937e72de977773a4eb42b8d31a8f8aac
[ "Apache-2.0" ]
permissive
agjftucker/mathlib
d634cd0d5256b6325e3c55bb7fb2403548371707
87fe50de17b00af533f72a102d0adefe4a2285e8
refs/heads/master
1,625,378,131,941
1,599,166,526,000
1,599,166,526,000
160,748,509
0
0
Apache-2.0
1,544,141,789,000
1,544,141,789,000
null
UTF-8
Lean
false
false
100,015
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, Jeremy Avigad -/ import order.zorn import order.copy import data.set.finite import tactic.monotonicity /-! # Theory of filters on sets ## Main definitions * `filter` : filters on a set; * `at_top`, `at_bot`, `cofinite`, `principal` : specific filters; * `map`, `comap`, `prod` : operations on filters; * `tendsto` : limit with respect to filters; * `eventually` : `f.eventually p` means `{x | p x} ∈ f`; * `frequently` : `f.frequently p` means `{x | ¬p x} ∉ f`; * `filter_upwards [h₁, ..., hₙ]` : takes a list of proofs `hᵢ : sᵢ ∈ f`, and replaces a goal `s ∈ f` with `∀ x, x ∈ s₁ → ... → x ∈ sₙ → x ∈ s`; * `ne_bot f` : an utility class stating that `f` is a non-trivial filter. Filters on a type `X` are sets of sets of `X` satisfying three conditions. They are mostly used to abstract two related kinds of ideas: * *limits*, including finite or infinite limits of sequences, finite or infinite limits of functions at a point or at infinity, etc... * *things happening eventually*, including things happening for large enough `n : ℕ`, or near enough a point `x`, or for close enough pairs of points, or things happening almost everywhere in the sense of measure theory. Dually, filters can also express the idea of *things happening often*: for arbitrarily large `n`, or at a point in any neighborhood of given a point etc... In this file, we define the type `filter X` of filters on `X`, and endow it with a complete lattice structure. This structure is lifted from the lattice structure on `set (set X)` using the Galois insertion which maps a filter to its elements in one direction, and an arbitrary set of sets to the smallest filter containing it in the other direction. We also prove `filter` is a monadic functor, with a push-forward operation `filter.map` and a pull-back operation `filter.comap` that form a Galois connections for the order on filters. Finally we describe a product operation `filter X → filter Y → filter (X × Y)`. The examples of filters appearing in the description of the two motivating ideas are: * `(at_top : filter ℕ)` : made of sets of `ℕ` containing `{n | n ≥ N}` for some `N` * `𝓝 x` : made of neighborhoods of `x` in a topological space (defined in topology.basic) * `𝓤 X` : made of entourages of a uniform space (those space are generalizations of metric spaces defined in topology.uniform_space.basic) * `μ.ae` : made of sets whose complement has zero measure with respect to `μ` (defined in `measure_theory.measure_space`) The general notion of limit of a map with respect to filters on the source and target types is `filter.tendsto`. It is defined in terms of the order and the push-forward operation. The predicate "happening eventually" is `filter.eventually`, and "happening often" is `filter.frequently`, whose definitions are immediate after `filter` is defined (but they come rather late in this file in order to immediately relate them to the lattice structure). For instance, anticipating on topology.basic, the statement: "if a sequence `u` converges to some `x` and `u n` belongs to a set `M` for `n` large enough then `x` is in the closure of `M`" is formalized as: `tendsto u at_top (𝓝 x) → (∀ᶠ n in at_top, u n ∈ M) → x ∈ closure M`, which is a special case of `mem_closure_of_tendsto` from topology.basic. ## Notations * `∀ᶠ x in f, p x` : `f.eventually p`; * `∃ᶠ x in f, p x` : `f.frequently p`; * `f =ᶠ[l] g` : `∀ᶠ x in l, f x = g x`; * `f ≤ᶠ[l] g` : `∀ᶠ x in l, f x ≤ g x`; * `f ×ᶠ g` : `filter.prod f g`, localized in `filter`; * `𝓟 s` : `principal s`, localized in `filter`. ## References * [N. Bourbaki, *General Topology*][bourbaki1966] Important note: Bourbaki requires that a filter on `X` cannot contain all sets of `X`, which we do *not* require. This gives `filter X` better formal properties, in particular a bottom element `⊥` for its lattice structure, at the cost of including the assumption `[ne_bot f]` in a number of lemmas and definitions. -/ open set universes u v w x y open_locale classical /-- A filter `F` on a type `α` is a collection of sets of `α` which contains the whole `α`, is upwards-closed, and is stable under intersection. We do not forbid this collection to be all sets of `α`. -/ structure filter (α : Type*) := (sets : set (set α)) (univ_sets : set.univ ∈ sets) (sets_of_superset {x y} : x ∈ sets → x ⊆ y → y ∈ sets) (inter_sets {x y} : x ∈ sets → y ∈ sets → x ∩ y ∈ sets) /-- If `F` is a filter on `α`, and `U` a subset of `α` then we can write `U ∈ F` as on paper. -/ @[reducible] instance {α : Type*}: has_mem (set α) (filter α) := ⟨λ U F, U ∈ F.sets⟩ namespace filter variables {α : Type u} {f g : filter α} {s t : set α} instance inhabited_mem : inhabited {s : set α // s ∈ f} := ⟨⟨univ, f.univ_sets⟩⟩ lemma filter_eq : ∀{f g : filter α}, f.sets = g.sets → f = g | ⟨a, _, _, _⟩ ⟨._, _, _, _⟩ rfl := rfl lemma filter_eq_iff : f = g ↔ f.sets = g.sets := ⟨congr_arg _, filter_eq⟩ protected lemma ext_iff : f = g ↔ ∀ s, s ∈ f ↔ s ∈ g := by rw [filter_eq_iff, ext_iff] @[ext] protected lemma ext : (∀ s, s ∈ f ↔ s ∈ g) → f = g := filter.ext_iff.2 lemma univ_mem_sets : univ ∈ f := f.univ_sets lemma mem_sets_of_superset : ∀{x y : set α}, x ∈ f → x ⊆ y → y ∈ f := f.sets_of_superset lemma inter_mem_sets : ∀{s t}, s ∈ f → t ∈ f → s ∩ t ∈ f := f.inter_sets lemma univ_mem_sets' (h : ∀ a, a ∈ s) : s ∈ f := mem_sets_of_superset univ_mem_sets (assume x _, h x) lemma mp_sets (hs : s ∈ f) (h : {x | x ∈ s → x ∈ t} ∈ f) : t ∈ f := mem_sets_of_superset (inter_mem_sets hs h) $ assume x ⟨h₁, h₂⟩, h₂ h₁ lemma congr_sets (h : {x | x ∈ s ↔ x ∈ t} ∈ f) : s ∈ f ↔ t ∈ f := ⟨λ hs, mp_sets hs (mem_sets_of_superset h (λ x, iff.mp)), λ hs, mp_sets hs (mem_sets_of_superset h (λ x, iff.mpr))⟩ lemma Inter_mem_sets {β : Type v} {s : β → set α} {is : set β} (hf : finite is) : (∀i∈is, s i ∈ f) → (⋂i∈is, s i) ∈ f := finite.induction_on hf (assume hs, by simp only [univ_mem_sets, mem_empty_eq, Inter_neg, Inter_univ, not_false_iff]) (assume i is _ hf hi hs, have h₁ : s i ∈ f, from hs i (by simp), have h₂ : (⋂x∈is, s x) ∈ f, from hi $ assume a ha, hs _ $ by simp only [ha, mem_insert_iff, or_true], by simp [inter_mem_sets h₁ h₂]) lemma sInter_mem_sets_of_finite {s : set (set α)} (hfin : finite s) (h_in : ∀ U ∈ s, U ∈ f) : ⋂₀ s ∈ f := by { rw sInter_eq_bInter, exact Inter_mem_sets hfin h_in } lemma Inter_mem_sets_of_fintype {β : Type v} {s : β → set α} [fintype β] (h : ∀i, s i ∈ f) : (⋂i, s i) ∈ f := by simpa using Inter_mem_sets finite_univ (λi hi, h i) lemma exists_sets_subset_iff : (∃t ∈ f, t ⊆ s) ↔ s ∈ f := ⟨assume ⟨t, ht, ts⟩, mem_sets_of_superset ht ts, assume hs, ⟨s, hs, subset.refl _⟩⟩ lemma monotone_mem_sets {f : filter α} : monotone (λs, s ∈ f) := assume s t hst h, mem_sets_of_superset h hst end filter namespace tactic.interactive open tactic interactive /-- `filter_upwards [h1, ⋯, hn]` replaces a goal of the form `s ∈ f` and terms `h1 : t1 ∈ f, ⋯, hn : tn ∈ f` with `∀x, x ∈ t1 → ⋯ → x ∈ tn → x ∈ s`. `filter_upwards [h1, ⋯, hn] e` is a short form for `{ filter_upwards [h1, ⋯, hn], exact e }`. -/ meta def filter_upwards (s : parse types.pexpr_list) (e' : parse $ optional types.texpr) : tactic unit := do s.reverse.mmap (λ e, eapplyc `filter.mp_sets >> eapply e), eapplyc `filter.univ_mem_sets', match e' with | some e := interactive.exact e | none := skip end end tactic.interactive namespace filter variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} section principal /-- The principal filter of `s` is the collection of all supersets of `s`. -/ def principal (s : set α) : filter α := { sets := {t | s ⊆ t}, univ_sets := subset_univ s, sets_of_superset := assume x y hx hy, subset.trans hx hy, inter_sets := assume x y, subset_inter } localized "notation `𝓟` := filter.principal" in filter instance : inhabited (filter α) := ⟨𝓟 ∅⟩ @[simp] lemma mem_principal_sets {s t : set α} : s ∈ 𝓟 t ↔ t ⊆ s := iff.rfl lemma mem_principal_self (s : set α) : s ∈ 𝓟 s := subset.refl _ end principal open_locale filter section join /-- The join of a filter of filters is defined by the relation `s ∈ join f ↔ {t | s ∈ t} ∈ f`. -/ def join (f : filter (filter α)) : filter α := { sets := {s | {t : filter α | s ∈ t} ∈ f}, univ_sets := by simp only [univ_mem_sets, mem_set_of_eq]; exact univ_mem_sets, sets_of_superset := assume x y hx xy, mem_sets_of_superset hx $ assume f h, mem_sets_of_superset h xy, inter_sets := assume x y hx hy, mem_sets_of_superset (inter_mem_sets hx hy) $ assume f ⟨h₁, h₂⟩, inter_mem_sets h₁ h₂ } @[simp] lemma mem_join_sets {s : set α} {f : filter (filter α)} : s ∈ join f ↔ {t | s ∈ t} ∈ f := iff.rfl end join section lattice instance : partial_order (filter α) := { le := λf g, ∀ ⦃U : set α⦄, U ∈ g → U ∈ f, le_antisymm := assume a b h₁ h₂, filter_eq $ subset.antisymm h₂ h₁, le_refl := assume a, subset.refl _, le_trans := assume a b c h₁ h₂, subset.trans h₂ h₁ } theorem le_def {f g : filter α} : f ≤ g ↔ ∀ x ∈ g, x ∈ f := iff.rfl /-- `generate_sets g s`: `s` is in the filter closure of `g`. -/ inductive generate_sets (g : set (set α)) : set α → Prop | basic {s : set α} : s ∈ g → generate_sets s | univ : generate_sets univ | superset {s t : set α} : generate_sets s → s ⊆ t → generate_sets t | inter {s t : set α} : generate_sets s → generate_sets t → generate_sets (s ∩ t) /-- `generate g` is the smallest filter containing the sets `g`. -/ def generate (g : set (set α)) : filter α := { sets := generate_sets g, univ_sets := generate_sets.univ, sets_of_superset := assume x y, generate_sets.superset, inter_sets := assume s t, generate_sets.inter } lemma sets_iff_generate {s : set (set α)} {f : filter α} : f ≤ filter.generate s ↔ s ⊆ f.sets := iff.intro (assume h u hu, h $ generate_sets.basic $ hu) (assume h u hu, hu.rec_on h univ_mem_sets (assume x y _ hxy hx, mem_sets_of_superset hx hxy) (assume x y _ _ hx hy, inter_mem_sets hx hy)) lemma mem_generate_iff (s : set $ set α) {U : set α} : U ∈ generate s ↔ ∃ t ⊆ s, finite t ∧ ⋂₀ t ⊆ U := begin split ; intro h, { induction h with V V_in V W V_in hVW hV V W V_in W_in hV hW, { use {V}, simp [V_in] }, { use ∅, simp [subset.refl, univ] }, { rcases hV with ⟨t, hts, htfin, hinter⟩, exact ⟨t, hts, htfin, subset.trans hinter hVW⟩ }, { rcases hV with ⟨t, hts, htfin, htinter⟩, rcases hW with ⟨z, hzs, hzfin, hzinter⟩, refine ⟨t ∪ z, union_subset hts hzs, htfin.union hzfin, _⟩, rw sInter_union, exact inter_subset_inter htinter hzinter } }, { rcases h with ⟨t, ts, tfin, h⟩, apply generate_sets.superset _ h, revert ts, apply finite.induction_on tfin, { intro h, rw sInter_empty, exact generate_sets.univ }, { intros V r hV rfin hinter h, cases insert_subset.mp h with V_in r_sub, rw [insert_eq V r, sInter_union], apply generate_sets.inter _ (hinter r_sub), rw sInter_singleton, exact generate_sets.basic V_in } }, end /-- `mk_of_closure s hs` constructs a filter on `α` whose elements set is exactly `s : set (set α)`, provided one gives the assumption `hs : (generate s).sets = s`. -/ protected def mk_of_closure (s : set (set α)) (hs : (generate s).sets = s) : filter α := { sets := s, univ_sets := hs ▸ (univ_mem_sets : univ ∈ generate s), sets_of_superset := assume x y, hs ▸ (mem_sets_of_superset : x ∈ generate s → x ⊆ y → y ∈ generate s), inter_sets := assume x y, hs ▸ (inter_mem_sets : x ∈ generate s → y ∈ generate s → x ∩ y ∈ generate s) } lemma mk_of_closure_sets {s : set (set α)} {hs : (generate s).sets = s} : filter.mk_of_closure s hs = generate s := filter.ext $ assume u, show u ∈ (filter.mk_of_closure s hs).sets ↔ u ∈ (generate s).sets, from hs.symm ▸ iff.rfl /-- Galois insertion from sets of sets into filters. -/ def gi_generate (α : Type*) : @galois_insertion (set (set α)) (order_dual (filter α)) _ _ filter.generate filter.sets := { gc := assume s f, sets_iff_generate, le_l_u := assume f u h, generate_sets.basic h, choice := λs hs, filter.mk_of_closure s (le_antisymm hs $ sets_iff_generate.1 $ le_refl _), choice_eq := assume s hs, mk_of_closure_sets } /-- The infimum of filters is the filter generated by intersections of elements of the two filters. -/ instance : has_inf (filter α) := ⟨λf g : filter α, { sets := {s | ∃ (a ∈ f) (b ∈ g), a ∩ b ⊆ s }, univ_sets := ⟨_, univ_mem_sets, _, univ_mem_sets, inter_subset_left _ _⟩, sets_of_superset := assume x y ⟨a, ha, b, hb, h⟩ xy, ⟨a, ha, b, hb, subset.trans h xy⟩, inter_sets := assume x y ⟨a, ha, b, hb, hx⟩ ⟨c, hc, d, hd, hy⟩, ⟨_, inter_mem_sets ha hc, _, inter_mem_sets hb hd, calc a ∩ c ∩ (b ∩ d) = (a ∩ b) ∩ (c ∩ d) : by ac_refl ... ⊆ x ∩ y : inter_subset_inter hx hy⟩ }⟩ @[simp] lemma mem_inf_sets {f g : filter α} {s : set α} : s ∈ f ⊓ g ↔ ∃t₁∈f, ∃t₂∈g, t₁ ∩ t₂ ⊆ s := iff.rfl lemma mem_inf_sets_of_left {f g : filter α} {s : set α} (h : s ∈ f) : s ∈ f ⊓ g := ⟨s, h, univ, univ_mem_sets, inter_subset_left _ _⟩ lemma mem_inf_sets_of_right {f g : filter α} {s : set α} (h : s ∈ g) : s ∈ f ⊓ g := ⟨univ, univ_mem_sets, s, h, inter_subset_right _ _⟩ lemma inter_mem_inf_sets {α : Type u} {f g : filter α} {s t : set α} (hs : s ∈ f) (ht : t ∈ g) : s ∩ t ∈ f ⊓ g := inter_mem_sets (mem_inf_sets_of_left hs) (mem_inf_sets_of_right ht) instance : has_top (filter α) := ⟨{ sets := {s | ∀x, x ∈ s}, univ_sets := assume x, mem_univ x, sets_of_superset := assume x y hx hxy a, hxy (hx a), inter_sets := assume x y hx hy a, mem_inter (hx _) (hy _) }⟩ lemma mem_top_sets_iff_forall {s : set α} : s ∈ (⊤ : filter α) ↔ (∀x, x ∈ s) := iff.rfl @[simp] lemma mem_top_sets {s : set α} : s ∈ (⊤ : filter α) ↔ s = univ := by rw [mem_top_sets_iff_forall, eq_univ_iff_forall] section complete_lattice /- We lift the complete lattice along the Galois connection `generate` / `sets`. Unfortunately, we want to have different definitional equalities for the lattice operations. So we define them upfront and change the lattice operations for the complete lattice instance. -/ private def original_complete_lattice : complete_lattice (filter α) := @order_dual.complete_lattice _ (gi_generate α).lift_complete_lattice local attribute [instance] original_complete_lattice instance : complete_lattice (filter α) := original_complete_lattice.copy /- le -/ filter.partial_order.le rfl /- top -/ (filter.has_top).1 (top_unique $ assume s hs, by have := univ_mem_sets ; finish) /- bot -/ _ rfl /- sup -/ _ rfl /- inf -/ (filter.has_inf).1 begin ext f g : 2, exact le_antisymm (le_inf (assume s, mem_inf_sets_of_left) (assume s, mem_inf_sets_of_right)) (assume s ⟨a, ha, b, hb, hs⟩, show s ∈ complete_lattice.inf f g, from mem_sets_of_superset (inter_mem_sets (@inf_le_left (filter α) _ _ _ _ ha) (@inf_le_right (filter α) _ _ _ _ hb)) hs) end /- Sup -/ (join ∘ 𝓟) (by ext s x; exact (@mem_bInter_iff _ _ s filter.sets x).symm) /- Inf -/ _ rfl end complete_lattice /-- A filter is `ne_bot` if it is not equal to `⊥`, or equivalently the empty set does not belong to the filter. Bourbaki include this assumption in the definition of a filter but we prefer to have a `complete_lattice` structure on filter, so we use a typeclass argument in lemmas instead. -/ @[class] def ne_bot (f : filter α) := f ≠ ⊥ lemma ne_bot.ne {f : filter α} (hf : ne_bot f) : f ≠ ⊥ := hf @[simp] lemma not_ne_bot {α : Type*} {f : filter α} : ¬ f.ne_bot ↔ f = ⊥ := not_not lemma ne_bot.mono {f g : filter α} (hf : ne_bot f) (hg : f ≤ g) : ne_bot g := ne_bot_of_le_ne_bot hf hg lemma ne_bot_of_le {f g : filter α} [hf : ne_bot f] (hg : f ≤ g) : ne_bot g := hf.mono hg lemma bot_sets_eq : (⊥ : filter α).sets = univ := rfl lemma sup_sets_eq {f g : filter α} : (f ⊔ g).sets = f.sets ∩ g.sets := (gi_generate α).gc.u_inf lemma Sup_sets_eq {s : set (filter α)} : (Sup s).sets = (⋂f∈s, (f:filter α).sets) := (gi_generate α).gc.u_Inf lemma supr_sets_eq {f : ι → filter α} : (supr f).sets = (⋂i, (f i).sets) := (gi_generate α).gc.u_infi lemma generate_empty : filter.generate ∅ = (⊤ : filter α) := (gi_generate α).gc.l_bot lemma generate_univ : filter.generate univ = (⊥ : filter α) := mk_of_closure_sets.symm lemma generate_union {s t : set (set α)} : filter.generate (s ∪ t) = filter.generate s ⊓ filter.generate t := (gi_generate α).gc.l_sup lemma generate_Union {s : ι → set (set α)} : filter.generate (⋃ i, s i) = (⨅ i, filter.generate (s i)) := (gi_generate α).gc.l_supr @[simp] lemma mem_bot_sets {s : set α} : s ∈ (⊥ : filter α) := trivial @[simp] lemma mem_sup_sets {f g : filter α} {s : set α} : s ∈ f ⊔ g ↔ s ∈ f ∧ s ∈ g := iff.rfl lemma union_mem_sup {f g : filter α} {s t : set α} (hs : s ∈ f) (ht : t ∈ g) : s ∪ t ∈ f ⊔ g := ⟨mem_sets_of_superset hs (subset_union_left s t), mem_sets_of_superset ht (subset_union_right s t)⟩ @[simp] lemma mem_Sup_sets {x : set α} {s : set (filter α)} : x ∈ Sup s ↔ (∀f∈s, x ∈ (f:filter α)) := iff.rfl @[simp] lemma mem_supr_sets {x : set α} {f : ι → filter α} : x ∈ supr f ↔ (∀i, x ∈ f i) := by simp only [supr_sets_eq, iff_self, mem_Inter] lemma infi_eq_generate (s : ι → filter α) : infi s = generate (⋃ i, (s i).sets) := show generate _ = generate _, from congr_arg _ supr_range lemma mem_infi_iff {ι} {s : ι → filter α} {U : set α} : (U ∈ ⨅ i, s i) ↔ ∃ I : set ι, finite I ∧ ∃ V : {i | i ∈ I} → set α, (∀ i, V i ∈ s i) ∧ (⋂ i, V i) ⊆ U := begin rw [infi_eq_generate, mem_generate_iff], split, { rintro ⟨t, tsub, tfin, tinter⟩, rcases eq_finite_Union_of_finite_subset_Union tfin tsub with ⟨I, Ifin, σ, σfin, σsub, rfl⟩, rw sInter_Union at tinter, let V := λ i, ⋂₀ σ i, have V_in : ∀ i, V i ∈ s i, { rintro ⟨i, i_in⟩, apply sInter_mem_sets_of_finite (σfin _), apply σsub }, exact ⟨I, Ifin, V, V_in, tinter⟩ }, { rintro ⟨I, Ifin, V, V_in, h⟩, refine ⟨range V, _, _, h⟩, { rintro _ ⟨i, rfl⟩, rw mem_Union, use [i, V_in i] }, { haveI : fintype {i : ι | i ∈ I} := finite.fintype Ifin, exact finite_range _ } }, end @[simp] lemma le_principal_iff {s : set α} {f : filter α} : f ≤ 𝓟 s ↔ s ∈ f := show (∀{t}, s ⊆ t → t ∈ f) ↔ s ∈ f, from ⟨assume h, h (subset.refl s), assume hs t ht, mem_sets_of_superset hs ht⟩ lemma principal_mono {s t : set α} : 𝓟 s ≤ 𝓟 t ↔ s ⊆ t := by simp only [le_principal_iff, iff_self, mem_principal_sets] @[mono] lemma monotone_principal : monotone (𝓟 : set α → filter α) := λ _ _, principal_mono.2 @[simp] lemma principal_eq_iff_eq {s t : set α} : 𝓟 s = 𝓟 t ↔ s = t := by simp only [le_antisymm_iff, le_principal_iff, mem_principal_sets]; refl @[simp] lemma join_principal_eq_Sup {s : set (filter α)} : join (𝓟 s) = Sup s := rfl @[simp] lemma principal_univ : 𝓟 (univ : set α) = ⊤ := top_unique $ by simp only [le_principal_iff, mem_top_sets, eq_self_iff_true] @[simp] lemma principal_empty : 𝓟 (∅ : set α) = ⊥ := bot_unique $ assume s _, empty_subset _ /-! ### Lattice equations -/ lemma empty_in_sets_eq_bot {f : filter α} : ∅ ∈ f ↔ f = ⊥ := ⟨assume h, bot_unique $ assume s _, mem_sets_of_superset h (empty_subset s), assume : f = ⊥, this.symm ▸ mem_bot_sets⟩ lemma nonempty_of_mem_sets {f : filter α} [hf : ne_bot f] {s : set α} (hs : s ∈ f) : s.nonempty := s.eq_empty_or_nonempty.elim (λ h, absurd hs (h.symm ▸ mt empty_in_sets_eq_bot.mp hf)) id lemma ne_bot.nonempty_of_mem {f : filter α} (hf : ne_bot f) {s : set α} (hs : s ∈ f) : s.nonempty := @nonempty_of_mem_sets α f hf s hs lemma nonempty_of_ne_bot (f : filter α) [ne_bot f] : nonempty α := nonempty_of_exists $ nonempty_of_mem_sets (univ_mem_sets : univ ∈ f) lemma filter_eq_bot_of_not_nonempty (f : filter α) (ne : ¬ nonempty α) : f = ⊥ := empty_in_sets_eq_bot.mp $ univ_mem_sets' $ assume x, false.elim (ne ⟨x⟩) lemma forall_sets_nonempty_iff_ne_bot {f : filter α} : (∀ (s : set α), s ∈ f → s.nonempty) ↔ ne_bot f := ⟨λ h hf, empty_not_nonempty (h ∅ $ hf.symm ▸ mem_bot_sets), @nonempty_of_mem_sets _ _⟩ lemma mem_sets_of_eq_bot {f : filter α} {s : set α} (h : f ⊓ 𝓟 sᶜ = ⊥) : s ∈ f := have ∅ ∈ f ⊓ 𝓟 sᶜ, from h.symm ▸ mem_bot_sets, let ⟨s₁, hs₁, s₂, (hs₂ : sᶜ ⊆ s₂), (hs : s₁ ∩ s₂ ⊆ ∅)⟩ := this in by filter_upwards [hs₁] assume a ha, classical.by_contradiction $ assume ha', hs ⟨ha, hs₂ ha'⟩ lemma inf_ne_bot_iff {f g : filter α} : ne_bot (f ⊓ g) ↔ ∀ {U V}, U ∈ f → V ∈ g → set.nonempty (U ∩ V) := begin rw ← forall_sets_nonempty_iff_ne_bot, simp_rw mem_inf_sets, split ; intro h, { intros U V U_in V_in, exact h (U ∩ V) ⟨U, U_in, V, V_in, subset.refl _⟩ }, { rintros S ⟨U, U_in, V, V_in, hUV⟩, cases h U_in V_in with a ha, use [a, hUV ha] } end lemma inf_principal_ne_bot_iff {f : filter α} {s : set α} : ne_bot (f ⊓ 𝓟 s) ↔ ∀ U ∈ f, (U ∩ s).nonempty := begin rw inf_ne_bot_iff, apply forall_congr, intros U, split, { intros h U_in, exact h U_in (mem_principal_self s) }, { intros h V U_in V_in, rw mem_principal_sets at V_in, cases h U_in with x hx, exact ⟨x, hx.1, V_in hx.2⟩ }, end lemma inf_eq_bot_iff {f g : filter α} : f ⊓ g = ⊥ ↔ ∃ U V, (U ∈ f) ∧ (V ∈ g) ∧ U ∩ V = ∅ := begin rw ← not_iff_not, apply inf_ne_bot_iff.trans, simp only [not_exists, not_and, ← ne.def, ne_empty_iff_nonempty] end protected lemma disjoint_iff {f g : filter α} : disjoint f g ↔ ∃ U V, (U ∈ f) ∧ (V ∈ g) ∧ U ∩ V = ∅ := disjoint_iff.trans inf_eq_bot_iff lemma eq_Inf_of_mem_sets_iff_exists_mem {S : set (filter α)} {l : filter α} (h : ∀ {s}, s ∈ l ↔ ∃ f ∈ S, s ∈ f) : l = Inf S := le_antisymm (le_Inf $ λ f hf s hs, h.2 ⟨f, hf, hs⟩) (λ s hs, let ⟨f, hf, hs⟩ := h.1 hs in (Inf_le hf : Inf S ≤ f) hs) lemma eq_infi_of_mem_sets_iff_exists_mem {f : ι → filter α} {l : filter α} (h : ∀ {s}, s ∈ l ↔ ∃ i, s ∈ f i) : l = infi f := eq_Inf_of_mem_sets_iff_exists_mem $ λ s, h.trans exists_range_iff.symm lemma eq_binfi_of_mem_sets_iff_exists_mem {f : ι → filter α} {p : ι → Prop} {l : filter α} (h : ∀ {s}, s ∈ l ↔ ∃ i (_ : p i), s ∈ f i) : l = ⨅ i (_ : p i), f i := begin rw [infi_subtype'], apply eq_infi_of_mem_sets_iff_exists_mem, intro s, exact h.trans ⟨λ ⟨i, pi, si⟩, ⟨⟨i, pi⟩, si⟩, λ ⟨⟨i, pi⟩, si⟩, ⟨i, pi, si⟩⟩ end lemma infi_sets_eq {f : ι → filter α} (h : directed (≥) f) [ne : nonempty ι] : (infi f).sets = (⋃ i, (f i).sets) := let ⟨i⟩ := ne, u := { filter . sets := (⋃ i, (f i).sets), univ_sets := by simp only [mem_Union]; exact ⟨i, univ_mem_sets⟩, sets_of_superset := by simp only [mem_Union, exists_imp_distrib]; intros x y i hx hxy; exact ⟨i, mem_sets_of_superset hx hxy⟩, inter_sets := begin simp only [mem_Union, exists_imp_distrib], assume x y a hx b hy, rcases h a b with ⟨c, ha, hb⟩, exact ⟨c, inter_mem_sets (ha hx) (hb hy)⟩ end } in have u = infi f, from eq_infi_of_mem_sets_iff_exists_mem (λ s, by simp only [mem_Union]), congr_arg filter.sets this.symm lemma mem_infi {f : ι → filter α} (h : directed (≥) f) [nonempty ι] (s) : s ∈ infi f ↔ ∃ i, s ∈ f i := by simp only [infi_sets_eq h, mem_Union] lemma binfi_sets_eq {f : β → filter α} {s : set β} (h : directed_on (f ⁻¹'o (≥)) s) (ne : s.nonempty) : (⨅ i∈s, f i).sets = (⋃ i ∈ s, (f i).sets) := by haveI := ne.to_subtype; calc (⨅ i ∈ s, f i).sets = (⨅ t : {t // t ∈ s}, (f t.val)).sets : by rw [infi_subtype]; refl ... = (⨆ t : {t // t ∈ s}, (f t.val).sets) : infi_sets_eq h.directed_coe ... = (⨆ t ∈ s, (f t).sets) : by rw [supr_subtype]; refl lemma mem_binfi {f : β → filter α} {s : set β} (h : directed_on (f ⁻¹'o (≥)) s) (ne : s.nonempty) {t : set α} : t ∈ (⨅ i∈s, f i) ↔ ∃ i ∈ s, t ∈ f i := by simp only [binfi_sets_eq h ne, mem_bUnion_iff] lemma infi_sets_eq_finite {ι : Type*} (f : ι → filter α) : (⨅i, f i).sets = (⋃t:finset ι, (⨅i∈t, f i).sets) := begin rw [infi_eq_infi_finset, infi_sets_eq], exact (directed_of_sup $ λs₁ s₂ hs, infi_le_infi $ λi, infi_le_infi_const $ λh, hs h), end lemma infi_sets_eq_finite' (f : ι → filter α) : (⨅i, f i).sets = (⋃t:finset (plift ι), (⨅i∈t, f (plift.down i)).sets) := by rw [← infi_sets_eq_finite, ← equiv.plift.surjective.infi_comp]; refl lemma mem_infi_finite {ι : Type*} {f : ι → filter α} (s) : s ∈ infi f ↔ s ∈ ⋃t:finset ι, (⨅i∈t, f i).sets := set.ext_iff.1 (infi_sets_eq_finite f) s lemma mem_infi_finite' {f : ι → filter α} (s) : s ∈ infi f ↔ s ∈ ⋃t:finset (plift ι), (⨅i∈t, f (plift.down i)).sets := set.ext_iff.1 (infi_sets_eq_finite' f) s @[simp] lemma sup_join {f₁ f₂ : filter (filter α)} : (join f₁ ⊔ join f₂) = join (f₁ ⊔ f₂) := filter_eq $ set.ext $ assume x, by simp only [supr_sets_eq, join, mem_sup_sets, iff_self, mem_set_of_eq] @[simp] lemma supr_join {ι : Sort w} {f : ι → filter (filter α)} : (⨆x, join (f x)) = join (⨆x, f x) := filter_eq $ set.ext $ assume x, by simp only [supr_sets_eq, join, iff_self, mem_Inter, mem_set_of_eq] instance : bounded_distrib_lattice (filter α) := { le_sup_inf := begin assume x y z s, simp only [and_assoc, mem_inf_sets, mem_sup_sets, exists_prop, exists_imp_distrib, and_imp], intros hs t₁ ht₁ t₂ ht₂ hts, exact ⟨s ∪ t₁, x.sets_of_superset hs $ subset_union_left _ _, y.sets_of_superset ht₁ $ subset_union_right _ _, s ∪ t₂, x.sets_of_superset hs $ subset_union_left _ _, z.sets_of_superset ht₂ $ subset_union_right _ _, subset.trans (@le_sup_inf (set α) _ _ _ _) (union_subset (subset.refl _) hts)⟩ end, ..filter.complete_lattice } /- the complementary version with ⨆i, f ⊓ g i does not hold! -/ lemma infi_sup_left {f : filter α} {g : ι → filter α} : (⨅ x, f ⊔ g x) = f ⊔ infi g := begin refine le_antisymm _ (le_infi $ assume i, sup_le_sup_left (infi_le _ _) _), rintros t ⟨h₁, h₂⟩, rw [infi_sets_eq_finite'] at h₂, simp only [mem_Union, (finset.inf_eq_infi _ _).symm] at h₂, rcases h₂ with ⟨s, hs⟩, suffices : (⨅i, f ⊔ g i) ≤ f ⊔ s.inf (λi, g i.down), { exact this ⟨h₁, hs⟩ }, refine finset.induction_on s _ _, { exact le_sup_right_of_le le_top }, { rintros ⟨i⟩ s his ih, rw [finset.inf_insert, sup_inf_left], exact le_inf (infi_le _ _) ih } end lemma infi_sup_right {f : filter α} {g : ι → filter α} : (⨅ x, g x ⊔ f) = infi g ⊔ f := by simp [sup_comm, ← infi_sup_left] lemma binfi_sup_right (p : ι → Prop) (f : ι → filter α) (g : filter α) : (⨅ i (h : p i), (f i ⊔ g)) = (⨅ i (h : p i), f i) ⊔ g := by rw [infi_subtype', infi_sup_right, infi_subtype'] lemma binfi_sup_left (p : ι → Prop) (f : ι → filter α) (g : filter α) : (⨅ i (h : p i), (g ⊔ f i)) = g ⊔ (⨅ i (h : p i), f i) := by rw [infi_subtype', infi_sup_left, infi_subtype'] lemma mem_infi_sets_finset {s : finset α} {f : α → filter β} : ∀t, t ∈ (⨅a∈s, f a) ↔ (∃p:α → set β, (∀a∈s, p a ∈ f a) ∧ (⋂a∈s, p a) ⊆ t) := show ∀t, t ∈ (⨅a∈s, f a) ↔ (∃p:α → set β, (∀a∈s, p a ∈ f a) ∧ (⨅a∈s, p a) ≤ t), begin simp only [(finset.inf_eq_infi _ _).symm], refine finset.induction_on s _ _, { simp only [finset.not_mem_empty, false_implies_iff, finset.inf_empty, top_le_iff, imp_true_iff, mem_top_sets, true_and, exists_const], intros; refl }, { intros a s has ih t, simp only [ih, finset.forall_mem_insert, finset.inf_insert, mem_inf_sets, exists_prop, iff_iff_implies_and_implies, exists_imp_distrib, and_imp, and_assoc] {contextual := tt}, split, { intros t₁ ht₁ t₂ p hp ht₂ ht, existsi function.update p a t₁, have : ∀a'∈s, function.update p a t₁ a' = p a', from assume a' ha', have a' ≠ a, from assume h, has $ h ▸ ha', function.update_noteq this _ _, have eq : s.inf (λj, function.update p a t₁ j) = s.inf (λj, p j) := finset.inf_congr rfl this, simp only [this, ht₁, hp, function.update_same, true_and, imp_true_iff, eq] {contextual := tt}, exact subset.trans (inter_subset_inter (subset.refl _) ht₂) ht }, assume p hpa hp ht, exact ⟨p a, hpa, (s.inf p), ⟨⟨p, hp, le_refl _⟩, ht⟩⟩ } end /-- If `f : ι → filter α` is directed, `ι` is not empty, and `∀ i, f i ≠ ⊥`, then `infi f ≠ ⊥`. See also `infi_ne_bot_of_directed` for a version assuming `nonempty α` instead of `nonempty ι`. -/ lemma infi_ne_bot_of_directed' {f : ι → filter α} [nonempty ι] (hd : directed (≥) f) (hb : ∀i, ne_bot (f i)) : ne_bot (infi f) := begin intro h, have he: ∅ ∈ (infi f), from h.symm ▸ (mem_bot_sets : ∅ ∈ (⊥ : filter α)), obtain ⟨i, hi⟩ : ∃i, ∅ ∈ f i, from (mem_infi hd ∅).1 he, exact hb i (empty_in_sets_eq_bot.1 hi) end /-- If `f : ι → filter α` is directed, `α` is not empty, and `∀ i, f i ≠ ⊥`, then `infi f ≠ ⊥`. See also `infi_ne_bot_of_directed'` for a version assuming `nonempty ι` instead of `nonempty α`. -/ lemma infi_ne_bot_of_directed {f : ι → filter α} [hn : nonempty α] (hd : directed (≥) f) (hb : ∀i, ne_bot (f i)) : ne_bot (infi f) := if hι : nonempty ι then @infi_ne_bot_of_directed' _ _ _ hι hd hb else assume h : infi f = ⊥, have univ ⊆ (∅ : set α), begin rw [←principal_mono, principal_univ, principal_empty, ←h], exact (le_infi $ assume i, false.elim $ hι ⟨i⟩) end, let ⟨x⟩ := hn in this (mem_univ x) lemma infi_ne_bot_iff_of_directed' {f : ι → filter α} [nonempty ι] (hd : directed (≥) f) : ne_bot (infi f) ↔ ∀i, ne_bot (f i) := ⟨assume H i, H.mono (infi_le _ i), infi_ne_bot_of_directed' hd⟩ lemma infi_ne_bot_iff_of_directed {f : ι → filter α} [nonempty α] (hd : directed (≥) f) : ne_bot (infi f) ↔ (∀i, ne_bot (f i)) := ⟨assume H i, H.mono (infi_le _ i), infi_ne_bot_of_directed hd⟩ lemma mem_infi_sets {f : ι → filter α} (i : ι) : ∀{s}, s ∈ f i → s ∈ ⨅i, f i := show (⨅i, f i) ≤ f i, from infi_le _ _ @[elab_as_eliminator] lemma infi_sets_induct {f : ι → filter α} {s : set α} (hs : s ∈ infi f) {p : set α → Prop} (uni : p univ) (ins : ∀{i s₁ s₂}, s₁ ∈ f i → p s₂ → p (s₁ ∩ s₂)) (upw : ∀{s₁ s₂}, s₁ ⊆ s₂ → p s₁ → p s₂) : p s := begin rw [mem_infi_finite'] at hs, simp only [mem_Union, (finset.inf_eq_infi _ _).symm] at hs, rcases hs with ⟨is, his⟩, revert s, refine finset.induction_on is _ _, { assume s hs, rwa [mem_top_sets.1 hs] }, { rintros ⟨i⟩ js his ih s hs, rw [finset.inf_insert, mem_inf_sets] at hs, rcases hs with ⟨s₁, hs₁, s₂, hs₂, hs⟩, exact upw hs (ins hs₁ (ih hs₂)) } end /- principal equations -/ @[simp] lemma inf_principal {s t : set α} : 𝓟 s ⊓ 𝓟 t = 𝓟 (s ∩ t) := le_antisymm (by simp; exact ⟨s, subset.refl s, t, subset.refl t, by simp⟩) (by simp [le_inf_iff, inter_subset_left, inter_subset_right]) @[simp] lemma sup_principal {s t : set α} : 𝓟 s ⊔ 𝓟 t = 𝓟 (s ∪ t) := filter_eq $ set.ext $ by simp only [union_subset_iff, union_subset_iff, mem_sup_sets, forall_const, iff_self, mem_principal_sets] @[simp] lemma supr_principal {ι : Sort w} {s : ι → set α} : (⨆x, 𝓟 (s x)) = 𝓟 (⋃i, s i) := filter_eq $ set.ext $ assume x, by simp only [supr_sets_eq, mem_principal_sets, mem_Inter]; exact (@supr_le_iff (set α) _ _ _ _).symm @[simp] lemma principal_eq_bot_iff {s : set α} : 𝓟 s = ⊥ ↔ s = ∅ := empty_in_sets_eq_bot.symm.trans $ mem_principal_sets.trans subset_empty_iff lemma principal_ne_bot_iff {s : set α} : ne_bot (𝓟 s) ↔ s.nonempty := (not_congr principal_eq_bot_iff).trans ne_empty_iff_nonempty lemma is_compl_principal (s : set α) : is_compl (𝓟 s) (𝓟 sᶜ) := ⟨by simp only [inf_principal, inter_compl_self, principal_empty, le_refl], by simp only [sup_principal, union_compl_self, principal_univ, le_refl]⟩ lemma inf_principal_eq_bot {f : filter α} {s : set α} (hs : sᶜ ∈ f) : f ⊓ 𝓟 s = ⊥ := empty_in_sets_eq_bot.mp ⟨_, hs, s, mem_principal_self s, assume x ⟨h₁, h₂⟩, h₁ h₂⟩ theorem mem_inf_principal {f : filter α} {s t : set α} : s ∈ f ⊓ 𝓟 t ↔ {x | x ∈ t → x ∈ s} ∈ f := begin simp only [← le_principal_iff, (is_compl_principal s).le_left_iff, disjoint, inf_assoc, inf_principal, imp_iff_not_or], rw [← disjoint, ← (is_compl_principal (t ∩ sᶜ)).le_right_iff, compl_inter, compl_compl], refl end lemma mem_iff_inf_principal_compl {f : filter α} {V : set α} : V ∈ f ↔ f ⊓ 𝓟 Vᶜ = ⊥ := begin rw inf_eq_bot_iff, split, { intro h, use [V, Vᶜ], simp [h, subset.refl] }, { rintros ⟨U, W, U_in, W_in, UW⟩, rw [mem_principal_sets, compl_subset_comm] at W_in, apply mem_sets_of_superset U_in, intros x x_in, apply W_in, intro H, have : x ∈ U ∩ W := ⟨x_in, H⟩, rwa UW at this }, end lemma le_iff_forall_inf_principal_compl {f g : filter α} : f ≤ g ↔ ∀ V ∈ g, f ⊓ 𝓟 Vᶜ = ⊥ := begin change (∀ V ∈ g, V ∈ f) ↔ _, simp_rw [mem_iff_inf_principal_compl], end lemma principal_le_iff {s : set α} {f : filter α} : 𝓟 s ≤ f ↔ ∀ V ∈ f, s ⊆ V := begin change (∀ V, V ∈ f → V ∈ _) ↔ _, simp_rw mem_principal_sets, end @[simp] lemma infi_principal_finset {ι : Type w} (s : finset ι) (f : ι → set α) : (⨅i∈s, 𝓟 (f i)) = 𝓟 (⋂i∈s, f i) := begin ext t, simp [mem_infi_sets_finset], split, { rintros ⟨p, hp, ht⟩, calc (⋂ (i : ι) (H : i ∈ s), f i) ≤ (⋂ (i : ι) (H : i ∈ s), p i) : infi_le_infi (λi, infi_le_infi (λhi, mem_principal_sets.1 (hp i hi))) ... ≤ t : ht }, { assume h, exact ⟨f, λi hi, subset.refl _, h⟩ } end @[simp] lemma infi_principal_fintype {ι : Type w} [fintype ι] (f : ι → set α) : (⨅i, 𝓟 (f i)) = 𝓟 (⋂i, f i) := by simpa using infi_principal_finset finset.univ f end lattice @[mono] lemma join_mono {f₁ f₂ : filter (filter α)} (h : f₁ ≤ f₂) : join f₁ ≤ join f₂ := λ s hs, h hs /-! ### Eventually -/ /-- `f.eventually p` or `∀ᶠ x in f, p x` mean that `{x | p x} ∈ f`. E.g., `∀ᶠ x in at_top, p x` means that `p` holds true for sufficiently large `x`. -/ protected def eventually (p : α → Prop) (f : filter α) : Prop := {x | p x} ∈ f notation `∀ᶠ` binders ` in ` f `, ` r:(scoped p, filter.eventually p f) := r lemma eventually_iff {f : filter α} {P : α → Prop} : (∀ᶠ x in f, P x) ↔ {x | P x} ∈ f := iff.rfl protected lemma ext' {f₁ f₂ : filter α} (h : ∀ p : α → Prop, (∀ᶠ x in f₁, p x) ↔ (∀ᶠ x in f₂, p x)) : f₁ = f₂ := filter.ext h lemma eventually.filter_mono {f₁ f₂ : filter α} (h : f₁ ≤ f₂) {p : α → Prop} (hp : ∀ᶠ x in f₂, p x) : ∀ᶠ x in f₁, p x := h hp lemma eventually_of_mem {f : filter α} {P : α → Prop} {U : set α} (hU : U ∈ f) (h : ∀ x ∈ U, P x) : ∀ᶠ x in f, P x := mem_sets_of_superset hU h protected lemma eventually.and {p q : α → Prop} {f : filter α} : f.eventually p → f.eventually q → ∀ᶠ x in f, p x ∧ q x := inter_mem_sets @[simp] lemma eventually_true (f : filter α) : ∀ᶠ x in f, true := univ_mem_sets lemma eventually_of_forall {p : α → Prop} {f : filter α} (hp : ∀ x, p x) : ∀ᶠ x in f, p x := univ_mem_sets' hp @[simp] lemma eventually_false_iff_eq_bot {f : filter α} : (∀ᶠ x in f, false) ↔ f = ⊥ := empty_in_sets_eq_bot @[simp] lemma eventually_const {f : filter α} [ne_bot f] {p : Prop} : (∀ᶠ x in f, p) ↔ p := classical.by_cases (λ h : p, by simp [h]) (λ h, by simpa [h]) lemma eventually_iff_exists_mem {p : α → Prop} {f : filter α} : (∀ᶠ x in f, p x) ↔ ∃ v ∈ f, ∀ y ∈ v, p y := exists_sets_subset_iff.symm lemma eventually.exists_mem {p : α → Prop} {f : filter α} (hp : ∀ᶠ x in f, p x) : ∃ v ∈ f, ∀ y ∈ v, p y := eventually_iff_exists_mem.1 hp lemma eventually.mp {p q : α → Prop} {f : filter α} (hp : ∀ᶠ x in f, p x) (hq : ∀ᶠ x in f, p x → q x) : ∀ᶠ x in f, q x := mp_sets hp hq lemma eventually.mono {p q : α → Prop} {f : filter α} (hp : ∀ᶠ x in f, p x) (hq : ∀ x, p x → q x) : ∀ᶠ x in f, q x := hp.mp (eventually_of_forall hq) @[simp] lemma eventually_and {p q : α → Prop} {f : filter α} : (∀ᶠ x in f, p x ∧ q x) ↔ (∀ᶠ x in f, p x) ∧ (∀ᶠ x in f, q x) := ⟨λ h, ⟨h.mono $ λ _, and.left, h.mono $ λ _, and.right⟩, λ h, h.1.and h.2⟩ lemma eventually.congr {f : filter α} {p q : α → Prop} (h' : ∀ᶠ x in f, p x) (h : ∀ᶠ x in f, p x ↔ q x) : ∀ᶠ x in f, q x := h'.mp (h.mono $ λ x hx, hx.mp) lemma eventually_congr {f : filter α} {p q : α → Prop} (h : ∀ᶠ x in f, p x ↔ q x) : (∀ᶠ x in f, p x) ↔ (∀ᶠ x in f, q x) := ⟨λ hp, hp.congr h, λ hq, hq.congr $ by simpa only [iff.comm] using h⟩ @[simp] lemma eventually_or_distrib_left {f : filter α} {p : Prop} {q : α → Prop} : (∀ᶠ x in f, p ∨ q x) ↔ (p ∨ ∀ᶠ x in f, q x) := classical.by_cases (λ h : p, by simp [h]) (λ h, by simp [h]) @[simp] lemma eventually_or_distrib_right {f : filter α} {p : α → Prop} {q : Prop} : (∀ᶠ x in f, p x ∨ q) ↔ ((∀ᶠ x in f, p x) ∨ q) := by simp only [or_comm _ q, eventually_or_distrib_left] @[simp] lemma eventually_imp_distrib_left {f : filter α} {p : Prop} {q : α → Prop} : (∀ᶠ x in f, p → q x) ↔ (p → ∀ᶠ x in f, q x) := by simp only [imp_iff_not_or, eventually_or_distrib_left] @[simp] lemma eventually_bot {p : α → Prop} : ∀ᶠ x in ⊥, p x := ⟨⟩ @[simp] lemma eventually_top {p : α → Prop} : (∀ᶠ x in ⊤, p x) ↔ (∀ x, p x) := iff.rfl lemma eventually_sup {p : α → Prop} {f g : filter α} : (∀ᶠ x in f ⊔ g, p x) ↔ (∀ᶠ x in f, p x) ∧ (∀ᶠ x in g, p x) := iff.rfl @[simp] lemma eventually_Sup {p : α → Prop} {fs : set (filter α)} : (∀ᶠ x in Sup fs, p x) ↔ (∀ f ∈ fs, ∀ᶠ x in f, p x) := iff.rfl @[simp] lemma eventually_supr {p : α → Prop} {fs : β → filter α} : (∀ᶠ x in (⨆ b, fs b), p x) ↔ (∀ b, ∀ᶠ x in fs b, p x) := mem_supr_sets @[simp] lemma eventually_principal {a : set α} {p : α → Prop} : (∀ᶠ x in 𝓟 a, p x) ↔ (∀ x ∈ a, p x) := iff.rfl theorem eventually_inf_principal {f : filter α} {p : α → Prop} {s : set α} : (∀ᶠ x in f ⊓ 𝓟 s, p x) ↔ ∀ᶠ x in f, x ∈ s → p x := mem_inf_principal /-! ### Frequently -/ /-- `f.frequently p` or `∃ᶠ x in f, p x` mean that `{x | ¬p x} ∉ f`. E.g., `∃ᶠ x in at_top, p x` means that there exist arbitrarily large `x` for which `p` holds true. -/ protected def frequently (p : α → Prop) (f : filter α) : Prop := ¬∀ᶠ x in f, ¬p x notation `∃ᶠ` binders ` in ` f `, ` r:(scoped p, filter.frequently p f) := r lemma eventually.frequently {f : filter α} [ne_bot f] {p : α → Prop} (h : ∀ᶠ x in f, p x) : ∃ᶠ x in f, p x := begin assume h', have := h.and h', simp only [and_not_self, eventually_false_iff_eq_bot] at this, contradiction end lemma frequently_of_forall {f : filter α} [ne_bot f] {p : α → Prop} (h : ∀ x, p x) : ∃ᶠ x in f, p x := eventually.frequently (eventually_of_forall h) lemma frequently.mp {p q : α → Prop} {f : filter α} (h : ∃ᶠ x in f, p x) (hpq : ∀ᶠ x in f, p x → q x) : ∃ᶠ x in f, q x := mt (λ hq, hq.mp $ hpq.mono $ λ x, mt) h lemma frequently.mono {p q : α → Prop} {f : filter α} (h : ∃ᶠ x in f, p x) (hpq : ∀ x, p x → q x) : ∃ᶠ x in f, q x := h.mp (eventually_of_forall hpq) lemma frequently.and_eventually {p q : α → Prop} {f : filter α} (hp : ∃ᶠ x in f, p x) (hq : ∀ᶠ x in f, q x) : ∃ᶠ x in f, p x ∧ q x := begin refine mt (λ h, hq.mp $ h.mono _) hp, assume x hpq hq hp, exact hpq ⟨hp, hq⟩ end lemma frequently.exists {p : α → Prop} {f : filter α} (hp : ∃ᶠ x in f, p x) : ∃ x, p x := begin by_contradiction H, replace H : ∀ᶠ x in f, ¬ p x, from eventually_of_forall (not_exists.1 H), exact hp H end lemma eventually.exists {p : α → Prop} {f : filter α} [ne_bot f] (hp : ∀ᶠ x in f, p x) : ∃ x, p x := hp.frequently.exists lemma frequently_iff_forall_eventually_exists_and {p : α → Prop} {f : filter α} : (∃ᶠ x in f, p x) ↔ ∀ {q : α → Prop}, (∀ᶠ x in f, q x) → ∃ x, p x ∧ q x := ⟨assume hp q hq, (hp.and_eventually hq).exists, assume H hp, by simpa only [and_not_self, exists_false] using H hp⟩ lemma frequently_iff {f : filter α} {P : α → Prop} : (∃ᶠ x in f, P x) ↔ ∀ {U}, U ∈ f → ∃ x ∈ U, P x := begin rw frequently_iff_forall_eventually_exists_and, split ; intro h, { intros U U_in, simpa [exists_prop, and_comm] using h U_in }, { intros H H', simpa [and_comm] using h H' }, end @[simp] lemma not_eventually {p : α → Prop} {f : filter α} : (¬ ∀ᶠ x in f, p x) ↔ (∃ᶠ x in f, ¬ p x) := by simp [filter.frequently] @[simp] lemma not_frequently {p : α → Prop} {f : filter α} : (¬ ∃ᶠ x in f, p x) ↔ (∀ᶠ x in f, ¬ p x) := by simp only [filter.frequently, not_not] @[simp] lemma frequently_true_iff_ne_bot (f : filter α) : (∃ᶠ x in f, true) ↔ ne_bot f := by simp [filter.frequently, -not_eventually, eventually_false_iff_eq_bot, ne_bot] @[simp] lemma frequently_false (f : filter α) : ¬ ∃ᶠ x in f, false := by simp @[simp] lemma frequently_const {f : filter α} [ne_bot f] {p : Prop} : (∃ᶠ x in f, p) ↔ p := classical.by_cases (λ h : p, by simpa [h]) (λ h, by simp [h]) @[simp] lemma frequently_or_distrib {f : filter α} {p q : α → Prop} : (∃ᶠ x in f, p x ∨ q x) ↔ (∃ᶠ x in f, p x) ∨ (∃ᶠ x in f, q x) := by simp only [filter.frequently, ← not_and_distrib, not_or_distrib, eventually_and] lemma frequently_or_distrib_left {f : filter α} [ne_bot f] {p : Prop} {q : α → Prop} : (∃ᶠ x in f, p ∨ q x) ↔ (p ∨ ∃ᶠ x in f, q x) := by simp lemma frequently_or_distrib_right {f : filter α} [ne_bot f] {p : α → Prop} {q : Prop} : (∃ᶠ x in f, p x ∨ q) ↔ (∃ᶠ x in f, p x) ∨ q := by simp @[simp] lemma frequently_imp_distrib {f : filter α} {p q : α → Prop} : (∃ᶠ x in f, p x → q x) ↔ ((∀ᶠ x in f, p x) → ∃ᶠ x in f, q x) := by simp [imp_iff_not_or, not_eventually, frequently_or_distrib] lemma frequently_imp_distrib_left {f : filter α} [ne_bot f] {p : Prop} {q : α → Prop} : (∃ᶠ x in f, p → q x) ↔ (p → ∃ᶠ x in f, q x) := by simp lemma frequently_imp_distrib_right {f : filter α} [ne_bot f] {p : α → Prop} {q : Prop} : (∃ᶠ x in f, p x → q) ↔ ((∀ᶠ x in f, p x) → q) := by simp @[simp] lemma eventually_imp_distrib_right {f : filter α} {p : α → Prop} {q : Prop} : (∀ᶠ x in f, p x → q) ↔ ((∃ᶠ x in f, p x) → q) := by simp only [imp_iff_not_or, eventually_or_distrib_right, not_frequently] @[simp] lemma frequently_bot {p : α → Prop} : ¬ ∃ᶠ x in ⊥, p x := by simp @[simp] lemma frequently_top {p : α → Prop} : (∃ᶠ x in ⊤, p x) ↔ (∃ x, p x) := by simp [filter.frequently] lemma inf_ne_bot_iff_frequently_left {f g : filter α} : ne_bot (f ⊓ g) ↔ ∀ {p : α → Prop}, (∀ᶠ x in f, p x) → ∃ᶠ x in g, p x := begin rw filter.inf_ne_bot_iff, split ; intro h, { intros U U_in H, rcases h U_in H with ⟨x, hx, hx'⟩, exact hx' hx}, { intros U V U_in V_in, classical, by_contra H, exact h U_in (mem_sets_of_superset V_in $ λ v v_in v_in', H ⟨v, v_in', v_in⟩) } end lemma inf_ne_bot_iff_frequently_right {f g : filter α} : ne_bot (f ⊓ g) ↔ ∀ {p : α → Prop}, (∀ᶠ x in g, p x) → ∃ᶠ x in f, p x := by { rw inf_comm, exact filter.inf_ne_bot_iff_frequently_left } @[simp] lemma frequently_principal {a : set α} {p : α → Prop} : (∃ᶠ x in 𝓟 a, p x) ↔ (∃ x ∈ a, p x) := by simp [filter.frequently, not_forall] lemma frequently_sup {p : α → Prop} {f g : filter α} : (∃ᶠ x in f ⊔ g, p x) ↔ (∃ᶠ x in f, p x) ∨ (∃ᶠ x in g, p x) := by simp only [filter.frequently, eventually_sup, not_and_distrib] @[simp] lemma frequently_Sup {p : α → Prop} {fs : set (filter α)} : (∃ᶠ x in Sup fs, p x) ↔ (∃ f ∈ fs, ∃ᶠ x in f, p x) := by simp [filter.frequently, -not_eventually, not_forall] @[simp] lemma frequently_supr {p : α → Prop} {fs : β → filter α} : (∃ᶠ x in (⨆ b, fs b), p x) ↔ (∃ b, ∃ᶠ x in fs b, p x) := by simp [filter.frequently, -not_eventually, not_forall] /-! ### Relation “eventually equal” -/ /-- Two functions `f` and `g` are *eventually equal* along a filter `l` if the set of `x` such that `f x = g x` belongs to `l`. -/ def eventually_eq (l : filter α) (f g : α → β) : Prop := ∀ᶠ x in l, f x = g x notation f ` =ᶠ[`:50 l:50 `] `:0 g:50 := eventually_eq l f g lemma eventually_eq.eventually {l : filter α} {f g : α → β} (h : f =ᶠ[l] g) : ∀ᶠ x in l, f x = g x := h lemma eventually_eq.rw {l : filter α} {f g : α → β} (h : f =ᶠ[l] g) (p : α → β → Prop) (hf : ∀ᶠ x in l, p x (f x)) : ∀ᶠ x in l, p x (g x) := hf.congr $ h.mono $ λ x hx, hx ▸ iff.rfl lemma eventually_eq_set {s t : set α} {l : filter α} : s =ᶠ[l] t ↔ ∀ᶠ x in l, x ∈ s ↔ x ∈ t := eventually_congr $ eventually_of_forall $ λ x, ⟨eq.to_iff, iff.to_eq⟩ alias eventually_eq_set ↔ filter.eventually_eq.mem_iff filter.eventually.set_eq lemma eventually_eq.exists_mem {l : filter α} {f g : α → β} (h : f =ᶠ[l] g) : ∃ s ∈ l, eq_on f g s := h.exists_mem lemma eventually_eq_of_mem {l : filter α} {f g : α → β} {s : set α} (hs : s ∈ l) (h : eq_on f g s) : f =ᶠ[l] g := eventually_of_mem hs h lemma eventually_eq_iff_exists_mem {l : filter α} {f g : α → β} : (f =ᶠ[l] g) ↔ ∃ s ∈ l, eq_on f g s := eventually_iff_exists_mem lemma eventually_eq.filter_mono {l l' : filter α} {f g : α → β} (h₁ : f =ᶠ[l] g) (h₂ : l' ≤ l) : f =ᶠ[l'] g := h₂ h₁ @[refl] lemma eventually_eq.refl (l : filter α) (f : α → β) : f =ᶠ[l] f := eventually_of_forall $ λ x, rfl @[symm] lemma eventually_eq.symm {f g : α → β} {l : filter α} (H : f =ᶠ[l] g) : g =ᶠ[l] f := H.mono $ λ _, eq.symm @[trans] lemma eventually_eq.trans {f g h : α → β} {l : filter α} (H₁ : f =ᶠ[l] g) (H₂ : g =ᶠ[l] h) : f =ᶠ[l] h := H₂.rw (λ x y, f x = y) H₁ lemma eventually_eq.prod_mk {l} {f f' : α → β} (hf : f =ᶠ[l] f') {g g' : α → γ} (hg : g =ᶠ[l] g') : (λ x, (f x, g x)) =ᶠ[l] (λ x, (f' x, g' x)) := hf.mp $ hg.mono $ by { intros, simp only * } lemma eventually_eq.fun_comp {f g : α → β} {l : filter α} (H : f =ᶠ[l] g) (h : β → γ) : (h ∘ f) =ᶠ[l] (h ∘ g) := H.mono $ λ x hx, congr_arg h hx lemma eventually_eq.comp₂ {δ} {f f' : α → β} {g g' : α → γ} {l} (Hf : f =ᶠ[l] f') (h : β → γ → δ) (Hg : g =ᶠ[l] g') : (λ x, h (f x) (g x)) =ᶠ[l] (λ x, h (f' x) (g' x)) := (Hf.prod_mk Hg).fun_comp (function.uncurry h) @[to_additive] lemma eventually_eq.mul [has_mul β] {f f' g g' : α → β} {l : filter α} (h : f =ᶠ[l] g) (h' : f' =ᶠ[l] g') : ((λ x, f x * f' x) =ᶠ[l] (λ x, g x * g' x)) := h.comp₂ (*) h' @[to_additive] lemma eventually_eq.inv [has_inv β] {f g : α → β} {l : filter α} (h : f =ᶠ[l] g) : ((λ x, (f x)⁻¹) =ᶠ[l] (λ x, (g x)⁻¹)) := h.fun_comp has_inv.inv lemma eventually_eq.div [group_with_zero β] {f f' g g' : α → β} {l : filter α} (h : f =ᶠ[l] g) (h' : f' =ᶠ[l] g') : ((λ x, f x / f' x) =ᶠ[l] (λ x, g x / g' x)) := h.mul h'.inv lemma eventually_eq.sub [add_group β] {f f' g g' : α → β} {l : filter α} (h : f =ᶠ[l] g) (h' : f' =ᶠ[l] g') : ((λ x, f x - f' x) =ᶠ[l] (λ x, g x - g' x)) := h.add h'.neg @[simp] lemma eventually_eq_principal {s : set α} {f g : α → β} : f =ᶠ[𝓟 s] g ↔ eq_on f g s := iff.rfl lemma eventually_eq_inf_principal_iff {F : filter α} {s : set α} {f g : α → β} : (f =ᶠ[F ⊓ 𝓟 s] g) ↔ ∀ᶠ x in F, x ∈ s → f x = g x := eventually_inf_principal section has_le variables [has_le β] {l : filter α} /-- A function `f` is eventually less than or equal to a function `g` at a filter `l`. -/ def eventually_le (l : filter α) (f g : α → β) : Prop := ∀ᶠ x in l, f x ≤ g x notation f ` ≤ᶠ[`:50 l:50 `] `:0 g:50 := eventually_le l f g lemma eventually_le.congr {f f' g g' : α → β} (H : f ≤ᶠ[l] g) (hf : f =ᶠ[l] f') (hg : g =ᶠ[l] g') : f' ≤ᶠ[l] g' := H.mp $ hg.mp $ hf.mono $ λ x hf hg H, by rwa [hf, hg] at H lemma eventually_le_congr {f f' g g' : α → β} (hf : f =ᶠ[l] f') (hg : g =ᶠ[l] g') : f ≤ᶠ[l] g ↔ f' ≤ᶠ[l] g' := ⟨λ H, H.congr hf hg, λ H, H.congr hf.symm hg.symm⟩ end has_le section preorder variables [preorder β] {l : filter α} {f g h : α → β} lemma eventually_eq.le (h : f =ᶠ[l] g) : f ≤ᶠ[l] g := h.mono $ λ x, le_of_eq @[refl] lemma eventually_le.refl (l : filter α) (f : α → β) : f ≤ᶠ[l] f := (eventually_eq.refl l f).le @[trans] lemma eventually_le.trans (H₁ : f ≤ᶠ[l] g) (H₂ : g ≤ᶠ[l] h) : f ≤ᶠ[l] h := H₂.mp $ H₁.mono $ λ x, le_trans @[trans] lemma eventually_eq.trans_le (H₁ : f =ᶠ[l] g) (H₂ : g ≤ᶠ[l] h) : f ≤ᶠ[l] h := H₁.le.trans H₂ @[trans] lemma eventually_le.trans_eq (H₁ : f ≤ᶠ[l] g) (H₂ : g =ᶠ[l] h) : f ≤ᶠ[l] h := H₁.trans H₂.le end preorder lemma eventually_le.antisymm [partial_order β] {l : filter α} {f g : α → β} (h₁ : f ≤ᶠ[l] g) (h₂ : g ≤ᶠ[l] f) : f =ᶠ[l] g := h₂.mp $ h₁.mono $ λ x, le_antisymm lemma join_le {f : filter (filter α)} {l : filter α} (h : ∀ᶠ m in f, m ≤ l) : join f ≤ l := λ s hs, h.mono $ λ m hm, hm hs /-! ### Push-forwards, pull-backs, and the monad structure -/ section map /-- The forward map of a filter -/ def map (m : α → β) (f : filter α) : filter β := { sets := preimage m ⁻¹' f.sets, univ_sets := univ_mem_sets, sets_of_superset := assume s t hs st, mem_sets_of_superset hs $ preimage_mono st, inter_sets := assume s t hs ht, inter_mem_sets hs ht } @[simp] lemma map_principal {s : set α} {f : α → β} : map f (𝓟 s) = 𝓟 (set.image f s) := filter_eq $ set.ext $ assume a, image_subset_iff.symm variables {f : filter α} {m : α → β} {m' : β → γ} {s : set α} {t : set β} @[simp] lemma eventually_map {P : β → Prop} : (∀ᶠ b in map m f, P b) ↔ ∀ᶠ a in f, P (m a) := iff.rfl @[simp] lemma frequently_map {P : β → Prop} : (∃ᶠ b in map m f, P b) ↔ ∃ᶠ a in f, P (m a) := iff.rfl @[simp] lemma mem_map : t ∈ map m f ↔ {x | m x ∈ t} ∈ f := iff.rfl lemma image_mem_map (hs : s ∈ f) : m '' s ∈ map m f := f.sets_of_superset hs $ subset_preimage_image m s lemma range_mem_map : range m ∈ map m f := by rw ←image_univ; exact image_mem_map univ_mem_sets lemma mem_map_sets_iff : t ∈ map m f ↔ (∃s∈f, m '' s ⊆ t) := iff.intro (assume ht, ⟨set.preimage m t, ht, image_preimage_subset _ _⟩) (assume ⟨s, hs, ht⟩, mem_sets_of_superset (image_mem_map hs) ht) @[simp] lemma map_id : filter.map id f = f := filter_eq $ rfl @[simp] lemma map_compose : filter.map m' ∘ filter.map m = filter.map (m' ∘ m) := funext $ assume _, filter_eq $ rfl @[simp] lemma map_map : filter.map m' (filter.map m f) = filter.map (m' ∘ m) f := congr_fun (@@filter.map_compose m m') f /-- If functions `m₁` and `m₂` are eventually equal at a filter `f`, then they map this filter to the same filter. -/ lemma map_congr {m₁ m₂ : α → β} {f : filter α} (h : m₁ =ᶠ[f] m₂) : map m₁ f = map m₂ f := filter.ext' $ λ p, by { simp only [eventually_map], exact eventually_congr (h.mono $ λ x hx, hx ▸ iff.rfl) } end map section comap /-- The inverse map of a filter -/ def comap (m : α → β) (f : filter β) : filter α := { sets := { s | ∃t∈ f, m ⁻¹' t ⊆ s }, univ_sets := ⟨univ, univ_mem_sets, by simp only [subset_univ, preimage_univ]⟩, sets_of_superset := assume a b ⟨a', ha', ma'a⟩ ab, ⟨a', ha', subset.trans ma'a ab⟩, inter_sets := assume a b ⟨a', ha₁, ha₂⟩ ⟨b', hb₁, hb₂⟩, ⟨a' ∩ b', inter_mem_sets ha₁ hb₁, inter_subset_inter ha₂ hb₂⟩ } @[simp] lemma eventually_comap {f : filter β} {φ : α → β} {P : α → Prop} : (∀ᶠ a in comap φ f, P a) ↔ ∀ᶠ b in f, ∀ a, φ a = b → P a := begin split ; intro h, { rcases h with ⟨t, t_in, ht⟩, apply mem_sets_of_superset t_in, rintros y y_in _ rfl, apply ht y_in }, { exact ⟨_, h, λ _ x_in, x_in _ rfl⟩ } end @[simp] lemma frequently_comap {f : filter β} {φ : α → β} {P : α → Prop} : (∃ᶠ a in comap φ f, P a) ↔ ∃ᶠ b in f, ∃ a, φ a = b ∧ P a := begin classical, erw [← not_iff_not, not_not, not_not, filter.eventually_comap], simp only [not_exists, not_and], end end comap /-- The monadic bind operation on filter is defined the usual way in terms of `map` and `join`. Unfortunately, this `bind` does not result in the expected applicative. See `filter.seq` for the applicative instance. -/ def bind (f : filter α) (m : α → filter β) : filter β := join (map m f) /-- The applicative sequentiation operation. This is not induced by the bind operation. -/ def seq (f : filter (α → β)) (g : filter α) : filter β := ⟨{ s | ∃u∈ f, ∃t∈ g, (∀m∈u, ∀x∈t, (m : α → β) x ∈ s) }, ⟨univ, univ_mem_sets, univ, univ_mem_sets, by simp only [forall_prop_of_true, mem_univ, forall_true_iff]⟩, assume s₀ s₁ ⟨t₀, t₁, h₀, h₁, h⟩ hst, ⟨t₀, t₁, h₀, h₁, assume x hx y hy, hst $ h _ hx _ hy⟩, assume s₀ s₁ ⟨t₀, ht₀, t₁, ht₁, ht⟩ ⟨u₀, hu₀, u₁, hu₁, hu⟩, ⟨t₀ ∩ u₀, inter_mem_sets ht₀ hu₀, t₁ ∩ u₁, inter_mem_sets ht₁ hu₁, assume x ⟨hx₀, hx₁⟩ x ⟨hy₀, hy₁⟩, ⟨ht _ hx₀ _ hy₀, hu _ hx₁ _ hy₁⟩⟩⟩ /-- `pure x` is the set of sets that contain `x`. It is equal to `𝓟 {x}` but with this definition we have `s ∈ pure a` defeq `a ∈ s`. -/ instance : has_pure filter := ⟨λ (α : Type u) x, { sets := {s | x ∈ s}, inter_sets := λ s t, and.intro, sets_of_superset := λ s t hs hst, hst hs, univ_sets := trivial }⟩ instance : has_bind filter := ⟨@filter.bind⟩ instance : has_seq filter := ⟨@filter.seq⟩ instance : functor filter := { map := @filter.map } lemma pure_sets (a : α) : (pure a : filter α).sets = {s | a ∈ s} := rfl @[simp] lemma mem_pure_sets {a : α} {s : set α} : s ∈ (pure a : filter α) ↔ a ∈ s := iff.rfl @[simp] lemma eventually_pure {a : α} {p : α → Prop} : (∀ᶠ x in pure a, p x) ↔ p a := iff.rfl @[simp] lemma principal_singleton (a : α) : 𝓟 {a} = pure a := filter.ext $ λ s, by simp only [mem_pure_sets, mem_principal_sets, singleton_subset_iff] @[simp] lemma map_pure (f : α → β) (a : α) : map f (pure a) = pure (f a) := rfl @[simp] lemma join_pure (f : filter α) : join (pure f) = f := filter.ext $ λ s, iff.rfl @[simp] lemma pure_bind (a : α) (m : α → filter β) : bind (pure a) m = m a := by simp only [has_bind.bind, bind, map_pure, join_pure] section -- this section needs to be before applicative, otherwise the wrong instance will be chosen /-- The monad structure on filters. -/ protected def monad : monad filter := { map := @filter.map } local attribute [instance] filter.monad protected lemma is_lawful_monad : is_lawful_monad filter := { id_map := assume α f, filter_eq rfl, pure_bind := assume α β, pure_bind, bind_assoc := assume α β γ f m₁ m₂, filter_eq rfl, bind_pure_comp_eq_map := assume α β f x, filter.ext $ λ s, by simp only [has_bind.bind, bind, functor.map, mem_map, mem_join_sets, mem_set_of_eq, function.comp, mem_pure_sets] } end instance : applicative filter := { map := @filter.map, seq := @filter.seq } instance : alternative filter := { failure := λα, ⊥, orelse := λα x y, x ⊔ y } @[simp] lemma map_def {α β} (m : α → β) (f : filter α) : m <$> f = map m f := rfl @[simp] lemma bind_def {α β} (f : filter α) (m : α → filter β) : f >>= m = bind f m := rfl /- map and comap equations -/ section map variables {f f₁ f₂ : filter α} {g g₁ g₂ : filter β} {m : α → β} {m' : β → γ} {s : set α} {t : set β} @[simp] theorem mem_comap_sets : s ∈ comap m g ↔ ∃t∈ g, m ⁻¹' t ⊆ s := iff.rfl theorem preimage_mem_comap (ht : t ∈ g) : m ⁻¹' t ∈ comap m g := ⟨t, ht, subset.refl _⟩ lemma comap_id : comap id f = f := le_antisymm (assume s, preimage_mem_comap) (assume s ⟨t, ht, hst⟩, mem_sets_of_superset ht hst) lemma comap_const_of_not_mem {x : α} {f : filter α} {V : set α} (hV : V ∈ f) (hx : x ∉ V) : comap (λ y : α, x) f = ⊥ := begin ext W, suffices : ∃ t ∈ f, (λ (y : α), x) ⁻¹' t ⊆ W, by simpa, use [V, hV], simp [preimage_const_of_not_mem hx], end lemma comap_const_of_mem {x : α} {f : filter α} (h : ∀ V ∈ f, x ∈ V) : comap (λ y : α, x) f = ⊤ := begin ext W, suffices : (∃ (t : set α), t ∈ f.sets ∧ (λ (y : α), x) ⁻¹' t ⊆ W) ↔ W = univ, by simpa, split, { rintros ⟨V, V_in, hW⟩, simpa [preimage_const_of_mem (h V V_in), univ_subset_iff] using hW }, { rintro rfl, use univ, simp [univ_mem_sets] }, end lemma comap_comap {m : γ → β} {n : β → α} : comap m (comap n f) = comap (n ∘ m) f := le_antisymm (assume c ⟨b, hb, (h : preimage (n ∘ m) b ⊆ c)⟩, ⟨preimage n b, preimage_mem_comap hb, h⟩) (assume c ⟨b, ⟨a, ha, (h₁ : preimage n a ⊆ b)⟩, (h₂ : preimage m b ⊆ c)⟩, ⟨a, ha, show preimage m (preimage n a) ⊆ c, from subset.trans (preimage_mono h₁) h₂⟩) @[simp] theorem comap_principal {t : set β} : comap m (𝓟 t) = 𝓟 (m ⁻¹' t) := filter_eq $ set.ext $ assume s, ⟨assume ⟨u, (hu : t ⊆ u), (b : preimage m u ⊆ s)⟩, subset.trans (preimage_mono hu) b, assume : preimage m t ⊆ s, ⟨t, subset.refl t, this⟩⟩ @[simp] theorem comap_pure {b : β} : comap m (pure b) = 𝓟 (m ⁻¹' {b}) := by rw [← principal_singleton, comap_principal] lemma map_le_iff_le_comap : map m f ≤ g ↔ f ≤ comap m g := ⟨assume h s ⟨t, ht, hts⟩, mem_sets_of_superset (h ht) hts, assume h s ht, h ⟨_, ht, subset.refl _⟩⟩ lemma gc_map_comap (m : α → β) : galois_connection (map m) (comap m) := assume f g, map_le_iff_le_comap @[mono] lemma map_mono : monotone (map m) := (gc_map_comap m).monotone_l @[mono] lemma comap_mono : monotone (comap m) := (gc_map_comap m).monotone_u @[simp] lemma map_bot : map m ⊥ = ⊥ := (gc_map_comap m).l_bot @[simp] lemma map_sup : map m (f₁ ⊔ f₂) = map m f₁ ⊔ map m f₂ := (gc_map_comap m).l_sup @[simp] lemma map_supr {f : ι → filter α} : map m (⨆i, f i) = (⨆i, map m (f i)) := (gc_map_comap m).l_supr @[simp] lemma comap_top : comap m ⊤ = ⊤ := (gc_map_comap m).u_top @[simp] lemma comap_inf : comap m (g₁ ⊓ g₂) = comap m g₁ ⊓ comap m g₂ := (gc_map_comap m).u_inf @[simp] lemma comap_infi {f : ι → filter β} : comap m (⨅i, f i) = (⨅i, comap m (f i)) := (gc_map_comap m).u_infi lemma le_comap_top (f : α → β) (l : filter α) : l ≤ comap f ⊤ := by rw [comap_top]; exact le_top lemma map_comap_le : map m (comap m g) ≤ g := (gc_map_comap m).l_u_le _ lemma le_comap_map : f ≤ comap m (map m f) := (gc_map_comap m).le_u_l _ @[simp] lemma comap_bot : comap m ⊥ = ⊥ := bot_unique $ assume s _, ⟨∅, by simp only [mem_bot_sets], by simp only [empty_subset, preimage_empty]⟩ lemma comap_supr {ι} {f : ι → filter β} {m : α → β} : comap m (supr f) = (⨆i, comap m (f i)) := le_antisymm (assume s hs, have ∀i, ∃t, t ∈ f i ∧ m ⁻¹' t ⊆ s, by simpa only [mem_comap_sets, exists_prop, mem_supr_sets] using mem_supr_sets.1 hs, let ⟨t, ht⟩ := classical.axiom_of_choice this in ⟨⋃i, t i, mem_supr_sets.2 $ assume i, (f i).sets_of_superset (ht i).1 (subset_Union _ _), begin rw [preimage_Union, Union_subset_iff], assume i, exact (ht i).2 end⟩) (supr_le $ assume i, comap_mono $ le_supr _ _) lemma comap_Sup {s : set (filter β)} {m : α → β} : comap m (Sup s) = (⨆f∈s, comap m f) := by simp only [Sup_eq_supr, comap_supr, eq_self_iff_true] lemma comap_sup : comap m (g₁ ⊔ g₂) = comap m g₁ ⊔ comap m g₂ := le_antisymm (assume s ⟨⟨t₁, ht₁, hs₁⟩, ⟨t₂, ht₂, hs₂⟩⟩, ⟨t₁ ∪ t₂, ⟨g₁.sets_of_superset ht₁ (subset_union_left _ _), g₂.sets_of_superset ht₂ (subset_union_right _ _)⟩, union_subset hs₁ hs₂⟩) ((@comap_mono _ _ m).le_map_sup _ _) lemma map_comap {f : filter β} {m : α → β} (hf : range m ∈ f) : (f.comap m).map m = f := le_antisymm map_comap_le (assume t' ⟨t, ht, sub⟩, by filter_upwards [ht, hf]; rintros x hxt ⟨y, rfl⟩; exact sub hxt) lemma image_mem_sets {f : filter α} {c : β → α} (h : range c ∈ f) {W : set β} (W_in : W ∈ comap c f) : c '' W ∈ f := begin rw ← map_comap h, exact image_mem_map W_in end lemma image_coe_mem_sets {f : filter α} {U : set α} (h : U ∈ f) {W : set U} (W_in : W ∈ comap (coe : U → α) f) : coe '' W ∈ f := image_mem_sets (by simp [h]) W_in lemma comap_map {f : filter α} {m : α → β} (h : ∀ x y, m x = m y → x = y) : comap m (map m f) = f := have ∀s, preimage m (image m s) = s, from assume s, preimage_image_eq s h, le_antisymm (assume s hs, ⟨ image m s, f.sets_of_superset hs $ by simp only [this, subset.refl], by simp only [this, subset.refl]⟩) le_comap_map lemma le_of_map_le_map_inj' {f g : filter α} {m : α → β} {s : set α} (hsf : s ∈ f) (hsg : s ∈ g) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y) (h : map m f ≤ map m g) : f ≤ g := assume t ht, by filter_upwards [hsf, h $ image_mem_map (inter_mem_sets hsg ht)] assume a has ⟨b, ⟨hbs, hb⟩, h⟩, have b = a, from hm _ hbs _ has h, this ▸ hb lemma le_of_map_le_map_inj_iff {f g : filter α} {m : α → β} {s : set α} (hsf : s ∈ f) (hsg : s ∈ g) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y) : map m f ≤ map m g ↔ f ≤ g := iff.intro (le_of_map_le_map_inj' hsf hsg hm) (λ h, map_mono h) lemma eq_of_map_eq_map_inj' {f g : filter α} {m : α → β} {s : set α} (hsf : s ∈ f) (hsg : s ∈ g) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y) (h : map m f = map m g) : f = g := le_antisymm (le_of_map_le_map_inj' hsf hsg hm $ le_of_eq h) (le_of_map_le_map_inj' hsg hsf hm $ le_of_eq h.symm) lemma map_inj {f g : filter α} {m : α → β} (hm : ∀ x y, m x = m y → x = y) (h : map m f = map m g) : f = g := have comap m (map m f) = comap m (map m g), by rw h, by rwa [comap_map hm, comap_map hm] at this theorem le_map_comap_of_surjective' {f : α → β} {l : filter β} {u : set β} (ul : u ∈ l) (hf : ∀ y ∈ u, ∃ x, f x = y) : l ≤ map f (comap f l) := assume s ⟨t, tl, ht⟩, have t ∩ u ⊆ s, from assume x ⟨xt, xu⟩, exists.elim (hf x xu) $ λ a faeq, by { rw ←faeq, apply ht, change f a ∈ t, rw faeq, exact xt }, mem_sets_of_superset (inter_mem_sets tl ul) this theorem map_comap_of_surjective' {f : α → β} {l : filter β} {u : set β} (ul : u ∈ l) (hf : ∀ y ∈ u, ∃ x, f x = y) : map f (comap f l) = l := le_antisymm map_comap_le (le_map_comap_of_surjective' ul hf) theorem le_map_comap_of_surjective {f : α → β} (hf : function.surjective f) (l : filter β) : l ≤ map f (comap f l) := le_map_comap_of_surjective' univ_mem_sets (λ y _, hf y) theorem map_comap_of_surjective {f : α → β} (hf : function.surjective f) (l : filter β) : map f (comap f l) = l := le_antisymm map_comap_le (le_map_comap_of_surjective hf l) lemma subtype_coe_map_comap (s : set α) (f : filter α) : map (coe : s → α) (comap (coe : s → α) f) = f ⊓ 𝓟 s := begin apply le_antisymm, { rw [map_le_iff_le_comap, comap_inf, comap_principal], have : (coe : s → α) ⁻¹' s = univ, by { ext x, simp }, rw [this, principal_univ], simp [le_refl _] }, { intros V V_in, rcases V_in with ⟨W, W_in, H⟩, rw mem_inf_sets, use [W, W_in, s, mem_principal_self s], erw [← image_subset_iff, subtype.image_preimage_coe] at H, exact H } end lemma subtype_coe_map_comap_prod (s : set α) (f : filter (α × α)) : map (coe : s × s → α × α) (comap (coe : s × s → α × α) f) = f ⊓ 𝓟 (s.prod s) := let φ (x : s × s) : s.prod s := ⟨⟨x.1.1, x.2.1⟩, ⟨x.1.2, x.2.2⟩⟩ in begin rw show (coe : s × s → α × α) = coe ∘ φ, by ext x; cases x; refl, rw [← filter.map_map, ← filter.comap_comap], rw map_comap_of_surjective, exact subtype_coe_map_comap _ _, exact λ ⟨⟨a, b⟩, ⟨ha, hb⟩⟩, ⟨⟨⟨a, ha⟩, ⟨b, hb⟩⟩, rfl⟩ end lemma comap_ne_bot_iff {f : filter β} {m : α → β} : ne_bot (comap m f) ↔ ∀ t ∈ f, ∃ a, m a ∈ t := begin rw ← forall_sets_nonempty_iff_ne_bot, exact ⟨λ h t t_in, h (m ⁻¹' t) ⟨t, t_in, subset.refl _⟩, λ h s ⟨u, u_in, hu⟩, let ⟨x, hx⟩ := h u u_in in ⟨x, hu hx⟩⟩, end lemma comap_ne_bot {f : filter β} {m : α → β} (hm : ∀t∈ f, ∃a, m a ∈ t) : ne_bot (comap m f) := comap_ne_bot_iff.mpr hm lemma ne_bot.comap_of_range_mem {f : filter β} {m : α → β} (hf : ne_bot f) (hm : range m ∈ f) : ne_bot (comap m f) := comap_ne_bot $ assume t ht, let ⟨_, ha, a, rfl⟩ := hf.nonempty_of_mem (inter_mem_sets ht hm) in ⟨a, ha⟩ lemma comap_inf_principal_ne_bot_of_image_mem {f : filter β} {m : α → β} (hf : ne_bot f) {s : set α} (hs : m '' s ∈ f) : ne_bot (comap m f ⊓ 𝓟 s) := begin refine compl_compl s ▸ mt mem_sets_of_eq_bot _, rintros ⟨t, ht, hts⟩, rcases hf.nonempty_of_mem (inter_mem_sets hs ht) with ⟨_, ⟨x, hxs, rfl⟩, hxt⟩, exact absurd hxs (hts hxt) end lemma ne_bot.comap_of_surj {f : filter β} {m : α → β} (hf : ne_bot f) (hm : function.surjective m) : ne_bot (comap m f) := hf.comap_of_range_mem $ univ_mem_sets' hm lemma ne_bot.comap_of_image_mem {f : filter β} {m : α → β} (hf : ne_bot f) {s : set α} (hs : m '' s ∈ f) : ne_bot (comap m f) := hf.comap_of_range_mem $ mem_sets_of_superset hs (image_subset_range _ _) @[simp] lemma map_eq_bot_iff : map m f = ⊥ ↔ f = ⊥ := ⟨by rw [←empty_in_sets_eq_bot, ←empty_in_sets_eq_bot]; exact id, assume h, by simp only [h, eq_self_iff_true, map_bot]⟩ lemma map_ne_bot_iff (f : α → β) {F : filter α} : ne_bot (map f F) ↔ ne_bot F := not_congr map_eq_bot_iff lemma ne_bot.map (hf : ne_bot f) (m : α → β) : ne_bot (map m f) := (map_ne_bot_iff m).2 hf instance map_ne_bot [hf : ne_bot f] : ne_bot (f.map m) := hf.map m lemma sInter_comap_sets (f : α → β) (F : filter β) : ⋂₀(comap f F).sets = ⋂ U ∈ F, f ⁻¹' U := begin ext x, suffices : (∀ (A : set α) (B : set β), B ∈ F → f ⁻¹' B ⊆ A → x ∈ A) ↔ ∀ (B : set β), B ∈ F → f x ∈ B, by simp only [mem_sInter, mem_Inter, mem_comap_sets, this, and_imp, mem_comap_sets, exists_prop, mem_sInter, iff_self, mem_Inter, mem_preimage, exists_imp_distrib], split, { intros h U U_in, simpa only [set.subset.refl, forall_prop_of_true, mem_preimage] using h (f ⁻¹' U) U U_in }, { intros h V U U_in f_U_V, exact f_U_V (h U U_in) }, end end map -- this is a generic rule for monotone functions: lemma map_infi_le {f : ι → filter α} {m : α → β} : map m (infi f) ≤ (⨅ i, map m (f i)) := le_infi $ assume i, map_mono $ infi_le _ _ lemma map_infi_eq {f : ι → filter α} {m : α → β} (hf : directed (≥) f) [nonempty ι] : map m (infi f) = (⨅ i, map m (f i)) := le_antisymm map_infi_le (assume s (hs : preimage m s ∈ infi f), have ∃i, preimage m s ∈ f i, by simp only [infi_sets_eq hf, mem_Union] at hs; assumption, let ⟨i, hi⟩ := this in have (⨅ i, map m (f i)) ≤ 𝓟 s, from infi_le_of_le i $ by simp only [le_principal_iff, mem_map]; assumption, by simp only [filter.le_principal_iff] at this; assumption) lemma map_binfi_eq {ι : Type w} {f : ι → filter α} {m : α → β} {p : ι → Prop} (h : directed_on (f ⁻¹'o (≥)) {x | p x}) (ne : ∃i, p i) : map m (⨅i (h : p i), f i) = (⨅i (h: p i), map m (f i)) := begin haveI := nonempty_subtype.2 ne, simp only [infi_subtype'], exact map_infi_eq h.directed_coe end lemma map_inf_le {f g : filter α} {m : α → β} : map m (f ⊓ g) ≤ map m f ⊓ map m g := (@map_mono _ _ m).map_inf_le f g lemma map_inf' {f g : filter α} {m : α → β} {t : set α} (htf : t ∈ f) (htg : t ∈ g) (h : ∀x∈t, ∀y∈t, m x = m y → x = y) : map m (f ⊓ g) = map m f ⊓ map m g := begin refine le_antisymm map_inf_le (assume s hs, _), simp only [map, mem_inf_sets, exists_prop, mem_map, mem_preimage, mem_inf_sets] at hs ⊢, rcases hs with ⟨t₁, h₁, t₂, h₂, hs⟩, refine ⟨m '' (t₁ ∩ t), _, m '' (t₂ ∩ t), _, _⟩, { filter_upwards [h₁, htf] assume a h₁ h₂, mem_image_of_mem _ ⟨h₁, h₂⟩ }, { filter_upwards [h₂, htg] assume a h₁ h₂, mem_image_of_mem _ ⟨h₁, h₂⟩ }, { rw [image_inter_on], { refine image_subset_iff.2 _, exact λ x ⟨⟨h₁, _⟩, h₂, _⟩, hs ⟨h₁, h₂⟩ }, { exact λ x ⟨_, hx⟩ y ⟨_, hy⟩, h x hx y hy } } end lemma map_inf {f g : filter α} {m : α → β} (h : function.injective m) : map m (f ⊓ g) = map m f ⊓ map m g := map_inf' univ_mem_sets univ_mem_sets (assume x _ y _ hxy, h hxy) lemma map_eq_comap_of_inverse {f : filter α} {m : α → β} {n : β → α} (h₁ : m ∘ n = id) (h₂ : n ∘ m = id) : map m f = comap n f := le_antisymm (assume b ⟨a, ha, (h : preimage n a ⊆ b)⟩, f.sets_of_superset ha $ calc a = preimage (n ∘ m) a : by simp only [h₂, preimage_id, eq_self_iff_true] ... ⊆ preimage m b : preimage_mono h) (assume b (hb : preimage m b ∈ f), ⟨preimage m b, hb, show preimage (m ∘ n) b ⊆ b, by simp only [h₁]; apply subset.refl⟩) lemma map_swap_eq_comap_swap {f : filter (α × β)} : prod.swap <$> f = comap prod.swap f := map_eq_comap_of_inverse prod.swap_swap_eq prod.swap_swap_eq lemma le_map {f : filter α} {m : α → β} {g : filter β} (h : ∀s∈ f, m '' s ∈ g) : g ≤ f.map m := assume s hs, mem_sets_of_superset (h _ hs) $ image_preimage_subset _ _ protected lemma push_pull (f : α → β) (F : filter α) (G : filter β) : map f (F ⊓ comap f G) = map f F ⊓ G := begin apply le_antisymm, { calc map f (F ⊓ comap f G) ≤ map f F ⊓ (map f $ comap f G) : map_inf_le ... ≤ map f F ⊓ G : inf_le_inf_left (map f F) map_comap_le }, { rintros U ⟨V, V_in, W, ⟨Z, Z_in, hZ⟩, h⟩, rw ← image_subset_iff at h, use [f '' V, image_mem_map V_in, Z, Z_in], refine subset.trans _ h, have : f '' (V ∩ f ⁻¹' Z) ⊆ f '' (V ∩ W), from image_subset _ (inter_subset_inter_right _ ‹_›), rwa image_inter_preimage at this } end protected lemma push_pull' (f : α → β) (F : filter α) (G : filter β) : map f (comap f G ⊓ F) = G ⊓ map f F := by simp only [filter.push_pull, inf_comm] section applicative lemma singleton_mem_pure_sets {a : α} : {a} ∈ (pure a : filter α) := mem_singleton a lemma pure_injective : function.injective (pure : α → filter α) := assume a b hab, (filter.ext_iff.1 hab {x | a = x}).1 rfl instance pure_ne_bot {α : Type u} {a : α} : ne_bot (pure a) := mt empty_in_sets_eq_bot.2 $ not_mem_empty a @[simp] lemma le_pure_iff {f : filter α} {a : α} : f ≤ pure a ↔ {a} ∈ f := ⟨λ h, h singleton_mem_pure_sets, λ h s hs, mem_sets_of_superset h $ singleton_subset_iff.2 hs⟩ lemma mem_seq_sets_def {f : filter (α → β)} {g : filter α} {s : set β} : s ∈ f.seq g ↔ (∃u ∈ f, ∃t ∈ g, ∀x∈u, ∀y∈t, (x : α → β) y ∈ s) := iff.rfl lemma mem_seq_sets_iff {f : filter (α → β)} {g : filter α} {s : set β} : s ∈ f.seq g ↔ (∃u ∈ f, ∃t ∈ g, set.seq u t ⊆ s) := by simp only [mem_seq_sets_def, seq_subset, exists_prop, iff_self] lemma mem_map_seq_iff {f : filter α} {g : filter β} {m : α → β → γ} {s : set γ} : s ∈ (f.map m).seq g ↔ (∃t u, t ∈ g ∧ u ∈ f ∧ ∀x∈u, ∀y∈t, m x y ∈ s) := iff.intro (assume ⟨t, ht, s, hs, hts⟩, ⟨s, m ⁻¹' t, hs, ht, assume a, hts _⟩) (assume ⟨t, s, ht, hs, hts⟩, ⟨m '' s, image_mem_map hs, t, ht, assume f ⟨a, has, eq⟩, eq ▸ hts _ has⟩) lemma seq_mem_seq_sets {f : filter (α → β)} {g : filter α} {s : set (α → β)} {t : set α} (hs : s ∈ f) (ht : t ∈ g) : s.seq t ∈ f.seq g := ⟨s, hs, t, ht, assume f hf a ha, ⟨f, hf, a, ha, rfl⟩⟩ lemma le_seq {f : filter (α → β)} {g : filter α} {h : filter β} (hh : ∀t ∈ f, ∀u ∈ g, set.seq t u ∈ h) : h ≤ seq f g := assume s ⟨t, ht, u, hu, hs⟩, mem_sets_of_superset (hh _ ht _ hu) $ assume b ⟨m, hm, a, ha, eq⟩, eq ▸ hs _ hm _ ha @[mono] lemma seq_mono {f₁ f₂ : filter (α → β)} {g₁ g₂ : filter α} (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁.seq g₁ ≤ f₂.seq g₂ := le_seq $ assume s hs t ht, seq_mem_seq_sets (hf hs) (hg ht) @[simp] lemma pure_seq_eq_map (g : α → β) (f : filter α) : seq (pure g) f = f.map g := begin refine le_antisymm (le_map $ assume s hs, _) (le_seq $ assume s hs t ht, _), { rw ← singleton_seq, apply seq_mem_seq_sets _ hs, exact singleton_mem_pure_sets }, { refine sets_of_superset (map g f) (image_mem_map ht) _, rintros b ⟨a, ha, rfl⟩, exact ⟨g, hs, a, ha, rfl⟩ } end @[simp] lemma seq_pure (f : filter (α → β)) (a : α) : seq f (pure a) = map (λg:α → β, g a) f := begin refine le_antisymm (le_map $ assume s hs, _) (le_seq $ assume s hs t ht, _), { rw ← seq_singleton, exact seq_mem_seq_sets hs singleton_mem_pure_sets }, { refine sets_of_superset (map (λg:α→β, g a) f) (image_mem_map hs) _, rintros b ⟨g, hg, rfl⟩, exact ⟨g, hg, a, ht, rfl⟩ } end @[simp] lemma seq_assoc (x : filter α) (g : filter (α → β)) (h : filter (β → γ)) : seq h (seq g x) = seq (seq (map (∘) h) g) x := begin refine le_antisymm (le_seq $ assume s hs t ht, _) (le_seq $ assume s hs t ht, _), { rcases mem_seq_sets_iff.1 hs with ⟨u, hu, v, hv, hs⟩, rcases mem_map_sets_iff.1 hu with ⟨w, hw, hu⟩, refine mem_sets_of_superset _ (set.seq_mono (subset.trans (set.seq_mono hu (subset.refl _)) hs) (subset.refl _)), rw ← set.seq_seq, exact seq_mem_seq_sets hw (seq_mem_seq_sets hv ht) }, { rcases mem_seq_sets_iff.1 ht with ⟨u, hu, v, hv, ht⟩, refine mem_sets_of_superset _ (set.seq_mono (subset.refl _) ht), rw set.seq_seq, exact seq_mem_seq_sets (seq_mem_seq_sets (image_mem_map hs) hu) hv } end lemma prod_map_seq_comm (f : filter α) (g : filter β) : (map prod.mk f).seq g = seq (map (λb a, (a, b)) g) f := begin refine le_antisymm (le_seq $ assume s hs t ht, _) (le_seq $ assume s hs t ht, _), { rcases mem_map_sets_iff.1 hs with ⟨u, hu, hs⟩, refine mem_sets_of_superset _ (set.seq_mono hs (subset.refl _)), rw ← set.prod_image_seq_comm, exact seq_mem_seq_sets (image_mem_map ht) hu }, { rcases mem_map_sets_iff.1 hs with ⟨u, hu, hs⟩, refine mem_sets_of_superset _ (set.seq_mono hs (subset.refl _)), rw set.prod_image_seq_comm, exact seq_mem_seq_sets (image_mem_map ht) hu } end instance : is_lawful_functor (filter : Type u → Type u) := { id_map := assume α f, map_id, comp_map := assume α β γ f g a, map_map.symm } instance : is_lawful_applicative (filter : Type u → Type u) := { pure_seq_eq_map := assume α β, pure_seq_eq_map, map_pure := assume α β, map_pure, seq_pure := assume α β, seq_pure, seq_assoc := assume α β γ, seq_assoc } instance : is_comm_applicative (filter : Type u → Type u) := ⟨assume α β f g, prod_map_seq_comm f g⟩ lemma {l} seq_eq_filter_seq {α β : Type l} (f : filter (α → β)) (g : filter α) : f <*> g = seq f g := rfl end applicative /- bind equations -/ section bind @[simp] lemma eventually_bind {f : filter α} {m : α → filter β} {p : β → Prop} : (∀ᶠ y in bind f m, p y) ↔ ∀ᶠ x in f, ∀ᶠ y in m x, p y := iff.rfl @[simp] lemma eventually_eq_bind {f : filter α} {m : α → filter β} {g₁ g₂ : β → γ} : (g₁ =ᶠ[bind f m] g₂) ↔ ∀ᶠ x in f, g₁ =ᶠ[m x] g₂ := iff.rfl @[simp] lemma eventually_le_bind [has_le γ] {f : filter α} {m : α → filter β} {g₁ g₂ : β → γ} : (g₁ ≤ᶠ[bind f m] g₂) ↔ ∀ᶠ x in f, g₁ ≤ᶠ[m x] g₂ := iff.rfl lemma mem_bind_sets' {s : set β} {f : filter α} {m : α → filter β} : s ∈ bind f m ↔ {a | s ∈ m a} ∈ f := iff.rfl @[simp] lemma mem_bind_sets {s : set β} {f : filter α} {m : α → filter β} : s ∈ bind f m ↔ ∃t ∈ f, ∀x ∈ t, s ∈ m x := calc s ∈ bind f m ↔ {a | s ∈ m a} ∈ f : iff.rfl ... ↔ (∃t ∈ f, t ⊆ {a | s ∈ m a}) : exists_sets_subset_iff.symm ... ↔ (∃t ∈ f, ∀x ∈ t, s ∈ m x) : iff.rfl lemma bind_le {f : filter α} {g : α → filter β} {l : filter β} (h : ∀ᶠ x in f, g x ≤ l) : f.bind g ≤ l := join_le $ eventually_map.2 h @[mono] lemma bind_mono {f₁ f₂ : filter α} {g₁ g₂ : α → filter β} (hf : f₁ ≤ f₂) (hg : g₁ ≤ᶠ[f₁] g₂) : bind f₁ g₁ ≤ bind f₂ g₂ := begin refine le_trans (λ s hs, _) (join_mono $ map_mono hf), simp only [mem_join_sets, mem_bind_sets', mem_map] at hs ⊢, filter_upwards [hg, hs], exact λ x hx hs, hx hs end lemma bind_inf_principal {f : filter α} {g : α → filter β} {s : set β} : f.bind (λ x, g x ⊓ 𝓟 s) = (f.bind g) ⊓ 𝓟 s := filter.ext $ λ s, by simp only [mem_bind_sets, mem_inf_principal] lemma sup_bind {f g : filter α} {h : α → filter β} : bind (f ⊔ g) h = bind f h ⊔ bind g h := by simp only [bind, sup_join, map_sup, eq_self_iff_true] lemma principal_bind {s : set α} {f : α → filter β} : (bind (𝓟 s) f) = (⨆x ∈ s, f x) := show join (map f (𝓟 s)) = (⨆x ∈ s, f x), by simp only [Sup_image, join_principal_eq_Sup, map_principal, eq_self_iff_true] end bind section list_traverse /- This is a separate section in order to open `list`, but mostly because of universe equality requirements in `traverse` -/ open list lemma sequence_mono : ∀(as bs : list (filter α)), forall₂ (≤) as bs → sequence as ≤ sequence bs | [] [] forall₂.nil := le_refl _ | (a::as) (b::bs) (forall₂.cons h hs) := seq_mono (map_mono h) (sequence_mono as bs hs) variables {α' β' γ' : Type u} {f : β' → filter α'} {s : γ' → set α'} lemma mem_traverse_sets : ∀(fs : list β') (us : list γ'), forall₂ (λb c, s c ∈ f b) fs us → traverse s us ∈ traverse f fs | [] [] forall₂.nil := mem_pure_sets.2 $ mem_singleton _ | (f::fs) (u::us) (forall₂.cons h hs) := seq_mem_seq_sets (image_mem_map h) (mem_traverse_sets fs us hs) lemma mem_traverse_sets_iff (fs : list β') (t : set (list α')) : t ∈ traverse f fs ↔ (∃us:list (set α'), forall₂ (λb (s : set α'), s ∈ f b) fs us ∧ sequence us ⊆ t) := begin split, { induction fs generalizing t, case nil { simp only [sequence, mem_pure_sets, imp_self, forall₂_nil_left_iff, exists_eq_left, set.pure_def, singleton_subset_iff, traverse_nil] }, case cons : b fs ih t { assume ht, rcases mem_seq_sets_iff.1 ht with ⟨u, hu, v, hv, ht⟩, rcases mem_map_sets_iff.1 hu with ⟨w, hw, hwu⟩, rcases ih v hv with ⟨us, hus, hu⟩, exact ⟨w :: us, forall₂.cons hw hus, subset.trans (set.seq_mono hwu hu) ht⟩ } }, { rintros ⟨us, hus, hs⟩, exact mem_sets_of_superset (mem_traverse_sets _ _ hus) hs } end end list_traverse /-! ### Limits -/ /-- `tendsto` is the generic "limit of a function" predicate. `tendsto f l₁ l₂` asserts that for every `l₂` neighborhood `a`, the `f`-preimage of `a` is an `l₁` neighborhood. -/ def tendsto (f : α → β) (l₁ : filter α) (l₂ : filter β) := l₁.map f ≤ l₂ lemma tendsto_def {f : α → β} {l₁ : filter α} {l₂ : filter β} : tendsto f l₁ l₂ ↔ ∀ s ∈ l₂, f ⁻¹' s ∈ l₁ := iff.rfl lemma tendsto_iff_eventually {f : α → β} {l₁ : filter α} {l₂ : filter β} : tendsto f l₁ l₂ ↔ ∀ ⦃p : β → Prop⦄, (∀ᶠ y in l₂, p y) → ∀ᶠ x in l₁, p (f x) := iff.rfl lemma tendsto.eventually {f : α → β} {l₁ : filter α} {l₂ : filter β} {p : β → Prop} (hf : tendsto f l₁ l₂) (h : ∀ᶠ y in l₂, p y) : ∀ᶠ x in l₁, p (f x) := hf h lemma tendsto.frequently {f : α → β} {l₁ : filter α} {l₂ : filter β} {p : β → Prop} (hf : tendsto f l₁ l₂) (h : ∃ᶠ x in l₁, p (f x)) : ∃ᶠ y in l₂, p y := mt hf.eventually h @[simp] lemma tendsto_bot {f : α → β} {l : filter β} : tendsto f ⊥ l := by simp [tendsto] lemma tendsto_of_not_nonempty {f : α → β} {la : filter α} {lb : filter β} (h : ¬nonempty α) : tendsto f la lb := by simp only [filter_eq_bot_of_not_nonempty la h, tendsto_bot] lemma eventually_eq_of_left_inv_of_right_inv {f : α → β} {g₁ g₂ : β → α} {fa : filter α} {fb : filter β} (hleft : ∀ᶠ x in fa, g₁ (f x) = x) (hright : ∀ᶠ y in fb, f (g₂ y) = y) (htendsto : tendsto g₂ fb fa) : g₁ =ᶠ[fb] g₂ := (htendsto.eventually hleft).mp $ hright.mono $ λ y hr hl, (congr_arg g₁ hr.symm).trans hl lemma tendsto_iff_comap {f : α → β} {l₁ : filter α} {l₂ : filter β} : tendsto f l₁ l₂ ↔ l₁ ≤ l₂.comap f := map_le_iff_le_comap alias tendsto_iff_comap ↔ filter.tendsto.le_comap _ lemma tendsto_congr' {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β} (hl : f₁ =ᶠ[l₁] f₂) : tendsto f₁ l₁ l₂ ↔ tendsto f₂ l₁ l₂ := by rw [tendsto, tendsto, map_congr hl] lemma tendsto.congr' {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β} (hl : f₁ =ᶠ[l₁] f₂) (h : tendsto f₁ l₁ l₂) : tendsto f₂ l₁ l₂ := (tendsto_congr' hl).1 h theorem tendsto_congr {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β} (h : ∀ x, f₁ x = f₂ x) : tendsto f₁ l₁ l₂ ↔ tendsto f₂ l₁ l₂ := tendsto_congr' (univ_mem_sets' h) theorem tendsto.congr {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β} (h : ∀ x, f₁ x = f₂ x) : tendsto f₁ l₁ l₂ → tendsto f₂ l₁ l₂ := (tendsto_congr h).1 lemma tendsto_id' {x y : filter α} : x ≤ y → tendsto id x y := by simp only [tendsto, map_id, forall_true_iff] {contextual := tt} lemma tendsto_id {x : filter α} : tendsto id x x := tendsto_id' $ le_refl x lemma tendsto.comp {f : α → β} {g : β → γ} {x : filter α} {y : filter β} {z : filter γ} (hg : tendsto g y z) (hf : tendsto f x y) : tendsto (g ∘ f) x z := calc map (g ∘ f) x = map g (map f x) : by rw [map_map] ... ≤ map g y : map_mono hf ... ≤ z : hg lemma tendsto.mono_left {f : α → β} {x y : filter α} {z : filter β} (hx : tendsto f x z) (h : y ≤ x) : tendsto f y z := le_trans (map_mono h) hx lemma tendsto.mono_right {f : α → β} {x : filter α} {y z : filter β} (hy : tendsto f x y) (hz : y ≤ z) : tendsto f x z := le_trans hy hz lemma tendsto.ne_bot {f : α → β} {x : filter α} {y : filter β} (h : tendsto f x y) [hx : ne_bot x] : ne_bot y := (hx.map _).mono h lemma tendsto_map {f : α → β} {x : filter α} : tendsto f x (map f x) := le_refl (map f x) lemma tendsto_map' {f : β → γ} {g : α → β} {x : filter α} {y : filter γ} (h : tendsto (f ∘ g) x y) : tendsto f (map g x) y := by rwa [tendsto, map_map] lemma tendsto_map'_iff {f : β → γ} {g : α → β} {x : filter α} {y : filter γ} : tendsto f (map g x) y ↔ tendsto (f ∘ g) x y := by rw [tendsto, map_map]; refl lemma tendsto_comap {f : α → β} {x : filter β} : tendsto f (comap f x) x := map_comap_le lemma tendsto_comap_iff {f : α → β} {g : β → γ} {a : filter α} {c : filter γ} : tendsto f a (c.comap g) ↔ tendsto (g ∘ f) a c := ⟨assume h, tendsto_comap.comp h, assume h, map_le_iff_le_comap.mp $ by rwa [map_map]⟩ lemma tendsto_comap'_iff {m : α → β} {f : filter α} {g : filter β} {i : γ → α} (h : range i ∈ f) : tendsto (m ∘ i) (comap i f) g ↔ tendsto m f g := by rw [tendsto, ← map_compose]; simp only [(∘), map_comap h, tendsto] lemma comap_eq_of_inverse {f : filter α} {g : filter β} {φ : α → β} (ψ : β → α) (eq : ψ ∘ φ = id) (hφ : tendsto φ f g) (hψ : tendsto ψ g f) : comap φ g = f := begin refine le_antisymm (le_trans (comap_mono $ map_le_iff_le_comap.1 hψ) _) (map_le_iff_le_comap.1 hφ), rw [comap_comap, eq, comap_id], exact le_refl _ end lemma map_eq_of_inverse {f : filter α} {g : filter β} {φ : α → β} (ψ : β → α) (eq : φ ∘ ψ = id) (hφ : tendsto φ f g) (hψ : tendsto ψ g f) : map φ f = g := begin refine le_antisymm hφ (le_trans _ (map_mono hψ)), rw [map_map, eq, map_id], exact le_refl _ end lemma tendsto_inf {f : α → β} {x : filter α} {y₁ y₂ : filter β} : tendsto f x (y₁ ⊓ y₂) ↔ tendsto f x y₁ ∧ tendsto f x y₂ := by simp only [tendsto, le_inf_iff, iff_self] lemma tendsto_inf_left {f : α → β} {x₁ x₂ : filter α} {y : filter β} (h : tendsto f x₁ y) : tendsto f (x₁ ⊓ x₂) y := le_trans (map_mono inf_le_left) h lemma tendsto_inf_right {f : α → β} {x₁ x₂ : filter α} {y : filter β} (h : tendsto f x₂ y) : tendsto f (x₁ ⊓ x₂) y := le_trans (map_mono inf_le_right) h lemma tendsto.inf {f : α → β} {x₁ x₂ : filter α} {y₁ y₂ : filter β} (h₁ : tendsto f x₁ y₁) (h₂ : tendsto f x₂ y₂) : tendsto f (x₁ ⊓ x₂) (y₁ ⊓ y₂) := tendsto_inf.2 ⟨tendsto_inf_left h₁, tendsto_inf_right h₂⟩ @[simp] lemma tendsto_infi {f : α → β} {x : filter α} {y : ι → filter β} : tendsto f x (⨅i, y i) ↔ ∀i, tendsto f x (y i) := by simp only [tendsto, iff_self, le_infi_iff] lemma tendsto_infi' {f : α → β} {x : ι → filter α} {y : filter β} (i : ι) (hi : tendsto f (x i) y) : tendsto f (⨅i, x i) y := hi.mono_left $ infi_le _ _ lemma tendsto_sup {f : α → β} {x₁ x₂ : filter α} {y : filter β} : tendsto f (x₁ ⊔ x₂) y ↔ tendsto f x₁ y ∧ tendsto f x₂ y := by simp only [tendsto, map_sup, sup_le_iff] lemma tendsto.sup {f : α → β} {x₁ x₂ : filter α} {y : filter β} : tendsto f x₁ y → tendsto f x₂ y → tendsto f (x₁ ⊔ x₂) y := λ h₁ h₂, tendsto_sup.mpr ⟨ h₁, h₂ ⟩ @[simp] lemma tendsto_principal {f : α → β} {l : filter α} {s : set β} : tendsto f l (𝓟 s) ↔ ∀ᶠ a in l, f a ∈ s := by simp only [tendsto, le_principal_iff, mem_map, filter.eventually] @[simp] lemma tendsto_principal_principal {f : α → β} {s : set α} {t : set β} : tendsto f (𝓟 s) (𝓟 t) ↔ ∀a∈s, f a ∈ t := by simp only [tendsto_principal, eventually_principal] @[simp] lemma tendsto_pure {f : α → β} {a : filter α} {b : β} : tendsto f a (pure b) ↔ ∀ᶠ x in a, f x = b := by simp only [tendsto, le_pure_iff, mem_map, mem_singleton_iff, filter.eventually] lemma tendsto_pure_pure (f : α → β) (a : α) : tendsto f (pure a) (pure (f a)) := tendsto_pure.2 rfl lemma tendsto_const_pure {a : filter α} {b : β} : tendsto (λx, b) a (pure b) := tendsto_pure.2 $ univ_mem_sets' $ λ _, rfl lemma pure_le_iff {a : α} {l : filter α} : pure a ≤ l ↔ ∀ s ∈ l, a ∈ s := iff.rfl lemma tendsto_pure_left {f : α → β} {a : α} {l : filter β} : tendsto f (pure a) l ↔ ∀ s ∈ l, f a ∈ s := iff.rfl /-- If two filters are disjoint, then a function cannot tend to both of them along a non-trivial filter. -/ lemma tendsto.not_tendsto {f : α → β} {a : filter α} {b₁ b₂ : filter β} (hf : tendsto f a b₁) [ne_bot a] (hb : disjoint b₁ b₂) : ¬ tendsto f a b₂ := λ hf', (tendsto_inf.2 ⟨hf, hf'⟩).ne_bot hb.eq_bot lemma tendsto_if {l₁ : filter α} {l₂ : filter β} {f g : α → β} {p : α → Prop} [decidable_pred p] (h₀ : tendsto f (l₁ ⊓ 𝓟 p) l₂) (h₁ : tendsto g (l₁ ⊓ 𝓟 { x | ¬ p x }) l₂) : tendsto (λ x, if p x then f x else g x) l₁ l₂ := begin revert h₀ h₁, simp only [tendsto_def, mem_inf_principal], intros h₀ h₁ s hs, apply mem_sets_of_superset (inter_mem_sets (h₀ s hs) (h₁ s hs)), rintros x ⟨hp₀, hp₁⟩, simp only [mem_preimage], by_cases h : p x, { rw if_pos h, exact hp₀ h }, rw if_neg h, exact hp₁ h end /-! ### Products of filters -/ section prod variables {s : set α} {t : set β} {f : filter α} {g : filter β} /- The product filter cannot be defined using the monad structure on filters. For example: F := do {x ← seq, y ← top, return (x, y)} hence: s ∈ F ↔ ∃n, [n..∞] × univ ⊆ s G := do {y ← top, x ← seq, return (x, y)} hence: s ∈ G ↔ ∀i:ℕ, ∃n, [n..∞] × {i} ⊆ s Now ⋃i, [i..∞] × {i} is in G but not in F. As product filter we want to have F as result. -/ /-- Product of filters. This is the filter generated by cartesian products of elements of the component filters. -/ protected def prod (f : filter α) (g : filter β) : filter (α × β) := f.comap prod.fst ⊓ g.comap prod.snd localized "infix ` ×ᶠ `:60 := filter.prod" in filter lemma prod_mem_prod {s : set α} {t : set β} {f : filter α} {g : filter β} (hs : s ∈ f) (ht : t ∈ g) : set.prod s t ∈ f ×ᶠ g := inter_mem_inf_sets (preimage_mem_comap hs) (preimage_mem_comap ht) lemma mem_prod_iff {s : set (α×β)} {f : filter α} {g : filter β} : s ∈ f ×ᶠ g ↔ (∃ t₁ ∈ f, ∃ t₂ ∈ g, set.prod t₁ t₂ ⊆ s) := begin simp only [filter.prod], split, exact assume ⟨t₁, ⟨s₁, hs₁, hts₁⟩, t₂, ⟨s₂, hs₂, hts₂⟩, h⟩, ⟨s₁, hs₁, s₂, hs₂, subset.trans (inter_subset_inter hts₁ hts₂) h⟩, exact assume ⟨t₁, ht₁, t₂, ht₂, h⟩, ⟨prod.fst ⁻¹' t₁, ⟨t₁, ht₁, subset.refl _⟩, prod.snd ⁻¹' t₂, ⟨t₂, ht₂, subset.refl _⟩, h⟩ end lemma comap_prod (f : α → β × γ) (b : filter β) (c : filter γ) : comap f (b ×ᶠ c) = (comap (prod.fst ∘ f) b) ⊓ (comap (prod.snd ∘ f) c) := by erw [comap_inf, filter.comap_comap, filter.comap_comap] lemma eventually_prod_iff {p : α × β → Prop} {f : filter α} {g : filter β} : (∀ᶠ x in f ×ᶠ g, p x) ↔ ∃ (pa : α → Prop) (ha : ∀ᶠ x in f, pa x) (pb : β → Prop) (hb : ∀ᶠ y in g, pb y), ∀ {x}, pa x → ∀ {y}, pb y → p (x, y) := by simpa only [set.prod_subset_iff] using @mem_prod_iff α β p f g lemma tendsto_fst {f : filter α} {g : filter β} : tendsto prod.fst (f ×ᶠ g) f := tendsto_inf_left tendsto_comap lemma tendsto_snd {f : filter α} {g : filter β} : tendsto prod.snd (f ×ᶠ g) g := tendsto_inf_right tendsto_comap lemma tendsto.prod_mk {f : filter α} {g : filter β} {h : filter γ} {m₁ : α → β} {m₂ : α → γ} (h₁ : tendsto m₁ f g) (h₂ : tendsto m₂ f h) : tendsto (λx, (m₁ x, m₂ x)) f (g ×ᶠ h) := tendsto_inf.2 ⟨tendsto_comap_iff.2 h₁, tendsto_comap_iff.2 h₂⟩ lemma eventually.prod_inl {la : filter α} {p : α → Prop} (h : ∀ᶠ x in la, p x) (lb : filter β) : ∀ᶠ x in la ×ᶠ lb, p (x : α × β).1 := tendsto_fst.eventually h lemma eventually.prod_inr {lb : filter β} {p : β → Prop} (h : ∀ᶠ x in lb, p x) (la : filter α) : ∀ᶠ x in la ×ᶠ lb, p (x : α × β).2 := tendsto_snd.eventually h lemma eventually.prod_mk {la : filter α} {pa : α → Prop} (ha : ∀ᶠ x in la, pa x) {lb : filter β} {pb : β → Prop} (hb : ∀ᶠ y in lb, pb y) : ∀ᶠ p in la ×ᶠ lb, pa (p : α × β).1 ∧ pb p.2 := (ha.prod_inl lb).and (hb.prod_inr la) lemma eventually.curry {la : filter α} {lb : filter β} {p : α × β → Prop} (h : ∀ᶠ x in la ×ᶠ lb, p x) : ∀ᶠ x in la, ∀ᶠ y in lb, p (x, y) := begin rcases eventually_prod_iff.1 h with ⟨pa, ha, pb, hb, h⟩, exact ha.mono (λ a ha, hb.mono $ λ b hb, h ha hb) end lemma prod_infi_left [nonempty ι] {f : ι → filter α} {g : filter β}: (⨅i, f i) ×ᶠ g = (⨅i, (f i) ×ᶠ g) := by rw [filter.prod, comap_infi, infi_inf]; simp only [filter.prod, eq_self_iff_true] lemma prod_infi_right [nonempty ι] {f : filter α} {g : ι → filter β} : f ×ᶠ (⨅i, g i) = (⨅i, f ×ᶠ (g i)) := by rw [filter.prod, comap_infi, inf_infi]; simp only [filter.prod, eq_self_iff_true] @[mono] lemma prod_mono {f₁ f₂ : filter α} {g₁ g₂ : filter β} (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁ ×ᶠ g₁ ≤ f₂ ×ᶠ g₂ := inf_le_inf (comap_mono hf) (comap_mono hg) lemma prod_comap_comap_eq {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x} {f₁ : filter α₁} {f₂ : filter α₂} {m₁ : β₁ → α₁} {m₂ : β₂ → α₂} : (comap m₁ f₁) ×ᶠ (comap m₂ f₂) = comap (λp:β₁×β₂, (m₁ p.1, m₂ p.2)) (f₁ ×ᶠ f₂) := by simp only [filter.prod, comap_comap, eq_self_iff_true, comap_inf] lemma prod_comm' : f ×ᶠ g = comap (prod.swap) (g ×ᶠ f) := by simp only [filter.prod, comap_comap, (∘), inf_comm, prod.fst_swap, eq_self_iff_true, prod.snd_swap, comap_inf] lemma prod_comm : f ×ᶠ g = map (λp:β×α, (p.2, p.1)) (g ×ᶠ f) := by rw [prod_comm', ← map_swap_eq_comap_swap]; refl lemma prod_map_map_eq {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x} {f₁ : filter α₁} {f₂ : filter α₂} {m₁ : α₁ → β₁} {m₂ : α₂ → β₂} : (map m₁ f₁) ×ᶠ (map m₂ f₂) = map (λp:α₁×α₂, (m₁ p.1, m₂ p.2)) (f₁ ×ᶠ f₂) := le_antisymm (assume s hs, let ⟨s₁, hs₁, s₂, hs₂, h⟩ := mem_prod_iff.mp hs in filter.sets_of_superset _ (prod_mem_prod (image_mem_map hs₁) (image_mem_map hs₂)) $ calc set.prod (m₁ '' s₁) (m₂ '' s₂) = (λp:α₁×α₂, (m₁ p.1, m₂ p.2)) '' set.prod s₁ s₂ : set.prod_image_image_eq ... ⊆ _ : by rwa [image_subset_iff]) ((tendsto.comp (le_refl _) tendsto_fst).prod_mk (tendsto.comp (le_refl _) tendsto_snd)) lemma tendsto.prod_map {δ : Type*} {f : α → γ} {g : β → δ} {a : filter α} {b : filter β} {c : filter γ} {d : filter δ} (hf : tendsto f a c) (hg : tendsto g b d) : tendsto (prod.map f g) (a ×ᶠ b) (c ×ᶠ d) := begin erw [tendsto, ← prod_map_map_eq], exact filter.prod_mono hf hg, end lemma map_prod (m : α × β → γ) (f : filter α) (g : filter β) : map m (f ×ᶠ g) = (f.map (λa b, m (a, b))).seq g := begin simp [filter.ext_iff, mem_prod_iff, mem_map_seq_iff], assume s, split, exact assume ⟨t, ht, s, hs, h⟩, ⟨s, hs, t, ht, assume x hx y hy, @h ⟨x, y⟩ ⟨hx, hy⟩⟩, exact assume ⟨s, hs, t, ht, h⟩, ⟨t, ht, s, hs, assume ⟨x, y⟩ ⟨hx, hy⟩, h x hx y hy⟩ end lemma prod_eq {f : filter α} {g : filter β} : f ×ᶠ g = (f.map prod.mk).seq g := have h : _ := map_prod id f g, by rwa [map_id] at h lemma prod_inf_prod {f₁ f₂ : filter α} {g₁ g₂ : filter β} : (f₁ ×ᶠ g₁) ⊓ (f₂ ×ᶠ g₂) = (f₁ ⊓ f₂) ×ᶠ (g₁ ⊓ g₂) := by simp only [filter.prod, comap_inf, inf_comm, inf_assoc, inf_left_comm] @[simp] lemma prod_bot {f : filter α} : f ×ᶠ (⊥ : filter β) = ⊥ := by simp [filter.prod] @[simp] lemma bot_prod {g : filter β} : (⊥ : filter α) ×ᶠ g = ⊥ := by simp [filter.prod] @[simp] lemma prod_principal_principal {s : set α} {t : set β} : (𝓟 s) ×ᶠ (𝓟 t) = 𝓟 (set.prod s t) := by simp only [filter.prod, comap_principal, principal_eq_iff_eq, comap_principal, inf_principal]; refl @[simp] lemma prod_pure_pure {a : α} {b : β} : (pure a) ×ᶠ (pure b) = pure (a, b) := by simp only [← principal_singleton, prod_principal_principal, singleton_prod_singleton] lemma prod_eq_bot {f : filter α} {g : filter β} : f ×ᶠ g = ⊥ ↔ (f = ⊥ ∨ g = ⊥) := begin split, { assume h, rcases mem_prod_iff.1 (empty_in_sets_eq_bot.2 h) with ⟨s, hs, t, ht, hst⟩, rw [subset_empty_iff, set.prod_eq_empty_iff] at hst, cases hst with s_eq t_eq, { left, exact empty_in_sets_eq_bot.1 (s_eq ▸ hs) }, { right, exact empty_in_sets_eq_bot.1 (t_eq ▸ ht) } }, { rintros (rfl | rfl), exact bot_prod, exact prod_bot } end lemma prod_ne_bot {f : filter α} {g : filter β} : ne_bot (f ×ᶠ g) ↔ (ne_bot f ∧ ne_bot g) := (not_congr prod_eq_bot).trans not_or_distrib lemma ne_bot.prod {f : filter α} {g : filter β} (hf : ne_bot f) (hg : ne_bot g) : ne_bot (f ×ᶠ g) := prod_ne_bot.2 ⟨hf, hg⟩ instance prod_ne_bot' {f : filter α} {g : filter β} [hf : ne_bot f] [hg : ne_bot g] : ne_bot (f ×ᶠ g) := hf.prod hg lemma tendsto_prod_iff {f : α × β → γ} {x : filter α} {y : filter β} {z : filter γ} : filter.tendsto f (x ×ᶠ y) z ↔ ∀ W ∈ z, ∃ U ∈ x, ∃ V ∈ y, ∀ x y, x ∈ U → y ∈ V → f (x, y) ∈ W := by simp only [tendsto_def, mem_prod_iff, prod_sub_preimage_iff, exists_prop, iff_self] end prod end filter open_locale filter lemma set.eq_on.eventually_eq {α β} {s : set α} {f g : α → β} (h : eq_on f g s) : f =ᶠ[𝓟 s] g := h
078ef76998919e6e7b14e3bb54823d3c8d6b209d
8e2026ac8a0660b5a490dfb895599fb445bb77a0
/library/init/meta/interactive.lean
d61a4baada605882aa1f75af0aa7af7214d0c887
[ "Apache-2.0" ]
permissive
pcmoritz/lean
6a8575115a724af933678d829b4f791a0cb55beb
35eba0107e4cc8a52778259bb5392300267bfc29
refs/heads/master
1,607,896,326,092
1,490,752,175,000
1,490,752,175,000
86,612,290
0
0
null
1,490,809,641,000
1,490,809,641,000
null
UTF-8
Lean
false
false
26,664
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.meta.tactic init.meta.rewrite_tactic init.meta.simp_tactic import init.meta.smt.congruence_closure init.category.combinators import init.meta.lean.parser init.meta.quote open lean open lean.parser local postfix `?`:9001 := optional local postfix *:9001 := many namespace interactive /-- (parse p) as the parameter type of an interactive tactic will instruct the Lean parser to run `p` when parsing the parameter and to pass the parsed value as an argument to the tactic. -/ @[reducible] meta def parse {α : Type} [has_quote α] (p : parser α) : Type := α namespace types variables {α β : Type} -- optimized pretty printer meta def brackets (l r : string) (p : parser α) := tk l *> p <* tk r meta def list_of (p : parser α) := brackets "[" "]" $ sep_by (skip_info (tk ",")) p /-- A 'tactic expression', which uses right-binding power 2 so that it is terminated by '<|>' (rbp 2), ';' (rbp 1), and ',' (rbp 0). It should be used for any (potentially) trailing expression parameters. -/ meta def texpr := qexpr 2 /-- Parse an identifier or a '_' -/ meta def ident_ : parser name := ident <|> tk "_" *> return `_ meta def using_ident := (tk "using" *> ident)? meta def with_ident_list := (tk "with" *> ident_*) <|> return [] meta def without_ident_list := (tk "without" *> ident*) <|> return [] meta def location := (tk "at" *> ident*) <|> return [] meta def qexpr_list := list_of (qexpr 0) meta def opt_qexpr_list := qexpr_list <|> return [] meta def qexpr_list_or_texpr := qexpr_list <|> list.ret <$> texpr end types /-- Use `desc` as the interactive description of `p`. -/ meta def with_desc {α : Type} (desc : format) (p : parser α) : parser α := p open expr format tactic types private meta def maybe_paren : list format → format | [] := "" | [f] := f | fs := paren (join fs) private meta def unfold (e : expr) : tactic expr := do (expr.const f_name f_lvls) ← return e.get_app_fn | failed, env ← get_env, decl ← env.get f_name, new_f ← decl.instantiate_value_univ_params f_lvls, head_beta (expr.mk_app new_f e.get_app_args) private meta def concat (f₁ f₂ : list format) := if f₁.empty then f₂ else if f₂.empty then f₁ else f₁ ++ [" "] ++ f₂ private meta def parser_desc_aux : expr → tactic (list format) | ```(ident) := return ["id"] | ```(ident_) := return ["id"] | ```(qexpr) := return ["expr"] | ```(tk %%c) := list.ret <$> to_fmt <$> eval_expr string c | ```(cur_pos) := return [] | ```(return ._) := return [] | ```(._ <$> %%p) := parser_desc_aux p | ```(skip_info %%p) := parser_desc_aux p | ```(set_goal_info_pos %%p) := parser_desc_aux p | ```(with_desc %%desc %%p) := list.ret <$> eval_expr format desc | ```(%%p₁ <*> %%p₂) := do f₁ ← parser_desc_aux p₁, f₂ ← parser_desc_aux p₂, return $ concat f₁ f₂ | ```(%%p₁ <* %%p₂) := do f₁ ← parser_desc_aux p₁, f₂ ← parser_desc_aux p₂, return $ concat f₁ f₂ | ```(%%p₁ *> %%p₂) := do f₁ ← parser_desc_aux p₁, f₂ ← parser_desc_aux p₂, return $ concat f₁ f₂ | ```(many %%p) := do f ← parser_desc_aux p, return [maybe_paren f ++ "*"] | ```(optional %%p) := do f ← parser_desc_aux p, return [maybe_paren f ++ "?"] | ```(sep_by %%sep %%p) := do f₁ ← parser_desc_aux sep, f₂ ← parser_desc_aux p, return [maybe_paren f₂ ++ join f₁, " ..."] | ```(%%p₁ <|> %%p₂) := do f₁ ← parser_desc_aux p₁, f₂ ← parser_desc_aux p₂, return $ if f₁.empty then [maybe_paren f₂ ++ "?"] else if f₂.empty then [maybe_paren f₁ ++ "?"] else [paren $ join $ f₁ ++ [to_fmt " | "] ++ f₂] | ```(brackets %%l %%r %%p) := do f ← parser_desc_aux p, l ← eval_expr string l, r ← eval_expr string r, -- much better than the naive [l, " ", f, " ", r] return [to_fmt l ++ join f ++ to_fmt r] | e := do e' ← (do e' ← unfold e, guard $ e' ≠ e, return e') <|> (do f ← pp e, fail $ to_fmt "don't know how to pretty print " ++ f), parser_desc_aux e' meta def param_desc : expr → tactic format | ```(parse %%p) := join <$> parser_desc_aux p | ```(opt_param %%t ._) := (++ "?") <$> pp t | e := if is_constant e ∧ (const_name e).components.ilast = `itactic then return $ to_fmt "{ tactic }" else paren <$> pp e end interactive namespace tactic meta def report_resolve_name_failure {α : Type} (e : expr) (n : name) : tactic α := if e.is_choice_macro then fail ("failed to resolve name '" ++ to_string n ++ "', it is overloaded") else fail ("failed to resolve name '" ++ to_string n ++ "', unexpected result") /- allows metavars and report errors -/ meta def i_to_expr (q : pexpr) : tactic expr := to_expr q tt /- doesn't allows metavars and report errors -/ meta def i_to_expr_strict (q : pexpr) : tactic expr := to_expr q ff namespace interactive open interactive interactive.types expr /- itactic: parse a nested "interactive" tactic. That is, parse `{` tactic `}` -/ meta def itactic : Type := tactic unit /-- This tactic applies to a goal that is either a Pi/forall or starts with a let binder. If the current goal is a Pi/forall `∀ x:T, U` (resp `let x:=t in U`) then intro puts `x:T` (resp `x:=t`) in the local context. The new subgoal target is `U`. If the goal is an arrow `T → U`, then it puts in the local context either `h:T`, and the new goal target is `U`. If the goal is neither a Pi/forall nor starting with a let definition, the tactic `intro` applies the tactic `whnf` until the tactic `intro` can be applied or the goal is not `head-reducible`. -/ meta def intro : parse ident_? → tactic unit | none := intro1 >> skip | (some h) := tactic.intro h >> skip /-- Similar to `intro` tactic. The tactic `intros` will keep introducing new hypotheses until the goal target is not a Pi/forall or let binder. The variant `intros h_1 ... h_n` introduces `n` new hypotheses using the given identifiers to name them. -/ meta def intros : parse ident_* → tactic unit | [] := tactic.intros >> skip | hs := intro_lst hs >> skip /-- The tactic `rename h₁ h₂` renames hypothesis `h₁` into `h₂` in the current local context. -/ meta def rename : parse ident → parse ident → tactic unit := tactic.rename /-- This tactic applies to any goal. The argument term is a term well-formed in the local context of the main goal. The tactic apply tries to match the current goal against the conclusion of the type of term. If it succeeds, then the tactic returns as many subgoals as the number of non-dependent premises that have not been fixed by type inference or type class resolution. The tactic `apply` uses higher-order pattern matching, type class resolution, and first-order unification with dependent types. -/ meta def apply (q : parse texpr) : tactic unit := i_to_expr q >>= tactic.apply /-- Similar to the `apply` tactic, but it also creates subgoals for dependent premises that have not been fixed by type inference or type class resolution. -/ meta def fapply (q : parse texpr) : tactic unit := i_to_expr q >>= tactic.fapply /-- This tactic tries to close the main goal `... |- U` using type class resolution. It succeeds if it generates a term of type `U` using type class resolution. -/ meta def apply_instance : tactic unit := tactic.apply_instance /-- This tactic applies to any goal. It behaves like `exact` with a big difference: the user can leave some holes `_` in the term. `refine` will generate as many subgoals as there are holes in the term. Note that some holes may be implicit. The type of holes must be either synthesized by the system or declared by an explicit type ascription like (e.g., `(_ : nat → Prop)`). -/ meta def refine (q : parse texpr) : tactic unit := tactic.refine q /-- This tactic looks in the local context for an hypothesis which type is equal to the goal target. If it is the case, the subgoal is proved. Otherwise, it fails. -/ meta def assumption : tactic unit := tactic.assumption /-- This tactic applies to any goal. `change U` replaces the main goal target `T` with `U` providing that `U` is well-formed with respect to the main goal local context, and `T` and `U` are definitionally equal. -/ meta def change (q : parse texpr) : tactic unit := i_to_expr q >>= tactic.change /-- This tactic applies to any goal. It gives directly the exact proof term of the goal. Let `T` be our goal, let `p` be a term of type `U` then `exact p` succeeds iff `T` and `U` are definitionally equal. -/ meta def exact (q : parse texpr) : tactic unit := do tgt : expr ← target, i_to_expr_strict ``(%%q : %%tgt) >>= tactic.exact private meta def get_locals : list name → tactic (list expr) | [] := return [] | (n::ns) := do h ← get_local n, hs ← get_locals ns, return (h::hs) /-- `revert h₁ ... hₙ` applies to any goal with hypotheses `h₁` ... `hₙ`. It moves the hypotheses and its dependencies to the goal target. This tactic is the inverse of `intro`. -/ meta def revert (ids : parse ident*) : tactic unit := do hs ← get_locals ids, revert_lst hs, skip private meta def resolve_name' (n : name) : tactic expr := do { p ← resolve_name n, match p.to_raw_expr with | expr.const n _ := mk_const n -- create metavars for universe levels | _ := i_to_expr p end } /- Version of to_expr that tries to bypass the elaborator if `p` is just a constant or local constant. This is not an optimization, by skipping the elaborator we make sure that no unwanted resolution is used. Example: the elaborator will force any unassigned ?A that must have be an instance of (has_one ?A) to nat. Remark: another benefit is that auxiliary temporary metavariables do not appear in error messages. -/ private meta def to_expr' (p : pexpr) : tactic expr := let e := p.to_raw_expr in match e with | (const c []) := do new_e ← resolve_name' c, save_type_info new_e e, return new_e | (local_const c _ _ _) := do new_e ← resolve_name' c, save_type_info new_e e, return new_e | _ := i_to_expr p end meta structure rw_rule := (pos : pos) (symm : bool) (rule : pexpr) meta instance rw_rule_has_quote : has_quote rw_rule := ⟨λ ⟨p, s, r⟩, ``(rw_rule.mk %%(quote p) %%(quote s) %%(quote r))⟩ private meta def rw_goal (m : transparency) (rs : list rw_rule) : tactic unit := rs.mfor' $ λ r, save_info r.pos >> to_expr' r.rule >>= rewrite_core m tt tt occurrences.all r.symm private meta def rw_hyp (m : transparency) (rs : list rw_rule) (hname : name) : tactic unit := rs.mfor' $ λ r, do h ← get_local hname, save_info r.pos, e ← to_expr' r.rule, rewrite_at_core m tt tt occurrences.all r.symm e h private meta def rw_hyps (m : transparency) (rs : list rw_rule) (hs : list name) : tactic unit := hs.mfor' (rw_hyp m rs) meta def rw_rule_p (ep : parser pexpr) : parser rw_rule := rw_rule.mk <$> cur_pos <*> (option.is_some <$> (tk "-")?) <*> ep meta structure rw_rules_t := (rules : list rw_rule) (end_pos : option pos) meta instance rw_rules_t_has_quote : has_quote rw_rules_t := ⟨λ ⟨rs, p⟩, ``(rw_rules_t.mk %%(quote rs) %%(quote p))⟩ -- accepts the same content as `qexpr_list_or_texpr`, but with correct goal info pos annotations meta def rw_rules : parser rw_rules_t := (tk "[" *> rw_rules_t.mk <$> sep_by (skip_info (tk ",")) (set_goal_info_pos $ rw_rule_p (qexpr 0)) <*> (some <$> cur_pos <* set_goal_info_pos (tk "]"))) <|> rw_rules_t.mk <$> (list.ret <$> rw_rule_p texpr) <*> return none private meta def rw_core (m : transparency) (rs : parse rw_rules) (loc : parse location) : tactic unit := match loc with | [] := rw_goal m rs.rules | hs := rw_hyps m rs.rules hs end >> try (reflexivity reducible) >> (returnopt rs.end_pos >>= save_info <|> skip) meta def rewrite : parse rw_rules → parse location → tactic unit := rw_core reducible meta def rw : parse rw_rules → parse location → tactic unit := rewrite /- rewrite followed by assumption -/ meta def rwa (q : parse rw_rules) (l : parse location) : tactic unit := rewrite q l >> try assumption meta def erewrite : parse rw_rules → parse location → tactic unit := rw_core semireducible meta def erw : parse rw_rules → parse location → tactic unit := erewrite private meta def get_type_name (e : expr) : tactic name := do e_type ← infer_type e >>= whnf, (const I ls) ← return $ get_app_fn e_type, return I meta def induction (p : parse texpr) (rec_name : parse using_ident) (ids : parse with_ident_list) : tactic unit := do e ← i_to_expr p, tactic.induction e ids rec_name, return () meta def cases (p : parse texpr) (ids : parse with_ident_list) : tactic unit := do e ← i_to_expr p, tactic.cases e ids meta def destruct (p : parse texpr) : tactic unit := i_to_expr p >>= tactic.destruct meta def generalize (p : parse qexpr) (x : parse ident) : tactic unit := do e ← i_to_expr p, tactic.generalize e x meta def trivial : tactic unit := tactic.triv <|> tactic.reflexivity <|> tactic.contradiction <|> fail "trivial tactic failed" /-- Closes the main goal using sorry. -/ meta def admit : tactic unit := tactic.admit /-- This tactic applies to any goal. The contradiction tactic attempts to find in the current local context an hypothesis that is equivalent to an empty inductive type (e.g. `false`), a hypothesis of the form `c_1 ... = c_2 ...` where `c_1` and `c_2` are distinct constructors, or two contradictory hypotheses. -/ meta def contradiction : tactic unit := tactic.contradiction meta def repeat : itactic → tactic unit := tactic.repeat meta def try : itactic → tactic unit := tactic.try meta def solve1 : itactic → tactic unit := tactic.solve1 meta def abstract (id : parse ident? ) (tac : itactic) : tactic unit := tactic.abstract tac id meta def all_goals : itactic → tactic unit := tactic.all_goals meta def any_goals : itactic → tactic unit := tactic.any_goals meta def focus (tac : itactic) : tactic unit := tactic.focus [tac] /-- This tactic applies to any goal. `assert h : T` adds a new hypothesis of name `h` and type `T` to the current goal and opens a new subgoal with target `T`. The new subgoal becomes the main goal. -/ meta def assert (h : parse ident) (q : parse $ tk ":" *> texpr) : tactic unit := do e ← i_to_expr_strict q, tactic.assert h e meta def define (h : parse ident) (q : parse $ tk ":" *> texpr) : tactic unit := do e ← i_to_expr_strict q, tactic.define h e /-- This tactic applies to any goal. `assertv h : T := p` adds a new hypothesis of name `h` and type `T` to the current goal if `p` a term of type `T`. -/ meta def assertv (h : parse ident) (q₁ : parse $ tk ":" *> texpr) (q₂ : parse $ tk ":=" *> texpr) : tactic unit := do t ← i_to_expr_strict q₁, v ← i_to_expr_strict ``(%%q₂ : %%t), tactic.assertv h t v meta def definev (h : parse ident) (q₁ : parse $ tk ":" *> texpr) (q₂ : parse $ tk ":=" *> texpr) : tactic unit := do t ← i_to_expr_strict q₁, v ← i_to_expr_strict ``(%%q₂ : %%t), tactic.definev h t v meta def note (h : parse ident) (q : parse $ tk ":=" *> texpr) : tactic unit := do p ← i_to_expr_strict q, tactic.note h p meta def pose (h : parse ident) (q : parse $ tk ":=" *> texpr) : tactic unit := do p ← i_to_expr_strict q, tactic.pose h p /-- This tactic displays the current state in the tracing buffer. -/ meta def trace_state : tactic unit := tactic.trace_state /-- `trace a` displays `a` in the tracing buffer. -/ meta def trace {α : Type} [has_to_tactic_format α] (a : α) : tactic unit := tactic.trace a meta def existsi (e : parse texpr) : tactic unit := i_to_expr e >>= tactic.existsi /-- This tactic applies to a goal such that its conclusion is an inductive type (say `I`). It tries to apply each constructor of `I` until it succeeds. -/ meta def constructor : tactic unit := tactic.constructor meta def left : tactic unit := tactic.left meta def right : tactic unit := tactic.right meta def split : tactic unit := tactic.split meta def exfalso : tactic unit := tactic.exfalso /-- The injection tactic is based on the fact that constructors of inductive datatypes are injections. That means that if `c` is a constructor of an inductive datatype, and if `(c t₁)` and `(c t₂)` are two terms that are equal then `t₁` and `t₂` are equal too. If `q` is a proof of a statement of conclusion `t₁ = t₂`, then injection applies injectivity to derive the equality of all arguments of `t₁` and `t₂` placed in the same positions. For example, from `(a::b) = (c::d)` we derive `a=c` and `b=d`. To use this tactic `t₁` and `t₂` should be constructor applications of the same constructor. Given `h : a::b = c::d`, the tactic `injection h` adds to new hypothesis with types `a = c` and `b = d` to the main goal. The tactic `injection h with h₁ h₂` uses the names `h₁` an `h₂` to name the new hypotheses. -/ meta def injection (q : parse texpr) (hs : parse with_ident_list) : tactic unit := do e ← i_to_expr q, tactic.injection_with e hs private meta def add_simps : simp_lemmas → list name → tactic simp_lemmas | s [] := return s | s (n::ns) := do s' ← s.add_simp n, add_simps s' ns private meta def report_invalid_simp_lemma {α : Type} (n : name): tactic α := fail ("invalid simplification lemma '" ++ to_string n ++ "' (use command 'set_option trace.simp_lemmas true' for more details)") private meta def simp_lemmas.resolve_and_add (s : simp_lemmas) (n : name) (ref : expr) : tactic simp_lemmas := do p ← resolve_name n, match p.to_raw_expr with | const n _ := (do b ← is_valid_simp_lemma_cnst reducible n, guard b, save_const_type_info n ref, s.add_simp n) <|> (do eqns ← get_eqn_lemmas_for tt n, guard (eqns.length > 0), save_const_type_info n ref, add_simps s eqns) <|> report_invalid_simp_lemma n | _ := (do e ← i_to_expr p, b ← is_valid_simp_lemma reducible e, guard b, try (save_type_info e ref), s.add e) <|> report_invalid_simp_lemma n end private meta def simp_lemmas.add_pexpr (s : simp_lemmas) (p : pexpr) : tactic simp_lemmas := let e := p.to_raw_expr in match e with | (const c []) := simp_lemmas.resolve_and_add s c e | (local_const c _ _ _) := simp_lemmas.resolve_and_add s c e | _ := do new_e ← i_to_expr p, s.add new_e end private meta def simp_lemmas.append_pexprs : simp_lemmas → list pexpr → tactic simp_lemmas | s [] := return s | s (l::ls) := do new_s ← simp_lemmas.add_pexpr s l, simp_lemmas.append_pexprs new_s ls private meta def mk_simp_set (attr_names : list name) (hs : list pexpr) (ex : list name) : tactic simp_lemmas := do s₀ ← join_user_simp_lemmas attr_names, s₁ ← simp_lemmas.append_pexprs s₀ hs, -- add equational lemmas, if any ex ← ex.mfor (λ n, list.cons n <$> get_eqn_lemmas_for tt n), return $ simp_lemmas.erase s₁ $ ex.join private meta def simp_goal (cfg : simp_config) : simp_lemmas → tactic unit | s := do (new_target, Heq) ← target >>= simplify_core cfg s `eq, tactic.assert `Htarget new_target, swap, Ht ← get_local `Htarget, mk_eq_mpr Heq Ht >>= tactic.exact private meta def simp_hyp (cfg : simp_config) (s : simp_lemmas) (h_name : name) : tactic unit := do h ← get_local h_name, htype ← infer_type h, (new_htype, eqpr) ← simplify_core cfg s `eq htype, tactic.assert (expr.local_pp_name h) new_htype, mk_eq_mp eqpr h >>= tactic.exact, try $ tactic.clear h private meta def simp_hyps (cfg : simp_config) : simp_lemmas → list name → tactic unit | s [] := skip | s (h::hs) := simp_hyp cfg s h >> simp_hyps s hs private meta def simp_core (cfg : simp_config) (ctx : list expr) (hs : list pexpr) (attr_names : list name) (ids : list name) (loc : list name) : tactic unit := do s ← mk_simp_set attr_names hs ids, s ← s.append ctx, match loc : _ → tactic unit with | [] := simp_goal cfg s | _ := simp_hyps cfg s loc end, try tactic.triv, try (tactic.reflexivity reducible) /-- This tactic uses lemmas and hypotheses to simplify the main goal target or non-dependent hypotheses. It has many variants. - `simp` simplifies the main goal target using lemmas tagged with the attribute `[simp]`. - `simp [h_1, ..., h_n]` simplifies the main goal target using the lemmas tagged with the attribute `[simp]` and the given `h_i`s. The `h_i`'s are terms. If a `h_i` is a definition `f`, then the equational lemmas associated with `f` are used. This is a convenient way to "unfold" `f`. - `simp without id_1 ... id_n` simplifies the main goal target using the lemmas tagged with the attribute `[simp]`, but removes the ones named `id_i`s. - `simp at h` simplifies the non dependent hypothesis `h : T`. The tactic fails if the target or another hypothesis depends on `h`. - `simp with attr` simplifies the main goal target using the lemmas tagged with the attribute `[attr]`. -/ meta def simp (hs : parse opt_qexpr_list) (attr_names : parse with_ident_list) (ids : parse without_ident_list) (loc : parse location) (cfg : simp_config := {}) : tactic unit := simp_core cfg [] hs attr_names ids loc /-- Similar to the `simp` tactic, but adds all applicable hypotheses as simplification rules. -/ meta def simp_using_hs (hs : parse opt_qexpr_list) (attr_names : parse with_ident_list) (ids : parse without_ident_list) (cfg : simp_config := {}) : tactic unit := do ctx ← collect_ctx_simps, simp_core cfg ctx hs attr_names ids [] meta def simph (hs : parse opt_qexpr_list) (attr_names : parse with_ident_list) (ids : parse without_ident_list) (cfg : simp_config := {}) : tactic unit := simp_using_hs hs attr_names ids cfg meta def simp_intros (ids : parse ident_*) (hs : parse opt_qexpr_list) (attr_names : parse with_ident_list) (wo_ids : parse without_ident_list) (cfg : simp_config := {}) : tactic unit := do s ← mk_simp_set attr_names hs wo_ids, match ids with | [] := simp_intros_using s cfg | ns := simp_intro_lst_using ns s cfg end, try triv >> try (reflexivity reducible) meta def simph_intros (ids : parse ident_*) (hs : parse opt_qexpr_list) (attr_names : parse with_ident_list) (wo_ids : parse without_ident_list) (cfg : simp_config := {}) : tactic unit := do s ← mk_simp_set attr_names hs wo_ids, match ids with | [] := simph_intros_using s cfg | ns := simph_intro_lst_using ns s cfg end, try triv >> try (reflexivity reducible) private meta def dsimp_hyps (s : simp_lemmas) : list name → tactic unit | [] := skip | (h::hs) := get_local h >>= dsimp_at_core s meta def dsimp (es : parse opt_qexpr_list) (attr_names : parse with_ident_list) (ids : parse without_ident_list) : parse location → tactic unit | [] := do s ← mk_simp_set attr_names es ids, tactic.dsimp_core s | hs := do s ← mk_simp_set attr_names es ids, dsimp_hyps s hs /-- This tactic applies to a goal that has the form `t ~ u` where `~` is a reflexive relation. That is, a relation which has a reflexivity lemma tagged with the attribute `[refl]`. The tactic checks whether `t` and `u` are definitionally equal and then solves the goal. -/ meta def reflexivity : tactic unit := tactic.reflexivity /-- Shorter name for the tactic `reflexivity`. -/ meta def refl : tactic unit := tactic.reflexivity meta def symmetry : tactic unit := tactic.symmetry meta def transitivity : tactic unit := tactic.transitivity meta def ac_reflexivity : tactic unit := tactic.ac_refl meta def ac_refl : tactic unit := tactic.ac_refl meta def cc : tactic unit := tactic.cc meta def subst (q : parse texpr) : tactic unit := i_to_expr q >>= tactic.subst >> try (tactic.reflexivity reducible) meta def clear : parse ident* → tactic unit := tactic.clear_lst private meta def to_qualified_name_core : name → list name → tactic name | n [] := fail $ "unknown declaration '" ++ to_string n ++ "'" | n (ns::nss) := do curr ← return $ ns ++ n, env ← get_env, if env.contains curr then return curr else to_qualified_name_core n nss private meta def to_qualified_name (n : name) : tactic name := do env ← get_env, if env.contains n then return n else do ns ← open_namespaces, to_qualified_name_core n ns private meta def to_qualified_names : list name → tactic (list name) | [] := return [] | (c::cs) := do new_c ← to_qualified_name c, new_cs ← to_qualified_names cs, return (new_c::new_cs) private meta def dunfold_hyps : list name → list name → tactic unit | cs [] := skip | cs (h::hs) := get_local h >>= dunfold_at cs >> dunfold_hyps cs hs meta def dunfold : parse ident* → parse location → tactic unit | cs [] := do new_cs ← to_qualified_names cs, tactic.dunfold new_cs | cs hs := do new_cs ← to_qualified_names cs, dunfold_hyps new_cs hs /- TODO(Leo): add support for non-refl lemmas -/ meta def unfold : parse ident* → parse location → tactic unit := dunfold private meta def dunfold_hyps_occs : name → occurrences → list name → tactic unit | c occs [] := skip | c occs (h::hs) := get_local h >>= dunfold_core_at occs [c] >> dunfold_hyps_occs c occs hs meta def dunfold_occs : parse ident → parse location → list nat → tactic unit | c [] ps := do new_c ← to_qualified_name c, tactic.dunfold_occs_of ps new_c | c hs ps := do new_c ← to_qualified_name c, dunfold_hyps_occs new_c (occurrences.pos ps) hs /- TODO(Leo): add support for non-refl lemmas -/ meta def unfold_occs : parse ident → parse location → list nat → tactic unit := dunfold_occs private meta def delta_hyps : list name → list name → tactic unit | cs [] := skip | cs (h::hs) := get_local h >>= delta_at cs >> dunfold_hyps cs hs meta def delta : parse ident* → parse location → tactic unit | cs [] := do new_cs ← to_qualified_names cs, tactic.delta new_cs | cs hs := do new_cs ← to_qualified_names cs, delta_hyps new_cs hs meta def apply_opt_param : tactic unit := tactic.apply_opt_param meta def apply_auto_param : tactic unit := tactic.apply_auto_param meta def fail_if_success (tac : itactic) : tactic unit := tactic.fail_if_success tac meta def guard_expr_eq (t : expr) (p : parse $ tk ":=" *> texpr) : tactic unit := do e ← to_expr p, guard (alpha_eqv t e) meta def guard_target (p : parse texpr) : tactic unit := do t ← target, guard_expr_eq t p end interactive end tactic
53b0d319cc4ac6a72f9929d3c919bcd05117d623
4727251e0cd73359b15b664c3170e5d754078599
/src/order/filter/indicator_function.lean
62c22c87cbecafa07661d5e764cd2837ce36c15b
[ "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
4,024
lean
/- Copyright (c) 2020 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou, Yury Kudryashov -/ import algebra.indicator_function import order.filter.at_top_bot /-! # Indicator function and filters Properties of indicator functions involving `=ᶠ` and `≤ᶠ`. ## Tags indicator, characteristic, filter -/ variables {α β M E : Type*} open set filter classical open_locale filter classical section has_zero variables [has_zero M] {s t : set α} {f g : α → M} {a : α} {l : filter α} lemma indicator_eventually_eq (hf : f =ᶠ[l ⊓ 𝓟 s] g) (hs : s =ᶠ[l] t) : indicator s f =ᶠ[l] indicator t g := (eventually_inf_principal.1 hf).mp $ hs.mem_iff.mono $ λ x hst hfg, by_cases (λ hxs : x ∈ s, by simp only [*, hst.1 hxs, indicator_of_mem]) (λ hxs, by simp only [indicator_of_not_mem hxs, indicator_of_not_mem (mt hst.2 hxs)]) end has_zero section add_monoid variables [add_monoid M] {s t : set α} {f g : α → M} {a : α} {l : filter α} lemma indicator_union_eventually_eq (h : ∀ᶠ a in l, a ∉ s ∩ t) : indicator (s ∪ t) f =ᶠ[l] indicator s f + indicator t f := h.mono $ λ a ha, indicator_union_of_not_mem_inter ha _ end add_monoid section order variables [has_zero β] [preorder β] {s t : set α} {f g : α → β} {a : α} {l : filter α} lemma indicator_eventually_le_indicator (h : f ≤ᶠ[l ⊓ 𝓟 s] g) : indicator s f ≤ᶠ[l] indicator s g := (eventually_inf_principal.1 h).mono $ assume a h, indicator_rel_indicator le_rfl h end order lemma monotone.tendsto_indicator {ι} [preorder ι] [has_zero β] (s : ι → set α) (hs : monotone s) (f : α → β) (a : α) : tendsto (λi, indicator (s i) f a) at_top (pure $ indicator (⋃ i, s i) f a) := begin by_cases h : ∃i, a ∈ s i, { rcases h with ⟨i, hi⟩, refine tendsto_pure.2 ((eventually_ge_at_top i).mono $ assume n hn, _), rw [indicator_of_mem (hs hn hi) _, indicator_of_mem ((subset_Union _ _) hi) _] }, { rw [not_exists] at h, simp only [indicator_of_not_mem (h _)], convert tendsto_const_pure, apply indicator_of_not_mem, simpa only [not_exists, mem_Union] } end lemma antitone.tendsto_indicator {ι} [preorder ι] [has_zero β] (s : ι → set α) (hs : antitone s) (f : α → β) (a : α) : tendsto (λi, indicator (s i) f a) at_top (pure $ indicator (⋂ i, s i) f a) := begin by_cases h : ∃i, a ∉ s i, { rcases h with ⟨i, hi⟩, refine tendsto_pure.2 ((eventually_ge_at_top i).mono $ assume n hn, _), rw [indicator_of_not_mem _ _, indicator_of_not_mem _ _], { simp only [mem_Inter, not_forall], exact ⟨i, hi⟩ }, { assume h, have := hs hn h, contradiction } }, { push_neg at h, simp only [indicator_of_mem, h, (mem_Inter.2 h), tendsto_const_pure] } end lemma tendsto_indicator_bUnion_finset {ι} [has_zero β] (s : ι → set α) (f : α → β) (a : α) : tendsto (λ (n : finset ι), indicator (⋃i∈n, s i) f a) at_top (pure $ indicator (Union s) f a) := begin rw Union_eq_Union_finset s, refine monotone.tendsto_indicator (λ n : finset ι, ⋃ i ∈ n, s i) _ f a, exact λ t₁ t₂, bUnion_subset_bUnion_left end lemma filter.eventually_eq.support [has_zero β] {f g : α → β} {l : filter α} (h : f =ᶠ[l] g) : function.support f =ᶠ[l] function.support g := begin filter_upwards [h] with x hx, rw eq_iff_iff, change f x ≠ 0 ↔ g x ≠ 0, rw hx, end lemma filter.eventually_eq.indicator [has_zero β] {l : filter α} {f g : α → β} {s : set α} (hfg : f =ᶠ[l] g) : s.indicator f =ᶠ[l] s.indicator g := begin filter_upwards [hfg] with x hx, by_cases x ∈ s, { rwa [indicator_of_mem h, indicator_of_mem h] }, { rw [indicator_of_not_mem h, indicator_of_not_mem h] } end lemma filter.eventually_eq.indicator_zero [has_zero β] {l : filter α} {f : α → β} {s : set α} (hf : f =ᶠ[l] 0) : s.indicator f =ᶠ[l] 0 := begin refine hf.indicator.trans _, rw indicator_zero' end
a58318eaa4bc328dac94b66802877bea38d0d282
6b2a480f27775cba4f3ae191b1c1387a29de586e
/group_rep1/group_representation.lean
0110d6adf43f6f718faad2d26b894d1b281ea85b
[]
no_license
Or7ando/group_representation
a681de2e19d1930a1e1be573d6735a2f0b8356cb
9b576984f17764ebf26c8caa2a542d248f1b50d2
refs/heads/master
1,662,413,107,324
1,590,302,389,000
1,590,302,389,000
258,130,829
0
1
null
null
null
null
UTF-8
Lean
false
false
770
lean
/- Copyright (c) 2020 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: ... -/ import linear_algebra.basic linear_algebra.finite_dimensional import algebra.module --infix ` * ` := linear_map.comp universe variables u v w open linear_map /-- A representation of a group `G` on an `R`-module `M` is a group homomorphism from `G` to `GL(M)`. -/ def group_representation (G R M : Type*) [group G] [ring R] [add_comm_group M] [module R M] : Type* := G →* M ≃ₗ[R] M variables {G : Type u} [group G] {R : Type v} [ring R] {M : Type w}[add_comm_group M] [module R M] instance : has_coe_to_fun (group_representation G R M) := ⟨_, λ ρ , ρ.to_fun⟩
c1006efc4d33ca0960d9f8288e2236ccd4d2a493
05b503addd423dd68145d68b8cde5cd595d74365
/test/solve_by_elim.lean
36cd79eeaa00c9cbeeaedea1d62cfc43ae5a963e
[ "Apache-2.0" ]
permissive
aestriplex/mathlib
77513ff2b176d74a3bec114f33b519069788811d
e2fa8b2b1b732d7c25119229e3cdfba8370cb00f
refs/heads/master
1,621,969,960,692
1,586,279,279,000
1,586,279,279,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,800
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Scott Morrison -/ import tactic.solve_by_elim example {a b : Prop} (h₀ : a → b) (h₁ : a) : b := begin apply_assumption, apply_assumption, end example {X : Type} (x : X) : x = x := by solve_by_elim example : true := by solve_by_elim example {a b : Prop} (h₀ : a → b) (h₁ : a) : b := by solve_by_elim example {α : Type} {a b : α → Prop} (h₀ : ∀ x : α, b x = a x) (y : α) : a y = b y := by solve_by_elim example {α : Type} {a b : α → Prop} (h₀ : b = a) (y : α) : a y = b y := by solve_by_elim example {α : Type} {a b : α → Prop} (h₀ : b = a) (y : α) : a y = b y := begin success_if_fail { solve_by_elim only [], }, success_if_fail { solve_by_elim only [h₀], }, solve_by_elim only [h₀, congr_fun] end example {α : Type} {a b : α → Prop} (h₀ : b = a) (y : α) : a y = b y := by solve_by_elim [h₀] example {α : Type} {a b : α → Prop} (h₀ : b = a) (y : α) : a y = b y := begin success_if_fail { solve_by_elim [*, -h₀] }, solve_by_elim [*] end example {α β : Type} (a b : α) (f : α → β) (i : function.injective f) (h : f a = f b) : a = b := begin success_if_fail { solve_by_elim only [i] }, success_if_fail { solve_by_elim only [h] }, solve_by_elim only [i,h] end @[user_attribute] meta def ex : user_attribute := { name := `ex, descr := "An example attribute for testing solve_by_elim." } @[ex] def f : ℕ := 0 example : ℕ := by solve_by_elim [f] example : ℕ := begin success_if_fail { solve_by_elim }, success_if_fail { solve_by_elim [-f] with ex }, solve_by_elim with ex, end example {α : Type} {p : α → Prop} (h₀ : ∀ x, p x) (y : α) : p y := begin apply_assumption, end open tactic example : true := begin (do gs ← get_goals, set_goals [], success_if_fail `[solve_by_elim], set_goals gs), trivial end example {α : Type} (r : α → α → Prop) (f : α → α → α) (l : ∀ a b c : α, r a b → r a (f b c) → r a c) (a b c : α) (h₁ : r a b) (h₂ : r a (f b c)) : r a c := begin solve_by_elim, end -- Verifying that `solve_by_elim*` acts on all remaining goals. example (n : ℕ) : ℕ × ℕ := begin split, solve_by_elim*, end -- Verifying that `solve_by_elim*` backtracks when given multiple goals. example (n m : ℕ) (f : ℕ → ℕ → Prop) (h : f n m) : ∃ p : ℕ × ℕ, f p.1 p.2 := begin repeat { split }, solve_by_elim*, end example {a b c : ℕ} (h₁ : a ≤ b) (h₂ : b ≤ c) : a ≤ c := begin apply le_trans, solve_by_elim { backtrack_all_goals := true }, end /- We now test the `accept` feature of `solve_by_elim`. Recall that the `accept` parameter has type `list expr → tactic unit`. At each branch (not just leaf) of the backtracking search tree, `accept` is invoked with the list of metavariables reported by `get_goals` when `solve_by_elim` was called (which by now may have been partially solved by previous `apply` steps), and if it fails this branch of the search is ignored. Non-leaf nodes of the search tree will contain metavariables, so we can test using `expr.has_mvar` when we're only interesting in filtering complete solutions. In this example, we only accept solutions that contain a given subexpression. -/ def solve_by_elim_use_b (a b : ℕ) : ℕ × ℕ × ℕ := begin split; [skip, split], (do b ← get_local `b, tactic.solve_by_elim { backtrack_all_goals := tt, -- We require that in some goal, the expression `b` is used. accept := (λ gs, gs.any_of (λ g, guard $ g.contains_expr_or_mvar b)) }) end -- We verify that the solution did use `b`. example : solve_by_elim_use_b 1 2 = (1, 1, 2) := rfl
980224a2d9aac165aa08e638c653963e6f24ccaa
6fb1523f14e3297f9ad9b10eb132e6170b011888
/src/2021/logic/sheet1.lean
d6230228a6ebf9ede3c09bf103cf9b0808c21891
[]
no_license
jfmc/M40001_lean
392ef2ca3984f0d56b2f9bb22eafc45416e694ba
4502e3eb1af550c345cfda3aef7ffa89474fac24
refs/heads/master
1,693,810,669,330
1,634,755,889,000
1,634,755,889,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,008
lean
/- Copyright (c) 2021 Kevin Buzzard. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author : Kevin Buzzard -/ import tactic -- imports all the Lean tactics /-! # Logic in Lean, example sheet 1 : "implies" (`→`) We learn about propositions, and implications `P → Q` between them. You can get this arrow by typing `\to` or `\r`. Mathematicians usually write the implication arrow as `P ⇒ Q` but Lean prefers a single arrow. ## Tactics you will need To solve the levels on this sheet you will need to know how to use the following tactics: * `intro` * `exact` * `apply` ### The `intro` tactic If your goal is `⊢ P → Q` then `intro hP,` will intrduce a hypothesis `hP : P` and change the goal to `⊢ Q`. ### The `exact` tactic If your goal is `⊢ P` and you have a hypothesis `h : P` then `exact h,` will solve it. ### The `apply` tactic If your goal is `⊢ Q` and you have `h : P → Q` then `apply h,` will change the goal to `⊢ P`. -/ -- Throughout this sheet, `P`, `Q` and `R` will denote propositions. variables (P Q R : Prop) -- Here are the levels. Delete the `sorry`s and replace them with -- comma-separated tactic proofs. /-- Every proposition implies itself. -/ example : P → P := begin sorry end /- The solution to this level: example : P → P := begin intro hP, exact hP, end -/ /- Note that `→` is not associative: in general `P → (Q → R)` and `(P → Q) → R` might not be equivalent. This is like subtraction on numbers -- in general `a - (b - c)` and `(a - b) - c` might not be equal. So if we write `P → Q → R` then we'd better know what this means. The convention in Lean is that it means `P → (Q → R)`. -/ example : P → Q → P := begin sorry end /-- If we know `P`, and we also know `P → Q`, we can deduce `Q`. This is called "Modus Ponens" by logicians. -/ example : P → (P → Q) → Q := begin sorry end /-- `→` is transitive. That is, if `P → Q` and `Q → R` are true, then so is `P → R`. -/ example : (P → Q) → (Q → R) → (P → R) := begin sorry, end -- If `h : P → Q → R` with goal `⊢ R` and you `apply h`, you'll get -- two goals! Note that tactics operate on only the first goal. example : (P → Q → R) → (P → Q) → (P → R) := begin sorry end -- Now they get a little harder variables (S T : Prop) example : (P → R) → (S → Q) → (R → T) → (Q → R) → S → T := begin sorry end example : (P → P → Q) → ((P → Q) → P) → Q := begin sorry end example : ((P → Q) → R) → ((Q → R) → P) → ((R → P) → Q) → P := begin sorry end example : ((Q → P) → P) → (Q → R) → (R → P) → P := begin sorry end example : (((P → Q) → Q) → Q) → (P → Q) := begin sorry end example : (((P → Q → Q) → ((P → Q) → Q)) → R) → ((((P → P) → Q) → (P → P → Q)) → R) → (((P → P → Q) → ((P → P) → Q)) → R) → R := begin sorry end
c647dad1e7c0656d4043507650fa111dc8611a99
f3849be5d845a1cb97680f0bbbe03b85518312f0
/tests/lean/run/id.lean
d4f49f8434c267385c70ed10052b8709738198b0
[ "Apache-2.0" ]
permissive
bjoeris/lean
0ed95125d762b17bfcb54dad1f9721f953f92eeb
4e496b78d5e73545fa4f9a807155113d8e6b0561
refs/heads/master
1,611,251,218,281
1,495,337,658,000
1,495,337,658,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
157
lean
#check id id set_option pp.universes true #check id id #check id Prop #check id num #check @id.{0} #check @id.{1} #check id num.zero #check @eq #check eq eq
2e3c0aa3b0c011b1c847f0476504e8c3dd1633da
6f1049e897f569e5c47237de40321e62f0181948
/src/solutions/06_sub_sequences.lean
90bd70c69d8099c96b9fae35e1543bccc0a2b348
[ "Apache-2.0" ]
permissive
anrddh/tutorials
f654a0807b9523608544836d9a81939f8e1dceb8
3ba43804e7b632201c494cdaa8da5406f1a255f9
refs/heads/master
1,655,542,921,827
1,588,846,595,000
1,588,846,595,000
262,330,134
0
0
null
1,588,944,346,000
1,588,944,345,000
null
UTF-8
Lean
false
false
4,788
lean
import tuto_lib /- This file continues the elementary study of limits of sequences. It can be skipped if the previous file was too easy, it won't introduce any new tactic or trick. Remember useful lemmas: abs_le (x y : ℝ) : |x| ≤ y ↔ -y ≤ x ∧ x ≤ y abs_add (x y : ℝ) : |x + y| ≤ |x| + |y| abs_sub (x y : ℝ) : |x - y| = |y - x| ge_max_iff (p q r) : r ≥ max p q ↔ r ≥ p ∧ r ≥ q le_max_left p q : p ≤ max p q le_max_right p q : q ≤ max p q and the definition: def seq_limit (u : ℕ → ℝ) (l : ℝ) : Prop := ∀ ε > 0, ∃ N, ∀ n ≥ N, |u n - l| ≤ ε You can also use a property proved in the previous file: unique_limit : seq_limit u l → seq_limit u l' → l = l' def extraction (φ : ℕ → ℕ) := ∀ n m, n < m → φ n < φ m -/ variable { φ : ℕ → ℕ} /- The next lemma is proved by an easy induction, but we haven't seen induction in this tutorial. If you did the natural number game then you can delete the proof below and try to reconstruct it. -/ /-- An extraction is greater than id -/ lemma id_le_extraction' : extraction φ → ∀ n, n ≤ φ n := begin intros hyp n, induction n with n hn, { exact nat.zero_le _ }, { exact nat.succ_le_of_lt (by linarith [hyp n (n+1) (by linarith)]) }, end /-- Extractions take arbitrarily large values for arbitrarily large inputs. -/ -- 0039 lemma extraction_ge : extraction φ → ∀ N N', ∃ n ≥ N', φ n ≥ N := begin -- sorry intros h N N', use max N N', split, apply le_max_right, calc N ≤ max N N' : by apply le_max_left ... ≤ φ (max N N') : by apply id_le_extraction' h -- sorry end /-- A real number `a` is a cluster point of a sequence `u` if `u` has a subsequence converging to `a`. def cluster_point (u : ℕ → ℝ) (a : ℝ) := ∃ φ, extraction φ ∧ seq_limit (u ∘ φ) a -/ variables {u : ℕ → ℝ} {a l : ℝ} /- In the exercise, we use `∃ n ≥ N, ...` which is the abbreviation of `∃ n, n ≥ N ∧ ...`. Lean can read this abbreviation, but displays as the confusing : `∃ (n : ℕ) (H : n ≥ N)` One gets used to it. Alternatively, one can get rid of it using the lemma exists_prop {p q : Prop} : (∃ (h : p), q) ↔ p ∧ q -/ /-- If `a` is a cluster point of `u` then there are values of `u` arbitrarily close to `a` for arbitrarily large input. -/ -- 0040 lemma near_cluster : cluster_point u a → ∀ ε > 0, ∀ N, ∃ n ≥ N, |u n - a| ≤ ε := begin -- sorry intros hyp ε ε_pos N, rcases hyp with ⟨φ, φ_extr, hφ⟩, cases hφ ε ε_pos with N' hN', rcases extraction_ge φ_extr N N' with ⟨q, hq, hq'⟩, exact ⟨φ q, hq', hN' _ hq⟩, -- sorry end /- The above exercice can be done in five lines. Hint: you can use the anonymous constructor syntax when proving existential statements. -/ /-- If `u` tends to `l` then its subsequences tend to `l`. -/ -- 0041 lemma subseq_tendsto_of_tendsto' (h : seq_limit u l) (hφ : extraction φ) : seq_limit (u ∘ φ) l := begin -- sorry intros ε ε_pos, cases h ε ε_pos with N hN, use N, intros n hn, apply hN, calc N ≤ n : hn ... ≤ φ n : id_le_extraction' hφ n, -- sorry end /-- If `u` tends to `l` all its cluster points are equal to `l`. -/ -- 0042 lemma cluster_limit (hl : seq_limit u l) (ha : cluster_point u a) : a = l := begin -- sorry rcases ha with ⟨φ, φ_extr, lim_u_φ⟩, have lim_u_φ' : seq_limit (u ∘ φ) l, from subseq_tendsto_of_tendsto' hl φ_extr, exact unique_limit lim_u_φ lim_u_φ', -- sorry end /-- cauchy_sequence sequence -/ def cauchy_sequence (u : ℕ → ℝ) := ∀ ε > 0, ∃ N, ∀ p q, p ≥ N → q ≥ N → |u p - u q| ≤ ε -- 0043 example : (∃ l, seq_limit u l) → cauchy_sequence u := begin -- sorry intro hyp, cases hyp with l hl, intros ε ε_pos, cases hl (ε/2) (by linarith) with N hN, use N, intros p q hp hq, calc |u p - u q| = |(u p - l) + (l - u q)| : by ring ... ≤ |u p - l| + |l - u q| : by apply abs_add ... = |u p - l| + |u q - l| : by rw abs_sub (u q) l ... ≤ ε : by linarith [hN p hp, hN q hq], -- sorry end /- In the next exercise, you can reuse near_cluster : cluster_point u a → ∀ ε > 0, ∀ N, ∃ n ≥ N, |u n - a| ≤ ε -/ -- 0044 example (hu : cauchy_sequence u) (hl : cluster_point u l) : seq_limit u l := begin -- sorry intros ε ε_pos, cases hu (ε/2) (by linarith) with N hN, use N, have clef : ∃ N' ≥ N, |u N' - l| ≤ ε/2, apply near_cluster hl (ε/2) (by linarith), cases clef with N' h, cases h with hNN' hN', intros n hn, calc |u n - l| = |(u n - u N') + (u N' - l)| : by ring ... ≤ |u n - u N'| + |u N' - l| : by apply abs_add ... ≤ ε : by linarith [hN n N' (by linarith) hNN'], -- sorry end
983aad0f49aa81d8f26a0177e8d785bbe56cc034
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/linear_algebra/clifford_algebra_auto.lean
54a4adb01221fa72efc0f7cfbfc5d01ff6c63200
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
7,707
lean
/- Copyright (c) 2020 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser, Utensil Song. -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.ring_quot import Mathlib.linear_algebra.tensor_algebra import Mathlib.linear_algebra.exterior_algebra import Mathlib.linear_algebra.quadratic_form import Mathlib.PostPort universes u_1 u_2 u_3 namespace Mathlib /-! # Clifford Algebras We construct the Clifford algebra of a module `M` over a commutative ring `R`, equipped with a quadratic_form `Q`. ## Notation The Clifford algebra of the `R`-module `M` equipped with a quadratic_form `Q` is denoted as `clifford_algebra Q`. Given a linear morphism `f : M → A` from a semimodule `M` to another `R`-algebra `A`, such that `cond : ∀ m, f m * f m = algebra_map _ _ (Q m)`, there is a (unique) lift of `f` to an `R`-algebra morphism, which is denoted `clifford_algebra.lift Q f cond`. The canonical linear map `M → clifford_algebra Q` is denoted `clifford_algebra.ι Q`. ## Theorems The main theorems proved ensure that `clifford_algebra Q` satisfies the universal property of the Clifford algebra. 1. `ι_comp_lift` is the fact that the composition of `ι Q` with `lift Q f cond` agrees with `f`. 2. `lift_unique` ensures the uniqueness of `lift Q f cond` with respect to 1. Additionally, when `Q = 0` an `alg_equiv` to the `exterior_algebra` is provided as `as_exterior`. ## Implementation details The Clifford algebra of `M` is constructed as a quotient of the tensor algebra, as follows. 1. We define a relation `clifford_algebra.rel Q` on `tensor_algebra R M`. This is the smallest relation which identifies squares of elements of `M` with `Q m`. 2. The Clifford algebra is the quotient of the tensor algebra by this relation. This file is almost identical to `linear_algebra/exterior_algebra.lean`. -/ namespace clifford_algebra /-- `rel` relates each `ι m * ι m`, for `m : M`, with `Q m`. The Clifford algebra of `M` is defined as the quotient modulo this relation. -/ inductive rel {R : Type u_1} [comm_ring R] {M : Type u_2} [add_comm_group M] [module R M] (Q : quadratic_form R M) : tensor_algebra R M → tensor_algebra R M → Prop where | of : ∀ (m : M), rel Q (coe_fn (tensor_algebra.ι R) m * coe_fn (tensor_algebra.ι R) m) (coe_fn (algebra_map R (tensor_algebra R M)) (coe_fn Q m)) end clifford_algebra /-- The Clifford algebra of an `R`-module `M` equipped with a quadratic_form `Q`. -/ def clifford_algebra {R : Type u_1} [comm_ring R] {M : Type u_2} [add_comm_group M] [module R M] (Q : quadratic_form R M) := ring_quot sorry namespace clifford_algebra /-- The canonical linear map `M →ₗ[R] clifford_algebra Q`. -/ def ι {R : Type u_1} [comm_ring R] {M : Type u_2} [add_comm_group M] [module R M] (Q : quadratic_form R M) : linear_map R M (clifford_algebra Q) := linear_map.comp (alg_hom.to_linear_map (ring_quot.mk_alg_hom R (rel Q))) (tensor_algebra.ι R) /-- As well as being linear, `ι Q` squares to the quadratic form -/ @[simp] theorem ι_square_scalar {R : Type u_1} [comm_ring R] {M : Type u_2} [add_comm_group M] [module R M] (Q : quadratic_form R M) (m : M) : coe_fn (ι Q) m * coe_fn (ι Q) m = coe_fn (algebra_map R (clifford_algebra Q)) (coe_fn Q m) := sorry @[simp] theorem comp_ι_square_scalar {R : Type u_1} [comm_ring R] {M : Type u_2} [add_comm_group M] [module R M] {Q : quadratic_form R M} {A : Type u_3} [semiring A] [algebra R A] (g : alg_hom R (clifford_algebra Q) A) (m : M) : coe_fn g (coe_fn (ι Q) m) * coe_fn g (coe_fn (ι Q) m) = coe_fn (algebra_map R A) (coe_fn Q m) := sorry /-- Given a linear map `f : M →ₗ[R] A` into an `R`-algebra `A`, which satisfies the condition: `cond : ∀ m : M, f m * f m = Q(m)`, this is the canonical lift of `f` to a morphism of `R`-algebras from `clifford_algebra Q` to `A`. -/ @[simp] theorem lift_symm_apply {R : Type u_1} [comm_ring R] {M : Type u_2} [add_comm_group M] [module R M] (Q : quadratic_form R M) {A : Type u_3} [semiring A] [algebra R A] (F : alg_hom R (clifford_algebra Q) A) : coe_fn (equiv.symm (lift Q)) F = { val := linear_map.comp (alg_hom.to_linear_map F) (ι Q), property := lift._proof_2 Q F } := Eq.refl (coe_fn (equiv.symm (lift Q)) F) @[simp] theorem ι_comp_lift {R : Type u_1} [comm_ring R] {M : Type u_2} [add_comm_group M] [module R M] {Q : quadratic_form R M} {A : Type u_3} [semiring A] [algebra R A] (f : linear_map R M A) (cond : ∀ (m : M), coe_fn f m * coe_fn f m = coe_fn (algebra_map R A) (coe_fn Q m)) : linear_map.comp (alg_hom.to_linear_map (coe_fn (lift Q) { val := f, property := cond })) (ι Q) = f := iff.mp subtype.mk_eq_mk (equiv.symm_apply_apply (lift Q) { val := f, property := cond }) @[simp] theorem lift_ι_apply {R : Type u_1} [comm_ring R] {M : Type u_2} [add_comm_group M] [module R M] {Q : quadratic_form R M} {A : Type u_3} [semiring A] [algebra R A] (f : linear_map R M A) (cond : ∀ (m : M), coe_fn f m * coe_fn f m = coe_fn (algebra_map R A) (coe_fn Q m)) (x : M) : coe_fn (coe_fn (lift Q) { val := f, property := cond }) (coe_fn (ι Q) x) = coe_fn f x := iff.mp linear_map.ext_iff (ι_comp_lift f cond) x @[simp] theorem lift_unique {R : Type u_1} [comm_ring R] {M : Type u_2} [add_comm_group M] [module R M] {Q : quadratic_form R M} {A : Type u_3} [semiring A] [algebra R A] (f : linear_map R M A) (cond : ∀ (m : M), coe_fn f m * coe_fn f m = coe_fn (algebra_map R A) (coe_fn Q m)) (g : alg_hom R (clifford_algebra Q) A) : linear_map.comp (alg_hom.to_linear_map g) (ι Q) = f ↔ g = coe_fn (lift Q) { val := f, property := cond } := sorry @[simp] theorem lift_comp_ι {R : Type u_1} [comm_ring R] {M : Type u_2} [add_comm_group M] [module R M] {Q : quadratic_form R M} {A : Type u_3} [semiring A] [algebra R A] (g : alg_hom R (clifford_algebra Q) A) : coe_fn (lift Q) { val := linear_map.comp (alg_hom.to_linear_map g) (ι Q), property := comp_ι_square_scalar g } = g := sorry /-- See note [partially-applied ext lemmas]. -/ theorem hom_ext {R : Type u_1} [comm_ring R] {M : Type u_2} [add_comm_group M] [module R M] {Q : quadratic_form R M} {A : Type u_3} [semiring A] [algebra R A] {f : alg_hom R (clifford_algebra Q) A} {g : alg_hom R (clifford_algebra Q) A} : linear_map.comp (alg_hom.to_linear_map f) (ι Q) = linear_map.comp (alg_hom.to_linear_map g) (ι Q) → f = g := sorry /-- A Clifford algebra with a zero quadratic form is isomorphic to an `exterior_algebra` -/ def as_exterior {R : Type u_1} [comm_ring R] {M : Type u_2} [add_comm_group M] [module R M] : alg_equiv R (clifford_algebra 0) (exterior_algebra R M) := alg_equiv.of_alg_hom (coe_fn (lift 0) { val := exterior_algebra.ι R, property := sorry }) (coe_fn (exterior_algebra.lift R) { val := ι 0, property := sorry }) sorry sorry end clifford_algebra namespace tensor_algebra /-- The canonical image of the `tensor_algebra` in the `clifford_algebra`, which maps `tensor_algebra.ι R x` to `clifford_algebra.ι Q x`. -/ def to_clifford {R : Type u_1} [comm_ring R] {M : Type u_2} [add_comm_group M] [module R M] {Q : quadratic_form R M} : alg_hom R (tensor_algebra R M) (clifford_algebra Q) := coe_fn (lift R) (clifford_algebra.ι Q) @[simp] theorem to_clifford_ι {R : Type u_1} [comm_ring R] {M : Type u_2} [add_comm_group M] [module R M] {Q : quadratic_form R M} (m : M) : coe_fn to_clifford (coe_fn (ι R) m) = coe_fn (clifford_algebra.ι Q) m := sorry end Mathlib
6907ad8ce9dfedbef50b4e6720f1f4fa781a57d9
7282d49021d38dacd06c4ce45a48d09627687fe0
/tests/lean/simp30.lean
dd71d6d4410da14917e2db8db0e22633a536f46b
[ "Apache-2.0" ]
permissive
steveluc/lean
5a0b4431acefaf77f15b25bbb49294c2449923ad
92ba4e8b2d040a799eda7deb8d2a7cdd3e69c496
refs/heads/master
1,611,332,256,930
1,391,013,244,000
1,391,013,244,000
16,361,079
1
0
null
null
null
null
UTF-8
Lean
false
false
1,064
lean
import cast variable vec : Nat → Type variable concat {n m : Nat} (v : vec n) (w : vec m) : vec (n + m) infixl 65 ; : concat axiom concat_assoc {n1 n2 n3 : Nat} (v1 : vec n1) (v2 : vec n2) (v3 : vec n3) : (v1 ; v2) ; v3 = cast (congr2 vec (symm (Nat::add_assoc n1 n2 n3))) (v1 ; (v2 ; v3)) variable empty : vec 0 axiom concat_empty {n : Nat} (v : vec n) : v ; empty = cast (congr2 vec (symm (Nat::add_zeror n))) v rewrite_set simple add_rewrite Nat::add_assoc Nat::add_zeror eq_id : simple add_rewrite concat_assoc concat_empty Nat::add_assoc Nat::add_zeror : simple (* local t = parse_lean('∀ (n : Nat) (v : vec (n + 0)) (w : vec n), v = w ; empty') print(t) local t2, pr = simplify(t, "simple") print("====>") print(t2) get_environment():type_check(pr) *) print "" (* local t = parse_lean('λ n : Nat, ∃ (v : vec (n + 0)) (w : vec n), v ≠ w ; empty') print(t) local t2, pr = simplify(t, "simple") print("====>") print(t2) get_environment():type_check(pr) *)
86de29ff069bb99dab3f6fb389d6681e34dfb335
367134ba5a65885e863bdc4507601606690974c1
/src/topology/category/CompHaus.lean
3fb69a62ca1a6d868987fd6e5d65672c10c5fa69
[ "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,058
lean
/- Copyright (c) 2020 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Adam Topaz, Bhavik Mehta -/ import category_theory.adjunction.reflective import topology.category.Top import topology.stone_cech /-! # The category of Compact Hausdorff Spaces We construct the category of compact Hausdorff spaces. The type of compact Hausdorff spaces is denoted `CompHaus`, and it is endowed with a category instance making it a full subcategory of `Top`. The fully faithful functor `CompHaus ⥤ Top` is denoted `CompHaus_to_Top`. **Note:** The file `topology/category/Compactum.lean` provides the equivalence between `Compactum`, which is defined as the category of algebras for the ultrafilter monad, and `CompHaus`. `Compactum_to_CompHaus` is the functor from `Compactum` to `CompHaus` which is proven to be an equivalence of categories in `Compactum_to_CompHaus.is_equivalence`. See `topology/category/Compactum.lean` for a more detailed discussion where these definitions are introduced. -/ open category_theory /-- The type of Compact Hausdorff topological spaces. -/ structure CompHaus := (to_Top : Top) [is_compact : compact_space to_Top] [is_hausdorff : t2_space to_Top] namespace CompHaus instance : inhabited CompHaus := ⟨{to_Top := { α := pempty }}⟩ instance : has_coe_to_sort CompHaus := ⟨Type*, λ X, X.to_Top⟩ instance {X : CompHaus} : compact_space X := X.is_compact instance {X : CompHaus} : t2_space X := X.is_hausdorff instance category : category CompHaus := induced_category.category to_Top @[simp] lemma coe_to_Top {X : CompHaus} : (X.to_Top : Type*) = X := rfl variables (X : Type*) [topological_space X] [compact_space X] [t2_space X] /-- A constructor for objects of the category `CompHaus`, taking a type, and bundling the compact Hausdorff topology found by typeclass inference. -/ def of : CompHaus := { to_Top := Top.of X, is_compact := ‹_›, is_hausdorff := ‹_› } @[simp] lemma coe_of : (CompHaus.of X : Type _) = X := rfl end CompHaus /-- The fully faithful embedding of `CompHaus` in `Top`. -/ @[simps {rhs_md := semireducible}, derive [full, faithful]] def CompHaus_to_Top : CompHaus ⥤ Top := induced_functor _ /-- (Implementation) The object part of the compactification functor from topological spaces to compact Hausdorff spaces. -/ @[simps] def StoneCech_obj (X : Top) : CompHaus := CompHaus.of (stone_cech X) /-- (Implementation) The bijection of homsets to establish the reflective adjunction of compact Hausdorff spaces in topological spaces. -/ noncomputable def stone_cech_equivalence (X : Top) (Y : CompHaus) : (StoneCech_obj X ⟶ Y) ≃ (X ⟶ CompHaus_to_Top.obj Y) := { to_fun := λ f, { to_fun := f ∘ stone_cech_unit, continuous_to_fun := f.2.comp (@continuous_stone_cech_unit X _) }, inv_fun := λ f, { to_fun := stone_cech_extend f.2, continuous_to_fun := continuous_stone_cech_extend f.2 }, left_inv := begin rintro ⟨f : stone_cech X ⟶ Y, hf : continuous f⟩, ext (x : stone_cech X), refine congr_fun _ x, apply continuous.ext_on dense_range_stone_cech_unit (continuous_stone_cech_extend _) hf, rintro _ ⟨y, rfl⟩, apply congr_fun (stone_cech_extend_extends (hf.comp _)) y, end, right_inv := begin rintro ⟨f : ↥X ⟶ Y, hf : continuous f⟩, ext, exact congr_fun (stone_cech_extend_extends hf) x, end } /-- The Stone-Cech compactification functor from topological spaces to compact Hausdorff spaces, left adjoint to the inclusion functor. -/ noncomputable def Top_to_CompHaus : Top ⥤ CompHaus := adjunction.left_adjoint_of_equiv stone_cech_equivalence (λ _ _ _ _ _, rfl) lemma Top_to_CompHaus_obj (X : Top) : ↥(Top_to_CompHaus.obj X) = stone_cech X := rfl /-- The category of compact Hausdorff spaces is reflective in the category of topological spaces. -/ noncomputable instance CompHaus_to_Top.reflective : reflective CompHaus_to_Top := { to_is_right_adjoint := ⟨Top_to_CompHaus, adjunction.adjunction_of_equiv_left _ _⟩ }
b8477c533f6c6d0d92a8516cc5dea8b8eee8b0f3
3dd1b66af77106badae6edb1c4dea91a146ead30
/tests/lean/run/e11.lean
57a8a4a94ae99066f07d2c88f7973e98e785860e
[ "Apache-2.0" ]
permissive
silky/lean
79c20c15c93feef47bb659a2cc139b26f3614642
df8b88dca2f8da1a422cb618cd476ef5be730546
refs/heads/master
1,610,737,587,697
1,406,574,534,000
1,406,574,534,000
22,362,176
1
0
null
null
null
null
UTF-8
Lean
false
false
897
lean
precedence `+`:65 namespace nat variable nat : Type.{1} variable add : nat → nat → nat infixl + := add end namespace int using nat (nat) variable int : Type.{1} variable add : int → int → int infixl + := add variable of_nat : nat → int coercion of_nat end section -- Using "only" the notation and declarations from the namespaces nat and int using [notation] nat using [notation] int using [decls] nat using [decls] int variables n m : nat variables i j : int check n + m check i + j -- The following check does not work, since we are not using the coercions -- check n + i -- Here is a possible trick for this kind of configuration definition add_ni (a : nat) (b : int) := (of_nat a) + b definition add_in (a : int) (b : nat) := a + (of_nat b) infixl + := add_ni infixl + := add_in check add_ni check i + n check n + i end
9d944b3a8d256e0ee1a3b01b971222056588224d
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/category_theory/preadditive/default.lean
7b24ecc674dca2b0d502bead30d88e5bcedec7ae
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,362
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 algebra.group.hom import category_theory.limits.shapes.kernels import algebra.big_operators.basic import algebra.module.basic import category_theory.endomorphism /-! # Preadditive categories A preadditive category is a category in which `X ⟶ Y` is an abelian group in such a way that composition of morphisms is linear in both variables. This file contains a definition of preadditive category that directly encodes the definition given above. The definition could also be phrased as follows: A preadditive category is a category enriched over the category of Abelian groups. Once the general framework to state this in Lean is available, the contents of this file should become obsolete. ## Main results * Definition of preadditive categories and basic properties * In a preadditive category, `f : Q ⟶ R` is mono if and only if `g ≫ f = 0 → g = 0` for all composable `g`. * A preadditive category with kernels has equalizers. ## Implementation notes The simp normal form for negation and composition is to push negations as far as possible to the outside. For example, `f ≫ (-g)` and `(-f) ≫ g` both become `-(f ≫ g)`, and `(-f) ≫ (-g)` is simplified to `f ≫ g`. ## References * [F. Borceux, *Handbook of Categorical Algebra 2*][borceux-vol2] ## Tags additive, preadditive, Hom group, Ab-category, Ab-enriched -/ universes v u open category_theory.limits open_locale big_operators namespace category_theory variables (C : Type u) [category.{v} C] /-- A category is called preadditive if `P ⟶ Q` is an abelian group such that composition is linear in both variables. -/ class preadditive := (hom_group : Π P Q : C, add_comm_group (P ⟶ Q) . tactic.apply_instance) (add_comp' : ∀ (P Q R : C) (f f' : P ⟶ Q) (g : Q ⟶ R), (f + f') ≫ g = f ≫ g + f' ≫ g . obviously) (comp_add' : ∀ (P Q R : C) (f : P ⟶ Q) (g g' : Q ⟶ R), f ≫ (g + g') = f ≫ g + f ≫ g' . obviously) attribute [instance] preadditive.hom_group restate_axiom preadditive.add_comp' restate_axiom preadditive.comp_add' attribute [simp,reassoc] preadditive.add_comp attribute [reassoc] preadditive.comp_add -- (the linter doesn't like `simp` on this lemma) attribute [simp] preadditive.comp_add end category_theory open category_theory namespace category_theory namespace preadditive section preadditive open add_monoid_hom variables {C : Type u} [category.{v} C] [preadditive C] section induced_category universes u' variables {C} {D : Type u'} (F : D → C) instance induced_category.category : preadditive.{v} (induced_category C F) := { hom_group := λ P Q, @preadditive.hom_group C _ _ (F P) (F Q), add_comp' := λ P Q R f f' g, add_comp' _ _ _ _ _ _, comp_add' := λ P Q R f g g', comp_add' _ _ _ _ _ _, } end induced_category instance (X : C) : add_comm_group (End X) := by { dsimp [End], apply_instance, } instance (X : C) : ring (End X) := { left_distrib := λ f g h, preadditive.add_comp X X X g h f, right_distrib := λ f g h, preadditive.comp_add X X X h f g, ..(infer_instance : add_comm_group (End X)), ..(infer_instance : monoid (End X)) } /-- Composition by a fixed left argument as a group homomorphism -/ def left_comp {P Q : C} (R : C) (f : P ⟶ Q) : (Q ⟶ R) →+ (P ⟶ R) := mk' (λ g, f ≫ g) $ λ g g', by simp /-- Composition by a fixed right argument as a group homomorphism -/ def right_comp (P : C) {Q R : C} (g : Q ⟶ R) : (P ⟶ Q) →+ (P ⟶ R) := mk' (λ f, f ≫ g) $ λ f f', by simp variables {P Q R : C} (f f' : P ⟶ Q) (g g' : Q ⟶ R) /-- Composition as a bilinear group homomorphism -/ def comp_hom : (P ⟶ Q) →+ (Q ⟶ R) →+ (P ⟶ R) := add_monoid_hom.mk' (λ f, left_comp _ f) $ λ f₁ f₂, add_monoid_hom.ext $ λ g, (right_comp _ g).map_add f₁ f₂ @[simp, reassoc] lemma sub_comp : (f - f') ≫ g = f ≫ g - f' ≫ g := map_sub (right_comp P g) f f' -- The redundant simp lemma linter says that simp can prove the reassoc version of this lemma. @[reassoc, simp] lemma comp_sub : f ≫ (g - g') = f ≫ g - f ≫ g' := map_sub (left_comp R f) g g' @[simp, reassoc] lemma neg_comp : (-f) ≫ g = -(f ≫ g) := map_neg (right_comp _ _) _ /- The redundant simp lemma linter says that simp can prove the reassoc version of this lemma. -/ @[reassoc, simp] lemma comp_neg : f ≫ (-g) = -(f ≫ g) := map_neg (left_comp _ _) _ @[reassoc] lemma neg_comp_neg : (-f) ≫ (-g) = f ≫ g := by simp lemma nsmul_comp (n : ℕ) : (n • f) ≫ g = n • (f ≫ g) := map_nsmul (right_comp _ _) _ _ lemma comp_nsmul (n : ℕ) : f ≫ (n • g) = n • (f ≫ g) := map_nsmul (left_comp _ _) _ _ lemma zsmul_comp (n : ℤ) : (n • f) ≫ g = n • (f ≫ g) := map_zsmul (right_comp _ _) _ _ lemma comp_zsmul (n : ℤ) : f ≫ (n • g) = n • (f ≫ g) := map_zsmul (left_comp _ _) _ _ @[reassoc] lemma comp_sum {P Q R : C} {J : Type*} (s : finset J) (f : P ⟶ Q) (g : J → (Q ⟶ R)) : f ≫ ∑ j in s, g j = ∑ j in s, f ≫ g j := map_sum (left_comp R f) _ _ @[reassoc] lemma sum_comp {P Q R : C} {J : Type*} (s : finset J) (f : J → (P ⟶ Q)) (g : Q ⟶ R) : (∑ j in s, f j) ≫ g = ∑ j in s, f j ≫ g := map_sum (right_comp P g) _ _ instance {P Q : C} {f : P ⟶ Q} [epi f] : epi (-f) := ⟨λ R g g' H, by rwa [neg_comp, neg_comp, ←comp_neg, ←comp_neg, cancel_epi, neg_inj] at H⟩ instance {P Q : C} {f : P ⟶ Q} [mono f] : mono (-f) := ⟨λ R g g' H, by rwa [comp_neg, comp_neg, ←neg_comp, ←neg_comp, cancel_mono, neg_inj] at H⟩ @[priority 100] instance preadditive_has_zero_morphisms : has_zero_morphisms C := { has_zero := infer_instance, comp_zero' := λ P Q f R, map_zero $ left_comp R f, zero_comp' := λ P Q R f, map_zero $ right_comp P f } lemma mono_of_cancel_zero {Q R : C} (f : Q ⟶ R) (h : ∀ {P : C} (g : P ⟶ Q), g ≫ f = 0 → g = 0) : mono f := ⟨λ P g g' hg, sub_eq_zero.1 $ h _ $ (map_sub (right_comp P f) g g').trans $ sub_eq_zero.2 hg⟩ lemma mono_iff_cancel_zero {Q R : C} (f : Q ⟶ R) : mono f ↔ ∀ (P : C) (g : P ⟶ Q), g ≫ f = 0 → g = 0 := ⟨λ m P g, by exactI zero_of_comp_mono _, mono_of_cancel_zero f⟩ lemma mono_of_kernel_zero {X Y : C} {f : X ⟶ Y} [has_limit (parallel_pair f 0)] (w : kernel.ι f = 0) : mono f := mono_of_cancel_zero f (λ P g h, by rw [←kernel.lift_ι f g h, w, limits.comp_zero]) lemma epi_of_cancel_zero {P Q : C} (f : P ⟶ Q) (h : ∀ {R : C} (g : Q ⟶ R), f ≫ g = 0 → g = 0) : epi f := ⟨λ R g g' hg, sub_eq_zero.1 $ h _ $ (map_sub (left_comp R f) g g').trans $ sub_eq_zero.2 hg⟩ lemma epi_iff_cancel_zero {P Q : C} (f : P ⟶ Q) : epi f ↔ ∀ (R : C) (g : Q ⟶ R), f ≫ g = 0 → g = 0 := ⟨λ e R g, by exactI zero_of_epi_comp _, epi_of_cancel_zero f⟩ lemma epi_of_cokernel_zero {X Y : C} {f : X ⟶ Y} [has_colimit (parallel_pair f 0 )] (w : cokernel.π f = 0) : epi f := epi_of_cancel_zero f (λ P g h, by rw [←cokernel.π_desc f g h, w, limits.zero_comp]) open_locale zero_object variables [has_zero_object C] lemma mono_of_kernel_iso_zero {X Y : C} {f : X ⟶ Y} [has_limit (parallel_pair f 0)] (w : kernel f ≅ 0) : mono f := mono_of_kernel_zero (zero_of_source_iso_zero _ w) lemma epi_of_cokernel_iso_zero {X Y : C} {f : X ⟶ Y} [has_colimit (parallel_pair f 0)] (w : cokernel f ≅ 0) : epi f := epi_of_cokernel_zero (zero_of_target_iso_zero _ w) end preadditive section equalizers variables {C : Type u} [category.{v} C] [preadditive C] section variables {X Y : C} (f : X ⟶ Y) (g : X ⟶ Y) /-- A kernel of `f - g` is an equalizer of `f` and `g`. -/ lemma has_limit_parallel_pair [has_kernel (f - g)] : has_limit (parallel_pair f g) := has_limit.mk { cone := fork.of_ι (kernel.ι (f - g)) (sub_eq_zero.1 $ by { rw ←comp_sub, exact kernel.condition _ }), is_limit := fork.is_limit.mk _ (λ s, kernel.lift (f - g) (fork.ι s) $ by { rw comp_sub, apply sub_eq_zero.2, exact fork.condition _ }) (λ s, by simp) (λ s m h, by { ext, simpa using h walking_parallel_pair.zero }) } end section /-- If a preadditive category has all kernels, then it also has all equalizers. -/ lemma has_equalizers_of_has_kernels [has_kernels C] : has_equalizers C := @has_equalizers_of_has_limit_parallel_pair _ _ (λ _ _ f g, has_limit_parallel_pair f g) end section variables {X Y : C} (f : X ⟶ Y) (g : X ⟶ Y) /-- A cokernel of `f - g` is a coequalizer of `f` and `g`. -/ lemma has_colimit_parallel_pair [has_cokernel (f - g)] : has_colimit (parallel_pair f g) := has_colimit.mk { cocone := cofork.of_π (cokernel.π (f - g)) (sub_eq_zero.1 $ by { rw ←sub_comp, exact cokernel.condition _ }), is_colimit := cofork.is_colimit.mk _ (λ s, cokernel.desc (f - g) (cofork.π s) $ by { rw sub_comp, apply sub_eq_zero.2, exact cofork.condition _ }) (λ s, by simp) (λ s m h, by { ext, simpa using h walking_parallel_pair.one }) } end section /-- If a preadditive category has all cokernels, then it also has all coequalizers. -/ lemma has_coequalizers_of_has_cokernels [has_cokernels C] : has_coequalizers C := @has_coequalizers_of_has_colimit_parallel_pair _ _ (λ _ _ f g, has_colimit_parallel_pair f g) end end equalizers end preadditive end category_theory
b9ced280a9cf05155c69ad7ad11f06dc7e06c03c
367134ba5a65885e863bdc4507601606690974c1
/src/field_theory/mv_polynomial.lean
4e51eb4c8d229679e5c17c72f82156f23da2f7da
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
1,530
lean
/- Copyright (c) 2019 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Johannes Hölzl -/ import ring_theory.mv_polynomial.basic /-! # Multivariate polynomials over fields This file contains basic facts about multivariate polynomials over fields, for example that the dimension of the space of multivariate polynomials over a field is equal to the cardinality of finitely supported functions from the indexing set to `ℕ`. -/ noncomputable theory open_locale classical open set linear_map submodule open_locale big_operators namespace mv_polynomial universes u v variables {σ : Type u} {K : Type v} variables (σ K) [field K] lemma quotient_mk_comp_C_injective (I : ideal (mv_polynomial σ K)) (hI : I ≠ ⊤) : function.injective ((ideal.quotient.mk I).comp mv_polynomial.C) := begin refine (ring_hom.injective_iff _).2 (λ x hx, _), rw [ring_hom.comp_apply, ideal.quotient.eq_zero_iff_mem] at hx, refine classical.by_contradiction (λ hx0, absurd (I.eq_top_iff_one.2 _) hI), have := I.smul_mem (mv_polynomial.C x⁻¹) hx, rwa [smul_eq_mul, ← mv_polynomial.C.map_mul, inv_mul_cancel hx0, mv_polynomial.C_1] at this, end end mv_polynomial namespace mv_polynomial universe u variables {σ : Type u} {K : Type u} [field K] open_locale classical lemma dim_mv_polynomial : vector_space.dim K (mv_polynomial σ K) = cardinal.mk (σ →₀ ℕ) := by rw [← cardinal.lift_inj, ← (is_basis_monomials σ K).mk_eq_dim] end mv_polynomial
4527cacdd2759fad5fd3790061718bdde89c7bf9
491068d2ad28831e7dade8d6dff871c3e49d9431
/tests/lean/run/eq16.lean
23e68b163490ed5506a4f08a9a15fc6a593ba47e
[ "Apache-2.0" ]
permissive
davidmueller13/lean
65a3ed141b4088cd0a268e4de80eb6778b21a0e9
c626e2e3c6f3771e07c32e82ee5b9e030de5b050
refs/heads/master
1,611,278,313,401
1,444,021,177,000
1,444,021,177,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
437
lean
import data.list open list variable {A : Type} set_option pp.implicit true definition append : list A → list A → list A | append nil l := l | append (h :: t) l := h :: (append t l) theorem append_nil (l : list A) : append nil l = l := rfl theorem append_cons (h : A) (t l : list A) : append (h :: t) l = h :: (append t l) := rfl example : append (1 :: 2 :: nil) (3 :: 4 :: 5 :: nil) = (1 :: 2 :: 3 :: 4 :: 5 :: nil) := rfl
7c41ea290b97b1c3dbd89056e4e7bdac1872c746
b3fced0f3ff82d577384fe81653e47df68bb2fa1
/src/linear_algebra/matrix.lean
00f4d91bd3acc79d8516a9b9db527db6487bb693
[ "Apache-2.0" ]
permissive
ratmice/mathlib
93b251ef5df08b6fd55074650ff47fdcc41a4c75
3a948a6a4cd5968d60e15ed914b1ad2f4423af8d
refs/heads/master
1,599,240,104,318
1,572,981,183,000
1,572,981,183,000
219,830,178
0
0
Apache-2.0
1,572,980,897,000
1,572,980,896,000
null
UTF-8
Lean
false
false
9,515
lean
/- Copyright (c) 2019 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Johannes Hölzl, Casper Putz The equivalence between matrices and linear maps. -/ import data.matrix.basic import linear_algebra.dimension linear_algebra.tensor_product /-! # Linear maps and matrices This file defines the maps to send matrices to a linear map, and to send linear maps between modules with a finite bases to matrices. This defines a linear equivalence between linear maps between finite-dimensional vector spaces and matrices indexed by the respective bases. Some results are proved about the linear map corresponding to a diagonal matrix (range, ker and rank). ## Main definitions to_lin, to_matrix, lin_equiv_matrix ## Tags linear_map, matrix, linear_equiv, diagonal -/ noncomputable theory open set submodule universes u v variables {l m n : Type u} [fintype l] [fintype m] [fintype n] namespace matrix variables {R : Type v} [comm_ring R] instance [decidable_eq m] [decidable_eq n] (R) [fintype R] : fintype (matrix m n R) := by unfold matrix; apply_instance /-- Evaluation of matrices gives a linear map from matrix m n R to linear maps (n → R) →ₗ[R] (m → R). -/ def eval : (matrix m n R) →ₗ[R] ((n → R) →ₗ[R] (m → R)) := begin refine linear_map.mk₂ R mul_vec _ _ _ _, { assume M N v, funext x, change finset.univ.sum (λy:n, (M x y + N x y) * v y) = _, simp only [_root_.add_mul, finset.sum_add_distrib], refl }, { assume c M v, funext x, change finset.univ.sum (λy:n, (c * M x y) * v y) = _, simp only [_root_.mul_assoc, finset.mul_sum.symm], refl }, { assume M v w, funext x, change finset.univ.sum (λy:n, M x y * (v y + w y)) = _, simp [_root_.mul_add, finset.sum_add_distrib], refl }, { assume c M v, funext x, change finset.univ.sum (λy:n, M x y * (c * v y)) = _, rw [show (λy:n, M x y * (c * v y)) = (λy:n, c * (M x y * v y)), { funext n, ac_refl }, ← finset.mul_sum], refl } end /-- Evaluation of matrices gives a map from matrix m n R to linear maps (n → R) →ₗ[R] (m → R). -/ def to_lin : matrix m n R → (n → R) →ₗ[R] (m → R) := eval.to_fun lemma to_lin_add (M N : matrix m n R) : (M + N).to_lin = M.to_lin + N.to_lin := matrix.eval.map_add M N @[simp] lemma to_lin_zero : (0 : matrix m n R).to_lin = 0 := matrix.eval.map_zero instance to_lin.is_linear_map : @is_linear_map R (matrix m n R) ((n → R) →ₗ[R] (m → R)) _ _ _ _ _ to_lin := matrix.eval.is_linear instance to_lin.is_add_monoid_hom : @is_add_monoid_hom (matrix m n R) ((n → R) →ₗ[R] (m → R)) _ _ to_lin := { map_zero := to_lin_zero, map_add := to_lin_add } @[simp] lemma to_lin_apply (M : matrix m n R) (v : n → R) : (M.to_lin : (n → R) → (m → R)) v = mul_vec M v := rfl lemma mul_to_lin [decidable_eq l] (M : matrix m n R) (N : matrix n l R) : (M.mul N).to_lin = M.to_lin.comp N.to_lin := begin ext v x, simp [to_lin_apply, mul_vec, matrix.mul, finset.sum_mul, finset.mul_sum], rw [finset.sum_comm], congr, funext x, congr, funext y, rw [mul_assoc] end end matrix namespace linear_map variables {R : Type v} [comm_ring R] /-- The linear map from linear maps (n → R) →ₗ[R] (m → R) to matrix m n R. -/ def to_matrixₗ [decidable_eq n] : ((n → R) →ₗ[R] (m → R)) →ₗ[R] matrix m n R := begin refine linear_map.mk (λ f i j, f (λ n, ite (j = n) 1 0) i) _ _, { assume f g, simp only [add_apply], refl }, { assume f g, simp only [smul_apply], refl } end /-- The map from linear maps (n → R) →ₗ[R] (m → R) to matrix m n R. -/ def to_matrix [decidable_eq n] : ((n → R) →ₗ[R] (m → R)) → matrix m n R := to_matrixₗ.to_fun end linear_map section lin_equiv_matrix variables {R : Type v} [comm_ring R] [decidable_eq n] open finsupp matrix linear_map /-- to_lin is the left inverse of to_matrix. -/ lemma to_matrix_to_lin {f : (n → R) →ₗ[R] (m → R)} : to_lin (to_matrix f) = f := begin ext : 1, -- Show that the two sides are equal by showing that they are equal on a basis convert linear_eq_on (set.range _) _ (is_basis.mem_span (@pi.is_basis_fun R n _ _) _), assume e he, rw [@std_basis_eq_single R _ _ _ 1] at he, cases (set.mem_range.mp he) with i h, ext j, change finset.univ.sum (λ k, (f.to_fun (λ l, ite (k = l) 1 0)) j * (e k)) = _, rw [←h], conv_lhs { congr, skip, funext, rw [mul_comm, ←smul_eq_mul, ←pi.smul_apply, ←linear_map.smul], rw [show _ = ite (i = k) (1:R) 0, by convert single_apply], rw [show f.to_fun (ite (i = k) (1:R) 0 • (λ l, ite (k = l) 1 0)) = ite (i = k) (f.to_fun _) 0, { split_ifs, { rw [one_smul] }, { rw [zero_smul], exact linear_map.map_zero f } }] }, convert finset.sum_eq_single i _ _, { rw [if_pos rfl], convert rfl, ext, congr }, { assume _ _ hbi, rw [if_neg $ ne.symm hbi], refl }, { assume hi, exact false.elim (hi $ finset.mem_univ i) } end /-- to_lin is the right inverse of to_matrix. -/ lemma to_lin_to_matrix {M : matrix m n R} : to_matrix (to_lin M) = M := begin ext, change finset.univ.sum (λ y, M i y * ite (j = y) 1 0) = M i j, have h1 : (λ y, M i y * ite (j = y) 1 0) = (λ y, ite (j = y) (M i y) 0), { ext, split_ifs, exact mul_one _, exact ring.mul_zero _ }, have h2 : finset.univ.sum (λ y, ite (j = y) (M i y) 0) = (finset.singleton j).sum (λ y, ite (j = y) (M i y) 0), { refine (finset.sum_subset _ _).symm, { intros _ H, rwa finset.mem_singleton.1 H, exact finset.mem_univ _ }, { exact λ _ _ H, if_neg (mt (finset.mem_singleton.2 ∘ eq.symm) H) } }, rw [h1, h2, finset.sum_singleton], exact if_pos rfl end /-- Linear maps (n → R) →ₗ[R] (m → R) are linearly equivalent to matrix m n R. -/ def lin_equiv_matrix' : ((n → R) →ₗ[R] (m → R)) ≃ₗ[R] matrix m n R := { to_fun := to_matrix, inv_fun := to_lin, right_inv := λ _, to_lin_to_matrix, left_inv := λ _, to_matrix_to_lin, add := to_matrixₗ.add, smul := to_matrixₗ.smul } /-- Given a basis of two modules M₁ and M₂ over a commutative ring R, we get a linear equivalence between linear maps M₁ →ₗ M₂ and matrices over R indexed by the bases. -/ def lin_equiv_matrix {ι κ M₁ M₂ : Type*} [add_comm_group M₁] [module R M₁] [add_comm_group M₂] [module R M₂] [fintype ι] [decidable_eq ι] [fintype κ] [decidable_eq κ] {v₁ : ι → M₁} {v₂ : κ → M₂} (hv₁ : is_basis R v₁) (hv₂ : is_basis R v₂) : (M₁ →ₗ[R] M₂) ≃ₗ[R] matrix κ ι R := linear_equiv.trans (linear_equiv.arrow_congr (equiv_fun_basis hv₁) (equiv_fun_basis hv₂)) lin_equiv_matrix' end lin_equiv_matrix namespace matrix section ring variables {R : Type v} [comm_ring R] open linear_map matrix lemma proj_diagonal [decidable_eq m] (i : m) (w : m → R) : (proj i).comp (to_lin (diagonal w)) = (w i) • proj i := by ext j; simp [mul_vec_diagonal] lemma diagonal_comp_std_basis [decidable_eq n] (w : n → R) (i : n) : (diagonal w).to_lin.comp (std_basis R (λ_:n, R) i) = (w i) • std_basis R (λ_:n, R) i := begin ext a j, simp only [linear_map.comp_apply, smul_apply, to_lin_apply, mul_vec_diagonal, smul_apply, pi.smul_apply, smul_eq_mul], by_cases i = j, { subst h }, { rw [std_basis_ne R (λ_:n, R) _ _ (ne.symm h), _root_.mul_zero, _root_.mul_zero] } end end ring section vector_space variables {K : Type u} [discrete_field K] -- maybe try to relax the universe constraint open linear_map matrix lemma rank_vec_mul_vec [decidable_eq n] (w : m → K) (v : n → K) : rank (vec_mul_vec w v).to_lin ≤ 1 := begin rw [vec_mul_vec_eq, mul_to_lin], refine le_trans (rank_comp_le1 _ _) _, refine le_trans (rank_le_domain _) _, rw [dim_fun', ← cardinal.fintype_card], exact le_refl _ end set_option class.instance_max_depth 100 lemma diagonal_to_lin [decidable_eq m] (w : m → K) : (diagonal w).to_lin = linear_map.pi (λi, w i • linear_map.proj i) := by ext v j; simp [mul_vec_diagonal] lemma ker_diagonal_to_lin [decidable_eq m] (w : m → K) : ker (diagonal w).to_lin = (⨆i∈{i | w i = 0 }, range (std_basis K (λi, K) i)) := begin rw [← comap_bot, ← infi_ker_proj], simp only [comap_infi, (ker_comp _ _).symm, proj_diagonal, ker_smul'], have : univ ⊆ {i : m | w i = 0} ∪ -{i : m | w i = 0}, { rw set.union_compl_self }, exact (supr_range_std_basis_eq_infi_ker_proj K (λi:m, K) (disjoint_compl {i | w i = 0}) this (finite.of_fintype _)).symm end lemma range_diagonal [decidable_eq m] (w : m → K) : (diagonal w).to_lin.range = (⨆ i ∈ {i | w i ≠ 0}, (std_basis K (λi, K) i).range) := begin dsimp only [mem_set_of_eq], rw [← map_top, ← supr_range_std_basis, map_supr], congr, funext i, rw [← linear_map.range_comp, diagonal_comp_std_basis, range_smul'], end lemma rank_diagonal [decidable_eq m] (w : m → K) : rank (diagonal w).to_lin = fintype.card { i // w i ≠ 0 } := begin have hu : univ ⊆ - {i : m | w i = 0} ∪ {i : m | w i = 0}, { rw set.compl_union_self }, have hd : disjoint {i : m | w i ≠ 0} {i : m | w i = 0} := (disjoint_compl {i | w i = 0}).symm, have h₁ := supr_range_std_basis_eq_infi_ker_proj K (λi:m, K) hd hu (finite.of_fintype _), have h₂ := @infi_ker_proj_equiv K _ _ (λi:m, K) _ _ _ _ (by simp; apply_instance) hd hu, rw [rank, range_diagonal, h₁, ←@dim_fun' K], apply linear_equiv.dim_eq, apply h₂, end end vector_space end matrix
a04c50659075c69fbf8a14050c18edc2ecb45fc4
359199d7253811b032ab92108191da7336eba86e
/src/homework/hw4.lean
5e9b1ec3ce3ebff46bbc5fc33c41664ea59cfba0
[]
no_license
arte-et-marte/my_cs2120f21
0bc6215cb5018a3b7c90d9d399a173233f587064
91609c3609ad81fda895bee8b97cc76813241e17
refs/heads/main
1,693,298,928,348
1,634,931,202,000
1,634,931,202,000
399,946,705
0
0
null
null
null
null
UTF-8
Lean
false
false
10,745
lean
-- 1 example : 0 ≠ 1 := begin -- 0 ≠ 1 => ¬(0 = 1), -- ¬(0 = 1) => (0 = 1) → false assume (h : 0 = 1), -- can also use 'trivial' contradiction, -- **COMMENT: You could have also used "cases" on your proof (of something that implies false) to obtain a proof of false.** -- ¬ (0 = 1) -- (0 = 1) → false assume h, trivial, end -- 2 example : 0 ≠ 0 → 2 = 3 := begin -- 0 ≠ 0 => 0 = 0 → false -- **?: 0 ≠ 0 → 2 = 3 => 0 = 0 → false → 2 = 3** assume h, -- have zeqz := eq.refl 0, -- contradiction, -- **COMMENT: Need to give h a proof of 0 = 0 (to get a proof of false).** have f : false := h (eq.refl 0), contradiction, -- **COMMENT: You could have also used "exact false.elim f,".** have zeqz := eq.refl 0, have f : false := h zeqz, exact false.elim (f), end -- **COMMENT: Whether you have a proof of false or you need a proof of false, you may be able to use "contradiction".** -- 3 example : ∀ (P : Prop), P → ¬¬P := -- **COMMENT: Trying to prove that the existence of the proof of P means that there isn't NOT a proof of P.** begin -- can CONSTRUCT a proof of double negation, don't need classical reasoning assume P, assume (p : P), assume h, -- **COMMENT: P → false is a premise to ¬¬P => (P → false) → false.** not not p just means not p implies false, so you can assume p -- **COMMENT: Need to give h a proof of P (to get a proof of false).** have f : false := h p, contradiction, -- **COMMENT: You could have also used "exact f".** end -- We might need classical (vs constructive) reasoning #check classical.em open classical #check em /- axiom em : ∀ (p : Prop), p ∨ ¬p This is the famous and historically controversial "law" (now axiom) of the excluded middle. It's is a key to proving many intuitive theorems in logic and mathematics. But it also leads to giving up on having evidence *why* something is either true or not true, in that you no longer need a proof of either P or of ¬P to have a proof of P ∨ ¬P. -/ -- 4 theorem neg_elim : ∀ (P : Prop), ¬¬P → P := begin assume P, assume h, -- h doesn't help us at all to get a proof of p, all we have left are our standalone props (P and Q), which we can use with the law of em -- ¬¬P => (P → false) → false // em says: there is a proof of ¬P, or there is not (P) have pornp := classical.em P, -- **COMMENT: Applying the axiom of the excluded middle to get a proof of P ∨ ¬P (to get a proof of P).** cases pornp with p np, -- **COMMENT: Performing case analysis (and assigning names to our assumed proofs).** assumption, -- **COMMENT: For the first case - applying our assumed proof of P to get a proof of P.** contradiction, -- **COMMENT: For the second case - pointing out that our axiom is being violated, we have a proof of ¬¬P and ¬P.** end -- 5 theorem demorgan_1 : ∀ (P Q : Prop), ¬(P ∧ Q) ↔ ¬P ∨ ¬Q := begin -- if p and q is false, then at least one of them is false -- if p or q is false, then there's no way p and q is true assume P Q, apply iff.intro _ _, -- could also use split -- forwards assume h, -- stuck? unless we look somewhere other than h... have p_or_np := classical.em P, have q_or_nq := classical.em Q, cases p_or_np with p np, cases q_or_nq with q nq, -- case 1: have pandq := and.intro p q, have f := h pandq, exact false.elim f, -- case 2: exact or.intro_right (¬P) nq, -- case 3: exact or.intro_left (¬Q) np, -- backwards assume h, apply or.elim h, assume np, assume pandq, -- **?** cases pandq with p q, contradiction, -- in the context (have np and p) assume nq, assume pandq, cases pandq with p q, contradiction, -- in the context (have nq and q) assume P Q, split, -- forward assume h, cases (classical.em P) with p np, cases (classical.em Q) with q nq, have pq := and.intro p q, contradiction, exact or.inr nq, exact or.inl np, -- backward admit, end -- 6 theorem demorgan_2 : ∀ (P Q : Prop), ¬(P ∨ Q) → ¬P ∧ ¬Q := theorem demorgan_2 : ∀ (P Q : Prop), ¬ (P ∨ Q) → (¬P ∧ ¬Q) := begin assume P Q, assume h, cases (classical.em P) with p np, cases (classical.em Q) with q nq, -- case 1 have porq := or.intro_left Q p, have f := h porq, exact false.elim f, -- case 2 have porq := or.intro_left Q p, have f := h porq, exact false.elim f, -- case 3 have q_or_nq := classical.em Q, cases q_or_nq with q nq, ---- subcase 1 ---- subcase 2 assume P Q, assume h, have np_or_nnp := classical.em ¬P, have nq_or_nnq := classical.em ¬Q, cases np_or_nnp with np nnp, cases nq_or_nnq with nq nnq, -- case 1: exact and.intro np nq, -- case 2: apply false.elim (h (_)), have q_or_nq := classical.em Q, cases q_or_nq with q nq, ---- subcase 1: exact or.intro_right P q, -- gave a proof of P ∨ Q to h ---- subcase 2: contradiction, -- no proof of P ∨ Q to give to h because there is a contradiction in context -- case 3: apply false.elim (h (_)), have p_or_np := classical.em P, cases p_or_np with p np, ---- subcase 1: exact or.intro_left Q p, ---- subcase 2: contradiction, have porq := or.intro_left Q p, contradiction, have porq := or.intro_left Q p, contradiction, cases (classical.em Q) with q nq, end -- 7 theorem disappearing_opposite : ∀ (P Q : Prop), P ∨ ¬P ∧ Q ↔ P ∨ Q := begin -- note for forwards: need just P to be true or Q to be true for P ∨ Q to be true assume P Q, apply iff.intro _ _, -- forwards assume left, cases left with p np_and_q, ---- case 1: exact or.intro_left Q p, ---- case 2: exact or.intro_right P (and.elim_right np_and_q), -- backwards assume p_or_q, cases p_or_q with p q, -- case 1: exact or.intro_left (¬P ∧ Q) p, -- case 2: have p_or_np := classical.em P, cases p_or_np with p np, ---- subcase 1: exact or.intro_left (¬P ∧ Q) p, ---- subcase 2: apply or.intro_right P _, exact and.intro np q, end -- 8 theorem distrib_and_or : ∀ (P Q R: Prop), (P ∨ Q) ∧ (P ∨ R) ↔ P ∨ (Q ∧ R) := begin -- need either a proof of P or a proof of Q and R (a proof of Q, a proof of R) assume P Q R, apply iff.intro _ _, -- forwards assume left, have p_or_q := and.elim_left left, have p_or_r := and.elim_right left, cases p_or_q with p q, ---- case 1 exact or.intro_left (Q ∧ R) p, ---- case 2 cases p_or_r with p r, ------ subcase 1 exact or.intro_left (Q ∧ R) p, ------ subcase 2 exact or.intro_right P (and.intro q r), -- backwards assume right, -- need a proof of porq AND a proof of porr apply and.intro _ _, apply or.elim right, ---- first goal assume p, exact or.intro_left Q p, ---- second goal assume qandr, exact or.intro_right P (and.elim_left qandr), -- backwards apply or.elim right, ---- first goal assume p, exact or.intro_left R p, ---- second goal assume qandr, exact or.intro_right P (and.elim_right qandr), end -- remember or is right associative -- you need this to know what the lefts and rights are -- 9 theorem distrib_and_or_foil : ∀ (P Q R S : Prop), (P ∨ Q) ∧ (R ∨ S) ↔ (P ∧ R) ∨ (P ∧ S) ∨ (Q ∧ R) ∨ (Q ∧ S) := begin -- ((P ∧ R) ∨ ((P ∧ S) ∨ ((Q ∧ R) ∨ (Q ∧ S)))) assume P Q R S, apply iff.intro _ _, -- Forwards assume porq_and_rors, have porq := and.elim_left porq_and_rors, have rors := and.elim_right porq_and_rors, cases porq with p q, cases rors with r s, -- 1 exact or.intro_left _ (and.intro p r), --2 exact or.intro_right _ (or.intro_left _ (and.intro p s)), --3 cases rors with r s, ---a exact or.intro_right _ (or.intro_right _ (or.intro_left _ (and.intro q r))), ---b exact or.intro_right _ (or.intro_right _ (or.intro_right _ (and.intro q s))), -- Backwards assume supercalifragilisticexpialidocious, apply and.intro _ _, -- first slot cases supercalifragilisticexpialidocious with pandr, ---- case 1 exact or.intro_left Q (and.elim_left pandr), ---- case 2 cases supercalifragilisticexpialidocious with pands, ------ subcase 1 exact or.intro_left Q (and.elim_left pands), ------subcase 2 cases supercalifragilisticexpialidocious with qandr, -------subcase of subcase 1 exact or.intro_right P (and.elim_left qandr), -------subcase of subcase 2 exact or.intro_right P (and.elim_left supercalifragilisticexpialidocious), --- case 3 cases supercalifragilisticexpialidocious with pandr, ---- subcase 1 exact or.intro_left S (and.elim_right pandr), ---- subcase 2 cases supercalifragilisticexpialidocious with pands, ----- subsubcase 1 exact or.intro_right R (and.elim_right pands), ----- subsubcase 2 cases supercalifragilisticexpialidocious with qandr, ------ subsubsubcase 1 exact or.intro_left S (and.elim_right qandr), ------ subsubsubcase 2 exact or.intro_right R (and.elim_right supercalifragilisticexpialidocious), end #check nat.add 3 /- 10 Formally state and prove the proposition that not every natural number is equal to zero. -/ lemma not_all_nats_are_zero : ¬ ∀ (n : ℕ), (n = 0) := begin assume n, have f := n 1, contradiction end -- 11. equivalence of P→Q and (¬P∨Q) example : ∀ (P Q : Prop), (P → Q) ↔ (¬P ∨ Q) := begin assume P Q, apply iff.intro _ _, -- Forwards assume p_imp_q, have p_or_np : P ∨ ¬P := classical.em P, cases p_or_np with p np, ---- First case have q : Q := p_imp_q p, exact or.intro_right (¬P) q, ---- Second case exact or.intro_left Q np, -- Backwards assume np_or_q, cases np_or_q with np q, ---- First case assume p, contradiction, -- in my context! assume p, assumption, end -- 12 example : ∀ (P Q : Prop), (P → Q) → (¬ Q → ¬ P) := begin assume P Q, assume p_imp_q, have p_or_np := classical.em P, have q_or_nq := classical.em Q, cases p_or_np with p np, cases q_or_nq with q nq, -- 1 assume nq, contradiction, -- 2 have q := p_imp_q p, contradiction, -- 3 cases q_or_nq with q nq, ---- a assume nq, contradiction, ---- b assume nq, exact np, end -- 13 example : ∀ (P Q : Prop), ( ¬P → ¬Q) → (Q → P) := begin assume P Q, assume np_imp_nq, have p_or_np := classical.em P, have q_or_nq := classical.em Q, cases p_or_np with p np, cases q_or_nq with q nq, -- 1 assume q, assumption, -- 2 assume q, contradiction, -- 3 cases q_or_nq with q nq, ---- a have np := np_imp_nq np, contradiction, ---- b assume q, contradiction, end end axioms (T : Type) (Q : Prop) (f : ∀ (t : T), Q) (t : T) example : Q := f t #check f
5620d7567a8ea0d305396b9081df7314d98e6f11
367134ba5a65885e863bdc4507601606690974c1
/src/topology/sheaves/presheaf.lean
5319e18c889da91985b9c0a2f4fd4ea550d3f380
[ "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
3,787
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Mario Carneiro, Reid Barton -/ import topology.category.Top.opens /-! # Presheaves on a topological space We define `presheaf C X` simply as `(opens X)ᵒᵖ ⥤ C`, and inherit the category structure with natural transformations as morphisms. We define * `pushforward_obj {X Y : Top.{v}} (f : X ⟶ Y) (ℱ : X.presheaf C) : Y.presheaf C` with notation `f _* ℱ` and for `ℱ : X.presheaf C` provide the natural isomorphisms * `pushforward.id : (𝟙 X) _* ℱ ≅ ℱ`` * `pushforward.comp : (f ≫ g) _* ℱ ≅ g _* (f _* ℱ)` along with their `@[simp]` lemmas. -/ universes v u open category_theory open topological_space open opposite variables (C : Type u) [category.{v} C] namespace Top @[derive category] def presheaf (X : Top.{v}) := (opens X)ᵒᵖ ⥤ C variables {C} namespace presheaf /-- Pushforward a presheaf on `X` along a continuous map `f : X ⟶ Y`, obtaining a presheaf on `Y`. -/ def pushforward_obj {X Y : Top.{v}} (f : X ⟶ Y) (ℱ : X.presheaf C) : Y.presheaf C := (opens.map f).op ⋙ ℱ infix ` _* `: 80 := pushforward_obj @[simp] lemma pushforward_obj_obj {X Y : Top.{v}} (f : X ⟶ Y) (ℱ : X.presheaf C) (U : (opens Y)ᵒᵖ) : (f _* ℱ).obj U = ℱ.obj ((opens.map f).op.obj U) := rfl @[simp] lemma pushforward_obj_map {X Y : Top.{v}} (f : X ⟶ Y) (ℱ : X.presheaf C) {U V : (opens Y)ᵒᵖ} (i : U ⟶ V) : (f _* ℱ).map i = ℱ.map ((opens.map f).op.map i) := rfl def pushforward_eq {X Y : Top.{v}} {f g : X ⟶ Y} (h : f = g) (ℱ : X.presheaf C) : f _* ℱ ≅ g _* ℱ := iso_whisker_right (nat_iso.op (opens.map_iso f g h).symm) ℱ @[simp] lemma pushforward_eq_hom_app {X Y : Top.{v}} {f g : X ⟶ Y} (h : f = g) (ℱ : X.presheaf C) (U) : (pushforward_eq h ℱ).hom.app U = ℱ.map (begin dsimp [functor.op], apply has_hom.hom.op, apply eq_to_hom, rw h, end) := rfl @[simp] lemma pushforward_eq_rfl {X Y : Top.{v}} (f : X ⟶ Y) (ℱ : X.presheaf C) (U) : (pushforward_eq (rfl : f = f) ℱ).hom.app (op U) = 𝟙 _ := begin dsimp [pushforward_eq], erw ℱ.map_id, end lemma pushforward_eq_eq {X Y : Top.{v}} {f g : X ⟶ Y} (h₁ h₂ : f = g) (ℱ : X.presheaf C) : ℱ.pushforward_eq h₁ = ℱ.pushforward_eq h₂ := rfl namespace pushforward variables {X : Top.{v}} (ℱ : X.presheaf C) def id : (𝟙 X) _* ℱ ≅ ℱ := (iso_whisker_right (nat_iso.op (opens.map_id X).symm) ℱ) ≪≫ functor.left_unitor _ @[simp] lemma id_hom_app' (U) (p) : (id ℱ).hom.app (op ⟨U, p⟩) = ℱ.map (𝟙 (op ⟨U, p⟩)) := by { dsimp [id], simp, } local attribute [tidy] tactic.op_induction' @[simp, priority 990] lemma id_hom_app (U) : (id ℱ).hom.app U = ℱ.map (eq_to_hom (opens.op_map_id_obj U)) := by tidy @[simp] lemma id_inv_app' (U) (p) : (id ℱ).inv.app (op ⟨U, p⟩) = ℱ.map (𝟙 (op ⟨U, p⟩)) := by { dsimp [id], simp, } def comp {Y Z : Top.{v}} (f : X ⟶ Y) (g : Y ⟶ Z) : (f ≫ g) _* ℱ ≅ g _* (f _* ℱ) := iso_whisker_right (nat_iso.op (opens.map_comp f g).symm) ℱ @[simp] lemma comp_hom_app {Y Z : Top.{v}} (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (comp ℱ f g).hom.app U = 𝟙 _ := by { dsimp [comp], tidy, } @[simp] lemma comp_inv_app {Y Z : Top.{v}} (f : X ⟶ Y) (g : Y ⟶ Z) (U) : (comp ℱ f g).inv.app U = 𝟙 _ := by { dsimp [comp], tidy, } end pushforward /-- A morphism of presheaves gives rise to a morphisms of the pushforwards of those presheaves. -/ @[simps] def pushforward_map {X Y : Top.{v}} (f : X ⟶ Y) {ℱ 𝒢 : X.presheaf C} (α : ℱ ⟶ 𝒢) : f _* ℱ ⟶ f _* 𝒢 := { app := λ U, α.app _, naturality' := λ U V i, by { erw α.naturality, refl, } } end presheaf end Top
210ef4a46d7f8987e5bc7edce67d730bb9232ec3
5e3548e65f2c037cb94cd5524c90c623fbd6d46a
/src_icannos_totilas/aops/2002-USAMO-Problem_4.lean
a74f0ea626e406198cc63b6974791c2e864e1f46
[]
no_license
ahayat16/lean_exos
d4f08c30adb601a06511a71b5ffb4d22d12ef77f
682f2552d5b04a8c8eb9e4ab15f875a91b03845c
refs/heads/main
1,693,101,073,585
1,636,479,336,000
1,636,479,336,000
415,000,441
0
0
null
null
null
null
UTF-8
Lean
false
false
176
lean
import data.real.basic theorem USAMO_Problem_4_2002 (f : ℝ → ℝ) : (∀ x y : ℝ, f(x^2 - y^2) = x*f(x) - y * f(y)) → ∃ l : ℝ, ∀ x : ℝ, f(x) = l*x := sorry
85b8bbe50f9c021c4c4a81ec6713dad92e0daa34
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/ring/idempotents.lean
912b8de75f6de96a8cb49b19cca4b3deb4ad9cee
[ "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,883
lean
/- Copyright (c) 2022 Christopher Hoskin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Christopher Hoskin -/ import order.basic import algebra.group_power.basic import algebra.ring.defs import tactic.nth_rewrite /-! # Idempotents This file defines idempotents for an arbitary multiplication and proves some basic results, including: * `is_idempotent_elem.mul_of_commute`: In a semigroup, the product of two commuting idempotents is an idempotent; * `is_idempotent_elem.one_sub_iff`: In a (non-associative) ring, `p` is an idempotent if and only if `1-p` is an idempotent. * `is_idempotent_elem.pow_succ_eq`: In a monoid `p ^ (n+1) = p` for `p` an idempotent and `n` a natural number. ## Tags projection, idempotent -/ variables {M N S M₀ M₁ R G G₀ : Type*} variables [has_mul M] [monoid N] [semigroup S] [mul_zero_class M₀] [mul_one_class M₁] [non_assoc_ring R] [group G] [cancel_monoid_with_zero G₀] /-- An element `p` is said to be idempotent if `p * p = p` -/ def is_idempotent_elem (p : M) : Prop := p * p = p namespace is_idempotent_elem lemma of_is_idempotent [is_idempotent M (*)] (a : M) : is_idempotent_elem a := is_idempotent.idempotent a lemma eq {p : M} (h : is_idempotent_elem p) : p * p = p := h lemma mul_of_commute {p q : S} (h : commute p q) (h₁ : is_idempotent_elem p) (h₂ : is_idempotent_elem q) : is_idempotent_elem (p * q) := by rw [is_idempotent_elem, mul_assoc, ← mul_assoc q, ← h.eq, mul_assoc p, h₂.eq, ← mul_assoc, h₁.eq] lemma zero : is_idempotent_elem (0 : M₀) := mul_zero _ lemma one : is_idempotent_elem (1 : M₁) := mul_one _ lemma one_sub {p : R} (h : is_idempotent_elem p) : is_idempotent_elem (1 - p) := by rw [is_idempotent_elem, mul_sub, mul_one, sub_mul, one_mul, h.eq, sub_self, sub_zero] @[simp] lemma one_sub_iff {p : R} : is_idempotent_elem (1 - p) ↔ is_idempotent_elem p := ⟨ λ h, sub_sub_cancel 1 p ▸ h.one_sub, is_idempotent_elem.one_sub ⟩ lemma pow {p : N} (n : ℕ) (h : is_idempotent_elem p) : is_idempotent_elem (p ^ n) := nat.rec_on n ((pow_zero p).symm ▸ one) (λ n ih, show p ^ n.succ * p ^ n.succ = p ^ n.succ, by { nth_rewrite 2 ←h.eq, rw [←sq, ←sq, ←pow_mul, ←pow_mul'] }) lemma pow_succ_eq {p : N} (n : ℕ) (h : is_idempotent_elem p) : p ^ (n + 1) = p := nat.rec_on n ((nat.zero_add 1).symm ▸ pow_one p) (λ n ih, by rw [pow_succ, ih, h.eq]) @[simp] lemma iff_eq_one {p : G} : is_idempotent_elem p ↔ p = 1 := iff.intro (λ h, mul_left_cancel ((mul_one p).symm ▸ h.eq : p * p = p * 1)) (λ h, h.symm ▸ one) @[simp] lemma iff_eq_zero_or_one {p : G₀} : is_idempotent_elem p ↔ p = 0 ∨ p = 1 := begin refine iff.intro (λ h, or_iff_not_imp_left.mpr (λ hp, _)) (λ h, h.elim (λ hp, hp.symm ▸ zero) (λ hp, hp.symm ▸ one)), exact mul_left_cancel₀ hp (h.trans (mul_one p).symm) end /-! ### Instances on `subtype is_idempotent_elem` -/ section instances instance : has_zero { p : M₀ // is_idempotent_elem p } := { zero := ⟨ 0, zero ⟩ } @[simp] lemma coe_zero : ↑(0 : {p : M₀ // is_idempotent_elem p}) = (0 : M₀) := rfl instance : has_one { p : M₁ // is_idempotent_elem p } := { one := ⟨ 1, one ⟩ } @[simp] lemma coe_one : ↑(1 : { p : M₁ // is_idempotent_elem p }) = (1 : M₁) := rfl instance : has_compl { p : R // is_idempotent_elem p } := ⟨λ p, ⟨1 - p, p.prop.one_sub⟩⟩ @[simp] lemma coe_compl (p : { p : R // is_idempotent_elem p }) : ↑(pᶜ) = (1 : R) - ↑p := rfl @[simp] lemma compl_compl (p : {p : R // is_idempotent_elem p}) : pᶜᶜ = p := subtype.ext $ sub_sub_cancel _ _ @[simp] lemma zero_compl : (0 : {p : R // is_idempotent_elem p})ᶜ = 1 := subtype.ext $ sub_zero _ @[simp] lemma one_compl : (1 : {p : R // is_idempotent_elem p})ᶜ = 0 := subtype.ext $ sub_self _ end instances end is_idempotent_elem
cb3edbd39d7b3c9747d3e9ff1f6f67e158071ca6
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/data/nat/gcd.lean
4b92ffd5474f19e35ecada477594dbc4c381e69c
[ "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
22,316
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura -/ import algebra.group_power.order /-! # Definitions and properties of `gcd`, `lcm`, and `coprime` -/ namespace nat /-! ### `gcd` -/ theorem gcd_dvd (m n : ℕ) : (gcd m n ∣ m) ∧ (gcd m n ∣ n) := gcd.induction m n (λn, by rw gcd_zero_left; exact ⟨dvd_zero n, dvd_refl n⟩) (λm n npos, by rw ←gcd_rec; exact λ ⟨IH₁, IH₂⟩, ⟨IH₂, (dvd_mod_iff IH₂).1 IH₁⟩) theorem gcd_dvd_left (m n : ℕ) : gcd m n ∣ m := (gcd_dvd m n).left theorem gcd_dvd_right (m n : ℕ) : gcd m n ∣ n := (gcd_dvd m n).right theorem gcd_le_left {m} (n) (h : 0 < m) : gcd m n ≤ m := le_of_dvd h $ gcd_dvd_left m n theorem gcd_le_right (m) {n} (h : 0 < n) : gcd m n ≤ n := le_of_dvd h $ gcd_dvd_right m n theorem dvd_gcd {m n k : ℕ} : k ∣ m → k ∣ n → k ∣ gcd m n := gcd.induction m n (λn _ kn, by rw gcd_zero_left; exact kn) (λn m mpos IH H1 H2, by rw gcd_rec; exact IH ((dvd_mod_iff H1).2 H2) H1) theorem dvd_gcd_iff {m n k : ℕ} : k ∣ gcd m n ↔ k ∣ m ∧ k ∣ n := iff.intro (λ h, ⟨h.trans (gcd_dvd m n).left, h.trans (gcd_dvd m n).right⟩) (λ h, dvd_gcd h.left h.right) theorem gcd_comm (m n : ℕ) : gcd m n = gcd n m := dvd_antisymm (dvd_gcd (gcd_dvd_right m n) (gcd_dvd_left m n)) (dvd_gcd (gcd_dvd_right n m) (gcd_dvd_left n m)) theorem gcd_eq_left_iff_dvd {m n : ℕ} : m ∣ n ↔ gcd m n = m := ⟨λ h, by rw [gcd_rec, mod_eq_zero_of_dvd h, gcd_zero_left], λ h, h ▸ gcd_dvd_right m n⟩ theorem gcd_eq_right_iff_dvd {m n : ℕ} : m ∣ n ↔ gcd n m = m := by rw gcd_comm; apply gcd_eq_left_iff_dvd theorem gcd_assoc (m n k : ℕ) : gcd (gcd m n) k = gcd m (gcd n k) := dvd_antisymm (dvd_gcd ((gcd_dvd_left (gcd m n) k).trans (gcd_dvd_left m n)) (dvd_gcd ((gcd_dvd_left (gcd m n) k).trans (gcd_dvd_right m n)) (gcd_dvd_right (gcd m n) k))) (dvd_gcd (dvd_gcd (gcd_dvd_left m (gcd n k)) ((gcd_dvd_right m (gcd n k)).trans (gcd_dvd_left n k))) ((gcd_dvd_right m (gcd n k)).trans (gcd_dvd_right n k))) @[simp] theorem gcd_one_right (n : ℕ) : gcd n 1 = 1 := eq.trans (gcd_comm n 1) $ gcd_one_left n theorem gcd_mul_left (m n k : ℕ) : gcd (m * n) (m * k) = m * gcd n k := gcd.induction n k (λk, by repeat {rw mul_zero <|> rw gcd_zero_left}) (λk n H IH, by rwa [←mul_mod_mul_left, ←gcd_rec, ←gcd_rec] at IH) theorem gcd_mul_right (m n k : ℕ) : gcd (m * n) (k * n) = gcd m k * n := by rw [mul_comm m n, mul_comm k n, mul_comm (gcd m k) n, gcd_mul_left] theorem gcd_pos_of_pos_left {m : ℕ} (n : ℕ) (mpos : 0 < m) : 0 < gcd m n := pos_of_dvd_of_pos (gcd_dvd_left m n) mpos theorem gcd_pos_of_pos_right (m : ℕ) {n : ℕ} (npos : 0 < n) : 0 < gcd m n := pos_of_dvd_of_pos (gcd_dvd_right m n) npos theorem eq_zero_of_gcd_eq_zero_left {m n : ℕ} (H : gcd m n = 0) : m = 0 := or.elim (nat.eq_zero_or_pos m) id (assume H1 : 0 < m, absurd (eq.symm H) (ne_of_lt (gcd_pos_of_pos_left _ H1))) theorem eq_zero_of_gcd_eq_zero_right {m n : ℕ} (H : gcd m n = 0) : n = 0 := by rw gcd_comm at H; exact eq_zero_of_gcd_eq_zero_left H @[simp] theorem gcd_eq_zero_iff {i j : ℕ} : gcd i j = 0 ↔ i = 0 ∧ j = 0 := begin split, { intro h, exact ⟨eq_zero_of_gcd_eq_zero_left h, eq_zero_of_gcd_eq_zero_right h⟩, }, { rintro ⟨rfl, rfl⟩, exact nat.gcd_zero_right 0 } end theorem gcd_div {m n k : ℕ} (H1 : k ∣ m) (H2 : k ∣ n) : gcd (m / k) (n / k) = gcd m n / k := or.elim (nat.eq_zero_or_pos k) (λk0, by rw [k0, nat.div_zero, nat.div_zero, nat.div_zero, gcd_zero_right]) (λH3, nat.eq_of_mul_eq_mul_right H3 $ by rw [ nat.div_mul_cancel (dvd_gcd H1 H2), ←gcd_mul_right, nat.div_mul_cancel H1, nat.div_mul_cancel H2]) theorem gcd_greatest {a b d : ℕ} (hda : d ∣ a) (hdb : d ∣ b) (hd : ∀ e : ℕ, e ∣ a → e ∣ b → e ∣ d) : d = a.gcd b := (dvd_antisymm (hd _ (gcd_dvd_left a b) (gcd_dvd_right a b)) (dvd_gcd hda hdb)).symm theorem gcd_dvd_gcd_of_dvd_left {m k : ℕ} (n : ℕ) (H : m ∣ k) : gcd m n ∣ gcd k n := dvd_gcd ((gcd_dvd_left m n).trans H) (gcd_dvd_right m n) theorem gcd_dvd_gcd_of_dvd_right {m k : ℕ} (n : ℕ) (H : m ∣ k) : gcd n m ∣ gcd n k := dvd_gcd (gcd_dvd_left n m) ((gcd_dvd_right n m).trans H) theorem gcd_dvd_gcd_mul_left (m n k : ℕ) : gcd m n ∣ gcd (k * m) n := gcd_dvd_gcd_of_dvd_left _ (dvd_mul_left _ _) theorem gcd_dvd_gcd_mul_right (m n k : ℕ) : gcd m n ∣ gcd (m * k) n := gcd_dvd_gcd_of_dvd_left _ (dvd_mul_right _ _) theorem gcd_dvd_gcd_mul_left_right (m n k : ℕ) : gcd m n ∣ gcd m (k * n) := gcd_dvd_gcd_of_dvd_right _ (dvd_mul_left _ _) theorem gcd_dvd_gcd_mul_right_right (m n k : ℕ) : gcd m n ∣ gcd m (n * k) := gcd_dvd_gcd_of_dvd_right _ (dvd_mul_right _ _) theorem gcd_eq_left {m n : ℕ} (H : m ∣ n) : gcd m n = m := dvd_antisymm (gcd_dvd_left _ _) (dvd_gcd dvd_rfl H) theorem gcd_eq_right {m n : ℕ} (H : n ∣ m) : gcd m n = n := by rw [gcd_comm, gcd_eq_left H] -- Lemmas where one argument is a multiple of the other @[simp] lemma gcd_mul_left_left (m n : ℕ) : gcd (m * n) n = n := dvd_antisymm (gcd_dvd_right _ _) (dvd_gcd (dvd_mul_left _ _) dvd_rfl) @[simp] lemma gcd_mul_left_right (m n : ℕ) : gcd n (m * n) = n := by rw [gcd_comm, gcd_mul_left_left] @[simp] lemma gcd_mul_right_left (m n : ℕ) : gcd (n * m) n = n := by rw [mul_comm, gcd_mul_left_left] @[simp] lemma gcd_mul_right_right (m n : ℕ) : gcd n (n * m) = n := by rw [gcd_comm, gcd_mul_right_left] -- Lemmas for repeated application of `gcd` @[simp] lemma gcd_gcd_self_right_left (m n : ℕ) : gcd m (gcd m n) = gcd m n := dvd_antisymm (gcd_dvd_right _ _) (dvd_gcd (gcd_dvd_left _ _) dvd_rfl) @[simp] lemma gcd_gcd_self_right_right (m n : ℕ) : gcd m (gcd n m) = gcd n m := by rw [gcd_comm n m, gcd_gcd_self_right_left] @[simp] lemma gcd_gcd_self_left_right (m n : ℕ) : gcd (gcd n m) m = gcd n m := by rw [gcd_comm, gcd_gcd_self_right_right] @[simp] lemma gcd_gcd_self_left_left (m n : ℕ) : gcd (gcd m n) m = gcd m n := by rw [gcd_comm m n, gcd_gcd_self_left_right] -- Lemmas where one argument consists of addition of a multiple of the other @[simp] lemma gcd_add_mul_right_right (m n k : ℕ) : gcd m (n + k * m) = gcd m n := by simp [gcd_rec m (n + k * m), gcd_rec m n] @[simp] lemma gcd_add_mul_left_right (m n k : ℕ) : gcd m (n + m * k) = gcd m n := by simp [gcd_rec m (n + m * k), gcd_rec m n] @[simp] lemma gcd_mul_right_add_right (m n k : ℕ) : gcd m (k * m + n) = gcd m n := by simp [add_comm _ n] @[simp] lemma gcd_mul_left_add_right (m n k : ℕ) : gcd m (m * k + n) = gcd m n := by simp [add_comm _ n] @[simp] lemma gcd_add_mul_right_left (m n k : ℕ) : gcd (m + k * n) n = gcd m n := by rw [gcd_comm, gcd_add_mul_right_right, gcd_comm] @[simp] lemma gcd_add_mul_left_left (m n k : ℕ) : gcd (m + n * k) n = gcd m n := by rw [gcd_comm, gcd_add_mul_left_right, gcd_comm] @[simp] lemma gcd_mul_right_add_left (m n k : ℕ) : gcd (k * n + m) n = gcd m n := by rw [gcd_comm, gcd_mul_right_add_right, gcd_comm] @[simp] lemma gcd_mul_left_add_left (m n k : ℕ) : gcd (n * k + m) n = gcd m n := by rw [gcd_comm, gcd_mul_left_add_right, gcd_comm] -- Lemmas where one argument consists of an addition of the other @[simp] lemma gcd_add_self_right (m n : ℕ) : gcd m (n + m) = gcd m n := eq.trans (by rw one_mul) (gcd_add_mul_right_right m n 1) @[simp] lemma gcd_add_self_left (m n : ℕ) : gcd (m + n) n = gcd m n := by rw [gcd_comm, gcd_add_self_right, gcd_comm] @[simp] lemma gcd_self_add_left (m n : ℕ) : gcd (m + n) m = gcd n m := by rw [add_comm, gcd_add_self_left] @[simp] lemma gcd_self_add_right (m n : ℕ) : gcd m (m + n) = gcd m n := by rw [add_comm, gcd_add_self_right] /-! ### `lcm` -/ theorem lcm_comm (m n : ℕ) : lcm m n = lcm n m := by delta lcm; rw [mul_comm, gcd_comm] @[simp] theorem lcm_zero_left (m : ℕ) : lcm 0 m = 0 := by delta lcm; rw [zero_mul, nat.zero_div] @[simp] theorem lcm_zero_right (m : ℕ) : lcm m 0 = 0 := lcm_comm 0 m ▸ lcm_zero_left m @[simp] theorem lcm_one_left (m : ℕ) : lcm 1 m = m := by delta lcm; rw [one_mul, gcd_one_left, nat.div_one] @[simp] theorem lcm_one_right (m : ℕ) : lcm m 1 = m := lcm_comm 1 m ▸ lcm_one_left m @[simp] theorem lcm_self (m : ℕ) : lcm m m = m := or.elim (nat.eq_zero_or_pos m) (λh, by rw [h, lcm_zero_left]) (λh, by delta lcm; rw [gcd_self, nat.mul_div_cancel _ h]) theorem dvd_lcm_left (m n : ℕ) : m ∣ lcm m n := dvd.intro (n / gcd m n) (nat.mul_div_assoc _ $ gcd_dvd_right m n).symm theorem dvd_lcm_right (m n : ℕ) : n ∣ lcm m n := lcm_comm n m ▸ dvd_lcm_left n m theorem gcd_mul_lcm (m n : ℕ) : gcd m n * lcm m n = m * n := by delta lcm; rw [nat.mul_div_cancel' ((gcd_dvd_left m n).trans (dvd_mul_right m n))] theorem lcm_dvd {m n k : ℕ} (H1 : m ∣ k) (H2 : n ∣ k) : lcm m n ∣ k := or.elim (nat.eq_zero_or_pos k) (λh, by rw h; exact dvd_zero _) (λkpos, dvd_of_mul_dvd_mul_left (gcd_pos_of_pos_left n (pos_of_dvd_of_pos H1 kpos)) $ by rw [gcd_mul_lcm, ←gcd_mul_right, mul_comm n k]; exact dvd_gcd (mul_dvd_mul_left _ H2) (mul_dvd_mul_right H1 _)) theorem lcm_dvd_mul (m n : ℕ) : lcm m n ∣ m * n := lcm_dvd (dvd_mul_right _ _) (dvd_mul_left _ _) lemma lcm_dvd_iff {m n k : ℕ} : lcm m n ∣ k ↔ m ∣ k ∧ n ∣ k := ⟨λ h, ⟨(dvd_lcm_left _ _).trans h, (dvd_lcm_right _ _).trans h⟩, and_imp.2 lcm_dvd⟩ theorem lcm_assoc (m n k : ℕ) : lcm (lcm m n) k = lcm m (lcm n k) := dvd_antisymm (lcm_dvd (lcm_dvd (dvd_lcm_left m (lcm n k)) ((dvd_lcm_left n k).trans (dvd_lcm_right m (lcm n k)))) ((dvd_lcm_right n k).trans (dvd_lcm_right m (lcm n k)))) (lcm_dvd ((dvd_lcm_left m n).trans (dvd_lcm_left (lcm m n) k)) (lcm_dvd ((dvd_lcm_right m n).trans (dvd_lcm_left (lcm m n) k)) (dvd_lcm_right (lcm m n) k))) theorem lcm_ne_zero {m n : ℕ} (hm : m ≠ 0) (hn : n ≠ 0) : lcm m n ≠ 0 := by { intro h, simpa [h, hm, hn] using gcd_mul_lcm m n, } /-! ### `coprime` See also `nat.coprime_of_dvd` and `nat.coprime_of_dvd'` to prove `nat.coprime m n`. -/ instance (m n : ℕ) : decidable (coprime m n) := by unfold coprime; apply_instance theorem coprime_iff_gcd_eq_one {m n : ℕ} : coprime m n ↔ gcd m n = 1 := iff.rfl theorem coprime.gcd_eq_one {m n : ℕ} (h : coprime m n) : gcd m n = 1 := h theorem coprime.lcm_eq_mul {m n : ℕ} (h : coprime m n) : lcm m n = m * n := by rw [←one_mul (lcm m n), ←h.gcd_eq_one, gcd_mul_lcm] theorem coprime.symm {m n : ℕ} : coprime n m → coprime m n := (gcd_comm m n).trans theorem coprime_comm {m n : ℕ} : coprime n m ↔ coprime m n := ⟨coprime.symm, coprime.symm⟩ theorem coprime.dvd_of_dvd_mul_right {m n k : ℕ} (H1 : coprime k n) (H2 : k ∣ m * n) : k ∣ m := let t := dvd_gcd (dvd_mul_left k m) H2 in by rwa [gcd_mul_left, H1.gcd_eq_one, mul_one] at t theorem coprime.dvd_of_dvd_mul_left {m n k : ℕ} (H1 : coprime k m) (H2 : k ∣ m * n) : k ∣ n := by rw mul_comm at H2; exact H1.dvd_of_dvd_mul_right H2 theorem coprime.dvd_mul_right {m n k : ℕ} (H : coprime k n) : k ∣ m * n ↔ k ∣ m := ⟨H.dvd_of_dvd_mul_right, λ h, dvd_mul_of_dvd_left h n⟩ theorem coprime.dvd_mul_left {m n k : ℕ} (H : coprime k m) : k ∣ m * n ↔ k ∣ n := ⟨H.dvd_of_dvd_mul_left, λ h, dvd_mul_of_dvd_right h m⟩ theorem coprime.gcd_mul_left_cancel {k : ℕ} (m : ℕ) {n : ℕ} (H : coprime k n) : gcd (k * m) n = gcd m n := have H1 : coprime (gcd (k * m) n) k, by rw [coprime, gcd_assoc, H.symm.gcd_eq_one, gcd_one_right], dvd_antisymm (dvd_gcd (H1.dvd_of_dvd_mul_left (gcd_dvd_left _ _)) (gcd_dvd_right _ _)) (gcd_dvd_gcd_mul_left _ _ _) theorem coprime.gcd_mul_right_cancel (m : ℕ) {k n : ℕ} (H : coprime k n) : gcd (m * k) n = gcd m n := by rw [mul_comm m k, H.gcd_mul_left_cancel m] theorem coprime.gcd_mul_left_cancel_right {k m : ℕ} (n : ℕ) (H : coprime k m) : gcd m (k * n) = gcd m n := by rw [gcd_comm m n, gcd_comm m (k * n), H.gcd_mul_left_cancel n] theorem coprime.gcd_mul_right_cancel_right {k m : ℕ} (n : ℕ) (H : coprime k m) : gcd m (n * k) = gcd m n := by rw [mul_comm n k, H.gcd_mul_left_cancel_right n] theorem coprime_div_gcd_div_gcd {m n : ℕ} (H : 0 < gcd m n) : coprime (m / gcd m n) (n / gcd m n) := by rw [coprime_iff_gcd_eq_one, gcd_div (gcd_dvd_left m n) (gcd_dvd_right m n), nat.div_self H] theorem not_coprime_of_dvd_of_dvd {m n d : ℕ} (dgt1 : 1 < d) (Hm : d ∣ m) (Hn : d ∣ n) : ¬ coprime m n := λ co, not_lt_of_ge (le_of_dvd zero_lt_one $ by rw [←co.gcd_eq_one]; exact dvd_gcd Hm Hn) dgt1 theorem exists_coprime {m n : ℕ} (H : 0 < gcd m n) : ∃ m' n', coprime m' n' ∧ m = m' * gcd m n ∧ n = n' * gcd m n := ⟨_, _, coprime_div_gcd_div_gcd H, (nat.div_mul_cancel (gcd_dvd_left m n)).symm, (nat.div_mul_cancel (gcd_dvd_right m n)).symm⟩ theorem exists_coprime' {m n : ℕ} (H : 0 < gcd m n) : ∃ g m' n', 0 < g ∧ coprime m' n' ∧ m = m' * g ∧ n = n' * g := let ⟨m', n', h⟩ := exists_coprime H in ⟨_, m', n', H, h⟩ @[simp] theorem coprime_add_self_right {m n : ℕ} : coprime m (n + m) ↔ coprime m n := by rw [coprime, coprime, gcd_add_self_right] @[simp] theorem coprime_self_add_right {m n : ℕ} : coprime m (m + n) ↔ coprime m n := by rw [add_comm, coprime_add_self_right] @[simp] theorem coprime_add_self_left {m n : ℕ} : coprime (m + n) n ↔ coprime m n := by rw [coprime, coprime, gcd_add_self_left] @[simp] theorem coprime_self_add_left {m n : ℕ} : coprime (m + n) m ↔ coprime n m := by rw [coprime, coprime, gcd_self_add_left] @[simp] lemma coprime_add_mul_right_right (m n k : ℕ) : coprime m (n + k * m) ↔ coprime m n := by rw [coprime, coprime, gcd_add_mul_right_right] @[simp] lemma coprime_add_mul_left_right (m n k : ℕ) : coprime m (n + m * k) ↔ coprime m n := by rw [coprime, coprime, gcd_add_mul_left_right] @[simp] lemma coprime_mul_right_add_right (m n k : ℕ) : coprime m (k * m + n) ↔ coprime m n := by rw [coprime, coprime, gcd_mul_right_add_right] @[simp] lemma coprime_mul_left_add_right (m n k : ℕ) : coprime m (m * k + n) ↔ coprime m n := by rw [coprime, coprime, gcd_mul_left_add_right] @[simp] lemma coprime_add_mul_right_left (m n k : ℕ) : coprime (m + k * n) n ↔ coprime m n := by rw [coprime, coprime, gcd_add_mul_right_left] @[simp] lemma coprime_add_mul_left_left (m n k : ℕ) : coprime (m + n * k) n ↔ coprime m n := by rw [coprime, coprime, gcd_add_mul_left_left] @[simp] lemma coprime_mul_right_add_left (m n k : ℕ) : coprime (k * n + m) n ↔ coprime m n := by rw [coprime, coprime, gcd_mul_right_add_left] @[simp] lemma coprime_mul_left_add_left (m n k : ℕ) : coprime (n * k + m) n ↔ coprime m n := by rw [coprime, coprime, gcd_mul_left_add_left] theorem coprime.mul {m n k : ℕ} (H1 : coprime m k) (H2 : coprime n k) : coprime (m * n) k := (H1.gcd_mul_left_cancel n).trans H2 theorem coprime.mul_right {k m n : ℕ} (H1 : coprime k m) (H2 : coprime k n) : coprime k (m * n) := (H1.symm.mul H2.symm).symm theorem coprime.coprime_dvd_left {m k n : ℕ} (H1 : m ∣ k) (H2 : coprime k n) : coprime m n := eq_one_of_dvd_one (by delta coprime at H2; rw ← H2; exact gcd_dvd_gcd_of_dvd_left _ H1) theorem coprime.coprime_dvd_right {m k n : ℕ} (H1 : n ∣ m) (H2 : coprime k m) : coprime k n := (H2.symm.coprime_dvd_left H1).symm theorem coprime.coprime_mul_left {k m n : ℕ} (H : coprime (k * m) n) : coprime m n := H.coprime_dvd_left (dvd_mul_left _ _) theorem coprime.coprime_mul_right {k m n : ℕ} (H : coprime (m * k) n) : coprime m n := H.coprime_dvd_left (dvd_mul_right _ _) theorem coprime.coprime_mul_left_right {k m n : ℕ} (H : coprime m (k * n)) : coprime m n := H.coprime_dvd_right (dvd_mul_left _ _) theorem coprime.coprime_mul_right_right {k m n : ℕ} (H : coprime m (n * k)) : coprime m n := H.coprime_dvd_right (dvd_mul_right _ _) theorem coprime.coprime_div_left {m n a : ℕ} (cmn : coprime m n) (dvd : a ∣ m) : coprime (m / a) n := begin by_cases a_split : (a = 0), { subst a_split, rw zero_dvd_iff at dvd, simpa [dvd] using cmn, }, { rcases dvd with ⟨k, rfl⟩, rw nat.mul_div_cancel_left _ (nat.pos_of_ne_zero a_split), exact coprime.coprime_mul_left cmn, }, end theorem coprime.coprime_div_right {m n a : ℕ} (cmn : coprime m n) (dvd : a ∣ n) : coprime m (n / a) := (coprime.coprime_div_left cmn.symm dvd).symm lemma coprime_mul_iff_left {k m n : ℕ} : coprime (m * n) k ↔ coprime m k ∧ coprime n k := ⟨λ h, ⟨coprime.coprime_mul_right h, coprime.coprime_mul_left h⟩, λ ⟨h, _⟩, by rwa [coprime_iff_gcd_eq_one, coprime.gcd_mul_left_cancel n h]⟩ lemma coprime_mul_iff_right {k m n : ℕ} : coprime k (m * n) ↔ coprime k m ∧ coprime k n := by simpa only [coprime_comm] using coprime_mul_iff_left lemma coprime.gcd_left (k : ℕ) {m n : ℕ} (hmn : coprime m n) : coprime (gcd k m) n := hmn.coprime_dvd_left $ gcd_dvd_right k m lemma coprime.gcd_right (k : ℕ) {m n : ℕ} (hmn : coprime m n) : coprime m (gcd k n) := hmn.coprime_dvd_right $ gcd_dvd_right k n lemma coprime.gcd_both (k l : ℕ) {m n : ℕ} (hmn : coprime m n) : coprime (gcd k m) (gcd l n) := (hmn.gcd_left k).gcd_right l lemma coprime.mul_dvd_of_dvd_of_dvd {a n m : ℕ} (hmn : coprime m n) (hm : m ∣ a) (hn : n ∣ a) : m * n ∣ a := let ⟨k, hk⟩ := hm in hk.symm ▸ mul_dvd_mul_left _ (hmn.symm.dvd_of_dvd_mul_left (hk ▸ hn)) theorem coprime_one_left : ∀ n, coprime 1 n := gcd_one_left theorem coprime_one_right : ∀ n, coprime n 1 := gcd_one_right theorem coprime.pow_left {m k : ℕ} (n : ℕ) (H1 : coprime m k) : coprime (m ^ n) k := nat.rec_on n (coprime_one_left _) (λn IH, H1.mul IH) theorem coprime.pow_right {m k : ℕ} (n : ℕ) (H1 : coprime k m) : coprime k (m ^ n) := (H1.symm.pow_left n).symm theorem coprime.pow {k l : ℕ} (m n : ℕ) (H1 : coprime k l) : coprime (k ^ m) (l ^ n) := (H1.pow_left _).pow_right _ lemma coprime_pow_left_iff {n : ℕ} (hn : 0 < n) (a b : ℕ) : nat.coprime (a ^ n) b ↔ nat.coprime a b := begin obtain ⟨n, rfl⟩ := exists_eq_succ_of_ne_zero hn.ne', rw [pow_succ, nat.coprime_mul_iff_left], exact ⟨and.left, λ hab, ⟨hab, hab.pow_left _⟩⟩ end lemma coprime_pow_right_iff {n : ℕ} (hn : 0 < n) (a b : ℕ) : nat.coprime a (b ^ n) ↔ nat.coprime a b := by rw [nat.coprime_comm, coprime_pow_left_iff hn, nat.coprime_comm] theorem coprime.eq_one_of_dvd {k m : ℕ} (H : coprime k m) (d : k ∣ m) : k = 1 := by rw [← H.gcd_eq_one, gcd_eq_left d] @[simp] theorem coprime_zero_left (n : ℕ) : coprime 0 n ↔ n = 1 := by simp [coprime] @[simp] theorem coprime_zero_right (n : ℕ) : coprime n 0 ↔ n = 1 := by simp [coprime] theorem not_coprime_zero_zero : ¬ coprime 0 0 := by simp @[simp] theorem coprime_one_left_iff (n : ℕ) : coprime 1 n ↔ true := by simp [coprime] @[simp] theorem coprime_one_right_iff (n : ℕ) : coprime n 1 ↔ true := by simp [coprime] @[simp] theorem coprime_self (n : ℕ) : coprime n n ↔ n = 1 := by simp [coprime] lemma coprime.eq_of_mul_eq_zero {m n : ℕ} (h : m.coprime n) (hmn : m * n = 0) : m = 0 ∧ n = 1 ∨ m = 1 ∧ n = 0 := (nat.eq_zero_of_mul_eq_zero hmn).imp (λ hm, ⟨hm, n.coprime_zero_left.mp $ hm ▸ h⟩) (λ hn, ⟨m.coprime_zero_left.mp $ hn ▸ h.symm, hn⟩) /-- Represent a divisor of `m * n` as a product of a divisor of `m` and a divisor of `n`. -/ def prod_dvd_and_dvd_of_dvd_prod {m n k : ℕ} (H : k ∣ m * n) : { d : {m' // m' ∣ m} × {n' // n' ∣ n} // k = d.1 * d.2 } := begin cases h0 : (gcd k m), case nat.zero { have : k = 0 := eq_zero_of_gcd_eq_zero_left h0, subst this, have : m = 0 := eq_zero_of_gcd_eq_zero_right h0, subst this, exact ⟨⟨⟨0, dvd_refl 0⟩, ⟨n, dvd_refl n⟩⟩, (zero_mul n).symm⟩ }, case nat.succ : tmp { have hpos : 0 < gcd k m := h0.symm ▸ nat.zero_lt_succ _; clear h0 tmp, have hd : gcd k m * (k / gcd k m) = k := (nat.mul_div_cancel' (gcd_dvd_left k m)), refine ⟨⟨⟨gcd k m, gcd_dvd_right k m⟩, ⟨k / gcd k m, _⟩⟩, hd.symm⟩, apply dvd_of_mul_dvd_mul_left hpos, rw [hd, ← gcd_mul_right], exact dvd_gcd (dvd_mul_right _ _) H } end theorem gcd_mul_dvd_mul_gcd (k m n : ℕ) : gcd k (m * n) ∣ gcd k m * gcd k n := begin rcases (prod_dvd_and_dvd_of_dvd_prod $ gcd_dvd_right k (m * n)) with ⟨⟨⟨m', hm'⟩, ⟨n', hn'⟩⟩, h⟩, replace h : gcd k (m * n) = m' * n' := h, rw h, have hm'n' : m' * n' ∣ k := h ▸ gcd_dvd_left _ _, apply mul_dvd_mul, { have hm'k : m' ∣ k := (dvd_mul_right m' n').trans hm'n', exact dvd_gcd hm'k hm' }, { have hn'k : n' ∣ k := (dvd_mul_left n' m').trans hm'n', exact dvd_gcd hn'k hn' } end theorem coprime.gcd_mul (k : ℕ) {m n : ℕ} (h : coprime m n) : gcd k (m * n) = gcd k m * gcd k n := dvd_antisymm (gcd_mul_dvd_mul_gcd k m n) ((h.gcd_both k k).mul_dvd_of_dvd_of_dvd (gcd_dvd_gcd_mul_right_right _ _ _) (gcd_dvd_gcd_mul_left_right _ _ _)) theorem pow_dvd_pow_iff {a b n : ℕ} (n0 : 0 < n) : a ^ n ∣ b ^ n ↔ a ∣ b := begin refine ⟨λ h, _, λ h, pow_dvd_pow_of_dvd h _⟩, cases nat.eq_zero_or_pos (gcd a b) with g0 g0, { simp [eq_zero_of_gcd_eq_zero_right g0] }, rcases exists_coprime' g0 with ⟨g, a', b', g0', co, rfl, rfl⟩, rw [mul_pow, mul_pow] at h, replace h := dvd_of_mul_dvd_mul_right (pow_pos g0' _) h, have := pow_dvd_pow a' n0, rw [pow_one, (co.pow n n).eq_one_of_dvd h] at this, simp [eq_one_of_dvd_one this] end lemma gcd_mul_gcd_of_coprime_of_mul_eq_mul {a b c d : ℕ} (cop : c.coprime d) (h : a * b = c * d) : a.gcd c * b.gcd c = c := begin apply dvd_antisymm, { apply nat.coprime.dvd_of_dvd_mul_right (nat.coprime.mul (cop.gcd_left _) (cop.gcd_left _)), rw ← h, apply mul_dvd_mul (gcd_dvd _ _).1 (gcd_dvd _ _).1 }, { rw [gcd_comm a _, gcd_comm b _], transitivity c.gcd (a * b), rw [h, gcd_mul_right_right d c], apply gcd_mul_dvd_mul_gcd } end /-- If `k:ℕ` divides coprime `a` and `b` then `k = 1` -/ lemma eq_one_of_dvd_coprimes {a b k : ℕ} (h_ab_coprime : coprime a b) (hka : k ∣ a) (hkb : k ∣ b) : k = 1 := begin rw coprime_iff_gcd_eq_one at h_ab_coprime, have h1 := dvd_gcd hka hkb, rw h_ab_coprime at h1, exact nat.dvd_one.mp h1, end end nat
b108be12d84a5335f4f795d79c370a9fd8dba989
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/571.lean
2b646fd9c99dfc80e583dd1d8cd7ea8f6c853c7c
[ "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
102
lean
open nat variables (P : ℕ → Prop) example (H : ∃n, P n) : ℕ := begin cases H with n p, end
8aad2723975a3e6135c817e976805b1c38a95061
367134ba5a65885e863bdc4507601606690974c1
/src/data/sym2.lean
a8c2943bddd3ac3bca91fafff39035ffcf1b3b0d
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
13,759
lean
/- Copyright (c) 2020 Kyle Miller All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Kyle Miller. -/ import tactic.linarith import data.sym open function open sym /-! # The symmetric square This file defines the symmetric square, which is `α × α` modulo swapping. This is also known as the type of unordered pairs. More generally, the symmetric square is the second symmetric power (see `data.sym`). The equivalence is `sym2.equiv_sym`. From the point of view that an unordered pair is equivalent to a multiset of cardinality two (see `sym2.equiv_multiset`), there is a `has_mem` instance `sym2.mem`, which is a `Prop`-valued membership test. Given `h : a ∈ z` for `z : sym2 α`, then `h.other` is the other element of the pair, defined using `classical.choice`. If `α` has decidable equality, then `h.other'` computably gives the other element. Recall that an undirected graph (allowing self loops, but no multiple edges) is equivalent to a symmetric relation on the vertex type `α`. Given a symmetric relation on `α`, the corresponding edge set is constructed by `sym2.from_rel`. ## Notation The symmetric square has a setoid instance, so `⟦(a, b)⟧` denotes a term of the symmetric square. ## Tags symmetric square, unordered pairs, symmetric powers -/ universe u variables {α : Type u} namespace sym2 /-- This is the relation capturing the notion of pairs equivalent up to permutations. -/ inductive rel (α : Type u) : (α × α) → (α × α) → Prop | refl (x y : α) : rel (x, y) (x, y) | swap (x y : α) : rel (x, y) (y, x) attribute [refl] rel.refl @[symm] lemma rel.symm {x y : α × α} : rel α x y → rel α y x := by rintro ⟨_, _⟩; constructor @[trans] lemma rel.trans {x y z : α × α} : rel α x y → rel α y z → rel α x z := by { intros a b, cases_matching* rel _ _ _; apply rel.refl <|> apply rel.swap } lemma rel.is_equivalence : equivalence (rel α) := by tidy; apply rel.trans; assumption instance rel.setoid (α : Type u) : setoid (α × α) := ⟨rel α, rel.is_equivalence⟩ end sym2 /-- `sym2 α` is the symmetric square of `α`, which, in other words, is the type of unordered pairs. It is equivalent in a natural way to multisets of cardinality 2 (see `sym2.equiv_multiset`). -/ @[reducible] def sym2 (α : Type u) := quotient (sym2.rel.setoid α) namespace sym2 lemma eq_swap {a b : α} : ⟦(a, b)⟧ = ⟦(b, a)⟧ := by { rw quotient.eq, apply rel.swap } lemma congr_right {a b c : α} : ⟦(a, b)⟧ = ⟦(a, c)⟧ ↔ b = c := by { split; intro h, { rw quotient.eq at h, cases h; refl }, rw h } lemma congr_left {a b c : α} : ⟦(b, a)⟧ = ⟦(c, a)⟧ ↔ b = c := by { split; intro h, { rw quotient.eq at h, cases h; refl }, rw h } /-- The functor `sym2` is functorial, and this function constructs the induced maps. -/ def map {α β : Type*} (f : α → β) : sym2 α → sym2 β := quotient.map (prod.map f f) (by { rintros _ _ h, cases h, { refl }, apply rel.swap }) @[simp] lemma map_id : sym2.map (@id α) = id := by tidy lemma map_comp {α β γ : Type*} {g : β → γ} {f : α → β} : sym2.map (g ∘ f) = sym2.map g ∘ sym2.map f := by tidy @[simp] lemma map_pair_eq {α β : Type*} (f : α → β) (x y : α) : map f ⟦(x, y)⟧ = ⟦(f x, f y)⟧ := by simp [map] section membership /-! ### Declarations about membership -/ /-- This is a predicate that determines whether a given term is a member of a term of the symmetric square. From this point of view, the symmetric square is the subtype of cardinality-two multisets on `α`. -/ def mem (x : α) (z : sym2 α) : Prop := ∃ (y : α), z = ⟦(x, y)⟧ instance : has_mem α (sym2 α) := ⟨mem⟩ lemma mk_has_mem (x y : α) : x ∈ ⟦(x, y)⟧ := ⟨y, rfl⟩ lemma mk_has_mem_right (x y : α) : y ∈ ⟦(x, y)⟧ := by { rw eq_swap, apply mk_has_mem } /-- Given an element of the unordered pair, give the other element using `classical.some`. See also `mem.other'` for the computable version. -/ noncomputable def mem.other {a : α} {z : sym2 α} (h : a ∈ z) : α := classical.some h @[simp] lemma mem_other_spec {a : α} {z : sym2 α} (h : a ∈ z) : ⟦(a, h.other)⟧ = z := by erw ← classical.some_spec h lemma eq_iff {x y z w : α} : ⟦(x, y)⟧ = ⟦(z, w)⟧ ↔ (x = z ∧ y = w) ∨ (x = w ∧ y = z) := begin split; intro h, { rw quotient.eq at h, cases h; tidy }, { cases h; rw [h.1, h.2], rw eq_swap } end @[simp] lemma mem_iff {a b c : α} : a ∈ ⟦(b, c)⟧ ↔ a = b ∨ a = c := { mp := by { rintro ⟨_, h⟩, rw eq_iff at h, tidy }, mpr := by { rintro ⟨_⟩; subst a, { apply mk_has_mem }, apply mk_has_mem_right } } lemma mem_other_mem {a : α} {z : sym2 α} (h : a ∈ z) : h.other ∈ z := by { convert mk_has_mem_right a h.other, rw mem_other_spec h } lemma elems_iff_eq {x y : α} {z : sym2 α} (hne : x ≠ y) : x ∈ z ∧ y ∈ z ↔ z = ⟦(x, y)⟧ := begin split, { refine quotient.rec_on_subsingleton z _, rintros ⟨z₁, z₂⟩ ⟨hx, hy⟩, rw eq_iff, cases mem_iff.mp hx with hx hx; cases mem_iff.mp hy with hy hy; cc }, { rintro rfl, simp }, end @[ext] lemma sym2_ext (z z' : sym2 α) (h : ∀ x, x ∈ z ↔ x ∈ z') : z = z' := begin refine quotient.rec_on_subsingleton z (λ w, _) h, refine quotient.rec_on_subsingleton z' (λ w', _), intro h, cases w with x y, cases w' with x' y', simp only [mem_iff] at h, apply eq_iff.mpr, have hx := h x, have hy := h y, have hx' := h x', have hy' := h y', simp only [true_iff, true_or, eq_self_iff_true, iff_true, or_true] at hx hy hx' hy', cases hx; subst x; cases hy; subst y; cases hx'; try { subst x' }; cases hy'; try { subst y' }; cc, end instance mem.decidable [decidable_eq α] (x : α) (z : sym2 α) : decidable (x ∈ z) := quotient.rec_on_subsingleton z (λ ⟨y₁, y₂⟩, decidable_of_iff' _ mem_iff) end membership /-- A type `α` is naturally included in the diagonal of `α × α`, and this function gives the image of this diagonal in `sym2 α`. -/ def diag (x : α) : sym2 α := ⟦(x, x)⟧ /-- A predicate for testing whether an element of `sym2 α` is on the diagonal. -/ def is_diag (z : sym2 α) : Prop := z ∈ set.range (@diag α) @[simp] lemma diag_is_diag (a : α) : is_diag (diag a) := by use a @[simp] lemma is_diag_iff_proj_eq (z : α × α) : is_diag ⟦z⟧ ↔ z.1 = z.2 := begin cases z with a, split, { rintro ⟨_, h⟩, erw eq_iff at h, cc }, { rintro ⟨⟩, use a, refl }, end instance is_diag.decidable_pred (α : Type u) [decidable_eq α] : decidable_pred (@is_diag α) := by { refine λ z, quotient.rec_on_subsingleton z (λ a, _), erw is_diag_iff_proj_eq, apply_instance } lemma mem_other_ne {a : α} {z : sym2 α} (hd : ¬is_diag z) (h : a ∈ z) : h.other ≠ a := begin intro hn, apply hd, have h' := sym2.mem_other_spec h, rw hn at h', rw ←h', simp, end section relations /-! ### Declarations about symmetric relations -/ variables {r : α → α → Prop} /-- Symmetric relations define a set on `sym2 α` by taking all those pairs of elements that are related. -/ def from_rel (sym : symmetric r) : set (sym2 α) := quotient.lift (uncurry r) (by { rintros _ _ ⟨_, _⟩, tidy }) @[simp] lemma from_rel_proj_prop {sym : symmetric r} {z : α × α} : ⟦z⟧ ∈ from_rel sym ↔ r z.1 z.2 := iff.rfl @[simp] lemma from_rel_prop {sym : symmetric r} {a b : α} : ⟦(a, b)⟧ ∈ from_rel sym ↔ r a b := by simp only [from_rel_proj_prop] lemma from_rel_irreflexive {sym : symmetric r} : irreflexive r ↔ ∀ {z}, z ∈ from_rel sym → ¬is_diag z := { mp := by { intros h z hr hd, induction z, erw is_diag_iff_proj_eq at hd, erw from_rel_proj_prop at hr, tidy }, mpr := by { intros h x hr, rw ← @from_rel_prop _ _ sym at hr, exact h hr ⟨x, rfl⟩ }} lemma mem_from_rel_irrefl_other_ne {sym : symmetric r} (irrefl : irreflexive r) {a : α} {z : sym2 α} (hz : z ∈ from_rel sym) (h : a ∈ z) : h.other ≠ a := mem_other_ne (from_rel_irreflexive.mp irrefl hz) h instance from_rel.decidable_pred (sym : symmetric r) [h : decidable_rel r] : decidable_pred (sym2.from_rel sym) := λ z, quotient.rec_on_subsingleton z (λ x, h _ _) end relations section sym_equiv /-! ### Equivalence to the second symmetric power -/ local attribute [instance] vector.perm.is_setoid private def from_vector {α : Type*} : vector α 2 → α × α | ⟨[a, b], h⟩ := (a, b) private lemma perm_card_two_iff {α : Type*} {a₁ b₁ a₂ b₂ : α} : [a₁, b₁].perm [a₂, b₂] ↔ (a₁ = a₂ ∧ b₁ = b₂) ∨ (a₁ = b₂ ∧ b₁ = a₂) := { mp := by { simp [← multiset.coe_eq_coe, ← multiset.cons_coe, multiset.cons_eq_cons]; tidy }, mpr := by { intro h, cases h; rw [h.1, h.2], apply list.perm.swap', refl } } /-- The symmetric square is equivalent to length-2 vectors up to permutations. -/ def sym2_equiv_sym' {α : Type*} : equiv (sym2 α) (sym' α 2) := { to_fun := quotient.map (λ (x : α × α), ⟨[x.1, x.2], rfl⟩) (by { rintros _ _ ⟨_⟩, { refl }, apply list.perm.swap', refl }), inv_fun := quotient.map from_vector (begin rintros ⟨x, hx⟩ ⟨y, hy⟩ h, cases x with _ x, { simp at hx; tauto }, cases x with _ x, { simp at hx; norm_num at hx }, cases x with _ x, swap, { exfalso, simp at hx; linarith [hx] }, cases y with _ y, { simp at hy; tauto }, cases y with _ y, { simp at hy; norm_num at hy }, cases y with _ y, swap, { exfalso, simp at hy; linarith [hy] }, rcases perm_card_two_iff.mp h with ⟨rfl,rfl⟩|⟨rfl,rfl⟩, { refl }, apply sym2.rel.swap, end), left_inv := by tidy, right_inv := λ x, begin refine quotient.rec_on_subsingleton x (λ x, _), { cases x with x hx, cases x with _ x, { simp at hx; tauto }, cases x with _ x, { simp at hx; norm_num at hx }, cases x with _ x, swap, { exfalso, simp at hx; linarith [hx] }, refl }, end } /-- The symmetric square is equivalent to the second symmetric power. -/ def equiv_sym (α : Type*) : sym2 α ≃ sym α 2 := equiv.trans sym2_equiv_sym' sym_equiv_sym'.symm /-- The symmetric square is equivalent to multisets of cardinality two. (This is currently a synonym for `equiv_sym`, but it's provided in case the definition for `sym` changes.) -/ def equiv_multiset (α : Type*) : sym2 α ≃ {s : multiset α // s.card = 2} := equiv_sym α end sym_equiv section decidable /-- An algorithm for computing `sym2.rel`. -/ def rel_bool [decidable_eq α] (x y : α × α) : bool := if x.1 = y.1 then x.2 = y.2 else if x.1 = y.2 then x.2 = y.1 else ff lemma rel_bool_spec [decidable_eq α] (x y : α × α) : ↥(rel_bool x y) ↔ rel α x y := begin cases x with x₁ x₂, cases y with y₁ y₂, dsimp [rel_bool], split_ifs; simp only [false_iff, bool.coe_sort_ff, bool.of_to_bool_iff], rotate 2, { contrapose! h, cases h; cc }, all_goals { subst x₁, split; intro h1, { subst h1; apply sym2.rel.swap }, { cases h1; cc } } end /-- Given `[decidable_eq α]` and `[fintype α]`, the following instance gives `fintype (sym2 α)`. -/ instance (α : Type*) [decidable_eq α] : decidable_rel (sym2.rel α) := λ x y, decidable_of_bool (rel_bool x y) (rel_bool_spec x y) /-- A function that gives the other element of a pair given one of the elements. Used in `mem.other'`. -/ private def pair_other [decidable_eq α] (a : α) (z : α × α) : α := if a = z.1 then z.2 else z.1 /-- Get the other element of the unordered pair using the decidable equality. This is the computable version of `mem.other`. -/ def mem.other' [decidable_eq α] {a : α} {z : sym2 α} (h : a ∈ z) : α := quot.rec (λ x h', pair_other a x) (begin clear h z, intros x y h, ext hy, convert_to pair_other a x = _, { have h' : ∀ {c e h}, @eq.rec _ ⟦x⟧ (λ s, a ∈ s → α) (λ _, pair_other a x) c e h = pair_other a x, { intros _ e _, subst e }, apply h', }, have h' := (rel_bool_spec x y).mpr h, cases x with x₁ x₂, cases y with y₁ y₂, cases mem_iff.mp hy with hy'; subst a; dsimp [rel_bool] at h'; split_ifs at h'; try { rw bool.of_to_bool_iff at h', subst x₁, subst x₂ }; dsimp [pair_other], simp only [ne.symm h_1, if_true, eq_self_iff_true, if_false], exfalso, exact bool.not_ff h', simp only [h_1, if_true, eq_self_iff_true, if_false], exfalso, exact bool.not_ff h', end) z h @[simp] lemma mem_other_spec' [decidable_eq α] {a : α} {z : sym2 α} (h : a ∈ z) : ⟦(a, h.other')⟧ = z := begin induction z, cases z with x y, have h' := mem_iff.mp h, dsimp [mem.other', quot.rec, pair_other], cases h'; subst a, { simp only [if_true, eq_self_iff_true], refl, }, { split_ifs, subst h_1, refl, rw eq_swap, refl, }, refl, end @[simp] lemma other_eq_other' [decidable_eq α] {a : α} {z : sym2 α} (h : a ∈ z) : h.other = h.other' := by rw [←congr_right, mem_other_spec' h, mem_other_spec] lemma mem_other_mem' [decidable_eq α] {a : α} {z : sym2 α} (h : a ∈ z) : h.other' ∈ z := by { rw ←other_eq_other', exact mem_other_mem h } lemma other_invol' [decidable_eq α] {a : α} {z : sym2 α} (ha : a ∈ z) (hb : ha.other' ∈ z): hb.other' = a := begin induction z, cases z with x y, dsimp [mem.other', quot.rec, pair_other] at hb, split_ifs at hb; dsimp [mem.other', quot.rec, pair_other], simp only [h, if_true, eq_self_iff_true], split_ifs, assumption, refl, simp only [h, if_false, if_true, eq_self_iff_true], cases mem_iff.mp ha; cc, refl, end lemma other_invol {a : α} {z : sym2 α} (ha : a ∈ z) (hb : ha.other ∈ z): hb.other = a := begin classical, rw other_eq_other' at hb ⊢, convert other_invol' ha hb, rw other_eq_other', end end decidable end sym2
c81f2c575ad670398a1a3a8fb118a15928750238
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/limits/types.lean
e9e7959d7b6b9addbec9d1d68b7b802e320ef0ed
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
18,069
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Reid Barton -/ import category_theory.limits.shapes.images import category_theory.filtered import tactic.equiv_rw /-! # Limits in the category of types. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. We show that the category of types has all (co)limits, by providing the usual concrete models. We also give a characterisation of filtered colimits in `Type`, via `colimit.ι F i xi = colimit.ι F j xj ↔ ∃ k (f : i ⟶ k) (g : j ⟶ k), F.map f xi = F.map g xj`. Finally, we prove the category of types has categorical images, and that these agree with the range of a function. -/ universes v u open category_theory open category_theory.limits namespace category_theory.limits.types variables {J : Type v} [small_category J] /-- (internal implementation) the limit cone of a functor, implemented as flat sections of a pi type -/ def limit_cone (F : J ⥤ Type (max v u)) : cone F := { X := F.sections, π := { app := λ j u, u.val j } } local attribute [elab_simple] congr_fun /-- (internal implementation) the fact that the proposed limit cone is the limit -/ def limit_cone_is_limit (F : J ⥤ Type (max v u)) : is_limit (limit_cone F) := { lift := λ s v, ⟨λ j, s.π.app j v, λ j j' f, congr_fun (cone.w s f) _⟩, uniq' := by { intros, ext x j, exact congr_fun (w j) x } } /-- The category of types has all limits. See <https://stacks.math.columbia.edu/tag/002U>. -/ instance has_limits_of_size : has_limits_of_size.{v} (Type (max v u)) := { has_limits_of_shape := λ J 𝒥, by exactI { has_limit := λ F, has_limit.mk { cone := limit_cone F, is_limit := limit_cone_is_limit F } } } instance : has_limits (Type u) := types.has_limits_of_size.{u u} /-- The equivalence between a limiting cone of `F` in `Type u` and the "concrete" definition as the sections of `F`. -/ def is_limit_equiv_sections {F : J ⥤ Type (max v u)} {c : cone F} (t : is_limit c) : c.X ≃ F.sections := (is_limit.cone_point_unique_up_to_iso t (limit_cone_is_limit F)).to_equiv @[simp] lemma is_limit_equiv_sections_apply {F : J ⥤ Type (max v u)} {c : cone F} (t : is_limit c) (j : J) (x : c.X) : (((is_limit_equiv_sections t) x) : Π j, F.obj j) j = c.π.app j x := rfl @[simp] lemma is_limit_equiv_sections_symm_apply {F : J ⥤ Type (max v u)} {c : cone F} (t : is_limit c) (x : F.sections) (j : J) : c.π.app j ((is_limit_equiv_sections t).symm x) = (x : Π j, F.obj j) j := begin equiv_rw (is_limit_equiv_sections t).symm at x, simp, end /-- The equivalence between the abstract limit of `F` in `Type u` and the "concrete" definition as the sections of `F`. -/ noncomputable def limit_equiv_sections (F : J ⥤ Type (max v u)) : (limit F : Type (max v u)) ≃ F.sections := is_limit_equiv_sections (limit.is_limit _) @[simp] lemma limit_equiv_sections_apply (F : J ⥤ Type (max v u)) (x : limit F) (j : J) : (((limit_equiv_sections F) x) : Π j, F.obj j) j = limit.π F j x := rfl @[simp] lemma limit_equiv_sections_symm_apply (F : J ⥤ Type (max v u)) (x : F.sections) (j : J) : limit.π F j ((limit_equiv_sections F).symm x) = (x : Π j, F.obj j) j := is_limit_equiv_sections_symm_apply _ _ _ @[simp] lemma limit_equiv_sections_symm_apply' (F : J ⥤ Type v) (x : F.sections) (j : J) : limit.π F j ((limit_equiv_sections.{v v} F).symm x) = (x : Π j, F.obj j) j := is_limit_equiv_sections_symm_apply _ _ _ /-- Construct a term of `limit F : Type u` from a family of terms `x : Π j, F.obj j` which are "coherent": `∀ (j j') (f : j ⟶ j'), F.map f (x j) = x j'`. -/ @[ext] noncomputable def limit.mk (F : J ⥤ Type (max v u)) (x : Π j, F.obj j) (h : ∀ (j j') (f : j ⟶ j'), F.map f (x j) = x j') : (limit F : Type (max v u)) := (limit_equiv_sections F).symm ⟨x, h⟩ @[simp] lemma limit.π_mk (F : J ⥤ Type (max v u)) (x : Π j, F.obj j) (h : ∀ (j j') (f : j ⟶ j'), F.map f (x j) = x j') (j) : limit.π F j (limit.mk F x h) = x j := by { dsimp [limit.mk], simp, } @[simp] lemma limit.π_mk' (F : J ⥤ Type v) (x : Π j, F.obj j) (h : ∀ (j j') (f : j ⟶ j'), F.map f (x j) = x j') (j) : limit.π F j (limit.mk.{v v} F x h) = x j := by { dsimp [limit.mk], simp, } -- PROJECT: prove this for concrete categories where the forgetful functor preserves limits @[ext] lemma limit_ext (F : J ⥤ Type (max v u)) (x y : limit F) (w : ∀ j, limit.π F j x = limit.π F j y) : x = y := begin apply (limit_equiv_sections F).injective, ext j, simp [w j], end @[ext] lemma limit_ext' (F : J ⥤ Type v) (x y : limit F) (w : ∀ j, limit.π F j x = limit.π F j y) : x = y := begin apply (limit_equiv_sections.{v v} F).injective, ext j, simp [w j], end lemma limit_ext_iff (F : J ⥤ Type (max v u)) (x y : limit F) : x = y ↔ (∀ j, limit.π F j x = limit.π F j y) := ⟨λ t _, t ▸ rfl, limit_ext _ _ _⟩ lemma limit_ext_iff' (F : J ⥤ Type v) (x y : limit F) : x = y ↔ (∀ j, limit.π F j x = limit.π F j y) := ⟨λ t _, t ▸ rfl, limit_ext _ _ _⟩ -- TODO: are there other limits lemmas that should have `_apply` versions? -- Can we generate these like with `@[reassoc]`? -- PROJECT: prove these for any concrete category where the forgetful functor preserves limits? @[simp] lemma limit.w_apply {F : J ⥤ Type (max v u)} {j j' : J} {x : limit F} (f : j ⟶ j') : F.map f (limit.π F j x) = limit.π F j' x := congr_fun (limit.w F f) x @[simp] lemma limit.lift_π_apply (F : J ⥤ Type (max v u)) (s : cone F) (j : J) (x : s.X) : limit.π F j (limit.lift F s x) = s.π.app j x := congr_fun (limit.lift_π s j) x @[simp] lemma limit.map_π_apply {F G : J ⥤ Type (max v u)} (α : F ⟶ G) (j : J) (x) : limit.π G j (lim_map α x) = α.app j (limit.π F j x) := congr_fun (lim_map_π α j) x @[simp] lemma limit.w_apply' {F : J ⥤ Type v} {j j' : J} {x : limit F} (f : j ⟶ j') : F.map f (limit.π F j x) = limit.π F j' x := congr_fun (limit.w F f) x @[simp] lemma limit.lift_π_apply' (F : J ⥤ Type v) (s : cone F) (j : J) (x : s.X) : limit.π F j (limit.lift F s x) = s.π.app j x := congr_fun (limit.lift_π s j) x @[simp] lemma limit.map_π_apply' {F G : J ⥤ Type v} (α : F ⟶ G) (j : J) (x) : limit.π G j (lim_map α x) = α.app j (limit.π F j x) := congr_fun (lim_map_π α j) x /-- The relation defining the quotient type which implements the colimit of a functor `F : J ⥤ Type u`. See `category_theory.limits.types.quot`. -/ def quot.rel (F : J ⥤ Type (max v u)) : (Σ j, F.obj j) → (Σ j, F.obj j) → Prop := (λ p p', ∃ f : p.1 ⟶ p'.1, p'.2 = F.map f p.2) /-- A quotient type implementing the colimit of a functor `F : J ⥤ Type u`, as pairs `⟨j, x⟩` where `x : F.obj j`, modulo the equivalence relation generated by `⟨j, x⟩ ~ ⟨j', x'⟩` whenever there is a morphism `f : j ⟶ j'` so `F.map f x = x'`. -/ @[nolint has_nonempty_instance] def quot (F : J ⥤ Type (max v u)) : Type (max v u) := @quot (Σ j, F.obj j) (quot.rel F) /-- (internal implementation) the colimit cocone of a functor, implemented as a quotient of a sigma type -/ def colimit_cocone (F : J ⥤ Type (max v u)) : cocone F := { X := quot F, ι := { app := λ j x, quot.mk _ ⟨j, x⟩, naturality' := λ j j' f, funext $ λ x, eq.symm (quot.sound ⟨f, rfl⟩) } } local attribute [elab_with_expected_type] quot.lift /-- (internal implementation) the fact that the proposed colimit cocone is the colimit -/ def colimit_cocone_is_colimit (F : J ⥤ Type (max v u)) : is_colimit (colimit_cocone F) := { desc := λ s, quot.lift (λ (p : Σ j, F.obj j), s.ι.app p.1 p.2) (assume ⟨j, x⟩ ⟨j', x'⟩ ⟨f, hf⟩, by rw hf; exact (congr_fun (cocone.w s f) x).symm) } /-- The category of types has all colimits. See <https://stacks.math.columbia.edu/tag/002U>. -/ instance has_colimits_of_size : has_colimits_of_size.{v} (Type (max v u)) := { has_colimits_of_shape := λ J 𝒥, by exactI { has_colimit := λ F, has_colimit.mk { cocone := colimit_cocone F, is_colimit := colimit_cocone_is_colimit F } } } instance : has_colimits (Type u) := types.has_colimits_of_size.{u u} /-- The equivalence between the abstract colimit of `F` in `Type u` and the "concrete" definition as a quotient. -/ noncomputable def colimit_equiv_quot (F : J ⥤ Type (max v u)) : (colimit F : Type (max v u)) ≃ quot F := (is_colimit.cocone_point_unique_up_to_iso (colimit.is_colimit F) (colimit_cocone_is_colimit F)).to_equiv @[simp] lemma colimit_equiv_quot_symm_apply (F : J ⥤ Type (max v u)) (j : J) (x : F.obj j) : (colimit_equiv_quot F).symm (quot.mk _ ⟨j, x⟩) = colimit.ι F j x := rfl @[simp] lemma colimit_equiv_quot_apply (F : J ⥤ Type (max v u)) (j : J) (x : F.obj j) : (colimit_equiv_quot F) (colimit.ι F j x) = quot.mk _ ⟨j, x⟩ := begin apply (colimit_equiv_quot F).symm.injective, simp, end @[simp] lemma colimit.w_apply {F : J ⥤ Type (max v u)} {j j' : J} {x : F.obj j} (f : j ⟶ j') : colimit.ι F j' (F.map f x) = colimit.ι F j x := congr_fun (colimit.w F f) x @[simp] lemma colimit.ι_desc_apply (F : J ⥤ Type (max v u)) (s : cocone F) (j : J) (x : F.obj j) : colimit.desc F s (colimit.ι F j x) = s.ι.app j x := congr_fun (colimit.ι_desc s j) x @[simp] lemma colimit.ι_map_apply {F G : J ⥤ Type (max v u)} (α : F ⟶ G) (j : J) (x) : colim.map α (colimit.ι F j x) = colimit.ι G j (α.app j x) := congr_fun (colimit.ι_map α j) x @[simp] lemma colimit.w_apply' {F : J ⥤ Type v} {j j' : J} {x : F.obj j} (f : j ⟶ j') : colimit.ι F j' (F.map f x) = colimit.ι F j x := congr_fun (colimit.w F f) x @[simp] lemma colimit.ι_desc_apply' (F : J ⥤ Type v) (s : cocone F) (j : J) (x : F.obj j) : colimit.desc F s (colimit.ι F j x) = s.ι.app j x := congr_fun (colimit.ι_desc s j) x @[simp] lemma colimit.ι_map_apply' {F G : J ⥤ Type v} (α : F ⟶ G) (j : J) (x) : colim.map α (colimit.ι F j x) = colimit.ι G j (α.app j x) := congr_fun (colimit.ι_map α j) x lemma colimit_sound {F : J ⥤ Type (max v u)} {j j' : J} {x : F.obj j} {x' : F.obj j'} (f : j ⟶ j') (w : F.map f x = x') : colimit.ι F j x = colimit.ι F j' x' := begin rw [←w], simp, end lemma colimit_sound' {F : J ⥤ Type (max v u)} {j j' : J} {x : F.obj j} {x' : F.obj j'} {j'' : J} (f : j ⟶ j'') (f' : j' ⟶ j'') (w : F.map f x = F.map f' x') : colimit.ι F j x = colimit.ι F j' x' := begin rw [←colimit.w _ f, ←colimit.w _ f'], rw [types_comp_apply, types_comp_apply, w], end lemma colimit_eq {F : J ⥤ Type (max v u)} {j j' : J} {x : F.obj j} {x' : F.obj j'} (w : colimit.ι F j x = colimit.ι F j' x') : eqv_gen (quot.rel F) ⟨j, x⟩ ⟨j', x'⟩ := begin apply quot.eq.1, simpa using congr_arg (colimit_equiv_quot F) w, end lemma jointly_surjective (F : J ⥤ Type (max v u)) {t : cocone F} (h : is_colimit t) (x : t.X) : ∃ j y, t.ι.app j y = x := begin suffices : (λ (x : t.X), ulift.up (∃ j y, t.ι.app j y = x)) = (λ _, ulift.up true), { have := congr_fun this x, have H := congr_arg ulift.down this, dsimp at H, rwa eq_true_iff at H }, refine h.hom_ext _, intro j, ext y, erw iff_true, exact ⟨j, y, rfl⟩ end /-- A variant of `jointly_surjective` for `x : colimit F`. -/ lemma jointly_surjective' {F : J ⥤ Type (max v u)} (x : colimit F) : ∃ j y, colimit.ι F j y = x := jointly_surjective F (colimit.is_colimit _) x namespace filtered_colimit /- For filtered colimits of types, we can give an explicit description of the equivalence relation generated by the relation used to form the colimit. -/ variables (F : J ⥤ Type (max v u)) /-- An alternative relation on `Σ j, F.obj j`, which generates the same equivalence relation as we use to define the colimit in `Type` above, but that is more convenient when working with filtered colimits. Elements in `F.obj j` and `F.obj j'` are equivalent if there is some `k : J` to the right where their images are equal. -/ protected def rel (x y : Σ j, F.obj j) : Prop := ∃ k (f : x.1 ⟶ k) (g : y.1 ⟶ k), F.map f x.2 = F.map g y.2 lemma rel_of_quot_rel (x y : Σ j, F.obj j) : quot.rel F x y → filtered_colimit.rel F x y := λ ⟨f, h⟩, ⟨y.1, f, 𝟙 y.1, by rw [← h, functor_to_types.map_id_apply]⟩ lemma eqv_gen_quot_rel_of_rel (x y : Σ j, F.obj j) : filtered_colimit.rel F x y → eqv_gen (quot.rel F) x y := λ ⟨k, f, g, h⟩, eqv_gen.trans _ ⟨k, F.map f x.2⟩ _ (eqv_gen.rel _ _ ⟨f, rfl⟩) (eqv_gen.symm _ _ (eqv_gen.rel _ _ ⟨g, h⟩)) local attribute [elab_simple] nat_trans.app /-- Recognizing filtered colimits of types. -/ noncomputable def is_colimit_of (t : cocone F) (hsurj : ∀ (x : t.X), ∃ i xi, x = t.ι.app i xi) (hinj : ∀ i j xi xj, t.ι.app i xi = t.ι.app j xj → ∃ k (f : i ⟶ k) (g : j ⟶ k), F.map f xi = F.map g xj) : is_colimit t := -- Strategy: Prove that the map from "the" colimit of F (defined above) to t.X -- is a bijection. begin apply is_colimit.of_iso_colimit (colimit.is_colimit F), refine cocones.ext (equiv.to_iso (equiv.of_bijective _ _)) _, { exact colimit.desc F t }, { split, { show function.injective _, intros a b h, rcases jointly_surjective F (colimit.is_colimit F) a with ⟨i, xi, rfl⟩, rcases jointly_surjective F (colimit.is_colimit F) b with ⟨j, xj, rfl⟩, change (colimit.ι F i ≫ colimit.desc F t) xi = (colimit.ι F j ≫ colimit.desc F t) xj at h, rw [colimit.ι_desc, colimit.ι_desc] at h, rcases hinj i j xi xj h with ⟨k, f, g, h'⟩, change colimit.ι F i xi = colimit.ι F j xj, rw [←colimit.w F f, ←colimit.w F g], change colimit.ι F k (F.map f xi) = colimit.ι F k (F.map g xj), rw h' }, { show function.surjective _, intro x, rcases hsurj x with ⟨i, xi, rfl⟩, use colimit.ι F i xi, simp } }, { intro j, apply colimit.ι_desc } end variables [is_filtered_or_empty J] protected lemma rel_equiv : equivalence (filtered_colimit.rel F) := ⟨λ x, ⟨x.1, 𝟙 x.1, 𝟙 x.1, rfl⟩, λ x y ⟨k, f, g, h⟩, ⟨k, g, f, h.symm⟩, λ x y z ⟨k, f, g, h⟩ ⟨k', f', g', h'⟩, let ⟨l, fl, gl, _⟩ := is_filtered_or_empty.cocone_objs k k', ⟨m, n, hn⟩ := is_filtered_or_empty.cocone_maps (g ≫ fl) (f' ≫ gl) in ⟨m, f ≫ fl ≫ n, g' ≫ gl ≫ n, calc F.map (f ≫ fl ≫ n) x.2 = F.map (fl ≫ n) (F.map f x.2) : by simp ... = F.map (fl ≫ n) (F.map g y.2) : by rw h ... = F.map ((g ≫ fl) ≫ n) y.2 : by simp ... = F.map ((f' ≫ gl) ≫ n) y.2 : by rw hn ... = F.map (gl ≫ n) (F.map f' y.2) : by simp ... = F.map (gl ≫ n) (F.map g' z.2) : by rw h' ... = F.map (g' ≫ gl ≫ n) z.2 : by simp⟩⟩ protected lemma rel_eq_eqv_gen_quot_rel : filtered_colimit.rel F = eqv_gen (quot.rel F) := begin ext ⟨j, x⟩ ⟨j', y⟩, split, { apply eqv_gen_quot_rel_of_rel }, { rw ←(filtered_colimit.rel_equiv F).eqv_gen_iff, exact eqv_gen.mono (rel_of_quot_rel F) } end lemma colimit_eq_iff_aux {i j : J} {xi : F.obj i} {xj : F.obj j} : (colimit_cocone F).ι.app i xi = (colimit_cocone F).ι.app j xj ↔ filtered_colimit.rel F ⟨i, xi⟩ ⟨j, xj⟩ := begin change quot.mk _ _ = quot.mk _ _ ↔ _, rw [quot.eq, filtered_colimit.rel_eq_eqv_gen_quot_rel], end lemma is_colimit_eq_iff {t : cocone F} (ht : is_colimit t) {i j : J} {xi : F.obj i} {xj : F.obj j} : t.ι.app i xi = t.ι.app j xj ↔ ∃ k (f : i ⟶ k) (g : j ⟶ k), F.map f xi = F.map g xj := let t' := colimit_cocone F, e : t' ≅ t := is_colimit.unique_up_to_iso (colimit_cocone_is_colimit F) ht, e' : t'.X ≅ t.X := (cocones.forget _).map_iso e in begin refine iff.trans _ (colimit_eq_iff_aux F), convert e'.to_equiv.apply_eq_iff_eq; rw ←e.hom.w; refl end lemma colimit_eq_iff {i j : J} {xi : F.obj i} {xj : F.obj j} : colimit.ι F i xi = colimit.ι F j xj ↔ ∃ k (f : i ⟶ k) (g : j ⟶ k), F.map f xi = F.map g xj := is_colimit_eq_iff _ (colimit.is_colimit F) end filtered_colimit variables {α β : Type u} (f : α ⟶ β) section -- implementation of `has_image` /-- the image of a morphism in Type is just `set.range f` -/ def image : Type u := set.range f instance [inhabited α] : inhabited (image f) := { default := ⟨f default, ⟨_, rfl⟩⟩ } /-- the inclusion of `image f` into the target -/ def image.ι : image f ⟶ β := subtype.val instance : mono (image.ι f) := (mono_iff_injective _).2 subtype.val_injective variables {f} /-- the universal property for the image factorisation -/ noncomputable def image.lift (F' : mono_factorisation f) : image f ⟶ F'.I := (λ x, F'.e (classical.indefinite_description _ x.2).1 : image f → F'.I) 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 Type through a mono. -/ def mono_factorisation : mono_factorisation f := { I := image f, m := image.ι f, e := set.range_factorization f } /-- the facorisation 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 } instance : has_image f := has_image.mk ⟨_, is_image f⟩ instance : has_images (Type u) := { has_image := by apply_instance } instance : has_image_maps (Type u) := { has_image_map := λ f g st, has_image_map.transport st (mono_factorisation f.hom) (is_image g.hom) (λ x, ⟨st.right x.1, ⟨st.left (classical.some x.2), begin have p := st.w, replace p := congr_fun p (classical.some x.2), simp only [functor.id_map, types_comp_apply, subtype.val_eq_coe] at p, erw [p, classical.some_spec x.2], end⟩⟩) rfl } end category_theory.limits.types
0af20221bd5de81b34b41b223bc55609c5a8cffe
63abd62053d479eae5abf4951554e1064a4c45b4
/src/category_theory/monoidal/CommMon_.lean
c9d99f88c1228d6f68045070c1e0be303db775b3
[ "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
5,390
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.monoidal.braided import category_theory.monoidal.Mon_ /-! # The category of commutative monoids in a braided monoidal category. -/ universes v₁ v₂ u₁ u₂ open category_theory open category_theory.monoidal_category variables (C : Type u₁) [category.{v₁} C] [monoidal_category.{v₁} C] [braided_category.{v₁} C] /-- A commutative monoid object internal to a monoidal category. -/ structure CommMon_ extends Mon_ C := (mul_comm' : (β_ _ _).hom ≫ mul = mul . obviously) restate_axiom CommMon_.mul_comm' attribute [simp, reassoc] CommMon_.mul_comm namespace CommMon_ /-- The trivial commutative monoid object. We later show this is initial in `CommMon_ C`. -/ @[simps] def trivial : CommMon_ C := { mul_comm' := begin dsimp, rw [braiding_left_unitor, unitors_equal], end ..Mon_.trivial C } instance : inhabited (CommMon_ C) := ⟨trivial C⟩ variables {C} {M : CommMon_ C} instance : category (CommMon_ C) := induced_category.category CommMon_.to_Mon_ @[simp] lemma id_hom (A : CommMon_ C) : Mon_.hom.hom (𝟙 A) = 𝟙 A.X := rfl @[simp] lemma comp_hom {R S T : CommMon_ C} (f : R ⟶ S) (g : S ⟶ T) : Mon_.hom.hom (f ≫ g) = f.hom ≫ g.hom := rfl section variables (C) /-- The forgetful functor from commutative monoid objects to monoid objects. -/ @[derive [full, faithful]] def forget₂_Mon_ : CommMon_ C ⥤ Mon_ C := induced_functor CommMon_.to_Mon_ @[simp] lemma forget₂_Mon_obj_one (A : CommMon_ C) : ((forget₂_Mon_ C).obj A).one = A.one := rfl @[simp] lemma forget₂_Mon_obj_mul (A : CommMon_ C) : ((forget₂_Mon_ C).obj A).mul = A.mul := rfl @[simp] lemma forget₂_Mon_map_hom {A B : CommMon_ C} (f : A ⟶ B) : ((forget₂_Mon_ C).map f).hom = f.hom := rfl end instance unique_hom_from_trivial (A : CommMon_ C) : unique (trivial C ⟶ A) := Mon_.unique_hom_from_trivial A.to_Mon_ open category_theory.limits instance : has_initial (CommMon_ C) := has_initial_of_unique (trivial C) end CommMon_ namespace category_theory.lax_braided_functor variables {C} {D : Type u₂} [category.{v₂} D] [monoidal_category.{v₂} D] [braided_category.{v₂} D] /-- A lax braided functor takes commutative monoid objects to commutative monoid objects. That is, a lax braided functor `F : C ⥤ D` induces a functor `CommMon_ C ⥤ CommMon_ D`. -/ @[simps] def map_CommMon (F : lax_braided_functor C D) : CommMon_ C ⥤ CommMon_ D := { obj := λ A, { mul_comm' := begin dsimp, have := F.braided, slice_lhs 1 2 { rw ←this, }, slice_lhs 2 3 { rw [←category_theory.functor.map_comp, A.mul_comm], }, end, ..F.to_lax_monoidal_functor.map_Mon.obj A.to_Mon_ }, map := λ A B f, F.to_lax_monoidal_functor.map_Mon.map f, } variables (C) (D) /-- `map_CommMon` is functorial in the lax braided functor. -/ def map_CommMon_functor : (lax_braided_functor C D) ⥤ (CommMon_ C ⥤ CommMon_ D) := { obj := map_CommMon, map := λ F G α, { app := λ A, { hom := α.app A.X, } } } end category_theory.lax_braided_functor namespace CommMon_ open category_theory.lax_braided_functor namespace equiv_lax_braided_functor_punit /-- Implementation of `CommMon_.equiv_lax_braided_functor_punit`. -/ @[simps] def lax_braided_to_CommMon : lax_braided_functor (discrete punit) C ⥤ CommMon_ C := { obj := λ F, (F.map_CommMon : CommMon_ _ ⥤ CommMon_ C).obj (trivial (discrete punit)), map := λ F G α, ((map_CommMon_functor (discrete punit) C).map α).app _ } /-- Implementation of `CommMon_.equiv_lax_braided_functor_punit`. -/ @[simps] def CommMon_to_lax_braided : CommMon_ C ⥤ lax_braided_functor (discrete punit) C := { obj := λ A, { obj := λ _, A.X, map := λ _ _ _, 𝟙 _, ε := A.one, μ := λ _ _, A.mul, map_id' := λ _, rfl, map_comp' := λ _ _ _ _ _, (category.id_comp (𝟙 A.X)).symm, }, map := λ A B f, { app := λ _, f.hom, naturality' := λ _ _ _, by { dsimp, rw [category.id_comp, category.comp_id], }, unit' := f.one_hom, tensor' := λ _ _, f.mul_hom, }, } /-- Implementation of `CommMon_.equiv_lax_braided_functor_punit`. -/ @[simps {rhs_md:=semireducible}] def unit_iso : 𝟭 (lax_braided_functor (discrete punit) C) ≅ lax_braided_to_CommMon C ⋙ CommMon_to_lax_braided C := nat_iso.of_components (λ F, lax_braided_functor.mk_iso (monoidal_nat_iso.of_components (λ _, F.to_lax_monoidal_functor.to_functor.map_iso (eq_to_iso (by ext))) (by tidy) (by tidy) (by tidy))) (by tidy) /-- Implementation of `CommMon_.equiv_lax_braided_functor_punit`. -/ @[simps {rhs_md:=semireducible}] def counit_iso : CommMon_to_lax_braided C ⋙ lax_braided_to_CommMon C ≅ 𝟭 (CommMon_ C) := nat_iso.of_components (λ F, { hom := { hom := 𝟙 _, }, inv := { hom := 𝟙 _, } }) (by tidy) end equiv_lax_braided_functor_punit open equiv_lax_braided_functor_punit /-- Commutative monoid objects in `C` are "just" braided lax monoidal functors from the trivial braided monoidal category to `C`. -/ @[simps] def equiv_lax_braided_functor_punit : lax_braided_functor (discrete punit) C ≌ CommMon_ C := { functor := lax_braided_to_CommMon C, inverse := CommMon_to_lax_braided C, unit_iso := unit_iso C, counit_iso := counit_iso C, } end CommMon_
6c3ebce4b9a0d86ad579a6dc20746593f1d15753
46125763b4dbf50619e8846a1371029346f4c3db
/src/group_theory/subgroup.lean
a37c9c790e711ceffc85193cf0fe25f660c9783d
[ "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
28,143
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, Mitchell Rowett, Scott Morrison, Johan Commelin, Mario Carneiro, Michael Howes -/ import group_theory.submonoid open set function variables {G : Type*} {H : Type*} {A : Type*} {a a₁ a₂ b c: G} section group variables [group G] [add_group A] @[to_additive] lemma injective_mul {a : G} : injective ((*) a) := assume a₁ a₂ h, have a⁻¹ * a * a₁ = a⁻¹ * a * a₂, by rw [mul_assoc, mul_assoc, h], by rwa [inv_mul_self, one_mul, one_mul] at this section prio set_option default_priority 100 -- see Note [default priority] /-- `s` is an additive subgroup: a set containing 0 and closed under addition and negation. -/ class is_add_subgroup (s : set A) extends is_add_submonoid s : Prop := (neg_mem {a} : a ∈ s → -a ∈ s) /-- `s` is a subgroup: a set containing 1 and closed under multiplication and inverse. -/ @[to_additive is_add_subgroup] class is_subgroup (s : set G) extends is_submonoid s : Prop := (inv_mem {a} : a ∈ s → a⁻¹ ∈ s) end prio lemma additive.is_add_subgroup (s : set G) [is_subgroup s] : @is_add_subgroup (additive G) _ s := ⟨@is_subgroup.inv_mem _ _ _ _⟩ theorem additive.is_add_subgroup_iff {s : set G} : @is_add_subgroup (additive G) _ s ↔ is_subgroup s := ⟨by rintro ⟨⟨h₁, h₂⟩, h₃⟩; exact @is_subgroup.mk G _ _ ⟨h₁, @h₂⟩ @h₃, λ h, by exactI additive.is_add_subgroup _⟩ lemma multiplicative.is_subgroup (s : set A) [is_add_subgroup s] : @is_subgroup (multiplicative A) _ s := ⟨@is_add_subgroup.neg_mem _ _ _ _⟩ theorem multiplicative.is_subgroup_iff {s : set A} : @is_subgroup (multiplicative A) _ s ↔ is_add_subgroup s := ⟨by rintro ⟨⟨h₁, h₂⟩, h₃⟩; exact @is_add_subgroup.mk A _ _ ⟨h₁, @h₂⟩ @h₃, λ h, by exactI multiplicative.is_subgroup _⟩ @[to_additive add_group] instance subtype.group {s : set G} [is_subgroup s] : group s := { inv := λ x, ⟨(x:G)⁻¹, is_subgroup.inv_mem x.2⟩, mul_left_inv := λ x, subtype.eq $ mul_left_inv x.1, .. subtype.monoid } @[to_additive add_comm_group] instance subtype.comm_group {G : Type*} [comm_group G] {s : set G} [is_subgroup s] : comm_group s := { .. subtype.group, .. subtype.comm_monoid } @[simp, to_additive] lemma is_subgroup.coe_inv {s : set G} [is_subgroup s] (a : s) : ((a⁻¹ : s) : G) = a⁻¹ := rfl @[simp] lemma is_subgroup.coe_gpow {s : set G} [is_subgroup s] (a : s) (n : ℤ) : ((a ^ n : s) : G) = a ^ n := by induction n; simp [is_submonoid.coe_pow a] @[simp] lemma is_add_subgroup.gsmul_coe {s : set A} [is_add_subgroup s] (a : s) (n : ℤ) : ((gsmul n a : s) : A) = gsmul n a := by induction n; simp [is_add_submonoid.smul_coe a] attribute [to_additive gsmul_coe] is_subgroup.coe_gpow @[to_additive of_add_neg] theorem is_subgroup.of_div (s : set G) (one_mem : (1:G) ∈ s) (div_mem : ∀{a b:G}, a ∈ s → b ∈ s → a * b⁻¹ ∈ s) : is_subgroup s := have inv_mem : ∀a, a ∈ s → a⁻¹ ∈ s, from assume a ha, have 1 * a⁻¹ ∈ s, from div_mem one_mem ha, by simpa, { inv_mem := inv_mem, mul_mem := assume a b ha hb, have a * b⁻¹⁻¹ ∈ s, from div_mem ha (inv_mem b hb), by simpa, one_mem := one_mem } theorem is_add_subgroup.of_sub (s : set A) (zero_mem : (0:A) ∈ s) (sub_mem : ∀{a b:A}, a ∈ s → b ∈ s → a - b ∈ s) : is_add_subgroup s := is_add_subgroup.of_add_neg s zero_mem (λ x y hx hy, sub_mem hx hy) @[to_additive] instance is_subgroup.inter (s₁ s₂ : set G) [is_subgroup s₁] [is_subgroup s₂] : is_subgroup (s₁ ∩ s₂) := { inv_mem := λ x hx, ⟨is_subgroup.inv_mem hx.1, is_subgroup.inv_mem hx.2⟩ } @[to_additive] instance is_subgroup.Inter {ι : Sort*} (s : ι → set G) [h : ∀ y : ι, is_subgroup (s y)] : is_subgroup (set.Inter s) := { inv_mem := λ x h, set.mem_Inter.2 $ λ y, is_subgroup.inv_mem (set.mem_Inter.1 h y) } @[to_additive is_add_subgroup_Union_of_directed] lemma is_subgroup_Union_of_directed {ι : Type*} [hι : nonempty ι] (s : ι → set G) [∀ i, is_subgroup (s i)] (directed : ∀ i j, ∃ k, s i ⊆ s k ∧ s j ⊆ s k) : is_subgroup (⋃i, s i) := { inv_mem := λ a ha, let ⟨i, hi⟩ := set.mem_Union.1 ha in set.mem_Union.2 ⟨i, is_subgroup.inv_mem hi⟩, to_is_submonoid := is_submonoid_Union_of_directed s directed } def gpowers (x : G) : set G := set.range ((^) x : ℤ → G) def gmultiples (x : A) : set A := set.range (λ i, gsmul i x) attribute [to_additive gmultiples] gpowers instance gpowers.is_subgroup (x : G) : is_subgroup (gpowers x) := { one_mem := ⟨(0:ℤ), by simp⟩, mul_mem := assume x₁ x₂ ⟨i₁, h₁⟩ ⟨i₂, h₂⟩, ⟨i₁ + i₂, by simp [gpow_add, *]⟩, inv_mem := assume x₀ ⟨i, h⟩, ⟨-i, by simp [h.symm]⟩ } instance gmultiples.is_add_subgroup (x : A) : is_add_subgroup (gmultiples x) := multiplicative.is_subgroup_iff.1 $ gpowers.is_subgroup _ attribute [to_additive is_add_subgroup] gpowers.is_subgroup lemma is_subgroup.gpow_mem {a : G} {s : set G} [is_subgroup s] (h : a ∈ s) : ∀{i:ℤ}, a ^ i ∈ s | (n : ℕ) := is_submonoid.pow_mem h | -[1+ n] := is_subgroup.inv_mem (is_submonoid.pow_mem h) lemma is_add_subgroup.gsmul_mem {a : A} {s : set A} [is_add_subgroup s] : a ∈ s → ∀{i:ℤ}, gsmul i a ∈ s := @is_subgroup.gpow_mem (multiplicative A) _ _ _ (multiplicative.is_subgroup _) lemma gpowers_subset {a : G} {s : set G} [is_subgroup s] (h : a ∈ s) : gpowers a ⊆ s := λ x hx, match x, hx with _, ⟨i, rfl⟩ := is_subgroup.gpow_mem h end lemma gmultiples_subset {a : A} {s : set A} [is_add_subgroup s] (h : a ∈ s) : gmultiples a ⊆ s := @gpowers_subset (multiplicative A) _ _ _ (multiplicative.is_subgroup _) h attribute [to_additive gmultiples_subset] gpowers_subset lemma mem_gpowers {a : G} : a ∈ gpowers a := ⟨1, by simp⟩ lemma mem_gmultiples {a : A} : a ∈ gmultiples a := ⟨1, by simp⟩ attribute [to_additive mem_gmultiples] mem_gpowers end group namespace is_subgroup open is_submonoid variables [group G] (s : set G) [is_subgroup s] @[to_additive] lemma inv_mem_iff : a⁻¹ ∈ s ↔ a ∈ s := ⟨λ h, by simpa using inv_mem h, inv_mem⟩ @[to_additive] lemma mul_mem_cancel_left (h : a ∈ s) : b * a ∈ s ↔ b ∈ s := ⟨λ hba, by simpa using mul_mem hba (inv_mem h), λ hb, mul_mem hb h⟩ @[to_additive] lemma mul_mem_cancel_right (h : a ∈ s) : a * b ∈ s ↔ b ∈ s := ⟨λ hab, by simpa using mul_mem (inv_mem h) hab, mul_mem h⟩ end is_subgroup theorem is_add_subgroup.sub_mem {A} [add_group A] (s : set A) [is_add_subgroup s] (a b : A) (ha : a ∈ s) (hb : b ∈ s) : a - b ∈ s := is_add_submonoid.add_mem ha (is_add_subgroup.neg_mem hb) section prio set_option default_priority 100 -- see Note [default priority] class normal_add_subgroup [add_group A] (s : set A) extends is_add_subgroup s : Prop := (normal : ∀ n ∈ s, ∀ g : A, g + n - g ∈ s) @[to_additive normal_add_subgroup] class normal_subgroup [group G] (s : set G) extends is_subgroup s : Prop := (normal : ∀ n ∈ s, ∀ g : G, g * n * g⁻¹ ∈ s) end prio @[to_additive normal_add_subgroup_of_add_comm_group] lemma normal_subgroup_of_comm_group [comm_group G] (s : set G) [hs : is_subgroup s] : normal_subgroup s := { normal := λ n hn g, by rwa [mul_right_comm, mul_right_inv, one_mul], ..hs } lemma additive.normal_add_subgroup [group G] (s : set G) [normal_subgroup s] : @normal_add_subgroup (additive G) _ s := @normal_add_subgroup.mk (additive G) _ _ (@additive.is_add_subgroup G _ _ _) (@normal_subgroup.normal _ _ _ _) theorem additive.normal_add_subgroup_iff [group G] {s : set G} : @normal_add_subgroup (additive G) _ s ↔ normal_subgroup s := ⟨by rintro ⟨h₁, h₂⟩; exact @normal_subgroup.mk G _ _ (additive.is_add_subgroup_iff.1 h₁) @h₂, λ h, by exactI additive.normal_add_subgroup _⟩ lemma multiplicative.normal_subgroup [add_group A] (s : set A) [normal_add_subgroup s] : @normal_subgroup (multiplicative A) _ s := @normal_subgroup.mk (multiplicative A) _ _ (@multiplicative.is_subgroup A _ _ _) (@normal_add_subgroup.normal _ _ _ _) theorem multiplicative.normal_subgroup_iff [add_group A] {s : set A} : @normal_subgroup (multiplicative A) _ s ↔ normal_add_subgroup s := ⟨by rintro ⟨h₁, h₂⟩; exact @normal_add_subgroup.mk A _ _ (multiplicative.is_subgroup_iff.1 h₁) @h₂, λ h, by exactI multiplicative.normal_subgroup _⟩ namespace is_subgroup variable [group G] -- Normal subgroup properties @[to_additive] lemma mem_norm_comm {s : set G} [normal_subgroup s] {a b : G} (hab : a * b ∈ s) : b * a ∈ s := have h : a⁻¹ * (a * b) * a⁻¹⁻¹ ∈ s, from normal_subgroup.normal (a * b) hab a⁻¹, by simp at h; exact h @[to_additive] lemma mem_norm_comm_iff {s : set G} [normal_subgroup s] {a b : G} : a * b ∈ s ↔ b * a ∈ s := ⟨mem_norm_comm, mem_norm_comm⟩ /-- The trivial subgroup -/ @[to_additive] def trivial (G : Type*) [group G] : set G := {1} @[simp, to_additive] lemma mem_trivial {g : G} : g ∈ trivial G ↔ g = 1 := mem_singleton_iff @[to_additive] instance trivial_normal : normal_subgroup (trivial G) := by refine {..}; simp [trivial] {contextual := tt} @[to_additive] lemma eq_trivial_iff {s : set G} [is_subgroup s] : s = trivial G ↔ (∀ x ∈ s, x = (1 : G)) := by simp only [set.ext_iff, is_subgroup.mem_trivial]; exact ⟨λ h x, (h x).1, λ h x, ⟨h x, λ hx, hx.symm ▸ is_submonoid.one_mem s⟩⟩ @[to_additive] instance univ_subgroup : normal_subgroup (@univ G) := by refine {..}; simp @[to_additive add_center] def center (G : Type*) [group G] : set G := {z | ∀ g, g * z = z * g} @[to_additive mem_add_center] lemma mem_center {a : G} : a ∈ center G ↔ ∀g, g * a = a * g := iff.rfl @[to_additive add_center_normal] instance center_normal : normal_subgroup (center G) := { one_mem := by simp [center], mul_mem := assume a b ha hb g, by rw [←mul_assoc, mem_center.2 ha g, mul_assoc, mem_center.2 hb g, ←mul_assoc], inv_mem := assume a ha g, calc g * a⁻¹ = a⁻¹ * (g * a) * a⁻¹ : by simp [ha g] ... = a⁻¹ * g : by rw [←mul_assoc, mul_assoc]; simp, normal := assume n ha g h, calc h * (g * n * g⁻¹) = h * n : by simp [ha g, mul_assoc] ... = g * g⁻¹ * n * h : by rw ha h; simp ... = g * n * g⁻¹ * h : by rw [mul_assoc g, ha g⁻¹, ←mul_assoc] } @[to_additive add_normalizer] def normalizer (s : set G) : set G := {g : G | ∀ n, n ∈ s ↔ g * n * g⁻¹ ∈ s} @[to_additive normalizer_is_add_subgroup] instance normalizer_is_subgroup (s : set G) : is_subgroup (normalizer s) := { one_mem := by simp [normalizer], mul_mem := λ a b (ha : ∀ n, n ∈ s ↔ a * n * a⁻¹ ∈ s) (hb : ∀ n, n ∈ s ↔ b * n * b⁻¹ ∈ s) n, by rw [mul_inv_rev, ← mul_assoc, mul_assoc a, mul_assoc a, ← ha, ← hb], inv_mem := λ a (ha : ∀ n, n ∈ s ↔ a * n * a⁻¹ ∈ s) n, by rw [ha (a⁻¹ * n * a⁻¹⁻¹)]; simp [mul_assoc] } @[to_additive subset_add_normalizer] lemma subset_normalizer (s : set G) [is_subgroup s] : s ⊆ normalizer s := λ g hg n, by rw [is_subgroup.mul_mem_cancel_left _ ((is_subgroup.inv_mem_iff _).2 hg), is_subgroup.mul_mem_cancel_right _ hg] /-- Every subgroup is a normal subgroup of its normalizer -/ @[to_additive add_normal_in_add_normalizer] instance normal_in_normalizer (s : set G) [is_subgroup s] : normal_subgroup (subtype.val ⁻¹' s : set (normalizer s)) := { one_mem := show (1 : G) ∈ s, from is_submonoid.one_mem _, mul_mem := λ a b ha hb, show (a * b : G) ∈ s, from is_submonoid.mul_mem ha hb, inv_mem := λ a ha, show (a⁻¹ : G) ∈ s, from is_subgroup.inv_mem ha, normal := λ a ha ⟨m, hm⟩, (hm a).1 ha } end is_subgroup -- Homomorphism subgroups namespace is_group_hom open is_submonoid is_subgroup open is_mul_hom (map_mul) @[to_additive] def ker [group H] (f : G → H) : set G := preimage f (trivial H) @[to_additive] lemma mem_ker [group H] (f : G → H) {x : G} : x ∈ ker f ↔ f x = 1 := mem_trivial variables [group G] [group H] @[to_additive] lemma one_ker_inv (f : G → H) [is_group_hom f] {a b : G} (h : f (a * b⁻¹) = 1) : f a = f b := begin rw [map_mul f, map_inv f] at h, rw [←inv_inv (f b), eq_inv_of_mul_eq_one h] end @[to_additive] lemma one_ker_inv' (f : G → H) [is_group_hom f] {a b : G} (h : f (a⁻¹ * b) = 1) : f a = f b := begin rw [map_mul f, map_inv f] at h, apply eq_of_inv_eq_inv, rw eq_inv_of_mul_eq_one h end @[to_additive] lemma inv_ker_one (f : G → H) [is_group_hom f] {a b : G} (h : f a = f b) : f (a * b⁻¹) = 1 := have f a * (f b)⁻¹ = 1, by rw [h, mul_right_inv], by rwa [←map_inv f, ←map_mul f] at this @[to_additive] lemma inv_ker_one' (f : G → H) [is_group_hom f] {a b : G} (h : f a = f b) : f (a⁻¹ * b) = 1 := have (f a)⁻¹ * f b = 1, by rw [h, mul_left_inv], by rwa [←map_inv f, ←map_mul f] at this @[to_additive] lemma one_iff_ker_inv (f : G → H) [is_group_hom f] (a b : G) : f a = f b ↔ f (a * b⁻¹) = 1 := ⟨inv_ker_one f, one_ker_inv f⟩ @[to_additive] lemma one_iff_ker_inv' (f : G → H) [is_group_hom f] (a b : G) : f a = f b ↔ f (a⁻¹ * b) = 1 := ⟨inv_ker_one' f, one_ker_inv' f⟩ @[to_additive] lemma inv_iff_ker (f : G → H) [w : is_group_hom f] (a b : G) : f a = f b ↔ a * b⁻¹ ∈ ker f := by rw [mem_ker]; exact one_iff_ker_inv _ _ _ @[to_additive] lemma inv_iff_ker' (f : G → H) [w : is_group_hom f] (a b : G) : f a = f b ↔ a⁻¹ * b ∈ ker f := by rw [mem_ker]; exact one_iff_ker_inv' _ _ _ @[to_additive image_add_subgroup] instance image_subgroup (f : G → H) [is_group_hom f] (s : set G) [is_subgroup s] : is_subgroup (f '' s) := { mul_mem := assume a₁ a₂ ⟨b₁, hb₁, eq₁⟩ ⟨b₂, hb₂, eq₂⟩, ⟨b₁ * b₂, mul_mem hb₁ hb₂, by simp [eq₁, eq₂, map_mul f]⟩, one_mem := ⟨1, one_mem s, map_one f⟩, inv_mem := assume a ⟨b, hb, eq⟩, ⟨b⁻¹, inv_mem hb, by rw map_inv f; simp *⟩ } @[to_additive range_add_subgroup] instance range_subgroup (f : G → H) [is_group_hom f] : is_subgroup (set.range f) := @set.image_univ _ _ f ▸ is_group_hom.image_subgroup f set.univ local attribute [simp] one_mem inv_mem mul_mem normal_subgroup.normal @[to_additive] instance preimage (f : G → H) [is_group_hom f] (s : set H) [is_subgroup s] : is_subgroup (f ⁻¹' s) := by refine {..}; simp [map_mul f, map_one f, map_inv f, @inv_mem H _ s] {contextual:=tt} @[to_additive] instance preimage_normal (f : G → H) [is_group_hom f] (s : set H) [normal_subgroup s] : normal_subgroup (f ⁻¹' s) := ⟨by simp [map_mul f, map_inv f] {contextual:=tt}⟩ @[to_additive] instance normal_subgroup_ker (f : G → H) [is_group_hom f] : normal_subgroup (ker f) := is_group_hom.preimage_normal f (trivial H) @[to_additive] lemma inj_of_trivial_ker (f : G → H) [is_group_hom f] (h : ker f = trivial G) : function.injective f := begin intros a₁ a₂ hfa, simp [ext_iff, ker, is_subgroup.trivial] at h, have ha : a₁ * a₂⁻¹ = 1, by rw ←h; exact inv_ker_one f hfa, rw [eq_inv_of_mul_eq_one ha, inv_inv a₂] end @[to_additive] lemma trivial_ker_of_inj (f : G → H) [is_group_hom f] (h : function.injective f) : ker f = trivial G := set.ext $ assume x, iff.intro (assume hx, suffices f x = f 1, by simpa using h this, by simp [map_one f]; rwa [mem_ker] at hx) (by simp [mem_ker, is_group_hom.map_one f] {contextual := tt}) @[to_additive] lemma inj_iff_trivial_ker (f : G → H) [is_group_hom f] : function.injective f ↔ ker f = trivial G := ⟨trivial_ker_of_inj f, inj_of_trivial_ker f⟩ @[to_additive] lemma trivial_ker_iff_eq_one (f : G → H) [is_group_hom f] : ker f = trivial G ↔ ∀ x, f x = 1 → x = 1 := by rw set.ext_iff; simp [ker]; exact ⟨λ h x hx, (h x).1 hx, λ h x, ⟨h x, λ hx, by rw [hx, map_one f]⟩⟩ end is_group_hom @[to_additive is_add_group_hom] instance subtype_val.is_group_hom [group G] {s : set G} [is_subgroup s] : is_group_hom (subtype.val : s → G) := { ..subtype_val.is_monoid_hom } @[to_additive is_add_group_hom] instance coe.is_group_hom [group G] {s : set G} [is_subgroup s] : is_group_hom (coe : s → G) := { ..subtype_val.is_monoid_hom } @[to_additive is_add_group_hom] instance subtype_mk.is_group_hom [group G] [group H] {s : set G} [is_subgroup s] (f : H → G) [is_group_hom f] (h : ∀ x, f x ∈ s) : is_group_hom (λ x, (⟨f x, h x⟩ : s)) := { ..subtype_mk.is_monoid_hom f h } @[to_additive is_add_group_hom] instance set_inclusion.is_group_hom [group G] {s t : set G} [is_subgroup s] [is_subgroup t] (h : s ⊆ t) : is_group_hom (set.inclusion h) := subtype_mk.is_group_hom _ _ namespace add_group variables [add_group A] inductive in_closure (s : set A) : A → Prop | basic {a : A} : a ∈ s → in_closure a | zero : in_closure 0 | neg {a : A} : in_closure a → in_closure (-a) | add {a b : A} : in_closure a → in_closure b → in_closure (a + b) end add_group namespace group open is_submonoid is_subgroup variables [group G] {s : set G} @[to_additive] inductive in_closure (s : set G) : G → Prop | basic {a : G} : a ∈ s → in_closure a | one : in_closure 1 | inv {a : G} : in_closure a → in_closure a⁻¹ | mul {a b : G} : in_closure a → in_closure b → in_closure (a * b) /-- `group.closure s` is the subgroup closed over `s`, i.e. the smallest subgroup containg s. -/ @[to_additive] def closure (s : set G) : set G := {a | in_closure s a } @[to_additive] lemma mem_closure {a : G} : a ∈ s → a ∈ closure s := in_closure.basic @[to_additive is_add_subgroup] instance closure.is_subgroup (s : set G) : is_subgroup (closure s) := { one_mem := in_closure.one s, mul_mem := assume a b, in_closure.mul, inv_mem := assume a, in_closure.inv } @[to_additive] theorem subset_closure {s : set G} : s ⊆ closure s := λ a, mem_closure @[to_additive] theorem closure_subset {s t : set G} [is_subgroup t] (h : s ⊆ t) : closure s ⊆ t := assume a ha, by induction ha; simp [h _, *, one_mem, mul_mem, inv_mem_iff] @[to_additive] lemma closure_subset_iff (s t : set G) [is_subgroup t] : closure s ⊆ t ↔ s ⊆ t := ⟨assume h b ha, h (mem_closure ha), assume h b ha, closure_subset h ha⟩ @[to_additive] theorem closure_mono {s t : set G} (h : s ⊆ t) : closure s ⊆ closure t := closure_subset $ set.subset.trans h subset_closure @[simp, to_additive closure_add_subgroup] lemma closure_subgroup (s : set G) [is_subgroup s] : closure s = s := set.subset.antisymm (closure_subset $ set.subset.refl s) subset_closure @[to_additive] theorem exists_list_of_mem_closure {s : set G} {a : G} (h : a ∈ closure s) : (∃l:list G, (∀x∈l, x ∈ s ∨ x⁻¹ ∈ s) ∧ l.prod = a) := in_closure.rec_on h (λ x hxs, ⟨[x], list.forall_mem_singleton.2 $ or.inl hxs, one_mul _⟩) ⟨[], list.forall_mem_nil _, rfl⟩ (λ x _ ⟨L, HL1, HL2⟩, ⟨L.reverse.map has_inv.inv, λ x hx, let ⟨y, hy1, hy2⟩ := list.exists_of_mem_map hx in hy2 ▸ or.imp id (by rw [inv_inv]; exact id) (HL1 _ $ list.mem_reverse.1 hy1).symm, HL2 ▸ list.rec_on L one_inv.symm (λ hd tl ih, by rw [list.reverse_cons, list.map_append, list.prod_append, ih, list.map_singleton, list.prod_cons, list.prod_nil, mul_one, list.prod_cons, mul_inv_rev])⟩) (λ x y hx hy ⟨L1, HL1, HL2⟩ ⟨L2, HL3, HL4⟩, ⟨L1 ++ L2, list.forall_mem_append.2 ⟨HL1, HL3⟩, by rw [list.prod_append, HL2, HL4]⟩) @[to_additive] lemma image_closure [group H] (f : G → H) [is_group_hom f] (s : set G) : f '' closure s = closure (f '' s) := le_antisymm begin rintros _ ⟨x, hx, rfl⟩, apply in_closure.rec_on hx; intros, { solve_by_elim [subset_closure, set.mem_image_of_mem] }, { rw [is_monoid_hom.map_one f], apply is_submonoid.one_mem }, { rw [is_group_hom.map_inv f], apply is_subgroup.inv_mem, assumption }, { rw [is_monoid_hom.map_mul f], solve_by_elim [is_submonoid.mul_mem] } end (closure_subset $ set.image_subset _ subset_closure) @[to_additive] theorem mclosure_subset {s : set G} : monoid.closure s ⊆ closure s := monoid.closure_subset $ subset_closure @[to_additive] theorem mclosure_inv_subset {s : set G} : monoid.closure (has_inv.inv ⁻¹' s) ⊆ closure s := monoid.closure_subset $ λ x hx, inv_inv x ▸ (is_subgroup.inv_mem $ subset_closure hx) @[to_additive] theorem closure_eq_mclosure {s : set G} : closure s = monoid.closure (s ∪ has_inv.inv ⁻¹' s) := set.subset.antisymm (@closure_subset _ _ _ (monoid.closure (s ∪ has_inv.inv ⁻¹' s)) { inv_mem := λ x hx, monoid.in_closure.rec_on hx (λ x hx, or.cases_on hx (λ hx, monoid.subset_closure $ or.inr $ show x⁻¹⁻¹ ∈ s, from (inv_inv x).symm ▸ hx) (λ hx, monoid.subset_closure $ or.inl hx)) ((@one_inv G _).symm ▸ is_submonoid.one_mem _) (λ x y hx hy ihx ihy, (mul_inv_rev x y).symm ▸ is_submonoid.mul_mem ihy ihx) } (set.subset.trans (set.subset_union_left _ _) monoid.subset_closure)) (monoid.closure_subset $ set.union_subset subset_closure $ λ x hx, inv_inv x ▸ (is_subgroup.inv_mem $ subset_closure hx)) @[to_additive] theorem mem_closure_union_iff {G : Type*} [comm_group G] {s t : set G} {x : G} : x ∈ closure (s ∪ t) ↔ ∃ y ∈ closure s, ∃ z ∈ closure t, y * z = x := begin simp only [closure_eq_mclosure, monoid.mem_closure_union_iff, exists_prop, preimage_union], split, { rintro ⟨_, ⟨ys, hys, yt, hyt, rfl⟩, _, ⟨zs, hzs, zt, hzt, rfl⟩, rfl⟩, refine ⟨_, ⟨_, hys, _, hzs, rfl⟩, _, ⟨_, hyt, _, hzt, rfl⟩, _⟩, rw [mul_assoc, mul_assoc, mul_left_comm zs], refl }, { rintro ⟨_, ⟨ys, hys, zs, hzs, rfl⟩, _, ⟨yt, hyt, zt, hzt, rfl⟩, rfl⟩, refine ⟨_, ⟨ys, hys, yt, hyt, rfl⟩, _, ⟨zs, hzs, zt, hzt, rfl⟩, _⟩, rw [mul_assoc, mul_assoc, mul_left_comm yt], refl } end @[to_additive gmultiples_eq_closure] theorem gpowers_eq_closure {a : G} : gpowers a = closure {a} := subset.antisymm (gpowers_subset $ mem_closure $ by simp) (closure_subset $ by simp [mem_gpowers]) end group namespace is_subgroup variable [group G] @[to_additive] lemma trivial_eq_closure : trivial G = group.closure ∅ := subset.antisymm (by simp [set.subset_def, is_submonoid.one_mem]) (group.closure_subset $ by simp) end is_subgroup /-The normal closure of a set s is the subgroup closure of all the conjugates of elements of s. It is the smallest normal subgroup containing s. -/ namespace group variables {s : set G} [group G] /-- Given an element a, conjugates a is the set of conjugates. -/ def conjugates (a : G) : set G := {b | is_conj a b} lemma mem_conjugates_self {a : G} : a ∈ conjugates a := is_conj_refl _ /-- Given a set s, conjugates_of_set s is the set of all conjugates of the elements of s. -/ def conjugates_of_set (s : set G) : set G := ⋃ a ∈ s, conjugates a lemma mem_conjugates_of_set_iff {x : G} : x ∈ conjugates_of_set s ↔ ∃ a ∈ s, is_conj a x := set.mem_bUnion_iff theorem subset_conjugates_of_set : s ⊆ conjugates_of_set s := λ (x : G) (h : x ∈ s), mem_conjugates_of_set_iff.2 ⟨x, h, is_conj_refl _⟩ theorem conjugates_of_set_mono {s t : set G} (h : s ⊆ t) : conjugates_of_set s ⊆ conjugates_of_set t := set.bUnion_subset_bUnion_left h lemma conjugates_subset {t : set G} [normal_subgroup t] {a : G} (h : a ∈ t) : conjugates a ⊆ t := λ x ⟨c,w⟩, begin have H := normal_subgroup.normal a h c, rwa ←w, end theorem conjugates_of_set_subset {s t : set G} [normal_subgroup t] (h : s ⊆ t) : conjugates_of_set s ⊆ t := set.bUnion_subset (λ x H, conjugates_subset (h H)) /-- The set of conjugates of s is closed under conjugation. -/ lemma conj_mem_conjugates_of_set {x c : G} : x ∈ conjugates_of_set s → (c * x * c⁻¹ ∈ conjugates_of_set s) := λ H, begin rcases (mem_conjugates_of_set_iff.1 H) with ⟨a,h₁,h₂⟩, exact mem_conjugates_of_set_iff.2 ⟨a, h₁, is_conj_trans h₂ ⟨c,rfl⟩⟩, end /-- The normal closure of a set s is the subgroup closure of all the conjugates of elements of s. It is the smallest normal subgroup containing s. -/ def normal_closure (s : set G) : set G := closure (conjugates_of_set s) theorem conjugates_of_set_subset_normal_closure : conjugates_of_set s ⊆ normal_closure s := subset_closure theorem subset_normal_closure : s ⊆ normal_closure s := set.subset.trans subset_conjugates_of_set conjugates_of_set_subset_normal_closure /-- The normal closure of a set is a subgroup. -/ instance normal_closure.is_subgroup (s : set G) : is_subgroup (normal_closure s) := closure.is_subgroup (conjugates_of_set s) /-- The normal closure of s is a normal subgroup. -/ instance normal_closure.is_normal : normal_subgroup (normal_closure s) := ⟨ λ n h g, begin induction h with x hx x hx ihx x y hx hy ihx ihy, {exact (conjugates_of_set_subset_normal_closure (conj_mem_conjugates_of_set hx))}, {simpa using (normal_closure.is_subgroup s).one_mem}, {rw ←conj_inv, exact (is_subgroup.inv_mem ihx)}, {rw ←conj_mul, exact (is_submonoid.mul_mem ihx ihy)}, end ⟩ /-- The normal closure of s is the smallest normal subgroup containing s. -/ theorem normal_closure_subset {s t : set G} [normal_subgroup t] (h : s ⊆ t) : normal_closure s ⊆ t := λ a w, begin induction w with x hx x hx ihx x y hx hy ihx ihy, {exact (conjugates_of_set_subset h $ hx)}, {exact is_submonoid.one_mem t}, {exact is_subgroup.inv_mem ihx}, {exact is_submonoid.mul_mem ihx ihy} end lemma normal_closure_subset_iff {s t : set G} [normal_subgroup t] : s ⊆ t ↔ normal_closure s ⊆ t := ⟨normal_closure_subset, set.subset.trans (subset_normal_closure)⟩ theorem normal_closure_mono {s t : set G} : s ⊆ t → normal_closure s ⊆ normal_closure t := λ h, normal_closure_subset (set.subset.trans h (subset_normal_closure)) end group section simple_group class simple_group (G : Type*) [group G] : Prop := (simple : ∀ (N : set G) [normal_subgroup N], N = is_subgroup.trivial G ∨ N = set.univ) class simple_add_group (A : Type*) [add_group A] : Prop := (simple : ∀ (N : set A) [normal_add_subgroup N], N = is_add_subgroup.trivial A ∨ N = set.univ) attribute [to_additive simple_add_group] simple_group theorem additive.simple_add_group_iff [group G] : simple_add_group (additive G) ↔ simple_group G := ⟨λ hs, ⟨λ N h, @simple_add_group.simple _ _ hs _ (by exactI additive.normal_add_subgroup_iff.2 h)⟩, λ hs, ⟨λ N h, @simple_group.simple _ _ hs _ (by exactI additive.normal_add_subgroup_iff.1 h)⟩⟩ instance additive.simple_add_group [group G] [simple_group G] : simple_add_group (additive G) := additive.simple_add_group_iff.2 (by apply_instance) theorem multiplicative.simple_group_iff [add_group A] : simple_group (multiplicative A) ↔ simple_add_group A := ⟨λ hs, ⟨λ N h, @simple_group.simple _ _ hs _ (by exactI multiplicative.normal_subgroup_iff.2 h)⟩, λ hs, ⟨λ N h, @simple_add_group.simple _ _ hs _ (by exactI multiplicative.normal_subgroup_iff.1 h)⟩⟩ instance multiplicative.simple_group [add_group A] [simple_add_group A] : simple_group (multiplicative A) := multiplicative.simple_group_iff.2 (by apply_instance) @[to_additive simple_add_group_of_surjective] lemma simple_group_of_surjective [group G] [group H] [simple_group G] (f : G → H) [is_group_hom f] (hf : function.surjective f) : simple_group H := ⟨λ H iH, have normal_subgroup (f ⁻¹' H), by resetI; apply_instance, begin resetI, cases simple_group.simple (f ⁻¹' H) with h h, { refine or.inl (is_subgroup.eq_trivial_iff.2 (λ x hx, _)), cases hf x with y hy, rw ← hy at hx, rw [← hy, is_subgroup.eq_trivial_iff.1 h y hx, is_group_hom.map_one f] }, { refine or.inr (set.eq_univ_of_forall (λ x, _)), cases hf x with y hy, rw set.eq_univ_iff_forall at h, rw ← hy, exact h y } end⟩ end simple_group
c72156f40728d42be45108d8ef92f4b1f82c37d5
6afa22d5eee6e9a56b6a2f1210eca8f7a1067466
/tests/lean/run/check_constants.lean
2182c29933bf4e7474cfa68f22638975e10ef837
[ "Apache-2.0" ]
permissive
giordano/lean
72a1fabfeb2f1ccfd38673e2719a719cd6ffbb40
56f8877f1efa22215aca0b82f1c0ce2ff975b9c3
refs/heads/master
1,663,091,511,168
1,590,688,082,000
1,590,688,082,000
268,183,678
0
0
Apache-2.0
1,590,885,425,000
1,590,885,424,000
null
UTF-8
Lean
false
false
11,413
lean
-- DO NOT EDIT, automatically generated file, generator scripts/gen_constants_cpp.py import smt system.io open tactic meta def script_check_id (n : name) : tactic unit := do env ← get_env, (env^.get n >> return ()) <|> (guard $ env^.is_namespace n) <|> (attribute.get_instances n >> return ()) <|> fail ("identifier '" ++ to_string n ++ "' is not a constant, namespace nor attribute") run_cmd script_check_id `absurd run_cmd script_check_id `acc.cases_on run_cmd script_check_id `acc.rec run_cmd script_check_id `and run_cmd script_check_id `and.cases_on run_cmd script_check_id `and.elim_left run_cmd script_check_id `and.elim_right run_cmd script_check_id `and.intro run_cmd script_check_id `and.rec run_cmd script_check_id `auto_param run_cmd script_check_id `bin_tree.empty run_cmd script_check_id `bin_tree.leaf run_cmd script_check_id `bin_tree.node run_cmd script_check_id `bit0 run_cmd script_check_id `bit1 run_cmd script_check_id `bool run_cmd script_check_id `bool.ff run_cmd script_check_id `bool.tt run_cmd script_check_id `cast run_cmd script_check_id `cast_heq run_cmd script_check_id `char run_cmd script_check_id `char.mk run_cmd script_check_id `char.ne_of_vne run_cmd script_check_id `char.of_nat run_cmd script_check_id `char.of_nat_ne_of_ne run_cmd script_check_id `coe run_cmd script_check_id `coe_fn run_cmd script_check_id `coe_sort run_cmd script_check_id `coe_to_lift run_cmd script_check_id `combinator.K run_cmd script_check_id `congr run_cmd script_check_id `congr_arg run_cmd script_check_id `congr_fun run_cmd script_check_id `decidable run_cmd script_check_id `decidable.to_bool run_cmd script_check_id `dite run_cmd script_check_id `empty run_cmd script_check_id `eq run_cmd script_check_id `eq.cases_on run_cmd script_check_id `eq.drec run_cmd script_check_id `eq.mp run_cmd script_check_id `eq.mpr run_cmd script_check_id `eq.rec run_cmd script_check_id `eq.refl run_cmd script_check_id `eq.subst run_cmd script_check_id `eq.symm run_cmd script_check_id `eq.trans run_cmd script_check_id `eq_false_intro run_cmd script_check_id `eq_of_heq run_cmd script_check_id `eq_rec_heq run_cmd script_check_id `eq_self_iff_true run_cmd script_check_id `eq_true_intro run_cmd script_check_id `Exists run_cmd script_check_id `expr run_cmd script_check_id `expr.subst run_cmd script_check_id `false run_cmd script_check_id `false.rec run_cmd script_check_id `false_of_true_eq_false run_cmd script_check_id `false_of_true_iff_false run_cmd script_check_id `fin.mk run_cmd script_check_id `fin.ne_of_vne run_cmd script_check_id `forall_congr run_cmd script_check_id `forall_congr_eq run_cmd script_check_id `forall_not_of_not_exists run_cmd script_check_id `format run_cmd script_check_id `funext run_cmd script_check_id `has_add run_cmd script_check_id `has_add.add run_cmd script_check_id `has_andthen.andthen run_cmd script_check_id `has_bind.and_then run_cmd script_check_id `has_bind.seq run_cmd script_check_id `has_coe_t run_cmd script_check_id `has_div run_cmd script_check_id `has_div.div run_cmd script_check_id `has_emptyc.emptyc run_cmd script_check_id `has_insert.insert run_cmd script_check_id `has_inv run_cmd script_check_id `has_inv.inv run_cmd script_check_id `has_le run_cmd script_check_id `has_le.le run_cmd script_check_id `has_lt run_cmd script_check_id `has_lt.lt run_cmd script_check_id `has_mul run_cmd script_check_id `has_mul.mul run_cmd script_check_id `has_neg run_cmd script_check_id `has_neg.neg run_cmd script_check_id `has_one run_cmd script_check_id `has_one.one run_cmd script_check_id `has_orelse.orelse run_cmd script_check_id `has_repr run_cmd script_check_id `has_sep.sep run_cmd script_check_id `has_singleton.singleton run_cmd script_check_id `has_sizeof run_cmd script_check_id `has_sizeof.mk run_cmd script_check_id `has_sub run_cmd script_check_id `has_sub.sub run_cmd script_check_id `has_to_format run_cmd script_check_id `has_well_founded run_cmd script_check_id `has_well_founded.r run_cmd script_check_id `has_well_founded.wf run_cmd script_check_id `has_zero run_cmd script_check_id `has_zero.zero run_cmd script_check_id `heq run_cmd script_check_id `heq.refl run_cmd script_check_id `heq.symm run_cmd script_check_id `heq.trans run_cmd script_check_id `heq_of_eq run_cmd script_check_id `hole_command run_cmd script_check_id `id run_cmd script_check_id `id_delta run_cmd script_check_id `id_rhs run_cmd script_check_id `if_neg run_cmd script_check_id `if_pos run_cmd script_check_id `iff run_cmd script_check_id `iff.intro run_cmd script_check_id `iff.mp run_cmd script_check_id `iff.mpr run_cmd script_check_id `iff.refl run_cmd script_check_id `iff.symm run_cmd script_check_id `iff.trans run_cmd script_check_id `iff_false_intro run_cmd script_check_id `iff_true_intro run_cmd script_check_id `imp_congr run_cmd script_check_id `imp_congr_ctx run_cmd script_check_id `imp_congr_ctx_eq run_cmd script_check_id `imp_congr_eq run_cmd script_check_id `implies run_cmd script_check_id `implies_of_if_neg run_cmd script_check_id `implies_of_if_pos run_cmd script_check_id `int run_cmd script_check_id `int.bit0_nonneg run_cmd script_check_id `int.bit0_pos run_cmd script_check_id `int.bit1_nonneg run_cmd script_check_id `int.bit1_pos run_cmd script_check_id `int.nat_abs_bit0_step run_cmd script_check_id `int.nat_abs_bit1_nonneg_step run_cmd script_check_id `int.nat_abs_one run_cmd script_check_id `int.nat_abs_zero run_cmd script_check_id `int.ne_neg_of_ne run_cmd script_check_id `int.ne_neg_of_pos run_cmd script_check_id `int.ne_of_nat_ne_nonneg_case run_cmd script_check_id `int.neg_ne_of_pos run_cmd script_check_id `int.neg_ne_zero_of_ne run_cmd script_check_id `int.one_nonneg run_cmd script_check_id `int.one_pos run_cmd script_check_id `int.zero_ne_neg_of_ne run_cmd script_check_id `int.zero_nonneg run_cmd script_check_id `interactive.executor run_cmd script_check_id `interactive.param_desc run_cmd script_check_id `interactive.parse run_cmd script_check_id `io run_cmd script_check_id `io_core run_cmd script_check_id `io_rand_nat run_cmd script_check_id `is_associative run_cmd script_check_id `is_associative.assoc run_cmd script_check_id `is_commutative run_cmd script_check_id `is_commutative.comm run_cmd script_check_id `is_valid_char_range_1 run_cmd script_check_id `is_valid_char_range_2 run_cmd script_check_id `ite run_cmd script_check_id `lean.parser run_cmd script_check_id `lean.parser.pexpr run_cmd script_check_id `lean.parser.reflectable.expr run_cmd script_check_id `lean.parser.tk run_cmd script_check_id `left_comm run_cmd script_check_id `list run_cmd script_check_id `list.cons run_cmd script_check_id `list.nil run_cmd script_check_id `match_failed run_cmd script_check_id `monad run_cmd script_check_id `monad_fail run_cmd script_check_id `monad_from_pure_bind run_cmd script_check_id `monad_io_environment_impl run_cmd script_check_id `monad_io_file_system_impl run_cmd script_check_id `monad_io_impl run_cmd script_check_id `monad_io_net_system_impl run_cmd script_check_id `monad_io_process_impl run_cmd script_check_id `monad_io_random_impl run_cmd script_check_id `monad_io_terminal_impl run_cmd script_check_id `name.anonymous run_cmd script_check_id `name.mk_numeral run_cmd script_check_id `name.mk_string run_cmd script_check_id `nat run_cmd script_check_id `nat.add run_cmd script_check_id `nat.bit0_lt run_cmd script_check_id `nat.bit0_lt_bit1 run_cmd script_check_id `nat.bit0_ne run_cmd script_check_id `nat.bit0_ne_bit1 run_cmd script_check_id `nat.bit0_ne_one run_cmd script_check_id `nat.bit0_ne_zero run_cmd script_check_id `nat.bit1_lt run_cmd script_check_id `nat.bit1_lt_bit0 run_cmd script_check_id `nat.bit1_ne run_cmd script_check_id `nat.bit1_ne_bit0 run_cmd script_check_id `nat.bit1_ne_one run_cmd script_check_id `nat.bit1_ne_zero run_cmd script_check_id `nat.cases_on run_cmd script_check_id `nat.has_add run_cmd script_check_id `nat.has_one run_cmd script_check_id `nat.has_zero run_cmd script_check_id `nat.le_of_lt run_cmd script_check_id `nat.le_refl run_cmd script_check_id `nat.one_lt_bit0 run_cmd script_check_id `nat.one_lt_bit1 run_cmd script_check_id `nat.one_ne_bit0 run_cmd script_check_id `nat.one_ne_bit1 run_cmd script_check_id `nat.one_ne_zero run_cmd script_check_id `nat.succ run_cmd script_check_id `nat.zero run_cmd script_check_id `nat.zero_lt_bit0 run_cmd script_check_id `nat.zero_lt_bit1 run_cmd script_check_id `nat.zero_lt_one run_cmd script_check_id `nat.zero_ne_bit0 run_cmd script_check_id `nat.zero_ne_bit1 run_cmd script_check_id `nat.zero_ne_one run_cmd script_check_id `ne run_cmd script_check_id `neq_of_not_iff run_cmd script_check_id `not run_cmd script_check_id `not_of_eq_false run_cmd script_check_id `not_of_iff_false run_cmd script_check_id `of_eq_true run_cmd script_check_id `of_iff_true run_cmd script_check_id `opt_param run_cmd script_check_id `or run_cmd script_check_id `out_param run_cmd script_check_id `partial_order run_cmd script_check_id `pprod run_cmd script_check_id `pprod.fst run_cmd script_check_id `pprod.mk run_cmd script_check_id `pprod.snd run_cmd script_check_id `prod.mk run_cmd script_check_id `propext run_cmd script_check_id `psigma run_cmd script_check_id `psigma.cases_on run_cmd script_check_id `psigma.fst run_cmd script_check_id `psigma.mk run_cmd script_check_id `psigma.snd run_cmd script_check_id `psum run_cmd script_check_id `psum.cases_on run_cmd script_check_id `psum.inl run_cmd script_check_id `psum.inr run_cmd script_check_id `punit run_cmd script_check_id `punit.cases_on run_cmd script_check_id `punit.star run_cmd script_check_id `quot.lift run_cmd script_check_id `quot.mk run_cmd script_check_id `reflected run_cmd script_check_id `reflected.subst run_cmd script_check_id `repr run_cmd script_check_id `rfl run_cmd script_check_id `scope_trace run_cmd script_check_id `set_of run_cmd script_check_id `sizeof run_cmd script_check_id `string run_cmd script_check_id `string.empty run_cmd script_check_id `string.empty_ne_str run_cmd script_check_id `string.str run_cmd script_check_id `string.str_ne_empty run_cmd script_check_id `string.str_ne_str_left run_cmd script_check_id `string.str_ne_str_right run_cmd script_check_id `subsingleton run_cmd script_check_id `subsingleton.elim run_cmd script_check_id `subsingleton.helim run_cmd script_check_id `subtype run_cmd script_check_id `subtype.mk run_cmd script_check_id `subtype.rec run_cmd script_check_id `subtype.val run_cmd script_check_id `tactic run_cmd script_check_id `tactic.mk_inj_eq run_cmd script_check_id `tactic.triv run_cmd script_check_id `tactic.try run_cmd script_check_id `thunk run_cmd script_check_id `to_fmt run_cmd script_check_id `to_pexpr run_cmd script_check_id `trans_rel_left run_cmd script_check_id `trans_rel_right run_cmd script_check_id `true run_cmd script_check_id `true.intro run_cmd script_check_id `true_eq_false_of_false run_cmd script_check_id `unification_hint run_cmd script_check_id `unification_hint.mk run_cmd script_check_id `unit run_cmd script_check_id `unit.star run_cmd script_check_id `user_attribute run_cmd script_check_id `user_attribute.parse_reflect run_cmd script_check_id `vm_monitor run_cmd script_check_id `well_founded.fix run_cmd script_check_id `well_founded.fix_eq run_cmd script_check_id `well_founded_tactics run_cmd script_check_id `well_founded_tactics.dec_tac run_cmd script_check_id `well_founded_tactics.default run_cmd script_check_id `well_founded_tactics.rel_tac
f3c6ec41094b1a10d47493d3b67081e03dc5aabe
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/ring_theory/adjoin/tower.lean
ee0cf59cea822d78dc061ab253eda9112090bea8
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
6,799
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import ring_theory.adjoin.fg /-! # Adjoining elements and being finitely generated in an algebra tower ## Main results * `algebra.fg_trans'`: if `S` is finitely generated as `R`-algebra and `A` as `S`-algebra, then `A` is finitely generated as `R`-algebra * `fg_of_fg_of_fg`: **Artin--Tate lemma**: if C/B/A is a tower of rings, and A is noetherian, and C is algebra-finite over A, and C is module-finite over B, then B is algebra-finite over A. -/ open_locale pointwise universes u v w u₁ variables (R : Type u) (S : Type v) (A : Type w) (B : Type u₁) namespace algebra theorem adjoin_algebra_map (R : Type u) (S : Type v) (A : Type w) [comm_semiring R] [comm_semiring S] [semiring A] [algebra R S] [algebra S A] [algebra R A] [is_scalar_tower R S A] (s : set S) : adjoin R (algebra_map S A '' s) = (adjoin R s).map (is_scalar_tower.to_alg_hom R S A) := le_antisymm (adjoin_le $ set.image_subset_iff.2 $ λ y hy, ⟨y, subset_adjoin hy, rfl⟩) (subalgebra.map_le.2 $ adjoin_le $ λ y hy, subset_adjoin ⟨y, hy, rfl⟩) lemma adjoin_restrict_scalars (C D E : Type*) [comm_semiring C] [comm_semiring D] [comm_semiring E] [algebra C D] [algebra C E] [algebra D E] [is_scalar_tower C D E] (S : set E) : (algebra.adjoin D S).restrict_scalars C = (algebra.adjoin ((⊤ : subalgebra C D).map (is_scalar_tower.to_alg_hom C D E)) S).restrict_scalars C := begin suffices : set.range (algebra_map D E) = set.range (algebra_map ((⊤ : subalgebra C D).map (is_scalar_tower.to_alg_hom C D E)) E), { ext x, change x ∈ subsemiring.closure (_ ∪ S) ↔ x ∈ subsemiring.closure (_ ∪ S), rw this }, ext x, split, { rintros ⟨y, hy⟩, exact ⟨⟨algebra_map D E y, ⟨y, ⟨algebra.mem_top, rfl⟩⟩⟩, hy⟩ }, { rintros ⟨⟨y, ⟨z, ⟨h0, h1⟩⟩⟩, h2⟩, exact ⟨z, eq.trans h1 h2⟩ }, end lemma adjoin_res_eq_adjoin_res (C D E F : Type*) [comm_semiring C] [comm_semiring D] [comm_semiring E] [comm_semiring F] [algebra C D] [algebra C E] [algebra C F] [algebra D F] [algebra E F] [is_scalar_tower C D F] [is_scalar_tower C E F] {S : set D} {T : set E} (hS : algebra.adjoin C S = ⊤) (hT : algebra.adjoin C T = ⊤) : (algebra.adjoin E (algebra_map D F '' S)).restrict_scalars C = (algebra.adjoin D (algebra_map E F '' T)).restrict_scalars C := by rw [adjoin_restrict_scalars C E, adjoin_restrict_scalars C D, ←hS, ←hT, ←algebra.adjoin_image, ←algebra.adjoin_image, ←alg_hom.coe_to_ring_hom, ←alg_hom.coe_to_ring_hom, is_scalar_tower.coe_to_alg_hom, is_scalar_tower.coe_to_alg_hom, ←adjoin_union_eq_adjoin_adjoin, ←adjoin_union_eq_adjoin_adjoin, set.union_comm] end algebra section open_locale classical lemma algebra.fg_trans' {R S A : Type*} [comm_semiring R] [comm_semiring S] [comm_semiring A] [algebra R S] [algebra S A] [algebra R A] [is_scalar_tower R S A] (hRS : (⊤ : subalgebra R S).fg) (hSA : (⊤ : subalgebra S A).fg) : (⊤ : subalgebra R A).fg := let ⟨s, hs⟩ := hRS, ⟨t, ht⟩ := hSA in ⟨s.image (algebra_map S A) ∪ t, by rw [finset.coe_union, finset.coe_image, algebra.adjoin_union_eq_adjoin_adjoin, algebra.adjoin_algebra_map, hs, algebra.map_top, is_scalar_tower.adjoin_range_to_alg_hom, ht, subalgebra.restrict_scalars_top]⟩ end section artin_tate variables (C : Type*) section semiring variables [comm_semiring A] [comm_semiring B] [semiring C] variables [algebra A B] [algebra B C] [algebra A C] [is_scalar_tower A B C] open finset submodule open_locale classical lemma exists_subalgebra_of_fg (hAC : (⊤ : subalgebra A C).fg) (hBC : (⊤ : submodule B C).fg) : ∃ B₀ : subalgebra A B, B₀.fg ∧ (⊤ : submodule B₀ C).fg := begin cases hAC with x hx, cases hBC with y hy, have := hy, simp_rw [eq_top_iff', mem_span_finset] at this, choose f hf, let s : finset B := finset.image₂ f (x ∪ (y * y)) y, have hxy : ∀ xi ∈ x, xi ∈ span (algebra.adjoin A (↑s : set B)) (↑(insert 1 y : finset C) : set C) := λ xi hxi, hf xi ▸ sum_mem (λ yj hyj, smul_mem (span (algebra.adjoin A (↑s : set B)) (↑(insert 1 y : finset C) : set C)) ⟨f xi yj, algebra.subset_adjoin $ mem_image₂_of_mem (mem_union_left _ hxi) hyj⟩ (subset_span $ mem_insert_of_mem hyj)), have hyy : span (algebra.adjoin A (↑s : set B)) (↑(insert 1 y : finset C) : set C) * span (algebra.adjoin A (↑s : set B)) (↑(insert 1 y : finset C) : set C) ≤ span (algebra.adjoin A (↑s : set B)) (↑(insert 1 y : finset C) : set C), { rw [span_mul_span, span_le, coe_insert], rintros _ ⟨yi, yj, rfl | hyi, rfl | hyj, rfl⟩, { rw mul_one, exact subset_span (set.mem_insert _ _) }, { rw one_mul, exact subset_span (set.mem_insert_of_mem _ hyj) }, { rw mul_one, exact subset_span (set.mem_insert_of_mem _ hyi) }, { rw ← hf (yi * yj), exact set_like.mem_coe.2 (sum_mem $ λ yk hyk, smul_mem (span (algebra.adjoin A (↑s : set B)) (insert 1 ↑y : set C)) ⟨f (yi * yj) yk, algebra.subset_adjoin $ mem_image₂_of_mem (mem_union_right _ $ mul_mem_mul hyi hyj) hyk⟩ (subset_span $ set.mem_insert_of_mem _ hyk : yk ∈ _)) } }, refine ⟨algebra.adjoin A (↑s : set B), subalgebra.fg_adjoin_finset _, insert 1 y, _⟩, refine restrict_scalars_injective A _ _ _, rw [restrict_scalars_top, eq_top_iff, ← algebra.top_to_submodule, ← hx, algebra.adjoin_eq_span, span_le], refine λ r hr, submonoid.closure_induction hr (λ c hc, hxy c hc) (subset_span $ mem_insert_self _ _) (λ p q hp hq, hyy $ submodule.mul_mem_mul hp hq) end end semiring section ring variables [comm_ring A] [comm_ring B] [comm_ring C] variables [algebra A B] [algebra B C] [algebra A C] [is_scalar_tower A B C] /-- **Artin--Tate lemma**: if A ⊆ B ⊆ C is a chain of subrings of commutative rings, and A is noetherian, and C is algebra-finite over A, and C is module-finite over B, then B is algebra-finite over A. References: Atiyah--Macdonald Proposition 7.8; Stacks 00IS; Altman--Kleiman 16.17. -/ theorem fg_of_fg_of_fg [is_noetherian_ring A] (hAC : (⊤ : subalgebra A C).fg) (hBC : (⊤ : submodule B C).fg) (hBCi : function.injective (algebra_map B C)) : (⊤ : subalgebra A B).fg := let ⟨B₀, hAB₀, hB₀C⟩ := exists_subalgebra_of_fg A B C hAC hBC in algebra.fg_trans' (B₀.fg_top.2 hAB₀) $ subalgebra.fg_of_submodule_fg $ have is_noetherian_ring B₀, from is_noetherian_ring_of_fg hAB₀, have is_noetherian B₀ C, by exactI is_noetherian_of_fg_of_noetherian' hB₀C, by exactI fg_of_injective (is_scalar_tower.to_alg_hom B₀ B C).to_linear_map hBCi end ring end artin_tate
83c0844fe92fe90c8160d35ec9589215706b1632
1abd1ed12aa68b375cdef28959f39531c6e95b84
/src/analysis/quaternion.lean
3ae29c43b58ef2c1d85819bf14dea109999ee7eb
[ "Apache-2.0" ]
permissive
jumpy4/mathlib
d3829e75173012833e9f15ac16e481e17596de0f
af36f1a35f279f0e5b3c2a77647c6bf2cfd51a13
refs/heads/master
1,693,508,842,818
1,636,203,271,000
1,636,203,271,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,360
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.quaternion import analysis.inner_product_space.basic /-! # Quaternions as a normed algebra In this file we define the following structures on the space `ℍ := ℍ[ℝ]` of quaternions: * inner product space; * normed ring; * normed space over `ℝ`. ## Notation The following notation is available with `open_locale quaternion`: * `ℍ` : quaternions ## Tags quaternion, normed ring, normed space, normed algebra -/ localized "notation `ℍ` := quaternion ℝ" in quaternion open_locale real_inner_product_space noncomputable theory namespace quaternion instance : has_inner ℝ ℍ := ⟨λ a b, (a * b.conj).re⟩ lemma inner_self (a : ℍ) : ⟪a, a⟫ = norm_sq a := rfl lemma inner_def (a b : ℍ) : ⟪a, b⟫ = (a * b.conj).re := rfl instance : inner_product_space ℝ ℍ := inner_product_space.of_core { inner := has_inner.inner, conj_sym := λ x y, by simp [inner_def, mul_comm], nonneg_re := λ x, norm_sq_nonneg, definite := λ x, norm_sq_eq_zero.1, add_left := λ x y z, by simp only [inner_def, add_mul, add_re], smul_left := λ x y r, by simp [inner_def] } lemma norm_sq_eq_norm_sq (a : ℍ) : norm_sq a = ∥a∥ * ∥a∥ := by rw [← inner_self, real_inner_self_eq_norm_sq] instance : norm_one_class ℍ := ⟨by rw [norm_eq_sqrt_real_inner, inner_self, norm_sq.map_one, real.sqrt_one]⟩ @[simp] lemma norm_mul (a b : ℍ) : ∥a * b∥ = ∥a∥ * ∥b∥ := begin simp only [norm_eq_sqrt_real_inner, inner_self, norm_sq.map_mul], exact real.sqrt_mul norm_sq_nonneg _ end @[simp, norm_cast] lemma norm_coe (a : ℝ) : ∥(a : ℍ)∥ = ∥a∥ := by rw [norm_eq_sqrt_real_inner, inner_self, norm_sq_coe, real.sqrt_sq_eq_abs, real.norm_eq_abs] noncomputable instance : normed_ring ℍ := { dist_eq := λ _ _, rfl, norm_mul := λ a b, (norm_mul a b).le } noncomputable instance : normed_algebra ℝ ℍ := { norm_algebra_map_eq := norm_coe, to_algebra := quaternion.algebra } instance : has_coe ℂ ℍ := ⟨λ z, ⟨z.re, z.im, 0, 0⟩⟩ @[simp, norm_cast] lemma coe_complex_re (z : ℂ) : (z : ℍ).re = z.re := rfl @[simp, norm_cast] lemma coe_complex_im_i (z : ℂ) : (z : ℍ).im_i = z.im := rfl @[simp, norm_cast] lemma coe_complex_im_j (z : ℂ) : (z : ℍ).im_j = 0 := rfl @[simp, norm_cast] lemma coe_complex_im_k (z : ℂ) : (z : ℍ).im_k = 0 := rfl @[simp, norm_cast] lemma coe_complex_add (z w : ℂ) : ↑(z + w) = (z + w : ℍ) := by ext; simp @[simp, norm_cast] lemma coe_complex_mul (z w : ℂ) : ↑(z * w) = (z * w : ℍ) := by ext; simp @[simp, norm_cast] lemma coe_complex_zero : ((0 : ℂ) : ℍ) = 0 := rfl @[simp, norm_cast] lemma coe_complex_one : ((1 : ℂ) : ℍ) = 1 := rfl @[simp, norm_cast] lemma coe_real_complex_mul (r : ℝ) (z : ℂ) : (r • z : ℍ) = ↑r * ↑z := by ext; simp @[simp, norm_cast] lemma coe_complex_coe (r : ℝ) : ((r : ℂ) : ℍ) = r := rfl /-- Coercion `ℂ →ₐ[ℝ] ℍ` as an algebra homomorphism. -/ def of_complex : ℂ →ₐ[ℝ] ℍ := { to_fun := coe, map_one' := rfl, map_zero' := rfl, map_add' := coe_complex_add, map_mul' := coe_complex_mul, commutes' := λ x, rfl } @[simp] lemma coe_of_complex : ⇑of_complex = coe := rfl end quaternion
7aed4948b449c4f4cf9c1999a282b728d30bb6fd
432d948a4d3d242fdfb44b81c9e1b1baacd58617
/src/data/polynomial/coeff.lean
0724b809b2c7e5405926b87f2ffe04922125a5dd
[ "Apache-2.0" ]
permissive
JLimperg/aesop3
306cc6570c556568897ed2e508c8869667252e8a
a4a116f650cc7403428e72bd2e2c4cda300fe03f
refs/heads/master
1,682,884,916,368
1,620,320,033,000
1,620,320,033,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
7,552
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import data.polynomial.basic import data.finset.nat_antidiagonal /-! # Theory of univariate polynomials The theorems include formulas for computing coefficients, such as `coeff_add`, `coeff_sum`, `coeff_mul` -/ noncomputable theory open finsupp finset add_monoid_algebra open_locale big_operators namespace polynomial universes u v variables {R : Type u} {S : Type v} {a b : R} {n m : ℕ} variables [semiring R] {p q r : polynomial R} section coeff lemma coeff_one (n : ℕ) : coeff (1 : polynomial R) n = if 0 = n then 1 else 0 := coeff_monomial @[simp] lemma coeff_add (p q : polynomial R) (n : ℕ) : coeff (p + q) n = coeff p n + coeff q n := rfl lemma coeff_sum [semiring S] (n : ℕ) (f : ℕ → R → polynomial S) : coeff (p.sum f) n = p.sum (λ a b, coeff (f a b) n) := finsupp.sum_apply lemma sum_def [add_comm_monoid S] (f : ℕ → R → S) : p.sum f = ∑ n in p.support, f n (p.coeff n) := rfl @[simp] lemma coeff_smul (p : polynomial R) (r : R) (n : ℕ) : coeff (r • p) n = r * coeff p n := finsupp.smul_apply _ _ _ @[simp] lemma mem_support_iff : n ∈ p.support ↔ p.coeff n ≠ 0 := by simp [support, coeff] lemma not_mem_support_iff : n ∉ p.support ↔ p.coeff n = 0 := by simp variable (R) /-- The nth coefficient, as a linear map. -/ def lcoeff (n : ℕ) : polynomial R →ₗ[R] R := finsupp.lapply n variable {R} @[simp] lemma lcoeff_apply (n : ℕ) (f : polynomial R) : lcoeff R n f = coeff f n := rfl @[simp] lemma finset_sum_coeff {ι : Type*} (s : finset ι) (f : ι → polynomial R) (n : ℕ) : coeff (∑ b in s, f b) n = ∑ b in s, coeff (f b) n := (s.sum_hom (λ q : polynomial R, lcoeff R n q)).symm /-- Decomposes the coefficient of the product `p * q` as a sum over `nat.antidiagonal`. A version which sums over `range (n + 1)` can be obtained by using `finset.nat.sum_antidiagonal_eq_sum_range_succ`. -/ lemma coeff_mul (p q : polynomial R) (n : ℕ) : coeff (p * q) n = ∑ x in nat.antidiagonal n, coeff p x.1 * coeff q x.2 := add_monoid_algebra.mul_apply_antidiagonal p q n _ (λ x, nat.mem_antidiagonal) @[simp] lemma mul_coeff_zero (p q : polynomial R) : coeff (p * q) 0 = coeff p 0 * coeff q 0 := by simp [coeff_mul] lemma coeff_mul_X_zero (p : polynomial R) : coeff (p * X) 0 = 0 := by simp lemma coeff_X_mul_zero (p : polynomial R) : coeff (X * p) 0 = 0 := by simp lemma coeff_C_mul_X (x : R) (k n : ℕ) : coeff (C x * X^k : polynomial R) n = if n = k then x else 0 := by rw [← single_eq_C_mul_X]; simp [monomial, single, eq_comm, coeff]; congr @[simp] lemma coeff_C_mul (p : polynomial R) : coeff (C a * p) n = a * coeff p n := add_monoid_algebra.single_zero_mul_apply p a n lemma C_mul' (a : R) (f : polynomial R) : C a * f = a • f := ext $ λ n, coeff_C_mul f @[simp] lemma coeff_mul_C (p : polynomial R) (n : ℕ) (a : R) : coeff (p * C a) n = coeff p n * a := add_monoid_algebra.mul_single_zero_apply p a n lemma coeff_X_pow (k n : ℕ) : coeff (X^k : polynomial R) n = if n = k then 1 else 0 := by { simp only [X_pow_eq_monomial, monomial, single, eq_comm], congr } @[simp] lemma coeff_X_pow_self (n : ℕ) : coeff (X^n : polynomial R) n = 1 := by simp [coeff_X_pow] theorem coeff_mul_X_pow (p : polynomial R) (n d : ℕ) : coeff (p * polynomial.X ^ n) (d + n) = coeff p d := begin rw [coeff_mul, sum_eq_single (d,n), coeff_X_pow, if_pos rfl, mul_one], { rintros ⟨i,j⟩ h1 h2, rw [coeff_X_pow, if_neg, mul_zero], rintro rfl, apply h2, rw [nat.mem_antidiagonal, add_right_cancel_iff] at h1, subst h1 }, { exact λ h1, (h1 (nat.mem_antidiagonal.2 rfl)).elim } end lemma coeff_mul_X_pow' (p : polynomial R) (n d : ℕ) : (p * X ^ n).coeff d = ite (n ≤ d) (p.coeff (d - n)) 0 := begin split_ifs, { rw [←@nat.sub_add_cancel d n h, coeff_mul_X_pow, nat.add_sub_cancel] }, { refine (coeff_mul _ _ _).trans (finset.sum_eq_zero (λ x hx, _)), rw [coeff_X_pow, if_neg, mul_zero], exact ne_of_lt (lt_of_le_of_lt (nat.le_of_add_le_right (le_of_eq (finset.nat.mem_antidiagonal.mp hx))) (not_le.mp h)) }, end @[simp] theorem coeff_mul_X (p : polynomial R) (n : ℕ) : coeff (p * X) (n + 1) = coeff p n := by simpa only [pow_one] using coeff_mul_X_pow p 1 n theorem mul_X_pow_eq_zero {p : polynomial R} {n : ℕ} (H : p * X ^ n = 0) : p = 0 := ext $ λ k, (coeff_mul_X_pow p n k).symm.trans $ ext_iff.1 H (k+n) lemma C_mul_X_pow_eq_monomial (c : R) (n : ℕ) : C c * X^n = monomial n c := by { ext1, rw [monomial_eq_smul_X, coeff_smul, coeff_C_mul] } lemma support_mul_X_pow (c : R) (n : ℕ) (H : c ≠ 0) : (C c * X^n).support = singleton n := by rw [C_mul_X_pow_eq_monomial, support_monomial n c H] lemma support_C_mul_X_pow' {c : R} {n : ℕ} : (C c * X^n).support ⊆ singleton n := by { rw [C_mul_X_pow_eq_monomial], exact support_monomial' n c } lemma C_dvd_iff_dvd_coeff (r : R) (φ : polynomial R) : C r ∣ φ ↔ ∀ i, r ∣ φ.coeff i := begin split, { rintros ⟨φ, rfl⟩ c, rw coeff_C_mul, apply dvd_mul_right }, { intro h, choose c hc using h, classical, let c' : ℕ → R := λ i, if i ∈ φ.support then c i else 0, let ψ : polynomial R := ∑ i in φ.support, monomial i (c' i), use ψ, ext i, simp only [ψ, c', coeff_C_mul, mem_support_iff, coeff_monomial, finset_sum_coeff, finset.sum_ite_eq'], split_ifs with hi hi, { rw hc }, { rw [not_not] at hi, rwa mul_zero } }, end end coeff open submodule polynomial set variables {f : polynomial R} {I : submodule (polynomial R) (polynomial R)} /-- If the coefficients of a polynomial belong to n ideal contains the submodule span of the coefficients of a polynomial. -/ lemma span_le_of_coeff_mem_C_inverse (cf : ∀ (i : ℕ), f.coeff i ∈ (C ⁻¹' I.carrier)) : (span (polynomial R) {g | ∃ i, g = C (f.coeff i)}) ≤ I := begin refine bInter_subset_of_mem _, rintros _ ⟨i, rfl⟩, exact set_like.mem_coe.mpr (cf i), end lemma mem_span_C_coeff : f ∈ span (polynomial R) {g : polynomial R | ∃ i : ℕ, g = (C (coeff f i))} := begin rw [← f.sum_single] {occs := occurrences.pos [1]}, refine sum_mem _ (λ i hi, _), change monomial i _ ∈ span _ _, rw [← C_mul_X_pow_eq_monomial, ← X_pow_mul, ← smul_eq_mul], exact smul_mem _ _ (subset_span ⟨i, rfl⟩), end lemma exists_coeff_not_mem_C_inverse : f ∉ I → ∃ i : ℕ , coeff f i ∉ (C ⁻¹' I.carrier) := imp_of_not_imp_not _ _ (λ cf, not_not.mpr ((span_le_of_coeff_mem_C_inverse (not_exists_not.mp cf)) mem_span_C_coeff)) section cast @[simp] lemma nat_cast_coeff_zero {n : ℕ} {R : Type*} [semiring R] : (n : polynomial R).coeff 0 = n := begin induction n with n ih, { simp, }, { simp [ih], }, end @[simp, norm_cast] theorem nat_cast_inj {m n : ℕ} {R : Type*} [semiring R] [char_zero R] : (↑m : polynomial R) = ↑n ↔ m = n := begin fsplit, { intro h, apply_fun (λ p, p.coeff 0) at h, simpa using h, }, { rintro rfl, refl, }, end @[simp] lemma int_cast_coeff_zero {i : ℤ} {R : Type*} [ring R] : (i : polynomial R).coeff 0 = i := by cases i; simp @[simp, norm_cast] theorem int_cast_inj {m n : ℤ} {R : Type*} [ring R] [char_zero R] : (↑m : polynomial R) = ↑n ↔ m = n := begin fsplit, { intro h, apply_fun (λ p, p.coeff 0) at h, simpa using h, }, { rintro rfl, refl, }, end end cast end polynomial
f672c9534add3951c226cb42c54d893951e505a7
9ad8d18fbe5f120c22b5e035bc240f711d2cbd7e
/src/combinatorics/gappy.lean
9585f069dc74a2531063ce7519d65674b9800586
[]
no_license
agusakov/lean_lib
c0e9cc29fc7d2518004e224376adeb5e69b5cc1a
f88d162da2f990b87c4d34f5f46bbca2bbc5948e
refs/heads/master
1,642,141,461,087
1,557,395,798,000
1,557,395,798,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
10,441
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 is about "gappy sets", ie subsets s in fin n = {0,...,n-1} such that s contains no adjacent pairs {i,i+1}. We define `(gappy n)` to be the set of such subsets. A key point is that there is a bijection `(gappy n) ⊕ (gappy n + 1) ≃ (gappy n + 2)`, which we define as `(gappy_equiv n)`. From this it follows inductively that the cardinality of `(gappy n)` is the (n + 2)nd Fibonacci number. -/ import data.fintype import combinatorics.fibonacci namespace combinatorics /- We find it convenient to introduce a new notation for the zero element in fin m. Notice that this only exists when m > 0, or equivalently, when m has the form n.succ = n + 1 for some n. -/ def fin.z {n : ℕ} : fin (n.succ) := 0 lemma fin.z_val {n : ℕ} : (@fin.z n).val = 0 := rfl lemma fin.succ_ne_z {n : ℕ} (a : fin n) : a.succ ≠ fin.z := begin intro e, replace e := fin.veq_of_eq e, rw[fin.succ_val,fin.z_val] at e, injection e, end lemma fin.succ_inj {n : ℕ} {a b : fin n} (e : a.succ = b.succ) : a = b := begin apply fin.eq_of_veq, replace e := congr_arg fin.val e, rw[fin.succ_val,fin.succ_val] at e, exact nat.succ_inj e, end /- Definition of gappiness -/ def is_gappy : ∀ {n : ℕ} (s : finset (fin n)), Prop | 0 _ := true | (nat.succ n) s := ∀ a : fin n, ¬ (a.cast_succ ∈ s ∧ a.succ ∈ s) instance is_gappy_decidable : forall {n : ℕ} (s : finset (fin n)), decidable (is_gappy s) | 0 _ := by {dsimp[is_gappy],apply_instance} | (nat.succ n) s := by {dsimp[is_gappy],apply_instance} def gappy' (n : ℕ) : finset (finset (fin n)) := finset.univ.filter is_gappy def gappy (n : ℕ) : Type := { s : finset (fin n) // is_gappy s } instance {n : ℕ} : fintype (gappy n) := by { dsimp[gappy], apply_instance } instance {n : ℕ} : decidable_eq (gappy n) := by { dsimp[gappy], apply_instance } /- How to generate a string describing a gappy set -/ instance {n : ℕ} : has_repr (gappy n) := ⟨λ (s : gappy n), repr s.val⟩ /- Given a set s ⊆ {0,..,n-1}, we can shift it to the right to get a set (shift s) = {i + 1 : i ∈ s} ⊆ {0,..,n} -/ def shift {n : ℕ} (s : finset (fin n)) : finset (fin n.succ) := s.image fin.succ /- Given a set s ⊆ {0,..,n}, we can shift it to the left to get a set (unshift s) = {i : i + 1 ∈ s} ⊆ {0,..,n-1} -/ def unshift {n : ℕ} (s : finset (fin n.succ)) : finset (fin n) := finset.univ.filter (λ a, a.succ ∈ s) lemma mem_shift {n : ℕ} (s : finset (fin n)) (a : fin n.succ) : a ∈ shift s ↔ ∃ b : fin n, b ∈ s ∧ b.succ = a := begin rw[shift],split, {intro a_in_shift, rcases finset.mem_image.mp a_in_shift with ⟨b,⟨b_in_s,e⟩⟩, use b, exact ⟨b_in_s,e⟩, },{ rintro ⟨b,⟨b_in_s,e⟩⟩, exact finset.mem_image.mpr ⟨b,⟨b_in_s,e⟩⟩, } end lemma zero_not_in_shift {n : ℕ} (s : finset (fin n)) : fin.z ∉ shift s := begin intro h0, rcases ((mem_shift s) 0).mp h0 with ⟨b,⟨b_in_s,e⟩⟩, let h1 := congr_arg fin.val e, rw[fin.succ_val] at h1, injection h1, end lemma succ_mem_shift_iff {n : ℕ} (s : finset (fin n)) (a : fin n) : a.succ ∈ shift s ↔ a ∈ s := begin rw[mem_shift s a.succ], split,{ rintro ⟨b,⟨b_in_s,u⟩⟩, rw[(fin.succ_inj u).symm], exact b_in_s, },{ intro a_in_s,use a,exact ⟨a_in_s,rfl⟩, } end lemma mem_unshift {n : ℕ} (s : finset (fin n.succ)) (a : fin n) : a ∈ unshift s ↔ a.succ ∈ s := begin rw[unshift,finset.mem_filter], split, {intro h,exact h.right}, {intro h,exact ⟨finset.mem_univ a,h⟩ } end lemma unshift_shift {n : ℕ} (s : finset (fin n)) : unshift (shift s) = s := begin ext,rw[mem_unshift (shift s) a],rw[succ_mem_shift_iff], end lemma unshift_insert {n : ℕ} (s : finset (fin n.succ)) : unshift (insert fin.z s) = unshift s := begin ext,rw[mem_unshift,mem_unshift,finset.mem_insert], split, {intro h,rcases h with h0 | h1, {exfalso,exact fin.succ_ne_z a h0}, {exact h1} }, {exact λ h,or.inr h} end lemma shift_unshift0 {n : ℕ} (s : finset (fin n.succ)) (h : fin.z ∉ s) : shift (unshift s) = s := begin ext, rcases a with ⟨_ | b_val,a_is_lt⟩, {have e : @fin.z n = ⟨0,a_is_lt⟩ := fin.eq_of_veq rfl, rw[← e],simp only[zero_not_in_shift,h], },{ let b : fin n := ⟨b_val,nat.lt_of_succ_lt_succ a_is_lt⟩, have e : b.succ = ⟨b_val.succ,a_is_lt⟩ := by { apply fin.eq_of_veq,rw[fin.succ_val], }, rw[← e,succ_mem_shift_iff (unshift s) b,mem_unshift s b], } end lemma shift_unshift1 {n : ℕ} (s : finset (fin n.succ)) (h : fin.z ∈ s) : insert fin.z (shift (unshift s)) = s := begin ext, rw[finset.mem_insert], rcases a with ⟨_ | b_val,a_is_lt⟩, {have e : @fin.z n = ⟨0,a_is_lt⟩ := fin.eq_of_veq rfl, rw[← e],simp only[h,eq_self_iff_true,true_or], },{ let b : fin n := ⟨b_val,nat.lt_of_succ_lt_succ a_is_lt⟩, have e : b.succ = ⟨b_val.succ,a_is_lt⟩ := by { apply fin.eq_of_veq,rw[fin.succ_val], }, rw[← e,succ_mem_shift_iff (unshift s) b,mem_unshift s b], split, {rintro (u0 | u1), {exfalso,exact fin.succ_ne_z b u0,}, {exact u1} }, {intro h,right,exact h,} } end /- Some lemmas about (un)shifting and gappiness -/ lemma shift_gappy : ∀ {n : ℕ} {s : finset (fin n)}, is_gappy s → is_gappy (shift s) | 0 _ _ := λ a, fin.elim0 a | (nat.succ n) s s_gappy := begin rintros a ⟨a_in_shift,a_succ_in_shift⟩, let a_in_s : a ∈ s := (succ_mem_shift_iff s a).mp a_succ_in_shift, rcases (mem_shift s a.cast_succ).mp a_in_shift with ⟨b,⟨b_in_s,eb⟩⟩, replace eb := congr_arg fin.val eb, rw[fin.succ_val,fin.cast_succ_val] at eb, let c_is_lt : b.val < n := nat.lt_of_succ_lt_succ (eb.symm ▸ a.is_lt), let c : fin n := ⟨b.val,c_is_lt⟩, have ebc : b = fin.cast_succ c := fin.eq_of_veq (by rw[fin.cast_succ_val]), have eac : a = fin.succ c := fin.eq_of_veq (nat.succ_inj (by rw[← eb,fin.succ_val])), rw[ebc] at b_in_s, rw[eac] at a_in_s, exact s_gappy c ⟨b_in_s,a_in_s⟩, end lemma unshift_gappy : ∀ {n : ℕ} {s : finset (fin n.succ)}, is_gappy s → is_gappy (unshift s) | 0 _ _ := trivial | (nat.succ n) s s_gappy := begin rintros a ⟨a_in_unshift,a_succ_in_unshift⟩, let a_succ_in_s := (mem_unshift s a.cast_succ).mp a_in_unshift, let a_succ_succ_in_s := (mem_unshift s a.succ).mp a_succ_in_unshift, have e : a.cast_succ.succ = a.succ.cast_succ := fin.eq_of_veq (by {rw[fin.succ_val,fin.cast_succ_val,fin.cast_succ_val,fin.succ_val]}), rw[e] at a_succ_in_s, exact s_gappy a.succ ⟨a_succ_in_s,a_succ_succ_in_s⟩, end lemma insert_gappy : ∀ {n : ℕ} {s : finset (fin n.succ.succ)}, is_gappy s → (∀ (a : fin n.succ.succ), a ∈ s → a.val ≥ 2) → is_gappy (insert fin.z s) := begin rintros n s s_gappy s_big a ⟨a_in_t,a_succ_in_t⟩, rcases finset.mem_insert.mp a_succ_in_t with a_succ_zero | a_succ_in_s, {exact fin.succ_ne_z a a_succ_zero}, let a_pos : 0 < a.val := nat.lt_of_succ_lt_succ ((fin.succ_val a) ▸ (s_big a.succ a_succ_in_s)), rcases finset.mem_insert.mp a_in_t with a_zero | a_in_s, {replace a_zero : a.val = 0 := (fin.cast_succ_val a).symm.trans (congr_arg fin.val a_zero), rw[a_zero] at a_pos, exact lt_irrefl 0 a_pos, },{ exact s_gappy a ⟨a_in_s,a_succ_in_s⟩, } end def i {n : ℕ} (s : gappy n) : gappy n.succ := ⟨shift s.val,shift_gappy s.property⟩ lemma i_val {n : ℕ} (s : gappy n) : (i s).val = shift s.val := rfl lemma zero_not_in_i {n : ℕ} (s : gappy n) : fin.z ∉ (i s).val := zero_not_in_shift s.val lemma shift_big {n : ℕ} (s : finset (fin n)) : ∀ (a : fin n.succ.succ), a ∈ shift (shift s) → a.val ≥ 2 := begin intros a ma, rcases (mem_shift (shift s) a).mp ma with ⟨b,⟨mb,eb⟩⟩, rcases (mem_shift s b).mp mb with ⟨c,⟨mc,ec⟩⟩, rw[← eb,← ec,fin.succ_val,fin.succ_val], apply nat.succ_le_succ, apply nat.succ_le_succ, exact nat.zero_le c.val, end def j {n : ℕ} (s : gappy n) : gappy n.succ.succ := ⟨insert fin.z (shift (shift s.val)), begin let h := insert_gappy (shift_gappy (shift_gappy s.property)) (shift_big s.val), exact h, end⟩ lemma j_val {n : ℕ} (s : gappy n) : (j s).val = insert fin.z (shift (shift s.val)) := rfl lemma zero_in_j {n : ℕ} (s : gappy n) : fin.z ∈ (j s).val := finset.mem_insert_self _ _ def p {n : ℕ} : (gappy n) ⊕ (gappy n.succ) → gappy n.succ.succ | (sum.inl s) := j s | (sum.inr s) := i s def q {n : ℕ} (s : gappy n.succ.succ) : (gappy n) ⊕ (gappy n.succ) := if fin.z ∈ s.val then sum.inl ⟨unshift (unshift s.val),unshift_gappy (unshift_gappy s.property)⟩ else sum.inr ⟨unshift s.val,unshift_gappy s.property⟩ lemma qp {n : ℕ} (s : (gappy n) ⊕ (gappy n.succ)) : q (p s) = s := begin rcases s with s | s; dsimp[p,q], {rw[if_pos (zero_in_j s)],congr,apply subtype.eq, change unshift (unshift (j s).val) = s.val, rw[j_val,unshift_insert,unshift_shift,unshift_shift], }, {rw[if_neg (zero_not_in_i s)],congr,apply subtype.eq, change unshift (i s).val = s.val, rw[i_val,unshift_shift], } end lemma pq {n : ℕ} (s : gappy n.succ.succ) : p (q s) = s := begin dsimp[q],split_ifs; dsimp[p]; apply subtype.eq, {rw[j_val], change insert fin.z (shift (shift (unshift (unshift s.val )))) = s.val, have z_not_in_us : fin.z ∉ unshift s.val := begin intro z_in_us, let z_succ_in_s := (mem_unshift s.val fin.z).mp z_in_us, exact s.property fin.z ⟨h,z_succ_in_s⟩, end, rw[shift_unshift0 (unshift s.val) z_not_in_us], rw[shift_unshift1 s.val h], },{ rw[i_val], change shift (unshift s.val) = s.val, exact shift_unshift0 s.val h, } end def gappy_equiv {n : ℕ} : ((gappy n) ⊕ (gappy n.succ)) ≃ (gappy n.succ.succ) := { to_fun := p, inv_fun := q, left_inv := qp, right_inv := pq } lemma gappy_card_step (n : ℕ) : fintype.card (gappy n.succ.succ) = fintype.card (gappy n) + fintype.card (gappy n.succ) := begin let e0 := fintype.card_congr (@gappy_equiv n), let e1 := fintype.card_sum (gappy n) (gappy n.succ), exact e0.symm.trans e1, end lemma gappy_card : ∀ (n : ℕ), fintype.card (gappy n) = fibonacci n.succ.succ | 0 := rfl | 1 := rfl | (nat.succ (nat.succ n)) := begin rw[gappy_card_step n,gappy_card n,gappy_card n.succ], dsimp[fibonacci],refl, end end combinatorics
bfe0aa27118f63c43be12c56e36754f40e8a504f
b1e80085f6d9158c0b431ffc4fa9d0d53cbac8e4
/src/covering.lean
de6d338e3c8b0694fce698a0578d7659ea0b82a7
[]
no_license
dwarn/nielsen-schreier-2
d73c20e4d2a8ae537fe4f8063272d0b72c58276a
e51a8c6511d374dc584698c7fa236a5be47e7dbe
refs/heads/master
1,679,911,740,113
1,615,656,058,000
1,615,656,058,000
344,111,212
1
0
null
null
null
null
UTF-8
Lean
false
false
4,794
lean
import category_theory.action group_theory.semidirect_product free open category_theory is_free_group semidirect_product universe u instance action_category_is_free_groupoid {G A : Type u} [group G] [mul_action G A] [is_free_group G] : is_free_groupoid (action_category G A) := let ac := (action_category.obj_equiv G A).symm in let gens := λ x y, { a : gp_gens G // (gp_emb a) • ac x = ac y } in let emb := λ x y (e : gens x y), (show x ⟶ y, from ⟨gp_emb e.val, e.property⟩) in { gpd_gens := gens, gpd_emb := emb, gpd_lift := begin introsI X _ f, set Y := (A → X) ⋊[my_mul_aut G A X] G, set f' : gp_gens G → Y := λ e, ⟨λ b, @f ⟨(), _⟩ ⟨(), b⟩ ⟨e, smul_inv_smul _ _⟩, gp_emb e⟩, rcases gp_lift f' with ⟨p, hp, up⟩, have sane : ∀ g : G, (p g).right = g, { set p' := right_hom.comp p, apply is_free_group_ext p', intro e, change (p _).right = _, rw ←hp }, let F : action_category G A ⥤ single_obj X, { refine { obj := λ _, (), map := λ a b q, (p q.val).left (ac b), map_id' := _, map_comp' := _ }, { intro a, change (p 1).left _ = 1, rw p.map_one, refl }, { intros a b c f g, change (p (_ * _)).left _ = _ * _, rw p.map_mul, change _ * _ = _ * _, congr, change (p f.val).left _ = _, congr, rw [inv_smul_eq_iff, sane], exact g.property.symm } }, refine ⟨F, _, _⟩, { intros a b e, change f e = (p _).left _, rw ←hp, change f _ = f _, rcases a with ⟨⟨⟩, a⟩, rcases b with ⟨⟨⟩, b⟩, rcases e with ⟨e, h⟩, rw ←eq_inv_smul_iff at h, cases h, refl }, { intros F' hF', have F'_congr : ∀ (a b c d : action_category G A) (f g : G) (h1 : (f • a.snd : A) = b.snd) (h2 : (g • c.snd : A) = d.snd), a = c → b = d → f = g → (F'.map ⟨f, h1⟩ : X) = (F'.map ⟨g, h2⟩ : X), { rintros _ _ _ _ _ _ _ _ ⟨⟩ ⟨⟩ ⟨⟩, refl }, have ssmul : ∀ (g : G) (b : A), (g • (ac.symm $ g⁻¹ • b).snd : A) = (ac.symm b).snd, { apply smul_inv_smul }, set p' : G →* Y := { to_fun := λ g, ⟨λ b, F'.map ⟨g, ssmul g b⟩, g⟩, map_one' := begin congr, funext, change F'.map _ = 1, have : (1 : X) = F'.map (𝟙 (ac.symm b)), { rw F'.map_id, refl }, rw this, apply F'_congr, { simp only [one_inv, one_smul] }, all_goals { refl }, end, map_mul' := begin intros g h, congr, funext, have : F'.map ⟨_, ssmul (g * h) b⟩ == F'.map ⟨h, ssmul h (g⁻¹ • b)⟩ ≫ F'.map ⟨g, ssmul g b⟩, { apply heq_of_eq, rw ←F'.map_comp, apply F'_congr, { rw [mul_inv_rev, mul_smul] }, all_goals { refl } }, rw heq_iff_eq at this, exact this end }, have : p' = p, { apply up, intro e, apply semidirect_product.ext, { funext, apply hF' }, { refl } }, apply functor.hext, { intro, apply unit.ext }, rintros ⟨⟨⟩, a : A⟩ ⟨⟨⟩, b : A⟩ ⟨g, h : g • a = b⟩, change _ == (p _).left _, rw ←this, change F'.map _ == F'.map _, apply heq_of_eq, apply F'_congr, { congr, rw eq_inv_smul_iff, exact h }, all_goals { refl } } end, ind := begin intros, let Q : G → Prop := λ g, ∀ (x y : action_category G A) (z : x ⟶ y), g = subtype.val z → P z, have claim : ∀ g, Q g, { apply is_free_group_induction Q, { rintros e x y ⟨_, h⟩ ⟨⟩, change P (emb x y ⟨e, h⟩), apply base }, { rintros ⟨⟨⟩, x: A⟩ ⟨⟨⟩, y⟩ ⟨_, h⟩ ⟨⟩, have : x = y, { change (1 : G) • x = y at h, simpa using h }, cases this, apply id }, { rintros g h gq hq x y ⟨_, prop⟩ ⟨⟩, let gh : x ⟶ y := ⟨g * h, prop⟩, change P gh, let mi : action_category G A := ac.symm (h • x.snd), let hh : x ⟶ mi := ⟨h, rfl⟩, let gg : mi ⟶ y := ⟨g, _⟩, have : gh = (hh ≫ gg), { ext, refl }, rw this, apply comp, { apply hq, refl }, { apply gq, refl }, change _ • _ • _ = _, rw smul_smul, exact prop, }, { rintros g gq x y ⟨_, prop⟩ ⟨⟩, let ii : y ⟶ x := ⟨g, _⟩, swap, { change _ • _ = _, symmetry, rw ←inv_smul_eq_iff, assumption }, change P (inv ii), apply comp_1, apply gq, refl } }, apply claim f.val, refl end }
62911aeef42584d15075c05cd84ac157ad75dee7
63abd62053d479eae5abf4951554e1064a4c45b4
/src/analysis/convex/cone.lean
b6a23b5a758d26b7d77fed568d974c1b35d259fd
[ "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
22,938
lean
/- Copyright (c) 2020 Yury Kudryashov All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Frédéric Dupuis -/ import linear_algebra.linear_pmap import analysis.convex.basic import order.zorn /-! # Convex cones In a vector space `E` over `ℝ`, we define a convex cone as a subset `s` such that `a • x + b • y ∈ s` whenever `x, y ∈ s` and `a, b > 0`. We prove that convex cones form a `complete_lattice`, and define their images (`convex_cone.map`) and preimages (`convex_cone.comap`) under linear maps. We define pointed, blunt, flat and salient cones, and prove the correspondence between convex cones and ordered semimodules. We also define `convex.to_cone` to be the minimal cone that includes a given convex set. ## Main statements We prove two extension theorems: * `riesz_extension`: [M. Riesz extension theorem](https://en.wikipedia.org/wiki/M._Riesz_extension_theorem) says that if `s` is a convex cone in a real vector space `E`, `p` is a submodule of `E` such that `p + s = E`, and `f` is a linear function `p → ℝ` which is nonnegative on `p ∩ s`, then there exists a globally defined linear function `g : E → ℝ` that agrees with `f` on `p`, and is nonnegative on `s`. * `exists_extension_of_le_sublinear`: Hahn-Banach theorem: if `N : E → ℝ` is a sublinear map, `f` is a linear map defined on a subspace of `E`, and `f x ≤ N x` for all `x` in the domain of `f`, then `f` can be extended to the whole space to a linear map `g` such that `g x ≤ N x` for all `x` ## Implementation notes While `convex` is a predicate on sets, `convex_cone` is a bundled convex cone. ## References * https://en.wikipedia.org/wiki/Convex_cone ## TODO * Define the dual cone. -/ universes u v open set linear_map open_locale classical variables (E : Type*) [add_comm_group E] [vector_space ℝ E] {F : Type*} [add_comm_group F] [vector_space ℝ F] {G : Type*} [add_comm_group G] [vector_space ℝ G] /-! ### Definition of `convex_cone` and basic properties -/ /-- A convex cone is a subset `s` of a vector space over `ℝ` such that `a • x + b • y ∈ s` whenever `a, b > 0` and `x, y ∈ s`. -/ structure convex_cone := (carrier : set E) (smul_mem' : ∀ ⦃c : ℝ⦄, 0 < c → ∀ ⦃x : E⦄, x ∈ carrier → c • x ∈ carrier) (add_mem' : ∀ ⦃x⦄ (hx : x ∈ carrier) ⦃y⦄ (hy : y ∈ carrier), x + y ∈ carrier) variable {E} namespace convex_cone variables (S T : convex_cone E) instance : has_coe (convex_cone E) (set E) := ⟨convex_cone.carrier⟩ instance : has_mem E (convex_cone E) := ⟨λ m S, m ∈ S.carrier⟩ instance : has_le (convex_cone E) := ⟨λ S T, S.carrier ⊆ T.carrier⟩ instance : has_lt (convex_cone E) := ⟨λ S T, S.carrier ⊂ T.carrier⟩ @[simp, norm_cast] lemma mem_coe {x : E} : x ∈ (S : set E) ↔ x ∈ S := iff.rfl @[simp] lemma mem_mk {s : set E} {h₁ h₂ x} : x ∈ mk s h₁ h₂ ↔ x ∈ s := iff.rfl /-- Two `convex_cone`s are equal if the underlying subsets are equal. -/ theorem ext' {S T : convex_cone E} (h : (S : set E) = T) : S = T := by cases S; cases T; congr' /-- Two `convex_cone`s are equal if and only if the underlying subsets are equal. -/ protected theorem ext'_iff {S T : convex_cone E} : (S : set E) = T ↔ S = T := ⟨ext', λ h, h ▸ rfl⟩ /-- Two `convex_cone`s are equal if they have the same elements. -/ @[ext] theorem ext {S T : convex_cone E} (h : ∀ x, x ∈ S ↔ x ∈ T) : S = T := ext' $ set.ext h lemma smul_mem {c : ℝ} {x : E} (hc : 0 < c) (hx : x ∈ S) : c • x ∈ S := S.smul_mem' hc hx lemma add_mem ⦃x⦄ (hx : x ∈ S) ⦃y⦄ (hy : y ∈ S) : x + y ∈ S := S.add_mem' hx hy lemma smul_mem_iff {c : ℝ} (hc : 0 < c) {x : E} : c • x ∈ S ↔ x ∈ S := ⟨λ h, by simpa only [smul_smul, inv_mul_cancel (ne_of_gt hc), one_smul] using S.smul_mem (inv_pos.2 hc) h, λ h, S.smul_mem hc h⟩ lemma convex : convex (S : set E) := convex_iff_forall_pos.2 $ λ x y hx hy a b ha hb hab, S.add_mem (S.smul_mem ha hx) (S.smul_mem hb hy) instance : has_inf (convex_cone E) := ⟨λ S T, ⟨S ∩ T, λ c hc x hx, ⟨S.smul_mem hc hx.1, T.smul_mem hc hx.2⟩, λ x hx y hy, ⟨S.add_mem hx.1 hy.1, T.add_mem hx.2 hy.2⟩⟩⟩ lemma coe_inf : ((S ⊓ T : convex_cone E) : set E) = ↑S ∩ ↑T := rfl lemma mem_inf {x} : x ∈ S ⊓ T ↔ x ∈ S ∧ x ∈ T := iff.rfl instance : has_Inf (convex_cone E) := ⟨λ S, ⟨⋂ s ∈ S, ↑s, λ c hc x hx, mem_bInter $ λ s hs, s.smul_mem hc $ by apply mem_bInter_iff.1 hx s hs, λ x hx y hy, mem_bInter $ λ s hs, s.add_mem (by apply mem_bInter_iff.1 hx s hs) (by apply mem_bInter_iff.1 hy s hs)⟩⟩ lemma mem_Inf {x : E} {S : set (convex_cone E)} : x ∈ Inf S ↔ ∀ s ∈ S, x ∈ s := mem_bInter_iff instance : has_bot (convex_cone E) := ⟨⟨∅, λ c hc x, false.elim, λ x, false.elim⟩⟩ lemma mem_bot (x : E) : x ∈ (⊥ : convex_cone E) = false := rfl instance : has_top (convex_cone E) := ⟨⟨univ, λ c hc x hx, mem_univ _, λ x hx y hy, mem_univ _⟩⟩ lemma mem_top (x : E) : x ∈ (⊤ : convex_cone E) := mem_univ x instance : complete_lattice (convex_cone E) := { le := (≤), lt := (<), bot := (⊥), bot_le := λ S x, false.elim, top := (⊤), le_top := λ S x hx, mem_top x, inf := (⊓), Inf := has_Inf.Inf, sup := λ a b, Inf {x | a ≤ x ∧ b ≤ x}, Sup := λ s, Inf {T | ∀ S ∈ s, S ≤ T}, le_sup_left := λ a b, λ x hx, mem_Inf.2 $ λ s hs, hs.1 hx, le_sup_right := λ a b, λ x hx, mem_Inf.2 $ λ s hs, hs.2 hx, sup_le := λ a b c ha hb x hx, mem_Inf.1 hx c ⟨ha, hb⟩, le_inf := λ a b c ha hb x hx, ⟨ha hx, hb hx⟩, inf_le_left := λ a b x, and.left, inf_le_right := λ a b x, and.right, le_Sup := λ s p hs x hx, mem_Inf.2 $ λ t ht, ht p hs hx, Sup_le := λ s p hs x hx, mem_Inf.1 hx p hs, le_Inf := λ s a ha x hx, mem_Inf.2 $ λ t ht, ha t ht hx, Inf_le := λ s a ha x hx, mem_Inf.1 hx _ ha, .. partial_order.lift (coe : convex_cone E → set E) (λ a b, ext') } instance : inhabited (convex_cone E) := ⟨⊥⟩ /-- The image of a convex cone under an `ℝ`-linear map is a convex cone. -/ def map (f : E →ₗ[ℝ] F) (S : convex_cone E) : convex_cone F := { carrier := f '' S, smul_mem' := λ c hc y ⟨x, hx, hy⟩, hy ▸ f.map_smul c x ▸ mem_image_of_mem f (S.smul_mem hc hx), add_mem' := λ y₁ ⟨x₁, hx₁, hy₁⟩ y₂ ⟨x₂, hx₂, hy₂⟩, hy₁ ▸ hy₂ ▸ f.map_add x₁ x₂ ▸ mem_image_of_mem f (S.add_mem hx₁ hx₂) } lemma map_map (g : F →ₗ[ℝ] G) (f : E →ₗ[ℝ] F) (S : convex_cone E) : (S.map f).map g = S.map (g.comp f) := ext' $ image_image g f S @[simp] lemma map_id : S.map linear_map.id = S := ext' $ image_id _ /-- The preimage of a convex cone under an `ℝ`-linear map is a convex cone. -/ def comap (f : E →ₗ[ℝ] F) (S : convex_cone F) : convex_cone E := { carrier := f ⁻¹' S, smul_mem' := λ c hc x hx, by { rw [mem_preimage, f.map_smul c], exact S.smul_mem hc hx }, add_mem' := λ x hx y hy, by { rw [mem_preimage, f.map_add], exact S.add_mem hx hy } } @[simp] lemma comap_id : S.comap linear_map.id = S := ext' preimage_id lemma comap_comap (g : F →ₗ[ℝ] G) (f : E →ₗ[ℝ] F) (S : convex_cone G) : (S.comap g).comap f = S.comap (g.comp f) := ext' $ preimage_comp.symm @[simp] lemma mem_comap {f : E →ₗ[ℝ] F} {S : convex_cone F} {x : E} : x ∈ S.comap f ↔ f x ∈ S := iff.rfl /-- Constructs an ordered semimodule given an `ordered_add_comm_group`, a cone, and a proof that the order relation is the one defined by the cone. -/ lemma to_ordered_semimodule {M : Type*} [ordered_add_comm_group M] [semimodule ℝ M] (S : convex_cone M) (h : ∀ x y : M, x ≤ y ↔ y - x ∈ S) : ordered_semimodule ℝ M := ordered_semimodule.mk' begin intros x y z xy hz, rw [h (z • x) (z • y), ←smul_sub z y x], exact smul_mem S hz ((h x y).mp (le_of_lt xy)) end /-! ### Convex cones with extra properties -/ /-- A convex cone is pointed if it includes 0. -/ def pointed (S : convex_cone E) : Prop := (0 : E) ∈ S /-- A convex cone is blunt if it doesn't include 0. -/ def blunt (S : convex_cone E) : Prop := (0 : E) ∉ S /-- A convex cone is flat if it contains some nonzero vector `x` and its opposite `-x`. -/ def flat (S : convex_cone E) : Prop := ∃ x ∈ S, x ≠ (0 : E) ∧ -x ∈ S /-- A convex cone is salient if it doesn't include `x` and `-x` for any nonzero `x`. -/ def salient (S : convex_cone E) : Prop := ∀ x ∈ S, x ≠ (0 : E) → -x ∉ S lemma pointed_iff_not_blunt (S : convex_cone E) : pointed S ↔ ¬blunt S := ⟨λ h₁ h₂, h₂ h₁, λ h, not_not.mp h⟩ lemma salient_iff_not_flat (S : convex_cone E) : salient S ↔ ¬flat S := begin split, { rintros h₁ ⟨x, xs, H₁, H₂⟩, exact h₁ x xs H₁ H₂ }, { intro h, unfold flat at h, push_neg at h, exact h } end /-- A blunt cone (one not containing 0) is always salient. -/ lemma salient_of_blunt (S : convex_cone E) : blunt S → salient S := begin intro h₁, rw [salient_iff_not_flat], intro h₂, obtain ⟨x, xs, H₁, H₂⟩ := h₂, have hkey : (0 : E) ∈ S := by rw [(show 0 = x + (-x), by simp)]; exact add_mem S xs H₂, exact h₁ hkey, end /-- A pointed convex cone defines a preorder. -/ def to_preorder (S : convex_cone E) (h₁ : pointed S) : preorder E := { le := λ x y, y - x ∈ S, le_refl := λ x, by change x - x ∈ S; rw [sub_self x]; exact h₁, le_trans := λ x y z xy zy, by simp [(show z - x = z - y + (y - x), by abel), add_mem S zy xy] } /-- A pointed and salient cone defines a partial order. -/ def to_partial_order (S : convex_cone E) (h₁ : pointed S) (h₂ : salient S) : partial_order E := { le_antisymm := begin intros a b ab ba, by_contradiction h, have h' : b - a ≠ 0 := λ h'', h (eq_of_sub_eq_zero h'').symm, have H := h₂ (b-a) ab h', rw [neg_sub b a] at H, exact H ba, end, ..to_preorder S h₁ } /-- A pointed and salient cone defines an `ordered_add_comm_group`. -/ def to_ordered_add_comm_group (S : convex_cone E) (h₁ : pointed S) (h₂ : salient S) : ordered_add_comm_group E := { add_le_add_left := begin intros a b hab c, change c + b - (c + a) ∈ S, rw [add_sub_add_left_eq_sub], exact hab, end, ..to_partial_order S h₁ h₂, ..show add_comm_group E, by apply_instance } /-! ### Positive cone of an ordered semimodule -/ section positive_cone variables (M : Type*) [ordered_add_comm_group M] [semimodule ℝ M] [ordered_semimodule ℝ M] /-- The positive cone is the convex cone formed by the set of nonnegative elements in an ordered semimodule. -/ def positive_cone : convex_cone M := { carrier := {x | 0 ≤ x}, smul_mem' := begin intros c hc x hx, have := smul_le_smul_of_nonneg (show 0 ≤ x, by exact hx) (le_of_lt hc), have h' : c • (0 : M) = 0, { simp only [smul_zero] }, rwa [h'] at this end, add_mem' := λ x hx y hy, add_nonneg (show 0 ≤ x, by exact hx) (show 0 ≤ y, by exact hy) } /-- The positive cone of an ordered semimodule is always salient. -/ lemma salient_of_positive_cone : salient (positive_cone M) := begin intros x xs hx hx', have := calc 0 < x : lt_of_le_of_ne xs hx.symm ... ≤ x + (-x) : (le_add_iff_nonneg_right x).mpr hx' ... = 0 : by rw [tactic.ring.add_neg_eq_sub x x]; exact sub_self x, exact lt_irrefl 0 this, end /-- The positive cone of an ordered semimodule is always pointed. -/ lemma pointed_of_positive_cone : pointed (positive_cone M) := le_refl 0 end positive_cone end convex_cone /-! ### Cone over a convex set -/ namespace convex /-- The set of vectors proportional to those in a convex set forms a convex cone. -/ def to_cone (s : set E) (hs : convex s) : convex_cone E := begin apply convex_cone.mk (⋃ c > 0, (c : ℝ) • s); simp only [mem_Union, mem_smul_set], { rintros c c_pos _ ⟨c', c'_pos, x, hx, rfl⟩, exact ⟨c * c', mul_pos c_pos c'_pos, x, hx, (smul_smul _ _ _).symm⟩ }, { rintros _ ⟨cx, cx_pos, x, hx, rfl⟩ _ ⟨cy, cy_pos, y, hy, rfl⟩, have : 0 < cx + cy, from add_pos cx_pos cy_pos, refine ⟨_, this, _, convex_iff_div.1 hs hx hy (le_of_lt cx_pos) (le_of_lt cy_pos) this, _⟩, simp only [smul_add, smul_smul, mul_div_assoc', mul_div_cancel_left _ (ne_of_gt this)] } end variables {s : set E} (hs : convex s) {x : E} lemma mem_to_cone : x ∈ hs.to_cone s ↔ ∃ (c > 0) (y ∈ s), (c : ℝ) • y = x := by simp only [to_cone, convex_cone.mem_mk, mem_Union, mem_smul_set, eq_comm, exists_prop] lemma mem_to_cone' : x ∈ hs.to_cone s ↔ ∃ c > 0, (c : ℝ) • x ∈ s := begin refine hs.mem_to_cone.trans ⟨_, _⟩, { rintros ⟨c, hc, y, hy, rfl⟩, exact ⟨c⁻¹, inv_pos.2 hc, by rwa [smul_smul, inv_mul_cancel (ne_of_gt hc), one_smul]⟩ }, { rintros ⟨c, hc, hcx⟩, exact ⟨c⁻¹, inv_pos.2 hc, _, hcx, by rw [smul_smul, inv_mul_cancel (ne_of_gt hc), one_smul]⟩ } end lemma subset_to_cone : s ⊆ hs.to_cone s := λ x hx, hs.mem_to_cone'.2 ⟨1, zero_lt_one, by rwa one_smul⟩ /-- `hs.to_cone s` is the least cone that includes `s`. -/ lemma to_cone_is_least : is_least { t : convex_cone E | s ⊆ t } (hs.to_cone s) := begin refine ⟨hs.subset_to_cone, λ t ht x hx, _⟩, rcases hs.mem_to_cone.1 hx with ⟨c, hc, y, hy, rfl⟩, exact t.smul_mem hc (ht hy) end lemma to_cone_eq_Inf : hs.to_cone s = Inf { t : convex_cone E | s ⊆ t } := hs.to_cone_is_least.is_glb.Inf_eq.symm end convex lemma convex_hull_to_cone_is_least (s : set E) : is_least {t : convex_cone E | s ⊆ t} ((convex_convex_hull s).to_cone _) := begin convert (convex_convex_hull s).to_cone_is_least, ext t, exact ⟨λ h, convex_hull_min h t.convex, λ h, subset.trans (subset_convex_hull s) h⟩ end lemma convex_hull_to_cone_eq_Inf (s : set E) : (convex_convex_hull s).to_cone _ = Inf {t : convex_cone E | s ⊆ t} := (convex_hull_to_cone_is_least s).is_glb.Inf_eq.symm /-! ### M. Riesz extension theorem Given a convex cone `s` in a vector space `E`, a submodule `p`, and a linear `f : p → ℝ`, assume that `f` is nonnegative on `p ∩ s` and `p + s = E`. Then there exists a globally defined linear function `g : E → ℝ` that agrees with `f` on `p`, and is nonnegative on `s`. We prove this theorem using Zorn's lemma. `riesz_extension.step` is the main part of the proof. It says that if the domain `p` of `f` is not the whole space, then `f` can be extended to a larger subspace `p ⊔ span ℝ {y}` without breaking the non-negativity condition. In `riesz_extension.exists_top` we use Zorn's lemma to prove that we can extend `f` to a linear map `g` on `⊤ : submodule E`. Mathematically this is the same as a linear map on `E` but in Lean `⊤ : submodule E` is isomorphic but is not equal to `E`. In `riesz_extension` we use this isomorphism to prove the theorem. -/ namespace riesz_extension open submodule variables (s : convex_cone E) (f : linear_pmap ℝ E ℝ) /-- Induction step in M. Riesz extension theorem. Given a convex cone `s` in a vector space `E`, a partially defined linear map `f : f.domain → ℝ`, assume that `f` is nonnegative on `f.domain ∩ p` and `p + s = E`. If `f` is not defined on the whole `E`, then we can extend it to a larger submodule without breaking the non-negativity condition. -/ lemma step (nonneg : ∀ x : f.domain, (x : E) ∈ s → 0 ≤ f x) (dense : ∀ y, ∃ x : f.domain, (x : E) + y ∈ s) (hdom : f.domain ≠ ⊤) : ∃ g, f < g ∧ ∀ x : g.domain, (x : E) ∈ s → 0 ≤ g x := begin rcases exists_of_lt (lt_top_iff_ne_top.2 hdom) with ⟨y, hy', hy⟩, clear hy', obtain ⟨c, le_c, c_le⟩ : ∃ c, (∀ x : f.domain, -(x:E) - y ∈ s → f x ≤ c) ∧ (∀ x : f.domain, (x:E) + y ∈ s → c ≤ f x), { set Sp := f '' {x : f.domain | (x:E) + y ∈ s}, set Sn := f '' {x : f.domain | -(x:E) - y ∈ s}, suffices : (upper_bounds Sn ∩ lower_bounds Sp).nonempty, by simpa only [set.nonempty, upper_bounds, lower_bounds, ball_image_iff] using this, refine exists_between_of_forall_le (nonempty.image f _) (nonempty.image f (dense y)) _, { rcases (dense (-y)) with ⟨x, hx⟩, rw [← neg_neg x, coe_neg] at hx, exact ⟨_, hx⟩ }, rintros a ⟨xn, hxn, rfl⟩ b ⟨xp, hxp, rfl⟩, have := s.add_mem hxp hxn, rw [add_assoc, add_sub_cancel'_right, ← sub_eq_add_neg, ← coe_sub] at this, replace := nonneg _ this, rwa [f.map_sub, sub_nonneg] at this }, have hy' : y ≠ 0, from λ hy₀, hy (hy₀.symm ▸ zero_mem _), refine ⟨f.sup (linear_pmap.mk_span_singleton y (-c) hy') _, _, _⟩, { refine linear_pmap.sup_h_of_disjoint _ _ (submodule.disjoint_span_singleton.2 _), exact (λ h, (hy h).elim) }, { refine lt_iff_le_not_le.2 ⟨f.left_le_sup _ _, λ H, _⟩, replace H := linear_pmap.domain_mono.monotone H, rw [linear_pmap.domain_sup, linear_pmap.domain_mk_span_singleton, sup_le_iff, span_le, singleton_subset_iff] at H, exact hy H.2 }, { rintros ⟨z, hz⟩ hzs, rcases mem_sup.1 hz with ⟨x, hx, y', hy', rfl⟩, rcases mem_span_singleton.1 hy' with ⟨r, rfl⟩, simp only [subtype.coe_mk] at hzs, rw [linear_pmap.sup_apply _ ⟨x, hx⟩ ⟨_, hy'⟩ ⟨_, hz⟩ rfl, linear_pmap.mk_span_singleton_apply, smul_neg, ← sub_eq_add_neg, sub_nonneg], rcases lt_trichotomy r 0 with hr|hr|hr, { have : -(r⁻¹ • x) - y ∈ s, by rwa [← s.smul_mem_iff (neg_pos.2 hr), smul_sub, smul_neg, neg_smul, neg_neg, smul_smul, mul_inv_cancel (ne_of_lt hr), one_smul, sub_eq_add_neg, neg_smul, neg_neg], replace := le_c (r⁻¹ • ⟨x, hx⟩) this, rwa [← mul_le_mul_left (neg_pos.2 hr), ← neg_mul_eq_neg_mul, ← neg_mul_eq_neg_mul, neg_le_neg_iff, f.map_smul, smul_eq_mul, ← mul_assoc, mul_inv_cancel (ne_of_lt hr), one_mul] at this }, { subst r, simp only [zero_smul, add_zero] at hzs ⊢, apply nonneg, exact hzs }, { have : r⁻¹ • x + y ∈ s, by rwa [← s.smul_mem_iff hr, smul_add, smul_smul, mul_inv_cancel (ne_of_gt hr), one_smul], replace := c_le (r⁻¹ • ⟨x, hx⟩) this, rwa [← mul_le_mul_left hr, f.map_smul, smul_eq_mul, ← mul_assoc, mul_inv_cancel (ne_of_gt hr), one_mul] at this } } end theorem exists_top (p : linear_pmap ℝ E ℝ) (hp_nonneg : ∀ x : p.domain, (x : E) ∈ s → 0 ≤ p x) (hp_dense : ∀ y, ∃ x : p.domain, (x : E) + y ∈ s) : ∃ q ≥ p, q.domain = ⊤ ∧ ∀ x : q.domain, (x : E) ∈ s → 0 ≤ q x := begin replace hp_nonneg : p ∈ { p | _ }, by { rw mem_set_of_eq, exact hp_nonneg }, obtain ⟨q, hqs, hpq, hq⟩ := zorn.zorn_partial_order₀ _ _ _ hp_nonneg, { refine ⟨q, hpq, _, hqs⟩, contrapose! hq, rcases step s q hqs _ hq with ⟨r, hqr, hr⟩, { exact ⟨r, hr, le_of_lt hqr, ne_of_gt hqr⟩ }, { exact λ y, let ⟨x, hx⟩ := hp_dense y in ⟨of_le hpq.left x, hx⟩ } }, { intros c hcs c_chain y hy, clear hp_nonneg hp_dense p, have cne : c.nonempty := ⟨y, hy⟩, refine ⟨linear_pmap.Sup c c_chain.directed_on, _, λ _, linear_pmap.le_Sup c_chain.directed_on⟩, rintros ⟨x, hx⟩ hxs, have hdir : directed_on (≤) (linear_pmap.domain '' c), from directed_on_image.2 (c_chain.directed_on.mono linear_pmap.domain_mono.monotone), rcases (mem_Sup_of_directed (cne.image _) hdir).1 hx with ⟨_, ⟨f, hfc, rfl⟩, hfx⟩, have : f ≤ linear_pmap.Sup c c_chain.directed_on, from linear_pmap.le_Sup _ hfc, convert ← hcs hfc ⟨x, hfx⟩ hxs, apply this.2, refl } end end riesz_extension /-- M. Riesz extension theorem: given a convex cone `s` in a vector space `E`, a submodule `p`, and a linear `f : p → ℝ`, assume that `f` is nonnegative on `p ∩ s` and `p + s = E`. Then there exists a globally defined linear function `g : E → ℝ` that agrees with `f` on `p`, and is nonnegative on `s`. -/ theorem riesz_extension (s : convex_cone E) (f : linear_pmap ℝ E ℝ) (nonneg : ∀ x : f.domain, (x : E) ∈ s → 0 ≤ f x) (dense : ∀ y, ∃ x : f.domain, (x : E) + y ∈ s) : ∃ g : E →ₗ[ℝ] ℝ, (∀ x : f.domain, g x = f x) ∧ (∀ x ∈ s, 0 ≤ g x) := begin rcases riesz_extension.exists_top s f nonneg dense with ⟨⟨g_dom, g⟩, ⟨hpg, hfg⟩, htop, hgs⟩, clear hpg, refine ⟨g.comp (linear_equiv.of_top _ htop).symm, _, _⟩; simp only [comp_apply, linear_equiv.coe_coe, linear_equiv.of_top_symm_apply], { exact λ x, (hfg (submodule.coe_mk _ _).symm).symm }, { exact λ x hx, hgs ⟨x, _⟩ hx } end /-- Hahn-Banach theorem: if `N : E → ℝ` is a sublinear map, `f` is a linear map defined on a subspace of `E`, and `f x ≤ N x` for all `x` in the domain of `f`, then `f` can be extended to the whole space to a linear map `g` such that `g x ≤ N x` for all `x`. -/ theorem exists_extension_of_le_sublinear (f : linear_pmap ℝ E ℝ) (N : E → ℝ) (N_hom : ∀ (c : ℝ), 0 < c → ∀ x, N (c • x) = c * N x) (N_add : ∀ x y, N (x + y) ≤ N x + N y) (hf : ∀ x : f.domain, f x ≤ N x) : ∃ g : E →ₗ[ℝ] ℝ, (∀ x : f.domain, g x = f x) ∧ (∀ x, g x ≤ N x) := begin let s : convex_cone (E × ℝ) := { carrier := {p : E × ℝ | N p.1 ≤ p.2 }, smul_mem' := λ c hc p hp, calc N (c • p.1) = c * N p.1 : N_hom c hc p.1 ... ≤ c * p.2 : mul_le_mul_of_nonneg_left hp (le_of_lt hc), add_mem' := λ x hx y hy, le_trans (N_add _ _) (add_le_add hx hy) }, obtain ⟨g, g_eq, g_nonneg⟩ := riesz_extension s ((-f).coprod (linear_map.id.to_pmap ⊤)) _ _; simp only [linear_pmap.coprod_apply, to_pmap_apply, id_apply, linear_pmap.neg_apply, ← sub_eq_neg_add, sub_nonneg, subtype.coe_mk] at *, replace g_eq : ∀ (x : f.domain) (y : ℝ), g (x, y) = y - f x, { intros x y, simpa only [subtype.coe_mk, subtype.coe_eta] using g_eq ⟨(x, y), ⟨x.2, trivial⟩⟩ }, { refine ⟨-g.comp (inl ℝ E ℝ), _, _⟩; simp only [neg_apply, inl_apply, comp_apply], { intro x, simp [g_eq x 0] }, { intro x, have A : (x, N x) = (x, 0) + (0, N x), by simp, have B := g_nonneg ⟨x, N x⟩ (le_refl (N x)), rw [A, map_add, ← neg_le_iff_add_nonneg] at B, have C := g_eq 0 (N x), simp only [submodule.coe_zero, f.map_zero, sub_zero] at C, rwa ← C } }, { exact λ x hx, le_trans (hf _) hx }, { rintros ⟨x, y⟩, refine ⟨⟨(0, N x - y), ⟨f.domain.zero_mem, trivial⟩⟩, _⟩, simp only [convex_cone.mem_mk, mem_set_of_eq, subtype.coe_mk, prod.fst_add, prod.snd_add, zero_add, sub_add_cancel] } end
183a9c165050293b7503d23ab46d37fef5c42e9d
86f6f4f8d827a196a32bfc646234b73328aeb306
/examples/sets_functions_and_relations/unnamed_1118.lean
815cf57de911d47acfe0ad71d2b67840daa964cd
[]
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
222
lean
import data.set.function open set variables {α β : Type*} variables (f : α → β) (s : set α) -- BEGIN example : inj_on f s ↔ ∀ x₁ ∈ s, ∀ x₂ ∈ s, f x₁ = f x₂ → x₁ = x₂ := iff.refl _ -- END
31ec1fe1ca64538b15e064b059ad1fea839aaefa
c9ba4946202cfd1e2586e71960dfed00503dcdf4
/src/object_k/object_pattern.lean
f8e1e2ddbe7b16e21f5756efc51666ab635417d0
[]
no_license
ammkrn/learning_semantics_of_k
f55f669b369e32ef8407c16521b21ac5c106dc4d
c1487b538e1decc0f1fd389cd36bc36d2da012ab
refs/heads/master
1,588,081,593,954
1,552,449,093,000
1,552,449,093,000
175,315,800
0
0
null
null
null
null
UTF-8
Lean
false
false
12,852
lean
import object_k.object_sort import .object_sort import .object_var import .object_symbol open nat open has_sort -- ######################################################### -- ##### object_pattern ############################## inductive object_pattern : Type | object_variable_as_pattern : ~Variable → object_pattern -- ?? | object_application : ~Symbol → list object_pattern → object_pattern | object_and : object_sort → object_pattern → object_pattern → object_pattern | object_not : object_sort → object_pattern → object_pattern | object_exsts : object_sort → ~Variable → object_pattern → object_pattern instance : decidable_eq object_pattern := by tactic.mk_dec_eq_instance open object_pattern notation `~Pattern` := object_pattern notation `~variableAsPattern` := object_variable_as_pattern notation `~PatternList` := list object_pattern notation φ `~∧` s `;` ψ := object_and s φ ψ notation `~apply` σ . L := object_application σ L notation `~∃` s `,` v `:`s `.` φ := object_exsts s (object_variable.mk v s) φ notation `~∃` s `,` v `.` φ := object_exsts s v φ notation `~∃*` v `.` φ := object_exsts (getSort v) v φ notation `~¬` := object_not notation `~¬` := object_not -- φ₁ ∨ φ₂ ≣ ¬ (¬ φ₁ ∧ ¬ φ₂) --| s φ ψ := (~¬s φ) ~∧s; (~¬s ψ) notation φ `~∨` s `;` ψ := object_not s (object_and s (object_not s φ) (object_not s ψ)) -- φ → ψ ≣ ¬ φ ∨ ψ notation φ `~` s `~>` ψ := (~¬s φ) ~∨ s; (ψ) --notation φ `~` s `~>` ψ := --| s φ ψ := object_and (s) (object_implies (s) (φ) (ψ)) (object_implies (s) (ψ) (φ)) -- φ ↔ ψ ≣ φ →s ψ ∧s ψ →s φ notation φ `<~>` s `;` ψ := (φ ~s~> ψ) ~∧s; (ψ ~s~> φ) notation `~∀` s `,` name `:` sort `.` pat := ~¬s (~∃s, name : sort . pat) notation `~∀` s `,` v `.` φ := ~¬s (~∃s, v . ~¬s φ) notation `~∀` v `:` s `.` φ := ~∀ (getSort v) , v : s . φ -- #'ceil is defined as a symbol. -- ⌈_⌉ (s1, s2) ∈ Σ s1, s2 def object_'ceil : object_sort → object_sort → ~Symbol | s1 s2 := ⟨ "'ceil", [s1, s2], [s1], s2 ⟩ notation `#'ceil` (s1, s2) := object_'ceil s1 s2 notation `⌈` φ `⌉` (s1, s2) := ~apply (object_'ceil s1 s2) . [φ] notation `⌊` φ `⌋` (s1, s2) := ~¬s2 ⌈~¬s1 φ⌉ (s1, s2) notation φ1 `~=` (s1, s2) `;` φ2 := ⌊ ((φ1) <~> s1; (φ2)) ⌋ (s1, s2) notation φ1 `¬~=` (s1, s2) `;` φ2 := object_not s1 (⌊ ((φ1) <~> s1; (φ2)) ⌋ (s1, s2)) notation φ `~∈` (s1, s2) `;` ψ := ⌈(φ ~∧s1; ψ)⌉ (s1, s2) --notation φ `#∈` (s1, s2) `;` ψ := ⌈object_and s1 φ ψ⌉ (s1, s2) notation `~⊤` s := object_exsts s (object_variable.mk "⊤" s) (~variableAsPattern (object_variable.mk "⊤" s)) notation `~⊥` s := object_not s (~⊤s) open object_pattern definition object_var_elems_to_pat : string → object_sort → object_pattern | str sort := ~variableAsPattern (object_variable.mk str sort) definition object_get_pattern_sort : object_pattern → object_sort | (object_variable_as_pattern v) := getSort v | (object_application (σ) (Sigma)) := getSort σ | (object_and s φ₁ φ₂) := s | (object_not s φ) := s | (object_exsts s v φ) := s -- instance object_pattern.has_sort : has_sort object_pattern object_sort := ⟨ object_get_pattern_sort ⟩ -- ################ Auxiliary stuff for ⊤ def object_btop : bool → (object_sort → ~Pattern) | tt s := ~⊤ s | ff s := ~⊥ s def object_is_top : object_pattern → bool | (object_exsts _ (object_variable.mk "⊤" _) (~variableAsPattern (object_variable.mk "⊤" _))) := tt | _ := ff def object_is_bottom : object_pattern → bool | (object_not _ φ) := if object_is_top φ then tt else ff | _ := ff mutual def object_pattern_to_string, object_pattern_list_to_string with object_pattern_to_string : object_pattern → string | (~variableAsPattern (v)) := "(~var : " ++ (repr v) ++ ")" | (object_application σ L) := "(~apply " ++ (repr σ) ++ " . (" ++ (object_pattern_list_to_string L) ++ ")" ++ ")" | (object_and (s) (φ1) (φ2)) := "(" ++ (object_pattern_to_string φ1) ++ " ∧" ++ (repr s) ++ " " ++ (object_pattern_to_string φ2) ++ ")" | (object_not (s) (φ)) := if object_is_top φ then ("( ⊥" ++ (repr (getSort φ))) ++ " )" else "( ¬" ++ (repr s) ++ " " ++ (object_pattern_to_string φ) ++ " )" | (object_exsts (s) (v) (φ)) := if (object_is_top (object_exsts s v φ)) then ("( ⊤" ++ repr s) ++ " )" else "(∃" ++ (repr s) ++ ", (" ++ (repr v) ++ ":" ++ (repr (getSort v)) ++ ") " ++ " . " ++ (object_pattern_to_string φ) ++ " )" with object_pattern_list_to_string : list object_pattern → string | [] := "" | (hd :: tl) := (object_pattern_to_string hd) ++ ", " ++ object_pattern_list_to_string tl instance : has_repr object_pattern := ⟨ object_pattern_to_string ⟩ -- ~apply object_get_fv . [φ1, φ2, ...] -- This definition is based on the axioms outlined on p. 33 -- The interesting case is the existential quantifier, which acts -- as variable binding, and therefore removes the bound variable -- from the eventual list of free variables. mutual def object_get_fv, object_get_fv_from_patterns with object_get_fv : ~Pattern → ~VariableList | (~variableAsPattern v) := [v] | (object_application σ L) := object_get_fv_from_patterns L | (object_and s φ1 φ2) := object_get_fv φ1 ++ object_get_fv φ2 | (object_not s φ) := object_get_fv φ | (object_exsts s v φ) := delete_variable_list v (object_get_fv φ) with object_get_fv_from_patterns : ~PatternList → ~VariableList | [] := [] | (hd :: tl) := object_get_fv hd ++ object_get_fv_from_patterns tl def object_occurs_free : object_sort → ~Variable → ~Pattern → ~Pattern | carrier v φ := object_btop (list.mem v (object_get_fv φ)) carrier def object_longest_string : list #String → #String | l := list.foldl (λ s : #String, (λ acc : #String, if string.length (s) > string.length (acc) then s else acc)) ("") (l) def object_fresh_name : ~PatternList → #String | l := let longest_varname := object_longest_string (list.map getName (object_get_fv_from_patterns (l))) in (longest_varname ++ "_a") definition object_unwrap : option ~Pattern → ~Pattern | none := ~⊥ (object_sort.mk ⟨ "BottomSort" ⟩ object_sort_list.nil) | (some φ) := φ --φ [ψ / v] definition object_substitute : ℕ → ~Pattern → ~Pattern → ~Variable → option ~Pattern | zero a b c := a | (succ n) (~variableAsPattern (u)) (ψ) (v) := if u = v then some ψ else some (~variableAsPattern (u)) --| (succ n) (object_application (σ) (L)) ψ v := ~apply σ . (list.map (λ p : object_pattern, object_substitute n p ψ v) L) | (succ n) (object_application (σ) (L)) ψ v := let mapped : list (option ~Pattern) := list.map (λ p : object_pattern, object_substitute n p ψ v) L, terminated : bool := list.any mapped (λ x : option ~Pattern, x = none), object_unwrapped : ~PatternList := list.map(λ p : option ~Pattern, object_unwrap p) mapped in if terminated then none else some (~apply σ . object_unwrapped) | (succ n) (object_and (s) (φ1) (φ2)) (ψ) (v) := let lhs : option ~Pattern := object_substitute n φ1 ψ v, rhs : option ~Pattern := object_substitute n φ2 ψ v in match (lhs, rhs) with (none, none) := none, (some l, none) := none, (none, some r) := none, (some l, some r) := some (l ~∧s; r) end | (succ n) (object_not (s) (φ)) (ψ) (v) := let mapped : option ~Pattern := object_substitute n φ ψ v in match mapped with (some φ') := some (~¬s φ'), (none) := none end | (succ n) (object_exsts (s') (x) φ) (ψ) (v) := let x'_var := object_variable.mk (object_fresh_name [φ, ψ, ~variableAsPattern v]) s', x'_pat := ~variableAsPattern x'_var, mapped_one : option ~Pattern := object_substitute (n) (φ) (x'_pat) (x) in match mapped_one with none := none, (some φ') := let mapped_two : option ~Pattern := object_substitute (n) (φ') (ψ) (v) in match mapped_two with none := none, (some φ'') := some (~∃s', (x'_var) . (x'_pat) ~∧s'; ~∃s', (x'_var) . (φ'')) end end -- gas parameter is made implicit by giving some default amount of gas. definition object_substitute_i : ~Pattern → ~Pattern → ~Variable → option ~Pattern := object_substitute 100 -- object_subst_rel v φ ψ τ ; the result of the object_substitution of ψ for all -- occurences of v in φ。 -- This is modeled as v → original pattern → object_substituting pattern → object_substitution result. -- So for the assertion ' the pattern τ properly represents object_substitution of ψ for v -- in pattern φ, you would prove 'subst v φ ψ τ' for the appropriate construction. mutual inductive object_subst, object_subst_list with object_subst : ~Variable → ~Pattern → ~Pattern → ~Pattern → Prop | application : ∀ {σ : ~Symbol} {L ζ : ~PatternList} {φ ψ : ~Pattern} {v : ~Variable}, object_subst_list v L ψ ζ → object_subst v (~apply σ . L) ψ (~apply σ . ζ) | and : ∀ {s : object_sort} {φ1 φ2 τ1 τ2 ψ: ~Pattern} {v : ~Variable}, object_subst v φ1 ψ (τ1) → object_subst v φ2 ψ (τ2) → object_subst v (φ1 ~∧s; φ2) ψ (τ1 ~∧s; τ2) | not : ∀ {s : object_sort} {φ ψ τ : ~Pattern} {v : ~Variable}, object_subst v φ ψ τ → object_subst v (~¬s φ) ψ (~¬s τ) | var_eq : ∀ {v : ~Variable} {φ ψ : ~Pattern}, φ = (~variableAsPattern v) → object_subst v φ ψ ψ | var_neq : ∀ {v : ~Variable} {φ ψ : ~Pattern}, (∃ u, ~variableAsPattern u = φ ∧ v ≠ u) → object_subst v φ ψ φ | exsts : ∀ {s s' : object_sort} {v : ~Variable} {φ ψ τ : ~Pattern}, -- if the object_substitution φ [ x':s / x:s ] is properly represented in τ, object_subst (object_variable.mk "x" s) φ (~variableAsPattern (object_variable.mk "x'" s)) τ -- and the object_substitution φ [ ψ / v ] is properly represented in τ, → object_subst (v) φ ψ τ -- then the big conjuctive object_substitution result is good. → object_subst v (~∃s', "x":s . φ) ψ (~∃s', " x' ":s . (~variableAsPattern $ object_variable.mk (object_fresh_name [φ, ψ, (~variableAsPattern v)]) (s)) ~∧s; (~∃s', " x' ":s . τ)) with object_subst_list : ~Variable → list ~Pattern → ~Pattern → list ~Pattern → Prop | nil : ∀ {v : ~Variable} {ψ τ : ~Pattern}, object_subst_list v [] ψ [] | cons : ∀ {v : ~Variable} {L ζ: list ~Pattern} {φ ψ τ : ~Pattern} , object_subst_list v L ψ ζ → object_subst v φ ψ τ → object_subst_list v (φ :: L) ψ (τ :: ζ) mutual def object_sc, object_sc_l with object_sc : object_sort → ~Pattern → ~Pattern → ~Pattern | carrier (φ) (~apply σ . L) := object_sc_l carrier φ L | carrier (φ) (ψ) := if φ = ψ then ~⊤ carrier else ~⊥ carrier with object_sc_l : object_sort → ~Pattern → ~PatternList → ~Pattern | carrier φ [] := ~⊥ carrier | carrier φ (hd :: tl) := have 2 < 1 + (1 + (1 + list.sizeof tl)), by { intros, generalize : (list.sizeof tl = n), simp, apply nat.lt_add_left, apply nat.lt.base, }, have 3 < 1 + (1 + (1 + (1 + list.sizeof tl))), by { intros, generalize : (list.sizeof tl = n), simp, apply nat.lt_add_left, apply nat.lt.base,}, if (object_sc carrier φ hd = ~⊤ carrier) then ~⊤ carrier else (object_sc_l carrier φ tl) -- ####### Coercions/Lifts -- ~Symbol → ~Pattern def object_symbol.to_object_pattern : ~Symbol → ~Pattern | σ := ~apply σ . [] instance object_symbol_to_object_pattern_lift : has_lift ~Symbol ~Pattern := ⟨ object_symbol.to_object_pattern ⟩ -- ~SymbolList → ~PatternList def object_symbol_list.to_object_pattern_list : ~SymbolList → ~PatternList | L := list.map (λ σ : ~Symbol, lift σ) L instance object_symbol_list_to_object_pattern_list_lift : has_lift ~SymbolList ~PatternList := ⟨ object_symbol_list.to_object_pattern_list ⟩ -- ~Variable → ~Pattern def object_variable.to_object_pattern_lift : ~Variable → ~Pattern | v := ~variableAsPattern v instance object_variable_to_object_pattern_lift : has_lift ~Variable ~Pattern := ⟨ object_variable.to_object_pattern_lift ⟩ instance : is_Pattern object_pattern := ⟨ ~Variable , object_sort , object_is_top , object_is_bottom , object_get_fv , object_get_fv_from_patterns , object_occurs_free , object_btop , object_sc , object_sc_l , object_fresh_name , object_substitute_i ⟩
5fdb8569fd6b98d5639b122cb305ae2b4610bc51
46125763b4dbf50619e8846a1371029346f4c3db
/src/order/filter/extr.lean
9a53058cc37ad1685cd78560d7a79055dbfbf925
[ "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
18,660
lean
/- Copyright (c) 2019 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import order.filter.basic logic.relator tactic.alias /-! # Minimum and maximum w.r.t. a filter and on a aet ## Main Definitions This file defines six predicates of the form `is_A_B`, where `A` is `min`, `max`, or `extr`, and `B` is `filter` or `on`. * `is_min_filter f l a` means that `f a ≤ f x` in some `l`-neighborhood of `a`; * `is_max_filter f l a` means that `f x ≤ f a` in some `l`-neighborhood of `a`; * `is_extr_filter f l a` means `is_min_filter f l a` or `is_max_filter f l a`. Similar predicates with `_on` suffix are particular cases for `l = principal s`. ## Main statements ### Change of the filter (set) argument * `is_*_filter.filter_mono` : replace the filter with a smaller one; * `is_*_filter.filter_inf` : replace a filter `l` with `l ⊓ l'`; * `is_*_on.on_subset` : restrict to a smaller set; * `is_*_on.inter` : replace a set `s` wtih `s ∩ t`. ### Composition * `is_*_*.comp_mono` : if `x` is an extremum for `f` and `g` is a monotone function, then `x` is an extremum for `g ∘ f`; * `is_*_*.comp_antimono` : similarly for the case of monotonically decreasing `g`; * `is_*_*.bicomp_mono` : if `x` is an extremum of the same type for `f` and `g` and a binary operation `op` is monotone in both arguments, then `x` is an extremum of the same type for `λ x, op (f x) (g x)`. * `is_*_filter.comp_tendsto` : if `g x` is an extremum for `f` w.r.t. `l'` and `tendsto g l l'`, then `x` is an extremum for `f ∘ g` w.r.t. `l`. * `is_*_on.on_preimage` : if `g x` is an extremum for `f` on `s`, then `x` is an extremum for `f ∘ g` on `g ⁻¹' s`. ### Algebraic operations * `is_*_*.add` : if `x` is an extremum of the same type for two functions, then it is an extremum of the same type for their sum; * `is_*_*.neg` : if `x` is an extremum for `f`, then it is an extremum of the opposite type for `-f`; * `is_*_*.sub` : if `x` is an a minimum for `f` and a maximum for `g`, then it is a minimum for `f - g` and a maximum for `g - f`; * `is_*_*.max`, `is_*_*.min`, `is_*_*.sup`, `is_*_*.inf` : similarly for `is_*_*.add` for pointwise `max`, `min`, `sup`, `inf`, respectively. ### Miscellaneous definitions * `is_*_*_const` : any point is both a minimum and maximum for a constant function; * `is_min/max_*.is_ext` : any minimum/maximum point is an extremum; * `is_*_*.dual`, `is_*_*.undual`: conversion between codomains `α` and `dual α`; ## Missing features (TODO) * Multiplication and division; * `is_*_*.bicompl` : if `x` is a minimum for `f`, `y` is a minimum for `g`, and `op` is a monotone binary operation, then `(x, y)` is a minimum for `uncurry' (bicompl op f g)`. From this point of view, `is_*_*.bicomp` is a composition * It would be nice to have a tactic that specializes `comp_(anti)mono` or `bicomp_mono` based on a proof of monotonicity of a given (binary) function. The tactic should maintain a `meta` list of known (anti)monotone (binary) functions with their names, as well as a list of special types of filters, and define the missing lemmas once one of these two lists grows. -/ universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} open set lattice filter section preorder variables [preorder β] [preorder γ] variables (f : α → β) (s : set α) (l : filter α) (a : α) /-! ### Definitions -/ /-- `is_min_filter f l a` means that `f a ≤ f x` in some `l`-neighborhood of `a` -/ def is_min_filter : Prop := ∀ᶠ x in l, f a ≤ f x /-- `is_max_filter f l a` means that `f x ≤ f a` in some `l`-neighborhood of `a` -/ def is_max_filter : Prop := ∀ᶠ x in l, f x ≤ f a /-- `is_extr_filter f l a` means `is_min_filter f l a` or `is_max_filter f l a` -/ def is_extr_filter : Prop := is_min_filter f l a ∨ is_max_filter f l a /-- `is_min_on f s a` means that `f a ≤ f x` for all `x ∈ a`. Note that we do not assume `a ∈ s`. -/ def is_min_on := is_min_filter f (principal s) a /-- `is_max_on f s a` means that `f x ≤ f a` for all `x ∈ a`. Note that we do not assume `a ∈ s`. -/ def is_max_on := is_max_filter f (principal s) a /-- `is_extr_on f s a` means `is_min_on f s a` or `is_max_on f s a` -/ def is_extr_on : Prop := is_extr_filter f (principal s) a variables {f s a l} {t : set α} {l' : filter α} lemma is_extr_on.elim {p : Prop} : is_extr_on f s a → (is_min_on f s a → p) → (is_max_on f s a → p) → p := or.elim lemma is_min_on_iff : is_min_on f s a ↔ ∀ x ∈ s, f a ≤ f x := iff.rfl lemma is_max_on_iff : is_max_on f s a ↔ ∀ x ∈ s, f x ≤ f a := iff.rfl lemma is_min_on_univ_iff : is_min_on f univ a ↔ ∀ x, f a ≤ f x := univ_subset_iff.trans eq_univ_iff_forall lemma is_max_on_univ_iff : is_max_on f univ a ↔ ∀ x, f x ≤ f a := univ_subset_iff.trans eq_univ_iff_forall /-! ### Conversion to `is_extr_*` -/ lemma is_min_filter.is_extr : is_min_filter f l a → is_extr_filter f l a := or.inl lemma is_max_filter.is_extr : is_max_filter f l a → is_extr_filter f l a := or.inr lemma is_min_on.is_extr (h : is_min_on f s a) : is_extr_on f s a := h.is_extr lemma is_max_on.is_extr (h : is_max_on f s a) : is_extr_on f s a := h.is_extr /-! ### Constant function -/ lemma is_min_filter_const {b : β} : is_min_filter (λ _, b) l a := univ_mem_sets' $ λ _, le_refl _ lemma is_max_filter_const {b : β} : is_max_filter (λ _, b) l a := univ_mem_sets' $ λ _, le_refl _ lemma is_extr_filter_const {b : β} : is_extr_filter (λ _, b) l a := is_min_filter_const.is_extr lemma is_min_on_const {b : β} : is_min_on (λ _, b) s a := is_min_filter_const lemma is_max_on_const {b : β} : is_max_on (λ _, b) s a := is_max_filter_const lemma is_extr_on_const {b : β} : is_extr_on (λ _, b) s a := is_extr_filter_const /-! ### Order dual -/ lemma is_min_filter_dual_iff : @is_min_filter α (order_dual β) _ f l a ↔ is_max_filter f l a := iff.rfl lemma is_max_filter_dual_iff : @is_max_filter α (order_dual β) _ f l a ↔ is_min_filter f l a := iff.rfl lemma is_extr_filter_dual_iff : @is_extr_filter α (order_dual β) _ f l a ↔ is_extr_filter f l a := or_comm _ _ alias is_min_filter_dual_iff ↔ is_min_filter.undual is_max_filter.dual alias is_max_filter_dual_iff ↔ is_max_filter.undual is_min_filter.dual alias is_extr_filter_dual_iff ↔ is_extr_filter.undual is_extr_filter.dual lemma is_min_on_dual_iff : @is_min_on α (order_dual β) _ f s a ↔ is_max_on f s a := iff.rfl lemma is_max_on_dual_iff : @is_max_on α (order_dual β) _ f s a ↔ is_min_on f s a := iff.rfl lemma is_extr_on_dual_iff : @is_extr_on α (order_dual β) _ f s a ↔ is_extr_on f s a := or_comm _ _ alias is_min_on_dual_iff ↔ is_min_on.undual is_max_on.dual alias is_max_on_dual_iff ↔ is_max_on.undual is_min_on.dual alias is_extr_on_dual_iff ↔ is_extr_on.undual is_extr_on.dual /-! ### Operations on the filter/set -/ lemma is_min_filter.filter_mono (h : is_min_filter f l a) (hl : l' ≤ l) : is_min_filter f l' a := hl h lemma is_max_filter.filter_mono (h : is_max_filter f l a) (hl : l' ≤ l) : is_max_filter f l' a := hl h lemma is_extr_filter.filter_mono (h : is_extr_filter f l a) (hl : l' ≤ l) : is_extr_filter f l' a := h.elim (λ h, (h.filter_mono hl).is_extr) (λ h, (h.filter_mono hl).is_extr) lemma is_min_filter.filter_inf (h : is_min_filter f l a) (l') : is_min_filter f (l ⊓ l') a := h.filter_mono inf_le_left lemma is_max_filter.filter_inf (h : is_max_filter f l a) (l') : is_max_filter f (l ⊓ l') a := h.filter_mono inf_le_left lemma is_extr_filter.filter_inf (h : is_extr_filter f l a) (l') : is_extr_filter f (l ⊓ l') a := h.filter_mono inf_le_left lemma is_min_on.on_subset (hf : is_min_on f t a) (h : s ⊆ t) : is_min_on f s a := hf.filter_mono $ principal_mono.2 h lemma is_max_on.on_subset (hf : is_max_on f t a) (h : s ⊆ t) : is_max_on f s a := hf.filter_mono $ principal_mono.2 h lemma is_extr_on.on_subset (hf : is_extr_on f t a) (h : s ⊆ t) : is_extr_on f s a := hf.filter_mono $ principal_mono.2 h lemma is_min_on.inter (hf : is_min_on f s a) (t) : is_min_on f (s ∩ t) a := hf.on_subset (inter_subset_left s t) lemma is_max_on.inter (hf : is_max_on f s a) (t) : is_max_on f (s ∩ t) a := hf.on_subset (inter_subset_left s t) lemma is_extr_on.inter (hf : is_extr_on f s a) (t) : is_extr_on f (s ∩ t) a := hf.on_subset (inter_subset_left s t) /-! ### Composition with (anti)monotone functions -/ lemma is_min_filter.comp_mono (hf : is_min_filter f l a) {g : β → γ} (hg : monotone g) : is_min_filter (g ∘ f) l a := mem_sets_of_superset hf $ λ x hx, hg hx lemma is_max_filter.comp_mono (hf : is_max_filter f l a) {g : β → γ} (hg : monotone g) : is_max_filter (g ∘ f) l a := mem_sets_of_superset hf $ λ x hx, hg hx lemma is_extr_filter.comp_mono (hf : is_extr_filter f l a) {g : β → γ} (hg : monotone g) : is_extr_filter (g ∘ f) l a := hf.elim (λ hf, (hf.comp_mono hg).is_extr) (λ hf, (hf.comp_mono hg).is_extr) lemma is_min_filter.comp_antimono (hf : is_min_filter f l a) {g : β → γ} (hg : ∀ ⦃x y⦄, x ≤ y → g y ≤ g x) : is_max_filter (g ∘ f) l a := hf.dual.comp_mono (λ x y h, hg h) lemma is_max_filter.comp_antimono (hf : is_max_filter f l a) {g : β → γ} (hg : ∀ ⦃x y⦄, x ≤ y → g y ≤ g x) : is_min_filter (g ∘ f) l a := hf.dual.comp_mono (λ x y h, hg h) lemma is_extr_filter.comp_antimono (hf : is_extr_filter f l a) {g : β → γ} (hg : ∀ ⦃x y⦄, x ≤ y → g y ≤ g x) : is_extr_filter (g ∘ f) l a := hf.dual.comp_mono (λ x y h, hg h) lemma is_min_on.comp_mono (hf : is_min_on f s a) {g : β → γ} (hg : monotone g) : is_min_on (g ∘ f) s a := hf.comp_mono hg lemma is_max_on.comp_mono (hf : is_max_on f s a) {g : β → γ} (hg : monotone g) : is_max_on (g ∘ f) s a := hf.comp_mono hg lemma is_extr_on.comp_mono (hf : is_extr_on f s a) {g : β → γ} (hg : monotone g) : is_extr_on (g ∘ f) s a := hf.comp_mono hg lemma is_min_on.comp_antimono (hf : is_min_on f s a) {g : β → γ} (hg : ∀ ⦃x y⦄, x ≤ y → g y ≤ g x) : is_max_on (g ∘ f) s a := hf.comp_antimono hg lemma is_max_on.comp_antimono (hf : is_max_on f s a) {g : β → γ} (hg : ∀ ⦃x y⦄, x ≤ y → g y ≤ g x) : is_min_on (g ∘ f) s a := hf.comp_antimono hg lemma is_extr_on.comp_antimono (hf : is_extr_on f s a) {g : β → γ} (hg : ∀ ⦃x y⦄, x ≤ y → g y ≤ g x) : is_extr_on (g ∘ f) s a := hf.comp_antimono hg lemma is_min_filter.bicomp_mono [preorder δ] {op : β → γ → δ} (hop : ((≤) ⇒ (≤) ⇒ (≤)) op op) (hf : is_min_filter f l a) {g : α → γ} (hg : is_min_filter g l a) : is_min_filter (λ x, op (f x) (g x)) l a := mem_sets_of_superset (inter_mem_sets hf hg) $ λ x ⟨hfx, hgx⟩, hop hfx hgx lemma is_max_filter.bicomp_mono [preorder δ] {op : β → γ → δ} (hop : ((≤) ⇒ (≤) ⇒ (≤)) op op) (hf : is_max_filter f l a) {g : α → γ} (hg : is_max_filter g l a) : is_max_filter (λ x, op (f x) (g x)) l a := mem_sets_of_superset (inter_mem_sets hf hg) $ λ x ⟨hfx, hgx⟩, hop hfx hgx -- No `extr` version because we need `hf` and `hg` to be of the same kind lemma is_min_on.bicomp_mono [preorder δ] {op : β → γ → δ} (hop : ((≤) ⇒ (≤) ⇒ (≤)) op op) (hf : is_min_on f s a) {g : α → γ} (hg : is_min_on g s a) : is_min_on (λ x, op (f x) (g x)) s a := hf.bicomp_mono hop hg lemma is_max_on.bicomp_mono [preorder δ] {op : β → γ → δ} (hop : ((≤) ⇒ (≤) ⇒ (≤)) op op) (hf : is_max_on f s a) {g : α → γ} (hg : is_max_on g s a) : is_max_on (λ x, op (f x) (g x)) s a := hf.bicomp_mono hop hg /-! ### Composition with `tendsto` -/ lemma is_min_filter.comp_tendsto {g : δ → α} {l' : filter δ} {b : δ} (hf : is_min_filter f l (g b)) (hg : tendsto g l' l) : is_min_filter (f ∘ g) l' b := hg hf lemma is_max_filter.comp_tendsto {g : δ → α} {l' : filter δ} {b : δ} (hf : is_max_filter f l (g b)) (hg : tendsto g l' l) : is_max_filter (f ∘ g) l' b := hg hf lemma is_extr_filter.comp_tendsto {g : δ → α} {l' : filter δ} {b : δ} (hf : is_extr_filter f l (g b)) (hg : tendsto g l' l) : is_extr_filter (f ∘ g) l' b := hf.elim (λ hf, (hf.comp_tendsto hg).is_extr) (λ hf, (hf.comp_tendsto hg).is_extr) lemma is_min_on.on_preimage (g : δ → α) {b : δ} (hf : is_min_on f s (g b)) : is_min_on (f ∘ g) (g ⁻¹' s) b := hf.comp_tendsto (tendsto_principal_principal.mpr $ subset.refl _) lemma is_max_on.on_preimage (g : δ → α) {b : δ} (hf : is_max_on f s (g b)) : is_max_on (f ∘ g) (g ⁻¹' s) b := hf.comp_tendsto (tendsto_principal_principal.mpr $ subset.refl _) lemma is_extr_on.on_preimage (g : δ → α) {b : δ} (hf : is_extr_on f s (g b)) : is_extr_on (f ∘ g) (g ⁻¹' s) b := hf.elim (λ hf, (hf.on_preimage g).is_extr) (λ hf, (hf.on_preimage g).is_extr) end preorder /-! ### Pointwise addition -/ section ordered_comm_monoid variables [ordered_comm_monoid β] {f g : α → β} {a : α} {s : set α} {l : filter α} lemma is_min_filter.add (hf : is_min_filter f l a) (hg : is_min_filter g l a) : is_min_filter (λ x, f x + g x) l a := show is_min_filter (λ x, f x + g x) l a, from hf.bicomp_mono (λ x x' hx y y' hy, add_le_add' hx hy) hg lemma is_max_filter.add (hf : is_max_filter f l a) (hg : is_max_filter g l a) : is_max_filter (λ x, f x + g x) l a := show is_max_filter (λ x, f x + g x) l a, from hf.bicomp_mono (λ x x' hx y y' hy, add_le_add' hx hy) hg lemma is_min_on.add (hf : is_min_on f s a) (hg : is_min_on g s a) : is_min_on (λ x, f x + g x) s a := hf.add hg lemma is_max_on.add (hf : is_max_on f s a) (hg : is_max_on g s a) : is_max_on (λ x, f x + g x) s a := hf.add hg end ordered_comm_monoid /-! ### Pointwise negation and subtraction -/ section ordered_comm_group variables [ordered_comm_group β] {f g : α → β} {a : α} {s : set α} {l : filter α} lemma is_min_filter.neg (hf : is_min_filter f l a) : is_max_filter (λ x, -f x) l a := hf.comp_antimono (λ x y hx, neg_le_neg hx) lemma is_max_filter.neg (hf : is_max_filter f l a) : is_min_filter (λ x, -f x) l a := hf.comp_antimono (λ x y hx, neg_le_neg hx) lemma is_extr_filter.neg (hf : is_extr_filter f l a) : is_extr_filter (λ x, -f x) l a := hf.elim (λ hf, hf.neg.is_extr) (λ hf, hf.neg.is_extr) lemma is_min_on.neg (hf : is_min_on f s a) : is_max_on (λ x, -f x) s a := hf.comp_antimono (λ x y hx, neg_le_neg hx) lemma is_max_on.neg (hf : is_max_on f s a) : is_min_on (λ x, -f x) s a := hf.comp_antimono (λ x y hx, neg_le_neg hx) lemma is_extr_on.neg (hf : is_extr_on f s a) : is_extr_on (λ x, -f x) s a := hf.elim (λ hf, hf.neg.is_extr) (λ hf, hf.neg.is_extr) lemma is_min_filter.sub (hf : is_min_filter f l a) (hg : is_max_filter g l a) : is_min_filter (λ x, f x - g x) l a := hf.add hg.neg lemma is_max_filter.sub (hf : is_max_filter f l a) (hg : is_min_filter g l a) : is_max_filter (λ x, f x - g x) l a := hf.add hg.neg lemma is_min_on.sub (hf : is_min_on f s a) (hg : is_max_on g s a) : is_min_on (λ x, f x - g x) s a := hf.add hg.neg lemma is_max_on.sub (hf : is_max_on f s a) (hg : is_min_on g s a) : is_max_on (λ x, f x - g x) s a := hf.add hg.neg end ordered_comm_group /-! ### Pointwise `sup`/`inf` -/ section semilattice_sup variables [semilattice_sup β] {f g : α → β} {a : α} {s : set α} {l : filter α} lemma is_min_filter.sup (hf : is_min_filter f l a) (hg : is_min_filter g l a) : is_min_filter (λ x, f x ⊔ g x) l a := show is_min_filter (λ x, f x ⊔ g x) l a, from hf.bicomp_mono (λ x x' hx y y' hy, sup_le_sup hx hy) hg lemma is_max_filter.sup (hf : is_max_filter f l a) (hg : is_max_filter g l a) : is_max_filter (λ x, f x ⊔ g x) l a := show is_max_filter (λ x, f x ⊔ g x) l a, from hf.bicomp_mono (λ x x' hx y y' hy, sup_le_sup hx hy) hg lemma is_min_on.sup (hf : is_min_on f s a) (hg : is_min_on g s a) : is_min_on (λ x, f x ⊔ g x) s a := hf.sup hg lemma is_max_on.sup (hf : is_max_on f s a) (hg : is_max_on g s a) : is_max_on (λ x, f x ⊔ g x) s a := hf.sup hg end semilattice_sup section semilattice_inf variables [semilattice_inf β] {f g : α → β} {a : α} {s : set α} {l : filter α} lemma is_min_filter.inf (hf : is_min_filter f l a) (hg : is_min_filter g l a) : is_min_filter (λ x, f x ⊓ g x) l a := show is_min_filter (λ x, f x ⊓ g x) l a, from hf.bicomp_mono (λ x x' hx y y' hy, inf_le_inf hx hy) hg lemma is_max_filter.inf (hf : is_max_filter f l a) (hg : is_max_filter g l a) : is_max_filter (λ x, f x ⊓ g x) l a := show is_max_filter (λ x, f x ⊓ g x) l a, from hf.bicomp_mono (λ x x' hx y y' hy, inf_le_inf hx hy) hg lemma is_min_on.inf (hf : is_min_on f s a) (hg : is_min_on g s a) : is_min_on (λ x, f x ⊓ g x) s a := hf.inf hg lemma is_max_on.inf (hf : is_max_on f s a) (hg : is_max_on g s a) : is_max_on (λ x, f x ⊓ g x) s a := hf.inf hg end semilattice_inf /-! ### Pointwise `min`/`max` -/ section decidable_linear_order variables [decidable_linear_order β] {f g : α → β} {a : α} {s : set α} {l : filter α} lemma is_min_filter.min (hf : is_min_filter f l a) (hg : is_min_filter g l a) : is_min_filter (λ x, min (f x) (g x)) l a := show is_min_filter (λ x, min (f x) (g x)) l a, from hf.bicomp_mono (λ x x' hx y y' hy, min_le_min hx hy) hg lemma is_max_filter.min (hf : is_max_filter f l a) (hg : is_max_filter g l a) : is_max_filter (λ x, min (f x) (g x)) l a := show is_max_filter (λ x, min (f x) (g x)) l a, from hf.bicomp_mono (λ x x' hx y y' hy, min_le_min hx hy) hg lemma is_min_on.min (hf : is_min_on f s a) (hg : is_min_on g s a) : is_min_on (λ x, min (f x) (g x)) s a := hf.min hg lemma is_max_on.min (hf : is_max_on f s a) (hg : is_max_on g s a) : is_max_on (λ x, min (f x) (g x)) s a := hf.min hg lemma is_min_filter.max (hf : is_min_filter f l a) (hg : is_min_filter g l a) : is_min_filter (λ x, max (f x) (g x)) l a := show is_min_filter (λ x, max (f x) (g x)) l a, from hf.bicomp_mono (λ x x' hx y y' hy, max_le_max hx hy) hg lemma is_max_filter.max (hf : is_max_filter f l a) (hg : is_max_filter g l a) : is_max_filter (λ x, max (f x) (g x)) l a := show is_max_filter (λ x, max (f x) (g x)) l a, from hf.bicomp_mono (λ x x' hx y y' hy, max_le_max hx hy) hg lemma is_min_on.max (hf : is_min_on f s a) (hg : is_min_on g s a) : is_min_on (λ x, max (f x) (g x)) s a := hf.max hg lemma is_max_on.max (hf : is_max_on f s a) (hg : is_max_on g s a) : is_max_on (λ x, max (f x) (g x)) s a := hf.max hg end decidable_linear_order
8c0493505aa243be6240936799bad0b49248b90f
6e41ee3ac9b96e8980a16295cc21f131e731884f
/library/algebra/ordered_group.lean
2c9860bb20b227fdc890eeec1099aa7219bf8a4f
[ "Apache-2.0" ]
permissive
EgbertRijke/lean
3426cfa0e5b3d35d12fc3fd7318b35574cb67dc3
4f2e0c6d7fc9274d953cfa1c37ab2f3e799ab183
refs/heads/master
1,610,834,871,476
1,422,159,801,000
1,422,159,801,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
20,371
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: algebra.ordered_group Authors: Jeremy Avigad Partially ordered additive groups, modeled on Isabelle's library. We could refine the structures, but we would have to declare more inheritance paths. -/ import logic.eq data.unit data.sigma data.prod import algebra.function algebra.binary import algebra.group algebra.order open eq eq.ops -- note: ⁻¹ will be overloaded namespace algebra variable {A : Type} /- partially ordered monoids, such as the natural numbers -/ structure ordered_cancel_comm_monoid [class] (A : Type) extends add_comm_monoid A, add_left_cancel_semigroup A, add_right_cancel_semigroup A, order_pair A := (add_le_add_left : ∀a b, le a b → ∀c, le (add c a) (add c b)) (le_of_add_le_add_left : ∀a b c, le (add a b) (add a c) → le b c) section variables [s : ordered_cancel_comm_monoid A] variables {a b c d e : A} include s theorem add_le_add_left (H : a ≤ b) (c : A) : c + a ≤ c + b := !ordered_cancel_comm_monoid.add_le_add_left H c theorem add_le_add_right (H : a ≤ b) (c : A) : a + c ≤ b + c := (add.comm c a) ▸ (add.comm c b) ▸ (add_le_add_left H c) theorem add_le_add (Hab : a ≤ b) (Hcd : c ≤ d) : a + c ≤ b + d := le.trans (add_le_add_right Hab c) (add_le_add_left Hcd b) theorem add_lt_add_left (H : a < b) (c : A) : c + a < c + b := have H1 : c + a ≤ c + b, from add_le_add_left (le_of_lt H) c, have H2 : c + a ≠ c + b, from take H3 : c + a = c + b, have H4 : a = b, from add.left_cancel H3, ne_of_lt H H4, lt_of_le_of_ne H1 H2 theorem add_lt_add_right (H : a < b) (c : A) : a + c < b + c := (add.comm c a) ▸ (add.comm c b) ▸ (add_lt_add_left H c) theorem le_add_of_nonneg_right (H : b ≥ 0) : a ≤ a + b := !add_zero ▸ add_le_add_left H a theorem le_add_of_nonneg_left (H : b ≥ 0) : a ≤ b + a := !zero_add ▸ add_le_add_right H a theorem add_lt_add (Hab : a < b) (Hcd : c < d) : a + c < b + d := lt.trans (add_lt_add_right Hab c) (add_lt_add_left Hcd b) theorem add_lt_add_of_le_of_lt (Hab : a ≤ b) (Hcd : c < d) : a + c < b + d := lt_of_le_of_lt (add_le_add_right Hab c) (add_lt_add_left Hcd b) theorem add_lt_add_of_lt_of_le (Hab : a < b) (Hcd : c ≤ d) : a + c < b + d := lt_of_lt_of_le (add_lt_add_right Hab c) (add_le_add_left Hcd b) theorem lt_add_of_pos_right (H : b > 0) : a < a + b := !add_zero ▸ add_lt_add_left H a theorem lt_add_of_pos_left (H : b > 0) : a < b + a := !zero_add ▸ add_lt_add_right H a -- here we start using le_of_add_le_add_left. theorem le_of_add_le_add_left (H : a + b ≤ a + c) : b ≤ c := !ordered_cancel_comm_monoid.le_of_add_le_add_left H theorem le_of_add_le_add_right (H : a + b ≤ c + b) : a ≤ c := le_of_add_le_add_left ((add.comm a b) ▸ (add.comm c b) ▸ H) theorem lt_of_add_lt_add_left (H : a + b < a + c) : b < c := have H1 : b ≤ c, from le_of_add_le_add_left (le_of_lt H), have H2 : b ≠ c, from assume H3 : b = c, lt.irrefl _ (H3 ▸ H), lt_of_le_of_ne H1 H2 theorem lt_of_add_lt_add_right (H : a + b < c + b) : a < c := lt_of_add_lt_add_left ((add.comm a b) ▸ (add.comm c b) ▸ H) theorem add_le_add_left_iff (a b c : A) : a + b ≤ a + c ↔ b ≤ c := iff.intro le_of_add_le_add_left (assume H, add_le_add_left H _) theorem add_le_add_right_iff (a b c : A) : a + b ≤ c + b ↔ a ≤ c := iff.intro le_of_add_le_add_right (assume H, add_le_add_right H _) theorem add_lt_add_left_iff (a b c : A) : a + b < a + c ↔ b < c := iff.intro lt_of_add_lt_add_left (assume H, add_lt_add_left H _) theorem add_lt_add_right_iff (a b c : A) : a + b < c + b ↔ a < c := iff.intro lt_of_add_lt_add_right (assume H, add_lt_add_right H _) -- here we start using properties of zero. theorem add_nonneg (Ha : 0 ≤ a) (Hb : 0 ≤ b) : 0 ≤ a + b := !zero_add ▸ (add_le_add Ha Hb) theorem add_pos (Ha : 0 < a) (Hb : 0 < b) : 0 < a + b := !zero_add ▸ (add_lt_add Ha Hb) theorem add_pos_of_pos_of_nonneg (Ha : 0 < a) (Hb : 0 ≤ b) : 0 < a + b := !zero_add ▸ (add_lt_add_of_lt_of_le Ha Hb) theorem add_pos_of_nonneg_of_pos (Ha : 0 ≤ a) (Hb : 0 < b) : 0 < a + b := !zero_add ▸ (add_lt_add_of_le_of_lt Ha Hb) theorem add_nonpos (Ha : a ≤ 0) (Hb : b ≤ 0) : a + b ≤ 0 := !zero_add ▸ (add_le_add Ha Hb) theorem add_neg (Ha : a < 0) (Hb : b < 0) : a + b < 0 := !zero_add ▸ (add_lt_add Ha Hb) theorem add_neg_of_neg_of_nonpos (Ha : a < 0) (Hb : b ≤ 0) : a + b < 0 := !zero_add ▸ (add_lt_add_of_lt_of_le Ha Hb) theorem add_neg_of_nonpos_of_neg (Ha : a ≤ 0) (Hb : b < 0) : a + b < 0 := !zero_add ▸ (add_lt_add_of_le_of_lt Ha Hb) -- TODO: add nonpos version (will be easier with simplifier) theorem add_eq_zero_iff_eq_zero_and_eq_zero_of_nonneg_of_nonneg (Ha : 0 ≤ a) (Hb : 0 ≤ b) : a + b = 0 ↔ a = 0 ∧ b = 0 := iff.intro (assume Hab : a + b = 0, have Ha' : a ≤ 0, from calc a = a + 0 : add_zero ... ≤ a + b : add_le_add_left Hb ... = 0 : Hab, have Haz : a = 0, from le.antisymm Ha' Ha, have Hb' : b ≤ 0, from calc b = 0 + b : zero_add ... ≤ a + b : add_le_add_right Ha ... = 0 : Hab, have Hbz : b = 0, from le.antisymm Hb' Hb, and.intro Haz Hbz) (assume Hab : a = 0 ∧ b = 0, (and.elim_left Hab)⁻¹ ▸ (and.elim_right Hab)⁻¹ ▸ (add_zero 0)) theorem le_add_of_nonneg_of_le (Ha : 0 ≤ a) (Hbc : b ≤ c) : b ≤ a + c := !zero_add ▸ add_le_add Ha Hbc theorem le_add_of_le_of_nonneg (Hbc : b ≤ c) (Ha : 0 ≤ a) : b ≤ c + a := !add_zero ▸ add_le_add Hbc Ha theorem lt_add_of_pos_of_le (Ha : 0 < a) (Hbc : b ≤ c) : b < a + c := !zero_add ▸ add_lt_add_of_lt_of_le Ha Hbc theorem lt_add_of_le_of_pos (Hbc : b ≤ c) (Ha : 0 < a) : b < c + a := !add_zero ▸ add_lt_add_of_le_of_lt Hbc Ha theorem add_le_of_nonpos_of_le (Ha : a ≤ 0) (Hbc : b ≤ c) : a + b ≤ c := !zero_add ▸ add_le_add Ha Hbc theorem add_le_of_le_of_nonpos (Hbc : b ≤ c) (Ha : a ≤ 0) : b + a ≤ c := !add_zero ▸ add_le_add Hbc Ha theorem add_lt_of_neg_of_le (Ha : a < 0) (Hbc : b ≤ c) : a + b < c := !zero_add ▸ add_lt_add_of_lt_of_le Ha Hbc theorem add_lt_of_le_of_neg (Hbc : b ≤ c) (Ha : a < 0) : b + a < c := !add_zero ▸ add_lt_add_of_le_of_lt Hbc Ha theorem lt_add_of_nonneg_of_lt (Ha : 0 ≤ a) (Hbc : b < c) : b < a + c := !zero_add ▸ add_lt_add_of_le_of_lt Ha Hbc theorem lt_add_of_lt_of_nonneg (Hbc : b < c) (Ha : 0 ≤ a) : b < c + a := !add_zero ▸ add_lt_add_of_lt_of_le Hbc Ha theorem lt_add_of_pos_of_lt (Ha : 0 < a) (Hbc : b < c) : b < a + c := !zero_add ▸ add_lt_add Ha Hbc theorem lt_add_of_lt_of_pos (Hbc : b < c) (Ha : 0 < a) : b < c + a := !add_zero ▸ add_lt_add Hbc Ha theorem add_lt_of_nonpos_of_lt (Ha : a ≤ 0) (Hbc : b < c) : a + b < c := !zero_add ▸ add_lt_add_of_le_of_lt Ha Hbc theorem add_lt_of_lt_of_nonpos (Hbc : b < c) (Ha : a ≤ 0) : b + a < c := !add_zero ▸ add_lt_add_of_lt_of_le Hbc Ha theorem add_lt_of_neg_of_lt (Ha : a < 0) (Hbc : b < c) : a + b < c := !zero_add ▸ add_lt_add Ha Hbc theorem add_lt_of_lt_of_neg (Hbc : b < c) (Ha : a < 0) : b + a < c := !add_zero ▸ add_lt_add Hbc Ha end -- TODO: add properties of max and min /- partially ordered groups -/ structure ordered_comm_group [class] (A : Type) extends add_comm_group A, order_pair A := (add_le_add_left : ∀a b, le a b → ∀c, le (add c a) (add c b)) theorem ordered_comm_group.le_of_add_le_add_left [s : ordered_comm_group A] {a b c : A} (H : a + b ≤ a + c) : b ≤ c := have H' : -a + (a + b) ≤ -a + (a + c), from ordered_comm_group.add_le_add_left _ _ H _, !neg_add_cancel_left ▸ !neg_add_cancel_left ▸ H' definition ordered_comm_group.to_ordered_cancel_comm_monoid [instance] [coercion] [reducible] [s : ordered_comm_group A] : ordered_cancel_comm_monoid A := ⦃ ordered_cancel_comm_monoid, s, add_left_cancel := @add.left_cancel A s, add_right_cancel := @add.right_cancel A s, le_of_add_le_add_left := @ordered_comm_group.le_of_add_le_add_left A s ⦄ section variables [s : ordered_comm_group A] (a b c d e : A) include s theorem neg_le_neg {a b : A} (H : a ≤ b) : -b ≤ -a := have H1 : 0 ≤ -a + b, from !add.left_inv ▸ !(add_le_add_left H), !add_neg_cancel_right ▸ !zero_add ▸ add_le_add_right H1 (-b) theorem le_of_neg_le_neg {a b : A} (H : -b ≤ -a) : a ≤ b := neg_neg a ▸ neg_neg b ▸ neg_le_neg H theorem neg_le_neg_iff_le : -a ≤ -b ↔ b ≤ a := iff.intro le_of_neg_le_neg neg_le_neg theorem nonneg_of_neg_nonpos {a : A} (H : -a ≤ 0) : 0 ≤ a := le_of_neg_le_neg (neg_zero⁻¹ ▸ H) theorem neg_nonpos_of_nonneg {a : A} (H : 0 ≤ a) : -a ≤ 0 := neg_zero ▸ neg_le_neg H theorem neg_nonpos_iff_nonneg : -a ≤ 0 ↔ 0 ≤ a := iff.intro nonneg_of_neg_nonpos neg_nonpos_of_nonneg theorem nonpos_of_neg_nonneg {a : A} (H : 0 ≤ -a) : a ≤ 0 := le_of_neg_le_neg (neg_zero⁻¹ ▸ H) theorem neg_nonneg_of_nonpos {a : A} (H : a ≤ 0) : 0 ≤ -a := neg_zero ▸ neg_le_neg H theorem neg_nonneg_iff_nonpos : 0 ≤ -a ↔ a ≤ 0 := iff.intro nonpos_of_neg_nonneg neg_nonneg_of_nonpos theorem neg_lt_neg {a b : A} (H : a < b) : -b < -a := have H1 : 0 < -a + b, from !add.left_inv ▸ !(add_lt_add_left H), !add_neg_cancel_right ▸ !zero_add ▸ add_lt_add_right H1 (-b) theorem lt_of_neg_lt_neg {a b : A} (H : -b < -a) : a < b := neg_neg a ▸ neg_neg b ▸ neg_lt_neg H theorem neg_lt_neg_iff_lt : -a < -b ↔ b < a := iff.intro lt_of_neg_lt_neg neg_lt_neg theorem pos_of_neg_neg {a : A} (H : -a < 0) : 0 < a := lt_of_neg_lt_neg (neg_zero⁻¹ ▸ H) theorem neg_neg_of_pos {a : A} (H : 0 < a) : -a < 0 := neg_zero ▸ neg_lt_neg H theorem neg_neg_iff_pos : -a < 0 ↔ 0 < a := iff.intro pos_of_neg_neg neg_neg_of_pos theorem neg_of_neg_pos {a : A} (H : 0 < -a) : a < 0 := lt_of_neg_lt_neg (neg_zero⁻¹ ▸ H) theorem neg_pos_of_neg {a : A} (H : a < 0) : 0 < -a := neg_zero ▸ neg_lt_neg H theorem neg_pos_iff_neg : 0 < -a ↔ a < 0 := iff.intro neg_of_neg_pos neg_pos_of_neg theorem le_neg_iff_le_neg : a ≤ -b ↔ b ≤ -a := !neg_neg ▸ !neg_le_neg_iff_le theorem neg_le_iff_neg_le : -a ≤ b ↔ -b ≤ a := !neg_neg ▸ !neg_le_neg_iff_le theorem lt_neg_iff_lt_neg : a < -b ↔ b < -a := !neg_neg ▸ !neg_lt_neg_iff_lt theorem neg_lt_iff_neg_lt : -a < b ↔ -b < a := !neg_neg ▸ !neg_lt_neg_iff_lt theorem sub_nonneg_iff_le : 0 ≤ a - b ↔ b ≤ a := !sub_self ▸ !add_le_add_right_iff theorem sub_nonpos_iff_le : a - b ≤ 0 ↔ a ≤ b := !sub_self ▸ !add_le_add_right_iff theorem sub_pos_iff_lt : 0 < a - b ↔ b < a := !sub_self ▸ !add_lt_add_right_iff theorem sub_neg_iff_lt : a - b < 0 ↔ a < b := !sub_self ▸ !add_lt_add_right_iff theorem add_le_iff_le_neg_add : a + b ≤ c ↔ b ≤ -a + c := have H: a + b ≤ c ↔ -a + (a + b) ≤ -a + c, from iff.symm (!add_le_add_left_iff), !neg_add_cancel_left ▸ H theorem add_le_iff_le_sub_left : a + b ≤ c ↔ b ≤ c - a := !add.comm ▸ !add_le_iff_le_neg_add theorem add_le_iff_le_sub_right : a + b ≤ c ↔ a ≤ c - b := have H: a + b ≤ c ↔ a + b - b ≤ c - b, from iff.symm (!add_le_add_right_iff), !add_neg_cancel_right ▸ H theorem le_add_iff_neg_add_le : a ≤ b + c ↔ -b + a ≤ c := have H: a ≤ b + c ↔ -b + a ≤ -b + (b + c), from iff.symm (!add_le_add_left_iff), !neg_add_cancel_left ▸ H theorem le_add_iff_sub_left_le : a ≤ b + c ↔ a - b ≤ c := !add.comm ▸ !le_add_iff_neg_add_le theorem le_add_iff_sub_right_le : a ≤ b + c ↔ a - c ≤ b := have H: a ≤ b + c ↔ a - c ≤ b + c - c, from iff.symm (!add_le_add_right_iff), !add_neg_cancel_right ▸ H theorem add_lt_iff_lt_neg_add_left : a + b < c ↔ b < -a + c := have H: a + b < c ↔ -a + (a + b) < -a + c, from iff.symm (!add_lt_add_left_iff), !neg_add_cancel_left ▸ H theorem add_lt_iff_lt_neg_add_right : a + b < c ↔ a < -b + c := !add.comm ▸ !add_lt_iff_lt_neg_add_left theorem add_lt_iff_lt_sub_left : a + b < c ↔ b < c - a := !add.comm ▸ !add_lt_iff_lt_neg_add_left theorem add_lt_add_iff_lt_sub_right : a + b < c ↔ a < c - b := have H: a + b < c ↔ a + b - b < c - b, from iff.symm (!add_lt_add_right_iff), !add_neg_cancel_right ▸ H theorem lt_add_iff_neg_add_lt_left : a < b + c ↔ -b + a < c := have H: a < b + c ↔ -b + a < -b + (b + c), from iff.symm (!add_lt_add_left_iff), !neg_add_cancel_left ▸ H theorem lt_add_iff_neg_add_lt_right : a < b + c ↔ -c + a < b := !add.comm ▸ !lt_add_iff_neg_add_lt_left theorem lt_add_iff_sub_lt_left : a < b + c ↔ a - b < c := !add.comm ▸ !lt_add_iff_neg_add_lt_left theorem lt_add_iff_sub_lt_right : a < b + c ↔ a - c < b := !add.comm ▸ !lt_add_iff_sub_lt_left -- TODO: the Isabelle library has varations on a + b ≤ b ↔ a ≤ 0 theorem le_iff_le_of_sub_eq_sub {a b c d : A} (H : a - b = c - d) : a ≤ b ↔ c ≤ d := calc a ≤ b ↔ a - b ≤ 0 : iff.symm (sub_nonpos_iff_le a b) ... ↔ c - d ≤ 0 : H ▸ !iff.refl ... ↔ c ≤ d : sub_nonpos_iff_le c d theorem lt_iff_lt_of_sub_eq_sub {a b c d : A} (H : a - b = c - d) : a < b ↔ c < d := calc a < b ↔ a - b < 0 : iff.symm (sub_neg_iff_lt a b) ... ↔ c - d < 0 : H ▸ !iff.refl ... ↔ c < d : sub_neg_iff_lt c d theorem sub_le_sub_left {a b : A} (H : a ≤ b) (c : A) : c - b ≤ c - a := add_le_add_left (neg_le_neg H) c theorem sub_le_sub_right {a b : A} (H : a ≤ b) (c : A) : a - c ≤ b - c := add_le_add_right H (-c) theorem sub_le_sub {a b c d : A} (Hab : a ≤ b) (Hcd : c ≤ d) : a - d ≤ b - c := add_le_add Hab (neg_le_neg Hcd) theorem sub_lt_sub_left {a b : A} (H : a < b) (c : A) : c - b < c - a := add_lt_add_left (neg_lt_neg H) c theorem sub_lt_sub_right {a b : A} (H : a < b) (c : A) : a - c < b - c := add_lt_add_right H (-c) theorem sub_lt_sub {a b c d : A} (Hab : a < b) (Hcd : c < d) : a - d < b - c := add_lt_add Hab (neg_lt_neg Hcd) theorem sub_lt_sub_of_le_of_lt {a b c d : A} (Hab : a ≤ b) (Hcd : c < d) : a - d < b - c := add_lt_add_of_le_of_lt Hab (neg_lt_neg Hcd) theorem sub_lt_sub_of_lt_of_le {a b c d : A} (Hab : a < b) (Hcd : c ≤ d) : a - d < b - c := add_lt_add_of_lt_of_le Hab (neg_le_neg Hcd) end structure decidable_linear_ordered_comm_group [class] (A : Type) extends ordered_comm_group A, decidable_linear_order A section variables [s : decidable_linear_ordered_comm_group A] variables {a b c d e : A} include s theorem eq_zero_of_neg_eq (H : -a = a) : a = 0 := lt.by_cases (assume H1 : a < 0, have H2: a > 0, from H ▸ neg_pos_of_neg H1, absurd H1 (lt.asymm H2)) (assume H1 : a = 0, H1) (assume H1 : a > 0, have H2: a < 0, from H ▸ neg_neg_of_pos H1, absurd H1 (lt.asymm H2)) definition abs (a : A) : A := if 0 ≤ a then a else -a notation `|` a `|` := abs a theorem abs_of_nonneg (H : a ≥ 0) : |a| = a := if_pos H theorem abs_of_pos (H : a > 0) : |a| = a := if_pos (le_of_lt H) theorem abs_of_neg (H : a < 0) : |a| = -a := if_neg (not_le_of_lt H) theorem abs_zero : |0| = 0 := abs_of_nonneg (le.refl _) theorem abs_of_nonpos (H : a ≤ 0) : |a| = -a := decidable.by_cases (assume H1 : a = 0, calc |a| = |0| : H1 ... = 0 : abs_zero ... = -0 : neg_zero ... = -a : H1) (assume H1 : a ≠ 0, have H2 : a < 0, from lt_of_le_of_ne H H1, abs_of_neg H2) theorem abs_neg (a : A) : |-a| = |a| := or.elim (le.total 0 a) (assume H1 : 0 ≤ a, calc |-a| = -(-a) : abs_of_nonpos (neg_nonpos_of_nonneg H1) ... = a : neg_neg ... = |a| : abs_of_nonneg H1) (assume H1 : a ≤ 0, calc |-a| = -a : abs_of_nonneg (neg_nonneg_of_nonpos H1) ... = |a| : abs_of_nonpos H1) theorem abs_nonneg (a : A) : | a | ≥ 0 := or.elim (le.total 0 a) (assume H : 0 ≤ a, calc 0 ≤ a : H ... = |a| : abs_of_nonneg H) (assume H : a ≤ 0, calc 0 ≤ -a : neg_nonneg_of_nonpos H ... = |a| : abs_of_nonpos H) theorem abs_abs (a : A) : | |a| | = |a| := abs_of_nonneg !abs_nonneg theorem le_abs_self (a : A) : a ≤ |a| := or.elim (le.total 0 a) (assume H : 0 ≤ a, abs_of_nonneg H ▸ !le.refl) (assume H : a ≤ 0, le.trans H !abs_nonneg) theorem neg_le_abs_self (a : A) : -a ≤ |a| := !abs_neg ▸ !le_abs_self theorem eq_zero_of_abs_eq_zero (H : |a| = 0) : a = 0 := have H1 : a ≤ 0, from H ▸ le_abs_self a, have H2 : -a ≤ 0, from H ▸ abs_neg a ▸ le_abs_self (-a), le.antisymm H1 (nonneg_of_neg_nonpos H2) theorem abs_eq_zero_iff_eq_zero (a : A) : |a| = 0 ↔ a = 0 := iff.intro eq_zero_of_abs_eq_zero (assume H, congr_arg abs H ⬝ !abs_zero) theorem abs_pos_of_pos (H : a > 0) : |a| > 0 := (abs_of_pos H)⁻¹ ▸ H theorem abs_pos_of_neg (H : a < 0) : |a| > 0 := !abs_neg ▸ abs_pos_of_pos (neg_pos_of_neg H) theorem abs_pos_of_ne_zero (H : a ≠ 0) : |a| > 0 := or.elim (lt_or_gt_of_ne H) abs_pos_of_neg abs_pos_of_pos theorem abs_sub (a b : A) : |a - b| = |b - a| := calc |a - b| = |-(b - a)| : neg_sub ... = |b - a| : abs_neg theorem abs.by_cases {P : A → Prop} {a : A} (H1 : P a) (H2 : P (-a)) : P |a| := or.elim (le.total 0 a) (assume H : 0 ≤ a, (abs_of_nonneg H)⁻¹ ▸ H1) (assume H : a ≤ 0, (abs_of_nonpos H)⁻¹ ▸ H2) theorem abs_le_of_le_of_neg_le (H1 : a ≤ b) (H2 : -a ≤ b) : |a| ≤ b := abs.by_cases H1 H2 theorem abs_lt_of_lt_of_neg_lt (H1 : a < b) (H2 : -a < b) : |a| < b := abs.by_cases H1 H2 -- the triangle inequality theorem abs_add_le_abs_add_abs (a b : A) : |a + b| ≤ |a| + |b| := have aux1 : ∀{a b}, a + b ≥ 0 → a ≥ 0 → |a + b| ≤ |a| + |b|, proof take a b, assume H1 : a + b ≥ 0, assume H2 : a ≥ 0, decidable.by_cases (assume H3 : b ≥ 0, calc |a + b| ≤ |a + b| : le.refl ... = a + b : abs_of_nonneg H1 ... = |a| + b : abs_of_nonneg H2 ... = |a| + |b| : abs_of_nonneg H3) (assume H3 : ¬ b ≥ 0, have H4 : b ≤ 0, from le_of_lt (lt_of_not_le H3), calc |a + b| = a + b : abs_of_nonneg H1 ... = |a| + b : abs_of_nonneg H2 ... ≤ |a| + 0 : add_le_add_left H4 ... ≤ |a| + -b : add_le_add_left (neg_nonneg_of_nonpos H4) ... = |a| + |b| : abs_of_nonpos H4) qed, have aux2 : ∀{a b}, a + b ≥ 0 → |a + b| ≤ |a| + |b|, proof take a b, assume H1 : a + b ≥ 0, or.elim (le.total b 0) (assume H2 : b ≤ 0, have H3 : ¬ a < 0, proof assume H4 : a < 0, have H5 : a + b < 0, from !add_zero ▸ add_lt_add_of_lt_of_le H4 H2, not_lt_of_le H1 H5 qed, aux1 H1 (le_of_not_lt H3)) (assume H2 : 0 ≤ b, have H3 : |b + a| ≤ |b| + |a|, from aux1 (!add.comm ▸ H1) H2, !add.comm ▸ !add.comm ▸ H3) qed, show |a + b| ≤ |a| + |b|, proof or.elim (le.total 0 (a + b)) (assume H2 : 0 ≤ a + b, aux2 H2) (assume H2 : a + b ≤ 0, have H3 : -a + -b = -(a + b), from !neg_add_distrib⁻¹, have H4 : -(a + b) ≥ 0, from iff.mp' !neg_nonneg_iff_nonpos H2, calc |a + b| = |-(a + b)| : abs_neg ... = |-a + -b| : neg_add_distrib ... ≤ |-a| + |-b| : aux2 (H3⁻¹ ▸ H4) ... = |a| + |-b| : abs_neg ... = |a| + |b| : abs_neg) qed theorem abs_sub_abs_le_abs_sub (a b : A) : |a| - |b| ≤ |a - b| := have H1 : |a| - |b| + |b| ≤ |a - b| + |b|, from calc |a| - |b| + |b| = |a| : sub_add_cancel ... = |a - b + b| : sub_add_cancel ... ≤ |a - b| + |b| : algebra.abs_add_le_abs_add_abs, algebra.le_of_add_le_add_right H1 end end algebra
c5b7f2a1a236dd5d0da9f89c9731ee1fbbebf64e
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/stage0/src/Init/Lean/Level.lean
56b6d1c7df9aa8b1f49d7de4e46f50c14d4cd95c
[ "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
16,352
lean
/- Copyright (c) 2018 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Data.Option.Basic import Init.Data.HashMap import Init.Data.PersistentHashMap import Init.Data.HashSet import Init.Data.PersistentHashSet import Init.Lean.Data.Name import Init.Lean.Data.Format def Nat.imax (n m : Nat) : Nat := if m = 0 then 0 else Nat.max n m namespace Lean /-- Cached hash code, cached results, and other data for `Level`. hash : 32-bits hasMVar : 1-bit hasParam : 1-bit depth : 24-bits -/ def Level.Data := UInt64 instance Level.Data.inhabited : Inhabited Level.Data := inferInstanceAs (Inhabited UInt64) def Level.Data.hash (c : Level.Data) : USize := c.toUInt32.toUSize instance Level.Data.hasBeq : HasBeq Level.Data := ⟨fun (a b : UInt64) => a == b⟩ def Level.Data.depth (c : Level.Data) : UInt32 := (c.shiftRight 40).toUInt32 def Level.Data.hasMVar (c : Level.Data) : Bool := ((c.shiftRight 32).land 1) == 1 def Level.Data.hasParam (c : Level.Data) : Bool := ((c.shiftRight 33).land 1) == 1 def Level.mkData (h : USize) (depth : Nat) (hasMVar hasParam : Bool) : Level.Data := if depth > Nat.pow 2 24 - 1 then panic! "universe level depth is too big" else let r : UInt64 := h.toUInt32.toUInt64 + hasMVar.toUInt64.shiftLeft 32 + hasParam.toUInt64.shiftLeft 33 + depth.toUInt64.shiftLeft 40; r open Level inductive Level | zero : Data → Level | succ : Level → Data → Level | max : Level → Level → Data → Level | imax : Level → Level → Data → Level | param : Name → Data → Level | mvar : Name → Data → Level namespace Level @[inline] def data : Level → Data | zero d => d | mvar _ d => d | param _ d => d | succ _ d => d | max _ _ d => d | imax _ _ d => d def hash (u : Level) : USize := u.data.hash instance : Hashable Level := ⟨hash⟩ def depth (u : Level) : Nat := u.data.depth.toNat def hasMVar (u : Level) : Bool := u.data.hasMVar def hasParam (u : Level) : Bool := u.data.hasParam @[export lean_level_hash] def hashEx : Level → USize := hash @[export lean_level_has_mvar] def hasMVarEx : Level → Bool := hasMVar @[export lean_level_has_param] def hasParamEx : Level → Bool := hasParam @[export lean_level_depth] def depthEx (u : Level) : UInt32 := u.data.depth end Level def levelZero := Level.zero $ mkData 2221 0 false false def mkLevelMVar (mvarId : Name) := Level.mvar mvarId $ mkData (mixHash 2237 $ hash mvarId) 0 true false def mkLevelParam (name : Name) := Level.param name $ mkData (mixHash 2239 $ hash name) 0 false true def mkLevelSucc (u : Level) := Level.succ u $ mkData (mixHash 2243 $ hash u) (u.depth + 1) u.hasMVar u.hasParam def mkLevelMax (u v : Level) := Level.max u v $ mkData (mixHash 2251 $ mixHash (hash u) (hash v)) (Nat.max u.depth v.depth + 1) (u.hasMVar || v.hasMVar) (u.hasParam || v.hasParam) def mkLevelIMax (u v : Level) := Level.imax u v $ mkData (mixHash 2267 $ mixHash (hash u) (hash v)) (Nat.max u.depth v.depth + 1) (u.hasMVar || v.hasMVar) (u.hasParam || v.hasParam) def levelOne := mkLevelSucc levelZero @[export lean_level_mk_zero] def mkLevelZeroEx : Unit → Level := fun _ => levelZero @[export lean_level_mk_succ] def mkLevelSuccEx : Level → Level := mkLevelSucc @[export lean_level_mk_mvar] def mkLevelMVarEx : Name → Level := mkLevelMVar @[export lean_level_mk_param] def mkLevelParamEx : Name → Level := mkLevelParam @[export lean_level_mk_max] def mkLevelMaxEx : Level → Level → Level := mkLevelMax @[export lean_level_mk_imax] def mkLevelIMaxEx : Level → Level → Level := mkLevelIMax namespace Level instance : Inhabited Level := ⟨levelZero⟩ def isZero : Level → Bool | zero _ => true | _ => false def isSucc : Level → Bool | succ _ _ => true | _ => false def isMax : Level → Bool | max _ _ _ => true | _ => false def isIMax : Level → Bool | imax _ _ _ => true | _ => false def isMaxIMax : Level → Bool | max _ _ _ => true | imax _ _ _ => true | _ => false def isParam : Level → Bool | param _ _ => true | _ => false def isMVar : Level → Bool | mvar _ _ => true | _ => false def mvarId! : Level → Name | mvar mvarId _ => mvarId | _ => panic! "metavariable expected" /-- If result is true, then forall assignments `A` which assigns all parameters and metavariables occuring in `l`, `l[A] != zero` -/ def isNeverZero : Level → Bool | zero _ => false | param _ _ => false | mvar _ _ => false | succ _ _ => true | max l₁ l₂ _ => isNeverZero l₁ || isNeverZero l₂ | imax l₁ l₂ _ => isNeverZero l₂ def ofNat : Nat → Level | 0 => levelZero | n+1 => mkLevelSucc (ofNat n) def addOffsetAux : Nat → Level → Level | 0, u => u | (n+1), u => addOffsetAux n (mkLevelSucc u) def addOffset (u : Level) (n : Nat) : Level := u.addOffsetAux n def isExplicit : Level → Bool | zero _ => true | succ u _ => !u.hasMVar && !u.hasParam && isExplicit u | _ => false def getOffsetAux : Level → Nat → Nat | succ u _, r => getOffsetAux u (r+1) | _, r => r def getOffset (lvl : Level) : Nat := getOffsetAux lvl 0 def getLevelOffset : Level → Level | succ u _ => getLevelOffset u | u => u def toNat (lvl : Level) : Option Nat := match lvl.getLevelOffset with | zero _ => lvl.getOffset | _ => none @[extern "lean_level_eq"] protected constant beq (a : @& Level) (b : @& Level) : Bool := arbitrary _ instance : HasBeq Level := ⟨Level.beq⟩ /-- `occurs u l` return `true` iff `u` occurs in `l`. -/ def occurs : Level → Level → Bool | u, v@(succ v₁ _) => u == v || occurs u v₁ | u, v@(max v₁ v₂ _) => u == v || occurs u v₁ || occurs u v₂ | u, v@(imax v₁ v₂ _) => u == v || occurs u v₁ || occurs u v₂ | u, v => u == v def ctorToNat : Level → Nat | zero _ => 0 | param _ _ => 1 | mvar _ _ => 2 | succ _ _ => 3 | max _ _ _ => 4 | imax _ _ _ => 5 /- TODO: use well founded recursion. -/ partial def normLtAux : Level → Nat → Level → Nat → Bool | succ l₁ _, k₁, l₂, k₂ => normLtAux l₁ (k₁+1) l₂ k₂ | l₁, k₁, succ l₂ _, k₂ => normLtAux l₁ k₁ l₂ (k₂+1) | l₁@(max l₁₁ l₁₂ _), k₁, l₂@(max l₂₁ l₂₂ _), k₂ => if l₁ == l₂ then k₁ < k₂ else if l₁₁ == l₂₁ then normLtAux l₁₁ 0 l₂₁ 0 else normLtAux l₁₂ 0 l₂₂ 0 | l₁@(imax l₁₁ l₁₂ _), k₁, l₂@(imax l₂₁ l₂₂ _), k₂ => if l₁ == l₂ then k₁ < k₂ else if l₁₁ == l₂₁ then normLtAux l₁₁ 0 l₂₁ 0 else normLtAux l₁₂ 0 l₂₂ 0 | param n₁ _, k₁, param n₂ _, k₂ => if n₁ == n₂ then k₁ < k₂ else Name.lt n₁ n₂ -- use Name.lt because it is lexicographical | mvar n₁ _, k₁, mvar n₂ _, k₂ => if n₁ == n₂ then k₁ < k₂ else Name.quickLt n₁ n₂ -- metavariables are temporary, the actual order doesn't matter | l₁, k₁, l₂, k₂ => if l₁ == l₂ then k₁ < k₂ else ctorToNat l₁ < ctorToNat l₂ /-- A total order on level expressions that has the following properties - `succ l` is an immediate successor of `l`. - `zero` is the minimal element. This total order is used in the normalization procedure. -/ def normLt (l₁ l₂ : Level) : Bool := normLtAux l₁ 0 l₂ 0 private def isAlreadyNormalizedCheap : Level → Bool | zero _ => true | param _ _ => true | mvar _ _ => true | succ u _ => isAlreadyNormalizedCheap u | _ => false /- Auxiliary function used at `normalize` -/ private def mkIMaxAux : Level → Level → Level | _, u@(zero _) => u | zero _, u => u | u₁, u₂ => if u₁ == u₂ then u₁ else mkLevelIMax u₁ u₂ /- Auxiliary function used at `normalize` -/ @[specialize] private partial def getMaxArgsAux (normalize : Level → Level) : Level → Bool → Array Level → Array Level | max l₁ l₂ _, alreadyNormalized, lvls => getMaxArgsAux l₂ alreadyNormalized (getMaxArgsAux l₁ alreadyNormalized lvls) | l, false, lvls => getMaxArgsAux (normalize l) true lvls | l, true, lvls => lvls.push l private def accMax (result : Level) (prev : Level) (offset : Nat) : Level := if result.isZero then prev.addOffset offset else mkLevelMax result (prev.addOffset offset) /- Auxiliary function used at `normalize`. Remarks: - `lvls` are sorted using `normLt` - `extraK` is the outter offset of the `max` term. We will push it inside. - `i` is the current array index - `prev + prevK` is the "previous" level that has not been added to `result` yet. - `result` is the accumulator -/ private partial def mkMaxAux (lvls : Array Level) (extraK : Nat) : Nat → Level → Nat → Level → Level | i, prev, prevK, result => if h : i < lvls.size then let lvl := lvls.get ⟨i, h⟩; let curr := lvl.getLevelOffset; let currK := lvl.getOffset; if curr == prev then mkMaxAux (i+1) curr currK result else mkMaxAux (i+1) curr currK (accMax result prev (extraK + prevK)) else accMax result prev (extraK + prevK) /- Auxiliary function for `normalize`. It assumes `lvls` has been sorted using `normLt`. It finds the first position that is not an explicit universe. -/ private partial def skipExplicit (lvls : Array Level) : Nat → Nat | i => if h : i < lvls.size then let lvl := lvls.get ⟨i, h⟩; if lvl.getLevelOffset.isZero then skipExplicit (i+1) else i else i /- Auxiliary function for `normalize`. `maxExplicit` is the maximum explicit universe level at `lvls`. Return true if it finds a level with offset ≥ maxExplicit. `i` starts at the first non explict level. It assumes `lvls` has been sorted using `normLt`. -/ private partial def isExplicitSubsumedAux (lvls : Array Level) (maxExplicit : Nat) : Nat → Bool | i => if h : i < lvls.size then let lvl := lvls.get ⟨i, h⟩; if lvl.getOffset ≥ maxExplicit then true else isExplicitSubsumedAux (i+1) else false /- Auxiliary function for `normalize`. See `isExplicitSubsumedAux` -/ private def isExplicitSubsumed (lvls : Array Level) (firstNonExplicit : Nat) : Bool := if firstNonExplicit == 0 then false else let max := (lvls.get! (firstNonExplicit - 1)).getOffset; isExplicitSubsumedAux lvls max firstNonExplicit partial def normalize : Level → Level | l => if isAlreadyNormalizedCheap l then l else let k := l.getOffset; let u := l.getLevelOffset; match u with | max l₁ l₂ _ => let lvls := getMaxArgsAux normalize l₁ false #[]; let lvls := getMaxArgsAux normalize l₂ false lvls; let lvls := lvls.qsort normLt; let firstNonExplicit := skipExplicit lvls 0; let i := if isExplicitSubsumed lvls firstNonExplicit then firstNonExplicit else firstNonExplicit - 1; let lvl₁ := lvls.get! i; let prev := lvl₁.getLevelOffset; let prevK := lvl₁.getOffset; mkMaxAux lvls k (i+1) prev prevK levelZero | imax l₁ l₂ _ => if l₂.isNeverZero then addOffset (normalize (mkLevelMax l₁ l₂)) k else let l₁ := normalize l₁; let l₂ := normalize l₂; addOffset (mkIMaxAux l₁ l₂) k | _ => unreachable! /- Return true if `u` and `v` denote the same level. Check is currently incomplete. -/ def isEquiv (u v : Level) : Bool := u == v || u.normalize == v.normalize /-- Reduce (if possible) universe level by 1 -/ def dec : Level → Option Level | zero _ => none | param _ _ => none | mvar _ _ => none | succ l _ => l | max l₁ l₂ _ => mkLevelMax <$> dec l₁ <*> dec l₂ /- Remark: `mkLevelMax` in the following line is not a typo. If `dec l₂` succeeds, then `imax l₁ l₂` is equivalent to `max l₁ l₂`. -/ | imax l₁ l₂ _ => mkLevelMax <$> dec l₁ <*> dec l₂ /- Level to Format -/ namespace LevelToFormat inductive Result | leaf : Format → Result | num : Nat → Result | offset : Result → Nat → Result | maxNode : List Result → Result | imaxNode : List Result → Result def Result.succ : Result → Result | Result.offset f k => Result.offset f (k+1) | Result.num k => Result.num (k+1) | f => Result.offset f 1 def Result.max : Result → Result → Result | f, Result.maxNode Fs => Result.maxNode (f::Fs) | f₁, f₂ => Result.maxNode [f₁, f₂] def Result.imax : Result → Result → Result | f, Result.imaxNode Fs => Result.imaxNode (f::Fs) | f₁, f₂ => Result.imaxNode [f₁, f₂] def parenIfFalse : Format → Bool → Format | f, true => f | f, false => f.paren @[specialize] private def formatLst (fmt : Result → Format) : List Result → Format | [] => Format.nil | r::rs => Format.line ++ fmt r ++ formatLst rs partial def Result.format : Result → Bool → Format | Result.leaf f, _ => f | Result.num k, _ => toString k | Result.offset f 0, r => Result.format f r | Result.offset f (k+1), r => let f' := Result.format f false; parenIfFalse (f' ++ "+" ++ fmt (k+1)) r | Result.maxNode fs, r => parenIfFalse (Format.group $ "max" ++ formatLst (fun r => Result.format r false) fs) r | Result.imaxNode fs, r => parenIfFalse (Format.group $ "imax" ++ formatLst (fun r => Result.format r false) fs) r def toResult : Level → Result | zero _ => Result.num 0 | succ l _ => Result.succ (toResult l) | max l₁ l₂ _ => Result.max (toResult l₁) (toResult l₂) | imax l₁ l₂ _ => Result.imax (toResult l₁) (toResult l₂) | param n _ => Result.leaf (fmt n) | mvar n _ => Result.leaf (fmt n) end LevelToFormat protected def format (l : Level) : Format := (LevelToFormat.toResult l).format true instance : HasFormat Level := ⟨Level.format⟩ instance : HasToString Level := ⟨Format.pretty ∘ Level.format⟩ /- The update functions here are defined using C code. They will try to avoid allocating new values using pointer equality. The hypotheses `(h : e.is... = true)` are used to ensure Lean will not crash at runtime. The `update*!` functions are inlined and provide a convenient way of using the update proofs without providing proofs. Note that if they are used under a match-expression, the compiler will eliminate the double-match. -/ @[extern "lean_level_update_succ"] def updateSucc (lvl : Level) (newLvl : Level) (h : lvl.isSucc = true) : Level := mkLevelSucc newLvl @[inline] def updateSucc! (lvl : Level) (newLvl : Level) : Level := match lvl with | succ lvl d => updateSucc (succ lvl d) newLvl rfl | _ => panic! "succ level expected" @[extern "lean_level_update_max"] def updateMax (lvl : Level) (newLhs : Level) (newRhs : Level) (h : lvl.isMax = true) : Level := mkLevelMax newLhs newRhs @[inline] def updateMax! (lvl : Level) (newLhs : Level) (newRhs : Level) : Level := match lvl with | max lhs rhs d => updateMax (max lhs rhs d) newLhs newRhs rfl | _ => panic! "max level expected" @[extern "lean_level_update_imax"] def updateIMax (lvl : Level) (newLhs : Level) (newRhs : Level) (h : lvl.isIMax = true) : Level := mkLevelIMax newLhs newRhs @[inline] def updateIMax! (lvl : Level) (newLhs : Level) (newRhs : Level) : Level := match lvl with | imax lhs rhs d => updateIMax (imax lhs rhs d) newLhs newRhs rfl | _ => panic! "imax level expected" @[specialize] def instantiateParams (s : Name → Option Level) : Level → Level | u@(zero _) => u | u@(succ v _) => if u.hasParam then u.updateSucc (instantiateParams v) rfl else u | u@(max v₁ v₂ _) => if u.hasParam then u.updateMax (instantiateParams v₁) (instantiateParams v₂) rfl else u | u@(imax v₁ v₂ _) => if u.hasParam then u.updateIMax (instantiateParams v₁) (instantiateParams v₂) rfl else u | u@(param n _) => match s n with | some u' => u' | none => u | u => u end Level abbrev LevelMap (α : Type) := HashMap Level α abbrev PersistentLevelMap (α : Type) := PHashMap Level α abbrev LevelSet := HashSet Level abbrev PersistentLevelSet := PHashSet Level abbrev PLevelSet := PersistentLevelSet end Lean abbrev Nat.toLevel (n : Nat) : Lean.Level := Lean.Level.ofNat n
1f26a82cc671e4eafd52df5b1d226d0c89397b58
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/src/Lean/Elab/Tactic/Match.lean
58f19fd82bae7557ea08b43579c9ab10e335c065
[ "Apache-2.0" ]
permissive
williamdemeo/lean4
72161c58fe65c3ad955d6a3050bb7d37c04c0d54
6d00fcf1d6d873e195f9220c668ef9c58e9c4a35
refs/heads/master
1,678,305,356,877
1,614,708,995,000
1,614,708,995,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,771
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Parser.Term import Lean.Elab.Match import Lean.Elab.Tactic.Basic import Lean.Elab.Tactic.Induction namespace Lean.Elab.Tactic open Meta /- Erase auxiliary `_discr` variables introduced by `match`-expression elaborator -/ @[builtinTactic Lean.Parser.Tactic.eraseAuxDiscrs] def evalEraseAuxDiscrs : Tactic := fun _ => do let (g, gs) ← getMainGoal withMVarContext g do let lctx ← getLCtx let auxDecls := lctx.foldl (init := []) fun auxDecls localDecl => if Term.isAuxDiscrName localDecl.userName then localDecl.fvarId :: auxDecls else auxDecls let mut g := g for auxDecl in auxDecls do g ← tryClear g auxDecl setGoals (g :: gs) structure AuxMatchTermState where nextIdx : Nat := 1 «cases» : Array Syntax := #[] private def mkAuxiliaryMatchTermAux (parentTag : Name) (matchTac : Syntax) : StateT AuxMatchTermState MacroM Syntax := do let matchAlts := matchTac[4] let alts := matchAlts[0].getArgs let newAlts ← alts.mapM fun alt => do let alt := alt.setKind ``Parser.Term.matchAlt let holeOrTacticSeq := alt[3] if holeOrTacticSeq.isOfKind ``Parser.Term.syntheticHole then pure alt else if holeOrTacticSeq.isOfKind ``Parser.Term.hole then let s ← get let tag := if alts.size > 1 then parentTag ++ (`match).appendIndexAfter s.nextIdx else parentTag let holeName := mkIdentFrom holeOrTacticSeq tag let newHole ← `(?$holeName:ident) modify fun s => { s with nextIdx := s.nextIdx + 1} pure <| alt.setArg 3 newHole else withFreshMacroScope do let newHole ← `(?rhs) let newHoleId := newHole[1] let newCase ← `(tactic| case $newHoleId => eraseAuxDiscrs!; ($holeOrTacticSeq:tacticSeq) ) modify fun s => { s with cases := s.cases.push newCase } pure <| alt.setArg 3 newHole let result := matchTac.setKind ``Parser.Term.«match» let result := result.setArg 4 (mkNode ``Parser.Term.matchAlts #[mkNullNode newAlts]) pure result private def mkAuxiliaryMatchTerm (parentTag : Name) (matchTac : Syntax) : MacroM (Syntax × Array Syntax) := do let (matchTerm, s) ← mkAuxiliaryMatchTermAux parentTag matchTac |>.run {} pure (matchTerm, s.cases) @[builtinTactic Lean.Parser.Tactic.match] def evalMatch : Tactic := fun stx => do let tag ← getMainTag let (matchTerm, cases) ← liftMacroM <| mkAuxiliaryMatchTerm tag stx let refineMatchTerm ← `(tactic| refine $matchTerm) let stxNew := mkNullNode (#[refineMatchTerm] ++ cases) withMacroExpansion stx stxNew <| evalTactic stxNew end Lean.Elab.Tactic
a6b2e112340211eb268cfe6089d2514c19446405
1437b3495ef9020d5413178aa33c0a625f15f15f
/data/real/cau_seq_filter.lean
1d429f2a124b69733547164666e5be65fad438b2
[ "Apache-2.0" ]
permissive
jean002/mathlib
c66bbb2d9fdc9c03ae07f869acac7ddbfce67a30
dc6c38a765799c99c4d9c8d5207d9e6c9e0e2cfd
refs/heads/master
1,587,027,806,375
1,547,306,358,000
1,547,306,358,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
11,288
lean
/- Copyright (c) 2018 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis, Sébastien Gouëzel Characterize completeness of metric spaces in terms of Cauchy sequences. In particular, reconcile the filter notion of Cauchy-ness with the cau_seq notion on normed spaces. -/ import analysis.topology.uniform_space analysis.normed_space data.real.cau_seq analysis.limits import tactic.linarith universes u v open set filter classical variable {β : Type v} /-We show that a metric space in which all Cauchy sequences converge is complete, i.e., all Cauchy filters converge. For this, we approximate any Cauchy filter by a Cauchy sequence, taking advantage of the fact that there is a sequence tending to `0` in ℝ.-/ namespace sequentially_complete section variables [metric_space β] {f : filter β} (hf : cauchy f) private lemma one_div_succ (n : ℕ) : 1 / ((n : ℝ) + 1) > 0 := one_div_pos_of_pos (by linarith using [show (↑n : ℝ) ≥ 0, from nat.cast_nonneg _]) def set_seq_of_cau_filter : ℕ → set β | 0 := some ((cauchy_of_metric.1 hf).2 _ (one_div_succ 0)) | (n+1) := (set_seq_of_cau_filter n) ∩ some ((cauchy_of_metric.1 hf).2 _ (one_div_succ (n + 1))) lemma set_seq_of_cau_filter_mem_sets : ∀ n, set_seq_of_cau_filter hf n ∈ f.sets | 0 := some (some_spec ((cauchy_of_metric.1 hf).2 _ (one_div_succ 0))) | (n+1) := inter_mem_sets (set_seq_of_cau_filter_mem_sets n) (some (some_spec ((cauchy_of_metric.1 hf).2 _ (one_div_succ (n + 1))))) lemma set_seq_of_cau_filter_inhabited (n : ℕ) : ∃ x, x ∈ set_seq_of_cau_filter hf n := inhabited_of_mem_sets (cauchy_of_metric.1 hf).1 (set_seq_of_cau_filter_mem_sets hf n) lemma set_seq_of_cau_filter_spec : ∀ n, ∀ {x y}, x ∈ set_seq_of_cau_filter hf n → y ∈ set_seq_of_cau_filter hf n → dist x y < 1/((n : ℝ) + 1) | 0 := some_spec (some_spec ((cauchy_of_metric.1 hf).2 _ (one_div_succ 0))) | (n+1) := λ x y hx hy, some_spec (some_spec ((cauchy_of_metric.1 hf).2 _ (one_div_succ (n+1)))) x y (mem_of_mem_inter_right hx) (mem_of_mem_inter_right hy) -- this must exist somewhere, no? private lemma mono_of_mono_succ_aux {α} [partial_order α] (f : ℕ → α) (h : ∀ n, f (n+1) ≤ f n) (m : ℕ) : ∀ n, f (m + n) ≤ f m | 0 := le_refl _ | (k+1) := le_trans (h _) (mono_of_mono_succ_aux _) lemma mono_of_mono_succ {α} [partial_order α] (f : ℕ → α) (h : ∀ n, f (n+1) ≤ f n) {m n : ℕ} (hmn : m ≤ n) : f n ≤ f m := let ⟨k, hk⟩ := nat.exists_eq_add_of_le hmn in by simpa [hk] using mono_of_mono_succ_aux f h m k lemma set_seq_of_cau_filter_monotone' (n : ℕ) : set_seq_of_cau_filter hf (n+1) ⊆ set_seq_of_cau_filter hf n := inter_subset_left _ _ lemma set_seq_of_cau_filter_monotone {n k : ℕ} (hle : n ≤ k) : set_seq_of_cau_filter hf k ⊆ set_seq_of_cau_filter hf n := mono_of_mono_succ (set_seq_of_cau_filter hf) (set_seq_of_cau_filter_monotone' hf) hle /--The approximating Cauchy sequence for the Cauchy filter `f`-/ noncomputable def seq_of_cau_filter (n : ℕ) : β := some (set_seq_of_cau_filter_inhabited hf n) lemma seq_of_cau_filter_mem_set_seq (n : ℕ) : seq_of_cau_filter hf n ∈ set_seq_of_cau_filter hf n := some_spec (set_seq_of_cau_filter_inhabited hf n) lemma seq_of_cau_filter_is_cauchy' {n k : ℕ} (hle : n ≤ k) : dist (seq_of_cau_filter hf n) (seq_of_cau_filter hf k) < 1 / ((n : ℝ) + 1) := set_seq_of_cau_filter_spec hf _ (seq_of_cau_filter_mem_set_seq hf n) (set_seq_of_cau_filter_monotone hf hle (seq_of_cau_filter_mem_set_seq hf k)) lemma cauchy_seq_of_dist_tendsto_0 {s : ℕ → β} {b : ℕ → ℝ} (h : ∀ {n k : ℕ}, n ≤ k → dist (s n) (s k) < b n) (hb : tendsto b at_top (nhds 0)) : cauchy_seq s := begin rw cauchy_seq_metric', assume ε hε, have hb : ∀ (i : set ℝ), (0:ℝ) ∈ i → is_open i → (∃ (a : ℕ), ∀ (c : ℕ), c ≥ a → b c ∈ i), { simpa [tendsto, nhds] using hb }, cases hb (ball 0 ε) (mem_ball_self hε) (is_open_ball) with N hN, existsi N, intros k hk, rw [dist_comm], apply lt.trans, apply h hk, have := hN _ (le_refl _), have bnn : ∀ n, b n ≥ 0, from λ n, le_of_lt (lt_of_le_of_lt dist_nonneg (h (le_refl n))), simpa [real.norm_eq_abs, abs_of_nonneg (bnn _)] using this end lemma tendsto_div : tendsto (λ n : ℕ, 1 / ((n : ℝ) + 1)) at_top (nhds 0) := suffices tendsto (λ n : ℕ, 1 / (↑(n + 1) : ℝ)) at_top (nhds 0), by simpa, tendsto_comp_succ_at_top_iff.2 tendsto_one_div_at_top_nhds_0_nat /--The approximating sequence is indeed Cauchy-/ lemma seq_of_cau_filter_is_cauchy : cauchy_seq (seq_of_cau_filter hf) := cauchy_seq_of_dist_tendsto_0 (@seq_of_cau_filter_is_cauchy' _ _ _ hf) tendsto_div /-- If the approximating Cauchy sequence is converging, to a limit `y`, then the original Cauchy filter `f` is also converging, to the same limit. Given `t1` in the filter `f` and `t2` a neighborhood of `y`, it suffices to show that `t1 ∩ t2` is nonempty. Pick `ε` so that the ε-ball around `y` is contained in `t2`. Pick `n` with `1/n < ε/2`, and `n2` such that `dist(seq n2, y) < ε/2`. Let `N = max(n, n2)`. We defined `seq` by looking at a decreasing sequence of sets of `f` with shrinking radius. The Nth one has radius `< 1/N < ε/2`. This set is in `f`, so we can find an element `x` that's also in `t1`. `dist(x, seq N) < ε/2` since `seq N` is in this set, and `dist (seq N, y) < ε/2`, so `x` is in the ε-ball around `y`, and thus in `t2`. -/ lemma le_nhds_cau_filter_lim {y : β} (H : tendsto (seq_of_cau_filter hf) at_top (nhds y)) : f ≤ nhds y := begin apply (le_nhds_iff_adhp_of_cauchy hf).2, apply forall_sets_neq_empty_iff_neq_bot.1, intros s hs, simp at hs, rcases hs with ⟨t1, ht1, t2, ht2, ht1t2⟩, apply ne_empty_iff_exists_mem.2, rcases mem_nhds_iff_metric.1 ht2 with ⟨ε, hε, ht2'⟩, cases cauchy_of_metric.1 hf with hfb _, have : ε / 2 > 0, from div_pos hε (by norm_num), have : ∃ n : ℕ, 1 / (↑n + 1) < ε / 2, from exists_nat_one_div_lt this, cases this with n hnε, cases tendsto_at_top_metric.1 H _ this with n2 hn2, let N := max n n2, have hNε : 1 / (↑N+1) < ε / 2, { apply lt_of_le_of_lt _ hnε, apply one_div_le_one_div_of_le, { exact add_pos_of_nonneg_of_pos (nat.cast_nonneg _) zero_lt_one }, { apply add_le_add_right, simp [le_max_left] }}, have ht1sn : t1 ∩ set_seq_of_cau_filter hf N ∈ f.sets, from inter_mem_sets ht1 (set_seq_of_cau_filter_mem_sets hf _), have hts1n_ne : t1 ∩ set_seq_of_cau_filter hf N ≠ ∅, from forall_sets_neq_empty_iff_neq_bot.2 hfb _ ht1sn, cases exists_mem_of_ne_empty hts1n_ne with x hx, have hdist1 := set_seq_of_cau_filter_spec hf _ hx.2 (seq_of_cau_filter_mem_set_seq hf N), have hdist2 := hn2 N (le_max_right _ _), replace hdist1 := lt_trans hdist1 hNε, rw [dist_comm] at hdist2, have hdist : dist x y < ε, from calc dist x y ≤ dist x (seq_of_cau_filter hf N) + dist y (seq_of_cau_filter hf N) : dist_triangle_right _ _ _ ... < ε/2 + ε/2 : add_lt_add hdist1 hdist2 ... = ε : add_halves _, have hxt2 : x ∈ t2, from ht2' hdist, existsi x, apply ht1t2, exact mem_inter hx.left hxt2 end end end sequentially_complete /--A metric space in which every Cauchy sequence converges is complete-/ theorem complete_of_cauchy_seq_tendsto {α : Type u} [metric_space α] (H : ∀(u : ℕ → α), cauchy_seq u → ∃x, tendsto u at_top (nhds x)) : complete_space α := ⟨begin /-Consider a Cauchy filter `f`-/ intros f hf, /-Introduce a sequence `u` approximating the filter `f`-/ let u := sequentially_complete.seq_of_cau_filter hf, /-It is Cauchy-/ have : cauchy_seq u := sequentially_complete.seq_of_cau_filter_is_cauchy hf, /-Therefore, it converges by assumption. Let `x` be its limit-/ rcases H u this with ⟨x, hx⟩, /-The original filter also converges to `x`-/ exact ⟨x, sequentially_complete.le_nhds_cau_filter_lim hf hx⟩ end⟩ section /-Now, we will apply these results to `cau_seq`, i.e., "Cauchy sequences" defined by a multiplicative absolute value on normed fields-/ lemma tendsto_limit [normed_ring β] [hn : is_absolute_value (norm : β → ℝ)] (f : cau_seq β norm) [cau_seq.is_complete β norm] : tendsto f at_top (nhds f.lim) := tendsto_nhds begin intros s lfs os, suffices : ∃ (a : ℕ), ∀ (b : ℕ), b ≥ a → f b ∈ s, by simpa using this, rcases is_open_metric.1 os _ lfs with ⟨ε, ⟨hε, hεs⟩⟩, cases setoid.symm (cau_seq.equiv_lim f) _ hε with N hN, existsi N, intros b hb, apply hεs, dsimp [ball], rw [dist_comm, dist_eq_norm], solve_by_elim end variables [normed_field β] /- This section shows that if we have a uniform space generated by an absolute value, topological completeness and Cauchy sequence completeness coincide. The problem is that there isn't a good notion of "uniform space generated by an absolute value", so right now this is specific to norm. Furthermore, norm only instantiates is_absolute_value on normed_field. This needs to be fixed, since it prevents showing that ℤ_[hp] is complete -/ instance normed_field.is_absolute_value : is_absolute_value (norm : β → ℝ) := { abv_nonneg := norm_nonneg, abv_eq_zero := norm_eq_zero, abv_add := norm_triangle, abv_mul := normed_field.norm_mul } lemma cauchy_of_filter_cauchy (f : ℕ → β) (hf : cauchy_seq f) : is_cau_seq norm f := begin cases cauchy_iff.1 hf with hf1 hf2, intros ε hε, rcases hf2 {x | dist x.1 x.2 < ε} (dist_mem_uniformity hε) with ⟨t, ⟨ht, htsub⟩⟩, simp at ht, cases ht with N hN, existsi N, intros j hj, rw ←dist_eq_norm, apply @htsub (f j, f N), apply set.mk_mem_prod; solve_by_elim [le_refl] end lemma filter_cauchy_of_cauchy (f : cau_seq β norm) : cauchy_seq f := begin apply cauchy_iff.2, split, { exact map_ne_bot at_top_ne_bot }, { intros s hs, rcases mem_uniformity_dist.1 hs with ⟨ε, ⟨hε, hεs⟩⟩, cases cau_seq.cauchy₂ f hε with N hN, existsi {n | n ≥ N}.image f, simp, split, { existsi N, intros b hb, existsi b, simp [hb] }, { rintros ⟨a, b⟩ ⟨⟨a', ⟨ha'1, ha'2⟩⟩, ⟨b', ⟨hb'1, hb'2⟩⟩⟩, dsimp at ha'1 ha'2 hb'1 hb'2, rw [←ha'2, ←hb'2], apply hεs, rw dist_eq_norm, apply hN; assumption }}, { apply_instance } end /--In a normed field, `cau_seq` coincides with the usual notion of Cauchy sequences-/ lemma cau_seq_iff_cauchy_seq {α : Type u} [normed_field α] {u : ℕ → α} : is_cau_seq norm u ↔ cauchy_seq u := ⟨λh, filter_cauchy_of_cauchy ⟨u, h⟩, λh, cauchy_of_filter_cauchy u h⟩ /--A complete normed field is complete as a metric space, as Cauchy sequences converge by assumption and this suffices to characterize completeness.-/ instance complete_space_of_cau_seq_complete [cau_seq.is_complete β norm] : complete_space β := begin apply complete_of_cauchy_seq_tendsto, assume u hu, have C : is_cau_seq norm u := cau_seq_iff_cauchy_seq.2 hu, existsi cau_seq.lim ⟨u, C⟩, rw tendsto_at_top_metric, assume ε εpos, cases (cau_seq.equiv_lim ⟨u, C⟩) _ εpos with N hN, existsi N, simpa [dist_eq_norm] using hN end end
69332ec0d90b896853a08880026b58a6cc8344b7
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/tactic31.lean
4f50fe4c7f8ffc1adf4b4c5ba40446fefc235f2b
[ "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
485
lean
import data.nat open algebra example (a b c : Prop) : a → b → c → a ∧ b ∧ c := begin intro Ha, intro Hb, intro Hc, apply and.intro Ha, apply and.intro Hb Hc end example (a b c : Prop) : a → b → c → a ∧ b ∧ c := by intro Ha; intro Hb; intro Hc; apply and.intro Ha; apply and.intro Hb Hc open nat example (a b c : nat) : a = b → b = 0 + c → a = c + 0:= begin intro ab, intro bc, change a = c, rewrite zero_add at bc, rewrite -bc, exact ab end
34f83c54cc0f6047294dcb7a35da99b706139324
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/real/ennreal_auto.lean
619baa9c2de22d09dd509d2d2b89fbd697324952
[]
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
48,832
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Johannes Hölzl, Yury Kudryashov -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.real.nnreal import Mathlib.data.set.intervals.default import Mathlib.PostPort universes u_1 u_3 namespace Mathlib /-! # Extended non-negative reals We define `ennreal := with_no ℝ≥0` to be the type of extended nonnegative real numbers, i.e., the interval `[0, +∞]`. This type is used as the codomain of a `measure_theory.measure`, and of the extended distance `edist` in a `emetric_space`. In this file we define some algebraic operations and a linear order on `ennreal` and prove basic properties of these operations, order, and conversions to/from `ℝ`, `ℝ≥0`, and `ℕ`. ## Main definitions * `ennreal`: the extended nonnegative real numbers `[0, ∞]`; defined as `with_top ℝ≥0`; it is equipped with the following structures: - coercion from `ℝ≥0` defined in the natural way; - the natural structure of a complete dense linear order: `↑p ≤ ↑q ↔ p ≤ q` and `∀ a, a ≤ ∞`; - `a + b` is defined so that `↑p + ↑q = ↑(p + q)` for `(p q : ℝ≥0)` and `a + ∞ = ∞ + a = ∞`; - `a * b` is defined so that `↑p * ↑q = ↑(p * q)` for `(p q : ℝ≥0)`, `0 * ∞ = ∞ * 0 = 0`, and `a * ∞ = ∞ * a = ∞` for `a ≠ 0`; - `a - b` is defined as the minimal `d` such that `a ≤ d + b`; this way we have `↑p - ↑q = ↑(p - q)`, `∞ - ↑p = ∞`, `↑p - ∞ = ∞ - ∞ = 0`; note that there is no negation, only subtraction; - `a⁻¹` is defined as `Inf {b | 1 ≤ a * b}`. This way we have `(↑p)⁻¹ = ↑(p⁻¹)` for `p : ℝ≥0`, `p ≠ 0`, `0⁻¹ = ∞`, and `∞⁻¹ = 0`. - `a / b` is defined as `a * b⁻¹`. The addition and multiplication defined this way together with `0 = ↑0` and `1 = ↑1` turn `ennreal` into a canonically ordered commutative semiring of characteristic zero. * Coercions to/from other types: - coercion `ℝ≥0 → ennreal` is defined as `has_coe`, so one can use `(p : ℝ≥0)` in a context that expects `a : ennreal`, and Lean will apply `coe` automatically; - `ennreal.to_nnreal` sends `↑p` to `p` and `∞` to `0`; - `ennreal.to_real := coe ∘ ennreal.to_nnreal` sends `↑p`, `p : ℝ≥0` to `(↑p : ℝ)` and `∞` to `0`; - `ennreal.of_real := coe ∘ nnreal.of_real` sends `x : ℝ` to `↑⟨max x 0, _⟩` - `ennreal.ne_top_equiv_nnreal` is an equivalence between `{a : ennreal // a ≠ 0}` and `ℝ≥0`. ## Implementation notes We define a `can_lift ennreal ℝ≥0` instance, so one of the ways to prove theorems about an `ennreal` number `a` is to consider the cases `a = ∞` and `a ≠ ∞`, and use the tactic `lift a to ℝ≥0 using ha` in the second case. This instance is even more useful if one already has `ha : a ≠ ∞` in the context, or if we have `(f : α → ennreal) (hf : ∀ x, f x ≠ ∞)`. ## Notations * `ℝ≥0`: type of nonnegative real numbers `[0, ∞)`; defined in `data.real.nnreal`; * `∞`: a localized notation in `ennreal` for `⊤ : ennreal`. -/ /-- The extended nonnegative real numbers. This is usually denoted [0, ∞], and is relevant as the codomain of a measure. -/ def ennreal := with_top nnreal namespace ennreal protected instance inhabited : Inhabited ennreal := { default := 0 } protected instance has_coe : has_coe nnreal ennreal := has_coe.mk some protected instance nnreal.can_lift : can_lift ennreal nnreal := can_lift.mk coe (fun (r : ennreal) => r ≠ ⊤) sorry @[simp] theorem none_eq_top : none = ⊤ := rfl @[simp] theorem some_eq_coe (a : nnreal) : some a = ↑a := rfl /-- `to_nnreal x` returns `x` if it is real, otherwise 0. -/ protected def to_nnreal : ennreal → nnreal := sorry /-- `to_real x` returns `x` if it is real, `0` otherwise. -/ protected def to_real (a : ennreal) : ℝ := ↑(ennreal.to_nnreal a) /-- `of_real x` returns `x` if it is nonnegative, `0` otherwise. -/ protected def of_real (r : ℝ) : ennreal := ↑(nnreal.of_real r) @[simp] theorem to_nnreal_coe {r : nnreal} : ennreal.to_nnreal ↑r = r := rfl @[simp] theorem coe_to_nnreal {a : ennreal} : a ≠ ⊤ → ↑(ennreal.to_nnreal a) = a := fun (ᾰ : a ≠ ⊤) => option.cases_on a (fun (ᾰ : none ≠ ⊤) => idRhs (↑(ennreal.to_nnreal none) = none) (false.elim (ᾰ rfl))) (fun (a : nnreal) (ᾰ : some a ≠ ⊤) => idRhs (↑(ennreal.to_nnreal (some a)) = ↑(ennreal.to_nnreal (some a))) rfl) ᾰ @[simp] theorem of_real_to_real {a : ennreal} (h : a ≠ ⊤) : ennreal.of_real (ennreal.to_real a) = a := sorry @[simp] theorem to_real_of_real {r : ℝ} (h : 0 ≤ r) : ennreal.to_real (ennreal.of_real r) = r := sorry theorem to_real_of_real' {r : ℝ} : ennreal.to_real (ennreal.of_real r) = max r 0 := rfl theorem coe_to_nnreal_le_self {a : ennreal} : ↑(ennreal.to_nnreal a) ≤ a := sorry theorem coe_nnreal_eq (r : nnreal) : ↑r = ennreal.of_real ↑r := sorry theorem of_real_eq_coe_nnreal {x : ℝ} (h : 0 ≤ x) : ennreal.of_real x = ↑{ val := x, property := h } := sorry @[simp] theorem of_real_coe_nnreal {p : nnreal} : ennreal.of_real ↑p = ↑p := Eq.symm (coe_nnreal_eq p) @[simp] theorem coe_zero : ↑0 = 0 := rfl @[simp] theorem coe_one : ↑1 = 1 := rfl @[simp] theorem to_real_nonneg {a : ennreal} : 0 ≤ ennreal.to_real a := sorry @[simp] theorem top_to_nnreal : ennreal.to_nnreal ⊤ = 0 := rfl @[simp] theorem top_to_real : ennreal.to_real ⊤ = 0 := rfl @[simp] theorem one_to_real : ennreal.to_real 1 = 1 := rfl @[simp] theorem one_to_nnreal : ennreal.to_nnreal 1 = 1 := rfl @[simp] theorem coe_to_real (r : nnreal) : ennreal.to_real ↑r = ↑r := rfl @[simp] theorem zero_to_nnreal : ennreal.to_nnreal 0 = 0 := rfl @[simp] theorem zero_to_real : ennreal.to_real 0 = 0 := rfl @[simp] theorem of_real_zero : ennreal.of_real 0 = 0 := sorry @[simp] theorem of_real_one : ennreal.of_real 1 = 1 := sorry theorem of_real_to_real_le {a : ennreal} : ennreal.of_real (ennreal.to_real a) ≤ a := dite (a = ⊤) (fun (ha : a = ⊤) => Eq.symm ha ▸ le_top) fun (ha : ¬a = ⊤) => le_of_eq (of_real_to_real ha) theorem forall_ennreal {p : ennreal → Prop} : (∀ (a : ennreal), p a) ↔ (∀ (r : nnreal), p ↑r) ∧ p ⊤ := sorry theorem forall_ne_top {p : ennreal → Prop} : (∀ (a : ennreal), a ≠ ⊤ → p a) ↔ ∀ (r : nnreal), p ↑r := option.ball_ne_none theorem exists_ne_top {p : ennreal → Prop} : (∃ (a : ennreal), ∃ (H : a ≠ ⊤), p a) ↔ ∃ (r : nnreal), p ↑r := option.bex_ne_none theorem to_nnreal_eq_zero_iff (x : ennreal) : ennreal.to_nnreal x = 0 ↔ x = 0 ∨ x = ⊤ := sorry theorem to_real_eq_zero_iff (x : ennreal) : ennreal.to_real x = 0 ↔ x = 0 ∨ x = ⊤ := sorry @[simp] theorem coe_ne_top {r : nnreal} : ↑r ≠ ⊤ := with_top.coe_ne_top @[simp] theorem top_ne_coe {r : nnreal} : ⊤ ≠ ↑r := with_top.top_ne_coe @[simp] theorem of_real_ne_top {r : ℝ} : ennreal.of_real r ≠ ⊤ := sorry @[simp] theorem of_real_lt_top {r : ℝ} : ennreal.of_real r < ⊤ := iff.mpr lt_top_iff_ne_top of_real_ne_top @[simp] theorem top_ne_of_real {r : ℝ} : ⊤ ≠ ennreal.of_real r := sorry @[simp] theorem zero_ne_top : 0 ≠ ⊤ := coe_ne_top @[simp] theorem top_ne_zero : ⊤ ≠ 0 := top_ne_coe @[simp] theorem one_ne_top : 1 ≠ ⊤ := coe_ne_top @[simp] theorem top_ne_one : ⊤ ≠ 1 := top_ne_coe @[simp] theorem coe_eq_coe {r : nnreal} {q : nnreal} : ↑r = ↑q ↔ r = q := with_top.coe_eq_coe @[simp] theorem coe_le_coe {r : nnreal} {q : nnreal} : ↑r ≤ ↑q ↔ r ≤ q := with_top.coe_le_coe @[simp] theorem coe_lt_coe {r : nnreal} {q : nnreal} : ↑r < ↑q ↔ r < q := with_top.coe_lt_coe theorem coe_mono : monotone coe := fun (_x _x_1 : nnreal) => iff.mpr coe_le_coe @[simp] theorem coe_eq_zero {r : nnreal} : ↑r = 0 ↔ r = 0 := coe_eq_coe @[simp] theorem zero_eq_coe {r : nnreal} : 0 = ↑r ↔ 0 = r := coe_eq_coe @[simp] theorem coe_eq_one {r : nnreal} : ↑r = 1 ↔ r = 1 := coe_eq_coe @[simp] theorem one_eq_coe {r : nnreal} : 1 = ↑r ↔ 1 = r := coe_eq_coe @[simp] theorem coe_nonneg {r : nnreal} : 0 ≤ ↑r ↔ 0 ≤ r := coe_le_coe @[simp] theorem coe_pos {r : nnreal} : 0 < ↑r ↔ 0 < r := coe_lt_coe @[simp] theorem coe_add {r : nnreal} {p : nnreal} : ↑(r + p) = ↑r + ↑p := with_top.coe_add @[simp] theorem coe_mul {r : nnreal} {p : nnreal} : ↑(r * p) = ↑r * ↑p := with_top.coe_mul @[simp] theorem coe_bit0 {r : nnreal} : ↑(bit0 r) = bit0 ↑r := coe_add @[simp] theorem coe_bit1 {r : nnreal} : ↑(bit1 r) = bit1 ↑r := sorry theorem coe_two : ↑(bit0 1) = bit0 1 := sorry protected theorem zero_lt_one : 0 < 1 := canonically_ordered_semiring.zero_lt_one @[simp] theorem one_lt_two : 1 < bit0 1 := sorry @[simp] theorem zero_lt_two : 0 < bit0 1 := lt_trans ennreal.zero_lt_one one_lt_two theorem two_ne_zero : bit0 1 ≠ 0 := ne.symm (ne_of_lt zero_lt_two) theorem two_ne_top : bit0 1 ≠ ⊤ := coe_two ▸ coe_ne_top /-- The set of `ennreal` numbers that are not equal to `∞` is equivalent to `ℝ≥0`. -/ def ne_top_equiv_nnreal : ↥(set_of fun (a : ennreal) => a ≠ ⊤) ≃ nnreal := equiv.mk (fun (x : ↥(set_of fun (a : ennreal) => a ≠ ⊤)) => ennreal.to_nnreal ↑x) (fun (x : nnreal) => { val := ↑x, property := coe_ne_top }) sorry sorry theorem cinfi_ne_top {α : Type u_1} [has_Inf α] (f : ennreal → α) : (infi fun (x : Subtype fun (x : ennreal) => x ≠ ⊤) => f ↑x) = infi fun (x : nnreal) => f ↑x := Eq.symm (infi_congr (⇑(equiv.symm ne_top_equiv_nnreal)) (equiv.surjective (equiv.symm ne_top_equiv_nnreal)) fun (x : nnreal) => rfl) theorem infi_ne_top {α : Type u_1} [complete_lattice α] (f : ennreal → α) : (infi fun (x : ennreal) => infi fun (H : x ≠ ⊤) => f x) = infi fun (x : nnreal) => f ↑x := sorry theorem csupr_ne_top {α : Type u_1} [has_Sup α] (f : ennreal → α) : (supr fun (x : Subtype fun (x : ennreal) => x ≠ ⊤) => f ↑x) = supr fun (x : nnreal) => f ↑x := cinfi_ne_top f theorem supr_ne_top {α : Type u_1} [complete_lattice α] (f : ennreal → α) : (supr fun (x : ennreal) => supr fun (H : x ≠ ⊤) => f x) = supr fun (x : nnreal) => f ↑x := infi_ne_top fun (x : ennreal) => f x theorem infi_ennreal {α : Type u_1} [complete_lattice α] {f : ennreal → α} : (infi fun (n : ennreal) => f n) = (infi fun (n : nnreal) => f ↑n) ⊓ f ⊤ := sorry theorem supr_ennreal {α : Type u_1} [complete_lattice α] {f : ennreal → α} : (supr fun (n : ennreal) => f n) = (supr fun (n : nnreal) => f ↑n) ⊔ f ⊤ := infi_ennreal @[simp] theorem add_top {a : ennreal} : a + ⊤ = ⊤ := with_top.add_top @[simp] theorem top_add {a : ennreal} : ⊤ + a = ⊤ := with_top.top_add /-- Coercion `ℝ≥0 → ennreal` as a `ring_hom`. -/ def of_nnreal_hom : nnreal →+* ennreal := ring_hom.mk coe coe_one sorry coe_zero sorry @[simp] theorem coe_of_nnreal_hom : ⇑of_nnreal_hom = coe := rfl @[simp] theorem coe_indicator {α : Type u_1} (s : set α) (f : α → nnreal) (a : α) : ↑(set.indicator s f a) = set.indicator s (fun (x : α) => ↑(f x)) a := add_monoid_hom.map_indicator (↑of_nnreal_hom) s f a @[simp] theorem coe_pow {r : nnreal} (n : ℕ) : ↑(r ^ n) = ↑r ^ n := ring_hom.map_pow of_nnreal_hom r n @[simp] theorem add_eq_top {a : ennreal} {b : ennreal} : a + b = ⊤ ↔ a = ⊤ ∨ b = ⊤ := with_top.add_eq_top @[simp] theorem add_lt_top {a : ennreal} {b : ennreal} : a + b < ⊤ ↔ a < ⊤ ∧ b < ⊤ := with_top.add_lt_top theorem to_nnreal_add {r₁ : ennreal} {r₂ : ennreal} (h₁ : r₁ < ⊤) (h₂ : r₂ < ⊤) : ennreal.to_nnreal (r₁ + r₂) = ennreal.to_nnreal r₁ + ennreal.to_nnreal r₂ := sorry /- rw has trouble with the generic lt_top_iff_ne_top and bot_lt_iff_ne_bot (contrary to erw). This is solved with the next lemmas -/ protected theorem lt_top_iff_ne_top {a : ennreal} : a < ⊤ ↔ a ≠ ⊤ := lt_top_iff_ne_top protected theorem bot_lt_iff_ne_bot {a : ennreal} : 0 < a ↔ a ≠ 0 := bot_lt_iff_ne_bot theorem not_lt_top {x : ennreal} : ¬x < ⊤ ↔ x = ⊤ := eq.mpr (id (Eq._oldrec (Eq.refl (¬x < ⊤ ↔ x = ⊤)) (propext lt_top_iff_ne_top))) (eq.mpr (id (Eq._oldrec (Eq.refl (¬x ≠ ⊤ ↔ x = ⊤)) (propext not_not))) (iff.refl (x = ⊤))) theorem add_ne_top {a : ennreal} {b : ennreal} : a + b ≠ ⊤ ↔ a ≠ ⊤ ∧ b ≠ ⊤ := sorry theorem mul_top {a : ennreal} : a * ⊤ = ite (a = 0) 0 ⊤ := sorry theorem top_mul {a : ennreal} : ⊤ * a = ite (a = 0) 0 ⊤ := sorry @[simp] theorem top_mul_top : ⊤ * ⊤ = ⊤ := with_top.top_mul_top theorem top_pow {n : ℕ} (h : 0 < n) : ⊤ ^ n = ⊤ := sorry theorem mul_eq_top {a : ennreal} {b : ennreal} : a * b = ⊤ ↔ a ≠ 0 ∧ b = ⊤ ∨ a = ⊤ ∧ b ≠ 0 := with_top.mul_eq_top_iff theorem mul_lt_top {a : ennreal} {b : ennreal} : a < ⊤ → b < ⊤ → a * b < ⊤ := with_top.mul_lt_top theorem mul_ne_top {a : ennreal} {b : ennreal} : a ≠ ⊤ → b ≠ ⊤ → a * b ≠ ⊤ := sorry theorem ne_top_of_mul_ne_top_left {a : ennreal} {b : ennreal} (h : a * b ≠ ⊤) (hb : b ≠ 0) : a ≠ ⊤ := sorry theorem ne_top_of_mul_ne_top_right {a : ennreal} {b : ennreal} (h : a * b ≠ ⊤) (ha : a ≠ 0) : b ≠ ⊤ := ne_top_of_mul_ne_top_left (eq.mpr (id (Eq._oldrec (Eq.refl (b * a ≠ ⊤)) (mul_comm b a))) h) ha theorem lt_top_of_mul_lt_top_left {a : ennreal} {b : ennreal} (h : a * b < ⊤) (hb : b ≠ 0) : a < ⊤ := eq.mpr (id (Eq._oldrec (Eq.refl (a < ⊤)) (propext ennreal.lt_top_iff_ne_top))) (ne_top_of_mul_ne_top_left (eq.mp (Eq._oldrec (Eq.refl (a * b < ⊤)) (propext ennreal.lt_top_iff_ne_top)) h) hb) theorem lt_top_of_mul_lt_top_right {a : ennreal} {b : ennreal} (h : a * b < ⊤) (ha : a ≠ 0) : b < ⊤ := lt_top_of_mul_lt_top_left (eq.mpr (id (Eq._oldrec (Eq.refl (b * a < ⊤)) (mul_comm b a))) h) ha theorem mul_lt_top_iff {a : ennreal} {b : ennreal} : a * b < ⊤ ↔ a < ⊤ ∧ b < ⊤ ∨ a = 0 ∨ b = 0 := sorry @[simp] theorem mul_pos {a : ennreal} {b : ennreal} : 0 < a * b ↔ 0 < a ∧ 0 < b := sorry theorem pow_eq_top {a : ennreal} (n : ℕ) : a ^ n = ⊤ → a = ⊤ := sorry theorem pow_ne_top {a : ennreal} (h : a ≠ ⊤) {n : ℕ} : a ^ n ≠ ⊤ := mt (pow_eq_top n) h theorem pow_lt_top {a : ennreal} : a < ⊤ → ∀ (n : ℕ), a ^ n < ⊤ := eq.mpr (id (imp_congr_eq (propext lt_top_iff_ne_top) (forall_congr_eq fun (n : ℕ) => propext lt_top_iff_ne_top))) (eq.mp (Eq.refl (a ≠ ⊤ → ∀ (n : ℕ), a ^ n ≠ ⊤)) pow_ne_top) @[simp] theorem coe_finset_sum {α : Type u_1} {s : finset α} {f : α → nnreal} : ↑(finset.sum s fun (a : α) => f a) = finset.sum s fun (a : α) => ↑(f a) := ring_hom.map_sum of_nnreal_hom f s @[simp] theorem coe_finset_prod {α : Type u_1} {s : finset α} {f : α → nnreal} : ↑(finset.prod s fun (a : α) => f a) = finset.prod s fun (a : α) => ↑(f a) := ring_hom.map_prod of_nnreal_hom f s @[simp] theorem bot_eq_zero : ⊥ = 0 := rfl @[simp] theorem coe_lt_top {r : nnreal} : ↑r < ⊤ := with_top.coe_lt_top r @[simp] theorem not_top_le_coe {r : nnreal} : ¬⊤ ≤ ↑r := with_top.not_top_le_coe r theorem zero_lt_coe_iff {p : nnreal} : 0 < ↑p ↔ 0 < p := coe_lt_coe @[simp] theorem one_le_coe_iff {r : nnreal} : 1 ≤ ↑r ↔ 1 ≤ r := coe_le_coe @[simp] theorem coe_le_one_iff {r : nnreal} : ↑r ≤ 1 ↔ r ≤ 1 := coe_le_coe @[simp] theorem coe_lt_one_iff {p : nnreal} : ↑p < 1 ↔ p < 1 := coe_lt_coe @[simp] theorem one_lt_coe_iff {p : nnreal} : 1 < ↑p ↔ 1 < p := coe_lt_coe @[simp] theorem coe_nat (n : ℕ) : ↑↑n = ↑n := with_top.coe_nat n @[simp] theorem of_real_coe_nat (n : ℕ) : ennreal.of_real ↑n = ↑n := sorry @[simp] theorem nat_ne_top (n : ℕ) : ↑n ≠ ⊤ := with_top.nat_ne_top n @[simp] theorem top_ne_nat (n : ℕ) : ⊤ ≠ ↑n := with_top.top_ne_nat n @[simp] theorem one_lt_top : 1 < ⊤ := coe_lt_top theorem le_coe_iff {a : ennreal} {r : nnreal} : a ≤ ↑r ↔ ∃ (p : nnreal), a = ↑p ∧ p ≤ r := with_top.le_coe_iff theorem coe_le_iff {a : ennreal} {r : nnreal} : ↑r ≤ a ↔ ∀ (p : nnreal), a = ↑p → r ≤ p := with_top.coe_le_iff theorem lt_iff_exists_coe {a : ennreal} {b : ennreal} : a < b ↔ ∃ (p : nnreal), a = ↑p ∧ ↑p < b := with_top.lt_iff_exists_coe @[simp] theorem coe_finset_sup {α : Type u_1} {s : finset α} {f : α → nnreal} : ↑(finset.sup s f) = finset.sup s fun (x : α) => ↑(f x) := finset.comp_sup_eq_sup_comp_of_is_total coe coe_mono rfl theorem pow_le_pow {a : ennreal} {n : ℕ} {m : ℕ} (ha : 1 ≤ a) (h : n ≤ m) : a ^ n ≤ a ^ m := sorry @[simp] theorem max_eq_zero_iff {a : ennreal} {b : ennreal} : max a b = 0 ↔ a = 0 ∧ b = 0 := sorry @[simp] theorem max_zero_left {a : ennreal} : max 0 a = a := max_eq_right (zero_le a) @[simp] theorem max_zero_right {a : ennreal} : max a 0 = a := max_eq_left (zero_le a) -- TODO: why this is not a `rfl`? There is some hidden diamond here. @[simp] theorem sup_eq_max {a : ennreal} {b : ennreal} : a ⊔ b = max a b := eq_of_forall_ge_iff fun (c : ennreal) => iff.trans sup_le_iff (iff.symm max_le_iff) protected theorem pow_pos {a : ennreal} : 0 < a → ∀ (n : ℕ), 0 < a ^ n := canonically_ordered_semiring.pow_pos protected theorem pow_ne_zero {a : ennreal} : a ≠ 0 → ∀ (n : ℕ), a ^ n ≠ 0 := eq.mpr (id (Eq.refl (a ≠ 0 → ∀ (n : ℕ), a ^ n ≠ 0))) (eq.mp (imp_congr_eq (propext pos_iff_ne_zero) (forall_congr_eq fun (n : ℕ) => propext pos_iff_ne_zero)) ennreal.pow_pos) @[simp] theorem not_lt_zero {a : ennreal} : ¬a < 0 := eq.mpr (id (Eq.trans (propext not_lt) (propext ((fun {α : Type} (a : α) => iff_true_intro (zero_le a)) a)))) trivial theorem add_lt_add_iff_left {a : ennreal} {b : ennreal} {c : ennreal} : a < ⊤ → (a + c < a + b ↔ c < b) := with_top.add_lt_add_iff_left theorem add_lt_add_iff_right {a : ennreal} {b : ennreal} {c : ennreal} : a < ⊤ → (c + a < b + a ↔ c < b) := with_top.add_lt_add_iff_right theorem lt_add_right {a : ennreal} {b : ennreal} (ha : a < ⊤) (hb : 0 < b) : a < a + b := eq.mp (Eq._oldrec (Eq.refl (a + 0 < a + b)) (add_zero a)) (eq.mp (Eq._oldrec (Eq.refl (0 < b)) (Eq.symm (propext (add_lt_add_iff_left ha)))) hb) theorem le_of_forall_pos_le_add {a : ennreal} {b : ennreal} : (∀ (ε : nnreal), 0 < ε → b < ⊤ → a ≤ b + ↑ε) → a ≤ b := sorry theorem lt_iff_exists_rat_btwn {a : ennreal} {b : ennreal} : a < b ↔ ∃ (q : ℚ), 0 ≤ q ∧ a < ↑(nnreal.of_real ↑q) ∧ ↑(nnreal.of_real ↑q) < b := sorry theorem lt_iff_exists_real_btwn {a : ennreal} {b : ennreal} : a < b ↔ ∃ (r : ℝ), 0 ≤ r ∧ a < ennreal.of_real r ∧ ennreal.of_real r < b := sorry theorem lt_iff_exists_nnreal_btwn {a : ennreal} {b : ennreal} : a < b ↔ ∃ (r : nnreal), a < ↑r ∧ ↑r < b := with_top.lt_iff_exists_coe_btwn theorem lt_iff_exists_add_pos_lt {a : ennreal} {b : ennreal} : a < b ↔ ∃ (r : nnreal), 0 < r ∧ a + ↑r < b := sorry theorem coe_nat_lt_coe {r : nnreal} {n : ℕ} : ↑n < ↑r ↔ ↑n < r := coe_nat n ▸ coe_lt_coe theorem coe_lt_coe_nat {r : nnreal} {n : ℕ} : ↑r < ↑n ↔ r < ↑n := coe_nat n ▸ coe_lt_coe theorem coe_nat_lt_coe_nat {m : ℕ} {n : ℕ} : ↑m < ↑n ↔ m < n := coe_nat n ▸ iff.trans coe_nat_lt_coe nat.cast_lt theorem coe_nat_ne_top {n : ℕ} : ↑n ≠ ⊤ := coe_nat n ▸ coe_ne_top theorem coe_nat_mono : strict_mono coe := fun (_x _x_1 : ℕ) => iff.mpr coe_nat_lt_coe_nat theorem coe_nat_le_coe_nat {m : ℕ} {n : ℕ} : ↑m ≤ ↑n ↔ m ≤ n := strict_mono.le_iff_le coe_nat_mono protected instance char_zero : char_zero ennreal := char_zero.mk (strict_mono.injective coe_nat_mono) protected theorem exists_nat_gt {r : ennreal} (h : r ≠ ⊤) : ∃ (n : ℕ), r < ↑n := sorry theorem add_lt_add {a : ennreal} {b : ennreal} {c : ennreal} {d : ennreal} (ac : a < c) (bd : b < d) : a + b < c + d := sorry theorem coe_min {r : nnreal} {p : nnreal} : ↑(min r p) = min ↑r ↑p := monotone.map_min coe_mono theorem coe_max {r : nnreal} {p : nnreal} : ↑(max r p) = max ↑r ↑p := monotone.map_max coe_mono theorem le_of_top_imp_top_of_to_nnreal_le {a : ennreal} {b : ennreal} (h : a = ⊤ → b = ⊤) (h_nnreal : a ≠ ⊤ → b ≠ ⊤ → ennreal.to_nnreal a ≤ ennreal.to_nnreal b) : a ≤ b := sorry theorem coe_Sup {s : set nnreal} : bdd_above s → ↑(Sup s) = supr fun (a : nnreal) => supr fun (H : a ∈ s) => ↑a := with_top.coe_Sup theorem coe_Inf {s : set nnreal} : set.nonempty s → ↑(Inf s) = infi fun (a : nnreal) => infi fun (H : a ∈ s) => ↑a := with_top.coe_Inf @[simp] theorem top_mem_upper_bounds {s : set ennreal} : ⊤ ∈ upper_bounds s := fun (x : ennreal) (hx : x ∈ s) => le_top theorem coe_mem_upper_bounds {r : nnreal} {s : set nnreal} : ↑r ∈ upper_bounds (coe '' s) ↔ r ∈ upper_bounds s := sorry theorem mul_le_mul {a : ennreal} {b : ennreal} {c : ennreal} {d : ennreal} : a ≤ b → c ≤ d → a * c ≤ b * d := canonically_ordered_semiring.mul_le_mul theorem mul_lt_mul {a : ennreal} {b : ennreal} {c : ennreal} {d : ennreal} (ac : a < c) (bd : b < d) : a * b < c * d := sorry theorem mul_left_mono {a : ennreal} : monotone (Mul.mul a) := fun (b c : ennreal) => mul_le_mul (le_refl a) theorem mul_right_mono {a : ennreal} : monotone fun (x : ennreal) => x * a := fun (b c : ennreal) (h : b ≤ c) => mul_le_mul h (le_refl a) theorem max_mul {a : ennreal} {b : ennreal} {c : ennreal} : max a b * c = max (a * c) (b * c) := monotone.map_max mul_right_mono theorem mul_max {a : ennreal} {b : ennreal} {c : ennreal} : a * max b c = max (a * b) (a * c) := monotone.map_max mul_left_mono theorem mul_eq_mul_left {a : ennreal} {b : ennreal} {c : ennreal} : a ≠ 0 → a ≠ ⊤ → (a * b = a * c ↔ b = c) := sorry theorem mul_eq_mul_right {a : ennreal} {b : ennreal} {c : ennreal} : c ≠ 0 → c ≠ ⊤ → (a * c = b * c ↔ a = b) := mul_comm c a ▸ mul_comm c b ▸ mul_eq_mul_left theorem mul_le_mul_left {a : ennreal} {b : ennreal} {c : ennreal} : a ≠ 0 → a ≠ ⊤ → (a * b ≤ a * c ↔ b ≤ c) := sorry theorem mul_le_mul_right {a : ennreal} {b : ennreal} {c : ennreal} : c ≠ 0 → c ≠ ⊤ → (a * c ≤ b * c ↔ a ≤ b) := mul_comm c a ▸ mul_comm c b ▸ mul_le_mul_left theorem mul_lt_mul_left {a : ennreal} {b : ennreal} {c : ennreal} : a ≠ 0 → a ≠ ⊤ → (a * b < a * c ↔ b < c) := sorry theorem mul_lt_mul_right {a : ennreal} {b : ennreal} {c : ennreal} : c ≠ 0 → c ≠ ⊤ → (a * c < b * c ↔ a < b) := mul_comm c a ▸ mul_comm c b ▸ mul_lt_mul_left protected instance has_sub : Sub ennreal := { sub := fun (a b : ennreal) => Inf (set_of fun (d : ennreal) => a ≤ d + b) } theorem coe_sub {r : nnreal} {p : nnreal} : ↑(p - r) = ↑p - ↑r := sorry @[simp] theorem top_sub_coe {r : nnreal} : ⊤ - ↑r = ⊤ := sorry @[simp] theorem sub_eq_zero_of_le {a : ennreal} {b : ennreal} (h : a ≤ b) : a - b = 0 := le_antisymm (Inf_le (le_add_left h)) (zero_le (a - b)) @[simp] theorem sub_self {a : ennreal} : a - a = 0 := sub_eq_zero_of_le (le_refl a) @[simp] theorem zero_sub {a : ennreal} : 0 - a = 0 := le_antisymm (Inf_le (zero_le (0 + a))) (zero_le (0 - a)) @[simp] theorem sub_infty {a : ennreal} : a - ⊤ = 0 := sorry theorem sub_le_sub {a : ennreal} {b : ennreal} {c : ennreal} {d : ennreal} (h₁ : a ≤ b) (h₂ : d ≤ c) : a - c ≤ b - d := Inf_le_Inf fun (e : ennreal) (h : b ≤ e + d) => le_trans (le_trans h₁ h) (add_le_add (le_refl e) h₂) @[simp] theorem add_sub_self {a : ennreal} {b : ennreal} : b < ⊤ → a + b - b = a := sorry @[simp] theorem add_sub_self' {a : ennreal} {b : ennreal} (h : a < ⊤) : a + b - a = b := eq.mpr (id (Eq._oldrec (Eq.refl (a + b - a = b)) (add_comm a b))) (eq.mpr (id (Eq._oldrec (Eq.refl (b + a - a = b)) (add_sub_self h))) (Eq.refl b)) theorem add_right_inj {a : ennreal} {b : ennreal} {c : ennreal} (h : a < ⊤) : a + b = a + c ↔ b = c := sorry theorem add_left_inj {a : ennreal} {b : ennreal} {c : ennreal} (h : a < ⊤) : b + a = c + a ↔ b = c := eq.mpr (id (Eq._oldrec (Eq.refl (b + a = c + a ↔ b = c)) (add_comm b a))) (eq.mpr (id (Eq._oldrec (Eq.refl (a + b = c + a ↔ b = c)) (add_comm c a))) (eq.mpr (id (Eq._oldrec (Eq.refl (a + b = a + c ↔ b = c)) (propext (add_right_inj h)))) (iff.refl (b = c)))) @[simp] theorem sub_add_cancel_of_le {a : ennreal} {b : ennreal} : b ≤ a → a - b + b = a := sorry @[simp] theorem add_sub_cancel_of_le {a : ennreal} {b : ennreal} (h : b ≤ a) : b + (a - b) = a := eq.mpr (id (Eq._oldrec (Eq.refl (b + (a - b) = a)) (add_comm b (a - b)))) (eq.mpr (id (Eq._oldrec (Eq.refl (a - b + b = a)) (sub_add_cancel_of_le h))) (Eq.refl a)) theorem sub_add_self_eq_max {a : ennreal} {b : ennreal} : a - b + b = max a b := sorry theorem le_sub_add_self {a : ennreal} {b : ennreal} : a ≤ a - b + b := eq.mpr (id (Eq._oldrec (Eq.refl (a ≤ a - b + b)) sub_add_self_eq_max)) (le_max_left a b) @[simp] protected theorem sub_le_iff_le_add {a : ennreal} {b : ennreal} {c : ennreal} : a - b ≤ c ↔ a ≤ c + b := { mp := fun (h : a - b ≤ c) => le_trans le_sub_add_self (add_le_add_right h b), mpr := fun (h : a ≤ c + b) => Inf_le h } protected theorem sub_le_iff_le_add' {a : ennreal} {b : ennreal} {c : ennreal} : a - b ≤ c ↔ a ≤ b + c := add_comm c b ▸ ennreal.sub_le_iff_le_add theorem sub_eq_of_add_eq {a : ennreal} {b : ennreal} {c : ennreal} : b ≠ ⊤ → a + b = c → c - b = a := fun (hb : b ≠ ⊤) (hc : a + b = c) => hc ▸ add_sub_self (iff.mpr lt_top_iff_ne_top hb) protected theorem sub_le_of_sub_le {a : ennreal} {b : ennreal} {c : ennreal} (h : a - b ≤ c) : a - c ≤ b := iff.mpr ennreal.sub_le_iff_le_add (eq.mpr (id (Eq._oldrec (Eq.refl (a ≤ b + c)) (add_comm b c))) (iff.mp ennreal.sub_le_iff_le_add h)) protected theorem sub_lt_self {a : ennreal} {b : ennreal} : a ≠ ⊤ → a ≠ 0 → 0 < b → a - b < a := sorry @[simp] theorem sub_eq_zero_iff_le {a : ennreal} {b : ennreal} : a - b = 0 ↔ a ≤ b := sorry @[simp] theorem zero_lt_sub_iff_lt {a : ennreal} {b : ennreal} : 0 < a - b ↔ b < a := sorry theorem lt_sub_iff_add_lt {a : ennreal} {b : ennreal} {c : ennreal} : a < b - c ↔ a + c < b := sorry theorem sub_le_self (a : ennreal) (b : ennreal) : a - b ≤ a := iff.mpr ennreal.sub_le_iff_le_add (le_add_right (le_refl a)) @[simp] theorem sub_zero {a : ennreal} : a - 0 = a := sorry /-- A version of triangle inequality for difference as a "distance". -/ theorem sub_le_sub_add_sub {a : ennreal} {b : ennreal} {c : ennreal} : a - c ≤ a - b + (b - c) := iff.mpr ennreal.sub_le_iff_le_add (trans_rel_left LessEq (le_trans le_sub_add_self (add_le_add_left le_sub_add_self (a - b))) (Eq.symm (add_assoc (a - b) (b - c) c))) theorem sub_sub_cancel {a : ennreal} {b : ennreal} (h : a < ⊤) (h2 : b ≤ a) : a - (a - b) = b := sorry theorem sub_right_inj {a : ennreal} {b : ennreal} {c : ennreal} (ha : a < ⊤) (hb : b ≤ a) (hc : c ≤ a) : a - b = a - c ↔ b = c := sorry theorem sub_mul {a : ennreal} {b : ennreal} {c : ennreal} (h : 0 < b → b < a → c ≠ ⊤) : (a - b) * c = a * c - b * c := sorry theorem mul_sub {a : ennreal} {b : ennreal} {c : ennreal} (h : 0 < c → c < b → a ≠ ⊤) : a * (b - c) = a * b - a * c := sorry theorem sub_mul_ge {a : ennreal} {b : ennreal} {c : ennreal} : a * c - b * c ≤ (a - b) * c := sorry /-- A product of finite numbers is still finite -/ theorem prod_lt_top {α : Type u_1} {s : finset α} {f : α → ennreal} (h : ∀ (a : α), a ∈ s → f a < ⊤) : (finset.prod s fun (a : α) => f a) < ⊤ := with_top.prod_lt_top h /-- A sum of finite numbers is still finite -/ theorem sum_lt_top {α : Type u_1} {s : finset α} {f : α → ennreal} : (∀ (a : α), a ∈ s → f a < ⊤) → (finset.sum s fun (a : α) => f a) < ⊤ := with_top.sum_lt_top /-- A sum of finite numbers is still finite -/ theorem sum_lt_top_iff {α : Type u_1} {s : finset α} {f : α → ennreal} : (finset.sum s fun (a : α) => f a) < ⊤ ↔ ∀ (a : α), a ∈ s → f a < ⊤ := with_top.sum_lt_top_iff /-- A sum of numbers is infinite iff one of them is infinite -/ theorem sum_eq_top_iff {α : Type u_1} {s : finset α} {f : α → ennreal} : (finset.sum s fun (x : α) => f x) = ⊤ ↔ ∃ (a : α), ∃ (H : a ∈ s), f a = ⊤ := with_top.sum_eq_top_iff /-- seeing `ennreal` as `ℝ≥0` does not change their sum, unless one of the `ennreal` is infinity -/ theorem to_nnreal_sum {α : Type u_1} {s : finset α} {f : α → ennreal} (hf : ∀ (a : α), a ∈ s → f a < ⊤) : ennreal.to_nnreal (finset.sum s fun (a : α) => f a) = finset.sum s fun (a : α) => ennreal.to_nnreal (f a) := sorry /-- seeing `ennreal` as `real` does not change their sum, unless one of the `ennreal` is infinity -/ theorem to_real_sum {α : Type u_1} {s : finset α} {f : α → ennreal} (hf : ∀ (a : α), a ∈ s → f a < ⊤) : ennreal.to_real (finset.sum s fun (a : α) => f a) = finset.sum s fun (a : α) => ennreal.to_real (f a) := sorry theorem of_real_sum_of_nonneg {α : Type u_1} {s : finset α} {f : α → ℝ} (hf : ∀ (i : α), i ∈ s → 0 ≤ f i) : ennreal.of_real (finset.sum s fun (i : α) => f i) = finset.sum s fun (i : α) => ennreal.of_real (f i) := sorry protected theorem Ico_eq_Iio {y : ennreal} : set.Ico 0 y = set.Iio y := sorry theorem mem_Iio_self_add {x : ennreal} {ε : ennreal} : x ≠ ⊤ → 0 < ε → x ∈ set.Iio (x + ε) := fun (xt : x ≠ ⊤) (ε0 : 0 < ε) => lt_add_right (eq.mpr (id (Eq._oldrec (Eq.refl (x < ⊤)) (propext lt_top_iff_ne_top))) xt) ε0 theorem not_mem_Ioo_self_sub {x : ennreal} {y : ennreal} {ε : ennreal} : x = 0 → ¬x ∈ set.Ioo (x - ε) y := sorry theorem mem_Ioo_self_sub_add {x : ennreal} {ε₁ : ennreal} {ε₂ : ennreal} : x ≠ ⊤ → x ≠ 0 → 0 < ε₁ → 0 < ε₂ → x ∈ set.Ioo (x - ε₁) (x + ε₂) := fun (xt : x ≠ ⊤) (x0 : x ≠ 0) (ε0 : 0 < ε₁) (ε0' : 0 < ε₂) => { left := ennreal.sub_lt_self xt x0 ε0, right := lt_add_right (eq.mpr (id (Eq._oldrec (Eq.refl (x < ⊤)) (propext lt_top_iff_ne_top))) xt) ε0' } @[simp] theorem bit0_inj {a : ennreal} {b : ennreal} : bit0 a = bit0 b ↔ a = b := sorry @[simp] theorem bit0_eq_zero_iff {a : ennreal} : bit0 a = 0 ↔ a = 0 := sorry @[simp] theorem bit0_eq_top_iff {a : ennreal} : bit0 a = ⊤ ↔ a = ⊤ := eq.mpr (id (Eq._oldrec (Eq.refl (bit0 a = ⊤ ↔ a = ⊤)) (bit0.equations._eqn_1 a))) (eq.mpr (id (Eq._oldrec (Eq.refl (a + a = ⊤ ↔ a = ⊤)) (propext add_eq_top))) (eq.mpr (id (Eq._oldrec (Eq.refl (a = ⊤ ∨ a = ⊤ ↔ a = ⊤)) (propext (or_self (a = ⊤))))) (iff.refl (a = ⊤)))) @[simp] theorem bit1_inj {a : ennreal} {b : ennreal} : bit1 a = bit1 b ↔ a = b := sorry @[simp] theorem bit1_ne_zero {a : ennreal} : bit1 a ≠ 0 := sorry @[simp] theorem bit1_eq_one_iff {a : ennreal} : bit1 a = 1 ↔ a = 0 := sorry @[simp] theorem bit1_eq_top_iff {a : ennreal} : bit1 a = ⊤ ↔ a = ⊤ := sorry protected instance has_inv : has_inv ennreal := has_inv.mk fun (a : ennreal) => Inf (set_of fun (b : ennreal) => 1 ≤ a * b) protected instance div_inv_monoid : div_inv_monoid ennreal := div_inv_monoid.mk monoid.mul sorry monoid.one sorry sorry has_inv.inv fun (a b : ennreal) => monoid.mul a (b⁻¹) @[simp] theorem inv_zero : 0⁻¹ = ⊤ := sorry @[simp] theorem inv_top : ⊤⁻¹ = 0 := sorry @[simp] theorem coe_inv {r : nnreal} (hr : r ≠ 0) : ↑(r⁻¹) = (↑r⁻¹) := sorry theorem coe_inv_le {r : nnreal} : ↑(r⁻¹) ≤ (↑r⁻¹) := sorry theorem coe_inv_two : ↑(bit0 1⁻¹) = (bit0 1⁻¹) := eq.mpr (id (Eq._oldrec (Eq.refl (↑(bit0 1⁻¹) = (bit0 1⁻¹))) (coe_inv (ne_of_gt zero_lt_two)))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑(bit0 1)⁻¹ = (bit0 1⁻¹))) coe_two)) (Eq.refl (bit0 1⁻¹))) @[simp] theorem coe_div {r : nnreal} {p : nnreal} (hr : r ≠ 0) : ↑(p / r) = ↑p / ↑r := sorry @[simp] theorem inv_one : 1⁻¹ = 1 := sorry @[simp] theorem div_one {a : ennreal} : a / 1 = a := eq.mpr (id (Eq._oldrec (Eq.refl (a / 1 = a)) (div_eq_mul_inv a 1))) (eq.mpr (id (Eq._oldrec (Eq.refl (a * (1⁻¹) = a)) inv_one)) (eq.mpr (id (Eq._oldrec (Eq.refl (a * 1 = a)) (mul_one a))) (Eq.refl a))) protected theorem inv_pow {a : ennreal} {n : ℕ} : a ^ n⁻¹ = a⁻¹ ^ n := sorry @[simp] theorem inv_inv {a : ennreal} : a⁻¹⁻¹ = a := sorry theorem inv_involutive : function.involutive fun (a : ennreal) => a⁻¹ := fun (a : ennreal) => inv_inv theorem inv_bijective : function.bijective fun (a : ennreal) => a⁻¹ := function.involutive.bijective inv_involutive @[simp] theorem inv_eq_inv {a : ennreal} {b : ennreal} : a⁻¹ = (b⁻¹) ↔ a = b := function.injective.eq_iff (and.left inv_bijective) @[simp] theorem inv_eq_top {a : ennreal} : a⁻¹ = ⊤ ↔ a = 0 := inv_zero ▸ inv_eq_inv theorem inv_ne_top {a : ennreal} : a⁻¹ ≠ ⊤ ↔ a ≠ 0 := sorry @[simp] theorem inv_lt_top {x : ennreal} : x⁻¹ < ⊤ ↔ 0 < x := sorry theorem div_lt_top {x : ennreal} {y : ennreal} (h1 : x < ⊤) (h2 : 0 < y) : x / y < ⊤ := mul_lt_top h1 (iff.mpr inv_lt_top h2) @[simp] theorem inv_eq_zero {a : ennreal} : a⁻¹ = 0 ↔ a = ⊤ := inv_top ▸ inv_eq_inv theorem inv_ne_zero {a : ennreal} : a⁻¹ ≠ 0 ↔ a ≠ ⊤ := sorry @[simp] theorem inv_pos {a : ennreal} : 0 < (a⁻¹) ↔ a ≠ ⊤ := iff.trans pos_iff_ne_zero inv_ne_zero @[simp] theorem inv_lt_inv {a : ennreal} {b : ennreal} : a⁻¹ < (b⁻¹) ↔ b < a := sorry theorem inv_lt_iff_inv_lt {a : ennreal} {b : ennreal} : a⁻¹ < b ↔ b⁻¹ < a := sorry theorem lt_inv_iff_lt_inv {a : ennreal} {b : ennreal} : a < (b⁻¹) ↔ b < (a⁻¹) := sorry @[simp] theorem inv_le_inv {a : ennreal} {b : ennreal} : a⁻¹ ≤ (b⁻¹) ↔ b ≤ a := sorry theorem inv_le_iff_inv_le {a : ennreal} {b : ennreal} : a⁻¹ ≤ b ↔ b⁻¹ ≤ a := sorry theorem le_inv_iff_le_inv {a : ennreal} {b : ennreal} : a ≤ (b⁻¹) ↔ b ≤ (a⁻¹) := sorry @[simp] theorem inv_lt_one {a : ennreal} : a⁻¹ < 1 ↔ 1 < a := iff.trans inv_lt_iff_inv_lt (eq.mpr (id (Eq._oldrec (Eq.refl (1⁻¹ < a ↔ 1 < a)) inv_one)) (iff.refl (1 < a))) @[simp] theorem div_top {a : ennreal} : a / ⊤ = 0 := eq.mpr (id (Eq._oldrec (Eq.refl (a / ⊤ = 0)) (div_eq_mul_inv a ⊤))) (eq.mpr (id (Eq._oldrec (Eq.refl (a * (⊤⁻¹) = 0)) inv_top)) (eq.mpr (id (Eq._oldrec (Eq.refl (a * 0 = 0)) (mul_zero a))) (Eq.refl 0))) @[simp] theorem top_div_coe {p : nnreal} : ⊤ / ↑p = ⊤ := sorry theorem top_div_of_ne_top {a : ennreal} (h : a ≠ ⊤) : ⊤ / a = ⊤ := sorry theorem top_div_of_lt_top {a : ennreal} (h : a < ⊤) : ⊤ / a = ⊤ := top_div_of_ne_top (has_lt.lt.ne h) theorem top_div {a : ennreal} : ⊤ / a = ite (a = ⊤) 0 ⊤ := sorry @[simp] theorem zero_div {a : ennreal} : 0 / a = 0 := zero_mul (a⁻¹) theorem div_eq_top {a : ennreal} {b : ennreal} : a / b = ⊤ ↔ a ≠ 0 ∧ b = 0 ∨ a = ⊤ ∧ b ≠ ⊤ := sorry theorem le_div_iff_mul_le {a : ennreal} {b : ennreal} {c : ennreal} (h0 : b ≠ 0 ∨ c ≠ 0) (ht : b ≠ ⊤ ∨ c ≠ ⊤) : a ≤ c / b ↔ a * b ≤ c := sorry theorem div_le_iff_le_mul {a : ennreal} {b : ennreal} {c : ennreal} (hb0 : b ≠ 0 ∨ c ≠ ⊤) (hbt : b ≠ ⊤ ∨ c ≠ 0) : a / b ≤ c ↔ a ≤ c * b := sorry theorem div_le_of_le_mul {a : ennreal} {b : ennreal} {c : ennreal} (h : a ≤ b * c) : a / c ≤ b := sorry protected theorem div_lt_iff {a : ennreal} {b : ennreal} {c : ennreal} (h0 : b ≠ 0 ∨ c ≠ 0) (ht : b ≠ ⊤ ∨ c ≠ ⊤) : c / b < a ↔ c < a * b := lt_iff_lt_of_le_iff_le (le_div_iff_mul_le h0 ht) theorem mul_lt_of_lt_div {a : ennreal} {b : ennreal} {c : ennreal} (h : a < b / c) : a * c < b := sorry theorem inv_le_iff_le_mul {a : ennreal} {b : ennreal} : (b = ⊤ → a ≠ 0) → (a = ⊤ → b ≠ 0) → (a⁻¹ ≤ b ↔ 1 ≤ a * b) := sorry @[simp] theorem le_inv_iff_mul_le {a : ennreal} {b : ennreal} : a ≤ (b⁻¹) ↔ a * b ≤ 1 := sorry theorem mul_inv_cancel {a : ennreal} (h0 : a ≠ 0) (ht : a ≠ ⊤) : a * (a⁻¹) = 1 := sorry theorem inv_mul_cancel {a : ennreal} (h0 : a ≠ 0) (ht : a ≠ ⊤) : a⁻¹ * a = 1 := mul_comm a (a⁻¹) ▸ mul_inv_cancel h0 ht theorem mul_le_iff_le_inv {a : ennreal} {b : ennreal} {r : ennreal} (hr₀ : r ≠ 0) (hr₁ : r ≠ ⊤) : r * a ≤ b ↔ a ≤ r⁻¹ * b := sorry theorem le_of_forall_nnreal_lt {x : ennreal} {y : ennreal} (h : ∀ (r : nnreal), ↑r < x → ↑r ≤ y) : x ≤ y := sorry theorem eq_top_of_forall_nnreal_le {x : ennreal} (h : ∀ (r : nnreal), ↑r ≤ x) : x = ⊤ := top_unique (le_of_forall_nnreal_lt fun (r : nnreal) (hr : ↑r < ⊤) => h r) theorem div_add_div_same {a : ennreal} {b : ennreal} {c : ennreal} : a / c + b / c = (a + b) / c := Eq.symm (right_distrib a b (c⁻¹)) theorem div_self {a : ennreal} (h0 : a ≠ 0) (hI : a ≠ ⊤) : a / a = 1 := mul_inv_cancel h0 hI theorem mul_div_cancel {a : ennreal} {b : ennreal} (h0 : a ≠ 0) (hI : a ≠ ⊤) : b / a * a = b := sorry theorem mul_div_cancel' {a : ennreal} {b : ennreal} (h0 : a ≠ 0) (hI : a ≠ ⊤) : a * (b / a) = b := eq.mpr (id (Eq._oldrec (Eq.refl (a * (b / a) = b)) (mul_comm a (b / a)))) (eq.mpr (id (Eq._oldrec (Eq.refl (b / a * a = b)) (mul_div_cancel h0 hI))) (Eq.refl b)) theorem mul_div_le {a : ennreal} {b : ennreal} : a * (b / a) ≤ b := sorry theorem inv_two_add_inv_two : bit0 1⁻¹ + (bit0 1⁻¹) = 1 := eq.mpr (id (Eq._oldrec (Eq.refl (bit0 1⁻¹ + (bit0 1⁻¹) = 1)) (Eq.symm (two_mul (bit0 1⁻¹))))) (eq.mpr (id (Eq._oldrec (Eq.refl (bit0 1 * (bit0 1⁻¹) = 1)) (Eq.symm (div_eq_mul_inv (bit0 1) (bit0 1))))) (eq.mpr (id (Eq._oldrec (Eq.refl (bit0 1 / bit0 1 = 1)) (div_self two_ne_zero two_ne_top))) (Eq.refl 1))) theorem add_halves (a : ennreal) : a / bit0 1 + a / bit0 1 = a := sorry @[simp] theorem div_zero_iff {a : ennreal} {b : ennreal} : a / b = 0 ↔ a = 0 ∨ b = ⊤ := sorry @[simp] theorem div_pos_iff {a : ennreal} {b : ennreal} : 0 < a / b ↔ a ≠ 0 ∧ b ≠ ⊤ := sorry theorem half_pos {a : ennreal} (h : 0 < a) : 0 < a / bit0 1 := sorry theorem one_half_lt_one : bit0 1⁻¹ < 1 := iff.mpr inv_lt_one one_lt_two theorem half_lt_self {a : ennreal} (hz : a ≠ 0) (ht : a ≠ ⊤) : a / bit0 1 < a := sorry theorem sub_half {a : ennreal} (h : a ≠ ⊤) : a - a / bit0 1 = a / bit0 1 := sorry theorem one_sub_inv_two : 1 - (bit0 1⁻¹) = (bit0 1⁻¹) := sorry theorem exists_inv_nat_lt {a : ennreal} (h : a ≠ 0) : ∃ (n : ℕ), ↑n⁻¹ < a := sorry theorem exists_nat_pos_mul_gt {a : ennreal} {b : ennreal} (ha : a ≠ 0) (hb : b ≠ ⊤) : ∃ (n : ℕ), ∃ (H : n > 0), b < ↑n * a := sorry theorem exists_nat_mul_gt {a : ennreal} {b : ennreal} (ha : a ≠ 0) (hb : b ≠ ⊤) : ∃ (n : ℕ), b < ↑n * a := Exists.imp (fun (n : ℕ) => Exists.snd) (exists_nat_pos_mul_gt ha hb) theorem exists_nat_pos_inv_mul_lt {a : ennreal} {b : ennreal} (ha : a ≠ ⊤) (hb : b ≠ 0) : ∃ (n : ℕ), ∃ (H : n > 0), ↑n⁻¹ * a < b := sorry theorem exists_nnreal_pos_mul_lt {a : ennreal} {b : ennreal} (ha : a ≠ ⊤) (hb : b ≠ 0) : ∃ (n : nnreal), ∃ (H : n > 0), ↑n * a < b := sorry theorem to_real_add {a : ennreal} {b : ennreal} (ha : a ≠ ⊤) (hb : b ≠ ⊤) : ennreal.to_real (a + b) = ennreal.to_real a + ennreal.to_real b := sorry theorem to_real_add_le {a : ennreal} {b : ennreal} : ennreal.to_real (a + b) ≤ ennreal.to_real a + ennreal.to_real b := sorry theorem of_real_add {p : ℝ} {q : ℝ} (hp : 0 ≤ p) (hq : 0 ≤ q) : ennreal.of_real (p + q) = ennreal.of_real p + ennreal.of_real q := sorry theorem of_real_add_le {p : ℝ} {q : ℝ} : ennreal.of_real (p + q) ≤ ennreal.of_real p + ennreal.of_real q := iff.mpr coe_le_coe nnreal.of_real_add_le @[simp] theorem to_real_le_to_real {a : ennreal} {b : ennreal} (ha : a ≠ ⊤) (hb : b ≠ ⊤) : ennreal.to_real a ≤ ennreal.to_real b ↔ a ≤ b := sorry @[simp] theorem to_real_lt_to_real {a : ennreal} {b : ennreal} (ha : a ≠ ⊤) (hb : b ≠ ⊤) : ennreal.to_real a < ennreal.to_real b ↔ a < b := sorry theorem to_real_max {a : ennreal} {b : ennreal} (hr : a ≠ ⊤) (hp : b ≠ ⊤) : ennreal.to_real (max a b) = max (ennreal.to_real a) (ennreal.to_real b) := sorry theorem to_nnreal_pos_iff {a : ennreal} : 0 < ennreal.to_nnreal a ↔ 0 < a ∧ a ≠ ⊤ := sorry theorem to_real_pos_iff {a : ennreal} : 0 < ennreal.to_real a ↔ 0 < a ∧ a ≠ ⊤ := iff.trans nnreal.coe_pos to_nnreal_pos_iff theorem of_real_le_of_real {p : ℝ} {q : ℝ} (h : p ≤ q) : ennreal.of_real p ≤ ennreal.of_real q := sorry theorem of_real_le_of_le_to_real {a : ℝ} {b : ennreal} (h : a ≤ ennreal.to_real b) : ennreal.of_real a ≤ b := has_le.le.trans (of_real_le_of_real h) of_real_to_real_le @[simp] theorem of_real_le_of_real_iff {p : ℝ} {q : ℝ} (h : 0 ≤ q) : ennreal.of_real p ≤ ennreal.of_real q ↔ p ≤ q := sorry @[simp] theorem of_real_lt_of_real_iff {p : ℝ} {q : ℝ} (h : 0 < q) : ennreal.of_real p < ennreal.of_real q ↔ p < q := sorry theorem of_real_lt_of_real_iff_of_nonneg {p : ℝ} {q : ℝ} (hp : 0 ≤ p) : ennreal.of_real p < ennreal.of_real q ↔ p < q := sorry @[simp] theorem of_real_pos {p : ℝ} : 0 < ennreal.of_real p ↔ 0 < p := sorry @[simp] theorem of_real_eq_zero {p : ℝ} : ennreal.of_real p = 0 ↔ p ≤ 0 := sorry theorem of_real_le_iff_le_to_real {a : ℝ} {b : ennreal} (hb : b ≠ ⊤) : ennreal.of_real a ≤ b ↔ a ≤ ennreal.to_real b := sorry theorem of_real_lt_iff_lt_to_real {a : ℝ} {b : ennreal} (ha : 0 ≤ a) (hb : b ≠ ⊤) : ennreal.of_real a < b ↔ a < ennreal.to_real b := sorry theorem le_of_real_iff_to_real_le {a : ennreal} {b : ℝ} (ha : a ≠ ⊤) (hb : 0 ≤ b) : a ≤ ennreal.of_real b ↔ ennreal.to_real a ≤ b := sorry theorem to_real_le_of_le_of_real {a : ennreal} {b : ℝ} (hb : 0 ≤ b) (h : a ≤ ennreal.of_real b) : ennreal.to_real a ≤ b := (fun (ha : a ≠ ⊤) => iff.mp (le_of_real_iff_to_real_le ha hb) h) (ne_top_of_le_ne_top of_real_ne_top h) theorem lt_of_real_iff_to_real_lt {a : ennreal} {b : ℝ} (ha : a ≠ ⊤) : a < ennreal.of_real b ↔ ennreal.to_real a < b := sorry theorem of_real_mul {p : ℝ} {q : ℝ} (hp : 0 ≤ p) : ennreal.of_real (p * q) = ennreal.of_real p * ennreal.of_real q := sorry theorem of_real_inv_of_pos {x : ℝ} (hx : 0 < x) : ennreal.of_real x⁻¹ = ennreal.of_real (x⁻¹) := sorry theorem of_real_div_of_pos {x : ℝ} {y : ℝ} (hy : 0 < y) : ennreal.of_real (x / y) = ennreal.of_real x / ennreal.of_real y := sorry theorem to_real_of_real_mul (c : ℝ) (a : ennreal) (h : 0 ≤ c) : ennreal.to_real (ennreal.of_real c * a) = c * ennreal.to_real a := sorry @[simp] theorem to_nnreal_mul_top (a : ennreal) : ennreal.to_nnreal (a * ⊤) = 0 := sorry @[simp] theorem to_nnreal_top_mul (a : ennreal) : ennreal.to_nnreal (⊤ * a) = 0 := eq.mpr (id (Eq._oldrec (Eq.refl (ennreal.to_nnreal (⊤ * a) = 0)) (mul_comm ⊤ a))) (eq.mpr (id (Eq._oldrec (Eq.refl (ennreal.to_nnreal (a * ⊤) = 0)) (to_nnreal_mul_top a))) (Eq.refl 0)) @[simp] theorem to_real_mul_top (a : ennreal) : ennreal.to_real (a * ⊤) = 0 := eq.mpr (id (Eq._oldrec (Eq.refl (ennreal.to_real (a * ⊤) = 0)) (to_real.equations._eqn_1 (a * ⊤)))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑(ennreal.to_nnreal (a * ⊤)) = 0)) (to_nnreal_mul_top a))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑0 = 0)) nnreal.coe_zero)) (Eq.refl 0))) @[simp] theorem to_real_top_mul (a : ennreal) : ennreal.to_real (⊤ * a) = 0 := eq.mpr (id (Eq._oldrec (Eq.refl (ennreal.to_real (⊤ * a) = 0)) (mul_comm ⊤ a))) (to_real_mul_top a) theorem to_real_eq_to_real {a : ennreal} {b : ennreal} (ha : a < ⊤) (hb : b < ⊤) : ennreal.to_real a = ennreal.to_real b ↔ a = b := sorry /-- `ennreal.to_nnreal` as a `monoid_hom`. -/ def to_nnreal_hom : ennreal →* nnreal := monoid_hom.mk ennreal.to_nnreal sorry sorry theorem to_nnreal_mul {a : ennreal} {b : ennreal} : ennreal.to_nnreal (a * b) = ennreal.to_nnreal a * ennreal.to_nnreal b := monoid_hom.map_mul to_nnreal_hom a b theorem to_nnreal_pow (a : ennreal) (n : ℕ) : ennreal.to_nnreal (a ^ n) = ennreal.to_nnreal a ^ n := monoid_hom.map_pow to_nnreal_hom a n theorem to_nnreal_prod {ι : Type u_1} {s : finset ι} {f : ι → ennreal} : ennreal.to_nnreal (finset.prod s fun (i : ι) => f i) = finset.prod s fun (i : ι) => ennreal.to_nnreal (f i) := monoid_hom.map_prod to_nnreal_hom (fun (i : ι) => f i) s /-- `ennreal.to_real` as a `monoid_hom`. -/ def to_real_hom : ennreal →* ℝ := monoid_hom.comp (↑nnreal.to_real_hom) to_nnreal_hom theorem to_real_mul {a : ennreal} {b : ennreal} : ennreal.to_real (a * b) = ennreal.to_real a * ennreal.to_real b := monoid_hom.map_mul to_real_hom a b theorem to_real_pow (a : ennreal) (n : ℕ) : ennreal.to_real (a ^ n) = ennreal.to_real a ^ n := monoid_hom.map_pow to_real_hom a n theorem to_real_prod {ι : Type u_1} {s : finset ι} {f : ι → ennreal} : ennreal.to_real (finset.prod s fun (i : ι) => f i) = finset.prod s fun (i : ι) => ennreal.to_real (f i) := monoid_hom.map_prod to_real_hom (fun (i : ι) => f i) s theorem of_real_prod_of_nonneg {α : Type u_1} {s : finset α} {f : α → ℝ} (hf : ∀ (i : α), i ∈ s → 0 ≤ f i) : ennreal.of_real (finset.prod s fun (i : α) => f i) = finset.prod s fun (i : α) => ennreal.of_real (f i) := sorry theorem infi_add {a : ennreal} {ι : Sort u_3} {f : ι → ennreal} : infi f + a = infi fun (i : ι) => f i + a := le_antisymm (le_infi fun (i : ι) => add_le_add (infi_le f i) (le_refl a)) (iff.mp ennreal.sub_le_iff_le_add (le_infi fun (i : ι) => iff.mpr ennreal.sub_le_iff_le_add (infi_le (fun (i : ι) => f i + a) i))) theorem supr_sub {a : ennreal} {ι : Sort u_3} {f : ι → ennreal} : (supr fun (i : ι) => f i) - a = supr fun (i : ι) => f i - a := sorry theorem sub_infi {a : ennreal} {ι : Sort u_3} {f : ι → ennreal} : (a - infi fun (i : ι) => f i) = supr fun (i : ι) => a - f i := sorry theorem Inf_add {a : ennreal} {s : set ennreal} : Inf s + a = infi fun (b : ennreal) => infi fun (H : b ∈ s) => b + a := sorry theorem add_infi {ι : Sort u_3} {f : ι → ennreal} {a : ennreal} : a + infi f = infi fun (b : ι) => a + f b := sorry theorem infi_add_infi {ι : Sort u_3} {f : ι → ennreal} {g : ι → ennreal} (h : ∀ (i j : ι), ∃ (k : ι), f k + g k ≤ f i + g j) : infi f + infi g = infi fun (a : ι) => f a + g a := sorry theorem infi_sum {α : Type u_1} {ι : Sort u_3} {f : ι → α → ennreal} {s : finset α} [Nonempty ι] (h : ∀ (t : finset α) (i j : ι), ∃ (k : ι), ∀ (a : α), a ∈ t → f k a ≤ f i a ∧ f k a ≤ f j a) : (infi fun (i : ι) => finset.sum s fun (a : α) => f i a) = finset.sum s fun (a : α) => infi fun (i : ι) => f i a := sorry theorem infi_mul {ι : Sort u_1} [Nonempty ι] {f : ι → ennreal} {x : ennreal} (h : x ≠ ⊤) : infi f * x = infi fun (i : ι) => f i * x := sorry theorem mul_infi {ι : Sort u_1} [Nonempty ι] {f : ι → ennreal} {x : ennreal} (h : x ≠ ⊤) : x * infi f = infi fun (i : ι) => x * f i := sorry /-! `supr_mul`, `mul_supr` and variants are in `topology.instances.ennreal`. -/ theorem supr_coe_nat : (supr fun (n : ℕ) => ↑n) = ⊤ := iff.mpr (supr_eq_top fun (n : ℕ) => ↑n) fun (b : ennreal) (hb : b < ⊤) => ennreal.exists_nat_gt (iff.mp lt_top_iff_ne_top hb) /-- `le_of_add_le_add_left` is normally applicable to `ordered_cancel_add_comm_monoid`, but it holds in `ennreal` with the additional assumption that `a < ∞`. -/ theorem le_of_add_le_add_left {a : ennreal} {b : ennreal} {c : ennreal} : a < ⊤ → a + b ≤ a + c → b ≤ c := sorry end Mathlib
5a70dc6e6d6412cd89b843f048eed008ad9abf99
ce6917c5bacabee346655160b74a307b4a5ab620
/src/ch2/ex0603.lean
c438f35178a48fbfaf1b482dc72bc54acf244fd3
[]
no_license
Ailrun/Theorem_Proving_in_Lean
ae6a23f3c54d62d401314d6a771e8ff8b4132db2
2eb1b5caf93c6a5a555c79e9097cf2ba5a66cf68
refs/heads/master
1,609,838,270,467
1,586,846,743,000
1,586,846,743,000
240,967,761
1
0
null
null
null
null
UTF-8
Lean
false
false
229
lean
variables (α β γ : Type) variables (g : β → γ) (f : α → β) (h : α → α) variable x : α def compose := g (f x) def do_twice := h (h x) def do_thrice := h (h (h x)) #check compose #check do_twice #check do_thrice
b946c8c0f111c85dc49d363119413e18e56e5a99
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/03_Propositions_and_Proofs.org.25.lean
424f7ad9dd43363f12ef3ba1af14ecfad0a524a3
[]
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
141
lean
/- page 41 -/ import standard variables p q : Prop -- BEGIN example (Hpq : p → q) (Hnq : ¬q) : ¬p := assume Hp : p, Hnq (Hpq Hp) -- END
146ff73af3b3963902ef8b085e062b954febe191
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/data/equiv/nat.lean
c91fbffaec65fa916b7fbdfe4d35fcfc16671a31
[ "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
1,981
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro Additional facts about equiv and encodable using the pairing function on nat. -/ import data.nat.pairing import data.pnat.basic open nat namespace equiv /-- An equivalence between `ℕ × ℕ` and `ℕ`, using the `mkpair` and `unpair` functions in `data.nat.pairing`. -/ @[simp] def nat_prod_nat_equiv_nat : ℕ × ℕ ≃ ℕ := ⟨λ p, nat.mkpair p.1 p.2, nat.unpair, λ p, begin cases p, apply nat.unpair_mkpair end, nat.mkpair_unpair⟩ /-- An equivalence between `bool × ℕ` and `ℕ`, by mapping `(tt, x)` to `2 * x + 1` and `(ff, x)` to `2 * x`. -/ @[simp] def bool_prod_nat_equiv_nat : bool × ℕ ≃ ℕ := ⟨λ ⟨b, n⟩, bit b n, bodd_div2, λ ⟨b, n⟩, by simp [bool_prod_nat_equiv_nat._match_1, bodd_bit, div2_bit], λ n, by simp [bool_prod_nat_equiv_nat._match_1, bit_decomp]⟩ /-- An equivalence between `ℕ ⊕ ℕ` and `ℕ`, by mapping `(sum.inl x)` to `2 * x` and `(sum.inr x)` to `2 * x + 1`. -/ @[simp] def nat_sum_nat_equiv_nat : ℕ ⊕ ℕ ≃ ℕ := (bool_prod_equiv_sum ℕ).symm.trans bool_prod_nat_equiv_nat /-- An equivalence between `ℤ` and `ℕ`, through `ℤ ≃ ℕ ⊕ ℕ` and `ℕ ⊕ ℕ ≃ ℕ`. -/ def int_equiv_nat : ℤ ≃ ℕ := int_equiv_nat_sum_nat.trans nat_sum_nat_equiv_nat /-- An equivalence between `α × α` and `α`, given that there is an equivalence between `α` and `ℕ`. -/ def prod_equiv_of_equiv_nat {α : Sort*} (e : α ≃ ℕ) : α × α ≃ α := calc α × α ≃ ℕ × ℕ : prod_congr e e ... ≃ ℕ : nat_prod_nat_equiv_nat ... ≃ α : e.symm /-- An equivalence between `ℕ+` and `ℕ`, by mapping `x` in `ℕ+` to `x - 1` in `ℕ`. -/ def pnat_equiv_nat : ℕ+ ≃ ℕ := ⟨λ n, pred n.1, succ_pnat, λ ⟨n, h⟩, by { cases n, cases h, simp [succ_pnat, h] }, λ n, by simp [succ_pnat] ⟩ end equiv
b300c7d03132899ae10e5297ec7dda6249cc4bb5
94e33a31faa76775069b071adea97e86e218a8ee
/src/data/set/intervals/basic.lean
d8d7307415c3a298725729c928cc34e4095ca272
[ "Apache-2.0" ]
permissive
urkud/mathlib
eab80095e1b9f1513bfb7f25b4fa82fa4fd02989
6379d39e6b5b279df9715f8011369a301b634e41
refs/heads/master
1,658,425,342,662
1,658,078,703,000
1,658,078,703,000
186,910,338
0
0
Apache-2.0
1,568,512,083,000
1,557,958,709,000
Lean
UTF-8
Lean
false
false
59,245
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot, Yury Kudryashov, Rémy Degenne -/ import algebra.order.group import order.rel_iso /-! # Intervals In any preorder `α`, we define intervals (which on each side can be either infinite, open, or closed) using the following naming conventions: - `i`: infinite - `o`: open - `c`: closed Each interval has the name `I` + letter for left side + letter for right side. For instance, `Ioc a b` denotes the inverval `(a, b]`. This file contains these definitions, and basic facts on inclusion, intersection, difference of intervals (where the precise statements may depend on the properties of the order, in particular for some statements it should be `linear_order` or `densely_ordered`). TODO: This is just the beginning; a lot of rules are missing -/ variables {α β : Type*} namespace set open set open order_dual (to_dual of_dual) section preorder variables [preorder α] {a a₁ a₂ b b₁ b₂ c x : α} /-- Left-open right-open interval -/ def Ioo (a b : α) := {x | a < x ∧ x < b} /-- Left-closed right-open interval -/ def Ico (a b : α) := {x | a ≤ x ∧ x < b} /-- Left-infinite right-open interval -/ def Iio (a : α) := {x | x < a} /-- Left-closed right-closed interval -/ def Icc (a b : α) := {x | a ≤ x ∧ x ≤ b} /-- Left-infinite right-closed interval -/ def Iic (b : α) := {x | x ≤ b} /-- Left-open right-closed interval -/ def Ioc (a b : α) := {x | a < x ∧ x ≤ b} /-- Left-closed right-infinite interval -/ def Ici (a : α) := {x | a ≤ x} /-- Left-open right-infinite interval -/ def Ioi (a : α) := {x | a < x} lemma Ioo_def (a b : α) : {x | a < x ∧ x < b} = Ioo a b := rfl lemma Ico_def (a b : α) : {x | a ≤ x ∧ x < b} = Ico a b := rfl lemma Iio_def (a : α) : {x | x < a} = Iio a := rfl lemma Icc_def (a b : α) : {x | a ≤ x ∧ x ≤ b} = Icc a b := rfl lemma Iic_def (b : α) : {x | x ≤ b} = Iic b := rfl lemma Ioc_def (a b : α) : {x | a < x ∧ x ≤ b} = Ioc a b := rfl lemma Ici_def (a : α) : {x | a ≤ x} = Ici a := rfl lemma Ioi_def (a : α) : {x | a < x} = Ioi a := rfl @[simp] lemma mem_Ioo : x ∈ Ioo a b ↔ a < x ∧ x < b := iff.rfl @[simp] lemma mem_Ico : x ∈ Ico a b ↔ a ≤ x ∧ x < b := iff.rfl @[simp] lemma mem_Iio : x ∈ Iio b ↔ x < b := iff.rfl @[simp] lemma mem_Icc : x ∈ Icc a b ↔ a ≤ x ∧ x ≤ b := iff.rfl @[simp] lemma mem_Iic : x ∈ Iic b ↔ x ≤ b := iff.rfl @[simp] lemma mem_Ioc : x ∈ Ioc a b ↔ a < x ∧ x ≤ b := iff.rfl @[simp] lemma mem_Ici : x ∈ Ici a ↔ a ≤ x := iff.rfl @[simp] lemma mem_Ioi : x ∈ Ioi a ↔ a < x := iff.rfl instance decidable_mem_Ioo [decidable (a < x ∧ x < b)] : decidable (x ∈ Ioo a b) := by assumption instance decidable_mem_Ico [decidable (a ≤ x ∧ x < b)] : decidable (x ∈ Ico a b) := by assumption instance decidable_mem_Iio [decidable (x < b)] : decidable (x ∈ Iio b) := by assumption instance decidable_mem_Icc [decidable (a ≤ x ∧ x ≤ b)] : decidable (x ∈ Icc a b) := by assumption instance decidable_mem_Iic [decidable (x ≤ b)] : decidable (x ∈ Iic b) := by assumption instance decidable_mem_Ioc [decidable (a < x ∧ x ≤ b)] : decidable (x ∈ Ioc a b) := by assumption instance decidable_mem_Ici [decidable (a ≤ x)] : decidable (x ∈ Ici a) := by assumption instance decidable_mem_Ioi [decidable (a < x)] : decidable (x ∈ Ioi a) := by assumption @[simp] lemma left_mem_Ioo : a ∈ Ioo a b ↔ false := by simp [lt_irrefl] @[simp] lemma left_mem_Ico : a ∈ Ico a b ↔ a < b := by simp [le_refl] @[simp] lemma left_mem_Icc : a ∈ Icc a b ↔ a ≤ b := by simp [le_refl] @[simp] lemma left_mem_Ioc : a ∈ Ioc a b ↔ false := by simp [lt_irrefl] lemma left_mem_Ici : a ∈ Ici a := by simp @[simp] lemma right_mem_Ioo : b ∈ Ioo a b ↔ false := by simp [lt_irrefl] @[simp] lemma right_mem_Ico : b ∈ Ico a b ↔ false := by simp [lt_irrefl] @[simp] lemma right_mem_Icc : b ∈ Icc a b ↔ a ≤ b := by simp [le_refl] @[simp] lemma right_mem_Ioc : b ∈ Ioc a b ↔ a < b := by simp [le_refl] lemma right_mem_Iic : a ∈ Iic a := by simp @[simp] lemma dual_Ici : Ici (to_dual a) = of_dual ⁻¹' Iic a := rfl @[simp] lemma dual_Iic : Iic (to_dual a) = of_dual ⁻¹' Ici a := rfl @[simp] lemma dual_Ioi : Ioi (to_dual a) = of_dual ⁻¹' Iio a := rfl @[simp] lemma dual_Iio : Iio (to_dual a) = of_dual ⁻¹' Ioi a := rfl @[simp] lemma dual_Icc : Icc (to_dual a) (to_dual b) = of_dual ⁻¹' Icc b a := set.ext $ λ x, and_comm _ _ @[simp] lemma dual_Ioc : Ioc (to_dual a) (to_dual b) = of_dual ⁻¹' Ico b a := set.ext $ λ x, and_comm _ _ @[simp] lemma dual_Ico : Ico (to_dual a) (to_dual b) = of_dual ⁻¹' Ioc b a := set.ext $ λ x, and_comm _ _ @[simp] lemma dual_Ioo : Ioo (to_dual a) (to_dual b) = of_dual ⁻¹' Ioo b a := set.ext $ λ x, and_comm _ _ @[simp] lemma nonempty_Icc : (Icc a b).nonempty ↔ a ≤ b := ⟨λ ⟨x, hx⟩, hx.1.trans hx.2, λ h, ⟨a, left_mem_Icc.2 h⟩⟩ @[simp] lemma nonempty_Ico : (Ico a b).nonempty ↔ a < b := ⟨λ ⟨x, hx⟩, hx.1.trans_lt hx.2, λ h, ⟨a, left_mem_Ico.2 h⟩⟩ @[simp] lemma nonempty_Ioc : (Ioc a b).nonempty ↔ a < b := ⟨λ ⟨x, hx⟩, hx.1.trans_le hx.2, λ h, ⟨b, right_mem_Ioc.2 h⟩⟩ @[simp] lemma nonempty_Ici : (Ici a).nonempty := ⟨a, left_mem_Ici⟩ @[simp] lemma nonempty_Iic : (Iic a).nonempty := ⟨a, right_mem_Iic⟩ @[simp] lemma nonempty_Ioo [densely_ordered α] : (Ioo a b).nonempty ↔ a < b := ⟨λ ⟨x, ha, hb⟩, ha.trans hb, exists_between⟩ @[simp] lemma nonempty_Ioi [no_max_order α] : (Ioi a).nonempty := exists_gt a @[simp] lemma nonempty_Iio [no_min_order α] : (Iio a).nonempty := exists_lt a lemma nonempty_Icc_subtype (h : a ≤ b) : nonempty (Icc a b) := nonempty.to_subtype (nonempty_Icc.mpr h) lemma nonempty_Ico_subtype (h : a < b) : nonempty (Ico a b) := nonempty.to_subtype (nonempty_Ico.mpr h) lemma nonempty_Ioc_subtype (h : a < b) : nonempty (Ioc a b) := nonempty.to_subtype (nonempty_Ioc.mpr h) /-- An interval `Ici a` is nonempty. -/ instance nonempty_Ici_subtype : nonempty (Ici a) := nonempty.to_subtype nonempty_Ici /-- An interval `Iic a` is nonempty. -/ instance nonempty_Iic_subtype : nonempty (Iic a) := nonempty.to_subtype nonempty_Iic lemma nonempty_Ioo_subtype [densely_ordered α] (h : a < b) : nonempty (Ioo a b) := nonempty.to_subtype (nonempty_Ioo.mpr h) /-- In an order without maximal elements, the intervals `Ioi` are nonempty. -/ instance nonempty_Ioi_subtype [no_max_order α] : nonempty (Ioi a) := nonempty.to_subtype nonempty_Ioi /-- In an order without minimal elements, the intervals `Iio` are nonempty. -/ instance nonempty_Iio_subtype [no_min_order α] : nonempty (Iio a) := nonempty.to_subtype nonempty_Iio instance [no_min_order α] : no_min_order (Iio a) := ⟨λ a, let ⟨b, hb⟩ := exists_lt (a : α) in ⟨⟨b, lt_trans hb a.2⟩, hb⟩⟩ instance [no_min_order α] : no_min_order (Iic a) := ⟨λ a, let ⟨b, hb⟩ := exists_lt (a : α) in ⟨⟨b, hb.le.trans a.2⟩, hb⟩⟩ instance [no_max_order α] : no_max_order (Ioi a) := order_dual.no_max_order (Iio (to_dual a)) instance [no_max_order α] : no_max_order (Ici a) := order_dual.no_max_order (Iic (to_dual a)) @[simp] lemma Icc_eq_empty (h : ¬a ≤ b) : Icc a b = ∅ := eq_empty_iff_forall_not_mem.2 $ λ x ⟨ha, hb⟩, h (ha.trans hb) @[simp] lemma Ico_eq_empty (h : ¬a < b) : Ico a b = ∅ := eq_empty_iff_forall_not_mem.2 $ λ x ⟨ha, hb⟩, h (ha.trans_lt hb) @[simp] lemma Ioc_eq_empty (h : ¬a < b) : Ioc a b = ∅ := eq_empty_iff_forall_not_mem.2 $ λ x ⟨ha, hb⟩, h (ha.trans_le hb) @[simp] lemma Ioo_eq_empty (h : ¬a < b) : Ioo a b = ∅ := eq_empty_iff_forall_not_mem.2 $ λ x ⟨ha, hb⟩, h (ha.trans hb) @[simp] lemma Icc_eq_empty_of_lt (h : b < a) : Icc a b = ∅ := Icc_eq_empty h.not_le @[simp] lemma Ico_eq_empty_of_le (h : b ≤ a) : Ico a b = ∅ := Ico_eq_empty h.not_lt @[simp] lemma Ioc_eq_empty_of_le (h : b ≤ a) : Ioc a b = ∅ := Ioc_eq_empty h.not_lt @[simp] lemma Ioo_eq_empty_of_le (h : b ≤ a) : Ioo a b = ∅ := Ioo_eq_empty h.not_lt @[simp] lemma Ico_self (a : α) : Ico a a = ∅ := Ico_eq_empty $ lt_irrefl _ @[simp] lemma Ioc_self (a : α) : Ioc a a = ∅ := Ioc_eq_empty $ lt_irrefl _ @[simp] lemma Ioo_self (a : α) : Ioo a a = ∅ := Ioo_eq_empty $ lt_irrefl _ lemma Ici_subset_Ici : Ici a ⊆ Ici b ↔ b ≤ a := ⟨λ h, h $ left_mem_Ici, λ h x hx, h.trans hx⟩ lemma Iic_subset_Iic : Iic a ⊆ Iic b ↔ a ≤ b := @Ici_subset_Ici αᵒᵈ _ _ _ lemma Ici_subset_Ioi : Ici a ⊆ Ioi b ↔ b < a := ⟨λ h, h left_mem_Ici, λ h x hx, h.trans_le hx⟩ lemma Iic_subset_Iio : Iic a ⊆ Iio b ↔ a < b := ⟨λ h, h right_mem_Iic, λ h x hx, lt_of_le_of_lt hx h⟩ lemma Ioo_subset_Ioo (h₁ : a₂ ≤ a₁) (h₂ : b₁ ≤ b₂) : Ioo a₁ b₁ ⊆ Ioo a₂ b₂ := λ x ⟨hx₁, hx₂⟩, ⟨h₁.trans_lt hx₁, hx₂.trans_le h₂⟩ lemma Ioo_subset_Ioo_left (h : a₁ ≤ a₂) : Ioo a₂ b ⊆ Ioo a₁ b := Ioo_subset_Ioo h le_rfl lemma Ioo_subset_Ioo_right (h : b₁ ≤ b₂) : Ioo a b₁ ⊆ Ioo a b₂ := Ioo_subset_Ioo le_rfl h lemma Ico_subset_Ico (h₁ : a₂ ≤ a₁) (h₂ : b₁ ≤ b₂) : Ico a₁ b₁ ⊆ Ico a₂ b₂ := λ x ⟨hx₁, hx₂⟩, ⟨h₁.trans hx₁, hx₂.trans_le h₂⟩ lemma Ico_subset_Ico_left (h : a₁ ≤ a₂) : Ico a₂ b ⊆ Ico a₁ b := Ico_subset_Ico h le_rfl lemma Ico_subset_Ico_right (h : b₁ ≤ b₂) : Ico a b₁ ⊆ Ico a b₂ := Ico_subset_Ico le_rfl h lemma Icc_subset_Icc (h₁ : a₂ ≤ a₁) (h₂ : b₁ ≤ b₂) : Icc a₁ b₁ ⊆ Icc a₂ b₂ := λ x ⟨hx₁, hx₂⟩, ⟨h₁.trans hx₁, le_trans hx₂ h₂⟩ lemma Icc_subset_Icc_left (h : a₁ ≤ a₂) : Icc a₂ b ⊆ Icc a₁ b := Icc_subset_Icc h le_rfl lemma Icc_subset_Icc_right (h : b₁ ≤ b₂) : Icc a b₁ ⊆ Icc a b₂ := Icc_subset_Icc le_rfl h lemma Icc_subset_Ioo (ha : a₂ < a₁) (hb : b₁ < b₂) : Icc a₁ b₁ ⊆ Ioo a₂ b₂ := λ x hx, ⟨ha.trans_le hx.1, hx.2.trans_lt hb⟩ lemma Icc_subset_Ici_self : Icc a b ⊆ Ici a := λ x, and.left lemma Icc_subset_Iic_self : Icc a b ⊆ Iic b := λ x, and.right lemma Ioc_subset_Iic_self : Ioc a b ⊆ Iic b := λ x, and.right lemma Ioc_subset_Ioc (h₁ : a₂ ≤ a₁) (h₂ : b₁ ≤ b₂) : Ioc a₁ b₁ ⊆ Ioc a₂ b₂ := λ x ⟨hx₁, hx₂⟩, ⟨h₁.trans_lt hx₁, hx₂.trans h₂⟩ lemma Ioc_subset_Ioc_left (h : a₁ ≤ a₂) : Ioc a₂ b ⊆ Ioc a₁ b := Ioc_subset_Ioc h le_rfl lemma Ioc_subset_Ioc_right (h : b₁ ≤ b₂) : Ioc a b₁ ⊆ Ioc a b₂ := Ioc_subset_Ioc le_rfl h lemma Ico_subset_Ioo_left (h₁ : a₁ < a₂) : Ico a₂ b ⊆ Ioo a₁ b := λ x, and.imp_left h₁.trans_le lemma Ioc_subset_Ioo_right (h : b₁ < b₂) : Ioc a b₁ ⊆ Ioo a b₂ := λ x, and.imp_right $ λ h', h'.trans_lt h lemma Icc_subset_Ico_right (h₁ : b₁ < b₂) : Icc a b₁ ⊆ Ico a b₂ := λ x, and.imp_right $ λ h₂, h₂.trans_lt h₁ lemma Ioo_subset_Ico_self : Ioo a b ⊆ Ico a b := λ x, and.imp_left le_of_lt lemma Ioo_subset_Ioc_self : Ioo a b ⊆ Ioc a b := λ x, and.imp_right le_of_lt lemma Ico_subset_Icc_self : Ico a b ⊆ Icc a b := λ x, and.imp_right le_of_lt lemma Ioc_subset_Icc_self : Ioc a b ⊆ Icc a b := λ x, and.imp_left le_of_lt lemma Ioo_subset_Icc_self : Ioo a b ⊆ Icc a b := subset.trans Ioo_subset_Ico_self Ico_subset_Icc_self lemma Ico_subset_Iio_self : Ico a b ⊆ Iio b := λ x, and.right lemma Ioo_subset_Iio_self : Ioo a b ⊆ Iio b := λ x, and.right lemma Ioc_subset_Ioi_self : Ioc a b ⊆ Ioi a := λ x, and.left lemma Ioo_subset_Ioi_self : Ioo a b ⊆ Ioi a := λ x, and.left lemma Ioi_subset_Ici_self : Ioi a ⊆ Ici a := λ x hx, le_of_lt hx lemma Iio_subset_Iic_self : Iio a ⊆ Iic a := λ x hx, le_of_lt hx lemma Ico_subset_Ici_self : Ico a b ⊆ Ici a := λ x, and.left lemma Ioi_ssubset_Ici_self : Ioi a ⊂ Ici a := ⟨Ioi_subset_Ici_self, λ h, lt_irrefl a (h le_rfl)⟩ lemma Iio_ssubset_Iic_self : Iio a ⊂ Iic a := @Ioi_ssubset_Ici_self αᵒᵈ _ _ lemma Icc_subset_Icc_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Icc a₂ b₂ ↔ a₂ ≤ a₁ ∧ b₁ ≤ b₂ := ⟨λ h, ⟨(h ⟨le_rfl, h₁⟩).1, (h ⟨h₁, le_rfl⟩).2⟩, λ ⟨h, h'⟩ x ⟨hx, hx'⟩, ⟨h.trans hx, hx'.trans h'⟩⟩ lemma Icc_subset_Ioo_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Ioo a₂ b₂ ↔ a₂ < a₁ ∧ b₁ < b₂ := ⟨λ h, ⟨(h ⟨le_rfl, h₁⟩).1, (h ⟨h₁, le_rfl⟩).2⟩, λ ⟨h, h'⟩ x ⟨hx, hx'⟩, ⟨h.trans_le hx, hx'.trans_lt h'⟩⟩ lemma Icc_subset_Ico_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Ico a₂ b₂ ↔ a₂ ≤ a₁ ∧ b₁ < b₂ := ⟨λ h, ⟨(h ⟨le_rfl, h₁⟩).1, (h ⟨h₁, le_rfl⟩).2⟩, λ ⟨h, h'⟩ x ⟨hx, hx'⟩, ⟨h.trans hx, hx'.trans_lt h'⟩⟩ lemma Icc_subset_Ioc_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Ioc a₂ b₂ ↔ a₂ < a₁ ∧ b₁ ≤ b₂ := ⟨λ h, ⟨(h ⟨le_rfl, h₁⟩).1, (h ⟨h₁, le_rfl⟩).2⟩, λ ⟨h, h'⟩ x ⟨hx, hx'⟩, ⟨h.trans_le hx, hx'.trans h'⟩⟩ lemma Icc_subset_Iio_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Iio b₂ ↔ b₁ < b₂ := ⟨λ h, h ⟨h₁, le_rfl⟩, λ h x ⟨hx, hx'⟩, hx'.trans_lt h⟩ lemma Icc_subset_Ioi_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Ioi a₂ ↔ a₂ < a₁ := ⟨λ h, h ⟨le_rfl, h₁⟩, λ h x ⟨hx, hx'⟩, h.trans_le hx⟩ lemma Icc_subset_Iic_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Iic b₂ ↔ b₁ ≤ b₂ := ⟨λ h, h ⟨h₁, le_rfl⟩, λ h x ⟨hx, hx'⟩, hx'.trans h⟩ lemma Icc_subset_Ici_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Ici a₂ ↔ a₂ ≤ a₁ := ⟨λ h, h ⟨le_rfl, h₁⟩, λ h x ⟨hx, hx'⟩, h.trans hx⟩ lemma Icc_ssubset_Icc_left (hI : a₂ ≤ b₂) (ha : a₂ < a₁) (hb : b₁ ≤ b₂) : Icc a₁ b₁ ⊂ Icc a₂ b₂ := (ssubset_iff_of_subset (Icc_subset_Icc (le_of_lt ha) hb)).mpr ⟨a₂, left_mem_Icc.mpr hI, not_and.mpr (λ f g, lt_irrefl a₂ (ha.trans_le f))⟩ lemma Icc_ssubset_Icc_right (hI : a₂ ≤ b₂) (ha : a₂ ≤ a₁) (hb : b₁ < b₂) : Icc a₁ b₁ ⊂ Icc a₂ b₂ := (ssubset_iff_of_subset (Icc_subset_Icc ha (le_of_lt hb))).mpr ⟨b₂, right_mem_Icc.mpr hI, (λ f, lt_irrefl b₁ (hb.trans_le f.2))⟩ /-- If `a ≤ b`, then `(b, +∞) ⊆ (a, +∞)`. In preorders, this is just an implication. If you need the equivalence in linear orders, use `Ioi_subset_Ioi_iff`. -/ lemma Ioi_subset_Ioi (h : a ≤ b) : Ioi b ⊆ Ioi a := λ x hx, h.trans_lt hx /-- If `a ≤ b`, then `(b, +∞) ⊆ [a, +∞)`. In preorders, this is just an implication. If you need the equivalence in dense linear orders, use `Ioi_subset_Ici_iff`. -/ lemma Ioi_subset_Ici (h : a ≤ b) : Ioi b ⊆ Ici a := subset.trans (Ioi_subset_Ioi h) Ioi_subset_Ici_self /-- If `a ≤ b`, then `(-∞, a) ⊆ (-∞, b)`. In preorders, this is just an implication. If you need the equivalence in linear orders, use `Iio_subset_Iio_iff`. -/ lemma Iio_subset_Iio (h : a ≤ b) : Iio a ⊆ Iio b := λ x hx, lt_of_lt_of_le hx h /-- If `a ≤ b`, then `(-∞, a) ⊆ (-∞, b]`. In preorders, this is just an implication. If you need the equivalence in dense linear orders, use `Iio_subset_Iic_iff`. -/ lemma Iio_subset_Iic (h : a ≤ b) : Iio a ⊆ Iic b := subset.trans (Iio_subset_Iio h) Iio_subset_Iic_self lemma Ici_inter_Iic : Ici a ∩ Iic b = Icc a b := rfl lemma Ici_inter_Iio : Ici a ∩ Iio b = Ico a b := rfl lemma Ioi_inter_Iic : Ioi a ∩ Iic b = Ioc a b := rfl lemma Ioi_inter_Iio : Ioi a ∩ Iio b = Ioo a b := rfl lemma Iic_inter_Ici : Iic a ∩ Ici b = Icc b a := inter_comm _ _ lemma Iio_inter_Ici : Iio a ∩ Ici b = Ico b a := inter_comm _ _ lemma Iic_inter_Ioi : Iic a ∩ Ioi b = Ioc b a := inter_comm _ _ lemma Iio_inter_Ioi : Iio a ∩ Ioi b = Ioo b a := inter_comm _ _ lemma mem_Icc_of_Ioo (h : x ∈ Ioo a b) : x ∈ Icc a b := Ioo_subset_Icc_self h lemma mem_Ico_of_Ioo (h : x ∈ Ioo a b) : x ∈ Ico a b := Ioo_subset_Ico_self h lemma mem_Ioc_of_Ioo (h : x ∈ Ioo a b) : x ∈ Ioc a b := Ioo_subset_Ioc_self h lemma mem_Icc_of_Ico (h : x ∈ Ico a b) : x ∈ Icc a b := Ico_subset_Icc_self h lemma mem_Icc_of_Ioc (h : x ∈ Ioc a b) : x ∈ Icc a b := Ioc_subset_Icc_self h lemma mem_Ici_of_Ioi (h : x ∈ Ioi a) : x ∈ Ici a := Ioi_subset_Ici_self h lemma mem_Iic_of_Iio (h : x ∈ Iio a) : x ∈ Iic a := Iio_subset_Iic_self h lemma Icc_eq_empty_iff : Icc a b = ∅ ↔ ¬a ≤ b := by rw [←not_nonempty_iff_eq_empty, not_iff_not, nonempty_Icc] lemma Ico_eq_empty_iff : Ico a b = ∅ ↔ ¬a < b := by rw [←not_nonempty_iff_eq_empty, not_iff_not, nonempty_Ico] lemma Ioc_eq_empty_iff : Ioc a b = ∅ ↔ ¬a < b := by rw [←not_nonempty_iff_eq_empty, not_iff_not, nonempty_Ioc] lemma Ioo_eq_empty_iff [densely_ordered α] : Ioo a b = ∅ ↔ ¬a < b := by rw [←not_nonempty_iff_eq_empty, not_iff_not, nonempty_Ioo] lemma _root_.is_top.Iic_eq (h : is_top a) : Iic a = univ := eq_univ_of_forall h lemma _root_.is_bot.Ici_eq (h : is_bot a) : Ici a = univ := eq_univ_of_forall h lemma _root_.is_max.Ioi_eq (h : is_max a) : Ioi a = ∅ := eq_empty_of_subset_empty $ λ b, h.not_lt lemma _root_.is_min.Iio_eq (h : is_min a) : Iio a = ∅ := eq_empty_of_subset_empty $ λ b, h.not_lt lemma Iic_inter_Ioc_of_le (h : a ≤ c) : Iic a ∩ Ioc b c = Ioc b a := ext $ λ x, ⟨λ H, ⟨H.2.1, H.1⟩, λ H, ⟨H.2, H.1, H.2.trans h⟩⟩ end preorder section partial_order variables [partial_order α] {a b c : α} @[simp] lemma Icc_self (a : α) : Icc a a = {a} := set.ext $ by simp [Icc, le_antisymm_iff, and_comm] @[simp] lemma Icc_eq_singleton_iff : Icc a b = {c} ↔ a = c ∧ b = c := begin refine ⟨λ h, _, _⟩, { have hab : a ≤ b := nonempty_Icc.1 (h.symm.subst $ singleton_nonempty c), exact ⟨eq_of_mem_singleton $ h.subst $ left_mem_Icc.2 hab, eq_of_mem_singleton $ h.subst $ right_mem_Icc.2 hab⟩ }, { rintro ⟨rfl, rfl⟩, exact Icc_self _ } end @[simp] lemma Icc_diff_left : Icc a b \ {a} = Ioc a b := ext $ λ x, by simp [lt_iff_le_and_ne, eq_comm, and.right_comm] @[simp] lemma Icc_diff_right : Icc a b \ {b} = Ico a b := ext $ λ x, by simp [lt_iff_le_and_ne, and_assoc] @[simp] lemma Ico_diff_left : Ico a b \ {a} = Ioo a b := ext $ λ x, by simp [and.right_comm, ← lt_iff_le_and_ne, eq_comm] @[simp] lemma Ioc_diff_right : Ioc a b \ {b} = Ioo a b := ext $ λ x, by simp [and_assoc, ← lt_iff_le_and_ne] @[simp] lemma Icc_diff_both : Icc a b \ {a, b} = Ioo a b := by rw [insert_eq, ← diff_diff, Icc_diff_left, Ioc_diff_right] @[simp] lemma Ici_diff_left : Ici a \ {a} = Ioi a := ext $ λ x, by simp [lt_iff_le_and_ne, eq_comm] @[simp] lemma Iic_diff_right : Iic a \ {a} = Iio a := ext $ λ x, by simp [lt_iff_le_and_ne] @[simp] lemma Ico_diff_Ioo_same (h : a < b) : Ico a b \ Ioo a b = {a} := by rw [← Ico_diff_left, diff_diff_cancel_left (singleton_subset_iff.2 $ left_mem_Ico.2 h)] @[simp] lemma Ioc_diff_Ioo_same (h : a < b) : Ioc a b \ Ioo a b = {b} := by rw [← Ioc_diff_right, diff_diff_cancel_left (singleton_subset_iff.2 $ right_mem_Ioc.2 h)] @[simp] lemma Icc_diff_Ico_same (h : a ≤ b) : Icc a b \ Ico a b = {b} := by rw [← Icc_diff_right, diff_diff_cancel_left (singleton_subset_iff.2 $ right_mem_Icc.2 h)] @[simp] lemma Icc_diff_Ioc_same (h : a ≤ b) : Icc a b \ Ioc a b = {a} := by rw [← Icc_diff_left, diff_diff_cancel_left (singleton_subset_iff.2 $ left_mem_Icc.2 h)] @[simp] lemma Icc_diff_Ioo_same (h : a ≤ b) : Icc a b \ Ioo a b = {a, b} := by { rw [← Icc_diff_both, diff_diff_cancel_left], simp [insert_subset, h] } @[simp] lemma Ici_diff_Ioi_same : Ici a \ Ioi a = {a} := by rw [← Ici_diff_left, diff_diff_cancel_left (singleton_subset_iff.2 left_mem_Ici)] @[simp] lemma Iic_diff_Iio_same : Iic a \ Iio a = {a} := by rw [← Iic_diff_right, diff_diff_cancel_left (singleton_subset_iff.2 right_mem_Iic)] @[simp] lemma Ioi_union_left : Ioi a ∪ {a} = Ici a := ext $ λ x, by simp [eq_comm, le_iff_eq_or_lt] @[simp] lemma Iio_union_right : Iio a ∪ {a} = Iic a := ext $ λ x, le_iff_lt_or_eq.symm lemma Ioo_union_left (hab : a < b) : Ioo a b ∪ {a} = Ico a b := by rw [← Ico_diff_left, diff_union_self, union_eq_self_of_subset_right (singleton_subset_iff.2 $ left_mem_Ico.2 hab)] lemma Ioo_union_right (hab : a < b) : Ioo a b ∪ {b} = Ioc a b := by simpa only [dual_Ioo, dual_Ico] using Ioo_union_left hab.dual lemma Ioc_union_left (hab : a ≤ b) : Ioc a b ∪ {a} = Icc a b := by rw [← Icc_diff_left, diff_union_self, union_eq_self_of_subset_right (singleton_subset_iff.2 $ left_mem_Icc.2 hab)] lemma Ico_union_right (hab : a ≤ b) : Ico a b ∪ {b} = Icc a b := by simpa only [dual_Ioc, dual_Icc] using Ioc_union_left hab.dual @[simp] lemma Ico_insert_right (h : a ≤ b) : insert b (Ico a b) = Icc a b := by rw [insert_eq, union_comm, Ico_union_right h] @[simp] lemma Ioc_insert_left (h : a ≤ b) : insert a (Ioc a b) = Icc a b := by rw [insert_eq, union_comm, Ioc_union_left h] @[simp] lemma Ioo_insert_left (h : a < b) : insert a (Ioo a b) = Ico a b := by rw [insert_eq, union_comm, Ioo_union_left h] @[simp] lemma Ioo_insert_right (h : a < b) : insert b (Ioo a b) = Ioc a b := by rw [insert_eq, union_comm, Ioo_union_right h] @[simp] lemma Iio_insert : insert a (Iio a) = Iic a := ext $ λ _, le_iff_eq_or_lt.symm @[simp] lemma Ioi_insert : insert a (Ioi a) = Ici a := ext $ λ _, (or_congr_left' eq_comm).trans le_iff_eq_or_lt.symm lemma mem_Ici_Ioi_of_subset_of_subset {s : set α} (ho : Ioi a ⊆ s) (hc : s ⊆ Ici a) : s ∈ ({Ici a, Ioi a} : set (set α)) := classical.by_cases (λ h : a ∈ s, or.inl $ subset.antisymm hc $ by rw [← Ioi_union_left, union_subset_iff]; simp *) (λ h, or.inr $ subset.antisymm (λ x hx, lt_of_le_of_ne (hc hx) (λ heq, h $ heq.symm ▸ hx)) ho) lemma mem_Iic_Iio_of_subset_of_subset {s : set α} (ho : Iio a ⊆ s) (hc : s ⊆ Iic a) : s ∈ ({Iic a, Iio a} : set (set α)) := @mem_Ici_Ioi_of_subset_of_subset αᵒᵈ _ a s ho hc lemma mem_Icc_Ico_Ioc_Ioo_of_subset_of_subset {s : set α} (ho : Ioo a b ⊆ s) (hc : s ⊆ Icc a b) : s ∈ ({Icc a b, Ico a b, Ioc a b, Ioo a b} : set (set α)) := begin classical, by_cases ha : a ∈ s; by_cases hb : b ∈ s, { refine or.inl (subset.antisymm hc _), rwa [← Ico_diff_left, diff_singleton_subset_iff, insert_eq_of_mem ha, ← Icc_diff_right, diff_singleton_subset_iff, insert_eq_of_mem hb] at ho }, { refine (or.inr $ or.inl $ subset.antisymm _ _), { rw [← Icc_diff_right], exact subset_diff_singleton hc hb }, { rwa [← Ico_diff_left, diff_singleton_subset_iff, insert_eq_of_mem ha] at ho } }, { refine (or.inr $ or.inr $ or.inl $ subset.antisymm _ _), { rw [← Icc_diff_left], exact subset_diff_singleton hc ha }, { rwa [← Ioc_diff_right, diff_singleton_subset_iff, insert_eq_of_mem hb] at ho } }, { refine (or.inr $ or.inr $ or.inr $ subset.antisymm _ ho), rw [← Ico_diff_left, ← Icc_diff_right], apply_rules [subset_diff_singleton] } end lemma eq_left_or_mem_Ioo_of_mem_Ico {x : α} (hmem : x ∈ Ico a b) : x = a ∨ x ∈ Ioo a b := hmem.1.eq_or_gt.imp_right $ λ h, ⟨h, hmem.2⟩ lemma eq_right_or_mem_Ioo_of_mem_Ioc {x : α} (hmem : x ∈ Ioc a b) : x = b ∨ x ∈ Ioo a b := hmem.2.eq_or_lt.imp_right $ and.intro hmem.1 lemma eq_endpoints_or_mem_Ioo_of_mem_Icc {x : α} (hmem : x ∈ Icc a b) : x = a ∨ x = b ∨ x ∈ Ioo a b := hmem.1.eq_or_gt.imp_right $ λ h, eq_right_or_mem_Ioo_of_mem_Ioc ⟨h, hmem.2⟩ lemma _root_.is_max.Ici_eq (h : is_max a) : Ici a = {a} := eq_singleton_iff_unique_mem.2 ⟨left_mem_Ici, λ b, h.eq_of_ge⟩ lemma _root_.is_min.Iic_eq (h : is_min a) : Iic a = {a} := h.to_dual.Ici_eq end partial_order section order_top @[simp] lemma Ici_top [partial_order α] [order_top α] : Ici (⊤ : α) = {⊤} := is_max_top.Ici_eq variables [preorder α] [order_top α] {a : α} @[simp] lemma Ioi_top : Ioi (⊤ : α) = ∅ := is_max_top.Ioi_eq @[simp] lemma Iic_top : Iic (⊤ : α) = univ := is_top_top.Iic_eq @[simp] lemma Icc_top : Icc a ⊤ = Ici a := by simp [← Ici_inter_Iic] @[simp] lemma Ioc_top : Ioc a ⊤ = Ioi a := by simp [← Ioi_inter_Iic] end order_top section order_bot @[simp] lemma Iic_bot [partial_order α] [order_bot α] : Iic (⊥ : α) = {⊥} := is_min_bot.Iic_eq variables [preorder α] [order_bot α] {a : α} @[simp] lemma Iio_bot : Iio (⊥ : α) = ∅ := is_min_bot.Iio_eq @[simp] lemma Ici_bot : Ici (⊥ : α) = univ := is_bot_bot.Ici_eq @[simp] lemma Icc_bot : Icc ⊥ a = Iic a := by simp [← Ici_inter_Iic] @[simp] lemma Ico_bot : Ico ⊥ a = Iio a := by simp [← Ici_inter_Iio] end order_bot lemma Icc_bot_top [partial_order α] [bounded_order α] : Icc (⊥ : α) ⊤ = univ := by simp section linear_order variables [linear_order α] {a a₁ a₂ b b₁ b₂ c d : α} lemma not_mem_Ici : c ∉ Ici a ↔ c < a := not_le lemma not_mem_Iic : c ∉ Iic b ↔ b < c := not_le lemma not_mem_Icc_of_lt (ha : c < a) : c ∉ Icc a b := not_mem_subset Icc_subset_Ici_self $ not_mem_Ici.mpr ha lemma not_mem_Icc_of_gt (hb : b < c) : c ∉ Icc a b := not_mem_subset Icc_subset_Iic_self $ not_mem_Iic.mpr hb lemma not_mem_Ico_of_lt (ha : c < a) : c ∉ Ico a b := not_mem_subset Ico_subset_Ici_self $ not_mem_Ici.mpr ha lemma not_mem_Ioc_of_gt (hb : b < c) : c ∉ Ioc a b := not_mem_subset Ioc_subset_Iic_self $ not_mem_Iic.mpr hb lemma not_mem_Ioi : c ∉ Ioi a ↔ c ≤ a := not_lt lemma not_mem_Iio : c ∉ Iio b ↔ b ≤ c := not_lt lemma not_mem_Ioc_of_le (ha : c ≤ a) : c ∉ Ioc a b := not_mem_subset Ioc_subset_Ioi_self $ not_mem_Ioi.mpr ha lemma not_mem_Ico_of_ge (hb : b ≤ c) : c ∉ Ico a b := not_mem_subset Ico_subset_Iio_self $ not_mem_Iio.mpr hb lemma not_mem_Ioo_of_le (ha : c ≤ a) : c ∉ Ioo a b := not_mem_subset Ioo_subset_Ioi_self $ not_mem_Ioi.mpr ha lemma not_mem_Ioo_of_ge (hb : b ≤ c) : c ∉ Ioo a b := not_mem_subset Ioo_subset_Iio_self $ not_mem_Iio.mpr hb @[simp] lemma compl_Iic : (Iic a)ᶜ = Ioi a := ext $ λ _, not_le @[simp] lemma compl_Ici : (Ici a)ᶜ = Iio a := ext $ λ _, not_le @[simp] lemma compl_Iio : (Iio a)ᶜ = Ici a := ext $ λ _, not_lt @[simp] lemma compl_Ioi : (Ioi a)ᶜ = Iic a := ext $ λ _, not_lt @[simp] lemma Ici_diff_Ici : Ici a \ Ici b = Ico a b := by rw [diff_eq, compl_Ici, Ici_inter_Iio] @[simp] lemma Ici_diff_Ioi : Ici a \ Ioi b = Icc a b := by rw [diff_eq, compl_Ioi, Ici_inter_Iic] @[simp] lemma Ioi_diff_Ioi : Ioi a \ Ioi b = Ioc a b := by rw [diff_eq, compl_Ioi, Ioi_inter_Iic] @[simp] lemma Ioi_diff_Ici : Ioi a \ Ici b = Ioo a b := by rw [diff_eq, compl_Ici, Ioi_inter_Iio] @[simp] lemma Iic_diff_Iic : Iic b \ Iic a = Ioc a b := by rw [diff_eq, compl_Iic, inter_comm, Ioi_inter_Iic] @[simp] lemma Iio_diff_Iic : Iio b \ Iic a = Ioo a b := by rw [diff_eq, compl_Iic, inter_comm, Ioi_inter_Iio] @[simp] lemma Iic_diff_Iio : Iic b \ Iio a = Icc a b := by rw [diff_eq, compl_Iio, inter_comm, Ici_inter_Iic] @[simp] lemma Iio_diff_Iio : Iio b \ Iio a = Ico a b := by rw [diff_eq, compl_Iio, inter_comm, Ici_inter_Iio] lemma Ico_subset_Ico_iff (h₁ : a₁ < b₁) : Ico a₁ b₁ ⊆ Ico a₂ b₂ ↔ a₂ ≤ a₁ ∧ b₁ ≤ b₂ := ⟨λ h, have a₂ ≤ a₁ ∧ a₁ < b₂ := h ⟨le_rfl, h₁⟩, ⟨this.1, le_of_not_lt $ λ h', lt_irrefl b₂ (h ⟨this.2.le, h'⟩).2⟩, λ ⟨h₁, h₂⟩, Ico_subset_Ico h₁ h₂⟩ lemma Ioc_subset_Ioc_iff (h₁ : a₁ < b₁) : Ioc a₁ b₁ ⊆ Ioc a₂ b₂ ↔ b₁ ≤ b₂ ∧ a₂ ≤ a₁ := by { convert @Ico_subset_Ico_iff αᵒᵈ _ b₁ b₂ a₁ a₂ h₁; exact (@dual_Ico α _ _ _).symm } lemma Ioo_subset_Ioo_iff [densely_ordered α] (h₁ : a₁ < b₁) : Ioo a₁ b₁ ⊆ Ioo a₂ b₂ ↔ a₂ ≤ a₁ ∧ b₁ ≤ b₂ := ⟨λ h, begin rcases exists_between h₁ with ⟨x, xa, xb⟩, split; refine le_of_not_lt (λ h', _), { have ab := (h ⟨xa, xb⟩).1.trans xb, exact lt_irrefl _ (h ⟨h', ab⟩).1 }, { have ab := xa.trans (h ⟨xa, xb⟩).2, exact lt_irrefl _ (h ⟨ab, h'⟩).2 } end, λ ⟨h₁, h₂⟩, Ioo_subset_Ioo h₁ h₂⟩ lemma Ico_eq_Ico_iff (h : a₁ < b₁ ∨ a₂ < b₂) : Ico a₁ b₁ = Ico a₂ b₂ ↔ a₁ = a₂ ∧ b₁ = b₂ := ⟨λ e, begin simp [subset.antisymm_iff] at e, simp [le_antisymm_iff], cases h; simp [Ico_subset_Ico_iff h] at e; [ rcases e with ⟨⟨h₁, h₂⟩, e'⟩, rcases e with ⟨e', ⟨h₁, h₂⟩⟩ ]; have := (Ico_subset_Ico_iff $ h₁.trans_lt $ h.trans_le h₂).1 e'; tauto end, λ ⟨h₁, h₂⟩, by rw [h₁, h₂]⟩ open_locale classical @[simp] lemma Ioi_subset_Ioi_iff : Ioi b ⊆ Ioi a ↔ a ≤ b := begin refine ⟨λ h, _, λ h, Ioi_subset_Ioi h⟩, by_contradiction ba, exact lt_irrefl _ (h (not_le.mp ba)) end @[simp] lemma Ioi_subset_Ici_iff [densely_ordered α] : Ioi b ⊆ Ici a ↔ a ≤ b := begin refine ⟨λ h, _, λ h, Ioi_subset_Ici h⟩, by_contradiction ba, obtain ⟨c, bc, ca⟩ : ∃c, b < c ∧ c < a := exists_between (not_le.mp ba), exact lt_irrefl _ (ca.trans_le (h bc)) end @[simp] lemma Iio_subset_Iio_iff : Iio a ⊆ Iio b ↔ a ≤ b := begin refine ⟨λ h, _, λ h, Iio_subset_Iio h⟩, by_contradiction ab, exact lt_irrefl _ (h (not_le.mp ab)) end @[simp] lemma Iio_subset_Iic_iff [densely_ordered α] : Iio a ⊆ Iic b ↔ a ≤ b := by rw [←diff_eq_empty, Iio_diff_Iic, Ioo_eq_empty_iff, not_lt] /-! ### Unions of adjacent intervals -/ /-! #### Two infinite intervals -/ @[simp] lemma Iic_union_Ici : Iic a ∪ Ici a = univ := eq_univ_of_forall (λ x, le_total x a) @[simp] lemma Iio_union_Ici : Iio a ∪ Ici a = univ := eq_univ_of_forall (λ x, lt_or_le x a) @[simp] lemma Iic_union_Ioi : Iic a ∪ Ioi a = univ := eq_univ_of_forall (λ x, le_or_lt x a) /-! #### A finite and an infinite interval -/ lemma Ioo_union_Ioi' (h₁ : c < b) : Ioo a b ∪ Ioi c = Ioi (min a c) := begin ext1 x, simp_rw [mem_union, mem_Ioo, mem_Ioi, min_lt_iff], by_cases hc : c < x, { tauto }, { have hxb : x < b := (le_of_not_gt hc).trans_lt h₁, tauto }, end lemma Ioo_union_Ioi (h : c < max a b) : Ioo a b ∪ Ioi c = Ioi (min a c) := begin cases le_total a b with hab hab; simp [hab] at h, { exact Ioo_union_Ioi' h }, { rw min_comm, simp [*, min_eq_left_of_lt] }, end lemma Ioi_subset_Ioo_union_Ici : Ioi a ⊆ Ioo a b ∪ Ici b := λ x hx, (lt_or_le x b).elim (λ hxb, or.inl ⟨hx, hxb⟩) (λ hxb, or.inr hxb) @[simp] lemma Ioo_union_Ici_eq_Ioi (h : a < b) : Ioo a b ∪ Ici b = Ioi a := subset.antisymm (λ x hx, hx.elim and.left h.trans_le) Ioi_subset_Ioo_union_Ici lemma Ici_subset_Ico_union_Ici : Ici a ⊆ Ico a b ∪ Ici b := λ x hx, (lt_or_le x b).elim (λ hxb, or.inl ⟨hx, hxb⟩) (λ hxb, or.inr hxb) @[simp] lemma Ico_union_Ici_eq_Ici (h : a ≤ b) : Ico a b ∪ Ici b = Ici a := subset.antisymm (λ x hx, hx.elim and.left h.trans) Ici_subset_Ico_union_Ici lemma Ico_union_Ici' (h₁ : c ≤ b) : Ico a b ∪ Ici c = Ici (min a c) := begin ext1 x, simp_rw [mem_union, mem_Ico, mem_Ici, min_le_iff], by_cases hc : c ≤ x, { tauto }, { have hxb : x < b := (lt_of_not_ge hc).trans_le h₁, tauto }, end lemma Ico_union_Ici (h : c ≤ max a b) : Ico a b ∪ Ici c = Ici (min a c) := begin cases le_total a b with hab hab; simp [hab] at h, { exact Ico_union_Ici' h }, { simp [*] }, end lemma Ioi_subset_Ioc_union_Ioi : Ioi a ⊆ Ioc a b ∪ Ioi b := λ x hx, (le_or_lt x b).elim (λ hxb, or.inl ⟨hx, hxb⟩) (λ hxb, or.inr hxb) @[simp] lemma Ioc_union_Ioi_eq_Ioi (h : a ≤ b) : Ioc a b ∪ Ioi b = Ioi a := subset.antisymm (λ x hx, hx.elim and.left h.trans_lt) Ioi_subset_Ioc_union_Ioi lemma Ioc_union_Ioi' (h₁ : c ≤ b) : Ioc a b ∪ Ioi c = Ioi (min a c) := begin ext1 x, simp_rw [mem_union, mem_Ioc, mem_Ioi, min_lt_iff], by_cases hc : c < x, { tauto }, { have hxb : x ≤ b := (le_of_not_gt hc).trans h₁, tauto }, end lemma Ioc_union_Ioi (h : c ≤ max a b) : Ioc a b ∪ Ioi c = Ioi (min a c) := begin cases le_total a b with hab hab; simp [hab] at h, { exact Ioc_union_Ioi' h }, { simp [*] }, end lemma Ici_subset_Icc_union_Ioi : Ici a ⊆ Icc a b ∪ Ioi b := λ x hx, (le_or_lt x b).elim (λ hxb, or.inl ⟨hx, hxb⟩) (λ hxb, or.inr hxb) @[simp] lemma Icc_union_Ioi_eq_Ici (h : a ≤ b) : Icc a b ∪ Ioi b = Ici a := subset.antisymm (λ x hx, hx.elim and.left $ λ hx', h.trans $ le_of_lt hx') Ici_subset_Icc_union_Ioi lemma Ioi_subset_Ioc_union_Ici : Ioi a ⊆ Ioc a b ∪ Ici b := subset.trans Ioi_subset_Ioo_union_Ici (union_subset_union_left _ Ioo_subset_Ioc_self) @[simp] lemma Ioc_union_Ici_eq_Ioi (h : a < b) : Ioc a b ∪ Ici b = Ioi a := subset.antisymm (λ x hx, hx.elim and.left h.trans_le) Ioi_subset_Ioc_union_Ici lemma Ici_subset_Icc_union_Ici : Ici a ⊆ Icc a b ∪ Ici b := subset.trans Ici_subset_Ico_union_Ici (union_subset_union_left _ Ico_subset_Icc_self) @[simp] lemma Icc_union_Ici_eq_Ici (h : a ≤ b) : Icc a b ∪ Ici b = Ici a := subset.antisymm (λ x hx, hx.elim and.left h.trans) Ici_subset_Icc_union_Ici lemma Icc_union_Ici' (h₁ : c ≤ b) : Icc a b ∪ Ici c = Ici (min a c) := begin ext1 x, simp_rw [mem_union, mem_Icc, mem_Ici, min_le_iff], by_cases hc : c ≤ x, { tauto }, { have hxb : x ≤ b := (le_of_not_ge hc).trans h₁, tauto }, end lemma Icc_union_Ici (h : c ≤ max a b) : Icc a b ∪ Ici c = Ici (min a c) := begin cases le_or_lt a b with hab hab; simp [hab] at h, { exact Icc_union_Ici' h }, { cases h, { simp [*] }, { have hca : c ≤ a := h.trans hab.le, simp [*] } }, end /-! #### An infinite and a finite interval -/ lemma Iic_subset_Iio_union_Icc : Iic b ⊆ Iio a ∪ Icc a b := λ x hx, (lt_or_le x a).elim (λ hxa, or.inl hxa) (λ hxa, or.inr ⟨hxa, hx⟩) @[simp] lemma Iio_union_Icc_eq_Iic (h : a ≤ b) : Iio a ∪ Icc a b = Iic b := subset.antisymm (λ x hx, hx.elim (λ hx, (le_of_lt hx).trans h) and.right) Iic_subset_Iio_union_Icc lemma Iio_subset_Iio_union_Ico : Iio b ⊆ Iio a ∪ Ico a b := λ x hx, (lt_or_le x a).elim (λ hxa, or.inl hxa) (λ hxa, or.inr ⟨hxa, hx⟩) @[simp] lemma Iio_union_Ico_eq_Iio (h : a ≤ b) : Iio a ∪ Ico a b = Iio b := subset.antisymm (λ x hx, hx.elim (λ hx', lt_of_lt_of_le hx' h) and.right) Iio_subset_Iio_union_Ico lemma Iio_union_Ico' (h₁ : c ≤ b) : Iio b ∪ Ico c d = Iio (max b d) := begin ext1 x, simp_rw [mem_union, mem_Iio, mem_Ico, lt_max_iff], by_cases hc : c ≤ x, { tauto }, { have hxb : x < b := (lt_of_not_ge hc).trans_le h₁, tauto }, end lemma Iio_union_Ico (h : min c d ≤ b) : Iio b ∪ Ico c d = Iio (max b d) := begin cases le_total c d with hcd hcd; simp [hcd] at h, { exact Iio_union_Ico' h }, { simp [*] }, end lemma Iic_subset_Iic_union_Ioc : Iic b ⊆ Iic a ∪ Ioc a b := λ x hx, (le_or_lt x a).elim (λ hxa, or.inl hxa) (λ hxa, or.inr ⟨hxa, hx⟩) @[simp] lemma Iic_union_Ioc_eq_Iic (h : a ≤ b) : Iic a ∪ Ioc a b = Iic b := subset.antisymm (λ x hx, hx.elim (λ hx', le_trans hx' h) and.right) Iic_subset_Iic_union_Ioc lemma Iic_union_Ioc' (h₁ : c < b) : Iic b ∪ Ioc c d = Iic (max b d) := begin ext1 x, simp_rw [mem_union, mem_Iic, mem_Ioc, le_max_iff], by_cases hc : c < x, { tauto }, { have hxb : x ≤ b := (le_of_not_gt hc).trans h₁.le, tauto }, end lemma Iic_union_Ioc (h : min c d < b) : Iic b ∪ Ioc c d = Iic (max b d) := begin cases le_total c d with hcd hcd; simp [hcd] at h, { exact Iic_union_Ioc' h }, { rw max_comm, simp [*, max_eq_right_of_lt h] }, end lemma Iio_subset_Iic_union_Ioo : Iio b ⊆ Iic a ∪ Ioo a b := λ x hx, (le_or_lt x a).elim (λ hxa, or.inl hxa) (λ hxa, or.inr ⟨hxa, hx⟩) @[simp] lemma Iic_union_Ioo_eq_Iio (h : a < b) : Iic a ∪ Ioo a b = Iio b := subset.antisymm (λ x hx, hx.elim (λ hx', lt_of_le_of_lt hx' h) and.right) Iio_subset_Iic_union_Ioo lemma Iio_union_Ioo' (h₁ : c < b) : Iio b ∪ Ioo c d = Iio (max b d) := begin ext x, cases lt_or_le x b with hba hba, { simp [hba, h₁] }, { simp only [mem_Iio, mem_union_eq, mem_Ioo, lt_max_iff], refine or_congr iff.rfl ⟨and.right, _⟩, exact λ h₂, ⟨h₁.trans_le hba, h₂⟩ }, end lemma Iio_union_Ioo (h : min c d < b) : Iio b ∪ Ioo c d = Iio (max b d) := begin cases le_total c d with hcd hcd; simp [hcd] at h, { exact Iio_union_Ioo' h }, { rw max_comm, simp [*, max_eq_right_of_lt h] }, end lemma Iic_subset_Iic_union_Icc : Iic b ⊆ Iic a ∪ Icc a b := subset.trans Iic_subset_Iic_union_Ioc (union_subset_union_right _ Ioc_subset_Icc_self) @[simp] lemma Iic_union_Icc_eq_Iic (h : a ≤ b) : Iic a ∪ Icc a b = Iic b := subset.antisymm (λ x hx, hx.elim (λ hx', le_trans hx' h) and.right) Iic_subset_Iic_union_Icc lemma Iic_union_Icc' (h₁ : c ≤ b) : Iic b ∪ Icc c d = Iic (max b d) := begin ext1 x, simp_rw [mem_union, mem_Iic, mem_Icc, le_max_iff], by_cases hc : c ≤ x, { tauto }, { have hxb : x ≤ b := (le_of_not_ge hc).trans h₁, tauto }, end lemma Iic_union_Icc (h : min c d ≤ b) : Iic b ∪ Icc c d = Iic (max b d) := begin cases le_or_lt c d with hcd hcd; simp [hcd] at h, { exact Iic_union_Icc' h }, { cases h, { have hdb : d ≤ b := hcd.le.trans h, simp [*] }, { simp [*] } }, end lemma Iio_subset_Iic_union_Ico : Iio b ⊆ Iic a ∪ Ico a b := subset.trans Iio_subset_Iic_union_Ioo (union_subset_union_right _ Ioo_subset_Ico_self) @[simp] lemma Iic_union_Ico_eq_Iio (h : a < b) : Iic a ∪ Ico a b = Iio b := subset.antisymm (λ x hx, hx.elim (λ hx', lt_of_le_of_lt hx' h) and.right) Iio_subset_Iic_union_Ico /-! #### Two finite intervals, `I?o` and `Ic?` -/ lemma Ioo_subset_Ioo_union_Ico : Ioo a c ⊆ Ioo a b ∪ Ico b c := λ x hx, (lt_or_le x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩) @[simp] lemma Ioo_union_Ico_eq_Ioo (h₁ : a < b) (h₂ : b ≤ c) : Ioo a b ∪ Ico b c = Ioo a c := subset.antisymm (λ x hx, hx.elim (λ hx, ⟨hx.1, hx.2.trans_le h₂⟩) (λ hx, ⟨h₁.trans_le hx.1, hx.2⟩)) Ioo_subset_Ioo_union_Ico lemma Ico_subset_Ico_union_Ico : Ico a c ⊆ Ico a b ∪ Ico b c := λ x hx, (lt_or_le x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩) @[simp] lemma Ico_union_Ico_eq_Ico (h₁ : a ≤ b) (h₂ : b ≤ c) : Ico a b ∪ Ico b c = Ico a c := subset.antisymm (λ x hx, hx.elim (λ hx, ⟨hx.1, hx.2.trans_le h₂⟩) (λ hx, ⟨h₁.trans hx.1, hx.2⟩)) Ico_subset_Ico_union_Ico lemma Ico_union_Ico' (h₁ : c ≤ b) (h₂ : a ≤ d) : Ico a b ∪ Ico c d = Ico (min a c) (max b d) := begin ext1 x, simp_rw [mem_union, mem_Ico, min_le_iff, lt_max_iff], by_cases hc : c ≤ x; by_cases hd : x < d, { tauto }, { have hax : a ≤ x := h₂.trans (le_of_not_gt hd), tauto }, { have hxb : x < b := (lt_of_not_ge hc).trans_le h₁, tauto }, { tauto }, end lemma Ico_union_Ico (h₁ : min a b ≤ max c d) (h₂ : min c d ≤ max a b) : Ico a b ∪ Ico c d = Ico (min a c) (max b d) := begin cases le_total a b with hab hab; cases le_total c d with hcd hcd; simp [hab, hcd] at h₁ h₂, { exact Ico_union_Ico' h₂ h₁ }, all_goals { simp [*] }, end lemma Icc_subset_Ico_union_Icc : Icc a c ⊆ Ico a b ∪ Icc b c := λ x hx, (lt_or_le x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩) @[simp] lemma Ico_union_Icc_eq_Icc (h₁ : a ≤ b) (h₂ : b ≤ c) : Ico a b ∪ Icc b c = Icc a c := subset.antisymm (λ x hx, hx.elim (λ hx, ⟨hx.1, hx.2.le.trans h₂⟩) (λ hx, ⟨h₁.trans hx.1, hx.2⟩)) Icc_subset_Ico_union_Icc lemma Ioc_subset_Ioo_union_Icc : Ioc a c ⊆ Ioo a b ∪ Icc b c := λ x hx, (lt_or_le x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩) @[simp] lemma Ioo_union_Icc_eq_Ioc (h₁ : a < b) (h₂ : b ≤ c) : Ioo a b ∪ Icc b c = Ioc a c := subset.antisymm (λ x hx, hx.elim (λ hx, ⟨hx.1, hx.2.le.trans h₂⟩) (λ hx, ⟨h₁.trans_le hx.1, hx.2⟩)) Ioc_subset_Ioo_union_Icc /-! #### Two finite intervals, `I?c` and `Io?` -/ lemma Ioo_subset_Ioc_union_Ioo : Ioo a c ⊆ Ioc a b ∪ Ioo b c := λ x hx, (le_or_lt x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩) @[simp] lemma Ioc_union_Ioo_eq_Ioo (h₁ : a ≤ b) (h₂ : b < c) : Ioc a b ∪ Ioo b c = Ioo a c := subset.antisymm (λ x hx, hx.elim (λ hx, ⟨hx.1, hx.2.trans_lt h₂⟩) (λ hx, ⟨h₁.trans_lt hx.1, hx.2⟩)) Ioo_subset_Ioc_union_Ioo lemma Ico_subset_Icc_union_Ioo : Ico a c ⊆ Icc a b ∪ Ioo b c := λ x hx, (le_or_lt x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩) @[simp] lemma Icc_union_Ioo_eq_Ico (h₁ : a ≤ b) (h₂ : b < c) : Icc a b ∪ Ioo b c = Ico a c := subset.antisymm (λ x hx, hx.elim (λ hx, ⟨hx.1, hx.2.trans_lt h₂⟩) (λ hx, ⟨h₁.trans hx.1.le, hx.2⟩)) Ico_subset_Icc_union_Ioo lemma Icc_subset_Icc_union_Ioc : Icc a c ⊆ Icc a b ∪ Ioc b c := λ x hx, (le_or_lt x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩) @[simp] lemma Icc_union_Ioc_eq_Icc (h₁ : a ≤ b) (h₂ : b ≤ c) : Icc a b ∪ Ioc b c = Icc a c := subset.antisymm (λ x hx, hx.elim (λ hx, ⟨hx.1, hx.2.trans h₂⟩) (λ hx, ⟨h₁.trans hx.1.le, hx.2⟩)) Icc_subset_Icc_union_Ioc lemma Ioc_subset_Ioc_union_Ioc : Ioc a c ⊆ Ioc a b ∪ Ioc b c := λ x hx, (le_or_lt x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩) @[simp] lemma Ioc_union_Ioc_eq_Ioc (h₁ : a ≤ b) (h₂ : b ≤ c) : Ioc a b ∪ Ioc b c = Ioc a c := subset.antisymm (λ x hx, hx.elim (λ hx, ⟨hx.1, hx.2.trans h₂⟩) (λ hx, ⟨h₁.trans_lt hx.1, hx.2⟩)) Ioc_subset_Ioc_union_Ioc lemma Ioc_union_Ioc' (h₁ : c ≤ b) (h₂ : a ≤ d) : Ioc a b ∪ Ioc c d = Ioc (min a c) (max b d) := begin ext1 x, simp_rw [mem_union, mem_Ioc, min_lt_iff, le_max_iff], by_cases hc : c < x; by_cases hd : x ≤ d, { tauto }, { have hax : a < x := h₂.trans_lt (lt_of_not_ge hd), tauto }, { have hxb : x ≤ b := (le_of_not_gt hc).trans h₁, tauto }, { tauto }, end lemma Ioc_union_Ioc (h₁ : min a b ≤ max c d) (h₂ : min c d ≤ max a b) : Ioc a b ∪ Ioc c d = Ioc (min a c) (max b d) := begin cases le_total a b with hab hab; cases le_total c d with hcd hcd; simp [hab, hcd] at h₁ h₂, { exact Ioc_union_Ioc' h₂ h₁ }, all_goals { simp [*] }, end /-! #### Two finite intervals with a common point -/ lemma Ioo_subset_Ioc_union_Ico : Ioo a c ⊆ Ioc a b ∪ Ico b c := subset.trans Ioo_subset_Ioc_union_Ioo (union_subset_union_right _ Ioo_subset_Ico_self) @[simp] lemma Ioc_union_Ico_eq_Ioo (h₁ : a < b) (h₂ : b < c) : Ioc a b ∪ Ico b c = Ioo a c := subset.antisymm (λ x hx, hx.elim (λ hx', ⟨hx'.1, hx'.2.trans_lt h₂⟩) (λ hx', ⟨h₁.trans_le hx'.1, hx'.2⟩)) Ioo_subset_Ioc_union_Ico lemma Ico_subset_Icc_union_Ico : Ico a c ⊆ Icc a b ∪ Ico b c := subset.trans Ico_subset_Icc_union_Ioo (union_subset_union_right _ Ioo_subset_Ico_self) @[simp] lemma Icc_union_Ico_eq_Ico (h₁ : a ≤ b) (h₂ : b < c) : Icc a b ∪ Ico b c = Ico a c := subset.antisymm (λ x hx, hx.elim (λ hx, ⟨hx.1, hx.2.trans_lt h₂⟩) (λ hx, ⟨h₁.trans hx.1, hx.2⟩)) Ico_subset_Icc_union_Ico lemma Icc_subset_Icc_union_Icc : Icc a c ⊆ Icc a b ∪ Icc b c := subset.trans Icc_subset_Icc_union_Ioc (union_subset_union_right _ Ioc_subset_Icc_self) @[simp] lemma Icc_union_Icc_eq_Icc (h₁ : a ≤ b) (h₂ : b ≤ c) : Icc a b ∪ Icc b c = Icc a c := subset.antisymm (λ x hx, hx.elim (λ hx, ⟨hx.1, hx.2.trans h₂⟩) (λ hx, ⟨h₁.trans hx.1, hx.2⟩)) Icc_subset_Icc_union_Icc lemma Icc_union_Icc' (h₁ : c ≤ b) (h₂ : a ≤ d) : Icc a b ∪ Icc c d = Icc (min a c) (max b d) := begin ext1 x, simp_rw [mem_union, mem_Icc, min_le_iff, le_max_iff], by_cases hc : c ≤ x; by_cases hd : x ≤ d, { tauto }, { have hax : a ≤ x := h₂.trans (le_of_not_ge hd), tauto }, { have hxb : x ≤ b := (le_of_not_ge hc).trans h₁, tauto }, { tauto } end /-- We cannot replace `<` by `≤` in the hypotheses. Otherwise for `b < a = d < c` the l.h.s. is `∅` and the r.h.s. is `{a}`. -/ lemma Icc_union_Icc (h₁ : min a b < max c d) (h₂ : min c d < max a b) : Icc a b ∪ Icc c d = Icc (min a c) (max b d) := begin cases le_or_lt a b with hab hab; cases le_or_lt c d with hcd hcd; simp only [min_eq_left, min_eq_right, max_eq_left, max_eq_right, min_eq_left_of_lt, min_eq_right_of_lt, max_eq_left_of_lt, max_eq_right_of_lt, hab, hcd] at h₁ h₂, { exact Icc_union_Icc' h₂.le h₁.le }, all_goals { simp [*, min_eq_left_of_lt, max_eq_left_of_lt, min_eq_right_of_lt, max_eq_right_of_lt] }, end lemma Ioc_subset_Ioc_union_Icc : Ioc a c ⊆ Ioc a b ∪ Icc b c := subset.trans Ioc_subset_Ioc_union_Ioc (union_subset_union_right _ Ioc_subset_Icc_self) @[simp] lemma Ioc_union_Icc_eq_Ioc (h₁ : a < b) (h₂ : b ≤ c) : Ioc a b ∪ Icc b c = Ioc a c := subset.antisymm (λ x hx, hx.elim (λ hx, ⟨hx.1, hx.2.trans h₂⟩) (λ hx, ⟨h₁.trans_le hx.1, hx.2⟩)) Ioc_subset_Ioc_union_Icc lemma Ioo_union_Ioo' (h₁ : c < b) (h₂ : a < d) : Ioo a b ∪ Ioo c d = Ioo (min a c) (max b d) := begin ext1 x, simp_rw [mem_union, mem_Ioo, min_lt_iff, lt_max_iff], by_cases hc : c < x; by_cases hd : x < d, { tauto }, { have hax : a < x := h₂.trans_le (le_of_not_lt hd), tauto }, { have hxb : x < b := (le_of_not_lt hc).trans_lt h₁, tauto }, { tauto } end lemma Ioo_union_Ioo (h₁ : min a b < max c d) (h₂ : min c d < max a b) : Ioo a b ∪ Ioo c d = Ioo (min a c) (max b d) := begin cases le_total a b with hab hab; cases le_total c d with hcd hcd; simp only [min_eq_left, min_eq_right, max_eq_left, max_eq_right, hab, hcd] at h₁ h₂, { exact Ioo_union_Ioo' h₂ h₁ }, all_goals { simp [*, min_eq_left_of_lt, min_eq_right_of_lt, max_eq_left_of_lt, max_eq_right_of_lt, le_of_lt h₂, le_of_lt h₁] }, end end linear_order section lattice section inf variables [semilattice_inf α] @[simp] lemma Iic_inter_Iic {a b : α} : Iic a ∩ Iic b = Iic (a ⊓ b) := by { ext x, simp [Iic] } @[simp] lemma Ioc_inter_Iic (a b c : α) : Ioc a b ∩ Iic c = Ioc a (b ⊓ c) := by rw [← Ioi_inter_Iic, ← Ioi_inter_Iic, inter_assoc, Iic_inter_Iic] end inf section sup variables [semilattice_sup α] @[simp] lemma Ici_inter_Ici {a b : α} : Ici a ∩ Ici b = Ici (a ⊔ b) := by { ext x, simp [Ici] } @[simp] lemma Ico_inter_Ici (a b c : α) : Ico a b ∩ Ici c = Ico (a ⊔ c) b := by rw [← Ici_inter_Iio, ← Ici_inter_Iio, ← Ici_inter_Ici, inter_right_comm] end sup section both variables [lattice α] {a b c a₁ a₂ b₁ b₂ : α} lemma Icc_inter_Icc : Icc a₁ b₁ ∩ Icc a₂ b₂ = Icc (a₁ ⊔ a₂) (b₁ ⊓ b₂) := by simp only [Ici_inter_Iic.symm, Ici_inter_Ici.symm, Iic_inter_Iic.symm]; ac_refl @[simp] lemma Icc_inter_Icc_eq_singleton (hab : a ≤ b) (hbc : b ≤ c) : Icc a b ∩ Icc b c = {b} := by rw [Icc_inter_Icc, sup_of_le_right hab, inf_of_le_left hbc, Icc_self] end both end lattice section linear_order variables [linear_order α] {a a₁ a₂ b b₁ b₂ c d : α} @[simp] lemma Ioi_inter_Ioi : Ioi a ∩ Ioi b = Ioi (a ⊔ b) := ext $ λ _, sup_lt_iff.symm @[simp] lemma Iio_inter_Iio : Iio a ∩ Iio b = Iio (a ⊓ b) := ext $ λ _, lt_inf_iff.symm lemma Ico_inter_Ico : Ico a₁ b₁ ∩ Ico a₂ b₂ = Ico (a₁ ⊔ a₂) (b₁ ⊓ b₂) := by simp only [Ici_inter_Iio.symm, Ici_inter_Ici.symm, Iio_inter_Iio.symm]; ac_refl lemma Ioc_inter_Ioc : Ioc a₁ b₁ ∩ Ioc a₂ b₂ = Ioc (a₁ ⊔ a₂) (b₁ ⊓ b₂) := by simp only [Ioi_inter_Iic.symm, Ioi_inter_Ioi.symm, Iic_inter_Iic.symm]; ac_refl lemma Ioo_inter_Ioo : Ioo a₁ b₁ ∩ Ioo a₂ b₂ = Ioo (a₁ ⊔ a₂) (b₁ ⊓ b₂) := by simp only [Ioi_inter_Iio.symm, Ioi_inter_Ioi.symm, Iio_inter_Iio.symm]; ac_refl lemma Ioc_inter_Ioo_of_left_lt (h : b₁ < b₂) : Ioc a₁ b₁ ∩ Ioo a₂ b₂ = Ioc (max a₁ a₂) b₁ := ext $ λ x, by simp [and_assoc, @and.left_comm (x ≤ _), and_iff_left_iff_imp.2 (λ h', lt_of_le_of_lt h' h)] lemma Ioc_inter_Ioo_of_right_le (h : b₂ ≤ b₁) : Ioc a₁ b₁ ∩ Ioo a₂ b₂ = Ioo (max a₁ a₂) b₂ := ext $ λ x, by simp [and_assoc, @and.left_comm (x ≤ _), and_iff_right_iff_imp.2 (λ h', ((le_of_lt h').trans h))] lemma Ioo_inter_Ioc_of_left_le (h : b₁ ≤ b₂) : Ioo a₁ b₁ ∩ Ioc a₂ b₂ = Ioo (max a₁ a₂) b₁ := by rw [inter_comm, Ioc_inter_Ioo_of_right_le h, max_comm] lemma Ioo_inter_Ioc_of_right_lt (h : b₂ < b₁) : Ioo a₁ b₁ ∩ Ioc a₂ b₂ = Ioc (max a₁ a₂) b₂ := by rw [inter_comm, Ioc_inter_Ioo_of_left_lt h, max_comm] @[simp] lemma Ico_diff_Iio : Ico a b \ Iio c = Ico (max a c) b := by rw [diff_eq, compl_Iio, Ico_inter_Ici, sup_eq_max] @[simp] lemma Ioc_diff_Ioi : Ioc a b \ Ioi c = Ioc a (min b c) := ext $ by simp [iff_def] {contextual:=tt} @[simp] lemma Ioc_inter_Ioi : Ioc a b ∩ Ioi c = Ioc (a ⊔ c) b := by rw [← Ioi_inter_Iic, inter_assoc, inter_comm, inter_assoc, Ioi_inter_Ioi, inter_comm, Ioi_inter_Iic, sup_comm] @[simp] lemma Ico_inter_Iio : Ico a b ∩ Iio c = Ico a (min b c) := ext $ by simp [iff_def] {contextual:=tt} @[simp] lemma Ioc_diff_Iic : Ioc a b \ Iic c = Ioc (max a c) b := by rw [diff_eq, compl_Iic, Ioc_inter_Ioi, sup_eq_max] @[simp] lemma Ioc_union_Ioc_right : Ioc a b ∪ Ioc a c = Ioc a (max b c) := by rw [Ioc_union_Ioc, min_self]; exact (min_le_left _ _).trans (le_max_left _ _) @[simp] lemma Ioc_union_Ioc_left : Ioc a c ∪ Ioc b c = Ioc (min a b) c := by rw [Ioc_union_Ioc, max_self]; exact (min_le_right _ _).trans (le_max_right _ _) @[simp] lemma Ioc_union_Ioc_symm : Ioc a b ∪ Ioc b a = Ioc (min a b) (max a b) := by { rw max_comm, apply Ioc_union_Ioc; rw max_comm; exact min_le_max } @[simp] lemma Ioc_union_Ioc_union_Ioc_cycle : Ioc a b ∪ Ioc b c ∪ Ioc c a = Ioc (min a (min b c)) (max a (max b c)) := begin rw [Ioc_union_Ioc, Ioc_union_Ioc], ac_refl, all_goals { solve_by_elim [min_le_of_left_le, min_le_of_right_le, le_max_of_le_left, le_max_of_le_right, le_refl] { max_depth := 5 }} end end linear_order /-! ### Closed intervals in `α × β` -/ section prod variables [preorder α] [preorder β] @[simp] lemma Iic_prod_Iic (a : α) (b : β) : Iic a ×ˢ Iic b = Iic (a, b) := rfl @[simp] lemma Ici_prod_Ici (a : α) (b : β) : Ici a ×ˢ Ici b = Ici (a, b) := rfl lemma Ici_prod_eq (a : α × β) : Ici a = Ici a.1 ×ˢ Ici a.2 := rfl lemma Iic_prod_eq (a : α × β) : Iic a = Iic a.1 ×ˢ Iic a.2 := rfl @[simp] lemma Icc_prod_Icc (a₁ a₂ : α) (b₁ b₂ : β) : Icc a₁ a₂ ×ˢ Icc b₁ b₂ = Icc (a₁, b₁) (a₂, b₂) := by { ext ⟨x, y⟩, simp [and.assoc, and_comm, and.left_comm] } lemma Icc_prod_eq (a b : α × β) : Icc a b = Icc a.1 b.1 ×ˢ Icc a.2 b.2 := by simp end prod /-! ### Lemmas about membership of arithmetic operations -/ section ordered_comm_group variables [ordered_comm_group α] {a b c d : α} /-! `inv_mem_Ixx_iff`, `sub_mem_Ixx_iff` -/ @[to_additive] lemma inv_mem_Icc_iff : a⁻¹ ∈ set.Icc c d ↔ a ∈ set.Icc (d⁻¹) (c⁻¹) := (and_comm _ _).trans $ and_congr inv_le' le_inv' @[to_additive] lemma inv_mem_Ico_iff : a⁻¹ ∈ set.Ico c d ↔ a ∈ set.Ioc (d⁻¹) (c⁻¹) := (and_comm _ _).trans $ and_congr inv_lt' le_inv' @[to_additive] lemma inv_mem_Ioc_iff : a⁻¹ ∈ set.Ioc c d ↔ a ∈ set.Ico (d⁻¹) (c⁻¹) := (and_comm _ _).trans $ and_congr inv_le' lt_inv' @[to_additive] lemma inv_mem_Ioo_iff : a⁻¹ ∈ set.Ioo c d ↔ a ∈ set.Ioo (d⁻¹) (c⁻¹) := (and_comm _ _).trans $ and_congr inv_lt' lt_inv' end ordered_comm_group section ordered_add_comm_group variables [ordered_add_comm_group α] {a b c d : α} /-! `add_mem_Ixx_iff_left` -/ lemma add_mem_Icc_iff_left : a + b ∈ set.Icc c d ↔ a ∈ set.Icc (c - b) (d - b) := (and_congr sub_le_iff_le_add le_sub_iff_add_le).symm lemma add_mem_Ico_iff_left : a + b ∈ set.Ico c d ↔ a ∈ set.Ico (c - b) (d - b) := (and_congr sub_le_iff_le_add lt_sub_iff_add_lt).symm lemma add_mem_Ioc_iff_left : a + b ∈ set.Ioc c d ↔ a ∈ set.Ioc (c - b) (d - b) := (and_congr sub_lt_iff_lt_add le_sub_iff_add_le).symm lemma add_mem_Ioo_iff_left : a + b ∈ set.Ioo c d ↔ a ∈ set.Ioo (c - b) (d - b) := (and_congr sub_lt_iff_lt_add lt_sub_iff_add_lt).symm /-! `add_mem_Ixx_iff_right` -/ lemma add_mem_Icc_iff_right : a + b ∈ set.Icc c d ↔ b ∈ set.Icc (c - a) (d - a) := (and_congr sub_le_iff_le_add' le_sub_iff_add_le').symm lemma add_mem_Ico_iff_right : a + b ∈ set.Ico c d ↔ b ∈ set.Ico (c - a) (d - a) := (and_congr sub_le_iff_le_add' lt_sub_iff_add_lt').symm lemma add_mem_Ioc_iff_right : a + b ∈ set.Ioc c d ↔ b ∈ set.Ioc (c - a) (d - a) := (and_congr sub_lt_iff_lt_add' le_sub_iff_add_le').symm lemma add_mem_Ioo_iff_right : a + b ∈ set.Ioo c d ↔ b ∈ set.Ioo (c - a) (d - a) := (and_congr sub_lt_iff_lt_add' lt_sub_iff_add_lt').symm /-! `sub_mem_Ixx_iff_left` -/ lemma sub_mem_Icc_iff_left : a - b ∈ set.Icc c d ↔ a ∈ set.Icc (c + b) (d + b) := and_congr le_sub_iff_add_le sub_le_iff_le_add lemma sub_mem_Ico_iff_left : a - b ∈ set.Ico c d ↔ a ∈ set.Ico (c + b) (d + b) := and_congr le_sub_iff_add_le sub_lt_iff_lt_add lemma sub_mem_Ioc_iff_left : a - b ∈ set.Ioc c d ↔ a ∈ set.Ioc (c + b) (d + b) := and_congr lt_sub_iff_add_lt sub_le_iff_le_add lemma sub_mem_Ioo_iff_left : a - b ∈ set.Ioo c d ↔ a ∈ set.Ioo (c + b) (d + b) := and_congr lt_sub_iff_add_lt sub_lt_iff_lt_add /-! `sub_mem_Ixx_iff_right` -/ lemma sub_mem_Icc_iff_right : a - b ∈ set.Icc c d ↔ b ∈ set.Icc (a - d) (a - c) := (and_comm _ _).trans $ and_congr sub_le le_sub lemma sub_mem_Ico_iff_right : a - b ∈ set.Ico c d ↔ b ∈ set.Ioc (a - d) (a - c) := (and_comm _ _).trans $ and_congr sub_lt le_sub lemma sub_mem_Ioc_iff_right : a - b ∈ set.Ioc c d ↔ b ∈ set.Ico (a - d) (a - c) := (and_comm _ _).trans $ and_congr sub_le lt_sub lemma sub_mem_Ioo_iff_right : a - b ∈ set.Ioo c d ↔ b ∈ set.Ioo (a - d) (a - c) := (and_comm _ _).trans $ and_congr sub_lt lt_sub -- I think that symmetric intervals deserve attention and API: they arise all the time, -- for instance when considering metric balls in `ℝ`. lemma mem_Icc_iff_abs_le {R : Type*} [linear_ordered_add_comm_group R] {x y z : R} : |x - y| ≤ z ↔ y ∈ Icc (x - z) (x + z) := abs_le.trans $ (and_comm _ _).trans $ and_congr sub_le neg_le_sub_iff_le_add end ordered_add_comm_group section linear_ordered_add_comm_group variables [linear_ordered_add_comm_group α] /-- If we remove a smaller interval from a larger, the result is nonempty -/ lemma nonempty_Ico_sdiff {x dx y dy : α} (h : dy < dx) (hx : 0 < dx) : nonempty ↥(Ico x (x + dx) \ Ico y (y + dy)) := begin cases lt_or_le x y with h' h', { use x, simp [*, not_le.2 h'] }, { use max x (x + dy), simp [*, le_refl] } end end linear_ordered_add_comm_group end set open set namespace order_iso section preorder variables [preorder α] [preorder β] @[simp] lemma preimage_Iic (e : α ≃o β) (b : β) : e ⁻¹' (Iic b) = Iic (e.symm b) := by { ext x, simp [← e.le_iff_le] } @[simp] lemma preimage_Ici (e : α ≃o β) (b : β) : e ⁻¹' (Ici b) = Ici (e.symm b) := by { ext x, simp [← e.le_iff_le] } @[simp] lemma preimage_Iio (e : α ≃o β) (b : β) : e ⁻¹' (Iio b) = Iio (e.symm b) := by { ext x, simp [← e.lt_iff_lt] } @[simp] lemma preimage_Ioi (e : α ≃o β) (b : β) : e ⁻¹' (Ioi b) = Ioi (e.symm b) := by { ext x, simp [← e.lt_iff_lt] } @[simp] lemma preimage_Icc (e : α ≃o β) (a b : β) : e ⁻¹' (Icc a b) = Icc (e.symm a) (e.symm b) := by simp [← Ici_inter_Iic] @[simp] lemma preimage_Ico (e : α ≃o β) (a b : β) : e ⁻¹' (Ico a b) = Ico (e.symm a) (e.symm b) := by simp [← Ici_inter_Iio] @[simp] lemma preimage_Ioc (e : α ≃o β) (a b : β) : e ⁻¹' (Ioc a b) = Ioc (e.symm a) (e.symm b) := by simp [← Ioi_inter_Iic] @[simp] lemma preimage_Ioo (e : α ≃o β) (a b : β) : e ⁻¹' (Ioo a b) = Ioo (e.symm a) (e.symm b) := by simp [← Ioi_inter_Iio] @[simp] lemma image_Iic (e : α ≃o β) (a : α) : e '' (Iic a) = Iic (e a) := by rw [e.image_eq_preimage, e.symm.preimage_Iic, e.symm_symm] @[simp] lemma image_Ici (e : α ≃o β) (a : α) : e '' (Ici a) = Ici (e a) := e.dual.image_Iic a @[simp] lemma image_Iio (e : α ≃o β) (a : α) : e '' (Iio a) = Iio (e a) := by rw [e.image_eq_preimage, e.symm.preimage_Iio, e.symm_symm] @[simp] lemma image_Ioi (e : α ≃o β) (a : α) : e '' (Ioi a) = Ioi (e a) := e.dual.image_Iio a @[simp] lemma image_Ioo (e : α ≃o β) (a b : α) : e '' (Ioo a b) = Ioo (e a) (e b) := by rw [e.image_eq_preimage, e.symm.preimage_Ioo, e.symm_symm] @[simp] lemma image_Ioc (e : α ≃o β) (a b : α) : e '' (Ioc a b) = Ioc (e a) (e b) := by rw [e.image_eq_preimage, e.symm.preimage_Ioc, e.symm_symm] @[simp] lemma image_Ico (e : α ≃o β) (a b : α) : e '' (Ico a b) = Ico (e a) (e b) := by rw [e.image_eq_preimage, e.symm.preimage_Ico, e.symm_symm] @[simp] lemma image_Icc (e : α ≃o β) (a b : α) : e '' (Icc a b) = Icc (e a) (e b) := by rw [e.image_eq_preimage, e.symm.preimage_Icc, e.symm_symm] end preorder /-- Order isomorphism between `Iic (⊤ : α)` and `α` when `α` has a top element -/ def Iic_top [preorder α] [order_top α] : set.Iic (⊤ : α) ≃o α := { map_rel_iff' := λ x y, by refl, .. (@equiv.subtype_univ_equiv α (set.Iic (⊤ : α)) (λ x, le_top)), } /-- Order isomorphism between `Ici (⊥ : α)` and `α` when `α` has a bottom element -/ def Ici_bot [preorder α] [order_bot α] : set.Ici (⊥ : α) ≃o α := { map_rel_iff' := λ x y, by refl, .. (@equiv.subtype_univ_equiv α (set.Ici (⊥ : α)) (λ x, bot_le)) } end order_iso /-! ### Lemmas about intervals in dense orders -/ section dense variables (α) [preorder α] [densely_ordered α] {x y : α} instance : no_min_order (set.Ioo x y) := ⟨λ ⟨a, ha₁, ha₂⟩, begin rcases exists_between ha₁ with ⟨b, hb₁, hb₂⟩, exact ⟨⟨b, hb₁, hb₂.trans ha₂⟩, hb₂⟩ end⟩ instance : no_min_order (set.Ioc x y) := ⟨λ ⟨a, ha₁, ha₂⟩, begin rcases exists_between ha₁ with ⟨b, hb₁, hb₂⟩, exact ⟨⟨b, hb₁, hb₂.le.trans ha₂⟩, hb₂⟩ end⟩ instance : no_min_order (set.Ioi x) := ⟨λ ⟨a, ha⟩, begin rcases exists_between ha with ⟨b, hb₁, hb₂⟩, exact ⟨⟨b, hb₁⟩, hb₂⟩ end⟩ instance : no_max_order (set.Ioo x y) := ⟨λ ⟨a, ha₁, ha₂⟩, begin rcases exists_between ha₂ with ⟨b, hb₁, hb₂⟩, exact ⟨⟨b, ha₁.trans hb₁, hb₂⟩, hb₁⟩ end⟩ instance : no_max_order (set.Ico x y) := ⟨λ ⟨a, ha₁, ha₂⟩, begin rcases exists_between ha₂ with ⟨b, hb₁, hb₂⟩, exact ⟨⟨b, ha₁.trans hb₁.le, hb₂⟩, hb₁⟩ end⟩ instance : no_max_order (set.Iio x) := ⟨λ ⟨a, ha⟩, begin rcases exists_between ha with ⟨b, hb₁, hb₂⟩, exact ⟨⟨b, hb₂⟩, hb₁⟩ end⟩ end dense
0f9660b4ec78ce95a61df6b2bd1087ab6d1dd74b
2a70b774d16dbdf5a533432ee0ebab6838df0948
/_target/deps/mathlib/src/topology/metric_space/hausdorff_distance.lean
91d952148d530130fe4d8dd7d4460ac3713f71a4
[ "Apache-2.0" ]
permissive
hjvromen/lewis
40b035973df7c77ebf927afab7878c76d05ff758
105b675f73630f028ad5d890897a51b3c1146fb0
refs/heads/master
1,677,944,636,343
1,676,555,301,000
1,676,555,301,000
327,553,599
0
0
null
null
null
null
UTF-8
Lean
false
false
33,616
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Sébastien Gouëzel -/ import topology.metric_space.isometry import topology.instances.ennreal /-! # Hausdorff distance The Hausdorff distance on subsets of a metric (or emetric) space. Given two subsets `s` and `t` of a metric space, their Hausdorff distance is the smallest `d` such that any point `s` is within `d` of a point in `t`, and conversely. This quantity is often infinite (think of `s` bounded and `t` unbounded), and therefore better expressed in the setting of emetric spaces. ## Main definitions This files introduces: * `inf_edist x s`, the infimum edistance of a point `x` to a set `s` in an emetric space * `Hausdorff_edist s t`, the Hausdorff edistance of two sets in an emetric space * Versions of these notions on metric spaces, called respectively `inf_dist` and `Hausdorff_dist`. -/ noncomputable theory open_locale classical nnreal universes u v w open classical set function topological_space filter namespace emetric section inf_edist open_locale ennreal variables {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {x y : α} {s t : set α} {Φ : α → β} /-! ### Distance of a point to a set as a function into `ennreal`. -/ /-- The minimal edistance of a point to a set -/ def inf_edist (x : α) (s : set α) : ennreal := Inf ((edist x) '' s) @[simp] lemma inf_edist_empty : inf_edist x ∅ = ∞ := by unfold inf_edist; simp /-- The edist to a union is the minimum of the edists -/ @[simp] lemma inf_edist_union : inf_edist x (s ∪ t) = inf_edist x s ⊓ inf_edist x t := by simp [inf_edist, image_union, Inf_union] /-- The edist to a singleton is the edistance to the single point of this singleton -/ @[simp] lemma inf_edist_singleton : inf_edist x {y} = edist x y := by simp [inf_edist] /-- The edist to a set is bounded above by the edist to any of its points -/ lemma inf_edist_le_edist_of_mem (h : y ∈ s) : inf_edist x s ≤ edist x y := Inf_le ((mem_image _ _ _).2 ⟨y, h, by refl⟩) /-- If a point `x` belongs to `s`, then its edist to `s` vanishes -/ lemma inf_edist_zero_of_mem (h : x ∈ s) : inf_edist x s = 0 := nonpos_iff_eq_zero.1 $ @edist_self _ _ x ▸ inf_edist_le_edist_of_mem h /-- The edist is monotonous with respect to inclusion -/ lemma inf_edist_le_inf_edist_of_subset (h : s ⊆ t) : inf_edist x t ≤ inf_edist x s := Inf_le_Inf (image_subset _ h) /-- If the edist to a set is `< r`, there exists a point in the set at edistance `< r` -/ lemma exists_edist_lt_of_inf_edist_lt {r : ennreal} (h : inf_edist x s < r) : ∃y∈s, edist x y < r := let ⟨t, ⟨ht, tr⟩⟩ := Inf_lt_iff.1 h in let ⟨y, ⟨ys, hy⟩⟩ := (mem_image _ _ _).1 ht in ⟨y, ys, by rwa ← hy at tr⟩ /-- The edist of `x` to `s` is bounded by the sum of the edist of `y` to `s` and the edist from `x` to `y` -/ lemma inf_edist_le_inf_edist_add_edist : inf_edist x s ≤ inf_edist y s + edist x y := begin have : ∀z ∈ s, Inf (edist x '' s) ≤ edist y z + edist x y := λz hz, calc Inf (edist x '' s) ≤ edist x z : Inf_le ((mem_image _ _ _).2 ⟨z, hz, by refl⟩) ... ≤ edist x y + edist y z : edist_triangle _ _ _ ... = edist y z + edist x y : add_comm _ _, have : (λz, z + edist x y) (Inf (edist y '' s)) = Inf ((λz, z + edist x y) '' (edist y '' s)), { refine map_Inf_of_continuous_at_of_monotone _ _ (by simp), { exact continuous_at_id.add continuous_at_const }, { assume a b h, simp, apply add_le_add_right h _ }}, simp only [inf_edist] at this, rw [inf_edist, inf_edist, this, ← image_comp], simpa only [and_imp, function.comp_app, le_Inf_iff, exists_imp_distrib, ball_image_iff] end /-- The edist to a set depends continuously on the point -/ lemma continuous_inf_edist : continuous (λx, inf_edist x s) := continuous_of_le_add_edist 1 (by simp) $ by simp only [one_mul, inf_edist_le_inf_edist_add_edist, forall_2_true_iff] /-- The edist to a set and to its closure coincide -/ lemma inf_edist_closure : inf_edist x (closure s) = inf_edist x s := begin refine le_antisymm (inf_edist_le_inf_edist_of_subset subset_closure) _, refine ennreal.le_of_forall_epsilon_le (λε εpos h, _), have εpos' : (0 : ennreal) < ε := by simpa, have : inf_edist x (closure s) < inf_edist x (closure s) + ε/2 := ennreal.lt_add_right h (ennreal.half_pos εpos'), rcases exists_edist_lt_of_inf_edist_lt this with ⟨y, ycs, hy⟩, -- y : α, ycs : y ∈ closure s, hy : edist x y < inf_edist x (closure s) + ↑ε / 2 rcases emetric.mem_closure_iff.1 ycs (ε/2) (ennreal.half_pos εpos') with ⟨z, zs, dyz⟩, -- z : α, zs : z ∈ s, dyz : edist y z < ↑ε / 2 calc inf_edist x s ≤ edist x z : inf_edist_le_edist_of_mem zs ... ≤ edist x y + edist y z : edist_triangle _ _ _ ... ≤ (inf_edist x (closure s) + ε / 2) + (ε/2) : add_le_add (le_of_lt hy) (le_of_lt dyz) ... = inf_edist x (closure s) + ↑ε : by rw [add_assoc, ennreal.add_halves] end /-- A point belongs to the closure of `s` iff its infimum edistance to this set vanishes -/ lemma mem_closure_iff_inf_edist_zero : x ∈ closure s ↔ inf_edist x s = 0 := ⟨λh, by rw ← inf_edist_closure; exact inf_edist_zero_of_mem h, λh, emetric.mem_closure_iff.2 $ λε εpos, exists_edist_lt_of_inf_edist_lt (by rwa h)⟩ /-- Given a closed set `s`, a point belongs to `s` iff its infimum edistance to this set vanishes -/ lemma mem_iff_ind_edist_zero_of_closed (h : is_closed s) : x ∈ s ↔ inf_edist x s = 0 := begin convert ← mem_closure_iff_inf_edist_zero, exact h.closure_eq end /-- The infimum edistance is invariant under isometries -/ lemma inf_edist_image (hΦ : isometry Φ) : inf_edist (Φ x) (Φ '' t) = inf_edist x t := begin simp only [inf_edist], apply congr_arg, ext b, split, { assume hb, rcases (mem_image _ _ _).1 hb with ⟨y, ⟨hy, hy'⟩⟩, rcases (mem_image _ _ _).1 hy with ⟨z, ⟨hz, hz'⟩⟩, rw [← hy', ← hz', hΦ x z], exact mem_image_of_mem _ hz }, { assume hb, rcases (mem_image _ _ _).1 hb with ⟨y, ⟨hy, hy'⟩⟩, rw [← hy', ← hΦ x y], exact mem_image_of_mem _ (mem_image_of_mem _ hy) } end end inf_edist --section /-! ### The Hausdorff distance as a function into `ennreal`. -/ /-- The Hausdorff edistance between two sets is the smallest `r` such that each set is contained in the `r`-neighborhood of the other one -/ def Hausdorff_edist {α : Type u} [emetric_space α] (s t : set α) : ennreal := Sup ((λx, inf_edist x t) '' s) ⊔ Sup ((λx, inf_edist x s) '' t) lemma Hausdorff_edist_def {α : Type u} [emetric_space α] (s t : set α) : Hausdorff_edist s t = Sup ((λx, inf_edist x t) '' s) ⊔ Sup ((λx, inf_edist x s) '' t) := rfl attribute [irreducible] Hausdorff_edist section Hausdorff_edist open_locale ennreal variables {α : Type u} {β : Type v} [emetric_space α] [emetric_space β] {x y : α} {s t u : set α} {Φ : α → β} /-- The Hausdorff edistance of a set to itself vanishes -/ @[simp] lemma Hausdorff_edist_self : Hausdorff_edist s s = 0 := begin erw [Hausdorff_edist_def, sup_idem, ← le_bot_iff], apply Sup_le _, simp [le_bot_iff, inf_edist_zero_of_mem, le_refl] {contextual := tt}, end /-- The Haudorff edistances of `s` to `t` and of `t` to `s` coincide -/ lemma Hausdorff_edist_comm : Hausdorff_edist s t = Hausdorff_edist t s := by unfold Hausdorff_edist; apply sup_comm /-- Bounding the Hausdorff edistance by bounding the edistance of any point in each set to the other set -/ lemma Hausdorff_edist_le_of_inf_edist {r : ennreal} (H1 : ∀x ∈ s, inf_edist x t ≤ r) (H2 : ∀x ∈ t, inf_edist x s ≤ r) : Hausdorff_edist s t ≤ r := begin simp only [Hausdorff_edist, -mem_image, set.ball_image_iff, Sup_le_iff, sup_le_iff], exact ⟨H1, H2⟩ end /-- Bounding the Hausdorff edistance by exhibiting, for any point in each set, another point in the other set at controlled distance -/ lemma Hausdorff_edist_le_of_mem_edist {r : ennreal} (H1 : ∀x ∈ s, ∃y ∈ t, edist x y ≤ r) (H2 : ∀x ∈ t, ∃y ∈ s, edist x y ≤ r) : Hausdorff_edist s t ≤ r := begin refine Hausdorff_edist_le_of_inf_edist _ _, { assume x xs, rcases H1 x xs with ⟨y, yt, hy⟩, exact le_trans (inf_edist_le_edist_of_mem yt) hy }, { assume x xt, rcases H2 x xt with ⟨y, ys, hy⟩, exact le_trans (inf_edist_le_edist_of_mem ys) hy } end /-- The distance to a set is controlled by the Hausdorff distance -/ lemma inf_edist_le_Hausdorff_edist_of_mem (h : x ∈ s) : inf_edist x t ≤ Hausdorff_edist s t := begin rw Hausdorff_edist_def, refine le_trans (le_Sup _) le_sup_left, exact mem_image_of_mem _ h end /-- If the Hausdorff distance is `<r`, then any point in one of the sets has a corresponding point at distance `<r` in the other set -/ lemma exists_edist_lt_of_Hausdorff_edist_lt {r : ennreal} (h : x ∈ s) (H : Hausdorff_edist s t < r) : ∃y∈t, edist x y < r := exists_edist_lt_of_inf_edist_lt $ calc inf_edist x t ≤ Sup ((λx, inf_edist x t) '' s) : le_Sup (mem_image_of_mem _ h) ... ≤ Sup ((λx, inf_edist x t) '' s) ⊔ Sup ((λx, inf_edist x s) '' t) : le_sup_left ... < r : by rwa Hausdorff_edist_def at H /-- The distance from `x` to `s` or `t` is controlled in terms of the Hausdorff distance between `s` and `t` -/ lemma inf_edist_le_inf_edist_add_Hausdorff_edist : inf_edist x t ≤ inf_edist x s + Hausdorff_edist s t := ennreal.le_of_forall_epsilon_le $ λε εpos h, begin have εpos' : (0 : ennreal) < ε := by simpa, have : inf_edist x s < inf_edist x s + ε/2 := ennreal.lt_add_right (ennreal.add_lt_top.1 h).1 (ennreal.half_pos εpos'), rcases exists_edist_lt_of_inf_edist_lt this with ⟨y, ys, dxy⟩, -- y : α, ys : y ∈ s, dxy : edist x y < inf_edist x s + ↑ε / 2 have : Hausdorff_edist s t < Hausdorff_edist s t + ε/2 := ennreal.lt_add_right (ennreal.add_lt_top.1 h).2 (ennreal.half_pos εpos'), rcases exists_edist_lt_of_Hausdorff_edist_lt ys this with ⟨z, zt, dyz⟩, -- z : α, zt : z ∈ t, dyz : edist y z < Hausdorff_edist s t + ↑ε / 2 calc inf_edist x t ≤ edist x z : inf_edist_le_edist_of_mem zt ... ≤ edist x y + edist y z : edist_triangle _ _ _ ... ≤ (inf_edist x s + ε/2) + (Hausdorff_edist s t + ε/2) : add_le_add (le_of_lt dxy) (le_of_lt dyz) ... = inf_edist x s + Hausdorff_edist s t + ε : by simp [ennreal.add_halves, add_comm, add_left_comm] end /-- The Hausdorff edistance is invariant under eisometries -/ lemma Hausdorff_edist_image (h : isometry Φ) : Hausdorff_edist (Φ '' s) (Φ '' t) = Hausdorff_edist s t := begin unfold Hausdorff_edist, congr, { ext b, split, { assume hb, rcases (mem_image _ _ _ ).1 hb with ⟨y, ⟨hy, hy'⟩⟩, rcases (mem_image _ _ _ ).1 hy with ⟨z, ⟨hz, hz'⟩⟩, rw [← hy', ← hz', inf_edist_image h], exact mem_image_of_mem _ hz }, { assume hb, rcases (mem_image _ _ _ ).1 hb with ⟨y, ⟨hy, hy'⟩⟩, rw [← hy', ← inf_edist_image h], exact mem_image_of_mem _ (mem_image_of_mem _ hy) }}, { ext b, split, { assume hb, rcases (mem_image _ _ _ ).1 hb with ⟨y, ⟨hy, hy'⟩⟩, rcases (mem_image _ _ _ ).1 hy with ⟨z, ⟨hz, hz'⟩⟩, rw [← hy', ← hz', inf_edist_image h], exact mem_image_of_mem _ hz }, { assume hb, rcases (mem_image _ _ _ ).1 hb with ⟨y, ⟨hy, hy'⟩⟩, rw [← hy', ← inf_edist_image h], exact mem_image_of_mem _ (mem_image_of_mem _ hy) }} end /-- The Hausdorff distance is controlled by the diameter of the union -/ lemma Hausdorff_edist_le_ediam (hs : s.nonempty) (ht : t.nonempty) : Hausdorff_edist s t ≤ diam (s ∪ t) := begin rcases hs with ⟨x, xs⟩, rcases ht with ⟨y, yt⟩, refine Hausdorff_edist_le_of_mem_edist _ _, { exact λz hz, ⟨y, yt, edist_le_diam_of_mem (subset_union_left _ _ hz) (subset_union_right _ _ yt)⟩ }, { exact λz hz, ⟨x, xs, edist_le_diam_of_mem (subset_union_right _ _ hz) (subset_union_left _ _ xs)⟩ } end /-- The Hausdorff distance satisfies the triangular inequality -/ lemma Hausdorff_edist_triangle : Hausdorff_edist s u ≤ Hausdorff_edist s t + Hausdorff_edist t u := begin rw Hausdorff_edist_def, simp only [and_imp, set.mem_image, Sup_le_iff, exists_imp_distrib, sup_le_iff, -mem_image, set.ball_image_iff], split, show ∀x ∈ s, inf_edist x u ≤ Hausdorff_edist s t + Hausdorff_edist t u, from λx xs, calc inf_edist x u ≤ inf_edist x t + Hausdorff_edist t u : inf_edist_le_inf_edist_add_Hausdorff_edist ... ≤ Hausdorff_edist s t + Hausdorff_edist t u : add_le_add_right (inf_edist_le_Hausdorff_edist_of_mem xs) _, show ∀x ∈ u, inf_edist x s ≤ Hausdorff_edist s t + Hausdorff_edist t u, from λx xu, calc inf_edist x s ≤ inf_edist x t + Hausdorff_edist t s : inf_edist_le_inf_edist_add_Hausdorff_edist ... ≤ Hausdorff_edist u t + Hausdorff_edist t s : add_le_add_right (inf_edist_le_Hausdorff_edist_of_mem xu) _ ... = Hausdorff_edist s t + Hausdorff_edist t u : by simp [Hausdorff_edist_comm, add_comm] end /-- The Hausdorff edistance between a set and its closure vanishes -/ @[simp, priority 1100] lemma Hausdorff_edist_self_closure : Hausdorff_edist s (closure s) = 0 := begin erw ← le_bot_iff, simp only [Hausdorff_edist, inf_edist_closure, -nonpos_iff_eq_zero, and_imp, set.mem_image, Sup_le_iff, exists_imp_distrib, sup_le_iff, set.ball_image_iff, ennreal.bot_eq_zero, -mem_image], simp only [inf_edist_zero_of_mem, mem_closure_iff_inf_edist_zero, le_refl, and_self, forall_true_iff] {contextual := tt} end /-- Replacing a set by its closure does not change the Hausdorff edistance. -/ @[simp] lemma Hausdorff_edist_closure₁ : Hausdorff_edist (closure s) t = Hausdorff_edist s t := begin refine le_antisymm _ _, { calc _ ≤ Hausdorff_edist (closure s) s + Hausdorff_edist s t : Hausdorff_edist_triangle ... = Hausdorff_edist s t : by simp [Hausdorff_edist_comm] }, { calc _ ≤ Hausdorff_edist s (closure s) + Hausdorff_edist (closure s) t : Hausdorff_edist_triangle ... = Hausdorff_edist (closure s) t : by simp } end /-- Replacing a set by its closure does not change the Hausdorff edistance. -/ @[simp] lemma Hausdorff_edist_closure₂ : Hausdorff_edist s (closure t) = Hausdorff_edist s t := by simp [@Hausdorff_edist_comm _ _ s _] /-- The Hausdorff edistance between sets or their closures is the same -/ @[simp] lemma Hausdorff_edist_closure : Hausdorff_edist (closure s) (closure t) = Hausdorff_edist s t := by simp /-- Two sets are at zero Hausdorff edistance if and only if they have the same closure -/ lemma Hausdorff_edist_zero_iff_closure_eq_closure : Hausdorff_edist s t = 0 ↔ closure s = closure t := ⟨begin assume h, refine subset.antisymm _ _, { have : s ⊆ closure t := λx xs, mem_closure_iff_inf_edist_zero.2 $ begin erw ← le_bot_iff, have := @inf_edist_le_Hausdorff_edist_of_mem _ _ _ _ t xs, rwa h at this, end, by rw ← @closure_closure _ _ t; exact closure_mono this }, { have : t ⊆ closure s := λx xt, mem_closure_iff_inf_edist_zero.2 $ begin erw ← le_bot_iff, have := @inf_edist_le_Hausdorff_edist_of_mem _ _ _ _ s xt, rw Hausdorff_edist_comm at h, rwa h at this, end, by rw ← @closure_closure _ _ s; exact closure_mono this } end, λh, by rw [← Hausdorff_edist_closure, h, Hausdorff_edist_self]⟩ /-- Two closed sets are at zero Hausdorff edistance if and only if they coincide -/ lemma Hausdorff_edist_zero_iff_eq_of_closed (hs : is_closed s) (ht : is_closed t) : Hausdorff_edist s t = 0 ↔ s = t := by rw [Hausdorff_edist_zero_iff_closure_eq_closure, hs.closure_eq, ht.closure_eq] /-- The Haudorff edistance to the empty set is infinite -/ lemma Hausdorff_edist_empty (ne : s.nonempty) : Hausdorff_edist s ∅ = ∞ := begin rcases ne with ⟨x, xs⟩, have : inf_edist x ∅ ≤ Hausdorff_edist s ∅ := inf_edist_le_Hausdorff_edist_of_mem xs, simpa using this, end /-- If a set is at finite Hausdorff edistance of a nonempty set, it is nonempty -/ lemma nonempty_of_Hausdorff_edist_ne_top (hs : s.nonempty) (fin : Hausdorff_edist s t ≠ ⊤) : t.nonempty := t.eq_empty_or_nonempty.elim (λ ht, (fin $ ht.symm ▸ Hausdorff_edist_empty hs).elim) id lemma empty_or_nonempty_of_Hausdorff_edist_ne_top (fin : Hausdorff_edist s t ≠ ⊤) : s = ∅ ∧ t = ∅ ∨ s.nonempty ∧ t.nonempty := begin cases s.eq_empty_or_nonempty with hs hs, { cases t.eq_empty_or_nonempty with ht ht, { exact or.inl ⟨hs, ht⟩ }, { rw Hausdorff_edist_comm at fin, exact or.inr ⟨nonempty_of_Hausdorff_edist_ne_top ht fin, ht⟩ } }, { exact or.inr ⟨hs, nonempty_of_Hausdorff_edist_ne_top hs fin⟩ } end end Hausdorff_edist -- section end emetric --namespace /-! Now, we turn to the same notions in metric spaces. To avoid the difficulties related to `Inf` and `Sup` on `ℝ` (which is only conditionally complete), we use the notions in `ennreal` formulated in terms of the edistance, and coerce them to `ℝ`. Then their properties follow readily from the corresponding properties in `ennreal`, modulo some tedious rewriting of inequalities from one to the other. -/ namespace metric section variables {α : Type u} {β : Type v} [metric_space α] [metric_space β] {s t u : set α} {x y : α} {Φ : α → β} open emetric /-! ### Distance of a point to a set as a function into `ℝ`. -/ /-- The minimal distance of a point to a set -/ def inf_dist (x : α) (s : set α) : ℝ := ennreal.to_real (inf_edist x s) /-- the minimal distance is always nonnegative -/ lemma inf_dist_nonneg : 0 ≤ inf_dist x s := by simp [inf_dist] /-- the minimal distance to the empty set is 0 (if you want to have the more reasonable value ∞ instead, use `inf_edist`, which takes values in ennreal) -/ @[simp] lemma inf_dist_empty : inf_dist x ∅ = 0 := by simp [inf_dist] /-- In a metric space, the minimal edistance to a nonempty set is finite -/ lemma inf_edist_ne_top (h : s.nonempty) : inf_edist x s ≠ ⊤ := begin rcases h with ⟨y, hy⟩, apply lt_top_iff_ne_top.1, calc inf_edist x s ≤ edist x y : inf_edist_le_edist_of_mem hy ... < ⊤ : lt_top_iff_ne_top.2 (edist_ne_top _ _) end /-- The minimal distance of a point to a set containing it vanishes -/ lemma inf_dist_zero_of_mem (h : x ∈ s) : inf_dist x s = 0 := by simp [inf_edist_zero_of_mem h, inf_dist] /-- The minimal distance to a singleton is the distance to the unique point in this singleton -/ @[simp] lemma inf_dist_singleton : inf_dist x {y} = dist x y := by simp [inf_dist, inf_edist, dist_edist] /-- The minimal distance to a set is bounded by the distance to any point in this set -/ lemma inf_dist_le_dist_of_mem (h : y ∈ s) : inf_dist x s ≤ dist x y := begin rw [dist_edist, inf_dist, ennreal.to_real_le_to_real (inf_edist_ne_top ⟨_, h⟩) (edist_ne_top _ _)], exact inf_edist_le_edist_of_mem h end /-- The minimal distance is monotonous with respect to inclusion -/ lemma inf_dist_le_inf_dist_of_subset (h : s ⊆ t) (hs : s.nonempty) : inf_dist x t ≤ inf_dist x s := begin have ht : t.nonempty := hs.mono h, rw [inf_dist, inf_dist, ennreal.to_real_le_to_real (inf_edist_ne_top ht) (inf_edist_ne_top hs)], exact inf_edist_le_inf_edist_of_subset h end /-- If the minimal distance to a set is `<r`, there exists a point in this set at distance `<r` -/ lemma exists_dist_lt_of_inf_dist_lt {r : real} (h : inf_dist x s < r) (hs : s.nonempty) : ∃y∈s, dist x y < r := begin have rpos : 0 < r := lt_of_le_of_lt inf_dist_nonneg h, have : inf_edist x s < ennreal.of_real r, { rwa [inf_dist, ← ennreal.to_real_of_real (le_of_lt rpos), ennreal.to_real_lt_to_real (inf_edist_ne_top hs)] at h, simp }, rcases exists_edist_lt_of_inf_edist_lt this with ⟨y, ys, hy⟩, rw [edist_dist, ennreal.of_real_lt_of_real_iff rpos] at hy, exact ⟨y, ys, hy⟩, end /-- The minimal distance from `x` to `s` is bounded by the distance from `y` to `s`, modulo the distance between `x` and `y` -/ lemma inf_dist_le_inf_dist_add_dist : inf_dist x s ≤ inf_dist y s + dist x y := begin cases s.eq_empty_or_nonempty with hs hs, { by simp [hs, dist_nonneg] }, { rw [inf_dist, inf_dist, dist_edist, ← ennreal.to_real_add (inf_edist_ne_top hs) (edist_ne_top _ _), ennreal.to_real_le_to_real (inf_edist_ne_top hs)], { apply inf_edist_le_inf_edist_add_edist }, { simp [ennreal.add_eq_top, inf_edist_ne_top hs, edist_ne_top] }} end variable (s) /-- The minimal distance to a set is Lipschitz in point with constant 1 -/ lemma lipschitz_inf_dist_pt : lipschitz_with 1 (λx, inf_dist x s) := lipschitz_with.of_le_add $ λ x y, inf_dist_le_inf_dist_add_dist /-- The minimal distance to a set is uniformly continuous in point -/ lemma uniform_continuous_inf_dist_pt : uniform_continuous (λx, inf_dist x s) := (lipschitz_inf_dist_pt s).uniform_continuous /-- The minimal distance to a set is continuous in point -/ lemma continuous_inf_dist_pt : continuous (λx, inf_dist x s) := (uniform_continuous_inf_dist_pt s).continuous variable {s} /-- The minimal distance to a set and its closure coincide -/ lemma inf_dist_eq_closure : inf_dist x (closure s) = inf_dist x s := by simp [inf_dist, inf_edist_closure] /-- A point belongs to the closure of `s` iff its infimum distance to this set vanishes -/ lemma mem_closure_iff_inf_dist_zero (h : s.nonempty) : x ∈ closure s ↔ inf_dist x s = 0 := by simp [mem_closure_iff_inf_edist_zero, inf_dist, ennreal.to_real_eq_zero_iff, inf_edist_ne_top h] /-- Given a closed set `s`, a point belongs to `s` iff its infimum distance to this set vanishes -/ lemma mem_iff_inf_dist_zero_of_closed (h : is_closed s) (hs : s.nonempty) : x ∈ s ↔ inf_dist x s = 0 := begin have := @mem_closure_iff_inf_dist_zero _ _ s x hs, rwa h.closure_eq at this end /-- The infimum distance is invariant under isometries -/ lemma inf_dist_image (hΦ : isometry Φ) : inf_dist (Φ x) (Φ '' t) = inf_dist x t := by simp [inf_dist, inf_edist_image hΦ] /-! ### Distance of a point to a set as a function into `ℝ≥0`. -/ /-- The minimal distance of a point to a set as a `ℝ≥0` -/ def inf_nndist (x : α) (s : set α) : ℝ≥0 := ennreal.to_nnreal (inf_edist x s) @[simp] lemma coe_inf_nndist : (inf_nndist x s : ℝ) = inf_dist x s := rfl /-- The minimal distance to a set (as `ℝ≥0`) is Lipschitz in point with constant 1 -/ lemma lipschitz_inf_nndist_pt (s : set α) : lipschitz_with 1 (λx, inf_nndist x s) := lipschitz_with.of_le_add $ λ x y, inf_dist_le_inf_dist_add_dist /-- The minimal distance to a set (as `ℝ≥0`) is uniformly continuous in point -/ lemma uniform_continuous_inf_nndist_pt (s : set α) : uniform_continuous (λx, inf_nndist x s) := (lipschitz_inf_nndist_pt s).uniform_continuous /-- The minimal distance to a set (as `ℝ≥0`) is continuous in point -/ lemma continuous_inf_nndist_pt (s : set α) : continuous (λx, inf_nndist x s) := (uniform_continuous_inf_nndist_pt s).continuous /-! ### The Hausdorff distance as a function into `ℝ`. -/ /-- The Hausdorff distance between two sets is the smallest nonnegative `r` such that each set is included in the `r`-neighborhood of the other. If there is no such `r`, it is defined to be `0`, arbitrarily -/ def Hausdorff_dist (s t : set α) : ℝ := ennreal.to_real (Hausdorff_edist s t) /-- The Hausdorff distance is nonnegative -/ lemma Hausdorff_dist_nonneg : 0 ≤ Hausdorff_dist s t := by simp [Hausdorff_dist] /-- If two sets are nonempty and bounded in a metric space, they are at finite Hausdorff edistance -/ lemma Hausdorff_edist_ne_top_of_nonempty_of_bounded (hs : s.nonempty) (ht : t.nonempty) (bs : bounded s) (bt : bounded t) : Hausdorff_edist s t ≠ ⊤ := begin rcases hs with ⟨cs, hcs⟩, rcases ht with ⟨ct, hct⟩, rcases (bounded_iff_subset_ball ct).1 bs with ⟨rs, hrs⟩, rcases (bounded_iff_subset_ball cs).1 bt with ⟨rt, hrt⟩, have : Hausdorff_edist s t ≤ ennreal.of_real (max rs rt), { apply Hausdorff_edist_le_of_mem_edist, { assume x xs, existsi [ct, hct], have : dist x ct ≤ max rs rt := le_trans (hrs xs) (le_max_left _ _), rwa [edist_dist, ennreal.of_real_le_of_real_iff], exact le_trans dist_nonneg this }, { assume x xt, existsi [cs, hcs], have : dist x cs ≤ max rs rt := le_trans (hrt xt) (le_max_right _ _), rwa [edist_dist, ennreal.of_real_le_of_real_iff], exact le_trans dist_nonneg this }}, exact ennreal.lt_top_iff_ne_top.1 (lt_of_le_of_lt this (by simp [lt_top_iff_ne_top])) end /-- The Hausdorff distance between a set and itself is zero -/ @[simp] lemma Hausdorff_dist_self_zero : Hausdorff_dist s s = 0 := by simp [Hausdorff_dist] /-- The Hausdorff distance from `s` to `t` and from `t` to `s` coincide -/ lemma Hausdorff_dist_comm : Hausdorff_dist s t = Hausdorff_dist t s := by simp [Hausdorff_dist, Hausdorff_edist_comm] /-- The Hausdorff distance to the empty set vanishes (if you want to have the more reasonable value ∞ instead, use `Hausdorff_edist`, which takes values in ennreal) -/ @[simp] lemma Hausdorff_dist_empty : Hausdorff_dist s ∅ = 0 := begin cases s.eq_empty_or_nonempty with h h, { simp [h] }, { simp [Hausdorff_dist, Hausdorff_edist_empty h] } end /-- The Hausdorff distance to the empty set vanishes (if you want to have the more reasonable value ∞ instead, use `Hausdorff_edist`, which takes values in ennreal) -/ @[simp] lemma Hausdorff_dist_empty' : Hausdorff_dist ∅ s = 0 := by simp [Hausdorff_dist_comm] /-- Bounding the Hausdorff distance by bounding the distance of any point in each set to the other set -/ lemma Hausdorff_dist_le_of_inf_dist {r : ℝ} (hr : 0 ≤ r) (H1 : ∀x ∈ s, inf_dist x t ≤ r) (H2 : ∀x ∈ t, inf_dist x s ≤ r) : Hausdorff_dist s t ≤ r := begin by_cases h1 : Hausdorff_edist s t = ⊤, by rwa [Hausdorff_dist, h1, ennreal.top_to_real], cases s.eq_empty_or_nonempty with hs hs, by rwa [hs, Hausdorff_dist_empty'], cases t.eq_empty_or_nonempty with ht ht, by rwa [ht, Hausdorff_dist_empty], have : Hausdorff_edist s t ≤ ennreal.of_real r, { apply Hausdorff_edist_le_of_inf_edist _ _, { assume x hx, have I := H1 x hx, rwa [inf_dist, ← ennreal.to_real_of_real hr, ennreal.to_real_le_to_real (inf_edist_ne_top ht) ennreal.of_real_ne_top] at I }, { assume x hx, have I := H2 x hx, rwa [inf_dist, ← ennreal.to_real_of_real hr, ennreal.to_real_le_to_real (inf_edist_ne_top hs) ennreal.of_real_ne_top] at I }}, rwa [Hausdorff_dist, ← ennreal.to_real_of_real hr, ennreal.to_real_le_to_real h1 ennreal.of_real_ne_top] end /-- Bounding the Hausdorff distance by exhibiting, for any point in each set, another point in the other set at controlled distance -/ lemma Hausdorff_dist_le_of_mem_dist {r : ℝ} (hr : 0 ≤ r) (H1 : ∀x ∈ s, ∃y ∈ t, dist x y ≤ r) (H2 : ∀x ∈ t, ∃y ∈ s, dist x y ≤ r) : Hausdorff_dist s t ≤ r := begin apply Hausdorff_dist_le_of_inf_dist hr, { assume x xs, rcases H1 x xs with ⟨y, yt, hy⟩, exact le_trans (inf_dist_le_dist_of_mem yt) hy }, { assume x xt, rcases H2 x xt with ⟨y, ys, hy⟩, exact le_trans (inf_dist_le_dist_of_mem ys) hy } end /-- The Hausdorff distance is controlled by the diameter of the union -/ lemma Hausdorff_dist_le_diam (hs : s.nonempty) (bs : bounded s) (ht : t.nonempty) (bt : bounded t) : Hausdorff_dist s t ≤ diam (s ∪ t) := begin rcases hs with ⟨x, xs⟩, rcases ht with ⟨y, yt⟩, refine Hausdorff_dist_le_of_mem_dist diam_nonneg _ _, { exact λz hz, ⟨y, yt, dist_le_diam_of_mem (bounded_union.2 ⟨bs, bt⟩) (subset_union_left _ _ hz) (subset_union_right _ _ yt)⟩ }, { exact λz hz, ⟨x, xs, dist_le_diam_of_mem (bounded_union.2 ⟨bs, bt⟩) (subset_union_right _ _ hz) (subset_union_left _ _ xs)⟩ } end /-- The distance to a set is controlled by the Hausdorff distance -/ lemma inf_dist_le_Hausdorff_dist_of_mem (hx : x ∈ s) (fin : Hausdorff_edist s t ≠ ⊤) : inf_dist x t ≤ Hausdorff_dist s t := begin have ht : t.nonempty := nonempty_of_Hausdorff_edist_ne_top ⟨x, hx⟩ fin, rw [Hausdorff_dist, inf_dist, ennreal.to_real_le_to_real (inf_edist_ne_top ht) fin], exact inf_edist_le_Hausdorff_edist_of_mem hx end /-- If the Hausdorff distance is `<r`, then any point in one of the sets is at distance `<r` of a point in the other set -/ lemma exists_dist_lt_of_Hausdorff_dist_lt {r : ℝ} (h : x ∈ s) (H : Hausdorff_dist s t < r) (fin : Hausdorff_edist s t ≠ ⊤) : ∃y∈t, dist x y < r := begin have r0 : 0 < r := lt_of_le_of_lt (Hausdorff_dist_nonneg) H, have : Hausdorff_edist s t < ennreal.of_real r, by rwa [Hausdorff_dist, ← ennreal.to_real_of_real (le_of_lt r0), ennreal.to_real_lt_to_real fin (ennreal.of_real_ne_top)] at H, rcases exists_edist_lt_of_Hausdorff_edist_lt h this with ⟨y, hy, yr⟩, rw [edist_dist, ennreal.of_real_lt_of_real_iff r0] at yr, exact ⟨y, hy, yr⟩ end /-- If the Hausdorff distance is `<r`, then any point in one of the sets is at distance `<r` of a point in the other set -/ lemma exists_dist_lt_of_Hausdorff_dist_lt' {r : ℝ} (h : y ∈ t) (H : Hausdorff_dist s t < r) (fin : Hausdorff_edist s t ≠ ⊤) : ∃x∈s, dist x y < r := begin rw Hausdorff_dist_comm at H, rw Hausdorff_edist_comm at fin, simpa [dist_comm] using exists_dist_lt_of_Hausdorff_dist_lt h H fin end /-- The infimum distance to `s` and `t` are the same, up to the Hausdorff distance between `s` and `t` -/ lemma inf_dist_le_inf_dist_add_Hausdorff_dist (fin : Hausdorff_edist s t ≠ ⊤) : inf_dist x t ≤ inf_dist x s + Hausdorff_dist s t := begin rcases empty_or_nonempty_of_Hausdorff_edist_ne_top fin with ⟨hs,ht⟩|⟨hs,ht⟩, { simp only [hs, ht, Hausdorff_dist_empty, inf_dist_empty, zero_add] }, rw [inf_dist, inf_dist, Hausdorff_dist, ← ennreal.to_real_add (inf_edist_ne_top hs) fin, ennreal.to_real_le_to_real (inf_edist_ne_top ht)], { exact inf_edist_le_inf_edist_add_Hausdorff_edist }, { exact ennreal.add_ne_top.2 ⟨inf_edist_ne_top hs, fin⟩ } end /-- The Hausdorff distance is invariant under isometries -/ lemma Hausdorff_dist_image (h : isometry Φ) : Hausdorff_dist (Φ '' s) (Φ '' t) = Hausdorff_dist s t := by simp [Hausdorff_dist, Hausdorff_edist_image h] /-- The Hausdorff distance satisfies the triangular inequality -/ lemma Hausdorff_dist_triangle (fin : Hausdorff_edist s t ≠ ⊤) : Hausdorff_dist s u ≤ Hausdorff_dist s t + Hausdorff_dist t u := begin by_cases Hausdorff_edist s u = ⊤, { calc Hausdorff_dist s u = 0 + 0 : by simp [Hausdorff_dist, h] ... ≤ Hausdorff_dist s t + Hausdorff_dist t u : add_le_add (Hausdorff_dist_nonneg) (Hausdorff_dist_nonneg) }, { have Dtu : Hausdorff_edist t u < ⊤ := calc Hausdorff_edist t u ≤ Hausdorff_edist t s + Hausdorff_edist s u : Hausdorff_edist_triangle ... = Hausdorff_edist s t + Hausdorff_edist s u : by simp [Hausdorff_edist_comm] ... < ⊤ : by simp [ennreal.add_lt_top]; simp [ennreal.lt_top_iff_ne_top, h, fin], rw [Hausdorff_dist, Hausdorff_dist, Hausdorff_dist, ← ennreal.to_real_add fin (lt_top_iff_ne_top.1 Dtu), ennreal.to_real_le_to_real h], { exact Hausdorff_edist_triangle }, { simp [ennreal.add_eq_top, lt_top_iff_ne_top.1 Dtu, fin] }} end /-- The Hausdorff distance satisfies the triangular inequality -/ lemma Hausdorff_dist_triangle' (fin : Hausdorff_edist t u ≠ ⊤) : Hausdorff_dist s u ≤ Hausdorff_dist s t + Hausdorff_dist t u := begin rw Hausdorff_edist_comm at fin, have I : Hausdorff_dist u s ≤ Hausdorff_dist u t + Hausdorff_dist t s := Hausdorff_dist_triangle fin, simpa [add_comm, Hausdorff_dist_comm] using I end /-- The Hausdorff distance between a set and its closure vanish -/ @[simp, priority 1100] lemma Hausdorff_dist_self_closure : Hausdorff_dist s (closure s) = 0 := by simp [Hausdorff_dist] /-- Replacing a set by its closure does not change the Hausdorff distance. -/ @[simp] lemma Hausdorff_dist_closure₁ : Hausdorff_dist (closure s) t = Hausdorff_dist s t := by simp [Hausdorff_dist] /-- Replacing a set by its closure does not change the Hausdorff distance. -/ @[simp] lemma Hausdorff_dist_closure₂ : Hausdorff_dist s (closure t) = Hausdorff_dist s t := by simp [Hausdorff_dist] /-- The Hausdorff distance between two sets and their closures coincide -/ @[simp] lemma Hausdorff_dist_closure : Hausdorff_dist (closure s) (closure t) = Hausdorff_dist s t := by simp [Hausdorff_dist] /-- Two sets are at zero Hausdorff distance if and only if they have the same closures -/ lemma Hausdorff_dist_zero_iff_closure_eq_closure (fin : Hausdorff_edist s t ≠ ⊤) : Hausdorff_dist s t = 0 ↔ closure s = closure t := by simp [Hausdorff_edist_zero_iff_closure_eq_closure.symm, Hausdorff_dist, ennreal.to_real_eq_zero_iff, fin] /-- Two closed sets are at zero Hausdorff distance if and only if they coincide -/ lemma Hausdorff_dist_zero_iff_eq_of_closed (hs : is_closed s) (ht : is_closed t) (fin : Hausdorff_edist s t ≠ ⊤) : Hausdorff_dist s t = 0 ↔ s = t := by simp [(Hausdorff_edist_zero_iff_eq_of_closed hs ht).symm, Hausdorff_dist, ennreal.to_real_eq_zero_iff, fin] end --section end metric --namespace
4f651c080a492b70336bbee570fb8a0e818e720c
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Elab/InfoTree.lean
2a976e5de43e20bd954d9d0c8fd9366ffdf20f3a
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
262
lean
/- Copyright (c) 2020 Wojciech Nawrocki. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Wojciech Nawrocki, Leonardo de Moura, Sebastian Ullrich -/ import Lean.Elab.InfoTree.Types import Lean.Elab.InfoTree.Main
123ff79aaa073a5e4734ff774e5afbc0a59a5a39
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/src/builtin/bin.lean
a2fbb8edb5cc6a47fc8b13197b4bdc71ab115837
[ "Apache-2.0" ]
permissive
codyroux/lean0.1
1ce92751d664aacff0529e139083304a7bbc8a71
0dc6fb974aa85ed6f305a2f4b10a53a44ee5f0ef
refs/heads/master
1,610,830,535,062
1,402,150,480,000
1,402,150,480,000
19,588,851
2
0
null
null
null
null
UTF-8
Lean
false
false
1,568
lean
import num tactic macros namespace num definition z := zero definition d (x : num) := x + x set_option simplifier::unfold true theorem add_d_d (x y : num) : d x + d y = d (x + y) := by simp theorem ssd (x : num) : succ (succ (d x)) = d (succ x) := by simp theorem add_sd_d (x y : num) : succ (d x) + d y = succ (d (x + y)) := by simp theorem add_d_sd (x y : num) : d x + succ (d y) = succ (d (x + y)) := by simp theorem add_sd_sd (x y : num) : succ (d x) + succ (d y) = d (succ (x + y)) := by simp theorem d_z : d zero = zero := by simp theorem s_s_z : succ (succ zero) = d (succ zero) := by simp definition d1 (x : num) := succ (d x) theorem d1_def (x : num) : d1 x = succ (d x) := refl _ set_opaque d true add_rewrite s_s_z d_z add_d_d ssd add_sd_d add_d_sd add_sd_sd d1_def scope theorem test1 : d1 z = one := by simp theorem test2 : d1 one = one + one + one := by simp theorem test3 : d (d1 one) = one + one + one + one + one + one := by simp theorem test4 : d (d1 (d (d1 one))) = d (d (d (d (d1 z)))) + d (d (d (d1 z))) + succ (succ z) := by simp theorem test5 : d (succ (succ (succ (succ (succ zero))))) = d (d1 (d one)) := by simp (* local s = parse_lean("num::succ") local z = parse_lean("num::zero") local d = parse_lean("num::d") local d1 = parse_lean("num::d1") local add = parse_lean("num::add") local t1 = s(s(s(s(s(s(s(s(s(s(z)))))))))) local t2, pr = simplify(t1) print(t2) print(pr) local t1 = add(d(d(d(d(d(d(s(z))))))), d(d(d(d(s(z)))))) local t2, pr = simplify(t1) print(t2) print(pr) *) pop_scope end
88b601e4f643fcbafc26eef12e789b9b04228f0a
4bcaca5dc83d49803f72b7b5920b75b6e7d9de2d
/stage0/src/Lean/Meta/UnificationHint.lean
333b6d838ef36502df379f126aecfe4bbdec2e5d
[ "Apache-2.0" ]
permissive
subfish-zhou/leanprover-zh_CN.github.io
30b9fba9bd790720bd95764e61ae796697d2f603
8b2985d4a3d458ceda9361ac454c28168d920d3f
refs/heads/master
1,689,709,967,820
1,632,503,056,000
1,632,503,056,000
409,962,097
1
0
null
null
null
null
UTF-8
Lean
false
false
5,410
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.ScopedEnvExtension import Lean.Util.Recognizers import Lean.Meta.DiscrTree import Lean.Meta.LevelDefEq import Lean.Meta.SynthInstance namespace Lean.Meta structure UnificationHintEntry where keys : Array DiscrTree.Key val : Name deriving Inhabited structure UnificationHints where discrTree : DiscrTree Name := DiscrTree.empty deriving Inhabited instance : ToFormat UnificationHints where format h := format h.discrTree def UnificationHints.add (hints : UnificationHints) (e : UnificationHintEntry) : UnificationHints := { hints with discrTree := hints.discrTree.insertCore e.keys e.val } builtin_initialize unificationHintExtension : SimpleScopedEnvExtension UnificationHintEntry UnificationHints ← registerSimpleScopedEnvExtension { name := `unifHints addEntry := UnificationHints.add initial := {} } structure UnificationConstraint where lhs : Expr rhs : Expr structure UnificationHint where pattern : UnificationConstraint constraints : List UnificationConstraint private partial def decodeUnificationHint (e : Expr) : ExceptT MessageData Id UnificationHint := do decode e #[] where decodeConstraint (e : Expr) : ExceptT MessageData Id UnificationConstraint := match e.eq? with | some (_, lhs, rhs) => return UnificationConstraint.mk lhs rhs | none => throw m!"invalid unification hint constraint, unexpected term{indentExpr e}" decode (e : Expr) (cs : Array UnificationConstraint) : ExceptT MessageData Id UnificationHint := do match e with | Expr.forallE _ d b _ => do let c ← decodeConstraint d if b.hasLooseBVars then throw m!"invalid unification hint constraint, unexpected dependency{indentExpr e}" decode b (cs.push c) | _ => do let p ← decodeConstraint e return { pattern := p, constraints := cs.toList } private partial def validateHint (declName : Name) (hint : UnificationHint) : MetaM Unit := do hint.constraints.forM fun c => do unless (← isDefEq c.lhs c.rhs) do throwError "invalid unification hint, failed to unify constraint left-hand-side{indentExpr c.lhs}\nwith right-hand-side{indentExpr c.rhs}" unless (← isDefEq hint.pattern.lhs hint.pattern.rhs) do throwError "invalid unification hint, failed to unify pattern left-hand-side{indentExpr hint.pattern.lhs}\nwith right-hand-side{indentExpr hint.pattern.rhs}" def addUnificationHint (declName : Name) (kind : AttributeKind) : MetaM Unit := withNewMCtxDepth do let info ← getConstInfo declName match info.value? with | none => throwError "invalid unification hint, it must be a definition" | some val => let (_, _, body) ← lambdaMetaTelescope val match decodeUnificationHint body with | Except.error msg => throwError msg | Except.ok hint => let keys ← DiscrTree.mkPath hint.pattern.lhs validateHint declName hint unificationHintExtension.add { keys := keys, val := declName } kind trace[Meta.debug] "addUnificationHint: {unificationHintExtension.getState (← getEnv)}" builtin_initialize registerBuiltinAttribute { name := `unificationHint descr := "unification hint" add := fun declName stx kind => do Attribute.Builtin.ensureNoArgs stx discard <| addUnificationHint declName kind |>.run } def tryUnificationHints (t s : Expr) : MetaM Bool := do trace[Meta.isDefEq.hint] "{t} =?= {s}" unless (← read).config.unificationHints do return false if t.isMVar then return false let hints := unificationHintExtension.getState (← getEnv) let candidates ← hints.discrTree.getMatch t for candidate in candidates do if (← tryCandidate candidate) then return true return false where isDefEqPattern p e := withReducible <| Meta.isExprDefEqAux p e tryCandidate candidate : MetaM Bool := traceCtx `Meta.isDefEq.hint <| checkpointDefEq do trace[Meta.isDefEq.hint] "trying hint {candidate} at {t} =?= {s}" let cinfo ← getConstInfo candidate let us ← cinfo.levelParams.mapM fun _ => mkFreshLevelMVar let val := cinfo.instantiateValueLevelParams us let (xs, bis, body) ← lambdaMetaTelescope val let hint? ← withConfig (fun cfg => { cfg with unificationHints := false }) do match decodeUnificationHint body with | Except.error _ => return none | Except.ok hint => if (← isDefEqPattern hint.pattern.lhs t <&&> isDefEqPattern hint.pattern.rhs s) then return some hint else return none match hint? with | none => return false | some hint => trace[Meta.isDefEq.hint] "{candidate} succeeded, applying constraints" for c in hint.constraints do unless (← Meta.isExprDefEqAux c.lhs c.rhs) do return false for x in xs, bi in bis do if bi == BinderInfo.instImplicit then match (← trySynthInstance (← inferType x)) with | LOption.some val => unless (← isDefEq x val) do return false | _ => return false return true builtin_initialize registerTraceClass `Meta.isDefEq.hint end Lean.Meta
650397fc10443b1f3c07c2278879843d10d68684
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/tests/lean/interactive/completionOption.lean
027619ef2d98a086a149a92c098d0b4c7a59e0bd
[ "Apache-2.0" ]
permissive
WojciechKarpiel/lean4
7f89706b8e3c1f942b83a2c91a3a00b05da0e65b
f6e1314fa08293dea66a329e05b6c196a0189163
refs/heads/master
1,686,633,402,214
1,625,821,189,000
1,625,821,258,000
384,640,886
0
0
Apache-2.0
1,625,903,617,000
1,625,903,026,000
null
UTF-8
Lean
false
false
67
lean
set_option pp.proofs --^ textDocument/completion
3b8fb125080d876e88287047ebd81aa3cbddae66
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/data/list/instances_auto.lean
3df3f26f60c0ef04e7ad5475187812311f51d28d
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
2,183
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.data.list.lemmas universes u_1 u namespace Mathlib protected instance list.monad : Monad List := { toApplicative := { toFunctor := { map := list.map, mapConst := fun (α β : Type u_1) => list.map ∘ function.const β }, toPure := { pure := list.ret }, toSeq := { seq := fun (α β : Type u_1) (f : List (α → β)) (x : List α) => list.bind f fun (_x : α → β) => list.map _x x }, toSeqLeft := { seqLeft := fun (α β : Type u_1) (a : List α) (b : List β) => (fun (α β : Type u_1) (f : List (α → β)) (x : List α) => list.bind f fun (_x : α → β) => list.map _x x) β α (list.map (function.const β) a) b }, toSeqRight := { seqRight := fun (α β : Type u_1) (a : List α) (b : List β) => (fun (α β : Type u_1) (f : List (α → β)) (x : List α) => list.bind f fun (_x : α → β) => list.map _x x) β β (list.map (function.const α id) a) b } }, toBind := { bind := list.bind } } protected instance list.is_lawful_monad : is_lawful_monad List := sorry protected instance list.alternative : alternative List := alternative.mk List.nil namespace list protected instance bin_tree_to_list {α : Type u} : has_coe (bin_tree α) (List α) := has_coe.mk bin_tree.to_list protected instance decidable_bex {α : Type u} (p : α → Prop) [decidable_pred p] (l : List α) : Decidable (∃ (x : α), ∃ (H : x ∈ l), p x) := sorry protected instance decidable_ball {α : Type u} (p : α → Prop) [decidable_pred p] (l : List α) : Decidable (∀ (x : α), x ∈ l → p x) := dite (∃ (x : α), ∃ (H : x ∈ l), ¬p x) (fun (h : ∃ (x : α), ∃ (H : x ∈ l), ¬p x) => isFalse sorry) fun (h : ¬∃ (x : α), ∃ (H : x ∈ l), ¬p x) => is_true sorry end Mathlib
dea4ddf173f46984af4ea7ddf59cd33fdf0a080d
4727251e0cd73359b15b664c3170e5d754078599
/src/category_theory/limits/yoneda.lean
277433e9b8f46f9261e915d10544fed8b9e42cf3
[ "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
5,276
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Bhavik Mehta -/ import category_theory.limits.functor_category /-! # Limit properties relating to the (co)yoneda embedding. We calculate the colimit of `Y ↦ (X ⟶ Y)`, which is just `punit`. (This is used in characterising cofinal functors.) We also show the (co)yoneda embeddings preserve limits and jointly reflect them. -/ open opposite open category_theory open category_theory.limits universes v u namespace category_theory namespace coyoneda variables {C : Type v} [small_category C] /-- The colimit cocone over `coyoneda.obj X`, with cocone point `punit`. -/ @[simps] def colimit_cocone (X : Cᵒᵖ) : cocone (coyoneda.obj X) := { X := punit, ι := { app := by tidy, } } /-- The proposed colimit cocone over `coyoneda.obj X` is a colimit cocone. -/ @[simps] def colimit_cocone_is_colimit (X : Cᵒᵖ) : is_colimit (colimit_cocone X) := { desc := λ s x, s.ι.app (unop X) (𝟙 _), fac' := λ s Y, by { ext f, convert congr_fun (s.w f).symm (𝟙 (unop X)), simp, }, uniq' := λ s m w, by { ext ⟨⟩, rw ← w, simp, } } instance (X : Cᵒᵖ) : has_colimit (coyoneda.obj X) := has_colimit.mk { cocone := _, is_colimit := colimit_cocone_is_colimit X } /-- The colimit of `coyoneda.obj X` is isomorphic to `punit`. -/ noncomputable def colimit_coyoneda_iso (X : Cᵒᵖ) : colimit (coyoneda.obj X) ≅ punit := colimit.iso_colimit_cocone { cocone := _, is_colimit := colimit_cocone_is_colimit X } end coyoneda variables {C : Type u} [category.{v} C] open limits /-- The yoneda embedding `yoneda.obj X : Cᵒᵖ ⥤ Type v` for `X : C` preserves limits. -/ instance yoneda_preserves_limits (X : C) : preserves_limits (yoneda.obj X) := { preserves_limits_of_shape := λ J 𝒥, by exactI { preserves_limit := λ K, { preserves := λ c t, { lift := λ s x, quiver.hom.unop (t.lift ⟨op X, λ j, (s.π.app j x).op, λ j₁ j₂ α, _⟩), fac' := λ s j, funext $ λ x, quiver.hom.op_inj (t.fac _ _), uniq' := λ s m w, funext $ λ x, begin refine quiver.hom.op_inj (t.uniq ⟨op X, _, _⟩ _ (λ j, _)), { dsimp, simp [← s.w α] }, -- See library note [dsimp, simp] { exact quiver.hom.unop_inj (congr_fun (w j) x) }, end } } } } /-- The coyoneda embedding `coyoneda.obj X : C ⥤ Type v` for `X : Cᵒᵖ` preserves limits. -/ instance coyoneda_preserves_limits (X : Cᵒᵖ) : preserves_limits (coyoneda.obj X) := { preserves_limits_of_shape := λ J 𝒥, by exactI { preserves_limit := λ K, { preserves := λ c t, { lift := λ s x, t.lift ⟨unop X, λ j, s.π.app j x, λ j₁ j₂ α, by { dsimp, simp [← s.w α]}⟩, -- See library note [dsimp, simp] fac' := λ s j, funext $ λ x, t.fac _ _, uniq' := λ s m w, funext $ λ x, begin refine (t.uniq ⟨unop X, _⟩ _ (λ j, _)), exact congr_fun (w j) x, end } } } } /-- The yoneda embeddings jointly reflect limits. -/ def yoneda_jointly_reflects_limits (J : Type v) [small_category J] (K : J ⥤ Cᵒᵖ) (c : cone K) (t : Π (X : C), is_limit ((yoneda.obj X).map_cone c)) : is_limit c := let s' : Π (s : cone K), cone (K ⋙ yoneda.obj s.X.unop) := λ s, ⟨punit, λ j _, (s.π.app j).unop, λ j₁ j₂ α, funext $ λ _, quiver.hom.op_inj (s.w α).symm⟩ in { lift := λ s, ((t s.X.unop).lift (s' s) punit.star).op, fac' := λ s j, quiver.hom.unop_inj (congr_fun ((t s.X.unop).fac (s' s) j) punit.star), uniq' := λ s m w, begin apply quiver.hom.unop_inj, suffices : (λ (x : punit), m.unop) = (t s.X.unop).lift (s' s), { apply congr_fun this punit.star }, apply (t _).uniq (s' s) _ (λ j, _), ext, exact quiver.hom.op_inj (w j), end } /-- The coyoneda embeddings jointly reflect limits. -/ def coyoneda_jointly_reflects_limits (J : Type v) [small_category J] (K : J ⥤ C) (c : cone K) (t : Π (X : Cᵒᵖ), is_limit ((coyoneda.obj X).map_cone c)) : is_limit c := let s' : Π (s : cone K), cone (K ⋙ coyoneda.obj (op s.X)) := λ s, ⟨punit, λ j _, s.π.app j, λ j₁ j₂ α, funext $ λ _, (s.w α).symm⟩ in { lift := λ s, (t (op s.X)).lift (s' s) punit.star, fac' := λ s j, congr_fun ((t _).fac (s' s) j) punit.star, uniq' := λ s m w, begin suffices : (λ (x : punit), m) = (t _).lift (s' s), { apply congr_fun this punit.star }, apply (t _).uniq (s' s) _ (λ j, _), ext, exact (w j), end } variables {D : Type u} [small_category D] instance yoneda_functor_preserves_limits : preserves_limits (@yoneda D _) := begin apply preserves_limits_of_evaluation, intro K, change preserves_limits (coyoneda.obj K), apply_instance end instance coyoneda_functor_preserves_limits : preserves_limits (@coyoneda D _) := begin apply preserves_limits_of_evaluation, intro K, change preserves_limits (yoneda.obj K), apply_instance end instance yoneda_functor_reflects_limits : reflects_limits (@yoneda D _) := limits.fully_faithful_reflects_limits _ instance coyoneda_functor_reflects_limits : reflects_limits (@coyoneda D _) := limits.fully_faithful_reflects_limits _ end category_theory
4fc97bc305b105bd1d6985ed6092d893c3eac655
4727251e0cd73359b15b664c3170e5d754078599
/src/ring_theory/chain_of_divisors.lean
60630db8efdb76feb5f0c0dc5682297518b7e4a8
[ "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
11,707
lean
/- Copyright (c) 2021 Paul Lezeau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen, Paul Lezeau -/ import algebra.is_prime_pow import algebra.squarefree /-! # Chains of divisors The results in this file show that in the monoid `associates M` of a `unique_factorization_monoid` `M`, an element `a` is an n-th prime power iff its set of divisors is a strictly increasing chain of length `n + 1`, meaning that we can find a strictly increasing bijection between `fin (n + 1)` and the set of factors of `a`. ## Main results - `divisor_chain.exists_chain_of_prime_pow` : existence of a chain for prime powers. - `divisor_chain.is_prime_pow_of_has_chain` : elements that have a chain are prime powers. - `multiplicity_prime_le_multiplicity_image_by_factor_order_iso` : if there is a monotone bijection `d` between the set of factors of `a : associates M` and the set of factors of `b : associates N`, then, for any prime `p ∣ a`, `multiplicity p a ≤ multiplicity (d p) b`. ## Todo - Show that under the assumptions of `multiplicity_prime_le_multiplicity_image_by_factor_order_iso`, `d p` is prime whenever `p` is prime. Applying `multiplicity_prime_le_multiplicity_image_by_factor_order_iso` on `d.symm` then gives us `multiplicity p a = multiplicity (d p) b`. - Create a structure for chains of divisors. -/ variables {M : Type*} [cancel_comm_monoid_with_zero M] open unique_factorization_monoid multiplicity irreducible namespace divisor_chain lemma exists_chain_of_prime_pow {p : associates M} {n : ℕ} (hn : n ≠ 0) (hp : prime p) : ∃ c : fin (n + 1) → associates M, c 1 = p ∧ strict_mono c ∧ ∀ {r : associates M}, r ≤ p^n ↔ ∃ i, r = c i := begin refine ⟨λ i, p^(i : ℕ), _, λ n m h, _, λ y, ⟨λ h, _, _⟩⟩, { rw [fin.coe_one', nat.mod_eq_of_lt, pow_one], exact nat.lt_succ_of_le (nat.one_le_iff_ne_zero.mpr hn) }, { exact associates.dvd_not_unit_iff_lt.mp ⟨pow_ne_zero n hp.ne_zero, p^(m - n : ℕ), not_is_unit_of_not_is_unit_dvd hp.not_unit (dvd_pow dvd_rfl (nat.sub_pos_of_lt h).ne'), (pow_mul_pow_sub p h.le).symm⟩ }, { obtain ⟨i, i_le, hi⟩ := (dvd_prime_pow hp n).1 h, rw associated_iff_eq at hi, exact ⟨⟨i, nat.lt_succ_of_le i_le⟩, hi⟩ }, { rintro ⟨i, rfl⟩, exact ⟨p^(n - i : ℕ), (pow_mul_pow_sub p (nat.succ_le_succ_iff.mp i.2)).symm⟩ } end lemma element_of_chain_not_is_unit_of_index_ne_zero {n : ℕ} {i : fin (n + 1)} (i_pos : i ≠ 0) {c : fin (n + 1) → associates M} (h₁ : strict_mono c) : ¬ is_unit (c i) := dvd_not_unit.not_unit (associates.dvd_not_unit_iff_lt.2 (h₁ $ show (0 : fin (n + 1)) < i, from i.pos_iff_ne_zero.mpr i_pos)) lemma first_of_chain_is_unit {q : associates M} {n : ℕ} {c : fin (n + 1) → associates M} (h₁ : strict_mono c) (h₂ : ∀ {r}, r ≤ q ↔ ∃ i, r = c i) : is_unit (c 0) := begin obtain ⟨i, hr⟩ := h₂.mp associates.one_le, rw [associates.is_unit_iff_eq_one, ← associates.le_one_iff, hr], exact h₁.monotone (fin.zero_le i) end /-- The second element of a chain is irreducible. -/ lemma second_of_chain_is_irreducible {q : associates M} {n : ℕ} (hn : n ≠ 0) {c : fin (n + 1) → associates M} (h₁ : strict_mono c) (h₂ : ∀ {r}, r ≤ q ↔ ∃ i, r = c i) (hq : q ≠ 0) : irreducible (c 1) := begin cases n, { contradiction }, refine (associates.is_atom_iff (ne_zero_of_dvd_ne_zero hq (h₂.2 ⟨1, rfl⟩))).mp ⟨_, λ b hb, _⟩, { exact ne_bot_of_gt (h₁ (show (0 : fin (n + 2)) < 1, from fin.one_pos)) }, obtain ⟨⟨i, hi⟩, rfl⟩ := h₂.1 (hb.le.trans (h₂.2 ⟨1, rfl⟩)), cases i, { exact (associates.is_unit_iff_eq_one _).mp (first_of_chain_is_unit h₁ @h₂) }, { simpa [fin.lt_iff_coe_lt_coe] using h₁.lt_iff_lt.mp hb }, end lemma eq_second_of_chain_of_prime_dvd {p q r : associates M} {n : ℕ} (hn : n ≠ 0) {c : fin (n + 1) → associates M} (h₁ : strict_mono c) (h₂ : ∀ {r : associates M}, r ≤ q ↔ ∃ i, r = c i) (hp : prime p) (hr : r ∣ q) (hp' : p ∣ r) : p = c 1 := begin cases n, { contradiction }, obtain ⟨i, rfl⟩ := h₂.1 (dvd_trans hp' hr), refine congr_arg c (eq_of_ge_of_not_gt _ $ λ hi, _), { rw [fin.le_iff_coe_le_coe, fin.coe_one, nat.succ_le_iff, ← fin.coe_zero, ← fin.lt_iff_coe_lt_coe, fin.pos_iff_ne_zero], rintro rfl, exact hp.not_unit (first_of_chain_is_unit h₁ @h₂) }, obtain (rfl | ⟨j, rfl⟩) := i.eq_zero_or_eq_succ, { cases hi }, refine not_irreducible_of_not_unit_dvd_not_unit (dvd_not_unit.not_unit (associates.dvd_not_unit_iff_lt.2 (h₁ (show (0 : fin (n + 2)) < j, from _)) )) _ hp.irreducible, { simpa [← fin.succ_zero_eq_one, fin.succ_lt_succ_iff] using hi }, { refine associates.dvd_not_unit_iff_lt.2 (h₁ _), simpa only [fin.coe_eq_cast_succ] using fin.lt_succ } end lemma card_subset_divisors_le_length_of_chain {q : associates M} {n : ℕ} {c : fin (n + 1) → associates M} (h₂ : ∀ {r}, r ≤ q ↔ ∃ i, r = c i) {m : finset (associates M)} (hm : ∀ r, r ∈ m → r ≤ q) : m.card ≤ n + 1 := begin classical, have mem_image : ∀ (r : associates M), r ≤ q → r ∈ finset.univ.image c, { intros r hr, obtain ⟨i, hi⟩ := h₂.1 hr, exact finset.mem_image.2 ⟨i, finset.mem_univ _, hi.symm⟩ }, rw ←finset.card_fin (n + 1), exact (finset.card_le_of_subset $ λ x hx, mem_image x $ hm x hx).trans finset.card_image_le, end variables [unique_factorization_monoid M] lemma element_of_chain_eq_pow_second_of_chain {q r : associates M} {n : ℕ} (hn : n ≠ 0) {c : fin (n + 1) → associates M} (h₁ : strict_mono c) (h₂ : ∀ {r}, r ≤ q ↔ ∃ i, r = c i) (hr : r ∣ q) (hq : q ≠ 0) : ∃ (i : fin (n + 1)), r = (c 1) ^ (i : ℕ) := begin classical, let i := (normalized_factors r).card, have hi : normalized_factors r = multiset.repeat (c 1) i, { apply multiset.eq_repeat_of_mem, intros b hb, refine eq_second_of_chain_of_prime_dvd hn h₁ (λ r', h₂) (prime_of_normalized_factor b hb) hr (dvd_of_mem_normalized_factors hb) }, have H : r = (c 1)^i, { have := unique_factorization_monoid.normalized_factors_prod (ne_zero_of_dvd_ne_zero hq hr), rw [associated_iff_eq, hi, multiset.prod_repeat] at this, rw this }, refine ⟨⟨i, _⟩, H⟩, have : (finset.univ.image (λ (m : fin (i + 1)), (c 1) ^ (m : ℕ))).card = i + 1, { conv_rhs { rw [← finset.card_fin (i+1)] }, cases n, { contradiction }, rw finset.card_image_eq_iff_inj_on, refine set.inj_on_of_injective (λ m m' h, fin.ext _) _, refine pow_injective_of_not_unit (element_of_chain_not_is_unit_of_index_ne_zero (by simp) h₁) _ h, exact irreducible.ne_zero (second_of_chain_is_irreducible hn h₁ @h₂ hq) }, suffices H' : ∀ r ∈ (finset.univ.image (λ (m : fin (i + 1)), (c 1) ^ (m : ℕ))), r ≤ q, { simp only [← nat.succ_le_iff, nat.succ_eq_add_one, ← this], apply card_subset_divisors_le_length_of_chain @h₂ H' }, simp only [finset.mem_image], rintros r ⟨a, ha, rfl⟩, refine dvd_trans _ hr, use (c 1)^(i - a), rw pow_mul_pow_sub (c 1), { exact H }, { exact nat.succ_le_succ_iff.mp a.2 } end lemma eq_pow_second_of_chain_of_has_chain {q : associates M} {n : ℕ} (hn : n ≠ 0) {c : fin (n + 1) → associates M} (h₁ : strict_mono c) (h₂ : ∀ {r : associates M}, r ≤ q ↔ ∃ i, r = c i) (hq : q ≠ 0) : q = (c 1)^n := begin classical, obtain ⟨i, hi'⟩ := element_of_chain_eq_pow_second_of_chain hn h₁ (λ r, h₂) (dvd_refl q) hq, convert hi', refine (nat.lt_succ_iff.1 i.prop).antisymm' (nat.le_of_succ_le_succ _), calc n + 1 = (finset.univ : finset (fin (n + 1))).card : (finset.card_fin _).symm ... = (finset.univ.image c).card : (finset.card_image_eq_iff_inj_on.mpr (h₁.injective.inj_on _)).symm ... ≤ (finset.univ.image (λ (m : fin (i + 1)), (c 1)^(m : ℕ))).card : finset.card_le_of_subset _ ... ≤ (finset.univ : finset (fin (i + 1))).card : finset.card_image_le ... = i + 1 : finset.card_fin _, intros r hr, obtain ⟨j, -, rfl⟩ := finset.mem_image.1 hr, have := h₂.2 ⟨j, rfl⟩, rw hi' at this, obtain ⟨u, hu, hu'⟩ := (dvd_prime_pow (show prime (c 1), from _) i).1 this, refine finset.mem_image.mpr ⟨u, finset.mem_univ _, _⟩, { rw associated_iff_eq at hu', rw [fin.coe_coe_of_lt (nat.lt_succ_of_le hu), hu'] }, { rw ← irreducible_iff_prime, exact second_of_chain_is_irreducible hn h₁ @h₂ hq, } end lemma is_prime_pow_of_has_chain {q : associates M} {n : ℕ} (hn : n ≠ 0) {c : fin (n + 1) → associates M} (h₁ : strict_mono c) (h₂ : ∀ {r : associates M}, r ≤ q ↔ ∃ i, r = c i) (hq : q ≠ 0) : is_prime_pow q := ⟨c 1, n, irreducible_iff_prime.mp (second_of_chain_is_irreducible hn h₁ @h₂ hq), zero_lt_iff.mpr hn, (eq_pow_second_of_chain_of_has_chain hn h₁ @h₂ hq).symm⟩ end divisor_chain variables {N : Type*} [cancel_comm_monoid_with_zero N] [unique_factorization_monoid N] [decidable_eq (associates M)] [unique_factorization_monoid M] open divisor_chain lemma pow_image_of_prime_by_factor_order_iso_dvd {m p : associates M} {n : associates N} (hn : n ≠ 0) (hp : p ∈ normalized_factors m) (d : {l : associates M // l ≤ m} ≃o {l : associates N // l ≤ n}) {s : ℕ} (hs' : p^s ≤ m) : (d ⟨p, dvd_of_mem_normalized_factors hp⟩ : associates N)^s ≤ n := begin by_cases hs : s = 0, { simp [hs], }, suffices : (d ⟨p, dvd_of_mem_normalized_factors hp⟩ : associates N)^s = ↑(d ⟨p^s, hs'⟩), { rw this, apply subtype.prop (d ⟨p^s, hs'⟩) }, obtain ⟨c₁, rfl, hc₁', hc₁''⟩ := exists_chain_of_prime_pow hs (prime_of_normalized_factor p hp), set c₂ : fin (s + 1) → associates N := λ t, d ⟨c₁ t, le_trans (hc₁''.2 ⟨t, by simp⟩) hs'⟩, have c₂.def : ∀ (t), c₂ t = d ⟨c₁ t, _⟩ := λ t, rfl, refine (congr_arg (^s) (c₂.def 1).symm).trans _, refine (eq_pow_second_of_chain_of_has_chain hs (λ t u h, _) (λ r, ⟨λ hr, _, _⟩) _).symm, { rw [c₂.def, c₂.def, subtype.coe_lt_coe, d.lt_iff_lt, subtype.mk_lt_mk, (hc₁').lt_iff_lt], exact h }, { have : r ≤ n := hr.trans (d ⟨c₁ 1 ^ s, _⟩).2, suffices : d.symm ⟨r, this⟩ ≤ ⟨c₁ 1 ^ s, hs'⟩, { obtain ⟨i, hi⟩ := hc₁''.1 this, use i, simp only [c₂.def, ← hi, d.apply_symm_apply, subtype.coe_eta, subtype.coe_mk] }, conv_rhs { rw ← d.symm_apply_apply ⟨c₁ 1 ^ s, hs'⟩ }, rw d.symm.le_iff_le, simpa only [← subtype.coe_le_coe, subtype.coe_mk] using hr }, { rintros ⟨i, hr⟩, rw [hr, c₂.def, subtype.coe_le_coe, d.le_iff_le], simpa [subtype.mk_le_mk] using hc₁''.2 ⟨i, rfl⟩ }, exact ne_zero_of_dvd_ne_zero hn (subtype.prop (d ⟨c₁ 1 ^ s, _⟩)) end variables [decidable_rel ((∣) : associates M → associates M → Prop)] [decidable_rel ((∣) : associates N → associates N → Prop)] lemma multiplicity_prime_le_multiplicity_image_by_factor_order_iso {m p : associates M} {n : associates N} (hp : p ∈ normalized_factors m) (d : {l : associates M // l ≤ m} ≃o {l : associates N // l ≤ n}) : multiplicity p m ≤ multiplicity ↑(d ⟨p, dvd_of_mem_normalized_factors hp⟩) n := begin by_cases hn : n = 0, { simp [hn], }, by_cases hm : m = 0, { simpa [hm] using hp, }, rw [←enat.coe_get (finite_iff_dom.1 $ finite_prime_left (prime_of_normalized_factor p hp) hm), ←pow_dvd_iff_le_multiplicity], exact pow_image_of_prime_by_factor_order_iso_dvd hn hp d (pow_multiplicity_dvd _), end
40005e1fd5c2fcdcb080063bc8c7af14e2a56d60
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/prod/tprod.lean
b89c8772ee701dd9103ea54fce749f601c9d0af1
[ "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,894
lean
/- Copyright (c) 2020 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import data.list.nodup /-! # Finite products of types This file defines the product of types over a list. For `l : list ι` and `α : ι → Type*` we define `list.tprod α l = l.foldr (λ i β, α i × β) punit`. This type should not be used if `Π i, α i` or `Π i ∈ l, α i` can be used instead (in the last expression, we could also replace the list `l` by a set or a finset). This type is used as an intermediary between binary products and finitary products. The application of this type is finitary product measures, but it could be used in any construction/theorem that is easier to define/prove on binary products than on finitary products. * Once we have the construction on binary products (like binary product measures in `measure_theory.prod`), we can easily define a finitary version on the type `tprod l α` by iterating. Properties can also be easily extended from the binary case to the finitary case by iterating. * Then we can use the equivalence `list.tprod.pi_equiv_tprod` below (or enhanced versions of it, like a `measurable_equiv` for product measures) to get the construction on `Π i : ι, α i`, at least when assuming `[fintype ι] [encodable ι]` (using `encodable.sorted_univ`). Using `local attribute [instance] fintype.to_encodable` we can get rid of the argument `[encodable ι]`. ## Main definitions * We have the equivalence `tprod.pi_equiv_tprod : (Π i, α i) ≃ tprod α l` if `l` contains every element of `ι` exactly once. * The product of sets is `set.tprod : (Π i, set (α i)) → set (tprod α l)`. -/ open list function variables {ι : Type*} {α : ι → Type*} {i j : ι} {l : list ι} {f : Π i, α i} namespace list variable (α) /-- The product of a family of types over a list. -/ def tprod (l : list ι) : Type* := l.foldr (λ i β, α i × β) punit variable {α} namespace tprod open list /-- Turning a function `f : Π i, α i` into an element of the iterated product `tprod α l`. -/ protected def mk : ∀ (l : list ι) (f : Π i, α i), tprod α l | [] := λ f, punit.star | (i :: is) := λ f, (f i, mk is f) instance [∀ i, inhabited (α i)] : inhabited (tprod α l) := ⟨tprod.mk l default⟩ @[simp] lemma fst_mk (i : ι) (l : list ι) (f : Π i, α i) : (tprod.mk (i::l) f).1 = f i := rfl @[simp] lemma snd_mk (i : ι) (l : list ι) (f : Π i, α i) : (tprod.mk (i::l) f).2 = tprod.mk l f := rfl variables [decidable_eq ι] /-- Given an element of the iterated product `l.prod α`, take a projection into direction `i`. If `i` appears multiple times in `l`, this chooses the first component in direction `i`. -/ protected def elim : ∀ {l : list ι} (v : tprod α l) {i : ι} (hi : i ∈ l), α i | (i :: is) v j hj := if hji : j = i then by { subst hji, exact v.1 } else elim v.2 (hj.resolve_left hji) @[simp] lemma elim_self (v : tprod α (i :: l)) : v.elim (l.mem_cons_self i) = v.1 := by simp [tprod.elim] @[simp] lemma elim_of_ne (hj : j ∈ i :: l) (hji : j ≠ i) (v : tprod α (i :: l)) : v.elim hj = tprod.elim v.2 (hj.resolve_left hji) := by simp [tprod.elim, hji] @[simp] lemma elim_of_mem (hl : (i :: l).nodup) (hj : j ∈ l) (v : tprod α (i :: l)) : v.elim (mem_cons_of_mem _ hj) = tprod.elim v.2 hj := by { apply elim_of_ne, rintro rfl, exact hl.not_mem hj } lemma elim_mk : ∀ (l : list ι) (f : Π i, α i) {i : ι} (hi : i ∈ l), (tprod.mk l f).elim hi = f i | (i :: is) f j hj := begin by_cases hji : j = i, { subst hji, simp }, { rw [elim_of_ne _ hji, snd_mk, elim_mk] } end @[ext] lemma ext : ∀ {l : list ι} (hl : l.nodup) {v w : tprod α l} (hvw : ∀ i (hi : i ∈ l), v.elim hi = w.elim hi), v = w | [] hl v w hvw := punit.ext | (i :: is) hl v w hvw := begin ext, rw [← elim_self v, hvw, elim_self], refine ext (nodup_cons.mp hl).2 (λ j hj, _), rw [← elim_of_mem hl, hvw, elim_of_mem hl] end /-- A version of `tprod.elim` when `l` contains all elements. In this case we get a function into `Π i, α i`. -/ @[simp] protected def elim' (h : ∀ i, i ∈ l) (v : tprod α l) (i : ι) : α i := v.elim (h i) lemma mk_elim (hnd : l.nodup) (h : ∀ i, i ∈ l) (v : tprod α l) : tprod.mk l (v.elim' h) = v := tprod.ext hnd (λ i hi, by simp [elim_mk]) /-- Pi-types are equivalent to iterated products. -/ def pi_equiv_tprod (hnd : l.nodup) (h : ∀ i, i ∈ l) : (Π i, α i) ≃ tprod α l := ⟨tprod.mk l, tprod.elim' h, λ f, funext $ λ i, elim_mk l f (h i), mk_elim hnd h⟩ end tprod end list namespace set open list /-- A product of sets in `tprod α l`. -/ @[simp] protected def tprod : ∀ (l : list ι) (t : Π i, set (α i)), set (tprod α l) | [] t := univ | (i :: is) t := t i ×ˢ tprod is t lemma mk_preimage_tprod : ∀ (l : list ι) (t : Π i, set (α i)), tprod.mk l ⁻¹' set.tprod l t = {i | i ∈ l}.pi t | [] t := by simp [set.tprod] | (i :: l) t := begin ext f, have : f ∈ tprod.mk l ⁻¹' set.tprod l t ↔ f ∈ {x | x ∈ l}.pi t, { rw [mk_preimage_tprod l t] }, change tprod.mk l f ∈ set.tprod l t ↔ ∀ (i : ι), i ∈ l → f i ∈ t i at this, /- `simp [set.tprod, tprod.mk, this]` can close this goal but is slow. -/ rw [set.tprod, tprod.mk, mem_preimage, mem_pi, prod_mk_mem_set_prod_eq], simp_rw [mem_set_of_eq, mem_cons_iff], rw [forall_eq_or_imp, and.congr_right_iff], exact λ _, this end lemma elim_preimage_pi [decidable_eq ι] {l : list ι} (hnd : l.nodup) (h : ∀ i, i ∈ l) (t : Π i, set (α i)) : tprod.elim' h ⁻¹' pi univ t = set.tprod l t := begin have : { i | i ∈ l} = univ, { ext i, simp [h] }, rw [← this, ← mk_preimage_tprod, preimage_preimage], convert preimage_id, simp [tprod.mk_elim hnd h, id_def] end end set
d6f47a0585a8bad71cb5ad232863ddcf4dd2b15c
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/finsupp/basic.lean
8e49f3d64eff32e4af7e2c441f405d56acfeec8f
[ "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
61,710
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Scott Morrison -/ import algebra.big_operators.finsupp import algebra.hom.group_action import algebra.regular.smul import data.finset.preimage import data.rat.big_operators /-! # Miscellaneous definitions, lemmas, and constructions using finsupp > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. ## Main declarations * `finsupp.graph`: the finset of input and output pairs with non-zero outputs. * `finsupp.map_range.equiv`: `finsupp.map_range` as an equiv. * `finsupp.map_domain`: maps the domain of a `finsupp` by a function and by summing. * `finsupp.comap_domain`: postcomposition of a `finsupp` with a function injective on the preimage of its support. * `finsupp.some`: restrict a finitely supported function on `option α` to a finitely supported function on `α`. * `finsupp.filter`: `filter p f` is the finitely supported function that is `f a` if `p a` is true and 0 otherwise. * `finsupp.frange`: the image of a finitely supported function on its support. * `finsupp.subtype_domain`: the restriction of a finitely supported function `f` to a subtype. ## Implementation notes This file is a `noncomputable theory` and uses classical logic throughout. ## TODO * This file is currently ~1600 lines long and is quite a miscellany of definitions and lemmas, so it should be divided into smaller pieces. * Expand the list of definitions and important lemmas to the module docstring. -/ noncomputable theory open finset function open_locale big_operators variables {α β γ ι M M' N P G H R S : Type*} namespace finsupp /-! ### Declarations about `graph` -/ section graph variable [has_zero M] /-- The graph of a finitely supported function over its support, i.e. the finset of input and output pairs with non-zero outputs. -/ def graph (f : α →₀ M) : finset (α × M) := f.support.map ⟨λ a, prod.mk a (f a), λ x y h, (prod.mk.inj h).1⟩ lemma mk_mem_graph_iff {a : α} {m : M} {f : α →₀ M} : (a, m) ∈ f.graph ↔ f a = m ∧ m ≠ 0 := begin simp_rw [graph, mem_map, mem_support_iff], split, { rintro ⟨b, ha, rfl, -⟩, exact ⟨rfl, ha⟩ }, { rintro ⟨rfl, ha⟩, exact ⟨a, ha, rfl⟩ } end @[simp] lemma mem_graph_iff {c : α × M} {f : α →₀ M} : c ∈ f.graph ↔ f c.1 = c.2 ∧ c.2 ≠ 0 := by { cases c, exact mk_mem_graph_iff } lemma mk_mem_graph (f : α →₀ M) {a : α} (ha : a ∈ f.support) : (a, f a) ∈ f.graph := mk_mem_graph_iff.2 ⟨rfl, mem_support_iff.1 ha⟩ lemma apply_eq_of_mem_graph {a : α} {m : M} {f : α →₀ M} (h : (a, m) ∈ f.graph) : f a = m := (mem_graph_iff.1 h).1 @[simp] lemma not_mem_graph_snd_zero (a : α) (f : α →₀ M) : (a, (0 : M)) ∉ f.graph := λ h, (mem_graph_iff.1 h).2.irrefl @[simp] lemma image_fst_graph [decidable_eq α] (f : α →₀ M) : f.graph.image prod.fst = f.support := begin classical, simp only [graph, map_eq_image, image_image, embedding.coe_fn_mk, (∘), image_id'], end lemma graph_injective (α M) [has_zero M] : injective (@graph α M _) := begin intros f g h, classical, have hsup : f.support = g.support, by rw [← image_fst_graph, h, image_fst_graph], refine ext_iff'.2 ⟨hsup, λ x hx, apply_eq_of_mem_graph $ h.symm ▸ _⟩, exact mk_mem_graph _ (hsup ▸ hx) end @[simp] lemma graph_inj {f g : α →₀ M} : f.graph = g.graph ↔ f = g := (graph_injective α M).eq_iff @[simp] lemma graph_zero : graph (0 : α →₀ M) = ∅ := by simp [graph] @[simp] lemma graph_eq_empty {f : α →₀ M} : f.graph = ∅ ↔ f = 0 := (graph_injective α M).eq_iff' graph_zero end graph end finsupp /-! ### Declarations about `map_range` -/ section map_range namespace finsupp section equiv variables [has_zero M] [has_zero N] [has_zero P] /-- `finsupp.map_range` as an equiv. -/ @[simps apply] def map_range.equiv (f : M ≃ N) (hf : f 0 = 0) (hf' : f.symm 0 = 0) : (α →₀ M) ≃ (α →₀ N) := { to_fun := (map_range f hf : (α →₀ M) → (α →₀ N)), inv_fun := (map_range f.symm hf' : (α →₀ N) → (α →₀ M)), left_inv := λ x, begin rw ←map_range_comp _ _ _ _; simp_rw equiv.symm_comp_self, { exact map_range_id _ }, { refl }, end, right_inv := λ x, begin rw ←map_range_comp _ _ _ _; simp_rw equiv.self_comp_symm, { exact map_range_id _ }, { refl }, end } @[simp] lemma map_range.equiv_refl : map_range.equiv (equiv.refl M) rfl rfl = equiv.refl (α →₀ M) := equiv.ext map_range_id lemma map_range.equiv_trans (f : M ≃ N) (hf : f 0 = 0) (hf') (f₂ : N ≃ P) (hf₂ : f₂ 0 = 0) (hf₂') : (map_range.equiv (f.trans f₂) (by rw [equiv.trans_apply, hf, hf₂]) (by rw [equiv.symm_trans_apply, hf₂', hf']) : (α →₀ _) ≃ _) = (map_range.equiv f hf hf').trans (map_range.equiv f₂ hf₂ hf₂') := equiv.ext $ map_range_comp _ _ _ _ _ @[simp] lemma map_range.equiv_symm (f : M ≃ N) (hf hf') : ((map_range.equiv f hf hf').symm : (α →₀ _) ≃ _) = map_range.equiv f.symm hf' hf := equiv.ext $ λ x, rfl end equiv section zero_hom variables [has_zero M] [has_zero N] [has_zero P] /-- Composition with a fixed zero-preserving homomorphism is itself an zero-preserving homomorphism on functions. -/ @[simps] def map_range.zero_hom (f : zero_hom M N) : zero_hom (α →₀ M) (α →₀ N) := { to_fun := (map_range f f.map_zero : (α →₀ M) → (α →₀ N)), map_zero' := map_range_zero } @[simp] lemma map_range.zero_hom_id : map_range.zero_hom (zero_hom.id M) = zero_hom.id (α →₀ M) := zero_hom.ext map_range_id lemma map_range.zero_hom_comp (f : zero_hom N P) (f₂ : zero_hom M N) : (map_range.zero_hom (f.comp f₂) : zero_hom (α →₀ _) _) = (map_range.zero_hom f).comp (map_range.zero_hom f₂) := zero_hom.ext $ map_range_comp _ _ _ _ _ end zero_hom section add_monoid_hom variables [add_comm_monoid M] [add_comm_monoid N] [add_comm_monoid P] /-- Composition with a fixed additive homomorphism is itself an additive homomorphism on functions. -/ @[simps] def map_range.add_monoid_hom (f : M →+ N) : (α →₀ M) →+ (α →₀ N) := { to_fun := (map_range f f.map_zero : (α →₀ M) → (α →₀ N)), map_zero' := map_range_zero, map_add' := λ a b, map_range_add f.map_add _ _ } @[simp] lemma map_range.add_monoid_hom_id : map_range.add_monoid_hom (add_monoid_hom.id M) = add_monoid_hom.id (α →₀ M) := add_monoid_hom.ext map_range_id lemma map_range.add_monoid_hom_comp (f : N →+ P) (f₂ : M →+ N) : (map_range.add_monoid_hom (f.comp f₂) : (α →₀ _) →+ _) = (map_range.add_monoid_hom f).comp (map_range.add_monoid_hom f₂) := add_monoid_hom.ext $ map_range_comp _ _ _ _ _ @[simp] lemma map_range.add_monoid_hom_to_zero_hom (f : M →+ N) : (map_range.add_monoid_hom f).to_zero_hom = (map_range.zero_hom f.to_zero_hom : zero_hom (α →₀ _) _) := zero_hom.ext $ λ _, rfl lemma map_range_multiset_sum (f : M →+ N) (m : multiset (α →₀ M)) : map_range f f.map_zero m.sum = (m.map $ λx, map_range f f.map_zero x).sum := (map_range.add_monoid_hom f : (α →₀ _) →+ _).map_multiset_sum _ lemma map_range_finset_sum (f : M →+ N) (s : finset ι) (g : ι → (α →₀ M)) : map_range f f.map_zero (∑ x in s, g x) = ∑ x in s, map_range f f.map_zero (g x) := (map_range.add_monoid_hom f : (α →₀ _) →+ _).map_sum _ _ /-- `finsupp.map_range.add_monoid_hom` as an equiv. -/ @[simps apply] def map_range.add_equiv (f : M ≃+ N) : (α →₀ M) ≃+ (α →₀ N) := { to_fun := (map_range f f.map_zero : (α →₀ M) → (α →₀ N)), inv_fun := (map_range f.symm f.symm.map_zero : (α →₀ N) → (α →₀ M)), left_inv := λ x, begin rw ←map_range_comp _ _ _ _; simp_rw add_equiv.symm_comp_self, { exact map_range_id _ }, { refl }, end, right_inv := λ x, begin rw ←map_range_comp _ _ _ _; simp_rw add_equiv.self_comp_symm, { exact map_range_id _ }, { refl }, end, ..(map_range.add_monoid_hom f.to_add_monoid_hom) } @[simp] lemma map_range.add_equiv_refl : map_range.add_equiv (add_equiv.refl M) = add_equiv.refl (α →₀ M) := add_equiv.ext map_range_id lemma map_range.add_equiv_trans (f : M ≃+ N) (f₂ : N ≃+ P) : (map_range.add_equiv (f.trans f₂) : (α →₀ _) ≃+ _) = (map_range.add_equiv f).trans (map_range.add_equiv f₂) := add_equiv.ext $ map_range_comp _ _ _ _ _ @[simp] lemma map_range.add_equiv_symm (f : M ≃+ N) : ((map_range.add_equiv f).symm : (α →₀ _) ≃+ _) = map_range.add_equiv f.symm := add_equiv.ext $ λ x, rfl @[simp] lemma map_range.add_equiv_to_add_monoid_hom (f : M ≃+ N) : (map_range.add_equiv f : (α →₀ _) ≃+ _).to_add_monoid_hom = (map_range.add_monoid_hom f.to_add_monoid_hom : (α →₀ _) →+ _) := add_monoid_hom.ext $ λ _, rfl @[simp] lemma map_range.add_equiv_to_equiv (f : M ≃+ N) : (map_range.add_equiv f).to_equiv = (map_range.equiv f.to_equiv f.map_zero f.symm.map_zero : (α →₀ _) ≃ _) := equiv.ext $ λ _, rfl end add_monoid_hom end finsupp end map_range /-! ### Declarations about `equiv_congr_left` -/ section equiv_congr_left variable [has_zero M] namespace finsupp /-- Given `f : α ≃ β`, we can map `l : α →₀ M` to `equiv_map_domain f l : β →₀ M` (computably) by mapping the support forwards and the function backwards. -/ def equiv_map_domain (f : α ≃ β) (l : α →₀ M) : β →₀ M := { support := l.support.map f.to_embedding, to_fun := λ a, l (f.symm a), mem_support_to_fun := λ a, by simp only [finset.mem_map_equiv, mem_support_to_fun]; refl } @[simp] lemma equiv_map_domain_apply (f : α ≃ β) (l : α →₀ M) (b : β) : equiv_map_domain f l b = l (f.symm b) := rfl lemma equiv_map_domain_symm_apply (f : α ≃ β) (l : β →₀ M) (a : α) : equiv_map_domain f.symm l a = l (f a) := rfl @[simp] lemma equiv_map_domain_refl (l : α →₀ M) : equiv_map_domain (equiv.refl _) l = l := by ext x; refl lemma equiv_map_domain_refl' : equiv_map_domain (equiv.refl _) = @id (α →₀ M) := by ext x; refl lemma equiv_map_domain_trans (f : α ≃ β) (g : β ≃ γ) (l : α →₀ M) : equiv_map_domain (f.trans g) l = equiv_map_domain g (equiv_map_domain f l) := by ext x; refl lemma equiv_map_domain_trans' (f : α ≃ β) (g : β ≃ γ) : @equiv_map_domain _ _ M _ (f.trans g) = equiv_map_domain g ∘ equiv_map_domain f := by ext x; refl @[simp] lemma equiv_map_domain_single (f : α ≃ β) (a : α) (b : M) : equiv_map_domain f (single a b) = single (f a) b := begin classical, ext x, simp only [single_apply, equiv.apply_eq_iff_eq_symm_apply, equiv_map_domain_apply], end @[simp] lemma equiv_map_domain_zero {f : α ≃ β} : equiv_map_domain f (0 : α →₀ M) = (0 : β →₀ M) := by ext x; simp only [equiv_map_domain_apply, coe_zero, pi.zero_apply] /-- Given `f : α ≃ β`, the finitely supported function spaces are also in bijection: `(α →₀ M) ≃ (β →₀ M)`. This is the finitely-supported version of `equiv.Pi_congr_left`. -/ def equiv_congr_left (f : α ≃ β) : (α →₀ M) ≃ (β →₀ M) := by refine ⟨equiv_map_domain f, equiv_map_domain f.symm, λ f, _, λ f, _⟩; ext x; simp only [equiv_map_domain_apply, equiv.symm_symm, equiv.symm_apply_apply, equiv.apply_symm_apply] @[simp] lemma equiv_congr_left_apply (f : α ≃ β) (l : α →₀ M) : equiv_congr_left f l = equiv_map_domain f l := rfl @[simp] lemma equiv_congr_left_symm (f : α ≃ β) : (@equiv_congr_left _ _ M _ f).symm = equiv_congr_left f.symm := rfl end finsupp end equiv_congr_left section cast_finsupp variables [has_zero M] (f : α →₀ M) namespace nat @[simp, norm_cast] lemma cast_finsupp_prod [comm_semiring R] (g : α → M → ℕ) : (↑(f.prod g) : R) = f.prod (λ a b, ↑(g a b)) := nat.cast_prod _ _ @[simp, norm_cast] lemma cast_finsupp_sum [comm_semiring R] (g : α → M → ℕ) : (↑(f.sum g) : R) = f.sum (λ a b, ↑(g a b)) := nat.cast_sum _ _ end nat namespace int @[simp, norm_cast] lemma cast_finsupp_prod [comm_ring R] (g : α → M → ℤ) : (↑(f.prod g) : R) = f.prod (λ a b, ↑(g a b)) := int.cast_prod _ _ @[simp, norm_cast] lemma cast_finsupp_sum [comm_ring R] (g : α → M → ℤ) : (↑(f.sum g) : R) = f.sum (λ a b, ↑(g a b)) := int.cast_sum _ _ end int namespace rat @[simp, norm_cast] lemma cast_finsupp_sum [division_ring R] [char_zero R] (g : α → M → ℚ) : (↑(f.sum g) : R) = f.sum (λ a b, g a b) := cast_sum _ _ @[simp, norm_cast] lemma cast_finsupp_prod [field R] [char_zero R] (g : α → M → ℚ) : (↑(f.prod g) : R) = f.prod (λ a b, g a b) := cast_prod _ _ end rat end cast_finsupp /-! ### Declarations about `map_domain` -/ namespace finsupp section map_domain variables [add_comm_monoid M] {v v₁ v₂ : α →₀ M} /-- Given `f : α → β` and `v : α →₀ M`, `map_domain f v : β →₀ M` is the finitely supported function whose value at `a : β` is the sum of `v x` over all `x` such that `f x = a`. -/ def map_domain (f : α → β) (v : α →₀ M) : β →₀ M := v.sum $ λa, single (f a) lemma map_domain_apply {f : α → β} (hf : function.injective f) (x : α →₀ M) (a : α) : map_domain f x (f a) = x a := begin rw [map_domain, sum_apply, sum, finset.sum_eq_single a, single_eq_same], { assume b _ hba, exact single_eq_of_ne (hf.ne hba) }, { assume h, rw [not_mem_support_iff.1 h, single_zero, zero_apply] } end lemma map_domain_notin_range {f : α → β} (x : α →₀ M) (a : β) (h : a ∉ set.range f) : map_domain f x a = 0 := begin rw [map_domain, sum_apply, sum], exact finset.sum_eq_zero (assume a' h', single_eq_of_ne $ assume eq, h $ eq ▸ set.mem_range_self _) end @[simp] lemma map_domain_id : map_domain id v = v := sum_single _ lemma map_domain_comp {f : α → β} {g : β → γ} : map_domain (g ∘ f) v = map_domain g (map_domain f v) := begin refine ((sum_sum_index _ _).trans _).symm, { intro, exact single_zero _ }, { intro, exact single_add _ }, refine sum_congr (λ _ _, sum_single_index _), { exact single_zero _ } end @[simp] lemma map_domain_single {f : α → β} {a : α} {b : M} : map_domain f (single a b) = single (f a) b := sum_single_index $ single_zero _ @[simp] lemma map_domain_zero {f : α → β} : map_domain f (0 : α →₀ M) = (0 : β →₀ M) := sum_zero_index lemma map_domain_congr {f g : α → β} (h : ∀x∈v.support, f x = g x) : v.map_domain f = v.map_domain g := finset.sum_congr rfl $ λ _ H, by simp only [h _ H] lemma map_domain_add {f : α → β} : map_domain f (v₁ + v₂) = map_domain f v₁ + map_domain f v₂ := sum_add_index' (λ _, single_zero _) (λ _, single_add _) @[simp] lemma map_domain_equiv_apply {f : α ≃ β} (x : α →₀ M) (a : β) : map_domain f x a = x (f.symm a) := begin conv_lhs { rw ←f.apply_symm_apply a }, exact map_domain_apply f.injective _ _, end /-- `finsupp.map_domain` is an `add_monoid_hom`. -/ @[simps] def map_domain.add_monoid_hom (f : α → β) : (α →₀ M) →+ (β →₀ M) := { to_fun := map_domain f, map_zero' := map_domain_zero, map_add' := λ _ _, map_domain_add} @[simp] lemma map_domain.add_monoid_hom_id : map_domain.add_monoid_hom id = add_monoid_hom.id (α →₀ M) := add_monoid_hom.ext $ λ _, map_domain_id lemma map_domain.add_monoid_hom_comp (f : β → γ) (g : α → β) : (map_domain.add_monoid_hom (f ∘ g) : (α →₀ M) →+ (γ →₀ M)) = (map_domain.add_monoid_hom f).comp (map_domain.add_monoid_hom g) := add_monoid_hom.ext $ λ _, map_domain_comp lemma map_domain_finset_sum {f : α → β} {s : finset ι} {v : ι → α →₀ M} : map_domain f (∑ i in s, v i) = ∑ i in s, map_domain f (v i) := (map_domain.add_monoid_hom f : (α →₀ M) →+ β →₀ M).map_sum _ _ lemma map_domain_sum [has_zero N] {f : α → β} {s : α →₀ N} {v : α → N → α →₀ M} : map_domain f (s.sum v) = s.sum (λa b, map_domain f (v a b)) := (map_domain.add_monoid_hom f : (α →₀ M) →+ β →₀ M).map_finsupp_sum _ _ lemma map_domain_support [decidable_eq β] {f : α → β} {s : α →₀ M} : (s.map_domain f).support ⊆ s.support.image f := finset.subset.trans support_sum $ finset.subset.trans (finset.bUnion_mono $ assume a ha, support_single_subset) $ by rw [finset.bUnion_singleton]; exact subset.refl _ lemma map_domain_apply' (S : set α) {f : α → β} (x : α →₀ M) (hS : (x.support : set α) ⊆ S) (hf : set.inj_on f S) {a : α} (ha : a ∈ S) : map_domain f x (f a) = x a := begin classical, rw [map_domain, sum_apply, sum], simp_rw single_apply, by_cases hax : a ∈ x.support, { rw [← finset.add_sum_erase _ _ hax, if_pos rfl], convert add_zero _, refine finset.sum_eq_zero (λ i hi, if_neg _), exact (hf.mono hS).ne (finset.mem_of_mem_erase hi) hax (finset.ne_of_mem_erase hi), }, { rw not_mem_support_iff.1 hax, refine finset.sum_eq_zero (λ i hi, if_neg _), exact hf.ne (hS hi) ha (ne_of_mem_of_not_mem hi hax) } end lemma map_domain_support_of_inj_on [decidable_eq β] {f : α → β} (s : α →₀ M) (hf : set.inj_on f s.support) : (map_domain f s).support = finset.image f s.support := finset.subset.antisymm map_domain_support $ begin intros x hx, simp only [mem_image, exists_prop, mem_support_iff, ne.def] at hx, rcases hx with ⟨hx_w, hx_h_left, rfl⟩, simp only [mem_support_iff, ne.def], rw map_domain_apply' (↑s.support : set _) _ _ hf, { exact hx_h_left, }, { simp only [mem_coe, mem_support_iff, ne.def], exact hx_h_left, }, { exact subset.refl _, }, end lemma map_domain_support_of_injective [decidable_eq β] {f : α → β} (hf : function.injective f) (s : α →₀ M) : (map_domain f s).support = finset.image f s.support := map_domain_support_of_inj_on s (hf.inj_on _) @[to_additive] lemma prod_map_domain_index [comm_monoid N] {f : α → β} {s : α →₀ M} {h : β → M → N} (h_zero : ∀b, h b 0 = 1) (h_add : ∀b m₁ m₂, h b (m₁ + m₂) = h b m₁ * h b m₂) : (map_domain f s).prod h = s.prod (λa m, h (f a) m) := (prod_sum_index h_zero h_add).trans $ prod_congr $ λ _ _, prod_single_index (h_zero _) /-- A version of `sum_map_domain_index` that takes a bundled `add_monoid_hom`, rather than separate linearity hypotheses. -/ -- Note that in `prod_map_domain_index`, `M` is still an additive monoid, -- so there is no analogous version in terms of `monoid_hom`. @[simp] lemma sum_map_domain_index_add_monoid_hom [add_comm_monoid N] {f : α → β} {s : α →₀ M} (h : β → M →+ N) : (map_domain f s).sum (λ b m, h b m) = s.sum (λ a m, h (f a) m) := @sum_map_domain_index _ _ _ _ _ _ _ _ (λ b m, h b m) (λ b, (h b).map_zero) (λ b m₁ m₂, (h b).map_add _ _) lemma emb_domain_eq_map_domain (f : α ↪ β) (v : α →₀ M) : emb_domain f v = map_domain f v := begin ext a, by_cases a ∈ set.range f, { rcases h with ⟨a, rfl⟩, rw [map_domain_apply f.injective, emb_domain_apply] }, { rw [map_domain_notin_range, emb_domain_notin_range]; assumption } end @[to_additive] lemma prod_map_domain_index_inj [comm_monoid N] {f : α → β} {s : α →₀ M} {h : β → M → N} (hf : function.injective f) : (s.map_domain f).prod h = s.prod (λa b, h (f a) b) := by rw [←function.embedding.coe_fn_mk f hf, ←emb_domain_eq_map_domain, prod_emb_domain] lemma map_domain_injective {f : α → β} (hf : function.injective f) : function.injective (map_domain f : (α →₀ M) → (β →₀ M)) := begin assume v₁ v₂ eq, ext a, have : map_domain f v₁ (f a) = map_domain f v₂ (f a), { rw eq }, rwa [map_domain_apply hf, map_domain_apply hf] at this, end /-- When `f` is an embedding we have an embedding `(α →₀ ℕ) ↪ (β →₀ ℕ)` given by `map_domain`. -/ @[simps] def map_domain_embedding {α β : Type*} (f : α ↪ β) : (α →₀ ℕ) ↪ β →₀ ℕ := ⟨finsupp.map_domain f, finsupp.map_domain_injective f.injective⟩ lemma map_domain.add_monoid_hom_comp_map_range [add_comm_monoid N] (f : α → β) (g : M →+ N) : (map_domain.add_monoid_hom f).comp (map_range.add_monoid_hom g) = (map_range.add_monoid_hom g).comp (map_domain.add_monoid_hom f) := by { ext, simp } /-- When `g` preserves addition, `map_range` and `map_domain` commute. -/ lemma map_domain_map_range [add_comm_monoid N] (f : α → β) (v : α →₀ M) (g : M → N) (h0 : g 0 = 0) (hadd : ∀ x y, g (x + y) = g x + g y) : map_domain f (map_range g h0 v) = map_range g h0 (map_domain f v) := let g' : M →+ N := { to_fun := g, map_zero' := h0, map_add' := hadd} in add_monoid_hom.congr_fun (map_domain.add_monoid_hom_comp_map_range f g') v lemma sum_update_add [add_comm_monoid α] [add_comm_monoid β] (f : ι →₀ α) (i : ι) (a : α) (g : ι → α → β) (hg : ∀ i, g i 0 = 0) (hgg : ∀ (j : ι) (a₁ a₂ : α), g j (a₁ + a₂) = g j a₁ + g j a₂) : (f.update i a).sum g + g i (f i) = f.sum g + g i a := begin rw [update_eq_erase_add_single, sum_add_index' hg hgg], conv_rhs { rw ← finsupp.update_self f i }, rw [update_eq_erase_add_single, sum_add_index' hg hgg, add_assoc, add_assoc], congr' 1, rw [add_comm, sum_single_index (hg _), sum_single_index (hg _)], end lemma map_domain_inj_on (S : set α) {f : α → β} (hf : set.inj_on f S) : set.inj_on (map_domain f : (α →₀ M) → (β →₀ M)) {w | (w.support : set α) ⊆ S} := begin intros v₁ hv₁ v₂ hv₂ eq, ext a, classical, by_cases h : a ∈ v₁.support ∪ v₂.support, { rw [← map_domain_apply' S _ hv₁ hf _, ← map_domain_apply' S _ hv₂ hf _, eq]; { apply set.union_subset hv₁ hv₂, exact_mod_cast h, }, }, { simp only [decidable.not_or_iff_and_not, mem_union, not_not, mem_support_iff] at h, simp [h], }, end lemma equiv_map_domain_eq_map_domain {M} [add_comm_monoid M] (f : α ≃ β) (l : α →₀ M) : equiv_map_domain f l = map_domain f l := by ext x; simp [map_domain_equiv_apply] end map_domain /-! ### Declarations about `comap_domain` -/ section comap_domain /-- Given `f : α → β`, `l : β →₀ M` and a proof `hf` that `f` is injective on the preimage of `l.support`, `comap_domain f l hf` is the finitely supported function from `α` to `M` given by composing `l` with `f`. -/ @[simps support] def comap_domain [has_zero M] (f : α → β) (l : β →₀ M) (hf : set.inj_on f (f ⁻¹' ↑l.support)) : α →₀ M := { support := l.support.preimage f hf, to_fun := (λ a, l (f a)), mem_support_to_fun := begin intros a, simp only [finset.mem_def.symm, finset.mem_preimage], exact l.mem_support_to_fun (f a), end } @[simp] lemma comap_domain_apply [has_zero M] (f : α → β) (l : β →₀ M) (hf : set.inj_on f (f ⁻¹' ↑l.support)) (a : α) : comap_domain f l hf a = l (f a) := rfl lemma sum_comap_domain [has_zero M] [add_comm_monoid N] (f : α → β) (l : β →₀ M) (g : β → M → N) (hf : set.bij_on f (f ⁻¹' ↑l.support) ↑l.support) : (comap_domain f l hf.inj_on).sum (g ∘ f) = l.sum g := begin simp only [sum, comap_domain_apply, (∘)], simp [comap_domain, finset.sum_preimage_of_bij f _ _ (λ x, g x (l x))], end lemma eq_zero_of_comap_domain_eq_zero [add_comm_monoid M] (f : α → β) (l : β →₀ M) (hf : set.bij_on f (f ⁻¹' ↑l.support) ↑l.support) : comap_domain f l hf.inj_on = 0 → l = 0 := begin rw [← support_eq_empty, ← support_eq_empty, comap_domain], simp only [finset.ext_iff, finset.not_mem_empty, iff_false, mem_preimage], assume h a ha, cases hf.2.2 ha with b hb, exact h b (hb.2.symm ▸ ha) end section f_injective section has_zero variables [has_zero M] /-- Note the `hif` argument is needed for this to work in `rw`. -/ @[simp] lemma comap_domain_zero (f : α → β) (hif : set.inj_on f (f ⁻¹' ↑((0 : β →₀ M).support)) := set.inj_on_empty _) : comap_domain f (0 : β →₀ M) hif = (0 : α →₀ M) := by { ext, refl } @[simp] lemma comap_domain_single (f : α → β) (a : α) (m : M) (hif : set.inj_on f (f ⁻¹' (single (f a) m).support)) : comap_domain f (finsupp.single (f a) m) hif = finsupp.single a m := begin rcases eq_or_ne m 0 with rfl | hm, { simp only [single_zero, comap_domain_zero] }, { rw [eq_single_iff, comap_domain_apply, comap_domain_support, ← finset.coe_subset, coe_preimage, support_single_ne_zero _ hm, coe_singleton, coe_singleton, single_eq_same], rw [support_single_ne_zero _ hm, coe_singleton] at hif, exact ⟨λ x hx, hif hx rfl hx, rfl⟩ } end end has_zero section add_zero_class variables [add_zero_class M] {f : α → β} lemma comap_domain_add (v₁ v₂ : β →₀ M) (hv₁ : set.inj_on f (f ⁻¹' ↑(v₁.support))) (hv₂ : set.inj_on f (f ⁻¹' ↑(v₂.support))) (hv₁₂ : set.inj_on f (f ⁻¹' ↑((v₁ + v₂).support))) : comap_domain f (v₁ + v₂) hv₁₂ = comap_domain f v₁ hv₁ + comap_domain f v₂ hv₂ := by { ext, simp only [comap_domain_apply, coe_add, pi.add_apply] } /-- A version of `finsupp.comap_domain_add` that's easier to use. -/ lemma comap_domain_add_of_injective (hf : function.injective f) (v₁ v₂ : β →₀ M) : comap_domain f (v₁ + v₂) (hf.inj_on _) = comap_domain f v₁ (hf.inj_on _) + comap_domain f v₂ (hf.inj_on _) := comap_domain_add _ _ _ _ _ /-- `finsupp.comap_domain` is an `add_monoid_hom`. -/ @[simps] def comap_domain.add_monoid_hom (hf : function.injective f) : (β →₀ M) →+ (α →₀ M) := { to_fun := λ x, comap_domain f x (hf.inj_on _), map_zero' := comap_domain_zero f, map_add' := comap_domain_add_of_injective hf } end add_zero_class variables [add_comm_monoid M] (f : α → β) lemma map_domain_comap_domain (hf : function.injective f) (l : β →₀ M) (hl : ↑l.support ⊆ set.range f) : map_domain f (comap_domain f l (hf.inj_on _)) = l := begin ext a, by_cases h_cases: a ∈ set.range f, { rcases set.mem_range.1 h_cases with ⟨b, hb⟩, rw [hb.symm, map_domain_apply hf, comap_domain_apply] }, { rw map_domain_notin_range _ _ h_cases, by_contra h_contr, apply h_cases (hl $ finset.mem_coe.2 $ mem_support_iff.2 $ λ h, h_contr h.symm) } end end f_injective end comap_domain /-! ### Declarations about finitely supported functions whose support is an `option` type -/ section option /-- Restrict a finitely supported function on `option α` to a finitely supported function on `α`. -/ def some [has_zero M] (f : option α →₀ M) : α →₀ M := f.comap_domain option.some (λ _, by simp) @[simp] lemma some_apply [has_zero M] (f : option α →₀ M) (a : α) : f.some a = f (option.some a) := rfl @[simp] lemma some_zero [has_zero M] : (0 : option α →₀ M).some = 0 := by { ext, simp, } @[simp] lemma some_add [add_comm_monoid M] (f g : option α →₀ M) : (f + g).some = f.some + g.some := by { ext, simp, } @[simp] lemma some_single_none [has_zero M] (m : M) : (single none m : option α →₀ M).some = 0 := by { ext, simp, } @[simp] lemma some_single_some [has_zero M] (a : α) (m : M) : (single (option.some a) m : option α →₀ M).some = single a m := by { classical, ext b, simp [single_apply], } @[to_additive] lemma prod_option_index [add_comm_monoid M] [comm_monoid N] (f : option α →₀ M) (b : option α → M → N) (h_zero : ∀ o, b o 0 = 1) (h_add : ∀ o m₁ m₂, b o (m₁ + m₂) = b o m₁ * b o m₂) : f.prod b = b none (f none) * f.some.prod (λ a, b (option.some a)) := begin classical, apply induction_linear f, { simp [some_zero, h_zero], }, { intros f₁ f₂ h₁ h₂, rw [finsupp.prod_add_index, h₁, h₂, some_add, finsupp.prod_add_index], simp only [h_add, pi.add_apply, finsupp.coe_add], rw mul_mul_mul_comm, all_goals { simp [h_zero, h_add], }, }, { rintros (_|a) m; simp [h_zero, h_add], } end lemma sum_option_index_smul [semiring R] [add_comm_monoid M] [module R M] (f : option α →₀ R) (b : option α → M) : f.sum (λ o r, r • b o) = f none • b none + f.some.sum (λ a r, r • b (option.some a)) := f.sum_option_index _ (λ _, zero_smul _ _) (λ _ _ _, add_smul _ _ _) end option /-! ### Declarations about `filter` -/ section filter section has_zero variables [has_zero M] (p : α → Prop) (f : α →₀ M) /-- `filter p f` is the finitely supported function that is `f a` if `p a` is true and 0 otherwise. -/ def filter (p : α → Prop) (f : α →₀ M) : α →₀ M := { to_fun := λ a, by haveI := classical.dec_pred p; exact if p a then f a else 0, support := by haveI := classical.dec_pred p; exact f.support.filter (λ a, p a), mem_support_to_fun := λ a, by split_ifs; { simp only [h, mem_filter, mem_support_iff], tauto } } lemma filter_apply (a : α) [D : decidable (p a)] : f.filter p a = if p a then f a else 0 := by rw subsingleton.elim D; refl lemma filter_eq_indicator : ⇑(f.filter p) = set.indicator {x | p x} f := rfl lemma filter_eq_zero_iff : f.filter p = 0 ↔ ∀ x, p x → f x = 0 := by simp only [fun_like.ext_iff, filter_eq_indicator, zero_apply, set.indicator_apply_eq_zero, set.mem_set_of_eq] lemma filter_eq_self_iff : f.filter p = f ↔ ∀ x, f x ≠ 0 → p x := by simp only [fun_like.ext_iff, filter_eq_indicator, set.indicator_apply_eq_self, set.mem_set_of_eq, not_imp_comm] @[simp] lemma filter_apply_pos {a : α} (h : p a) : f.filter p a = f a := by { classical, convert if_pos h } @[simp] lemma filter_apply_neg {a : α} (h : ¬ p a) : f.filter p a = 0 := by { classical, convert if_neg h } @[simp] lemma support_filter [D : decidable_pred p] : (f.filter p).support = f.support.filter p := by rw subsingleton.elim D; refl lemma filter_zero : (0 : α →₀ M).filter p = 0 := by { classical, rw [← support_eq_empty, support_filter, support_zero, finset.filter_empty] } @[simp] lemma filter_single_of_pos {a : α} {b : M} (h : p a) : (single a b).filter p = single a b := (filter_eq_self_iff _ _).2 $ λ x hx, (single_apply_ne_zero.1 hx).1.symm ▸ h @[simp] lemma filter_single_of_neg {a : α} {b : M} (h : ¬ p a) : (single a b).filter p = 0 := (filter_eq_zero_iff _ _).2 $ λ x hpx, single_apply_eq_zero.2 $ λ hxa, absurd hpx (hxa.symm ▸ h) @[to_additive] lemma prod_filter_index [comm_monoid N] (g : α → M → N) : (f.filter p).prod g = ∏ x in (f.filter p).support, g x (f x) := begin classical, refine finset.prod_congr rfl (λ x hx, _), rw [support_filter, finset.mem_filter] at hx, rw [filter_apply_pos _ _ hx.2] end @[simp, to_additive] lemma prod_filter_mul_prod_filter_not [comm_monoid N] (g : α → M → N) : (f.filter p).prod g * (f.filter (λ a, ¬ p a)).prod g = f.prod g := begin classical, simp_rw [prod_filter_index, support_filter, prod_filter_mul_prod_filter_not, finsupp.prod] end @[simp, to_additive] lemma prod_div_prod_filter [comm_group G] (g : α → M → G) : f.prod g / (f.filter p).prod g = (f.filter (λ a, ¬p a)).prod g := div_eq_of_eq_mul' (prod_filter_mul_prod_filter_not _ _ _).symm end has_zero lemma filter_pos_add_filter_neg [add_zero_class M] (f : α →₀ M) (p : α → Prop) : f.filter p + f.filter (λa, ¬ p a) = f := coe_fn_injective $ set.indicator_self_add_compl {x | p x} f end filter /-! ### Declarations about `frange` -/ section frange variables [has_zero M] /-- `frange f` is the image of `f` on the support of `f`. -/ def frange (f : α →₀ M) : finset M := by haveI := classical.dec_eq M; exact finset.image f f.support theorem mem_frange {f : α →₀ M} {y : M} : y ∈ f.frange ↔ y ≠ 0 ∧ ∃ x, f x = y := by classical; exact finset.mem_image.trans ⟨λ ⟨x, hx1, hx2⟩, ⟨hx2 ▸ mem_support_iff.1 hx1, x, hx2⟩, λ ⟨hy, x, hx⟩, ⟨x, mem_support_iff.2 (hx.symm ▸ hy), hx⟩⟩ theorem zero_not_mem_frange {f : α →₀ M} : (0:M) ∉ f.frange := λ H, (mem_frange.1 H).1 rfl theorem frange_single {x : α} {y : M} : frange (single x y) ⊆ {y} := λ r hr, let ⟨t, ht1, ht2⟩ := mem_frange.1 hr in ht2 ▸ begin classical, rw single_apply at ht2 ⊢, split_ifs at ht2 ⊢, { exact finset.mem_singleton_self _ }, { exact (t ht2.symm).elim } end end frange /-! ### Declarations about `subtype_domain` -/ section subtype_domain section zero variables [has_zero M] {p : α → Prop} /-- `subtype_domain p f` is the restriction of the finitely supported function `f` to subtype `p`. -/ def subtype_domain (p : α → Prop) (f : α →₀ M) : (subtype p →₀ M) := { support := by haveI := classical.dec_pred p; exact f.support.subtype p, to_fun := f ∘ coe, mem_support_to_fun := λ a, by simp only [mem_subtype, mem_support_iff] } @[simp] lemma support_subtype_domain [D : decidable_pred p] {f : α →₀ M} : (subtype_domain p f).support = f.support.subtype p := by rw subsingleton.elim D; refl @[simp] lemma subtype_domain_apply {a : subtype p} {v : α →₀ M} : (subtype_domain p v) a = v (a.val) := rfl @[simp] lemma subtype_domain_zero : subtype_domain p (0 : α →₀ M) = 0 := rfl lemma subtype_domain_eq_zero_iff' {f : α →₀ M} : f.subtype_domain p = 0 ↔ ∀ x, p x → f x = 0 := begin classical, simp_rw [← support_eq_empty, support_subtype_domain, subtype_eq_empty, not_mem_support_iff] end lemma subtype_domain_eq_zero_iff {f : α →₀ M} (hf : ∀ x ∈ f.support , p x) : f.subtype_domain p = 0 ↔ f = 0 := subtype_domain_eq_zero_iff'.trans ⟨λ H, ext $ λ x, by classical; exact if hx : p x then H x hx else not_mem_support_iff.1 $ mt (hf x) hx, λ H x _, by simp [H]⟩ @[to_additive] lemma prod_subtype_domain_index [comm_monoid N] {v : α →₀ M} {h : α → M → N} (hp : ∀x∈v.support, p x) : (v.subtype_domain p).prod (λa b, h a b) = v.prod h := prod_bij (λp _, p.val) (λ _, by classical; exact mem_subtype.1) (λ _ _, rfl) (λ _ _ _ _, subtype.eq) (λ b hb, ⟨⟨b, hp b hb⟩, by classical; exact mem_subtype.2 hb, rfl⟩) end zero section add_zero_class variables [add_zero_class M] {p : α → Prop} {v v' : α →₀ M} @[simp] lemma subtype_domain_add {v v' : α →₀ M} : (v + v').subtype_domain p = v.subtype_domain p + v'.subtype_domain p := ext $ λ _, rfl /-- `subtype_domain` but as an `add_monoid_hom`. -/ def subtype_domain_add_monoid_hom : (α →₀ M) →+ subtype p →₀ M := { to_fun := subtype_domain p, map_zero' := subtype_domain_zero, map_add' := λ _ _, subtype_domain_add } /-- `finsupp.filter` as an `add_monoid_hom`. -/ def filter_add_hom (p : α → Prop) : (α →₀ M) →+ (α →₀ M) := { to_fun := filter p, map_zero' := filter_zero p, map_add' := λ f g, coe_fn_injective $ set.indicator_add {x | p x} f g } @[simp] lemma filter_add {v v' : α →₀ M} : (v + v').filter p = v.filter p + v'.filter p := (filter_add_hom p).map_add v v' end add_zero_class section comm_monoid variables [add_comm_monoid M] {p : α → Prop} lemma subtype_domain_sum {s : finset ι} {h : ι → α →₀ M} : (∑ c in s, h c).subtype_domain p = ∑ c in s, (h c).subtype_domain p := (subtype_domain_add_monoid_hom : _ →+ subtype p →₀ M).map_sum _ s lemma subtype_domain_finsupp_sum [has_zero N] {s : β →₀ N} {h : β → N → α →₀ M} : (s.sum h).subtype_domain p = s.sum (λc d, (h c d).subtype_domain p) := subtype_domain_sum lemma filter_sum (s : finset ι) (f : ι → α →₀ M) : (∑ a in s, f a).filter p = ∑ a in s, filter p (f a) := (filter_add_hom p : (α →₀ M) →+ _).map_sum f s lemma filter_eq_sum (p : α → Prop) [D : decidable_pred p] (f : α →₀ M) : f.filter p = ∑ i in f.support.filter p, single i (f i) := (f.filter p).sum_single.symm.trans $ finset.sum_congr (by rw subsingleton.elim D; refl) $ λ x hx, by rw [filter_apply_pos _ _ (mem_filter.1 hx).2] end comm_monoid section group variables [add_group G] {p : α → Prop} {v v' : α →₀ G} @[simp] lemma subtype_domain_neg : (- v).subtype_domain p = - v.subtype_domain p := ext $ λ _, rfl @[simp] lemma subtype_domain_sub : (v - v').subtype_domain p = v.subtype_domain p - v'.subtype_domain p := ext $ λ _, rfl @[simp] lemma single_neg (a : α) (b : G) : single a (-b) = -single a b := (single_add_hom a : G →+ _).map_neg b @[simp] lemma single_sub (a : α) (b₁ b₂ : G) : single a (b₁ - b₂) = single a b₁ - single a b₂ := (single_add_hom a : G →+ _).map_sub b₁ b₂ @[simp] lemma erase_neg (a : α) (f : α →₀ G) : erase a (-f) = -erase a f := (erase_add_hom a : (_ →₀ G) →+ _).map_neg f @[simp] lemma erase_sub (a : α) (f₁ f₂ : α →₀ G) : erase a (f₁ - f₂) = erase a f₁ - erase a f₂ := (erase_add_hom a : (_ →₀ G) →+ _).map_sub f₁ f₂ @[simp] lemma filter_neg (p : α → Prop) (f : α →₀ G) : filter p (-f) = -filter p f := (filter_add_hom p : (_ →₀ G) →+ _).map_neg f @[simp] lemma filter_sub (p : α → Prop) (f₁ f₂ : α →₀ G) : filter p (f₁ - f₂) = filter p f₁ - filter p f₂ := (filter_add_hom p : (_ →₀ G) →+ _).map_sub f₁ f₂ end group end subtype_domain lemma mem_support_multiset_sum [add_comm_monoid M] {s : multiset (α →₀ M)} (a : α) : a ∈ s.sum.support → ∃f∈s, a ∈ (f : α →₀ M).support := multiset.induction_on s false.elim begin assume f s ih ha, by_cases a ∈ f.support, { exact ⟨f, multiset.mem_cons_self _ _, h⟩ }, { simp only [multiset.sum_cons, mem_support_iff, add_apply, not_mem_support_iff.1 h, zero_add] at ha, rcases ih (mem_support_iff.2 ha) with ⟨f', h₀, h₁⟩, exact ⟨f', multiset.mem_cons_of_mem h₀, h₁⟩ } end lemma mem_support_finset_sum [add_comm_monoid M] {s : finset ι} {h : ι → α →₀ M} (a : α) (ha : a ∈ (∑ c in s, h c).support) : ∃ c ∈ s, a ∈ (h c).support := let ⟨f, hf, hfa⟩ := mem_support_multiset_sum a ha in let ⟨c, hc, eq⟩ := multiset.mem_map.1 hf in ⟨c, hc, eq.symm ▸ hfa⟩ /-! ### Declarations about `curry` and `uncurry` -/ section curry_uncurry variables [add_comm_monoid M] [add_comm_monoid N] /-- Given a finitely supported function `f` from a product type `α × β` to `γ`, `curry f` is the "curried" finitely supported function from `α` to the type of finitely supported functions from `β` to `γ`. -/ protected def curry (f : (α × β) →₀ M) : α →₀ (β →₀ M) := f.sum $ λp c, single p.1 (single p.2 c) @[simp] lemma curry_apply (f : (α × β) →₀ M) (x : α) (y : β) : f.curry x y = f (x, y) := begin classical, have : ∀ (b : α × β), single b.fst (single b.snd (f b)) x y = if b = (x, y) then f b else 0, { rintros ⟨b₁, b₂⟩, simp [single_apply, ite_apply, prod.ext_iff, ite_and], split_ifs; simp [single_apply, *] }, rw [finsupp.curry, sum_apply, sum_apply, finsupp.sum, finset.sum_eq_single, this, if_pos rfl], { intros b hb b_ne, rw [this b, if_neg b_ne] }, { intros hxy, rw [this (x, y), if_pos rfl, not_mem_support_iff.mp hxy] } end lemma sum_curry_index (f : (α × β) →₀ M) (g : α → β → M → N) (hg₀ : ∀ a b, g a b 0 = 0) (hg₁ : ∀a b c₀ c₁, g a b (c₀ + c₁) = g a b c₀ + g a b c₁) : f.curry.sum (λa f, f.sum (g a)) = f.sum (λp c, g p.1 p.2 c) := begin rw [finsupp.curry], transitivity, { exact sum_sum_index (assume a, sum_zero_index) (assume a b₀ b₁, sum_add_index' (assume a, hg₀ _ _) (assume c d₀ d₁, hg₁ _ _ _ _)) }, congr, funext p c, transitivity, { exact sum_single_index sum_zero_index }, exact sum_single_index (hg₀ _ _) end /-- Given a finitely supported function `f` from `α` to the type of finitely supported functions from `β` to `M`, `uncurry f` is the "uncurried" finitely supported function from `α × β` to `M`. -/ protected def uncurry (f : α →₀ (β →₀ M)) : (α × β) →₀ M := f.sum $ λa g, g.sum $ λb c, single (a, b) c /-- `finsupp_prod_equiv` defines the `equiv` between `((α × β) →₀ M)` and `(α →₀ (β →₀ M))` given by currying and uncurrying. -/ def finsupp_prod_equiv : ((α × β) →₀ M) ≃ (α →₀ (β →₀ M)) := { to_fun := finsupp.curry, inv_fun := finsupp.uncurry, left_inv := λ f, begin rw [finsupp.uncurry, sum_curry_index], { simp_rw [prod.mk.eta, sum_single], }, { intros, apply single_zero }, { intros, apply single_add } end, right_inv := λ f, by simp only [ finsupp.curry, finsupp.uncurry, sum_sum_index, sum_zero_index, sum_add_index, sum_single_index, single_zero, single_add, eq_self_iff_true, forall_true_iff, forall_3_true_iff, prod.mk.eta, (single_sum _ _ _).symm, sum_single] } lemma filter_curry (f : α × β →₀ M) (p : α → Prop) : (f.filter (λa:α×β, p a.1)).curry = f.curry.filter p := begin classical, rw [finsupp.curry, finsupp.curry, finsupp.sum, finsupp.sum, filter_sum, support_filter, sum_filter], refine finset.sum_congr rfl _, rintros ⟨a₁, a₂⟩ ha, dsimp only, split_ifs, { rw [filter_apply_pos, filter_single_of_pos]; exact h }, { rwa [filter_single_of_neg] } end lemma support_curry [decidable_eq α] (f : α × β →₀ M) : f.curry.support ⊆ f.support.image prod.fst := begin rw ← finset.bUnion_singleton, refine finset.subset.trans support_sum _, refine finset.bUnion_mono (assume a _, support_single_subset) end end curry_uncurry /-! ### Declarations about finitely supported functions whose support is a `sum` type -/ section sum /-- `finsupp.sum_elim f g` maps `inl x` to `f x` and `inr y` to `g y`. -/ def sum_elim {α β γ : Type*} [has_zero γ] (f : α →₀ γ) (g : β →₀ γ) : α ⊕ β →₀ γ := on_finset (by haveI := classical.dec_eq α; haveI := classical.dec_eq β; exact (f.support.map ⟨_, sum.inl_injective⟩) ∪ g.support.map ⟨_, sum.inr_injective⟩) (sum.elim f g) (λ ab h, by { cases ab with a b; simp only [sum.elim_inl, sum.elim_inr] at h; simpa }) @[simp] lemma coe_sum_elim {α β γ : Type*} [has_zero γ] (f : α →₀ γ) (g : β →₀ γ) : ⇑(sum_elim f g) = sum.elim f g := rfl lemma sum_elim_apply {α β γ : Type*} [has_zero γ] (f : α →₀ γ) (g : β →₀ γ) (x : α ⊕ β) : sum_elim f g x = sum.elim f g x := rfl lemma sum_elim_inl {α β γ : Type*} [has_zero γ] (f : α →₀ γ) (g : β →₀ γ) (x : α) : sum_elim f g (sum.inl x) = f x := rfl lemma sum_elim_inr {α β γ : Type*} [has_zero γ] (f : α →₀ γ) (g : β →₀ γ) (x : β) : sum_elim f g (sum.inr x) = g x := rfl /-- The equivalence between `(α ⊕ β) →₀ γ` and `(α →₀ γ) × (β →₀ γ)`. This is the `finsupp` version of `equiv.sum_arrow_equiv_prod_arrow`. -/ @[simps apply symm_apply] def sum_finsupp_equiv_prod_finsupp {α β γ : Type*} [has_zero γ] : ((α ⊕ β) →₀ γ) ≃ (α →₀ γ) × (β →₀ γ) := { to_fun := λ f, ⟨f.comap_domain sum.inl (sum.inl_injective.inj_on _), f.comap_domain sum.inr (sum.inr_injective.inj_on _)⟩, inv_fun := λ fg, sum_elim fg.1 fg.2, left_inv := λ f, by { ext ab, cases ab with a b; simp }, right_inv := λ fg, by { ext; simp } } lemma fst_sum_finsupp_equiv_prod_finsupp {α β γ : Type*} [has_zero γ] (f : (α ⊕ β) →₀ γ) (x : α) : (sum_finsupp_equiv_prod_finsupp f).1 x = f (sum.inl x) := rfl lemma snd_sum_finsupp_equiv_prod_finsupp {α β γ : Type*} [has_zero γ] (f : (α ⊕ β) →₀ γ) (y : β) : (sum_finsupp_equiv_prod_finsupp f).2 y = f (sum.inr y) := rfl lemma sum_finsupp_equiv_prod_finsupp_symm_inl {α β γ : Type*} [has_zero γ] (fg : (α →₀ γ) × (β →₀ γ)) (x : α) : (sum_finsupp_equiv_prod_finsupp.symm fg) (sum.inl x) = fg.1 x := rfl lemma sum_finsupp_equiv_prod_finsupp_symm_inr {α β γ : Type*} [has_zero γ] (fg : (α →₀ γ) × (β →₀ γ)) (y : β) : (sum_finsupp_equiv_prod_finsupp.symm fg) (sum.inr y) = fg.2 y := rfl variables [add_monoid M] /-- The additive equivalence between `(α ⊕ β) →₀ M` and `(α →₀ M) × (β →₀ M)`. This is the `finsupp` version of `equiv.sum_arrow_equiv_prod_arrow`. -/ @[simps apply symm_apply] def sum_finsupp_add_equiv_prod_finsupp {α β : Type*} : ((α ⊕ β) →₀ M) ≃+ (α →₀ M) × (β →₀ M) := { map_add' := by { intros, ext; simp only [equiv.to_fun_as_coe, prod.fst_add, prod.snd_add, add_apply, snd_sum_finsupp_equiv_prod_finsupp, fst_sum_finsupp_equiv_prod_finsupp] }, .. sum_finsupp_equiv_prod_finsupp } lemma fst_sum_finsupp_add_equiv_prod_finsupp {α β : Type*} (f : (α ⊕ β) →₀ M) (x : α) : (sum_finsupp_add_equiv_prod_finsupp f).1 x = f (sum.inl x) := rfl lemma snd_sum_finsupp_add_equiv_prod_finsupp {α β : Type*} (f : (α ⊕ β) →₀ M) (y : β) : (sum_finsupp_add_equiv_prod_finsupp f).2 y = f (sum.inr y) := rfl lemma sum_finsupp_add_equiv_prod_finsupp_symm_inl {α β : Type*} (fg : (α →₀ M) × (β →₀ M)) (x : α) : (sum_finsupp_add_equiv_prod_finsupp.symm fg) (sum.inl x) = fg.1 x := rfl lemma sum_finsupp_add_equiv_prod_finsupp_symm_inr {α β : Type*} (fg : (α →₀ M) × (β →₀ M)) (y : β) : (sum_finsupp_add_equiv_prod_finsupp.symm fg) (sum.inr y) = fg.2 y := rfl end sum /-! ### Declarations about scalar multiplication -/ section variables [has_zero M] [monoid_with_zero R] [mul_action_with_zero R M] @[simp] lemma single_smul (a b : α) (f : α → M) (r : R) : (single a r b) • (f a) = single a (r • f b) b := by by_cases a = b; simp [h] end section variables [monoid G] [mul_action G α] [add_comm_monoid M] /-- Scalar multiplication acting on the domain. This is not an instance as it would conflict with the action on the range. See the `instance_diamonds` test for examples of such conflicts. -/ def comap_has_smul : has_smul G (α →₀ M) := { smul := λ g, map_domain ((•) g) } local attribute [instance] comap_has_smul lemma comap_smul_def (g : G) (f : α →₀ M) : g • f = map_domain ((•) g) f := rfl @[simp] lemma comap_smul_single (g : G) (a : α) (b : M) : g • single a b = single (g • a) b := map_domain_single /-- `finsupp.comap_has_smul` is multiplicative -/ def comap_mul_action : mul_action G (α →₀ M) := { one_smul := λ f, by rw [comap_smul_def, one_smul_eq_id, map_domain_id], mul_smul := λ g g' f, by rw [comap_smul_def, comap_smul_def, comap_smul_def, ←comp_smul_left, map_domain_comp], } local attribute [instance] comap_mul_action /-- `finsupp.comap_has_smul` is distributive -/ def comap_distrib_mul_action : distrib_mul_action G (α →₀ M) := { smul_zero := λ g, by { ext, dsimp [(•)], simp, }, smul_add := λ g f f', by { ext, dsimp [(•)], simp [map_domain_add], }, } end section variables [group G] [mul_action G α] [add_comm_monoid M] local attribute [instance] comap_has_smul comap_mul_action comap_distrib_mul_action /-- When `G` is a group, `finsupp.comap_has_smul` acts by precomposition with the action of `g⁻¹`. -/ @[simp] lemma comap_smul_apply (g : G) (f : α →₀ M) (a : α) : (g • f) a = f (g⁻¹ • a) := begin conv_lhs { rw ←smul_inv_smul g a }, exact map_domain_apply (mul_action.injective g) _ (g⁻¹ • a), end end section instance [has_zero M] [smul_zero_class R M] : smul_zero_class R (α →₀ M) := { smul := λ a v, v.map_range ((•) a) (smul_zero _), smul_zero := λ a, by { ext, apply smul_zero } } /-! Throughout this section, some `monoid` and `semiring` arguments are specified with `{}` instead of `[]`. See note [implicit instance arguments]. -/ @[simp] lemma coe_smul [has_zero M] [smul_zero_class R M] (b : R) (v : α →₀ M) : ⇑(b • v) = b • v := rfl lemma smul_apply [has_zero M] [smul_zero_class R M] (b : R) (v : α →₀ M) (a : α) : (b • v) a = b • (v a) := rfl lemma _root_.is_smul_regular.finsupp [has_zero M] [smul_zero_class R M] {k : R} (hk : is_smul_regular M k) : is_smul_regular (α →₀ M) k := λ _ _ h, ext $ λ i, hk (congr_fun h i) instance [nonempty α] [has_zero M] [smul_zero_class R M] [has_faithful_smul R M] : has_faithful_smul R (α →₀ M) := { eq_of_smul_eq_smul := λ r₁ r₂ h, let ⟨a⟩ := ‹nonempty α› in eq_of_smul_eq_smul $ λ m : M, by simpa using congr_fun (h (single a m)) a } variables (α M) instance [add_zero_class M] [distrib_smul R M] : distrib_smul R (α →₀ M) := { smul := (•), smul_add := λ a x y, ext $ λ _, smul_add _ _ _, smul_zero := λ x, ext $ λ _, smul_zero _ } instance [monoid R] [add_monoid M] [distrib_mul_action R M] : distrib_mul_action R (α →₀ M) := { smul := (•), one_smul := λ x, ext $ λ _, one_smul _ _, mul_smul := λ r s x, ext $ λ _, mul_smul _ _ _, ..finsupp.distrib_smul _ _ } instance [has_zero M] [smul_zero_class R M] [smul_zero_class S M] [has_smul R S] [is_scalar_tower R S M] : is_scalar_tower R S (α →₀ M) := { smul_assoc := λ r s a, ext $ λ _, smul_assoc _ _ _ } instance [has_zero M] [smul_zero_class R M] [smul_zero_class S M] [smul_comm_class R S M] : smul_comm_class R S (α →₀ M) := { smul_comm := λ r s a, ext $ λ _, smul_comm _ _ _ } instance [has_zero M] [smul_zero_class R M] [smul_zero_class Rᵐᵒᵖ M] [is_central_scalar R M] : is_central_scalar R (α →₀ M) := { op_smul_eq_smul := λ r a, ext $ λ _, op_smul_eq_smul _ _ } instance [semiring R] [add_comm_monoid M] [module R M] : module R (α →₀ M) := { smul := (•), zero_smul := λ x, ext $ λ _, zero_smul _ _, add_smul := λ a x y, ext $ λ _, add_smul _ _ _, .. finsupp.distrib_mul_action α M } variables {α M} {R} lemma support_smul [add_monoid M] [smul_zero_class R M] {b : R} {g : α →₀ M} : (b • g).support ⊆ g.support := λ a, by { simp only [smul_apply, mem_support_iff, ne.def], exact mt (λ h, h.symm ▸ smul_zero _) } @[simp] lemma support_smul_eq [semiring R] [add_comm_monoid M] [module R M] [no_zero_smul_divisors R M] {b : R} (hb : b ≠ 0) {g : α →₀ M} : (b • g).support = g.support := finset.ext (λ a, by simp [finsupp.smul_apply, hb]) section variables {p : α → Prop} @[simp] lemma filter_smul {_ : monoid R} [add_monoid M] [distrib_mul_action R M] {b : R} {v : α →₀ M} : (b • v).filter p = b • v.filter p := coe_fn_injective $ set.indicator_const_smul {x | p x} b v end lemma map_domain_smul {_ : monoid R} [add_comm_monoid M] [distrib_mul_action R M] {f : α → β} (b : R) (v : α →₀ M) : map_domain f (b • v) = b • map_domain f v := map_domain_map_range _ _ _ _ (smul_add b) @[simp] lemma smul_single [has_zero M] [smul_zero_class R M] (c : R) (a : α) (b : M) : c • finsupp.single a b = finsupp.single a (c • b) := map_range_single @[simp] lemma smul_single' {_ : semiring R} (c : R) (a : α) (b : R) : c • finsupp.single a b = finsupp.single a (c * b) := smul_single _ _ _ lemma map_range_smul {_ : monoid R} [add_monoid M] [distrib_mul_action R M] [add_monoid N] [distrib_mul_action R N] {f : M → N} {hf : f 0 = 0} (c : R) (v : α →₀ M) (hsmul : ∀ x, f (c • x) = c • f x) : map_range f hf (c • v) = c • map_range f hf v := begin erw ←map_range_comp, have : (f ∘ (•) c) = ((•) c ∘ f) := funext hsmul, simp_rw this, apply map_range_comp, rw [function.comp_apply, smul_zero, hf], end lemma smul_single_one [semiring R] (a : α) (b : R) : b • single a 1 = single a b := by rw [smul_single, smul_eq_mul, mul_one] lemma comap_domain_smul [add_monoid M] [monoid R] [distrib_mul_action R M] {f : α → β} (r : R) (v : β →₀ M) (hfv : set.inj_on f (f ⁻¹' ↑(v.support))) (hfrv : set.inj_on f (f ⁻¹' ↑((r • v).support)) := hfv.mono $ set.preimage_mono $ finset.coe_subset.mpr support_smul): comap_domain f (r • v) hfrv = r • comap_domain f v hfv := by { ext, refl } /-- A version of `finsupp.comap_domain_smul` that's easier to use. -/ lemma comap_domain_smul_of_injective [add_monoid M] [monoid R] [distrib_mul_action R M] {f : α → β} (hf : function.injective f) (r : R) (v : β →₀ M) : comap_domain f (r • v) (hf.inj_on _) = r • comap_domain f v (hf.inj_on _) := comap_domain_smul _ _ _ _ end lemma sum_smul_index [semiring R] [add_comm_monoid M] {g : α →₀ R} {b : R} {h : α → R → M} (h0 : ∀i, h i 0 = 0) : (b • g).sum h = g.sum (λi a, h i (b * a)) := finsupp.sum_map_range_index h0 lemma sum_smul_index' [add_monoid M] [distrib_smul R M] [add_comm_monoid N] {g : α →₀ M} {b : R} {h : α → M → N} (h0 : ∀i, h i 0 = 0) : (b • g).sum h = g.sum (λi c, h i (b • c)) := finsupp.sum_map_range_index h0 /-- A version of `finsupp.sum_smul_index'` for bundled additive maps. -/ lemma sum_smul_index_add_monoid_hom [add_monoid M] [add_comm_monoid N] [distrib_smul R M] {g : α →₀ M} {b : R} {h : α → M →+ N} : (b • g).sum (λ a, h a) = g.sum (λ i c, h i (b • c)) := sum_map_range_index (λ i, (h i).map_zero) instance [semiring R] [add_comm_monoid M] [module R M] {ι : Type*} [no_zero_smul_divisors R M] : no_zero_smul_divisors R (ι →₀ M) := ⟨λ c f h, or_iff_not_imp_left.mpr (λ hc, finsupp.ext (λ i, (smul_eq_zero.mp (finsupp.ext_iff.mp h i)).resolve_left hc))⟩ section distrib_mul_action_hom variables [semiring R] variables [add_comm_monoid M] [add_comm_monoid N] [distrib_mul_action R M] [distrib_mul_action R N] /-- `finsupp.single` as a `distrib_mul_action_hom`. See also `finsupp.lsingle` for the version as a linear map. -/ def distrib_mul_action_hom.single (a : α) : M →+[R] (α →₀ M) := { map_smul' := λ k m, by simp only [add_monoid_hom.to_fun_eq_coe, single_add_hom_apply, smul_single], .. single_add_hom a } lemma distrib_mul_action_hom_ext {f g : (α →₀ M) →+[R] N} (h : ∀ (a : α) (m : M), f (single a m) = g (single a m)) : f = g := distrib_mul_action_hom.to_add_monoid_hom_injective $ add_hom_ext h /-- See note [partially-applied ext lemmas]. -/ @[ext] lemma distrib_mul_action_hom_ext' {f g : (α →₀ M) →+[R] N} (h : ∀ (a : α), f.comp (distrib_mul_action_hom.single a) = g.comp (distrib_mul_action_hom.single a)) : f = g := distrib_mul_action_hom_ext $ λ a, distrib_mul_action_hom.congr_fun (h a) end distrib_mul_action_hom section variables [has_zero R] /-- The `finsupp` version of `pi.unique`. -/ instance unique_of_right [subsingleton R] : unique (α →₀ R) := fun_like.coe_injective.unique /-- The `finsupp` version of `pi.unique_of_is_empty`. -/ instance unique_of_left [is_empty α] : unique (α →₀ R) := fun_like.coe_injective.unique end /-- Given an `add_comm_monoid M` and `s : set α`, `restrict_support_equiv s M` is the `equiv` between the subtype of finitely supported functions with support contained in `s` and the type of finitely supported functions from `s`. -/ def restrict_support_equiv (s : set α) (M : Type*) [add_comm_monoid M] : {f : α →₀ M // ↑f.support ⊆ s } ≃ (s →₀ M) := { to_fun := λ f, subtype_domain (λ x, x ∈ s) f.1, inv_fun := λ f, ⟨f.map_domain subtype.val, begin classical, refine set.subset.trans (finset.coe_subset.2 map_domain_support) _, rw [finset.coe_image, set.image_subset_iff], exact assume x hx, x.2, end⟩, left_inv := begin rintros ⟨f, hf⟩, apply subtype.eq, ext a, dsimp only, refine classical.by_cases (assume h : a ∈ set.range (subtype.val : s → α), _) (assume h, _), { rcases h with ⟨x, rfl⟩, rw [map_domain_apply subtype.val_injective, subtype_domain_apply] }, { convert map_domain_notin_range _ _ h, rw [← not_mem_support_iff], refine mt _ h, exact assume ha, ⟨⟨a, hf ha⟩, rfl⟩ } end, right_inv := λ f, begin ext ⟨a, ha⟩, dsimp only, rw [subtype_domain_apply, map_domain_apply subtype.val_injective] end } /-- Given `add_comm_monoid M` and `e : α ≃ β`, `dom_congr e` is the corresponding `equiv` between `α →₀ M` and `β →₀ M`. This is `finsupp.equiv_congr_left` as an `add_equiv`. -/ @[simps apply] protected def dom_congr [add_comm_monoid M] (e : α ≃ β) : (α →₀ M) ≃+ (β →₀ M) := { to_fun := equiv_map_domain e, inv_fun := equiv_map_domain e.symm, left_inv := λ v, begin simp only [← equiv_map_domain_trans, equiv.self_trans_symm], exact equiv_map_domain_refl _ end, right_inv := begin assume v, simp only [← equiv_map_domain_trans, equiv.symm_trans_self], exact equiv_map_domain_refl _ end, map_add' := λ a b, by simp only [equiv_map_domain_eq_map_domain]; exact map_domain_add } @[simp] lemma dom_congr_refl [add_comm_monoid M] : finsupp.dom_congr (equiv.refl α) = add_equiv.refl (α →₀ M) := add_equiv.ext $ λ _, equiv_map_domain_refl _ @[simp] lemma dom_congr_symm [add_comm_monoid M] (e : α ≃ β) : (finsupp.dom_congr e).symm = (finsupp.dom_congr e.symm : (β →₀ M) ≃+ (α →₀ M)):= add_equiv.ext $ λ _, rfl @[simp] lemma dom_congr_trans [add_comm_monoid M] (e : α ≃ β) (f : β ≃ γ) : (finsupp.dom_congr e).trans (finsupp.dom_congr f) = (finsupp.dom_congr (e.trans f) : (α →₀ M) ≃+ _) := add_equiv.ext $ λ _, (equiv_map_domain_trans _ _ _).symm end finsupp namespace finsupp /-! ### Declarations about sigma types -/ section sigma variables {αs : ι → Type*} [has_zero M] (l : (Σ i, αs i) →₀ M) /-- Given `l`, a finitely supported function from the sigma type `Σ (i : ι), αs i` to `M` and an index element `i : ι`, `split l i` is the `i`th component of `l`, a finitely supported function from `as i` to `M`. This is the `finsupp` version of `sigma.curry`. -/ def split (i : ι) : αs i →₀ M := l.comap_domain (sigma.mk i) (λ x1 x2 _ _ hx, heq_iff_eq.1 (sigma.mk.inj hx).2) lemma split_apply (i : ι) (x : αs i) : split l i x = l ⟨i, x⟩ := begin dunfold split, rw comap_domain_apply end /-- Given `l`, a finitely supported function from the sigma type `Σ (i : ι), αs i` to `β`, `split_support l` is the finset of indices in `ι` that appear in the support of `l`. -/ def split_support (l : (Σ i, αs i) →₀ M) : finset ι := by haveI := classical.dec_eq ι; exact l.support.image sigma.fst lemma mem_split_support_iff_nonzero (i : ι) : i ∈ split_support l ↔ split l i ≠ 0 := begin rw [split_support, mem_image, ne.def, ← support_eq_empty, ← ne.def, ← finset.nonempty_iff_ne_empty, split, comap_domain, finset.nonempty], simp only [exists_prop, finset.mem_preimage, exists_and_distrib_right, exists_eq_right, mem_support_iff, sigma.exists, ne.def] end /-- Given `l`, a finitely supported function from the sigma type `Σ i, αs i` to `β` and an `ι`-indexed family `g` of functions from `(αs i →₀ β)` to `γ`, `split_comp` defines a finitely supported function from the index type `ι` to `γ` given by composing `g i` with `split l i`. -/ def split_comp [has_zero N] (g : Π i, (αs i →₀ M) → N) (hg : ∀ i x, x = 0 ↔ g i x = 0) : ι →₀ N := { support := split_support l, to_fun := λ i, g i (split l i), mem_support_to_fun := begin intros i, rw [mem_split_support_iff_nonzero, not_iff_not, hg], end } lemma sigma_support : l.support = l.split_support.sigma (λ i, (l.split i).support) := by simp only [finset.ext_iff, split_support, split, comap_domain, mem_image, mem_preimage, sigma.forall, mem_sigma]; tauto lemma sigma_sum [add_comm_monoid N] (f : (Σ (i : ι), αs i) → M → N) : l.sum f = ∑ i in split_support l, (split l i).sum (λ (a : αs i) b, f ⟨i, a⟩ b) := by simp only [sum, sigma_support, sum_sigma, split_apply] variables {η : Type*} [fintype η] {ιs : η → Type*} [has_zero α] /-- On a `fintype η`, `finsupp.split` is an equivalence between `(Σ (j : η), ιs j) →₀ α` and `Π j, (ιs j →₀ α)`. This is the `finsupp` version of `equiv.Pi_curry`. -/ noncomputable def sigma_finsupp_equiv_pi_finsupp : ((Σ j, ιs j) →₀ α) ≃ Π j, (ιs j →₀ α) := { to_fun := split, inv_fun := λ f, on_finset (finset.univ.sigma (λ j, (f j).support)) (λ ji, f ji.1 ji.2) (λ g hg, finset.mem_sigma.mpr ⟨finset.mem_univ _, mem_support_iff.mpr hg⟩), left_inv := λ f, by { ext, simp [split] }, right_inv := λ f, by { ext, simp [split] } } @[simp] lemma sigma_finsupp_equiv_pi_finsupp_apply (f : (Σ j, ιs j) →₀ α) (j i) : sigma_finsupp_equiv_pi_finsupp f j i = f ⟨j, i⟩ := rfl /-- On a `fintype η`, `finsupp.split` is an additive equivalence between `(Σ (j : η), ιs j) →₀ α` and `Π j, (ιs j →₀ α)`. This is the `add_equiv` version of `finsupp.sigma_finsupp_equiv_pi_finsupp`. -/ noncomputable def sigma_finsupp_add_equiv_pi_finsupp {α : Type*} {ιs : η → Type*} [add_monoid α] : ((Σ j, ιs j) →₀ α) ≃+ Π j, (ιs j →₀ α) := { map_add' := λ f g, by { ext, simp }, .. sigma_finsupp_equiv_pi_finsupp } @[simp] lemma sigma_finsupp_add_equiv_pi_finsupp_apply {α : Type*} {ιs : η → Type*} [add_monoid α] (f : (Σ j, ιs j) →₀ α) (j i) : sigma_finsupp_add_equiv_pi_finsupp f j i = f ⟨j, i⟩ := rfl end sigma /-! ### Meta declarations -/ /-- Stringify a `finsupp` as a sequence of `finsupp.single` terms. Note this is `meta` as it has to choose some order for the terms. -/ meta instance (ι α : Type*) [has_zero α] [has_repr ι] [has_repr α] : has_repr (ι →₀ α) := { repr := λ f, if f.support.card = 0 then "0" else " + ".intercalate $ f.support.val.unquot.map (λ i, "finsupp.single " ++ repr i ++ " " ++ repr (f i)) } end finsupp
73e552bb10b6b5834142aef3356544222f9f4905
aa2345b30d710f7e75f13157a35845ee6d48c017
/analysis/topology/continuity.lean
8046700144298ebd3dccc30d1b3889b1b61b6337
[ "Apache-2.0" ]
permissive
CohenCyril/mathlib
5241b20a3fd0ac0133e48e618a5fb7761ca7dcbe
a12d5a192f5923016752f638d19fc1a51610f163
refs/heads/master
1,586,031,957,957
1,541,432,824,000
1,541,432,824,000
156,246,337
0
0
Apache-2.0
1,541,434,514,000
1,541,434,513,000
null
UTF-8
Lean
false
false
70,732
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot Continuous functions. Parts of the formalization is based on the books: N. Bourbaki: General Topology I. M. James: Topologies and Uniformities A major difference is that this formalization is heavily based on the filter library. -/ import analysis.topology.topological_space noncomputable theory open set filter lattice local attribute [instance] classical.prop_decidable variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} section variables [topological_space α] [topological_space β] [topological_space γ] /-- A function between topological spaces is continuous if the preimage of every open set is open. -/ def continuous (f : α → β) := ∀s, is_open s → is_open (f ⁻¹' s) lemma continuous_id : continuous (id : α → α) := assume s h, h lemma continuous.comp {f : α → β} {g : β → γ} (hf : continuous f) (hg : continuous g): continuous (g ∘ f) := assume s h, hf _ (hg s h) lemma continuous.tendsto {f : α → β} (hf : continuous f) (x) : tendsto f (nhds x) (nhds (f x)) | s := show s ∈ (nhds (f x)).sets → s ∈ (map f (nhds x)).sets, by simp [nhds_sets]; exact assume t t_subset t_open fx_in_t, ⟨f ⁻¹' t, preimage_mono t_subset, hf t t_open, fx_in_t⟩ lemma continuous_iff_tendsto {f : α → β} : continuous f ↔ (∀x, tendsto f (nhds x) (nhds (f x))) := ⟨continuous.tendsto, assume hf : ∀x, tendsto f (nhds x) (nhds (f x)), assume s, assume hs : is_open s, have ∀a, f a ∈ s → s ∈ (nhds (f a)).sets, by simp [nhds_sets]; exact assume a ha, ⟨s, subset.refl s, hs, ha⟩, show is_open (f ⁻¹' s), by simp [is_open_iff_nhds]; exact assume a ha, hf a (this a ha)⟩ lemma continuous_const {b : β} : continuous (λa:α, b) := continuous_iff_tendsto.mpr $ assume a, tendsto_const_nhds lemma continuous_iff_is_closed {f : α → β} : continuous f ↔ (∀s, is_closed s → is_closed (f ⁻¹' s)) := ⟨assume hf s hs, hf (-s) hs, assume hf s, by rw [←is_closed_compl_iff, ←is_closed_compl_iff]; exact hf _⟩ lemma continuous_at_iff_ultrafilter {f : α → β} (x) : tendsto f (nhds x) (nhds (f x)) ↔ ∀ g, is_ultrafilter g → g ≤ nhds x → g.map f ≤ nhds (f x) := tendsto_iff_ultrafilter f (nhds x) (nhds (f x)) lemma continuous_iff_ultrafilter {f : α → β} : continuous f ↔ ∀ x g, is_ultrafilter g → g ≤ nhds x → g.map f ≤ nhds (f x) := by simp only [continuous_iff_tendsto, continuous_at_iff_ultrafilter] lemma continuous_if {p : α → Prop} {f g : α → β} {h : ∀a, decidable (p a)} (hp : ∀a∈frontier {a | p a}, f a = g a) (hf : continuous f) (hg : continuous g) : continuous (λa, @ite (p a) (h a) β (f a) (g a)) := continuous_iff_is_closed.mpr $ assume s hs, have (λa, ite (p a) (f a) (g a)) ⁻¹' s = (closure {a | p a} ∩ f ⁻¹' s) ∪ (closure {a | ¬ p a} ∩ g ⁻¹' s), from set.ext $ assume a, classical.by_cases (assume : a ∈ frontier {a | p a}, have hac : a ∈ closure {a | p a}, from this.left, have hai : a ∈ closure {a | ¬ p a}, from have a ∈ - interior {a | p a}, from this.right, by rwa [←closure_compl] at this, by by_cases p a; simp [h, hp a this, hac, hai, iff_def] {contextual := tt}) (assume hf : a ∈ - frontier {a | p a}, classical.by_cases (assume : p a, have hc : a ∈ closure {a | p a}, from subset_closure this, have hnc : a ∉ closure {a | ¬ p a}, by show a ∉ closure (- {a | p a}); rw [closure_compl]; simpa [frontier, hc] using hf, by simp [this, hc, hnc]) (assume : ¬ p a, have hc : a ∈ closure {a | ¬ p a}, from subset_closure this, have hnc : a ∉ closure {a | p a}, begin have hc : a ∈ closure (- {a | p a}), from hc, simp [closure_compl] at hc, simpa [frontier, hc] using hf end, by simp [this, hc, hnc])), by rw [this]; exact is_closed_union (is_closed_inter is_closed_closure $ continuous_iff_is_closed.mp hf s hs) (is_closed_inter is_closed_closure $ continuous_iff_is_closed.mp hg s hs) lemma image_closure_subset_closure_image {f : α → β} {s : set α} (h : continuous f) : f '' closure s ⊆ closure (f '' s) := have ∀ (a : α), nhds a ⊓ principal s ≠ ⊥ → nhds (f a) ⊓ principal (f '' s) ≠ ⊥, from assume a ha, have h₁ : ¬ map f (nhds a ⊓ principal s) = ⊥, by rwa[map_eq_bot_iff], have h₂ : map f (nhds a ⊓ principal s) ≤ nhds (f a) ⊓ principal (f '' s), from le_inf (le_trans (map_mono inf_le_left) $ by rw [continuous_iff_tendsto] at h; exact h a) (le_trans (map_mono inf_le_right) $ by simp; exact subset.refl _), neq_bot_of_le_neq_bot h₁ h₂, by simp [image_subset_iff, closure_eq_nhds]; assumption lemma mem_closure [topological_space α] [topological_space β] {s : set α} {t : set β} {f : α → β} {a : α} (hf : continuous f) (ha : a ∈ closure s) (ht : ∀a∈s, f a ∈ t) : f a ∈ closure t := subset.trans (image_closure_subset_closure_image hf) (closure_mono $ image_subset_iff.2 ht) $ (mem_image_of_mem f ha) lemma compact_image {s : set α} {f : α → β} (hs : compact s) (hf : continuous f) : compact (f '' s) := compact_of_finite_subcover $ assume c hco hcs, have hdo : ∀t∈c, is_open (f ⁻¹' t), from assume t' ht, hf _ $ hco _ ht, have hds : s ⊆ ⋃i∈c, f ⁻¹' i, by simpa [subset_def, -mem_image] using hcs, let ⟨d', hcd', hfd', hd'⟩ := compact_elim_finite_subcover_image hs hdo hds in ⟨d', hcd', hfd', by simpa [subset_def, -mem_image, image_subset_iff] using hd'⟩ end section constructions local notation `cont` := @continuous _ _ local notation `tspace` := topological_space open topological_space variables {f : α → β} {ι : Sort*} lemma continuous_iff_le_coinduced {t₁ : tspace α} {t₂ : tspace β} : cont t₁ t₂ f ↔ t₂ ≤ coinduced f t₁ := iff.rfl lemma continuous_iff_induced_le {t₁ : tspace α} {t₂ : tspace β} : cont t₁ t₂ f ↔ induced f t₂ ≤ t₁ := iff.trans continuous_iff_le_coinduced (gc_induced_coinduced f _ _).symm theorem continuous_generated_from {t : tspace α} {b : set (set β)} (h : ∀s∈b, is_open (f ⁻¹' s)) : cont t (generate_from b) f := continuous_iff_le_coinduced.2 $ generate_from_le h lemma continuous_induced_dom {t : tspace β} : cont (induced f t) t f := assume s h, ⟨_, h, rfl⟩ lemma continuous_induced_rng {g : γ → α} {t₂ : tspace β} {t₁ : tspace γ} (h : cont t₁ t₂ (f ∘ g)) : cont t₁ (induced f t₂) g := assume s ⟨t, ht, s_eq⟩, s_eq.symm ▸ h t ht lemma continuous_coinduced_rng {t : tspace α} : cont t (coinduced f t) f := assume s h, h lemma continuous_coinduced_dom {g : β → γ} {t₁ : tspace α} {t₂ : tspace γ} (h : cont t₁ t₂ (g ∘ f)) : cont (coinduced f t₁) t₂ g := assume s hs, h s hs lemma continuous_le_dom {t₁ t₂ : tspace α} {t₃ : tspace β} (h₁ : t₁ ≤ t₂) (h₂ : cont t₁ t₃ f) : cont t₂ t₃ f := assume s h, h₁ _ (h₂ s h) lemma continuous_le_rng {t₁ : tspace α} {t₂ t₃ : tspace β} (h₁ : t₃ ≤ t₂) (h₂ : cont t₁ t₂ f) : cont t₁ t₃ f := assume s h, h₂ s (h₁ s h) lemma continuous_inf_dom {t₁ t₂ : tspace α} {t₃ : tspace β} (h₁ : cont t₁ t₃ f) (h₂ : cont t₂ t₃ f) : cont (t₁ ⊓ t₂) t₃ f := assume s h, ⟨h₁ s h, h₂ s h⟩ lemma continuous_inf_rng_left {t₁ : tspace α} {t₃ t₂ : tspace β} : cont t₁ t₂ f → cont t₁ (t₂ ⊓ t₃) f := continuous_le_rng inf_le_left lemma continuous_inf_rng_right {t₁ : tspace α} {t₃ t₂ : tspace β} : cont t₁ t₃ f → cont t₁ (t₂ ⊓ t₃) f := continuous_le_rng inf_le_right lemma continuous_Inf_dom {t₁ : set (tspace α)} {t₂ : tspace β} (h : ∀t∈t₁, cont t t₂ f) : cont (Inf t₁) t₂ f := continuous_iff_induced_le.2 $ le_Inf $ assume t ht, continuous_iff_induced_le.1 $ h t ht lemma continuous_Inf_rng {t₁ : tspace α} {t₂ : set (tspace β)} {t : tspace β} (h₁ : t ∈ t₂) (hf : cont t₁ t f) : cont t₁ (Inf t₂) f := continuous_iff_le_coinduced.2 $ Inf_le_of_le h₁ $ continuous_iff_le_coinduced.1 hf lemma continuous_infi_dom {t₁ : ι → tspace α} {t₂ : tspace β} (h : ∀i, cont (t₁ i) t₂ f) : cont (infi t₁) t₂ f := continuous_Inf_dom $ assume t ⟨i, (t_eq : t = t₁ i)⟩, t_eq.symm ▸ h i lemma continuous_infi_rng {t₁ : tspace α} {t₂ : ι → tspace β} {i : ι} (h : cont t₁ (t₂ i) f) : cont t₁ (infi t₂) f := continuous_Inf_rng ⟨i, rfl⟩ h lemma continuous_sup_rng {t₁ : tspace α} {t₂ t₃ : tspace β} (h₁ : cont t₁ t₂ f) (h₂ : cont t₁ t₃ f) : cont t₁ (t₂ ⊔ t₃) f := continuous_iff_le_coinduced.2 $ sup_le (continuous_iff_le_coinduced.1 h₁) (continuous_iff_le_coinduced.1 h₂) lemma continuous_sup_dom_left {t₁ t₂ : tspace α} {t₃ : tspace β} : cont t₁ t₃ f → cont (t₁ ⊔ t₂) t₃ f := continuous_le_dom le_sup_left lemma continuous_sup_dom_right {t₁ t₂ : tspace α} {t₃ : tspace β} : cont t₂ t₃ f → cont (t₁ ⊔ t₂) t₃ f := continuous_le_dom le_sup_right lemma continuous_Sup_dom {t₁ : set (tspace α)} {t₂ : tspace β} {t : tspace α} (h₁ : t ∈ t₁) : cont t t₂ f → cont (Sup t₁) t₂ f := continuous_le_dom $ le_Sup h₁ lemma continuous_Sup_rng {t₁ : tspace α} {t₂ : set (tspace β)} (h : ∀t∈t₂, cont t₁ t f) : cont t₁ (Sup t₂) f := continuous_iff_le_coinduced.2 $ Sup_le $ assume b hb, continuous_iff_le_coinduced.1 $ h b hb lemma continuous_supr_dom {t₁ : ι → tspace α} {t₂ : tspace β} {i : ι} : cont (t₁ i) t₂ f → cont (supr t₁) t₂ f := continuous_le_dom $ le_supr _ _ lemma continuous_supr_rng {t₁ : tspace α} {t₂ : ι → tspace β} (h : ∀i, cont t₁ (t₂ i) f) : cont t₁ (supr t₂) f := continuous_iff_le_coinduced.2 $ supr_le $ assume i, continuous_iff_le_coinduced.1 $ h i lemma continuous_top {t : tspace β} : cont ⊤ t f := continuous_iff_induced_le.2 $ le_top lemma continuous_bot {t : tspace α} : cont t ⊥ f := continuous_iff_le_coinduced.2 $ bot_le end constructions section induced open topological_space variables [t : topological_space β] {f : α → β} theorem is_open_induced {s : set β} (h : is_open s) : (induced f t).is_open (f ⁻¹' s) := ⟨s, h, rfl⟩ lemma nhds_induced_eq_comap {a : α} : @nhds α (induced f t) a = comap f (nhds (f a)) := le_antisymm (assume s ⟨s', hs', (h_s : f ⁻¹' s' ⊆ s)⟩, let ⟨t', hsub, ht', hin⟩ := mem_nhds_sets_iff.1 hs' in (@nhds α (induced f t) a).sets_of_superset begin simp [mem_nhds_sets_iff], exact ⟨preimage f t', preimage_mono hsub, is_open_induced ht', hin⟩ end h_s) (le_infi $ assume s, le_infi $ assume ⟨as, s', is_open_s', s_eq⟩, begin simp [comap, mem_nhds_sets_iff, s_eq], exact ⟨s', ⟨s', subset.refl _, is_open_s', by rwa [s_eq] at as⟩, subset.refl _⟩ end) lemma map_nhds_induced_eq {a : α} (h : image f univ ∈ (nhds (f a)).sets) : map f (@nhds α (induced f t) a) = nhds (f a) := le_antisymm (@continuous.tendsto α β (induced f t) _ _ continuous_induced_dom a) (assume s, assume hs : f ⁻¹' s ∈ (@nhds α (induced f t) a).sets, let ⟨t', t_subset, is_open_t, a_in_t⟩ := mem_nhds_sets_iff.mp h in let ⟨s', s'_subset, ⟨s'', is_open_s'', s'_eq⟩, a_in_s'⟩ := (@mem_nhds_sets_iff _ (induced f t) _ _).mp hs in by subst s'_eq; exact (mem_nhds_sets_iff.mpr $ ⟨t' ∩ s'', assume x ⟨h₁, h₂⟩, match x, h₂, t_subset h₁ with | x, h₂, ⟨y, _, y_eq⟩ := begin subst y_eq, exact s'_subset h₂ end end, is_open_inter is_open_t is_open_s'', ⟨a_in_t, a_in_s'⟩⟩)) lemma closure_induced [t : topological_space β] {f : α → β} {a : α} {s : set α} (hf : ∀x y, f x = f y → x = y) : a ∈ @closure α (topological_space.induced f t) s ↔ f a ∈ closure (f '' s) := have comap f (nhds (f a) ⊓ principal (f '' s)) ≠ ⊥ ↔ nhds (f a) ⊓ principal (f '' s) ≠ ⊥, from ⟨assume h₁ h₂, h₁ $ h₂.symm ▸ comap_bot, assume h, forall_sets_neq_empty_iff_neq_bot.mp $ assume s₁ ⟨s₂, hs₂, (hs : f ⁻¹' s₂ ⊆ s₁)⟩, have f '' s ∈ (nhds (f a) ⊓ principal (f '' s)).sets, from mem_inf_sets_of_right $ by simp [subset.refl], have s₂ ∩ f '' s ∈ (nhds (f a) ⊓ principal (f '' s)).sets, from inter_mem_sets hs₂ this, let ⟨b, hb₁, ⟨a, ha, ha₂⟩⟩ := inhabited_of_mem_sets h this in ne_empty_of_mem $ hs $ by rwa [←ha₂] at hb₁⟩, calc a ∈ @closure α (topological_space.induced f t) s ↔ (@nhds α (topological_space.induced f t) a) ⊓ principal s ≠ ⊥ : by rw [closure_eq_nhds]; refl ... ↔ comap f (nhds (f a)) ⊓ principal (f ⁻¹' (f '' s)) ≠ ⊥ : by rw [nhds_induced_eq_comap, preimage_image_eq _ hf] ... ↔ comap f (nhds (f a) ⊓ principal (f '' s)) ≠ ⊥ : by rw [comap_inf, ←comap_principal] ... ↔ _ : by rwa [closure_eq_nhds] end induced section embedding /-- A function between topological spaces is an embedding if it is injective, and for all `s : set α`, `s` is open iff it is the preimage of an open set. -/ def embedding [tα : topological_space α] [tβ : topological_space β] (f : α → β) : Prop := function.injective f ∧ tα = tβ.induced f variables [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] lemma embedding_id : embedding (@id α) := ⟨assume a₁ a₂ h, h, induced_id.symm⟩ lemma embedding_compose {f : α → β} {g : β → γ} (hf : embedding f) (hg : embedding g) : embedding (g ∘ f) := ⟨assume a₁ a₂ h, hf.left $ hg.left h, by rw [hf.right, hg.right, induced_compose]⟩ lemma embedding_prod_mk {f : α → β} {g : γ → δ} (hf : embedding f) (hg : embedding g) : embedding (λx:α×γ, (f x.1, g x.2)) := ⟨assume ⟨x₁, x₂⟩ ⟨y₁, y₂⟩, by simp; exact assume h₁ h₂, ⟨hf.left h₁, hg.left h₂⟩, by rw [prod.topological_space, prod.topological_space, hf.right, hg.right, induced_compose, induced_compose, induced_sup, induced_compose, induced_compose]⟩ lemma embedding_of_embedding_compose {f : α → β} {g : β → γ} (hf : continuous f) (hg : continuous g) (hgf : embedding (g ∘ f)) : embedding f := ⟨assume a₁ a₂ h, hgf.left $ by simp [h, (∘)], le_antisymm (by rw [hgf.right, ← continuous_iff_induced_le]; apply continuous_induced_dom.comp hg) (by rwa ← continuous_iff_induced_le)⟩ lemma embedding_open {f : α → β} {s : set α} (hf : embedding f) (h : is_open (range f)) (hs : is_open s) : is_open (f '' s) := let ⟨t, ht, h_eq⟩ := by rw [hf.right] at hs; exact hs in have is_open (t ∩ range f), from is_open_inter ht h, h_eq.symm ▸ by rwa [image_preimage_eq_inter_range] lemma embedding_is_closed {f : α → β} {s : set α} (hf : embedding f) (h : is_closed (range f)) (hs : is_closed s) : is_closed (f '' s) := let ⟨t, ht, h_eq⟩ := by rw [hf.right, is_closed_induced_iff] at hs; exact hs in have is_closed (t ∩ range f), from is_closed_inter ht h, h_eq.symm ▸ by rwa [image_preimage_eq_inter_range] lemma embedding.map_nhds_eq [topological_space α] [topological_space β] {f : α → β} (hf : embedding f) (a : α) (h : f '' univ ∈ (nhds (f a)).sets) : (nhds a).map f = nhds (f a) := by rw [hf.2]; exact map_nhds_induced_eq h lemma embedding.tendsto_nhds_iff {ι : Type*} {f : ι → β} {g : β → γ} {a : filter ι} {b : β} (hg : embedding g) : tendsto f a (nhds b) ↔ tendsto (g ∘ f) a (nhds (g b)) := by rw [tendsto, tendsto, hg.right, nhds_induced_eq_comap, ← map_le_iff_le_comap, filter.map_map] lemma embedding.continuous_iff {f : α → β} {g : β → γ} (hg : embedding g) : continuous f ↔ continuous (g ∘ f) := by simp [continuous_iff_tendsto, embedding.tendsto_nhds_iff hg] lemma embedding.continuous {f : α → β} (hf : embedding f) : continuous f := hf.continuous_iff.mp continuous_id lemma compact_iff_compact_image_of_embedding {s : set α} {f : α → β} (hf : embedding f) : compact s ↔ compact (f '' s) := iff.intro (assume h, compact_image h hf.continuous) $ assume h, begin rw compact_iff_ultrafilter_le_nhds at ⊢ h, intros u hu us', let u' : filter β := map f u, have : u' ≤ principal (f '' s), begin rw [map_le_iff_le_comap, comap_principal], convert us', exact preimage_image_eq _ hf.1 end, rcases h u' (ultrafilter_map hu) this with ⟨_, ⟨a, ha, ⟨⟩⟩, _⟩, refine ⟨a, ha, _⟩, rwa [hf.2, nhds_induced_eq_comap, ←map_le_iff_le_comap] end lemma embedding.closure_eq_preimage_closure_image {e : α → β} (he : embedding e) (s : set α) : closure s = e ⁻¹' closure (e '' s) := by ext x; rw [set.mem_preimage_eq, ← closure_induced he.1, he.2] end embedding /-- A function between topological spaces is a quotient map if it is surjective, and for all `s : set β`, `s` is open iff its preimage is an open set. -/ def quotient_map [tα : topological_space α] [tβ : topological_space β] (f : α → β) : Prop := function.surjective f ∧ tβ = tα.coinduced f namespace quotient_map variables [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] protected lemma id : quotient_map (@id α) := ⟨assume a, ⟨a, rfl⟩, coinduced_id.symm⟩ protected lemma comp {f : α → β} {g : β → γ} (hf : quotient_map f) (hg : quotient_map g) : quotient_map (g ∘ f) := ⟨function.surjective_comp hg.left hf.left, by rw [hg.right, hf.right, coinduced_compose]⟩ protected lemma of_quotient_map_compose {f : α → β} {g : β → γ} (hf : continuous f) (hg : continuous g) (hgf : quotient_map (g ∘ f)) : quotient_map g := ⟨assume b, let ⟨a, h⟩ := hgf.left b in ⟨f a, h⟩, le_antisymm (by rwa ← continuous_iff_le_coinduced) (by rw [hgf.right, ← continuous_iff_le_coinduced]; apply hf.comp continuous_coinduced_rng)⟩ protected lemma continuous_iff {f : α → β} {g : β → γ} (hf : quotient_map f) : continuous g ↔ continuous (g ∘ f) := by rw [continuous_iff_le_coinduced, continuous_iff_le_coinduced, hf.right, coinduced_compose] protected lemma continuous {f : α → β} (hf : quotient_map f) : continuous f := hf.continuous_iff.mp continuous_id end quotient_map section is_open_map variables [topological_space α] [topological_space β] def is_open_map (f : α → β) := ∀ U : set α, is_open U → is_open (f '' U) lemma is_open_map_iff_nhds_le (f : α → β) : is_open_map f ↔ ∀(a:α), nhds (f a) ≤ (nhds a).map f := begin split, { assume h a s hs, rcases mem_nhds_sets_iff.1 hs with ⟨t, hts, ht, hat⟩, exact filter.mem_sets_of_superset (mem_nhds_sets (h t ht) (mem_image_of_mem _ hat)) (image_subset_iff.2 hts) }, { refine assume h s hs, is_open_iff_mem_nhds.2 _, rintros b ⟨a, ha, rfl⟩, exact h _ (filter.image_mem_map $ mem_nhds_sets hs ha) } end end is_open_map namespace is_open_map variables [topological_space α] [topological_space β] [topological_space γ] open function protected lemma id : is_open_map (@id α) := assume s hs, by rwa [image_id] protected lemma comp {f : α → β} {g : β → γ} (hf : is_open_map f) (hg : is_open_map g) : is_open_map (g ∘ f) := by intros s hs; rw [image_comp]; exact hg _ (hf _ hs) lemma of_inverse {f : α → β} {f' : β → α} (h : continuous f') (l_inv : left_inverse f f') (r_inv : right_inverse f f') : is_open_map f := assume s hs, have f' ⁻¹' s = f '' s, by ext x; simp [mem_image_iff_of_inverse r_inv l_inv], this ▸ h s hs lemma to_quotient_map {f : α → β} (open_map : is_open_map f) (cont : continuous f) (surj : function.surjective f) : quotient_map f := ⟨ surj, begin ext s, show is_open s ↔ is_open (f ⁻¹' s), split, { exact cont s }, { assume h, rw ← @image_preimage_eq _ _ _ s surj, exact open_map _ h } end⟩ end is_open_map section sierpinski variables [topological_space α] @[simp] lemma is_open_singleton_true : is_open ({true} : set Prop) := topological_space.generate_open.basic _ (by simp) lemma continuous_Prop {p : α → Prop} : continuous p ↔ is_open {x | p x} := ⟨assume h : continuous p, have is_open (p ⁻¹' {true}), from h _ is_open_singleton_true, by simp [preimage, eq_true] at this; assumption, assume h : is_open {x | p x}, continuous_generated_from $ assume s (hs : s ∈ {{true}}), by simp at hs; simp [hs, preimage, eq_true, h]⟩ end sierpinski section prod open topological_space variables [topological_space α] [topological_space β] [topological_space γ] lemma continuous_fst : continuous (@prod.fst α β) := continuous_sup_dom_left continuous_induced_dom lemma continuous_snd : continuous (@prod.snd α β) := continuous_sup_dom_right continuous_induced_dom lemma continuous.prod_mk {f : γ → α} {g : γ → β} (hf : continuous f) (hg : continuous g) : continuous (λx, prod.mk (f x) (g x)) := continuous_sup_rng (continuous_induced_rng hf) (continuous_induced_rng hg) lemma continuous_swap : continuous (prod.swap : α × β → β × α) := continuous.prod_mk continuous_snd continuous_fst lemma is_open_prod {s : set α} {t : set β} (hs : is_open s) (ht : is_open t) : is_open (set.prod s t) := is_open_inter (continuous_fst s hs) (continuous_snd t ht) lemma nhds_prod_eq {a : α} {b : β} : nhds (a, b) = filter.prod (nhds a) (nhds b) := by rw [filter.prod, prod.topological_space, nhds_sup, nhds_induced_eq_comap, nhds_induced_eq_comap] instance [topological_space α] [discrete_topology α] [topological_space β] [discrete_topology β] : discrete_topology (α × β) := ⟨eq_of_nhds_eq_nhds $ assume ⟨a, b⟩, by rw [nhds_prod_eq, nhds_discrete α, nhds_discrete β, nhds_top, filter.prod_pure_pure]⟩ lemma prod_mem_nhds_sets {s : set α} {t : set β} {a : α} {b : β} (ha : s ∈ (nhds a).sets) (hb : t ∈ (nhds b).sets) : set.prod s t ∈ (nhds (a, b)).sets := by rw [nhds_prod_eq]; exact prod_mem_prod ha hb lemma nhds_swap (a : α) (b : β) : nhds (a, b) = (nhds (b, a)).map prod.swap := by rw [nhds_prod_eq, filter.prod_comm, nhds_prod_eq]; refl lemma tendsto_prod_mk_nhds {γ} {a : α} {b : β} {f : filter γ} {ma : γ → α} {mb : γ → β} (ha : tendsto ma f (nhds a)) (hb : tendsto mb f (nhds b)) : tendsto (λc, (ma c, mb c)) f (nhds (a, b)) := by rw [nhds_prod_eq]; exact filter.tendsto.prod_mk ha hb lemma prod_generate_from_generate_from_eq {s : set (set α)} {t : set (set β)} (hs : ⋃₀ s = univ) (ht : ⋃₀ t = univ) : @prod.topological_space α β (generate_from s) (generate_from t) = generate_from {g | ∃u∈s, ∃v∈t, g = set.prod u v} := let G := generate_from {g | ∃u∈s, ∃v∈t, g = set.prod u v} in le_antisymm (sup_le (induced_le_iff_le_coinduced.mpr $ generate_from_le $ assume u hu, have (⋃v∈t, set.prod u v) = prod.fst ⁻¹' u, from calc (⋃v∈t, set.prod u v) = set.prod u univ : set.ext $ assume ⟨a, b⟩, by rw ← ht; simp [and.left_comm] {contextual:=tt} ... = prod.fst ⁻¹' u : by simp [set.prod, preimage], show G.is_open (prod.fst ⁻¹' u), from this ▸ @is_open_Union _ _ G _ $ assume v, @is_open_Union _ _ G _ $ assume hv, generate_open.basic _ ⟨_, hu, _, hv, rfl⟩) (induced_le_iff_le_coinduced.mpr $ generate_from_le $ assume v hv, have (⋃u∈s, set.prod u v) = prod.snd ⁻¹' v, from calc (⋃u∈s, set.prod u v) = set.prod univ v: set.ext $ assume ⟨a, b⟩, by rw [←hs]; by_cases b ∈ v; simp [h] {contextual:=tt} ... = prod.snd ⁻¹' v : by simp [set.prod, preimage], show G.is_open (prod.snd ⁻¹' v), from this ▸ @is_open_Union _ _ G _ $ assume u, @is_open_Union _ _ G _ $ assume hu, generate_open.basic _ ⟨_, hu, _, hv, rfl⟩)) (generate_from_le $ assume g ⟨u, hu, v, hv, g_eq⟩, g_eq.symm ▸ @is_open_prod _ _ (generate_from s) (generate_from t) _ _ (generate_open.basic _ hu) (generate_open.basic _ hv)) lemma prod_eq_generate_from [tα : topological_space α] [tβ : topological_space β] : prod.topological_space = generate_from {g | ∃(s:set α) (t:set β), is_open s ∧ is_open t ∧ g = set.prod s t} := le_antisymm (sup_le (assume s ⟨t, ht, s_eq⟩, have set.prod t univ = s, by simp [s_eq, preimage, set.prod], this ▸ (generate_open.basic _ ⟨t, univ, ht, is_open_univ, rfl⟩)) (assume s ⟨t, ht, s_eq⟩, have set.prod univ t = s, by simp [s_eq, preimage, set.prod], this ▸ (generate_open.basic _ ⟨univ, t, is_open_univ, ht, rfl⟩))) (generate_from_le $ assume g ⟨s, t, hs, ht, g_eq⟩, g_eq.symm ▸ is_open_prod hs ht) lemma is_open_prod_iff {s : set (α×β)} : is_open s ↔ (∀a b, (a, b) ∈ s → ∃u v, is_open u ∧ is_open v ∧ a ∈ u ∧ b ∈ v ∧ set.prod u v ⊆ s) := begin rw [is_open_iff_nhds], simp [nhds_prod_eq, mem_prod_iff], simp [mem_nhds_sets_iff], exact forall_congr (assume a, ball_congr $ assume b h, ⟨assume ⟨u', ⟨u, us, uo, au⟩, v', ⟨v, vs, vo, bv⟩, h⟩, ⟨u, uo, v, vo, au, bv, subset.trans (set.prod_mono us vs) h⟩, assume ⟨u, uo, v, vo, au, bv, h⟩, ⟨u, ⟨u, subset.refl u, uo, au⟩, v, ⟨v, subset.refl v, vo, bv⟩, h⟩⟩) end lemma closure_prod_eq {s : set α} {t : set β} : closure (set.prod s t) = set.prod (closure s) (closure t) := set.ext $ assume ⟨a, b⟩, have filter.prod (nhds a) (nhds b) ⊓ principal (set.prod s t) = filter.prod (nhds a ⊓ principal s) (nhds b ⊓ principal t), by rw [←prod_inf_prod, prod_principal_principal], by simp [closure_eq_nhds, nhds_prod_eq, this]; exact prod_neq_bot lemma mem_closure2 [topological_space α] [topological_space β] [topological_space γ] {s : set α} {t : set β} {u : set γ} {f : α → β → γ} {a : α} {b : β} (hf : continuous (λp:α×β, f p.1 p.2)) (ha : a ∈ closure s) (hb : b ∈ closure t) (hu : ∀a b, a ∈ s → b ∈ t → f a b ∈ u) : f a b ∈ closure u := have (a, b) ∈ closure (set.prod s t), by rw [closure_prod_eq]; from ⟨ha, hb⟩, show (λp:α×β, f p.1 p.2) (a, b) ∈ closure u, from mem_closure hf this $ assume ⟨a, b⟩ ⟨ha, hb⟩, hu a b ha hb lemma is_closed_prod [topological_space α] [topological_space β] {s₁ : set α} {s₂ : set β} (h₁ : is_closed s₁) (h₂ : is_closed s₂) : is_closed (set.prod s₁ s₂) := closure_eq_iff_is_closed.mp $ by simp [h₁, h₂, closure_prod_eq, closure_eq_of_is_closed] protected lemma is_open_map.prod [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] {f : α → β} {g : γ → δ} (hf : is_open_map f) (hg : is_open_map g) : is_open_map (λ p : α × γ, (f p.1, g p.2)) := begin rw [is_open_map_iff_nhds_le], rintros ⟨a, b⟩, rw [nhds_prod_eq, nhds_prod_eq, ← filter.prod_map_map_eq], exact filter.prod_mono ((is_open_map_iff_nhds_le f).1 hf a) ((is_open_map_iff_nhds_le g).1 hg b) end section tube_lemma def nhds_contain_boxes (s : set α) (t : set β) : Prop := ∀ (n : set (α × β)) (hn : is_open n) (hp : set.prod s t ⊆ n), ∃ (u : set α) (v : set β), is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ set.prod u v ⊆ n lemma nhds_contain_boxes.symm {s : set α} {t : set β} : nhds_contain_boxes s t → nhds_contain_boxes t s := assume H n hn hp, let ⟨u, v, uo, vo, su, tv, p⟩ := H (prod.swap ⁻¹' n) (continuous_swap n hn) (by rwa [←image_subset_iff, prod.swap, image_swap_prod]) in ⟨v, u, vo, uo, tv, su, by rwa [←image_subset_iff, prod.swap, image_swap_prod] at p⟩ lemma nhds_contain_boxes.comm {s : set α} {t : set β} : nhds_contain_boxes s t ↔ nhds_contain_boxes t s := iff.intro nhds_contain_boxes.symm nhds_contain_boxes.symm lemma nhds_contain_boxes_of_singleton {x : α} {y : β} : nhds_contain_boxes ({x} : set α) ({y} : set β) := assume n hn hp, let ⟨u, v, uo, vo, xu, yv, hp'⟩ := is_open_prod_iff.mp hn x y (hp $ by simp) in ⟨u, v, uo, vo, by simpa, by simpa, hp'⟩ lemma nhds_contain_boxes_of_compact {s : set α} (hs : compact s) (t : set β) (H : ∀ x ∈ s, nhds_contain_boxes ({x} : set α) t) : nhds_contain_boxes s t := assume n hn hp, have ∀x : subtype s, ∃uv : set α × set β, is_open uv.1 ∧ is_open uv.2 ∧ {↑x} ⊆ uv.1 ∧ t ⊆ uv.2 ∧ set.prod uv.1 uv.2 ⊆ n, from assume ⟨x, hx⟩, have set.prod {x} t ⊆ n, from subset.trans (prod_mono (by simpa) (subset.refl _)) hp, let ⟨ux,vx,H1⟩ := H x hx n hn this in ⟨⟨ux,vx⟩,H1⟩, let ⟨uvs, h⟩ := classical.axiom_of_choice this in have us_cover : s ⊆ ⋃i, (uvs i).1, from assume x hx, set.subset_Union _ ⟨x,hx⟩ (by simpa using (h ⟨x,hx⟩).2.2.1), let ⟨s0, _, s0_fin, s0_cover⟩ := compact_elim_finite_subcover_image hs (λi _, (h i).1) $ by rw bUnion_univ; exact us_cover in let u := ⋃(i ∈ s0), (uvs i).1 in let v := ⋂(i ∈ s0), (uvs i).2 in have is_open u, from is_open_bUnion (λi _, (h i).1), have is_open v, from is_open_bInter s0_fin (λi _, (h i).2.1), have t ⊆ v, from subset_bInter (λi _, (h i).2.2.2.1), have set.prod u v ⊆ n, from assume ⟨x',y'⟩ ⟨hx',hy'⟩, have ∃i ∈ s0, x' ∈ (uvs i).1, by simpa using hx', let ⟨i,is0,hi⟩ := this in (h i).2.2.2.2 ⟨hi, (bInter_subset_of_mem is0 : v ⊆ (uvs i).2) hy'⟩, ⟨u, v, ‹is_open u›, ‹is_open v›, s0_cover, ‹t ⊆ v›, ‹set.prod u v ⊆ n›⟩ lemma generalized_tube_lemma {s : set α} (hs : compact s) {t : set β} (ht : compact t) {n : set (α × β)} (hn : is_open n) (hp : set.prod s t ⊆ n) : ∃ (u : set α) (v : set β), is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ set.prod u v ⊆ n := have _, from nhds_contain_boxes_of_compact hs t $ assume x _, nhds_contain_boxes.symm $ nhds_contain_boxes_of_compact ht {x} $ assume y _, nhds_contain_boxes_of_singleton, this n hn hp end tube_lemma lemma is_closed_diagonal [topological_space α] [t2_space α] : is_closed {p:α×α | p.1 = p.2} := is_closed_iff_nhds.mpr $ assume ⟨a₁, a₂⟩ h, eq_of_nhds_neq_bot $ assume : nhds a₁ ⊓ nhds a₂ = ⊥, h $ let ⟨t₁, ht₁, t₂, ht₂, (h' : t₁ ∩ t₂ ⊆ ∅)⟩ := by rw [←empty_in_sets_eq_bot, mem_inf_sets] at this; exact this in begin rw [nhds_prod_eq, ←empty_in_sets_eq_bot], apply filter.sets_of_superset, apply inter_mem_inf_sets (prod_mem_prod ht₁ ht₂) (mem_principal_sets.mpr (subset.refl _)), exact assume ⟨x₁, x₂⟩ ⟨⟨hx₁, hx₂⟩, (heq : x₁ = x₂)⟩, show false, from @h' x₁ ⟨hx₁, heq.symm ▸ hx₂⟩ end lemma is_closed_eq [topological_space α] [t2_space α] [topological_space β] {f g : β → α} (hf : continuous f) (hg : continuous g) : is_closed {x:β | f x = g x} := continuous_iff_is_closed.mp (hf.prod_mk hg) _ is_closed_diagonal lemma diagonal_eq_range_diagonal_map : {p:α×α | p.1 = p.2} = range (λx, (x,x)) := ext $ assume p, iff.intro (assume h, ⟨p.1, prod.ext_iff.2 ⟨rfl, h⟩⟩) (assume ⟨x, hx⟩, show p.1 = p.2, by rw ←hx) lemma prod_subset_compl_diagonal_iff_disjoint {s t : set α} : set.prod s t ⊆ - {p:α×α | p.1 = p.2} ↔ s ∩ t = ∅ := by rw [eq_empty_iff_forall_not_mem, subset_compl_comm, diagonal_eq_range_diagonal_map, range_subset_iff]; simp lemma compact_compact_separated [t2_space α] {s t : set α} (hs : compact s) (ht : compact t) (hst : s ∩ t = ∅) : ∃u v : set α, is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ u ∩ v = ∅ := by simp only [prod_subset_compl_diagonal_iff_disjoint.symm] at ⊢ hst; exact generalized_tube_lemma hs ht is_closed_diagonal hst lemma closed_of_compact [t2_space α] (s : set α) (hs : compact s) : is_closed s := is_open_compl_iff.mpr $ is_open_iff_forall_mem_open.mpr $ assume x hx, let ⟨u, v, uo, vo, su, xv, uv⟩ := compact_compact_separated hs (compact_singleton : compact {x}) (by rwa [inter_comm, ←subset_compl_iff_disjoint, singleton_subset_iff]) in have v ⊆ -s, from subset_compl_comm.mp (subset.trans su (subset_compl_iff_disjoint.mpr uv)), ⟨v, this, vo, by simpa using xv⟩ /- TODO: more fine grained instances for first_countable_topology, separable_space, t2_space, ... -/ instance [second_countable_topology α] [second_countable_topology β] : second_countable_topology (α × β) := ⟨let ⟨a, ha₁, ha₂, ha₃, ha₄, ha₅⟩ := is_open_generated_countable_inter α in let ⟨b, hb₁, hb₂, hb₃, hb₄, hb₅⟩ := is_open_generated_countable_inter β in ⟨{g | ∃u∈a, ∃v∈b, g = set.prod u v}, have {g | ∃u∈a, ∃v∈b, g = set.prod u v} = (⋃u∈a, ⋃v∈b, {set.prod u v}), by apply set.ext; simp, by rw [this]; exact (countable_bUnion ha₁ $ assume u hu, countable_bUnion hb₁ $ by simp), by rw [ha₅, hb₅, prod_generate_from_generate_from_eq ha₄ hb₄]⟩⟩ end prod section compact_and_proper_spaces /--Type class for compact spaces. Separation is sometimes included in the definition, especially in the French literature, but we do not include it here.-/ class compact_space (α : Type*) [topological_space α] : Prop := (compact_univ : compact (univ : set α)) lemma compact_univ [topological_space α] [h : compact_space α] : compact (univ : set α) := h.compact_univ lemma compact_of_closed [topological_space α] [compact_space α] {s : set α} (h : is_closed s) : compact s := compact_of_is_closed_subset compact_univ h (subset_univ _) /-- There are various definitions of "locally compact space" in the literature, which agree for Hausdorff spaces but not in general. This one is the precise condition on X needed for the evaluation `map C(X, Y) × X → Y` to be continuous for all `Y` when `C(X, Y)` is given the compact-open topology. -/ class locally_compact_space (α : Type*) [topological_space α] : Prop := (local_compact_nhds : ∀ (x : α) (n ∈ (nhds x).sets), ∃ s ∈ (nhds x).sets, s ⊆ n ∧ compact s) lemma locally_compact_of_compact_nhds [topological_space α] [t2_space α] (h : ∀ x : α, ∃ s, s ∈ (nhds x).sets ∧ compact s) : locally_compact_space α := ⟨assume x n hn, let ⟨u, un, uo, xu⟩ := mem_nhds_sets_iff.mp hn in let ⟨k, kx, kc⟩ := h x in -- K is compact but not necessarily contained in N. -- K \ U is again compact and doesn't contain x, so -- we may find open sets V, W separating x from K \ U. -- Then K \ W is a compact neighborhood of x contained in U. let ⟨v, w, vo, wo, xv, kuw, vw⟩ := compact_compact_separated compact_singleton (compact_diff kc uo) (by rw [singleton_inter_eq_empty]; exact λ h, h.2 xu) in have wn : -w ∈ (nhds x).sets, from mem_nhds_sets_iff.mpr ⟨v, subset_compl_iff_disjoint.mpr vw, vo, singleton_subset_iff.mp xv⟩, ⟨k - w, filter.inter_mem_sets kx wn, subset.trans (diff_subset_comm.mp kuw) un, compact_diff kc wo⟩⟩ instance locally_compact_of_compact [topological_space α] [t2_space α] [compact_space α] : locally_compact_space α := locally_compact_of_compact_nhds (assume x, ⟨univ, mem_nhds_sets is_open_univ trivial, compact_univ⟩) -- We can't make this an instance because it could cause an instance loop. lemma normal_of_compact_t2 [topological_space α] [compact_space α] [t2_space α] : normal_space α := ⟨assume s t hs ht st, compact_compact_separated (compact_of_closed hs) (compact_of_closed ht) st⟩ end compact_and_proper_spaces section sum variables [topological_space α] [topological_space β] [topological_space γ] lemma continuous_inl : continuous (@sum.inl α β) := continuous_inf_rng_left continuous_coinduced_rng lemma continuous_inr : continuous (@sum.inr α β) := continuous_inf_rng_right continuous_coinduced_rng lemma continuous_sum_rec {f : α → γ} {g : β → γ} (hf : continuous f) (hg : continuous g) : @continuous (α ⊕ β) γ _ _ (@sum.rec α β (λ_, γ) f g) := continuous_inf_dom hf hg lemma embedding_inl : embedding (@sum.inl α β) := ⟨λ _ _, sum.inl.inj_iff.mp, begin unfold sum.topological_space, apply le_antisymm, { intros u hu, existsi (sum.inl '' u), change (is_open (sum.inl ⁻¹' (@sum.inl α β '' u)) ∧ is_open (sum.inr ⁻¹' (@sum.inl α β '' u))) ∧ u = sum.inl ⁻¹' (sum.inl '' u), have : sum.inl ⁻¹' (@sum.inl α β '' u) = u := preimage_image_eq u (λ _ _, sum.inl.inj_iff.mp), rw this, have : sum.inr ⁻¹' (@sum.inl α β '' u) = ∅ := eq_empty_iff_forall_not_mem.mpr (assume a ⟨b, _, h⟩, sum.inl_ne_inr h), rw this, exact ⟨⟨hu, is_open_empty⟩, rfl⟩ }, { rw induced_le_iff_le_coinduced, exact lattice.inf_le_left } end⟩ lemma embedding_inr : embedding (@sum.inr α β) := ⟨λ _ _, sum.inr.inj_iff.mp, begin unfold sum.topological_space, apply le_antisymm, { intros u hu, existsi (sum.inr '' u), change (is_open (sum.inl ⁻¹' (@sum.inr α β '' u)) ∧ is_open (sum.inr ⁻¹' (@sum.inr α β '' u))) ∧ u = sum.inr ⁻¹' (sum.inr '' u), have : sum.inl ⁻¹' (@sum.inr α β '' u) = ∅ := eq_empty_iff_forall_not_mem.mpr (assume b ⟨a, _, h⟩, sum.inr_ne_inl h), rw this, have : sum.inr ⁻¹' (@sum.inr α β '' u) = u := preimage_image_eq u (λ _ _, sum.inr.inj_iff.mp), rw this, exact ⟨⟨is_open_empty, hu⟩, rfl⟩ }, { rw induced_le_iff_le_coinduced, exact lattice.inf_le_right } end⟩ end sum section subtype variables [topological_space α] [topological_space β] [topological_space γ] {p : α → Prop} lemma embedding_graph {f : α → β} (hf : continuous f) : embedding (λx, (x, f x)) := embedding_of_embedding_compose (continuous_id.prod_mk hf) continuous_fst embedding_id lemma embedding_subtype_val : embedding (@subtype.val α p) := ⟨assume a₁ a₂, subtype.eq, rfl⟩ lemma continuous_subtype_val : continuous (@subtype.val α p) := continuous_induced_dom lemma continuous_subtype_mk {f : β → α} (hp : ∀x, p (f x)) (h : continuous f) : continuous (λx, (⟨f x, hp x⟩ : subtype p)) := continuous_induced_rng h lemma tendsto_subtype_val [topological_space α] {p : α → Prop} {a : subtype p} : tendsto subtype.val (nhds a) (nhds a.val) := continuous_iff_tendsto.1 continuous_subtype_val _ lemma map_nhds_subtype_val_eq {a : α} (ha : p a) (h : {a | p a} ∈ (nhds a).sets) : map (@subtype.val α p) (nhds ⟨a, ha⟩) = nhds a := map_nhds_induced_eq (by simp [subtype_val_image, h]) lemma nhds_subtype_eq_comap {a : α} {h : p a} : nhds (⟨a, h⟩ : subtype p) = comap subtype.val (nhds a) := nhds_induced_eq_comap lemma tendsto_subtype_rng [topological_space α] {p : α → Prop} {b : filter β} {f : β → subtype p} : ∀{a:subtype p}, tendsto f b (nhds a) ↔ tendsto (λx, subtype.val (f x)) b (nhds a.val) | ⟨a, ha⟩ := by rw [nhds_subtype_eq_comap, tendsto_comap_iff] lemma continuous_subtype_nhds_cover {ι : Sort*} {f : α → β} {c : ι → α → Prop} (c_cover : ∀x:α, ∃i, {x | c i x} ∈ (nhds x).sets) (f_cont : ∀i, continuous (λ(x : subtype (c i)), f x.val)) : continuous f := continuous_iff_tendsto.mpr $ assume x, let ⟨i, (c_sets : {x | c i x} ∈ (nhds x).sets)⟩ := c_cover x in let x' : subtype (c i) := ⟨x, mem_of_nhds c_sets⟩ in calc map f (nhds x) = map f (map subtype.val (nhds x')) : congr_arg (map f) (map_nhds_subtype_val_eq _ $ c_sets).symm ... = map (λx:subtype (c i), f x.val) (nhds x') : rfl ... ≤ nhds (f x) : continuous_iff_tendsto.mp (f_cont i) x' lemma continuous_subtype_is_closed_cover {ι : Sort*} {f : α → β} (c : ι → α → Prop) (h_lf : locally_finite (λi, {x | c i x})) (h_is_closed : ∀i, is_closed {x | c i x}) (h_cover : ∀x, ∃i, c i x) (f_cont : ∀i, continuous (λ(x : subtype (c i)), f x.val)) : continuous f := continuous_iff_is_closed.mpr $ assume s hs, have ∀i, is_closed (@subtype.val α {x | c i x} '' (f ∘ subtype.val ⁻¹' s)), from assume i, embedding_is_closed embedding_subtype_val (by simp [subtype_val_range]; exact h_is_closed i) (continuous_iff_is_closed.mp (f_cont i) _ hs), have is_closed (⋃i, @subtype.val α {x | c i x} '' (f ∘ subtype.val ⁻¹' s)), from is_closed_Union_of_locally_finite (locally_finite_subset h_lf $ assume i x ⟨⟨x', hx'⟩, _, heq⟩, heq ▸ hx') this, have f ⁻¹' s = (⋃i, @subtype.val α {x | c i x} '' (f ∘ subtype.val ⁻¹' s)), begin apply set.ext, have : ∀ (x : α), f x ∈ s ↔ ∃ (i : ι), c i x ∧ f x ∈ s := λ x, ⟨λ hx, let ⟨i, hi⟩ := h_cover x in ⟨i, hi, hx⟩, λ ⟨i, hi, hx⟩, hx⟩, simp [and.comm, and.left_comm], simpa [(∘)], end, by rwa [this] lemma closure_subtype {x : {a // p a}} {s : set {a // p a}}: x ∈ closure s ↔ x.val ∈ closure (subtype.val '' s) := closure_induced $ assume x y, subtype.eq lemma compact_iff_compact_in_subtype {s : set {a // p a}} : compact s ↔ compact (subtype.val '' s) := compact_iff_compact_image_of_embedding embedding_subtype_val lemma compact_iff_compact_univ {s : set α} : compact s ↔ compact (univ : set (subtype s)) := by rw [compact_iff_compact_in_subtype, image_univ, subtype_val_range]; refl end subtype section quotient variables [topological_space α] [topological_space β] [topological_space γ] variables {r : α → α → Prop} {s : setoid α} lemma quotient_map_quot_mk : quotient_map (@quot.mk α r) := ⟨quot.exists_rep, rfl⟩ lemma continuous_quot_mk : continuous (@quot.mk α r) := continuous_coinduced_rng lemma continuous_quot_lift {f : α → β} (hr : ∀ a b, r a b → f a = f b) (h : continuous f) : continuous (quot.lift f hr : quot r → β) := continuous_coinduced_dom h lemma quotient_map_quotient_mk : quotient_map (@quotient.mk α s) := quotient_map_quot_mk lemma continuous_quotient_mk : continuous (@quotient.mk α s) := continuous_coinduced_rng lemma continuous_quotient_lift {f : α → β} (hs : ∀ a b, a ≈ b → f a = f b) (h : continuous f) : continuous (quotient.lift f hs : quotient s → β) := continuous_coinduced_dom h instance quot.compact_space {r : α → α → Prop} [topological_space α] [compact_space α] : compact_space (quot r) := ⟨begin have : quot.mk r '' univ = univ, by rw [image_univ, range_iff_surjective]; exact quot.exists_rep, rw ←this, exact compact_image compact_univ continuous_quot_mk end⟩ instance quotient.compact_space {s : setoid α} [topological_space α] [compact_space α] : compact_space (quotient s) := quot.compact_space end quotient section pi variables {ι : Type*} {π : ι → Type*} open topological_space lemma continuous_pi [topological_space α] [∀i, topological_space (π i)] {f : α → Πi:ι, π i} (h : ∀i, continuous (λa, f a i)) : continuous f := continuous_supr_rng $ assume i, continuous_induced_rng $ h i lemma continuous_apply [∀i, topological_space (π i)] (i : ι) : continuous (λp:Πi, π i, p i) := continuous_supr_dom continuous_induced_dom lemma nhds_pi [t : ∀i, topological_space (π i)] {a : Πi, π i} : nhds a = (⨅i, comap (λx, x i) (nhds (a i))) := calc nhds a = (⨅i, @nhds _ (@topological_space.induced _ _ (λx:Πi, π i, x i) (t i)) a) : nhds_supr ... = (⨅i, comap (λx, x i) (nhds (a i))) : by simp [nhds_induced_eq_comap] /-- Tychonoff's theorem -/ lemma compact_pi_infinite [∀i, topological_space (π i)] {s : Πi:ι, set (π i)} : (∀i, compact (s i)) → compact {x : Πi:ι, π i | ∀i, x i ∈ s i} := begin simp [compact_iff_ultrafilter_le_nhds, nhds_pi], exact assume h f hf hfs, let p : Πi:ι, filter (π i) := λi, map (λx:Πi:ι, π i, x i) f in have ∀i:ι, ∃a, a∈s i ∧ p i ≤ nhds a, from assume i, h i (p i) (ultrafilter_map hf) $ show (λx:Πi:ι, π i, x i) ⁻¹' s i ∈ f.sets, from mem_sets_of_superset hfs $ assume x (hx : ∀i, x i ∈ s i), hx i, let ⟨a, ha⟩ := classical.axiom_of_choice this in ⟨a, assume i, (ha i).left, assume i, map_le_iff_le_comap.mp $ (ha i).right⟩ end lemma is_open_set_pi [∀a, topological_space (π a)] {i : set ι} {s : Πa, set (π a)} (hi : finite i) (hs : ∀a∈i, is_open (s a)) : is_open (pi i s) := by rw [pi_def]; exact (is_open_bInter hi $ assume a ha, continuous_apply a _ $ hs a ha) lemma pi_eq_generate_from [∀a, topological_space (π a)] : Pi.topological_space = generate_from {g | ∃(s:Πa, set (π a)) (i : finset ι), (∀a∈i, is_open (s a)) ∧ g = pi ↑i s} := le_antisymm (supr_le $ assume a s ⟨t, ht, s_eq⟩, generate_open.basic _ $ ⟨function.update (λa, univ) a t, {a}, by simpa using ht, by ext f; simp [s_eq, pi]⟩) (generate_from_le $ assume g ⟨s, i, hi, eq⟩, eq.symm ▸ is_open_set_pi (finset.finite_to_set _) hi) lemma pi_generate_from_eq {g : Πa, set (set (π a))} : @Pi.topological_space ι π (λa, generate_from (g a)) = generate_from {t | ∃(s:Πa, set (π a)) (i : finset ι), (∀a∈i, s a ∈ g a) ∧ t = pi ↑i s} := let G := {t | ∃(s:Πa, set (π a)) (i : finset ι), (∀a∈i, s a ∈ g a) ∧ t = pi ↑i s} in begin rw [pi_eq_generate_from], refine le_antisymm (generate_from_le _) (generate_from_mono _), { rintros s ⟨t, i, hi, rfl⟩, rw [pi_def], apply is_open_bInter (finset.finite_to_set _), assume a ha, show ((generate_from G).coinduced (λf:Πa, π a, f a)).is_open (t a), refine generate_from_le _ _ (hi a ha), exact assume s hs, generate_open.basic _ ⟨function.update (λa, univ) a s, {a}, by simp [hs]⟩ }, exact assume s ⟨t, i, ht, eq⟩, ⟨t, i, assume a ha, generate_open.basic _ (ht a ha), eq⟩ end lemma pi_generate_from_eq_fintype {g : Πa, set (set (π a))} [fintype ι] (hg : ∀a, ⋃₀ g a = univ) : @Pi.topological_space ι π (λa, generate_from (g a)) = generate_from {t | ∃(s:Πa, set (π a)), (∀a, s a ∈ g a) ∧ t = pi univ s} := let G := {t | ∃(s:Πa, set (π a)), (∀a, s a ∈ g a) ∧ t = pi univ s} in begin rw [pi_generate_from_eq], refine le_antisymm (generate_from_le _) (generate_from_mono _), { rintros s ⟨t, i, ht, rfl⟩, apply is_open_iff_forall_mem_open.2 _, assume f hf, choose c hc using show ∀a, ∃s, s ∈ g a ∧ f a ∈ s, { assume a, have : f a ∈ ⋃₀ g a, { rw [hg], apply mem_univ }, simpa }, refine ⟨pi univ (λa, if a ∈ i then t a else (c : Πa, set (π a)) a), _, _, _⟩, { simp [pi_if] }, { refine generate_open.basic _ ⟨_, assume a, _, rfl⟩, by_cases a ∈ i; simp [*, pi] at * }, { have : f ∈ pi {a | a ∉ i} c, { simp [*, pi] at * }, simpa [pi_if, hf] } }, exact assume s ⟨t, ht, eq⟩, ⟨t, finset.univ, by simp [ht, eq]⟩ end instance second_countable_topology_fintype [fintype ι] [t : ∀a, topological_space (π a)] [sc : ∀a, second_countable_topology (π a)] : second_countable_topology (∀a, π a) := have ∀i, ∃b : set (set (π i)), countable b ∧ ∅ ∉ b ∧ is_topological_basis b, from assume a, @is_open_generated_countable_inter (π a) _ (sc a), let ⟨g, hg⟩ := classical.axiom_of_choice this in have t = (λa, generate_from (g a)), from funext $ assume a, (hg a).2.2.2.2, begin constructor, refine ⟨pi univ '' pi univ g, countable_image _ _, _⟩, { suffices : countable {f : Πa, set (π a) | ∀a, f a ∈ g a}, { simpa [pi] }, exact countable_pi (assume i, (hg i).1), }, rw [this, pi_generate_from_eq_fintype], { congr' 1, ext f, simp [pi, eq_comm] }, exact assume a, (hg a).2.2.2.1 end end pi namespace list variables [topological_space α] [topological_space β] lemma tendsto_cons' {a : α} {l : list α} : tendsto (λp:α×list α, list.cons p.1 p.2) ((nhds a).prod (nhds l)) (nhds (a :: l)) := by rw [nhds_cons, tendsto, map_prod]; exact le_refl _ lemma tendsto_cons {f : α → β} {g : α → list β} {a : _root_.filter α} {b : β} {l : list β} (hf : tendsto f a (nhds b)) (hg : tendsto g a (nhds l)): tendsto (λa, list.cons (f a) (g a)) a (nhds (b :: l)) := (tendsto.prod_mk hf hg).comp tendsto_cons' lemma tendsto_cons_iff [topological_space β] {f : list α → β} {b : _root_.filter β} {a : α} {l : list α} : tendsto f (nhds (a :: l)) b ↔ tendsto (λp:α×list α, f (p.1 :: p.2)) ((nhds a).prod (nhds l)) b := have nhds (a :: l) = ((nhds a).prod (nhds l)).map (λp:α×list α, (p.1 :: p.2)), begin simp only [nhds_cons, prod_eq, (filter.map_def _ _).symm, (filter.seq_eq_filter_seq _ _).symm], simp [-filter.seq_eq_filter_seq, -filter.map_def, (∘)] with functor_norm, end, by rw [this, filter.tendsto_map'_iff] lemma tendsto_nhds [topological_space β] {f : list α → β} {r : list α → _root_.filter β} (h_nil : tendsto f (pure []) (r [])) (h_cons : ∀l a, tendsto f (nhds l) (r l) → tendsto (λp:α×list α, f (p.1 :: p.2)) ((nhds a).prod (nhds l)) (r (a::l))) : ∀l, tendsto f (nhds l) (r l) | [] := by rwa [nhds_nil] | (a::l) := by rw [tendsto_cons_iff]; exact h_cons l a (tendsto_nhds l) lemma tendsto_length [topological_space α] : ∀(l : list α), tendsto list.length (nhds l) (nhds l.length) := begin simp only [nhds_discrete], refine tendsto_nhds _ _, { exact tendsto_pure_pure _ _ }, { assume l a ih, dsimp only [list.length], refine tendsto.comp _ (tendsto_pure_pure (λx, x + 1) _), refine tendsto.comp tendsto_snd ih } end lemma tendsto_insert_nth' {a : α} : ∀{n : ℕ} {l : list α}, tendsto (λp:α×list α, insert_nth n p.1 p.2) ((nhds a).prod (nhds l)) (nhds (insert_nth n a l)) | 0 l := tendsto_cons' | (n+1) [] := suffices tendsto (λa, []) (nhds a) (nhds ([] : list α)), by simpa [nhds_nil, tendsto, map_prod, -filter.pure_def, (∘), insert_nth], tendsto_const_nhds | (n+1) (a'::l) := have (nhds a).prod (nhds (a' :: l)) = ((nhds a).prod ((nhds a').prod (nhds l))).map (λp:α×α×list α, (p.1, p.2.1 :: p.2.2)), begin simp only [nhds_cons, prod_eq, (filter.map_def _ _).symm, (filter.seq_eq_filter_seq _ _).symm], simp [-filter.seq_eq_filter_seq, -filter.map_def, (∘)] with functor_norm end, begin rw [this, tendsto_map'_iff], exact tendsto_cons (tendsto_snd.comp tendsto_fst) ((tendsto.prod_mk tendsto_fst (tendsto_snd.comp tendsto_snd)).comp (@tendsto_insert_nth' n l)) end lemma tendsto_insert_nth {n : ℕ} {a : α} {l : list α} {f : β → α} {g : β → list α} {b : _root_.filter β} (hf : tendsto f b (nhds a)) (hg : tendsto g b (nhds l)) : tendsto (λb:β, insert_nth n (f b) (g b)) b (nhds (insert_nth n a l)) := (tendsto.prod_mk hf hg).comp tendsto_insert_nth' lemma continuous_insert_nth {n : ℕ} : continuous (λp:α×list α, insert_nth n p.1 p.2) := continuous_iff_tendsto.2 $ assume ⟨a, l⟩, by rw [nhds_prod_eq]; exact tendsto_insert_nth' lemma tendsto_remove_nth : ∀{n : ℕ} {l : list α}, tendsto (λl, remove_nth l n) (nhds l) (nhds (remove_nth l n)) | _ [] := by rw [nhds_nil]; exact tendsto_pure_nhds _ _ | 0 (a::l) := by rw [tendsto_cons_iff]; exact tendsto_snd | (n+1) (a::l) := begin rw [tendsto_cons_iff], dsimp [remove_nth], exact tendsto_cons tendsto_fst (tendsto_snd.comp (@tendsto_remove_nth n l)) end lemma continuous_remove_nth {n : ℕ} : continuous (λl : list α, remove_nth l n) := continuous_iff_tendsto.2 $ assume a, tendsto_remove_nth end list namespace vector open list filter instance (n : ℕ) [topological_space α] : topological_space (vector α n) := by unfold vector; apply_instance lemma cons_val {n : ℕ} {a : α} : ∀{v : vector α n}, (a :: v).val = a :: v.val | ⟨l, hl⟩ := rfl lemma tendsto_cons [topological_space α] {n : ℕ} {a : α} {l : vector α n}: tendsto (λp:α×vector α n, vector.cons p.1 p.2) ((nhds a).prod (nhds l)) (nhds (a :: l)) := by simp [tendsto_subtype_rng, cons_val]; exact tendsto_cons tendsto_fst (tendsto.comp tendsto_snd tendsto_subtype_val) lemma tendsto_insert_nth [topological_space α] {n : ℕ} {i : fin (n+1)} {a:α} : ∀{l:vector α n}, tendsto (λp:α×vector α n, insert_nth p.1 i p.2) ((nhds a).prod (nhds l)) (nhds (insert_nth a i l)) | ⟨l, hl⟩ := begin rw [insert_nth, tendsto_subtype_rng], simp [insert_nth_val], exact list.tendsto_insert_nth tendsto_fst (tendsto.comp tendsto_snd tendsto_subtype_val) end lemma continuous_insert_nth' [topological_space α] {n : ℕ} {i : fin (n+1)} : continuous (λp:α×vector α n, insert_nth p.1 i p.2) := continuous_iff_tendsto.2 $ assume ⟨a, l⟩, by rw [nhds_prod_eq]; exact tendsto_insert_nth lemma continuous_insert_nth [topological_space α] [topological_space β] {n : ℕ} {i : fin (n+1)} {f : β → α} {g : β → vector α n} (hf : continuous f) (hg : continuous g) : continuous (λb, insert_nth (f b) i (g b)) := continuous.comp (continuous.prod_mk hf hg) continuous_insert_nth' lemma tendsto_remove_nth [topological_space α] {n : ℕ} {i : fin (n+1)} : ∀{l:vector α (n+1)}, tendsto (remove_nth i) (nhds l) (nhds (remove_nth i l)) | ⟨l, hl⟩ := begin rw [remove_nth, tendsto_subtype_rng], simp [remove_nth_val], exact tendsto_subtype_val.comp list.tendsto_remove_nth end lemma continuous_remove_nth [topological_space α] {n : ℕ} {i : fin (n+1)} : continuous (remove_nth i : vector α (n+1) → vector α n) := continuous_iff_tendsto.2 $ assume ⟨a, l⟩, tendsto_remove_nth end vector -- TODO: use embeddings from above! structure dense_embedding [topological_space α] [topological_space β] (e : α → β) : Prop := (dense : ∀x, x ∈ closure (range e)) (inj : function.injective e) (induced : ∀a, comap e (nhds (e a)) = nhds a) theorem dense_embedding.mk' [topological_space α] [topological_space β] (e : α → β) (c : continuous e) (dense : ∀x, x ∈ closure (range e)) (inj : function.injective e) (H : ∀ (a:α) s ∈ (nhds a).sets, ∃t ∈ (nhds (e a)).sets, ∀ b, e b ∈ t → b ∈ s) : dense_embedding e := ⟨dense, inj, λ a, le_antisymm (by simpa [le_def] using H a) (tendsto_iff_comap.1 $ c.tendsto _)⟩ namespace dense_embedding variables [topological_space α] [topological_space β] variables {e : α → β} (de : dense_embedding e) protected lemma embedding (de : dense_embedding e) : embedding e := ⟨de.inj, eq_of_nhds_eq_nhds begin intro a, rw [← de.induced a, nhds_induced_eq_comap] end⟩ protected lemma tendsto (de : dense_embedding e) {a : α} : tendsto e (nhds a) (nhds (e a)) := by rw [←de.induced a]; exact tendsto_comap protected lemma continuous (de : dense_embedding e) {a : α} : continuous e := continuous_iff_tendsto.2 $ λ a, de.tendsto lemma inj_iff (de : dense_embedding e) {x y} : e x = e y ↔ x = y := de.inj.eq_iff lemma closure_range : closure (range e) = univ := let h := de.dense in set.ext $ assume x, ⟨assume _, trivial, assume _, @h x⟩ lemma self_sub_closure_image_preimage_of_open {s : set β} (de : dense_embedding e) : is_open s → s ⊆ closure (e '' (e ⁻¹' s)) := begin intros s_op b b_in_s, rw [image_preimage_eq_inter_range, mem_closure_iff], intros U U_op b_in, rw ←inter_assoc, have ne_e : U ∩ s ≠ ∅ := ne_empty_of_mem ⟨b_in, b_in_s⟩, exact (dense_iff_inter_open.1 de.closure_range) _ (is_open_inter U_op s_op) ne_e end lemma closure_image_nhds_of_nhds {s : set α} {a : α} (de : dense_embedding e) : s ∈ (nhds a).sets → closure (e '' s) ∈ (nhds (e a)).sets := begin rw [← de.induced a, mem_comap_sets], intro h, rcases h with ⟨t, t_nhd, sub⟩, rw mem_nhds_sets_iff at t_nhd, rcases t_nhd with ⟨U, U_sub, ⟨U_op, e_a_in_U⟩⟩, have := calc e ⁻¹' U ⊆ e⁻¹' t : preimage_mono U_sub ... ⊆ s : sub, have := calc U ⊆ closure (e '' (e ⁻¹' U)) : self_sub_closure_image_preimage_of_open de U_op ... ⊆ closure (e '' s) : closure_mono (image_subset e this), have U_nhd : U ∈ (nhds (e a)).sets := mem_nhds_sets U_op e_a_in_U, exact (nhds (e a)).sets_of_superset U_nhd this end variables [topological_space δ] {f : γ → α} {g : γ → δ} {h : δ → β} /-- γ -f→ α g↓ ↓e δ -h→ β -/ lemma tendsto_comap_nhds_nhds {d : δ} {a : α} (de : dense_embedding e) (H : tendsto h (nhds d) (nhds (e a))) (comm : h ∘ g = e ∘ f) : tendsto f (comap g (nhds d)) (nhds a) := begin have lim1 : map g (comap g (nhds d)) ≤ nhds d := map_comap_le, replace lim1 : map h (map g (comap g (nhds d))) ≤ map h (nhds d) := map_mono lim1, rw [filter.map_map, comm, ← filter.map_map, map_le_iff_le_comap] at lim1, have lim2 : comap e (map h (nhds d)) ≤ comap e (nhds (e a)) := comap_mono H, rw de.induced at lim2, exact le_trans lim1 lim2, end protected lemma nhds_inf_neq_bot (de : dense_embedding e) {b : β} : nhds b ⊓ principal (range e) ≠ ⊥ := begin have h := de.dense, simp [closure_eq_nhds] at h, exact h _ end lemma comap_nhds_neq_bot (de : dense_embedding e) {b : β} : comap e (nhds b) ≠ ⊥ := forall_sets_neq_empty_iff_neq_bot.mp $ assume s ⟨t, ht, (hs : e ⁻¹' t ⊆ s)⟩, have t ∩ range e ∈ (nhds b ⊓ principal (range e)).sets, from inter_mem_inf_sets ht (subset.refl _), let ⟨_, ⟨hx₁, y, rfl⟩⟩ := inhabited_of_mem_sets de.nhds_inf_neq_bot this in subset_ne_empty hs $ ne_empty_of_mem hx₁ variables [topological_space γ] /-- If `e : α → β` is a dense embedding, then any function `α → γ` extends to a function `β → γ`. It only extends the parts of `β` which are not mapped by `e`, everything else equal to `f (e a)`. This allows us to gain equality even if `γ` is not T2. -/ def extend (de : dense_embedding e) (f : α → γ) (b : β) : γ := have nonempty γ, from let ⟨_, ⟨_, a, _⟩⟩ := exists_mem_of_ne_empty (mem_closure_iff.1 (de.dense b) _ is_open_univ trivial) in ⟨f a⟩, if hb : b ∈ range e then f (classical.some hb) else @lim _ (classical.inhabited_of_nonempty this) _ (map f (comap e (nhds b))) lemma extend_e_eq {f : α → γ} (a : α) : de.extend f (e a) = f a := have e a ∈ range e := ⟨a, rfl⟩, begin simp [extend, this], congr, refine classical.some_spec2 (λx, x = a) _, exact assume a h, de.inj h end lemma extend_eq [t2_space γ] {b : β} {c : γ} {f : α → γ} (hf : map f (comap e (nhds b)) ≤ nhds c) : de.extend f b = c := begin by_cases hb : b ∈ range e, { rcases hb with ⟨a, rfl⟩, rw [extend_e_eq], have f_a_c : tendsto f (pure a) (nhds c), { rw [de.induced] at hf, refine le_trans (map_mono _) hf, exact pure_le_nhds a }, have f_a_fa : tendsto f (pure a) (nhds (f a)), { rw [tendsto, filter.map_pure], exact pure_le_nhds _ }, exact tendsto_nhds_unique pure_neq_bot f_a_fa f_a_c }, { simp [extend, hb], exact @lim_eq _ (id _) _ _ _ _ (by simp; exact comap_nhds_neq_bot de) hf } end lemma tendsto_extend [regular_space γ] {b : β} {f : α → γ} (de : dense_embedding e) (hf : {b | ∃c, tendsto f (comap e $ nhds b) (nhds c)} ∈ (nhds b).sets) : tendsto (de.extend f) (nhds b) (nhds (de.extend f b)) := let φ := {b | tendsto f (comap e $ nhds b) (nhds $ de.extend f b)} in have hφ : φ ∈ (nhds b).sets, from (nhds b).sets_of_superset hf $ assume b ⟨c, hc⟩, show tendsto f (comap e (nhds b)) (nhds (de.extend f b)), from (de.extend_eq hc).symm ▸ hc, assume s hs, let ⟨s'', hs''₁, hs''₂, hs''₃⟩ := nhds_is_closed hs in let ⟨s', hs'₁, (hs'₂ : e ⁻¹' s' ⊆ f ⁻¹' s'')⟩ := mem_of_nhds hφ hs''₁ in let ⟨t, (ht₁ : t ⊆ φ ∩ s'), ht₂, ht₃⟩ := mem_nhds_sets_iff.mp $ inter_mem_sets hφ hs'₁ in have h₁ : closure (f '' (e ⁻¹' s')) ⊆ s'', by rw [closure_subset_iff_subset_of_is_closed hs''₃, image_subset_iff]; exact hs'₂, have h₂ : t ⊆ de.extend f ⁻¹' closure (f '' (e ⁻¹' t)), from assume b' hb', have nhds b' ≤ principal t, by simp; exact mem_nhds_sets ht₂ hb', have map f (comap e (nhds b')) ≤ nhds (de.extend f b') ⊓ principal (f '' (e ⁻¹' t)), from calc _ ≤ map f (comap e (nhds b' ⊓ principal t)) : map_mono $ comap_mono $ le_inf (le_refl _) this ... ≤ map f (comap e (nhds b')) ⊓ map f (comap e (principal t)) : le_inf (map_mono $ comap_mono $ inf_le_left) (map_mono $ comap_mono $ inf_le_right) ... ≤ map f (comap e (nhds b')) ⊓ principal (f '' (e ⁻¹' t)) : by simp [le_refl] ... ≤ _ : inf_le_inf ((ht₁ hb').left) (le_refl _), show de.extend f b' ∈ closure (f '' (e ⁻¹' t)), begin rw [closure_eq_nhds], apply neq_bot_of_le_neq_bot _ this, simp, exact de.comap_nhds_neq_bot end, (nhds b).sets_of_superset (show t ∈ (nhds b).sets, from mem_nhds_sets ht₂ ht₃) (calc t ⊆ de.extend f ⁻¹' closure (f '' (e ⁻¹' t)) : h₂ ... ⊆ de.extend f ⁻¹' closure (f '' (e ⁻¹' s')) : preimage_mono $ closure_mono $ image_subset f $ preimage_mono $ subset.trans ht₁ $ inter_subset_right _ _ ... ⊆ de.extend f ⁻¹' s'' : preimage_mono h₁ ... ⊆ de.extend f ⁻¹' s : preimage_mono hs''₂) lemma continuous_extend [regular_space γ] {f : α → γ} (de : dense_embedding e) (hf : ∀b, ∃c, tendsto f (comap e (nhds b)) (nhds c)) : continuous (de.extend f) := continuous_iff_tendsto.mpr $ assume b, de.tendsto_extend $ univ_mem_sets' hf end dense_embedding namespace dense_embedding variables [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] /-- The product of two dense embeddings is a dense embedding -/ protected def prod {e₁ : α → β} {e₂ : γ → δ} (de₁ : dense_embedding e₁) (de₂ : dense_embedding e₂) : dense_embedding (λ(p : α × γ), (e₁ p.1, e₂ p.2)) := { dense_embedding . dense := have closure (range (λ(p : α × γ), (e₁ p.1, e₂ p.2))) = set.prod (closure (range e₁)) (closure (range e₂)), by rw [←closure_prod_eq, prod_range_range_eq], assume ⟨b, d⟩, begin rw [this], simp, constructor, apply de₁.dense, apply de₂.dense end, inj := assume ⟨x₁, x₂⟩ ⟨y₁, y₂⟩, by simp; exact assume h₁ h₂, ⟨de₁.inj h₁, de₂.inj h₂⟩, induced := assume ⟨a, b⟩, by rw [nhds_prod_eq, nhds_prod_eq, ←prod_comap_comap_eq, de₁.induced, de₂.induced] } def subtype_emb (p : α → Prop) {e : α → β} (de : dense_embedding e) (x : {x // p x}) : {x // x ∈ closure (e '' {x | p x})} := ⟨e x.1, subset_closure $ mem_image_of_mem e x.2⟩ protected def subtype (p : α → Prop) {e : α → β} (de : dense_embedding e) : dense_embedding (de.subtype_emb p) := { dense_embedding . dense := assume ⟨x, hx⟩, closure_subtype.mpr $ have (λ (x : {x // p x}), e (x.val)) = e ∘ subtype.val, from rfl, begin rw ← image_univ, simp [(image_comp _ _ _).symm, (∘), subtype_emb, -image_univ], rw [this, image_comp, subtype_val_image], simp, assumption end, inj := assume ⟨x, hx⟩ ⟨y, hy⟩ h, subtype.eq $ de.inj $ @@congr_arg subtype.val h, induced := assume ⟨x, hx⟩, by simp [subtype_emb, nhds_subtype_eq_comap, comap_comap_comp, (∘), (de.induced x).symm] } end dense_embedding lemma is_closed_property [topological_space α] [topological_space β] {e : α → β} {p : β → Prop} (he : closure (range e) = univ) (hp : is_closed {x | p x}) (h : ∀a, p (e a)) : ∀b, p b := have univ ⊆ {b | p b}, from calc univ = closure (range e) : he.symm ... ⊆ closure {b | p b} : closure_mono $ range_subset_iff.mpr h ... = _ : closure_eq_of_is_closed hp, assume b, this trivial lemma is_closed_property2 [topological_space α] [topological_space β] {e : α → β} {p : β → β → Prop} (he : dense_embedding e) (hp : is_closed {q:β×β | p q.1 q.2}) (h : ∀a₁ a₂, p (e a₁) (e a₂)) : ∀b₁ b₂, p b₁ b₂ := have ∀q:β×β, p q.1 q.2, from is_closed_property (he.prod he).closure_range hp $ assume a, h _ _, assume b₁ b₂, this ⟨b₁, b₂⟩ lemma is_closed_property3 [topological_space α] [topological_space β] {e : α → β} {p : β → β → β → Prop} (he : dense_embedding e) (hp : is_closed {q:β×β×β | p q.1 q.2.1 q.2.2}) (h : ∀a₁ a₂ a₃, p (e a₁) (e a₂) (e a₃)) : ∀b₁ b₂ b₃, p b₁ b₂ b₃ := have ∀q:β×β×β, p q.1 q.2.1 q.2.2, from is_closed_property (he.prod $ he.prod he).closure_range hp $ assume ⟨a₁, a₂, a₃⟩, h _ _ _, assume b₁ b₂ b₃, this ⟨b₁, b₂, b₃⟩ lemma mem_closure_of_continuous [topological_space α] [topological_space β] {f : α → β} {a : α} {s : set α} {t : set β} (hf : continuous f) (ha : a ∈ closure s) (h : ∀a∈s, f a ∈ closure t) : f a ∈ closure t := calc f a ∈ f '' closure s : mem_image_of_mem _ ha ... ⊆ closure (f '' s) : image_closure_subset_closure_image hf ... ⊆ closure (closure t) : closure_mono $ image_subset_iff.mpr $ h ... ⊆ closure t : begin rw [closure_eq_of_is_closed], exact subset.refl _, exact is_closed_closure end lemma mem_closure_of_continuous2 [topological_space α] [topological_space β] [topological_space γ] {f : α → β → γ} {a : α} {b : β} {s : set α} {t : set β} {u : set γ} (hf : continuous (λp:α×β, f p.1 p.2)) (ha : a ∈ closure s) (hb : b ∈ closure t) (h : ∀a∈s, ∀b∈t, f a b ∈ closure u) : f a b ∈ closure u := have (a,b) ∈ closure (set.prod s t), by simp [closure_prod_eq, ha, hb], show f (a, b).1 (a, b).2 ∈ closure u, from @mem_closure_of_continuous (α×β) _ _ _ (λp:α×β, f p.1 p.2) (a,b) _ u hf this $ assume ⟨p₁, p₂⟩ ⟨h₁, h₂⟩, h p₁ h₁ p₂ h₂ /-- α and β are homeomorph, also called topological isomoph -/ structure homeomorph (α : Type*) (β : Type*) [topological_space α] [topological_space β] extends α ≃ β := (continuous_to_fun : continuous to_fun) (continuous_inv_fun : continuous inv_fun) infix ` ≃ₜ `:50 := homeomorph namespace homeomorph variables [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] instance : has_coe_to_fun (α ≃ₜ β) := ⟨λ_, α → β, λe, e.to_equiv⟩ lemma coe_eq_to_equiv (h : α ≃ₜ β) (a : α) : h a = h.to_equiv a := rfl protected def refl (α : Type*) [topological_space α] : α ≃ₜ α := { continuous_to_fun := continuous_id, continuous_inv_fun := continuous_id, .. equiv.refl α } protected def trans (h₁ : α ≃ₜ β) (h₂ : β ≃ₜ γ) : α ≃ₜ γ := { continuous_to_fun := h₁.continuous_to_fun.comp h₂.continuous_to_fun, continuous_inv_fun := h₂.continuous_inv_fun.comp h₁.continuous_inv_fun, .. equiv.trans h₁.to_equiv h₂.to_equiv } protected def symm (h : α ≃ₜ β) : β ≃ₜ α := { continuous_to_fun := h.continuous_inv_fun, continuous_inv_fun := h.continuous_to_fun, .. h.to_equiv.symm } protected def continuous (h : α ≃ₜ β) : continuous h := h.continuous_to_fun lemma symm_comp_self (h : α ≃ₜ β) : ⇑h.symm ∘ ⇑h = id := funext $ assume a, h.to_equiv.left_inv a lemma self_comp_symm (h : α ≃ₜ β) : ⇑h ∘ ⇑h.symm = id := funext $ assume a, h.to_equiv.right_inv a lemma range_coe (h : α ≃ₜ β) : range h = univ := eq_univ_of_forall $ assume b, ⟨h.symm b, congr_fun h.self_comp_symm b⟩ lemma image_symm (h : α ≃ₜ β) : image h.symm = preimage h := image_eq_preimage_of_inverse h.symm.to_equiv.left_inv h.symm.to_equiv.right_inv lemma preimage_symm (h : α ≃ₜ β) : preimage h.symm = image h := (image_eq_preimage_of_inverse h.to_equiv.left_inv h.to_equiv.right_inv).symm lemma induced_eq {α : Type*} {β : Type*} [tα : topological_space α] [tβ : topological_space β] (h : α ≃ₜ β) : tβ.induced h = tα := le_antisymm (induced_le_iff_le_coinduced.2 h.continuous) (calc tα = (tα.induced h.symm).induced h : by rw [induced_compose, symm_comp_self, induced_id] ... ≤ tβ.induced h : induced_mono $ (induced_le_iff_le_coinduced.2 h.symm.continuous)) lemma coinduced_eq {α : Type*} {β : Type*} [tα : topological_space α] [tβ : topological_space β] (h : α ≃ₜ β) : tα.coinduced h = tβ := le_antisymm (calc tα.coinduced h ≤ (tβ.coinduced h.symm).coinduced h : coinduced_mono h.symm.continuous ... = tβ : by rw [coinduced_compose, self_comp_symm, coinduced_id]) h.continuous protected lemma embedding (h : α ≃ₜ β) : embedding h := ⟨h.to_equiv.bijective.1, h.induced_eq.symm⟩ protected lemma dense_embedding (h : α ≃ₜ β) : dense_embedding h := { dense := assume a, by rw [h.range_coe, closure_univ]; trivial, inj := h.to_equiv.bijective.1, induced := assume a, by rw [← nhds_induced_eq_comap, h.induced_eq] } protected lemma is_open_map (h : α ≃ₜ β) : is_open_map h := begin assume s, rw ← h.preimage_symm, exact h.symm.continuous s end protected lemma quotient_map (h : α ≃ₜ β) : quotient_map h := ⟨h.to_equiv.bijective.2, h.coinduced_eq.symm⟩ def prod_congr (h₁ : α ≃ₜ β) (h₂ : γ ≃ₜ δ) : (α × γ) ≃ₜ (β × δ) := { continuous_to_fun := continuous.prod_mk (continuous_fst.comp h₁.continuous) (continuous_snd.comp h₂.continuous), continuous_inv_fun := continuous.prod_mk (continuous_fst.comp h₁.symm.continuous) (continuous_snd.comp h₂.symm.continuous), .. h₁.to_equiv.prod_congr h₂.to_equiv } section variables (α β γ) def prod_comm : (α × β) ≃ₜ (β × α) := { continuous_to_fun := continuous.prod_mk continuous_snd continuous_fst, continuous_inv_fun := continuous.prod_mk continuous_snd continuous_fst, .. equiv.prod_comm α β } def prod_assoc : ((α × β) × γ) ≃ₜ (α × (β × γ)) := { continuous_to_fun := continuous.prod_mk (continuous_fst.comp continuous_fst) (continuous.prod_mk (continuous_fst.comp continuous_snd) continuous_snd), continuous_inv_fun := continuous.prod_mk (continuous.prod_mk continuous_fst (continuous_snd.comp continuous_fst)) (continuous_snd.comp continuous_snd), .. equiv.prod_assoc α β γ } end end homeomorph
7fb7d9b08cff3bd6493a201ea45c6172e4fb9f88
b7f22e51856f4989b970961f794f1c435f9b8f78
/hott/types/unit.hlean
d2e43cbdcff61c5db0cb9488a295242fbfd821e1
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
1,404
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn Theorems about the unit type -/ open is_equiv equiv option eq pointed is_trunc function namespace unit protected definition eta : Π(u : unit), ⋆ = u | eta ⋆ := idp definition unit_equiv_option_empty [constructor] : unit ≃ option empty := begin fapply equiv.MK, { intro u, exact none}, { intro e, exact star}, { intro e, cases e, reflexivity, contradiction}, { intro u, cases u, reflexivity}, end -- equivalences involving unit and other type constructors are in the file -- of the other constructor /- pointed and truncated unit -/ definition punit [constructor] : Set* := pSet.mk' unit notation `unit*` := punit definition unit_arrow_eq {X : Type} (f : unit → X) : (λx, f ⋆) = f := by apply eq_of_homotopy; intro u; induction u; reflexivity open funext definition unit_arrow_eq_compose {X Y : Type} (g : X → Y) (f : unit → X) : unit_arrow_eq (g ∘ f) = ap (λf, g ∘ f) (unit_arrow_eq f) := begin apply eq_of_fn_eq_fn' apd10, refine right_inv apd10 _ ⬝ _, refine _ ⬝ ap apd10 (!compose_eq_of_homotopy)⁻¹, refine _ ⬝ (right_inv apd10 _)⁻¹, apply eq_of_homotopy, intro u, induction u, reflexivity end end unit open unit is_trunc
ff0d5c357b86288070d0f7560a6b21f4c9b898ea
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/doc/examples/ICERM2022/notation.lean
a8b9562d8b0c7de9f2fddba7d1d246cdf9e25552
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
1,001
lean
import Lean def f (x y : Nat) := x * y + 1 infixl:65 " *' " => f #check 2 *' 3 notation "unitTest " x => Prod.mk x () #check unitTest 42 notation "parenthesisTest " x => Nat.sub (x) #check parenthesisTest 12 def Set (α : Type u) := α → Prop def setOf {α : Type} (p : α → Prop) : Set α := p notation "{ " x " | " p " }" => setOf (fun x => p) #check { x | x ≤ 1 } notation "cdotTest " "(" x ", " y ")" => Prod.map (· + 1) (1 + ·) (x, y) #check cdotTest (13, 12) notation "tupleFunctionTest " "(" x ", " y ")"=> Prod.map (Nat.add 1) (Nat.add 2) (x, y) #check tupleFunctionTest (15, 12) notation "diag " x => Prod.mk x x #check diag 12 open Lean Meta PrettyPrinter Delaborator SubExpr in @[delab app.Prod.mk] def delabDoubleRhsTest : Delab := do let e ← getExpr let #[_, _, x, y] := e.getAppArgs | failure guard (← isDefEq x y) let stx ← withAppArg delab `(diag $stx) #check diag 3 #check (3, 3) #check (3, 4) #check (2+1, 3) #check (true, true)
ad88ff2b93dd6e87d392569047131c220f65d3a5
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/geometry/manifold/instances/sphere.lean
1bc71762cb9551fbf526f9a6be623d0e42287968
[ "Apache-2.0" ]
permissive
hikari0108/mathlib
b7ea2b7350497ab1a0b87a09d093ecc025a50dfa
a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901
refs/heads/master
1,690,483,608,260
1,631,541,580,000
1,631,541,580,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
20,131
lean
/- Copyright (c) 2021 Heather Macbeth. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Heather Macbeth -/ import geometry.manifold.instances.real import analysis.complex.circle /-! # Manifold structure on the sphere This file defines stereographic projection from the sphere in an inner product space `E`, and uses it to put a smooth manifold structure on the sphere. ## Main results For a unit vector `v` in `E`, the definition `stereographic` gives the stereographic projection centred at `v`, a local homeomorphism from the sphere to `(ℝ ∙ v)ᗮ` (the orthogonal complement of `v`). For finite-dimensional `E`, we then construct a smooth manifold instance on the sphere; the charts here are obtained by composing the local homeomorphisms `stereographic` with arbitrary isometries from `(ℝ ∙ v)ᗮ` to Euclidean space. We prove two lemmas about smooth maps: * `times_cont_mdiff_coe_sphere` states that the coercion map from the sphere into `E` is smooth; this is a useful tool for constructing smooth maps *from* the sphere. * `times_cont_mdiff.cod_restrict_sphere` states that a map from a manifold into the sphere is smooth if its lift to a map to `E` is smooth; this is a useful tool for constructing smooth maps *to* the sphere. As an application we prove `times_cont_mdiff_neg_sphere`, that the antipodal map is smooth. Finally, we equip the `circle` (defined in `analysis.complex.circle` to be the sphere in `ℂ` centred at `0` of radius `1`) with the following structure: * a charted space with model space `euclidean_space ℝ (fin 1)` (inherited from `metric.sphere`) * a Lie group with model with corners `𝓡 1` We furthermore show that `exp_map_circle` (defined in `analysis.complex.circle` to be the natural map `λ t, exp (t * I)` from `ℝ` to `circle`) is smooth. ## Implementation notes The model space for the charted space instance is `euclidean_space ℝ (fin n)`, where `n` is a natural number satisfying the typeclass assumption `[fact (finrank ℝ E = n + 1)]`. This may seem a little awkward, but it is designed to circumvent the problem that the literal expression for the dimension of the model space (up to definitional equality) determines the type. If one used the naive expression `euclidean_space ℝ (fin (finrank ℝ E - 1))` for the model space, then the sphere in `ℂ` would be a manifold with model space `euclidean_space ℝ (fin (2 - 1))` but not with model space `euclidean_space ℝ (fin 1)`. -/ variables {E : Type*} [inner_product_space ℝ E] noncomputable theory open metric finite_dimensional open_locale manifold local attribute [instance] fact_finite_dimensional_of_finrank_eq_succ section stereographic_projection variables (v : E) /-! ### Construction of the stereographic projection -/ /-- Stereographic projection, forward direction. This is a map from an inner product space `E` to the orthogonal complement of an element `v` of `E`. It is smooth away from the affine hyperplane through `v` parallel to the orthogonal complement. It restricts on the sphere to the stereographic projection. -/ def stereo_to_fun [complete_space E] (x : E) : (ℝ ∙ v)ᗮ := (2 / ((1:ℝ) - inner_right v x)) • orthogonal_projection (ℝ ∙ v)ᗮ x variables {v} @[simp] lemma stereo_to_fun_apply [complete_space E] (x : E) : stereo_to_fun v x = (2 / ((1:ℝ) - inner_right v x)) • orthogonal_projection (ℝ ∙ v)ᗮ x := rfl lemma times_cont_diff_on_stereo_to_fun [complete_space E] : times_cont_diff_on ℝ ⊤ (stereo_to_fun v) {x : E | inner_right v x ≠ (1:ℝ)} := begin refine times_cont_diff_on.smul _ (orthogonal_projection ((ℝ ∙ v)ᗮ)).times_cont_diff.times_cont_diff_on, refine times_cont_diff_const.times_cont_diff_on.div _ _, { exact (times_cont_diff_const.sub (inner_right v).times_cont_diff).times_cont_diff_on }, { intros x h h', exact h (sub_eq_zero.mp h').symm } end lemma continuous_on_stereo_to_fun [complete_space E] : continuous_on (stereo_to_fun v) {x : E | inner_right v x ≠ (1:ℝ)} := times_cont_diff_on_stereo_to_fun.continuous_on variables (v) /-- Auxiliary function for the construction of the reverse direction of the stereographic projection. This is a map from the orthogonal complement of a unit vector `v` in an inner product space `E` to `E`; we will later prove that it takes values in the unit sphere. For most purposes, use `stereo_inv_fun`, not `stereo_inv_fun_aux`. -/ def stereo_inv_fun_aux (w : E) : E := (∥w∥ ^ 2 + 4)⁻¹ • ((4:ℝ) • w + (∥w∥ ^ 2 - 4) • v) variables {v} @[simp] lemma stereo_inv_fun_aux_apply (w : E) : stereo_inv_fun_aux v w = (∥w∥ ^ 2 + 4)⁻¹ • ((4:ℝ) • w + (∥w∥ ^ 2 - 4) • v) := rfl lemma stereo_inv_fun_aux_mem (hv : ∥v∥ = 1) {w : E} (hw : w ∈ (ℝ ∙ v)ᗮ) : stereo_inv_fun_aux v w ∈ (sphere (0:E) 1) := begin have h₁ : 0 ≤ ∥w∥ ^ 2 + 4 := by nlinarith, suffices : ∥(4:ℝ) • w + (∥w∥ ^ 2 - 4) • v∥ = ∥w∥ ^ 2 + 4, { have h₂ : ∥w∥ ^ 2 + 4 ≠ 0 := by nlinarith, simp only [mem_sphere_zero_iff_norm, norm_smul, real.norm_eq_abs, abs_inv, this, abs_of_nonneg h₁, stereo_inv_fun_aux_apply], field_simp }, suffices : ∥(4:ℝ) • w + (∥w∥ ^ 2 - 4) • v∥ ^ 2 = (∥w∥ ^ 2 + 4) ^ 2, { have h₃ : 0 ≤ ∥stereo_inv_fun_aux v w∥ := norm_nonneg _, simpa [h₁, h₃, -one_pow] using this }, simp [norm_add_sq_real, norm_smul, inner_smul_left, inner_smul_right, inner_left_of_mem_orthogonal_singleton _ hw, mul_pow, real.norm_eq_abs, hv], ring end lemma times_cont_diff_stereo_inv_fun_aux : times_cont_diff ℝ ⊤ (stereo_inv_fun_aux v) := begin have h₀ : times_cont_diff ℝ ⊤ (λ w : E, ∥w∥ ^ 2) := times_cont_diff_norm_sq, have h₁ : times_cont_diff ℝ ⊤ (λ w : E, (∥w∥ ^ 2 + 4)⁻¹), { refine (h₀.add times_cont_diff_const).inv _, intros x, nlinarith }, have h₂ : times_cont_diff ℝ ⊤ (λ w, (4:ℝ) • w + (∥w∥ ^ 2 - 4) • v), { refine (times_cont_diff_const.smul times_cont_diff_id).add _, refine (h₀.sub times_cont_diff_const).smul times_cont_diff_const }, exact h₁.smul h₂ end /-- Stereographic projection, reverse direction. This is a map from the orthogonal complement of a unit vector `v` in an inner product space `E` to the unit sphere in `E`. -/ def stereo_inv_fun (hv : ∥v∥ = 1) (w : (ℝ ∙ v)ᗮ) : sphere (0:E) 1 := ⟨stereo_inv_fun_aux v (w:E), stereo_inv_fun_aux_mem hv w.2⟩ @[simp] lemma stereo_inv_fun_apply (hv : ∥v∥ = 1) (w : (ℝ ∙ v)ᗮ) : (stereo_inv_fun hv w : E) = (∥w∥ ^ 2 + 4)⁻¹ • ((4:ℝ) • w + (∥w∥ ^ 2 - 4) • v) := rfl lemma stereo_inv_fun_ne_north_pole (hv : ∥v∥ = 1) (w : (ℝ ∙ v)ᗮ) : stereo_inv_fun hv w ≠ (⟨v, by simp [hv]⟩ : sphere (0:E) 1) := begin refine subtype.ne_of_val_ne _, rw ← inner_lt_one_iff_real_of_norm_one _ hv, { have hw : ⟪v, w⟫_ℝ = 0 := inner_right_of_mem_orthogonal_singleton v w.2, have hw' : (∥(w:E)∥ ^ 2 + 4)⁻¹ * (∥(w:E)∥ ^ 2 - 4) < 1, { refine (inv_mul_lt_iff' _).mpr _, { nlinarith }, linarith }, simpa [real_inner_comm, inner_add_right, inner_smul_right, real_inner_self_eq_norm_sq, hw, hv] using hw' }, { simpa using stereo_inv_fun_aux_mem hv w.2 } end lemma continuous_stereo_inv_fun (hv : ∥v∥ = 1) : continuous (stereo_inv_fun hv) := continuous_induced_rng (times_cont_diff_stereo_inv_fun_aux.continuous.comp continuous_subtype_coe) variables [complete_space E] lemma stereo_left_inv (hv : ∥v∥ = 1) {x : sphere (0:E) 1} (hx : (x:E) ≠ v) : stereo_inv_fun hv (stereo_to_fun v x) = x := begin ext, simp only [stereo_to_fun_apply, stereo_inv_fun_apply, smul_add], -- name two frequently-occuring quantities and write down their basic properties set a : ℝ := inner_right v x, set y := orthogonal_projection (ℝ ∙ v)ᗮ x, have split : ↑x = a • v + ↑y, { convert eq_sum_orthogonal_projection_self_orthogonal_complement (ℝ ∙ v) x, exact (orthogonal_projection_unit_singleton ℝ hv x).symm }, have hvy : ⟪v, y⟫_ℝ = 0 := inner_right_of_mem_orthogonal_singleton v y.2, have pythag : 1 = a ^ 2 + ∥y∥ ^ 2, { have hvy' : ⟪a • v, y⟫_ℝ = 0 := by simp [inner_smul_left, hvy], convert norm_add_sq_eq_norm_sq_add_norm_sq_of_inner_eq_zero _ _ hvy' using 2, { simp [← split] }, { simp [norm_smul, hv, real.norm_eq_abs, ← sq, sq_abs] }, { exact sq _ } }, -- two facts which will be helpful for clearing denominators in the main calculation have ha : 1 - a ≠ 0, { have : a < 1 := (inner_lt_one_iff_real_of_norm_one hv (by simp)).mpr hx.symm, linarith }, have : 2 ^ 2 * ∥y∥ ^ 2 + 4 * (1 - a) ^ 2 ≠ 0, { refine ne_of_gt _, have := norm_nonneg (y:E), have : 0 < (1 - a) ^ 2 := sq_pos_of_ne_zero (1 - a) ha, nlinarith }, -- the core of the problem is these two algebraic identities: have h₁ : (2 ^ 2 / (1 - a) ^ 2 * ∥y∥ ^ 2 + 4)⁻¹ * 4 * (2 / (1 - a)) = 1, { field_simp, nlinarith }, have h₂ : (2 ^ 2 / (1 - a) ^ 2 * ∥y∥ ^ 2 + 4)⁻¹ * (2 ^ 2 / (1 - a) ^ 2 * ∥y∥ ^ 2 - 4) = a, { field_simp, transitivity (1 - a) ^ 2 * (a * (2 ^ 2 * ∥y∥ ^ 2 + 4 * (1 - a) ^ 2)), { congr, nlinarith }, ring_nf, ring }, -- deduce the result convert congr_arg2 has_add.add (congr_arg (λ t, t • (y:E)) h₁) (congr_arg (λ t, t • v) h₂) using 1, { simp [inner_add_right, inner_smul_right, hvy, real_inner_self_eq_norm_sq, hv, mul_smul, mul_pow, real.norm_eq_abs, sq_abs, norm_smul] }, { simp [split, add_comm] } end lemma stereo_right_inv (hv : ∥v∥ = 1) (w : (ℝ ∙ v)ᗮ) : stereo_to_fun v (stereo_inv_fun hv w) = w := begin have : 2 / (1 - (∥(w:E)∥ ^ 2 + 4)⁻¹ * (∥(w:E)∥ ^ 2 - 4)) * (∥(w:E)∥ ^ 2 + 4)⁻¹ * 4 = 1, { have : ∥(w:E)∥ ^ 2 + 4 ≠ 0 := by nlinarith, have : (4:ℝ) + 4 ≠ 0 := by nlinarith, field_simp, ring }, convert congr_arg (λ c, c • w) this, { have h₁ : orthogonal_projection (ℝ ∙ v)ᗮ v = 0 := orthogonal_projection_orthogonal_complement_singleton_eq_zero v, have h₂ : orthogonal_projection (ℝ ∙ v)ᗮ w = w := orthogonal_projection_mem_subspace_eq_self w, have h₃ : inner_right v w = (0:ℝ) := inner_right_of_mem_orthogonal_singleton v w.2, have h₄ : inner_right v v = (1:ℝ) := by simp [real_inner_self_eq_norm_sq, hv], simp [h₁, h₂, h₃, h₄, continuous_linear_map.map_add, continuous_linear_map.map_smul, mul_smul] }, { simp } end /-- Stereographic projection from the unit sphere in `E`, centred at a unit vector `v` in `E`; this is the version as a local homeomorphism. -/ def stereographic (hv : ∥v∥ = 1) : local_homeomorph (sphere (0:E) 1) (ℝ ∙ v)ᗮ := { to_fun := (stereo_to_fun v) ∘ coe, inv_fun := stereo_inv_fun hv, source := {⟨v, by simp [hv]⟩}ᶜ, target := set.univ, map_source' := by simp, map_target' := λ w _, stereo_inv_fun_ne_north_pole hv w, left_inv' := λ _ hx, stereo_left_inv hv (λ h, hx (subtype.ext h)), right_inv' := λ w _, stereo_right_inv hv w, open_source := is_open_compl_singleton, open_target := is_open_univ, continuous_to_fun := continuous_on_stereo_to_fun.comp continuous_subtype_coe.continuous_on (λ w h, h ∘ subtype.ext ∘ eq.symm ∘ (inner_eq_norm_mul_iff_of_norm_one hv (by simp)).mp), continuous_inv_fun := (continuous_stereo_inv_fun hv).continuous_on } @[simp] lemma stereographic_source (hv : ∥v∥ = 1) : (stereographic hv).source = {⟨v, by simp [hv]⟩}ᶜ := rfl @[simp] lemma stereographic_target (hv : ∥v∥ = 1) : (stereographic hv).target = set.univ := rfl end stereographic_projection section charted_space /-! ### Charted space structure on the sphere In this section we construct a charted space structure on the unit sphere in a finite-dimensional real inner product space `E`; that is, we show that it is locally homeomorphic to the Euclidean space of dimension one less than `E`. The restriction to finite dimension is for convenience. The most natural `charted_space` structure for the sphere uses the stereographic projection from the antipodes of a point as the canonical chart at this point. However, the codomain of the stereographic projection constructed in the previous section is `(ℝ ∙ v)ᗮ`, the orthogonal complement of the vector `v` in `E` which is the "north pole" of the projection, so a priori these charts all have different codomains. So it is necessary to prove that these codomains are all continuously linearly equivalent to a fixed normed space. This could be proved in general by a simple case of Gram-Schmidt orthogonalization, but in the finite-dimensional case it follows more easily by dimension-counting. -/ /-- Variant of the stereographic projection, for the sphere in an `n + 1`-dimensional inner product space `E`. This version has codomain the Euclidean space of dimension `n`, and is obtained by composing the original sterographic projection (`stereographic`) with an arbitrary linear isometry from `(ℝ ∙ v)ᗮ` to the Euclidean space. -/ def stereographic' (n : ℕ) [fact (finrank ℝ E = n + 1)] (v : sphere (0:E) 1) : local_homeomorph (sphere (0:E) 1) (euclidean_space ℝ (fin n)) := (stereographic (norm_eq_of_mem_sphere v)) ≫ₕ (linear_isometry_equiv.from_orthogonal_span_singleton n (nonzero_of_mem_unit_sphere v)).to_homeomorph.to_local_homeomorph @[simp] lemma stereographic'_source {n : ℕ} [fact (finrank ℝ E = n + 1)] (v : sphere (0:E) 1) : (stereographic' n v).source = {v}ᶜ := by simp [stereographic'] @[simp] lemma stereographic'_target {n : ℕ} [fact (finrank ℝ E = n + 1)] (v : sphere (0:E) 1) : (stereographic' n v).target = set.univ := by simp [stereographic'] /-- The unit sphere in an `n + 1`-dimensional inner product space `E` is a charted space modelled on the Euclidean space of dimension `n`. -/ instance {n : ℕ} [fact (finrank ℝ E = n + 1)] : charted_space (euclidean_space ℝ (fin n)) (sphere (0:E) 1) := { atlas := {f | ∃ v : (sphere (0:E) 1), f = stereographic' n v}, chart_at := λ v, stereographic' n (-v), mem_chart_source := λ v, by simpa using ne_neg_of_mem_unit_sphere ℝ v, chart_mem_atlas := λ v, ⟨-v, rfl⟩ } end charted_space section smooth_manifold /-! ### Smooth manifold structure on the sphere -/ /-- The unit sphere in an `n + 1`-dimensional inner product space `E` is a smooth manifold, modelled on the Euclidean space of dimension `n`. -/ instance {n : ℕ} [fact (finrank ℝ E = n + 1)] : smooth_manifold_with_corners (𝓡 n) (sphere (0:E) 1) := smooth_manifold_with_corners_of_times_cont_diff_on (𝓡 n) (sphere (0:E) 1) begin rintros _ _ ⟨v, rfl⟩ ⟨v', rfl⟩, let U : (ℝ ∙ (v:E))ᗮ ≃ₗᵢ[ℝ] euclidean_space ℝ (fin n) := linear_isometry_equiv.from_orthogonal_span_singleton n (nonzero_of_mem_unit_sphere v), let U' : (ℝ ∙ (v':E))ᗮ ≃ₗᵢ[ℝ] euclidean_space ℝ (fin n) := linear_isometry_equiv.from_orthogonal_span_singleton n (nonzero_of_mem_unit_sphere v'), have hUv : stereographic' n v = (stereographic (norm_eq_of_mem_sphere v)) ≫ₕ U.to_homeomorph.to_local_homeomorph := rfl, have hU'v' : stereographic' n v' = (stereographic (norm_eq_of_mem_sphere v')).trans U'.to_homeomorph.to_local_homeomorph := rfl, have H₁ := U'.times_cont_diff.comp_times_cont_diff_on times_cont_diff_on_stereo_to_fun, have H₂ := (times_cont_diff_stereo_inv_fun_aux.comp (ℝ ∙ (v:E))ᗮ.subtypeL.times_cont_diff).comp U.symm.times_cont_diff, convert H₁.comp' (H₂.times_cont_diff_on : times_cont_diff_on ℝ ⊤ _ set.univ) using 1, have h_set : ∀ p : sphere (0:E) 1, p = v' ↔ ⟪(p:E), v'⟫_ℝ = 1, { simp [subtype.ext_iff, inner_eq_norm_mul_iff_of_norm_one] }, ext, simp [h_set, hUv, hU'v', stereographic, real_inner_comm] end /-- The inclusion map (i.e., `coe`) from the sphere in `E` to `E` is smooth. -/ lemma times_cont_mdiff_coe_sphere {n : ℕ} [fact (finrank ℝ E = n + 1)] : times_cont_mdiff (𝓡 n) 𝓘(ℝ, E) ∞ (coe : (sphere (0:E) 1) → E) := begin rw times_cont_mdiff_iff, split, { exact continuous_subtype_coe }, { intros v _, let U : (ℝ ∙ ((-v):E))ᗮ ≃ₗᵢ[ℝ] euclidean_space ℝ (fin n) := linear_isometry_equiv.from_orthogonal_span_singleton n (nonzero_of_mem_unit_sphere (-v)), exact ((times_cont_diff_stereo_inv_fun_aux.comp (ℝ ∙ ((-v):E))ᗮ.subtypeL.times_cont_diff).comp U.symm.times_cont_diff).times_cont_diff_on } end variables {F : Type*} [normed_group F] [normed_space ℝ F] variables {H : Type*} [topological_space H] {I : model_with_corners ℝ F H} variables {M : Type*} [topological_space M] [charted_space H M] [smooth_manifold_with_corners I M] /-- If a `times_cont_mdiff` function `f : M → E`, where `M` is some manifold, takes values in the sphere, then it restricts to a `times_cont_mdiff` function from `M` to the sphere. -/ lemma times_cont_mdiff.cod_restrict_sphere {n : ℕ} [fact (finrank ℝ E = n + 1)] {m : with_top ℕ} {f : M → E} (hf : times_cont_mdiff I 𝓘(ℝ, E) m f) (hf' : ∀ x, f x ∈ sphere (0:E) 1) : times_cont_mdiff I (𝓡 n) m (set.cod_restrict _ _ hf' : M → (sphere (0:E) 1)) := begin rw times_cont_mdiff_iff_target, refine ⟨continuous_induced_rng hf.continuous, _⟩, intros v, let U : (ℝ ∙ ((-v):E))ᗮ ≃ₗᵢ[ℝ] euclidean_space ℝ (fin n) := (linear_isometry_equiv.from_orthogonal_span_singleton n (nonzero_of_mem_unit_sphere (-v))), have h : times_cont_diff_on ℝ ⊤ U set.univ := U.times_cont_diff.times_cont_diff_on, have H₁ := (h.comp' times_cont_diff_on_stereo_to_fun).times_cont_mdiff_on, have H₂ : times_cont_mdiff_on _ _ _ _ set.univ := hf.times_cont_mdiff_on, convert (H₁.of_le le_top).comp' H₂ using 1, ext x, have hfxv : f x = -↑v ↔ ⟪f x, -↑v⟫_ℝ = 1, { have hfx : ∥f x∥ = 1 := by simpa using hf' x, rw inner_eq_norm_mul_iff_of_norm_one hfx, exact norm_eq_of_mem_sphere (-v) }, dsimp [chart_at], simp [not_iff_not, subtype.ext_iff, hfxv, real_inner_comm] end /-- The antipodal map is smooth. -/ lemma times_cont_mdiff_neg_sphere {n : ℕ} [fact (finrank ℝ E = n + 1)] : times_cont_mdiff (𝓡 n) (𝓡 n) ∞ (λ x : sphere (0:E) 1, -x) := (times_cont_diff_neg.times_cont_mdiff.comp times_cont_mdiff_coe_sphere).cod_restrict_sphere _ end smooth_manifold section circle open complex local attribute [instance] finrank_real_complex_fact /-- The unit circle in `ℂ` is a charted space modelled on `euclidean_space ℝ (fin 1)`. This follows by definition from the corresponding result for `metric.sphere`. -/ instance : charted_space (euclidean_space ℝ (fin 1)) circle := metric.sphere.charted_space instance : smooth_manifold_with_corners (𝓡 1) circle := metric.sphere.smooth_manifold_with_corners /-- The unit circle in `ℂ` is a Lie group. -/ instance : lie_group (𝓡 1) circle := { smooth_mul := begin let c : circle → ℂ := coe, have h₁ : times_cont_mdiff _ _ _ (prod.map c c) := times_cont_mdiff_coe_sphere.prod_map times_cont_mdiff_coe_sphere, have h₂ : times_cont_mdiff (𝓘(ℝ, ℂ).prod 𝓘(ℝ, ℂ)) 𝓘(ℝ, ℂ) ∞ (λ (z : ℂ × ℂ), z.fst * z.snd), { rw times_cont_mdiff_iff, exact ⟨continuous_mul, λ x y, (times_cont_diff_mul.restrict_scalars ℝ).times_cont_diff_on⟩ }, exact (h₂.comp h₁).cod_restrict_sphere _, end, smooth_inv := (complex.conj_cle.times_cont_diff.times_cont_mdiff.comp times_cont_mdiff_coe_sphere).cod_restrict_sphere _, .. metric.sphere.smooth_manifold_with_corners } /-- The map `λ t, exp (t * I)` from `ℝ` to the unit circle in `ℂ` is smooth. -/ lemma times_cont_mdiff_exp_map_circle : times_cont_mdiff 𝓘(ℝ, ℝ) (𝓡 1) ∞ exp_map_circle := (((times_cont_diff_exp.restrict_scalars ℝ).comp (times_cont_diff_id.smul times_cont_diff_const)).times_cont_mdiff).cod_restrict_sphere _ end circle
364a4f066e01e445e0313ecc06de7f95741fc7a6
ae1e94c332e17c7dc7051ce976d5a9eebe7ab8a5
/src/Lean/Elab/Do.lean
a205d578074ea287fdcc4aa4ec86f70a058dae47
[ "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
66,634
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Elab.Term import Lean.Elab.Binders import Lean.Elab.Match import Lean.Elab.Quotation.Util import Lean.Parser.Do namespace Lean.Elab.Term open Lean.Parser.Term open Meta private def getDoSeqElems (doSeq : Syntax) : List Syntax := if doSeq.getKind == `Lean.Parser.Term.doSeqBracketed then doSeq[1].getArgs.toList.map fun arg => arg[0] else if doSeq.getKind == `Lean.Parser.Term.doSeqIndent then doSeq[0].getArgs.toList.map fun arg => arg[0] else [] private def getDoSeq (doStx : Syntax) : Syntax := doStx[1] @[builtinTermElab liftMethod] def elabLiftMethod : TermElab := fun stx _ => throwErrorAt stx "invalid use of `(<- ...)`, must be nested inside a 'do' expression" /-- Return true if we should not lift `(<- ...)` actions nested in the syntax nodes with the given kind. -/ private def liftMethodDelimiter (k : SyntaxNodeKind) : Bool := k == `Lean.Parser.Term.do || k == `Lean.Parser.Term.doSeqIndent || k == `Lean.Parser.Term.doSeqBracketed || k == `Lean.Parser.Term.termReturn || k == `Lean.Parser.Term.termUnless || k == `Lean.Parser.Term.termTry || k == `Lean.Parser.Term.termFor private partial def hasLiftMethod : Syntax → Bool | Syntax.node k args => if liftMethodDelimiter k then false -- NOTE: We don't check for lifts in quotations here, which doesn't break anything but merely makes this rare case a -- bit slower else if k == `Lean.Parser.Term.liftMethod then true else args.any hasLiftMethod | _ => false structure ExtractMonadResult where m : Expr α : Expr hasBindInst : Expr expectedType : Expr private def mkIdBindFor (type : Expr) : TermElabM ExtractMonadResult := do let u ← getDecLevel type let id := Lean.mkConst `Id [u] let idBindVal := Lean.mkConst `Id.hasBind [u] pure { m := id, hasBindInst := idBindVal, α := type, expectedType := mkApp id type } private def extractBind (expectedType? : Option Expr) : TermElabM ExtractMonadResult := do match expectedType? with | none => throwError "invalid 'do' notation, expected type is not available" | some expectedType => let type ← withReducible $ whnf expectedType if type.getAppFn.isMVar then throwError "invalid 'do' notation, expected type is not available" match type with | Expr.app m α _ => try let bindInstType ← mkAppM `Bind #[m] let bindInstVal ← synthesizeInst bindInstType pure { m := m, hasBindInst := bindInstVal, α := α, expectedType := expectedType } catch _ => mkIdBindFor type | _ => mkIdBindFor type namespace Do /- A `doMatch` alternative. `vars` is the array of variables declared by `patterns`. -/ structure Alt (σ : Type) where ref : Syntax vars : Array Name patterns : Syntax rhs : σ deriving Inhabited /- Auxiliary datastructure for representing a `do` code block, and compiling "reassignments" (e.g., `x := x + 1`). We convert `Code` into a `Syntax` term representing the: - `do`-block, or - the visitor argument for the `forIn` combinator. We say the following constructors are terminals: - `break`: for interrupting a `for x in s` - `continue`: for interrupting the current iteration of a `for x in s` - `return e`: for returning `e` as the result for the whole `do` computation block - `action a`: for executing action `a` as a terminal - `ite`: if-then-else - `match`: pattern matching - `jmp` a goto to a join-point We say the terminals `break`, `continue`, `action`, and `return` are "exit points" Note that, `return e` is not equivalent to `action (pure e)`. Here is an example: ``` def f (x : Nat) : IO Unit := do if x == 0 then return () IO.println "hello" ``` Executing `#eval f 0` will not print "hello". Now, consider ``` def g (x : Nat) : IO Unit := do if x == 0 then pure () IO.println "hello" ``` The `if` statement is essentially a noop, and "hello" is printed when we execute `g 0`. - `decl` represents all declaration-like `doElem`s (e.g., `let`, `have`, `let rec`). The field `stx` is the actual `doElem`, `vars` is the array of variables declared by it, and `cont` is the next instruction in the `do` code block. `vars` is an array since we have declarations such as `let (a, b) := s`. - `reassign` is an reassignment-like `doElem` (e.g., `x := x + 1`). - `joinpoint` is a join point declaration: an auxiliary `let`-declaration used to represent the control-flow. - `seq a k` executes action `a`, ignores its result, and then executes `k`. We also store the do-elements `dbgTrace!` and `assert!` as actions in a `seq`. A code block `C` is well-formed if - For every `jmp ref j as` in `C`, there is a `joinpoint j ps b k` and `jmp ref j as` is in `k`, and `ps.size == as.size` -/ inductive Code where | decl (xs : Array Name) (doElem : Syntax) (k : Code) | reassign (xs : Array Name) (doElem : Syntax) (k : Code) /- The Boolean value in `params` indicates whether we should use `(x : typeof! x)` when generating term Syntax or not -/ | joinpoint (name : Name) (params : Array (Name × Bool)) (body : Code) (k : Code) | seq (action : Syntax) (k : Code) | action (action : Syntax) | «break» (ref : Syntax) | «continue» (ref : Syntax) | «return» (ref : Syntax) (val : Syntax) /- Recall that an if-then-else may declare a variable using `optIdent` for the branches `thenBranch` and `elseBranch`. We store the variable name at `var?`. -/ | ite (ref : Syntax) (h? : Option Name) (optIdent : Syntax) (cond : Syntax) (thenBranch : Code) (elseBranch : Code) | «match» (ref : Syntax) (discrs : Syntax) (optType : Syntax) (alts : Array (Alt Code)) | jmp (ref : Syntax) (jpName : Name) (args : Array Syntax) deriving Inhabited /- A code block, and the collection of variables updated by it. -/ structure CodeBlock where code : Code uvars : NameSet := {} -- set of variables updated by `code` private def nameSetToArray (s : NameSet) : Array Name := s.fold (fun (xs : Array Name) x => xs.push x) #[] private def varsToMessageData (vars : Array Name) : MessageData := MessageData.joinSep (vars.toList.map fun n => MessageData.ofName (n.simpMacroScopes)) " " partial def CodeBlocl.toMessageData (codeBlock : CodeBlock) : MessageData := let us := MessageData.ofList $ (nameSetToArray codeBlock.uvars).toList.map MessageData.ofName let rec loop : Code → MessageData | Code.decl xs _ k => m!"let {varsToMessageData xs} := ...\n{loop k}" | Code.reassign xs _ k => m!"{varsToMessageData xs} := ...\n{loop k}" | Code.joinpoint n ps body k => m!"let {n.simpMacroScopes} {varsToMessageData (ps.map Prod.fst)} := {indentD (loop body)}\n{loop k}" | Code.seq e k => m!"{e}\n{loop k}" | Code.action e => e | Code.ite _ _ _ c t e => m!"if {c} then {indentD (loop t)}\nelse{loop e}" | Code.jmp _ j xs => m!"jmp {j.simpMacroScopes} {xs.toList}" | Code.«break» _ => m!"break {us}" | Code.«continue» _ => m!"continue {us}" | Code.«return» _ v => m!"return {v} {us}" | Code.«match» _ ds t alts => m!"match {ds} with" ++ alts.foldl (init := m!"") fun acc alt => acc ++ m!"\n| {alt.patterns} => {loop alt.rhs}" loop codeBlock.code /- Return true if the give code contains an exit point that satisfies `p` -/ @[inline] partial def hasExitPointPred (c : Code) (p : Code → Bool) : Bool := let rec @[specialize] loop : Code → Bool | Code.decl _ _ k => loop k | Code.reassign _ _ k => loop k | Code.joinpoint _ _ b k => loop b || loop k | Code.seq _ k => loop k | Code.ite _ _ _ _ t e => loop t || loop e | Code.«match» _ _ _ alts => alts.any (loop ·.rhs) | Code.jmp _ _ _ => false | c => p c loop c def hasExitPoint (c : Code) : Bool := hasExitPointPred c fun c => true def hasReturn (c : Code) : Bool := hasExitPointPred c fun | Code.«return» _ _ => true | _ => false def hasTerminalAction (c : Code) : Bool := hasExitPointPred c fun | Code.«action» _ => true | _ => false def hasBreakContinue (c : Code) : Bool := hasExitPointPred c fun | Code.«break» _ => true | Code.«continue» _ => true | _ => false def hasBreakContinueReturn (c : Code) : Bool := hasExitPointPred c fun | Code.«break» _ => true | Code.«continue» _ => true | Code.«return» _ _ => true | _ => false def mkAuxDeclFor {m} [Monad m] [MonadQuotation m] (e : Syntax) (mkCont : Syntax → m Code) : m Code := withRef e <| withFreshMacroScope do let y ← `(y) let yName := y.getId let doElem ← `(doElem| let y ← $e:term) -- Add elaboration hint for producing sane error message let y ← `(ensureExpectedType! "type mismatch, result value" $y) let k ← mkCont y pure $ Code.decl #[yName] doElem k /- Convert `action _ e` instructions in `c` into `let y ← e; jmp _ jp (xs y)`. -/ partial def convertTerminalActionIntoJmp (code : Code) (jp : Name) (xs : Array Name) : MacroM Code := let rec loop : Code → MacroM Code | Code.decl xs stx k => do Code.decl xs stx (← loop k) | Code.reassign xs stx k => do Code.reassign xs stx (← loop k) | Code.joinpoint n ps b k => do Code.joinpoint n ps (← loop b) (← loop k) | Code.seq e k => do Code.seq e (← loop k) | Code.ite ref x? h c t e => do Code.ite ref x? h c (← loop t) (← loop e) | Code.«match» ref ds t alts => do Code.«match» ref ds t (← alts.mapM fun alt => do pure { alt with rhs := (← loop alt.rhs) }) | Code.action e => mkAuxDeclFor e fun y => let ref := e -- We jump to `jp` with xs **and** y let jmpArgs := xs.map $ mkIdentFrom ref let jmpArgs := jmpArgs.push y pure $ Code.jmp ref jp jmpArgs | c => pure c loop code structure JPDecl where name : Name params : Array (Name × Bool) body : Code def attachJP (jpDecl : JPDecl) (k : Code) : Code := Code.joinpoint jpDecl.name jpDecl.params jpDecl.body k def attachJPs (jpDecls : Array JPDecl) (k : Code) : Code := jpDecls.foldr attachJP k def mkFreshJP (ps : Array (Name × Bool)) (body : Code) : TermElabM JPDecl := do let ps ← if ps.isEmpty then let y ← mkFreshUserName `y pure #[(y, false)] else pure ps -- Remark: the compiler frontend implemented in C++ currently detects jointpoints created by -- the "do" notation by testing the name. See hack at method `visit_let` at `lcnf.cpp` -- We will remove this hack when we re-implement the compiler frontend in Lean. let name ← mkFreshUserName `_do_jp pure { name := name, params := ps, body := body } def mkFreshJP' (xs : Array Name) (body : Code) : TermElabM JPDecl := mkFreshJP (xs.map fun x => (x, true)) body def addFreshJP (ps : Array (Name × Bool)) (body : Code) : StateRefT (Array JPDecl) TermElabM Name := do let jp ← mkFreshJP ps body modify fun (jps : Array JPDecl) => jps.push jp pure jp.name def insertVars (rs : NameSet) (xs : Array Name) : NameSet := xs.foldl (·.insert ·) rs def eraseVars (rs : NameSet) (xs : Array Name) : NameSet := xs.foldl (·.erase ·) rs def eraseOptVar (rs : NameSet) (x? : Option Name) : NameSet := match x? with | none => rs | some x => rs.insert x /- Create a new jointpoint for `c`, and jump to it with the variables `rs` -/ def mkSimpleJmp (ref : Syntax) (rs : NameSet) (c : Code) : StateRefT (Array JPDecl) TermElabM Code := do let xs := nameSetToArray rs let jp ← addFreshJP (xs.map fun x => (x, true)) c if xs.isEmpty then let unit ← `(Unit.unit) return Code.jmp ref jp #[unit] else return Code.jmp ref jp (xs.map $ mkIdentFrom ref) /- Create a new joinpoint that takes `rs` and `val` as arguments. `val` must be syntax representing a pure value. The body of the joinpoint is created using `mkJPBody yFresh`, where `yFresh` is a fresh variable created by this method. -/ def mkJmp (ref : Syntax) (rs : NameSet) (val : Syntax) (mkJPBody : Syntax → MacroM Code) : StateRefT (Array JPDecl) TermElabM Code := do let xs := nameSetToArray rs let args := xs.map $ mkIdentFrom ref let args := args.push val let yFresh ← mkFreshUserName `y let ps := xs.map fun x => (x, true) let ps := ps.push (yFresh, false) let jpBody ← liftMacroM $ mkJPBody (mkIdentFrom ref yFresh) let jp ← addFreshJP ps jpBody pure $ Code.jmp ref jp args /- `pullExitPointsAux rs c` auxiliary method for `pullExitPoints`, `rs` is the set of update variable in the current path. -/ partial def pullExitPointsAux : NameSet → Code → StateRefT (Array JPDecl) TermElabM Code | rs, Code.decl xs stx k => do Code.decl xs stx (← pullExitPointsAux (eraseVars rs xs) k) | rs, Code.reassign xs stx k => do Code.reassign xs stx (← pullExitPointsAux (insertVars rs xs) k) | rs, Code.joinpoint j ps b k => do Code.joinpoint j ps (← pullExitPointsAux rs b) (← pullExitPointsAux rs k) | rs, Code.seq e k => do Code.seq e (← pullExitPointsAux rs k) | rs, Code.ite ref x? o c t e => do Code.ite ref x? o c (← pullExitPointsAux (eraseOptVar rs x?) t) (← pullExitPointsAux (eraseOptVar rs x?) e) | rs, Code.«match» ref ds t alts => do Code.«match» ref ds t (← alts.mapM fun alt => do pure { alt with rhs := (← pullExitPointsAux (eraseVars rs alt.vars) alt.rhs) }) | rs, c@(Code.jmp _ _ _) => pure c | rs, Code.«break» ref => mkSimpleJmp ref rs (Code.«break» ref) | rs, Code.«continue» ref => mkSimpleJmp ref rs (Code.«continue» ref) | rs, Code.«return» ref val => mkJmp ref rs val (fun y => pure $ Code.«return» ref y) | rs, Code.action e => -- We use `mkAuxDeclFor` because `e` is not pure. mkAuxDeclFor e fun y => let ref := e mkJmp ref rs y (fun yFresh => do pure $ Code.action (← `(Pure.pure $yFresh))) /- Auxiliary operation for adding new variables to the collection of updated variables in a CodeBlock. When a new variable is not already in the collection, but is shadowed by some declaration in `c`, we create auxiliary join points to make sure we preserve the semantics of the code block. Example: suppose we have the code block `print x; let x := 10; return x`. And we want to extend it with the reassignment `x := x + 1`. We first use `pullExitPoints` to create ``` let jp (x!1) := return x!1; print x; let x := 10; jmp jp x ``` and then we add the reassignment ``` x := x + 1 let jp (x!1) := return x!1; print x; let x := 10; jmp jp x ``` Note that we created a fresh variable `x!1` to avoid accidental name capture. As another example, consider ``` print x; let x := 10 y := y + 1; return x; ``` We transform it into ``` let jp (y x!1) := return x!1; print x; let x := 10 y := y + 1; jmp jp y x ``` and then we add the reassignment as in the previous example. We need to include `y` in the jump, because each exit point is implicitly returning the set of update variables. We implement the method as follows. Let `us` be `c.uvars`, then 1- for each `return _ y` in `c`, we create a join point `let j (us y!1) := return y!1` and replace the `return _ y` with `jmp us y` 2- for each `break`, we create a join point `let j (us) := break` and replace the `break` with `jmp us`. 3- Same as 2 for `continue`. -/ def pullExitPoints (c : Code) : TermElabM Code := do if hasExitPoint c then let (c, jpDecls) ← (pullExitPointsAux {} c).run #[] pure $ attachJPs jpDecls c else pure c partial def extendUpdatedVarsAux (c : Code) (ws : NameSet) : TermElabM Code := let rec update : Code → TermElabM Code | Code.joinpoint j ps b k => do Code.joinpoint j ps (← update b) (← update k) | Code.seq e k => do Code.seq e (← update k) | c@(Code.«match» ref ds t alts) => do if alts.any fun alt => alt.vars.any fun x => ws.contains x then -- If a pattern variable is shadowing a variable in ws, we `pullExitPoints` pullExitPoints c else Code.«match» ref ds t (← alts.mapM fun alt => do pure { alt with rhs := (← update alt.rhs) }) | Code.ite ref none o c t e => do Code.ite ref none o c (← update t) (← update e) | c@(Code.ite ref (some h) o cond t e) => do if ws.contains h then -- if the `h` at `if h:c then t else e` shadows a variable in `ws`, we `pullExitPoints` pullExitPoints c else Code.ite ref (some h) o cond (← update t) (← update e) | Code.reassign xs stx k => do Code.reassign xs stx (← update k) | c@(Code.decl xs stx k) => do if xs.any fun x => ws.contains x then -- One the declared variables is shadowing a variable in `ws` pullExitPoints c else Code.decl xs stx (← update k) | c => pure c update c /- Extend the set of updated variables. It assumes `ws` is a super set of `c.uvars`. We **cannot** simply update the field `c.uvars`, because `c` may have shadowed some variable in `ws`. See discussion at `pullExitPoints`. -/ partial def extendUpdatedVars (c : CodeBlock) (ws : NameSet) : TermElabM CodeBlock := do if ws.any fun x => !c.uvars.contains x then -- `ws` contains a variable that is not in `c.uvars`, but in `c.dvars` (i.e., it has been shadowed) pure { code := (← extendUpdatedVarsAux c.code ws), uvars := ws } else pure { c with uvars := ws } private def union (s₁ s₂ : NameSet) : NameSet := s₁.fold (·.insert ·) s₂ /- Given two code blocks `c₁` and `c₂`, make sure they have the same set of updated variables. Let `ws` the union of the updated variables in `c₁‵ and ‵c₂`. We use `extendUpdatedVars c₁ ws` and `extendUpdatedVars c₂ ws` -/ def homogenize (c₁ c₂ : CodeBlock) : TermElabM (CodeBlock × CodeBlock) := do let ws := union c₁.uvars c₂.uvars let c₁ ← extendUpdatedVars c₁ ws let c₂ ← extendUpdatedVars c₂ ws pure (c₁, c₂) /- Extending code blocks with variable declarations: `let x : t := v` and `let x : t ← v`. We remove `x` from the collection of updated varibles. Remark: `stx` is the syntax for the declaration (e.g., `letDecl`), and `xs` are the variables declared by it. It is an array because we have let-declarations that declare multiple variables. Example: `let (x, y) := t` -/ def mkVarDeclCore (xs : Array Name) (stx : Syntax) (c : CodeBlock) : CodeBlock := { code := Code.decl xs stx c.code, uvars := eraseVars c.uvars xs } /- Extending code blocks with reassignments: `x : t := v` and `x : t ← v`. Remark: `stx` is the syntax for the declaration (e.g., `letDecl`), and `xs` are the variables declared by it. It is an array because we have let-declarations that declare multiple variables. Example: `(x, y) ← t` -/ def mkReassignCore (xs : Array Name) (stx : Syntax) (c : CodeBlock) : TermElabM CodeBlock := do let us := c.uvars let ws := insertVars us xs -- If `xs` contains a new updated variable, then we must use `extendUpdatedVars`. -- See discussion at `pullExitPoints` let code ← if xs.any fun x => !us.contains x then extendUpdatedVarsAux c.code ws else pure c.code pure { code := Code.reassign xs stx code, uvars := ws } def mkSeq (action : Syntax) (c : CodeBlock) : CodeBlock := { c with code := Code.seq action c.code } def mkTerminalAction (action : Syntax) : CodeBlock := { code := Code.action action } def mkReturn (ref : Syntax) (val : Syntax) : CodeBlock := { code := Code.«return» ref val } def mkBreak (ref : Syntax) : CodeBlock := { code := Code.«break» ref } def mkContinue (ref : Syntax) : CodeBlock := { code := Code.«continue» ref } def mkIte (ref : Syntax) (optIdent : Syntax) (cond : Syntax) (thenBranch : CodeBlock) (elseBranch : CodeBlock) : TermElabM CodeBlock := do let x? := if optIdent.isNone then none else some optIdent[0].getId let (thenBranch, elseBranch) ← homogenize thenBranch elseBranch pure { code := Code.ite ref x? optIdent cond thenBranch.code elseBranch.code, uvars := thenBranch.uvars, } private def mkUnit (ref : Syntax) : MacroM Syntax := do let unit ← `(PUnit.unit) pure $ unit.copyInfo ref private def mkPureUnit (ref : Syntax) : MacroM Syntax := do let unit ← mkUnit ref let pureUnit ← `(Pure.pure $(unit.copyInfo ref)) pure $ pureUnit.copyInfo ref def mkPureUnitAction (ref : Syntax) : MacroM CodeBlock := do mkTerminalAction (← mkPureUnit ref) def mkUnless (ref : Syntax) (cond : Syntax) (c : CodeBlock) : MacroM CodeBlock := do let thenBranch ← mkPureUnitAction ref pure { c with code := Code.ite ref none mkNullNode cond thenBranch.code c.code } def mkMatch (ref : Syntax) (discrs : Syntax) (optType : Syntax) (alts : Array (Alt CodeBlock)) : TermElabM CodeBlock := do -- nary version of homogenize let ws := alts.foldl (union · ·.rhs.uvars) {} let alts ← alts.mapM fun alt => do let rhs ← extendUpdatedVars alt.rhs ws pure { ref := alt.ref, vars := alt.vars, patterns := alt.patterns, rhs := rhs.code : Alt Code } pure { code := Code.«match» ref discrs optType alts, uvars := ws } /- Return a code block that executes `terminal` and then `k` with the value produced by `terminal`. This method assumes `terminal` is a terminal -/ def concat (terminal : CodeBlock) (kRef : Syntax) (y? : Option Name) (k : CodeBlock) : TermElabM CodeBlock := do unless hasTerminalAction terminal.code do throwErrorAt kRef "'do' element is unreachable" let (terminal, k) ← homogenize terminal k let xs := nameSetToArray k.uvars let y ← match y? with | some y => pure y | none => mkFreshUserName `y let ps := xs.map fun x => (x, true) let ps := ps.push (y, false) let jpDecl ← mkFreshJP ps k.code let jp := jpDecl.name let terminal ← liftMacroM $ convertTerminalActionIntoJmp terminal.code jp xs pure { code := attachJP jpDecl terminal, uvars := k.uvars } def getLetIdDeclVar (letIdDecl : Syntax) : Name := letIdDecl[0].getId def getPatternVarNames (pvars : Array PatternVar) : Array Name := pvars.filterMap fun | PatternVar.localVar x => some x | _ => none -- support both regular and syntax match def getPatternVarsEx (pattern : Syntax) : TermElabM (Array Name) := getPatternVarNames <$> getPatternVars pattern <|> Array.map Syntax.getId <$> Quotation.getPatternVars pattern def getPatternsVarsEx (patterns : Array Syntax) : TermElabM (Array Name) := getPatternVarNames <$> getPatternsVars patterns <|> Array.map Syntax.getId <$> Quotation.getPatternsVars patterns def getLetPatDeclVars (letPatDecl : Syntax) : TermElabM (Array Name) := do let pattern := letPatDecl[0] getPatternVarsEx pattern def getLetEqnsDeclVar (letEqnsDecl : Syntax) : Name := letEqnsDecl[0].getId def getLetDeclVars (letDecl : Syntax) : TermElabM (Array Name) := do let arg := letDecl[0] if arg.getKind == `Lean.Parser.Term.letIdDecl then pure #[getLetIdDeclVar arg] else if arg.getKind == `Lean.Parser.Term.letPatDecl then getLetPatDeclVars arg else if arg.getKind == `Lean.Parser.Term.letEqnsDecl then pure #[getLetEqnsDeclVar arg] else throwError "unexpected kind of let declaration" def getDoLetVars (doLet : Syntax) : TermElabM (Array Name) := -- parser! "let " >> optional "mut " >> letDecl getLetDeclVars doLet[2] def getDoHaveVar (doHave : Syntax) : Name := /- `parser! "have " >> Term.haveDecl` where ``` haveDecl := optIdent >> termParser >> (haveAssign <|> fromTerm <|> byTactic) optIdent := optional (try (ident >> " : ")) ``` -/ let optIdent := doHave[1] if optIdent.isNone then `this else optIdent[0].getId def getDoLetRecVars (doLetRec : Syntax) : TermElabM (Array Name) := do -- letRecDecls is an array of `(group (optional attributes >> letDecl))` let letRecDecls := doLetRec[1].getSepArgs let letDecls := letRecDecls.map fun p => p[2] let mut allVars := #[] for letDecl in letDecls do let vars ← getLetDeclVars letDecl allVars := allVars ++ vars pure allVars -- ident >> optType >> leftArrow >> termParser def getDoIdDeclVar (doIdDecl : Syntax) : Name := doIdDecl[0].getId -- termParser >> leftArrow >> termParser >> optional (" | " >> termParser) def getDoPatDeclVars (doPatDecl : Syntax) : TermElabM (Array Name) := do let pattern := doPatDecl[0] getPatternVarsEx pattern -- parser! "let " >> optional "mut " >> (doIdDecl <|> doPatDecl) def getDoLetArrowVars (doLetArrow : Syntax) : TermElabM (Array Name) := do let decl := doLetArrow[2] if decl.getKind == `Lean.Parser.Term.doIdDecl then pure #[getDoIdDeclVar decl] else if decl.getKind == `Lean.Parser.Term.doPatDecl then getDoPatDeclVars decl else throwError "unexpected kind of 'do' declaration" def getDoReassignVars (doReassign : Syntax) : TermElabM (Array Name) := do let arg := doReassign[0] if arg.getKind == `Lean.Parser.Term.letIdDecl then pure #[getLetIdDeclVar arg] else if arg.getKind == `Lean.Parser.Term.letPatDecl then getLetPatDeclVars arg else throwError "unexpected kind of reassignment" def mkDoSeq (doElems : Array Syntax) : Syntax := mkNode `Lean.Parser.Term.doSeqIndent #[mkNullNode $ doElems.map fun doElem => mkNullNode #[doElem, mkNullNode]] def mkSingletonDoSeq (doElem : Syntax) : Syntax := mkDoSeq #[doElem] /- If the given syntax is a `doIf`, return an equivalente `doIf` that has an `else` but no `else if`s or `if let`s. -/ private def expandDoIf? (stx : Syntax) : MacroM (Option Syntax) := match stx with | `(doElem|if $p:doIfProp then $t else $e) => pure none | `(doElem|if%$i $cond:doIfCond then $t $[else if%$is $conds:doIfCond then $ts]* $[else $e?]?) => withRef stx do let mut e := e?.getD (← `(doSeq|pure PUnit.unit)) let mut eIsSeq := true for (i, cond, t) in Array.zip (is.reverse.push i) (Array.zip (conds.reverse.push cond) (ts.reverse.push t)) do e ← if eIsSeq then e else `(doSeq|$e:doElem) e ← withRef cond <| match cond with | `(doIfCond|let $pat := $d) => `(doElem| match%$i $d:term with | $pat:term => $t | _ => $e) | `(doIfCond|let $pat ← $d) => `(doElem| match%$i ← $d with | $pat:term => $t | _ => $e) | _ => `(doElem| if%$i $cond:doIfCond then $t else $e) eIsSeq := false return some e | _ => pure none structure DoIfView where ref : Syntax optIdent : Syntax cond : Syntax thenBranch : Syntax elseBranch : Syntax /- This method assumes `expandDoIf?` is not applicable. -/ private def mkDoIfView (doIf : Syntax) : MacroM DoIfView := do pure { ref := doIf, optIdent := doIf[1][0], cond := doIf[1][1], thenBranch := doIf[3], elseBranch := doIf[5][1] } /- We use `MProd` instead of `Prod` to group values when expanding the `do` notation. `MProd` is a universe monomorphic product. The motivation is to generate simpler universe constraints in code that was not written by the user. Note that we are not restricting the macro power since the `Bind.bind` combinator already forces values computed by monadic actions to be in the same universe. -/ private def mkTuple (ref : Syntax) (elems : Array Syntax) : MacroM Syntax := do if elems.size == 0 then mkUnit ref else if elems.size == 1 then pure elems[0] else (elems.extract 0 (elems.size - 1)).foldrM (fun elem tuple => do let tuple ← `(MProd.mk $elem $tuple) pure $ tuple.copyInfo ref) (elems.back) /- Return `some action` if `doElem` is a `doExpr <action>`-/ def isDoExpr? (doElem : Syntax) : Option Syntax := if doElem.getKind == `Lean.Parser.Term.doExpr then some doElem[0] else none /- The procedure `ToTerm.run` converts a `CodeBlock` into a `Syntax` term. We use this method to convert 1- The `CodeBlock` for a root `do ...` term into a `Syntax` term. This kind of `CodeBlock` never contains `break` nor `continue`. Moreover, the collection of updated variables is not packed into the result. Thus, we have two kinds of exit points - `Code.action e` which is converted into `e` - `Code.return _ e` which is converted into `pure e` We use `Kind.regular` for this case. 2- The `CodeBlock` for `b` at `for x in xs do b`. In this case, we need to generate a `Syntax` term representing a function for the `xs.forIn` combinator. a) If `b` contain a `Code.return _ a` exit point. The generated `Syntax` term has type `m (ForInStep (Option α × σ))`, where `a : α`, and the `σ` is the type of the tuple of variables reassigned by `b`. We use `Kind.forInWithReturn` for this case b) If `b` does not contain a `Code.return _ a` exit point. Then, the generated `Syntax` term has type `m (ForInStep σ)`. We use `Kind.forIn` for this case. 3- The `CodeBlock` `c` for a `do` sequence nested in a monadic combinator (e.g., `MonadExcept.tryCatch`). The generated `Syntax` term for `c` must inform whether `c` "exited" using `Code.action`, `Code.return`, `Code.break` or `Code.continue`. We use the auxiliary types `DoResult`s for storing this information. For example, the auxiliary type `DoResultPBC α σ` is used for a code block that exits with `Code.action`, **and** `Code.break`/`Code.continue`, `α` is the type of values produced by the exit `action`, and `σ` is the type of the tuple of reassigned variables. The type `DoResult α β σ` is usedf for code blocks that exit with `Code.action`, `Code.return`, **and** `Code.break`/`Code.continue`, `β` is the type of the returned values. We don't use `DoResult α β σ` for all cases because: a) The elaborator would not be able to infer all type parameters without extra annotations. For example, if the code block does not contain `Code.return _ _`, the elaborator will not be able to infer `β`. b) We need to pattern match on the result produced by the combinator (e.g., `MonadExcept.tryCatch`), but we don't want to consider "unreachable" cases. We do not distinguish between cases that contain `break`, but not `continue`, and vice versa. When listing all cases, we use `a` to indicate the code block contains `Code.action _`, `r` for `Code.return _ _`, and `b/c` for a code block that contains `Code.break _` or `Code.continue _`. - `a`: `Kind.regular`, type `m (α × σ)` - `r`: `Kind.regular`, type `m (α × σ)` Note that the code that pattern matches on the result will behave differently in this case. It produces `return a` for this case, and `pure a` for the previous one. - `b/c`: `Kind.nestedBC`, type `m (DoResultBC σ)` - `a` and `r`: `Kind.nestedPR`, type `m (DoResultPR α β σ)` - `a` and `bc`: `Kind.nestedSBC`, type `m (DoResultSBC α σ)` - `r` and `bc`: `Kind.nestedSBC`, type `m (DoResultSBC α σ)` Again the code that pattern matches on the result will behave differently in this case and the previous one. It produces `return a` for the constructor `DoResultSPR.pureReturn a u` for this case, and `pure a` for the previous case. - `a`, `r`, `b/c`: `Kind.nestedPRBC`, type type `m (DoResultPRBC α β σ)` Here is the recipe for adding new combinators with nested `do`s. Example: suppose we want to support `repeat doSeq`. Assuming we have `repeat : m α → m α` 1- Convert `doSeq` into `codeBlock : CodeBlock` 2- Create term `term` using `mkNestedTerm code m uvars a r bc` where `code` is `codeBlock.code`, `uvars` is an array containing `codeBlock.uvars`, `m` is a `Syntax` representing the Monad, and `a` is true if `code` contains `Code.action _`, `r` is true if `code` contains `Code.return _ _`, `bc` is true if `code` contains `Code.break _` or `Code.continue _`. Remark: for combinators such as `repeat` that take a single `doSeq`, all arguments, but `m`, are extracted from `codeBlock`. 3- Create the term `repeat $term` 4- and then, convert it into a `doSeq` using `matchNestedTermResult ref (repeat $term) uvsar a r bc` -/ namespace ToTerm inductive Kind where | regular | forIn | forInWithReturn | nestedBC | nestedPR | nestedSBC | nestedPRBC instance : Inhabited Kind := ⟨Kind.regular⟩ def Kind.isRegular : Kind → Bool | Kind.regular => true | _ => false structure Context where m : Syntax -- Syntax to reference the monad associated with the do notation. uvars : Array Name kind : Kind abbrev M := ReaderT Context MacroM def mkUVarTuple (ref : Syntax) : M Syntax := do let ctx ← read let uvarIdents := ctx.uvars.map fun x => mkIdentFrom ref x mkTuple ref uvarIdents def returnToTermCore (ref : Syntax) (val : Syntax) : M Syntax := do let ctx ← read let u ← mkUVarTuple ref match ctx.kind with | Kind.regular => if ctx.uvars.isEmpty then `(Pure.pure $val) else `(Pure.pure (MProd.mk $val $u)) | Kind.forIn => `(Pure.pure (ForInStep.done $u)) | Kind.forInWithReturn => `(Pure.pure (ForInStep.done (MProd.mk (some $val) $u))) | Kind.nestedBC => unreachable! | Kind.nestedPR => `(Pure.pure (DoResultPR.«return» $val $u)) | Kind.nestedSBC => `(Pure.pure (DoResultSBC.«pureReturn» $val $u)) | Kind.nestedPRBC => `(Pure.pure (DoResultPRBC.«return» $val $u)) def returnToTerm (ref : Syntax) (val : Syntax) : M Syntax := do let r ← returnToTermCore ref val pure $ r.copyInfo ref def continueToTermCore (ref : Syntax) : M Syntax := do let ctx ← read let u ← mkUVarTuple ref match ctx.kind with | Kind.regular => unreachable! | Kind.forIn => `(Pure.pure (ForInStep.yield $u)) | Kind.forInWithReturn => `(Pure.pure (ForInStep.yield (MProd.mk none $u))) | Kind.nestedBC => `(Pure.pure (DoResultBC.«continue» $u)) | Kind.nestedPR => unreachable! | Kind.nestedSBC => `(Pure.pure (DoResultSBC.«continue» $u)) | Kind.nestedPRBC => `(Pure.pure (DoResultPRBC.«continue» $u)) def continueToTerm (ref : Syntax) : M Syntax := do let r ← continueToTermCore ref pure $ r.copyInfo ref def breakToTermCore (ref : Syntax) : M Syntax := do let ctx ← read let u ← mkUVarTuple ref match ctx.kind with | Kind.regular => unreachable! | Kind.forIn => `(Pure.pure (ForInStep.done $u)) | Kind.forInWithReturn => `(Pure.pure (ForInStep.done (MProd.mk none $u))) | Kind.nestedBC => `(Pure.pure (DoResultBC.«break» $u)) | Kind.nestedPR => unreachable! | Kind.nestedSBC => `(Pure.pure (DoResultSBC.«break» $u)) | Kind.nestedPRBC => `(Pure.pure (DoResultPRBC.«break» $u)) def breakToTerm (ref : Syntax) : M Syntax := do let r ← breakToTermCore ref pure $ r.copyInfo ref def actionTerminalToTermCore (action : Syntax) : M Syntax := withFreshMacroScope do let ref := action let ctx ← read let u ← mkUVarTuple ref match ctx.kind with | Kind.regular => if ctx.uvars.isEmpty then pure action else `(Bind.bind $action fun y => Pure.pure (MProd.mk y $u)) | Kind.forIn => `(Bind.bind $action fun (_ : PUnit) => Pure.pure (ForInStep.yield $u)) | Kind.forInWithReturn => `(Bind.bind $action fun (_ : PUnit) => Pure.pure (ForInStep.yield (MProd.mk none $u))) | Kind.nestedBC => unreachable! | Kind.nestedPR => `(Bind.bind $action fun y => (Pure.pure (DoResultPR.«pure» y $u))) | Kind.nestedSBC => `(Bind.bind $action fun y => (Pure.pure (DoResultSBC.«pureReturn» y $u))) | Kind.nestedPRBC => `(Bind.bind $action fun y => (Pure.pure (DoResultPRBC.«pure» y $u))) def actionTerminalToTerm (action : Syntax) : M Syntax := do let ref := action let r ← actionTerminalToTermCore action pure $ r.copyInfo ref def seqToTermCore (action : Syntax) (k : Syntax) : M Syntax := withFreshMacroScope do if action.getKind == `Lean.Parser.Term.doDbgTrace then let msg := action[1] `(dbgTrace! $msg; $k) else if action.getKind == `Lean.Parser.Term.doAssert then let cond := action[1] `(assert! $cond; $k) else let action := Syntax.copyRangePos (← `(($action : $((←read).m) PUnit))) action `(Bind.bind $action (fun (_ : PUnit) => $k)) def seqToTerm (action : Syntax) (k : Syntax) : M Syntax := do let r ← seqToTermCore action k return r.copyInfo action def declToTermCore (decl : Syntax) (k : Syntax) : M Syntax := withFreshMacroScope do let kind := decl.getKind if kind == `Lean.Parser.Term.doLet then let letDecl := decl[2] `(let $letDecl:letDecl; $k) else if kind == `Lean.Parser.Term.doLetRec then let letRecToken := decl[0] let letRecDecls := decl[1] pure $ mkNode `Lean.Parser.Term.letrec #[letRecToken, letRecDecls, mkNullNode, k] else if kind == `Lean.Parser.Term.doLetArrow then let arg := decl[2] let ref := arg if arg.getKind == `Lean.Parser.Term.doIdDecl then let id := arg[0] let type := expandOptType ref arg[1] let doElem := arg[3] -- `doElem` must be a `doExpr action`. See `doLetArrowToCode` match isDoExpr? doElem with | some action => let action := Syntax.copyRangePos (← `(($action : $((← read).m) $type))) action `(Bind.bind $action (fun ($id:ident : $type) => $k)) | none => Macro.throwErrorAt decl "unexpected kind of 'do' declaration" else Macro.throwErrorAt decl "unexpected kind of 'do' declaration" else if kind == `Lean.Parser.Term.doHave then -- The `have` term is of the form `"have " >> haveDecl >> optSemicolon termParser` let args := decl.getArgs let args := args ++ #[mkNullNode /- optional ';' -/, k] pure $ mkNode `Lean.Parser.Term.«have» args else Macro.throwErrorAt decl "unexpected kind of 'do' declaration" def declToTerm (decl : Syntax) (k : Syntax) : M Syntax := do let r ← declToTermCore decl k pure $ r.copyInfo decl def reassignToTermCore (reassign : Syntax) (k : Syntax) : MacroM Syntax := withFreshMacroScope do let kind := reassign.getKind if kind == `Lean.Parser.Term.doReassign then -- doReassign := parser! (letIdDecl <|> letPatDecl) let arg := reassign[0] if arg.getKind == `Lean.Parser.Term.letIdDecl then -- letIdDecl := parser! ident >> many (ppSpace >> bracketedBinder) >> optType >> " := " >> termParser let x := arg[0] let val := arg[4] let newVal ← `(ensureTypeOf! $x $(quote "invalid reassignment, value") $val) let arg := arg.setArg 4 newVal let letDecl := mkNode `Lean.Parser.Term.letDecl #[arg] `(let $letDecl:letDecl; $k) else -- TODO: ensure the types did not change let letDecl := mkNode `Lean.Parser.Term.letDecl #[arg] `(let $letDecl:letDecl; $k) else -- Note that `doReassignArrow` is expanded by `doReassignArrowToCode Macro.throwErrorAt reassign "unexpected kind of 'do' reassignment" def reassignToTerm (reassign : Syntax) (k : Syntax) : MacroM Syntax := do let r ← reassignToTermCore reassign k pure $ r.copyInfo reassign def mkIte (ref : Syntax) (optIdent : Syntax) (cond : Syntax) (thenBranch : Syntax) (elseBranch : Syntax) : MacroM Syntax := do let r ← if optIdent.isNone then `(ite $cond $thenBranch $elseBranch) else let h := optIdent[0] `(dite $cond (fun $h => $thenBranch) (fun $h => $elseBranch)) return r.copyInfo ref def mkJoinPointCore (j : Name) (ps : Array (Name × Bool)) (body : Syntax) (k : Syntax) : M Syntax := withFreshMacroScope do let ref := body let binders ← ps.mapM fun ⟨id, useTypeOf⟩ => do let type ← if useTypeOf then `(typeOf! $(mkIdentFrom ref id)) else `(_) let binderType := mkNullNode #[mkAtomFrom ref ":", type] pure $ mkNode `Lean.Parser.Term.explicitBinder #[mkAtomFrom ref "(", mkNullNode #[mkIdentFrom ref id], binderType, mkNullNode, mkAtomFrom ref ")"] let m := (← read).m let type ← `($m _) /- We use `let*` instead of `let` for joinpoints to make sure `$k` is elaborated before `$body`. By elaborating `$k` first, we "learn" more about `$body`'s type. For example, consider the following example `do` expression ``` def f (x : Nat) : IO Unit := do if x > 0 then IO.println "x is not zero" -- Error is here IO.mkRef true ``` it is expanded into ``` def f (x : Nat) : IO Unit := do let jp (u : Unit) : IO _ := IO.mkRef true; if x > 0 then IO.println "not zero" jp () else jp () ``` If we use the regular `let` instead of `let*`, the joinpoint `jp` will be elaborated and its type will be inferred to be `Unit → IO (IO.Ref Bool)`. Then, we get a typing error at `jp ()`. By using `let*`, we first elaborate `if x > 0 ...` and learn that `jp` has type `Unit → IO Unit`. Then, we get the expected type mismatch error at `IO.mkRef true`. -/ `(let* $(mkIdentFrom ref j):ident $binders:explicitBinder* : $type := $body; $k) def mkJoinPoint (j : Name) (ps : Array (Name × Bool)) (body : Syntax) (k : Syntax) : M Syntax := do let r ← mkJoinPointCore j ps body k pure $ r.copyInfo body def mkJmp (ref : Syntax) (j : Name) (args : Array Syntax) : Syntax := Syntax.mkApp (mkIdentFrom ref j) args partial def toTerm : Code → M Syntax | Code.«return» ref val => returnToTerm ref val | Code.«continue» ref => continueToTerm ref | Code.«break» ref => breakToTerm ref | Code.action e => actionTerminalToTerm e | Code.joinpoint j ps b k => do mkJoinPoint j ps (← toTerm b) (← toTerm k) | Code.jmp ref j args => pure $ mkJmp ref j args | Code.decl _ stx k => do declToTerm stx (← toTerm k) | Code.reassign _ stx k => do reassignToTerm stx (← toTerm k) | Code.seq stx k => do seqToTerm stx (← toTerm k) | Code.ite ref _ o c t e => do mkIte ref o c (← toTerm t) (← toTerm e) | Code.«match» ref discrs optType alts => do let mut termAlts := #[] for alt in alts do let rhs ← toTerm alt.rhs let termAlt := mkNode `Lean.Parser.Term.matchAlt #[mkAtomFrom alt.ref "|", alt.patterns, mkAtomFrom alt.ref "=>", rhs] termAlts := termAlts.push termAlt let termMatchAlts := mkNode `Lean.Parser.Term.matchAlts #[mkNullNode termAlts] pure $ mkNode `Lean.Parser.Term.«match» #[mkAtomFrom ref "match", discrs, optType, mkAtomFrom ref "with", termMatchAlts] def run (code : Code) (m : Syntax) (uvars : Array Name := #[]) (kind := Kind.regular) : MacroM Syntax := do let term ← toTerm code { m := m, kind := kind, uvars := uvars } pure term /- Given - `a` is true if the code block has a `Code.action _` exit point - `r` is true if the code block has a `Code.return _ _` exit point - `bc` is true if the code block has a `Code.break _` or `Code.continue _` exit point generate Kind. See comment at the beginning of the `ToTerm` namespace. -/ def mkNestedKind (a r bc : Bool) : Kind := match a, r, bc with | true, false, false => Kind.regular | false, true, false => Kind.regular | false, false, true => Kind.nestedBC | true, true, false => Kind.nestedPR | true, false, true => Kind.nestedSBC | false, true, true => Kind.nestedSBC | true, true, true => Kind.nestedPRBC | false, false, false => unreachable! def mkNestedTerm (code : Code) (m : Syntax) (uvars : Array Name) (a r bc : Bool) : MacroM Syntax := do ToTerm.run code m uvars (mkNestedKind a r bc) /- Given a term `term` produced by `ToTerm.run`, pattern match on its result. See comment at the beginning of the `ToTerm` namespace. - `a` is true if the code block has a `Code.action _` exit point - `r` is true if the code block has a `Code.return _ _` exit point - `bc` is true if the code block has a `Code.break _` or `Code.continue _` exit point The result is a sequence of `doElem` -/ def matchNestedTermResult (ref : Syntax) (term : Syntax) (uvars : Array Name) (a r bc : Bool) : MacroM (List Syntax) := do let toDoElems (auxDo : Syntax) : List Syntax := getDoSeqElems (getDoSeq auxDo) let u ← mkTuple ref (uvars.map (mkIdentFrom ref)) match a, r, bc with | true, false, false => if uvars.isEmpty then toDoElems (← `(do $term:term)) else toDoElems (← `(do let r ← $term:term; $u:term := r.2; pure r.1)) | false, true, false => if uvars.isEmpty then toDoElems (← `(do let r ← $term:term; return r)) else toDoElems (← `(do let r ← $term:term; $u:term := r.2; return r.1)) | false, false, true => toDoElems <$> `(do let r ← $term:term; match r with | DoResultBC.«break» u => $u:term := u; break | DoResultBC.«continue» u => $u:term := u; continue) | true, true, false => toDoElems <$> `(do let r ← $term:term; match r with | DoResultPR.«pure» a u => $u:term := u; pure a | DoResultPR.«return» b u => $u:term := u; return b) | true, false, true => toDoElems <$> `(do let r ← $term:term; match r with | DoResultSBC.«pureReturn» a u => $u:term := u; pure a | DoResultSBC.«break» u => $u:term := u; break | DoResultSBC.«continue» u => $u:term := u; continue) | false, true, true => toDoElems <$> `(do let r ← $term:term; match r with | DoResultSBC.«pureReturn» a u => $u:term := u; return a | DoResultSBC.«break» u => $u:term := u; break | DoResultSBC.«continue» u => $u:term := u; continue) | true, true, true => toDoElems <$> `(do let r ← $term:term; match r with | DoResultPRBC.«pure» a u => $u:term := u; pure a | DoResultPRBC.«return» a u => $u:term := u; return a | DoResultPRBC.«break» u => $u:term := u; break | DoResultPRBC.«continue» u => $u:term := u; continue) | false, false, false => unreachable! end ToTerm def isMutableLet (doElem : Syntax) : Bool := let kind := doElem.getKind (kind == `Lean.Parser.Term.doLetArrow || kind == `Lean.Parser.Term.doLet) && !doElem[1].isNone namespace ToCodeBlock structure Context where ref : Syntax m : Syntax -- Syntax representing the monad associated with the do notation. mutableVars : NameSet := {} insideFor : Bool := false abbrev M := ReaderT Context TermElabM @[inline] def withNewMutableVars {α} (newVars : Array Name) (mutable : Bool) (x : M α) : M α := withReader (fun ctx => if mutable then { ctx with mutableVars := insertVars ctx.mutableVars newVars } else ctx) x def checkReassignable (xs : Array Name) : M Unit := do let throwInvalidReassignment (x : Name) : M Unit := throwError! "'{x.simpMacroScopes}' cannot be reassigned" let ctx ← read for x in xs do unless ctx.mutableVars.contains x do throwInvalidReassignment x @[inline] def withFor {α} (x : M α) : M α := withReader (fun ctx => { ctx with insideFor := true }) x structure ToForInTermResult where uvars : Array Name term : Syntax def mkForInBody (x : Syntax) (forInBody : CodeBlock) : M ToForInTermResult := do let ctx ← read let uvars := forInBody.uvars let uvars := nameSetToArray uvars let term ← liftMacroM $ ToTerm.run forInBody.code ctx.m uvars (if hasReturn forInBody.code then ToTerm.Kind.forInWithReturn else ToTerm.Kind.forIn) pure ⟨uvars, term⟩ def ensureInsideFor : M Unit := unless (← read).insideFor do throwError "invalid 'do' element, it must be inside 'for'" def ensureEOS (doElems : List Syntax) : M Unit := unless doElems.isEmpty do throwError "must be last element in a 'do' sequence" private partial def expandLiftMethodAux (inQuot : Bool) : Syntax → StateT (List Syntax) MacroM Syntax | stx@(Syntax.node k args) => if liftMethodDelimiter k then pure stx else if k == `Lean.Parser.Term.liftMethod && !inQuot then withFreshMacroScope do let term := args[1] let term ← expandLiftMethodAux inQuot term let auxDoElem ← `(doElem| let a ← $term:term) modify fun s => s ++ [auxDoElem] `(a) else do let inAntiquot := stx.isAntiquot && !stx.isEscapedAntiquot let args ← args.mapM (expandLiftMethodAux (inQuot && !inAntiquot || stx.isQuot)) pure $ Syntax.node k args | stx => pure stx def expandLiftMethod (doElem : Syntax) : MacroM (List Syntax × Syntax) := do if !hasLiftMethod doElem then pure ([], doElem) else let (doElem, doElemsNew) ← (expandLiftMethodAux false doElem).run [] pure (doElemsNew, doElem) def checkLetArrowRHS (doElem : Syntax) : M Unit := do let kind := doElem.getKind if kind == `Lean.Parser.Term.doLetArrow || kind == `Lean.Parser.Term.doLet || kind == `Lean.Parser.Term.doLetRec || kind == `Lean.Parser.Term.doHave || kind == `Lean.Parser.Term.doReassign || kind == `Lean.Parser.Term.doReassignArrow then throwErrorAt! doElem "invalid kind of value '{kind}' in an assignment" /- Generate `CodeBlock` for `doReturn` which is of the form ``` "return " >> optional termParser ``` `doElems` is only used for sanity checking. -/ def doReturnToCode (doReturn : Syntax) (doElems: List Syntax) : M CodeBlock := do let ref := doReturn ensureEOS doElems let argOpt := doReturn[1] let arg ← if argOpt.isNone then liftMacroM <| mkUnit ref else pure argOpt[0] return mkReturn ref arg structure Catch where x : Syntax optType : Syntax codeBlock : CodeBlock def getTryCatchUpdatedVars (tryCode : CodeBlock) (catches : Array Catch) (finallyCode? : Option CodeBlock) : NameSet := let ws := tryCode.uvars let ws := catches.foldl (fun ws alt => union alt.codeBlock.uvars ws) ws let ws := match finallyCode? with | none => ws | some c => union c.uvars ws ws def tryCatchPred (tryCode : CodeBlock) (catches : Array Catch) (finallyCode? : Option CodeBlock) (p : Code → Bool) : Bool := p tryCode.code || catches.any (fun «catch» => p «catch».codeBlock.code) || match finallyCode? with | none => false | some finallyCode => p finallyCode.code mutual /- "Concatenate" `c` with `doSeqToCode doElems` -/ partial def concatWith (c : CodeBlock) (doElems : List Syntax) : M CodeBlock := match doElems with | [] => pure c | nextDoElem :: _ => do let k ← doSeqToCode doElems let ref := nextDoElem concat c ref none k /- Generate `CodeBlock` for `doLetArrow; doElems` `doLetArrow` is of the form ``` "let " >> optional "mut " >> (doIdDecl <|> doPatDecl) ``` where ``` def doIdDecl := parser! ident >> optType >> leftArrow >> doElemParser def doPatDecl := parser! termParser >> leftArrow >> doElemParser >> optional (" | " >> doElemParser) ``` -/ partial def doLetArrowToCode (doLetArrow : Syntax) (doElems : List Syntax) : M CodeBlock := do let ref := doLetArrow let decl := doLetArrow[2] if decl.getKind == `Lean.Parser.Term.doIdDecl then let y := decl[0].getId let doElem := decl[3] let k ← withNewMutableVars #[y] (isMutableLet doLetArrow) (doSeqToCode doElems) match isDoExpr? doElem with | some action => pure $ mkVarDeclCore #[y] doLetArrow k | none => checkLetArrowRHS doElem let c ← doSeqToCode [doElem] match doElems with | [] => pure c | kRef::_ => concat c kRef y k else if decl.getKind == `Lean.Parser.Term.doPatDecl then let pattern := decl[0] let doElem := decl[2] let optElse := decl[3] if optElse.isNone then withFreshMacroScope do let auxDo ← if isMutableLet doLetArrow then `(do let discr ← $doElem; let mut $pattern:term := discr) else `(do let discr ← $doElem; let $pattern:term := discr) doSeqToCode <| getDoSeqElems (getDoSeq auxDo) ++ doElems else if isMutableLet doLetArrow then throwError! "'mut' is currently not supported in let-decls with 'else' case" let contSeq := mkDoSeq doElems.toArray let elseSeq := mkSingletonDoSeq optElse[1] let auxDo ← `(do let discr ← $doElem; match discr with | $pattern:term => $contSeq | _ => $elseSeq) doSeqToCode <| getDoSeqElems (getDoSeq auxDo) else throwError "unexpected kind of 'do' declaration" /- Generate `CodeBlock` for `doReassignArrow; doElems` `doReassignArrow` is of the form ``` (doIdDecl <|> doPatDecl) ``` -/ partial def doReassignArrowToCode (doReassignArrow : Syntax) (doElems : List Syntax) : M CodeBlock := do let ref := doReassignArrow let decl := doReassignArrow[0] if decl.getKind == `Lean.Parser.Term.doIdDecl then let doElem := decl[3] let y := decl[0] let auxDo ← `(do let r ← $doElem; $y:ident := r) doSeqToCode <| getDoSeqElems (getDoSeq auxDo) ++ doElems else if decl.getKind == `Lean.Parser.Term.doPatDecl then let pattern := decl[0] let doElem := decl[2] let optElse := decl[3] if optElse.isNone then withFreshMacroScope do let auxDo ← `(do let discr ← $doElem; $pattern:term := discr) doSeqToCode <| getDoSeqElems (getDoSeq auxDo) ++ doElems else throwError "reassignment with `|` (i.e., \"else clause\") is not currently supported" else throwError "unexpected kind of 'do' reassignment" /- Generate `CodeBlock` for `doIf; doElems` `doIf` is of the form ``` "if " >> optIdent >> termParser >> " then " >> doSeq >> many (group (try (group (" else " >> " if ")) >> optIdent >> termParser >> " then " >> doSeq)) >> optional (" else " >> doSeq) ``` -/ partial def doIfToCode (doIf : Syntax) (doElems : List Syntax) : M CodeBlock := do let view ← liftMacroM $ mkDoIfView doIf let thenBranch ← doSeqToCode (getDoSeqElems view.thenBranch) let elseBranch ← doSeqToCode (getDoSeqElems view.elseBranch) let ite ← mkIte view.ref view.optIdent view.cond thenBranch elseBranch concatWith ite doElems /- Generate `CodeBlock` for `doUnless; doElems` `doUnless` is of the form ``` "unless " >> termParser >> "do " >> doSeq ``` -/ partial def doUnlessToCode (doUnless : Syntax) (doElems : List Syntax) : M CodeBlock := do let ref := doUnless let cond := doUnless[1] let doSeq := doUnless[3] let body ← doSeqToCode (getDoSeqElems doSeq) let unlessCode ← liftMacroM <| mkUnless ref cond body concatWith unlessCode doElems /- Generate `CodeBlock` for `doFor; doElems` `doFor` is of the form ``` def doForDecl := parser! termParser >> " in " >> withForbidden "do" termParser def doFor := parser! "for " >> sepBy1 doForDecl ", " >> "do " >> doSeq ``` -/ partial def doForToCode (doFor : Syntax) (doElems : List Syntax) : M CodeBlock := do let doForDecls := doFor[1].getSepArgs if doForDecls.size > 1 then /- Expand ``` for x in xs, y in ys do body ``` into ``` let s := toStream ys for x in xs do match Stream.next? s with | none => break | some (y, s') => s := s' body ``` -/ -- Extract second element let doForDecl := doForDecls[1] let y := doForDecl[0] let ys := doForDecl[2] let doForDecls := doForDecls.eraseIdx 1 let body := doFor[3] withFreshMacroScope do let toStreamFn ← `(toStream) let toStreamFn := toStreamFn.copyInfo ys let auxDo ← `(do let mut s := $toStreamFn:ident $ys for $doForDecls:doForDecl,* do match Stream.next? s with | none => break | some ($y, s') => s := s' do $body) doSeqToCode (getDoSeqElems (getDoSeq auxDo) ++ doElems) else let ref := doFor let x := doForDecls[0][0] let xs := doForDecls[0][2] let forElems := getDoSeqElems doFor[3] let forInBodyCodeBlock ← withFor (doSeqToCode forElems) let ⟨uvars, forInBody⟩ ← mkForInBody x forInBodyCodeBlock let uvarsTuple ← liftMacroM $ mkTuple ref (uvars.map (mkIdentFrom ref)) if hasReturn forInBodyCodeBlock.code then let forInTerm ← `($(xs).forIn (MProd.mk none $uvarsTuple) fun $x (MProd.mk _ $uvarsTuple) => $forInBody) let auxDo ← `(do let r ← $forInTerm:term; $uvarsTuple:term := r.2; match r.1 with | none => Pure.pure (ensureExpectedType! "type mismatch, 'for'" PUnit.unit) | some a => return ensureExpectedType! "type mismatch, 'for'" a) doSeqToCode (getDoSeqElems (getDoSeq auxDo) ++ doElems) else let forInTerm ← `($(xs).forIn $uvarsTuple fun $x $uvarsTuple => $forInBody) if doElems.isEmpty then let auxDo ← `(do let r ← $forInTerm:term; $uvarsTuple:term := r; Pure.pure (ensureExpectedType! "type mismatch, 'for'" PUnit.unit)) doSeqToCode <| getDoSeqElems (getDoSeq auxDo) else let auxDo ← `(do let r ← $forInTerm:term; $uvarsTuple:term := r) doSeqToCode <| getDoSeqElems (getDoSeq auxDo) ++ doElems /-- Generate `CodeBlock` for `doMatch; doElems` -/ partial def doMatchToCode (doMatch : Syntax) (doElems: List Syntax) : M CodeBlock := do let ref := doMatch let discrs := doMatch[1] let optType := doMatch[2] let matchAlts := doMatch[4][0].getArgs -- Array of `doMatchAlt` let alts ← matchAlts.mapM fun matchAlt => do let patterns := matchAlt[1] let vars ← getPatternsVarsEx patterns.getSepArgs let rhs := matchAlt[3] let rhs ← doSeqToCode (getDoSeqElems rhs) pure { ref := matchAlt, vars := vars, patterns := patterns, rhs := rhs : Alt CodeBlock } let matchCode ← mkMatch ref discrs optType alts concatWith matchCode doElems /-- Generate `CodeBlock` for `doTry; doElems` ``` def doTry := parser! "try " >> doSeq >> many (doCatch <|> doCatchMatch) >> optional doFinally def doCatch := parser! "catch " >> binderIdent >> optional (":" >> termParser) >> darrow >> doSeq def doCatchMatch := parser! "catch " >> doMatchAlts def doFinally := parser! "finally " >> doSeq ``` -/ partial def doTryToCode (doTry : Syntax) (doElems: List Syntax) : M CodeBlock := do let ref := doTry let tryCode ← doSeqToCode (getDoSeqElems doTry[1]) let optFinally := doTry[3] let catches ← doTry[2].getArgs.mapM fun catchStx => do if catchStx.getKind == `Lean.Parser.Term.doCatch then let x := catchStx[1] let optType := catchStx[2] let c ← doSeqToCode (getDoSeqElems catchStx[4]) pure { x := x, optType := optType, codeBlock := c : Catch } else if catchStx.getKind == `Lean.Parser.Term.doCatchMatch then let matchAlts := catchStx[1] let x ← `(ex) let auxDo ← `(do match ex with $matchAlts) let c ← doSeqToCode (getDoSeqElems (getDoSeq auxDo)) pure { x := x, codeBlock := c, optType := mkNullNode : Catch } else throwError "unexpected kind of 'catch'" let finallyCode? ← if optFinally.isNone then pure none else some <$> doSeqToCode (getDoSeqElems optFinally[0][1]) if catches.isEmpty && finallyCode?.isNone then throwError "invalid 'try', it must have a 'catch' or 'finally'" let ctx ← read let ws := getTryCatchUpdatedVars tryCode catches finallyCode? let uvars := nameSetToArray ws let a := tryCatchPred tryCode catches finallyCode? hasTerminalAction let r := tryCatchPred tryCode catches finallyCode? hasReturn let bc := tryCatchPred tryCode catches finallyCode? hasBreakContinue let toTerm (codeBlock : CodeBlock) : M Syntax := do let codeBlock ← liftM $ extendUpdatedVars codeBlock ws liftMacroM $ ToTerm.mkNestedTerm codeBlock.code ctx.m uvars a r bc let term ← toTerm tryCode let term ← catches.foldlM (fun term «catch» => do let catchTerm ← toTerm «catch».codeBlock if catch.optType.isNone then `(MonadExcept.tryCatch $term (fun $(«catch».x):ident => $catchTerm)) else let type := «catch».optType[1] `(tryCatchThe $type $term (fun $(«catch».x):ident => $catchTerm))) term let term ← match finallyCode? with | none => pure term | some finallyCode => withRef optFinally do unless finallyCode.uvars.isEmpty do throwError "'finally' currently does not support reassignments" if hasBreakContinueReturn finallyCode.code then throwError "'finally' currently does 'return', 'break', nor 'continue'" let finallyTerm ← liftMacroM <| ToTerm.run finallyCode.code ctx.m {} ToTerm.Kind.regular `(tryFinally $term $finallyTerm) let doElemsNew ← liftMacroM <| ToTerm.matchNestedTermResult ref term uvars a r bc doSeqToCode (doElemsNew ++ doElems) partial def doSeqToCode : List Syntax → M CodeBlock | [] => do liftMacroM <| mkPureUnitAction (← read).ref | doElem::doElems => withRef doElem do match (← liftMacroM <| expandMacro? doElem) with | some doElem => doSeqToCode (doElem::doElems) | none => match (← liftMacroM <| expandDoIf? doElem) with | some doElem => doSeqToCode (doElem::doElems) | none => let (liftedDoElems, doElem) ← liftM (liftMacroM <| expandLiftMethod doElem : TermElabM _) if !liftedDoElems.isEmpty then doSeqToCode (liftedDoElems ++ [doElem] ++ doElems) else let ref := doElem let concatWithRest (c : CodeBlock) : M CodeBlock := concatWith c doElems let k := doElem.getKind if k == `Lean.Parser.Term.doLet then let vars ← getDoLetVars doElem mkVarDeclCore vars doElem <$> withNewMutableVars vars (isMutableLet doElem) (doSeqToCode doElems) else if k == `Lean.Parser.Term.doHave then let var := getDoHaveVar doElem mkVarDeclCore #[var] doElem <$> (doSeqToCode doElems) else if k == `Lean.Parser.Term.doLetRec then let vars ← getDoLetRecVars doElem mkVarDeclCore vars doElem <$> (doSeqToCode doElems) else if k == `Lean.Parser.Term.doReassign then let vars ← getDoReassignVars doElem checkReassignable vars let k ← doSeqToCode doElems mkReassignCore vars doElem k else if k == `Lean.Parser.Term.doLetArrow then doLetArrowToCode doElem doElems else if k == `Lean.Parser.Term.doReassignArrow then doReassignArrowToCode doElem doElems else if k == `Lean.Parser.Term.doIf then doIfToCode doElem doElems else if k == `Lean.Parser.Term.doUnless then doUnlessToCode doElem doElems else if k == `Lean.Parser.Term.doFor then withFreshMacroScope do doForToCode doElem doElems else if k == `Lean.Parser.Term.doMatch then doMatchToCode doElem doElems else if k == `Lean.Parser.Term.doTry then doTryToCode doElem doElems else if k == `Lean.Parser.Term.doBreak then ensureInsideFor ensureEOS doElems return mkBreak ref else if k == `Lean.Parser.Term.doContinue then ensureInsideFor ensureEOS doElems return mkContinue ref else if k == `Lean.Parser.Term.doReturn then doReturnToCode doElem doElems else if k == `Lean.Parser.Term.doDbgTrace then return mkSeq doElem (← doSeqToCode doElems) else if k == `Lean.Parser.Term.doAssert then return mkSeq doElem (← doSeqToCode doElems) else if k == `Lean.Parser.Term.doNested then let nestedDoSeq := doElem[1] doSeqToCode (getDoSeqElems nestedDoSeq ++ doElems) else if k == `Lean.Parser.Term.doExpr then let term := doElem[0] if doElems.isEmpty then return mkTerminalAction term else return mkSeq term (← doSeqToCode doElems) else throwError! "unexpected do-element\n{doElem}" end def run (doStx : Syntax) (m : Syntax) : TermElabM CodeBlock := (doSeqToCode <| getDoSeqElems <| getDoSeq doStx).run { ref := doStx, m := m } end ToCodeBlock /- Create a synthetic metavariable `?m` and assign `m` to it. We use `?m` to refer to `m` when expanding the `do` notation. -/ private def mkMonadAlias (m : Expr) : TermElabM Syntax := do let result ← `(?m) let mType ← inferType m let mvar ← elabTerm result mType assignExprMVar mvar.mvarId! m pure result @[builtinTermElab «do»] def elabDo : TermElab := fun stx expectedType? => do tryPostponeIfNoneOrMVar expectedType? let bindInfo ← extractBind expectedType? let m ← mkMonadAlias bindInfo.m let codeBlock ← ToCodeBlock.run stx m let stxNew ← liftMacroM $ ToTerm.run codeBlock.code m trace[Elab.do]! stxNew withMacroExpansion stx stxNew $ elabTermEnsuringType stxNew bindInfo.expectedType end Do builtin_initialize registerTraceClass `Elab.do private def toDoElem (newKind : SyntaxNodeKind) : Macro := fun stx => do let stx := stx.setKind newKind let stxNew ← `(do $stx:doElem) return stxNew.copyInfo stx @[builtinMacro Lean.Parser.Term.termFor] def expandTermFor : Macro := toDoElem `Lean.Parser.Term.doFor @[builtinMacro Lean.Parser.Term.termTry] def expandTermTry : Macro := toDoElem `Lean.Parser.Term.doTry @[builtinMacro Lean.Parser.Term.termUnless] def expandTermUnless : Macro := toDoElem `Lean.Parser.Term.doUnless @[builtinMacro Lean.Parser.Term.termReturn] def expandTermReturn : Macro := toDoElem `Lean.Parser.Term.doReturn end Term end Elab end Lean
52cbc14282b4424a5c0185b22381eb0985ac1764
d9ed0fce1c218297bcba93e046cb4e79c83c3af8
/tests/lean/def_inaccessible_issue.lean
8d013155406f83f15e7241f7cccd0ebcc0142c94
[ "Apache-2.0" ]
permissive
leodemoura/lean_clone
005c63aa892a6492f2d4741ee3c2cb07a6be9d7f
cc077554b584d39bab55c360bc12a6fe7957afe6
refs/heads/master
1,610,506,475,484
1,482,348,354,000
1,482,348,543,000
77,091,586
0
0
null
null
null
null
UTF-8
Lean
false
false
382
lean
open nat set_option pp.binder_types true inductive bv : nat → Type | nil : bv 0 | cons : ∀ (n) (hd : bool) (tl : bv n), bv (n+1) open bv variable (f : bool → bool → bool) definition map2 : ∀ {n}, bv n → bv n → bv n | 0 nil nil := nil | (n+1) (cons .n b1 v1) (cons .n b2 v2) := cons n (f b1 b2) (map2 v1 v2) check map2.equations.eqn_2
1f72b462e5babf59599b19b9874fbe1d6c9d4519
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/lake/Lake/Config/Env.lean
09ad13c348cdcf2815dc9968aad1b55ee37d3fe8
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
4,605
lean
/- Copyright (c) 2022 Mac Malone. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mac Malone -/ import Lake.Util.NativeLib import Lake.Config.InstallPath open System /-! # Lake's Environment Definitions related to a Lake environment. A Lake environment is computed on Lake's startup from user-specified CLI options and the process environment. -/ namespace Lake /-- A Lake environment. -/ structure Env where /-- The Lake installation of the environment. -/ lake : LakeInstall /-- The Lean installation of the environment. -/ lean : LeanInstall /-- The initial Lean library search path of the environment (i.e., `LEAN_PATH`). -/ initLeanPath : SearchPath /-- The initial Lean source search path of the environment (i.e., `LEAN_SRC_PATH`). -/ initLeanSrcPath : SearchPath /-- The initial shared library search path of the environment. -/ initSharedLibPath : SearchPath /-- The initial binary search path of the environment (i.e., `PATH`). -/ initPath : SearchPath deriving Inhabited, Repr namespace Env /-- Compute an `Lake.Env` object from the given installs and set environment variables. -/ def compute (lake : LakeInstall) (lean : LeanInstall) : BaseIO Env := return { lake, lean, initLeanPath := ← getSearchPath "LEAN_PATH", initLeanSrcPath := ← getSearchPath "LEAN_SRC_PATH", initSharedLibPath := ← getSearchPath sharedLibPathEnvVar, initPath := ← getSearchPath "PATH" } /-- The Lean library search path of the environment (i.e., `LEAN_PATH`). Combines the initial path of the environment with that of the Lake installation. -/ def path (env : Env) : SearchPath := if env.lake.binDir = env.lean.binDir then env.lean.binDir :: env.initPath else env.lake.binDir :: env.lean.binDir :: env.initPath /- We include Lake's installation in the cases below to ensure that the Lake being used to build is available to the environment (and thus, e.g., the Lean server). Otherwise, it may fall back on whatever the default Lake instance is. -/ /-- The Lean library search path of the environment (i.e., `LEAN_PATH`). Combines the initial path of the environment with that of the Lake installation. -/ def leanPath (env : Env) : SearchPath := env.lake.libDir :: env.initLeanPath /-- The Lean source search path of the environment (i.e., `LEAN_SRC_PATH`). Combines the initial path of the environment with that of the Lake abd Lean installations. -/ def leanSrcPath (env : Env) : SearchPath := env.lake.srcDir :: env.initLeanSrcPath /-- The shared library search path of the environment. Combines the initial path of the environment with that of the Lean installation. -/ def sharedLibPath (env : Env) : SearchPath := env.lean.sharedLibPath ++ env.initSharedLibPath /-- Environment variable settings based only on the Lean and Lake installations. -/ def installVars (env : Env) : Array (String × Option String) := #[ ("LAKE", env.lake.lake.toString), ("LAKE_HOME", env.lake.home.toString), ("LEAN_SYSROOT", env.lean.sysroot.toString), ("LEAN_AR", env.lean.ar.toString), ("LEAN_CC", env.lean.leanCc?) ] /-- Environment variable settings for the `Lake.Env`. -/ def vars (env : Env) : Array (String × Option String) := let vars := env.installVars ++ #[ ("LEAN_PATH", some env.leanPath.toString), ("LEAN_SRC_PATH", some env.leanSrcPath.toString), ("PATH", some env.path.toString) ] if Platform.isWindows then vars else vars.push (sharedLibPathEnvVar, some <| env.sharedLibPath.toString) /-- The default search path the Lake executable uses when interpreting package configuration files. In order to use the Lean stdlib (e.g., `Init`), the executable needs the search path to include the directory with the stdlib's `.olean` files (e.g., from `<lean-sysroot>/lib/lean`). In order to use Lake's modules as well, the search path also needs to include Lake's `.olean` files (e.g., from `build`). While this can be done by having the user augment `LEAN_PATH` with the necessary directories, Lake also intelligently augments the initial search path with the `.olean` directories of the provided Lean and Lake installations. See `findInstall?` for more information on how Lake determines those directories. If everything is configured as expected, the user will not need to augment `LEAN_PATH`. Otherwise, they will need to provide Lake with more information (either through `LEAN_PATH` or through other options). -/ def leanSearchPath (env : Lake.Env) : SearchPath := env.lake.libDir :: env.lean.leanLibDir :: env.leanPath
b8351b8eab63a0dbf930b777e1ec9ed9193929a0
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/algebra/char_p.lean
7d0c177110b4e9fe9370c7ada58a2195e8d4a765
[ "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
14,171
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Kenny Lau, Joey van Langen, Casper Putz -/ import data.fintype.basic import data.nat.choose import data.int.modeq import algebra.module.basic import algebra.iterate_hom import group_theory.order_of_element import algebra.group.type_tags /-! # Characteristic of semirings -/ universes u v /-- The generator of the kernel of the unique homomorphism ℕ → α for a semiring α -/ class char_p (α : Type u) [semiring α] (p : ℕ) : Prop := (cast_eq_zero_iff [] : ∀ x:ℕ, (x:α) = 0 ↔ p ∣ x) theorem char_p.cast_eq_zero (α : Type u) [semiring α] (p : ℕ) [char_p α p] : (p:α) = 0 := (char_p.cast_eq_zero_iff α p p).2 (dvd_refl p) @[simp] lemma char_p.cast_card_eq_zero (R : Type*) [ring R] [fintype R] : (fintype.card R : R) = 0 := begin have : fintype.card R •ℕ (1 : R) = 0 := @pow_card_eq_one (multiplicative R) _ _ (multiplicative.of_add 1), simpa only [mul_one, nsmul_eq_mul] end lemma char_p.int_cast_eq_zero_iff (R : Type u) [ring R] (p : ℕ) [char_p R p] (a : ℤ) : (a : R) = 0 ↔ (p:ℤ) ∣ a := begin rcases lt_trichotomy a 0 with h|rfl|h, { rw [← neg_eq_zero, ← int.cast_neg, ← dvd_neg], lift -a to ℕ using neg_nonneg.mpr (le_of_lt h) with b, rw [int.cast_coe_nat, char_p.cast_eq_zero_iff R p, int.coe_nat_dvd] }, { simp only [int.cast_zero, eq_self_iff_true, dvd_zero] }, { lift a to ℕ using (le_of_lt h) with b, rw [int.cast_coe_nat, char_p.cast_eq_zero_iff R p, int.coe_nat_dvd] } end lemma char_p.int_coe_eq_int_coe_iff (R : Type*) [ring R] (p : ℕ) [char_p R p] (a b : ℤ) : (a : R) = (b : R) ↔ a ≡ b [ZMOD p] := by rw [eq_comm, ←sub_eq_zero, ←int.cast_sub, char_p.int_cast_eq_zero_iff R p, int.modeq.modeq_iff_dvd] theorem char_p.eq (α : Type u) [semiring α] {p q : ℕ} (c1 : char_p α p) (c2 : char_p α q) : p = q := nat.dvd_antisymm ((char_p.cast_eq_zero_iff α p q).1 (char_p.cast_eq_zero _ _)) ((char_p.cast_eq_zero_iff α q p).1 (char_p.cast_eq_zero _ _)) instance char_p.of_char_zero (α : Type u) [semiring α] [char_zero α] : char_p α 0 := ⟨λ x, by rw [zero_dvd_iff, ← nat.cast_zero, nat.cast_inj]⟩ theorem char_p.exists (α : Type u) [semiring α] : ∃ p, char_p α p := by letI := classical.dec_eq α; exact classical.by_cases (assume H : ∀ p:ℕ, (p:α) = 0 → p = 0, ⟨0, ⟨λ x, by rw [zero_dvd_iff]; exact ⟨H x, by rintro rfl; refl⟩⟩⟩) (λ H, ⟨nat.find (not_forall.1 H), ⟨λ x, ⟨λ H1, nat.dvd_of_mod_eq_zero (by_contradiction $ λ H2, nat.find_min (not_forall.1 H) (nat.mod_lt x $ nat.pos_of_ne_zero $ not_of_not_imp $ nat.find_spec (not_forall.1 H)) (not_imp_of_and_not ⟨by rwa [← nat.mod_add_div x (nat.find (not_forall.1 H)), nat.cast_add, nat.cast_mul, of_not_not (not_not_of_not_imp $ nat.find_spec (not_forall.1 H)), zero_mul, add_zero] at H1, H2⟩)), λ H1, by rw [← nat.mul_div_cancel' H1, nat.cast_mul, of_not_not (not_not_of_not_imp $ nat.find_spec (not_forall.1 H)), zero_mul]⟩⟩⟩) theorem char_p.exists_unique (α : Type u) [semiring α] : ∃! p, char_p α p := let ⟨c, H⟩ := char_p.exists α in ⟨c, H, λ y H2, char_p.eq α H2 H⟩ /-- Noncomputable function that outputs the unique characteristic of a semiring. -/ noncomputable def ring_char (α : Type u) [semiring α] : ℕ := classical.some (char_p.exists_unique α) theorem ring_char.spec (α : Type u) [semiring α] : ∀ x:ℕ, (x:α) = 0 ↔ ring_char α ∣ x := by letI := (classical.some_spec (char_p.exists_unique α)).1; unfold ring_char; exact char_p.cast_eq_zero_iff α (ring_char α) theorem ring_char.eq (α : Type u) [semiring α] {p : ℕ} (C : char_p α p) : p = ring_char α := (classical.some_spec (char_p.exists_unique α)).2 p C theorem add_pow_char_of_commute (R : Type u) [ring R] {p : ℕ} [fact p.prime] [char_p R p] (x y : R) (h : commute x y) : (x + y)^p = x^p + y^p := begin rw [commute.add_pow h, finset.sum_range_succ, nat.sub_self, pow_zero, nat.choose_self], rw [nat.cast_one, mul_one, mul_one, add_right_inj], convert finset.sum_eq_single 0 _ _, { simp }, swap, { intro h1, contrapose! h1, rw finset.mem_range, apply nat.prime.pos, assumption }, intros b h1 h2, suffices : (p.choose b : R) = 0, { rw this, simp }, rw char_p.cast_eq_zero_iff R p, apply nat.prime.dvd_choose_self, assumption', { omega }, rwa ← finset.mem_range end theorem add_pow_char_pow_of_commute (R : Type u) [ring R] {p : ℕ} [fact p.prime] [char_p R p] {n : ℕ} (x y : R) (h : commute x y) : (x + y) ^ (p ^ n) = x ^ (p ^ n) + y ^ (p ^ n) := begin induction n, { simp, }, rw [pow_succ', pow_mul, pow_mul, pow_mul, n_ih], apply add_pow_char_of_commute, apply commute.pow_pow h, end theorem sub_pow_char_of_commute (R : Type u) [ring R] {p : ℕ} [fact p.prime] [char_p R p] (x y : R) (h : commute x y) : (x - y)^p = x^p - y^p := begin rw [eq_sub_iff_add_eq, ← add_pow_char_of_commute _ _ _ (commute.sub_left h rfl)], simp, repeat {apply_instance}, end theorem sub_pow_char_pow_of_commute (R : Type u) [ring R] {p : ℕ} [fact p.prime] [char_p R p] {n : ℕ} (x y : R) (h : commute x y) : (x - y) ^ (p ^ n) = x ^ (p ^ n) - y ^ (p ^ n) := begin induction n, { simp, }, rw [pow_succ', pow_mul, pow_mul, pow_mul, n_ih], apply sub_pow_char_of_commute, apply commute.pow_pow h, end theorem add_pow_char (α : Type u) [comm_ring α] {p : ℕ} [fact p.prime] [char_p α p] (x y : α) : (x + y)^p = x^p + y^p := add_pow_char_of_commute _ _ _ (commute.all _ _) theorem add_pow_char_pow (R : Type u) [comm_ring R] {p : ℕ} [fact p.prime] [char_p R p] {n : ℕ} (x y : R) : (x + y) ^ (p ^ n) = x ^ (p ^ n) + y ^ (p ^ n) := add_pow_char_pow_of_commute _ _ _ (commute.all _ _) theorem sub_pow_char (α : Type u) [comm_ring α] {p : ℕ} [fact p.prime] [char_p α p] (x y : α) : (x - y)^p = x^p - y^p := sub_pow_char_of_commute _ _ _ (commute.all _ _) theorem sub_pow_char_pow (R : Type u) [comm_ring R] {p : ℕ} [fact p.prime] [char_p R p] {n : ℕ} (x y : R) : (x - y) ^ (p ^ n) = x ^ (p ^ n) - y ^ (p ^ n) := sub_pow_char_pow_of_commute _ _ _ (commute.all _ _) lemma eq_iff_modeq_int (R : Type*) [ring R] (p : ℕ) [char_p R p] (a b : ℤ) : (a : R) = b ↔ a ≡ b [ZMOD p] := by rw [eq_comm, ←sub_eq_zero, ←int.cast_sub, char_p.int_cast_eq_zero_iff R p, int.modeq.modeq_iff_dvd] lemma char_p.neg_one_ne_one (R : Type*) [ring R] (p : ℕ) [char_p R p] [fact (2 < p)] : (-1 : R) ≠ (1 : R) := begin suffices : (2 : R) ≠ 0, { symmetry, rw [ne.def, ← sub_eq_zero, sub_neg_eq_add], exact this }, assume h, rw [show (2 : R) = (2 : ℕ), by norm_cast] at h, have := (char_p.cast_eq_zero_iff R p 2).mp h, have := nat.le_of_dvd dec_trivial this, rw fact at *, linarith, end section frobenius variables (R : Type u) [comm_ring R] {S : Type v} [comm_ring S] (f : R →* S) (g : R →+* S) (p : ℕ) [fact p.prime] [char_p R p] [char_p S p] (x y : R) /-- The frobenius map that sends x to x^p -/ def frobenius : R →+* R := { to_fun := λ x, x^p, map_one' := one_pow p, map_mul' := λ x y, mul_pow x y p, map_zero' := zero_pow (lt_trans zero_lt_one ‹nat.prime p›.one_lt), map_add' := add_pow_char R } variable {R} theorem frobenius_def : frobenius R p x = x ^ p := rfl theorem iterate_frobenius (n : ℕ) : (frobenius R p)^[n] x = x ^ p ^ n := begin induction n, {simp}, rw [function.iterate_succ', pow_succ', pow_mul, function.comp_apply, frobenius_def, n_ih] end theorem frobenius_mul : frobenius R p (x * y) = frobenius R p x * frobenius R p y := (frobenius R p).map_mul x y theorem frobenius_one : frobenius R p 1 = 1 := one_pow _ variable {R} theorem monoid_hom.map_frobenius : f (frobenius R p x) = frobenius S p (f x) := f.map_pow x p theorem ring_hom.map_frobenius : g (frobenius R p x) = frobenius S p (g x) := g.map_pow x p theorem monoid_hom.map_iterate_frobenius (n : ℕ) : f (frobenius R p^[n] x) = (frobenius S p^[n] (f x)) := function.semiconj.iterate_right (f.map_frobenius p) n x theorem ring_hom.map_iterate_frobenius (n : ℕ) : g (frobenius R p^[n] x) = (frobenius S p^[n] (g x)) := g.to_monoid_hom.map_iterate_frobenius p x n theorem monoid_hom.iterate_map_frobenius (f : R →* R) (p : ℕ) [fact p.prime] [char_p R p] (n : ℕ) : f^[n] (frobenius R p x) = frobenius R p (f^[n] x) := f.iterate_map_pow _ _ _ theorem ring_hom.iterate_map_frobenius (f : R →+* R) (p : ℕ) [fact p.prime] [char_p R p] (n : ℕ) : f^[n] (frobenius R p x) = frobenius R p (f^[n] x) := f.iterate_map_pow _ _ _ variable (R) theorem frobenius_zero : frobenius R p 0 = 0 := (frobenius R p).map_zero theorem frobenius_add : frobenius R p (x + y) = frobenius R p x + frobenius R p y := (frobenius R p).map_add x y theorem frobenius_neg : frobenius R p (-x) = -frobenius R p x := (frobenius R p).map_neg x theorem frobenius_sub : frobenius R p (x - y) = frobenius R p x - frobenius R p y := (frobenius R p).map_sub x y theorem frobenius_nat_cast (n : ℕ) : frobenius R p n = n := (frobenius R p).map_nat_cast n end frobenius theorem frobenius_inj (α : Type u) [integral_domain α] (p : ℕ) [fact p.prime] [char_p α p] : function.injective (frobenius α p) := λ x h H, by { rw ← sub_eq_zero at H ⊢, rw ← frobenius_sub at H, exact pow_eq_zero H } namespace char_p section variables (α : Type u) [ring α] lemma char_p_to_char_zero [char_p α 0] : char_zero α := add_group.char_zero_of_inj_zero $ λ n h0, eq_zero_of_zero_dvd ((cast_eq_zero_iff α 0 n).mp h0) lemma cast_eq_mod (p : ℕ) [char_p α p] (k : ℕ) : (k : α) = (k % p : ℕ) := calc (k : α) = ↑(k % p + p * (k / p)) : by rw [nat.mod_add_div] ... = ↑(k % p) : by simp[cast_eq_zero] theorem char_ne_zero_of_fintype (p : ℕ) [hc : char_p α p] [fintype α] : p ≠ 0 := assume h : p = 0, have char_zero α := @char_p_to_char_zero α _ (h ▸ hc), absurd (@nat.cast_injective α _ _ this) (not_injective_infinite_fintype coe) end section integral_domain open nat variables (α : Type u) [integral_domain α] theorem char_ne_one (p : ℕ) [hc : char_p α p] : p ≠ 1 := assume hp : p = 1, have ( 1 : α) = 0, by simpa using (cast_eq_zero_iff α p 1).mpr (hp ▸ dvd_refl p), absurd this one_ne_zero theorem char_is_prime_of_two_le (p : ℕ) [hc : char_p α p] (hp : 2 ≤ p) : nat.prime p := suffices ∀d ∣ p, d = 1 ∨ d = p, from ⟨hp, this⟩, assume (d : ℕ) (hdvd : ∃ e, p = d * e), let ⟨e, hmul⟩ := hdvd in have (p : α) = 0, from (cast_eq_zero_iff α p p).mpr (dvd_refl p), have (d : α) * e = 0, from (@cast_mul α _ d e) ▸ (hmul ▸ this), or.elim (eq_zero_or_eq_zero_of_mul_eq_zero this) (assume hd : (d : α) = 0, have p ∣ d, from (cast_eq_zero_iff α p d).mp hd, show d = 1 ∨ d = p, from or.inr (dvd_antisymm ⟨e, hmul⟩ this)) (assume he : (e : α) = 0, have p ∣ e, from (cast_eq_zero_iff α p e).mp he, have e ∣ p, from dvd_of_mul_left_eq d (eq.symm hmul), have e = p, from dvd_antisymm ‹e ∣ p› ‹p ∣ e›, have h₀ : p > 0, from gt_of_ge_of_gt hp (nat.zero_lt_succ 1), have d * p = 1 * p, by rw ‹e = p› at hmul; rw [one_mul]; exact eq.symm hmul, show d = 1 ∨ d = p, from or.inl (eq_of_mul_eq_mul_right h₀ this)) theorem char_is_prime_or_zero (p : ℕ) [hc : char_p α p] : nat.prime p ∨ p = 0 := match p, hc with | 0, _ := or.inr rfl | 1, hc := absurd (eq.refl (1 : ℕ)) (@char_ne_one α _ (1 : ℕ) hc) | (m+2), hc := or.inl (@char_is_prime_of_two_le α _ (m+2) hc (nat.le_add_left 2 m)) end lemma char_is_prime_of_pos (p : ℕ) [h : fact (0 < p)] [char_p α p] : fact p.prime := (char_p.char_is_prime_or_zero α _).resolve_right (nat.pos_iff_ne_zero.1 h) theorem char_is_prime [fintype α] (p : ℕ) [char_p α p] : p.prime := or.resolve_right (char_is_prime_or_zero α p) (char_ne_zero_of_fintype α p) end integral_domain section char_one variables {R : Type*} @[priority 100] -- see Note [lower instance priority] instance [semiring R] [char_p R 1] : subsingleton R := subsingleton.intro $ suffices ∀ (r : R), r = 0, from assume a b, show a = b, by rw [this a, this b], assume r, calc r = 1 * r : by rw one_mul ... = (1 : ℕ) * r : by rw nat.cast_one ... = 0 * r : by rw char_p.cast_eq_zero ... = 0 : by rw zero_mul lemma false_of_nontrivial_of_char_one [semiring R] [nontrivial R] [char_p R 1] : false := false_of_nontrivial_of_subsingleton R lemma ring_char_ne_one [semiring R] [nontrivial R] : ring_char R ≠ 1 := by { intros h, apply @zero_ne_one R, symmetry, rw [←nat.cast_one, ring_char.spec, h], } lemma nontrivial_of_char_ne_one {v : ℕ} (hv : v ≠ 1) {R : Type*} [semiring R] [hr : char_p R v] : nontrivial R := ⟨⟨(1 : ℕ), 0, λ h, hv $ by rwa [char_p.cast_eq_zero_iff _ v, nat.dvd_one] at h; assumption ⟩⟩ end char_one end char_p section variables (n : ℕ) (R : Type*) [comm_ring R] [fintype R] lemma char_p_of_ne_zero (hn : fintype.card R = n) (hR : ∀ i < n, (i : R) = 0 → i = 0) : char_p R n := { cast_eq_zero_iff := begin have H : (n : R) = 0, by { rw [← hn, char_p.cast_card_eq_zero] }, intro k, split, { intro h, rw [← nat.mod_add_div k n, nat.cast_add, nat.cast_mul, H, zero_mul, add_zero] at h, rw nat.dvd_iff_mod_eq_zero, apply hR _ (nat.mod_lt _ _) h, rw [← hn, gt, fintype.card_pos_iff], exact ⟨0⟩, }, { rintro ⟨k, rfl⟩, rw [nat.cast_mul, H, zero_mul] } end } lemma char_p_of_prime_pow_injective (p : ℕ) [hp : fact p.prime] (n : ℕ) (hn : fintype.card R = p ^ n) (hR : ∀ i ≤ n, (p ^ i : R) = 0 → i = n) : char_p R (p ^ n) := begin obtain ⟨c, hc⟩ := char_p.exists R, resetI, have hcpn : c ∣ p ^ n, { rw [← char_p.cast_eq_zero_iff R c, ← hn, char_p.cast_card_eq_zero], }, obtain ⟨i, hi, hc⟩ : ∃ i ≤ n, c = p ^ i, by rwa nat.dvd_prime_pow hp at hcpn, obtain rfl : i = n, { apply hR i hi, rw [← nat.cast_pow, ← hc, char_p.cast_eq_zero] }, rwa ← hc end end
d00d4417b4907b250b8ce32d9df0fcb97a35bf0d
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/run/priority_test2.lean
46a582ac462d4e96d8c519d06c370e314a1a9502
[ "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
748
lean
open nat structure [class] foo := (a : nat) (b : nat) attribute [instance, priority std.priority.default-2] definition i1 : foo := foo.mk 1 1 example : foo.a = 1 := rfl attribute [instance, priority std.priority.default-1] definition i2 : foo := foo.mk 2 2 example : foo.a = 2 := rfl attribute [instance] definition i3 : foo := foo.mk 3 3 example : foo.a = 3 := rfl attribute [instance, priority std.priority.default-1] definition i4 : foo := foo.mk 4 4 example : foo.a = 3 := rfl attribute i4 [instance, priority std.priority.default+2] example : foo.a = 4 := rfl attribute i1 [instance, priority std.priority.default+3] example : foo.a = 1 := rfl attribute i2 [instance, priority std.priority.default+4] example : foo.a = 2 := rfl
e0fc8251db22c239ab0c72891d869db86ade09f7
367134ba5a65885e863bdc4507601606690974c1
/src/data/equiv/ring_aut.lean
e779fbcd1268d5a688a7d1eb46f180be3a5ee827
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
1,992
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Callum Sutton, Yury Kudryashov -/ import data.equiv.ring import data.equiv.mul_add_aut /-! # Ring automorphisms This file defines the automorphism group structure on `ring_aut R := ring_equiv R R`. ## Implementation notes The definition of multiplication in the automorphism group agrees with function composition, multiplication in `equiv.perm`, and multiplication in `category_theory.End`, but not with `category_theory.comp`. This file is kept separate from `data/equiv/ring` so that `group_theory.perm` is free to use equivalences (and other files that use them) before the group structure is defined. ## Tags ring_aut -/ /-- The group of ring automorphisms. -/ @[reducible] def ring_aut (R : Type*) [has_mul R] [has_add R] := ring_equiv R R namespace ring_aut variables (R : Type*) [has_mul R] [has_add R] /-- The group operation on automorphisms of a ring is defined by `λ g h, ring_equiv.trans h g`. This means that multiplication agrees with composition, `(g*h)(x) = g (h x)`. -/ instance : group (ring_aut R) := by refine_struct { mul := λ g h, ring_equiv.trans h g, one := ring_equiv.refl R, inv := ring_equiv.symm, div := _ }; intros; ext; try { refl }; apply equiv.left_inv instance : inhabited (ring_aut R) := ⟨1⟩ /-- Monoid homomorphism from ring automorphisms to additive automorphisms. -/ def to_add_aut : ring_aut R →* add_aut R := by refine_struct { to_fun := ring_equiv.to_add_equiv }; intros; refl /-- Monoid homomorphism from ring automorphisms to multiplicative automorphisms. -/ def to_mul_aut : ring_aut R →* mul_aut R := by refine_struct { to_fun := ring_equiv.to_mul_equiv }; intros; refl /-- Monoid homomorphism from ring automorphisms to permutations. -/ def to_perm : ring_aut R →* equiv.perm R := by refine_struct { to_fun := ring_equiv.to_equiv }; intros; refl end ring_aut
d8d20ba9fdb2c9198c987da6b78f8b0ebc76a35f
4bcaca5dc83d49803f72b7b5920b75b6e7d9de2d
/src/Lean/Elab/BuiltinTerm.lean
6157a9ecfc83054a124dc38642ed92c3dc7d7a6e
[ "Apache-2.0" ]
permissive
subfish-zhou/leanprover-zh_CN.github.io
30b9fba9bd790720bd95764e61ae796697d2f603
8b2985d4a3d458ceda9361ac454c28168d920d3f
refs/heads/master
1,689,709,967,820
1,632,503,056,000
1,632,503,056,000
409,962,097
1
0
null
null
null
null
UTF-8
Lean
false
false
8,537
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.Elab.Term namespace Lean.Elab.Term open Meta @[builtinTermElab «prop»] def elabProp : TermElab := fun _ _ => return mkSort levelZero private def elabOptLevel (stx : Syntax) : TermElabM Level := if stx.isNone then pure levelZero else elabLevel stx[0] @[builtinTermElab «sort»] def elabSort : TermElab := fun stx _ => return mkSort (← elabOptLevel stx[1]) @[builtinTermElab «type»] def elabTypeStx : TermElab := fun stx _ => return mkSort (mkLevelSucc (← elabOptLevel stx[1])) /- the method `resolveName` adds a completion point for it using the given expected type. Thus, we propagate the expected type if `stx[0]` is an identifier. It doesn't "hurt" if the identifier can be resolved because the expected type is not used in this case. Recall that if the name resolution fails a synthetic sorry is returned.-/ @[builtinTermElab «pipeCompletion»] def elabPipeCompletion : TermElab := fun stx expectedType? => do let e ← elabTerm stx[0] none unless e.isSorry do addDotCompletionInfo stx e expectedType? throwErrorAt stx[1] "invalid field notation, identifier or numeral expected" @[builtinTermElab «completion»] def elabCompletion : TermElab := fun stx expectedType? => do /- `ident.` is ambiguous in Lean, we may try to be completing a declaration name or access a "field". -/ if stx[0].isIdent then /- If we can elaborate the identifier successfully, we assume it a dot-completion. Otherwise, we treat it as identifier completion with a dangling `.`. Recall that the server falls back to identifier completion when dot-completion fails. -/ let s ← saveState try let e ← elabTerm stx[0] none addDotCompletionInfo stx e expectedType? catch _ => s.restore addCompletionInfo <| CompletionInfo.id stx stx[0].getId (danglingDot := true) (← getLCtx) expectedType? throwErrorAt stx[1] "invalid field notation, identifier or numeral expected" else elabPipeCompletion stx expectedType? @[builtinTermElab «hole»] def elabHole : TermElab := fun stx expectedType? => do let mvar ← mkFreshExprMVar expectedType? registerMVarErrorHoleInfo mvar.mvarId! stx pure mvar @[builtinTermElab «syntheticHole»] def elabSyntheticHole : TermElab := fun stx expectedType? => do let arg := stx[1] let userName := if arg.isIdent then arg.getId else Name.anonymous let mkNewHole : Unit → TermElabM Expr := fun _ => do let mvar ← mkFreshExprMVar expectedType? MetavarKind.syntheticOpaque userName registerMVarErrorHoleInfo mvar.mvarId! stx pure mvar if userName.isAnonymous then mkNewHole () else let mctx ← getMCtx match mctx.findUserName? userName with | none => mkNewHole () | some mvarId => let mvar := mkMVar mvarId let mvarDecl ← getMVarDecl mvarId let lctx ← getLCtx if mvarDecl.lctx.isSubPrefixOf lctx then pure mvar else match mctx.getExprAssignment? mvarId with | some val => let val ← instantiateMVars val if mctx.isWellFormed lctx val then pure val else withLCtx mvarDecl.lctx mvarDecl.localInstances do throwError "synthetic hole has already been defined and assigned to value incompatible with the current context{indentExpr val}" | none => if mctx.isDelayedAssigned mvarId then -- We can try to improve this case if needed. throwError "synthetic hole has already beend defined and delayed assigned with an incompatible local context" else if lctx.isSubPrefixOf mvarDecl.lctx then let mvarNew ← mkNewHole () modifyMCtx fun mctx => mctx.assignExpr mvarId mvarNew pure mvarNew else throwError "synthetic hole has already been defined with an incompatible local context" private def mkTacticMVar (type : Expr) (tacticCode : Syntax) : TermElabM Expr := do let mvar ← mkFreshExprMVar type MetavarKind.syntheticOpaque let mvarId := mvar.mvarId! let ref ← getRef let declName? ← getDeclName? registerSyntheticMVar ref mvarId <| SyntheticMVarKind.tactic tacticCode (← saveContext) return mvar @[builtinTermElab byTactic] def elabByTactic : TermElab := fun stx expectedType? => match expectedType? with | some expectedType => mkTacticMVar expectedType stx | none => throwError ("invalid 'by' tactic, expected type has not been provided") @[builtinTermElab noImplicitLambda] def elabNoImplicitLambda : TermElab := fun stx expectedType? => elabTerm stx[1] (mkNoImplicitLambdaAnnotation <$> expectedType?) @[builtinTermElab cdot] def elabBadCDot : TermElab := fun stx _ => throwError "invalid occurrence of `·` notation, it must be surrounded by parentheses (e.g. `(· + 1)`)" @[builtinTermElab strLit] def elabStrLit : TermElab := fun stx _ => do match stx.isStrLit? with | some val => pure $ mkStrLit val | none => throwIllFormedSyntax private def mkFreshTypeMVarFor (expectedType? : Option Expr) : TermElabM Expr := do let typeMVar ← mkFreshTypeMVar MetavarKind.synthetic match expectedType? with | some expectedType => discard <| isDefEq expectedType typeMVar | _ => pure () return typeMVar @[builtinTermElab numLit] def elabNumLit : TermElab := fun stx expectedType? => do let val ← match stx.isNatLit? with | some val => pure val | none => throwIllFormedSyntax let typeMVar ← mkFreshTypeMVarFor expectedType? let u ← getDecLevel typeMVar let mvar ← mkInstMVar (mkApp2 (Lean.mkConst ``OfNat [u]) typeMVar (mkRawNatLit val)) let r := mkApp3 (Lean.mkConst ``OfNat.ofNat [u]) typeMVar (mkRawNatLit val) mvar registerMVarErrorImplicitArgInfo mvar.mvarId! stx r return r @[builtinTermElab rawNatLit] def elabRawNatLit : TermElab := fun stx expectedType? => do match stx[1].isNatLit? with | some val => return mkRawNatLit val | none => throwIllFormedSyntax @[builtinTermElab scientificLit] def elabScientificLit : TermElab := fun stx expectedType? => do match stx.isScientificLit? with | none => throwIllFormedSyntax | some (m, sign, e) => let typeMVar ← mkFreshTypeMVarFor expectedType? let u ← getDecLevel typeMVar let mvar ← mkInstMVar (mkApp (Lean.mkConst ``OfScientific [u]) typeMVar) let r := mkApp5 (Lean.mkConst ``OfScientific.ofScientific [u]) typeMVar mvar (mkRawNatLit m) (toExpr sign) (mkRawNatLit e) registerMVarErrorImplicitArgInfo mvar.mvarId! stx r return r @[builtinTermElab charLit] def elabCharLit : TermElab := fun stx _ => do match stx.isCharLit? with | some val => return mkApp (Lean.mkConst ``Char.ofNat) (mkRawNatLit val.toNat) | none => throwIllFormedSyntax @[builtinTermElab quotedName] def elabQuotedName : TermElab := fun stx _ => match stx[0].isNameLit? with | some val => pure $ toExpr val | none => throwIllFormedSyntax @[builtinTermElab doubleQuotedName] def elabDoubleQuotedName : TermElab := fun stx _ => do toExpr (← resolveGlobalConstNoOverloadWithInfo stx[2]) @[builtinTermElab typeOf] def elabTypeOf : TermElab := fun stx _ => do inferType (← elabTerm stx[1] none) @[builtinTermElab ensureTypeOf] def elabEnsureTypeOf : TermElab := fun stx expectedType? => match stx[2].isStrLit? with | none => throwIllFormedSyntax | some msg => do let refTerm ← elabTerm stx[1] none let refTermType ← inferType refTerm elabTermEnsuringType stx[3] refTermType (errorMsgHeader? := msg) @[builtinTermElab ensureExpectedType] def elabEnsureExpectedType : TermElab := fun stx expectedType? => match stx[1].isStrLit? with | none => throwIllFormedSyntax | some msg => elabTermEnsuringType stx[2] expectedType? (errorMsgHeader? := msg) @[builtinTermElab «open»] def elabOpen : TermElab := fun stx expectedType? => do try pushScope let openDecls ← elabOpenDecl stx[1] withTheReader Core.Context (fun ctx => { ctx with openDecls := openDecls }) do elabTerm stx[3] expectedType? finally popScope @[builtinTermElab «set_option»] def elabSetOption : TermElab := fun stx expectedType? => do let options ← Elab.elabSetOption stx[1] stx[2] withTheReader Core.Context (fun ctx => { ctx with maxRecDepth := maxRecDepth.get options, options := options }) do elabTerm stx[4] expectedType? end Lean.Elab.Term
a674a8be11ce8931442c11333b88c36ca54ddb00
6dc0c8ce7a76229dd81e73ed4474f15f88a9e294
/tests/lean/simpcfg.lean
7d21c2c7e3c5c4944bf9983596577297ba1240ba
[ "Apache-2.0" ]
permissive
williamdemeo/lean4
72161c58fe65c3ad955d6a3050bb7d37c04c0d54
6d00fcf1d6d873e195f9220c668ef9c58e9c4a35
refs/heads/master
1,678,305,356,877
1,614,708,995,000
1,614,708,995,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
173
lean
theorem ex6 (f : Nat → Nat) (x y z : Nat) (h : (x, z).1 = (fun x => x) y) : f x = f y := by simp { beta := false } at h traceState simp at h traceState simp [h]
76e3e57db0675748bc31fcf7a7053893c8c8b050
8f67b34bba98f894155dedf263bc8d61c31e89cd
/2ltt/inverse.lean
a75071de359e3d84ccfe8b0520da4b659e02847b
[]
no_license
5HT/two-level
3b1523db242cba819681b862fbc8f490d9571a66
370f5a91311db3b463b10a31891370721e2476e2
refs/heads/master
1,648,254,367,420
1,576,269,550,000
1,576,269,550,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
7,285
lean
-- inverse categories import algebra.category data.nat import algebra.category.constructions import .fibrant .finite import open nat open nat.le functor open category category.ops eq.ops definition nat_cat_op [instance] : category ℕ := ⦃ category, hom := λ a b, a ≥ b, comp := λ a b c, @nat.le_trans c b a, ID := nat.le_refl, assoc := λ a b c d h g f, eq.refl _, id_left := λ a b f, eq.refl _, id_right := λ a b f, eq.refl _ ⦄ definition ℕop := Mk nat_cat_op lemma hom_ℕop_id {C : Category} {x : ℕop} {f : x ⟶ x} : f = id := rfl -- ref:def:inverse-category -- Definition 4.1 namespace invcat open sigma.ops iff definition id_reflect {C D: Category} (φ : C ⇒ D) := Π ⦃x y : C⦄ (f : x ⟶ y), (Σ (q : φ x = φ y), q ▹ φ f = id) → Σ (p : x = y), p ▹ f = id definition id_reflect' {C D: Category} := Π (φ : C ⇒ D) ⦃x y : C⦄ (f : x ⟶ y), (Σ (q : φ x = φ y), q ▹ φ f = id) → Σ (p : x = y), p ▹ f = id definition transport {A : Type} := @eq.rec_on A notation x ≡ y := eq x y definition id_reflect'' {C D: Category} := Π (φ : C ⇒ D) {x y : C} (f : hom x y), (Σ (q : φ y ≡ φ x), transport q (φ f) ≡ id) → Σ (p : y ≡ x), @transport _ _ _ _ p f ≡ id -- definition if "refect identity" property specific to ℕop. -- Though this definition doesn't require for φ(f) to be an identity, we will show that -- id_reflect(C,ℕop) and id_reflect_ℕop(C) are logically equivalent definition id_reflect_ℕop {C : Category} (φ : C ⇒ ℕop) := Π ⦃x y : C⦄ (f : x ⟶ y), φ x = φ y → (Σ (p : x = y), p ▹ f = id) definition id_reflect_ℕop' {C : Category} (φ : C ⇒ ℕop) := Π {x y : C} (f : x ⟶ y), φ x ≡ φ y → (Σ (p : x ≡ y), transport p f ≡ id) -- have to pack functor with the property that it reflects identities, -- because functor itself is not a type class structure has_idreflect [class] (C D : Category) := (φ : C ⇒ D) (reflecting_id : id_reflect φ) section id_reflect_ℕop_iff -- showing that id_reflect_ℕop φ ↔ id_reflect φ -- NOTE: (Danil) I couldn't find instance of iff for Type definition map_id_reflect_ℕop {C : Category} (φ : C ⇒ ℕop): id_reflect_ℕop φ → id_reflect φ := begin intros id_r_ℕop x y f s, cases s with [q, Heq], cases id_r_ℕop f q with [p₁, p₂], existsi p₁, cases p₁, esimp at *, apply p₂ end definition map_ℕop_id_reflect {C : Category} (φ : C ⇒ ℕop): id_reflect φ → id_reflect_ℕop φ := begin intros id_r x y f q, -- here we use the fact that any morphism x ⟶ x in ℕop only can be an identity morphism have f_is_id : q ▹ morphism φ f = id, from rfl, cases id_r f ⟨q,f_is_id⟩ with [p₁, p₂], existsi p₁, cases p₁, esimp at *, apply p₂ end end id_reflect_ℕop_iff structure invcat [class] (C : Category) := (reflecting_id_ℕop : has_idreflect C ℕop) end invcat -- some facts about inverse categories section invcat_facts open invcat function sigma.ops definition endomorphism_is_id {C : Category} [invC : invcat C] {c : C} (f : c ⟶ c) : f = id := begin cases invC with H, cases H with [φ, id_r], apply (id_r f ⟨rfl,rfl⟩).2 end lemma idreflect_inj_hom {C : Category} {x y : C} [idr : has_idreflect C ℕop] (f : x ⟶ y) : (has_idreflect.φ C ℕop) x = (has_idreflect.φ C ℕop) y → x = y := begin cases idr with [φ, idr_φ], esimp, intro H, cases (idr_φ f ⟨H,rfl⟩), assumption end definition has_le_ℕop [instance] : has_le ℕop := nat_has_le definition has_lt_ℕop [instance] : has_lt ℕop := nat_has_lt definition strict_order_ℕop [instance] : strict_order ℕop := strict_order.mk nat.lt begin intros, refine (@lt.irrefl ℕ _ a a_1) end begin intros a b c, apply nat.lt_trans end definition weak_order_ℕop [instance] : weak_order ℕop := weak_order.mk nat.le (@le.refl ℕ _) (λ a b c, @nat.le_trans a b c) (λ a b, nat.le_antisymm) definition no_incoming_non_id_arrows {C : Category.{1 1}} (z : C) {φ : C ⇒ ℕop} {max_rank : ∀ y, φ y ≤ φ z} {reflecting_id : id_reflect φ} : ¬ ∃ y : C, ∃ (f : y ⟶ z), y ≠ z := begin intro H, cases H with [y, s], cases s with [f, y_ne_z], unfold id_reflect at *, have H : φ y ≥ φ z, from φ f, cases reflecting_id f ⟨le.antisymm (max_rank y) H, rfl⟩ with [p, Heq], apply y_ne_z, assumption end end invcat_facts open invcat open unit definition triv_funct : Category_one ⇒ ℕop := functor.mk (λ (x : unit), zero) (λ a b p, id) (λa, eq.refl _) (λa b c p q, eq.refl _) definition triv_funct_id_reflect [instance] : has_idreflect Category_one ℕop := has_idreflect.mk triv_funct begin intros x y f, cases x, cases y, intro p, existsi (eq.refl _), cases f, reflexivity end definition triv_cat_inverse [instance] : invcat Category_one := invcat.mk _ structure subcat_obj (C : Category) (p : objects C → Prop) := (obj : objects C) (prop : p obj) open subcat_obj function attribute subcat_obj.obj [coercion] section subcategory variables {C : Category.{1 1}} {D : Category} definition subcat [instance] (C : Category) (p : C → Prop) : category (subcat_obj C p) := ⦃ category, hom := λ (a b : subcat_obj C p), obj a ⟶ obj b, comp := λ a b c, @comp _ _ _ _ _, ID := λ (a : subcat_obj C p), ID (obj a), assoc := λ a b c d h g f, assoc h g f, id_left := λ a b f, id_left f, id_right := λ a b f, id_right f ⦄ definition Subcat (C : Category) (p : C → Prop) : Category := Mk (subcat C p) -- A subcategory of [C] with one object removed. -- We also refer to this category as C' definition C_without_z {C : Category.{1 1}}(z : C) : Category := Mk (subcat C (λ c, c ≠ z)) -- use apply tactic, as it allows to infer correct implicits definition Functor_from_C' [reducible] (z : C) (X : C ⇒ D) : C_without_z z ⇒ D := ⦃ functor, object := λ ob, X (obj ob), morphism := λ a b f, by apply X f, respect_id := λ a, by apply respect_id X (obj a), respect_comp := λ a b c g f, by apply @respect_comp _ _ X (obj a) (obj b) (obj c) _ _ ⦄ open eq.ops function definition C_without_z_invcat [instance] (z : C) [invC : invcat C] : invcat (C_without_z z) := begin unfold C_without_z, cases invC, cases reflecting_id_ℕop, refine invcat.mk (has_idreflect.mk _ _), apply Functor_from_C' z φ, intros x y f p, cases reflecting_id f p with [p1, p2], refine ⟨_,_⟩, { cases x, cases y, congruence, apply p1 }, { cases x, cases y, esimp at *, induction p1 using eq.drec, esimp at *, apply p2} end open equiv equiv.ops sigma sigma.ops definition C_without_z_sigma_equiv {C : Category} (z : C) : C_without_z z ≃ Σ (c : C), c ≠ z := equiv.mk (λ c', ⟨obj c', prop c'⟩) (λc, mk c.1 c.2) begin intros, cases x, esimp, end begin intros, cases x, esimp end definition C_without_z_is_obj_finite [instance] {n : ℕ} (z : C) [φ : objects C ≃ fin (nat.succ n)] : objects (C_without_z z) ≃ fin n := (fincat.fincat_ob_remove_fin_equiv z) ∘ (C_without_z_sigma_equiv z) end subcategory
7590f309e182847dac042d0c5652dc4c47c29501
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/topology/algebra/constructions.lean
f2d7c3657c7c9934173922c05d9bdb6f41948848
[ "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,601
lean
/- Copyright (c) 2021 Nicolò Cavalleri. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Nicolò Cavalleri -/ import topology.homeomorph /-! # Topological space structure on the opposite monoid and on the units group In this file we define `topological_space` structure on `Mᵐᵒᵖ`, `Mᵃᵒᵖ`, `Mˣ`, and `add_units M`. This file does not import definitions of a topological monoid and/or a continuous multiplicative action, so we postpone the proofs of `has_continuous_mul Mᵐᵒᵖ` etc till we have these definitions. ## Tags topological space, opposite monoid, units -/ variables {M X : Type*} open filter open_locale topological_space namespace mul_opposite /-- Put the same topological space structure on the opposite monoid as on the original space. -/ @[to_additive "Put the same topological space structure on the opposite monoid as on the original space."] instance [topological_space M] : topological_space Mᵐᵒᵖ := topological_space.induced (unop : Mᵐᵒᵖ → M) ‹_› variables [topological_space M] @[continuity, to_additive] lemma continuous_unop : continuous (unop : Mᵐᵒᵖ → M) := continuous_induced_dom @[continuity, to_additive] lemma continuous_op : continuous (op : M → Mᵐᵒᵖ) := continuous_induced_rng.2 continuous_id /-- `mul_opposite.op` as a homeomorphism. -/ @[to_additive "`add_opposite.op` as a homeomorphism.", simps] def op_homeomorph : M ≃ₜ Mᵐᵒᵖ := { to_equiv := op_equiv, continuous_to_fun := continuous_op, continuous_inv_fun := continuous_unop } @[to_additive] instance [t2_space M] : t2_space Mᵐᵒᵖ := op_homeomorph.symm.embedding.t2_space @[simp, to_additive] lemma map_op_nhds (x : M) : map (op : M → Mᵐᵒᵖ) (𝓝 x) = 𝓝 (op x) := op_homeomorph.map_nhds_eq x @[simp, to_additive] lemma map_unop_nhds (x : Mᵐᵒᵖ) : map (unop : Mᵐᵒᵖ → M) (𝓝 x) = 𝓝 (unop x) := op_homeomorph.symm.map_nhds_eq x @[simp, to_additive] lemma comap_op_nhds (x : Mᵐᵒᵖ) : comap (op : M → Mᵐᵒᵖ) (𝓝 x) = 𝓝 (unop x) := op_homeomorph.comap_nhds_eq x @[simp, to_additive] lemma comap_unop_nhds (x : M) : comap (unop : Mᵐᵒᵖ → M) (𝓝 x) = 𝓝 (op x) := op_homeomorph.symm.comap_nhds_eq x end mul_opposite namespace units open mul_opposite variables [topological_space M] [monoid M] [topological_space X] /-- The units of a monoid are equipped with a topology, via the embedding into `M × M`. -/ @[to_additive "The additive units of a monoid are equipped with a topology, via the embedding into `M × M`."] instance : topological_space Mˣ := prod.topological_space.induced (embed_product M) @[to_additive] lemma inducing_embed_product : inducing (embed_product M) := ⟨rfl⟩ @[to_additive] lemma embedding_embed_product : embedding (embed_product M) := ⟨inducing_embed_product, embed_product_injective M⟩ @[to_additive] lemma continuous_embed_product : continuous (embed_product M) := continuous_induced_dom @[to_additive] lemma continuous_coe : continuous (coe : Mˣ → M) := (@continuous_embed_product M _ _).fst @[to_additive] protected lemma continuous_iff {f : X → Mˣ} : continuous f ↔ continuous (coe ∘ f : X → M) ∧ continuous (λ x, ↑(f x)⁻¹ : X → M) := by simp only [inducing_embed_product.continuous_iff, embed_product_apply, (∘), continuous_prod_mk, op_homeomorph.symm.inducing.continuous_iff, op_homeomorph_symm_apply, unop_op] @[to_additive] lemma continuous_coe_inv : continuous (λ u, ↑u⁻¹ : Mˣ → M) := (units.continuous_iff.1 continuous_id).2 end units
5266232fac768aa8568ae60af5d5e513c92f3052
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/algebra/group_power/basic.lean
ebb42154f9ce2d0cbbd386f8e00d4a5e67932a30
[ "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
17,356
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 -/ import algebra.ordered_ring import tactic.monotonicity.basic import group_theory.group_action.defs /-! # Power operations on monoids and groups The power operation on monoids and groups. We separate this from group, because it depends on `ℕ`, which in turn depends on other parts of algebra. This module contains the definitions of `monoid.pow` and `group.pow` and their additive counterparts `nsmul` and `gsmul`, along with a few lemmas. Further lemmas can be found in `algebra.group_power.lemmas`. ## Notation The class `has_pow α β` provides the notation `a^b` for powers. We define instances of `has_pow M ℕ`, for monoids `M`, and `has_pow G ℤ` for groups `G`. Scalar multiplication by naturals and integers is handled by the `•` (`has_scalar.smul`) notation defined elsewhere. ## Implementation details We adopt the convention that `0^0 = 1`. This module provides the instance `has_pow ℕ ℕ` (via `monoid.has_pow`) and is imported by `data.nat.basic`, so it has to live low in the import hierarchy. Not all of its imports are needed yet; the intent is to move more lemmas here from `.lemmas` so that they are available in `data.nat.basic`, and the imports will be required then. -/ universes u v w x y z u₁ u₂ variables {M : Type u} {N : Type v} {G : Type w} {H : Type x} {A : Type y} {B : Type z} {R : Type u₁} {S : Type u₂} instance monoid.has_pow [monoid M] : has_pow M ℕ := ⟨λ x n, npow n x⟩ instance add_monoid.has_scalar_nat [add_monoid M] : has_scalar ℕ M := ⟨nsmul⟩ attribute [to_additive add_monoid.has_scalar_nat] monoid.has_pow instance div_inv_monoid.has_pow [div_inv_monoid M] : has_pow M ℤ := ⟨λ x n, gpow n x⟩ instance sub_neg_monoid.has_scalar_int [sub_neg_monoid M] : has_scalar ℤ M := ⟨gsmul⟩ attribute [to_additive sub_neg_monoid.has_scalar_int] div_inv_monoid.has_pow @[simp, to_additive nsmul_eq_smul] lemma npow_eq_pow {M : Type*} [monoid M] (n : ℕ) (x : M) : npow n x = x^n := rfl @[simp, to_additive gsmul_eq_smul] lemma gpow_eq_pow {M : Type*} [div_inv_monoid M] (n : ℤ) (x : M) : gpow n x = x^n := rfl /-! ### Commutativity First we prove some facts about `semiconj_by` and `commute`. They do not require any theory about `pow` and/or `nsmul` and will be useful later in this file. -/ namespace semiconj_by variables [monoid M] attribute [to_additive add_monoid.nsmul_zero'] monoid.npow_zero' @[simp, to_additive] lemma pow_right {a x y : M} (h : semiconj_by a x y) (n : ℕ) : semiconj_by a (x^n) (y^n) := begin induction n with n ih, { simp [← npow_eq_pow, monoid.npow_zero'], }, { simp only [← npow_eq_pow, nat.succ_eq_add_one, npow_one, npow_add] at ⊢ ih, exact ih.mul_right h } end end semiconj_by namespace commute variables [monoid M] {a b : M} @[simp, to_additive] theorem pow_right (h : commute a b) (n : ℕ) : commute a (b ^ n) := h.pow_right n @[simp, to_additive] theorem pow_left (h : commute a b) (n : ℕ) : commute (a ^ n) b := (h.symm.pow_right n).symm @[simp, to_additive] theorem pow_pow (h : commute a b) (m n : ℕ) : commute (a ^ m) (b ^ n) := (h.pow_left m).pow_right n @[simp, to_additive] theorem self_pow (a : M) (n : ℕ) : commute a (a ^ n) := (commute.refl a).pow_right n @[simp, to_additive] theorem pow_self (a : M) (n : ℕ) : commute (a ^ n) a := (commute.refl a).pow_left n @[simp, to_additive] theorem pow_pow_self (a : M) (m n : ℕ) : commute (a ^ m) (a ^ n) := (commute.refl a).pow_pow m n end commute section monoid variables [monoid M] [monoid N] [add_monoid A] [add_monoid B] -- the attributes are intentionally out of order. `zero_smul` proves `zero_nsmul`. @[to_additive zero_nsmul, simp] theorem pow_zero (a : M) : a^0 = 1 := monoid.npow_zero' _ @[to_additive succ_nsmul] theorem pow_succ (a : M) (n : ℕ) : a^(n+1) = a * a^n := by rw [← npow_eq_pow, nat.add_comm, npow_add, npow_one, npow_eq_pow] /-- Note that most of the lemmas about powers of two refer to it as `sq`. -/ @[to_additive two_nsmul] theorem pow_two (a : M) : a^2 = a * a := by rw [← npow_eq_pow, show 2 = 1 + 1, by refl, npow_add, npow_one] alias pow_two ← sq @[to_additive nsmul_add_comm'] theorem pow_mul_comm' (a : M) (n : ℕ) : a^n * a = a * a^n := commute.pow_self a n @[to_additive succ_nsmul'] theorem pow_succ' (a : M) (n : ℕ) : a^(n+1) = a^n * a := by rw [pow_succ, pow_mul_comm'] @[to_additive add_nsmul] theorem pow_add (a : M) (m n : ℕ) : a^(m + n) = a^m * a^n := by induction n with n ih; [rw [nat.add_zero, pow_zero, mul_one], rw [pow_succ', ← mul_assoc, ← ih, ← pow_succ', nat.add_assoc]] @[simp, to_additive one_nsmul] theorem pow_one (a : M) : a^1 = a := by rw [← npow_eq_pow, npow_one] @[simp] lemma pow_ite (P : Prop) [decidable P] (a : M) (b c : ℕ) : a ^ (if P then b else c) = if P then a ^ b else a ^ c := by split_ifs; refl @[simp] lemma ite_pow (P : Prop) [decidable P] (a b : M) (c : ℕ) : (if P then a else b) ^ c = if P then a ^ c else b ^ c := by split_ifs; refl @[simp] lemma pow_boole (P : Prop) [decidable P] (a : M) : a ^ (if P then 1 else 0) = if P then a else 1 := by simp -- the attributes are intentionally out of order. `smul_zero` proves `nsmul_zero`. @[to_additive nsmul_zero, simp] theorem one_pow (n : ℕ) : (1 : M)^n = 1 := by induction n with n ih; [exact pow_zero _, rw [pow_succ, ih, one_mul]] @[to_additive mul_nsmul'] theorem pow_mul (a : M) (m n : ℕ) : a^(m * n) = (a^m)^n := begin induction n with n ih, { rw [nat.mul_zero, pow_zero, pow_zero] }, { rw [nat.mul_succ, pow_add, pow_succ', ih] } end @[to_additive mul_nsmul] theorem pow_mul' (a : M) (m n : ℕ) : a^(m * n) = (a^n)^m := by rw [nat.mul_comm, pow_mul] @[to_additive nsmul_add_sub_nsmul] theorem pow_mul_pow_sub (a : M) {m n : ℕ} (h : m ≤ n) : a ^ m * a ^ (n - m) = a ^ n := by rw [←pow_add, nat.add_comm, nat.sub_add_cancel h] @[to_additive sub_nsmul_nsmul_add] theorem pow_sub_mul_pow (a : M) {m n : ℕ} (h : m ≤ n) : a ^ (n - m) * a ^ m = a ^ n := by rw [←pow_add, nat.sub_add_cancel h] @[to_additive bit0_nsmul] theorem pow_bit0 (a : M) (n : ℕ) : a ^ bit0 n = a^n * a^n := pow_add _ _ _ @[to_additive bit1_nsmul] theorem pow_bit1 (a : M) (n : ℕ) : a ^ bit1 n = a^n * a^n * a := by rw [bit1, pow_succ', pow_bit0] @[to_additive nsmul_add_comm] theorem pow_mul_comm (a : M) (m n : ℕ) : a^m * a^n = a^n * a^m := commute.pow_pow_self a m n @[simp, to_additive add_monoid_hom.map_nsmul] theorem monoid_hom.map_pow (f : M →* N) (a : M) : ∀(n : ℕ), f (a ^ n) = (f a) ^ n | 0 := by rw [pow_zero, pow_zero, f.map_one] | (n+1) := by rw [pow_succ, pow_succ, f.map_mul, monoid_hom.map_pow] @[to_additive] lemma commute.mul_pow {a b : M} (h : commute a b) (n : ℕ) : (a * b) ^ n = a ^ n * b ^ n := nat.rec_on n (by simp only [pow_zero, one_mul]) $ λ n ihn, by simp only [pow_succ, ihn, ← mul_assoc, (h.pow_left n).right_comm] theorem neg_pow [ring R] (a : R) (n : ℕ) : (- a) ^ n = (-1) ^ n * a ^ n := (neg_one_mul a) ▸ (commute.neg_one_left a).mul_pow n @[to_additive bit0_nsmul'] theorem pow_bit0' (a : M) (n : ℕ) : a ^ bit0 n = (a * a) ^ n := by rw [pow_bit0, (commute.refl a).mul_pow] @[to_additive bit1_nsmul'] theorem pow_bit1' (a : M) (n : ℕ) : a ^ bit1 n = (a * a) ^ n * a := by rw [bit1, pow_succ', pow_bit0'] @[simp] theorem neg_pow_bit0 [ring R] (a : R) (n : ℕ) : (- a) ^ (bit0 n) = a ^ (bit0 n) := by rw [pow_bit0', neg_mul_neg, pow_bit0'] @[simp] theorem neg_pow_bit1 [ring R] (a : R) (n : ℕ) : (- a) ^ (bit1 n) = - a ^ (bit1 n) := by simp only [bit1, pow_succ, neg_pow_bit0, neg_mul_eq_neg_mul] end monoid /-! ### Commutative (additive) monoid -/ section comm_monoid variables [comm_monoid M] [add_comm_monoid A] @[to_additive nsmul_add] theorem mul_pow (a b : M) (n : ℕ) : (a * b)^n = a^n * b^n := (commute.all a b).mul_pow n /-- The `n`th power map on a commutative monoid for a natural `n`, considered as a morphism of monoids. -/ @[to_additive nsmul_add_monoid_hom "Multiplication by a natural `n` on a commutative additive monoid, considered as a morphism of additive monoids.", simps] def pow_monoid_hom (n : ℕ) : M →* M := { to_fun := (^ n), map_one' := one_pow _, map_mul' := λ a b ,mul_pow a b n } -- the below line causes the linter to complain :-/ -- attribute [simps] pow_monoid_hom nsmul_add_monoid_hom lemma dvd_pow {x y : M} : ∀ {n : ℕ} (hxy : x ∣ y) (hn : n ≠ 0), x ∣ y^n | 0 hxy hn := (hn rfl).elim | (n+1) hxy hn := by { rw [pow_succ], exact dvd_mul_of_dvd_left hxy _ } end comm_monoid section div_inv_monoid variable [div_inv_monoid G] open int @[simp, norm_cast, to_additive] theorem gpow_coe_nat (a : G) (n : ℕ) : a ^ (n:ℤ) = a ^ n := begin induction n with n ih, { change gpow 0 a = a ^ 0, rw [div_inv_monoid.gpow_zero', pow_zero] }, { change gpow (of_nat n) a = a ^ n at ih, change gpow (of_nat n.succ) a = a ^ n.succ, rw [div_inv_monoid.gpow_succ', pow_succ, ih] } end @[to_additive] theorem gpow_of_nat (a : G) (n : ℕ) : a ^ of_nat n = a ^ n := gpow_coe_nat _ _ @[simp, to_additive] theorem gpow_neg_succ_of_nat (a : G) (n : ℕ) : a ^ -[1+n] = (a ^ n.succ)⁻¹ := by { rw ← gpow_coe_nat, exact div_inv_monoid.gpow_neg' n a } @[simp, to_additive zero_gsmul] theorem gpow_zero (a : G) : a ^ (0:ℤ) = 1 := by { convert pow_zero a using 1, exact gpow_coe_nat a 0 } @[simp, to_additive one_gsmul] theorem gpow_one (a : G) : a ^ (1:ℤ) = a := by { convert pow_one a using 1, exact gpow_coe_nat a 1 } end div_inv_monoid section group variables [group G] [group H] [add_group A] [add_group B] open int section nat @[simp, to_additive neg_nsmul] theorem inv_pow (a : G) (n : ℕ) : (a⁻¹)^n = (a^n)⁻¹ := begin induction n with n ih, { rw [pow_zero, pow_zero, one_inv] }, { rw [pow_succ', pow_succ, ih, mul_inv_rev] } end @[to_additive nsmul_sub] -- rename to sub_nsmul? theorem pow_sub (a : G) {m n : ℕ} (h : n ≤ m) : a^(m - n) = a^m * (a^n)⁻¹ := have h1 : m - n + n = m, from nat.sub_add_cancel h, have h2 : a^(m - n) * a^n = a^m, by rw [←pow_add, h1], eq_mul_inv_of_mul_eq h2 @[to_additive nsmul_neg_comm] theorem pow_inv_comm (a : G) (m n : ℕ) : (a⁻¹)^m * a^n = a^n * (a⁻¹)^m := (commute.refl a).inv_left.pow_pow m n end nat @[simp, to_additive gsmul_zero] theorem one_gpow : ∀ (n : ℤ), (1 : G) ^ n = 1 | (n : ℕ) := by rw [gpow_coe_nat, one_pow] | -[1+ n] := by rw [gpow_neg_succ_of_nat, one_pow, one_inv] @[simp, to_additive neg_gsmul] theorem gpow_neg (a : G) : ∀ (n : ℤ), a ^ -n = (a ^ n)⁻¹ | (n+1:ℕ) := div_inv_monoid.gpow_neg' _ _ | 0 := by { change a ^ (0 : ℤ) = (a ^ (0 : ℤ))⁻¹, simp } | -[1+ n] := by { rw [gpow_neg_succ_of_nat, inv_inv, ← gpow_coe_nat], refl } lemma mul_gpow_neg_one (a b : G) : (a*b)^(-(1:ℤ)) = b^(-(1:ℤ))*a^(-(1:ℤ)) := by simp only [mul_inv_rev, gpow_one, gpow_neg] @[to_additive neg_one_gsmul] theorem gpow_neg_one (x : G) : x ^ (-1:ℤ) = x⁻¹ := by { rw [← congr_arg has_inv.inv (pow_one x), gpow_neg, ← gpow_coe_nat], refl } @[to_additive gsmul_neg] theorem inv_gpow (a : G) : ∀n:ℤ, a⁻¹ ^ n = (a ^ n)⁻¹ | (n : ℕ) := by rw [gpow_coe_nat, gpow_coe_nat, inv_pow] | -[1+ n] := by rw [gpow_neg_succ_of_nat, gpow_neg_succ_of_nat, inv_pow] @[to_additive add_commute.gsmul_add] theorem commute.mul_gpow {a b : G} (h : commute a b) : ∀ n : ℤ, (a * b) ^ n = a ^ n * b ^ n | (n : ℕ) := by simp [gpow_coe_nat, h.mul_pow n] | -[1+n] := by simp [h.mul_pow, (h.pow_pow n.succ n.succ).inv_inv.symm.eq] end group section comm_group variables [comm_group G] [add_comm_group A] @[to_additive gsmul_add] theorem mul_gpow (a b : G) (n : ℤ) : (a * b)^n = a^n * b^n := (commute.all a b).mul_gpow n @[to_additive gsmul_sub] theorem div_gpow (a b : G) (n : ℤ) : (a / b) ^ n = a ^ n / b ^ n := by rw [div_eq_mul_inv, div_eq_mul_inv, mul_gpow, inv_gpow] /-- The `n`th power map (`n` an integer) on a commutative group, considered as a group homomorphism. -/ @[to_additive "Multiplication by an integer `n` on a commutative additive group, considered as an additive group homomorphism.", simps] def gpow_group_hom (n : ℤ) : G →* G := { to_fun := (^ n), map_one' := one_gpow n, map_mul' := λ a b, mul_gpow a b n } end comm_group lemma zero_pow [monoid_with_zero R] : ∀ {n : ℕ}, 0 < n → (0 : R) ^ n = 0 | (n+1) _ := by rw [pow_succ, zero_mul] lemma zero_pow_eq [monoid_with_zero R] (n : ℕ) : (0 : R)^n = if n = 0 then 1 else 0 := begin split_ifs with h, { rw [h, pow_zero], }, { rw [zero_pow (nat.pos_of_ne_zero h)] }, end lemma pow_eq_zero_of_le [monoid_with_zero M] {x : M} {n m : ℕ} (hn : n ≤ m) (hx : x^n = 0) : x^m = 0 := by rw [← nat.sub_add_cancel hn, pow_add, hx, mul_zero] namespace ring_hom variables [semiring R] [semiring S] @[simp] lemma map_pow (f : R →+* S) (a) : ∀ n : ℕ, f (a ^ n) = (f a) ^ n := f.to_monoid_hom.map_pow a end ring_hom section variables (R) theorem neg_one_pow_eq_or [ring R] : ∀ n : ℕ, (-1 : R)^n = 1 ∨ (-1 : R)^n = -1 | 0 := or.inl (pow_zero _) | (n+1) := (neg_one_pow_eq_or n).swap.imp (λ h, by rw [pow_succ, h, neg_one_mul, neg_neg]) (λ h, by rw [pow_succ, h, mul_one]) end @[simp] lemma neg_one_pow_mul_eq_zero_iff [ring R] {n : ℕ} {r : R} : (-1)^n * r = 0 ↔ r = 0 := by rcases neg_one_pow_eq_or R n; simp [h] @[simp] lemma mul_neg_one_pow_eq_zero_iff [ring R] {n : ℕ} {r : R} : r * (-1)^n = 0 ↔ r = 0 := by rcases neg_one_pow_eq_or R n; simp [h] lemma pow_dvd_pow [monoid R] (a : R) {m n : ℕ} (h : m ≤ n) : a ^ m ∣ a ^ n := ⟨a ^ (n - m), by rw [← pow_add, nat.add_comm, nat.sub_add_cancel h]⟩ theorem pow_dvd_pow_of_dvd [comm_monoid R] {a b : R} (h : a ∣ b) : ∀ n : ℕ, a ^ n ∣ b ^ n | 0 := by rw [pow_zero, pow_zero] | (n+1) := by { rw [pow_succ, pow_succ], exact mul_dvd_mul h (pow_dvd_pow_of_dvd n) } lemma sq_sub_sq {R : Type*} [comm_ring R] (a b : R) : a ^ 2 - b ^ 2 = (a + b) * (a - b) := by rw [sq, sq, mul_self_sub_mul_self] alias sq_sub_sq ← pow_two_sub_pow_two lemma eq_or_eq_neg_of_sq_eq_sq [integral_domain R] (a b : R) (h : a ^ 2 = b ^ 2) : a = b ∨ a = -b := by rwa [← add_eq_zero_iff_eq_neg, ← sub_eq_zero, or_comm, ← mul_eq_zero, ← sq_sub_sq a b, sub_eq_zero] theorem pow_eq_zero [monoid_with_zero R] [no_zero_divisors R] {x : R} {n : ℕ} (H : x^n = 0) : x = 0 := begin induction n with n ih, { rw pow_zero at H, rw [← mul_one x, H, mul_zero] }, { rw pow_succ at H, exact or.cases_on (mul_eq_zero.1 H) id ih } end @[simp] lemma pow_eq_zero_iff [monoid_with_zero R] [no_zero_divisors R] {a : R} {n : ℕ} (hn : 0 < n) : a ^ n = 0 ↔ a = 0 := begin refine ⟨pow_eq_zero, _⟩, rintros rfl, exact zero_pow hn, end lemma pow_ne_zero_iff [monoid_with_zero R] [no_zero_divisors R] {a : R} {n : ℕ} (hn : 0 < n) : a ^ n ≠ 0 ↔ a ≠ 0 := by rwa [not_iff_not, pow_eq_zero_iff] @[field_simps] theorem pow_ne_zero [monoid_with_zero R] [no_zero_divisors R] {a : R} (n : ℕ) (h : a ≠ 0) : a ^ n ≠ 0 := mt pow_eq_zero h section semiring variables [semiring R] lemma min_pow_dvd_add {n m : ℕ} {a b c : R} (ha : c ^ n ∣ a) (hb : c ^ m ∣ b) : c ^ (min n m) ∣ a + b := begin replace ha := (pow_dvd_pow c (min_le_left n m)).trans ha, replace hb := (pow_dvd_pow c (min_le_right n m)).trans hb, exact dvd_add ha hb end end semiring section comm_semiring variables [comm_semiring R] lemma add_sq (a b : R) : (a + b) ^ 2 = a ^ 2 + 2 * a * b + b ^ 2 := by simp only [sq, add_mul_self_eq] alias add_sq ← add_pow_two end comm_semiring @[simp] lemma neg_sq {α} [ring α] (z : α) : (-z)^2 = z^2 := by simp [sq] alias neg_sq ← neg_pow_two lemma sub_sq {R} [comm_ring R] (a b : R) : (a - b) ^ 2 = a ^ 2 - 2 * a * b + b ^ 2 := by rw [sub_eq_add_neg, add_sq, neg_sq, mul_neg_eq_neg_mul_symm, ← sub_eq_add_neg] alias sub_sq ← sub_pow_two lemma of_add_nsmul [add_monoid A] (x : A) (n : ℕ) : multiplicative.of_add (n • x) = (multiplicative.of_add x)^n := rfl lemma of_add_gsmul [add_group A] (x : A) (n : ℤ) : multiplicative.of_add (n • x) = (multiplicative.of_add x)^n := rfl lemma of_mul_pow {A : Type*} [monoid A] (x : A) (n : ℕ) : additive.of_mul (x ^ n) = n • (additive.of_mul x) := rfl lemma of_mul_gpow [group G] (x : G) (n : ℤ) : additive.of_mul (x ^ n) = n • additive.of_mul x := rfl @[simp] lemma semiconj_by.gpow_right [group G] {a x y : G} (h : semiconj_by a x y) : ∀ m : ℤ, semiconj_by a (x^m) (y^m) | (n : ℕ) := by simp [gpow_coe_nat, h.pow_right n] | -[1+n] := by simp [(h.pow_right n.succ).inv_right] namespace commute variables [group G] {a b : G} @[simp] lemma gpow_right (h : commute a b) (m : ℤ) : commute a (b^m) := h.gpow_right m @[simp] lemma gpow_left (h : commute a b) (m : ℤ) : commute (a^m) b := (h.symm.gpow_right m).symm lemma gpow_gpow (h : commute a b) (m n : ℤ) : commute (a^m) (b^n) := (h.gpow_left m).gpow_right n variables (a) (m n : ℤ) @[simp] theorem self_gpow : commute a (a ^ n) := (commute.refl a).gpow_right n @[simp] theorem gpow_self : commute (a ^ n) a := (commute.refl a).gpow_left n @[simp] theorem gpow_gpow_self : commute (a ^ m) (a ^ n) := (commute.refl a).gpow_gpow m n end commute
a81e49f37332d75fd3b14c941b7d7f8acc3d3efd
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/special_functions/log/basic.lean
da7392ce78e955cc22343138f70d21bf77c7dda3
[ "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
13,485
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 -/ import analysis.special_functions.exp import data.nat.factorization.basic /-! # Real logarithm > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we define `real.log` to be the logarithm of a real number. As usual, we extend it from its domain `(0, +∞)` to a globally defined function. We choose to do it so that `log 0 = 0` and `log (-x) = log x`. We prove some basic properties of this function and show that it is continuous. ## Tags logarithm, continuity -/ open set filter function open_locale topology noncomputable theory namespace real variables {x y : ℝ} /-- The real logarithm function, equal to the inverse of the exponential for `x > 0`, to `log |x|` for `x < 0`, and to `0` for `0`. We use this unconventional extension to `(-∞, 0]` as it gives the formula `log (x * y) = log x + log y` for all nonzero `x` and `y`, and the derivative of `log` is `1/x` away from `0`. -/ @[pp_nodot] noncomputable def log (x : ℝ) : ℝ := if hx : x = 0 then 0 else exp_order_iso.symm ⟨|x|, abs_pos.2 hx⟩ lemma log_of_ne_zero (hx : x ≠ 0) : log x = exp_order_iso.symm ⟨|x|, abs_pos.2 hx⟩ := dif_neg hx lemma log_of_pos (hx : 0 < x) : log x = exp_order_iso.symm ⟨x, hx⟩ := by { rw [log_of_ne_zero hx.ne'], congr, exact abs_of_pos hx } lemma exp_log_eq_abs (hx : x ≠ 0) : exp (log x) = |x| := by rw [log_of_ne_zero hx, ← coe_exp_order_iso_apply, order_iso.apply_symm_apply, subtype.coe_mk] lemma exp_log (hx : 0 < x) : exp (log x) = x := by { rw exp_log_eq_abs hx.ne', exact abs_of_pos hx } lemma exp_log_of_neg (hx : x < 0) : exp (log x) = -x := by { rw exp_log_eq_abs (ne_of_lt hx), exact abs_of_neg hx } lemma le_exp_log (x : ℝ) : x ≤ exp (log x) := begin by_cases h_zero : x = 0, { rw [h_zero, log, dif_pos rfl, exp_zero], exact zero_le_one, }, { rw exp_log_eq_abs h_zero, exact le_abs_self _, }, end @[simp] lemma log_exp (x : ℝ) : log (exp x) = x := exp_injective $ exp_log (exp_pos x) lemma surj_on_log : surj_on log (Ioi 0) univ := λ x _, ⟨exp x, exp_pos x, log_exp x⟩ lemma log_surjective : surjective log := λ x, ⟨exp x, log_exp x⟩ @[simp] lemma range_log : range log = univ := log_surjective.range_eq @[simp] lemma log_zero : log 0 = 0 := dif_pos rfl @[simp] lemma log_one : log 1 = 0 := exp_injective $ by rw [exp_log zero_lt_one, exp_zero] @[simp] lemma log_abs (x : ℝ) : log (|x|) = log x := begin by_cases h : x = 0, { simp [h] }, { rw [← exp_eq_exp, exp_log_eq_abs h, exp_log_eq_abs (abs_pos.2 h).ne', abs_abs] } end @[simp] lemma log_neg_eq_log (x : ℝ) : log (-x) = log x := by rw [← log_abs x, ← log_abs (-x), abs_neg] lemma sinh_log {x : ℝ} (hx : 0 < x) : sinh (log x) = (x - x⁻¹) / 2 := by rw [sinh_eq, exp_neg, exp_log hx] lemma cosh_log {x : ℝ} (hx : 0 < x) : cosh (log x) = (x + x⁻¹) / 2 := by rw [cosh_eq, exp_neg, exp_log hx] lemma surj_on_log' : surj_on log (Iio 0) univ := λ x _, ⟨-exp x, neg_lt_zero.2 $ exp_pos x, by rw [log_neg_eq_log, log_exp]⟩ lemma log_mul (hx : x ≠ 0) (hy : y ≠ 0) : log (x * y) = log x + log y := exp_injective $ by rw [exp_log_eq_abs (mul_ne_zero hx hy), exp_add, exp_log_eq_abs hx, exp_log_eq_abs hy, abs_mul] lemma log_div (hx : x ≠ 0) (hy : y ≠ 0) : log (x / y) = log x - log y := exp_injective $ by rw [exp_log_eq_abs (div_ne_zero hx hy), exp_sub, exp_log_eq_abs hx, exp_log_eq_abs hy, abs_div] @[simp] lemma log_inv (x : ℝ) : log (x⁻¹) = -log x := begin by_cases hx : x = 0, { simp [hx] }, rw [← exp_eq_exp, exp_log_eq_abs (inv_ne_zero hx), exp_neg, exp_log_eq_abs hx, abs_inv] end lemma log_le_log (h : 0 < x) (h₁ : 0 < y) : log x ≤ log y ↔ x ≤ y := by rw [← exp_le_exp, exp_log h, exp_log h₁] lemma log_lt_log (hx : 0 < x) : x < y → log x < log y := by { intro h, rwa [← exp_lt_exp, exp_log hx, exp_log (lt_trans hx h)] } lemma log_lt_log_iff (hx : 0 < x) (hy : 0 < y) : log x < log y ↔ x < y := by { rw [← exp_lt_exp, exp_log hx, exp_log hy] } lemma log_le_iff_le_exp (hx : 0 < x) : log x ≤ y ↔ x ≤ exp y := by rw [←exp_le_exp, exp_log hx] lemma log_lt_iff_lt_exp (hx : 0 < x) : log x < y ↔ x < exp y := by rw [←exp_lt_exp, exp_log hx] lemma le_log_iff_exp_le (hy : 0 < y) : x ≤ log y ↔ exp x ≤ y := by rw [←exp_le_exp, exp_log hy] lemma lt_log_iff_exp_lt (hy : 0 < y) : x < log y ↔ exp x < y := by rw [←exp_lt_exp, exp_log hy] lemma log_pos_iff (hx : 0 < x) : 0 < log x ↔ 1 < x := by { rw ← log_one, exact log_lt_log_iff zero_lt_one hx } lemma log_pos (hx : 1 < x) : 0 < log x := (log_pos_iff (lt_trans zero_lt_one hx)).2 hx lemma log_neg_iff (h : 0 < x) : log x < 0 ↔ x < 1 := by { rw ← log_one, exact log_lt_log_iff h zero_lt_one } lemma log_neg (h0 : 0 < x) (h1 : x < 1) : log x < 0 := (log_neg_iff h0).2 h1 lemma log_nonneg_iff (hx : 0 < x) : 0 ≤ log x ↔ 1 ≤ x := by rw [← not_lt, log_neg_iff hx, not_lt] lemma log_nonneg (hx : 1 ≤ x) : 0 ≤ log x := (log_nonneg_iff (zero_lt_one.trans_le hx)).2 hx lemma log_nonpos_iff (hx : 0 < x) : log x ≤ 0 ↔ x ≤ 1 := by rw [← not_lt, log_pos_iff hx, not_lt] lemma log_nonpos_iff' (hx : 0 ≤ x) : log x ≤ 0 ↔ x ≤ 1 := begin rcases hx.eq_or_lt with (rfl|hx), { simp [le_refl, zero_le_one] }, exact log_nonpos_iff hx end lemma log_nonpos (hx : 0 ≤ x) (h'x : x ≤ 1) : log x ≤ 0 := (log_nonpos_iff' hx).2 h'x lemma strict_mono_on_log : strict_mono_on log (set.Ioi 0) := λ x hx y hy hxy, log_lt_log hx hxy lemma strict_anti_on_log : strict_anti_on log (set.Iio 0) := begin rintros x (hx : x < 0) y (hy : y < 0) hxy, rw [← log_abs y, ← log_abs x], refine log_lt_log (abs_pos.2 hy.ne) _, rwa [abs_of_neg hy, abs_of_neg hx, neg_lt_neg_iff] end lemma log_inj_on_pos : set.inj_on log (set.Ioi 0) := strict_mono_on_log.inj_on lemma log_lt_sub_one_of_pos (hx1 : 0 < x) (hx2 : x ≠ 1) : log x < x - 1 := begin have h : log x ≠ 0, { rw [← log_one, log_inj_on_pos.ne_iff hx1 zero_lt_one], exact hx2 }, linarith [add_one_lt_exp_of_nonzero h, exp_log hx1], end lemma eq_one_of_pos_of_log_eq_zero {x : ℝ} (h₁ : 0 < x) (h₂ : log x = 0) : x = 1 := log_inj_on_pos (set.mem_Ioi.2 h₁) (set.mem_Ioi.2 zero_lt_one) (h₂.trans real.log_one.symm) lemma log_ne_zero_of_pos_of_ne_one {x : ℝ} (hx_pos : 0 < x) (hx : x ≠ 1) : log x ≠ 0 := mt (eq_one_of_pos_of_log_eq_zero hx_pos) hx @[simp] lemma log_eq_zero {x : ℝ} : log x = 0 ↔ x = 0 ∨ x = 1 ∨ x = -1 := begin split, { intros h, rcases lt_trichotomy x 0 with x_lt_zero | rfl | x_gt_zero, { refine or.inr (or.inr (neg_eq_iff_eq_neg.mp _)), rw [←log_neg_eq_log x] at h, exact eq_one_of_pos_of_log_eq_zero (neg_pos.mpr x_lt_zero) h, }, { exact or.inl rfl }, { exact or.inr (or.inl (eq_one_of_pos_of_log_eq_zero x_gt_zero h)), }, }, { rintro (rfl|rfl|rfl); simp only [log_one, log_zero, log_neg_eq_log], } end lemma log_ne_zero {x : ℝ} : log x ≠ 0 ↔ x ≠ 0 ∧ x ≠ 1 ∧ x ≠ -1 := by simpa only [not_or_distrib] using log_eq_zero.not @[simp] lemma log_pow (x : ℝ) (n : ℕ) : log (x ^ n) = n * log x := begin induction n with n ih, { simp }, rcases eq_or_ne x 0 with rfl | hx, { simp }, rw [pow_succ', log_mul (pow_ne_zero _ hx) hx, ih, nat.cast_succ, add_mul, one_mul], end @[simp] lemma log_zpow (x : ℝ) (n : ℤ) : log (x ^ n) = n * log x := begin induction n, { rw [int.of_nat_eq_coe, zpow_coe_nat, log_pow, int.cast_coe_nat] }, rw [zpow_neg_succ_of_nat, log_inv, log_pow, int.cast_neg_succ_of_nat, nat.cast_add_one, neg_mul_eq_neg_mul], end lemma log_sqrt {x : ℝ} (hx : 0 ≤ x) : log (sqrt x) = log x / 2 := by { rw [eq_div_iff, mul_comm, ← nat.cast_two, ← log_pow, sq_sqrt hx], exact two_ne_zero } lemma log_le_sub_one_of_pos {x : ℝ} (hx : 0 < x) : log x ≤ x - 1 := begin rw le_sub_iff_add_le, convert add_one_le_exp (log x), rw exp_log hx, end /-- Bound for `|log x * x|` in the interval `(0, 1]`. -/ lemma abs_log_mul_self_lt (x: ℝ) (h1 : 0 < x) (h2 : x ≤ 1) : |log x * x| < 1 := begin have : 0 < 1/x := by simpa only [one_div, inv_pos] using h1, replace := log_le_sub_one_of_pos this, replace : log (1 / x) < 1/x := by linarith, rw [log_div one_ne_zero h1.ne', log_one, zero_sub, lt_div_iff h1] at this, have aux : 0 ≤ -log x * x, { refine mul_nonneg _ h1.le, rw ←log_inv, apply log_nonneg, rw [←(le_inv h1 zero_lt_one), inv_one], exact h2, }, rw [←(abs_of_nonneg aux), neg_mul, abs_neg] at this, exact this, end /-- The real logarithm function tends to `+∞` at `+∞`. -/ lemma tendsto_log_at_top : tendsto log at_top at_top := tendsto_comp_exp_at_top.1 $ by simpa only [log_exp] using tendsto_id lemma tendsto_log_nhds_within_zero : tendsto log (𝓝[≠] 0) at_bot := begin rw [← (show _ = log, from funext log_abs)], refine tendsto.comp _ tendsto_abs_nhds_within_zero, simpa [← tendsto_comp_exp_at_bot] using tendsto_id end lemma continuous_on_log : continuous_on log {0}ᶜ := begin rw [continuous_on_iff_continuous_restrict, restrict], conv in (log _) { rw [log_of_ne_zero (show (x : ℝ) ≠ 0, from x.2)] }, exact exp_order_iso.symm.continuous.comp (continuous_subtype_coe.norm.subtype_mk _) end @[continuity] lemma continuous_log : continuous (λ x : {x : ℝ // x ≠ 0}, log x) := continuous_on_iff_continuous_restrict.1 $ continuous_on_log.mono $ λ x hx, hx @[continuity] lemma continuous_log' : continuous (λ x : {x : ℝ // 0 < x}, log x) := continuous_on_iff_continuous_restrict.1 $ continuous_on_log.mono $ λ x hx, ne_of_gt hx lemma continuous_at_log (hx : x ≠ 0) : continuous_at log x := (continuous_on_log x hx).continuous_at $ is_open.mem_nhds is_open_compl_singleton hx @[simp] lemma continuous_at_log_iff : continuous_at log x ↔ x ≠ 0 := begin refine ⟨_, continuous_at_log⟩, rintros h rfl, exact not_tendsto_nhds_of_tendsto_at_bot tendsto_log_nhds_within_zero _ (h.tendsto.mono_left inf_le_left) end open_locale big_operators lemma log_prod {α : Type*} (s : finset α) (f : α → ℝ) (hf : ∀ x ∈ s, f x ≠ 0): log (∏ i in s, f i) = ∑ i in s, log (f i) := begin induction s using finset.cons_induction_on with a s ha ih, { simp }, { rw [finset.forall_mem_cons] at hf, simp [ih hf.2, log_mul hf.1 (finset.prod_ne_zero_iff.2 hf.2)] } end lemma log_nat_eq_sum_factorization (n : ℕ) : log n = n.factorization.sum (λ p t, t * log p) := begin rcases eq_or_ne n 0 with rfl | hn, { simp }, nth_rewrite 0 [←nat.factorization_prod_pow_eq_self hn], rw [finsupp.prod, nat.cast_prod, log_prod _ _ (λ p hp, _), finsupp.sum], { simp_rw [nat.cast_pow, log_pow] }, { norm_cast, exact pow_ne_zero _ (nat.prime_of_mem_factorization hp).ne_zero }, end lemma tendsto_pow_log_div_mul_add_at_top (a b : ℝ) (n : ℕ) (ha : a ≠ 0) : tendsto (λ x, log x ^ n / (a * x + b)) at_top (𝓝 0) := ((tendsto_div_pow_mul_exp_add_at_top a b n ha.symm).comp tendsto_log_at_top).congr' (by filter_upwards [eventually_gt_at_top (0 : ℝ)] with x hx using by simp [exp_log hx]) lemma is_o_pow_log_id_at_top {n : ℕ} : (λ x, log x ^ n) =o[at_top] id := begin rw asymptotics.is_o_iff_tendsto', { simpa using tendsto_pow_log_div_mul_add_at_top 1 0 n one_ne_zero }, filter_upwards [eventually_ne_at_top (0 : ℝ)] with x h₁ h₂ using (h₁ h₂).elim, end lemma is_o_log_id_at_top : log =o[at_top] id := is_o_pow_log_id_at_top.congr_left (λ x, pow_one _) end real section continuity open real variables {α : Type*} lemma filter.tendsto.log {f : α → ℝ} {l : filter α} {x : ℝ} (h : tendsto f l (𝓝 x)) (hx : x ≠ 0) : tendsto (λ x, log (f x)) l (𝓝 (log x)) := (continuous_at_log hx).tendsto.comp h variables [topological_space α] {f : α → ℝ} {s : set α} {a : α} lemma continuous.log (hf : continuous f) (h₀ : ∀ x, f x ≠ 0) : continuous (λ x, log (f x)) := continuous_on_log.comp_continuous hf h₀ lemma continuous_at.log (hf : continuous_at f a) (h₀ : f a ≠ 0) : continuous_at (λ x, log (f x)) a := hf.log h₀ lemma continuous_within_at.log (hf : continuous_within_at f s a) (h₀ : f a ≠ 0) : continuous_within_at (λ x, log (f x)) s a := hf.log h₀ lemma continuous_on.log (hf : continuous_on f s) (h₀ : ∀ x ∈ s, f x ≠ 0) : continuous_on (λ x, log (f x)) s := λ x hx, (hf x hx).log (h₀ x hx) end continuity section tendsto_comp_add_sub open filter namespace real lemma tendsto_log_comp_add_sub_log (y : ℝ) : tendsto (λ x:ℝ, log (x + y) - log x) at_top (𝓝 0) := begin refine tendsto.congr' (_ : ∀ᶠ (x : ℝ) in at_top, log (1 + y / x) = _) _, { refine eventually.mp ((eventually_ne_at_top 0).and (eventually_gt_at_top (-y))) (eventually_of_forall (λ x hx, _)), rw ← log_div _ hx.1, { congr' 1, field_simp [hx.1] }, { linarith [hx.2] } }, { suffices : tendsto (λ (x : ℝ), log (1 + y / x)) at_top (𝓝 (log (1 + 0))), by simpa, refine tendsto.log _ (by simp), exact tendsto_const_nhds.add (tendsto_const_nhds.div_at_top tendsto_id) }, end lemma tendsto_log_nat_add_one_sub_log : tendsto (λ (k : ℕ), log (k + 1) - log k) at_top (𝓝 0) := (tendsto_log_comp_add_sub_log 1).comp tendsto_coe_nat_at_top_at_top end real end tendsto_comp_add_sub
13ba1d89250eb90b9ba4f253e81a5fe3fc70c551
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/ring_theory/ideals.lean
066995af39675d285e4bd105c357e17886192ee8
[ "Apache-2.0" ]
permissive
dupuisf/mathlib
62de4ec6544bf3b79086afd27b6529acfaf2c1bb
8582b06b0a5d06c33ee07d0bdf7c646cae22cf36
refs/heads/master
1,669,494,854,016
1,595,692,409,000
1,595,692,409,000
272,046,630
0
0
Apache-2.0
1,592,066,143,000
1,592,066,142,000
null
UTF-8
Lean
false
false
21,373
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Chris Hughes, Mario Carneiro -/ import algebra.associated import linear_algebra.basic import order.zorn universes u v variables {α : Type u} {β : Type v} {a b : α} open set function open_locale classical big_operators namespace ideal variables [comm_ring α] (I : ideal α) @[ext] lemma ext {I J : ideal α} (h : ∀ x, x ∈ I ↔ x ∈ J) : I = J := submodule.ext h theorem eq_top_of_unit_mem (x y : α) (hx : x ∈ I) (h : y * x = 1) : I = ⊤ := eq_top_iff.2 $ λ z _, calc z = z * (y * x) : by simp [h] ... = (z * y) * x : eq.symm $ mul_assoc z y x ... ∈ I : I.mul_mem_left hx theorem eq_top_of_is_unit_mem {x} (hx : x ∈ I) (h : is_unit x) : I = ⊤ := let ⟨y, hy⟩ := is_unit_iff_exists_inv'.1 h in eq_top_of_unit_mem I x y hx hy theorem eq_top_iff_one : I = ⊤ ↔ (1:α) ∈ I := ⟨by rintro rfl; trivial, λ h, eq_top_of_unit_mem _ _ 1 h (by simp)⟩ theorem ne_top_iff_one : I ≠ ⊤ ↔ (1:α) ∉ I := not_congr I.eq_top_iff_one /-- The ideal generated by a subset of a ring -/ def span (s : set α) : ideal α := submodule.span α s lemma subset_span {s : set α} : s ⊆ span s := submodule.subset_span lemma span_le {s : set α} {I} : span s ≤ I ↔ s ⊆ I := submodule.span_le lemma span_mono {s t : set α} : s ⊆ t → span s ≤ span t := submodule.span_mono @[simp] lemma span_eq : span (I : set α) = I := submodule.span_eq _ @[simp] lemma span_singleton_one : span (1 : set α) = ⊤ := (eq_top_iff_one _).2 $ subset_span $ mem_singleton _ lemma mem_span_insert {s : set α} {x y} : x ∈ span (insert y s) ↔ ∃ a (z ∈ span s), x = a * y + z := submodule.mem_span_insert lemma mem_span_insert' {s : set α} {x y} : x ∈ span (insert y s) ↔ ∃a, x + a * y ∈ span s := submodule.mem_span_insert' lemma mem_span_singleton' {x y : α} : x ∈ span ({y} : set α) ↔ ∃ a, a * y = x := submodule.mem_span_singleton lemma mem_span_singleton {x y : α} : x ∈ span ({y} : set α) ↔ y ∣ x := mem_span_singleton'.trans $ exists_congr $ λ _, by rw [eq_comm, mul_comm] lemma span_singleton_le_span_singleton {x y : α} : span ({x} : set α) ≤ span ({y} : set α) ↔ y ∣ x := span_le.trans $ singleton_subset_iff.trans mem_span_singleton lemma span_singleton_eq_span_singleton {α : Type u} [integral_domain α] {x y : α} : span ({x} : set α) = span ({y} : set α) ↔ associated x y := begin rw [←dvd_dvd_iff_associated, le_antisymm_iff, and_comm], apply and_congr; rw span_singleton_le_span_singleton, end lemma span_eq_bot {s : set α} : span s = ⊥ ↔ ∀ x ∈ s, (x:α) = 0 := submodule.span_eq_bot @[simp] lemma span_singleton_eq_bot {x} : span ({x} : set α) = ⊥ ↔ x = 0 := submodule.span_singleton_eq_bot @[simp] lemma span_zero : span (0 : set α) = ⊥ := by rw [←set.singleton_zero, span_singleton_eq_bot] lemma span_singleton_eq_top {x} : span ({x} : set α) = ⊤ ↔ is_unit x := by rw [is_unit_iff_dvd_one, ← span_singleton_le_span_singleton, singleton_one, span_singleton_one, eq_top_iff] lemma span_singleton_mul_right_unit {a : α} (h2 : is_unit a) (x : α) : span ({x * a} : set α) = span {x} := begin apply le_antisymm, { rw span_singleton_le_span_singleton, use a}, { rw span_singleton_le_span_singleton, rw mul_dvd_of_is_unit_right h2} end lemma span_singleton_mul_left_unit {a : α} (h2 : is_unit a) (x : α) : span ({a * x} : set α) = span {x} := by rw [mul_comm, span_singleton_mul_right_unit h2] /-- An ideal `P` of a ring `R` is prime if `P ≠ R` and `xy ∈ P → x ∈ P ∨ y ∈ P` -/ @[class] def is_prime (I : ideal α) : Prop := I ≠ ⊤ ∧ ∀ {x y : α}, x * y ∈ I → x ∈ I ∨ y ∈ I theorem is_prime.mem_or_mem {I : ideal α} (hI : I.is_prime) : ∀ {x y : α}, x * y ∈ I → x ∈ I ∨ y ∈ I := hI.2 theorem is_prime.mem_or_mem_of_mul_eq_zero {I : ideal α} (hI : I.is_prime) {x y : α} (h : x * y = 0) : x ∈ I ∨ y ∈ I := hI.2 (h.symm ▸ I.zero_mem) theorem is_prime.mem_of_pow_mem {I : ideal α} (hI : I.is_prime) {r : α} (n : ℕ) (H : r^n ∈ I) : r ∈ I := begin induction n with n ih, { exact (mt (eq_top_iff_one _).2 hI.1).elim H }, exact or.cases_on (hI.mem_or_mem H) id ih end theorem zero_ne_one_of_proper {I : ideal α} (h : I ≠ ⊤) : (0:α) ≠ 1 := λ hz, I.ne_top_iff_one.1 h $ hz ▸ I.zero_mem theorem span_singleton_prime {p : α} (hp : p ≠ 0) : is_prime (span ({p} : set α)) ↔ prime p := by simp [is_prime, prime, span_singleton_eq_top, hp, mem_span_singleton] /-- An ideal is maximal if it is maximal in the collection of proper ideals. -/ @[class] def is_maximal (I : ideal α) : Prop := I ≠ ⊤ ∧ ∀ J, I < J → J = ⊤ theorem is_maximal_iff {I : ideal α} : I.is_maximal ↔ (1:α) ∉ I ∧ ∀ (J : ideal α) x, I ≤ J → x ∉ I → x ∈ J → (1:α) ∈ J := and_congr I.ne_top_iff_one $ forall_congr $ λ J, by rw [lt_iff_le_not_le]; exact ⟨λ H x h hx₁ hx₂, J.eq_top_iff_one.1 $ H ⟨h, not_subset.2 ⟨_, hx₂, hx₁⟩⟩, λ H ⟨h₁, h₂⟩, let ⟨x, xJ, xI⟩ := not_subset.1 h₂ in J.eq_top_iff_one.2 $ H x h₁ xI xJ⟩ theorem is_maximal.eq_of_le {I J : ideal α} (hI : I.is_maximal) (hJ : J ≠ ⊤) (IJ : I ≤ J) : I = J := eq_iff_le_not_lt.2 ⟨IJ, λ h, hJ (hI.2 _ h)⟩ theorem is_maximal.exists_inv {I : ideal α} (hI : I.is_maximal) {x} (hx : x ∉ I) : ∃ y, y * x - 1 ∈ I := begin cases is_maximal_iff.1 hI with H₁ H₂, rcases mem_span_insert'.1 (H₂ (span (insert x I)) x (set.subset.trans (subset_insert _ _) subset_span) hx (subset_span (mem_insert _ _))) with ⟨y, hy⟩, rw [span_eq, ← neg_mem_iff, add_comm, neg_add', neg_mul_eq_neg_mul] at hy, exact ⟨-y, hy⟩ end theorem is_maximal.is_prime {I : ideal α} (H : I.is_maximal) : I.is_prime := ⟨H.1, λ x y hxy, or_iff_not_imp_left.2 $ λ hx, begin cases H.exists_inv hx with z hz, have := I.mul_mem_left hz, rw [mul_sub, mul_one, mul_comm, mul_assoc] at this, exact I.neg_mem_iff.1 ((I.add_mem_iff_right $ I.mul_mem_left hxy).1 this) end⟩ @[priority 100] -- see Note [lower instance priority] instance is_maximal.is_prime' (I : ideal α) : ∀ [H : I.is_maximal], I.is_prime := is_maximal.is_prime theorem exists_le_maximal (I : ideal α) (hI : I ≠ ⊤) : ∃ M : ideal α, M.is_maximal ∧ I ≤ M := begin rcases zorn.zorn_partial_order₀ { J : ideal α | J ≠ ⊤ } _ I hI with ⟨M, M0, IM, h⟩, { refine ⟨M, ⟨M0, λ J hJ, by_contradiction $ λ J0, _⟩, IM⟩, cases h J J0 (le_of_lt hJ), exact lt_irrefl _ hJ }, { intros S SC cC I IS, refine ⟨Sup S, λ H, _, λ _, le_Sup⟩, obtain ⟨J, JS, J0⟩ : ∃ J ∈ S, (1 : α) ∈ J, from (submodule.mem_Sup_of_directed ⟨I, IS⟩ cC.directed_on).1 ((eq_top_iff_one _).1 H), exact SC JS ((eq_top_iff_one _).2 J0) } end /-- If P is not properly contained in any maximal ideal then it is not properly contained in any proper ideal -/ lemma maximal_of_no_maximal {R : Type u} [comm_ring R] {P : ideal R} (hmax : ∀ m : ideal R, P < m → ¬is_maximal m) (J : ideal R) (hPJ : P < J) : J = ⊤ := begin by_contradiction hnonmax, rcases exists_le_maximal J hnonmax with ⟨M, hM1, hM2⟩, exact hmax M (lt_of_lt_of_le hPJ hM2) hM1, end theorem mem_span_pair {x y z : α} : z ∈ span ({x, y} : set α) ↔ ∃ a b, a * x + b * y = z := by simp [mem_span_insert, mem_span_singleton', @eq_comm _ _ z] lemma span_singleton_lt_span_singleton [integral_domain β] {x y : β} : span ({x} : set β) < span ({y} : set β) ↔ y ≠ 0 ∧ ∃ d : β, ¬ is_unit d ∧ x = y * d := by rw [lt_iff_le_not_le, span_singleton_le_span_singleton, span_singleton_le_span_singleton, dvd_and_not_dvd_iff] lemma factors_decreasing [integral_domain β] (b₁ b₂ : β) (h₁ : b₁ ≠ 0) (h₂ : ¬ is_unit b₂) : span ({b₁ * b₂} : set β) < span {b₁} := lt_of_le_not_le (ideal.span_le.2 $ singleton_subset_iff.2 $ ideal.mem_span_singleton.2 ⟨b₂, rfl⟩) $ λ h, h₂ $ is_unit_of_dvd_one _ $ (mul_dvd_mul_iff_left h₁).1 $ by rwa [mul_one, ← ideal.span_singleton_le_span_singleton] /-- The quotient `R/I` of a ring `R` by an ideal `I`. -/ def quotient (I : ideal α) := I.quotient namespace quotient variables {I} {x y : α} instance (I : ideal α) : has_one I.quotient := ⟨submodule.quotient.mk 1⟩ instance (I : ideal α) : has_mul I.quotient := ⟨λ a b, quotient.lift_on₂' a b (λ a b, submodule.quotient.mk (a * b)) $ λ a₁ a₂ b₁ b₂ h₁ h₂, quot.sound $ begin refine calc a₁ * a₂ - b₁ * b₂ = a₂ * (a₁ - b₁) + (a₂ - b₂) * b₁ : _ ... ∈ I : I.add_mem (I.mul_mem_left h₁) (I.mul_mem_right h₂), rw [mul_sub, sub_mul, sub_add_sub_cancel, mul_comm, mul_comm b₁] end⟩ instance (I : ideal α) : comm_ring I.quotient := { mul := (*), one := 1, mul_assoc := λ a b c, quotient.induction_on₃' a b c $ λ a b c, congr_arg submodule.quotient.mk (mul_assoc a b c), mul_comm := λ a b, quotient.induction_on₂' a b $ λ a b, congr_arg submodule.quotient.mk (mul_comm a b), one_mul := λ a, quotient.induction_on' a $ λ a, congr_arg submodule.quotient.mk (one_mul a), mul_one := λ a, quotient.induction_on' a $ λ a, congr_arg submodule.quotient.mk (mul_one a), left_distrib := λ a b c, quotient.induction_on₃' a b c $ λ a b c, congr_arg submodule.quotient.mk (left_distrib a b c), right_distrib := λ a b c, quotient.induction_on₃' a b c $ λ a b c, congr_arg submodule.quotient.mk (right_distrib a b c), ..submodule.quotient.add_comm_group I } /-- The ring homomorphism from a ring `R` to a quotient ring `R/I`. -/ def mk (I : ideal α) : α →+* I.quotient := ⟨λ a, submodule.quotient.mk a, rfl, λ _ _, rfl, rfl, λ _ _, rfl⟩ instance : inhabited (quotient I) := ⟨mk I 37⟩ protected theorem eq : mk I x = mk I y ↔ x - y ∈ I := submodule.quotient.eq I @[simp] theorem mk_eq_mk (x : α) : (submodule.quotient.mk x : quotient I) = mk I x := rfl lemma eq_zero_iff_mem {I : ideal α} : mk I a = 0 ↔ a ∈ I := by conv {to_rhs, rw ← sub_zero a }; exact quotient.eq' theorem zero_eq_one_iff {I : ideal α} : (0 : I.quotient) = 1 ↔ I = ⊤ := eq_comm.trans $ eq_zero_iff_mem.trans (eq_top_iff_one _).symm theorem zero_ne_one_iff {I : ideal α} : (0 : I.quotient) ≠ 1 ↔ I ≠ ⊤ := not_congr zero_eq_one_iff protected theorem nontrivial {I : ideal α} (hI : I ≠ ⊤) : nontrivial I.quotient := ⟨⟨0, 1, zero_ne_one_iff.2 hI⟩⟩ lemma mk_surjective : function.surjective (mk I) := λ y, quotient.induction_on' y (λ x, exists.intro x rfl) instance (I : ideal α) [hI : I.is_prime] : integral_domain I.quotient := { eq_zero_or_eq_zero_of_mul_eq_zero := λ a b, quotient.induction_on₂' a b $ λ a b hab, (hI.mem_or_mem (eq_zero_iff_mem.1 hab)).elim (or.inl ∘ eq_zero_iff_mem.2) (or.inr ∘ eq_zero_iff_mem.2), .. quotient.comm_ring I, .. quotient.nontrivial hI.1 } lemma exists_inv {I : ideal α} [hI : I.is_maximal] : ∀ {a : I.quotient}, a ≠ 0 → ∃ b : I.quotient, a * b = 1 := begin rintro ⟨a⟩ h, cases hI.exists_inv (mt eq_zero_iff_mem.2 h) with b hb, rw [mul_comm] at hb, exact ⟨mk _ b, quot.sound hb⟩ end /-- quotient by maximal ideal is a field. def rather than instance, since users will have computable inverses in some applications -/ protected noncomputable def field (I : ideal α) [hI : I.is_maximal] : field I.quotient := { inv := λ a, if ha : a = 0 then 0 else classical.some (exists_inv ha), mul_inv_cancel := λ a (ha : a ≠ 0), show a * dite _ _ _ = _, by rw dif_neg ha; exact classical.some_spec (exists_inv ha), inv_zero := dif_pos rfl, ..quotient.integral_domain I } variable [comm_ring β] /-- Given a ring homomorphism `f : α →+* β` sending all elements of an ideal to zero, lift it to the quotient by this ideal. -/ def lift (S : ideal α) (f : α →+* β) (H : ∀ (a : α), a ∈ S → f a = 0) : quotient S →+* β := { to_fun := λ x, quotient.lift_on' x f $ λ (a b) (h : _ ∈ _), eq_of_sub_eq_zero $ by rw [← f.map_sub, H _ h], map_one' := f.map_one, map_zero' := f.map_zero, map_add' := λ a₁ a₂, quotient.induction_on₂' a₁ a₂ f.map_add, map_mul' := λ a₁ a₂, quotient.induction_on₂' a₁ a₂ f.map_mul } @[simp] lemma lift_mk (S : ideal α) (f : α →+* β) (H : ∀ (a : α), a ∈ S → f a = 0) : lift S f H (mk S a) = f a := rfl end quotient section lattice variables {R : Type u} [comm_ring R] theorem mem_Inf {s : set (ideal R)} {x : R} : x ∈ Inf s ↔ ∀ ⦃I⦄, I ∈ s → x ∈ I := ⟨λ hx I his, hx I ⟨I, infi_pos his⟩, λ H I ⟨J, hij⟩, hij ▸ λ S ⟨hj, hS⟩, hS ▸ H hj⟩ end lattice /-- All ideals in a field are trivial. -/ lemma eq_bot_or_top {K : Type u} [field K] (I : ideal K) : I = ⊥ ∨ I = ⊤ := begin rw classical.or_iff_not_imp_right, change _ ≠ _ → _, rw ideal.ne_top_iff_one, intro h1, rw eq_bot_iff, intros r hr, by_cases H : r = 0, {simpa}, simpa [H, h1] using submodule.smul_mem I r⁻¹ hr, end lemma eq_bot_of_prime {K : Type u} [field K] (I : ideal K) [h : I.is_prime] : I = ⊥ := classical.or_iff_not_imp_right.mp I.eq_bot_or_top h.1 lemma bot_is_maximal {K : Type u} [field K] : is_maximal (⊥ : ideal K) := ⟨λ h, absurd ((eq_top_iff_one (⊤ : ideal K)).mp rfl) (by rw ← h; simp), λ I hI, or_iff_not_imp_left.mp (eq_bot_or_top I) (ne_of_gt hI)⟩ end ideal /-- The set of non-invertible elements of a monoid. -/ def nonunits (α : Type u) [monoid α] : set α := { a | ¬is_unit a } @[simp] theorem mem_nonunits_iff [comm_monoid α] : a ∈ nonunits α ↔ ¬ is_unit a := iff.rfl theorem mul_mem_nonunits_right [comm_monoid α] : b ∈ nonunits α → a * b ∈ nonunits α := mt is_unit_of_mul_is_unit_right theorem mul_mem_nonunits_left [comm_monoid α] : a ∈ nonunits α → a * b ∈ nonunits α := mt is_unit_of_mul_is_unit_left theorem zero_mem_nonunits [semiring α] : 0 ∈ nonunits α ↔ (0:α) ≠ 1 := not_congr is_unit_zero_iff @[simp] theorem one_not_mem_nonunits [monoid α] : (1:α) ∉ nonunits α := not_not_intro is_unit_one theorem coe_subset_nonunits [comm_ring α] {I : ideal α} (h : I ≠ ⊤) : (I : set α) ⊆ nonunits α := λ x hx hu, h $ I.eq_top_of_is_unit_mem hx hu lemma exists_max_ideal_of_mem_nonunits [comm_ring α] (h : a ∈ nonunits α) : ∃ I : ideal α, I.is_maximal ∧ a ∈ I := begin have : ideal.span ({a} : set α) ≠ ⊤, { intro H, rw ideal.span_singleton_eq_top at H, contradiction }, rcases ideal.exists_le_maximal _ this with ⟨I, Imax, H⟩, use [I, Imax], apply H, apply ideal.subset_span, exact set.mem_singleton a end section prio set_option default_priority 100 -- see Note [default priority] /-- A commutative ring is local if it has a unique maximal ideal. Note that `local_ring` is a predicate. -/ class local_ring (α : Type u) [comm_ring α] extends nontrivial α : Prop := (is_local : ∀ (a : α), (is_unit a) ∨ (is_unit (1 - a))) end prio namespace local_ring variables [comm_ring α] [local_ring α] lemma is_unit_or_is_unit_one_sub_self (a : α) : (is_unit a) ∨ (is_unit (1 - a)) := is_local a lemma is_unit_of_mem_nonunits_one_sub_self (a : α) (h : (1 - a) ∈ nonunits α) : is_unit a := or_iff_not_imp_right.1 (is_local a) h lemma is_unit_one_sub_self_of_mem_nonunits (a : α) (h : a ∈ nonunits α) : is_unit (1 - a) := or_iff_not_imp_left.1 (is_local a) h lemma nonunits_add {x y} (hx : x ∈ nonunits α) (hy : y ∈ nonunits α) : x + y ∈ nonunits α := begin rintros ⟨u, hu⟩, apply hy, suffices : is_unit ((↑u⁻¹ : α) * y), { rcases this with ⟨s, hs⟩, use u * s, convert congr_arg (λ z, (u : α) * z) hs, rw ← mul_assoc, simp }, rw show (↑u⁻¹ * y) = (1 - ↑u⁻¹ * x), { rw eq_sub_iff_add_eq, replace hu := congr_arg (λ z, (↑u⁻¹ : α) * z) hu.symm, simpa [mul_add, add_comm] using hu }, apply is_unit_one_sub_self_of_mem_nonunits, exact mul_mem_nonunits_right hx end variable (α) /-- The ideal of elements that are not units. -/ def maximal_ideal : ideal α := { carrier := nonunits α, zero_mem' := zero_mem_nonunits.2 $ zero_ne_one, add_mem' := λ x y hx hy, nonunits_add hx hy, smul_mem' := λ a x, mul_mem_nonunits_right } instance maximal_ideal.is_maximal : (maximal_ideal α).is_maximal := begin rw ideal.is_maximal_iff, split, { intro h, apply h, exact is_unit_one }, { intros I x hI hx H, erw not_not at hx, rcases hx with ⟨u,rfl⟩, simpa using I.smul_mem ↑u⁻¹ H } end lemma maximal_ideal_unique : ∃! I : ideal α, I.is_maximal := ⟨maximal_ideal α, maximal_ideal.is_maximal α, λ I hI, hI.eq_of_le (maximal_ideal.is_maximal α).1 $ λ x hx, hI.1 ∘ I.eq_top_of_is_unit_mem hx⟩ variable {α} lemma eq_maximal_ideal {I : ideal α} (hI : I.is_maximal) : I = maximal_ideal α := unique_of_exists_unique (maximal_ideal_unique α) hI $ maximal_ideal.is_maximal α lemma le_maximal_ideal {J : ideal α} (hJ : J ≠ ⊤) : J ≤ maximal_ideal α := begin rcases ideal.exists_le_maximal J hJ with ⟨M, hM1, hM2⟩, rwa ←eq_maximal_ideal hM1 end @[simp] lemma mem_maximal_ideal (x) : x ∈ maximal_ideal α ↔ x ∈ nonunits α := iff.rfl end local_ring lemma local_of_nonunits_ideal [comm_ring α] (hnze : (0:α) ≠ 1) (h : ∀ x y ∈ nonunits α, x + y ∈ nonunits α) : local_ring α := { exists_pair_ne := ⟨0, 1, hnze⟩, is_local := λ x, or_iff_not_imp_left.mpr $ λ hx, begin by_contra H, apply h _ _ hx H, simp [-sub_eq_add_neg, add_sub_cancel'_right] end } lemma local_of_unique_max_ideal [comm_ring α] (h : ∃! I : ideal α, I.is_maximal) : local_ring α := local_of_nonunits_ideal (let ⟨I, Imax, _⟩ := h in (λ (H : 0 = 1), Imax.1 $ I.eq_top_iff_one.2 $ H ▸ I.zero_mem)) $ λ x y hx hy H, let ⟨I, Imax, Iuniq⟩ := h in let ⟨Ix, Ixmax, Hx⟩ := exists_max_ideal_of_mem_nonunits hx in let ⟨Iy, Iymax, Hy⟩ := exists_max_ideal_of_mem_nonunits hy in have xmemI : x ∈ I, from ((Iuniq Ix Ixmax) ▸ Hx), have ymemI : y ∈ I, from ((Iuniq Iy Iymax) ▸ Hy), Imax.1 $ I.eq_top_of_is_unit_mem (I.add_mem xmemI ymemI) H lemma local_of_unique_nonzero_prime (R : Type u) [comm_ring R] (h : ∃! P : ideal R, P ≠ ⊥ ∧ ideal.is_prime P) : local_ring R := local_of_unique_max_ideal begin rcases h with ⟨P, ⟨hPnonzero, hPnot_top, _⟩, hPunique⟩, refine ⟨P, ⟨hPnot_top, _⟩, λ M hM, hPunique _ ⟨_, ideal.is_maximal.is_prime hM⟩⟩, { refine ideal.maximal_of_no_maximal (λ M hPM hM, ne_of_lt hPM _), exact (hPunique _ ⟨ne_bot_of_gt hPM, ideal.is_maximal.is_prime hM⟩).symm }, { rintro rfl, exact hPnot_top (hM.2 P (bot_lt_iff_ne_bot.2 hPnonzero)) }, end section prio set_option default_priority 100 -- see Note [default priority] /-- A local ring homomorphism is a homomorphism between local rings such that the image of the maximal ideal of the source is contained within the maximal ideal of the target. -/ class is_local_ring_hom [semiring α] [semiring β] (f : α →+* β) : Prop := (map_nonunit : ∀ a, is_unit (f a) → is_unit a) end prio @[simp] lemma is_unit_of_map_unit [semiring α] [semiring β] (f : α →+* β) [is_local_ring_hom f] (a) (h : is_unit (f a)) : is_unit a := is_local_ring_hom.map_nonunit a h theorem of_irreducible_map [semiring α] [semiring β] (f : α →+* β) [h : is_local_ring_hom f] {x : α} (hfx : irreducible (f x)) : irreducible x := ⟨λ h, hfx.1 $ is_unit.map f.to_monoid_hom h, λ p q hx, let ⟨H⟩ := h in or.imp (H p) (H q) $ hfx.2 _ _ $ f.map_mul p q ▸ congr_arg f hx⟩ section open local_ring variables [comm_ring α] [local_ring α] [comm_ring β] [local_ring β] variables (f : α →+* β) [is_local_ring_hom f] lemma map_nonunit (a) (h : a ∈ maximal_ideal α) : f a ∈ maximal_ideal β := λ H, h $ is_unit_of_map_unit f a H end namespace local_ring variables [comm_ring α] [local_ring α] [comm_ring β] [local_ring β] variable (α) /-- The residue field of a local ring is the quotient of the ring by its maximal ideal. -/ def residue_field := (maximal_ideal α).quotient noncomputable instance residue_field.field : field (residue_field α) := ideal.quotient.field (maximal_ideal α) noncomputable instance : inhabited (residue_field α) := ⟨37⟩ /-- The quotient map from a local ring to its residue field. -/ def residue : α →+* (residue_field α) := ideal.quotient.mk _ namespace residue_field variables {α β} /-- The map on residue fields induced by a local homomorphism between local rings -/ noncomputable def map (f : α →+* β) [is_local_ring_hom f] : residue_field α →+* residue_field β := ideal.quotient.lift (maximal_ideal α) ((ideal.quotient.mk _).comp f) $ λ a ha, begin erw ideal.quotient.eq_zero_iff_mem, exact map_nonunit f a ha end end residue_field end local_ring namespace field variables [field α] @[priority 100] -- see Note [lower instance priority] instance : local_ring α := { is_local := λ a, if h : a = 0 then or.inr (by rw [h, sub_zero]; exact is_unit_one) else or.inl $ is_unit_of_mul_eq_one a a⁻¹ $ div_self h } end field
a84005e7d984d3383ba37a7c356ce9b52b5644c5
a339bc2ac96174381fb610f4b2e1ba42df2be819
/hott/homotopy/wedge.hlean
633f8f3c6657dbaf71d8a49243332896ae2f3355
[ "Apache-2.0" ]
permissive
kalfsvag/lean2
25b2dccc07a98e5aa20f9a11229831f9d3edf2e7
4d4a0c7c53a9922c5f630f6f8ebdccf7ddef2cc7
refs/heads/master
1,610,513,122,164
1,483,135,198,000
1,483,135,198,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,617
hlean
/- Copyright (c) 2016 Jakob von Raumer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jakob von Raumer, Ulrik Buchholtz The Wedge Sum of Two Pointed Types -/ import hit.pushout .connectedness types.unit open eq pushout pointed unit trunc_index definition wedge (A B : Type*) : Type := ppushout (pconst punit A) (pconst punit B) local attribute wedge [reducible] definition pwedge (A B : Type*) : Type* := pointed.mk' (wedge A B) namespace wedge protected definition rec {A B : Type*} {P : wedge A B → Type} (Pinl : Π(x : A), P (inl x)) (Pinr : Π(x : B), P (inr x)) (Pglue : pathover P (Pinl pt) (glue ⋆) (Pinr pt)) (y : wedge A B) : P y := by induction y; apply Pinl; apply Pinr; induction x; exact Pglue protected definition elim {A B : Type*} {P : Type} (Pinl : A → P) (Pinr : B → P) (Pglue : Pinl pt = Pinr pt) (y : wedge A B) : P := by induction y with a b x; exact Pinl a; exact Pinr b; induction x; exact Pglue end wedge attribute wedge.rec wedge.elim [recursor 7] [unfold 7] namespace wedge -- TODO maybe find a cleaner proof protected definition unit (A : Type*) : A ≃* pwedge punit A := begin fapply pequiv_of_pmap, { fapply pmap.mk, intro a, apply pinr a, apply respect_pt }, { fapply is_equiv.adjointify, intro x, fapply pushout.elim_on x, exact λ x, Point A, exact id, intro u, reflexivity, intro x, fapply pushout.rec_on x, intro u, cases u, esimp, apply (glue unit.star)⁻¹, intro a, reflexivity, intro u, cases u, esimp, apply eq_pathover, refine _ ⬝hp !ap_id⁻¹, fapply eq_hconcat, apply ap_compose inr, krewrite elim_glue, fapply eq_hconcat, apply ap_idp, apply square_of_eq, apply con.left_inv, intro a, reflexivity}, end end wedge open trunc is_trunc is_conn function namespace wedge_extension section -- The wedge connectivity lemma (Lemma 8.6.2) parameters {A B : Type*} (n m : ℕ) [cA : is_conn n A] [cB : is_conn m B] (P : A → B → Type) [HP : Πa b, is_trunc (m + n) (P a b)] (f : Πa : A, P a pt) (g : Πb : B, P pt b) (p : f pt = g pt) include cA cB HP private definition Q (a : A) : Type := fiber (λs : (Πb : B, P a b), s (Point B)) (f a) private definition is_trunc_Q (a : A) : is_trunc (n.-1) (Q a) := begin refine @is_conn.elim_general (m.-1) _ _ _ (P a) _ (f a), rewrite [-succ_add_succ, of_nat_add_of_nat], intro b, apply HP end local attribute is_trunc_Q [instance] private definition Q_sec : Πa : A, Q a := is_conn.elim (n.-1) Q (fiber.mk g p⁻¹) protected definition ext : Π(a : A)(b : B), P a b := λa, fiber.point (Q_sec a) protected definition β_left (a : A) : ext a (Point B) = f a := fiber.point_eq (Q_sec a) private definition coh_aux : Σq : ext (Point A) = g, β_left (Point A) = ap (λs : (Πb : B, P (Point A) b), s (Point B)) q ⬝ p⁻¹ := equiv.to_fun (fiber.fiber_eq_equiv (Q_sec (Point A)) (fiber.mk g p⁻¹)) (is_conn.elim_β (n.-1) Q (fiber.mk g p⁻¹)) protected definition β_right (b : B) : ext (Point A) b = g b := apd10 (sigma.pr1 coh_aux) b private definition lem : β_left (Point A) = β_right (Point B) ⬝ p⁻¹ := begin unfold β_right, unfold β_left, krewrite (apd10_eq_ap_eval (sigma.pr1 coh_aux) (Point B)), exact sigma.pr2 coh_aux, end protected definition coh : (β_left (Point A))⁻¹ ⬝ β_right (Point B) = p := by rewrite [lem,con_inv,inv_inv,con.assoc,con.left_inv] end end wedge_extension
5693669ba4e4d6660a1ee8245872820904664571
5756a081670ba9c1d1d3fca7bd47cb4e31beae66
/Oneshot/lean3-in/main.lean
78b673949cf9c7b7d2cbaf0d6b2a2289e850b9a7
[ "Apache-2.0" ]
permissive
leanprover-community/mathport
2c9bdc8292168febf59799efdc5451dbf0450d4a
13051f68064f7638970d39a8fecaede68ffbf9e1
refs/heads/master
1,693,841,364,079
1,693,813,111,000
1,693,813,111,000
379,357,010
27
10
Apache-2.0
1,691,309,132,000
1,624,384,521,000
Lean
UTF-8
Lean
false
false
144
lean
-- Insert lean 3 code here. import other namespace foo /-- test -/ @[simp] def foo := other theorem foo_eq_one : foo.foo = 1 := rfl end foo
8d6e52dc5b3f47cc5f3a422c49a7d4b8587263e9
842b7df4a999c5c50bbd215b8617dd705e43c2e1
/nat_num_game/src/Advanced_Proposition_World/adv_prop_wrld7.lean
034e07d0c5379b82890b5bdc1eb2c34bb2fd9a34
[]
no_license
Samyak-Surti/LeanCode
1c245631f74b00057d20483c8ac75916e8643b14
944eac3e5f43e2614ed246083b97fbdf24181d83
refs/heads/master
1,669,023,730,828
1,595,534,784,000
1,595,534,784,000
282,037,186
0
0
null
null
null
null
UTF-8
Lean
false
false
147
lean
lemma or_symm (P Q : Prop) : P ∨ Q → Q ∨ P := begin intro fpq, cases fpq with p q, right, exact p, left, exact q, end
64b8e1e2281f7abb524da425a43ba90f22ca4824
968e2f50b755d3048175f176376eff7139e9df70
/examples/prop_logic_lean_summary/unnamed_484.lean
989b8a7fa57bb370b0fb2eb753ea302fe3416e16
[]
no_license
gihanmarasingha/mth1001_sphinx
190a003269ba5e54717b448302a27ca26e31d491
05126586cbf5786e521be1ea2ef5b4ba3c44e74a
refs/heads/master
1,672,913,933,677
1,604,516,583,000
1,604,516,583,000
309,245,750
1
0
null
null
null
null
UTF-8
Lean
false
false
112
lean
variables p q : Prop -- BEGIN example (h₁ : p → q) (h₂ : q → p) : p ↔ q := iff.intro h₁ h₂ -- END
191350db7ebb64765f25b55c08408798b2950feb
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/control/traversable/instances.lean
5df7144d64268c44a809249ac09fd212eb975a79
[]
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,819
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Simon Hudon Instances of `traversable` for types from the core library -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.list.forall2 import Mathlib.data.set.lattice import Mathlib.control.traversable.lemmas import Mathlib.PostPort universes u_1 u namespace Mathlib theorem option.id_traverse {α : Type u_1} (x : Option α) : option.traverse id.mk x = x := option.cases_on x (Eq.refl (option.traverse id.mk none)) fun (x : α) => Eq.refl (option.traverse id.mk (some x)) theorem option.comp_traverse {F : Type u → Type u} {G : Type u → Type u} [Applicative F] [Applicative G] [is_lawful_applicative F] [is_lawful_applicative G] {α : Type u_1} {β : Type u} {γ : Type u} (f : β → F γ) (g : α → G β) (x : Option α) : option.traverse (functor.comp.mk ∘ Functor.map f ∘ g) x = functor.comp.mk (option.traverse f <$> option.traverse g x) := sorry theorem option.traverse_eq_map_id {α : Type u_1} {β : Type u_1} (f : α → β) (x : Option α) : traverse (id.mk ∘ f) x = id.mk (f <$> x) := option.cases_on x (Eq.refl (traverse (id.mk ∘ f) none)) fun (x : α) => Eq.refl (traverse (id.mk ∘ f) (some x)) theorem option.naturality {F : Type u → Type u} {G : Type u → Type u} [Applicative F] [Applicative G] [is_lawful_applicative F] [is_lawful_applicative G] (η : applicative_transformation F G) {α : Type u_1} {β : Type u} (f : α → F β) (x : Option α) : coe_fn η (Option β) (option.traverse f x) = option.traverse (coe_fn η β ∘ f) x := sorry protected instance option.is_lawful_traversable : is_lawful_traversable Option := is_lawful_traversable.mk option.id_traverse option.comp_traverse option.traverse_eq_map_id option.naturality namespace list protected theorem id_traverse {α : Type u_1} (xs : List α) : list.traverse id.mk xs = xs := sorry protected theorem comp_traverse {F : Type u → Type u} {G : Type u → Type u} [Applicative F] [Applicative G] [is_lawful_applicative F] [is_lawful_applicative G] {α : Type u_1} {β : Type u} {γ : Type u} (f : β → F γ) (g : α → G β) (x : List α) : list.traverse (functor.comp.mk ∘ Functor.map f ∘ g) x = functor.comp.mk (list.traverse f <$> list.traverse g x) := sorry protected theorem traverse_eq_map_id {α : Type u_1} {β : Type u_1} (f : α → β) (x : List α) : list.traverse (id.mk ∘ f) x = id.mk (f <$> x) := sorry protected theorem naturality {F : Type u → Type u} {G : Type u → Type u} [Applicative F] [Applicative G] [is_lawful_applicative F] [is_lawful_applicative G] (η : applicative_transformation F G) {α : Type u_1} {β : Type u} (f : α → F β) (x : List α) : coe_fn η (List β) (list.traverse f x) = list.traverse (coe_fn η β ∘ f) x := sorry protected instance is_lawful_traversable : is_lawful_traversable List := is_lawful_traversable.mk list.id_traverse list.comp_traverse list.traverse_eq_map_id list.naturality @[simp] theorem traverse_nil {F : Type u → Type u} [Applicative F] {α' : Type u} {β' : Type u} (f : α' → F β') : traverse f [] = pure [] := rfl @[simp] theorem traverse_cons {F : Type u → Type u} [Applicative F] {α' : Type u} {β' : Type u} (f : α' → F β') (a : α') (l : List α') : traverse f (a :: l) = (fun (_x : β') (_y : List β') => _x :: _y) <$> f a <*> traverse f l := rfl @[simp] theorem traverse_append {F : Type u → Type u} [Applicative F] {α' : Type u} {β' : Type u} (f : α' → F β') [is_lawful_applicative F] (as : List α') (bs : List α') : traverse f (as ++ bs) = append <$> traverse f as <*> traverse f bs := sorry theorem mem_traverse {α' : Type u} {β' : Type u} {f : α' → set β'} (l : List α') (n : List β') : n ∈ traverse f l ↔ forall₂ (fun (b : β') (a : α') => b ∈ f a) n l := sorry end list namespace sum protected theorem traverse_map {σ : Type u} {G : Type u → Type u} [Applicative G] {α : Type u} {β : Type u} {γ : Type u} (g : α → β) (f : β → G γ) (x : σ ⊕ α) : sum.traverse f (g <$> x) = sum.traverse (f ∘ g) x := sorry protected theorem id_traverse {σ : Type u_1} {α : Type u_1} (x : σ ⊕ α) : sum.traverse id.mk x = x := sum.cases_on x (fun (x : σ) => Eq.refl (sum.traverse id.mk (inl x))) fun (x : α) => Eq.refl (sum.traverse id.mk (inr x)) protected theorem comp_traverse {σ : Type u} {F : Type u → Type u} {G : Type u → Type u} [Applicative F] [Applicative G] [is_lawful_applicative F] [is_lawful_applicative G] {α : Type u_1} {β : Type u} {γ : Type u} (f : β → F γ) (g : α → G β) (x : σ ⊕ α) : sum.traverse (functor.comp.mk ∘ Functor.map f ∘ g) x = functor.comp.mk (sum.traverse f <$> sum.traverse g x) := sorry protected theorem traverse_eq_map_id {σ : Type u} {α : Type u} {β : Type u} (f : α → β) (x : σ ⊕ α) : sum.traverse (id.mk ∘ f) x = id.mk (f <$> x) := sorry protected theorem map_traverse {σ : Type u} {G : Type u → Type u} [Applicative G] [is_lawful_applicative G] {α : Type u_1} {β : Type u} {γ : Type u} (g : α → G β) (f : β → γ) (x : σ ⊕ α) : Functor.map f <$> sum.traverse g x = sum.traverse (Functor.map f ∘ g) x := sorry protected theorem naturality {σ : Type u} {F : Type u → Type u} {G : Type u → Type u} [Applicative F] [Applicative G] [is_lawful_applicative F] [is_lawful_applicative G] (η : applicative_transformation F G) {α : Type u_1} {β : Type u} (f : α → F β) (x : σ ⊕ α) : coe_fn η (σ ⊕ β) (sum.traverse f x) = sum.traverse (coe_fn η β ∘ f) x := sorry protected instance is_lawful_traversable {σ : Type u} : is_lawful_traversable (sum σ) := is_lawful_traversable.mk sum.id_traverse sum.comp_traverse sum.traverse_eq_map_id sum.naturality
22cf03975a5dc989d08a744e9845e8fca0d04d9f
e6b8240a90527fd55d42d0ec6649253d5d0bd414
/src/topology/instances/real.lean
d9ff22c75e3f2dcaa6341a2c457d093306f4a289
[ "Apache-2.0" ]
permissive
mattearnshaw/mathlib
ac90f9fb8168aa642223bea3ffd0286b0cfde44f
d8dc1445cf8a8c74f8df60b9f7a1f5cf10946666
refs/heads/master
1,606,308,351,137
1,576,594,130,000
1,576,594,130,000
228,666,195
0
0
Apache-2.0
1,576,603,094,000
1,576,603,093,000
null
UTF-8
Lean
false
false
19,385
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 The real numbers ℝ. They are constructed as the topological completion of ℚ. With the following steps: (1) prove that ℚ forms a uniform space. (2) subtraction and addition are uniform continuous functions in this space (3) for multiplication and inverse this only holds on bounded subsets (4) ℝ is defined as separated Cauchy filters over ℚ (the separation requires a quotient construction) (5) extend the uniform continuous functions along the completion (6) proof field properties using the principle of extension of identities TODO generalizations: * topological groups & rings * order topologies * Archimedean fields -/ import topology.metric_space.basic topology.algebra.uniform_group topology.algebra.ring tactic.linarith noncomputable theory open classical set lattice filter topological_space metric open_locale classical open_locale topological_space universes u v w variables {α : Type u} {β : Type v} {γ : Type w} instance : metric_space ℚ := metric_space.induced coe rat.cast_injective real.metric_space theorem rat.dist_eq (x y : ℚ) : dist x y = abs (x - y) := rfl @[elim_cast, simp] lemma rat.dist_cast (x y : ℚ) : dist (x : ℝ) y = dist x y := rfl section low_prio -- we want to ignore this instance for the next declaration local attribute [instance, priority 10] int.uniform_space instance : metric_space ℤ := begin letI M := metric_space.induced coe int.cast_injective real.metric_space, refine @metric_space.replace_uniformity _ int.uniform_space M (le_antisymm refl_le_uniformity $ λ r ru, mem_uniformity_dist.2 ⟨1, zero_lt_one, λ a b h, mem_principal_sets.1 ru $ dist_le_zero.1 (_ : (abs (a - b) : ℝ) ≤ 0)⟩), simpa using (@int.cast_le ℝ _ _ 0).2 (int.lt_add_one_iff.1 $ (@int.cast_lt ℝ _ (abs (a - b)) 1).1 $ by simpa using h) end end low_prio theorem int.dist_eq (x y : ℤ) : dist x y = abs (x - y) := rfl @[elim_cast, simp] theorem int.dist_cast_real (x y : ℤ) : dist (x : ℝ) y = dist x y := rfl @[elim_cast, simp] theorem int.dist_cast_rat (x y : ℤ) : dist (x : ℚ) y = dist x y := by rw [← int.dist_cast_real, ← rat.dist_cast]; congr' 1; norm_cast theorem uniform_continuous_of_rat : uniform_continuous (coe : ℚ → ℝ) := uniform_continuous_comap theorem uniform_embedding_of_rat : uniform_embedding (coe : ℚ → ℝ) := uniform_embedding_comap rat.cast_injective theorem dense_embedding_of_rat : dense_embedding (coe : ℚ → ℝ) := uniform_embedding_of_rat.dense_embedding $ λ x, mem_closure_iff_nhds.2 $ λ t ht, let ⟨ε,ε0, hε⟩ := mem_nhds_iff.1 ht in let ⟨q, h⟩ := exists_rat_near x ε0 in ne_empty_iff_exists_mem.2 ⟨_, hε (mem_ball'.2 h), q, rfl⟩ theorem embedding_of_rat : embedding (coe : ℚ → ℝ) := dense_embedding_of_rat.to_embedding theorem continuous_of_rat : continuous (coe : ℚ → ℝ) := uniform_continuous_of_rat.continuous theorem real.uniform_continuous_add : uniform_continuous (λp : ℝ × ℝ, p.1 + p.2) := metric.uniform_continuous_iff.2 $ λ ε ε0, let ⟨δ, δ0, Hδ⟩ := rat_add_continuous_lemma abs ε0 in ⟨δ, δ0, λ a b h, let ⟨h₁, h₂⟩ := max_lt_iff.1 h in Hδ h₁ h₂⟩ -- TODO(Mario): Find a way to use rat_add_continuous_lemma theorem rat.uniform_continuous_add : uniform_continuous (λp : ℚ × ℚ, p.1 + p.2) := uniform_embedding_of_rat.to_uniform_inducing.uniform_continuous_iff.2 $ by simp [(∘)]; exact real.uniform_continuous_add.comp ((uniform_continuous_of_rat.comp uniform_continuous_fst).prod_mk (uniform_continuous_of_rat.comp uniform_continuous_snd)) theorem real.uniform_continuous_neg : uniform_continuous (@has_neg.neg ℝ _) := metric.uniform_continuous_iff.2 $ λ ε ε0, ⟨_, ε0, λ a b h, by rw dist_comm at h; simpa [real.dist_eq] using h⟩ theorem rat.uniform_continuous_neg : uniform_continuous (@has_neg.neg ℚ _) := metric.uniform_continuous_iff.2 $ λ ε ε0, ⟨_, ε0, λ a b h, by rw dist_comm at h; simpa [rat.dist_eq] using h⟩ instance : uniform_add_group ℝ := uniform_add_group.mk' real.uniform_continuous_add real.uniform_continuous_neg instance : uniform_add_group ℚ := uniform_add_group.mk' rat.uniform_continuous_add rat.uniform_continuous_neg -- short-circuit type class inference instance : topological_add_group ℝ := by apply_instance instance : topological_add_group ℚ := by apply_instance instance : orderable_topology ℚ := induced_orderable_topology _ (λ x y, rat.cast_lt) (@exists_rat_btwn _ _ _) lemma real.is_topological_basis_Ioo_rat : @is_topological_basis ℝ _ (⋃(a b : ℚ) (h : a < b), {Ioo a b}) := is_topological_basis_of_open_of_nhds (by simp [is_open_Ioo] {contextual:=tt}) (assume a v hav hv, let ⟨l, u, hl, hu, h⟩ := (mem_nhds_unbounded (no_top _) (no_bot _)).mp (mem_nhds_sets hv hav), ⟨q, hlq, hqa⟩ := exists_rat_btwn hl, ⟨p, hap, hpu⟩ := exists_rat_btwn hu in ⟨Ioo q p, by simp; exact ⟨q, p, rat.cast_lt.1 $ lt_trans hqa hap, rfl⟩, ⟨hqa, hap⟩, assume a' ⟨hqa', ha'p⟩, h _ (lt_trans hlq hqa') (lt_trans ha'p hpu)⟩) instance : second_countable_topology ℝ := ⟨⟨(⋃(a b : ℚ) (h : a < b), {Ioo a b}), by simp [countable_Union, countable_Union_Prop], real.is_topological_basis_Ioo_rat.2.2⟩⟩ /- TODO(Mario): Prove that these are uniform isomorphisms instead of uniform embeddings lemma uniform_embedding_add_rat {r : ℚ} : uniform_embedding (λp:ℚ, p + r) := _ lemma uniform_embedding_mul_rat {q : ℚ} (hq : q ≠ 0) : uniform_embedding ((*) q) := _ -/ lemma real.uniform_continuous_inv (s : set ℝ) {r : ℝ} (r0 : 0 < r) (H : ∀ x ∈ s, r ≤ abs x) : uniform_continuous (λp:s, p.1⁻¹) := metric.uniform_continuous_iff.2 $ λ ε ε0, let ⟨δ, δ0, Hδ⟩ := rat_inv_continuous_lemma abs ε0 r0 in ⟨δ, δ0, λ a b h, Hδ (H _ a.2) (H _ b.2) h⟩ lemma real.uniform_continuous_abs : uniform_continuous (abs : ℝ → ℝ) := metric.uniform_continuous_iff.2 $ λ ε ε0, ⟨ε, ε0, λ a b, lt_of_le_of_lt (abs_abs_sub_abs_le_abs_sub _ _)⟩ lemma real.continuous_abs : continuous (abs : ℝ → ℝ) := real.uniform_continuous_abs.continuous lemma rat.uniform_continuous_abs : uniform_continuous (abs : ℚ → ℚ) := metric.uniform_continuous_iff.2 $ λ ε ε0, ⟨ε, ε0, λ a b h, lt_of_le_of_lt (by simpa [rat.dist_eq] using abs_abs_sub_abs_le_abs_sub _ _) h⟩ lemma rat.continuous_abs : continuous (abs : ℚ → ℚ) := rat.uniform_continuous_abs.continuous lemma real.tendsto_inv {r : ℝ} (r0 : r ≠ 0) : tendsto (λq, q⁻¹) (𝓝 r) (𝓝 r⁻¹) := by rw ← abs_pos_iff at r0; exact tendsto_of_uniform_continuous_subtype (real.uniform_continuous_inv {x | abs r / 2 < abs x} (half_pos r0) (λ x h, le_of_lt h)) (mem_nhds_sets (real.continuous_abs _ $ is_open_lt' (abs r / 2)) (half_lt_self r0)) lemma real.continuous_inv : continuous (λa:{r:ℝ // r ≠ 0}, a.val⁻¹) := continuous_iff_continuous_at.mpr $ assume ⟨r, hr⟩, tendsto.comp (real.tendsto_inv hr) (continuous_iff_continuous_at.mp continuous_subtype_val _) lemma real.continuous.inv [topological_space α] {f : α → ℝ} (h : ∀a, f a ≠ 0) (hf : continuous f) : continuous (λa, (f a)⁻¹) := show continuous ((has_inv.inv ∘ @subtype.val ℝ (λr, r ≠ 0)) ∘ λa, ⟨f a, h a⟩), from real.continuous_inv.comp (continuous_subtype_mk _ hf) lemma real.uniform_continuous_mul_const {x : ℝ} : uniform_continuous ((*) x) := metric.uniform_continuous_iff.2 $ λ ε ε0, begin cases no_top (abs x) with y xy, have y0 := lt_of_le_of_lt (abs_nonneg _) xy, refine ⟨_, div_pos ε0 y0, λ a b h, _⟩, rw [real.dist_eq, ← mul_sub, abs_mul, ← mul_div_cancel' ε (ne_of_gt y0)], exact mul_lt_mul' (le_of_lt xy) h (abs_nonneg _) y0 end lemma real.uniform_continuous_mul (s : set (ℝ × ℝ)) {r₁ r₂ : ℝ} (H : ∀ x ∈ s, abs (x : ℝ × ℝ).1 < r₁ ∧ abs x.2 < r₂) : uniform_continuous (λp:s, p.1.1 * p.1.2) := metric.uniform_continuous_iff.2 $ λ ε ε0, let ⟨δ, δ0, Hδ⟩ := rat_mul_continuous_lemma abs ε0 in ⟨δ, δ0, λ a b h, let ⟨h₁, h₂⟩ := max_lt_iff.1 h in Hδ (H _ a.2).1 (H _ b.2).2 h₁ h₂⟩ protected lemma real.continuous_mul : continuous (λp : ℝ × ℝ, p.1 * p.2) := continuous_iff_continuous_at.2 $ λ ⟨a₁, a₂⟩, tendsto_of_uniform_continuous_subtype (real.uniform_continuous_mul ({x | abs x < abs a₁ + 1}.prod {x | abs x < abs a₂ + 1}) (λ x, id)) (mem_nhds_sets (is_open_prod (real.continuous_abs _ $ is_open_gt' (abs a₁ + 1)) (real.continuous_abs _ $ is_open_gt' (abs a₂ + 1))) ⟨lt_add_one (abs a₁), lt_add_one (abs a₂)⟩) instance : topological_ring ℝ := { continuous_mul := real.continuous_mul, ..real.topological_add_group } instance : topological_semiring ℝ := by apply_instance -- short-circuit type class inference lemma rat.continuous_mul : continuous (λp : ℚ × ℚ, p.1 * p.2) := embedding_of_rat.continuous_iff.2 $ by simp [(∘)]; exact real.continuous_mul.comp ((continuous_of_rat.comp continuous_fst).prod_mk (continuous_of_rat.comp continuous_snd)) instance : topological_ring ℚ := { continuous_mul := rat.continuous_mul, ..rat.topological_add_group } theorem real.ball_eq_Ioo (x ε : ℝ) : ball x ε = Ioo (x - ε) (x + ε) := set.ext $ λ y, by rw [mem_ball, real.dist_eq, abs_sub_lt_iff, sub_lt_iff_lt_add', and_comm, sub_lt]; refl theorem real.Ioo_eq_ball (x y : ℝ) : Ioo x y = ball ((x + y) / 2) ((y - x) / 2) := by rw [real.ball_eq_Ioo, ← sub_div, add_comm, ← sub_add, add_sub_cancel', add_self_div_two, ← add_div, add_assoc, add_sub_cancel'_right, add_self_div_two] lemma real.totally_bounded_Ioo (a b : ℝ) : totally_bounded (Ioo a b) := metric.totally_bounded_iff.2 $ λ ε ε0, begin rcases exists_nat_gt ((b - a) / ε) with ⟨n, ba⟩, rw [div_lt_iff' ε0, sub_lt_iff_lt_add'] at ba, let s := (λ i:ℕ, a + ε * i) '' {i:ℕ | i < n}, refine ⟨s, finite_image _ ⟨set.fintype_lt_nat _⟩, λ x h, _⟩, rcases h with ⟨ax, xb⟩, let i : ℕ := ⌊(x - a) / ε⌋.to_nat, have : (i : ℤ) = ⌊(x - a) / ε⌋ := int.to_nat_of_nonneg (floor_nonneg.2 $ le_of_lt (div_pos (sub_pos.2 ax) ε0)), simp, refine ⟨_, ⟨i, _, rfl⟩, _⟩, { rw [← int.coe_nat_lt, this], refine int.cast_lt.1 (lt_of_le_of_lt (floor_le _) _), rw [int.cast_coe_nat, div_lt_iff' ε0, sub_lt_iff_lt_add'], exact lt_trans xb ba }, { rw [real.dist_eq, ← int.cast_coe_nat, this, abs_of_nonneg, ← sub_sub, sub_lt_iff_lt_add'], { have := lt_floor_add_one ((x - a) / ε), rwa [div_lt_iff' ε0, mul_add, mul_one] at this }, { have := floor_le ((x - a) / ε), rwa [ge, sub_nonneg, ← le_sub_iff_add_le', ← le_div_iff' ε0] } } end lemma real.totally_bounded_ball (x ε : ℝ) : totally_bounded (ball x ε) := by rw real.ball_eq_Ioo; apply real.totally_bounded_Ioo lemma real.totally_bounded_Ico (a b : ℝ) : totally_bounded (Ico a b) := let ⟨c, ac⟩ := no_bot a in totally_bounded_subset (by exact λ x ⟨h₁, h₂⟩, ⟨lt_of_lt_of_le ac h₁, h₂⟩) (real.totally_bounded_Ioo c b) lemma real.totally_bounded_Icc (a b : ℝ) : totally_bounded (Icc a b) := let ⟨c, bc⟩ := no_top b in totally_bounded_subset (by exact λ x ⟨h₁, h₂⟩, ⟨h₁, lt_of_le_of_lt h₂ bc⟩) (real.totally_bounded_Ico a c) lemma rat.totally_bounded_Icc (a b : ℚ) : totally_bounded (Icc a b) := begin have := totally_bounded_preimage uniform_embedding_of_rat (real.totally_bounded_Icc a b), rwa (set.ext (λ q, _) : Icc _ _ = _), simp end -- TODO(Mario): Generalize to first-countable uniform spaces? instance : complete_space ℝ := ⟨λ f cf, begin let g : ℕ → {ε:ℝ//ε>0} := λ n, ⟨n.to_pnat'⁻¹, inv_pos (nat.cast_pos.2 n.to_pnat'.pos)⟩, choose S hS hS_dist using show ∀n:ℕ, ∃t ∈ f.sets, ∀ x y ∈ t, dist x y < g n, from assume n, let ⟨t, tf, h⟩ := (metric.cauchy_iff.1 cf).2 (g n).1 (g n).2 in ⟨t, tf, h⟩, let F : ℕ → set ℝ := λn, ⋂i≤n, S i, have hF : ∀n, F n ∈ f.sets := assume n, Inter_mem_sets (finite_le_nat n) (λ i _, hS i), have hF_dist : ∀n, ∀ x y ∈ F n, dist x y < g n := assume n x y hx hy, have F n ⊆ S n := bInter_subset_of_mem (le_refl n), (hS_dist n) _ _ (this hx) (this hy), choose G hG using assume n:ℕ, inhabited_of_mem_sets cf.1 (hF n), have hg : ∀ ε > 0, ∃ n, ∀ j ≥ n, (g j : ℝ) < ε, { intros ε ε0, cases exists_nat_gt ε⁻¹ with n hn, refine ⟨n, λ j nj, _⟩, have hj := lt_of_lt_of_le hn (nat.cast_le.2 nj), have j0 := lt_trans (inv_pos ε0) hj, have jε := (inv_lt j0 ε0).2 hj, rwa ← pnat.to_pnat'_coe (nat.cast_pos.1 j0) at jε }, let c : cau_seq ℝ abs, { refine ⟨λ n, G n, λ ε ε0, _⟩, cases hg _ ε0 with n hn, refine ⟨n, λ j jn, _⟩, have : F j ⊆ F n := bInter_subset_bInter_left (λ i h, @le_trans _ _ i n j h jn), exact lt_trans (hF_dist n _ _ (this (hG j)) (hG n)) (hn _ $ le_refl _) }, refine ⟨cau_seq.lim c, λ s h, _⟩, rcases metric.mem_nhds_iff.1 h with ⟨ε, ε0, hε⟩, cases exists_forall_ge_and (hg _ $ half_pos ε0) (cau_seq.equiv_lim c _ $ half_pos ε0) with n hn, cases hn _ (le_refl _) with h₁ h₂, refine sets_of_superset _ (hF n) (subset.trans _ $ subset.trans (ball_half_subset (G n) h₂) hε), exact λ x h, lt_trans ((hF_dist n) x (G n) h (hG n)) h₁ end⟩ lemma tendsto_coe_nat_real_at_top_iff {f : α → ℕ} {l : filter α} : tendsto (λ n, (f n : ℝ)) l at_top ↔ tendsto f l at_top := tendsto_at_top_embedding (assume a₁ a₂, nat.cast_le) $ assume r, let ⟨n, hn⟩ := exists_nat_gt r in ⟨n, le_of_lt hn⟩ lemma tendsto_coe_nat_real_at_top_at_top : tendsto (coe : ℕ → ℝ) at_top at_top := tendsto_coe_nat_real_at_top_iff.2 tendsto_id lemma tendsto_coe_int_real_at_top_iff {f : α → ℤ} {l : filter α} : tendsto (λ n, (f n : ℝ)) l at_top ↔ tendsto f l at_top := tendsto_at_top_embedding (assume a₁ a₂, int.cast_le) $ assume r, let ⟨n, hn⟩ := exists_nat_gt r in ⟨(n:ℤ), le_of_lt $ by rwa [int.cast_coe_nat]⟩ lemma tendsto_coe_int_real_at_top_at_top : tendsto (coe : ℤ → ℝ) at_top at_top := tendsto_coe_int_real_at_top_iff.2 tendsto_id section lemma closure_of_rat_image_lt {q : ℚ} : closure ((coe:ℚ → ℝ) '' {x | q < x}) = {r | ↑q ≤ r} := subset.antisymm ((closure_subset_iff_subset_of_is_closed (is_closed_ge' _)).2 (image_subset_iff.2 $ λ p h, le_of_lt $ (@rat.cast_lt ℝ _ _ _).2 h)) $ λ x hx, mem_closure_iff_nhds.2 $ λ t ht, let ⟨ε, ε0, hε⟩ := metric.mem_nhds_iff.1 ht in let ⟨p, h₁, h₂⟩ := exists_rat_btwn ((lt_add_iff_pos_right x).2 ε0) in ne_empty_iff_exists_mem.2 ⟨_, hε (show abs _ < _, by rwa [abs_of_nonneg (le_of_lt $ sub_pos.2 h₁), sub_lt_iff_lt_add']), p, rat.cast_lt.1 (@lt_of_le_of_lt ℝ _ _ _ _ hx h₁), rfl⟩ /- TODO(Mario): Put these back only if needed later lemma closure_of_rat_image_le_eq {q : ℚ} : closure ((coe:ℚ → ℝ) '' {x | q ≤ x}) = {r | ↑q ≤ r} := _ lemma closure_of_rat_image_le_le_eq {a b : ℚ} (hab : a ≤ b) : closure (of_rat '' {q:ℚ | a ≤ q ∧ q ≤ b}) = {r:ℝ | of_rat a ≤ r ∧ r ≤ of_rat b} := _-/ lemma compact_Icc {a b : ℝ} : compact (Icc a b) := compact_of_totally_bounded_is_closed (real.totally_bounded_Icc a b) (is_closed_inter (is_closed_ge' a) (is_closed_le' b)) instance : proper_space ℝ := { compact_ball := λx r, by rw closed_ball_Icc; apply compact_Icc } open real lemma real.intermediate_value {f : ℝ → ℝ} {a b t : ℝ} (hf : ∀ x, a ≤ x → x ≤ b → tendsto f (𝓝 x) (𝓝 (f x))) (ha : f a ≤ t) (hb : t ≤ f b) (hab : a ≤ b) : ∃ x : ℝ, a ≤ x ∧ x ≤ b ∧ f x = t := let x := real.Sup {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b} in have hx₁ : ∃ y, ∀ g ∈ {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b}, g ≤ y := ⟨b, λ _ h, h.2.2⟩, have hx₂ : ∃ y, y ∈ {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b} := ⟨a, ha, le_refl _, hab⟩, have hax : a ≤ x, from le_Sup _ hx₁ ⟨ha, le_refl _, hab⟩, have hxb : x ≤ b, from (Sup_le _ hx₂ hx₁).2 (λ _ h, h.2.2), ⟨x, hax, hxb, eq_of_forall_dist_le $ λ ε ε0, let ⟨δ, hδ0, hδ⟩ := metric.tendsto_nhds_nhds.1 (hf _ hax hxb) ε ε0 in (le_total t (f x)).elim (λ h, le_of_not_gt $ λ hfε, begin rw [dist_eq, abs_of_nonneg (sub_nonneg.2 h)] at hfε, refine mt (Sup_le {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b} hx₂ hx₁).2 (not_le_of_gt (sub_lt_self x (half_pos hδ0))) (λ g hg, le_of_not_gt (λ hgδ, not_lt_of_ge hg.1 (lt_trans (lt_sub.1 hfε) (sub_lt_of_sub_lt (lt_of_le_of_lt (le_abs_self _) _))))), rw abs_sub, exact hδ (abs_sub_lt_iff.2 ⟨lt_of_le_of_lt (sub_nonpos.2 (le_Sup _ hx₁ hg)) hδ0, by simp only [x] at *; linarith⟩) end) (λ h, le_of_not_gt $ λ hfε, begin rw [dist_eq, abs_of_nonpos (sub_nonpos.2 h)] at hfε, exact mt (le_Sup {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b}) (λ h : ∀ k, k ∈ {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b} → k ≤ x, not_le_of_gt ((lt_add_iff_pos_left x).2 (half_pos hδ0)) (h _ ⟨le_trans (le_sub_iff_add_le.2 (le_trans (le_abs_self _) (le_of_lt (hδ $ by rw [dist_eq, add_sub_cancel, abs_of_nonneg (le_of_lt (half_pos hδ0))]; exact half_lt_self hδ0)))) (by linarith), le_trans hax (le_of_lt ((lt_add_iff_pos_left _).2 (half_pos hδ0))), le_of_not_gt (λ hδy, not_lt_of_ge hb (lt_of_le_of_lt (show f b ≤ f b - f x - ε + t, by linarith) (add_lt_of_neg_of_le (sub_neg_of_lt (lt_of_le_of_lt (le_abs_self _) (@hδ b (abs_sub_lt_iff.2 ⟨by simp only [x] at *; linarith, by linarith⟩)))) (le_refl _))))⟩)) hx₁ end)⟩ lemma real.intermediate_value' {f : ℝ → ℝ} {a b t : ℝ} (hf : ∀ x, a ≤ x → x ≤ b → tendsto f (𝓝 x) (𝓝 (f x))) (ha : t ≤ f a) (hb : f b ≤ t) (hab : a ≤ b) : ∃ x : ℝ, a ≤ x ∧ x ≤ b ∧ f x = t := let ⟨x, hx₁, hx₂, hx₃⟩ := @real.intermediate_value (λ x, - f x) a b (-t) (λ x hax hxb, (hf x hax hxb).neg) (neg_le_neg ha) (neg_le_neg hb) hab in ⟨x, hx₁, hx₂, neg_inj hx₃⟩ lemma real.bounded_iff_bdd_below_bdd_above {s : set ℝ} : bounded s ↔ bdd_below s ∧ bdd_above s := ⟨begin assume bdd, rcases (bounded_iff_subset_ball 0).1 bdd with ⟨r, hr⟩, -- hr : s ⊆ closed_ball 0 r rw closed_ball_Icc at hr, -- hr : s ⊆ Icc (0 - r) (0 + r) exact ⟨⟨-r, λy hy, by simpa using (hr hy).1⟩, ⟨r, λy hy, by simpa using (hr hy).2⟩⟩ end, begin rintros ⟨⟨m, hm⟩, ⟨M, hM⟩⟩, have I : s ⊆ Icc m M := λx hx, ⟨hm hx, hM hx⟩, have : Icc m M = closed_ball ((m+M)/2) ((M-m)/2) := by rw closed_ball_Icc; congr; ring, rw this at I, exact bounded.subset I bounded_closed_ball end⟩ end
ca65bc392dc3bfbbaaf32a430f2ffe0758795f87
159fed64bfae88f3b6a6166836d6278f953bcbf9
/Structure/Generic/Instances/Bundled.lean
5dad80eb1f8917ddf73a1e47604826a5aeafd85b
[ "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,705
lean
import Structure.Generic.Axioms.Universes import Structure.Generic.Axioms.AbstractFunctors import Structure.Generic.Instances.Basic set_option autoBoundImplicitLocal false --set_option pp.universes true universes u v def bundledUniverse {U : Universe.{u}} (C : GeneralizedTypeClass.{u, u} U) : Universe.{u} := ⟨Bundled C⟩ instance bundledInstance {U : Universe.{u}} (C : GeneralizedTypeClass.{u, u} U) (S : bundledUniverse C) : C S.α := S.inst def SimpleTypeClass := GeneralizedTypeClass.{u + 1, u + 1} sort.{u + 1} def simpleBundledUniverse (C : SimpleTypeClass.{u}) : Universe.{u + 1} := bundledUniverse C instance simpleBundledInstance (C : SimpleTypeClass.{u}) (S : simpleBundledUniverse C) : C S.α := bundledInstance C S -- TODO: Make `universeUniverse` more specific so that it makes sense to speak of functors between -- universes, i.e. generalized type classes. def universeUniverse : Universe.{u + 1} := simpleBundledUniverse HasInstances.{u, u + 1} namespace Bundled class HasFunctoriality (C : SimpleTypeClass.{u}) (D : SimpleTypeClass.{v}) : Type ((max u v) + 1) where (IsFunctorial {S : simpleBundledUniverse C} {T : simpleBundledUniverse D} : (S → T) → Type (max u v)) instance hasExternalFunctors (C : SimpleTypeClass.{u}) (D : SimpleTypeClass.{v}) [h : HasFunctoriality.{u, v} C D] : HasExternalFunctors.{u + 1, v + 1} (simpleBundledUniverse C) (simpleBundledUniverse D) := ⟨h.IsFunctorial⟩ class HasFunctorInstances (C : SimpleTypeClass.{u}) [h : HasFunctoriality.{u, u} C C] : Type (u + 1) where (funInst (S T : simpleBundledUniverse C) : C (S ⟶' T)) -- Work around type class resolution problems. class HasFunctorInstances' (C : SimpleTypeClass.{u}) (h : HasFunctoriality.{u, u} C C) : Type (u + 1) where (funInst (S T : simpleBundledUniverse C) : C (S ⟶' T)) instance (C : SimpleTypeClass.{u}) (h : HasFunctoriality.{u, u} C C) [h' : HasFunctorInstances' C h] : HasFunctorInstances C := ⟨h'.funInst⟩ instance hasInternalFunctors (C : SimpleTypeClass.{u}) [HasFunctoriality.{u, u} C C] [h : HasFunctorInstances.{u} C] : HasInternalFunctors.{u + 1} (simpleBundledUniverse C) := { Fun := λ S T => { α := S ⟶' T, inst := h.funInst S T }, funEquiv := λ S T => Equiv.refl (S ⟶' T) } instance hasInternalFunctors' (C : SimpleTypeClass.{u}) (h : HasFunctoriality.{u, u} C C) [h' : HasFunctorInstances'.{u} C h] : HasInternalFunctors.{u + 1} (simpleBundledUniverse C) := hasInternalFunctors C end Bundled
ba7a5cc0f79d7f65a9fb64f5364102b27fed149f
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/tactic/split_ifs.lean
645e0895ce0dafdd8bc2000682f21837ba7df454
[ "Apache-2.0" ]
permissive
waynemunro/mathlib
e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552
065a70810b5480d584033f7bbf8e0409480c2118
refs/heads/master
1,693,417,182,397
1,634,644,781,000
1,634,644,781,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,306
lean
/- Copyright (c) 2018 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner Tactic to split if-then-else-expressions. -/ import tactic.hint open expr tactic namespace tactic open interactive meta def find_if_cond : expr → option expr | e := e.fold none $ λ e _ acc, acc <|> do c ← match e with | `(@ite _ %%c %%_ _ _) := some c | `(@dite _ %%c %%_ _ _) := some c | _ := none end, guard ¬c.has_var, find_if_cond c <|> return c meta def find_if_cond_at (at_ : loc) : tactic (option expr) := do lctx ← at_.get_locals, lctx ← lctx.mmap infer_type, tgt ← target, let es := if at_.include_goal then tgt::lctx else lctx, return $ find_if_cond $ es.foldr app (default expr) run_cmd mk_simp_attr `split_if_reduction run_cmd add_doc_string `simp_attr.split_if_reduction "Simp set for if-then-else statements" attribute [split_if_reduction] if_pos if_neg dif_pos dif_neg meta def reduce_ifs_at (at_ : loc) : tactic unit := do sls ← get_user_simp_lemmas `split_if_reduction, let cfg : simp_config := { fail_if_unchanged := ff }, let discharger := assumption <|> (applyc `not_not_intro >> assumption), hs ← at_.get_locals, hs.mmap' (λ h, simp_hyp sls [] h cfg discharger >> skip), when at_.include_goal (simp_target sls [] cfg discharger >> skip) meta def split_if1 (c : expr) (n : name) (at_ : loc) : tactic unit := by_cases c n; reduce_ifs_at at_ private meta def get_next_name (names : ref (list name)) : tactic name := do ns ← read_ref names, match ns with | [] := get_unused_name `h | n::ns := do write_ref names ns, return n end private meta def value_known (c : expr) : tactic bool := do lctx ← local_context, lctx ← lctx.mmap infer_type, return $ c ∈ lctx ∨ `(¬%%c) ∈ lctx private meta def split_ifs_core (at_ : loc) (names : ref (list name)) : list expr → tactic unit | done := do some cond ← find_if_cond_at at_ | fail "no if-then-else expressions to split", let cond := match cond with `(¬%%p) := p | p := p end, if cond ∈ done then skip else do no_split ← value_known cond, if no_split then reduce_ifs_at at_; try (split_ifs_core (cond :: done)) else do n ← get_next_name names, split_if1 cond n at_; try (split_ifs_core (cond :: done)) meta def split_ifs (names : list name) (at_ : loc := loc.ns [none]) := using_new_ref names $ λ names, split_ifs_core at_ names [] namespace interactive open interactive interactive.types expr lean.parser /-- Splits all if-then-else-expressions into multiple goals. Given a goal of the form `g (if p then x else y)`, `split_ifs` will produce two goals: `p ⊢ g x` and `¬p ⊢ g y`. If there are multiple ite-expressions, then `split_ifs` will split them all, starting with a top-most one whose condition does not contain another ite-expression. `split_ifs at *` splits all ite-expressions in all hypotheses as well as the goal. `split_ifs with h₁ h₂ h₃` overrides the default names for the hypotheses. -/ meta def split_ifs (at_ : parse location) (names : parse with_ident_list) : tactic unit := tactic.split_ifs names at_ add_hint_tactic "split_ifs" add_tactic_doc { name := "split_ifs", category := doc_category.tactic, decl_names := [``split_ifs], tags := ["case bashing"] } end interactive end tactic
906d15e1c2819b14f61670e8e3cbb0ecd686b4b7
f09e92753b1d3d2eb3ce2cfb5288a7f5d1d4bd89
/src/Tate_ring.lean
c697696ec3a866d011afd43143af8cd8fd147a31
[ "Apache-2.0" ]
permissive
PatrickMassot/lean-perfectoid-spaces
7f63c581db26461b5a92d968e7563247e96a5597
5f70b2020b3c6d508431192b18457fa988afa50d
refs/heads/master
1,625,797,721,782
1,547,308,357,000
1,547,309,364,000
136,658,414
0
1
Apache-2.0
1,528,486,100,000
1,528,486,100,000
null
UTF-8
Lean
false
false
2,659
lean
import tactic.linarith import analysis.topology.topological_structures import ring_theory.subring import power_bounded -- Scholze : "Recall that a topological ring R is Tate if it contains an -- open and bounded subring R₀ ⊂ R and a topologically nilpotent unit ϖ ∈ R; such elements are -- called pseudo-uniformizers." universe u variables {R : Type u} [comm_ring R] [topological_space R] [topological_ring R] open filter function lemma half_nhds {s : set R} (hs : s ∈ (nhds (0 : R)).sets) : ∃ V ∈ (nhds (0 : R)).sets, ∀ v w ∈ V, v * w ∈ s := begin have : ((λa:R×R, a.1 * a.2) ⁻¹' s) ∈ (nhds ((0, 0) : R × R)).sets := tendsto_mul' (by simpa using hs), rw nhds_prod_eq at this, rcases filter.mem_prod_iff.1 this with ⟨V₁, H₁, V₂, H₂, H⟩, exact ⟨V₁ ∩ V₂, filter.inter_mem_sets H₁ H₂, assume v w ⟨hv, _⟩ ⟨_, hw⟩, @H (v, w) ⟨hv, hw⟩⟩ end def topologically_nilpotent (r : R) : Prop := ∀ U ∈ (nhds (0 :R)).sets, ∃ N : ℕ, ∀ n : ℕ, n > N → r^n ∈ U def is_pseudo_uniformizer (ϖ : units R) : Prop := topologically_nilpotent ϖ.val variable (R) def pseudo_uniformizer := { ϖ : units R // topologically_nilpotent ϖ.val} instance pseudo_unif.power_bounded: has_coe (pseudo_uniformizer R) (power_bounded_subring R) := ⟨λ ⟨ϖ, h⟩, ⟨ϖ, begin intros U U_nhds, rcases half_nhds U_nhds with ⟨U', ⟨U'_nhds, U'_prod⟩⟩, rcases h U' U'_nhds with ⟨N, H⟩, let V : set R := (λ u, u*ϖ^(N+1)) '' U', have V_nhds : V ∈ (nhds (0 : R)).sets, { dsimp [V], have inv : left_inverse (λ (u : R), u * (↑ϖ⁻¹)^((N + 1))) (λ (u : R), u * ϖ^(N + 1)) ∧ right_inverse (λ (u : R), u * (↑ϖ⁻¹)^(N + 1)) (λ (u : R), u * ϖ^(N + 1)), by split ; intro ; simp [mul_assoc, (mul_pow _ _ _).symm], rw set.image_eq_preimage_of_inverse inv.1 inv.2, have : tendsto (λ (u : R), u * ↑ϖ⁻¹ ^ (N + 1)) (nhds 0) (nhds 0), { conv {congr, skip, skip, rw ←(zero_mul (↑ϖ⁻¹ ^ (N + 1) : R))}, exact tendsto_mul tendsto_id tendsto_const_nhds }, exact this U'_nhds }, use [V, V_nhds], rintros v ⟨u, u_in, uv⟩ b ⟨n, h'⟩, rw [← h', ←uv, mul_assoc, ← pow_add], apply U'_prod _ _ u_in (H _ _), clear uv h', -- otherwise linarith gets confused linarith end⟩⟩ class Tate_ring (R : Type*) extends comm_ring R, topological_space R, topological_ring R := (R₀ : set R) (R₀_is_open : is_open R₀) (R₀_is_bounded : is_bounded R₀) (R₀_is_subring : is_subring R₀) (ϖ : units R) (ϖ_is_pseudo_uniformizer : is_pseudo_uniformizer ϖ) -- need an instance from Tate to Huber
c631f1d46b100f8ca472912dfeed1f098317f776
4727251e0cd73359b15b664c3170e5d754078599
/src/category_theory/filtered.lean
72cadd5325b45526f6c3cf7560a523cb02948476
[ "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
28,024
lean
/- Copyright (c) 2019 Reid Barton. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Reid Barton, Scott Morrison -/ import category_theory.fin_category import category_theory.limits.cones import category_theory.adjunction.basic import category_theory.category.preorder import category_theory.category.ulift import order.bounded_order /-! # Filtered categories A category is filtered if every finite diagram admits a cocone. We give a simple characterisation of this condition as 1. for every pair of objects there exists another object "to the right", 2. for every pair of parallel morphisms there exists a morphism to the right so the compositions are equal, and 3. there exists some object. Filtered colimits are often better behaved than arbitrary colimits. See `category_theory/limits/types` for some details. Filtered categories are nice because colimits indexed by filtered categories tend to be easier to describe than general colimits (and more often preserved by functors). In this file we show that any functor from a finite category to a filtered category admits a cocone: * `cocone_nonempty [fin_category J] [is_filtered C] (F : J ⥤ C) : nonempty (cocone F)` More generally, for any finite collection of objects and morphisms between them in a filtered category (even if not closed under composition) there exists some object `Z` receiving maps from all of them, so that all the triangles (one edge from the finite set, two from morphisms to `Z`) commute. This formulation is often more useful in practice and is available via `sup_exists`, which takes a finset of objects, and an indexed family (indexed by source and target) of finsets of morphisms. Furthermore, we give special support for two diagram categories: The `bowtie` and the `tulip`. This is because these shapes show up in the proofs that forgetful functors of algebraic categories (e.g. `Mon`, `CommRing`, ...) preserve filtered colimits. All of the above API, except for the `bowtie` and the `tulip`, is also provided for cofiltered categories. ## See also In `category_theory.limits.filtered_colimit_commutes_finite_limit` we show that filtered colimits commute with finite limits. -/ open function universes v v₁ u u₁ u₂ -- declare the `v`'s first; see `category_theory.category` for an explanation namespace category_theory variables (C : Type u) [category.{v} C] /-- A category `is_filtered_or_empty` if 1. for every pair of objects there exists another object "to the right", and 2. for every pair of parallel morphisms there exists a morphism to the right so the compositions are equal. -/ class is_filtered_or_empty : Prop := (cocone_objs : ∀ (X Y : C), ∃ Z (f : X ⟶ Z) (g : Y ⟶ Z), true) (cocone_maps : ∀ ⦃X Y : C⦄ (f g : X ⟶ Y), ∃ Z (h : Y ⟶ Z), f ≫ h = g ≫ h) /-- A category `is_filtered` if 1. for every pair of objects there exists another object "to the right", 2. for every pair of parallel morphisms there exists a morphism to the right so the compositions are equal, and 3. there exists some object. See <https://stacks.math.columbia.edu/tag/002V>. (They also define a diagram being filtered.) -/ class is_filtered extends is_filtered_or_empty C : Prop := [nonempty : nonempty C] @[priority 100] instance is_filtered_or_empty_of_semilattice_sup (α : Type u) [semilattice_sup α] : is_filtered_or_empty α := { cocone_objs := λ X Y, ⟨X ⊔ Y, hom_of_le le_sup_left, hom_of_le le_sup_right, trivial⟩, cocone_maps := λ X Y f g, ⟨Y, 𝟙 _, (by ext)⟩, } @[priority 100] instance is_filtered_of_semilattice_sup_nonempty (α : Type u) [semilattice_sup α] [nonempty α] : is_filtered α := {} @[priority 100] instance is_filtered_or_empty_of_directed_le (α : Type u) [preorder α] [is_directed α (≤)] : is_filtered_or_empty α := { cocone_objs := λ X Y, let ⟨Z, h1, h2⟩ := exists_ge_ge X Y in ⟨Z, hom_of_le h1, hom_of_le h2, trivial⟩, cocone_maps := λ X Y f g, ⟨Y, 𝟙 _, by simp⟩ } @[priority 100] instance is_filtered_of_directed_le_nonempty (α : Type u) [preorder α] [is_directed α (≤)] [nonempty α] : is_filtered α := {} -- Sanity checks example (α : Type u) [semilattice_sup α] [order_bot α] : is_filtered α := by apply_instance example (α : Type u) [semilattice_sup α] [order_top α] : is_filtered α := by apply_instance namespace is_filtered variables {C} [is_filtered C] /-- `max j j'` is an arbitrary choice of object to the right of both `j` and `j'`, whose existence is ensured by `is_filtered`. -/ noncomputable def max (j j' : C) : C := (is_filtered_or_empty.cocone_objs j j').some /-- `left_to_max j j'` is an arbitrarily choice of morphism from `j` to `max j j'`, whose existence is ensured by `is_filtered`. -/ noncomputable def left_to_max (j j' : C) : j ⟶ max j j' := (is_filtered_or_empty.cocone_objs j j').some_spec.some /-- `right_to_max j j'` is an arbitrarily choice of morphism from `j'` to `max j j'`, whose existence is ensured by `is_filtered`. -/ noncomputable def right_to_max (j j' : C) : j' ⟶ max j j' := (is_filtered_or_empty.cocone_objs j j').some_spec.some_spec.some /-- `coeq f f'`, for morphisms `f f' : j ⟶ j'`, is an arbitrary choice of object which admits a morphism `coeq_hom f f' : j' ⟶ coeq f f'` such that `coeq_condition : f ≫ coeq_hom f f' = f' ≫ coeq_hom f f'`. Its existence is ensured by `is_filtered`. -/ noncomputable def coeq {j j' : C} (f f' : j ⟶ j') : C := (is_filtered_or_empty.cocone_maps f f').some /-- `coeq_hom f f'`, for morphisms `f f' : j ⟶ j'`, is an arbitrary choice of morphism `coeq_hom f f' : j' ⟶ coeq f f'` such that `coeq_condition : f ≫ coeq_hom f f' = f' ≫ coeq_hom f f'`. Its existence is ensured by `is_filtered`. -/ noncomputable def coeq_hom {j j' : C} (f f' : j ⟶ j') : j' ⟶ coeq f f' := (is_filtered_or_empty.cocone_maps f f').some_spec.some /-- `coeq_condition f f'`, for morphisms `f f' : j ⟶ j'`, is the proof that `f ≫ coeq_hom f f' = f' ≫ coeq_hom f f'`. -/ @[simp, reassoc] lemma coeq_condition {j j' : C} (f f' : j ⟶ j') : f ≫ coeq_hom f f' = f' ≫ coeq_hom f f' := (is_filtered_or_empty.cocone_maps f f').some_spec.some_spec open category_theory.limits /-- Any finite collection of objects in a filtered category has an object "to the right". -/ lemma sup_objs_exists (O : finset C) : ∃ (S : C), ∀ {X}, X ∈ O → _root_.nonempty (X ⟶ S) := begin classical, apply finset.induction_on O, { exact ⟨is_filtered.nonempty.some, (by rintros - ⟨⟩)⟩, }, { rintros X O' nm ⟨S', w'⟩, use max X S', rintros Y mY, obtain rfl|h := eq_or_ne Y X, { exact ⟨left_to_max _ _⟩, }, { exact ⟨(w' (finset.mem_of_mem_insert_of_ne mY h)).some ≫ right_to_max _ _⟩, }, } end variables (O : finset C) (H : finset (Σ' (X Y : C) (mX : X ∈ O) (mY : Y ∈ O), X ⟶ Y)) /-- Given any `finset` of objects `{X, ...}` and indexed collection of `finset`s of morphisms `{f, ...}` in `C`, there exists an object `S`, with a morphism `T X : X ⟶ S` from each `X`, such that the triangles commute: `f ≫ T Y = T X`, for `f : X ⟶ Y` in the `finset`. -/ lemma sup_exists : ∃ (S : C) (T : Π {X : C}, X ∈ O → (X ⟶ S)), ∀ {X Y : C} (mX : X ∈ O) (mY : Y ∈ O) {f : X ⟶ Y}, (⟨X, Y, mX, mY, f⟩ : (Σ' (X Y : C) (mX : X ∈ O) (mY : Y ∈ O), X ⟶ Y)) ∈ H → f ≫ T mY = T mX := begin classical, apply finset.induction_on H, { obtain ⟨S, f⟩ := sup_objs_exists O, refine ⟨S, λ X mX, (f mX).some, _⟩, rintros - - - - - ⟨⟩, }, { rintros ⟨X, Y, mX, mY, f⟩ H' nmf ⟨S', T', w'⟩, refine ⟨coeq (f ≫ T' mY) (T' mX), λ Z mZ, T' mZ ≫ coeq_hom (f ≫ T' mY) (T' mX), _⟩, intros X' Y' mX' mY' f' mf', rw [←category.assoc], by_cases h : X = X' ∧ Y = Y', { rcases h with ⟨rfl, rfl⟩, by_cases hf : f = f', { subst hf, apply coeq_condition, }, { rw @w' _ _ mX mY f' (by simpa [hf ∘ eq.symm] using mf') }, }, { rw @w' _ _ mX' mY' f' _, apply finset.mem_of_mem_insert_of_ne mf', contrapose! h, obtain ⟨rfl, h⟩ := h, rw [heq_iff_eq, psigma.mk.inj_iff] at h, exact ⟨rfl, h.1.symm⟩ }, }, end /-- An arbitrary choice of object "to the right" of a finite collection of objects `O` and morphisms `H`, making all the triangles commute. -/ noncomputable def sup : C := (sup_exists O H).some /-- The morphisms to `sup O H`. -/ noncomputable def to_sup {X : C} (m : X ∈ O) : X ⟶ sup O H := (sup_exists O H).some_spec.some m /-- The triangles of consisting of a morphism in `H` and the maps to `sup O H` commute. -/ lemma to_sup_commutes {X Y : C} (mX : X ∈ O) (mY : Y ∈ O) {f : X ⟶ Y} (mf : (⟨X, Y, mX, mY, f⟩ : Σ' (X Y : C) (mX : X ∈ O) (mY : Y ∈ O), X ⟶ Y) ∈ H) : f ≫ to_sup O H mY = to_sup O H mX := (sup_exists O H).some_spec.some_spec mX mY mf variables {J : Type v} [small_category J] [fin_category J] /-- If we have `is_filtered C`, then for any functor `F : J ⥤ C` with `fin_category J`, there exists a cocone over `F`. -/ lemma cocone_nonempty (F : J ⥤ C) : _root_.nonempty (cocone F) := begin classical, let O := (finset.univ.image F.obj), let H : finset (Σ' (X Y : C) (mX : X ∈ O) (mY : Y ∈ O), X ⟶ Y) := finset.univ.bUnion (λ X : J, finset.univ.bUnion (λ Y : J, finset.univ.image (λ f : X ⟶ Y, ⟨F.obj X, F.obj Y, by simp, by simp, F.map f⟩))), obtain ⟨Z, f, w⟩ := sup_exists O H, refine ⟨⟨Z, ⟨λ X, f (by simp), _⟩⟩⟩, intros j j' g, dsimp, simp only [category.comp_id], apply w, simp only [finset.mem_univ, finset.mem_bUnion, exists_and_distrib_left, exists_prop_of_true, finset.mem_image], exact ⟨j, rfl, j', g, (by simp)⟩, end /-- An arbitrary choice of cocone over `F : J ⥤ C`, for `fin_category J` and `is_filtered C`. -/ noncomputable def cocone (F : J ⥤ C) : cocone F := (cocone_nonempty F).some variables {D : Type u₁} [category.{v₁} D] /-- If `C` is filtered, and we have a functor `R : C ⥤ D` with a left adjoint, then `D` is filtered. -/ lemma of_right_adjoint {L : D ⥤ C} {R : C ⥤ D} (h : L ⊣ R) : is_filtered D := { cocone_objs := λ X Y, ⟨_, h.hom_equiv _ _ (left_to_max _ _), h.hom_equiv _ _ (right_to_max _ _), ⟨⟩⟩, cocone_maps := λ X Y f g, ⟨_, h.hom_equiv _ _ (coeq_hom _ _), by rw [← h.hom_equiv_naturality_left, ← h.hom_equiv_naturality_left, coeq_condition]⟩, nonempty := is_filtered.nonempty.map R.obj } /-- If `C` is filtered, and we have a right adjoint functor `R : C ⥤ D`, then `D` is filtered. -/ lemma of_is_right_adjoint (R : C ⥤ D) [is_right_adjoint R] : is_filtered D := of_right_adjoint (adjunction.of_right_adjoint R) /-- Being filtered is preserved by equivalence of categories. -/ lemma of_equivalence (h : C ≌ D) : is_filtered D := of_right_adjoint h.symm.to_adjunction section special_shapes /-- `max₃ j₁ j₂ j₃` is an arbitrary choice of object to the right of `j₁`, `j₂` and `j₃`, whose existence is ensured by `is_filtered`. -/ noncomputable def max₃ (j₁ j₂ j₃ : C) : C := max (max j₁ j₂) j₃ /-- `first_to_max₃ j₁ j₂ j₃` is an arbitrarily choice of morphism from `j₁` to `max₃ j₁ j₂ j₃`, whose existence is ensured by `is_filtered`. -/ noncomputable def first_to_max₃ (j₁ j₂ j₃ : C) : j₁ ⟶ max₃ j₁ j₂ j₃ := left_to_max j₁ j₂ ≫ left_to_max (max j₁ j₂) j₃ /-- `second_to_max₃ j₁ j₂ j₃` is an arbitrarily choice of morphism from `j₂` to `max₃ j₁ j₂ j₃`, whose existence is ensured by `is_filtered`. -/ noncomputable def second_to_max₃ (j₁ j₂ j₃ : C) : j₂ ⟶ max₃ j₁ j₂ j₃ := right_to_max j₁ j₂ ≫ left_to_max (max j₁ j₂) j₃ /-- `third_to_max₃ j₁ j₂ j₃` is an arbitrarily choice of morphism from `j₃` to `max₃ j₁ j₂ j₃`, whose existence is ensured by `is_filtered`. -/ noncomputable def third_to_max₃ (j₁ j₂ j₃ : C) : j₃ ⟶ max₃ j₁ j₂ j₃ := right_to_max (max j₁ j₂) j₃ /-- `coeq₃ f g h`, for morphisms `f g h : j₁ ⟶ j₂`, is an arbitrary choice of object which admits a morphism `coeq₃_hom f g h : j₂ ⟶ coeq₃ f g h` such that `coeq₃_condition₁`, `coeq₃_condition₂` and `coeq₃_condition₃` are satisfied. Its existence is ensured by `is_filtered`. -/ noncomputable def coeq₃ {j₁ j₂ : C} (f g h : j₁ ⟶ j₂) : C := coeq (coeq_hom f g ≫ left_to_max (coeq f g) (coeq g h)) (coeq_hom g h ≫ right_to_max (coeq f g) (coeq g h)) /-- `coeq₃_hom f g h`, for morphisms `f g h : j₁ ⟶ j₂`, is an arbitrary choice of morphism `j₂ ⟶ coeq₃ f g h` such that `coeq₃_condition₁`, `coeq₃_condition₂` and `coeq₃_condition₃` are satisfied. Its existence is ensured by `is_filtered`. -/ noncomputable def coeq₃_hom {j₁ j₂ : C} (f g h : j₁ ⟶ j₂) : j₂ ⟶ coeq₃ f g h := coeq_hom f g ≫ left_to_max (coeq f g) (coeq g h) ≫ coeq_hom (coeq_hom f g ≫ left_to_max (coeq f g) (coeq g h)) (coeq_hom g h ≫ right_to_max (coeq f g) (coeq g h)) lemma coeq₃_condition₁ {j₁ j₂ : C} (f g h : j₁ ⟶ j₂) : f ≫ coeq₃_hom f g h = g ≫ coeq₃_hom f g h := begin dsimp [coeq₃_hom], slice_lhs 1 2 { rw coeq_condition f g }, simp only [category.assoc], end lemma coeq₃_condition₂ {j₁ j₂ : C} (f g h : j₁ ⟶ j₂) : g ≫ coeq₃_hom f g h = h ≫ coeq₃_hom f g h := begin dsimp [coeq₃_hom], slice_lhs 2 4 { rw [← category.assoc, coeq_condition _ _] }, slice_rhs 2 4 { rw [← category.assoc, coeq_condition _ _] }, slice_lhs 1 3 { rw [← category.assoc, coeq_condition _ _] }, simp only [category.assoc], end lemma coeq₃_condition₃ {j₁ j₂ : C} (f g h : j₁ ⟶ j₂) : f ≫ coeq₃_hom f g h = h ≫ coeq₃_hom f g h := eq.trans (coeq₃_condition₁ f g h) (coeq₃_condition₂ f g h) /-- Given a "bowtie" of morphisms ``` j₁ j₂ |\ /| | \/ | | /\ | |/ \∣ vv vv k₁ k₂ ``` in a filtered category, we can construct an object `s` and two morphisms from `k₁` and `k₂` to `s`, making the resulting squares commute. -/ lemma bowtie {j₁ j₂ k₁ k₂ : C} (f₁ : j₁ ⟶ k₁) (g₁ : j₁ ⟶ k₂) (f₂ : j₂ ⟶ k₁) (g₂ : j₂ ⟶ k₂) : ∃ (s : C) (α : k₁ ⟶ s) (β : k₂ ⟶ s), f₁ ≫ α = g₁ ≫ β ∧ f₂ ≫ α = g₂ ≫ β := begin let sa := max k₁ k₂, let sb := coeq (f₁ ≫ left_to_max _ _) (g₁ ≫ right_to_max _ _), let sc := coeq (f₂ ≫ left_to_max _ _) (g₂ ≫ right_to_max _ _), let sd := max sb sc, let s := coeq ((coeq_hom _ _ : sa ⟶ sb) ≫ left_to_max _ _) ((coeq_hom _ _ : sa ⟶ sc) ≫ right_to_max _ _), use s, fsplit, exact left_to_max k₁ k₂ ≫ coeq_hom _ _ ≫ left_to_max sb sc ≫ coeq_hom _ _, fsplit, exact right_to_max k₁ k₂ ≫ coeq_hom _ _ ≫ right_to_max sb sc ≫ coeq_hom _ _, fsplit, { slice_lhs 1 3 { rw [←category.assoc, coeq_condition], }, slice_lhs 3 5 { rw [←category.assoc, coeq_condition], }, simp only [category.assoc], }, { slice_lhs 3 5 { rw [←category.assoc, coeq_condition], }, slice_lhs 1 3 { rw [←category.assoc, coeq_condition], }, simp only [category.assoc], } end /-- Given a "tulip" of morphisms ``` j₁ j₂ j₃ |\ / \ / | | \ / \ / | | vv vv | \ k₁ k₂ / \ / \ / \ / \ / v v l ``` in a filtered category, we can construct an object `s` and three morphisms from `k₁`, `k₂` and `l` to `s`, making the resulting sqaures commute. -/ lemma tulip {j₁ j₂ j₃ k₁ k₂ l : C} (f₁ : j₁ ⟶ k₁) (f₂ : j₂ ⟶ k₁) (f₃ : j₂ ⟶ k₂) (f₄ : j₃ ⟶ k₂) (g₁ : j₁ ⟶ l) (g₂ : j₃ ⟶ l) : ∃ (s : C) (α : k₁ ⟶ s) (β : l ⟶ s) (γ : k₂ ⟶ s), f₁ ≫ α = g₁ ≫ β ∧ f₂ ≫ α = f₃ ≫ γ ∧ f₄ ≫ γ = g₂ ≫ β := begin let sa := max₃ k₁ l k₂, let sb := coeq (f₁ ≫ first_to_max₃ k₁ l k₂) (g₁ ≫ second_to_max₃ k₁ l k₂), let sc := coeq (f₂ ≫ first_to_max₃ k₁ l k₂) (f₃ ≫ third_to_max₃ k₁ l k₂), let sd := coeq (f₄ ≫ third_to_max₃ k₁ l k₂) (g₂ ≫ second_to_max₃ k₁ l k₂), let se := max₃ sb sc sd, let sf := coeq₃ (coeq_hom _ _ ≫ first_to_max₃ sb sc sd) (coeq_hom _ _ ≫ second_to_max₃ sb sc sd) (coeq_hom _ _ ≫ third_to_max₃ sb sc sd), use sf, use first_to_max₃ k₁ l k₂ ≫ coeq_hom _ _ ≫ first_to_max₃ sb sc sd ≫ coeq₃_hom _ _ _, use second_to_max₃ k₁ l k₂ ≫ coeq_hom _ _ ≫ second_to_max₃ sb sc sd ≫ coeq₃_hom _ _ _, use third_to_max₃ k₁ l k₂ ≫ coeq_hom _ _ ≫ third_to_max₃ sb sc sd ≫ coeq₃_hom _ _ _, fsplit, slice_lhs 1 3 { rw [← category.assoc, coeq_condition] }, slice_lhs 3 6 { rw [← category.assoc, coeq₃_condition₁] }, simp only [category.assoc], fsplit, slice_lhs 3 6 { rw [← category.assoc, coeq₃_condition₁] }, slice_lhs 1 3 { rw [← category.assoc, coeq_condition] }, slice_rhs 3 6 { rw [← category.assoc, ← coeq₃_condition₂] }, simp only [category.assoc], slice_rhs 3 6 { rw [← category.assoc, coeq₃_condition₂] }, slice_rhs 1 3 { rw [← category.assoc, ← coeq_condition] }, simp only [category.assoc], end end special_shapes end is_filtered /-- A category `is_cofiltered_or_empty` if 1. for every pair of objects there exists another object "to the left", and 2. for every pair of parallel morphisms there exists a morphism to the left so the compositions are equal. -/ class is_cofiltered_or_empty : Prop := (cocone_objs : ∀ (X Y : C), ∃ W (f : W ⟶ X) (g : W ⟶ Y), true) (cocone_maps : ∀ ⦃X Y : C⦄ (f g : X ⟶ Y), ∃ W (h : W ⟶ X), h ≫ f = h ≫ g) /-- A category `is_cofiltered` if 1. for every pair of objects there exists another object "to the left", 2. for every pair of parallel morphisms there exists a morphism to the left so the compositions are equal, and 3. there exists some object. See <https://stacks.math.columbia.edu/tag/04AZ>. -/ class is_cofiltered extends is_cofiltered_or_empty C : Prop := [nonempty : nonempty C] @[priority 100] instance is_cofiltered_or_empty_of_semilattice_inf (α : Type u) [semilattice_inf α] : is_cofiltered_or_empty α := { cocone_objs := λ X Y, ⟨X ⊓ Y, hom_of_le inf_le_left, hom_of_le inf_le_right, trivial⟩, cocone_maps := λ X Y f g, ⟨X, 𝟙 _, (by ext)⟩, } @[priority 100] instance is_cofiltered_of_semilattice_inf_nonempty (α : Type u) [semilattice_inf α] [nonempty α] : is_cofiltered α := {} @[priority 100] instance is_cofiltered_or_empty_of_directed_ge (α : Type u) [preorder α] [is_directed α (swap (≤))] : is_cofiltered_or_empty α := { cocone_objs := λ X Y, let ⟨Z, hX, hY⟩ := exists_le_le X Y in ⟨Z, hom_of_le hX, hom_of_le hY, trivial⟩, cocone_maps := λ X Y f g, ⟨X, 𝟙 _, by simp⟩ } @[priority 100] instance is_cofiltered_of_directed_ge_nonempty (α : Type u) [preorder α] [is_directed α (swap (≤))] [nonempty α] : is_cofiltered α := {} -- Sanity checks example (α : Type u) [semilattice_inf α] [order_bot α] : is_cofiltered α := by apply_instance example (α : Type u) [semilattice_inf α] [order_top α] : is_cofiltered α := by apply_instance namespace is_cofiltered variables {C} [is_cofiltered C] /-- `min j j'` is an arbitrary choice of object to the left of both `j` and `j'`, whose existence is ensured by `is_cofiltered`. -/ noncomputable def min (j j' : C) : C := (is_cofiltered_or_empty.cocone_objs j j').some /-- `min_to_left j j'` is an arbitrarily choice of morphism from `min j j'` to `j`, whose existence is ensured by `is_cofiltered`. -/ noncomputable def min_to_left (j j' : C) : min j j' ⟶ j := (is_cofiltered_or_empty.cocone_objs j j').some_spec.some /-- `min_to_right j j'` is an arbitrarily choice of morphism from `min j j'` to `j'`, whose existence is ensured by `is_cofiltered`. -/ noncomputable def min_to_right (j j' : C) : min j j' ⟶ j' := (is_cofiltered_or_empty.cocone_objs j j').some_spec.some_spec.some /-- `eq f f'`, for morphisms `f f' : j ⟶ j'`, is an arbitrary choice of object which admits a morphism `eq_hom f f' : eq f f' ⟶ j` such that `eq_condition : eq_hom f f' ≫ f = eq_hom f f' ≫ f'`. Its existence is ensured by `is_cofiltered`. -/ noncomputable def eq {j j' : C} (f f' : j ⟶ j') : C := (is_cofiltered_or_empty.cocone_maps f f').some /-- `eq_hom f f'`, for morphisms `f f' : j ⟶ j'`, is an arbitrary choice of morphism `eq_hom f f' : eq f f' ⟶ j` such that `eq_condition : eq_hom f f' ≫ f = eq_hom f f' ≫ f'`. Its existence is ensured by `is_cofiltered`. -/ noncomputable def eq_hom {j j' : C} (f f' : j ⟶ j') : eq f f' ⟶ j := (is_cofiltered_or_empty.cocone_maps f f').some_spec.some /-- `eq_condition f f'`, for morphisms `f f' : j ⟶ j'`, is the proof that `eq_hom f f' ≫ f = eq_hom f f' ≫ f'`. -/ @[simp, reassoc] lemma eq_condition {j j' : C} (f f' : j ⟶ j') : eq_hom f f' ≫ f = eq_hom f f' ≫ f' := (is_cofiltered_or_empty.cocone_maps f f').some_spec.some_spec open category_theory.limits /-- Any finite collection of objects in a cofiltered category has an object "to the left". -/ lemma inf_objs_exists (O : finset C) : ∃ (S : C), ∀ {X}, X ∈ O → _root_.nonempty (S ⟶ X) := begin classical, apply finset.induction_on O, { exact ⟨is_cofiltered.nonempty.some, (by rintros - ⟨⟩)⟩, }, { rintros X O' nm ⟨S', w'⟩, use min X S', rintros Y mY, obtain rfl|h := eq_or_ne Y X, { exact ⟨min_to_left _ _⟩, }, { exact ⟨min_to_right _ _ ≫ (w' (finset.mem_of_mem_insert_of_ne mY h)).some⟩, }, } end variables (O : finset C) (H : finset (Σ' (X Y : C) (mX : X ∈ O) (mY : Y ∈ O), X ⟶ Y)) /-- Given any `finset` of objects `{X, ...}` and indexed collection of `finset`s of morphisms `{f, ...}` in `C`, there exists an object `S`, with a morphism `T X : S ⟶ X` from each `X`, such that the triangles commute: `T X ≫ f = T Y`, for `f : X ⟶ Y` in the `finset`. -/ lemma inf_exists : ∃ (S : C) (T : Π {X : C}, X ∈ O → (S ⟶ X)), ∀ {X Y : C} (mX : X ∈ O) (mY : Y ∈ O) {f : X ⟶ Y}, (⟨X, Y, mX, mY, f⟩ : (Σ' (X Y : C) (mX : X ∈ O) (mY : Y ∈ O), X ⟶ Y)) ∈ H → T mX ≫ f = T mY := begin classical, apply finset.induction_on H, { obtain ⟨S, f⟩ := inf_objs_exists O, refine ⟨S, λ X mX, (f mX).some, _⟩, rintros - - - - - ⟨⟩, }, { rintros ⟨X, Y, mX, mY, f⟩ H' nmf ⟨S', T', w'⟩, refine ⟨eq (T' mX ≫ f) (T' mY), λ Z mZ, eq_hom (T' mX ≫ f) (T' mY) ≫ T' mZ, _⟩, intros X' Y' mX' mY' f' mf', rw [category.assoc], by_cases h : X = X' ∧ Y = Y', { rcases h with ⟨rfl, rfl⟩, by_cases hf : f = f', { subst hf, apply eq_condition, }, { rw @w' _ _ mX mY f' (by simpa [hf ∘ eq.symm] using mf') }, }, { rw @w' _ _ mX' mY' f' _, apply finset.mem_of_mem_insert_of_ne mf', contrapose! h, obtain ⟨rfl, h⟩ := h, rw [heq_iff_eq, psigma.mk.inj_iff] at h, exact ⟨rfl, h.1.symm⟩ }, }, end /-- An arbitrary choice of object "to the left" of a finite collection of objects `O` and morphisms `H`, making all the triangles commute. -/ noncomputable def inf : C := (inf_exists O H).some /-- The morphisms from `inf O H`. -/ noncomputable def inf_to {X : C} (m : X ∈ O) : inf O H ⟶ X := (inf_exists O H).some_spec.some m /-- The triangles consisting of a morphism in `H` and the maps from `inf O H` commute. -/ lemma inf_to_commutes {X Y : C} (mX : X ∈ O) (mY : Y ∈ O) {f : X ⟶ Y} (mf : (⟨X, Y, mX, mY, f⟩ : Σ' (X Y : C) (mX : X ∈ O) (mY : Y ∈ O), X ⟶ Y) ∈ H) : inf_to O H mX ≫ f = inf_to O H mY := (inf_exists O H).some_spec.some_spec mX mY mf variables {J : Type v} [small_category J] [fin_category J] /-- If we have `is_cofiltered C`, then for any functor `F : J ⥤ C` with `fin_category J`, there exists a cone over `F`. -/ lemma cone_nonempty (F : J ⥤ C) : _root_.nonempty (cone F) := begin classical, let O := (finset.univ.image F.obj), let H : finset (Σ' (X Y : C) (mX : X ∈ O) (mY : Y ∈ O), X ⟶ Y) := finset.univ.bUnion (λ X : J, finset.univ.bUnion (λ Y : J, finset.univ.image (λ f : X ⟶ Y, ⟨F.obj X, F.obj Y, by simp, by simp, F.map f⟩))), obtain ⟨Z, f, w⟩ := inf_exists O H, refine ⟨⟨Z, ⟨λ X, f (by simp), _⟩⟩⟩, intros j j' g, dsimp, simp only [category.id_comp], symmetry, apply w, simp only [finset.mem_univ, finset.mem_bUnion, exists_and_distrib_left, exists_prop_of_true, finset.mem_image], exact ⟨j, rfl, j', g, (by simp)⟩, end /-- An arbitrary choice of cone over `F : J ⥤ C`, for `fin_category J` and `is_cofiltered C`. -/ noncomputable def cone (F : J ⥤ C) : cone F := (cone_nonempty F).some variables {D : Type u₁} [category.{v₁} D] /-- If `C` is cofiltered, and we have a functor `L : C ⥤ D` with a right adjoint, then `D` is cofiltered. -/ lemma of_left_adjoint {L : C ⥤ D} {R : D ⥤ C} (h : L ⊣ R) : is_cofiltered D := { cocone_objs := λ X Y, ⟨L.obj (min (R.obj X) (R.obj Y)), (h.hom_equiv _ X).symm (min_to_left _ _), (h.hom_equiv _ Y).symm (min_to_right _ _), ⟨⟩⟩, cocone_maps := λ X Y f g, ⟨L.obj (eq (R.map f) (R.map g)), (h.hom_equiv _ _).symm (eq_hom _ _), by rw [← h.hom_equiv_naturality_right_symm, ← h.hom_equiv_naturality_right_symm, eq_condition]⟩, nonempty := is_cofiltered.nonempty.map L.obj } /-- If `C` is cofiltered, and we have a left adjoint functor `L : C ⥤ D`, then `D` is cofiltered. -/ lemma of_is_left_adjoint (L : C ⥤ D) [is_left_adjoint L] : is_cofiltered D := of_left_adjoint (adjunction.of_left_adjoint L) /-- Being cofiltered is preserved by equivalence of categories. -/ lemma of_equivalence (h : C ≌ D) : is_cofiltered D := of_left_adjoint h.to_adjunction end is_cofiltered section opposite open opposite instance is_cofiltered_op_of_is_filtered [is_filtered C] : is_cofiltered Cᵒᵖ := { cocone_objs := λ X Y, ⟨op (is_filtered.max X.unop Y.unop), (is_filtered.left_to_max _ _).op, (is_filtered.right_to_max _ _).op, trivial⟩, cocone_maps := λ X Y f g, ⟨op (is_filtered.coeq f.unop g.unop), (is_filtered.coeq_hom _ _).op, begin rw [(show f = f.unop.op, by simp), (show g = g.unop.op, by simp), ← op_comp, ← op_comp], congr' 1, exact is_filtered.coeq_condition f.unop g.unop, end⟩, nonempty := ⟨op is_filtered.nonempty.some⟩ } instance is_filtered_op_of_is_cofiltered [is_cofiltered C] : is_filtered Cᵒᵖ := { cocone_objs := λ X Y, ⟨op (is_cofiltered.min X.unop Y.unop), (is_cofiltered.min_to_left X.unop Y.unop).op, (is_cofiltered.min_to_right X.unop Y.unop).op, trivial⟩, cocone_maps := λ X Y f g, ⟨op (is_cofiltered.eq f.unop g.unop), (is_cofiltered.eq_hom f.unop g.unop).op, begin rw [(show f = f.unop.op, by simp), (show g = g.unop.op, by simp), ← op_comp, ← op_comp], congr' 1, exact is_cofiltered.eq_condition f.unop g.unop, end⟩, nonempty := ⟨op is_cofiltered.nonempty.some⟩ } end opposite section ulift instance [is_filtered C] : is_filtered (ulift.{u₂} C) := is_filtered.of_equivalence ulift.equivalence instance [is_cofiltered C] : is_cofiltered (ulift.{u₂} C) := is_cofiltered.of_equivalence ulift.equivalence instance [is_filtered C] : is_filtered (ulift_hom C) := is_filtered.of_equivalence ulift_hom.equiv instance [is_cofiltered C] : is_cofiltered (ulift_hom C) := is_cofiltered.of_equivalence ulift_hom.equiv instance [is_filtered C] : is_filtered (as_small C) := is_filtered.of_equivalence as_small.equiv instance [is_cofiltered C] : is_cofiltered (as_small C) := is_cofiltered.of_equivalence as_small.equiv end ulift end category_theory
7f2a3491b69ae8d2d2428951c8aa8d1a98c39aa4
d406927ab5617694ec9ea7001f101b7c9e3d9702
/archive/imo/imo2001_q6.lean
c2fa9f829a85588b13dcc78b86e05b5ceeb5b460
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
1,607
lean
/- Copyright (c) 2021 Sara Díaz Real. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sara Díaz Real -/ import algebra.associated import tactic.linarith import tactic.linear_combination /-! # IMO 2001 Q6 Let $a$, $b$, $c$, $d$ be integers with $a > b > c > d > 0$. Suppose that $$ a*c + b*d = (a + b - c + d) * (-a + b + c + d). $$ Prove that $a*b + c*d$ is not prime. -/ variables {a b c d : ℤ} theorem imo2001_q6 (hd : 0 < d) (hdc : d < c) (hcb : c < b) (hba : b < a) (h : a*c + b*d = (a + b - c + d) * (-a + b + c + d)) : ¬ prime (a*b + c*d) := begin assume h0 : prime (a*b + c*d), have ha : 0 < a, { linarith }, have hb : 0 < b, { linarith }, have hc : 0 < c, { linarith }, -- the key step is to show that `a*c + b*d` divides the product `(a*b + c*d) * (a*d + b*c)` have dvd_mul : a*c + b*d ∣ (a*b + c*d) * (a*d + b*c), { use b^2 + b*d + d^2, linear_combination b*d*h }, -- since `a*b + c*d` is prime (by assumption), it must divide `a*c + b*d` or `a*d + b*c` obtain (h1 : a*b + c*d ∣ a*c + b*d) | (h2 : a*c + b*d ∣ a*d + b*c) := h0.left_dvd_or_dvd_right_of_dvd_mul dvd_mul, -- in both cases, we derive a contradiction { have aux : 0 < a*c + b*d, { nlinarith only [ha, hb, hc, hd] }, have : a*b + c*d ≤ a*c + b*d, { from int.le_of_dvd aux h1 }, nlinarith only [hba, hcb, hdc, h, this] }, { have aux : 0 < a*d + b*c, { nlinarith only [ha, hb, hc, hd] }, have : a*c + b*d ≤ a*d + b*c, { from int.le_of_dvd aux h2 }, nlinarith only [hba, hdc, h, this] }, end
f8027b2563d0cd53c9bb632e5f35bc71a5b57a71
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/group_theory/group_action/conj_act.lean
f37624b777e3e6656890c7fde2ad846904b38fb4
[ "Apache-2.0" ]
permissive
ayush1801/mathlib
78949b9f789f488148142221606bf15c02b960d2
ce164e28f262acbb3de6281b3b03660a9f744e3c
refs/heads/master
1,692,886,907,941
1,635,270,866,000
1,635,270,866,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,860
lean
/- Copyright (c) 2021 . All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import group_theory.group_action.basic import group_theory.subgroup.basic /-! # Conjugation action of a group on itself This file defines the conjugation action of a group on itself. See also `mul_aut.conj` for the definition of conjugation as a homomorphism into the automorphism group. ## Main definitions A type alias `conj_act G` is introduced for a group `G`. The group `conj_act G` acts on `G` by conjugation. The group `conj_act G` also acts on any normal subgroup of `G` by conjugation. ## Implementation Notes The scalar action in defined in this file can also be written using `mul_aut.conj g • h`. This has the advantage of not using the type alias `conj_act`, but the downside of this approach is that some theorems about the group actions will not apply when since this `mul_aut.conj g • h` describes an action of `mul_aut G` on `G`, and not an action of `G`. -/ variables (G : Type*) /-- A type alias for a group `G`. `conj_act G` acts on `G` by conjugation -/ def conj_act : Type* := G namespace conj_act open mul_action subgroup variable {G} instance : Π [group G], group (conj_act G) := id instance : Π [div_inv_monoid G], div_inv_monoid (conj_act G) := id instance : Π [group_with_zero G], group_with_zero (conj_act G) := id instance : Π [fintype G], fintype (conj_act G) := id @[simp] lemma card [fintype G] : fintype.card (conj_act G) = fintype.card G := rfl section div_inv_monoid variable [div_inv_monoid G] instance : inhabited (conj_act G) := ⟨1⟩ /-- Reinterpret `g : conj_act G` as an element of `G`. -/ def of_conj_act : conj_act G ≃* G := ⟨id, id, λ _, rfl, λ _, rfl, λ _ _, rfl⟩ /-- Reinterpret `g : G` as an element of `conj_act G`. -/ def to_conj_act : G ≃* conj_act G := of_conj_act.symm /-- A recursor for `conj_act`, for use as `induction x using conj_act.rec` when `x : conj_act G`. -/ protected def rec {C : conj_act G → Sort*} (h : Π g, C (to_conj_act g)) : Π g, C g := h @[simp] lemma of_mul_symm_eq : (@of_conj_act G _).symm = to_conj_act := rfl @[simp] lemma to_mul_symm_eq : (@to_conj_act G _).symm = of_conj_act := rfl @[simp] lemma to_conj_act_of_conj_act (x : conj_act G) : to_conj_act (of_conj_act x) = x := rfl @[simp] lemma of_conj_act_to_conj_act (x : G) : of_conj_act (to_conj_act x) = x := rfl @[simp] lemma of_conj_act_one : of_conj_act (1 : conj_act G) = 1 := rfl @[simp] lemma to_conj_act_one : to_conj_act (1 : G) = 1 := rfl @[simp] lemma of_conj_act_inv (x : conj_act G) : of_conj_act (x⁻¹) = (of_conj_act x)⁻¹ := rfl @[simp] lemma to_conj_act_inv (x : G) : to_conj_act (x⁻¹) = (to_conj_act x)⁻¹ := rfl @[simp] lemma of_conj_act_mul (x y : conj_act G) : of_conj_act (x * y) = of_conj_act x * of_conj_act y := rfl @[simp] lemma to_conj_act_mul (x y : G) : to_conj_act (x * y) = to_conj_act x * to_conj_act y := rfl instance : has_scalar (conj_act G) G := { smul := λ g h, of_conj_act g * h * (of_conj_act g)⁻¹ } lemma smul_def (g : conj_act G) (h : G) : g • h = of_conj_act g * h * (of_conj_act g)⁻¹ := rfl @[simp] lemma «forall» (p : conj_act G → Prop) : (∀ (x : conj_act G), p x) ↔ ∀ x : G, p (to_conj_act x) := iff.rfl end div_inv_monoid section group_with_zero variable [group_with_zero G] @[simp] lemma of_conj_act_zero : of_conj_act (0 : conj_act G) = 0 := rfl @[simp] lemma to_conj_act_zero : to_conj_act (0 : G) = 0 := rfl instance : mul_action (conj_act G) G := { smul := (•), one_smul := by simp [smul_def], mul_smul := by simp [smul_def, mul_assoc, mul_inv_rev₀] } end group_with_zero variables [group G] instance : mul_distrib_mul_action (conj_act G) G := { smul := (•), smul_mul := by simp [smul_def, mul_assoc], smul_one := by simp [smul_def], one_smul := by simp [smul_def], mul_smul := by simp [smul_def, mul_assoc] } lemma smul_eq_mul_aut_conj (g : conj_act G) (h : G) : g • h = mul_aut.conj (of_conj_act g) h := rfl /-- The set of fixed points of the conjugation action of `G` on itself is the center of `G`. -/ lemma fixed_points_eq_center : fixed_points (conj_act G) G = center G := begin ext x, simp [mem_center_iff, smul_def, mul_inv_eq_iff_eq_mul] end /-- As normal subgroups are closed under conjugation, they inherit the conjugation action of the underlying group. -/ instance subgroup.conj_action {H : subgroup G} [hH : H.normal] : has_scalar (conj_act G) H := ⟨λ g h, ⟨g • h, hH.conj_mem h.1 h.2 (of_conj_act g)⟩⟩ lemma subgroup.coe_conj_smul {H : subgroup G} [hH : H.normal] (g : conj_act G) (h : H) : ↑(g • h) = g • (h : G) := rfl instance subgroup.conj_mul_distrib_mul_action {H : subgroup G} [hH : H.normal] : mul_distrib_mul_action (conj_act G) H := (subtype.coe_injective).mul_distrib_mul_action H.subtype subgroup.coe_conj_smul /-- Group conjugation on a normal subgroup. Analogous to `mul_aut.conj`. -/ def _root_.mul_aut.conj_normal {H : subgroup G} [hH : H.normal] : G →* mul_aut H := (mul_distrib_mul_action.to_mul_aut (conj_act G) H).comp to_conj_act.to_monoid_hom @[simp] lemma _root_.mul_aut.conj_normal_apply {H : subgroup G} [H.normal] (g : G) (h : H) : ↑(mul_aut.conj_normal g h) = g * h * g⁻¹ := rfl @[simp] lemma _root_.mul_aut.conj_normal_symm_apply {H : subgroup G} [H.normal] (g : G) (h : H) : ↑((mul_aut.conj_normal g).symm h) = g⁻¹ * h * g := by { change _ * (_)⁻¹⁻¹ = _, rw inv_inv, refl } @[simp] lemma _root_.mul_aut.conj_normal_inv_apply {H : subgroup G} [H.normal] (g : G) (h : H) : ↑((mul_aut.conj_normal g)⁻¹ h) = g⁻¹ * h * g := mul_aut.conj_normal_symm_apply g h lemma _root_.mul_aut.conj_normal_coe {H : subgroup G} [H.normal] {h : H} : mul_aut.conj_normal ↑h = mul_aut.conj h := mul_equiv.ext (λ x, rfl) end conj_act
f013354c43b7fbe41c0630d84a81125faf37c73f
c062f1c97fdef9ac746f08754e7d766fd6789aa9
/data/set/basic.lean
c3bd54bd3c54484ce2ae4497fdaebccefb68b971
[]
no_license
emberian/library_dev
00c7a985b21bdebe912f4127a363f2874e1e7555
f3abd7db0238edc18a397540e361a1da2f51503c
refs/heads/master
1,624,153,474,804
1,490,147,180,000
1,490,147,180,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
29,091
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 -- TODO: in emacs mode, change "\sub" to regular subset, use "\ssub" for strict, similarly for "\sup" -- QUESTION: can make the first argument in ∀ x ∈ a, ... implicit? -- QUESTION: how should we handle facts that only hold classically? -/ import logic.basic data.set -- from the library in the main repo import ...algebra.lattice open function tactic set lattice universes u v w variables {α : Type u} {β : Type v} {γ : Type w} namespace set @[simp] lemma mem_set_of {a : α} {p : α → Prop} : a ∈ {a | p a} = p a := rfl theorem union_subset {s t r : set α} (sr : s ⊆ r) (tr : t ⊆ r) : s ∪ t ⊆ r := λ x xst, or.elim xst (λ xs, sr xs) (λ xt, tr xt) theorem inter_subset_left (s t : set α) : s ∩ t ⊆ s := λ x H, and.left H theorem inter_subset_right (s t : set α) : s ∩ t ⊆ t := λ x H, and.right H theorem subset_inter {s t r : set α} (rs : r ⊆ s) (rt : r ⊆ t) : r ⊆ s ∩ t := λ x xr, and.intro (rs xr) (rt xr) instance lattice_set : complete_lattice (set α) := { lattice.complete_lattice . le := subset, le_refl := subset.refl, le_trans := take a b c, subset.trans, le_antisymm := take a b, subset.antisymm, sup := union, le_sup_left := subset_union_left, le_sup_right := subset_union_right, sup_le := take a b c, union_subset, inf := set.inter, inf_le_left := inter_subset_left, inf_le_right := inter_subset_right, le_inf := take a b c, subset_inter, top := {a | true }, le_top := take s a h, trivial, bot := ∅, bot_le := take s a, false.elim, Sup := λs, {a | ∃ t ∈ s, a ∈ t }, le_Sup := take s t t_in a a_in, ⟨t, ⟨t_in, a_in⟩⟩, Sup_le := take s t h a ⟨t', ⟨t'_in, a_in⟩⟩, h t' t'_in a_in, Inf := λs, {a | ∀ t ∈ s, a ∈ t }, le_Inf := take s t h a a_in t' t'_in, h t' t'_in a_in, Inf_le := take s t t_in a h, h _ t_in } /- strict subset -/ def strict_subset (a b : set α) := a ⊆ b ∧ a ≠ b instance : has_ssubset (set α) := ⟨strict_subset⟩ /- empty set -/ attribute [simp] mem_empty_eq empty_subset theorem exists_mem_of_ne_empty {s : set α} (h : s ≠ ∅) : ∃ x, x ∈ s := classical.by_contradiction (suppose ¬ ∃ x, x ∈ s, have ∀ x, x ∉ s, from forall_not_of_not_exists this, show false, from h (eq_empty_of_forall_not_mem this)) theorem subset_empty_iff (s : set α) : s ⊆ ∅ ↔ s = ∅ := iff.intro eq_empty_of_subset_empty (take xeq, begin rw xeq, apply subset.refl end) lemma bounded_forall_empty_iff {p : α → Prop} : (∀ x ∈ (∅ : set α), p x) ↔ true := iff.intro (take H, true.intro) (take H x H1, absurd H1 (not_mem_empty _)) /- universal set -/ theorem mem_univ (x : α) : x ∈ @univ α := by triv theorem mem_univ_iff (x : α) : x ∈ @univ α ↔ true := iff.rfl @[simp] theorem mem_univ_eq (x : α) : x ∈ @univ α = true := rfl theorem empty_ne_univ [h : inhabited α] : (∅ : set α) ≠ univ := assume H : ∅ = univ, absurd (mem_univ (inhabited.default α)) (eq.rec_on H (not_mem_empty _)) @[simp] theorem subset_univ (s : set α) : s ⊆ univ := λ x H, trivial theorem eq_univ_of_univ_subset {s : set α} (h : univ ⊆ s) : s = univ := eq_of_subset_of_subset (subset_univ s) h theorem eq_univ_of_forall {s : set α} (H : ∀ x, x ∈ s) : s = univ := ext (take x, iff.intro (assume H', trivial) (assume H', H x)) /- union -/ theorem mem_union_left {x : α} {a : set α} (b : set α) : x ∈ a → x ∈ a ∪ b := assume h, or.inl h theorem mem_union_right {x : α} {b : set α} (a : set α) : x ∈ b → x ∈ a ∪ b := assume h, or.inr h theorem mem_unionl {x : α} {a b : set α} : x ∈ a → x ∈ a ∪ b := assume h, or.inl h theorem mem_unionr {x : α} {a b : set α} : x ∈ b → x ∈ a ∪ b := assume h, or.inr h theorem mem_or_mem_of_mem_union {x : α} {a b : set α} (H : x ∈ a ∪ b) : x ∈ a ∨ x ∈ b := H theorem mem_union.elim {x : α} {a b : set α} {P : Prop} (H₁ : x ∈ a ∪ b) (H₂ : x ∈ a → P) (H₃ : x ∈ b → P) : P := or.elim H₁ H₂ H₃ theorem mem_union_iff (x : α) (a b : set α) : x ∈ a ∪ b ↔ x ∈ a ∨ x ∈ b := iff.rfl theorem mem_union_eq (x : α) (a b : set α) : x ∈ a ∪ b = (x ∈ a ∨ x ∈ b) := rfl attribute [simp] union_self union_empty empty_union -- union_comm union_assoc theorem union_left_comm (s₁ s₂ s₃ : set α) : s₁ ∪ (s₂ ∪ s₃) = s₂ ∪ (s₁ ∪ s₃) := by rw [-union_assoc, union_comm s₁, union_assoc] theorem union_right_comm (s₁ s₂ s₃ : set α) : (s₁ ∪ s₂) ∪ s₃ = (s₁ ∪ s₃) ∪ s₂ := by rw [union_assoc, union_comm s₂, union_assoc] theorem union_eq_self_of_subset_left {s t : set α} (h : s ⊆ t) : s ∪ t = t := eq_of_subset_of_subset (union_subset h (subset.refl _)) (subset_union_right _ _) theorem union_eq_self_of_subset_right {s t : set α} (h : t ⊆ s) : s ∪ t = s := by rw [union_comm, union_eq_self_of_subset_left h] attribute [simp] union_comm union_assoc union_left_comm /- intersection -/ theorem mem_inter_iff (x : α) (a b : set α) : x ∈ a ∩ b ↔ x ∈ a ∧ x ∈ b := iff.rfl @[simp] theorem mem_inter_eq (x : α) (a b : set α) : x ∈ a ∩ b = (x ∈ a ∧ x ∈ b) := rfl theorem mem_inter {x : α} {a b : set α} (ha : x ∈ a) (hb : x ∈ b) : x ∈ a ∩ b := ⟨ha, hb⟩ theorem mem_of_mem_inter_left {x : α} {a b : set α} (h : x ∈ a ∩ b) : x ∈ a := h^.left theorem mem_of_mem_inter_right {x : α} {a b : set α} (h : x ∈ a ∩ b) : x ∈ b := h^.right attribute [simp] inter_self inter_empty empty_inter -- inter_comm inter_assoc theorem nonempty_of_inter_nonempty_right {T : Type} {s t : set T} (h : s ∩ t ≠ ∅) : t ≠ ∅ := suppose t = ∅, have s ∩ t = ∅, from eq.subst (eq.symm this) (inter_empty s), h this theorem nonempty_of_inter_nonempty_left {T : Type} {s t : set T} (h : s ∩ t ≠ ∅) : s ≠ ∅ := suppose s = ∅, have s ∩ t = ∅, begin rw this, apply empty_inter end, h this theorem inter_left_comm (s₁ s₂ s₃ : set α) : s₁ ∩ (s₂ ∩ s₃) = s₂ ∩ (s₁ ∩ s₃) := by rw [-inter_assoc, inter_comm s₁, inter_assoc] theorem inter_right_comm (s₁ s₂ s₃ : set α) : (s₁ ∩ s₂) ∩ s₃ = (s₁ ∩ s₃) ∩ s₂ := by rw [inter_assoc, inter_comm s₂, inter_assoc] theorem inter_univ (a : set α) : a ∩ univ = a := ext (take x, and_true _) theorem univ_inter (a : set α) : univ ∩ a = a := ext (take x, true_and _) theorem inter_subset_inter_right {s t : set α} (u : set α) (H : s ⊆ t) : s ∩ u ⊆ t ∩ u := take x, assume xsu, and.intro (H (and.left xsu)) (and.right xsu) theorem inter_subset_inter_left {s t : set α} (u : set α) (H : s ⊆ t) : u ∩ s ⊆ u ∩ t := take x, assume xus, and.intro (and.left xus) (H (and.right xus)) theorem inter_eq_self_of_subset_left {s t : set α} (h : s ⊆ t) : s ∩ t = s := eq_of_subset_of_subset (inter_subset_left _ _) (subset_inter (subset.refl _) h) theorem inter_eq_self_of_subset_right {s t : set α} (h : t ⊆ s) : s ∩ t = t := by rw [inter_comm, inter_eq_self_of_subset_left h] attribute [simp] inter_comm inter_assoc inter_left_comm /- distributivity laws -/ theorem inter_distrib_left (s t u : set α) : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) := ext (take x, and_distrib _ _ _) theorem inter_distrib_right (s t u : set α) : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) := ext (take x, and_distrib_right _ _ _) theorem union_distrib_left (s t u : set α) : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) := ext (take x, or_distrib _ _ _) theorem union_distrib_right (s t u : set α) : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) := ext (take x, or_distrib_right _ _ _) /- insert -/ theorem subset_insert (x : α) (a : set α) : a ⊆ insert x a := take y, assume ys, or.inr ys theorem mem_insert (x : α) (s : set α) : x ∈ insert x s := or.inl rfl theorem mem_insert_of_mem {x : α} {s : set α} (y : α) : x ∈ s → x ∈ insert y s := assume h, or.inr h theorem eq_or_mem_of_mem_insert {x a : α} {s : set α} : x ∈ insert a s → x = a ∨ x ∈ s := assume h, h theorem mem_of_mem_insert_of_ne {x a : α} {s : set α} (xin : x ∈ insert a s) : x ≠ a → x ∈ s := or_resolve_right (eq_or_mem_of_mem_insert xin) @[simp] theorem mem_insert_iff (x a : α) (s : set α) : x ∈ insert a s ↔ (x = a ∨ x ∈ s) := iff.intro eq_or_mem_of_mem_insert (λ h, or.elim h (λ h', begin rw h', apply mem_insert a s end) (λ h', mem_insert_of_mem _ h')) @[simp] theorem insert_eq_of_mem {a : α} {s : set α} (h : a ∈ s) : insert a s = s := ext (take x, iff.intro (begin intro h, cases h with h' h', rw h', exact h, exact h' end) (mem_insert_of_mem _)) theorem insert_comm (a b : α) (s : set α) : insert a (insert b s) = insert b (insert a s) := ext (take c, by simp) theorem insert_ne_empty (a : α) (s : set α) : insert a s ≠ ∅ := λ h, absurd (mem_insert a s) begin rw h, apply not_mem_empty end -- useful in proofs by induction theorem forall_of_forall_insert {P : α → Prop} {a : α} {s : set α} (h : ∀ x, x ∈ insert a s → P x) : ∀ x, x ∈ s → P x := λ x xs, h x (mem_insert_of_mem _ xs) theorem forall_insert_of_forall {P : α → Prop} {a : α} {s : set α} (h : ∀ x, x ∈ s → P x) (ha : P a) : ∀ x, x ∈ insert a s → P x | ._ (or.inl rfl) := ha | x (or.inr p) := h x p lemma bounded_forall_insert_iff {P : α → Prop} {a : α} {s : set α} : (∀ x ∈ insert a s, P x) ↔ P a ∧ (∀x ∈ s, P x) := ⟨take h, ⟨h a $ mem_insert a s, forall_of_forall_insert h⟩, take ⟨P_a, h⟩, forall_insert_of_forall h P_a⟩ /- properties of singletons -/ theorem singleton_eq (a : α) : ({a} : set α) = insert a ∅ := rfl -- TODO: interesting: the theorem fails to elaborate without the annotation @[simp] theorem mem_singleton_iff (a b : α) : a ∈ ({b} : set α) ↔ a = b := iff.intro (assume ainb, or.elim (ainb : a = b ∨ false) (λ aeqb, aeqb) (λ f, false.elim f)) (assume aeqb, or.inl aeqb) -- TODO: again, annotation needed @[simp] theorem mem_singleton (a : α) : a ∈ ({a} : set α) := mem_insert a _ theorem eq_of_mem_singleton {x y : α} (h : x ∈ ({y} : set α)) : x = y := or.elim (eq_or_mem_of_mem_insert h) (suppose x = y, this) (suppose x ∈ (∅ : set α), absurd this (not_mem_empty _)) theorem mem_singleton_of_eq {x y : α} (H : x = y) : x ∈ ({y} : set α) := eq.subst (eq.symm H) (mem_singleton y) theorem insert_eq (x : α) (s : set α) : insert x s = ({x} : set α) ∪ s := ext (take y, iff.intro (suppose y ∈ insert x s, or.elim this (suppose y = x, or.inl (or.inl this)) (suppose y ∈ s, or.inr this)) (suppose y ∈ ({x} : set α) ∪ s, or.elim this (suppose y ∈ ({x} : set α), or.inl (eq_of_mem_singleton this)) (suppose y ∈ s, or.inr this))) @[simp] theorem pair_eq_singleton (a : α) : ({a, a} : set α) = {a} := begin rw insert_eq_of_mem, apply mem_singleton end theorem singleton_ne_empty (a : α) : ({a} : set α) ≠ ∅ := insert_ne_empty _ _ /- separation -/ theorem mem_sep {s : set α} {p : α → Prop} {x : α} (xs : x ∈ s) (px : p x) : x ∈ {x ∈ s | p x} := ⟨xs, px⟩ theorem eq_sep_of_subset {s t : set α} (ssubt : s ⊆ t) : s = {x ∈ t | x ∈ s} := ext (take x, iff.intro (suppose x ∈ s, ⟨ssubt this, this⟩) (suppose x ∈ {x ∈ t | x ∈ s}, this^.right)) @[simp] theorem mem_sep_eq {s : set α} {p : α → Prop} {x : α} : x ∈ {x ∈ s | p x} = (x ∈ s ∧ p x) := rfl theorem mem_sep_iff {s : set α} {p : α → Prop} {x : α} : x ∈ {x ∈ s | p x} ↔ x ∈ s ∧ p x := iff.rfl theorem sep_subset (s : set α) (p : α → Prop) : {x ∈ s | p x} ⊆ s := take x, assume H, and.left H theorem forall_not_of_sep_empty {s : set α} {p : α → Prop} (h : {x ∈ s | p x} = ∅) : ∀ x ∈ s, ¬ p x := take x, suppose x ∈ s, suppose p x, have x ∈ {x ∈ s | p x}, from ⟨by assumption, this⟩, show false, from ne_empty_of_mem this h /- complement -/ theorem mem_compl {s : set α} {x : α} (h : x ∉ s) : x ∈ -s := h theorem not_mem_of_mem_compl {s : set α} {x : α} (h : x ∈ -s) : x ∉ s := h @[simp] theorem mem_compl_eq (s : set α) (x : α) : x ∈ -s = (x ∉ s) := rfl theorem mem_compl_iff (s : set α) (x : α) : x ∈ -s ↔ x ∉ s := iff.rfl @[simp] theorem inter_compl_self (s : set α) : s ∩ -s = ∅ := ext (take x, and_not_self_iff _) @[simp] theorem compl_inter_self (s : set α) : -s ∩ s = ∅ := ext (take x, not_and_self_iff _) @[simp] theorem compl_empty : -(∅ : set α) = univ := ext (take x, not_false_iff) @[simp] theorem compl_union (s t : set α) : -(s ∪ t) = -s ∩ -t := ext (take x, not_or_iff _ _) -- don't declare @[simp], since it is classical theorem compl_compl (s : set α) : -(-s) = s := ext (take x, classical.not_not_iff _) -- ditto theorem compl_inter (s t : set α) : -(s ∩ t) = -s ∪ -t := ext (take x, classical.not_and_iff _ _) @[simp] theorem compl_univ : -(univ : set α) = ∅ := ext (take x, not_true_iff) theorem union_eq_compl_compl_inter_compl (s t : set α) : s ∪ t = -(-s ∩ -t) := by simp [compl_inter, compl_compl] theorem inter_eq_compl_compl_union_compl (s t : set α) : s ∩ t = -(-s ∪ -t) := by simp [compl_compl] theorem union_compl_self (s : set α) : s ∪ -s = univ := ext (take x, classical.or_not_self_iff _) theorem compl_union_self (s : set α) : -s ∪ s = univ := ext (take x, classical.not_or_self_iff _) theorem compl_comp_compl : compl ∘ compl = @id (set α) := funext (λ s, compl_compl s) /- set difference -/ theorem diff_eq (s t : set α) : s \ t = s ∩ -t := rfl theorem mem_diff {s t : set α} {x : α} (h1 : x ∈ s) (h2 : x ∉ t) : x ∈ s \ t := ⟨h1, h2⟩ theorem mem_of_mem_diff {s t : set α} {x : α} (h : x ∈ s \ t) : x ∈ s := h^.left theorem not_mem_of_mem_diff {s t : set α} {x : α} (h : x ∈ s \ t) : x ∉ t := h^.right theorem mem_diff_iff (s t : set α) (x : α) : x ∈ s \ t ↔ x ∈ s ∧ x ∉ t := iff.rfl @[simp] theorem mem_diff_eq (s t : set α) (x : α) : x ∈ s \ t = (x ∈ s ∧ x ∉ t) := rfl theorem union_diff_cancel {s t : set α} (h : s ⊆ t) : s ∪ (t \ s) = t := begin rw [diff_eq, union_distrib_left, union_compl_self, inter_univ, union_eq_self_of_subset_left h] end theorem diff_subset (s t : set α) : s \ t ⊆ s := @inter_subset_left _ s _ theorem compl_eq_univ_diff (s : set α) : -s = univ \ s := ext (take x, iff.intro (assume H, and.intro trivial H) (assume H, and.right H)) /- powerset -/ theorem mem_powerset {x s : set α} (h : x ⊆ s) : x ∈ powerset s := h theorem subset_of_mem_powerset {x s : set α} (h : x ∈ powerset s) : x ⊆ s := h theorem mem_powerset_iff (x s : set α) : x ∈ powerset s ↔ x ⊆ s := iff.rfl /- function image -/ section image @[reducible] def eq_on (f1 f2 : α → β) (a : set α) : Prop := ∀ x ∈ a, f1 x = f2 x -- TODO(Jeremy): is this a bad idea? infix ` ' `:80 := image -- TODO(Jeremy): use bounded exists in image theorem mem_image_eq (f : α → β) (s : set α) (y: β) : y ∈ f ' s = ∃ x, x ∈ s ∧ f x = y := rfl -- the introduction rule theorem mem_image {f : α → β} {s : set α} {x : α} {y : β} (h₁ : x ∈ s) (h₂ : f x = y) : y ∈ f ' s := ⟨x, h₁, h₂⟩ theorem mem_image_of_mem (f : α → β) {x : α} {a : set α} (h : x ∈ a) : f x ∈ image f a := mem_image h rfl -- facilitate cases on being in the image -- REMOVE: should be not necessary with the new equations compiler /- inductive is_mem_image (f : α → β) (s : set α) (y : β) : Prop | mk : Π x : α, x ∈ s → f x = y → is_mem_image theorem mem_image_dest {f : α → β} {s : set α} {y : β} (h : y ∈ f ' s) : is_mem_image f s y := exists.elim h (take x hx, and.elim hx (take xs fxeq, is_mem_image.mk x xs fxeq)) -/ def mem_image_elim {f : α → β} {s : set α} {C : β → Prop} (h : ∀ (x : α), x ∈ s → C (f x)) : ∀{y : β}, y ∈ f ' s → C y | ._ ⟨a, a_in, rfl⟩ := h a a_in def mem_image_elim_on {f : α → β} {s : set α} {C : β → Prop} {y : β} (h_y : y ∈ f ' s) (h : ∀ (x : α), x ∈ s → C (f x)) : C y := mem_image_elim h h_y theorem image_eq_image_of_eq_on {f₁ f₂ : α → β} {s : set α} (heq : eq_on f₁ f₂ s) : f₁ ' s = f₂ ' s := ext (take y, iff.intro (assume ⟨x, xs, f₁xeq⟩, mem_image xs ((heq x xs)^.symm^.trans f₁xeq)) (assume ⟨x, xs, f₂xeq⟩, mem_image xs ((heq x xs)^.trans f₂xeq))) lemma image_comp (f : β → γ) (g : α → β) (a : set α) : (f ∘ g) ' a = f ' (g ' a) := ext (take z, iff.intro (assume ⟨x, (hx₁ : x ∈ a), (hx₂ : f (g x) = z)⟩, have g x ∈ g ' a, from mem_image hx₁ rfl, show z ∈ f ' (g ' a), from mem_image this hx₂) (assume ⟨y, ⟨x, (hz₁ : x ∈ a), (hz₂ : g x = y)⟩, (hy₂ : f y = z)⟩, have f (g x) = z, from eq.subst (eq.symm hz₂) hy₂, show z ∈ (f ∘ g) ' a, from mem_image hz₁ this)) lemma image_subset {a b : set α} (f : α → β) (h : a ⊆ b) : f ' a ⊆ f ' b := take y, assume ⟨x, hx₁, hx₂⟩, mem_image (h hx₁) hx₂ theorem image_union (f : α → β) (s t : set α) : image f (s ∪ t) = image f s ∪ image f t := ext (take y, iff.intro (assume ⟨x, (xst : x ∈ s ∪ t), (fxy : f x = y)⟩, or.elim xst (assume xs, or.inl (mem_image xs fxy)) (assume xt, or.inr (mem_image xt fxy))) (assume H : y ∈ image f s ∪ image f t, or.elim H (assume ⟨x, (xs : x ∈ s), (fxy : f x = y)⟩, mem_image (or.inl xs) fxy) (assume ⟨x, (xt : x ∈ t), (fxy : f x = y)⟩, mem_image (or.inr xt) fxy))) theorem image_empty (f : α → β) : image f ∅ = ∅ := eq_empty_of_forall_not_mem (take y, assume ⟨x, (h : x ∈ ∅), h'⟩, h) theorem mem_image_compl (t : set α) (S : set (set α)) : t ∈ compl ' S ↔ -t ∈ S := iff.intro (assume ⟨t', (Ht' : t' ∈ S), (Ht : -t' = t)⟩, show -t ∈ S, begin rw [-Ht, compl_compl], exact Ht' end) (suppose -t ∈ S, have -(-t) ∈ compl ' S, from mem_image_of_mem compl this, show t ∈ compl ' S, from compl_compl t ▸ this) theorem image_id (s : set α) : id ' s = s := ext (take x, iff.intro (assume ⟨x', (hx' : x' ∈ s), (x'eq : x' = x)⟩, show x ∈ s, begin rw [-x'eq], apply hx' end) (suppose x ∈ s, mem_image_of_mem id this)) theorem compl_compl_image (S : set (set α)) : compl ' (compl ' S) = S := by rw [-image_comp, compl_comp_compl, image_id] lemma bounded_forall_image_of_bounded_forall {f : α → β} {s : set α} {p : β → Prop} (h : ∀ x ∈ s, p (f x)) : ∀ y ∈ f ' s, p y | ._ ⟨x, s_in, rfl⟩ := h x s_in lemma bounded_forall_image_iff {f : α → β} {s : set α} {p : β → Prop} : (∀ y ∈ f ' s, p y) ↔ (∀ x ∈ s, p (f x)) := iff.intro (take h x xs, h _ (mem_image_of_mem _ xs)) bounded_forall_image_of_bounded_forall lemma image_insert_eq {f : α → β} {a : α} {s : set α} : f ' insert a s = insert (f a) (f ' s) := set.ext $ take x, ⟨ take h, match x, h with | ._, ⟨._, ⟨or.inl rfl, rfl⟩⟩ := mem_insert _ _ | ._, ⟨b, ⟨or.inr h, rfl⟩⟩ := mem_insert_of_mem _ $ mem_image h rfl end, take h, match x, h with | ._, or.inl rfl := mem_image (mem_insert _ _) rfl | ._, or.inr ⟨x, ⟨_, rfl⟩⟩ := mem_image (mem_insert_of_mem _ ‹x ∈ s›) rfl end⟩ end image /- union and intersection over a family of sets indexed by a type -/ @[reducible] def Union {α : Sort u} (s : α → set β) : set β := supr s @[reducible] def Inter {α : Sort u} (s : α → set β) : set β := infi s notation `⋃` binders `, ` r:(scoped f, Union f) := r notation `⋂` binders `, ` r:(scoped f, Inter f) := r @[simp] theorem mem_Union_eq {α : Sort u} (x : β) (s : α → set β) : (x ∈ ⋃ i, s i) = (∃ i, x ∈ s i) := propext ⟨take ⟨t, ⟨⟨a, (t_eq : t = s a)⟩, (h : x ∈ t)⟩⟩, ⟨a, t_eq ▸ h⟩, take ⟨a, h⟩, ⟨s a, ⟨⟨a, rfl⟩, h⟩⟩⟩ /- alternative proof: dsimp [Union, supr, Sup]; simp -/ -- TODO: more rewrite rules wrt forall / existentials and logical connectives -- TODO: also eliminate ∃i, ... ∧ i = t ∧ ... @[simp] theorem mem_Inter_eq {α : Sort u} (x : β) (s : α → set β) : (x ∈ ⋂ i, s i) = (∀ i, x ∈ s i) := propext ⟨take (h : ∀a ∈ {a : set β | ∃i : α, a = s i}, x ∈ a) a, h (s a) ⟨a, rfl⟩, take h t ⟨a, (eq : t = s a)⟩, eq^.symm ▸ h a⟩ theorem Union_subset {s : α → set β} {t : set β} (h : ∀ i, s i ⊆ t) : (⋃ i, s i) ⊆ t := -- TODO: should be simpler when sets' order is based on lattices @supr_le (set β) _ set.lattice_set _ _ h theorem subset_Inter {t : set β} {s : α → set β} (h : ∀ i, t ⊆ s i) : t ⊆ ⋂ i, s i := -- TODO: should be simpler when sets' order is based on lattices @le_infi (set β) _ set.lattice_set _ _ h @[simp] -- complete_boolean_algebra theorem compl_Union (s : α → set β) : - (⋃ i, s i) = (⋂ i, - s i) := ext (λ x, begin simp, apply not_exists_iff_forall_not end) -- classical -- complete_boolean_algebra theorem compl_Inter (s : α → set β) : -(⋂ i, s i) = (⋃ i, - s i) := ext (λ x, begin simp, apply classical.not_forall_iff_exists_not end) -- classical -- complete_boolean_algebra theorem Union_eq_comp_Inter_comp (s : α → set β) : (⋃ i, s i) = - (⋂ i, - s i) := by simp [compl_Inter, compl_compl] -- classical -- complete_boolean_algebra theorem Inter_eq_comp_Union_comp (s : α → set β) : (⋂ i, s i) = - (⋃ i, -s i) := by simp [compl_compl] theorem inter_distrib_Union_left (s : set β) (t : α → set β) : s ∩ (⋃ i, t i) = ⋃ i, s ∩ t i := sorry -- complete_distrib_lattice -- classical theorem union_distrib_Inter_left (s : set β) (t : α → set β) : s ∪ (⋂ i, t i) = ⋂ i, s ∪ t i := sorry -- complete_distrib_lattice, really classical? /- bounded unions and intersections -/ theorem mem_bUnion {s : set α} {t : α → set β} {x : α} {y : β} (xs : x ∈ s) (ytx : y ∈ t x) : y ∈ ⋃ x ∈ s, t x := begin simp; exact ⟨x, ⟨xs, ytx⟩⟩ end -- TODO: If we write by there, mem_bInter fails with a syntax error theorem mem_bInter {s : set α} {t : α → set β} {y : β} (h : ∀ x ∈ s, y ∈ t x) : y ∈ ⋂ x ∈ s, t x := by simp; assumption theorem bUnion_subset {s : set α} {t : set β} {u : α → set β} (h : ∀ x ∈ s, u x ⊆ t) : (⋃ x ∈ s, u x) ⊆ t := show (⨆ x ∈ s, u x) ≤ t, -- TODO: should not be necessary when sets' order is based on lattices from supr_le $ take x, supr_le (h x) theorem subset_bInter {s : set α} {t : set β} {u : α → set β} (h : ∀ x ∈ s, t ⊆ u x) : t ⊆ (⋂ x ∈ s, u x) := show t ≤ (⨅ x ∈ s, u x), -- TODO: should not be necessary when sets' order is based on lattices from le_infi $ take x, le_infi (h x) theorem subset_bUnion_of_mem {s : set α} {u : α → set β} {x : α} (xs : x ∈ s) : u x ⊆ (⋃ x ∈ s, u x) := show u x ≤ (⨆ x ∈ s, u x), from le_supr_of_le x $ le_supr _ xs theorem bInter_subset_of_mem {s : set α} {t : α → set β} {x : α} (xs : x ∈ s) : (⋂ x ∈ s, t x) ⊆ t x := show (⨅x ∈ s, t x) ≤ t x, from infi_le_of_le x $ infi_le _ xs @[simp] theorem bInter_empty (u : α → set β) : (⋂ x ∈ (∅ : set α), u x) = univ := show (⨅x ∈ (∅ : set α), u x) = ⊤, -- simplifier should be able to rewrite x ∈ ∅ to false. from infi_emptyset @[simp] theorem bInter_univ (u : α → set β) : (⋂ x ∈ @univ α, u x) = ⋂ x, u x := infi_univ -- TODO(Jeremy): here is an artifact of the the encoding of bounded intersection: -- without dsimp, the next theorem fails to type check, because there is a lambda -- in a type that needs to be contracted. Using simp [eq_of_mem_singleton xa] also works. @[simp] theorem bInter_singleton (a : α) (s : α → set β) : (⋂ x ∈ ({a} : set α), s x) = s a := show (⨅ x ∈ ({a} : set α), s x) = s a, by simp theorem bInter_union (s t : set α) (u : α → set β) : (⋂ x ∈ s ∪ t, u x) = (⋂ x ∈ s, u x) ∩ (⋂ x ∈ t, u x) := show (⨅ x ∈ s ∪ t, u x) = (⨅ x ∈ s, u x) ⊓ (⨅ x ∈ t, u x), from infi_union -- TODO(Jeremy): simp [insert_eq, bInter_union] doesn't work @[simp] theorem bInter_insert (a : α) (s : set α) (t : α → set β) : (⋂ x ∈ insert a s, t x) = t a ∩ (⋂ x ∈ s, t x) := begin rw insert_eq, simp [bInter_union] end -- TODO(Jeremy): another example of where an annotation is needed theorem bInter_pair (a b : α) (s : α → set β) : (⋂ x ∈ ({a, b} : set α), s x) = s a ∩ s b := by simp @[simp] theorem bUnion_empty (s : α → set β) : (⋃ x ∈ (∅ : set α), s x) = ∅ := supr_emptyset @[simp] theorem bUnion_univ (s : α → set β) : (⋃ x ∈ @univ α, s x) = ⋃ x, s x := supr_univ @[simp] theorem bUnion_singleton (a : α) (s : α → set β) : (⋃ x ∈ ({a} : set α), s x) = s a := supr_singleton theorem bUnion_union (s t : set α) (u : α → set β) : (⋃ x ∈ s ∪ t, u x) = (⋃ x ∈ s, u x) ∪ (⋃ x ∈ t, u x) := supr_union -- TODO(Jeremy): once again, simp doesn't do it alone. @[simp] theorem bUnion_insert (a : α) (s : set α) (t : α → set β) : (⋃ x ∈ insert a s, t x) = t a ∪ (⋃ x ∈ s, t x) := begin rw [insert_eq], simp [bUnion_union] end theorem bUnion_pair (a b : α) (s : α → set β) : (⋃ x ∈ ({a, b} : set α), s x) = s a ∪ s b := by simp @[reducible] definition sUnion (S : set (set α)) : set α := Sup S @[reducible] definition sInter (S : set (set α)) : set α := Inf S prefix `⋃₀`:110 := sUnion prefix `⋂₀`:110 := sInter theorem mem_sUnion {x : α} {t : set α} {S : set (set α)} (hx : x ∈ t) (ht : t ∈ S) : x ∈ ⋃₀ S := ⟨t, ⟨ht, hx⟩⟩ -- is this lemma really necessary? theorem not_mem_of_not_mem_sUnion {x : α} {t : set α} {S : set (set α)} (hx : x ∉ ⋃₀ S) (ht : t ∈ S) : x ∉ t := suppose x ∈ t, have x ∈ ⋃₀ S, from mem_sUnion this ht, show false, from hx this theorem mem_sInter {x : α} {t : set α} {S : set (set α)} (h : ∀ t ∈ S, x ∈ t) : x ∈ ⋂₀ S := h theorem sInter_subset_of_mem {S : set (set α)} {t : set α} (tS : t ∈ S) : (⋂₀ S) ⊆ t := Inf_le tS theorem subset_sUnion_of_mem {S : set (set α)} {t : set α} (tS : t ∈ S) : t ⊆ (⋃₀ S) := le_Sup tS @[simp] theorem sUnion_empty : ⋃₀ ∅ = (∅ : set α) := Sup_empty @[simp] theorem sInter_empty : ⋂₀ ∅ = (univ : set α) := Inf_empty @[simp] theorem sUnion_singleton (s : set α) : ⋃₀ {s} = s := Sup_singleton @[simp] theorem sInter_singleton (s : set α) : ⋂₀ {s} = s := Inf_singleton theorem sUnion_union (S T : set (set α)) : ⋃₀ (S ∪ T) = ⋃₀ S ∪ ⋃₀ T := Sup_union theorem sInter_union (S T : set (set α)) : ⋂₀ (S ∪ T) = ⋂₀ S ∩ ⋂₀ T := Inf_union @[simp] theorem sUnion_insert (s : set α) (T : set (set α)) : ⋃₀ (insert s T) = s ∪ ⋃₀ T := Sup_insert @[simp] theorem sInter_insert (s : set α) (T : set (set α)) : ⋂₀ (insert s T) = s ∩ ⋂₀ T := Inf_insert @[simp] theorem sUnion_image (f : α → set β) (s : set α) : ⋃₀ (f ' s) = ⋃ x ∈ s, f x := Sup_image @[simp] theorem sInter_image (f : α → set β) (s : set α) : ⋂₀ (f ' s) = ⋂ x ∈ s, f x := Inf_image theorem compl_sUnion (S : set (set α)) : - ⋃₀ S = ⋂₀ (compl ' S) := sorry -- begin simp, reflexivity end -- classical theorem sUnion_eq_compl_sInter_compl (S : set (set α)) : ⋃₀ S = - ⋂₀ (compl ' S) := by rw [-compl_compl (⋃₀ S), compl_sUnion] -- classical theorem compl_sInter (S : set (set α)) : - ⋂₀ S = ⋃₀ (compl ' S) := by rw [sUnion_eq_compl_sInter_compl, compl_compl_image] -- classical theorem sInter_eq_comp_sUnion_compl (S : set (set α)) : ⋂₀ S = -(⋃₀ (compl ' S)) := by rw [-compl_compl (⋂₀ S), compl_sInter] theorem inter_empty_of_inter_sUnion_empty {s t : set α} {S : set (set α)} (hs : t ∈ S) (h : s ∩ ⋃₀ S = ∅) : s ∩ t = ∅ := eq_empty_of_subset_empty begin rw -h, apply inter_subset_inter_left, apply subset_sUnion_of_mem hs end theorem Union_eq_sUnion_image (s : α → set β) : (⋃ i, s i) = ⋃₀ (s ' univ) := by simp theorem Inter_eq_sInter_image {α I : Type} (s : I → set α) : (⋂ i, s i) = ⋂₀ (s ' univ) := by simp section disjoint variable [semilattice_inf_bot α] definition disjoint (a b : α) : Prop := a ⊓ b = ⊥ lemma disjoint_symm {a b : α} : disjoint a b → disjoint b a := suppose a ⊓ b = ⊥, show b ⊓ a = ⊥, from this ▸ inf_comm lemma disjoint_bot_left {a : α} : disjoint ⊥ a := bot_inf_eq lemma disjoint_bot_right {a : α} : disjoint a ⊥ := inf_bot_eq end disjoint end set
f1d2ec3d4238a06f0d3f5f45710062ed5abe9854
957a80ea22c5abb4f4670b250d55534d9db99108
/tests/lean/task.lean
eaf200fa3bbc4f09e25683376b0cde3d5d030b46
[ "Apache-2.0" ]
permissive
GaloisInc/lean
aa1e64d604051e602fcf4610061314b9a37ab8cd
f1ec117a24459b59c6ff9e56a1d09d9e9e60a6c0
refs/heads/master
1,592,202,909,807
1,504,624,387,000
1,504,624,387,000
75,319,626
2
1
Apache-2.0
1,539,290,164,000
1,480,616,104,000
C++
UTF-8
Lean
false
false
253
lean
run_cmd tactic.run_async (tactic.trace "trace message from a different task") def {u} foo {α : Type u} {n : ℕ} : array α (0+n) → array α n := if n = 0 then λ v, cast (by async { simp }) v else λ v, cast (by async { simp }) v #print foo
e4462906d182309d505acb9d2d3c23582a0bdb73
63abd62053d479eae5abf4951554e1064a4c45b4
/src/topology/metric_space/isometry.lean
897b67f6effd741201ecdf80a6d846c7a7cc4669
[ "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
16,538
lean
/- Copyright (c) 2018 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Isometries of emetric and metric spaces Authors: Sébastien Gouëzel -/ import topology.bounded_continuous_function import topology.compacts /-! # Isometries We define isometries, i.e., maps between emetric spaces that preserve the edistance (on metric spaces, these are exactly the maps that preserve distances), and prove their basic properties. We also introduce isometric bijections. -/ noncomputable theory universes u v w variables {α : Type u} {β : Type v} {γ : Type w} open function set /-- An isometry (also known as isometric embedding) is a map preserving the edistance between emetric spaces, or equivalently the distance between metric space. -/ def isometry [emetric_space α] [emetric_space β] (f : α → β) : Prop := ∀x1 x2 : α, edist (f x1) (f x2) = edist x1 x2 /-- On metric spaces, a map is an isometry if and only if it preserves distances. -/ lemma isometry_emetric_iff_metric [metric_space α] [metric_space β] {f : α → β} : isometry f ↔ (∀x y, dist (f x) (f y) = dist x y) := ⟨assume H x y, by simp [dist_edist, H x y], assume H x y, by simp [edist_dist, H x y]⟩ /-- An isometry preserves edistances. -/ theorem isometry.edist_eq [emetric_space α] [emetric_space β] {f : α → β} (hf : isometry f) (x y : α) : edist (f x) (f y) = edist x y := hf x y /-- An isometry preserves distances. -/ theorem isometry.dist_eq [metric_space α] [metric_space β] {f : α → β} (hf : isometry f) (x y : α) : dist (f x) (f y) = dist x y := by rw [dist_edist, dist_edist, hf] section emetric_isometry variables [emetric_space α] [emetric_space β] [emetric_space γ] variables {f : α → β} {x y z : α} {s : set α} lemma isometry.lipschitz (h : isometry f) : lipschitz_with 1 f := lipschitz_with.of_edist_le $ λ x y, le_of_eq (h x y) lemma isometry.antilipschitz (h : isometry f) : antilipschitz_with 1 f := λ x y, by simp only [h x y, ennreal.coe_one, one_mul, le_refl] /-- An isometry is injective -/ lemma isometry.injective (h : isometry f) : injective f := h.antilipschitz.injective /-- Any map on a subsingleton is an isometry -/ theorem isometry_subsingleton [subsingleton α] : isometry f := λx y, by rw subsingleton.elim x y; simp /-- The identity is an isometry -/ lemma isometry_id : isometry (id : α → α) := λx y, rfl /-- The composition of isometries is an isometry -/ theorem isometry.comp {g : β → γ} {f : α → β} (hg : isometry g) (hf : isometry f) : isometry (g ∘ f) := assume x y, calc edist ((g ∘ f) x) ((g ∘ f) y) = edist (f x) (f y) : hg _ _ ... = edist x y : hf _ _ /-- An isometry is an embedding -/ theorem isometry.uniform_embedding (hf : isometry f) : uniform_embedding f := hf.antilipschitz.uniform_embedding hf.lipschitz.uniform_continuous /-- An isometry is continuous. -/ lemma isometry.continuous (hf : isometry f) : continuous f := hf.lipschitz.continuous /-- The right inverse of an isometry is an isometry. -/ lemma isometry.right_inv {f : α → β} {g : β → α} (h : isometry f) (hg : right_inverse g f) : isometry g := λ x y, by rw [← h, hg _, hg _] /-- Isometries preserve the diameter in emetric spaces. -/ lemma isometry.ediam_image (hf : isometry f) (s : set α) : emetric.diam (f '' s) = emetric.diam s := eq_of_forall_ge_iff $ λ d, by simp only [emetric.diam_le_iff_forall_edist_le, ball_image_iff, hf.edist_eq] lemma isometry.ediam_range (hf : isometry f) : emetric.diam (range f) = emetric.diam (univ : set α) := by { rw ← image_univ, exact hf.ediam_image univ } /-- The injection from a subtype is an isometry -/ lemma isometry_subtype_coe {s : set α} : isometry (coe : s → α) := λx y, rfl end emetric_isometry --section /-- An isometry preserves the diameter in metric spaces. -/ lemma isometry.diam_image [metric_space α] [metric_space β] {f : α → β} (hf : isometry f) (s : set α) : metric.diam (f '' s) = metric.diam s := by rw [metric.diam, metric.diam, hf.ediam_image] lemma isometry.diam_range [metric_space α] [metric_space β] {f : α → β} (hf : isometry f) : metric.diam (range f) = metric.diam (univ : set α) := by { rw ← image_univ, exact hf.diam_image univ } /-- `α` and `β` are isometric if there is an isometric bijection between them. -/ @[nolint has_inhabited_instance] -- such a bijection need not exist structure isometric (α : Type*) (β : Type*) [emetric_space α] [emetric_space β] extends α ≃ β := (isometry_to_fun : isometry to_fun) infix ` ≃ᵢ `:25 := isometric namespace isometric variables [emetric_space α] [emetric_space β] [emetric_space γ] instance : has_coe_to_fun (α ≃ᵢ β) := ⟨λ_, α → β, λe, e.to_equiv⟩ lemma coe_eq_to_equiv (h : α ≃ᵢ β) (a : α) : h a = h.to_equiv a := rfl @[simp] lemma coe_to_equiv (h : α ≃ᵢ β) : ⇑h.to_equiv = h := rfl protected lemma isometry (h : α ≃ᵢ β) : isometry h := h.isometry_to_fun protected lemma edist_eq (h : α ≃ᵢ β) (x y : α) : edist (h x) (h y) = edist x y := h.isometry.edist_eq x y protected lemma dist_eq {α β : Type*} [metric_space α] [metric_space β] (h : α ≃ᵢ β) (x y : α) : dist (h x) (h y) = dist x y := h.isometry.dist_eq x y protected lemma continuous (h : α ≃ᵢ β) : continuous h := h.isometry.continuous lemma to_equiv_inj : ∀ ⦃h₁ h₂ : α ≃ᵢ β⦄, (h₁.to_equiv = h₂.to_equiv) → h₁ = h₂ | ⟨e₁, h₁⟩ ⟨e₂, h₂⟩ H := by { dsimp at H, subst e₁ } @[ext] lemma ext ⦃h₁ h₂ : α ≃ᵢ β⦄ (H : ∀ x, h₁ x = h₂ x) : h₁ = h₂ := to_equiv_inj $ equiv.ext H /-- Alternative constructor for isometric bijections, taking as input an isometry, and a right inverse. -/ def mk' (f : α → β) (g : β → α) (hfg : ∀ x, f (g x) = x) (hf : isometry f) : α ≃ᵢ β := { to_fun := f, inv_fun := g, left_inv := λ x, hf.injective $ hfg _, right_inv := hfg, isometry_to_fun := hf } /-- The identity isometry of a space. -/ protected def refl (α : Type*) [emetric_space α] : α ≃ᵢ α := { isometry_to_fun := isometry_id, .. equiv.refl α } /-- The composition of two isometric isomorphisms, as an isometric isomorphism. -/ protected def trans (h₁ : α ≃ᵢ β) (h₂ : β ≃ᵢ γ) : α ≃ᵢ γ := { isometry_to_fun := h₂.isometry_to_fun.comp h₁.isometry_to_fun, .. equiv.trans h₁.to_equiv h₂.to_equiv } @[simp] lemma trans_apply (h₁ : α ≃ᵢ β) (h₂ : β ≃ᵢ γ) (x : α) : h₁.trans h₂ x = h₂ (h₁ x) := rfl /-- The inverse of an isometric isomorphism, as an isometric isomorphism. -/ protected def symm (h : α ≃ᵢ β) : β ≃ᵢ α := { isometry_to_fun := h.isometry.right_inv h.right_inv, to_equiv := h.to_equiv.symm } @[simp] lemma symm_symm (h : α ≃ᵢ β) : h.symm.symm = h := to_equiv_inj h.to_equiv.symm_symm @[simp] lemma apply_symm_apply (h : α ≃ᵢ β) (y : β) : h (h.symm y) = y := h.to_equiv.apply_symm_apply y @[simp] lemma symm_apply_apply (h : α ≃ᵢ β) (x : α) : h.symm (h x) = x := h.to_equiv.symm_apply_apply x lemma symm_apply_eq (h : α ≃ᵢ β) {x : α} {y : β} : h.symm y = x ↔ y = h x := h.to_equiv.symm_apply_eq lemma eq_symm_apply (h : α ≃ᵢ β) {x : α} {y : β} : x = h.symm y ↔ h x = y := h.to_equiv.eq_symm_apply lemma symm_comp_self (h : α ≃ᵢ β) : ⇑h.symm ∘ ⇑h = id := funext $ assume a, h.to_equiv.left_inv a lemma self_comp_symm (h : α ≃ᵢ β) : ⇑h ∘ ⇑h.symm = id := funext $ assume a, h.to_equiv.right_inv a lemma range_coe (h : α ≃ᵢ β) : range h = univ := eq_univ_of_forall $ assume b, ⟨h.symm b, congr_fun h.self_comp_symm b⟩ lemma image_symm (h : α ≃ᵢ β) : image h.symm = preimage h := image_eq_preimage_of_inverse h.symm.to_equiv.left_inv h.symm.to_equiv.right_inv lemma preimage_symm (h : α ≃ᵢ β) : preimage h.symm = image h := (image_eq_preimage_of_inverse h.to_equiv.left_inv h.to_equiv.right_inv).symm @[simp] lemma symm_trans_apply (h₁ : α ≃ᵢ β) (h₂ : β ≃ᵢ γ) (x : γ) : (h₁.trans h₂).symm x = h₁.symm (h₂.symm x) := rfl /-- The (bundled) homeomorphism associated to an isometric isomorphism. -/ protected def to_homeomorph (h : α ≃ᵢ β) : α ≃ₜ β := { continuous_to_fun := h.continuous, continuous_inv_fun := h.symm.continuous, .. h } @[simp] lemma coe_to_homeomorph (h : α ≃ᵢ β) : ⇑(h.to_homeomorph) = h := rfl @[simp] lemma to_homeomorph_to_equiv (h : α ≃ᵢ β) : h.to_homeomorph.to_equiv = h.to_equiv := rfl /-- The group of isometries. -/ instance : group (α ≃ᵢ α) := { one := isometric.refl _, mul := λ e₁ e₂, e₁.trans e₂, inv := isometric.symm, mul_assoc := λ e₁ e₂ e₃, rfl, one_mul := λ e, ext $ λ _, rfl, mul_one := λ e, ext $ λ _, rfl, mul_left_inv := λ e, ext e.apply_symm_apply } @[simp] lemma coe_one : ⇑(1 : α ≃ᵢ α) = id := rfl @[simp] lemma coe_mul (e₁ e₂ : α ≃ᵢ α) : ⇑(e₁ * e₂) = e₂ ∘ e₁ := rfl lemma mul_apply (e₁ e₂ : α ≃ᵢ α) (x : α) : (e₁ * e₂) x = e₂ (e₁ x) := rfl @[simp] lemma inv_apply_self (e : α ≃ᵢ α) (x: α) : e⁻¹ (e x) = x := e.symm_apply_apply x @[simp] lemma apply_inv_self (e : α ≃ᵢ α) (x: α) : e (e⁻¹ x) = x := e.apply_symm_apply x section normed_group variables {G : Type*} [normed_group G] /-- Addition `y ↦ y + x` as an `isometry`. -/ protected def add_right (x : G) : G ≃ᵢ G := { isometry_to_fun := isometry_emetric_iff_metric.2 $ λ y z, dist_add_right _ _ _, .. equiv.add_right x } @[simp] lemma add_right_to_equiv (x : G) : (isometric.add_right x).to_equiv = equiv.add_right x := rfl @[simp] lemma coe_add_right (x : G) : (isometric.add_right x : G → G) = λ y, y + x := rfl lemma add_right_apply (x y : G) : (isometric.add_right x : G → G) y = y + x := rfl @[simp] lemma add_right_symm (x : G) : (isometric.add_right x).symm = isometric.add_right (-x) := ext $ λ y, rfl /-- Addition `y ↦ x + y` as an `isometry`. -/ protected def add_left (x : G) : G ≃ᵢ G := { isometry_to_fun := isometry_emetric_iff_metric.2 $ λ y z, dist_add_left _ _ _, to_equiv := equiv.add_left x } @[simp] lemma add_left_to_equiv (x : G) : (isometric.add_left x).to_equiv = equiv.add_left x := rfl @[simp] lemma coe_add_left (x : G) : ⇑(isometric.add_left x) = (+) x := rfl @[simp] lemma add_left_symm (x : G) : (isometric.add_left x).symm = isometric.add_left (-x) := ext $ λ y, rfl variable (G) /-- Negation `x ↦ -x` as an `isometry`. -/ protected def neg : G ≃ᵢ G := { isometry_to_fun := isometry_emetric_iff_metric.2 $ λ x y, dist_neg_neg _ _, to_equiv := equiv.neg G } variable {G} @[simp] lemma neg_symm : (isometric.neg G).symm = isometric.neg G := rfl @[simp] lemma neg_to_equiv : (isometric.neg G).to_equiv = equiv.neg G := rfl @[simp] lemma coe_neg : ⇑(isometric.neg G) = has_neg.neg := rfl end normed_group end isometric /-- An isometry induces an isometric isomorphism between the source space and the range of the isometry. -/ def isometry.isometric_on_range [emetric_space α] [emetric_space β] {f : α → β} (h : isometry f) : α ≃ᵢ range f := { isometry_to_fun := λx y, by simpa [subtype.edist_eq] using h x y, .. equiv.set.range f h.injective } @[simp] lemma isometry.isometric_on_range_apply [emetric_space α] [emetric_space β] {f : α → β} (h : isometry f) (x : α) : h.isometric_on_range x = ⟨f x, mem_range_self _⟩ := rfl /-- In a normed algebra, the inclusion of the base field in the extended field is an isometry. -/ lemma algebra_map_isometry (𝕜 : Type*) (𝕜' : Type*) [normed_field 𝕜] [normed_ring 𝕜'] [normed_algebra 𝕜 𝕜'] : isometry (algebra_map 𝕜 𝕜') := begin refine isometry_emetric_iff_metric.2 (λx y, _), rw [dist_eq_norm, dist_eq_norm, ← ring_hom.map_sub, norm_algebra_map_eq], end /-- The space of bounded sequences, with its sup norm -/ @[reducible] def ℓ_infty_ℝ : Type := bounded_continuous_function ℕ ℝ open bounded_continuous_function metric topological_space namespace Kuratowski_embedding /-! ### In this section, we show that any separable metric space can be embedded isometrically in ℓ^∞(ℝ) -/ variables {f g : ℓ_infty_ℝ} {n : ℕ} {C : ℝ} [metric_space α] (x : ℕ → α) (a b : α) /-- A metric space can be embedded in `l^∞(ℝ)` via the distances to points in a fixed countable set, if this set is dense. This map is given in the next definition, without density assumptions. -/ def embedding_of_subset : ℓ_infty_ℝ := of_normed_group_discrete (λn, dist a (x n) - dist (x 0) (x n)) (dist a (x 0)) (λ_, abs_dist_sub_le _ _ _) lemma embedding_of_subset_coe : embedding_of_subset x a n = dist a (x n) - dist (x 0) (x n) := rfl /-- The embedding map is always a semi-contraction. -/ lemma embedding_of_subset_dist_le (a b : α) : dist (embedding_of_subset x a) (embedding_of_subset x b) ≤ dist a b := begin refine (dist_le dist_nonneg).2 (λn, _), simp only [embedding_of_subset_coe, real.dist_eq], convert abs_dist_sub_le a b (x n) using 2, ring end /-- When the reference set is dense, the embedding map is an isometry on its image. -/ lemma embedding_of_subset_isometry (H : dense_range x) : isometry (embedding_of_subset x) := begin refine isometry_emetric_iff_metric.2 (λa b, _), refine le_antisymm (embedding_of_subset_dist_le x a b) (real.le_of_forall_epsilon_le (λe epos, _)), /- First step: find n with dist a (x n) < e -/ rcases metric.mem_closure_range_iff.1 (H a) (e/2) (half_pos epos) with ⟨n, hn⟩, /- Second step: use the norm control at index n to conclude -/ have C : dist b (x n) - dist a (x n) = embedding_of_subset x b n - embedding_of_subset x a n := by { simp only [embedding_of_subset_coe, sub_sub_sub_cancel_right] }, have := calc dist a b ≤ dist a (x n) + dist (x n) b : dist_triangle _ _ _ ... = 2 * dist a (x n) + (dist b (x n) - dist a (x n)) : by { simp [dist_comm], ring } ... ≤ 2 * dist a (x n) + abs (dist b (x n) - dist a (x n)) : by apply_rules [add_le_add_left, le_abs_self] ... ≤ 2 * (e/2) + abs (embedding_of_subset x b n - embedding_of_subset x a n) : begin rw [C], apply_rules [add_le_add, mul_le_mul_of_nonneg_left, le_of_lt hn, le_refl], norm_num end ... ≤ 2 * (e/2) + dist (embedding_of_subset x b) (embedding_of_subset x a) : begin rw [← sub_apply], apply add_le_add_left, rw [sub_apply, ←real.dist_eq], apply dist_coe_le_dist end ... = dist (embedding_of_subset x b) (embedding_of_subset x a) + e : by ring, simpa [dist_comm] using this end /-- Every separable metric space embeds isometrically in ℓ_infty_ℝ. -/ theorem exists_isometric_embedding (α : Type u) [metric_space α] [separable_space α] : ∃(f : α → ℓ_infty_ℝ), isometry f := begin cases (univ : set α).eq_empty_or_nonempty with h h, { use (λ_, 0), assume x, exact absurd h (nonempty.ne_empty ⟨x, mem_univ x⟩) }, { /- We construct a map x : ℕ → α with dense image -/ rcases h with ⟨basepoint⟩, haveI : inhabited α := ⟨basepoint⟩, have : ∃s:set α, countable s ∧ dense s := exists_countable_dense α, rcases this with ⟨S, ⟨S_countable, S_dense⟩⟩, rcases countable_iff_exists_surjective.1 S_countable with ⟨x, x_range⟩, /- Use embedding_of_subset to construct the desired isometry -/ exact ⟨embedding_of_subset x, embedding_of_subset_isometry x (S_dense.mono x_range)⟩ } end end Kuratowski_embedding open topological_space Kuratowski_embedding /-- The Kuratowski embedding is an isometric embedding of a separable metric space in ℓ^∞(ℝ) -/ def Kuratowski_embedding (α : Type u) [metric_space α] [separable_space α] : α → ℓ_infty_ℝ := classical.some (Kuratowski_embedding.exists_isometric_embedding α) /-- The Kuratowski embedding is an isometry -/ protected lemma Kuratowski_embedding.isometry (α : Type u) [metric_space α] [separable_space α] : isometry (Kuratowski_embedding α) := classical.some_spec (exists_isometric_embedding α) /-- Version of the Kuratowski embedding for nonempty compacts -/ def nonempty_compacts.Kuratowski_embedding (α : Type u) [metric_space α] [compact_space α] [nonempty α] : nonempty_compacts ℓ_infty_ℝ := ⟨range (Kuratowski_embedding α), range_nonempty _, compact_range (Kuratowski_embedding.isometry α).continuous⟩
84ce8ebe0bf6a13a606f89b3f497605eadb5a3eb
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/real/nnreal.lean
cca419cf48609e1dbb9e70c3851b5f6843ceecc2
[ "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
35,112
lean
/- Copyright (c) 2018 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import algebra.algebra.basic import algebra.order.field.canonical.basic import algebra.order.nonneg.field import algebra.order.nonneg.floor import data.real.pointwise import order.conditionally_complete_lattice.group import tactic.positivity /-! # Nonnegative real numbers > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we define `nnreal` (notation: `ℝ≥0`) to be the type of non-negative real numbers, a.k.a. the interval `[0, ∞)`. We also define the following operations and structures on `ℝ≥0`: * the order on `ℝ≥0` is the restriction of the order on `ℝ`; these relations define a conditionally complete linear order with a bottom element, `conditionally_complete_linear_order_bot`; * `a + b` and `a * b` are the restrictions of addition and multiplication of real numbers to `ℝ≥0`; these operations together with `0 = ⟨0, _⟩` and `1 = ⟨1, _⟩` turn `ℝ≥0` into a conditionally complete linear ordered archimedean commutative semifield; we have no typeclass for this in `mathlib` yet, so we define the following instances instead: - `linear_ordered_semiring ℝ≥0`; - `ordered_comm_semiring ℝ≥0`; - `canonically_ordered_comm_semiring ℝ≥0`; - `linear_ordered_comm_group_with_zero ℝ≥0`; - `canonically_linear_ordered_add_monoid ℝ≥0`; - `archimedean ℝ≥0`; - `conditionally_complete_linear_order_bot ℝ≥0`. These instances are derived from corresponding instances about the type `{x : α // 0 ≤ x}` in an appropriate ordered field/ring/group/monoid `α`. See `algebra/order/nonneg`. * `real.to_nnreal x` is defined as `⟨max x 0, _⟩`, i.e. `↑(real.to_nnreal x) = x` when `0 ≤ x` and `↑(real.to_nnreal x) = 0` otherwise. We also define an instance `can_lift ℝ ℝ≥0`. This instance can be used by the `lift` tactic to replace `x : ℝ` and `hx : 0 ≤ x` in the proof context with `x : ℝ≥0` while replacing all occurences of `x` with `↑x`. This tactic also works for a function `f : α → ℝ` with a hypothesis `hf : ∀ x, 0 ≤ f x`. ## Notations This file defines `ℝ≥0` as a localized notation for `nnreal`. -/ open_locale classical big_operators /-- Nonnegative real numbers. -/ @[derive [ strict_ordered_semiring, comm_monoid_with_zero, -- to ensure these instances are computable floor_semiring, comm_semiring, semiring, semilattice_inf, semilattice_sup, distrib_lattice, densely_ordered, order_bot, canonically_linear_ordered_semifield, linear_ordered_comm_group_with_zero, archimedean, linear_ordered_semiring, ordered_comm_semiring, canonically_ordered_comm_semiring, has_sub, has_ordered_sub, has_div, inhabited]] def nnreal := {r : ℝ // 0 ≤ r} localized "notation (name := nnreal) `ℝ≥0` := nnreal" in nnreal namespace nnreal instance : has_coe ℝ≥0 ℝ := ⟨subtype.val⟩ /- Simp lemma to put back `n.val` into the normal form given by the coercion. -/ @[simp] lemma val_eq_coe (n : ℝ≥0) : n.val = n := rfl instance can_lift : can_lift ℝ ℝ≥0 coe (λ r, 0 ≤ r) := subtype.can_lift _ protected lemma eq {n m : ℝ≥0} : (n : ℝ) = (m : ℝ) → n = m := subtype.eq protected lemma eq_iff {n m : ℝ≥0} : (n : ℝ) = (m : ℝ) ↔ n = m := iff.intro nnreal.eq (congr_arg coe) lemma ne_iff {x y : ℝ≥0} : (x : ℝ) ≠ (y : ℝ) ↔ x ≠ y := not_iff_not_of_iff $ nnreal.eq_iff protected lemma «forall» {p : ℝ≥0 → Prop} : (∀ x : ℝ≥0, p x) ↔ ∀ (x : ℝ) (hx : 0 ≤ x), p ⟨x, hx⟩ := subtype.forall protected lemma «exists» {p : ℝ≥0 → Prop} : (∃ x : ℝ≥0, p x) ↔ ∃ (x : ℝ) (hx : 0 ≤ x), p ⟨x, hx⟩ := subtype.exists /-- Reinterpret a real number `r` as a non-negative real number. Returns `0` if `r < 0`. -/ noncomputable def _root_.real.to_nnreal (r : ℝ) : ℝ≥0 := ⟨max r 0, le_max_right _ _⟩ lemma _root_.real.coe_to_nnreal (r : ℝ) (hr : 0 ≤ r) : (real.to_nnreal r : ℝ) = r := max_eq_left hr lemma _root_.real.to_nnreal_of_nonneg {r : ℝ} (hr : 0 ≤ r) : r.to_nnreal = ⟨r, hr⟩ := by simp_rw [real.to_nnreal, max_eq_left hr] lemma _root_.real.le_coe_to_nnreal (r : ℝ) : r ≤ real.to_nnreal r := le_max_left r 0 lemma coe_nonneg (r : ℝ≥0) : (0 : ℝ) ≤ r := r.2 @[norm_cast] theorem coe_mk (a : ℝ) (ha) : ((⟨a, ha⟩ : ℝ≥0) : ℝ) = a := rfl example : has_zero ℝ≥0 := by apply_instance example : has_one ℝ≥0 := by apply_instance example : has_add ℝ≥0 := by apply_instance noncomputable example : has_sub ℝ≥0 := by apply_instance example : has_mul ℝ≥0 := by apply_instance noncomputable example : has_inv ℝ≥0 := by apply_instance noncomputable example : has_div ℝ≥0 := by apply_instance example : has_le ℝ≥0 := by apply_instance example : has_bot ℝ≥0 := by apply_instance example : inhabited ℝ≥0 := by apply_instance example : nontrivial ℝ≥0 := by apply_instance protected lemma coe_injective : function.injective (coe : ℝ≥0 → ℝ) := subtype.coe_injective @[simp, norm_cast] protected lemma coe_eq {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) = r₂ ↔ r₁ = r₂ := nnreal.coe_injective.eq_iff protected lemma coe_zero : ((0 : ℝ≥0) : ℝ) = 0 := rfl protected lemma coe_one : ((1 : ℝ≥0) : ℝ) = 1 := rfl protected lemma coe_add (r₁ r₂ : ℝ≥0) : ((r₁ + r₂ : ℝ≥0) : ℝ) = r₁ + r₂ := rfl protected lemma coe_mul (r₁ r₂ : ℝ≥0) : ((r₁ * r₂ : ℝ≥0) : ℝ) = r₁ * r₂ := rfl protected lemma coe_inv (r : ℝ≥0) : ((r⁻¹ : ℝ≥0) : ℝ) = r⁻¹ := rfl protected lemma coe_div (r₁ r₂ : ℝ≥0) : ((r₁ / r₂ : ℝ≥0) : ℝ) = r₁ / r₂ := rfl @[simp, norm_cast] protected lemma coe_bit0 (r : ℝ≥0) : ((bit0 r : ℝ≥0) : ℝ) = bit0 r := rfl @[simp, norm_cast] protected lemma coe_bit1 (r : ℝ≥0) : ((bit1 r : ℝ≥0) : ℝ) = bit1 r := rfl protected lemma coe_two : ((2 : ℝ≥0) : ℝ) = 2 := rfl @[simp, norm_cast] protected lemma coe_sub {r₁ r₂ : ℝ≥0} (h : r₂ ≤ r₁) : ((r₁ - r₂ : ℝ≥0) : ℝ) = r₁ - r₂ := max_eq_left $ le_sub_comm.2 $ by simp [show (r₂ : ℝ) ≤ r₁, from h] @[simp, norm_cast] protected lemma coe_eq_zero (r : ℝ≥0) : ↑r = (0 : ℝ) ↔ r = 0 := by rw [← nnreal.coe_zero, nnreal.coe_eq] @[simp, norm_cast] protected lemma coe_eq_one (r : ℝ≥0) : ↑r = (1 : ℝ) ↔ r = 1 := by rw [← nnreal.coe_one, nnreal.coe_eq] lemma coe_ne_zero {r : ℝ≥0} : (r : ℝ) ≠ 0 ↔ r ≠ 0 := by norm_cast example : comm_semiring ℝ≥0 := by apply_instance /-- Coercion `ℝ≥0 → ℝ` as a `ring_hom`. -/ def to_real_hom : ℝ≥0 →+* ℝ := ⟨coe, nnreal.coe_one, nnreal.coe_mul, nnreal.coe_zero, nnreal.coe_add⟩ @[simp] lemma coe_to_real_hom : ⇑to_real_hom = coe := rfl section actions /-- A `mul_action` over `ℝ` restricts to a `mul_action` over `ℝ≥0`. -/ instance {M : Type*} [mul_action ℝ M] : mul_action ℝ≥0 M := mul_action.comp_hom M to_real_hom.to_monoid_hom lemma smul_def {M : Type*} [mul_action ℝ M] (c : ℝ≥0) (x : M) : c • x = (c : ℝ) • x := rfl instance {M N : Type*} [mul_action ℝ M] [mul_action ℝ N] [has_smul M N] [is_scalar_tower ℝ M N] : is_scalar_tower ℝ≥0 M N := { smul_assoc := λ r, (smul_assoc (r : ℝ) : _)} instance smul_comm_class_left {M N : Type*} [mul_action ℝ N] [has_smul M N] [smul_comm_class ℝ M N] : smul_comm_class ℝ≥0 M N := { smul_comm := λ r, (smul_comm (r : ℝ) : _)} instance smul_comm_class_right {M N : Type*} [mul_action ℝ N] [has_smul M N] [smul_comm_class M ℝ N] : smul_comm_class M ℝ≥0 N := { smul_comm := λ m r, (smul_comm m (r : ℝ) : _)} /-- A `distrib_mul_action` over `ℝ` restricts to a `distrib_mul_action` over `ℝ≥0`. -/ instance {M : Type*} [add_monoid M] [distrib_mul_action ℝ M] : distrib_mul_action ℝ≥0 M := distrib_mul_action.comp_hom M to_real_hom.to_monoid_hom /-- A `module` over `ℝ` restricts to a `module` over `ℝ≥0`. -/ instance {M : Type*} [add_comm_monoid M] [module ℝ M] : module ℝ≥0 M := module.comp_hom M to_real_hom /-- An `algebra` over `ℝ` restricts to an `algebra` over `ℝ≥0`. -/ instance {A : Type*} [semiring A] [algebra ℝ A] : algebra ℝ≥0 A := { smul := (•), commutes' := λ r x, by simp [algebra.commutes], smul_def' := λ r x, by simp [←algebra.smul_def (r : ℝ) x, smul_def], to_ring_hom := ((algebra_map ℝ A).comp (to_real_hom : ℝ≥0 →+* ℝ)) } -- verify that the above produces instances we might care about example : algebra ℝ≥0 ℝ := by apply_instance example : distrib_mul_action ℝ≥0ˣ ℝ := by apply_instance end actions example : monoid_with_zero ℝ≥0 := by apply_instance example : comm_monoid_with_zero ℝ≥0 := by apply_instance noncomputable example : comm_group_with_zero ℝ≥0 := by apply_instance @[simp, norm_cast] lemma coe_indicator {α} (s : set α) (f : α → ℝ≥0) (a : α) : ((s.indicator f a : ℝ≥0) : ℝ) = s.indicator (λ x, f x) a := (to_real_hom : ℝ≥0 →+ ℝ).map_indicator _ _ _ @[simp, norm_cast] lemma coe_pow (r : ℝ≥0) (n : ℕ) : ((r^n : ℝ≥0) : ℝ) = r^n := to_real_hom.map_pow r n @[simp, norm_cast] lemma coe_zpow (r : ℝ≥0) (n : ℤ) : ((r^n : ℝ≥0) : ℝ) = r^n := by cases n; simp @[norm_cast] lemma coe_list_sum (l : list ℝ≥0) : ((l.sum : ℝ≥0) : ℝ) = (l.map coe).sum := to_real_hom.map_list_sum l @[norm_cast] lemma coe_list_prod (l : list ℝ≥0) : ((l.prod : ℝ≥0) : ℝ) = (l.map coe).prod := to_real_hom.map_list_prod l @[norm_cast] lemma coe_multiset_sum (s : multiset ℝ≥0) : ((s.sum : ℝ≥0) : ℝ) = (s.map coe).sum := to_real_hom.map_multiset_sum s @[norm_cast] lemma coe_multiset_prod (s : multiset ℝ≥0) : ((s.prod : ℝ≥0) : ℝ) = (s.map coe).prod := to_real_hom.map_multiset_prod s @[norm_cast] lemma coe_sum {α} {s : finset α} {f : α → ℝ≥0} : ↑(∑ a in s, f a) = ∑ a in s, (f a : ℝ) := to_real_hom.map_sum _ _ lemma _root_.real.to_nnreal_sum_of_nonneg {α} {s : finset α} {f : α → ℝ} (hf : ∀ a, a ∈ s → 0 ≤ f a) : real.to_nnreal (∑ a in s, f a) = ∑ a in s, real.to_nnreal (f a) := begin rw [←nnreal.coe_eq, nnreal.coe_sum, real.coe_to_nnreal _ (finset.sum_nonneg hf)], exact finset.sum_congr rfl (λ x hxs, by rw real.coe_to_nnreal _ (hf x hxs)), end @[norm_cast] lemma coe_prod {α} {s : finset α} {f : α → ℝ≥0} : ↑(∏ a in s, f a) = ∏ a in s, (f a : ℝ) := to_real_hom.map_prod _ _ lemma _root_.real.to_nnreal_prod_of_nonneg {α} {s : finset α} {f : α → ℝ} (hf : ∀ a, a ∈ s → 0 ≤ f a) : real.to_nnreal (∏ a in s, f a) = ∏ a in s, real.to_nnreal (f a) := begin rw [←nnreal.coe_eq, nnreal.coe_prod, real.coe_to_nnreal _ (finset.prod_nonneg hf)], exact finset.prod_congr rfl (λ x hxs, by rw real.coe_to_nnreal _ (hf x hxs)), end lemma nsmul_coe (r : ℝ≥0) (n : ℕ) : ↑(n • r) = n • (r:ℝ) := by norm_cast @[simp, norm_cast] protected lemma coe_nat_cast (n : ℕ) : (↑(↑n : ℝ≥0) : ℝ) = n := map_nat_cast to_real_hom n noncomputable example : linear_order ℝ≥0 := by apply_instance @[simp, norm_cast] protected lemma coe_le_coe {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) ≤ r₂ ↔ r₁ ≤ r₂ := iff.rfl @[simp, norm_cast] protected lemma coe_lt_coe {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) < r₂ ↔ r₁ < r₂ := iff.rfl @[simp, norm_cast] protected lemma coe_pos {r : ℝ≥0} : (0 : ℝ) < r ↔ 0 < r := iff.rfl protected lemma coe_mono : monotone (coe : ℝ≥0 → ℝ) := λ _ _, nnreal.coe_le_coe.2 protected lemma _root_.real.to_nnreal_mono : monotone real.to_nnreal := λ x y h, max_le_max h (le_refl 0) @[simp] lemma _root_.real.to_nnreal_coe {r : ℝ≥0} : real.to_nnreal r = r := nnreal.eq $ max_eq_left r.2 @[simp] lemma mk_coe_nat (n : ℕ) : @eq ℝ≥0 (⟨(n : ℝ), n.cast_nonneg⟩ : ℝ≥0) n := nnreal.eq (nnreal.coe_nat_cast n).symm @[simp] lemma to_nnreal_coe_nat (n : ℕ) : real.to_nnreal n = n := nnreal.eq $ by simp [real.coe_to_nnreal] /-- `real.to_nnreal` and `coe : ℝ≥0 → ℝ` form a Galois insertion. -/ noncomputable def gi : galois_insertion real.to_nnreal coe := galois_insertion.monotone_intro nnreal.coe_mono real.to_nnreal_mono real.le_coe_to_nnreal (λ _, real.to_nnreal_coe) -- note that anything involving the (decidability of the) linear order, -- will be noncomputable, everything else should not be. example : order_bot ℝ≥0 := by apply_instance example : partial_order ℝ≥0 := by apply_instance noncomputable example : canonically_linear_ordered_add_monoid ℝ≥0 := by apply_instance noncomputable example : linear_ordered_add_comm_monoid ℝ≥0 := by apply_instance example : distrib_lattice ℝ≥0 := by apply_instance example : semilattice_inf ℝ≥0 := by apply_instance example : semilattice_sup ℝ≥0 := by apply_instance noncomputable example : linear_ordered_semiring ℝ≥0 := by apply_instance example : ordered_comm_semiring ℝ≥0 := by apply_instance noncomputable example : linear_ordered_comm_monoid ℝ≥0 := by apply_instance noncomputable example : linear_ordered_comm_monoid_with_zero ℝ≥0 := by apply_instance noncomputable example : linear_ordered_comm_group_with_zero ℝ≥0 := by apply_instance example : canonically_ordered_comm_semiring ℝ≥0 := by apply_instance example : densely_ordered ℝ≥0 := by apply_instance example : no_max_order ℝ≥0 := by apply_instance /-- If `a` is a nonnegative real number, then the closed interval `[0, a]` in `ℝ` is order isomorphic to the interval `set.Iic a`. -/ @[simps apply_coe_coe] def order_iso_Icc_zero_coe (a : ℝ≥0) : set.Icc (0 : ℝ) a ≃o set.Iic a := { to_equiv := equiv.set.sep (set.Ici 0) (λ x, x ≤ a), map_rel_iff' := λ x y, iff.rfl } @[simp] lemma order_iso_Icc_zero_coe_symm_apply_coe (a : ℝ≥0) (b : set.Iic a) : ((order_iso_Icc_zero_coe a).symm b : ℝ) = b := rfl -- note we need the `@` to make the `has_mem.mem` have a sensible type lemma coe_image {s : set ℝ≥0} : coe '' s = {x : ℝ | ∃ h : 0 ≤ x, @has_mem.mem (ℝ≥0) _ _ ⟨x, h⟩ s} := subtype.coe_image lemma bdd_above_coe {s : set ℝ≥0} : bdd_above ((coe : ℝ≥0 → ℝ) '' s) ↔ bdd_above s := iff.intro (assume ⟨b, hb⟩, ⟨real.to_nnreal b, assume ⟨y, hy⟩ hys, show y ≤ max b 0, from le_max_of_le_left $ hb $ set.mem_image_of_mem _ hys⟩) (assume ⟨b, hb⟩, ⟨b, assume y ⟨x, hx, eq⟩, eq ▸ hb hx⟩) lemma bdd_below_coe (s : set ℝ≥0) : bdd_below ((coe : ℝ≥0 → ℝ) '' s) := ⟨0, assume r ⟨q, _, eq⟩, eq ▸ q.2⟩ noncomputable instance : conditionally_complete_linear_order_bot ℝ≥0 := nonneg.conditionally_complete_linear_order_bot real.Sup_empty.le @[norm_cast] lemma coe_Sup (s : set ℝ≥0) : (↑(Sup s) : ℝ) = Sup ((coe : ℝ≥0 → ℝ) '' s) := eq.symm $ @subset_Sup_of_within ℝ (set.Ici 0) _ ⟨(0 : ℝ≥0)⟩ s $ real.Sup_nonneg _ $ λ y ⟨x, _, hy⟩, hy ▸ x.2 @[norm_cast] lemma coe_supr {ι : Sort*} (s : ι → ℝ≥0) : (↑(⨆ i, s i) : ℝ) = ⨆ i, (s i) := by rw [supr, supr, coe_Sup, set.range_comp] @[norm_cast] lemma coe_Inf (s : set ℝ≥0) : (↑(Inf s) : ℝ) = Inf ((coe : ℝ≥0 → ℝ) '' s) := eq.symm $ @subset_Inf_of_within ℝ (set.Ici 0) _ ⟨(0 : ℝ≥0)⟩ s $ real.Inf_nonneg _ $ λ y ⟨x, _, hy⟩, hy ▸ x.2 @[simp] lemma Inf_empty : Inf (∅ : set ℝ≥0) = 0 := by rw [← nnreal.coe_eq_zero, coe_Inf, set.image_empty, real.Inf_empty] @[norm_cast] lemma coe_infi {ι : Sort*} (s : ι → ℝ≥0) : (↑(⨅ i, s i) : ℝ) = ⨅ i, (s i) := by rw [infi, infi, coe_Inf, set.range_comp] lemma le_infi_add_infi {ι ι' : Sort*} [nonempty ι] [nonempty ι'] {f : ι → ℝ≥0} {g : ι' → ℝ≥0} {a : ℝ≥0} (h : ∀ i j, a ≤ f i + g j) : a ≤ (⨅ i, f i) + ⨅ j, g j := begin rw [← nnreal.coe_le_coe, nnreal.coe_add, coe_infi, coe_infi], exact le_cinfi_add_cinfi h end example : archimedean ℝ≥0 := by apply_instance -- TODO: why are these three instances necessary? why aren't they inferred? instance covariant_add : covariant_class ℝ≥0 ℝ≥0 (+) (≤) := ordered_add_comm_monoid.to_covariant_class_left ℝ≥0 instance contravariant_add : contravariant_class ℝ≥0 ℝ≥0 (+) (<) := ordered_cancel_add_comm_monoid.to_contravariant_class_left ℝ≥0 instance covariant_mul : covariant_class ℝ≥0 ℝ≥0 (*) (≤) := ordered_comm_monoid.to_covariant_class_left ℝ≥0 -- Why isn't `nnreal.contravariant_add` inferred? lemma le_of_forall_pos_le_add {a b : ℝ≥0} (h : ∀ε, 0 < ε → a ≤ b + ε) : a ≤ b := @le_of_forall_pos_le_add _ _ _ _ _ _ nnreal.contravariant_add _ _ h lemma lt_iff_exists_rat_btwn (a b : ℝ≥0) : a < b ↔ (∃q:ℚ, 0 ≤ q ∧ a < real.to_nnreal q ∧ real.to_nnreal q < b) := iff.intro (assume (h : (↑a:ℝ) < (↑b:ℝ)), let ⟨q, haq, hqb⟩ := exists_rat_btwn h in have 0 ≤ (q : ℝ), from le_trans a.2 $ le_of_lt haq, ⟨q, rat.cast_nonneg.1 this, by simp [real.coe_to_nnreal _ this, nnreal.coe_lt_coe.symm, haq, hqb]⟩) (assume ⟨q, _, haq, hqb⟩, lt_trans haq hqb) lemma bot_eq_zero : (⊥ : ℝ≥0) = 0 := rfl lemma mul_sup (a b c : ℝ≥0) : a * (b ⊔ c) = (a * b) ⊔ (a * c) := mul_max_of_nonneg _ _ $ zero_le a lemma sup_mul (a b c : ℝ≥0) : (a ⊔ b) * c = (a * c) ⊔ (b * c) := max_mul_of_nonneg _ _ $ zero_le c lemma mul_finset_sup {α} (r : ℝ≥0) (s : finset α) (f : α → ℝ≥0) : r * s.sup f = s.sup (λ a, r * f a) := (finset.comp_sup_eq_sup_comp _ (nnreal.mul_sup r) (mul_zero r)) lemma finset_sup_mul {α} (s : finset α) (f : α → ℝ≥0) (r : ℝ≥0) : s.sup f * r = s.sup (λ a, f a * r) := (finset.comp_sup_eq_sup_comp (* r) (λ x y, nnreal.sup_mul x y r) (zero_mul r)) lemma finset_sup_div {α} {f : α → ℝ≥0} {s : finset α} (r : ℝ≥0) : s.sup f / r = s.sup (λ a, f a / r) := by simp only [div_eq_inv_mul, mul_finset_sup] @[simp, norm_cast] lemma coe_max (x y : ℝ≥0) : ((max x y : ℝ≥0) : ℝ) = max (x : ℝ) (y : ℝ) := nnreal.coe_mono.map_max @[simp, norm_cast] lemma coe_min (x y : ℝ≥0) : ((min x y : ℝ≥0) : ℝ) = min (x : ℝ) (y : ℝ) := nnreal.coe_mono.map_min @[simp] lemma zero_le_coe {q : ℝ≥0} : 0 ≤ (q : ℝ) := q.2 end nnreal namespace real section to_nnreal @[simp] lemma to_nnreal_zero : real.to_nnreal 0 = 0 := by simp [real.to_nnreal]; refl @[simp] lemma to_nnreal_one : real.to_nnreal 1 = 1 := by simp [real.to_nnreal, max_eq_left (zero_le_one : (0 :ℝ) ≤ 1)]; refl @[simp] lemma to_nnreal_pos {r : ℝ} : 0 < real.to_nnreal r ↔ 0 < r := by simp [real.to_nnreal, nnreal.coe_lt_coe.symm, lt_irrefl] @[simp] lemma to_nnreal_eq_zero {r : ℝ} : real.to_nnreal r = 0 ↔ r ≤ 0 := by simpa [-to_nnreal_pos] using (not_iff_not.2 (@to_nnreal_pos r)) lemma to_nnreal_of_nonpos {r : ℝ} : r ≤ 0 → real.to_nnreal r = 0 := to_nnreal_eq_zero.2 @[simp] lemma coe_to_nnreal' (r : ℝ) : (real.to_nnreal r : ℝ) = max r 0 := rfl @[simp] lemma to_nnreal_le_to_nnreal_iff {r p : ℝ} (hp : 0 ≤ p) : real.to_nnreal r ≤ real.to_nnreal p ↔ r ≤ p := by simp [nnreal.coe_le_coe.symm, real.to_nnreal, hp] @[simp] lemma to_nnreal_eq_to_nnreal_iff {r p : ℝ} (hr : 0 ≤ r) (hp : 0 ≤ p) : real.to_nnreal r = real.to_nnreal p ↔ r = p := by simp [← nnreal.coe_eq, coe_to_nnreal, hr, hp] @[simp] lemma to_nnreal_lt_to_nnreal_iff' {r p : ℝ} : real.to_nnreal r < real.to_nnreal p ↔ r < p ∧ 0 < p := nnreal.coe_lt_coe.symm.trans max_lt_max_left_iff lemma to_nnreal_lt_to_nnreal_iff {r p : ℝ} (h : 0 < p) : real.to_nnreal r < real.to_nnreal p ↔ r < p := to_nnreal_lt_to_nnreal_iff'.trans (and_iff_left h) lemma to_nnreal_lt_to_nnreal_iff_of_nonneg {r p : ℝ} (hr : 0 ≤ r) : real.to_nnreal r < real.to_nnreal p ↔ r < p := to_nnreal_lt_to_nnreal_iff'.trans ⟨and.left, λ h, ⟨h, lt_of_le_of_lt hr h⟩⟩ @[simp] lemma to_nnreal_add {r p : ℝ} (hr : 0 ≤ r) (hp : 0 ≤ p) : real.to_nnreal (r + p) = real.to_nnreal r + real.to_nnreal p := nnreal.eq $ by simp [real.to_nnreal, hr, hp, add_nonneg] lemma to_nnreal_add_to_nnreal {r p : ℝ} (hr : 0 ≤ r) (hp : 0 ≤ p) : real.to_nnreal r + real.to_nnreal p = real.to_nnreal (r + p) := (real.to_nnreal_add hr hp).symm lemma to_nnreal_le_to_nnreal {r p : ℝ} (h : r ≤ p) : real.to_nnreal r ≤ real.to_nnreal p := real.to_nnreal_mono h lemma to_nnreal_add_le {r p : ℝ} : real.to_nnreal (r + p) ≤ real.to_nnreal r + real.to_nnreal p := nnreal.coe_le_coe.1 $ max_le (add_le_add (le_max_left _ _) (le_max_left _ _)) nnreal.zero_le_coe lemma to_nnreal_le_iff_le_coe {r : ℝ} {p : ℝ≥0} : real.to_nnreal r ≤ p ↔ r ≤ ↑p := nnreal.gi.gc r p lemma le_to_nnreal_iff_coe_le {r : ℝ≥0} {p : ℝ} (hp : 0 ≤ p) : r ≤ real.to_nnreal p ↔ ↑r ≤ p := by rw [← nnreal.coe_le_coe, real.coe_to_nnreal p hp] lemma le_to_nnreal_iff_coe_le' {r : ℝ≥0} {p : ℝ} (hr : 0 < r) : r ≤ real.to_nnreal p ↔ ↑r ≤ p := (le_or_lt 0 p).elim le_to_nnreal_iff_coe_le $ λ hp, by simp only [(hp.trans_le r.coe_nonneg).not_le, to_nnreal_eq_zero.2 hp.le, hr.not_le] lemma to_nnreal_lt_iff_lt_coe {r : ℝ} {p : ℝ≥0} (ha : 0 ≤ r) : real.to_nnreal r < p ↔ r < ↑p := by rw [← nnreal.coe_lt_coe, real.coe_to_nnreal r ha] lemma lt_to_nnreal_iff_coe_lt {r : ℝ≥0} {p : ℝ} : r < real.to_nnreal p ↔ ↑r < p := begin cases le_total 0 p, { rw [← nnreal.coe_lt_coe, real.coe_to_nnreal p h] }, { rw [to_nnreal_eq_zero.2 h], split, { intro, have := not_lt_of_le (zero_le r), contradiction }, { intro rp, have : ¬(p ≤ 0) := not_le_of_lt (lt_of_le_of_lt (nnreal.coe_nonneg _) rp), contradiction } } end @[simp] lemma to_nnreal_bit0 (r : ℝ) : real.to_nnreal (bit0 r) = bit0 (real.to_nnreal r) := begin cases le_total r 0 with hr hr, { rw [to_nnreal_of_nonpos hr, to_nnreal_of_nonpos, bit0_zero], exact add_nonpos hr hr }, { exact to_nnreal_add hr hr } end @[simp] lemma to_nnreal_bit1 {r : ℝ} (hr : 0 ≤ r) : real.to_nnreal (bit1 r) = bit1 (real.to_nnreal r) := (real.to_nnreal_add (by simp [hr]) zero_le_one).trans (by simp [bit1]) lemma to_nnreal_pow {x : ℝ} (hx : 0 ≤ x) (n : ℕ) : (x ^ n).to_nnreal = (x.to_nnreal) ^ n := by rw [← nnreal.coe_eq, nnreal.coe_pow, real.coe_to_nnreal _ (pow_nonneg hx _), real.coe_to_nnreal x hx] end to_nnreal end real open real namespace nnreal section mul lemma mul_eq_mul_left {a b c : ℝ≥0} (h : a ≠ 0) : (a * b = a * c ↔ b = c) := by rw [mul_eq_mul_left_iff, or_iff_left h] lemma _root_.real.to_nnreal_mul {p q : ℝ} (hp : 0 ≤ p) : real.to_nnreal (p * q) = real.to_nnreal p * real.to_nnreal q := begin cases le_total 0 q with hq hq, { apply nnreal.eq, simp [real.to_nnreal, hp, hq, max_eq_left, mul_nonneg] }, { have hpq := mul_nonpos_of_nonneg_of_nonpos hp hq, rw [to_nnreal_eq_zero.2 hq, to_nnreal_eq_zero.2 hpq, mul_zero] } end end mul section pow lemma pow_antitone_exp {a : ℝ≥0} (m n : ℕ) (mn : m ≤ n) (a1 : a ≤ 1) : a ^ n ≤ a ^ m := pow_le_pow_of_le_one (zero_le a) a1 mn lemma exists_pow_lt_of_lt_one {a b : ℝ≥0} (ha : 0 < a) (hb : b < 1) : ∃ n : ℕ, b ^ n < a := by simpa only [← coe_pow, nnreal.coe_lt_coe] using exists_pow_lt_of_lt_one (nnreal.coe_pos.2 ha) (nnreal.coe_lt_coe.2 hb) lemma exists_mem_Ico_zpow {x : ℝ≥0} {y : ℝ≥0} (hx : x ≠ 0) (hy : 1 < y) : ∃ n : ℤ, x ∈ set.Ico (y ^ n) (y ^ (n + 1)) := begin obtain ⟨n, hn, h'n⟩ : ∃ n : ℤ, (y : ℝ) ^ n ≤ x ∧ (x : ℝ) < y ^ (n + 1) := exists_mem_Ico_zpow (bot_lt_iff_ne_bot.mpr hx) hy, rw ← nnreal.coe_zpow at hn h'n, exact ⟨n, hn, h'n⟩, end lemma exists_mem_Ioc_zpow {x : ℝ≥0} {y : ℝ≥0} (hx : x ≠ 0) (hy : 1 < y) : ∃ n : ℤ, x ∈ set.Ioc (y ^ n) (y ^ (n + 1)) := begin obtain ⟨n, hn, h'n⟩ : ∃ n : ℤ, (y : ℝ) ^ n < x ∧ (x : ℝ) ≤ y ^ (n + 1) := exists_mem_Ioc_zpow (bot_lt_iff_ne_bot.mpr hx) hy, rw ← nnreal.coe_zpow at hn h'n, exact ⟨n, hn, h'n⟩, end end pow section sub /-! ### Lemmas about subtraction In this section we provide a few lemmas about subtraction that do not fit well into any other typeclass. For lemmas about subtraction and addition see lemmas about `has_ordered_sub` in the file `algebra.order.sub`. See also `mul_tsub` and `tsub_mul`. -/ lemma sub_def {r p : ℝ≥0} : r - p = real.to_nnreal (r - p) := rfl lemma coe_sub_def {r p : ℝ≥0} : ↑(r - p) = max (r - p : ℝ) 0 := rfl example : has_ordered_sub ℝ≥0 := by apply_instance lemma sub_div (a b c : ℝ≥0) : (a - b) / c = a / c - b / c := tsub_div _ _ _ end sub section inv lemma sum_div {ι} (s : finset ι) (f : ι → ℝ≥0) (b : ℝ≥0) : (∑ i in s, f i) / b = ∑ i in s, (f i / b) := finset.sum_div @[simp] lemma inv_le {r p : ℝ≥0} (h : r ≠ 0) : r⁻¹ ≤ p ↔ 1 ≤ r * p := by rw [← mul_le_mul_left (pos_iff_ne_zero.2 h), mul_inv_cancel h] lemma inv_le_of_le_mul {r p : ℝ≥0} (h : 1 ≤ r * p) : r⁻¹ ≤ p := by by_cases r = 0; simp [*, inv_le] @[simp] lemma le_inv_iff_mul_le {r p : ℝ≥0} (h : p ≠ 0) : (r ≤ p⁻¹ ↔ r * p ≤ 1) := by rw [← mul_le_mul_left (pos_iff_ne_zero.2 h), mul_inv_cancel h, mul_comm] @[simp] lemma lt_inv_iff_mul_lt {r p : ℝ≥0} (h : p ≠ 0) : (r < p⁻¹ ↔ r * p < 1) := by rw [← mul_lt_mul_left (pos_iff_ne_zero.2 h), mul_inv_cancel h, mul_comm] lemma mul_le_iff_le_inv {a b r : ℝ≥0} (hr : r ≠ 0) : r * a ≤ b ↔ a ≤ r⁻¹ * b := have 0 < r, from lt_of_le_of_ne (zero_le r) hr.symm, by rw [← mul_le_mul_left (inv_pos.mpr this), ← mul_assoc, inv_mul_cancel hr, one_mul] lemma le_div_iff_mul_le {a b r : ℝ≥0} (hr : r ≠ 0) : a ≤ b / r ↔ a * r ≤ b := le_div_iff₀ hr lemma div_le_iff {a b r : ℝ≥0} (hr : r ≠ 0) : a / r ≤ b ↔ a ≤ b * r := div_le_iff₀ hr lemma div_le_iff' {a b r : ℝ≥0} (hr : r ≠ 0) : a / r ≤ b ↔ a ≤ r * b := @div_le_iff' ℝ _ a r b $ pos_iff_ne_zero.2 hr lemma div_le_of_le_mul {a b c : ℝ≥0} (h : a ≤ b * c) : a / c ≤ b := if h0 : c = 0 then by simp [h0] else (div_le_iff h0).2 h lemma div_le_of_le_mul' {a b c : ℝ≥0} (h : a ≤ b * c) : a / b ≤ c := div_le_of_le_mul $ mul_comm b c ▸ h lemma le_div_iff {a b r : ℝ≥0} (hr : r ≠ 0) : a ≤ b / r ↔ a * r ≤ b := @le_div_iff ℝ _ a b r $ pos_iff_ne_zero.2 hr lemma le_div_iff' {a b r : ℝ≥0} (hr : r ≠ 0) : a ≤ b / r ↔ r * a ≤ b := @le_div_iff' ℝ _ a b r $ pos_iff_ne_zero.2 hr lemma div_lt_iff {a b r : ℝ≥0} (hr : r ≠ 0) : a / r < b ↔ a < b * r := lt_iff_lt_of_le_iff_le (le_div_iff hr) lemma div_lt_iff' {a b r : ℝ≥0} (hr : r ≠ 0) : a / r < b ↔ a < r * b := lt_iff_lt_of_le_iff_le (le_div_iff' hr) lemma lt_div_iff {a b r : ℝ≥0} (hr : r ≠ 0) : a < b / r ↔ a * r < b := lt_iff_lt_of_le_iff_le (div_le_iff hr) lemma lt_div_iff' {a b r : ℝ≥0} (hr : r ≠ 0) : a < b / r ↔ r * a < b := lt_iff_lt_of_le_iff_le (div_le_iff' hr) lemma mul_lt_of_lt_div {a b r : ℝ≥0} (h : a < b / r) : a * r < b := begin refine (lt_div_iff $ λ hr, false.elim _).1 h, subst r, simpa using h end lemma div_le_div_left_of_le {a b c : ℝ≥0} (b0 : 0 < b) (c0 : 0 < c) (cb : c ≤ b) : a / b ≤ a / c := begin by_cases a0 : a = 0, { rw [a0, zero_div, zero_div] }, { cases a with a ha, replace a0 : 0 < a := lt_of_le_of_ne ha (ne_of_lt (zero_lt_iff.mpr a0)), exact (div_le_div_left a0 b0 c0).mpr cb } end lemma div_le_div_left {a b c : ℝ≥0} (a0 : 0 < a) (b0 : 0 < b) (c0 : 0 < c) : a / b ≤ a / c ↔ c ≤ b := div_le_div_left a0 b0 c0 lemma le_of_forall_lt_one_mul_le {x y : ℝ≥0} (h : ∀a<1, a * x ≤ y) : x ≤ y := le_of_forall_ge_of_dense $ assume a ha, have hx : x ≠ 0 := pos_iff_ne_zero.1 (lt_of_le_of_lt (zero_le _) ha), have hx' : x⁻¹ ≠ 0, by rwa [(≠), inv_eq_zero], have a * x⁻¹ < 1, by rwa [← lt_inv_iff_mul_lt hx', inv_inv], have (a * x⁻¹) * x ≤ y, from h _ this, by rwa [mul_assoc, inv_mul_cancel hx, mul_one] at this lemma half_le_self (a : ℝ≥0) : a / 2 ≤ a := half_le_self bot_le lemma half_lt_self {a : ℝ≥0} (h : a ≠ 0) : a / 2 < a := half_lt_self h.bot_lt lemma div_lt_one_of_lt {a b : ℝ≥0} (h : a < b) : a / b < 1 := begin rwa [div_lt_iff, one_mul], exact ne_of_gt (lt_of_le_of_lt (zero_le _) h) end lemma _root_.real.to_nnreal_inv {x : ℝ} : real.to_nnreal x⁻¹ = (real.to_nnreal x)⁻¹ := begin by_cases hx : 0 ≤ x, { nth_rewrite 0 ← real.coe_to_nnreal x hx, rw [←nnreal.coe_inv, real.to_nnreal_coe], }, { have hx' := le_of_not_ge hx, rw [to_nnreal_eq_zero.mpr hx', inv_zero, to_nnreal_eq_zero.mpr (inv_nonpos.mpr hx')], }, end lemma _root_.real.to_nnreal_div {x y : ℝ} (hx : 0 ≤ x) : real.to_nnreal (x / y) = real.to_nnreal x / real.to_nnreal y := by rw [div_eq_mul_inv, div_eq_mul_inv, ← real.to_nnreal_inv, ← real.to_nnreal_mul hx] lemma _root_.real.to_nnreal_div' {x y : ℝ} (hy : 0 ≤ y) : real.to_nnreal (x / y) = real.to_nnreal x / real.to_nnreal y := by rw [div_eq_inv_mul, div_eq_inv_mul, real.to_nnreal_mul (inv_nonneg.2 hy), real.to_nnreal_inv] lemma inv_lt_one_iff {x : ℝ≥0} (hx : x ≠ 0) : x⁻¹ < 1 ↔ 1 < x := by rwa [← one_div, div_lt_iff hx, one_mul] lemma zpow_pos {x : ℝ≥0} (hx : x ≠ 0) (n : ℤ) : 0 < x ^ n := begin cases n, { simp [pow_pos hx.bot_lt _] }, { simp [pow_pos hx.bot_lt _] } end lemma inv_lt_inv {x y : ℝ≥0} (hx : x ≠ 0) (h : x < y) : y⁻¹ < x⁻¹ := inv_lt_inv_of_lt hx.bot_lt h end inv @[simp] lemma abs_eq (x : ℝ≥0) : |(x : ℝ)| = x := abs_of_nonneg x.property section csupr open set variables {ι : Sort*} {f : ι → ℝ≥0} lemma le_to_nnreal_of_coe_le {x : ℝ≥0} {y : ℝ} (h : ↑x ≤ y) : x ≤ y.to_nnreal := (le_to_nnreal_iff_coe_le $ x.2.trans h).2 h lemma Sup_of_not_bdd_above {s : set ℝ≥0} (hs : ¬bdd_above s) : has_Sup.Sup s = 0 := begin rw [← bdd_above_coe] at hs, rw [← nnreal.coe_eq, coe_Sup], exact Sup_of_not_bdd_above hs, end lemma supr_of_not_bdd_above (hf : ¬ bdd_above (range f)) : (⨆ i, f i) = 0 := Sup_of_not_bdd_above hf lemma infi_empty [is_empty ι] (f : ι → ℝ≥0) : (⨅ i, f i) = 0 := by { rw [← nnreal.coe_eq, coe_infi], exact real.cinfi_empty _, } @[simp] lemma infi_const_zero {α : Sort*} : (⨅ i : α, (0 : ℝ≥0)) = 0 := by { rw [← nnreal.coe_eq, coe_infi], exact real.cinfi_const_zero, } lemma infi_mul (f : ι → ℝ≥0) (a : ℝ≥0) : infi f * a = ⨅ i, f i * a := begin rw [← nnreal.coe_eq, nnreal.coe_mul, coe_infi, coe_infi], exact real.infi_mul_of_nonneg (nnreal.coe_nonneg _) _, end lemma mul_infi (f : ι → ℝ≥0) (a : ℝ≥0) : a * infi f = ⨅ i, a * f i := by simpa only [mul_comm] using infi_mul f a lemma mul_supr (f : ι → ℝ≥0) (a : ℝ≥0) : a * (⨆ i, f i) = ⨆ i, a * f i := begin rw [← nnreal.coe_eq, nnreal.coe_mul, nnreal.coe_supr, nnreal.coe_supr], exact real.mul_supr_of_nonneg (nnreal.coe_nonneg _) _, end lemma supr_mul (f : ι → ℝ≥0) (a : ℝ≥0) : (⨆ i, f i) * a = ⨆ i, f i * a := by { rw [mul_comm, mul_supr], simp_rw [mul_comm] } lemma supr_div (f : ι → ℝ≥0) (a : ℝ≥0) : (⨆ i, f i) / a = ⨆ i, f i / a := by simp only [div_eq_mul_inv, supr_mul] variable [nonempty ι] lemma le_mul_infi {a : ℝ≥0} {g : ℝ≥0} {h : ι → ℝ≥0} (H : ∀ j, a ≤ g * h j) : a ≤ g * infi h := by { rw [mul_infi], exact le_cinfi H } lemma mul_supr_le {a : ℝ≥0} {g : ℝ≥0} {h : ι → ℝ≥0} (H : ∀ j, g * h j ≤ a) : g * supr h ≤ a := by { rw [mul_supr], exact csupr_le H } lemma le_infi_mul {a : ℝ≥0} {g : ι → ℝ≥0} {h : ℝ≥0} (H : ∀ i, a ≤ g i * h) : a ≤ infi g * h := by { rw infi_mul, exact le_cinfi H } lemma supr_mul_le {a : ℝ≥0} {g : ι → ℝ≥0} {h : ℝ≥0} (H : ∀ i, g i * h ≤ a) : supr g * h ≤ a := by { rw supr_mul, exact csupr_le H } lemma le_infi_mul_infi {a : ℝ≥0} {g h : ι → ℝ≥0} (H : ∀ i j, a ≤ g i * h j) : a ≤ infi g * infi h := le_infi_mul $ λ i, le_mul_infi $ H i lemma supr_mul_supr_le {a : ℝ≥0} {g h : ι → ℝ≥0} (H : ∀ i j, g i * h j ≤ a) : supr g * supr h ≤ a := supr_mul_le $ λ i, mul_supr_le $ H _ end csupr end nnreal namespace set namespace ord_connected variables {s : set ℝ} {t : set ℝ≥0} lemma preimage_coe_nnreal_real (h : s.ord_connected) : (coe ⁻¹' s : set ℝ≥0).ord_connected := h.preimage_mono nnreal.coe_mono lemma image_coe_nnreal_real (h : t.ord_connected) : (coe '' t : set ℝ).ord_connected := ⟨ball_image_iff.2 $ λ x hx, ball_image_iff.2 $ λ y hy z hz, ⟨⟨z, x.2.trans hz.1⟩, h.out hx hy hz, rfl⟩⟩ lemma image_real_to_nnreal (h : s.ord_connected) : (real.to_nnreal '' s).ord_connected := begin refine ⟨ball_image_iff.2 $ λ x hx, ball_image_iff.2 $ λ y hy z hz, _⟩, cases le_total y 0 with hy₀ hy₀, { rw [mem_Icc, real.to_nnreal_of_nonpos hy₀, nonpos_iff_eq_zero] at hz, exact ⟨y, hy, (to_nnreal_of_nonpos hy₀).trans hz.2.symm⟩ }, { lift y to ℝ≥0 using hy₀, rw [to_nnreal_coe] at hz, exact ⟨z, h.out hx hy ⟨to_nnreal_le_iff_le_coe.1 hz.1, hz.2⟩, to_nnreal_coe⟩ } end lemma preimage_real_to_nnreal (h : t.ord_connected) : (real.to_nnreal ⁻¹' t).ord_connected := h.preimage_mono real.to_nnreal_mono end ord_connected end set namespace real /-- The absolute value on `ℝ` as a map to `ℝ≥0`. -/ @[pp_nodot] def nnabs : ℝ →*₀ ℝ≥0 := { to_fun := λ x, ⟨|x|, abs_nonneg x⟩, map_zero' := by { ext, simp }, map_one' := by { ext, simp }, map_mul' := λ x y, by { ext, simp [abs_mul] } } @[norm_cast, simp] lemma coe_nnabs (x : ℝ) : (nnabs x : ℝ) = |x| := rfl @[simp] lemma nnabs_of_nonneg {x : ℝ} (h : 0 ≤ x) : nnabs x = to_nnreal x := by { ext, simp [coe_to_nnreal x h, abs_of_nonneg h] } lemma nnabs_coe (x : ℝ≥0) : nnabs x = x := by simp lemma coe_to_nnreal_le (x : ℝ) : (to_nnreal x : ℝ) ≤ |x| := max_le (le_abs_self _) (abs_nonneg _) lemma cast_nat_abs_eq_nnabs_cast (n : ℤ) : (n.nat_abs : ℝ≥0) = nnabs n := by { ext, rw [nnreal.coe_nat_cast, int.cast_nat_abs, real.coe_nnabs] } end real namespace tactic open positivity private lemma nnreal_coe_pos {r : ℝ≥0} : 0 < r → 0 < (r : ℝ) := nnreal.coe_pos.2 /-- Extension for the `positivity` tactic: cast from `ℝ≥0` to `ℝ`. -/ @[positivity] meta def positivity_coe_nnreal_real : expr → tactic strictness | `(@coe _ _ %%inst %%a) := do unify inst `(@coe_to_lift _ _ $ @coe_base _ _ nnreal.real.has_coe), strictness_a ← core a, match strictness_a with | positive p := positive <$> mk_app ``nnreal_coe_pos [p] | _ := nonnegative <$> mk_app ``nnreal.coe_nonneg [a] end | e := pp e >>= fail ∘ format.bracket "The expression " " is not of the form `(r : ℝ)` for `r : ℝ≥0`" end tactic
cb807a466e1d61a84752b437f9945b4018a349af
e61a235b8468b03aee0120bf26ec615c045005d2
/src/Init/Lean/Meta/FunInfo.lean
4dfb02e78dcf3cb67672e2d0a6db99a2ce883d71
[ "Apache-2.0" ]
permissive
SCKelemen/lean4
140dc63a80539f7c61c8e43e1c174d8500ec3230
e10507e6615ddbef73d67b0b6c7f1e4cecdd82bc
refs/heads/master
1,660,973,595,917
1,590,278,033,000
1,590,278,033,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,213
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 -/ prelude import Init.Lean.Meta.Basic import Init.Lean.Meta.InferType namespace Lean namespace Meta @[inline] private def checkFunInfoCache (fn : Expr) (maxArgs? : Option Nat) (k : MetaM FunInfo) : MetaM FunInfo := do s ← get; t ← getTransparency; match s.cache.funInfo.find? ⟨t, fn, maxArgs?⟩ with | some finfo => pure finfo | none => do finfo ← k; modify $ fun s => { s with cache := { s.cache with funInfo := s.cache.funInfo.insert ⟨t, fn, maxArgs?⟩ finfo } }; pure finfo @[inline] private def whenHasVar {α} (e : Expr) (deps : α) (k : α → α) : α := if e.hasFVar then k deps else deps private def collectDepsAux (fvars : Array Expr) : Expr → Array Nat → Array Nat | e@(Expr.app f a _), deps => whenHasVar e deps (collectDepsAux a ∘ collectDepsAux f) | e@(Expr.forallE _ d b _), deps => whenHasVar e deps (collectDepsAux b ∘ collectDepsAux d) | e@(Expr.lam _ d b _), deps => whenHasVar e deps (collectDepsAux b ∘ collectDepsAux d) | e@(Expr.letE _ t v b _), deps => whenHasVar e deps (collectDepsAux b ∘ collectDepsAux v ∘ collectDepsAux t) | Expr.proj _ _ e _, deps => collectDepsAux e deps | Expr.mdata _ e _, deps => collectDepsAux e deps | e@(Expr.fvar _ _), deps => match fvars.indexOf e with | none => deps | some i => if deps.contains i.val then deps else deps.push i.val | _, deps => deps private def collectDeps (fvars : Array Expr) (e : Expr) : Array Nat := let deps := collectDepsAux fvars e #[]; deps.qsort (fun i j => i < j) /-- Update `hasFwdDeps` fields using new `backDeps` -/ private def updateHasFwdDeps (pinfo : Array ParamInfo) (backDeps : Array Nat) : Array ParamInfo := if backDeps.size == 0 then pinfo else -- update hasFwdDeps fields pinfo.mapIdx $ fun i info => if info.hasFwdDeps then info else if backDeps.contains i then { info with hasFwdDeps := true } else info private def getFunInfoAux (fn : Expr) (maxArgs? : Option Nat) : MetaM FunInfo := checkFunInfoCache fn maxArgs? $ do fnType ← inferType fn; withTransparency TransparencyMode.default $ forallBoundedTelescope fnType maxArgs? $ fun fvars type => do pinfo ← fvars.size.foldM (fun (i : Nat) (pinfo : Array ParamInfo) => do let fvar := fvars.get! i; decl ← getFVarLocalDecl fvar; let backDeps := collectDeps fvars decl.type; let pinfo := updateHasFwdDeps pinfo backDeps; pure $ pinfo.push { backDeps := backDeps, implicit := decl.binderInfo == BinderInfo.implicit, instImplicit := decl.binderInfo == BinderInfo.instImplicit }) #[]; let resultDeps := collectDeps fvars type; let pinfo := updateHasFwdDeps pinfo resultDeps; pure { resultDeps := resultDeps, paramInfo := pinfo } def getFunInfo (fn : Expr) : MetaM FunInfo := getFunInfoAux fn none def getFunInfoNArgs (fn : Expr) (nargs : Nat) : MetaM FunInfo := getFunInfoAux fn (some nargs) end Meta end Lean