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
c6a690653a02ae13938d39cfe2f5b42ae56dcbc0
ec62863c729b7eedee77b86d974f2c529fa79d25
/10/b.lean
c698433de3ffb29ce52750bf92b20c900a9e71a4
[]
no_license
rwbarton/advent-of-lean-4
2ac9b17ba708f66051e3d8cd694b0249bc433b65
417c7e2718253ba7148c0279fcb251b6fc291477
refs/heads/main
1,675,917,092,057
1,609,864,581,000
1,609,864,581,000
317,700,289
24
0
null
null
null
null
UTF-8
Lean
false
false
546
lean
def solve (vals : Array Int) : Int := do let mut counts : Array Int := Array.mkArray vals.size 0 for i in [0:vals.size] do if vals.get! i ≤ 3 then counts := counts.set! i (counts.get! i + 1) for j in [0:i] do if vals.get! i - vals.get! j ≤ 3 then counts := counts.set! i (counts.get! i + counts.get! j) counts.get! (vals.size - 1) def main : IO Unit := do let input ← IO.FS.lines "a.in" let mut vals := (input.map String.toInt!).qsort (· < ·) vals := vals.push (vals.get! (vals.size - 1) + 3) IO.print s!"{solve vals}\n"
9c486bb9c6f66e04988715ae711b5fb0deb11926
42610cc2e5db9c90269470365e6056df0122eaa0
/library/data/real/division.lean
5d1acfb7ceb33b6c69c1562ec191e797b902ce39
[ "Apache-2.0" ]
permissive
tomsib2001/lean
2ab59bfaebd24a62109f800dcf4a7139ebd73858
eb639a7d53fb40175bea5c8da86b51d14bb91f76
refs/heads/master
1,586,128,387,740
1,468,968,950,000
1,468,968,950,000
61,027,234
0
0
null
1,465,813,585,000
1,465,813,585,000
null
UTF-8
Lean
false
false
23,978
lean
/- Copyright (c) 2015 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Robert Y. Lewis The real numbers, constructed as equivalence classes of Cauchy sequences of rationals. This construction follows Bishop and Bridges (1985). At this point, we no longer proceed constructively: this file makes heavy use of decidability and excluded middle. -/ import data.real.basic data.real.order data.rat data.nat open rat open nat open eq.ops pnat classical namespace rat_seq local postfix ⁻¹ := pnat.inv ----------------------------- -- Facts about absolute values of sequences, to define inverse definition s_abs (s : seq) : seq := λ n, abs (s n) theorem abs_reg_of_reg {s : seq} (Hs : regular s) : regular (s_abs s) := begin intros, apply le.trans, apply abs_abs_sub_abs_le_abs_sub, apply Hs end theorem abs_pos_of_nonzero {s : seq} (Hs : regular s) (Hnz : sep s zero) : ∃ N : ℕ+, ∀ m : ℕ+, m ≥ N → abs (s m) ≥ N⁻¹ := begin rewrite [↑sep at Hnz, ↑s_lt at Hnz], apply or.elim Hnz, intro Hnz1, have H' : pos (sneg s), begin apply pos_of_pos_equiv, rotate 2, apply Hnz1, rotate 1, apply s_zero_add, repeat (assumption | apply reg_add_reg | apply reg_neg_reg | apply zero_is_reg) end, cases bdd_away_of_pos (reg_neg_reg Hs) H' with [N, HN], existsi N, intro m Hm, apply le.trans, apply HN m Hm, rewrite ↑sneg, apply neg_le_abs_self, intro Hnz2, let H' := pos_of_pos_equiv (reg_add_reg Hs (reg_neg_reg zero_is_reg)) (s_add_zero s Hs) Hnz2, let H'' := bdd_away_of_pos Hs H', cases H'' with [N, HN], existsi N, intro m Hm, apply le.trans, apply HN m Hm, apply le_abs_self end theorem abs_well_defined {s t : seq} (Hs : regular s) (Ht : regular t) (Heq : s ≡ t) : s_abs s ≡ s_abs t := begin rewrite [↑equiv at *], intro n, rewrite ↑s_abs, apply le.trans, apply abs_abs_sub_abs_le_abs_sub, apply Heq end theorem sep_zero_of_pos {s : seq} (Hs : regular s) (Hpos : pos s) : sep s zero := begin apply or.inr, apply pos_of_pos_equiv, rotate 2, apply Hpos, apply Hs, apply equiv.symm, apply s_sub_zero Hs end ------------------------ -- This section could be cleaned up. private noncomputable definition pb {s : seq} (Hs : regular s) (Hpos : pos s) := some (abs_pos_of_nonzero Hs (sep_zero_of_pos Hs Hpos)) private noncomputable definition ps {s : seq} (Hs : regular s) (Hsep : sep s zero) := some (abs_pos_of_nonzero Hs Hsep) private theorem pb_spec {s : seq} (Hs : regular s) (Hpos : pos s) : ∀ m : ℕ+, m ≥ (pb Hs Hpos) → abs (s m) ≥ (pb Hs Hpos)⁻¹ := some_spec (abs_pos_of_nonzero Hs (sep_zero_of_pos Hs Hpos)) private theorem ps_spec {s : seq} (Hs : regular s) (Hsep : sep s zero) : ∀ m : ℕ+, m ≥ (ps Hs Hsep) → abs (s m) ≥ (ps Hs Hsep)⁻¹ := some_spec (abs_pos_of_nonzero Hs Hsep) noncomputable definition s_inv {s : seq} (Hs : regular s) (n : ℕ+) : ℚ := if H : sep s zero then (if n < (ps Hs H) then 1 / (s ((ps Hs H) * (ps Hs H) * (ps Hs H))) else 1 / (s ((ps Hs H) * (ps Hs H) * n))) else 0 private theorem peq {s : seq} (Hsep : sep s zero) (Hpos : pos s) (Hs : regular s) : pb Hs Hpos = ps Hs Hsep := rfl private theorem s_inv_of_sep_lt_p {s : seq} (Hs : regular s) (Hsep : sep s zero) {n : ℕ+} (Hn : n < (ps Hs Hsep)) : s_inv Hs n = 1 / s ((ps Hs Hsep) * (ps Hs Hsep) * (ps Hs Hsep)) := begin apply eq.trans, apply dif_pos Hsep, apply dif_pos Hn end private theorem s_inv_of_sep_gt_p {s : seq} (Hs : regular s) (Hsep : sep s zero) {n : ℕ+} (Hn : n ≥ (ps Hs Hsep)) : s_inv Hs n = 1 / s ((ps Hs Hsep) * (ps Hs Hsep) * n) := begin apply eq.trans, apply dif_pos Hsep, apply dif_neg (pnat.not_lt_of_ge Hn) end private theorem s_inv_of_pos_lt_p {s : seq} (Hs : regular s) (Hpos : pos s) {n : ℕ+} (Hn : n < (pb Hs Hpos)) : s_inv Hs n = 1 / s ((pb Hs Hpos) * (pb Hs Hpos) * (pb Hs Hpos)) := s_inv_of_sep_lt_p Hs (sep_zero_of_pos Hs Hpos) Hn private theorem s_inv_of_pos_gt_p {s : seq} (Hs : regular s) (Hpos : pos s) {n : ℕ+} (Hn : n ≥ (pb Hs Hpos)) : s_inv Hs n = 1 / s ((pb Hs Hpos) * (pb Hs Hpos) * n) := s_inv_of_sep_gt_p Hs (sep_zero_of_pos Hs Hpos) Hn private theorem le_ps {s : seq} (Hs : regular s) (Hsep : sep s zero) (n : ℕ+) : abs (s_inv Hs n) ≤ (rat_of_pnat (ps Hs Hsep)) := if Hn : n < ps Hs Hsep then (begin rewrite [(s_inv_of_sep_lt_p Hs Hsep Hn), abs_one_div], apply div_le_pnat, apply ps_spec, apply pnat.mul_le_mul_left end) else (begin rewrite [(s_inv_of_sep_gt_p Hs Hsep (pnat.le_of_not_gt Hn)), abs_one_div], apply div_le_pnat, apply ps_spec, rewrite pnat.mul_assoc, apply pnat.mul_le_mul_right end) theorem s_inv_zero : s_inv zero_is_reg = zero := funext (λ n, dif_neg (!not_sep_self)) private theorem s_inv_of_zero' {s : seq} (Hs : regular s) (Hz : ¬ sep s zero) (n : ℕ+) : s_inv Hs n = 0 := dif_neg Hz theorem s_inv_of_zero {s : seq} (Hs : regular s) (Hz : ¬ sep s zero) : s_inv Hs = zero := begin apply funext, intro n, apply s_inv_of_zero' Hs Hz n end private theorem s_ne_zero_of_ge_p {s : seq} (Hs : regular s) (Hsep : sep s zero) {n : ℕ+} (Hn : n ≥ (ps Hs Hsep)) : s n ≠ 0 := begin let Hps := ps_spec Hs Hsep, apply ne_zero_of_abs_ne_zero, apply ne_of_gt, apply gt_of_ge_of_gt, apply Hps, apply Hn, apply pnat.inv_pos end theorem reg_inv_reg {s : seq} (Hs : regular s) (Hsep : sep s zero) : regular (s_inv Hs) := begin rewrite ↑regular, intros, have Hsp : s ((ps Hs Hsep) * (ps Hs Hsep) * (ps Hs Hsep)) ≠ 0, from s_ne_zero_of_ge_p Hs Hsep !pnat.mul_le_mul_left, have Hspn : s ((ps Hs Hsep) * (ps Hs Hsep) * n) ≠ 0, from s_ne_zero_of_ge_p Hs Hsep (show (ps Hs Hsep) * (ps Hs Hsep) * n ≥ ps Hs Hsep, by rewrite pnat.mul_assoc; apply pnat.mul_le_mul_right), have Hspm : s ((ps Hs Hsep) * (ps Hs Hsep) * m) ≠ 0, from s_ne_zero_of_ge_p Hs Hsep (show (ps Hs Hsep) * (ps Hs Hsep) * m ≥ ps Hs Hsep, by rewrite pnat.mul_assoc; apply pnat.mul_le_mul_right), cases em (m < ps Hs Hsep) with [Hmlt, Hmlt], cases em (n < ps Hs Hsep) with [Hnlt, Hnlt], rewrite [(s_inv_of_sep_lt_p Hs Hsep Hmlt), (s_inv_of_sep_lt_p Hs Hsep Hnlt)], rewrite [sub_self, abs_zero], apply add_invs_nonneg, rewrite [(s_inv_of_sep_lt_p Hs Hsep Hmlt), (s_inv_of_sep_gt_p Hs Hsep (pnat.le_of_not_gt Hnlt))], rewrite [(!div_sub_div Hsp Hspn), div_eq_mul_one_div, *abs_mul, *mul_one, *one_mul], apply le.trans, apply mul_le_mul, apply Hs, rewrite [-(mul_one 1), -(!field.div_mul_div Hsp Hspn), abs_mul], apply mul_le_mul, rewrite -(s_inv_of_sep_lt_p Hs Hsep Hmlt), apply le_ps Hs Hsep, rewrite -(s_inv_of_sep_gt_p Hs Hsep (pnat.le_of_not_gt Hnlt)), apply le_ps Hs Hsep, apply abs_nonneg, apply le_of_lt !rat_of_pnat_is_pos, apply abs_nonneg, apply add_invs_nonneg, rewrite [right_distrib, *pnat_cancel', add.comm], apply add_le_add_right, apply inv_ge_of_le, apply pnat.le_of_lt, apply Hmlt, cases em (n < ps Hs Hsep) with [Hnlt, Hnlt], rewrite [(s_inv_of_sep_lt_p Hs Hsep Hnlt), (s_inv_of_sep_gt_p Hs Hsep (pnat.le_of_not_gt Hmlt))], rewrite [(!div_sub_div Hspm Hsp), div_eq_mul_one_div, *abs_mul, *mul_one, *one_mul], apply le.trans, apply mul_le_mul, apply Hs, rewrite [-(mul_one 1), -(!field.div_mul_div Hspm Hsp), abs_mul], apply mul_le_mul, rewrite -(s_inv_of_sep_gt_p Hs Hsep (pnat.le_of_not_gt Hmlt)), apply le_ps Hs Hsep, rewrite -(s_inv_of_sep_lt_p Hs Hsep Hnlt), apply le_ps Hs Hsep, apply abs_nonneg, apply le_of_lt !rat_of_pnat_is_pos, apply abs_nonneg, apply add_invs_nonneg, rewrite [right_distrib, *pnat_cancel', add.comm], apply rat.add_le_add_left, apply inv_ge_of_le, apply pnat.le_of_lt, apply Hnlt, rewrite [(s_inv_of_sep_gt_p Hs Hsep (pnat.le_of_not_gt Hnlt)), (s_inv_of_sep_gt_p Hs Hsep (pnat.le_of_not_gt Hmlt))], rewrite [(!div_sub_div Hspm Hspn), div_eq_mul_one_div, abs_mul, *one_mul, *mul_one], apply le.trans, apply mul_le_mul, apply Hs, rewrite [-(mul_one 1), -(!field.div_mul_div Hspm Hspn), abs_mul], apply mul_le_mul, rewrite -(s_inv_of_sep_gt_p Hs Hsep (pnat.le_of_not_gt Hmlt)), apply le_ps Hs Hsep, rewrite -(s_inv_of_sep_gt_p Hs Hsep (pnat.le_of_not_gt Hnlt)), apply le_ps Hs Hsep, apply abs_nonneg, apply le_of_lt !rat_of_pnat_is_pos, apply abs_nonneg, apply add_invs_nonneg, rewrite [right_distrib, *pnat_cancel', add.comm], apply le.refl end theorem s_inv_ne_zero {s : seq} (Hs : regular s) (Hsep : sep s zero) (n : ℕ+) : s_inv Hs n ≠ 0 := if H : n ≥ ps Hs Hsep then (begin rewrite (s_inv_of_sep_gt_p Hs Hsep H), apply one_div_ne_zero, apply s_ne_zero_of_ge_p, apply pnat.le_trans, apply H, apply pnat.mul_le_mul_left end) else (begin rewrite (s_inv_of_sep_lt_p Hs Hsep (pnat.lt_of_not_le H)), apply one_div_ne_zero, apply s_ne_zero_of_ge_p, apply pnat.mul_le_mul_left end) protected theorem mul_inv {s : seq} (Hs : regular s) (Hsep : sep s zero) : smul s (s_inv Hs) ≡ one := begin let Rsi := reg_inv_reg Hs Hsep, let Rssi := reg_mul_reg Hs Rsi, apply eq_of_bdd Rssi one_is_reg, intros, existsi max (ps Hs Hsep) j, intro n Hn, have Hnz : s_inv Hs ((K₂ s (s_inv Hs)) * 2 * n) ≠ 0, from s_inv_ne_zero Hs Hsep _, rewrite [↑smul, ↑one, mul.comm, -(mul_one_div_cancel Hnz), -mul_sub_left_distrib, abs_mul], apply le.trans, apply mul_le_mul_of_nonneg_right, apply canon_2_bound_right s, apply Rsi, apply abs_nonneg, have Hp : (K₂ s (s_inv Hs)) * 2 * n ≥ ps Hs Hsep, begin apply pnat.le_trans, apply pnat.max_left, rotate 1, apply pnat.le_trans, apply Hn, apply pnat.mul_le_mul_left end, have Hnz' : s (((ps Hs Hsep) * (ps Hs Hsep)) * ((K₂ s (s_inv Hs)) * 2 * n)) ≠ 0, from s_ne_zero_of_ge_p Hs Hsep (show ps Hs Hsep ≤ ((ps Hs Hsep) * (ps Hs Hsep)) * ((K₂ s (s_inv Hs)) * 2 * n), by rewrite *pnat.mul_assoc; apply pnat.mul_le_mul_right), rewrite [(s_inv_of_sep_gt_p Hs Hsep Hp), (division_ring.one_div_one_div Hnz')], apply rat.le_trans, apply mul_le_mul_of_nonneg_left, apply Hs, apply le_of_lt, apply rat_of_pnat_is_pos, rewrite [left_distrib, pnat.mul_comm ((ps Hs Hsep) * (ps Hs Hsep)), *pnat.mul_assoc, *(@pnat.inv_mul_eq_mul_inv (K₂ s (s_inv Hs))), -*mul.assoc, *pnat.inv_cancel_left, *one_mul, -(pnat.add_halves j)], apply add_le_add, apply inv_ge_of_le, apply pnat_mul_le_mul_left', apply pnat.le_trans, rotate 1, apply Hn, rotate_right 1, apply pnat.max_right, apply inv_ge_of_le, apply pnat_mul_le_mul_left', apply pnat.le_trans, apply pnat.max_right, rotate 1, apply pnat.le_trans, apply Hn, apply pnat.mul_le_mul_right end protected theorem inv_mul {s : seq} (Hs : regular s) (Hsep : sep s zero) : smul (s_inv Hs) s ≡ one := begin apply equiv.trans, rotate 3, apply s_mul_comm, apply rat_seq.mul_inv, repeat (assumption | apply reg_mul_reg | apply reg_inv_reg | apply zero_is_reg) end theorem sep_of_equiv_sep {s t : seq} (Hs : regular s) (Ht : regular t) (Heq : s ≡ t) (Hsep : sep s zero) : sep t zero := begin apply or.elim Hsep, intro Hslt, apply or.inl, rewrite ↑s_lt at *, apply pos_of_pos_equiv, rotate 2, apply Hslt, rotate_right 1, apply add_well_defined, rotate 4, apply equiv.refl, apply neg_well_defined, apply Heq, intro Hslt, apply or.inr, rewrite ↑s_lt at *, apply pos_of_pos_equiv, rotate 2, apply Hslt, rotate_right 1, apply add_well_defined, rotate 5, apply equiv.refl, repeat (assumption | apply reg_neg_reg | apply reg_add_reg | apply zero_is_reg) end theorem inv_unique {s t : seq} (Hs : regular s) (Ht : regular t) (Hsep : sep s zero) (Heq : smul s t ≡ one) : s_inv Hs ≡ t := begin apply equiv.trans, rotate 3, apply equiv.symm, apply s_mul_one, rotate 1, apply equiv.trans, rotate 3, apply mul_well_defined, rotate 4, apply equiv.refl, apply equiv.symm, apply Heq, apply equiv.trans, rotate 3, apply equiv.symm, apply s_mul_assoc, rotate 3, apply equiv.trans, rotate 3, apply mul_well_defined, rotate 4, apply rat_seq.inv_mul, rotate 1, apply equiv.refl, apply s_one_mul, repeat (assumption | apply reg_inv_reg | apply reg_mul_reg | apply one_is_reg) end theorem inv_well_defined {s t : seq} (Hs : regular s) (Ht : regular t) (Heq : s ≡ t) : s_inv Hs ≡ s_inv Ht := if Hsep : sep s zero then (begin note Hsept := sep_of_equiv_sep Hs Ht Heq Hsep, have Hm : smul t (s_inv Hs) ≡ smul s (s_inv Hs), begin apply mul_well_defined, repeat (assumption | apply reg_inv_reg), apply equiv.symm s t Heq, apply equiv.refl end, apply equiv.symm, apply inv_unique, rotate 2, apply equiv.trans, rotate 3, apply Hm, apply rat_seq.mul_inv, repeat (assumption | apply reg_inv_reg | apply reg_mul_reg), apply one_is_reg end) else (have H : s_inv Hs = zero, from funext (λ n, dif_neg Hsep), have Hsept : ¬ sep t zero, from assume H', Hsep (sep_of_equiv_sep Ht Hs (equiv.symm _ _ Heq) H'), have H' : s_inv Ht = zero, from funext (λ n, dif_neg Hsept), by rewrite [H', H]; apply equiv.refl) theorem s_neg_neg {s : seq} : sneg (sneg s) ≡ s := begin rewrite [↑equiv, ↑sneg], intro n, rewrite [neg_neg, sub_self, abs_zero], apply add_invs_nonneg end theorem s_neg_sub {s t : seq} (Hs : regular s) (Ht : regular t) : sneg (sadd s (sneg t)) ≡ sadd t (sneg s) := begin apply equiv.trans, rotate 3, apply s_neg_add_eq_s_add_neg, apply equiv.trans, rotate 3, apply add_well_defined, rotate 4, apply equiv.refl, apply s_neg_neg, apply s_add_comm, repeat (assumption | apply reg_add_reg | apply reg_neg_reg) end theorem s_le_total {s t : seq} (Hs : regular s) (Ht : regular t) : s_le s t ∨ s_le t s := if H : s_le s t then or.inl H else or.inr begin rewrite [↑s_le at *], have H' : ∃ n : ℕ+, -n⁻¹ > sadd t (sneg s) n, begin apply by_contradiction, intro Hex, have Hex' : ∀ n : ℕ+, -n⁻¹ ≤ sadd t (sneg s) n, begin intro m, apply by_contradiction, intro Hm, note Hm' := lt_of_not_ge Hm, note Hex'' := exists.intro m Hm', apply Hex Hex'' end, apply H Hex' end, eapply exists.elim H', intro m Hm, note Hm' := neg_lt_neg Hm, rewrite neg_neg at Hm', apply s_nonneg_of_pos, rotate 1, apply pos_of_pos_equiv, rotate 1, apply s_neg_sub, rotate 2, rewrite [↑pos, ↑sneg], existsi m, apply Hm', repeat (assumption | apply reg_add_reg | apply reg_neg_reg) end theorem s_le_of_not_lt {s t : seq} (Hle : ¬ s_lt s t) : s_le t s := begin rewrite [↑s_le, ↑nonneg, ↑s_lt at Hle, ↑pos at Hle], let Hle' := iff.mp forall_iff_not_exists Hle, intro n, let Hn := neg_le_neg (le_of_not_gt (Hle' n)), rewrite [↑sadd, ↑sneg, add_neg_eq_neg_add_rev], apply Hn end theorem sep_of_nequiv {s t : seq} (Hs : regular s) (Ht : regular t) (Hneq : ¬ equiv s t) : sep s t := begin rewrite ↑sep, apply by_contradiction, intro Hnor, let Hand := iff.mp !not_or_iff_not_and_not Hnor, let Hle1 := s_le_of_not_lt (and.left Hand), let Hle2 := s_le_of_not_lt (and.right Hand), apply Hneq (equiv_of_le_of_ge Hs Ht Hle2 Hle1) end theorem s_zero_inv_equiv_zero : s_inv zero_is_reg ≡ zero := by rewrite s_inv_zero; apply equiv.refl theorem lt_or_equiv_of_le {s t : seq} (Hs : regular s) (Ht : regular t) (Hle : s_le s t) : s_lt s t ∨ s ≡ t := if H : s ≡ t then or.inr H else or.inl (lt_of_le_and_sep Hs Ht (and.intro Hle (sep_of_nequiv Hs Ht H))) theorem s_le_of_equiv_le_left {s t u : seq} (Hs : regular s) (Ht : regular t) (Hu : regular u) (Heq : s ≡ t) (Hle : s_le s u) : s_le t u := begin rewrite ↑s_le at *, apply nonneg_of_nonneg_equiv, rotate 2, apply add_well_defined, rotate 4, apply equiv.refl, apply neg_well_defined, apply Heq, repeat (assumption | apply reg_add_reg | apply reg_neg_reg) end theorem s_le_of_equiv_le_right {s t u : seq} (Hs : regular s) (Ht : regular t) (Hu : regular u) (Heq : t ≡ u) (Hle : s_le s t) : s_le s u := begin rewrite ↑s_le at *, apply nonneg_of_nonneg_equiv, rotate 2, apply add_well_defined, rotate 4, apply Heq, apply equiv.refl, repeat (assumption | apply reg_add_reg | apply reg_neg_reg) end ----------------------------- noncomputable definition r_inv (s : reg_seq) : reg_seq := reg_seq.mk (s_inv (reg_seq.is_reg s)) (if H : sep (reg_seq.sq s) zero then reg_inv_reg (reg_seq.is_reg s) H else have Hz : s_inv (reg_seq.is_reg s) = zero, from funext (λ n, dif_neg H), by rewrite Hz; apply zero_is_reg) theorem r_inv_zero : requiv (r_inv r_zero) r_zero := s_zero_inv_equiv_zero theorem r_inv_well_defined {s t : reg_seq} (H : requiv s t) : requiv (r_inv s) (r_inv t) := inv_well_defined (reg_seq.is_reg s) (reg_seq.is_reg t) H theorem r_le_total (s t : reg_seq) : r_le s t ∨ r_le t s := s_le_total (reg_seq.is_reg s) (reg_seq.is_reg t) theorem r_mul_inv (s : reg_seq) (Hsep : r_sep s r_zero) : requiv (s * (r_inv s)) r_one := rat_seq.mul_inv (reg_seq.is_reg s) Hsep theorem r_sep_of_nequiv (s t : reg_seq) (Hneq : ¬ requiv s t) : r_sep s t := sep_of_nequiv (reg_seq.is_reg s) (reg_seq.is_reg t) Hneq theorem r_lt_or_equiv_of_le (s t : reg_seq) (Hle : r_le s t) : r_lt s t ∨ requiv s t := lt_or_equiv_of_le (reg_seq.is_reg s) (reg_seq.is_reg t) Hle theorem r_le_of_equiv_le_left {s t u : reg_seq} (Heq : requiv s t) (Hle : r_le s u) : r_le t u := s_le_of_equiv_le_left (reg_seq.is_reg s) (reg_seq.is_reg t) (reg_seq.is_reg u) Heq Hle theorem r_le_of_equiv_le_right {s t u : reg_seq} (Heq : requiv t u) (Hle : r_le s t) : r_le s u := s_le_of_equiv_le_right (reg_seq.is_reg s) (reg_seq.is_reg t) (reg_seq.is_reg u) Heq Hle definition r_abs (s : reg_seq) : reg_seq := reg_seq.mk (s_abs (reg_seq.sq s)) (abs_reg_of_reg (reg_seq.is_reg s)) theorem r_abs_well_defined {s t : reg_seq} (H : requiv s t) : requiv (r_abs s) (r_abs t) := abs_well_defined (reg_seq.is_reg s) (reg_seq.is_reg t) H end rat_seq namespace real open [class] rat_seq noncomputable protected definition inv (x : ℝ) : ℝ := quot.lift_on x (λ a, quot.mk (rat_seq.r_inv a)) (λ a b H, quot.sound (rat_seq.r_inv_well_defined H)) noncomputable definition real_has_inv [instance] [priority real.prio] : has_inv real := has_inv.mk real.inv noncomputable protected definition div (x y : ℝ) : ℝ := x * y⁻¹ noncomputable definition real_has_div : has_div real := has_div.mk real.div local attribute real_has_div [instance] [priority real.prio] protected theorem le_total (x y : ℝ) : x ≤ y ∨ y ≤ x := quot.induction_on₂ x y (λ s t, rat_seq.r_le_total s t) protected theorem mul_inv_cancel' (x : ℝ) : x ≢ 0 → x * x⁻¹ = 1 := quot.induction_on x (λ s H, quot.sound (rat_seq.r_mul_inv s H)) protected theorem inv_mul_cancel' (x : ℝ) : x ≢ 0 → x⁻¹ * x = 1 := by rewrite real.mul_comm; apply real.mul_inv_cancel' theorem neq_of_sep {x y : ℝ} (H : x ≢ y) : ¬ x = y := assume Heq, !not_sep_self (Heq ▸ H) theorem sep_of_neq {x y : ℝ} : ¬ x = y → x ≢ y := quot.induction_on₂ x y (λ s t H, rat_seq.r_sep_of_nequiv s t (assume Heq, H (quot.sound Heq))) theorem sep_is_neq (x y : ℝ) : (x ≢ y) = (¬ x = y) := propext (iff.intro neq_of_sep sep_of_neq) protected theorem mul_inv_cancel (x : ℝ) : x ≠ 0 → x * x⁻¹ = 1 := !sep_is_neq ▸ !real.mul_inv_cancel' protected theorem inv_mul_cancel (x : ℝ) : x ≠ 0 → x⁻¹ * x = 1 := !sep_is_neq ▸ !real.inv_mul_cancel' protected theorem inv_zero : (0 : ℝ)⁻¹ = 0 := quot.sound (rat_seq.r_inv_zero) protected theorem lt_or_eq_of_le (x y : ℝ) : x ≤ y → x < y ∨ x = y := quot.induction_on₂ x y (λ s t H, or.elim (rat_seq.r_lt_or_equiv_of_le s t H) (assume H1, or.inl H1) (assume H2, or.inr (quot.sound H2))) protected theorem le_iff_lt_or_eq (x y : ℝ) : x ≤ y ↔ x < y ∨ x = y := iff.intro (real.lt_or_eq_of_le x y) (real.le_of_lt_or_eq x y) noncomputable definition dec_lt : decidable_rel real.lt := begin rewrite ↑decidable_rel, intros, apply prop_decidable end protected noncomputable definition discrete_linear_ordered_field [trans_instance]: discrete_linear_ordered_field ℝ := ⦃ discrete_linear_ordered_field, real.comm_ring, real.ordered_ring, le_total := real.le_total, mul_inv_cancel := real.mul_inv_cancel, inv_mul_cancel := real.inv_mul_cancel, zero_lt_one := real.zero_lt_one, inv_zero := real.inv_zero, le_iff_lt_or_eq := real.le_iff_lt_or_eq, decidable_lt := dec_lt ⦄ theorem of_rat_divide (x y : ℚ) : of_rat (x / y) = of_rat x / of_rat y := by_cases (assume yz : y = 0, by krewrite [yz, div_zero, +of_rat_zero, div_zero]) (assume ynz : y ≠ 0, have ynz' : of_rat y ≠ 0, from assume yz', ynz (of_rat.inj yz'), !eq_div_of_mul_eq ynz' (by krewrite [-of_rat_mul, !div_mul_cancel ynz])) open int theorem of_int_div (x y : ℤ) (H : y ∣ x) : of_int (x / y) = of_int x / of_int y := by rewrite [of_int_eq, rat.of_int_div H, of_rat_divide] theorem of_nat_div (x y : ℕ) (H : y ∣ x) : of_nat (x / y) = of_nat x / of_nat y := by rewrite [of_nat_eq, rat.of_nat_div H, of_rat_divide] /- useful for proving equalities -/ theorem eq_zero_of_nonneg_of_forall_lt {x : ℝ} (xnonneg : x ≥ 0) (H : ∀ ε : ℝ, ε > 0 → x < ε) : x = 0 := decidable.by_contradiction (suppose x ≠ 0, have x > 0, from lt_of_le_of_ne xnonneg (ne.symm this), have x < x, from H x this, show false, from !lt.irrefl this) theorem eq_zero_of_nonneg_of_forall_le {x : ℝ} (xnonneg : x ≥ 0) (H : ∀ ε : ℝ, ε > 0 → x ≤ ε) : x = 0 := have ∀ ε : ℝ, ε > 0 → x < ε, from take ε, suppose ε > 0, have e2pos : ε / 2 > 0, from div_pos_of_pos_of_pos `ε > 0` two_pos, have ε / 2 < ε, from div_two_lt_of_pos `ε > 0`, begin apply lt_of_le_of_lt, apply H _ e2pos, apply this end, eq_zero_of_nonneg_of_forall_lt xnonneg this theorem eq_zero_of_forall_abs_le {x : ℝ} (H : ∀ ε : ℝ, ε > 0 → abs x ≤ ε) : x = 0 := by_contradiction (suppose x ≠ 0, have abs x = 0, from eq_zero_of_nonneg_of_forall_le !abs_nonneg H, show false, from `x ≠ 0` (eq_zero_of_abs_eq_zero this)) theorem eq_of_forall_abs_sub_le {x y : ℝ} (H : ∀ ε : ℝ, ε > 0 → abs (x - y) ≤ ε) : x = y := have x - y = 0, from eq_zero_of_forall_abs_le H, eq_of_sub_eq_zero this end real
92dd1c5842b9c079f9eb19b6e2398a71482d3871
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/src/Init/Lean/Compiler/IR.lean
043a049ffac04ae184cee9a4e656e1cf9a7eed78
[ "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
2,736
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.Compiler.IR.Basic import Init.Lean.Compiler.IR.Format import Init.Lean.Compiler.IR.CompilerM import Init.Lean.Compiler.IR.PushProj import Init.Lean.Compiler.IR.ElimDeadVars import Init.Lean.Compiler.IR.SimpCase import Init.Lean.Compiler.IR.ResetReuse import Init.Lean.Compiler.IR.NormIds import Init.Lean.Compiler.IR.Checker import Init.Lean.Compiler.IR.Borrow import Init.Lean.Compiler.IR.Boxing import Init.Lean.Compiler.IR.RC import Init.Lean.Compiler.IR.ExpandResetReuse import Init.Lean.Compiler.IR.UnboxResult import Init.Lean.Compiler.IR.ElimDeadBranches import Init.Lean.Compiler.IR.EmitC import Init.Lean.Compiler.IR.CtorLayout namespace Lean namespace IR private def compileAux (decls : Array Decl) : CompilerM Unit := do logDecls `init decls; checkDecls decls; decls ← elimDeadBranches decls; logDecls `elim_dead_branches decls; let decls := decls.map Decl.pushProj; logDecls `push_proj decls; let decls := decls.map Decl.insertResetReuse; logDecls `reset_reuse decls; let decls := decls.map Decl.elimDead; logDecls `elim_dead decls; let decls := decls.map Decl.simpCase; logDecls `simp_case decls; let decls := decls.map Decl.normalizeIds; decls ← inferBorrow decls; logDecls `borrow decls; decls ← explicitBoxing decls; logDecls `boxing decls; decls ← explicitRC decls; logDecls `rc decls; let decls := decls.map Decl.expandResetReuse; logDecls `expand_reset_reuse decls; let decls := decls.map Decl.pushProj; logDecls `push_proj decls; logDecls `result decls; checkDecls decls; addDecls decls; pure () @[export lean_ir_compile] def compile (env : Environment) (opts : Options) (decls : Array Decl) : Log × (Except String Environment) := match (compileAux decls opts).run { env := env } with | EStateM.Result.ok _ s => (s.log, Except.ok s.env) | EStateM.Result.error msg s => (s.log, Except.error msg) def addBoxedVersionAux (decl : Decl) : CompilerM Unit := do env ← getEnv; if !ExplicitBoxing.requiresBoxedVersion env decl then pure () else do let decl := ExplicitBoxing.mkBoxedVersion decl; let decls : Array Decl := #[decl]; decls ← explicitRC decls; decls.forM $ fun decl => modifyEnv $ fun env => addDeclAux env decl; pure () -- Remark: we are ignoring the `Log` here. This should be fine. @[export lean_ir_add_boxed_version] def addBoxedVersion (env : Environment) (decl : Decl) : Except String Environment := match (addBoxedVersionAux decl Options.empty).run { env := env } with | EStateM.Result.ok _ s => Except.ok s.env | EStateM.Result.error msg s => Except.error msg end IR end Lean
5883a64a8cd461914badbf23d78c42b008260600
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/order/boolean_algebra.lean
b6772b7ac56b7dde8948b3cec0907a16e4518ec9
[ "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
31,562
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, Bryan Gin-ge Chen -/ import order.heyting.basic /-! # (Generalized) Boolean algebras > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. A Boolean algebra is a bounded distributive lattice with a complement operator. Boolean algebras generalize the (classical) logic of propositions and the lattice of subsets of a set. Generalized Boolean algebras may be less familiar, but they are essentially Boolean algebras which do not necessarily have a top element (`⊤`) (and hence not all elements may have complements). One example in mathlib is `finset α`, the type of all finite subsets of an arbitrary (not-necessarily-finite) type `α`. `generalized_boolean_algebra α` is defined to be a distributive lattice with bottom (`⊥`) admitting a *relative* complement operator, written using "set difference" notation as `x \ y` (`sdiff x y`). For convenience, the `boolean_algebra` type class is defined to extend `generalized_boolean_algebra` so that it is also bundled with a `\` operator. (A terminological point: `x \ y` is the complement of `y` relative to the interval `[⊥, x]`. We do not yet have relative complements for arbitrary intervals, as we do not even have lattice intervals.) ## Main declarations * `generalized_boolean_algebra`: a type class for generalized Boolean algebras * `boolean_algebra`: a type class for Boolean algebras. * `Prop.boolean_algebra`: the Boolean algebra instance on `Prop` ## Implementation notes The `sup_inf_sdiff` and `inf_inf_sdiff` axioms for the relative complement operator in `generalized_boolean_algebra` are taken from [Wikipedia](https://en.wikipedia.org/wiki/Boolean_algebra_(structure)#Generalizations). [Stone's paper introducing generalized Boolean algebras][Stone1935] does not define a relative complement operator `a \ b` for all `a`, `b`. Instead, the postulates there amount to an assumption that for all `a, b : α` where `a ≤ b`, the equations `x ⊔ a = b` and `x ⊓ a = ⊥` have a solution `x`. `disjoint.sdiff_unique` proves that this `x` is in fact `b \ a`. ## References * <https://en.wikipedia.org/wiki/Boolean_algebra_(structure)#Generalizations> * [*Postulates for Boolean Algebras and Generalized Boolean Algebras*, M.H. Stone][Stone1935] * [*Lattice Theory: Foundation*, George Grätzer][Gratzer2011] ## Tags generalized Boolean algebras, Boolean algebras, lattices, sdiff, compl -/ set_option old_structure_cmd true open function order_dual universes u v variables {α : Type u} {β : Type*} {w x y z : α} /-! ### Generalized Boolean algebras Some of the lemmas in this section are from: * [*Lattice Theory: Foundation*, George Grätzer][Gratzer2011] * <https://ncatlab.org/nlab/show/relative+complement> * <https://people.math.gatech.edu/~mccuan/courses/4317/symmetricdifference.pdf> -/ /-- A generalized Boolean algebra is a distributive lattice with `⊥` and a relative complement operation `\` (called `sdiff`, after "set difference") satisfying `(a ⊓ b) ⊔ (a \ b) = a` and `(a ⊓ b) ⊓ (a \ b) = ⊥`, i.e. `a \ b` is the complement of `b` in `a`. This is a generalization of Boolean algebras which applies to `finset α` for arbitrary (not-necessarily-`fintype`) `α`. -/ class generalized_boolean_algebra (α : Type u) extends distrib_lattice α, has_sdiff α, has_bot α := (sup_inf_sdiff : ∀a b:α, (a ⊓ b) ⊔ (a \ b) = a) (inf_inf_sdiff : ∀a b:α, (a ⊓ b) ⊓ (a \ b) = ⊥) -- We might want a `is_compl_of` predicate (for relative complements) generalizing `is_compl`, -- however we'd need another type class for lattices with bot, and all the API for that. section generalized_boolean_algebra variables [generalized_boolean_algebra α] @[simp] theorem sup_inf_sdiff (x y : α) : (x ⊓ y) ⊔ (x \ y) = x := generalized_boolean_algebra.sup_inf_sdiff _ _ @[simp] theorem inf_inf_sdiff (x y : α) : (x ⊓ y) ⊓ (x \ y) = ⊥ := generalized_boolean_algebra.inf_inf_sdiff _ _ @[simp] theorem sup_sdiff_inf (x y : α) : (x \ y) ⊔ (x ⊓ y) = x := by rw [sup_comm, sup_inf_sdiff] @[simp] theorem inf_sdiff_inf (x y : α) : (x \ y) ⊓ (x ⊓ y) = ⊥ := by rw [inf_comm, inf_inf_sdiff] @[priority 100] -- see Note [lower instance priority] instance generalized_boolean_algebra.to_order_bot : order_bot α := { bot_le := λ a, by { rw [←inf_inf_sdiff a a, inf_assoc], exact inf_le_left }, ..generalized_boolean_algebra.to_has_bot α } theorem disjoint_inf_sdiff : disjoint (x ⊓ y) (x \ y) := disjoint_iff_inf_le.mpr (inf_inf_sdiff x y).le -- TODO: in distributive lattices, relative complements are unique when they exist theorem sdiff_unique (s : (x ⊓ y) ⊔ z = x) (i : (x ⊓ y) ⊓ z = ⊥) : x \ y = z := begin conv_rhs at s { rw [←sup_inf_sdiff x y, sup_comm] }, rw sup_comm at s, conv_rhs at i { rw [←inf_inf_sdiff x y, inf_comm] }, rw inf_comm at i, exact (eq_of_inf_eq_sup_eq i s).symm, end -- Use `sdiff_le` private lemma sdiff_le' : x \ y ≤ x := calc x \ y ≤ (x ⊓ y) ⊔ (x \ y) : le_sup_right ... = x : sup_inf_sdiff x y -- Use `sdiff_sup_self` private lemma sdiff_sup_self' : y \ x ⊔ x = y ⊔ x := calc y \ x ⊔ x = y \ x ⊔ (x ⊔ x ⊓ y) : by rw sup_inf_self ... = (y ⊓ x) ⊔ y \ x ⊔ x : by ac_refl ... = y ⊔ x : by rw sup_inf_sdiff @[simp] lemma sdiff_inf_sdiff : x \ y ⊓ (y \ x) = ⊥ := eq.symm $ calc ⊥ = (x ⊓ y) ⊓ (x \ y) : by rw inf_inf_sdiff ... = (x ⊓ (y ⊓ x ⊔ y \ x)) ⊓ (x \ y) : by rw sup_inf_sdiff ... = (x ⊓ (y ⊓ x) ⊔ x ⊓ (y \ x)) ⊓ (x \ y) : by rw inf_sup_left ... = (y ⊓ (x ⊓ x) ⊔ x ⊓ (y \ x)) ⊓ (x \ y) : by ac_refl ... = (y ⊓ x ⊔ x ⊓ (y \ x)) ⊓ (x \ y) : by rw inf_idem ... = (x ⊓ y ⊓ (x \ y)) ⊔ (x ⊓ (y \ x) ⊓ (x \ y)) : by rw [inf_sup_right, @inf_comm _ _ x y] ... = x ⊓ (y \ x) ⊓ (x \ y) : by rw [inf_inf_sdiff, bot_sup_eq] ... = x ⊓ (x \ y) ⊓ (y \ x) : by ac_refl ... = (x \ y) ⊓ (y \ x) : by rw inf_of_le_right sdiff_le' lemma disjoint_sdiff_sdiff : disjoint (x \ y) (y \ x) := disjoint_iff_inf_le.mpr sdiff_inf_sdiff.le @[simp] theorem inf_sdiff_self_right : x ⊓ (y \ x) = ⊥ := calc x ⊓ (y \ x) = ((x ⊓ y) ⊔ (x \ y)) ⊓ (y \ x) : by rw sup_inf_sdiff ... = (x ⊓ y) ⊓ (y \ x) ⊔ (x \ y) ⊓ (y \ x) : by rw inf_sup_right ... = ⊥ : by rw [@inf_comm _ _ x y, inf_inf_sdiff, sdiff_inf_sdiff, bot_sup_eq] @[simp] theorem inf_sdiff_self_left : (y \ x) ⊓ x = ⊥ := by rw [inf_comm, inf_sdiff_self_right] @[priority 100] -- see Note [lower instance priority] instance generalized_boolean_algebra.to_generalized_coheyting_algebra : generalized_coheyting_algebra α := { sdiff := (\), sdiff_le_iff := λ y x z, ⟨λ h, le_of_inf_le_sup_le (le_of_eq (calc y ⊓ (y \ x) = y \ x : inf_of_le_right sdiff_le' ... = (x ⊓ (y \ x)) ⊔ (z ⊓ (y \ x)) : by rw [inf_eq_right.2 h, inf_sdiff_self_right, bot_sup_eq] ... = (x ⊔ z) ⊓ (y \ x) : inf_sup_right.symm)) (calc y ⊔ y \ x = y : sup_of_le_left sdiff_le' ... ≤ y ⊔ (x ⊔ z) : le_sup_left ... = ((y \ x) ⊔ x) ⊔ z : by rw [←sup_assoc, ←@sdiff_sup_self' _ x y] ... = x ⊔ z ⊔ y \ x : by ac_refl), λ h, le_of_inf_le_sup_le (calc y \ x ⊓ x = ⊥ : inf_sdiff_self_left ... ≤ z ⊓ x : bot_le) (calc y \ x ⊔ x = y ⊔ x : sdiff_sup_self' ... ≤ (x ⊔ z) ⊔ x : sup_le_sup_right h x ... ≤ z ⊔ x : by rw [sup_assoc, sup_comm, sup_assoc, sup_idem])⟩, ..‹generalized_boolean_algebra α›, ..generalized_boolean_algebra.to_order_bot } theorem disjoint_sdiff_self_left : disjoint (y \ x) x := disjoint_iff_inf_le.mpr inf_sdiff_self_left.le theorem disjoint_sdiff_self_right : disjoint x (y \ x) := disjoint_iff_inf_le.mpr inf_sdiff_self_right.le lemma le_sdiff : x ≤ y \ z ↔ x ≤ y ∧ disjoint x z := ⟨λ h, ⟨h.trans sdiff_le, disjoint_sdiff_self_left.mono_left h⟩, λ h, by { rw ←h.2.sdiff_eq_left, exact sdiff_le_sdiff_right h.1 }⟩ @[simp] lemma sdiff_eq_left : x \ y = x ↔ disjoint x y := ⟨λ h, disjoint_sdiff_self_left.mono_left h.ge, disjoint.sdiff_eq_left⟩ /- TODO: we could make an alternative constructor for `generalized_boolean_algebra` using `disjoint x (y \ x)` and `x ⊔ (y \ x) = y` as axioms. -/ theorem disjoint.sdiff_eq_of_sup_eq (hi : disjoint x z) (hs : x ⊔ z = y) : y \ x = z := have h : y ⊓ x = x := inf_eq_right.2 $ le_sup_left.trans hs.le, sdiff_unique (by rw [h, hs]) (by rw [h, hi.eq_bot]) protected theorem disjoint.sdiff_unique (hd : disjoint x z) (hz : z ≤ y) (hs : y ≤ x ⊔ z) : y \ x = z := sdiff_unique (begin rw ←inf_eq_right at hs, rwa [sup_inf_right, inf_sup_right, @sup_comm _ _ x, inf_sup_self, inf_comm, @sup_comm _ _ z, hs, sup_eq_left], end) (by rw [inf_assoc, hd.eq_bot, inf_bot_eq]) -- cf. `is_compl.disjoint_left_iff` and `is_compl.disjoint_right_iff` lemma disjoint_sdiff_iff_le (hz : z ≤ y) (hx : x ≤ y) : disjoint z (y \ x) ↔ z ≤ x := ⟨λ H, le_of_inf_le_sup_le (le_trans H.le_bot bot_le) (begin rw sup_sdiff_cancel_right hx, refine le_trans (sup_le_sup_left sdiff_le z) _, rw sup_eq_right.2 hz, end), λ H, disjoint_sdiff_self_right.mono_left H⟩ -- cf. `is_compl.le_left_iff` and `is_compl.le_right_iff` lemma le_iff_disjoint_sdiff (hz : z ≤ y) (hx : x ≤ y) : z ≤ x ↔ disjoint z (y \ x) := (disjoint_sdiff_iff_le hz hx).symm -- cf. `is_compl.inf_left_eq_bot_iff` and `is_compl.inf_right_eq_bot_iff` lemma inf_sdiff_eq_bot_iff (hz : z ≤ y) (hx : x ≤ y) : z ⊓ (y \ x) = ⊥ ↔ z ≤ x := by { rw ←disjoint_iff, exact disjoint_sdiff_iff_le hz hx } -- cf. `is_compl.left_le_iff` and `is_compl.right_le_iff` lemma le_iff_eq_sup_sdiff (hz : z ≤ y) (hx : x ≤ y) : x ≤ z ↔ y = z ⊔ (y \ x) := ⟨λ H, begin apply le_antisymm, { conv_lhs { rw ←sup_inf_sdiff y x, }, apply sup_le_sup_right, rwa inf_eq_right.2 hx, }, { apply le_trans, { apply sup_le_sup_right hz, }, { rw sup_sdiff_left, } } end, λ H, begin conv_lhs at H { rw ←sup_sdiff_cancel_right hx, }, refine le_of_inf_le_sup_le _ H.le, rw inf_sdiff_self_right, exact bot_le, end⟩ -- cf. `is_compl.sup_inf` lemma sdiff_sup : y \ (x ⊔ z) = (y \ x) ⊓ (y \ z) := sdiff_unique (calc y ⊓ (x ⊔ z) ⊔ y \ x ⊓ (y \ z) = (y ⊓ (x ⊔ z) ⊔ y \ x) ⊓ (y ⊓ (x ⊔ z) ⊔ (y \ z)) : by rw sup_inf_left ... = (y ⊓ x ⊔ y ⊓ z ⊔ y \ x) ⊓ (y ⊓ x ⊔ y ⊓ z ⊔ (y \ z)) : by rw @inf_sup_left _ _ y ... = (y ⊓ z ⊔ (y ⊓ x ⊔ y \ x)) ⊓ (y ⊓ x ⊔ (y ⊓ z ⊔ (y \ z))) : by ac_refl ... = (y ⊓ z ⊔ y) ⊓ (y ⊓ x ⊔ y) : by rw [sup_inf_sdiff, sup_inf_sdiff] ... = (y ⊔ y ⊓ z) ⊓ (y ⊔ y ⊓ x) : by ac_refl ... = y : by rw [sup_inf_self, sup_inf_self, inf_idem]) (calc y ⊓ (x ⊔ z) ⊓ ((y \ x) ⊓ (y \ z)) = (y ⊓ x ⊔ y ⊓ z) ⊓ ((y \ x) ⊓ (y \ z)) : by rw inf_sup_left ... = ((y ⊓ x) ⊓ ((y \ x) ⊓ (y \ z))) ⊔ ((y ⊓ z) ⊓ ((y \ x) ⊓ (y \ z))) : by rw inf_sup_right ... = ((y ⊓ x) ⊓ (y \ x) ⊓ (y \ z)) ⊔ ((y \ x) ⊓ ((y \ z) ⊓ (y ⊓ z))) : by ac_refl ... = ⊥ : by rw [inf_inf_sdiff, bot_inf_eq, bot_sup_eq, @inf_comm _ _ (y \ z), inf_inf_sdiff, inf_bot_eq]) lemma sdiff_eq_sdiff_iff_inf_eq_inf : y \ x = y \ z ↔ y ⊓ x = y ⊓ z := ⟨λ h, eq_of_inf_eq_sup_eq (by rw [inf_inf_sdiff, h, inf_inf_sdiff]) (by rw [sup_inf_sdiff, h, sup_inf_sdiff]), λ h, by rw [←sdiff_inf_self_right, ←sdiff_inf_self_right z y, inf_comm, h, inf_comm]⟩ theorem sdiff_eq_self_iff_disjoint : x \ y = x ↔ disjoint y x := calc x \ y = x ↔ x \ y = x \ ⊥ : by rw sdiff_bot ... ↔ x ⊓ y = x ⊓ ⊥ : sdiff_eq_sdiff_iff_inf_eq_inf ... ↔ disjoint y x : by rw [inf_bot_eq, inf_comm, disjoint_iff] theorem sdiff_eq_self_iff_disjoint' : x \ y = x ↔ disjoint x y := by rw [sdiff_eq_self_iff_disjoint, disjoint.comm] lemma sdiff_lt (hx : y ≤ x) (hy : y ≠ ⊥) : x \ y < x := begin refine sdiff_le.lt_of_ne (λ h, hy _), rw [sdiff_eq_self_iff_disjoint', disjoint_iff] at h, rw [←h, inf_eq_right.mpr hx], end @[simp] lemma le_sdiff_iff : x ≤ y \ x ↔ x = ⊥ := ⟨λ h, disjoint_self.1 (disjoint_sdiff_self_right.mono_right h), λ h, h.le.trans bot_le⟩ lemma sdiff_lt_sdiff_right (h : x < y) (hz : z ≤ x) : x \ z < y \ z := (sdiff_le_sdiff_right h.le).lt_of_not_le $ λ h', h.not_le $ le_sdiff_sup.trans $ sup_le_of_le_sdiff_right h' hz lemma sup_inf_inf_sdiff : (x ⊓ y) ⊓ z ⊔ (y \ z) = (x ⊓ y) ⊔ (y \ z) := calc (x ⊓ y) ⊓ z ⊔ (y \ z) = x ⊓ (y ⊓ z) ⊔ (y \ z) : by rw inf_assoc ... = (x ⊔ (y \ z)) ⊓ y : by rw [sup_inf_right, sup_inf_sdiff] ... = (x ⊓ y) ⊔ (y \ z) : by rw [inf_sup_right, inf_sdiff_left] lemma sdiff_sdiff_right : x \ (y \ z) = (x \ y) ⊔ (x ⊓ y ⊓ z) := begin rw [sup_comm, inf_comm, ←inf_assoc, sup_inf_inf_sdiff], apply sdiff_unique, { calc x ⊓ (y \ z) ⊔ (z ⊓ x ⊔ x \ y) = (x ⊔ (z ⊓ x ⊔ x \ y)) ⊓ (y \ z ⊔ (z ⊓ x ⊔ x \ y)) : by rw sup_inf_right ... = (x ⊔ x ⊓ z ⊔ x \ y) ⊓ (y \ z ⊔ (x ⊓ z ⊔ x \ y)) : by ac_refl ... = x ⊓ (y \ z ⊔ x ⊓ z ⊔ x \ y) : by rw [sup_inf_self, sup_sdiff_left, ←sup_assoc] ... = x ⊓ (y \ z ⊓ (z ⊔ y) ⊔ x ⊓ (z ⊔ y) ⊔ x \ y) : by rw [sup_inf_left, sdiff_sup_self', inf_sup_right, @sup_comm _ _ y] ... = x ⊓ (y \ z ⊔ (x ⊓ z ⊔ x ⊓ y) ⊔ x \ y) : by rw [inf_sdiff_sup_right, @inf_sup_left _ _ x z y] ... = x ⊓ (y \ z ⊔ (x ⊓ z ⊔ (x ⊓ y ⊔ x \ y))) : by ac_refl ... = x ⊓ (y \ z ⊔ (x ⊔ x ⊓ z)) : by rw [sup_inf_sdiff, @sup_comm _ _ (x ⊓ z)] ... = x : by rw [sup_inf_self, sup_comm, inf_sup_self] }, { calc x ⊓ (y \ z) ⊓ (z ⊓ x ⊔ x \ y) = x ⊓ (y \ z) ⊓ (z ⊓ x) ⊔ x ⊓ (y \ z) ⊓ (x \ y) : by rw inf_sup_left ... = x ⊓ (y \ z ⊓ z ⊓ x) ⊔ x ⊓ (y \ z) ⊓ (x \ y) : by ac_refl ... = x ⊓ (y \ z) ⊓ (x \ y) : by rw [inf_sdiff_self_left, bot_inf_eq, inf_bot_eq, bot_sup_eq] ... = x ⊓ (y \ z ⊓ y) ⊓ (x \ y) : by conv_lhs { rw ←inf_sdiff_left } ... = x ⊓ (y \ z ⊓ (y ⊓ (x \ y))) : by ac_refl ... = ⊥ : by rw [inf_sdiff_self_right, inf_bot_eq, inf_bot_eq] } end lemma sdiff_sdiff_right' : x \ (y \ z) = (x \ y) ⊔ (x ⊓ z) := calc x \ (y \ z) = (x \ y) ⊔ (x ⊓ y ⊓ z) : sdiff_sdiff_right ... = z ⊓ x ⊓ y ⊔ (x \ y) : by ac_refl ... = (x \ y) ⊔ (x ⊓ z) : by rw [sup_inf_inf_sdiff, sup_comm, inf_comm] lemma sdiff_sdiff_eq_sdiff_sup (h : z ≤ x) : x \ (y \ z) = x \ y ⊔ z := by rw [sdiff_sdiff_right', inf_eq_right.2 h] @[simp] lemma sdiff_sdiff_right_self : x \ (x \ y) = x ⊓ y := by rw [sdiff_sdiff_right, inf_idem, sdiff_self, bot_sup_eq] lemma sdiff_sdiff_eq_self (h : y ≤ x) : x \ (x \ y) = y := by rw [sdiff_sdiff_right_self, inf_of_le_right h] lemma sdiff_eq_symm (hy : y ≤ x) (h : x \ y = z) : x \ z = y := by rw [←h, sdiff_sdiff_eq_self hy] lemma sdiff_eq_comm (hy : y ≤ x) (hz : z ≤ x) : x \ y = z ↔ x \ z = y := ⟨sdiff_eq_symm hy, sdiff_eq_symm hz⟩ lemma eq_of_sdiff_eq_sdiff (hxz : x ≤ z) (hyz : y ≤ z) (h : z \ x = z \ y) : x = y := by rw [←sdiff_sdiff_eq_self hxz, h, sdiff_sdiff_eq_self hyz] lemma sdiff_sdiff_left' : (x \ y) \ z = (x \ y) ⊓ (x \ z) := by rw [sdiff_sdiff_left, sdiff_sup] lemma sdiff_sdiff_sup_sdiff : z \ (x \ y ⊔ y \ x) = z ⊓ (z \ x ⊔ y) ⊓ (z \ y ⊔ x) := calc z \ (x \ y ⊔ y \ x) = (z \ x ⊔ z ⊓ x ⊓ y) ⊓ (z \ y ⊔ z ⊓ y ⊓ x) : by rw [sdiff_sup, sdiff_sdiff_right, sdiff_sdiff_right] ... = z ⊓ (z \ x ⊔ y) ⊓ (z \ y ⊔ z ⊓ y ⊓ x) : by rw [sup_inf_left, sup_comm, sup_inf_sdiff] ... = z ⊓ (z \ x ⊔ y) ⊓ (z ⊓ (z \ y ⊔ x)) : by rw [sup_inf_left, @sup_comm _ _ (z \ y), sup_inf_sdiff] ... = z ⊓ z ⊓ (z \ x ⊔ y) ⊓ (z \ y ⊔ x) : by ac_refl ... = z ⊓ (z \ x ⊔ y) ⊓ (z \ y ⊔ x) : by rw inf_idem lemma sdiff_sdiff_sup_sdiff' : z \ (x \ y ⊔ y \ x) = z ⊓ x ⊓ y ⊔ ((z \ x) ⊓ (z \ y)) := calc z \ (x \ y ⊔ y \ x) = z \ (x \ y) ⊓ (z \ (y \ x)) : sdiff_sup ... = (z \ x ⊔ z ⊓ x ⊓ y) ⊓ (z \ y ⊔ z ⊓ y ⊓ x) : by rw [sdiff_sdiff_right, sdiff_sdiff_right] ... = (z \ x ⊔ z ⊓ y ⊓ x) ⊓ (z \ y ⊔ z ⊓ y ⊓ x) : by ac_refl ... = (z \ x) ⊓ (z \ y) ⊔ z ⊓ y ⊓ x : sup_inf_right.symm ... = z ⊓ x ⊓ y ⊔ ((z \ x) ⊓ (z \ y)) : by ac_refl lemma inf_sdiff : (x ⊓ y) \ z = (x \ z) ⊓ (y \ z) := sdiff_unique (calc (x ⊓ y) ⊓ z ⊔ ((x \ z) ⊓ (y \ z)) = ((x ⊓ y) ⊓ z ⊔ (x \ z)) ⊓ ((x ⊓ y) ⊓ z ⊔ (y \ z)) : by rw [sup_inf_left] ... = (x ⊓ y ⊓ (z ⊔ x) ⊔ x \ z) ⊓ (x ⊓ y ⊓ z ⊔ y \ z) : by rw [sup_inf_right, sup_sdiff_self_right, inf_sup_right, inf_sdiff_sup_right] ... = (y ⊓ (x ⊓ (x ⊔ z)) ⊔ x \ z) ⊓ (x ⊓ y ⊓ z ⊔ y \ z) : by ac_refl ... = ((y ⊓ x) ⊔ (x \ z)) ⊓ ((x ⊓ y) ⊔ (y \ z)) : by rw [inf_sup_self, sup_inf_inf_sdiff] ... = (x ⊓ y) ⊔ ((x \ z) ⊓ (y \ z)) : by rw [@inf_comm _ _ y, sup_inf_left] ... = x ⊓ y : sup_eq_left.2 (inf_le_inf sdiff_le sdiff_le)) (calc (x ⊓ y) ⊓ z ⊓ ((x \ z) ⊓ (y \ z)) = x ⊓ y ⊓ (z ⊓ (x \ z)) ⊓ (y \ z) : by ac_refl ... = ⊥ : by rw [inf_sdiff_self_right, inf_bot_eq, bot_inf_eq]) lemma inf_sdiff_assoc : (x ⊓ y) \ z = x ⊓ (y \ z) := sdiff_unique (calc x ⊓ y ⊓ z ⊔ x ⊓ (y \ z) = x ⊓ (y ⊓ z) ⊔ x ⊓ (y \ z) : by rw inf_assoc ... = x ⊓ ((y ⊓ z) ⊔ y \ z) : inf_sup_left.symm ... = x ⊓ y : by rw sup_inf_sdiff) (calc x ⊓ y ⊓ z ⊓ (x ⊓ (y \ z)) = x ⊓ x ⊓ ((y ⊓ z) ⊓ (y \ z)) : by ac_refl ... = ⊥ : by rw [inf_inf_sdiff, inf_bot_eq]) lemma inf_sdiff_right_comm : x \ z ⊓ y = (x ⊓ y) \ z := by rw [@inf_comm _ _ x, inf_comm, inf_sdiff_assoc] lemma inf_sdiff_distrib_left (a b c : α) : a ⊓ b \ c = (a ⊓ b) \ (a ⊓ c) := by rw [sdiff_inf, sdiff_eq_bot_iff.2 inf_le_left, bot_sup_eq, inf_sdiff_assoc] lemma inf_sdiff_distrib_right (a b c : α) : a \ b ⊓ c = (a ⊓ c) \ (b ⊓ c) := by simp_rw [@inf_comm _ _ _ c, inf_sdiff_distrib_left] lemma disjoint_sdiff_comm : disjoint (x \ z) y ↔ disjoint x (y \ z) := by simp_rw [disjoint_iff, inf_sdiff_right_comm, inf_sdiff_assoc] lemma sup_eq_sdiff_sup_sdiff_sup_inf : x ⊔ y = (x \ y) ⊔ (y \ x) ⊔ (x ⊓ y) := eq.symm $ calc (x \ y) ⊔ (y \ x) ⊔ (x ⊓ y) = ((x \ y) ⊔ (y \ x) ⊔ x) ⊓ ((x \ y) ⊔ (y \ x) ⊔ y) : by rw sup_inf_left ... = ((x \ y) ⊔ x ⊔ (y \ x)) ⊓ ((x \ y) ⊔ ((y \ x) ⊔ y)) : by ac_refl ... = (x ⊔ (y \ x)) ⊓ ((x \ y) ⊔ y) : by rw [sup_sdiff_right, sup_sdiff_right] ... = x ⊔ y : by rw [sup_sdiff_self_right, sup_sdiff_self_left, inf_idem] lemma sup_lt_of_lt_sdiff_left (h : y < z \ x) (hxz : x ≤ z) : x ⊔ y < z := begin rw ←sup_sdiff_cancel_right hxz, refine (sup_le_sup_left h.le _).lt_of_not_le (λ h', h.not_le _), rw ←sdiff_idem, exact (sdiff_le_sdiff_of_sup_le_sup_left h').trans sdiff_le, end lemma sup_lt_of_lt_sdiff_right (h : x < z \ y) (hyz : y ≤ z) : x ⊔ y < z := begin rw ←sdiff_sup_cancel hyz, refine (sup_le_sup_right h.le _).lt_of_not_le (λ h', h.not_le _), rw ←sdiff_idem, exact (sdiff_le_sdiff_of_sup_le_sup_right h').trans sdiff_le, end instance pi.generalized_boolean_algebra {α : Type u} {β : Type v} [generalized_boolean_algebra β] : generalized_boolean_algebra (α → β) := by pi_instance end generalized_boolean_algebra /-! ### Boolean algebras -/ /-- A Boolean algebra is a bounded distributive lattice with a complement operator `ᶜ` such that `x ⊓ xᶜ = ⊥` and `x ⊔ xᶜ = ⊤`. For convenience, it must also provide a set difference operation `\` and a Heyting implication `⇨` satisfying `x \ y = x ⊓ yᶜ` and `x ⇨ y = y ⊔ xᶜ`. This is a generalization of (classical) logic of propositions, or the powerset lattice. Since `bounded_order`, `order_bot`, and `order_top` are mixins that require `has_le` to be present at define-time, the `extends` mechanism does not work with them. Instead, we extend using the underlying `has_bot` and `has_top` data typeclasses, and replicate the order axioms of those classes here. A "forgetful" instance back to `bounded_order` is provided. -/ class boolean_algebra (α : Type u) extends distrib_lattice α, has_compl α, has_sdiff α, has_himp α, has_top α, has_bot α := (inf_compl_le_bot : ∀x:α, x ⊓ xᶜ ≤ ⊥) (top_le_sup_compl : ∀x:α, ⊤ ≤ x ⊔ xᶜ) (le_top : ∀ a : α, a ≤ ⊤) (bot_le : ∀ a : α, ⊥ ≤ a) (sdiff := λ x y, x ⊓ yᶜ) (himp := λ x y, y ⊔ xᶜ) (sdiff_eq : ∀ x y : α, x \ y = x ⊓ yᶜ . obviously) (himp_eq : ∀ x y : α, x ⇨ y = y ⊔ xᶜ . obviously) @[priority 100] -- see Note [lower instance priority] instance boolean_algebra.to_bounded_order [h : boolean_algebra α] : bounded_order α := { ..h } /-- A bounded generalized boolean algebra is a boolean algebra. -/ @[reducible] -- See note [reducible non instances] def generalized_boolean_algebra.to_boolean_algebra [generalized_boolean_algebra α] [order_top α] : boolean_algebra α := { compl := λ a, ⊤ \ a, inf_compl_le_bot := λ _, disjoint_sdiff_self_right.le_bot, top_le_sup_compl := λ _, le_sup_sdiff, sdiff_eq := λ _ _, by { rw [←inf_sdiff_assoc, inf_top_eq], refl }, ..‹generalized_boolean_algebra α›, ..generalized_boolean_algebra.to_order_bot, ..‹order_top α› } section boolean_algebra variables [boolean_algebra α] @[simp] lemma inf_compl_eq_bot' : x ⊓ xᶜ = ⊥ := bot_unique $ boolean_algebra.inf_compl_le_bot x @[simp] lemma sup_compl_eq_top : x ⊔ xᶜ = ⊤ := top_unique $ boolean_algebra.top_le_sup_compl x @[simp] lemma compl_sup_eq_top : xᶜ ⊔ x = ⊤ := sup_comm.trans sup_compl_eq_top lemma is_compl_compl : is_compl x xᶜ := is_compl.of_eq inf_compl_eq_bot' sup_compl_eq_top lemma sdiff_eq : x \ y = x ⊓ yᶜ := boolean_algebra.sdiff_eq x y lemma himp_eq : x ⇨ y = y ⊔ xᶜ := boolean_algebra.himp_eq x y @[priority 100] instance boolean_algebra.to_complemented_lattice : complemented_lattice α := ⟨λ x, ⟨xᶜ, is_compl_compl⟩⟩ @[priority 100] -- see Note [lower instance priority] instance boolean_algebra.to_generalized_boolean_algebra : generalized_boolean_algebra α := { sup_inf_sdiff := λ a b, by rw [sdiff_eq, ←inf_sup_left, sup_compl_eq_top, inf_top_eq], inf_inf_sdiff := λ a b, by { rw [sdiff_eq, ←inf_inf_distrib_left, inf_compl_eq_bot', inf_bot_eq], congr }, ..‹boolean_algebra α› } @[priority 100] -- See note [lower instance priority] instance boolean_algebra.to_biheyting_algebra : biheyting_algebra α := { hnot := compl, le_himp_iff := λ a b c, by rw [himp_eq, is_compl_compl.le_sup_right_iff_inf_left_le], himp_bot := λ _, himp_eq.trans bot_sup_eq, top_sdiff := λ a, by rw [sdiff_eq, top_inf_eq], ..‹boolean_algebra α›, ..generalized_boolean_algebra.to_generalized_coheyting_algebra } @[simp] lemma hnot_eq_compl : ¬x = xᶜ := rfl @[simp] lemma top_sdiff : ⊤ \ x = xᶜ := top_sdiff' _ theorem eq_compl_iff_is_compl : x = yᶜ ↔ is_compl x y := ⟨λ h, by { rw h, exact is_compl_compl.symm }, is_compl.eq_compl⟩ theorem compl_eq_iff_is_compl : xᶜ = y ↔ is_compl x y := ⟨λ h, by { rw ←h, exact is_compl_compl }, is_compl.compl_eq⟩ theorem compl_eq_comm : xᶜ = y ↔ yᶜ = x := by rw [eq_comm, compl_eq_iff_is_compl, eq_compl_iff_is_compl] theorem eq_compl_comm : x = yᶜ ↔ y = xᶜ := by rw [eq_comm, compl_eq_iff_is_compl, eq_compl_iff_is_compl] @[simp] theorem compl_compl (x : α) : xᶜᶜ = x := (@is_compl_compl _ x _).symm.compl_eq theorem compl_comp_compl : compl ∘ compl = @id α := funext compl_compl @[simp] theorem compl_involutive : function.involutive (compl : α → α) := compl_compl theorem compl_bijective : function.bijective (compl : α → α) := compl_involutive.bijective theorem compl_surjective : function.surjective (compl : α → α) := compl_involutive.surjective theorem compl_injective : function.injective (compl : α → α) := compl_involutive.injective @[simp] theorem compl_inj_iff : xᶜ = yᶜ ↔ x = y := compl_injective.eq_iff theorem is_compl.compl_eq_iff (h : is_compl x y) : zᶜ = y ↔ z = x := h.compl_eq ▸ compl_inj_iff @[simp] theorem compl_eq_top : xᶜ = ⊤ ↔ x = ⊥ := is_compl_bot_top.compl_eq_iff @[simp] theorem compl_eq_bot : xᶜ = ⊥ ↔ x = ⊤ := is_compl_top_bot.compl_eq_iff @[simp] theorem compl_inf : (x ⊓ y)ᶜ = xᶜ ⊔ yᶜ := hnot_inf_distrib _ _ @[simp] theorem compl_le_compl_iff_le : yᶜ ≤ xᶜ ↔ x ≤ y := ⟨assume h, by have h := compl_le_compl h; simp at h; assumption, compl_le_compl⟩ theorem compl_le_of_compl_le (h : yᶜ ≤ x) : xᶜ ≤ y := by simpa only [compl_compl] using compl_le_compl h theorem compl_le_iff_compl_le : xᶜ ≤ y ↔ yᶜ ≤ x := ⟨compl_le_of_compl_le, compl_le_of_compl_le⟩ @[simp] lemma sdiff_compl : x \ yᶜ = x ⊓ y := by rw [sdiff_eq, compl_compl] instance : boolean_algebra αᵒᵈ := { compl := λ a, to_dual (of_dual a)ᶜ, sdiff := λ a b, to_dual (of_dual b ⇨ of_dual a), himp := λ a b, to_dual (of_dual b \ of_dual a), inf_compl_le_bot := λ a, (@codisjoint_hnot_right _ _ (of_dual a)).top_le, top_le_sup_compl := λ a, (@disjoint_compl_right _ _ (of_dual a)).le_bot, sdiff_eq := λ _ _, himp_eq, himp_eq := λ _ _, sdiff_eq, ..order_dual.distrib_lattice α, ..order_dual.bounded_order α } @[simp] lemma sup_inf_inf_compl : (x ⊓ y) ⊔ (x ⊓ yᶜ) = x := by rw [← sdiff_eq, sup_inf_sdiff _ _] @[simp] lemma compl_sdiff : (x \ y)ᶜ = x ⇨ y := by rw [sdiff_eq, himp_eq, compl_inf, compl_compl, sup_comm] @[simp] lemma compl_himp : (x ⇨ y)ᶜ = x \ y := @compl_sdiff αᵒᵈ _ _ _ @[simp] lemma compl_sdiff_compl : xᶜ \ yᶜ = y \ x := by rw [sdiff_compl, sdiff_eq, inf_comm] @[simp] lemma compl_himp_compl : xᶜ ⇨ yᶜ = y ⇨ x := @compl_sdiff_compl αᵒᵈ _ _ _ lemma disjoint_compl_left_iff : disjoint xᶜ y ↔ y ≤ x := by rw [←le_compl_iff_disjoint_left, compl_compl] lemma disjoint_compl_right_iff : disjoint x yᶜ ↔ x ≤ y := by rw [←le_compl_iff_disjoint_right, compl_compl] lemma codisjoint_himp_self_left : codisjoint (x ⇨ y) x := @disjoint_sdiff_self_left αᵒᵈ _ _ _ lemma codisjoint_himp_self_right : codisjoint x (x ⇨ y) := @disjoint_sdiff_self_right αᵒᵈ _ _ _ lemma himp_le : x ⇨ y ≤ z ↔ y ≤ z ∧ codisjoint x z := (@le_sdiff αᵒᵈ _ _ _ _).trans $ and_congr_right' codisjoint.comm end boolean_algebra instance Prop.boolean_algebra : boolean_algebra Prop := { compl := not, himp_eq := λ p q, propext imp_iff_or_not, inf_compl_le_bot := λ p ⟨Hp, Hpc⟩, Hpc Hp, top_le_sup_compl := λ p H, classical.em p, .. Prop.heyting_algebra, ..generalized_heyting_algebra.to_distrib_lattice } instance pi.boolean_algebra {ι : Type u} {α : ι → Type v} [∀ i, boolean_algebra (α i)] : boolean_algebra (Π i, α i) := { sdiff_eq := λ x y, funext $ λ i, sdiff_eq, himp_eq := λ x y, funext $ λ i, himp_eq, inf_compl_le_bot := λ _ _, boolean_algebra.inf_compl_le_bot _, top_le_sup_compl := λ _ _, boolean_algebra.top_le_sup_compl _, .. pi.has_sdiff, .. pi.heyting_algebra, .. pi.distrib_lattice } instance : boolean_algebra bool := { sup := bor, le_sup_left := bool.left_le_bor, le_sup_right := bool.right_le_bor, sup_le := λ _ _ _, bool.bor_le, inf := band, inf_le_left := bool.band_le_left, inf_le_right := bool.band_le_right, le_inf := λ _ _ _, bool.le_band, le_sup_inf := dec_trivial, compl := bnot, inf_compl_le_bot := λ a, a.band_bnot_self.le, top_le_sup_compl := λ a, a.bor_bnot_self.ge, ..bool.linear_order, ..bool.bounded_order } @[simp] lemma bool.sup_eq_bor : (⊔) = bor := rfl @[simp] lemma bool.inf_eq_band : (⊓) = band := rfl @[simp] lemma bool.compl_eq_bnot : has_compl.compl = bnot := rfl section lift /-- Pullback a `generalized_boolean_algebra` along an injection. -/ @[reducible] -- See note [reducible non-instances] protected def function.injective.generalized_boolean_algebra [has_sup α] [has_inf α] [has_bot α] [has_sdiff α] [generalized_boolean_algebra β] (f : α → β) (hf : injective f) (map_sup : ∀ a b, f (a ⊔ b) = f a ⊔ f b) (map_inf : ∀ a b, f (a ⊓ b) = f a ⊓ f b) (map_bot : f ⊥ = ⊥) (map_sdiff : ∀ a b, f (a \ b) = f a \ f b) : generalized_boolean_algebra α := { sup_inf_sdiff := λ a b, hf $ by erw [map_sup, map_sdiff, map_inf, sup_inf_sdiff], inf_inf_sdiff := λ a b, hf $ by erw [map_inf, map_sdiff, map_inf, inf_inf_sdiff, map_bot], ..hf.generalized_coheyting_algebra f map_sup map_inf map_bot map_sdiff, ..hf.distrib_lattice f map_sup map_inf } /-- Pullback a `boolean_algebra` along an injection. -/ @[reducible] -- See note [reducible non-instances] protected def function.injective.boolean_algebra [has_sup α] [has_inf α] [has_top α] [has_bot α] [has_compl α] [has_sdiff α] [boolean_algebra β] (f : α → β) (hf : injective f) (map_sup : ∀ a b, f (a ⊔ b) = f a ⊔ f b) (map_inf : ∀ a b, f (a ⊓ b) = f a ⊓ f b) (map_top : f ⊤ = ⊤) (map_bot : f ⊥ = ⊥) (map_compl : ∀ a, f aᶜ = (f a)ᶜ) (map_sdiff : ∀ a b, f (a \ b) = f a \ f b) : boolean_algebra α := { compl := compl, top := ⊤, le_top := λ a, (@le_top β _ _ _).trans map_top.ge, bot_le := λ a, map_bot.le.trans bot_le, inf_compl_le_bot := λ a, ((map_inf _ _).trans $ by rw [map_compl, inf_compl_eq_bot, map_bot]).le, top_le_sup_compl := λ a, ((map_sup _ _).trans $ by rw [map_compl, sup_compl_eq_top, map_top]).ge, sdiff_eq := λ a b, hf $ (map_sdiff _ _).trans $ sdiff_eq.trans $ by { convert (map_inf _ _).symm, exact (map_compl _).symm }, ..hf.generalized_boolean_algebra f map_sup map_inf map_bot map_sdiff } end lift instance : boolean_algebra punit := by refine_struct { ..punit.biheyting_algebra }; intros; trivial <|> exact subsingleton.elim _ _
21920eb4c9c95a0df7f67aad1d952cc52a66517a
968e2f50b755d3048175f176376eff7139e9df70
/examples/pred_logic/unnamed_155.lean
ca998baa21591d1e9db3db688038da8e9529a5c4
[]
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
399
lean
def avg_three (x y z : ℤ) : ℤ := (x + y + z)/2 #check avg_three -- `avg_three` has type `ℤ → ℤ → ℤ → ℤ`, i.e., `ℤ → (ℤ → (ℤ → ℤ))` #check (avg_three 10) -- `avg_three 10` has type `ℤ → (ℤ → ℤ)`. #check (avg_three 10 5) -- `avg_three 10 5` has type `ℤ → ℤ`. #check (avg_three 10 5 6) -- `avg_three 10 5 6` has type `ℤ`, i.e. is an integer.
7cda00d023a8a85d154dde93f389aa0ab96ef197
b0d97c09d47e3b0a68128c64cad26ab132d23108
/src/compcont/ex5.lean
cddaa6bd10e5b489c47cc65dc11c66c022b5f3dd
[]
no_license
jmvlangen/RIP-seminar
2ad32c1c6fceb59ac7ae3b2251baf08f4c0c0caa
ed6771404dd4bcec298de2dfdc89d5e9cfd331bb
refs/heads/master
1,585,865,817,237
1,546,865,823,000
1,546,865,823,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,227
lean
import analysis.topology.topological_space import analysis.topology.continuity import analysis.topology.topological_structures import ex4 analysis.normed_space open tactic expr function has_add variables {X : Type*} {Y : Type*} {Z : Type*} {W : Type*} {f f₁ f₂ : X → Y} {g : Y → Z} {h : Z → W} variables [topological_space X] [topological_space Y] [topological_space Z] [topological_space W] [ring X] [topological_ring X] declare_trace elementary lemma continuous_subs {X : Type*} [topological_space X] [add_group X] [topological_add_group X] : continuous (λ p : X × X, p.1 - p.2) := sorry lemma continuous_smul' {E : Type*} {k : Type*} [normed_field k] [normed_space k E] : continuous ((λ p , p.fst • p.snd) : (k × E → E)) := sorry meta def elementary : tactic unit := do `(continuous %%e) ← target, (when_tracing `elementary (do trace e, trace " elementary trying statements")), (do tgt : expr ← target, to_expr ``(continuous_id : %%tgt) >>= exact <|> to_expr ``(continuous_const : %%tgt) >>= exact <|> to_expr ``(topological_add_monoid.continuous_add _ : %%tgt) >>= exact <|> to_expr ``(topological_group.continuous_inv _ : %%tgt) >>= exact <|> to_expr ``(topological_add_group.continuous_neg _ : %%tgt) >>= exact <|> to_expr ``(topological_monoid.continuous_mul _ : %%tgt) >>= exact <|> to_expr ``(continuous_subs : %%tgt) >>= exact <|> to_expr ``(continuous_smul' : %%tgt) >>= exact ) <|> ((when_tracing `elementary $ (tactic.trace " elementary trying assumption")); assumption) <|> ((when_tracing `elementary $ (tactic.trace " elementary trying contcomp3")); contcomp3; elementary) <|> ((when_tracing `elementary $ (tactic.trace " elementary trying contprod")); contprod; elementary) <|> skip example : continuous ( λ x : X, x + 1 + x) := by elementary example {E : Type*} {k : Type*} [normed_field k] [normed_space ℝ E] (L₁ : E → E) (L₂ : E → E) (ψ₁ : E → E) (ψ₂ : E → E) (_ : continuous ψ₁) (_ : continuous ψ₂) (v : E) : continuous (λ t : ℝ, L₁ v - L₂ v + ∥v∥ • (ψ₁ (t • v) - ψ₂ (t • v))) := by sorry
ee662f0af6cdf9c443459c060fe80eaa4572a321
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/linear_algebra/finite_dimensional.lean
936182bdee16463dcd530f17fa48dc2b2cd0eb68
[ "Apache-2.0" ]
permissive
ilitzroth/mathlib
ea647e67f1fdfd19a0f7bdc5504e8acec6180011
5254ef14e3465f6504306132fe3ba9cec9ffff16
refs/heads/master
1,680,086,661,182
1,617,715,647,000
1,617,715,647,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
57,720
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import linear_algebra.dimension import ring_theory.principal_ideal_domain import algebra.algebra.subalgebra /-! # Finite dimensional vector spaces Definition and basic properties of finite dimensional vector spaces, of their dimensions, and of linear maps on such spaces. ## Main definitions Assume `V` is a vector space over a field `K`. There are (at least) three equivalent definitions of finite-dimensionality of `V`: - it admits a finite basis. - it is finitely generated. - it is noetherian, i.e., every subspace is finitely generated. We introduce a typeclass `finite_dimensional K V` capturing this property. For ease of transfer of proof, it is defined using the third point of view, i.e., as `is_noetherian`. However, we prove that all these points of view are equivalent, with the following lemmas (in the namespace `finite_dimensional`): - `exists_is_basis_finite` states that a finite-dimensional vector space has a finite basis - `of_fintype_basis` states that the existence of a basis indexed by a finite type implies finite-dimensionality - `of_finset_basis` states that the existence of a basis indexed by a `finset` implies finite-dimensionality - `of_finite_basis` states that the existence of a basis indexed by a finite set implies finite-dimensionality - `iff_fg` states that the space is finite-dimensional if and only if it is finitely generated Also defined is `findim`, the dimension of a finite dimensional space, returning a `nat`, as opposed to `dim`, which returns a `cardinal`. When the space has infinite dimension, its `findim` is by convention set to `0`. Preservation of finite-dimensionality and formulas for the dimension are given for - submodules - quotients (for the dimension of a quotient, see `findim_quotient_add_findim`) - linear equivs, in `linear_equiv.finite_dimensional` and `linear_equiv.findim_eq` - image under a linear map (the rank-nullity formula is in `findim_range_add_findim_ker`) Basic properties of linear maps of a finite-dimensional vector space are given. Notably, the equivalence of injectivity and surjectivity is proved in `linear_map.injective_iff_surjective`, and the equivalence between left-inverse and right-inverse in `mul_eq_one_comm` and `comp_eq_id_comm`. ## Implementation notes Most results are deduced from the corresponding results for the general dimension (as a cardinal), in `dimension.lean`. Not all results have been ported yet. One of the characterizations of finite-dimensionality is in terms of finite generation. This property is currently defined only for submodules, so we express it through the fact that the maximal submodule (which, as a set, coincides with the whole space) is finitely generated. This is not very convenient to use, although there are some helper functions. However, this becomes very convenient when speaking of submodules which are finite-dimensional, as this notion coincides with the fact that the submodule is finitely generated (as a submodule of the whole space). This equivalence is proved in `submodule.fg_iff_finite_dimensional`. -/ universes u v v' w open_locale classical open vector_space cardinal submodule module function variables {K : Type u} {V : Type v} [field K] [add_comm_group V] [vector_space K V] {V₂ : Type v'} [add_comm_group V₂] [vector_space K V₂] /-- `finite_dimensional` vector spaces are defined to be noetherian modules. Use `finite_dimensional.iff_fg` or `finite_dimensional.of_fintype_basis` to prove finite dimension from a conventional definition. -/ @[reducible] def finite_dimensional (K V : Type*) [field K] [add_comm_group V] [vector_space K V] := is_noetherian K V namespace finite_dimensional open is_noetherian /-- A vector space is finite-dimensional if and only if its dimension (as a cardinal) is strictly less than the first infinite cardinal `omega`. -/ lemma finite_dimensional_iff_dim_lt_omega : finite_dimensional K V ↔ dim K V < omega.{v} := begin cases exists_is_basis K V with b hb, have := is_basis.mk_eq_dim hb, simp only [lift_id] at this, rw [← this, lt_omega_iff_fintype, ← @set.set_of_mem_eq _ b, ← subtype.range_coe_subtype], split, { intro, resetI, convert finite_of_linear_independent hb.1, simp }, { assume hbfinite, refine @is_noetherian_of_linear_equiv K (⊤ : submodule K V) V _ _ _ _ _ (linear_equiv.of_top _ rfl) (id _), refine is_noetherian_of_fg_of_noetherian _ ⟨set.finite.to_finset hbfinite, _⟩, rw [set.finite.coe_to_finset, ← hb.2], refl } end /-- The dimension of a finite-dimensional vector space, as a cardinal, is strictly less than the first infinite cardinal `omega`. -/ lemma dim_lt_omega (K V : Type*) [field K] [add_comm_group V] [vector_space K V] : ∀ [finite_dimensional K V], dim K V < omega.{v} := finite_dimensional_iff_dim_lt_omega.1 /-- In a finite dimensional space, there exists a finite basis. A basis is in general given as a function from an arbitrary type to the vector space. Here, we think of a basis as a set (instead of a function), and use as parametrizing type this set (and as a function the coercion `coe : s → V`). -/ variables (K V) lemma exists_is_basis_finite [finite_dimensional K V] : ∃ s : set V, (is_basis K (coe : s → V)) ∧ s.finite := begin cases exists_is_basis K V with s hs, exact ⟨s, hs, finite_of_linear_independent hs.1⟩ end /-- In a finite dimensional space, there exists a finite basis. Provides the basis as a finset. This is in contrast to `exists_is_basis_finite`, which provides a set and a `set.finite`. -/ lemma exists_is_basis_finset [finite_dimensional K V] : ∃ b : finset V, is_basis K (coe : (↑b : set V) → V) := begin obtain ⟨s, s_basis, s_finite⟩ := exists_is_basis_finite K V, refine ⟨s_finite.to_finset, _⟩, rw set.finite.coe_to_finset, exact s_basis, end /-- A finite dimensional vector space over a finite field is finite -/ noncomputable def fintype_of_fintype [fintype K] [finite_dimensional K V] : fintype V := module.fintype_of_fintype (classical.some_spec (finite_dimensional.exists_is_basis_finset K V) : _) variables {K V} /-- A vector space is finite-dimensional if and only if it is finitely generated. As the finitely-generated property is a property of submodules, we formulate this in terms of the maximal submodule, equal to the whole space as a set by definition.-/ lemma iff_fg : finite_dimensional K V ↔ (⊤ : submodule K V).fg := begin split, { introI h, rcases exists_is_basis_finite K V with ⟨s, s_basis, s_finite⟩, exact ⟨s_finite.to_finset, by { convert s_basis.2, simp }⟩ }, { rintros ⟨s, hs⟩, rw [finite_dimensional_iff_dim_lt_omega, ← dim_top, ← hs], exact lt_of_le_of_lt (dim_span_le _) (lt_omega_iff_finite.2 (set.finite_mem_finset s)) } end /-- If a vector space has a finite basis, then it is finite-dimensional. -/ lemma of_fintype_basis {ι : Type w} [fintype ι] {b : ι → V} (h : is_basis K b) : finite_dimensional K V := iff_fg.2 $ ⟨finset.univ.image b, by {convert h.2, simp} ⟩ /-- If a vector space has a basis indexed by elements of a finite set, then it is finite-dimensional. -/ lemma of_finite_basis {ι} {s : set ι} {b : s → V} (h : is_basis K b) (hs : set.finite s) : finite_dimensional K V := by haveI := hs.fintype; exact of_fintype_basis h /-- If a vector space has a finite basis, then it is finite-dimensional, finset style. -/ lemma of_finset_basis {ι} {s : finset ι} {b : (↑s : set ι) → V} (h : is_basis K b) : finite_dimensional K V := of_finite_basis h s.finite_to_set /-- A subspace of a finite-dimensional space is also finite-dimensional. -/ instance finite_dimensional_submodule [finite_dimensional K V] (S : submodule K V) : finite_dimensional K S := finite_dimensional_iff_dim_lt_omega.2 (lt_of_le_of_lt (dim_submodule_le _) (dim_lt_omega K V)) /-- A quotient of a finite-dimensional space is also finite-dimensional. -/ instance finite_dimensional_quotient [finite_dimensional K V] (S : submodule K V) : finite_dimensional K (quotient S) := finite_dimensional_iff_dim_lt_omega.2 (lt_of_le_of_lt (dim_quotient_le _) (dim_lt_omega K V)) /-- The dimension of a finite-dimensional vector space as a natural number. Defined by convention to be `0` if the space is infinite-dimensional. -/ noncomputable def findim (K V : Type*) [field K] [add_comm_group V] [vector_space K V] : ℕ := (dim K V).to_nat /-- In a finite-dimensional space, its dimension (seen as a cardinal) coincides with its `findim`. -/ lemma findim_eq_dim (K : Type u) (V : Type v) [field K] [add_comm_group V] [vector_space K V] [finite_dimensional K V] : (findim K V : cardinal.{v}) = dim K V := by rw [findim, cast_to_nat_of_lt_omega (dim_lt_omega K V)] lemma findim_eq_of_dim_eq {n : ℕ} (h : dim K V = ↑ n) : findim K V = n := begin apply_fun to_nat at h, rw to_nat_cast at h, exact_mod_cast h, end lemma findim_of_infinite_dimensional {K V : Type*} [field K] [add_comm_group V] [vector_space K V] (h : ¬finite_dimensional K V) : findim K V = 0 := dif_neg $ mt finite_dimensional_iff_dim_lt_omega.2 h lemma finite_dimensional_of_findim {K V : Type*} [field K] [add_comm_group V] [vector_space K V] (h : 0 < findim K V) : finite_dimensional K V := by { contrapose h, simp [findim_of_infinite_dimensional h] } /-- We can infer `finite_dimensional K V` in the presence of `[fact (findim K V = n + 1)]`. Declare this as a local instance where needed. -/ lemma finite_dimensional_of_findim_eq_succ {K V : Type*} [field K] [add_comm_group V] [vector_space K V] (n : ℕ) [fact (findim K V = n + 1)] : finite_dimensional K V := finite_dimensional_of_findim $ by convert nat.succ_pos n; apply fact.out /-- If a vector space has a finite basis, then its dimension (seen as a cardinal) is equal to the cardinality of the basis. -/ lemma dim_eq_card_basis {ι : Type w} [fintype ι] {b : ι → V} (h : is_basis K b) : dim K V = fintype.card ι := by rw [←h.mk_range_eq_dim, cardinal.fintype_card, set.card_range_of_injective h.injective] /-- If a vector space has a finite basis, then its dimension is equal to the cardinality of the basis. -/ lemma findim_eq_card_basis {ι : Type w} [fintype ι] {b : ι → V} (h : is_basis K b) : findim K V = fintype.card ι := begin haveI : finite_dimensional K V := of_fintype_basis h, have := dim_eq_card_basis h, rw ← findim_eq_dim at this, exact_mod_cast this end /-- If a vector space is finite-dimensional, then the cardinality of any basis is equal to its `findim`. -/ lemma findim_eq_card_basis' [finite_dimensional K V] {ι : Type w} {b : ι → V} (h : is_basis K b) : (findim K V : cardinal.{w}) = cardinal.mk ι := begin rcases exists_is_basis_finite K V with ⟨s, s_basis, s_finite⟩, letI: fintype s := s_finite.fintype, have A : cardinal.mk s = fintype.card s := fintype_card _, have B : findim K V = fintype.card s := findim_eq_card_basis s_basis, have C : cardinal.lift.{w v} (cardinal.mk ι) = cardinal.lift.{v w} (cardinal.mk s) := mk_eq_mk_of_basis h s_basis, rw [A, ← B, lift_nat_cast] at C, have : cardinal.lift.{w v} (cardinal.mk ι) = cardinal.lift.{w v} (findim K V), by { simp, exact C }, exact (lift_inj.mp this).symm end /-- If a vector space has a finite basis, then its dimension is equal to the cardinality of the basis. This lemma uses a `finset` instead of indexed types. -/ lemma findim_eq_card_finset_basis {b : finset V} (h : is_basis K (subtype.val : (↑b : set V) -> V)) : findim K V = finset.card b := by { rw [findim_eq_card_basis h, fintype.subtype_card], intros x, refl } lemma equiv_fin {ι : Type*} [finite_dimensional K V] {v : ι → V} (hv : is_basis K v) : ∃ g : fin (findim K V) ≃ ι, is_basis K (v ∘ g) := begin have : (cardinal.mk (fin $ findim K V)).lift = (cardinal.mk ι).lift, { simp [cardinal.mk_fin (findim K V), ← findim_eq_card_basis' hv] }, rcases cardinal.lift_mk_eq.mp this with ⟨g⟩, exact ⟨g, hv.comp _ g.bijective⟩ end lemma equiv_fin_of_dim_eq {ι : Type*} [finite_dimensional K V] {n : ℕ} (hn : findim K V = n) {v : ι → V} (hv : is_basis K v) : ∃ g : fin n ≃ ι, is_basis K (v ∘ g) := let ⟨g₁, hg₁⟩ := equiv_fin hv, ⟨g₂⟩ := fin.equiv_iff_eq.mpr hn in ⟨g₂.symm.trans g₁, hv.comp _ (g₂.symm.trans g₁).bijective⟩ variables (K V) lemma fin_basis [finite_dimensional K V] : ∃ v : fin (findim K V) → V, is_basis K v := let ⟨B, hB, B_fin⟩ := exists_is_basis_finite K V, ⟨g, hg⟩ := finite_dimensional.equiv_fin hB in ⟨coe ∘ g, hg⟩ variables {K V} lemma cardinal_mk_le_findim_of_linear_independent [finite_dimensional K V] {ι : Type w} {b : ι → V} (h : linear_independent K b) : cardinal.mk ι ≤ findim K V := begin rw ← lift_le.{_ (max v w)}, simpa [← findim_eq_dim K V] using cardinal_lift_le_dim_of_linear_independent.{_ _ _ (max v w)} h end lemma fintype_card_le_findim_of_linear_independent [finite_dimensional K V] {ι : Type*} [fintype ι] {b : ι → V} (h : linear_independent K b) : fintype.card ι ≤ findim K V := by simpa [fintype_card] using cardinal_mk_le_findim_of_linear_independent h lemma finset_card_le_findim_of_linear_independent [finite_dimensional K V] {b : finset V} (h : linear_independent K (λ x, x : (↑b : set V) → V)) : b.card ≤ findim K V := begin rw ←fintype.card_coe, exact fintype_card_le_findim_of_linear_independent h, end lemma lt_omega_of_linear_independent {ι : Type w} [finite_dimensional K V] {v : ι → V} (h : linear_independent K v) : cardinal.mk ι < cardinal.omega := begin apply cardinal.lift_lt.1, apply lt_of_le_of_lt, apply linear_independent_le_dim h, rw [←findim_eq_dim, cardinal.lift_omega, cardinal.lift_nat_cast], apply cardinal.nat_lt_omega, end lemma not_linear_independent_of_infinite {ι : Type w} [inf : infinite ι] [finite_dimensional K V] (v : ι → V) : ¬ linear_independent K v := begin intro h_lin_indep, have : ¬ omega ≤ mk ι := not_le.mpr (lt_omega_of_linear_independent h_lin_indep), have : omega ≤ mk ι := infinite_iff.mp inf, contradiction end /-- A finite dimensional space has positive `findim` iff it has a nonzero element. -/ lemma findim_pos_iff_exists_ne_zero [finite_dimensional K V] : 0 < findim K V ↔ ∃ x : V, x ≠ 0 := iff.trans (by { rw ← findim_eq_dim, norm_cast }) (@dim_pos_iff_exists_ne_zero K V _ _ _) /-- A finite dimensional space has positive `findim` iff it is nontrivial. -/ lemma findim_pos_iff [finite_dimensional K V] : 0 < findim K V ↔ nontrivial V := iff.trans (by { rw ← findim_eq_dim, norm_cast }) (@dim_pos_iff_nontrivial K V _ _ _) /-- A nontrivial finite dimensional space has positive `findim`. -/ lemma findim_pos [finite_dimensional K V] [h : nontrivial V] : 0 < findim K V := findim_pos_iff.mpr h /-- A finite dimensional space has zero `findim` iff it is a subsingleton. This is the `findim` version of `dim_zero_iff`. -/ lemma findim_zero_iff [finite_dimensional K V] : findim K V = 0 ↔ subsingleton V := iff.trans (by { rw ← findim_eq_dim, norm_cast }) (@dim_zero_iff K V _ _ _) /-- A finite dimensional space that is a subsingleton has zero `findim`. -/ lemma findim_zero_of_subsingleton [finite_dimensional K V] [h : subsingleton V] : findim K V = 0 := findim_zero_iff.2 h section open_locale big_operators open finset /-- If a finset has cardinality larger than the dimension of the space, then there is a nontrivial linear relation amongst its elements. -/ lemma exists_nontrivial_relation_of_dim_lt_card [finite_dimensional K V] {t : finset V} (h : findim K V < t.card) : ∃ f : V → K, ∑ e in t, f e • e = 0 ∧ ∃ x ∈ t, f x ≠ 0 := begin have := mt finset_card_le_findim_of_linear_independent (by { simpa using h }), rw linear_dependent_iff at this, obtain ⟨s, g, sum, z, zm, nonzero⟩ := this, -- Now we have to extend `g` to all of `t`, then to all of `V`. let f : V → K := λ x, if h : x ∈ t then if (⟨x, h⟩ : (↑t : set V)) ∈ s then g ⟨x, h⟩ else 0 else 0, -- and finally clean up the mess caused by the extension. refine ⟨f, _, _⟩, { dsimp [f], rw ← sum, fapply sum_bij_ne_zero (λ v hvt _, (⟨v, hvt⟩ : {v // v ∈ t})), { intros v hvt H, dsimp, rw [dif_pos hvt] at H, contrapose! H, rw [if_neg H, zero_smul], }, { intros _ _ _ _ _ _, exact subtype.mk.inj, }, { intros b hbs hb, use b, simpa only [hbs, exists_prop, dif_pos, finset.mk_coe, and_true, if_true, finset.coe_mem, eq_self_iff_true, exists_prop_of_true, ne.def] using hb, }, { intros a h₁, dsimp, rw [dif_pos h₁], intro h₂, rw [if_pos], contrapose! h₂, rw [if_neg h₂, zero_smul], }, }, { refine ⟨z, z.2, _⟩, dsimp only [f], erw [dif_pos z.2, if_pos]; rwa [subtype.coe_eta] }, end /-- If a finset has cardinality larger than `findim + 1`, then there is a nontrivial linear relation amongst its elements, such that the coefficients of the relation sum to zero. -/ lemma exists_nontrivial_relation_sum_zero_of_dim_succ_lt_card [finite_dimensional K V] {t : finset V} (h : findim K V + 1 < t.card) : ∃ f : V → K, ∑ e in t, f e • e = 0 ∧ ∑ e in t, f e = 0 ∧ ∃ x ∈ t, f x ≠ 0 := begin -- Pick an element x₀ ∈ t, have card_pos : 0 < t.card := lt_trans (nat.succ_pos _) h, obtain ⟨x₀, m⟩ := (finset.card_pos.1 card_pos).bex, -- and apply the previous lemma to the {xᵢ - x₀} let shift : V ↪ V := ⟨λ x, x - x₀, sub_left_injective⟩, let t' := (t.erase x₀).map shift, have h' : findim K V < t'.card, { simp only [t', card_map, finset.card_erase_of_mem m], exact nat.lt_pred_iff.mpr h, }, -- to obtain a function `g`. obtain ⟨g, gsum, x₁, x₁_mem, nz⟩ := exists_nontrivial_relation_of_dim_lt_card h', -- Then obtain `f` by translating back by `x₀`, -- and setting the value of `f` at `x₀` to ensure `∑ e in t, f e = 0`. let f : V → K := λ z, if z = x₀ then - ∑ z in (t.erase x₀), g (z - x₀) else g (z - x₀), refine ⟨f, _ ,_ ,_⟩, -- After this, it's a matter of verifiying the properties, -- based on the corresponding properties for `g`. { show ∑ (e : V) in t, f e • e = 0, -- We prove this by splitting off the `x₀` term of the sum, -- which is itself a sum over `t.erase x₀`, -- combining the two sums, and -- observing that after reindexing we have exactly -- ∑ (x : V) in t', g x • x = 0. simp only [f], conv_lhs { apply_congr, skip, rw [ite_smul], }, rw [finset.sum_ite], conv { congr, congr, apply_congr, simp [filter_eq', m], }, conv { congr, congr, skip, apply_congr, simp [filter_ne'], }, rw [sum_singleton, neg_smul, add_comm, ←sub_eq_add_neg, sum_smul, ←sum_sub_distrib], simp only [←smul_sub], -- At the end we have to reindex the sum, so we use `change` to -- express the summand using `shift`. change (∑ (x : V) in t.erase x₀, (λ e, g e • e) (shift x)) = 0, rw ←sum_map _ shift, exact gsum, }, { show ∑ (e : V) in t, f e = 0, -- Again we split off the `x₀` term, -- observing that it exactly cancels the other terms. rw [← insert_erase m, sum_insert (not_mem_erase x₀ t)], dsimp [f], rw [if_pos rfl], conv_lhs { congr, skip, apply_congr, skip, rw if_neg (show x ≠ x₀, from (mem_erase.mp H).1), }, exact neg_add_self _, }, { show ∃ (x : V) (H : x ∈ t), f x ≠ 0, -- We can use x₁ + x₀. refine ⟨x₁ + x₀, _, _⟩, { rw finset.mem_map at x₁_mem, rcases x₁_mem with ⟨x₁, x₁_mem, rfl⟩, rw mem_erase at x₁_mem, simp only [x₁_mem, sub_add_cancel, function.embedding.coe_fn_mk], }, { dsimp only [f], rwa [if_neg, add_sub_cancel], rw [add_left_eq_self], rintro rfl, simpa only [sub_eq_zero, exists_prop, finset.mem_map, embedding.coe_fn_mk, eq_self_iff_true, mem_erase, not_true, exists_eq_right, ne.def, false_and] using x₁_mem, } }, end section variables {L : Type*} [linear_ordered_field L] variables {W : Type v} [add_comm_group W] [vector_space L W] /-- A slight strengthening of `exists_nontrivial_relation_sum_zero_of_dim_succ_lt_card` available when working over an ordered field: we can ensure a positive coefficient, not just a nonzero coefficient. -/ lemma exists_relation_sum_zero_pos_coefficient_of_dim_succ_lt_card [finite_dimensional L W] {t : finset W} (h : findim L W + 1 < t.card) : ∃ f : W → L, ∑ e in t, f e • e = 0 ∧ ∑ e in t, f e = 0 ∧ ∃ x ∈ t, 0 < f x := begin obtain ⟨f, sum, total, nonzero⟩ := exists_nontrivial_relation_sum_zero_of_dim_succ_lt_card h, exact ⟨f, sum, total, exists_pos_of_sum_zero_of_exists_nonzero f total nonzero⟩, end end end /-- If a submodule has maximal dimension in a finite dimensional space, then it is equal to the whole space. -/ lemma eq_top_of_findim_eq [finite_dimensional K V] {S : submodule K V} (h : findim K S = findim K V) : S = ⊤ := begin cases exists_is_basis K S with bS hbS, have : linear_independent K (subtype.val : (subtype.val '' bS : set V) → V), from @linear_independent.image_subtype _ _ _ _ _ _ _ _ _ (submodule.subtype S) hbS.1 (by simp), cases exists_subset_is_basis this with b hb, letI : fintype b := classical.choice (finite_of_linear_independent hb.2.1), letI : fintype (subtype.val '' bS) := classical.choice (finite_of_linear_independent this), letI : fintype bS := classical.choice (finite_of_linear_independent hbS.1), have : subtype.val '' bS = b, from set.eq_of_subset_of_card_le hb.1 (by rw [set.card_image_of_injective _ subtype.val_injective, ← findim_eq_card_basis hbS, ← findim_eq_card_basis hb.2, h]; apply_instance), erw [← hb.2.2, subtype.range_coe, ← this, ← subtype_eq_val, span_image], have := hbS.2, erw [subtype.range_coe] at this, rw [this, map_top (submodule.subtype S), range_subtype], end variable (K) /-- A field is one-dimensional as a vector space over itself. -/ @[simp] lemma findim_of_field : findim K K = 1 := begin have := dim_of_field K, rw [← findim_eq_dim] at this, exact_mod_cast this end instance finite_dimensional_self : finite_dimensional K K := by apply_instance /-- The vector space of functions on a fintype ι has findim equal to the cardinality of ι. -/ @[simp] lemma findim_fintype_fun_eq_card {ι : Type v} [fintype ι] : findim K (ι → K) = fintype.card ι := begin have : vector_space.dim K (ι → K) = fintype.card ι := dim_fun', rwa [← findim_eq_dim, nat_cast_inj] at this, end /-- The vector space of functions on `fin n` has findim equal to `n`. -/ @[simp] lemma findim_fin_fun {n : ℕ} : findim K (fin n → K) = n := by simp /-- The submodule generated by a finite set is finite-dimensional. -/ theorem span_of_finite {A : set V} (hA : set.finite A) : finite_dimensional K (submodule.span K A) := is_noetherian_span_of_finite K hA /-- The submodule generated by a single element is finite-dimensional. -/ instance (x : V) : finite_dimensional K (K ∙ x) := by {apply span_of_finite, simp} end finite_dimensional section zero_dim open vector_space finite_dimensional lemma finite_dimensional_of_dim_eq_zero (h : vector_space.dim K V = 0) : finite_dimensional K V := by rw [finite_dimensional_iff_dim_lt_omega, h]; exact cardinal.omega_pos lemma finite_dimensional_of_dim_eq_one (h : vector_space.dim K V = 1) : finite_dimensional K V := by rw [finite_dimensional_iff_dim_lt_omega, h]; exact one_lt_omega lemma findim_eq_zero_of_dim_eq_zero [finite_dimensional K V] (h : vector_space.dim K V = 0) : findim K V = 0 := begin convert findim_eq_dim K V, rw h, norm_cast end lemma findim_eq_zero_of_not_exists_basis (h : ¬ ∃ s : finset V, is_basis K (λ x, x : (↑s : set V) → V)) : findim K V = 0 := dif_neg (mt (λ h, @exists_is_basis_finset K V _ _ _ (finite_dimensional_iff_dim_lt_omega.mpr h)) h) variables (K V) lemma finite_dimensional_bot : finite_dimensional K (⊥ : submodule K V) := finite_dimensional_of_dim_eq_zero $ by simp @[simp] lemma findim_bot : findim K (⊥ : submodule K V) = 0 := begin haveI := finite_dimensional_bot K V, convert findim_eq_dim K (⊥ : submodule K V), rw dim_bot, norm_cast end variables {K V} lemma bot_eq_top_of_dim_eq_zero (h : vector_space.dim K V = 0) : (⊥ : submodule K V) = ⊤ := begin haveI := finite_dimensional_of_dim_eq_zero h, apply eq_top_of_findim_eq, rw [findim_bot, findim_eq_zero_of_dim_eq_zero h] end @[simp] theorem dim_eq_zero {S : submodule K V} : dim K S = 0 ↔ S = ⊥ := ⟨λ h, (submodule.eq_bot_iff _).2 $ λ x hx, congr_arg subtype.val $ ((submodule.eq_bot_iff _).1 $ eq.symm $ bot_eq_top_of_dim_eq_zero h) ⟨x, hx⟩ submodule.mem_top, λ h, by rw [h, dim_bot]⟩ @[simp] theorem findim_eq_zero {S : submodule K V} [finite_dimensional K S] : findim K S = 0 ↔ S = ⊥ := by rw [← dim_eq_zero, ← findim_eq_dim, ← @nat.cast_zero cardinal, cardinal.nat_cast_inj] end zero_dim namespace submodule open finite_dimensional /-- A submodule is finitely generated if and only if it is finite-dimensional -/ theorem fg_iff_finite_dimensional (s : submodule K V) : s.fg ↔ finite_dimensional K s := ⟨λh, is_noetherian_of_fg_of_noetherian s h, λh, by { rw ← map_subtype_top s, exact fg_map (iff_fg.1 h) }⟩ /-- A submodule contained in a finite-dimensional submodule is finite-dimensional. -/ lemma finite_dimensional_of_le {S₁ S₂ : submodule K V} [finite_dimensional K S₂] (h : S₁ ≤ S₂) : finite_dimensional K S₁ := finite_dimensional_iff_dim_lt_omega.2 (lt_of_le_of_lt (dim_le_of_submodule _ _ h) (dim_lt_omega K S₂)) /-- The inf of two submodules, the first finite-dimensional, is finite-dimensional. -/ instance finite_dimensional_inf_left (S₁ S₂ : submodule K V) [finite_dimensional K S₁] : finite_dimensional K (S₁ ⊓ S₂ : submodule K V) := finite_dimensional_of_le inf_le_left /-- The inf of two submodules, the second finite-dimensional, is finite-dimensional. -/ instance finite_dimensional_inf_right (S₁ S₂ : submodule K V) [finite_dimensional K S₂] : finite_dimensional K (S₁ ⊓ S₂ : submodule K V) := finite_dimensional_of_le inf_le_right /-- The sup of two finite-dimensional submodules is finite-dimensional. -/ instance finite_dimensional_sup (S₁ S₂ : submodule K V) [h₁ : finite_dimensional K S₁] [h₂ : finite_dimensional K S₂] : finite_dimensional K (S₁ ⊔ S₂ : submodule K V) := begin rw ←submodule.fg_iff_finite_dimensional at *, exact submodule.fg_sup h₁ h₂ end /-- In a finite-dimensional vector space, the dimensions of a submodule and of the corresponding quotient add up to the dimension of the space. -/ theorem findim_quotient_add_findim [finite_dimensional K V] (s : submodule K V) : findim K s.quotient + findim K s = findim K V := begin have := dim_quotient_add_dim s, rw [← findim_eq_dim, ← findim_eq_dim, ← findim_eq_dim] at this, exact_mod_cast this end /-- The dimension of a submodule is bounded by the dimension of the ambient space. -/ lemma findim_le [finite_dimensional K V] (s : submodule K V) : findim K s ≤ findim K V := by { rw ← s.findim_quotient_add_findim, exact nat.le_add_left _ _ } /-- The dimension of a strict submodule is strictly bounded by the dimension of the ambient space. -/ lemma findim_lt [finite_dimensional K V] {s : submodule K V} (h : s < ⊤) : findim K s < findim K V := begin rw [← s.findim_quotient_add_findim, add_comm], exact nat.lt_add_of_zero_lt_left _ _ (findim_pos_iff.mpr (quotient.nontrivial_of_lt_top _ h)) end /-- The dimension of a quotient is bounded by the dimension of the ambient space. -/ lemma findim_quotient_le [finite_dimensional K V] (s : submodule K V) : findim K s.quotient ≤ findim K V := by { rw ← s.findim_quotient_add_findim, exact nat.le_add_right _ _ } /-- The sum of the dimensions of s + t and s ∩ t is the sum of the dimensions of s and t -/ theorem dim_sup_add_dim_inf_eq (s t : submodule K V) [finite_dimensional K s] [finite_dimensional K t] : findim K ↥(s ⊔ t) + findim K ↥(s ⊓ t) = findim K ↥s + findim K ↥t := begin have key : dim K ↥(s ⊔ t) + dim K ↥(s ⊓ t) = dim K s + dim K t := dim_sup_add_dim_inf_eq s t, repeat { rw ←findim_eq_dim at key }, norm_cast at key, exact key end lemma eq_top_of_disjoint [finite_dimensional K V] (s t : submodule K V) (hdim : findim K s + findim K t = findim K V) (hdisjoint : disjoint s t) : s ⊔ t = ⊤ := begin have h_findim_inf : findim K ↥(s ⊓ t) = 0, { rw [disjoint, le_bot_iff] at hdisjoint, rw [hdisjoint, findim_bot] }, apply eq_top_of_findim_eq, rw ←hdim, convert s.dim_sup_add_dim_inf_eq t, rw h_findim_inf, refl, end end submodule namespace linear_equiv open finite_dimensional /-- Finite dimensionality is preserved under linear equivalence. -/ protected theorem finite_dimensional (f : V ≃ₗ[K] V₂) [finite_dimensional K V] : finite_dimensional K V₂ := is_noetherian_of_linear_equiv f /-- The dimension of a finite dimensional space is preserved under linear equivalence. -/ theorem findim_eq (f : V ≃ₗ[K] V₂) [finite_dimensional K V] : findim K V = findim K V₂ := begin haveI : finite_dimensional K V₂ := f.finite_dimensional, simpa [← findim_eq_dim] using f.lift_dim_eq end end linear_equiv namespace finite_dimensional /-- Two finite-dimensional vector spaces are isomorphic if they have the same (finite) dimension. -/ theorem nonempty_linear_equiv_of_findim_eq [finite_dimensional K V] [finite_dimensional K V₂] (cond : findim K V = findim K V₂) : nonempty (V ≃ₗ[K] V₂) := nonempty_linear_equiv_of_lift_dim_eq $ by simp only [← findim_eq_dim, cond, lift_nat_cast] /-- Two finite-dimensional vector spaces are isomorphic if and only if they have the same (finite) dimension. -/ theorem nonempty_linear_equiv_iff_findim_eq [finite_dimensional K V] [finite_dimensional K V₂] : nonempty (V ≃ₗ[K] V₂) ↔ findim K V = findim K V₂ := ⟨λ ⟨h⟩, h.findim_eq, λ h, nonempty_linear_equiv_of_findim_eq h⟩ section variables (V V₂) /-- Two finite-dimensional vector spaces are isomorphic if they have the same (finite) dimension. -/ noncomputable def linear_equiv.of_findim_eq [finite_dimensional K V] [finite_dimensional K V₂] (cond : findim K V = findim K V₂) : V ≃ₗ[K] V₂ := classical.choice $ nonempty_linear_equiv_of_findim_eq cond end lemma eq_of_le_of_findim_le {S₁ S₂ : submodule K V} [finite_dimensional K S₂] (hle : S₁ ≤ S₂) (hd : findim K S₂ ≤ findim K S₁) : S₁ = S₂ := begin rw ←linear_equiv.findim_eq (submodule.comap_subtype_equiv_of_le hle) at hd, exact le_antisymm hle (submodule.comap_subtype_eq_top.1 (eq_top_of_findim_eq (le_antisymm (comap (submodule.subtype S₂) S₁).findim_le hd))), end /-- If a submodule is less than or equal to a finite-dimensional submodule with the same dimension, they are equal. -/ lemma eq_of_le_of_findim_eq {S₁ S₂ : submodule K V} [finite_dimensional K S₂] (hle : S₁ ≤ S₂) (hd : findim K S₁ = findim K S₂) : S₁ = S₂ := eq_of_le_of_findim_le hle hd.ge variables [finite_dimensional K V] [finite_dimensional K V₂] /-- Given isomorphic subspaces `p q` of vector spaces `V` and `V₁` respectively, `p.quotient` is isomorphic to `q.quotient`. -/ noncomputable def linear_equiv.quot_equiv_of_equiv {p : subspace K V} {q : subspace K V₂} (f₁ : p ≃ₗ[K] q) (f₂ : V ≃ₗ[K] V₂) : p.quotient ≃ₗ[K] q.quotient := linear_equiv.of_findim_eq _ _ begin rw [← @add_right_cancel_iff _ _ (findim K p), submodule.findim_quotient_add_findim, linear_equiv.findim_eq f₁, submodule.findim_quotient_add_findim, linear_equiv.findim_eq f₂], end /-- Given the subspaces `p q`, if `p.quotient ≃ₗ[K] q`, then `q.quotient ≃ₗ[K] p` -/ noncomputable def linear_equiv.quot_equiv_of_quot_equiv {p q : subspace K V} (f : p.quotient ≃ₗ[K] q) : q.quotient ≃ₗ[K] p := linear_equiv.of_findim_eq _ _ begin rw [← @add_right_cancel_iff _ _ (findim K q), submodule.findim_quotient_add_findim, ← linear_equiv.findim_eq f, add_comm, submodule.findim_quotient_add_findim] end @[simp] lemma findim_map_subtype_eq (p : subspace K V) (q : subspace K p) : finite_dimensional.findim K (q.map p.subtype) = finite_dimensional.findim K q := (submodule.equiv_subtype_map p q).symm.findim_eq end finite_dimensional namespace linear_map open finite_dimensional /-- On a finite-dimensional space, an injective linear map is surjective. -/ lemma surjective_of_injective [finite_dimensional K V] {f : V →ₗ[K] V} (hinj : injective f) : surjective f := begin have h := dim_eq_of_injective _ hinj, rw [← findim_eq_dim, ← findim_eq_dim, nat_cast_inj] at h, exact range_eq_top.1 (eq_top_of_findim_eq h.symm) end /-- On a finite-dimensional space, a linear map is injective if and only if it is surjective. -/ lemma injective_iff_surjective [finite_dimensional K V] {f : V →ₗ[K] V} : injective f ↔ surjective f := ⟨surjective_of_injective, λ hsurj, let ⟨g, hg⟩ := f.exists_right_inverse_of_surjective (range_eq_top.2 hsurj) in have function.right_inverse g f, from linear_map.ext_iff.1 hg, (left_inverse_of_surjective_of_right_inverse (surjective_of_injective this.injective) this).injective⟩ lemma ker_eq_bot_iff_range_eq_top [finite_dimensional K V] {f : V →ₗ[K] V} : f.ker = ⊥ ↔ f.range = ⊤ := by rw [range_eq_top, ker_eq_bot, injective_iff_surjective] /-- In a finite-dimensional space, if linear maps are inverse to each other on one side then they are also inverse to each other on the other side. -/ lemma mul_eq_one_of_mul_eq_one [finite_dimensional K V] {f g : V →ₗ[K] V} (hfg : f * g = 1) : g * f = 1 := have ginj : injective g, from has_left_inverse.injective ⟨f, (λ x, show (f * g) x = (1 : V →ₗ[K] V) x, by rw hfg; refl)⟩, let ⟨i, hi⟩ := g.exists_right_inverse_of_surjective (range_eq_top.2 (injective_iff_surjective.1 ginj)) in have f * (g * i) = f * 1, from congr_arg _ hi, by rw [← mul_assoc, hfg, one_mul, mul_one] at this; rwa ← this /-- In a finite-dimensional space, linear maps are inverse to each other on one side if and only if they are inverse to each other on the other side. -/ lemma mul_eq_one_comm [finite_dimensional K V] {f g : V →ₗ[K] V} : f * g = 1 ↔ g * f = 1 := ⟨mul_eq_one_of_mul_eq_one, mul_eq_one_of_mul_eq_one⟩ /-- In a finite-dimensional space, linear maps are inverse to each other on one side if and only if they are inverse to each other on the other side. -/ lemma comp_eq_id_comm [finite_dimensional K V] {f g : V →ₗ[K] V} : f.comp g = id ↔ g.comp f = id := mul_eq_one_comm /-- The image under an onto linear map of a finite-dimensional space is also finite-dimensional. -/ lemma finite_dimensional_of_surjective [h : finite_dimensional K V] (f : V →ₗ[K] V₂) (hf : f.range = ⊤) : finite_dimensional K V₂ := is_noetherian_of_surjective V f hf /-- The range of a linear map defined on a finite-dimensional space is also finite-dimensional. -/ instance finite_dimensional_range [h : finite_dimensional K V] (f : V →ₗ[K] V₂) : finite_dimensional K f.range := f.quot_ker_equiv_range.finite_dimensional /-- rank-nullity theorem : the dimensions of the kernel and the range of a linear map add up to the dimension of the source space. -/ theorem findim_range_add_findim_ker [finite_dimensional K V] (f : V →ₗ[K] V₂) : findim K f.range + findim K f.ker = findim K V := by { rw [← f.quot_ker_equiv_range.findim_eq], exact submodule.findim_quotient_add_findim _ } end linear_map namespace linear_equiv open finite_dimensional variables [finite_dimensional K V] /-- The linear equivalence corresponging to an injective endomorphism. -/ noncomputable def of_injective_endo (f : V →ₗ[K] V) (h_inj : f.ker = ⊥) : V ≃ₗ[K] V := (linear_equiv.of_injective f h_inj).trans (linear_equiv.of_top _ (linear_map.ker_eq_bot_iff_range_eq_top.1 h_inj)) @[simp] lemma coe_of_injective_endo (f : V →ₗ[K] V) (h_inj : f.ker = ⊥) : ⇑(of_injective_endo f h_inj) = f := rfl @[simp] lemma of_injective_endo_right_inv (f : V →ₗ[K] V) (h_inj : f.ker = ⊥) : f * (of_injective_endo f h_inj).symm = 1 := linear_map.ext $ (of_injective_endo f h_inj).apply_symm_apply @[simp] lemma of_injective_endo_left_inv (f : V →ₗ[K] V) (h_inj : f.ker = ⊥) : ((of_injective_endo f h_inj).symm : V →ₗ[K] V) * f = 1 := linear_map.ext $ (of_injective_endo f h_inj).symm_apply_apply end linear_equiv namespace linear_map lemma is_unit_iff [finite_dimensional K V] (f : V →ₗ[K] V): is_unit f ↔ f.ker = ⊥ := begin split, { rintro ⟨u, rfl⟩, exact linear_map.ker_eq_bot_of_inverse u.inv_mul }, { intro h_inj, exact ⟨⟨f, (linear_equiv.of_injective_endo f h_inj).symm.to_linear_map, linear_equiv.of_injective_endo_right_inv f h_inj, linear_equiv.of_injective_endo_left_inv f h_inj⟩, rfl⟩ } end end linear_map open vector_space finite_dimensional section top @[simp] theorem findim_top : findim K (⊤ : submodule K V) = findim K V := by { unfold findim, simp [dim_top] } end top lemma findim_zero_iff_forall_zero [finite_dimensional K V] : findim K V = 0 ↔ ∀ x : V, x = 0 := findim_zero_iff.trans (subsingleton_iff_forall_eq 0) lemma is_basis_of_findim_zero [finite_dimensional K V] {ι : Type*} (h : ¬ nonempty ι) (hV : findim K V = 0) : is_basis K (λ x : ι, (0 : V)) := begin haveI : subsingleton V := findim_zero_iff.1 hV, exact is_basis_empty _ h end lemma is_basis_of_findim_zero' [finite_dimensional K V] (hV : findim K V = 0) : is_basis K (λ x : fin 0, (0 : V)) := is_basis_of_findim_zero (finset.univ_eq_empty.mp rfl) hV namespace linear_map theorem injective_iff_surjective_of_findim_eq_findim [finite_dimensional K V] [finite_dimensional K V₂] (H : findim K V = findim K V₂) {f : V →ₗ[K] V₂} : function.injective f ↔ function.surjective f := begin have := findim_range_add_findim_ker f, rw [← ker_eq_bot, ← range_eq_top], refine ⟨λ h, _, λ h, _⟩, { rw [h, findim_bot, add_zero, H] at this, exact eq_top_of_findim_eq this }, { rw [h, findim_top, H] at this, exact findim_eq_zero.1 (add_right_injective _ this) } end lemma ker_eq_bot_iff_range_eq_top_of_findim_eq_findim [finite_dimensional K V] [finite_dimensional K V₂] (H : findim K V = findim K V₂) {f : V →ₗ[K] V₂} : f.ker = ⊥ ↔ f.range = ⊤ := by rw [range_eq_top, ker_eq_bot, injective_iff_surjective_of_findim_eq_findim H] theorem findim_le_findim_of_injective [finite_dimensional K V] [finite_dimensional K V₂] {f : V →ₗ[K] V₂} (hf : function.injective f) : findim K V ≤ findim K V₂ := calc findim K V = findim K f.range + findim K f.ker : (findim_range_add_findim_ker f).symm ... = findim K f.range : by rw [ker_eq_bot.2 hf, findim_bot, add_zero] ... ≤ findim K V₂ : submodule.findim_le _ /-- Given a linear map `f` between two vector spaces with the same dimension, if `ker f = ⊥` then `linear_equiv_of_ker_eq_bot` is the induced isomorphism between the two vector spaces. -/ noncomputable def linear_equiv_of_ker_eq_bot [finite_dimensional K V] [finite_dimensional K V₂] (f : V →ₗ[K] V₂) (hf : f.ker = ⊥) (hdim : findim K V = findim K V₂) : V ≃ₗ[K] V₂ := linear_equiv.of_bijective f hf (linear_map.range_eq_top.2 $ (linear_map.injective_iff_surjective_of_findim_eq_findim hdim).1 (linear_map.ker_eq_bot.1 hf)) @[simp] lemma linear_equiv_of_ker_eq_bot_apply [finite_dimensional K V] [finite_dimensional K V₂] {f : V →ₗ[K] V₂} (hf : f.ker = ⊥) (hdim : findim K V = findim K V₂) (x : V) : f.linear_equiv_of_ker_eq_bot hf hdim x = f x := rfl end linear_map namespace alg_hom lemma bijective {F : Type*} [field F] {E : Type*} [field E] [algebra F E] [finite_dimensional F E] (ϕ : E →ₐ[F] E) : function.bijective ϕ := have inj : function.injective ϕ.to_linear_map := ϕ.to_ring_hom.injective, ⟨inj, (linear_map.injective_iff_surjective_of_findim_eq_findim rfl).mp inj⟩ end alg_hom /-- Bijection between algebra equivalences and algebra homomorphisms -/ noncomputable def alg_equiv_equiv_alg_hom (F : Type u) [field F] (E : Type v) [field E] [algebra F E] [finite_dimensional F E] : (E ≃ₐ[F] E) ≃ (E →ₐ[F] E) := { to_fun := λ ϕ, ϕ.to_alg_hom, inv_fun := λ ϕ, alg_equiv.of_bijective ϕ ϕ.bijective, left_inv := λ _, by {ext, refl}, right_inv := λ _, by {ext, refl} } section /-- An integral domain that is module-finite as an algebra over a field is a field. -/ noncomputable def field_of_finite_dimensional (F K : Type*) [field F] [integral_domain K] [algebra F K] [finite_dimensional F K] : field K := { inv := λ x, if H : x = 0 then 0 else classical.some $ (show function.surjective (algebra.lmul_left F x), from linear_map.injective_iff_surjective.1 $ λ _ _, (mul_right_inj' H).1) 1, mul_inv_cancel := λ x hx, show x * dite _ _ _ = _, by { rw dif_neg hx, exact classical.some_spec ((show function.surjective (algebra.lmul_left F x), from linear_map.injective_iff_surjective.1 $ λ _ _, (mul_right_inj' hx).1) 1) }, inv_zero := dif_pos rfl, .. ‹integral_domain K› } end namespace submodule lemma findim_mono [finite_dimensional K V] : monotone (λ (s : submodule K V), findim K s) := λ s t hst, calc findim K s = findim K (comap t.subtype s) : linear_equiv.findim_eq (comap_subtype_equiv_of_le hst).symm ... ≤ findim K t : submodule.findim_le _ lemma lt_of_le_of_findim_lt_findim {s t : submodule K V} (le : s ≤ t) (lt : findim K s < findim K t) : s < t := lt_of_le_of_ne le (λ h, ne_of_lt lt (by rw h)) lemma lt_top_of_findim_lt_findim {s : submodule K V} (lt : findim K s < findim K V) : s < ⊤ := begin rw ← @findim_top K V at lt, exact lt_of_le_of_findim_lt_findim le_top lt end lemma findim_lt_findim_of_lt [finite_dimensional K V] {s t : submodule K V} (hst : s < t) : findim K s < findim K t := begin rw linear_equiv.findim_eq (comap_subtype_equiv_of_le (le_of_lt hst)).symm, refine findim_lt (lt_of_le_of_ne le_top _), intro h_eq_top, rw comap_subtype_eq_top at h_eq_top, apply not_le_of_lt hst h_eq_top, end lemma findim_add_eq_of_is_compl [finite_dimensional K V] {U W : submodule K V} (h : is_compl U W) : findim K U + findim K W = findim K V := begin rw [← submodule.dim_sup_add_dim_inf_eq, top_le_iff.1 h.2, le_bot_iff.1 h.1, findim_bot, add_zero], exact findim_top end end submodule section span open submodule lemma findim_span_le_card (s : set V) [fin : fintype s] : findim K (span K s) ≤ s.to_finset.card := begin haveI := span_of_finite K ⟨fin⟩, have : dim K (span K s) ≤ (mk s : cardinal) := dim_span_le s, rw [←findim_eq_dim, cardinal.fintype_card, ←set.to_finset_card] at this, exact_mod_cast this end lemma findim_span_eq_card {ι : Type*} [fintype ι] {b : ι → V} (hb : linear_independent K b) : findim K (span K (set.range b)) = fintype.card ι := begin haveI : finite_dimensional K (span K (set.range b)) := span_of_finite K (set.finite_range b), have : dim K (span K (set.range b)) = (mk (set.range b) : cardinal) := dim_span hb, rwa [←findim_eq_dim, ←lift_inj, mk_range_eq_of_injective hb.injective, cardinal.fintype_card, lift_nat_cast, lift_nat_cast, nat_cast_inj] at this, end lemma findim_span_set_eq_card (s : set V) [fin : fintype s] (hs : linear_independent K (coe : s → V)) : findim K (span K s) = s.to_finset.card := begin haveI := span_of_finite K ⟨fin⟩, have : dim K (span K s) = (mk s : cardinal) := dim_span_set hs, rw [←findim_eq_dim, cardinal.fintype_card, ←set.to_finset_card] at this, exact_mod_cast this end lemma span_lt_of_subset_of_card_lt_findim {s : set V} [fintype s] {t : submodule K V} (subset : s ⊆ t) (card_lt : s.to_finset.card < findim K t) : span K s < t := lt_of_le_of_findim_lt_findim (span_le.mpr subset) (lt_of_le_of_lt (findim_span_le_card _) card_lt) lemma span_lt_top_of_card_lt_findim {s : set V} [fintype s] (card_lt : s.to_finset.card < findim K V) : span K s < ⊤ := lt_top_of_findim_lt_findim (lt_of_le_of_lt (findim_span_le_card _) card_lt) lemma findim_span_singleton {v : V} (hv : v ≠ 0) : findim K (K ∙ v) = 1 := begin apply le_antisymm, { exact findim_span_le_card ({v} : set V) }, { rw [nat.succ_le_iff, findim_pos_iff], use [⟨v, mem_span_singleton_self v⟩, 0], simp [hv] } end end span section is_basis lemma linear_independent_of_span_eq_top_of_card_eq_findim {ι : Type*} [fintype ι] {b : ι → V} (span_eq : span K (set.range b) = ⊤) (card_eq : fintype.card ι = findim K V) : linear_independent K b := linear_independent_iff'.mpr $ λ s g dependent i i_mem_s, begin by_contra gx_ne_zero, -- We'll derive a contradiction by showing `b '' (univ \ {i})` of cardinality `n - 1` -- spans a vector space of dimension `n`. refine ne_of_lt (span_lt_top_of_card_lt_findim (show (b '' (set.univ \ {i})).to_finset.card < findim K V, from _)) _, { calc (b '' (set.univ \ {i})).to_finset.card = ((set.univ \ {i}).to_finset.image b).card : by rw [set.to_finset_card, fintype.card_of_finset] ... ≤ (set.univ \ {i}).to_finset.card : finset.card_image_le ... = (finset.univ.erase i).card : congr_arg finset.card (finset.ext (by simp [and_comm])) ... < finset.univ.card : finset.card_erase_lt_of_mem (finset.mem_univ i) ... = findim K V : card_eq }, -- We already have that `b '' univ` spans the whole space, -- so we only need to show that the span of `b '' (univ \ {i})` contains each `b j`. refine trans (le_antisymm (span_mono (set.image_subset_range _ _)) (span_le.mpr _)) span_eq, rintros _ ⟨j, rfl, rfl⟩, -- The case that `j ≠ i` is easy because `b j ∈ b '' (univ \ {i})`. by_cases j_eq : j = i, swap, { refine subset_span ⟨j, (set.mem_diff _).mpr ⟨set.mem_univ _, _⟩, rfl⟩, exact mt set.mem_singleton_iff.mp j_eq }, -- To show `b i ∈ span (b '' (univ \ {i}))`, we use that it's a weighted sum -- of the other `b j`s. rw [j_eq, set_like.mem_coe, show b i = -((g i)⁻¹ • (s.erase i).sum (λ j, g j • b j)), from _], { refine submodule.neg_mem _ (smul_mem _ _ (sum_mem _ (λ k hk, _))), obtain ⟨k_ne_i, k_mem⟩ := finset.mem_erase.mp hk, refine smul_mem _ _ (subset_span ⟨k, _, rfl⟩), simpa using k_mem }, -- To show `b i` is a weighted sum of the other `b j`s, we'll rewrite this sum -- to have the form of the assumption `dependent`. apply eq_neg_of_add_eq_zero, calc b i + (g i)⁻¹ • (s.erase i).sum (λ j, g j • b j) = (g i)⁻¹ • (g i • b i + (s.erase i).sum (λ j, g j • b j)) : by rw [smul_add, ←mul_smul, inv_mul_cancel gx_ne_zero, one_smul] ... = (g i)⁻¹ • 0 : congr_arg _ _ ... = 0 : smul_zero _, -- And then it's just a bit of manipulation with finite sums. rwa [← finset.insert_erase i_mem_s, finset.sum_insert (finset.not_mem_erase _ _)] at dependent end /-- A finite family of vectors is linearly independent if and only if its cardinality equals the dimension of its span. -/ lemma linear_independent_iff_card_eq_findim_span {ι : Type*} [fintype ι] {b : ι → V} : linear_independent K b ↔ fintype.card ι = findim K (span K (set.range b)) := begin split, { intro h, exact (findim_span_eq_card h).symm }, { intro hc, let f := (submodule.subtype (span K (set.range b))), let b' : ι → span K (set.range b) := λ i, ⟨b i, mem_span.2 (λ p hp, hp (set.mem_range_self _))⟩, have hs : span K (set.range b') = ⊤, { rw eq_top_iff', intro x, have h : span K (f '' (set.range b')) = map f (span K (set.range b')) := span_image f, have hf : f '' (set.range b') = set.range b, { ext x, simp [set.mem_image, set.mem_range] }, rw hf at h, have hx : (x : V) ∈ span K (set.range b) := x.property, conv at hx { congr, skip, rw h }, simpa [mem_map] using hx }, have hi : f.ker = ⊥ := ker_subtype _, convert (linear_independent_of_span_eq_top_of_card_eq_findim hs hc).map' _ hi } end lemma is_basis_of_span_eq_top_of_card_eq_findim {ι : Type*} [fintype ι] {b : ι → V} (span_eq : span K (set.range b) = ⊤) (card_eq : fintype.card ι = findim K V) : is_basis K b := ⟨linear_independent_of_span_eq_top_of_card_eq_findim span_eq card_eq, span_eq⟩ lemma finset_is_basis_of_span_eq_top_of_card_eq_findim {s : finset V} (span_eq : span K (↑s : set V) = ⊤) (card_eq : s.card = findim K V) : is_basis K (coe : (↑s : set V) → V) := is_basis_of_span_eq_top_of_card_eq_findim ((@subtype.range_coe_subtype _ (λ x, x ∈ s)).symm ▸ span_eq) (trans (fintype.card_coe _) card_eq) lemma set_is_basis_of_span_eq_top_of_card_eq_findim {s : set V} [fintype s] (span_eq : span K s = ⊤) (card_eq : s.to_finset.card = findim K V) : is_basis K (λ (x : s), (x : V)) := is_basis_of_span_eq_top_of_card_eq_findim ((@subtype.range_coe_subtype _ s).symm ▸ span_eq) (trans s.to_finset_card.symm card_eq) lemma span_eq_top_of_linear_independent_of_card_eq_findim {ι : Type*} [hι : nonempty ι] [fintype ι] {b : ι → V} (lin_ind : linear_independent K b) (card_eq : fintype.card ι = findim K V) : span K (set.range b) = ⊤ := begin by_cases fin : (finite_dimensional K V), { haveI := fin, by_contra ne_top, have lt_top : span K (set.range b) < ⊤ := lt_of_le_of_ne le_top ne_top, exact ne_of_lt (submodule.findim_lt lt_top) (trans (findim_span_eq_card lin_ind) card_eq) }, { exfalso, apply ne_of_lt (fintype.card_pos_iff.mpr hι), symmetry, calc fintype.card ι = findim K V : card_eq ... = 0 : dif_neg (mt finite_dimensional_iff_dim_lt_omega.mpr fin) } end lemma is_basis_of_linear_independent_of_card_eq_findim {ι : Type*} [nonempty ι] [fintype ι] {b : ι → V} (lin_ind : linear_independent K b) (card_eq : fintype.card ι = findim K V) : is_basis K b := ⟨lin_ind, span_eq_top_of_linear_independent_of_card_eq_findim lin_ind card_eq⟩ lemma finset_is_basis_of_linear_independent_of_card_eq_findim {s : finset V} (hs : s.nonempty) (lin_ind : linear_independent K (coe : (↑s : set V) → V)) (card_eq : s.card = findim K V) : is_basis K (coe : (↑s : set V) → V) := @is_basis_of_linear_independent_of_card_eq_findim _ _ _ _ _ _ ⟨(⟨hs.some, hs.some_spec⟩ : (↑s : set V))⟩ _ _ lin_ind (trans (fintype.card_coe _) card_eq) lemma set_is_basis_of_linear_independent_of_card_eq_findim {s : set V} [nonempty s] [fintype s] (lin_ind : linear_independent K (coe : s → V)) (card_eq : s.to_finset.card = findim K V) : is_basis K (coe : s → V) := is_basis_of_linear_independent_of_card_eq_findim lin_ind (trans s.to_finset_card.symm card_eq) end is_basis section subalgebra_dim open vector_space variables {F E : Type*} [field F] [field E] [algebra F E] lemma subalgebra.dim_eq_one_of_eq_bot {S : subalgebra F E} (h : S = ⊥) : dim F S = 1 := begin rw [← S.to_submodule_equiv.dim_eq, h, (linear_equiv.of_eq (⊥ : subalgebra F E).to_submodule _ algebra.to_submodule_bot).dim_eq, dim_span_set], exacts [mk_singleton _, linear_independent_singleton one_ne_zero] end @[simp] lemma subalgebra.dim_bot : dim F (⊥ : subalgebra F E) = 1 := subalgebra.dim_eq_one_of_eq_bot rfl lemma subalgebra_top_dim_eq_submodule_top_dim : dim F (⊤ : subalgebra F E) = dim F (⊤ : submodule F E) := by { rw ← algebra.coe_top, refl } lemma subalgebra_top_findim_eq_submodule_top_findim : findim F (⊤ : subalgebra F E) = findim F (⊤ : submodule F E) := by { rw ← algebra.coe_top, refl } lemma subalgebra.dim_top : dim F (⊤ : subalgebra F E) = dim F E := by { rw subalgebra_top_dim_eq_submodule_top_dim, exact dim_top } lemma subalgebra.finite_dimensional_bot : finite_dimensional F (⊥ : subalgebra F E) := finite_dimensional_of_dim_eq_one subalgebra.dim_bot @[simp] lemma subalgebra.findim_bot : findim F (⊥ : subalgebra F E) = 1 := begin haveI : finite_dimensional F (⊥ : subalgebra F E) := subalgebra.finite_dimensional_bot, have : dim F (⊥ : subalgebra F E) = 1 := subalgebra.dim_bot, rw ← findim_eq_dim at this, norm_cast at *, simp *, end lemma subalgebra.findim_eq_one_of_eq_bot {S : subalgebra F E} (h : S = ⊥) : findim F S = 1 := by { rw h, exact subalgebra.findim_bot } lemma subalgebra.eq_bot_of_findim_one {S : subalgebra F E} (h : findim F S = 1) : S = ⊥ := begin rw eq_bot_iff, let b : set S := {1}, have : fintype b := unique.fintype, have b_lin_ind : linear_independent F (coe : b → S) := linear_independent_singleton one_ne_zero, have b_card : fintype.card b = 1 := fintype.card_of_subsingleton _, obtain ⟨_, b_spans⟩ := set_is_basis_of_linear_independent_of_card_eq_findim b_lin_ind (by simp only [*, set.to_finset_card]), intros x hx, rw [algebra.mem_bot], have x_in_span_b : (⟨x, hx⟩ : S) ∈ submodule.span F b, { rw subtype.range_coe at b_spans, rw b_spans, exact submodule.mem_top, }, obtain ⟨a, ha⟩ := submodule.mem_span_singleton.mp x_in_span_b, replace ha : a • 1 = x := by injections with ha, exact ⟨a, by rw [← ha, algebra.smul_def, mul_one]⟩, end lemma subalgebra.eq_bot_of_dim_one {S : subalgebra F E} (h : dim F S = 1) : S = ⊥ := begin haveI : finite_dimensional F S := finite_dimensional_of_dim_eq_one h, rw ← findim_eq_dim at h, norm_cast at h, exact subalgebra.eq_bot_of_findim_one h, end @[simp] lemma subalgebra.bot_eq_top_of_dim_eq_one (h : dim F E = 1) : (⊥ : subalgebra F E) = ⊤ := begin rw [← dim_top, ← subalgebra_top_dim_eq_submodule_top_dim] at h, exact eq.symm (subalgebra.eq_bot_of_dim_one h), end @[simp] lemma subalgebra.bot_eq_top_of_findim_eq_one (h : findim F E = 1) : (⊥ : subalgebra F E) = ⊤ := begin rw [← findim_top, ← subalgebra_top_findim_eq_submodule_top_findim] at h, exact eq.symm (subalgebra.eq_bot_of_findim_one h), end @[simp] theorem subalgebra.dim_eq_one_iff {S : subalgebra F E} : dim F S = 1 ↔ S = ⊥ := ⟨subalgebra.eq_bot_of_dim_one, subalgebra.dim_eq_one_of_eq_bot⟩ @[simp] theorem subalgebra.findim_eq_one_iff {S : subalgebra F E} : findim F S = 1 ↔ S = ⊥ := ⟨subalgebra.eq_bot_of_findim_one, subalgebra.findim_eq_one_of_eq_bot⟩ end subalgebra_dim namespace module namespace End lemma exists_ker_pow_eq_ker_pow_succ [finite_dimensional K V] (f : End K V) : ∃ (k : ℕ), k ≤ findim K V ∧ (f ^ k).ker = (f ^ k.succ).ker := begin classical, by_contradiction h_contra, simp_rw [not_exists, not_and] at h_contra, have h_le_ker_pow : ∀ (n : ℕ), n ≤ (findim K V).succ → n ≤ findim K (f ^ n).ker, { intros n hn, induction n with n ih, { exact zero_le (findim _ _) }, { have h_ker_lt_ker : (f ^ n).ker < (f ^ n.succ).ker, { refine lt_of_le_of_ne _ (h_contra n (nat.le_of_succ_le_succ hn)), rw pow_succ, apply linear_map.ker_le_ker_comp }, have h_findim_lt_findim : findim K (f ^ n).ker < findim K (f ^ n.succ).ker, { apply submodule.findim_lt_findim_of_lt h_ker_lt_ker }, calc n.succ ≤ (findim K ↥(linear_map.ker (f ^ n))).succ : nat.succ_le_succ (ih (nat.le_of_succ_le hn)) ... ≤ findim K ↥(linear_map.ker (f ^ n.succ)) : nat.succ_le_of_lt h_findim_lt_findim } }, have h_le_findim_V : ∀ n, findim K (f ^ n).ker ≤ findim K V := λ n, submodule.findim_le _, have h_any_n_lt: ∀ n, n ≤ (findim K V).succ → n ≤ findim K V := λ n hn, (h_le_ker_pow n hn).trans (h_le_findim_V n), show false, from nat.not_succ_le_self _ (h_any_n_lt (findim K V).succ (findim K V).succ.le_refl), end lemma ker_pow_constant {f : End K V} {k : ℕ} (h : (f ^ k).ker = (f ^ k.succ).ker) : ∀ m, (f ^ k).ker = (f ^ (k + m)).ker | 0 := by simp | (m + 1) := begin apply le_antisymm, { rw [add_comm, pow_add], apply linear_map.ker_le_ker_comp }, { rw [ker_pow_constant m, add_comm m 1, ←add_assoc, pow_add, pow_add f k m], change linear_map.ker ((f ^ (k + 1)).comp (f ^ m)) ≤ linear_map.ker ((f ^ k).comp (f ^ m)), rw [linear_map.ker_comp, linear_map.ker_comp, h, nat.add_one], exact le_refl _, } end lemma ker_pow_eq_ker_pow_findim_of_le [finite_dimensional K V] {f : End K V} {m : ℕ} (hm : findim K V ≤ m) : (f ^ m).ker = (f ^ findim K V).ker := begin obtain ⟨k, h_k_le, hk⟩ : ∃ k, k ≤ findim K V ∧ linear_map.ker (f ^ k) = linear_map.ker (f ^ k.succ) := exists_ker_pow_eq_ker_pow_succ f, calc (f ^ m).ker = (f ^ (k + (m - k))).ker : by rw nat.add_sub_of_le (h_k_le.trans hm) ... = (f ^ k).ker : by rw ker_pow_constant hk _ ... = (f ^ (k + (findim K V - k))).ker : ker_pow_constant hk (findim K V - k) ... = (f ^ findim K V).ker : by rw nat.add_sub_of_le h_k_le end lemma ker_pow_le_ker_pow_findim [finite_dimensional K V] (f : End K V) (m : ℕ) : (f ^ m).ker ≤ (f ^ findim K V).ker := begin by_cases h_cases: m < findim K V, { rw [←nat.add_sub_of_le (nat.le_of_lt h_cases), add_comm, pow_add], apply linear_map.ker_le_ker_comp }, { rw [ker_pow_eq_ker_pow_findim_of_le (le_of_not_lt h_cases)], exact le_refl _ } end end End end module
cf749caff9d09591f0af53c8f5334f13f411930b
9dc8cecdf3c4634764a18254e94d43da07142918
/src/ring_theory/valuation/valuation_subring.lean
94acc78a0b841bf0884a601664f94eefa1d32276
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
29,230
lean
/- Copyright (c) 2022 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Adam Topaz, Junyan Xu, Jack McKoen -/ import ring_theory.valuation.valuation_ring import ring_theory.localization.as_subring import ring_theory.subring.pointwise import algebraic_geometry.prime_spectrum.basic /-! # Valuation subrings of a field ## Projects The order structure on `valuation_subring K`. -/ open_locale classical noncomputable theory variables (K : Type*) [field K] /-- A valuation subring of a field `K` is a subring `A` such that for every `x : K`, either `x ∈ A` or `x⁻¹ ∈ A`. -/ structure valuation_subring extends subring K := (mem_or_inv_mem' : ∀ x : K, x ∈ carrier ∨ x⁻¹ ∈ carrier) namespace valuation_subring variables {K} (A : valuation_subring K) instance : set_like (valuation_subring K) K := { coe := λ A, A.to_subring, coe_injective' := by { rintro ⟨⟨⟩⟩ ⟨⟨⟩⟩ _, congr' } } @[simp] lemma mem_carrier (x : K) : x ∈ A.carrier ↔ x ∈ A := iff.refl _ @[simp] lemma mem_to_subring (x : K) : x ∈ A.to_subring ↔ x ∈ A := iff.refl _ @[ext] lemma ext (A B : valuation_subring K) (h : ∀ x, x ∈ A ↔ x ∈ B) : A = B := set_like.ext h lemma zero_mem : (0 : K) ∈ A := A.to_subring.zero_mem lemma one_mem : (1 : K) ∈ A := A.to_subring.one_mem lemma add_mem (x y : K) : x ∈ A → y ∈ A → x + y ∈ A := A.to_subring.add_mem lemma mul_mem (x y : K) : x ∈ A → y ∈ A → x * y ∈ A := A.to_subring.mul_mem lemma neg_mem (x : K) : x ∈ A → (-x) ∈ A := A.to_subring.neg_mem lemma mem_or_inv_mem (x : K) : x ∈ A ∨ x⁻¹ ∈ A := A.mem_or_inv_mem' _ lemma to_subring_injective : function.injective (to_subring : valuation_subring K → subring K) := λ x y h, by { cases x, cases y, congr' } instance : comm_ring A := show comm_ring A.to_subring, by apply_instance instance : is_domain A := show is_domain A.to_subring, by apply_instance instance : has_top (valuation_subring K) := has_top.mk $ { mem_or_inv_mem' := λ x, or.inl trivial, ..(⊤ : subring K) } lemma mem_top (x : K) : x ∈ (⊤ : valuation_subring K) := trivial lemma le_top : A ≤ ⊤ := λ a ha, mem_top _ instance : order_top (valuation_subring K) := { top := ⊤, le_top := le_top } instance : inhabited (valuation_subring K) := ⟨⊤⟩ instance : valuation_ring A := { cond := λ a b, begin by_cases (b : K) = 0, { use 0, left, ext, simp [h] }, by_cases (a : K) = 0, { use 0, right, ext, simp [h] }, cases A.mem_or_inv_mem (a/b) with hh hh, { use ⟨a/b, hh⟩, right, ext, field_simp, ring }, { rw (show (a/b : K)⁻¹ = b/a, by field_simp) at hh, use ⟨b/a, hh⟩, left, ext, field_simp, ring }, end } instance : algebra A K := show algebra A.to_subring K, by apply_instance @[simp] lemma algebra_map_apply (a : A) : algebra_map A K a = a := rfl instance : is_fraction_ring A K := { map_units := λ ⟨y, hy⟩, (units.mk0 (y : K) (λ c, non_zero_divisors.ne_zero hy $ subtype.ext c)).is_unit, surj := λ z, begin by_cases z = 0, { use (0, 1), simp [h] }, cases A.mem_or_inv_mem z with hh hh, { use (⟨z, hh⟩, 1), simp }, { refine ⟨⟨1, ⟨⟨_, hh⟩, _⟩⟩, mul_inv_cancel h⟩, exact mem_non_zero_divisors_iff_ne_zero.2 (λ c, h (inv_eq_zero.mp (congr_arg coe c))) }, end, eq_iff_exists := λ a b, ⟨ λ h, ⟨1, by { ext, simpa using h }⟩, λ ⟨c, h⟩, congr_arg coe ((mul_eq_mul_right_iff.1 h).resolve_right (non_zero_divisors.ne_zero c.2)) ⟩ } /-- The value group of the valuation associated to `A`. Note: it is actually a group with zero. -/ @[derive linear_ordered_comm_group_with_zero] def value_group := valuation_ring.value_group A K /-- Any valuation subring of `K` induces a natural valuation on `K`. -/ def valuation : valuation K A.value_group := valuation_ring.valuation A K instance inhabited_value_group : inhabited A.value_group := ⟨A.valuation 0⟩ lemma valuation_le_one (a : A) : A.valuation a ≤ 1 := (valuation_ring.mem_integer_iff A K _).2 ⟨a, rfl⟩ lemma mem_of_valuation_le_one (x : K) (h : A.valuation x ≤ 1) : x ∈ A := let ⟨a, ha⟩ := (valuation_ring.mem_integer_iff A K x).1 h in ha ▸ a.2 lemma valuation_le_one_iff (x : K) : A.valuation x ≤ 1 ↔ x ∈ A := ⟨mem_of_valuation_le_one _ _, λ ha, A.valuation_le_one ⟨x, ha⟩⟩ lemma valuation_eq_iff (x y : K) : A.valuation x = A.valuation y ↔ ∃ a : Aˣ, (a : K) * y = x := quotient.eq' lemma valuation_le_iff (x y : K) : A.valuation x ≤ A.valuation y ↔ ∃ a : A, (a : K) * y = x := iff.rfl lemma valuation_surjective : function.surjective A.valuation := surjective_quot_mk _ lemma valuation_unit (a : Aˣ) : A.valuation a = 1 := by { rw [← A.valuation.map_one, valuation_eq_iff], use a, simp } lemma valuation_eq_one_iff (a : A) : is_unit a ↔ A.valuation a = 1 := ⟨ λ h, A.valuation_unit h.unit, λ h, begin have ha : (a : K) ≠ 0, { intro c, rw [c, A.valuation.map_zero] at h, exact zero_ne_one h }, have ha' : (a : K)⁻¹ ∈ A, { rw [← valuation_le_one_iff, map_inv₀, h, inv_one] }, apply is_unit_of_mul_eq_one a ⟨a⁻¹, ha'⟩, ext, field_simp, end ⟩ lemma valuation_lt_one_or_eq_one (a : A) : A.valuation a < 1 ∨ A.valuation a = 1 := lt_or_eq_of_le (A.valuation_le_one a) lemma valuation_lt_one_iff (a : A) : a ∈ local_ring.maximal_ideal A ↔ A.valuation a < 1 := begin rw local_ring.mem_maximal_ideal, dsimp [nonunits], rw valuation_eq_one_iff, exact (A.valuation_le_one a).lt_iff_ne.symm, end /-- A subring `R` of `K` such that for all `x : K` either `x ∈ R` or `x⁻¹ ∈ R` is a valuation subring of `K`. -/ def of_subring (R : subring K) (hR : ∀ x : K, x ∈ R ∨ x⁻¹ ∈ R) : valuation_subring K := { mem_or_inv_mem' := hR, ..R } @[simp] lemma mem_of_subring (R : subring K) (hR : ∀ x : K, x ∈ R ∨ x⁻¹ ∈ R) (x : K) : x ∈ of_subring R hR ↔ x ∈ R := iff.refl _ /-- An overring of a valuation ring is a valuation ring. -/ def of_le (R : valuation_subring K) (S : subring K) (h : R.to_subring ≤ S) : valuation_subring K := { mem_or_inv_mem' := λ x, (R.mem_or_inv_mem x).imp (@h x) (@h _), ..S} section order instance : semilattice_sup (valuation_subring K) := { sup := λ R S, of_le R (R.to_subring ⊔ S.to_subring) $ le_sup_left, le_sup_left := λ R S x hx, (le_sup_left : R.to_subring ≤ R.to_subring ⊔ S.to_subring) hx, le_sup_right := λ R S x hx, (le_sup_right : S.to_subring ≤ R.to_subring ⊔ S.to_subring) hx, sup_le := λ R S T hR hT x hx, (sup_le hR hT : R.to_subring ⊔ S.to_subring ≤ T.to_subring) hx, ..(infer_instance : partial_order (valuation_subring K)) } /-- The ring homomorphism induced by the partial order. -/ def inclusion (R S : valuation_subring K) (h : R ≤ S) : R →+* S := subring.inclusion h /-- The canonical ring homomorphism from a valuation ring to its field of fractions. -/ def subtype (R : valuation_subring K) : R →+* K := subring.subtype R.to_subring /-- The canonical map on value groups induced by a coarsening of valuation rings. -/ def map_of_le (R S : valuation_subring K) (h : R ≤ S) : R.value_group →*₀ S.value_group := { to_fun := quotient.map' id $ λ x y ⟨u, hu⟩, ⟨units.map (R.inclusion S h).to_monoid_hom u, hu⟩, map_zero' := rfl, map_one' := rfl, map_mul' := by { rintro ⟨⟩ ⟨⟩, refl } } @[mono] lemma monotone_map_of_le (R S : valuation_subring K) (h : R ≤ S) : monotone (R.map_of_le S h) := by { rintros ⟨⟩ ⟨⟩ ⟨a, ha⟩, exact ⟨R.inclusion S h a, ha⟩ } @[simp] lemma map_of_le_comp_valuation (R S : valuation_subring K) (h : R ≤ S) : R.map_of_le S h ∘ R.valuation = S.valuation := by { ext, refl } @[simp] lemma map_of_le_valuation_apply (R S : valuation_subring K) (h : R ≤ S) (x : K) : R.map_of_le S h (R.valuation x) = S.valuation x := rfl /-- The ideal corresponding to a coarsening of a valuation ring. -/ def ideal_of_le (R S : valuation_subring K) (h : R ≤ S) : ideal R := (local_ring.maximal_ideal S).comap (R.inclusion S h) instance prime_ideal_of_le (R S : valuation_subring K) (h : R ≤ S) : (ideal_of_le R S h).is_prime := (local_ring.maximal_ideal S).comap_is_prime _ /-- The coarsening of a valuation ring associated to a prime ideal. -/ def of_prime (A : valuation_subring K) (P : ideal A) [P.is_prime] : valuation_subring K := of_le A (localization.subalgebra.of_field K P.prime_compl $ le_non_zero_divisors_of_no_zero_divisors $ not_not_intro P.zero_mem).to_subring $ λ a ha, subalgebra.algebra_map_mem _ (⟨a, ha⟩ : A) instance of_prime_algebra (A : valuation_subring K) (P : ideal A) [P.is_prime] : algebra A (A.of_prime P) := subalgebra.algebra _ instance of_prime_scalar_tower (A : valuation_subring K) (P : ideal A) [P.is_prime] : is_scalar_tower A (A.of_prime P) K := is_scalar_tower.subalgebra' A K K _ instance of_prime_localization (A : valuation_subring K) (P : ideal A) [P.is_prime] : is_localization.at_prime (A.of_prime P) P := by apply localization.subalgebra.is_localization_of_field K lemma le_of_prime (A : valuation_subring K) (P : ideal A) [P.is_prime] : A ≤ of_prime A P := λ a ha, subalgebra.algebra_map_mem _ (⟨a, ha⟩ : A) lemma of_prime_valuation_eq_one_iff_mem_prime_compl (A : valuation_subring K) (P : ideal A) [P.is_prime] (x : A) : (of_prime A P).valuation x = 1 ↔ x ∈ P.prime_compl := begin rw [← is_localization.at_prime.is_unit_to_map_iff (A.of_prime P) P x, valuation_eq_one_iff], refl, end @[simp] lemma ideal_of_le_of_prime (A : valuation_subring K) (P : ideal A) [P.is_prime] : ideal_of_le A (of_prime A P) (le_of_prime A P) = P := by { ext, apply is_localization.at_prime.to_map_mem_maximal_iff } @[simp] lemma of_prime_ideal_of_le (R S : valuation_subring K) (h : R ≤ S) : of_prime R (ideal_of_le R S h) = S := begin ext x, split, { rintro ⟨a, r, hr, rfl⟩, apply mul_mem, { exact h a.2 }, { rw [← valuation_le_one_iff, map_inv₀, ← inv_one, inv_le_inv₀], { exact not_lt.1 ((not_iff_not.2 $ valuation_lt_one_iff S _).1 hr) }, { intro hh, erw [valuation.zero_iff, subring.coe_eq_zero_iff] at hh, apply hr, rw hh, apply ideal.zero_mem (R.ideal_of_le S h) }, { exact one_ne_zero } } }, { intro hx, by_cases hr : x ∈ R, { exact R.le_of_prime _ hr }, have : x ≠ 0 := λ h, hr (by { rw h, exact R.zero_mem }), replace hr := (R.mem_or_inv_mem x).resolve_left hr, { use [1, x⁻¹, hr], split, { change (⟨x⁻¹, h hr⟩ : S) ∉ nonunits S, erw [mem_nonunits_iff, not_not], apply is_unit_of_mul_eq_one _ (⟨x, hx⟩ : S), ext, field_simp }, { field_simp } } }, end lemma of_prime_le_of_le (P Q : ideal A) [P.is_prime] [Q.is_prime] (h : P ≤ Q) : of_prime A Q ≤ of_prime A P := λ x ⟨a, s, hs, he⟩, ⟨a, s, λ c, hs (h c), he⟩ lemma ideal_of_le_le_of_le (R S : valuation_subring K) (hR : A ≤ R) (hS : A ≤ S) (h : R ≤ S) : ideal_of_le A S hS ≤ ideal_of_le A R hR := λ x hx, (valuation_lt_one_iff R _).2 begin by_contra c, push_neg at c, replace c := monotone_map_of_le R S h c, rw [(map_of_le _ _ _).map_one, map_of_le_valuation_apply] at c, apply not_le_of_lt ((valuation_lt_one_iff S _).1 hx) c, end /-- The equivalence between coarsenings of a valuation ring and its prime ideals.-/ @[simps] def prime_spectrum_equiv : prime_spectrum A ≃ { S | A ≤ S } := { to_fun := λ P, ⟨of_prime A P.as_ideal, le_of_prime _ _⟩, inv_fun := λ S, ⟨ideal_of_le _ S S.2, infer_instance⟩, left_inv := λ P, by { ext1, simpa }, right_inv := λ S, by { ext1, simp } } /-- An ordered variant of `prime_spectrum_equiv`. -/ @[simps] def prime_spectrum_order_equiv : (prime_spectrum A)ᵒᵈ ≃o {S | A ≤ S} := { map_rel_iff' := λ P Q, ⟨ λ h, begin have := ideal_of_le_le_of_le A _ _ _ _ h, iterate 2 { erw ideal_of_le_of_prime at this }, exact this, end, λ h, by { apply of_prime_le_of_le, exact h } ⟩, ..(prime_spectrum_equiv A) } instance linear_order_overring : linear_order { S | A ≤ S } := { le_total := let i : is_total (prime_spectrum A) (≤) := (subtype.rel_embedding _ _).is_total in by exactI (prime_spectrum_order_equiv A).symm.to_rel_embedding.is_total.total, decidable_le := infer_instance, ..(infer_instance : partial_order _) } end order end valuation_subring namespace valuation variables {K} {Γ Γ₁ Γ₂ : Type*} [linear_ordered_comm_group_with_zero Γ] [linear_ordered_comm_group_with_zero Γ₁] [linear_ordered_comm_group_with_zero Γ₂] (v : valuation K Γ) (v₁ : valuation K Γ₁) (v₂ : valuation K Γ₂) /-- The valuation subring associated to a valuation. -/ def valuation_subring : valuation_subring K := { mem_or_inv_mem' := begin intros x, cases le_or_lt (v x) 1, { left, exact h }, { right, change v x⁻¹ ≤ 1, rw [map_inv₀ v, ← inv_one, inv_le_inv₀], { exact le_of_lt h }, { intro c, simpa [c] using h }, { exact one_ne_zero } } end, .. v.integer } @[simp] lemma mem_valuation_subring_iff (x : K) : x ∈ v.valuation_subring ↔ v x ≤ 1 := iff.refl _ lemma is_equiv_iff_valuation_subring : v₁.is_equiv v₂ ↔ v₁.valuation_subring = v₂.valuation_subring := begin split, { intros h, ext x, specialize h x 1, simpa using h }, { intros h, apply is_equiv_of_val_le_one, intros x, have : x ∈ v₁.valuation_subring ↔ x ∈ v₂.valuation_subring, by rw h, simpa using this } end lemma is_equiv_valuation_valuation_subring : v.is_equiv v.valuation_subring.valuation := begin rw [is_equiv_iff_val_le_one], intro x, rw valuation_subring.valuation_le_one_iff, refl, end end valuation namespace valuation_subring variables {K} (A : valuation_subring K) @[simp] lemma valuation_subring_valuation : A.valuation.valuation_subring = A := by { ext, rw ← A.valuation_le_one_iff, refl } section unit_group /-- The unit group of a valuation subring, as a subgroup of `Kˣ`. -/ def unit_group : subgroup Kˣ := (A.valuation.to_monoid_with_zero_hom.to_monoid_hom.comp (units.coe_hom K)).ker @[simp] lemma mem_unit_group_iff (x : Kˣ) : x ∈ A.unit_group ↔ A.valuation x = 1 := iff.rfl /-- For a valuation subring `A`, `A.unit_group` agrees with the units of `A`. -/ def unit_group_mul_equiv : A.unit_group ≃* Aˣ := { to_fun := λ x, { val := ⟨x, mem_of_valuation_le_one A _ x.prop.le⟩, inv := ⟨↑(x⁻¹), mem_of_valuation_le_one _ _ (x⁻¹).prop.le⟩, val_inv := subtype.ext (units.mul_inv x), inv_val := subtype.ext (units.inv_mul x) }, inv_fun := λ x, ⟨units.map A.subtype.to_monoid_hom x, A.valuation_unit x⟩, left_inv := λ a, by { ext, refl }, right_inv := λ a, by { ext, refl }, map_mul' := λ a b, by { ext, refl } } @[simp] lemma coe_unit_group_mul_equiv_apply (a : A.unit_group) : (A.unit_group_mul_equiv a : K) = a := rfl @[simp] lemma coe_unit_group_mul_equiv_symm_apply (a : Aˣ) : (A.unit_group_mul_equiv.symm a : K) = a := rfl lemma unit_group_le_unit_group {A B : valuation_subring K} : A.unit_group ≤ B.unit_group ↔ A ≤ B := begin split, { intros h x hx, rw [← A.valuation_le_one_iff x, le_iff_lt_or_eq] at hx, by_cases h_1 : x = 0, { simp only [h_1, zero_mem] }, by_cases h_2 : 1 + x = 0, { simp only [← add_eq_zero_iff_neg_eq.1 h_2, neg_mem _ _ (one_mem _)] }, cases hx, { have := h (show (units.mk0 _ h_2) ∈ A.unit_group, from A.valuation.map_one_add_of_lt hx), simpa using B.add_mem _ _ (show 1 + x ∈ B, from set_like.coe_mem ((B.unit_group_mul_equiv ⟨_, this⟩) : B)) (B.neg_mem _ B.one_mem) }, { have := h (show (units.mk0 x h_1) ∈ A.unit_group, from hx), refine set_like.coe_mem ((B.unit_group_mul_equiv ⟨_, this⟩) : B) } }, { rintros h x (hx : A.valuation x = 1), apply_fun A.map_of_le B h at hx, simpa using hx } end lemma unit_group_injective : function.injective (unit_group : valuation_subring K → subgroup _) := λ A B h, by { simpa only [le_antisymm_iff, unit_group_le_unit_group] using h} lemma eq_iff_unit_group {A B : valuation_subring K} : A = B ↔ A.unit_group = B.unit_group := unit_group_injective.eq_iff.symm /-- The map on valuation subrings to their unit groups is an order embedding. -/ def unit_group_order_embedding : valuation_subring K ↪o subgroup Kˣ := { to_fun := λ A, A.unit_group, inj' := unit_group_injective, map_rel_iff' := λ A B, unit_group_le_unit_group } lemma unit_group_strict_mono : strict_mono (unit_group : valuation_subring K → subgroup _) := unit_group_order_embedding.strict_mono end unit_group section nonunits /-- The nonunits of a valuation subring of `K`, as a subsemigroup of `K`-/ def nonunits : subsemigroup K := { carrier := { x | A.valuation x < 1 }, mul_mem' := λ a b ha hb, (mul_lt_mul₀ ha hb).trans_eq $ mul_one _ } lemma mem_nonunits_iff {x : K} : x ∈ A.nonunits ↔ A.valuation x < 1 := iff.rfl lemma nonunits_le_nonunits {A B : valuation_subring K} : B.nonunits ≤ A.nonunits ↔ A ≤ B := begin split, { intros h x hx, by_cases h_1 : x = 0, { simp only [h_1, zero_mem] }, rw [← valuation_le_one_iff, ← not_lt, valuation.one_lt_val_iff _ h_1] at hx ⊢, by_contra h_2, from hx (h h_2) }, { intros h x hx, by_contra h_1, from not_lt.2 (monotone_map_of_le _ _ h (not_lt.1 h_1)) hx } end lemma nonunits_injective : function.injective (nonunits : valuation_subring K → subsemigroup _) := λ A B h, by { simpa only [le_antisymm_iff, nonunits_le_nonunits] using h.symm } lemma nonunits_inj {A B : valuation_subring K} : A.nonunits = B.nonunits ↔ A = B := nonunits_injective.eq_iff /-- The map on valuation subrings to their nonunits is a dual order embedding. -/ def nonunits_order_embedding : valuation_subring K ↪o (subsemigroup K)ᵒᵈ := { to_fun := λ A, A.nonunits, inj' := nonunits_injective, map_rel_iff' := λ A B, nonunits_le_nonunits } variables {A} /-- The elements of `A.nonunits` are those of the maximal ideal of `A` after coercion to `K`. See also `mem_nonunits_iff_exists_mem_maximal_ideal`, which gets rid of the coercion to `K`, at the expense of a more complicated right hand side. -/ theorem coe_mem_nonunits_iff {a : A} : (a : K) ∈ A.nonunits ↔ a ∈ local_ring.maximal_ideal A := (valuation_lt_one_iff _ _).symm lemma nonunits_le : A.nonunits ≤ A.to_subring.to_submonoid.to_subsemigroup := λ a ha, (A.valuation_le_one_iff _).mp (A.mem_nonunits_iff.mp ha).le lemma nonunits_subset : (A.nonunits : set K) ⊆ A := nonunits_le /-- The elements of `A.nonunits` are those of the maximal ideal of `A`. See also `coe_mem_nonunits_iff`, which has a simpler right hand side but requires the element to be in `A` already. -/ theorem mem_nonunits_iff_exists_mem_maximal_ideal {a : K} : a ∈ A.nonunits ↔ ∃ ha, (⟨a, ha⟩ : A) ∈ local_ring.maximal_ideal A := ⟨λ h, ⟨nonunits_subset h, coe_mem_nonunits_iff.mp h⟩, λ ⟨ha, h⟩, coe_mem_nonunits_iff.mpr h⟩ /-- `A.nonunits` agrees with the maximal ideal of `A`, after taking its image in `K`. -/ theorem image_maximal_ideal : (coe : A → K) '' local_ring.maximal_ideal A = A.nonunits := begin ext a, simp only [set.mem_image, set_like.mem_coe, mem_nonunits_iff_exists_mem_maximal_ideal], erw subtype.exists, simp_rw [subtype.coe_mk, exists_and_distrib_right, exists_eq_right], end end nonunits section principal_unit_group /-- The principal unit group of a valuation subring, as a subgroup of `Kˣ`. -/ def principal_unit_group : subgroup Kˣ := { carrier := { x | A.valuation (x - 1) < 1 }, mul_mem' := begin intros a b ha hb, refine lt_of_le_of_lt _ (max_lt hb ha), rw [← one_mul (A.valuation (b - 1)), ← A.valuation.map_one_add_of_lt ha, add_sub_cancel'_right, ← valuation.map_mul, mul_sub_one, ← sub_add_sub_cancel], exact A.valuation.map_add _ _, end, one_mem' := by simpa using zero_lt_one₀, inv_mem' := begin dsimp, intros a ha, conv {to_lhs, rw [← mul_one (A.valuation _), ← A.valuation.map_one_add_of_lt ha]}, rwa [add_sub_cancel'_right, ← valuation.map_mul, sub_mul, units.inv_mul, ← neg_sub, one_mul, valuation.map_neg], end } lemma principal_units_le_units : A.principal_unit_group ≤ A.unit_group := λ a h, by simpa only [add_sub_cancel'_right] using A.valuation.map_one_add_of_lt h lemma mem_principal_unit_group_iff (x : Kˣ) : x ∈ A.principal_unit_group ↔ A.valuation ((x : K) - 1) < 1 := iff.rfl lemma principal_unit_group_le_principal_unit_group {A B : valuation_subring K} : B.principal_unit_group ≤ A.principal_unit_group ↔ A ≤ B := begin split, { intros h x hx, by_cases h_1 : x = 0, { simp only [h_1, zero_mem] }, by_cases h_2 : x⁻¹ + 1 = 0, { rw [add_eq_zero_iff_eq_neg, inv_eq_iff_inv_eq, inv_neg, inv_one] at h_2, simpa only [h_2] using B.neg_mem _ B.one_mem }, { rw [← valuation_le_one_iff, ← not_lt, valuation.one_lt_val_iff _ h_1, ← add_sub_cancel x⁻¹, ← units.coe_mk0 h_2, ← mem_principal_unit_group_iff] at hx ⊢, simpa only [hx] using @h (units.mk0 (x⁻¹ + 1) h_2) } }, { intros h x hx, by_contra h_1, from not_lt.2 (monotone_map_of_le _ _ h (not_lt.1 h_1)) hx } end lemma principal_unit_group_injective : function.injective (principal_unit_group : valuation_subring K → subgroup _) := λ A B h, by { simpa [le_antisymm_iff, principal_unit_group_le_principal_unit_group] using h.symm } lemma eq_iff_principal_unit_group {A B : valuation_subring K} : A = B ↔ A.principal_unit_group = B.principal_unit_group := principal_unit_group_injective.eq_iff.symm /-- The map on valuation subrings to their principal unit groups is an order embedding. -/ def principal_unit_group_order_embedding : valuation_subring K ↪o (subgroup Kˣ)ᵒᵈ := { to_fun := λ A, A.principal_unit_group, inj' := principal_unit_group_injective, map_rel_iff' := λ A B, principal_unit_group_le_principal_unit_group } lemma coe_mem_principal_unit_group_iff {x : A.unit_group} : (x : Kˣ) ∈ A.principal_unit_group ↔ A.unit_group_mul_equiv x ∈ (units.map (local_ring.residue A).to_monoid_hom).ker := begin rw [monoid_hom.mem_ker, units.ext_iff], dsimp, let π := ideal.quotient.mk (local_ring.maximal_ideal A), change _ ↔ π _ = _, rw [← π.map_one, ← sub_eq_zero, ← π.map_sub, ideal.quotient.eq_zero_iff_mem, valuation_lt_one_iff], simpa, end /-- The principal unit group agrees with the kernel of the canonical map from the units of `A` to the units of the residue field of `A`. -/ def principal_unit_group_equiv : A.principal_unit_group ≃* (units.map (local_ring.residue A).to_monoid_hom).ker := { to_fun := λ x, ⟨A.unit_group_mul_equiv ⟨_, A.principal_units_le_units x.2⟩, A.coe_mem_principal_unit_group_iff.1 x.2⟩, inv_fun := λ x, ⟨A.unit_group_mul_equiv.symm x, by { rw A.coe_mem_principal_unit_group_iff, simpa using set_like.coe_mem x }⟩, left_inv := λ x, by simp, right_inv := λ x, by simp, map_mul' := λ x y, by refl, } @[simp] lemma principal_unit_group_equiv_apply (a : A.principal_unit_group) : (principal_unit_group_equiv A a : K) = a := rfl @[simp] lemma principal_unit_group_symm_apply (a : (units.map (local_ring.residue A).to_monoid_hom).ker) : (A.principal_unit_group_equiv.symm a : K) = a := rfl /-- The canonical map from the unit group of `A` to the units of the residue field of `A`. -/ def unit_group_to_residue_field_units : A.unit_group →* (local_ring.residue_field A)ˣ := monoid_hom.comp (units.map $ (ideal.quotient.mk _).to_monoid_hom) A.unit_group_mul_equiv.to_monoid_hom @[simp] lemma coe_unit_group_to_residue_field_units_apply (x : A.unit_group) : (A.unit_group_to_residue_field_units x : (local_ring.residue_field A) ) = (ideal.quotient.mk _ (A.unit_group_mul_equiv x : A)) := rfl lemma ker_unit_group_to_residue_field_units : A.unit_group_to_residue_field_units.ker = A.principal_unit_group.comap A.unit_group.subtype := by { ext, simpa only [subgroup.mem_comap, subgroup.coe_subtype, coe_mem_principal_unit_group_iff] } lemma surjective_unit_group_to_residue_field_units : function.surjective A.unit_group_to_residue_field_units := (local_ring.surjective_units_map_of_local_ring_hom _ ideal.quotient.mk_surjective local_ring.is_local_ring_hom_residue).comp (mul_equiv.surjective _) /-- The quotient of the unit group of `A` by the principal unit group of `A` agrees with the units of the residue field of `A`. -/ def units_mod_principal_units_equiv_residue_field_units : (A.unit_group ⧸ (A.principal_unit_group.comap A.unit_group.subtype)) ≃* (local_ring.residue_field A)ˣ := (quotient_group.quotient_mul_equiv_of_eq A.ker_unit_group_to_residue_field_units.symm).trans (quotient_group.quotient_ker_equiv_of_surjective _ A.surjective_unit_group_to_residue_field_units) @[simp] lemma units_mod_principal_units_equiv_residue_field_units_comp_quotient_group_mk : A.units_mod_principal_units_equiv_residue_field_units.to_monoid_hom.comp (quotient_group.mk' _) = A.unit_group_to_residue_field_units := rfl @[simp] lemma units_mod_principal_units_equiv_residue_field_units_comp_quotient_group_mk_apply (x : A.unit_group) : A.units_mod_principal_units_equiv_residue_field_units.to_monoid_hom (quotient_group.mk x) = A.unit_group_to_residue_field_units x := rfl end principal_unit_group /-! ### Pointwise actions This transfers the action from `subring.pointwise_mul_action`, noting that it only applies when the action is by a group. Notably this provides an instances when `G` is `K ≃+* K`. These instances are in the `pointwise` locale. The lemmas in this section are copied from `ring_theory/subring/pointwise.lean`; try to keep these in sync. -/ section pointwise_actions open_locale pointwise variables {G : Type*} [group G] [mul_semiring_action G K] /-- The action on a valuation subring corresponding to applying the action to every element. This is available as an instance in the `pointwise` locale. -/ def pointwise_has_smul : has_smul G (valuation_subring K) := { smul := λ g S, -- TODO: if we add `valuation_subring.map` at a later date, we should use it here { mem_or_inv_mem' := λ x, (mem_or_inv_mem S (g⁻¹ • x)).imp (subring.mem_pointwise_smul_iff_inv_smul_mem.mpr) (λ h, subring.mem_pointwise_smul_iff_inv_smul_mem.mpr $ by rwa smul_inv''), .. g • S.to_subring } } localized "attribute [instance] valuation_subring.pointwise_has_smul" in pointwise open_locale pointwise @[simp] lemma coe_pointwise_smul (g : G) (S : valuation_subring K) : ↑(g • S) = g • (S : set K) := rfl @[simp] lemma pointwise_smul_to_subring (g : G) (S : valuation_subring K) : (g • S).to_subring = g • S.to_subring := rfl /-- The action on a valuation subring corresponding to applying the action to every element. This is available as an instance in the `pointwise` locale. This is a stronger version of `valuation_subring.pointwise_has_smul`. -/ def pointwise_mul_action : mul_action G (valuation_subring K) := to_subring_injective.mul_action to_subring pointwise_smul_to_subring localized "attribute [instance] valuation_subring.pointwise_mul_action" in pointwise open_locale pointwise lemma smul_mem_pointwise_smul (g : G) (x : K) (S : valuation_subring K) : x ∈ S → g • x ∈ g • S := (set.smul_mem_smul_set : _ → _ ∈ g • (S : set K)) lemma mem_smul_pointwise_iff_exists (g : G) (x : K) (S : valuation_subring K) : x ∈ g • S ↔ ∃ (s : K), s ∈ S ∧ g • s = x := (set.mem_smul_set : x ∈ g • (S : set K) ↔ _) instance pointwise_central_scalar [mul_semiring_action Gᵐᵒᵖ K] [is_central_scalar G K] : is_central_scalar G (valuation_subring K) := ⟨λ g S, to_subring_injective $ by exact op_smul_eq_smul g S.to_subring⟩ @[simp] lemma smul_mem_pointwise_smul_iff {g : G} {S : valuation_subring K} {x : K} : g • x ∈ g • S ↔ x ∈ S := set.smul_mem_smul_set_iff lemma mem_pointwise_smul_iff_inv_smul_mem {g : G} {S : valuation_subring K} {x : K} : x ∈ g • S ↔ g⁻¹ • x ∈ S := set.mem_smul_set_iff_inv_smul_mem lemma mem_inv_pointwise_smul_iff {g : G} {S : valuation_subring K} {x : K} : x ∈ g⁻¹ • S ↔ g • x ∈ S := set.mem_inv_smul_set_iff @[simp] lemma pointwise_smul_le_pointwise_smul_iff {g : G} {S T : valuation_subring K} : g • S ≤ g • T ↔ S ≤ T := set.set_smul_subset_set_smul_iff lemma pointwise_smul_subset_iff {g : G} {S T : valuation_subring K} : g • S ≤ T ↔ S ≤ g⁻¹ • T := set.set_smul_subset_iff lemma subset_pointwise_smul_iff {g : G} {S T : valuation_subring K} : S ≤ g • T ↔ g⁻¹ • S ≤ T := set.subset_set_smul_iff end pointwise_actions end valuation_subring namespace valuation variables {Γ : Type*} [linear_ordered_comm_group_with_zero Γ] (v : valuation K Γ) (x : Kˣ) @[simp] lemma mem_unit_group_iff : x ∈ v.valuation_subring.unit_group ↔ v x = 1 := (valuation.is_equiv_iff_val_eq_one _ _).mp (valuation.is_equiv_valuation_valuation_subring _).symm end valuation
72871042dac132653e29852148573676654bcd4e
dfd42d30132c2867977fefe7edae98b6dc703aeb
/src/liouville_theorem.lean
4f0d18c0b35dd02fcca8ba8ff15be346aacc6fbb
[]
no_license
justadzr/lean-2021
1e42057ac75c794c94b8f148a27a24150c685f68
dfc6b30de2f27bdba5fbc51183e2b84e73a920d1
refs/heads/master
1,689,652,366,522
1,630,313,809,000
1,630,313,809,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
48,924
lean
import analysis.calculus.conformal import analysis.normed_space.banach import analysis.normed_space.dual import similarity import bilin_form_lemmas import analysis.calculus.times_cont_diff import analysis.calculus.fderiv_symmetric noncomputable theory open conformal_at set open_locale classical real_inner_product_space filter topological_space section quick lemma quick1 {F : Type*} [add_comm_group F] {a b c d e e' f : F} (h : a + b + (c + d) + (e + f) = d + b + (c + a) + (e' + f)) : e = e' := begin simp_rw [← add_assoc] at h, rw [add_right_cancel_iff] at h, nth_rewrite 1 add_comm at h, simp_rw [← add_assoc] at h, nth_rewrite 2 add_comm at h, simp_rw [← add_assoc] at h, nth_rewrite 3 add_comm at h, nth_rewrite 4 add_assoc at h, nth_rewrite 7 add_comm at h, simpa [← add_assoc, add_left_cancel_iff] using h end end quick section linear_conformal_prep open submodule variables {E F : Type*} [inner_product_space ℝ E] [inner_product_space ℝ F] {x : E} lemma eventually_is_conformal_map_of_eventually_conformal {f : E → F} (hf : ∀ᶠ x' in 𝓝 x, conformal_at f x') : ∀ᶠ x' in 𝓝 x, is_conformal_map (fderiv ℝ f x') := hf.mono (λ y hy, conformal_at_iff_is_conformal_map_fderiv.mp hy) lemma A {f' : E →L[ℝ] F} (h : is_conformal_map f') {u v : E} : ⟪u, v⟫ = 0 ↔ ⟪f' u, f' v⟫ = 0 := begin rcases (is_conformal_map_iff _).mp h with ⟨c, p, q⟩, split, { intros huv, convert q u v, rw [huv, mul_zero] }, { intros huv, rw q u v at huv, exact eq_zero_of_ne_zero_of_mul_left_eq_zero (ne_of_gt p) huv } end lemma A' {f' : E → (E →L[ℝ] F)} {u v : E} (huv : ⟪u, v⟫ = 0) (h : ∀ᶠ x' in 𝓝 x, is_conformal_map $ f' x') : (λ x, ⟪f' x u, f' x v⟫) =ᶠ[𝓝 x] λ x, (0 : ℝ) := begin apply (filter.eventually_of_forall $ λ x, huv).mp, simp only [congr_arg], rcases filter.eventually_iff_exists_mem.mp h with ⟨s, hs, hys⟩, exact filter.eventually_iff_exists_mem.mpr ⟨s, hs, λ y hy p, (A $ hys y hy).mp p⟩ end lemma B {f' : E →L[ℝ] F} {K : submodule ℝ E} (hf : function.surjective f') (h : is_conformal_map f') : (Kᗮ).map (f' : E →ₗ[ℝ] F) = (K.map f')ᗮ := begin ext1 y'', simp only [mem_map, mem_orthogonal], split, { rintros ⟨u, hu, huy⟩, intros v hv, rcases hv with ⟨z, hz, hzv⟩, rw [← huy, ← hzv, continuous_linear_map.coe_coe, ← A h], exact hu z hz }, { intros H, rcases hf y'' with ⟨y', hy'⟩, refine ⟨y', λ u hu, _, hy'⟩, rw [A h, hy'], exact H (f' u) ⟨u, hu, rfl⟩ } end lemma C {f' : E →L[ℝ] F} (hf : function.surjective f') (h : is_conformal_map f') {u v : E} {w : F} (H : ∀ (t : E), t ∈ (span ℝ ({u} ∪ {v} : set E))ᗮ → ⟪w, f' t⟫ = 0) : w ∈ (span ℝ ({f' u} ∪ {f' v} : set F)) := begin have triv₁ : {f' u} ∪ {f' v} = f' '' ({u} ∪ {v}) := by simp only [image_union, image_singleton], rw [triv₁, ← continuous_linear_map.coe_coe, ← map_span], have triv₂ : is_complete (span ℝ ({u} ∪ {v} : set E) : set E), { haveI : finite_dimensional ℝ (span ℝ ({u} ∪ {v} : set E)) := finite_dimensional.span_of_finite ℝ ((finite_singleton _).union $ finite_singleton _), exact complete_of_finite_dimensional _ }, haveI : complete_space (span ℝ ({u} ∪ {v} : set E)) := triv₂.complete_space_coe, rw [← orthogonal_orthogonal (span ℝ ({u} ∪ {v} : set E)), B hf h, mem_orthogonal], intros y hy, rw [mem_map] at hy, rcases hy with ⟨y', hy', Hy'⟩, rw [real_inner_comm, ← Hy'], exact H y' hy' end end linear_conformal_prep open continuous_linear_map open_locale topological_space filter section diff_elementary lemma DD1 {E F : Type*} [normed_group E] [normed_space ℝ E] [normed_group F] [normed_space ℝ F] {f : E → F} {f' : E → (E →L[ℝ] F)} {y u : E} (hf : ∀ᶠ (x : E) in 𝓝 y, has_fderiv_at f (f' x) x) (hf' : differentiable_at ℝ f' y) : fderiv ℝ (λ x, f' x u) y = fderiv ℝ f' y u := begin have : (λ x, f' x u) = λ x, ((apply ℝ _ _) ∘ f') x := by simp only [function.comp_app, apply_apply], simp only [this, congr_arg], rw fderiv.comp _ (apply ℝ F u).differentiable_at hf', ext1 v, simp only [(apply ℝ F u).fderiv, coe_comp', function.comp_app, apply_apply], exact second_derivative_symmetric_of_eventually hf hf'.has_fderiv_at _ _ end lemma DD1' {E F : Type*} [normed_group E] [normed_space ℝ E] [normed_group F] [normed_space ℝ F] {f' : E → E →L[ℝ] F} {f'' : E → (E →L[ℝ] E →L[ℝ] F)} {y u v w : E} (hf : ∀ᶠ (x : E) in 𝓝 y, has_fderiv_at f' (f'' x) x) (hf' : differentiable_at ℝ f'' y) : fderiv ℝ (λ x, f'' x u v) y w = fderiv ℝ f'' y w u v := begin have triv : (λ x, f'' x u v) = λ x, ((apply ℝ _ _) ∘ (λ x', f'' x' u)) x := by simp only [function.comp_app, apply_apply], simp only [triv], rw [fderiv.comp _ (apply ℝ F v).differentiable_at, DD1 hf hf'], rw second_derivative_symmetric_of_eventually hf hf'.has_fderiv_at _ _, simp only [congr_arg, coe_comp', (apply ℝ F v).fderiv, apply_apply, function.comp_app], exact (apply ℝ (E →L[ℝ] F) u).differentiable_at.comp _ hf' end lemma is_open.is_const_of_fderiv_eq_zero {E F 𝕜 : Type*} [normed_group E] [normed_space ℝ E] [is_R_or_C 𝕜] [normed_space 𝕜 E] [is_scalar_tower ℝ 𝕜 E] [normed_group F] [normed_space 𝕜 F] {f : E → F} {s : set E} (hs : is_open s) (hs' : is_connected s) (hf : differentiable_on 𝕜 f s) (h : ∀ x ∈ s, fderiv 𝕜 f x = 0) {x y : E} (hx : x ∈ s) (hy : y ∈ s) : f x = f y := begin rw is_connected_iff_connected_space at hs'; resetI, let S : set s := {a : s | f a = f x}, have triv₁ : S.nonempty := ⟨⟨x, hx⟩, rfl⟩, have triv₂ := continuous_on_iff_continuous_restrict.mp hf.continuous_on, have minor₁ : is_closed S := is_closed_eq triv₂ continuous_const, have minor₂ : is_open S := is_open_iff_forall_mem_open.mpr begin intros t ht, rcases metric.is_open_iff.mp hs t.1 t.2 with ⟨ε, hε, hball⟩, have subminor₁ : ∀ (x' : E), x' ∈ metric.ball t.1 ε → fderiv_within 𝕜 f (metric.ball t.1 ε) x' = 0 := λ x' hx', begin convert h x' (hball hx'), exact fderiv_within_of_open metric.is_open_ball hx' end, have subminor₂ : coe⁻¹' (metric.ball t.1 ε) ⊆ S := λ a ha, begin have := (convex_ball t.1 ε).is_const_of_fderiv_within_eq_zero (hf.mono hball) subminor₁ ha (metric.mem_ball_self hε), simp only [set.mem_set_of_eq] at ht, rw [subtype.val_eq_coe, ht] at this, exact this end, refine ⟨coe⁻¹' (metric.ball t.1 ε), subminor₂, metric.is_open_ball.preimage continuous_subtype_coe, _⟩, simp only [subtype.val_eq_coe], exact metric.mem_ball_self hε end, have key : f y = f x := begin suffices new : (⟨y, hy⟩ : s) ∈ S, { exact new }, { rw eq_univ_of_nonempty_clopen triv₁ ⟨minor₂, minor₁⟩, exact mem_univ _ } end, exact key.symm end lemma is_open.eq_sub_add_of_fderiv_eq_fderiv {E F 𝕜 : Type*} [normed_group E] [normed_space ℝ E] [is_R_or_C 𝕜] [normed_space 𝕜 E] [is_scalar_tower ℝ 𝕜 E] [normed_group F] [normed_space 𝕜 F] {f g : E → F} {s : set E} (hs : is_open s) (hs' : is_connected s) (hf : differentiable_on 𝕜 f s) (hg : differentiable_on 𝕜 g s) (h : ∀ x ∈ s, fderiv 𝕜 f x = fderiv 𝕜 g x) {x₀ : E} (hx₀ : x₀ ∈ s) : ∀ x ∈ s, f x = g x - g x₀ + f x₀ := begin refine λ x hx, sub_eq_zero.mp _, rw [sub_add_eq_add_sub, ← add_sub], have triv₁ : f x₀ - (g x₀ + (f x₀ - g x₀)) = 0 := by simp, rw ← triv₁, have triv₂ : differentiable_on 𝕜 (λ y, f y - (g y + (f x₀ - g x₀))) s := hf.sub (hg.add_const _), refine hs.is_const_of_fderiv_eq_zero hs' triv₂ (λ y hy, _) hx hx₀, rw [fderiv_sub ((hf y hy).differentiable_at $ hs.mem_nhds hy) (((hg y hy).differentiable_at $ hs.mem_nhds hy).add_const _), fderiv_add_const, h y hy, sub_self] end /-- Strangely the last statement cannot be simped... even if it's extremely simple -/ lemma is_open.exists_of_fderiv_eq_fderiv {E F 𝕜 : Type*} [normed_group E] [normed_space ℝ E] [is_R_or_C 𝕜] [normed_space 𝕜 E] [is_scalar_tower ℝ 𝕜 E] [normed_group F] [normed_space 𝕜 F] {f g : E → F} {s : set E} (hs : is_open s) (hs' : is_connected s) (hf : differentiable_on 𝕜 f s) (hg : differentiable_on 𝕜 g s) (h : ∀ x ∈ s, fderiv 𝕜 f x = fderiv 𝕜 g x) : ∃ y₀, ∀ x ∈ s, f x = g x - y₀ := let ⟨x₀, hx₀⟩ := hs'.nonempty in ⟨- (f x₀ - g x₀), λ x hx, by simpa [sub_neg, sub_add] using hs.eq_sub_add_of_fderiv_eq_fderiv hs' hf hg h hx₀ x hx⟩ -- lemma is_open.exists_of_fderiv_eq_fderiv_of_has_fderiv_at -- {E F 𝕜 : Type*} [normed_group E] [normed_space ℝ E] [is_R_or_C 𝕜] -- [normed_space 𝕜 E] [is_scalar_tower ℝ 𝕜 E] [normed_group F] [normed_space 𝕜 F] -- {f g : E → F} {f'} {s : set E} (hs : is_open s) (hs' : is_connected s) -- (hf : differentiable_on 𝕜 f s) (hg : differentiable_on 𝕜 g s) -- (h : ∀ x ∈ s, fderiv 𝕜 f x = fderiv 𝕜 g x) : -- ∃ x₀ ∈ s, ∀ x ∈ s, f x = g x - g x₀ + f x₀ := -- begin -- end end diff_elementary section diff_prep variables {E F : Type*} [normed_group E] [normed_group F] [normed_space ℝ E] [normed_space ℝ F] {f : E → F} lemma D21 {y : E} {n : ℕ} (hf : times_cont_diff_at ℝ n.succ f y) : ∀ᶠ (x : E) in 𝓝 y, has_fderiv_at f (fderiv ℝ f x) x := begin rcases times_cont_diff_at_succ_iff_has_fderiv_at.mp hf with ⟨f', ⟨s, hs, hxs⟩, hf'⟩, have minor₁ : ∀ (x : E), x ∈ s → differentiable_at ℝ f x := λ x hx, ⟨f' x, hxs x hx⟩, have minor₂ : ∀ (x : E), x ∈ s → has_fderiv_at f (fderiv ℝ f x) x := λ x hx, (minor₁ x hx).has_fderiv_at, rw filter.eventually_iff_exists_mem, exact ⟨s, hs, minor₂⟩ end lemma D22 {y : E} {n : ℕ} (hf : times_cont_diff_at ℝ n.succ f y) : times_cont_diff_at ℝ n (fderiv ℝ f) y := begin have triv₁ : (n : with_top ℕ) ≤ n + 1 := by { apply with_top.coe_le_coe.mpr, exact nat.le_succ _ }, have triv₂ : (1 : with_top ℕ) ≤ n + 1 := by { apply with_top.coe_le_coe.mpr, linarith }, rcases times_cont_diff_at_succ_iff_has_fderiv_at.mp hf with ⟨f', ⟨s, hs, hxs⟩, hf'⟩, have minor₁ : ∀ (x : E), x ∈ s → differentiable_at ℝ f x := λ x hx, ⟨f' x, hxs x hx⟩, have minor₂ : set.eq_on (fderiv ℝ f) f' s, { intros x hxmem, have := (hf.differentiable_at triv₂).has_fderiv_at, exact (minor₁ x hxmem).has_fderiv_at.unique (hxs x hxmem) }, exact hf'.congr_of_eventually_eq (filter.eventually_eq_of_mem hs minor₂) end lemma D23 {y : E} {n : ℕ} (hn : 0 < n) (hf : times_cont_diff_at ℝ (n + 1) f y) : differentiable_at ℝ (fderiv ℝ f) y := (D22 hf).differentiable_at (with_top.coe_le_coe.mpr $ nat.succ_le_of_lt hn) lemma DD2 {y : E} {n : ℕ} (hn : 0 < n) (hf : times_cont_diff_at ℝ (n + 1) f y) (u : E) : differentiable_at ℝ (λ x, fderiv ℝ f x u) y := (apply ℝ F u).differentiable_at.comp _ (D23 hn hf) lemma third_order_symmetric {x u v w : E} (hf' : ∀ᶠ x' in 𝓝 x, times_cont_diff_at ℝ 3 f x') : fderiv ℝ (fderiv ℝ $ fderiv ℝ f) x w u v = fderiv ℝ (fderiv ℝ $ fderiv ℝ f) x v u w := begin have minor₁ : ∀ᶠ x' in 𝓝 x, has_fderiv_at ((apply ℝ _ u) ∘ (fderiv ℝ f)) ((apply ℝ _ u).comp $ fderiv ℝ (fderiv ℝ f) x') x' := hf'.mono (λ y hy, (apply ℝ F u).has_fderiv_at.comp _ (D23 zero_lt_two hy).has_fderiv_at), have minor₂ : (λ x', (apply ℝ _ u).comp $ fderiv ℝ (fderiv ℝ f) x') =ᶠ[𝓝 x] λ x', (((apply ℝ (E →L[ℝ] F)) u) ∘ fderiv ℝ (fderiv ℝ f)) x' := hf'.mono (λ y hy, begin ext1, simp only [coe_comp', function.comp_app, apply_apply], rw second_derivative_symmetric_of_eventually (D21 hy) (D23 zero_lt_two hy).has_fderiv_at end), have key := (apply ℝ (E →L[ℝ] F) u).has_fderiv_at.comp _ (D23 zero_lt_one $ D22 hf'.self_of_nhds).has_fderiv_at, have := second_derivative_symmetric_of_eventually minor₁ (key.congr_of_eventually_eq minor₂) v w, simp only [coe_comp', function.comp_app, apply_apply] at this, rw this end end diff_prep section tot_diff_eq open submodule variables {E F : Type*} [inner_product_space ℝ E] [inner_product_space ℝ F] {f : E → F} lemma D' (u v w : E) {y : E} {n : ℕ} (hn : 0 < n) (hf : times_cont_diff_at ℝ (n + 1) f y) : fderiv ℝ (λ x, ⟪fderiv ℝ f x u, fderiv ℝ f x v⟫) y w = ⟪fderiv ℝ (fderiv ℝ f) y u w, fderiv ℝ f y v⟫ + ⟪fderiv ℝ f y u, fderiv ℝ (fderiv ℝ f) y v w⟫ := begin rw [fderiv_inner_apply (DD2 hn hf _) (DD2 hn hf _)], simp only [congr_arg, DD1 (D21 hf) (D23 hn hf), congr_arg, add_comm] end variables {x : E} (hf : ∀ᶠ x' in 𝓝 x, conformal_at f x') {f' : E → (E →L[ℝ] F)} (Hf : ∀ (x' : E), is_conformal_map $ f' x') (Heven : fderiv ℝ f =ᶠ[𝓝 x] f') localized "notation `conf_diff` := eventually_is_conformal_map_of_eventually_conformal hf" in liouville_do_not_use localized "notation `conf_diff'` := (eventually_is_conformal_map_of_eventually_conformal hf).self_of_nhds" in liouville_do_not_use include hf lemma D (hf' : times_cont_diff_at ℝ 2 f x) {u v w : E} (huv : ⟪u, v⟫ = 0) (hwu : ⟪w, u⟫ = 0) (hwv : ⟪w, v⟫ = 0) : ⟪fderiv ℝ (fderiv ℝ f) x u v, fderiv ℝ f x w⟫ = 0 := begin rw real_inner_comm at hwv, have m₁ := D' u v w zero_lt_one hf', have m₂ := D' v w u zero_lt_one hf', have m₃ := D' w u v zero_lt_one hf', rw [(A' huv conf_diff).fderiv_eq] at m₁, rw [(A' hwv conf_diff).fderiv_eq] at m₂, rw [(A' hwu conf_diff).fderiv_eq] at m₃, rw [fderiv_const, pi.zero_apply, continuous_linear_map.zero_apply] at m₁ m₂ m₃, rw add_comm at m₁ m₃, nth_rewrite 0 real_inner_comm at m₃ m₁, nth_rewrite 1 real_inner_comm at m₁, rw [second_derivative_symmetric_of_eventually (D21 hf') (D23 zero_lt_one hf').has_fderiv_at v u, second_derivative_symmetric_of_eventually (D21 hf') (D23 zero_lt_one hf').has_fderiv_at w u] at m₂, rw [second_derivative_symmetric_of_eventually (D21 hf') (D23 zero_lt_one hf').has_fderiv_at w v] at m₃, have triv₂ : ∀ {a b c : ℝ}, a + b = 0 → b + c = 0 → a + c = 0 → a = 0 := λ a b c hab hbc hac, begin rw [← hab, ← zero_add (a + b), ← hac, ← add_assoc, ← zero_add (b + c)] at hbc, nth_rewrite 3 add_comm at hbc, rw [add_assoc, add_assoc] at hbc, nth_rewrite 1 ← add_assoc at hbc, nth_rewrite 4 add_comm at hbc, exact (add_self_eq_zero.mp $ add_right_cancel hbc.symm) end, exact triv₂ m₃.symm m₁.symm m₂.symm end lemma G'' (hf' : times_cont_diff_at ℝ 2 f x) (h : function.surjective (fderiv ℝ f x)) {u v : E} (huv : ⟪u, v⟫ = 0) : fderiv ℝ (fderiv ℝ f) x u v ∈ span ℝ ({fderiv ℝ f x u} ∪ {fderiv ℝ f x v} : set F) := begin refine C h conf_diff' (λ t ht, _), rw mem_orthogonal at ht, have triv₁ : u ∈ span ℝ ({u} ∪ {v} : set E) := subset_span (or.intro_left _ $ mem_singleton _), have triv₂ : v ∈ span ℝ ({u} ∪ {v} : set E) := subset_span (or.intro_right _ $ mem_singleton _), have minor₁ := ht u triv₁, have minor₂ := ht v triv₂, rw real_inner_comm at minor₁ minor₂, exact D hf hf' huv minor₁ minor₂ end lemma G' (hf' : times_cont_diff_at ℝ 2 f x) (h : function.surjective (fderiv ℝ f x)) {u v : E} (huv : ⟪u, v⟫ = 0) : fderiv ℝ (fderiv ℝ f) x u v = (⟪fderiv ℝ f x u, fderiv ℝ (fderiv ℝ f) x u v⟫ / ↑∥fderiv ℝ f x u∥ ^ 2) • fderiv ℝ f x u + (⟪fderiv ℝ f x v, fderiv ℝ (fderiv ℝ f) x u v⟫ / ↑∥fderiv ℝ f x v∥ ^ 2) • fderiv ℝ f x v := begin rw [← orthogonal_projection_singleton, ← orthogonal_projection_singleton], have := G'' hf hf' h huv, rw [span_union, mem_sup] at this, rcases this with ⟨p₁, hp₁, p₂, hp₂, hp₁p₂⟩, have triv₁ : fderiv ℝ (fderiv ℝ f) x u v - p₂ = p₁ := by rw [← hp₁p₂, ← add_sub, sub_self, add_zero], have triv₂ : fderiv ℝ (fderiv ℝ f) x u v - p₁ = p₂ := by { rw [← hp₁p₂, add_comm], rw [← add_sub, sub_self, add_zero] }, rcases mem_span_singleton.mp hp₁ with ⟨s₁, hs₁⟩, rcases mem_span_singleton.mp hp₂ with ⟨s₂, hs₂⟩, have key₁ : ∀ (w : F), w ∈ span ℝ ({fderiv ℝ f x u} : set F) → ⟪fderiv ℝ (fderiv ℝ f) x u v - p₁, w⟫ = 0 := λ w hw, begin rcases mem_span_singleton.mp hw with ⟨s', hs'⟩, rw [← hs', triv₂, ← hs₂, real_inner_smul_left, real_inner_smul_right], rw [real_inner_comm, A conf_diff'] at huv, rw [huv, mul_zero, mul_zero] end, have key₂ : ∀ (w : F), w ∈ span ℝ ({fderiv ℝ f x v} : set F) → ⟪fderiv ℝ (fderiv ℝ f) x u v - p₂, w⟫ = 0 := λ w hw, begin rcases mem_span_singleton.mp hw with ⟨s', hs'⟩, rw [← hs', triv₁, ← hs₁, real_inner_smul_left, real_inner_smul_right], rw [A conf_diff'] at huv, rw [huv, mul_zero, mul_zero] end, rw [eq_orthogonal_projection_of_mem_of_inner_eq_zero hp₁ key₁, eq_orthogonal_projection_of_mem_of_inner_eq_zero hp₂ key₂], exact hp₁p₂.symm end include Hf Heven lemma G [nontrivial E] (hf' : times_cont_diff_at ℝ 2 f x) (u v : E) : ⟪fderiv ℝ (fderiv ℝ f) x u v, fderiv ℝ f x u⟫ + ⟪fderiv ℝ f x u, fderiv ℝ (fderiv ℝ f) x u v⟫ = 2 * ((similarity_factor_sqrt x conf_diff') * (fderiv ℝ (λ y, similarity_factor_sqrt y $ Hf y) x v) * ⟪u, u⟫) := begin rcases filter.eventually_eq_iff_exists_mem.mp Heven with ⟨s, hs, heq⟩, rw ← D' u u v zero_lt_one hf', have : (λ (y : E), ⟪fderiv ℝ f y u, fderiv ℝ f y u⟫) =ᶠ[𝓝 x] (λ y, ⟪u, u⟫ * id y) ∘ (λ y, similarity_factor y $ Hf y), { rw filter.eventually_eq_iff_exists_mem, refine ⟨s, hs, _⟩, intros z hz, simp only [function.comp_app, congr_arg], rw [mul_comm, heq hz], exact (similarity_factor_prop z $ Hf z).2 u u }, have minor₁ := (D22 hf').congr_of_eventually_eq Heven.symm, have minor₂ := (similarity_factor_times_cont_diff_at x Hf minor₁).differentiable_at (le_of_eq rfl), have minor₃ := (similarity_factor_sqrt_times_cont_diff_at x Hf minor₁).differentiable_at (le_of_eq rfl), rw [this.fderiv_eq, fderiv.comp _ (differentiable_at_id.const_mul _) minor₂, fderiv_const_mul differentiable_at_id ⟪u, u⟫, fderiv_id], rw ← similarity_factor_sqrt_eq Hf, simp only [pow_two], rw [fderiv_mul minor₃ minor₃, coe_comp'], simp only [function.comp_app, coe_add', pi.add_apply, continuous_linear_map.smul_apply, smul_eq_mul, coe_id'], simp only [_root_.id], rw similarity_factor_sqrt_eq_of_eq conf_diff' Heven.self_of_nhds, ring end lemma GG' {u v : E} (hu : u ≠ 0) (hf' : times_cont_diff_at ℝ 2 f x) : ⟪fderiv ℝ (fderiv ℝ f) x u v, fderiv ℝ f x u⟫ / ⟪u, u⟫ = similarity_factor_sqrt x conf_diff' * (fderiv ℝ (λ y, similarity_factor_sqrt y $ Hf y) x v) := begin haveI : nontrivial E := nontrivial_of_ne u 0 hu, have key := G hf Hf Heven hf' u v, rw [real_inner_comm, ← two_mul, real_inner_comm] at key, have triv : ⟪u, u⟫ ≠ 0 := λ W, hu (inner_self_eq_zero.mp W), rw div_eq_iff_mul_eq triv, convert (mul_left_cancel' _ key).symm, exact two_ne_zero end lemma GG1 {u v : E} (hu : u ≠ 0) (hf' : times_cont_diff_at ℝ 2 f x) : ⟪fderiv ℝ f x u, fderiv ℝ (fderiv ℝ f) x u v⟫ / ∥fderiv ℝ f x u∥ ^ 2 = (fderiv ℝ (λ y, similarity_factor_sqrt y $ Hf y) x v) * similarity_factor_sqrt_inv x conf_diff' := begin rw [pow_two, ← real_inner_self_eq_norm_sq], have triv₁ : ⟪u, u⟫ ≠ 0 := λ W, hu (inner_self_eq_zero.mp W), rw [← div_mul_div_cancel _ triv₁, (similarity_factor_sqrt_inv_prop x conf_diff').2, real_inner_comm, GG' hf Hf Heven hu hf'], simp only [similarity_factor_sqrt_inv, inv_inv', congr_arg], field_simp [triv₁, (similarity_factor_sqrt_prop x conf_diff').1], ring end lemma GG2 {u v : E} (hv : v ≠ 0) (hf' : times_cont_diff_at ℝ 2 f x) : ⟪fderiv ℝ f x v, fderiv ℝ (fderiv ℝ f) x u v⟫ / ∥fderiv ℝ f x v∥ ^ 2 = (fderiv ℝ (λ y, similarity_factor_sqrt y $ Hf y) x u) * similarity_factor_sqrt_inv x conf_diff' := begin rw second_derivative_symmetric_of_eventually (D21 hf') (D23 zero_lt_one hf').has_fderiv_at u v, exact GG1 hf Hf Heven hv hf' end open filter open_locale filter lemma GGG_eventually_eq {u v : E} {s : set E} (hxs : x ∈ s) (hs : is_open s) (hu : u ≠ 0) (hv : v ≠ 0) (huv : ⟪u, v⟫ = 0) (hf' : ∀ y ∈ s, times_cont_diff_at ℝ 2 f y) (h : ∀ y ∈ s, function.surjective (fderiv ℝ f y)) : (λ x', (similarity_factor_sqrt_inv x' $ Hf x') • (fderiv ℝ (fderiv ℝ f) x' u v) + (fderiv ℝ (λ y, similarity_factor_sqrt_inv y $ Hf y) x' v) • fderiv ℝ f x' u + (fderiv ℝ (λ y, similarity_factor_sqrt_inv y $ Hf y) x' u) • fderiv ℝ f x' v) =ᶠ[𝓝 x] λ x', (0 : F) := begin haveI : nontrivial E := nontrivial_of_ne u 0 hu, rcases eventually_iff_exists_mem.mp hf with ⟨s₁, hs₁, hy₁⟩, rcases eventually_eq_iff_exists_mem.mp Heven with ⟨s₂, hs₂, hy₂⟩, have triv₁ : (s₁ ∩ s₂) ∩ s ∈ 𝓝 x := inter_mem (inter_mem hs₁ hs₂) (hs.mem_nhds hxs), rcases mem_nhds_iff.mp triv₁ with ⟨t, ht, hxt₁, hxt₂⟩, refine eventually_eq_of_mem (hxt₁.mem_nhds hxt₂) (λ y hy, _), have minor₁ : ∀ᶠ x' in 𝓝 y, conformal_at f x' := eventually_iff_exists_mem.mpr ⟨t, hxt₁.mem_nhds hy, λ y' hy', hy₁ y' (ht hy').1.1⟩, have minor₂ : fderiv ℝ f =ᶠ[𝓝 y] f' := eventually_iff_exists_mem.mpr ⟨t, hxt₁.mem_nhds hy, λ y' hy', hy₂ (ht hy').1.2⟩, simp only [congr_arg], have key₁ := (hf' y (ht hy).2), have key₂ := h y (ht hy).2, have minor₃ := (D22 key₁).congr_of_eventually_eq minor₂.symm, have key := similarity_factor_sqrt_inv_fderiv y Hf zero_lt_one minor₃, rw [G' minor₁ key₁ key₂ huv, key], simp only [is_R_or_C.coe_real_eq_id, _root_.id], rw [GG1 minor₁ Hf minor₂ hu key₁, GG2 minor₁ Hf minor₂ hv key₁], simp only [smul_add, smul_smul, pi.neg_apply, pi.mul_apply, congr_arg], rw [← similarity_factor_sqrt_inv_eq', inv_pow', inv_inv', pow_two], rw similarity_factor_sqrt_inv_eq_of_eq (Hf y) minor₂.symm.self_of_nhds, nth_rewrite 1 add_comm, simp only [← add_assoc, ← add_smul, add_assoc, ← add_smul], rw [neg_mul_eq_neg_mul_symm, neg_add_eq_sub], simp only [mul_assoc, mul_comm, sub_self, zero_smul], simp end lemma J1 {u : E} (v w : E) (hu : u ≠ 0) (hf' : ∀ᶠ x' in 𝓝 x, times_cont_diff_at ℝ 3 f x') : fderiv ℝ (λ x, (fderiv ℝ (λ y, similarity_factor_sqrt_inv y $ Hf y) x v) • fderiv ℝ f x u) x w = fderiv ℝ (fderiv ℝ $ λ y, similarity_factor_sqrt_inv y $ Hf y) x w v • fderiv ℝ f x u + fderiv ℝ (λ y, similarity_factor_sqrt_inv y $ Hf y) x v • fderiv ℝ (fderiv ℝ f) x w u := begin haveI : nontrivial E := nontrivial_of_ne u 0 hu, have minor₀ := similarity_factor_sqrt_inv_times_cont_diff_at x Hf ((D22 hf'.self_of_nhds).congr_of_eventually_eq Heven.symm), have minor₁ := hf.mono (λ x' hx', hx'.differentiable_at.has_fderiv_at), have minor₂ := D23 zero_lt_two hf'.self_of_nhds, have minor₃ : ∀ᶠ x' in 𝓝 x, times_cont_diff_at ℝ 2 (fderiv ℝ f) x' := hf'.mono (λ a ha, D22 ha), have minor₄ : ∀ᶠ x' in 𝓝 x, has_fderiv_at (λ y, similarity_factor_sqrt_inv y $ Hf y) (fderiv ℝ (λ y, similarity_factor_sqrt_inv y $ Hf y) x') x' := D21 (similarity_factor_sqrt_inv_times_cont_diff_at _ Hf $ minor₃.self_of_nhds.congr_of_eventually_eq Heven.symm), have minor₅ := D23 zero_lt_one minor₀, rw fderiv_smul, simp only [continuous_linear_map.add_apply, continuous_linear_map.smul_apply, continuous_linear_map.smul_right_apply, congr_arg], rw [DD1 minor₁ minor₂, DD1 minor₄ minor₅], simp only [congr_arg], rw [second_derivative_symmetric_of_eventually minor₁ minor₂.has_fderiv_at, second_derivative_symmetric_of_eventually minor₄ minor₅.has_fderiv_at, add_comm], exact DD2 zero_lt_one (similarity_factor_sqrt_inv_times_cont_diff_at _ Hf $ minor₃.self_of_nhds.congr_of_eventually_eq Heven.symm) v, exact DD2 zero_lt_two hf'.self_of_nhds u end lemma J2 {u : E} (v w : E) (hu : u ≠ 0) (hf' : times_cont_diff_at ℝ 4 f x) : fderiv ℝ (λ x', (similarity_factor_sqrt_inv x' $ Hf x') • fderiv ℝ (fderiv ℝ f) x' u v) x w = fderiv ℝ (λ x', similarity_factor_sqrt_inv x' $ Hf x') x w • fderiv ℝ (fderiv ℝ f) x u v + similarity_factor_sqrt_inv x conf_diff' • fderiv ℝ (fderiv ℝ $ fderiv ℝ f) x w u v := begin haveI : nontrivial E := nontrivial_of_ne u 0 hu, have := similarity_factor_sqrt_inv_times_cont_diff_at x Hf ((D22 hf').congr_of_eventually_eq Heven.symm), rw fderiv_smul, simp only [add_apply, smul_apply, smul_right_apply, congr_arg], rw [DD1' (D21 $ D22 hf') (D23 zero_lt_two $ D22 hf')], simp only [add_comm, congr_arg], rw similarity_factor_sqrt_inv_eq_of_eq _ Heven.self_of_nhds, exact this.differentiable_at (with_top.coe_le_coe.mpr $ nat.succ_le_succ zero_le_two), exact (apply ℝ F v).differentiable_at.comp _ ((apply ℝ (E →L[ℝ] F) u).differentiable_at.comp _ $ D23 zero_lt_two $ D22 hf'), end lemma J2' {u : E} (v w : E) (hu : u ≠ 0) (hf' : ∀ᶠ x' in 𝓝 x, times_cont_diff_at ℝ 4 f x') : fderiv ℝ (λ x', (similarity_factor_sqrt_inv x' $ Hf x') • fderiv ℝ (fderiv ℝ f) x' u v) x w = fderiv ℝ (λ x', similarity_factor_sqrt_inv x' $ Hf x') x w • fderiv ℝ (fderiv ℝ f) x u v + similarity_factor_sqrt_inv x conf_diff' • fderiv ℝ (fderiv ℝ $ fderiv ℝ f) x v u w := by rw [J2 hf Hf Heven v w hu hf'.self_of_nhds, third_order_symmetric (hf'.mono $ λ a ha, ha.of_le $ by { apply with_top.coe_le_coe.mpr, norm_num })] lemma tot1 {u v w : E} (hw : w ≠ 0) (huv : ⟪u, v⟫ = 0) (huw : ⟪u, w⟫ = 0) (hwv : ⟪w, v⟫ = 0) (hf' : ∀ᶠ x' in 𝓝 x, times_cont_diff_at ℝ 4 f x') (h : ∀ᶠ x' in 𝓝 x , function.surjective (fderiv ℝ f x')) : fderiv ℝ (fderiv ℝ $ λ y, similarity_factor_sqrt_inv y $ Hf y) x v u = 0 := begin by_cases hv : v ≠ 0; by_cases hu : u ≠ 0, { have triv₁ : (2 : with_top ℕ) ≤ 4, { apply with_top.coe_le_coe.mpr, norm_num }, have triv₂ : (3 : with_top ℕ) ≤ 4, { apply with_top.coe_le_coe.mpr, norm_num }, have triv₃ : (1 : with_top ℕ) ≤ 3, { apply with_top.coe_le_coe.mpr, norm_num }, haveI : nontrivial E := nontrivial_of_ne u 0 hu, have minor₁ := similarity_factor_sqrt_inv_times_cont_diff_at x Hf ((D22 hf'.self_of_nhds).congr_of_eventually_eq Heven.symm), have minor₂ := hf.mono (λ x' hx', hx'.differentiable_at.has_fderiv_at), have minor₃ : ∀ᶠ x' in 𝓝 x, times_cont_diff_at ℝ 2 (fderiv ℝ f) x' := hf'.mono (λ a ha, D22 $ ha.of_le triv₂), have minor₄ : ∀ᶠ x' in 𝓝 x, has_fderiv_at (λ y, similarity_factor_sqrt_inv y $ Hf y) (fderiv ℝ (λ y, similarity_factor_sqrt_inv y $ Hf y) x') x' := D21 (similarity_factor_sqrt_inv_times_cont_diff_at _ Hf $ minor₃.self_of_nhds.congr_of_eventually_eq Heven.symm), rcases eventually_iff_exists_mem.mp hf' with ⟨s₁, hs₁, hy₁⟩, rcases eventually_iff_exists_mem.mp h with ⟨s₂, hs₂, hy₂⟩, rcases mem_nhds_iff.mp (inter_mem hs₁ hs₂) with ⟨t, ht, Ht₁, Ht₂⟩, have m₁ : fderiv ℝ _ _ w = (0 : F), { rw (GGG_eventually_eq hf Hf Heven Ht₂ Ht₁ hu hv huv (λ y' hy', (hy₁ y' (ht hy').1).of_le triv₁) $ λ y' hy', hy₂ y' (ht hy').2).fderiv_eq, simp only [congr_arg, fderiv_const, pi.zero_apply, zero_apply] }, have m₂ : fderiv ℝ _ _ v = (0 : F), { rw (GGG_eventually_eq hf Hf Heven Ht₂ Ht₁ hu hw huw (λ y' hy', (hy₁ y' (ht hy').1).of_le triv₁) $ λ y' hy', hy₂ y' (ht hy').2).fderiv_eq, simp only [congr_arg, fderiv_const, pi.zero_apply, zero_apply] }, rw ← m₂ at m₁, have diff₁ := (apply ℝ ℝ u).differentiable_at.comp _ (D23 zero_lt_two minor₁), have diff₁' := (apply ℝ ℝ v).differentiable_at.comp _ (D23 zero_lt_two minor₁), have diff₁'' := (apply ℝ ℝ w).differentiable_at.comp _ (D23 zero_lt_two minor₁), have diff₂ := (apply ℝ F v).differentiable_at.comp _ ((D22 hf'.self_of_nhds).differentiable_at triv₃), have diff₂' := (apply ℝ F u).differentiable_at.comp _ ((D22 hf'.self_of_nhds).differentiable_at triv₃), have diff₂'' := (apply ℝ F w).differentiable_at.comp _ ((D22 hf'.self_of_nhds).differentiable_at triv₃), have diff₃ := (apply ℝ F v).differentiable_at.comp _ ((apply ℝ (E →L[ℝ] F) u).differentiable_at.comp _ $ D23 zero_lt_two $ D22 hf'.self_of_nhds), have diff₃' := (apply ℝ F w).differentiable_at.comp _ ((apply ℝ (E →L[ℝ] F) u).differentiable_at.comp _ $ D23 zero_lt_two $ D22 hf'.self_of_nhds), have diff_mk₁ := diff₁.smul diff₂, have diff_mk₁' := diff₁.smul diff₂'', have diff_mk₂ := diff₁'.smul diff₂', have diff_mk₂' := diff₁''.smul diff₂', have diff_mk₃ := (minor₁.differentiable_at triv₃).smul diff₃, have diff_mk₃' := (minor₁.differentiable_at triv₃).smul diff₃', simp only [congr_arg, function.comp_app, apply_apply] at diff_mk₁ diff_mk₁' diff_mk₂ diff_mk₂' diff_mk₃ diff_mk₃', have times₁ := hf'.mono (λ a ha, ha.of_le triv₂), rw [fderiv_add (diff_mk₃.add diff_mk₂) diff_mk₁, fderiv_add diff_mk₃ diff_mk₂, fderiv_add (diff_mk₃'.add diff_mk₂') diff_mk₁', fderiv_add diff_mk₃' diff_mk₂'] at m₁, simp only [add_apply] at m₁, rw [J1 hf Hf Heven v w hu times₁, J1 hf Hf Heven u w hv times₁, J1 hf Hf Heven w v hu times₁, J1 hf Hf Heven u v hw times₁] at m₁, rw [J2' hf Hf Heven v w hu hf', J2 hf Hf Heven w v hu hf'.self_of_nhds] at m₁, rw [second_derivative_symmetric_of_eventually (D21 hf'.self_of_nhds) (D23 zero_lt_three hf'.self_of_nhds).has_fderiv_at w u, second_derivative_symmetric_of_eventually (D21 hf'.self_of_nhds) (D23 zero_lt_three hf'.self_of_nhds).has_fderiv_at u v, second_derivative_symmetric_of_eventually (D21 hf'.self_of_nhds) (D23 zero_lt_three hf'.self_of_nhds).has_fderiv_at w v] at m₁, rw second_derivative_symmetric_of_eventually minor₄ (D23 zero_lt_two minor₁).has_fderiv_at at m₁, clear minor₁ minor₂ minor₃ minor₄ m₂ diff₁ diff₁' diff₁'' diff₂ diff₂' diff₂'' diff₃ diff₃' diff_mk₁ diff_mk₁' diff_mk₂ diff_mk₂' diff_mk₃ diff_mk₃' times₁, -- if I don't make a `quick1` lemma the there will be a time-out failure. have key := quick1 m₁, clear m₁, have triv₄ : ⟪fderiv ℝ f x w, fderiv ℝ f x w⟫ ≠ 0 := λ W, (hw $ inner_self_eq_zero.mp $ (A conf_diff').mpr W), rw [← mul_div_cancel (fderiv ℝ (fderiv ℝ $ λ y, similarity_factor_sqrt_inv y $ Hf y) x v u) triv₄], simp only [congr_arg] at key, rw [← real_inner_smul_right, ← key, real_inner_smul_right, (A conf_diff').mp hwv, mul_zero, zero_div] }, { rw not_not.mp hu, simp only [continuous_linear_map.map_zero] }, { rw not_not.mp hv, simp only [continuous_linear_map.map_zero, continuous_linear_map.zero_apply] }, { rw not_not.mp hu, simp only [continuous_linear_map.map_zero] } end end tot_diff_eq section bilin_form_and_local_prop open continuous_linear_map filter variables {E F : Type*} [inner_product_space ℝ E] [inner_product_space ℝ F] {f : E → F} {s : set E} (hs : is_open s) (hfs : ∀ x ∈ s, conformal_at f x) (hf's : ∀ x ∈ s, times_cont_diff_at ℝ 4 f x) (hsurj : ∀ x ∈ s , function.surjective (fderiv ℝ f x)) {f' : E → (E →L[ℝ] F)} (Hf : ∀ (x' : E), is_conformal_map $ f' x') (Hevens : ∀ x ∈ s, fderiv ℝ f x = f' x) def to_sym_bilin_form (x : E) : bilin_form ℝ E := { bilin := λ u v, fderiv ℝ (fderiv ℝ $ λ y, similarity_factor_sqrt_inv y $ Hf y) x v u, bilin_add_left := λ x y z, by simp only [map_add], bilin_smul_left := λ s x y, by simp only [map_smul, smul_eq_mul], bilin_add_right := λ x y z, by simp only [map_add, add_apply], bilin_smul_right := λ s x y, by simp only [map_smul, smul_apply, smul_eq_mul] } include hs Hevens hf's lemma is_sym_to_sym_bilin_form [nontrivial E] {x : E} (hx : x ∈ s) : sym_bilin_form.is_sym (to_sym_bilin_form Hf x) := λ u v, begin have Heven := eventually_eq_iff_exists_mem.mpr ⟨s, hs.mem_nhds hx, λ a ha, Hevens a ha⟩, have minor₁ := similarity_factor_sqrt_inv_times_cont_diff_at x Hf ((D22 $ hf's x hx).congr_of_eventually_eq Heven.symm), have minor₂ : ∀ᶠ x' in 𝓝 x, has_fderiv_at (λ y, similarity_factor_sqrt_inv y $ Hf y) (fderiv ℝ (λ y, similarity_factor_sqrt_inv y $ Hf y) x') x' := D21 (similarity_factor_sqrt_inv_times_cont_diff_at _ Hf $ (D22 $ hf's x hx).congr_of_eventually_eq Heven.symm), rw [to_sym_bilin_form, bilin_form.coe_fn_mk, second_derivative_symmetric_of_eventually minor₂ (D23 zero_lt_two minor₁).has_fderiv_at] end include hfs hsurj lemma hB (hrank3 : ∀ (u v : E), ∃ w, w ≠ 0 ∧ ⟪u, w⟫ = 0 ∧ ⟪w, v⟫ = 0) : ∀ x' (hx' : x' ∈ s) u' v', ⟪u', v'⟫ = 0 → to_sym_bilin_form Hf x' u' v' = 0 := λ x' hx' u' v' huv', begin have hf := eventually_iff_exists_mem.mpr ⟨s, hs.mem_nhds hx', λ a ha, hfs a ha⟩, have Heven := eventually_eq_iff_exists_mem.mpr ⟨s, hs.mem_nhds hx', λ a ha, Hevens a ha⟩, have hf' := eventually_iff_exists_mem.mpr ⟨s, hs.mem_nhds hx', λ a ha, hf's a ha⟩, have h := eventually_iff_exists_mem.mpr ⟨s, hs.mem_nhds hx', λ a ha, hsurj a ha⟩, simp only [to_sym_bilin_form], rcases hrank3 u' v' with ⟨w', hw', huw', hwv'⟩, exact tot1 hf Hf Heven hw' huv' huw' hwv' hf' h end variables [complete_space E] [nontrivial E] (hrank3 : ∀ (u v : E), ∃ w, w ≠ 0 ∧ ⟪u, w⟫ = 0 ∧ ⟪w, v⟫ = 0) lemma diff_bilin {x : E} (hx : x ∈ s) : differentiable_at ℝ (λ x', bilin_form_factor (hB hs hfs hf's hsurj Hf Hevens hrank3) (λ y hy, is_sym_to_sym_bilin_form hs hf's Hf Hevens hy) x') x := begin rcases hrank3 0 0 with ⟨w₀, hw₀, _⟩, have hb := hB hs hfs hf's hsurj Hf Hevens hrank3, have hb' := λ y hy, is_sym_to_sym_bilin_form hs hf's Hf Hevens hy, have triv₁ : ⟪w₀, w₀⟫ ≠ 0 := λ W, hw₀ (inner_self_eq_zero.mp W), have minor₁ : (λ x', to_sym_bilin_form Hf x' w₀ w₀ / ⟪w₀, w₀⟫) =ᶠ[𝓝 x] λ x', (bilin_form_factor hb hb' x'), { refine eventually_eq_iff_exists_mem.mpr ⟨s, hs.mem_nhds hx, λ y hy, _⟩, simp only [congr_arg, bilin_form_factor_prop hb hb' hy], rw mul_div_cancel _ triv₁ }, simp only [to_sym_bilin_form, bilin_form.coe_fn_mk] at minor₁, refine differentiable_at.congr_of_eventually_eq _ minor₁.symm, simp only [div_eq_mul_inv, ← smul_eq_mul], apply differentiable_at.smul_const, have Heven := eventually_eq_iff_exists_mem.mpr ⟨s, hs.mem_nhds hx, λ a ha, Hevens a ha⟩, have triv₂ : (λ x', fderiv ℝ (fderiv ℝ $ λ y, similarity_factor_sqrt_inv y $ Hf y) x' w₀ w₀) = (apply ℝ _ w₀) ∘ (λ x', fderiv ℝ (fderiv ℝ $ λ y, similarity_factor_sqrt_inv y $ Hf y) x' w₀), { ext1, simp only [apply_apply, function.comp_app] }, rw triv₂, refine (apply ℝ ℝ w₀).differentiable_at.comp _ (DD2 zero_lt_one (D22 _) w₀), exact similarity_factor_sqrt_inv_times_cont_diff_at x Hf ((D22 $ hf's x hx).congr_of_eventually_eq Heven.symm) end localized "notation `H₁` := hB hs hfs hf's hsurj Hf Hevens hrank3" in liouville_do_not_use localized "notation `H₂` := λ y hy, is_sym_to_sym_bilin_form hs hf's Hf Hevens hy" in liouville_do_not_use lemma fderiv_fderiv_eq_bilin_form_factor_mul {x : E} (hx : x ∈ s) (u v : E) : (λ x', fderiv ℝ (fderiv ℝ $ λ y, similarity_factor_sqrt_inv y $ Hf y) x' v u) =ᶠ[𝓝 x] λ x', (bilin_form_factor H₁ H₂ x') * ⟪u, v⟫ := eventually_eq_iff_exists_mem.mpr ⟨s, hs.mem_nhds hx, λ y hy, by simpa [to_sym_bilin_form, bilin_form.coe_fn_congr] using bilin_form_factor_prop H₁ H₂ hy u v⟩ /-- Not sure if `is_connected s` is a correct hypothesis. But it seems that this argument is used to show that the `bilin_form_factor` is indeed a constant. -/ lemma is_const_bilin_form_factor (hs' : is_connected s) : ∃ (c : ℝ), ∀ x (hx : x ∈ s), bilin_form_factor H₁ H₂ x = c := begin rcases hs'.nonempty with ⟨x₀, hx₀⟩, refine ⟨bilin_form_factor H₁ H₂ x₀, λ x hx, _⟩, have : ∀ y ∈ s, fderiv ℝ (λ x', bilin_form_factor H₁ H₂ x') y = 0 := λ y hy, begin have triv₁ : ∀ᶠ x' in 𝓝 y, times_cont_diff_at ℝ 3 (λ y, similarity_factor_sqrt_inv y $ Hf y) x' := eventually_iff_exists_mem.mpr ⟨s, hs.mem_nhds hy, λ x' hx', similarity_factor_sqrt_inv_times_cont_diff_at x' Hf ((D22 $ hf's x' hx').congr_of_eventually_eq (eventually_eq_iff_exists_mem.mpr ⟨s, hs.mem_nhds hx', λ a ha, Hevens a ha⟩).symm)⟩, have minor₁ := fderiv_fderiv_eq_bilin_form_factor_mul hs hfs hf's hsurj Hf Hevens hrank3 hy, have minor₂ := diff_bilin hs hfs hf's hsurj Hf Hevens hrank3 hy, have minor₃ : ∀ u v w, fderiv ℝ (fderiv ℝ $ fderiv ℝ $ λ y, similarity_factor_sqrt_inv y $ Hf y) y w u v = fderiv ℝ (λ x', bilin_form_factor H₁ H₂ x') y w * ⟪u, v⟫ := λ u v w, begin have Heven := eventually_eq_iff_exists_mem.mpr ⟨s, hs.mem_nhds hy, λ a ha, Hevens a ha⟩, have subkey₁ := D21 (D22 $ similarity_factor_sqrt_inv_times_cont_diff_at _ Hf $ (D22 $ hf's y hy).congr_of_eventually_eq Heven.symm), rw [← DD1' subkey₁ (D23 zero_lt_one $ D22 triv₁.self_of_nhds), (minor₁ v u).fderiv_eq, fderiv_mul_const minor₂, smul_apply, real_inner_comm, smul_eq_mul, mul_comm] end, ext1 v, simp only [zero_apply], rcases hrank3 v v with ⟨w, hw, hvw, _⟩, have key_aux : fderiv ℝ (λ x', bilin_form_factor H₁ H₂ x') y w • v - fderiv ℝ (λ x', bilin_form_factor H₁ H₂ x') y v • w = 0 := by rw [← inner_self_eq_zero, inner_sub_right, real_inner_smul_right, real_inner_smul_right, ← minor₃, ← minor₃, third_order_symmetric triv₁, sub_self], have key := eq_of_sub_eq_zero key_aux, have minor₅ : (fderiv ℝ (λ x', bilin_form_factor H₁ H₂ x') y v) * (fderiv ℝ (λ x', bilin_form_factor H₁ H₂ x') y v) * ⟪w, w⟫ = 0 := by rw [mul_assoc, ← real_inner_smul_left, ← key, real_inner_smul_left, hvw, mul_zero, mul_zero], exact mul_self_eq_zero.mp (eq_zero_of_ne_zero_of_mul_right_eq_zero (λ W, hw $ inner_self_eq_zero.mp W) minor₅) end, exact hs.is_const_of_fderiv_eq_zero hs' (λ x' hx', (diff_bilin hs hfs hf's hsurj Hf Hevens hrank3 hx').differentiable_within_at) this hx hx₀ end end bilin_form_and_local_prop section integrate open continuous_linear_map filter variables {E F : Type*} [inner_product_space ℝ E] [inner_product_space ℝ F] {f : E → F} {s : set E} (hs : is_open s) (hs' : is_connected s) (hfs : ∀ x ∈ s, conformal_at f x) (hf's : ∀ x ∈ s, times_cont_diff_at ℝ 4 f x) (hsurj : ∀ x ∈ s , function.surjective (fderiv ℝ f x)) {f' : E → (E →L[ℝ] F)} (Hf : ∀ (x' : E), is_conformal_map $ f' x') (Hevens : ∀ x ∈ s, fderiv ℝ f x = f' x) variables [complete_space E] [nontrivial E] (hrank3 : ∀ (u v : E), ∃ w, w ≠ 0 ∧ ⟪u, w⟫ = 0 ∧ ⟪w, v⟫ = 0) localized "notation `H₁` := hB hs hfs hf's hsurj Hf Hevens hrank3" in liouville_do_not_use localized "notation `H₂` := λ y hy, is_sym_to_sym_bilin_form hs hf's Hf Hevens hy" in liouville_do_not_use include hs hs' hfs hf's hsurj Hf Hevens hrank3 open inner_product_space lemma similarity_factor_sqrt_inv_eq_const_mul_dist_add_const (hnonzero : ∀ x ∈ s, bilin_form_factor H₁ H₂ x ≠ 0) : ∃ (α β : ℝ) (hα : α ≠ 0) (x₀ : E), ∀ x ∈ s, similarity_factor_sqrt_inv x (Hf x) = α * ∥x - x₀∥ ^ 2 + β := begin rcases is_const_bilin_form_factor hs hfs hf's hsurj Hf Hevens hrank3 hs' with ⟨c, hc⟩, have key₁ : ∀ x ∈ s, fderiv ℝ (fderiv ℝ $ λ y, similarity_factor_sqrt_inv y $ Hf y) x = fderiv ℝ (λ y, c • to_dual y) x := λ x hx, begin ext v u, have triv₁ := (fderiv_fderiv_eq_bilin_form_factor_mul hs hfs hf's hsurj Hf Hevens hrank3 hx u v).self_of_nhds, simp only [congr_arg] at triv₁, rw [fderiv_const_smul (continuous_linear_equiv.differentiable_at _), smul_apply, smul_apply, continuous_linear_equiv.fderiv, to_dual.coe_coe, to_dual_apply, triv₁, smul_eq_mul, real_inner_comm, hc x hx] end, have triv₁ := λ y (hy : y ∈ s), (D23 zero_lt_two $ similarity_factor_sqrt_inv_times_cont_diff_at _ Hf $ (D22 $ hf's y hy).congr_of_eventually_eq (eventually_eq_iff_exists_mem.mpr ⟨s, hs.mem_nhds hy, λ a ha, Hevens a ha⟩).symm).differentiable_within_at, rcases hs.exists_of_fderiv_eq_fderiv hs' triv₁ (λ y hy, (continuous_linear_equiv.differentiable_within_at _).const_smul _) key₁ with ⟨map, h⟩, simp only [congr_arg] at h, have Hc : c ≠ 0 := λ W, begin rcases hs'.nonempty with ⟨x', hx'⟩, simp only [W] at hc, have := hnonzero x' hx', rw hc x' hx' at this, exact this rfl end, let x₀ := to_dual.symm (c⁻¹ • map), have triv₃ : c • to_dual x₀ = map := by simp only [x₀, to_dual.apply_symm_apply, smul_inv_smul' Hc], simp only [← triv₃, ← smul_sub, ← to_dual.map_sub] at h, have key₂ : ∀ x ∈ s, fderiv ℝ (λ y, similarity_factor_sqrt_inv y $ Hf y) x = fderiv ℝ (λ y, c / 2 * ⟪id y - x₀, id y⟫ - (c / 2) * to_dual x₀ (y - x₀)) x := λ x hx, begin ext1 v, rw [h x hx, fderiv_sub (((differentiable_at_id.sub_const x₀).inner differentiable_at_id).const_mul _)]; [skip, exact ((continuous_linear_map.differentiable_at _).comp _ $ differentiable_at_id.sub_const _).const_mul (c / 2)], rw [fderiv_const_mul ((differentiable_at_id.sub_const x₀).inner differentiable_at_id)], rw [smul_apply, sub_apply, smul_apply, fderiv_inner_apply (differentiable_at_id.sub_const x₀) differentiable_at_id], rw [fderiv_sub_const], simp only [fderiv_id], simp only [id_apply, _root_.id], rw [fderiv_const_mul]; [skip, exact ((continuous_linear_map.differentiable_at _).comp _ $ differentiable_at_id.sub_const _)], rw [fderiv.comp]; [skip, exact continuous_linear_map.differentiable_at _, exact differentiable_at_id.sub_const _], simp only [continuous_linear_map.fderiv, fderiv_sub_const, smul_apply, coe_comp', function.comp_app, to_dual_apply, fderiv_id', id_apply, smul_add], nth_rewrite 2 real_inner_comm, simp only [inner_sub_left, smul_sub, smul_eq_mul], ring end, have triv₄ := λ y hy, ((similarity_factor_sqrt_inv_times_cont_diff_at _ Hf $ (D22 $ hf's y hy).congr_of_eventually_eq (eventually_eq_iff_exists_mem.mpr ⟨s, hs.mem_nhds hy, λ a ha, Hevens a ha⟩).symm).differentiable_at $ by apply with_top.coe_le_coe.mpr; norm_num).differentiable_within_at, rcases hs.exists_of_fderiv_eq_fderiv hs' triv₄ _ key₂ with ⟨β, H⟩, simp only [congr_arg, _root_.id] at H, refine ⟨c / 2, -β, div_ne_zero Hc two_ne_zero, x₀, λ x hx, _⟩, convert H x hx, simp only [smul_eq_mul, to_dual_apply], rw [real_inner_comm, ← mul_sub, ← inner_sub_left, real_inner_self_eq_norm_sq, pow_two], intros y hy, refine ((((differentiable_at_id.sub_const x₀).inner differentiable_at_id).const_mul _).sub _).differentiable_within_at, exact ((continuous_linear_map.differentiable_at _).comp _ $ differentiable_at_id.sub_const _).const_mul (c / 2) end lemma similarity_factor_sqrt_inv_eq_inner_add_const (hzero : ∃ x ∈ s, bilin_form_factor H₁ H₂ x = 0) : ∃ (β : ℝ) (x₀ : E), ∀ x ∈ s, similarity_factor_sqrt_inv x (Hf x) = ⟪x, x₀⟫ + β := begin rcases is_const_bilin_form_factor hs hfs hf's hsurj Hf Hevens hrank3 hs' with ⟨c, hc⟩, have key₁ : ∀ x ∈ s, fderiv ℝ (fderiv ℝ $ λ y, similarity_factor_sqrt_inv y $ Hf y) x = fderiv ℝ (λ y, c • to_dual y) x := λ x hx, begin ext v u, have triv₁ := (fderiv_fderiv_eq_bilin_form_factor_mul hs hfs hf's hsurj Hf Hevens hrank3 hx u v).self_of_nhds, simp only [congr_arg] at triv₁, rw [fderiv_const_smul (continuous_linear_equiv.differentiable_at _), smul_apply, smul_apply, continuous_linear_equiv.fderiv, to_dual.coe_coe, to_dual_apply, triv₁, smul_eq_mul, real_inner_comm, hc x hx] end, have triv₁ := λ y (hy : y ∈ s), (D23 zero_lt_two $ similarity_factor_sqrt_inv_times_cont_diff_at _ Hf $ (D22 $ hf's y hy).congr_of_eventually_eq (eventually_eq_iff_exists_mem.mpr ⟨s, hs.mem_nhds hy, λ a ha, Hevens a ha⟩).symm).differentiable_within_at, rcases hs.exists_of_fderiv_eq_fderiv hs' triv₁ (λ y hy, (continuous_linear_equiv.differentiable_within_at _).const_smul _) key₁ with ⟨map, h⟩, simp only [congr_arg] at h, have Hc : c = 0 := begin rcases hzero with ⟨x'', hx'', Hx''⟩, rwa hc x'' hx'' at Hx'', end, simp only [Hc, zero_smul, zero_sub] at h, have key₂ : ∀ x ∈ s, fderiv ℝ (λ y, similarity_factor_sqrt_inv y $ Hf y) x = fderiv ℝ (-map : E →L[ℝ] ℝ) x := λ x hx, by ext1 v; rw [h x hx, (-map).fderiv], have triv₄ := λ y hy, ((similarity_factor_sqrt_inv_times_cont_diff_at _ Hf $ (D22 $ hf's y hy).congr_of_eventually_eq (eventually_eq_iff_exists_mem.mpr ⟨s, hs.mem_nhds hy, λ a ha, Hevens a ha⟩).symm).differentiable_at $ by apply with_top.coe_le_coe.mpr; norm_num).differentiable_within_at, rcases hs.exists_of_fderiv_eq_fderiv hs' triv₄ (continuous_linear_map.differentiable_on _) key₂ with ⟨β, H⟩, refine ⟨-β, to_dual.symm (-map), λ x hx, _⟩, rw [real_inner_comm, ← to_dual_apply, to_dual.apply_symm_apply], exact H x hx end end integrate section conformality_of_local_inverse variables {E : Type*} [inner_product_space ℝ E] [complete_space E] [nontrivial E] -- {f' : E → (E →L[ℝ] F)} (Hf : ∀ (x' : E), is_conformal_map $ f' x') -- (Hevens : ∀ x ∈ s, fderiv ℝ f x = f' x) -- def def_helper (f : E → E) (s : set E) (x : E) := -- if x ∈ s then fderiv ℝ f x else id ℝ E -- lemma def_helper_eq (f : local_homeomorph E E) (s : set E) {x : E} (hx : x ∈ s) : -- fderiv ℝ f x = def_helper f s x := -- by simp only [def_helper, if_pos hx] variables {f : local_homeomorph E E} {s : set E} (hs : is_open s) (hs' : is_connected s) (hs'' : s ⊆ f.source) (hfs : ∀ x ∈ s, conformal_at f x) (hf's : ∀ x ∈ s, times_cont_diff_at ℝ 4 f x) (hsurj : ∀ x ∈ s , function.surjective (fderiv ℝ f x)) -- lemma def_helper_is_conformal_map {x : E} : -- is_conformal_map (def_helper f s x) := -- begin -- simp only [def_helper], -- by_cases h : x ∈ s, -- { rw if_pos h, -- exact (conformal_at_iff_is_conformal_map_fderiv.mp $ hfs x h) }, -- { rw if_neg h, -- exact is_conformal_map_id } -- end include hfs hsurj def bijective_differentials {x : E} (hx : x ∈ s) : E ≃L[ℝ] E := continuous_linear_equiv.of_bijective (fderiv ℝ f x) (linear_map.ker_eq_bot.mpr (conformal_at_iff_is_conformal_map_fderiv.mp $ hfs x hx).injective) (linear_map.range_eq_top.mpr $ hsurj x hx) lemma bijective_differentials1 {x : E} (hx : x ∈ s) : (bijective_differentials hfs hsurj hx : E →L[ℝ] E) = fderiv ℝ f x := by simp only [bijective_differentials, continuous_linear_equiv.coe_of_bijective] lemma bijective_differentials2 {x : E} (hx : x ∈ s) : has_fderiv_at f (bijective_differentials hfs hsurj hx : E →L[ℝ] E) x := begin rw bijective_differentials1 hfs hsurj hx, exact (hfs x hx).differentiable_at.has_fderiv_at end end conformality_of_local_inverse -- h = u -- k = v -- l = w
16529616177611ee4d23f163b9090eca7499cee3
618003631150032a5676f229d13a079ac875ff77
/src/linear_algebra/linear_pmap.lean
613b44908637ee67c88f96dcf7bd44513af6a21f
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
15,709
lean
/- Copyright (c) 2020 Yury Kudryashov All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import linear_algebra.basic /-! # Partially defined linear maps A `linear_pmap R E F` is a linear map from a submodule of `E` to `F`. We define a `semilattice_inf_bot` instance on this this, and define three operations: * `mk_span_singleton` defines a partial linear map defined on the span of a singleton. * `sup` takes two partial linear maps `f`, `g` that agree on the intersection of their domains, and returns the unique partial linear map on `f.domain ⊔ g.domain` that extends both `f` and `g`. * `Sup` takes a `directed_on (≤)` set of partial linear maps, and returns the unique partial linear map on the `Sup` of their domains that extends all these maps. Partially defined maps are currently used in `mathlib` to prove Hahn-Banach theorem and its variations. Namely, `linear_pmap.Sup` implies that every chain of `linear_pmap`s is bounded above. Another possible use (not yet in `mathlib`) would be the theory of unbounded linear operators. -/ lemma subtype.coe_prop {α : Type*} {p : α → Prop} (x : subtype p) : p x := x.2 open set universes u v w /-- A `linear_pmap R E F` is a linear map from a submodule of `E` to `F`. -/ structure linear_pmap (R : Type u) [ring R] (E : Type v) [add_comm_group E] [module R E] (F : Type w) [add_comm_group F] [module R F] := (domain : submodule R E) (to_fun : domain →ₗ[R] F) variables {R : Type*} [ring R] {E : Type*} [add_comm_group E] [module R E] {F : Type*} [add_comm_group F] [module R F] {G : Type*} [add_comm_group G] [module R G] namespace linear_pmap open submodule instance : has_coe_to_fun (linear_pmap R E F) := ⟨λ f : linear_pmap R E F, f.domain → F, λ f, f.to_fun⟩ @[simp] lemma to_fun_eq_coe (f : linear_pmap R E F) (x : f.domain) : f.to_fun x = f x := rfl @[simp] lemma map_zero (f : linear_pmap R E F) : f 0 = 0 := f.to_fun.map_zero lemma map_add (f : linear_pmap R E F) (x y : f.domain) : f (x + y) = f x + f y := f.to_fun.map_add x y lemma map_neg (f : linear_pmap R E F) (x : f.domain) : f (-x) = -f x := f.to_fun.map_neg x lemma map_sub (f : linear_pmap R E F) (x y : f.domain) : f (x - y) = f x - f y := f.to_fun.map_sub x y lemma map_smul (f : linear_pmap R E F) (c : R) (x : f.domain) : f (c • x) = c • f x := f.to_fun.map_smul c x @[simp] lemma mk_apply (p : submodule R E) (f : p →ₗ[R] F) (x : p) : mk p f x = f x := rfl /-- The unique `linear_pmap` on `span R {x}` that sends `x` to `y`. This version works for modules over rings, and requires a proof of `∀ c, c • x = 0 → c • y = 0`. -/ noncomputable def mk_span_singleton' (x : E) (y : F) (H : ∀ c : R, c • x = 0 → c • y = 0) : linear_pmap R E F := begin replace H : ∀ c₁ c₂ : R, c₁ • x = c₂ • x → c₁ • y = c₂ • y, { intros c₁ c₂ h, rw [← sub_eq_zero, ← sub_smul] at h ⊢, exact H _ h }, refine ⟨span R {x}, λ z, _, _, _⟩, { exact (classical.some (mem_span_singleton.1 z.coe_prop) • y) }, { intros z₁ z₂, rw [← add_smul], apply H, simp only [add_smul, sub_smul, classical.some_spec (mem_span_singleton.1 _)], apply coe_add }, { intros c z, rw [smul_smul], apply H, simp only [mul_smul, classical.some_spec (mem_span_singleton.1 _)], apply coe_smul } end @[simp] lemma domain_mk_span_singleton (x : E) (y : F) (H : ∀ c : R, c • x = 0 → c • y = 0) : (mk_span_singleton' x y H).domain = span R {x} := rfl @[simp] lemma mk_span_singleton_apply (x : E) (y : F) (H : ∀ c : R, c • x = 0 → c • y = 0) (c : R) (h) : mk_span_singleton' x y H ⟨c • x, h⟩ = c • y := begin dsimp [mk_span_singleton'], rw [← sub_eq_zero, ← sub_smul], apply H, simp only [sub_smul, one_smul, sub_eq_zero], apply classical.some_spec (mem_span_singleton.1 h), end /-- The unique `linear_pmap` on `span R {x}` that sends a non-zero vector `x` to `y`. This version works for modules over division rings. -/ @[reducible] noncomputable def mk_span_singleton {K E F : Type*} [division_ring K] [add_comm_group E] [module K E] [add_comm_group F] [module K F] (x : E) (y : F) (hx : x ≠ 0) : linear_pmap K E F := mk_span_singleton' x y $ λ c hc, (smul_eq_zero.1 hc).elim (λ hc, by rw [hc, zero_smul]) (λ hx', absurd hx' hx) /-- Projection to the first coordinate as a `linear_pmap` -/ protected def fst (p : submodule R E) (p' : submodule R F) : linear_pmap R (E × F) E := { domain := p.prod p', to_fun := (linear_map.fst R E F).comp (p.prod p').subtype } @[simp] lemma fst_apply (p : submodule R E) (p' : submodule R F) (x : p.prod p') : linear_pmap.fst p p' x = (x : E × F).1 := rfl /-- Projection to the second coordinate as a `linear_pmap` -/ protected def snd (p : submodule R E) (p' : submodule R F) : linear_pmap R (E × F) F := { domain := p.prod p', to_fun := (linear_map.snd R E F).comp (p.prod p').subtype } @[simp] lemma snd_apply (p : submodule R E) (p' : submodule R F) (x : p.prod p') : linear_pmap.snd p p' x = (x : E × F).2 := rfl instance : has_neg (linear_pmap R E F) := ⟨λ f, ⟨f.domain, -f.to_fun⟩⟩ @[simp] lemma neg_apply (f : linear_pmap R E F) (x) : (-f) x = -(f x) := rfl instance : has_le (linear_pmap R E F) := ⟨λ f g, f.domain ≤ g.domain ∧ ∀ ⦃x : f.domain⦄ ⦃y : g.domain⦄ (h : (x:E) = y), f x = g y⟩ lemma eq_of_le_of_domain_eq {f g : linear_pmap R E F} (hle : f ≤ g) (heq : f.domain = g.domain) : f = g := begin rcases f with ⟨f_dom, f⟩, rcases g with ⟨g_dom, g⟩, change f_dom = g_dom at heq, subst g_dom, have : f = g, from linear_map.ext (λ x, hle.2 rfl), subst g end /-- Given two partial linear maps `f`, `g`, the set of points `x` such that both `f` and `g` are defined at `x` and `f x = g x` form a submodule. -/ def eq_locus (f g : linear_pmap R E F) : submodule R E := { carrier := {x | ∃ (hf : x ∈ f.domain) (hg : x ∈ g.domain), f ⟨x, hf⟩ = g ⟨x, hg⟩}, zero := ⟨zero_mem _, zero_mem _, f.map_zero.trans g.map_zero.symm⟩, add := λ x y ⟨hfx, hgx, hx⟩ ⟨hfy, hgy, hy⟩, ⟨add_mem _ hfx hfy, add_mem _ hgx hgy, by erw [f.map_add ⟨x, hfx⟩ ⟨y, hfy⟩, g.map_add ⟨x, hgx⟩ ⟨y, hgy⟩, hx, hy]⟩, smul := λ c x ⟨hfx, hgx, hx⟩, ⟨smul_mem _ c hfx, smul_mem _ c hgx, by erw [f.map_smul c ⟨x, hfx⟩, g.map_smul c ⟨x, hgx⟩, hx]⟩ } instance : has_inf (linear_pmap R E F) := ⟨λ f g, ⟨f.eq_locus g, f.to_fun.comp $ of_le $ λ x hx, hx.fst⟩⟩ instance : has_bot (linear_pmap R E F) := ⟨⟨⊥, 0⟩⟩ instance : inhabited (linear_pmap R E F) := ⟨⊥⟩ instance : semilattice_inf_bot (linear_pmap R E F) := { le := (≤), le_refl := λ f, ⟨le_refl f.domain, λ x y h, subtype.eq h ▸ rfl⟩, le_trans := λ f g h ⟨fg_le, fg_eq⟩ ⟨gh_le, gh_eq⟩, ⟨le_trans fg_le gh_le, λ x z hxz, have hxy : (x:E) = of_le fg_le x, from rfl, (fg_eq hxy).trans (gh_eq $ hxy.symm.trans hxz)⟩, le_antisymm := λ f g fg gf, eq_of_le_of_domain_eq fg (le_antisymm fg.1 gf.1), bot := ⊥, bot_le := λ f, ⟨bot_le, λ x y h, have hx : x = 0, from subtype.eq ((mem_bot R).1 x.2), have hy : y = 0, from subtype.eq (h.symm.trans (congr_arg _ hx)), by rw [hx, hy, map_zero, map_zero]⟩, inf := (⊓), le_inf := λ f g h ⟨fg_le, fg_eq⟩ ⟨fh_le, fh_eq⟩, ⟨λ x hx, ⟨fg_le hx, fh_le hx, by refine (fg_eq _).symm.trans (fh_eq _); [exact ⟨x, hx⟩, refl, refl]⟩, λ x ⟨y, yg, hy⟩ h, by { apply fg_eq, exact h }⟩, inf_le_left := λ f g, ⟨λ x hx, hx.fst, λ x y h, congr_arg f $ subtype.eq $ by exact h⟩, inf_le_right := λ f g, ⟨λ x hx, hx.snd.fst, λ ⟨x, xf, xg, hx⟩ y h, hx.trans $ congr_arg g $ subtype.eq $ by exact h⟩ } lemma le_of_eq_locus_ge {f g : linear_pmap R E F} (H : f.domain ≤ f.eq_locus g) : f ≤ g := suffices f ≤ f ⊓ g, from le_trans this inf_le_right, ⟨H, λ x y hxy, ((inf_le_left : f ⊓ g ≤ f).2 hxy.symm).symm⟩ lemma domain_mono : strict_mono (@domain R _ E _ _ F _ _) := λ f g hlt, lt_of_le_of_ne hlt.1.1 $ λ heq, ne_of_lt hlt $ eq_of_le_of_domain_eq (le_of_lt hlt) heq private lemma sup_aux (f g : linear_pmap R E F) (h : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y) : ∃ fg : ↥(f.domain ⊔ g.domain) →ₗ[R] F, ∀ (x : f.domain) (y : g.domain) (z), (x:E) + y = ↑z → fg z = f x + g y := begin choose x hx y hy hxy using λ z : f.domain ⊔ g.domain, mem_sup.1 z.coe_prop, set fg := λ z, f ⟨x z, hx z⟩ + g ⟨y z, hy z⟩, have fg_eq : ∀ (x' : f.domain) (y' : g.domain) (z' : f.domain ⊔ g.domain) (H : (x':E) + y' = z'), fg z' = f x' + g y', { intros x' y' z' H, dsimp [fg], rw [add_comm, ← sub_eq_sub_iff_add_eq_add, eq_comm, ← map_sub, ← map_sub], apply h, simp only [← eq_sub_iff_add_eq] at hxy, simp only [coe_sub, coe_mk, coe_mk, hxy, ← sub_add, ← sub_sub, sub_self, zero_sub, ← H], apply neg_add_eq_sub }, refine ⟨⟨fg, _, _⟩, fg_eq⟩, { rintros ⟨z₁, hz₁⟩ ⟨z₂, hz₂⟩, rw [← add_assoc, add_right_comm (f _), ← map_add, add_assoc, ← map_add], apply fg_eq, simp only [coe_add, coe_mk, ← add_assoc], rw [add_right_comm (x _), hxy, add_assoc, hxy, coe_mk, coe_mk] }, { intros c z, rw [smul_add, ← map_smul, ← map_smul], apply fg_eq, simp only [coe_smul, coe_mk, ← smul_add, hxy] }, end /-- Given two partial linear maps that agree on the intersection of their domains, `f.sup g h` is the unique partial linear map on `f.domain ⊔ g.domain` that agrees with `f` and `g`. -/ protected noncomputable def sup (f g : linear_pmap R E F) (h : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y) : linear_pmap R E F := ⟨_, classical.some (sup_aux f g h)⟩ @[simp] lemma domain_sup (f g : linear_pmap R E F) (h : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y) : (f.sup g h).domain = f.domain ⊔ g.domain := rfl lemma sup_apply {f g : linear_pmap R E F} (H : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y) (x y z) (hz : (↑x:E) + ↑y = ↑z) : f.sup g H z = f x + g y := classical.some_spec (sup_aux f g H) x y z hz protected lemma left_le_sup (f g : linear_pmap R E F) (h : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y) : f ≤ f.sup g h := begin refine ⟨le_sup_left, λ z₁ z₂ hz, _⟩, rw [← add_zero (f _), ← g.map_zero], refine (sup_apply h _ _ _ _).symm, simpa end protected lemma right_le_sup (f g : linear_pmap R E F) (h : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y) : g ≤ f.sup g h := begin refine ⟨le_sup_right, λ z₁ z₂ hz, _⟩, rw [← zero_add (g _), ← f.map_zero], refine (sup_apply h _ _ _ _).symm, simpa end protected lemma sup_le {f g h : linear_pmap R E F} (H : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y) (fh : f ≤ h) (gh : g ≤ h) : f.sup g H ≤ h := have Hf : f ≤ (f.sup g H) ⊓ h, from le_inf (f.left_le_sup g H) fh, have Hg : g ≤ (f.sup g H) ⊓ h, from le_inf (f.right_le_sup g H) gh, le_of_eq_locus_ge $ sup_le Hf.1 Hg.1 /-- Hypothesis for `linear_pmap.sup` holds, if `f.domain` is disjoint with `g.domain`. -/ lemma sup_h_of_disjoint (f g : linear_pmap R E F) (h : disjoint f.domain g.domain) (x : f.domain) (y : g.domain) (hxy : (x:E) = y) : f x = g y := begin rw [disjoint_def] at h, have hy : y = 0, from subtype.eq (h y (hxy ▸ x.2) y.2), have hx : x = 0, from subtype.eq (hxy.trans $ congr_arg _ hy), simp [*] end private lemma Sup_aux (c : set (linear_pmap R E F)) (hc : directed_on (≤) c) : ∃ f : ↥(Sup (domain '' c)) →ₗ[R] F, (⟨_, f⟩ : linear_pmap R E F) ∈ upper_bounds c := begin cases c.eq_empty_or_nonempty with ceq cne, { subst c, simp }, have hdir : directed_on (≤) (domain '' c), from (directed_on_image _).2 (hc.mono _ domain_mono.monotone), have P : Π x : Sup (domain '' c), {p : c // (x : E) ∈ p.val.domain }, { rintros x, apply classical.indefinite_description, have := (mem_Sup_of_directed (cne.image _) hdir).1 x.2, rwa [bex_image_iff, set_coe.exists'] at this }, set f : Sup (domain '' c) → F := λ x, (P x).val.val ⟨x, (P x).property⟩, have f_eq : ∀ (p : c) (x : Sup (domain '' c)) (y : p.1.1) (hxy : (x : E) = y), f x = p.1 y, { intros p x y hxy, rcases hc (P x).1.1 (P x).1.2 p.1 p.2 with ⟨q, hqc, hxq, hpq⟩, refine (hxq.2 _).trans (hpq.2 _).symm, exacts [of_le hpq.1 y, hxy, rfl] }, refine ⟨⟨f, _, _⟩, _⟩, { intros x y, rcases hc (P x).1.1 (P x).1.2 (P y).1.1 (P y).1.2 with ⟨p, hpc, hpx, hpy⟩, set x' := of_le hpx.1 ⟨x, (P x).2⟩, set y' := of_le hpy.1 ⟨y, (P y).2⟩, rw [f_eq ⟨p, hpc⟩ x x' rfl, f_eq ⟨p, hpc⟩ y y' rfl, f_eq ⟨p, hpc⟩ (x + y) (x' + y') rfl, map_add] }, { intros c x, rw [f_eq (P x).1 (c • x) (c • ⟨x, (P x).2⟩) rfl, ← map_smul] }, { intros p hpc, refine ⟨le_Sup $ mem_image_of_mem domain hpc, λ x y hxy, eq.symm _⟩, exact f_eq ⟨p, hpc⟩ _ _ hxy.symm } end /-- Glue a collection of partially defined linear maps to a linear map defined on `Sup` of these submodules. -/ protected noncomputable def Sup (c : set (linear_pmap R E F)) (hc : directed_on (≤) c) : linear_pmap R E F := ⟨_, classical.some $ Sup_aux c hc⟩ protected lemma le_Sup {c : set (linear_pmap R E F)} (hc : directed_on (≤) c) {f : linear_pmap R E F} (hf : f ∈ c) : f ≤ linear_pmap.Sup c hc := classical.some_spec (Sup_aux c hc) hf protected lemma Sup_le {c : set (linear_pmap R E F)} (hc : directed_on (≤) c) {g : linear_pmap R E F} (hg : ∀ f ∈ c, f ≤ g) : linear_pmap.Sup c hc ≤ g := le_of_eq_locus_ge $ Sup_le $ λ _ ⟨f, hf, eq⟩, eq ▸ have f ≤ (linear_pmap.Sup c hc) ⊓ g, from le_inf (linear_pmap.le_Sup _ hf) (hg f hf), this.1 end linear_pmap namespace linear_map /-- Restrict a linear map to a submodule, reinterpreting the result as a `linear_pmap`. -/ def to_pmap (f : E →ₗ[R] F) (p : submodule R E) : linear_pmap R E F := ⟨p, f.comp p.subtype⟩ @[simp] lemma to_pmap_apply (f : E →ₗ[R] F) (p : submodule R E) (x : p) : f.to_pmap p x = f x := rfl /-- Compose a linear map with a `linear_pmap` -/ def comp_pmap (g : F →ₗ[R] G) (f : linear_pmap R E F) : linear_pmap R E G := { domain := f.domain, to_fun := g.comp f.to_fun } @[simp] lemma comp_pmap_apply (g : F →ₗ[R] G) (f : linear_pmap R E F) (x) : g.comp_pmap f x = g (f x) := rfl end linear_map namespace linear_pmap /-- Restrict codomain of a `linear_pmap` -/ def cod_restrict (f : linear_pmap R E F) (p : submodule R F) (H : ∀ x, f x ∈ p) : linear_pmap R E p := { domain := f.domain, to_fun := f.to_fun.cod_restrict p H } /-- Compose two `linear_pmap`s -/ def comp (g : linear_pmap R F G) (f : linear_pmap R E F) (H : ∀ x : f.domain, f x ∈ g.domain) : linear_pmap R E G := g.to_fun.comp_pmap $ f.cod_restrict _ H /-- `f.coprod g` is the partially defined linear map defined on `f.domain × g.domain`, and sending `p` to `f p.1 + g p.2`. -/ def coprod (f : linear_pmap R E G) (g : linear_pmap R F G) : linear_pmap R (E × F) G := { domain := f.domain.prod g.domain, to_fun := (f.comp (linear_pmap.fst f.domain g.domain) (λ x, x.2.1)).to_fun + (g.comp (linear_pmap.snd f.domain g.domain) (λ x, x.2.2)).to_fun } @[simp] lemma coprod_apply (f : linear_pmap R E G) (g : linear_pmap R F G) (x) : f.coprod g x = f ⟨(x : E × F).1, x.2.1⟩ + g ⟨(x : E × F).2, x.2.2⟩ := rfl end linear_pmap
9b05b07a939978b5b3abfad1c5fba450abd69631
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/nat/choose/factorization.lean
35268691fd3dd0acbd287a1442b986eeb0e8a2b5
[ "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
6,538
lean
/- Copyright (c) 2022 Bolton Bailey. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bolton Bailey, Patrick Stevens, Thomas Browning -/ import data.nat.choose.central import data.nat.factorization.basic import data.nat.multiplicity /-! # Factorization of Binomial Coefficients > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file contains a few results on the multiplicity of prime factors within certain size bounds in binomial coefficients. These include: * `nat.factorization_choose_le_log`: a logarithmic upper bound on the multiplicity of a prime in a binomial coefficient. * `nat.factorization_choose_le_one`: Primes above `sqrt n` appear at most once in the factorization of `n` choose `k`. * `nat.factorization_central_binom_of_two_mul_self_lt_three_mul`: Primes from `2 * n / 3` to `n` do not appear in the factorization of the `n`th central binomial coefficient. * `nat.factorization_choose_eq_zero_of_lt`: Primes greater than `n` do not appear in the factorization of `n` choose `k`. These results appear in the [Erdős proof of Bertrand's postulate](aigner1999proofs). -/ open_locale big_operators namespace nat variables {p n k : ℕ} /-- A logarithmic upper bound on the multiplicity of a prime in a binomial coefficient. -/ lemma factorization_choose_le_log : (choose n k).factorization p ≤ log p n := begin by_cases h : (choose n k).factorization p = 0, { simp [h] }, have hp : p.prime := not.imp_symm (choose n k).factorization_eq_zero_of_non_prime h, have hkn : k ≤ n, { refine le_of_not_lt (λ hnk, h _), simp [choose_eq_zero_of_lt hnk] }, rw [factorization_def _ hp, @padic_val_nat_def _ ⟨hp⟩ _ (choose_pos hkn)], simp only [hp.multiplicity_choose hkn (lt_add_one _), part_enat.get_coe], refine (finset.card_filter_le _ _).trans (le_of_eq (nat.card_Ico _ _)), end /-- A `pow` form of `nat.factorization_choose_le` -/ lemma pow_factorization_choose_le (hn : 0 < n) : p ^ (choose n k).factorization p ≤ n := pow_le_of_le_log hn.ne' factorization_choose_le_log /-- Primes greater than about `sqrt n` appear only to multiplicity 0 or 1 in the binomial coefficient. -/ lemma factorization_choose_le_one (p_large : n < p ^ 2) : (choose n k).factorization p ≤ 1 := begin apply factorization_choose_le_log.trans, rcases eq_or_ne n 0 with rfl | hn0, { simp }, exact lt_succ_iff.1 (log_lt_of_lt_pow hn0 p_large), end lemma factorization_choose_of_lt_three_mul (hp' : p ≠ 2) (hk : p ≤ k) (hk' : p ≤ n - k) (hn : n < 3 * p) : (choose n k).factorization p = 0 := begin cases em' p.prime with hp hp, { exact factorization_eq_zero_of_non_prime (choose n k) hp }, cases lt_or_le n k with hnk hkn, { simp [choose_eq_zero_of_lt hnk] }, rw [factorization_def _ hp, @padic_val_nat_def _ ⟨hp⟩ _ (choose_pos hkn)], simp only [hp.multiplicity_choose hkn (lt_add_one _), part_enat.get_coe, finset.card_eq_zero, finset.filter_eq_empty_iff, not_le], intros i hi, rcases eq_or_lt_of_le (finset.mem_Ico.mp hi).1 with rfl | hi, { rw [pow_one, ←add_lt_add_iff_left (2 * p), ←succ_mul, two_mul, add_add_add_comm], exact lt_of_le_of_lt (add_le_add (add_le_add_right (le_mul_of_one_le_right' ((one_le_div_iff hp.pos).mpr hk)) (k % p)) (add_le_add_right (le_mul_of_one_le_right' ((one_le_div_iff hp.pos).mpr hk')) ((n - k) % p))) (by rwa [div_add_mod, div_add_mod, add_tsub_cancel_of_le hkn]) }, { replace hn : n < p ^ i, { calc n < 3 * p : hn ... ≤ p * p : mul_le_mul_right' (lt_of_le_of_ne hp.two_le hp'.symm) p ... = p ^ 2 : (sq p).symm ... ≤ p ^ i : pow_le_pow hp.one_lt.le hi }, rwa [mod_eq_of_lt (lt_of_le_of_lt hkn hn), mod_eq_of_lt (lt_of_le_of_lt tsub_le_self hn), add_tsub_cancel_of_le hkn] }, end /-- Primes greater than about `2 * n / 3` and less than `n` do not appear in the factorization of `central_binom n`. -/ lemma factorization_central_binom_of_two_mul_self_lt_three_mul (n_big : 2 < n) (p_le_n : p ≤ n) (big : 2 * n < 3 * p) : (central_binom n).factorization p = 0 := begin refine factorization_choose_of_lt_three_mul _ p_le_n (p_le_n.trans _) big, { rintro rfl, linarith }, { rw [two_mul, add_tsub_cancel_left] }, end lemma factorization_factorial_eq_zero_of_lt (h : n < p) : (factorial n).factorization p = 0 := begin induction n with n hn, { simp }, rw [factorial_succ, factorization_mul n.succ_ne_zero n.factorial_ne_zero, finsupp.coe_add, pi.add_apply, hn (lt_of_succ_lt h), add_zero, factorization_eq_zero_of_lt h], end lemma factorization_choose_eq_zero_of_lt (h : n < p) : (choose n k).factorization p = 0 := begin by_cases hnk : n < k, { simp [choose_eq_zero_of_lt hnk] }, rw [choose_eq_factorial_div_factorial (le_of_not_lt hnk), factorization_div (factorial_mul_factorial_dvd_factorial (le_of_not_lt hnk)), finsupp.coe_tsub, pi.sub_apply, factorization_factorial_eq_zero_of_lt h, zero_tsub], end /-- If a prime `p` has positive multiplicity in the `n`th central binomial coefficient, `p` is no more than `2 * n` -/ lemma factorization_central_binom_eq_zero_of_two_mul_lt (h : 2 * n < p) : (central_binom n).factorization p = 0 := factorization_choose_eq_zero_of_lt h /-- Contrapositive form of `nat.factorization_central_binom_eq_zero_of_two_mul_lt` -/ lemma le_two_mul_of_factorization_central_binom_pos (h_pos : 0 < (central_binom n).factorization p) : p ≤ 2 * n := le_of_not_lt (pos_iff_ne_zero.mp h_pos ∘ factorization_central_binom_eq_zero_of_two_mul_lt) /-- A binomial coefficient is the product of its prime factors, which are at most `n`. -/ lemma prod_pow_factorization_choose (n k : ℕ) (hkn : k ≤ n) : ∏ p in (finset.range (n + 1)), p ^ ((nat.choose n k).factorization p) = choose n k := begin nth_rewrite_rhs 0 ←factorization_prod_pow_eq_self (choose_pos hkn).ne', rw eq_comm, apply finset.prod_subset, { intros p hp, rw finset.mem_range, contrapose! hp, rw [finsupp.mem_support_iff, not_not, factorization_choose_eq_zero_of_lt hp] }, { intros p _ h2, simp [not_not.1 (mt finsupp.mem_support_iff.2 h2)] }, end /-- The `n`th central binomial coefficient is the product of its prime factors, which are at most `2n`. -/ lemma prod_pow_factorization_central_binom (n : ℕ) : ∏ p in (finset.range (2 * n + 1)), p ^ ((central_binom n).factorization p) = central_binom n := begin apply prod_pow_factorization_choose, linarith, end end nat
6beffd82bb33832e6911894eab934a5985240e44
94637389e03c919023691dcd05bd4411b1034aa5
/src/zzz_junk/lang/imp.lean
0616b4856b64d7b14eee4f65088faa409cdba179
[]
no_license
kevinsullivan/complogic-s21
7c4eef2105abad899e46502270d9829d913e8afc
99039501b770248c8ceb39890be5dfe129dc1082
refs/heads/master
1,682,985,669,944
1,621,126,241,000
1,621,126,241,000
335,706,272
0
38
null
1,618,325,669,000
1,612,374,118,000
Lean
UTF-8
Lean
false
false
2,011
lean
import .arith_expr import .bool_expr /- Syntax of our little language (OLL). OLL supports mutable variables and values of types arithmetic (bool) and arithmetic (nat). It has an assignment command for each of these two types. It also supports sequential composition of smaller programs into larger ones. -/ inductive cmd : Type | skip | a_assn (v : var nat) (e : arith_expr) | b_assn (v : var bool) (e : bool_expr) | seq (c1 c2 : cmd) : cmd | ifelse (b : bool_expr) (c1 c2 : cmd) -- | cond (b : bool_expr) (c1 c2 : cmd) : cmd -- | while (b : bool_expr) (c : cmd) : cmd open cmd notation v = e := b_assn v e notation v = a := a_assn v a notation c1 ; c2 := seq c1 c2 notation `cond ` b ` then ` c1 ` else ` c2 := ifelse b c1 c2 /- Computational definition of semantics -/ def c_eval : cmd → env → env | skip st := st | (b_assn v e) st := override_bool st v e | (a_assn v e) st := override_nat st v e | (c1 ; c2) st := c_eval c2 (c_eval c1 st) -- new | (IF b THEN c1 ELSE c2) st := if (bool_eval b st) then c_eval c1 st else c_eval c2 st /- Logical definition of semantics -/ inductive c_sem : cmd → env → env → Prop | c_sem_skip : ∀ (st : env), c_sem skip st st | c_sem_arith_assn : ∀ (pre post : env) (v : var nat) (e : arith_expr), (override_nat pre v e = post) → c_sem (a_assn v e) pre post | c_sem_bool_assn : ∀ (pre post : env) (v : var bool) (e : bool_expr), (override_bool pre v e = post) → c_sem (b_assn v e) pre post | c_sem_seq : ∀ (pre is post : env) (c1 c2 : cmd), c_sem c1 pre is → c_sem c2 is post → c_sem (c1 ; c2) pre post -- New | c_sem_if_false : ∀ (pre is post : env) (b : bool_expr) (c1 c2 : cmd), bool_eval b pre = ff → c_sem c2 pre post → c_sem (IF b THEN c1 ELSE c2) pre post | c_sem_if_true : ∀ (pre is post : env) (b : bool_expr) (c1 c2 : cmd), bool_eval b pre = ff → c_sem c1 pre post → c_sem (IF b THEN c1 ELSE c2) pre post
86a8579bee3b85522079bb0beb1a13c95456ce98
491068d2ad28831e7dade8d6dff871c3e49d9431
/tests/lean/num3.lean
bba618b9834abdef1d56fc6eea538fe57a785556
[ "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
199
lean
import data.num set_option pp.notation false set_option pp.implicit true constant N : Type.{1} constant z : N constant o : N constant a : N notation 0 := z notation 1 := o check a = 0 check 2 = 1
a6f9d36af22844ad677708eca0af5829b3272f69
b7f22e51856f4989b970961f794f1c435f9b8f78
/library/algebra/lattice.lean
f5a9022d146eefc1546984683af1c02ad9fa0c25
[ "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
5,163
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad -/ import .order variable {A : Type} /- lattices (we could split this to upper- and lower-semilattices, if needed) -/ structure lattice [class] (A : Type) extends weak_order A := (inf : A → A → A) (sup : A → A → A) (inf_le_left : ∀ a b, le (inf a b) a) (inf_le_right : ∀ a b, le (inf a b) b) (le_inf : ∀a b c, le c a → le c b → le c (inf a b)) (le_sup_left : ∀ a b, le a (sup a b)) (le_sup_right : ∀ a b, le b (sup a b)) (sup_le : ∀ a b c, le a c → le b c → le (sup a b) c) definition inf := @lattice.inf definition sup := @lattice.sup infix ` ⊓ `:70 := inf infix ` ⊔ `:65 := sup section variable [s : lattice A] include s theorem inf_le_left (a b : A) : a ⊓ b ≤ a := !lattice.inf_le_left theorem inf_le_right (a b : A) : a ⊓ b ≤ b := !lattice.inf_le_right theorem le_inf {a b c : A} (H₁ : c ≤ a) (H₂ : c ≤ b) : c ≤ a ⊓ b := !lattice.le_inf H₁ H₂ theorem le_sup_left (a b : A) : a ≤ a ⊔ b := !lattice.le_sup_left theorem le_sup_right (a b : A) : b ≤ a ⊔ b := !lattice.le_sup_right theorem sup_le {a b c : A} (H₁ : a ≤ c) (H₂ : b ≤ c) : a ⊔ b ≤ c := !lattice.sup_le H₁ H₂ /- inf -/ theorem eq_inf {a b c : A} (H₁ : c ≤ a) (H₂ : c ≤ b) (H₃ : ∀{d}, d ≤ a → d ≤ b → d ≤ c) : c = a ⊓ b := le.antisymm (le_inf H₁ H₂) (H₃ !inf_le_left !inf_le_right) theorem inf.comm (a b : A) : a ⊓ b = b ⊓ a := eq_inf !inf_le_right !inf_le_left (λ c H₁ H₂, le_inf H₂ H₁) theorem inf.assoc (a b c : A) : (a ⊓ b) ⊓ c = a ⊓ (b ⊓ c) := begin apply eq_inf, { apply le.trans, apply inf_le_left, apply inf_le_left }, { apply le_inf, apply le.trans, apply inf_le_left, apply inf_le_right, apply inf_le_right }, { intros [d, H₁, H₂], apply le_inf, apply le_inf H₁, apply le.trans H₂, apply inf_le_left, apply le.trans H₂, apply inf_le_right } end theorem inf.left_comm (a b c : A) : a ⊓ (b ⊓ c) = b ⊓ (a ⊓ c) := binary.left_comm (@inf.comm A s) (@inf.assoc A s) a b c theorem inf.right_comm (a b c : A) : (a ⊓ b) ⊓ c = (a ⊓ c) ⊓ b := binary.right_comm (@inf.comm A s) (@inf.assoc A s) a b c theorem inf_self (a : A) : a ⊓ a = a := by apply eq.symm; apply eq_inf (le.refl a) !le.refl; intros; assumption theorem inf_eq_left {a b : A} (H : a ≤ b) : a ⊓ b = a := by apply eq.symm; apply eq_inf !le.refl H; intros; assumption theorem inf_eq_right {a b : A} (H : b ≤ a) : a ⊓ b = b := eq.subst !inf.comm (inf_eq_left H) /- sup -/ theorem eq_sup {a b c : A} (H₁ : a ≤ c) (H₂ : b ≤ c) (H₃ : ∀{d}, a ≤ d → b ≤ d → c ≤ d) : c = a ⊔ b := le.antisymm (H₃ !le_sup_left !le_sup_right) (sup_le H₁ H₂) theorem sup.comm (a b : A) : a ⊔ b = b ⊔ a := eq_sup !le_sup_right !le_sup_left (λ c H₁ H₂, sup_le H₂ H₁) theorem sup.assoc (a b c : A) : (a ⊔ b) ⊔ c = a ⊔ (b ⊔ c) := begin apply eq_sup, { apply le.trans, apply le_sup_left a b, apply le_sup_left }, { apply sup_le, apply le.trans, apply le_sup_right a b, apply le_sup_left, apply le_sup_right }, { intros [d, H₁, H₂], apply sup_le, apply sup_le H₁, apply le.trans !le_sup_left H₂, apply le.trans !le_sup_right H₂} end theorem sup.left_comm (a b c : A) : a ⊔ (b ⊔ c) = b ⊔ (a ⊔ c) := binary.left_comm (@sup.comm A s) (@sup.assoc A s) a b c theorem sup.right_comm (a b c : A) : (a ⊔ b) ⊔ c = (a ⊔ c) ⊔ b := binary.right_comm (@sup.comm A s) (@sup.assoc A s) a b c theorem sup_self (a : A) : a ⊔ a = a := by apply eq.symm; apply eq_sup (le.refl a) !le.refl; intros; assumption theorem sup_eq_left {a b : A} (H : b ≤ a) : a ⊔ b = a := by apply eq.symm; apply eq_sup !le.refl H; intros; assumption theorem sup_eq_right {a b : A} (H : a ≤ b) : a ⊔ b = b := eq.subst !sup.comm (sup_eq_left H) end /- lattice instances -/ definition lattice_Prop [instance] : lattice Prop := ⦃ lattice, weak_order_Prop, inf := and, le_inf := take a b c Ha Hb Hc, and.intro (Ha Hc) (Hb Hc), inf_le_left := @and.elim_left, inf_le_right := @and.elim_right, sup := or, sup_le := @or.rec, le_sup_left := @or.intro_left, le_sup_right := @or.intro_right ⦄ definition lattice_fun [instance] (A B : Type) [lattice B] : lattice (A → B) := ⦃ lattice, weak_order_fun A B, inf := λf g x, inf (f x) (g x), le_inf := take f g h Hf Hg x, le_inf (Hf x) (Hg x), inf_le_left := take f g x, !inf_le_left, inf_le_right := take f g x, !inf_le_right, sup := λf g x, sup (f x) (g x), sup_le := take f g h Hf Hg x, sup_le (Hf x) (Hg x), le_sup_left := take f g x, !le_sup_left, le_sup_right := take t g x, !le_sup_right ⦄ /- Should we add a trans-instance from total orders to lattices? If we added we should add it with lower priority: Prop is added as a lattice, but in the classical case it is a total order! -/
8b747af4e2ec07ccf12afc5009b23435d0546ead
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/group_theory/group_action/group.lean
b28ba8a5517ee9a8f2f01570885edf30b3676e73
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
6,208
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import group_theory.group_action.defs import algebra.group.units import algebra.group_with_zero import data.equiv.mul_add import data.equiv.mul_add_aut import group_theory.perm.basic import group_theory.group_action.units /-! # Group actions applied to various types of group This file contains lemmas about `smul` on `group_with_zero`, and `group`. -/ universes u v w variables {α : Type u} {β : Type v} {γ : Type w} section mul_action section group variables [group α] [mul_action α β] @[simp, to_additive] lemma inv_smul_smul (c : α) (x : β) : c⁻¹ • c • x = x := by rw [smul_smul, mul_left_inv, one_smul] @[simp, to_additive] lemma smul_inv_smul (c : α) (x : β) : c • c⁻¹ • x = x := by rw [smul_smul, mul_right_inv, one_smul] /-- Given an action of a group `α` on `β`, each `g : α` defines a permutation of `β`. -/ @[to_additive] def mul_action.to_perm (a : α) : equiv.perm β := ⟨λ x, a • x, λ x, a⁻¹ • x, inv_smul_smul a, smul_inv_smul a⟩ /-- Given an action of an additive group `α` on `β`, each `g : α` defines a permutation of `β`. -/ add_decl_doc add_action.to_perm variables (α) (β) /-- Given an action of a group `α` on a set `β`, each `g : α` defines a permutation of `β`. -/ def mul_action.to_perm_hom : α →* equiv.perm β := { to_fun := mul_action.to_perm, map_one' := equiv.ext $ one_smul α, map_mul' := λ u₁ u₂, equiv.ext $ mul_smul (u₁:α) u₂ } /-- Given an action of a additive group `α` on a set `β`, each `g : α` defines a permutation of `β`. -/ def add_action.to_perm_hom (α : Type*) [add_group α] [add_action α β] : α →+ additive (equiv.perm β) := { to_fun := λ a, additive.of_mul $ add_action.to_perm a, map_zero' := equiv.ext $ zero_vadd α, map_add' := λ a₁ a₂, equiv.ext $ add_vadd a₁ a₂ } variables {α} {β} @[to_additive] lemma inv_smul_eq_iff {a : α} {x y : β} : a⁻¹ • x = y ↔ x = a • y := (mul_action.to_perm a).symm_apply_eq @[to_additive] lemma eq_inv_smul_iff {a : α} {x y : β} : x = a⁻¹ • y ↔ a • x = y := (mul_action.to_perm a).eq_symm_apply lemma smul_inv [group β] [smul_comm_class α β β] [is_scalar_tower α β β] (c : α) (x : β) : (c • x)⁻¹ = c⁻¹ • x⁻¹ := by rw [inv_eq_iff_mul_eq_one, smul_mul_smul, mul_right_inv, mul_right_inv, one_smul] @[to_additive] protected lemma mul_action.bijective (g : α) : function.bijective (λ b : β, g • b) := (mul_action.to_perm g).bijective @[to_additive] protected lemma mul_action.injective (g : α) : function.injective (λ b : β, g • b) := (mul_action.bijective g).injective @[to_additive] lemma smul_left_cancel (g : α) {x y : β} (h : g • x = g • y) : x = y := mul_action.injective g h @[simp, to_additive] lemma smul_left_cancel_iff (g : α) {x y : β} : g • x = g • y ↔ x = y := (mul_action.injective g).eq_iff @[to_additive] lemma smul_eq_iff_eq_inv_smul (g : α) {x y : β} : g • x = y ↔ x = g⁻¹ • y := (mul_action.to_perm g).apply_eq_iff_eq_symm_apply end group section gwz variables [group_with_zero α] [mul_action α β] @[simp] lemma inv_smul_smul' {c : α} (hc : c ≠ 0) (x : β) : c⁻¹ • c • x = x := inv_smul_smul (units.mk0 c hc) x @[simp] lemma smul_inv_smul' {c : α} (hc : c ≠ 0) (x : β) : c • c⁻¹ • x = x := smul_inv_smul (units.mk0 c hc) x lemma inv_smul_eq_iff' {a : α} (ha : a ≠ 0) {x y : β} : a⁻¹ • x = y ↔ x = a • y := (mul_action.to_perm (units.mk0 a ha)).symm_apply_eq lemma eq_inv_smul_iff' {a : α} (ha : a ≠ 0) {x y : β} : x = a⁻¹ • y ↔ a • x = y := (mul_action.to_perm (units.mk0 a ha)).eq_symm_apply end gwz end mul_action section distrib_mul_action section group variables [group α] [add_monoid β] [distrib_mul_action α β] theorem smul_eq_zero_iff_eq (a : α) {x : β} : a • x = 0 ↔ x = 0 := ⟨λ h, by rw [← inv_smul_smul a x, h, smul_zero], λ h, h.symm ▸ smul_zero _⟩ theorem smul_ne_zero_iff_ne (a : α) {x : β} : a • x ≠ 0 ↔ x ≠ 0 := not_congr $ smul_eq_zero_iff_eq a end group section gwz variables [group_with_zero α] [add_monoid β] [distrib_mul_action α β] theorem smul_eq_zero_iff_eq' {a : α} (ha : a ≠ 0) {x : β} : a • x = 0 ↔ x = 0 := smul_eq_zero_iff_eq (units.mk0 a ha) theorem smul_ne_zero_iff_ne' {a : α} (ha : a ≠ 0) {x : β} : a • x ≠ 0 ↔ x ≠ 0 := smul_ne_zero_iff_ne (units.mk0 a ha) end gwz end distrib_mul_action section arrow /-- If `G` acts on `A`, then it acts also on `A → B`, by `(g • F) a = F (g⁻¹ • a)`. -/ @[simps] def arrow_action {G A B : Type*} [group G] [mul_action G A] : mul_action G (A → B) := { smul := λ g F a, F (g⁻¹ • a), one_smul := by { intro, simp only [one_inv, one_smul] }, mul_smul := by { intros, simp only [mul_smul, mul_inv_rev] } } local attribute [instance] arrow_action /-- Given groups `G H` with `G` acting on `A`, `G` acts by multiplicative automorphisms on `A → H`. -/ @[simps] def mul_aut_arrow {G A H} [group G] [mul_action G A] [group H] : G →* mul_aut (A → H) := { to_fun := λ g, { to_fun := λ F, g • F, inv_fun := λ F, g⁻¹ • F, left_inv := λ F, inv_smul_smul g F, right_inv := λ F, smul_inv_smul g F, map_mul' := by { intros, ext, simp only [arrow_action_to_has_scalar_smul, pi.mul_apply] } }, map_one' := by { ext, simp only [mul_aut.one_apply, mul_equiv.coe_mk, one_smul] }, map_mul' := by { intros, ext, simp only [mul_smul, mul_equiv.coe_mk, mul_aut.mul_apply] } } end arrow namespace is_unit section mul_action variables [monoid α] [mul_action α β] @[to_additive] lemma smul_left_cancel {a : α} (ha : is_unit a) {x y : β} : a • x = a • y ↔ x = y := let ⟨u, hu⟩ := ha in hu ▸ smul_left_cancel_iff u end mul_action section distrib_mul_action variables [monoid α] [add_monoid β] [distrib_mul_action α β] @[simp] theorem smul_eq_zero {u : α} (hu : is_unit u) {x : β} : u • x = 0 ↔ x = 0 := exists.elim hu $ λ u hu, hu ▸ smul_eq_zero_iff_eq u end distrib_mul_action end is_unit
e0dc2b8703a59604140b295f59afd0f57c6bc555
38bf3fd2bb651ab70511408fcf70e2029e2ba310
/src/category_theory/category/default.lean
d9c2ee7f6e0e21005dec5e379a90252d614864f2
[ "Apache-2.0" ]
permissive
JaredCorduan/mathlib
130392594844f15dad65a9308c242551bae6cd2e
d5de80376088954d592a59326c14404f538050a1
refs/heads/master
1,595,862,206,333
1,570,816,457,000
1,570,816,457,000
209,134,499
0
0
Apache-2.0
1,568,746,811,000
1,568,746,811,000
null
UTF-8
Lean
false
false
5,587
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Stephen Morgan, Scott Morrison, Johannes Hölzl, Reid Barton Defines a category, as a typeclass parametrised by the type of objects. Introduces notations `X ⟶ Y` for the morphism spaces, `f ≫ g` for composition in the 'arrows' convention. Users may like to add `f ⊚ g` for composition in the standard convention, using ``` local notation f ` ⊚ `:80 g:80 := category.comp g f -- type as \oo ``` -/ import tactic.basic import tactic.tidy universes v u -- The order in this declaration matters: v often needs to be explicitly specified while u often can be omitted namespace category_theory /- The propositional fields of `category` are annotated with the auto_param `obviously`, which is defined here as a [`replacer` tactic](https://github.com/leanprover/mathlib/blob/master/docs/tactics.md#def_replacer). We then immediately set up `obviously` to call `tidy`. Later, this can be replaced with more powerful tactics. -/ def_replacer obviously @[obviously] meta def obviously' := tactic.tidy class has_hom (obj : Type u) : Type (max u (v+1)) := (hom : obj → obj → Type v) infixr ` ⟶ `:10 := has_hom.hom -- type as \h class category_struct (obj : Type u) extends has_hom.{v} obj : Type (max u (v+1)) := (id : Π X : obj, hom X X) (comp : Π {X Y Z : obj}, (X ⟶ Y) → (Y ⟶ Z) → (X ⟶ Z)) notation `𝟙` := category_struct.id -- type as \b1 infixr ` ≫ `:80 := category_struct.comp -- type as \gg /-- The typeclass `category C` describes morphisms associated to objects of type `C`. The universe levels of the objects and morphisms are unconstrained, and will often need to be specified explicitly, as `category.{v} C`. (See also `large_category` and `small_category`.) -/ class category (obj : Type u) extends category_struct.{v} obj : Type (max u (v+1)) := (id_comp' : ∀ {X Y : obj} (f : hom X Y), 𝟙 X ≫ f = f . obviously) (comp_id' : ∀ {X Y : obj} (f : hom X Y), f ≫ 𝟙 Y = f . obviously) (assoc' : ∀ {W X Y Z : obj} (f : hom W X) (g : hom X Y) (h : hom Y Z), (f ≫ g) ≫ h = f ≫ (g ≫ h) . obviously) -- `restate_axiom` is a command that creates a lemma from a structure field, -- discarding any auto_param wrappers from the type. -- (It removes a backtick from the name, if it finds one, and otherwise adds "_lemma".) restate_axiom category.id_comp' restate_axiom category.comp_id' restate_axiom category.assoc' attribute [simp] category.id_comp category.comp_id category.assoc attribute [trans] category_struct.comp lemma category.assoc_symm {C : Type u} [category.{v} C] {W X Y Z : C} (f : W ⟶ X) (g : X ⟶ Y) (h : Y ⟶ Z) : f ≫ (g ≫ h) = (f ≫ g) ≫ h := by rw ←category.assoc /-- A `large_category` has objects in one universe level higher than the universe level of the morphisms. It is useful for examples such as the category of types, or the category of groups, etc. -/ abbreviation large_category (C : Type (u+1)) : Type (u+1) := category.{u} C /-- A `small_category` has objects and morphisms in the same universe level. -/ abbreviation small_category (C : Type u) : Type (u+1) := category.{u} C section variables {C : Type u} [𝒞 : category.{v} C] {X Y Z : C} include 𝒞 lemma eq_of_comp_left_eq {f g : X ⟶ Y} (w : ∀ {Z : C} (h : Y ⟶ Z), f ≫ h = g ≫ h) : f = g := by { convert w (𝟙 Y), tidy } lemma eq_of_comp_right_eq {f g : Y ⟶ Z} (w : ∀ {X : C} (h : X ⟶ Y), h ≫ f = h ≫ g) : f = g := by { convert w (𝟙 Y), tidy } lemma eq_of_comp_left_eq' (f g : X ⟶ Y) (w : (λ {Z : C} (h : Y ⟶ Z), f ≫ h) = (λ {Z : C} (h : Y ⟶ Z), g ≫ h)) : f = g := eq_of_comp_left_eq (λ Z h, by convert congr_fun (congr_fun w Z) h) lemma eq_of_comp_right_eq' (f g : Y ⟶ Z) (w : (λ {X : C} (h : X ⟶ Y), h ≫ f) = (λ {X : C} (h : X ⟶ Y), h ≫ g)) : f = g := eq_of_comp_right_eq (λ X h, by convert congr_fun (congr_fun w X) h) lemma id_of_comp_left_id (f : X ⟶ X) (w : ∀ {Y : C} (g : X ⟶ Y), f ≫ g = g) : f = 𝟙 X := by { convert w (𝟙 X), tidy } lemma id_of_comp_right_id (f : X ⟶ X) (w : ∀ {Y : C} (g : Y ⟶ X), g ≫ f = g) : f = 𝟙 X := by { convert w (𝟙 X), tidy } class epi (f : X ⟶ Y) : Prop := (left_cancellation : Π {Z : C} (g h : Y ⟶ Z) (w : f ≫ g = f ≫ h), g = h) class mono (f : X ⟶ Y) : Prop := (right_cancellation : Π {Z : C} (g h : Z ⟶ X) (w : g ≫ f = h ≫ f), g = h) @[simp] lemma cancel_epi (f : X ⟶ Y) [epi f] {g h : Y ⟶ Z} : (f ≫ g = f ≫ h) ↔ g = h := ⟨ λ p, epi.left_cancellation g h p, begin intro a, subst a end ⟩ @[simp] lemma cancel_mono (f : X ⟶ Y) [mono f] {g h : Z ⟶ X} : (g ≫ f = h ≫ f) ↔ g = h := ⟨ λ p, mono.right_cancellation g h p, begin intro a, subst a end ⟩ end section variable (C : Type u) variable [category.{v} C] universe u' instance ulift_category : category.{v} (ulift.{u'} C) := { hom := λ X Y, (X.down ⟶ Y.down), id := λ X, 𝟙 X.down, comp := λ _ _ _ f g, f ≫ g } -- We verify that this previous instance can lift small categories to large categories. example (D : Type u) [small_category D] : large_category (ulift.{u+1} D) := by apply_instance end end category_theory open category_theory namespace preorder variables (α : Type u) instance small_category [preorder α] : small_category α := { hom := λ U V, ulift (plift (U ≤ V)), id := λ X, ⟨ ⟨ le_refl X ⟩ ⟩, comp := λ X Y Z f g, ⟨ ⟨ le_trans _ _ _ f.down.down g.down.down ⟩ ⟩ } end preorder
1e23b4b01f1806695b7e5e9fdd1c57dc9a73f3fc
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/library/theories/number_theory/prime_factorization.lean
87649d5925dd391e1c9448bfd2a6bcf428dd7c53
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
14,077
lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad Multiplicity and prime factors. We have: mult p n := the greatest power of p dividing n if p > 1 and n > 0, and 0 otherwise. prime_factors n := the finite set of prime factors of n, assuming n > 0 -/ import data.nat data.finset .primes algebra.group_bigops open eq.ops finset well_founded decidable namespace nat -- TODO: this should be proved more generally in ring_bigops theorem Prod_pos {A : Type} [deceqA : decidable_eq A] {s : finset A} {f : A → ℕ} (fpos : ∀ n, n ∈ s → f n > 0) : (∏ n ∈ s, f n) > 0 := begin induction s with a s anins ih, {rewrite Prod_empty; exact zero_lt_one}, rewrite [!Prod_insert_of_not_mem anins], exact (mul_pos (fpos a (mem_insert a _)) (ih (forall_of_forall_insert fpos))) end /- multiplicity -/ theorem mult_rec_decreasing {p n : ℕ} (Hp : p > 1) (Hn : n > 0) : n / p < n := have H' : n < n * p, by rewrite [-mul_one n at {1}]; apply mul_lt_mul_of_pos_left Hp Hn, nat.div_lt_of_lt_mul H' private definition mult.F (p : ℕ) (n : ℕ) (f: Π {m : ℕ}, m < n → ℕ) : ℕ := if H : (p > 1 ∧ n > 0) ∧ p ∣ n then succ (f (mult_rec_decreasing (and.left (and.left H)) (and.right (and.left H)))) else 0 definition mult (p n : ℕ) : ℕ := fix (mult.F p) n theorem mult_rec {p n : ℕ} (pgt1 : p > 1) (ngt0 : n > 0) (pdivn : p ∣ n) : mult p n = succ (mult p (n / p)) := have (p > 1 ∧ n > 0) ∧ p ∣ n, from and.intro (and.intro pgt1 ngt0) pdivn, eq.trans (well_founded.fix_eq (mult.F p) n) (dif_pos this) private theorem mult_base {p n : ℕ} (H : ¬ ((p > 1 ∧ n > 0) ∧ p ∣ n)) : mult p n = 0 := eq.trans (well_founded.fix_eq (mult.F p) n) (dif_neg H) theorem mult_zero_right (p : ℕ) : mult p 0 = 0 := mult_base (assume H, !lt.irrefl (and.right (and.left H))) theorem mult_eq_zero_of_not_dvd {p n : ℕ} (H : ¬ p ∣ n) : mult p n = 0 := mult_base (assume H', H (and.right H')) theorem mult_eq_zero_of_le_one {p : ℕ} (n : ℕ) (H : p ≤ 1) : mult p n = 0 := mult_base (assume H', not_lt_of_ge H (and.left (and.left H'))) theorem mult_zero_left (n : ℕ) : mult 0 n = 0 := mult_eq_zero_of_le_one n !dec_trivial theorem mult_one_left (n : ℕ) : mult 1 n = 0 := mult_eq_zero_of_le_one n !dec_trivial theorem mult_pos_of_dvd {p n : ℕ} (pgt1 : p > 1) (npos : n > 0) (pdvdn : p ∣ n) : mult p n > 0 := by rewrite (mult_rec pgt1 npos pdvdn); apply succ_pos theorem not_dvd_of_mult_eq_zero {p n : ℕ} (pgt1 : p > 1) (npos : n > 0) (H : mult p n = 0) : ¬ p ∣ n := suppose p ∣ n, ne_of_gt (mult_pos_of_dvd pgt1 npos this) H theorem dvd_of_mult_pos {p n : ℕ} (H : mult p n > 0) : p ∣ n := by_contradiction (suppose ¬ p ∣ n, ne_of_gt H (mult_eq_zero_of_not_dvd this)) /- properties of mult -/ theorem mult_eq_zero_of_prime_of_ne {p q : ℕ} (primep : prime p) (primeq : prime q) (pneq : p ≠ q) : mult p q = 0 := mult_eq_zero_of_not_dvd (not_dvd_of_prime_of_coprime primep (coprime_primes primep primeq pneq)) theorem pow_mult_dvd (p n : ℕ) : p^(mult p n) ∣ n := begin induction n using nat.strong_induction_on with [n, ih], cases eq_zero_or_pos n with [nz, npos], {rewrite nz, apply dvd_zero}, cases le_or_gt p 1 with [ple1, pgt1], {rewrite [!mult_eq_zero_of_le_one ple1, pow_zero], apply one_dvd}, cases (or.swap (em (p ∣ n))) with [pndvdn, pdvdn], {rewrite [mult_eq_zero_of_not_dvd pndvdn, pow_zero], apply one_dvd}, show p ^ (mult p n) ∣ n, from dvd.elim pdvdn (take n', suppose n = p * n', have p > 0, from lt.trans zero_lt_one pgt1, have n / p = n', from !nat.div_eq_of_eq_mul_right this `n = p * n'`, have n' < n, by rewrite -this; apply mult_rec_decreasing pgt1 npos, begin rewrite [mult_rec pgt1 npos pdvdn, `n / p = n'`, pow_succ], subst n, apply mul_dvd_mul !dvd.refl, apply ih _ this end) end theorem mult_one_right (p : ℕ) : mult p 1 = 0:= have H : p^(mult p 1) = 1, from eq_one_of_dvd_one !pow_mult_dvd, or.elim (le_or_gt p 1) (suppose p ≤ 1, by rewrite [!mult_eq_zero_of_le_one this]) (suppose p > 1, by_contradiction (suppose mult p 1 ≠ 0, have mult p 1 > 0, from pos_of_ne_zero this, have p^(mult p 1) > 1, from pow_gt_one `p > 1` this, show false, by rewrite H at this; apply !lt.irrefl this)) private theorem mult_pow_mul {p n : ℕ} (i : ℕ) (pgt1 : p > 1) (npos : n > 0) : mult p (p^i * n) = i + mult p n := begin induction i with [i, ih], {krewrite [pow_zero, one_mul, zero_add]}, have p > 0, from lt.trans zero_lt_one pgt1, have psin_pos : p^(succ i) * n > 0, from mul_pos (!pow_pos_of_pos this) npos, have p ∣ p^(succ i) * n, by rewrite [pow_succ, mul.assoc]; apply dvd_mul_right, rewrite [mult_rec pgt1 psin_pos this, pow_succ', mul.right_comm, !nat.mul_div_cancel `p > 0`, ih], rewrite [add.comm i, add.comm (succ i)] end theorem mult_pow_self {p : ℕ} (i : ℕ) (pgt1 : p > 1) : mult p (p^i) = i := by rewrite [-(mul_one (p^i)), mult_pow_mul i pgt1 zero_lt_one, mult_one_right] theorem mult_self {p : ℕ} (pgt1 : p > 1) : mult p p = 1 := by rewrite [-pow_one p at {2}]; apply mult_pow_self 1 pgt1 theorem le_mult {p i n : ℕ} (pgt1 : p > 1) (npos : n > 0) (pidvd : p^i ∣ n) : i ≤ mult p n := dvd.elim pidvd (take m, suppose n = p^i * m, have m > 0, from pos_of_mul_pos_left (this ▸ npos), by subst n; rewrite [mult_pow_mul i pgt1 this]; apply le_add_right) theorem not_dvd_div_pow_mult {p n : ℕ} (pgt1 : p > 1) (npos : n > 0) : ¬ p ∣ n / p^(mult p n) := assume pdvd : p ∣ n / p^(mult p n), obtain m (H : n / p^(mult p n) = p * m), from exists_eq_mul_right_of_dvd pdvd, have n = p^(succ (mult p n)) * m, from calc n = p^mult p n * (n / p^mult p n) : by rewrite (nat.mul_div_cancel' !pow_mult_dvd) ... = p^(succ (mult p n)) * m : by rewrite [H, pow_succ', mul.assoc], have p^(succ (mult p n)) ∣ n, by rewrite this at {2}; apply dvd_mul_right, have succ (mult p n) ≤ mult p n, from le_mult pgt1 npos this, show false, from !not_succ_le_self this theorem mult_mul {p m n : ℕ} (primep : prime p) (mpos : m > 0) (npos : n > 0) : mult p (m * n) = mult p m + mult p n := let m' := m / p^mult p m, n' := n / p^mult p n in have p > 1, from gt_one_of_prime primep, have meq : m = p^mult p m * m', by rewrite (nat.mul_div_cancel' !pow_mult_dvd), have neq : n = p^mult p n * n', by rewrite (nat.mul_div_cancel' !pow_mult_dvd), have m'pos : m' > 0, from pos_of_mul_pos_left (meq ▸ mpos), have n'pos : n' > 0, from pos_of_mul_pos_left (neq ▸ npos), have npdvdm' : ¬ p ∣ m', from !not_dvd_div_pow_mult `p > 1` mpos, have npdvdn' : ¬ p ∣ n', from !not_dvd_div_pow_mult `p > 1` npos, have npdvdm'n' : ¬ p ∣ m' * n', from not_dvd_mul_of_prime primep npdvdm' npdvdn', have m'n'pos : m' * n' > 0, from mul_pos m'pos n'pos, have multm'n' : mult p (m' * n') = 0, from mult_eq_zero_of_not_dvd npdvdm'n', calc mult p (m * n) = mult p (p^(mult p m + mult p n) * (m' * n')) : by rewrite [pow_add, mul.right_comm, -mul.assoc, -meq, mul.assoc, mul.comm (n / _), -neq] ... = mult p m + mult p n : by rewrite [!mult_pow_mul `p > 1` m'n'pos, multm'n'] theorem mult_pow {p m : ℕ} (n : ℕ) (mpos : m > 0) (primep : prime p) : mult p (m^n) = n * mult p m := begin induction n with n ih, krewrite [pow_zero, mult_one_right, zero_mul], rewrite [pow_succ, mult_mul primep mpos (!pow_pos_of_pos mpos), ih, succ_mul, add.comm] end theorem dvd_of_forall_prime_mult_le {m n : ℕ} (mpos : m > 0) (H : ∀ {p}, prime p → mult p m ≤ mult p n) : m ∣ n := begin revert H, revert n, induction m using nat.strong_induction_on with [m, ih], cases (decidable.em (m = 1)) with [meq, mneq], {intros, rewrite meq, apply one_dvd}, have mgt1 : m > 1, from lt_of_le_of_ne (succ_le_of_lt mpos) (ne.symm mneq), have mge2 : m ≥ 2, from succ_le_of_lt mgt1, have hpd : ∃ p, prime p ∧ p ∣ m, from exists_prime_and_dvd mge2, cases hpd with [p, H1], cases H1 with [primep, pdvdm], intro n, cases (eq_zero_or_pos n) with [nz, npos], {intros; rewrite nz; apply dvd_zero}, assume H : ∀ {p : ℕ}, prime p → mult p m ≤ mult p n, obtain m' (meq : m = p * m'), from exists_eq_mul_right_of_dvd pdvdm, have pgt1 : p > 1, from gt_one_of_prime primep, have m'pos : m' > 0, from pos_of_ne_zero (assume m'z, by revert mpos; rewrite [meq, m'z, mul_zero]; apply not_lt_zero), have m'ltm : m' < m, by rewrite [meq, -one_mul m' at {1}]; apply mul_lt_mul_of_lt_of_le m'pos pgt1 !le.refl, have multpm : mult p m ≥ 1, from le_mult pgt1 mpos (by rewrite pow_one; apply pdvdm), have multpn : mult p n ≥ 1, from le.trans multpm (H primep), obtain n' (neq : n = p * n'), from exists_eq_mul_right_of_dvd (dvd_of_mult_pos (lt_of_succ_le multpn)), have n'pos : n' > 0, from pos_of_ne_zero (assume n'z, by revert npos; rewrite [neq, n'z, mul_zero]; apply not_lt_zero), have ∀q, prime q → mult q m' ≤ mult q n', from (take q, assume primeq : prime q, have multqm : mult q m = mult q p + mult q m', by rewrite [meq, mult_mul primeq (pos_of_prime primep) m'pos], have multqn : mult q n = mult q p + mult q n', by rewrite [neq, mult_mul primeq (pos_of_prime primep) n'pos], show mult q m' ≤ mult q n', from le_of_add_le_add_left (multqm ▸ multqn ▸ H primeq)), have m'dvdn' : m' ∣ n', from ih m' m'ltm m'pos n' this, show m ∣ n, by rewrite [meq, neq]; apply mul_dvd_mul !dvd.refl m'dvdn' end theorem eq_of_forall_prime_mult_eq {m n : ℕ} (mpos : m > 0) (npos : n > 0) (H : ∀ p, prime p → mult p m = mult p n) : m = n := dvd.antisymm (dvd_of_forall_prime_mult_le mpos (take p, assume primep, H _ primep ▸ !le.refl)) (dvd_of_forall_prime_mult_le npos (take p, assume primep, H _ primep ▸ !le.refl)) /- prime factors -/ definition prime_factors (n : ℕ) : finset ℕ := { p ∈ upto (succ n) | prime p ∧ p ∣ n } theorem prime_of_mem_prime_factors {p n : ℕ} (H : p ∈ prime_factors n) : prime p := and.left (of_mem_sep H) theorem dvd_of_mem_prime_factors {p n : ℕ} (H : p ∈ prime_factors n) : p ∣ n := and.right (of_mem_sep H) theorem mem_prime_factors {p n : ℕ} (npos : n > 0) (primep : prime p) (pdvdn : p ∣ n) : p ∈ prime_factors n := have plen : p ≤ n, from le_of_dvd npos pdvdn, mem_sep_of_mem (mem_upto_of_lt (lt_succ_of_le plen)) (and.intro primep pdvdn) /- prime factorization -/ theorem mult_pow_eq_zero_of_prime_of_ne {p q : ℕ} (primep : prime p) (primeq : prime q) (pneq : p ≠ q) (i : ℕ) : mult p (q^i) = 0 := begin induction i with i ih, {krewrite [pow_zero, mult_one_right]}, have qpos : q > 0, from pos_of_prime primeq, have qipos : q^i > 0, from !pow_pos_of_pos qpos, rewrite [pow_succ', mult_mul primep qipos qpos, ih, mult_eq_zero_of_prime_of_ne primep primeq pneq] end theorem mult_prod_pow_of_not_mem {p : ℕ} (primep : prime p) {s : finset ℕ} (sprimes : ∀ p, p ∈ s → prime p) (f : ℕ → ℕ) (pns : p ∉ s) : mult p (∏ q ∈ s, q^(f q)) = 0 := begin induction s with a s anins ih, {rewrite [Prod_empty, mult_one_right]}, have pnea : p ≠ a, from assume peqa, by rewrite peqa at pns; exact pns !mem_insert, have primea : prime a, from sprimes a !mem_insert, have afapos : a ^ f a > 0, from !pow_pos_of_pos (pos_of_prime primea), have prodpos : (∏ q ∈ s, q ^ f q) > 0, from Prod_pos (take q, assume qs, !pow_pos_of_pos (pos_of_prime (forall_of_forall_insert sprimes q qs))), rewrite [!Prod_insert_of_not_mem anins, mult_mul primep afapos prodpos], rewrite (mult_pow_eq_zero_of_prime_of_ne primep primea pnea), rewrite (ih (forall_of_forall_insert sprimes) (λ H, pns (!mem_insert_of_mem H))) end theorem mult_prod_pow_of_mem {p : ℕ} (primep : prime p) {s : finset ℕ} (sprimes : ∀ p, p ∈ s → prime p) (f : ℕ → ℕ) (ps : p ∈ s) : mult p (∏ q ∈ s, q^(f q)) = f p := begin induction s with a s anins ih, {exact absurd ps !not_mem_empty}, have primea : prime a, from sprimes a !mem_insert, have afapos : a ^ f a > 0, from !pow_pos_of_pos (pos_of_prime primea), have prodpos : (∏ q ∈ s, q ^ f q) > 0, from Prod_pos (take q, assume qs, !pow_pos_of_pos (pos_of_prime (forall_of_forall_insert sprimes q qs))), rewrite [!Prod_insert_of_not_mem anins, mult_mul primep afapos prodpos], cases eq_or_mem_of_mem_insert ps with peqa pins, {rewrite [peqa, !mult_pow_self (gt_one_of_prime primea)], rewrite [mult_prod_pow_of_not_mem primea (forall_of_forall_insert sprimes) _ anins]}, have pnea : p ≠ a, from by intro peqa; rewrite peqa at pins; exact anins pins, rewrite [mult_pow_eq_zero_of_prime_of_ne primep primea pnea, zero_add], exact (ih (forall_of_forall_insert sprimes) pins) end theorem eq_prime_factorization {n : ℕ} (npos : n > 0) : n = (∏ p ∈ prime_factors n, p^(mult p n)) := let nprod := ∏ p ∈ prime_factors n, p^(mult p n) in have primefactors : ∀ p, p ∈ prime_factors n → prime p, from take p, @prime_of_mem_prime_factors p n, have prodpos : (∏ q ∈ prime_factors n, q^(mult q n)) > 0, from Prod_pos (take q, assume qpf, !pow_pos_of_pos (pos_of_prime (prime_of_mem_prime_factors qpf))), eq_of_forall_prime_mult_eq npos prodpos (take p, assume primep, decidable.by_cases (assume pprimefactors : p ∈ prime_factors n, eq.symm (mult_prod_pow_of_mem primep primefactors (λ p, mult p n) pprimefactors)) (assume pnprimefactors : p ∉ prime_factors n, have ¬ p ∣ n, from assume H, pnprimefactors (mem_prime_factors npos primep H), have mult p n = 0, from mult_eq_zero_of_not_dvd this, by rewrite [this, mult_prod_pow_of_not_mem primep primefactors _ pnprimefactors])) end nat
e75b0ceefbfc4effc8a0d17654e779923961aede
bde6690019e9da475b0c91d5a066e0f6681a1179
/library/standard/nat.lean
2b192310866de15e05f29745648ba6251dd6c82d
[ "Apache-2.0" ]
permissive
leodemoura/libraries
ae67d491abc580407aa837d65736d515bec39263
14afd47544daa9520ea382d33ba7f6f05c949063
refs/heads/master
1,473,601,302,073
1,403,713,370,000
1,403,713,370,000
19,831,525
1
0
null
null
null
null
UTF-8
Lean
false
false
52,912
lean
---------------------------------------------------------------------------------------------------- --- Copyright (c) 2014 Floris van Doorn. All rights reserved. --- Released under Apache 2.0 license as described in the file LICENSE. --- Author: Floris van Doorn ---------------------------------------------------------------------------------------------------- -- Theory nat -- ========== -- -- The natural numbers, with addition, multiplication, ordering, and subtraction. -- Axioms -- ------ import kernel macros tactic variable nat : Type alias ℕ : nat unary_nat -- enables numerals namespace nat variable zero : nat variable succ : nat -> nat axiom rec {P : nat → Type} (x : P 0) (f : ∀m : nat, P m → P (succ m)) (m : ℕ) : P m axiom rec_zero {P : nat → Type} (x : P 0) (f : ∀m : nat, P m → P (succ m)) : rec x f 0 = x axiom rec_succ {P : nat → Type} (x : P 0) (f : ∀m : nat, P m → P (succ m)) (n : ℕ) : rec x f (succ n) = f n (rec x f n) theorem induction_on {P : nat → Bool} (a : ℕ) (H1 : P 0) (H2 : ∀(n : ℕ) (IH : P n), P (succ n)) : P a := rec H1 H2 a -- Successor and predecessor -- ------------------------- theorem succ_ne_zero (n : ℕ) : succ n ≠ 0 := not_intro (take H : succ n = 0, have H2 : true = false, from (let f : nat -> Bool := (rec false (fun a b,true)) in calc true = f (succ n) : symm (rec_succ _ _ _) ... = f 0 : {H} ... = false : rec_zero _ _), absurd H2 true_ne_false) definition pred (n : ℕ) := rec 0 (fun m x, m) n theorem pred_zero : pred 0 = 0 := rec_zero _ _ theorem pred_succ (n : ℕ) : pred (succ n) = n := rec_succ _ _ _ set_opaque pred true theorem zero_or_succ_pred (n : ℕ) : n = 0 ∨ n = succ (pred n) := induction_on n (or_intro_left _ (refl 0)) (take m IH, or_intro_right _ (show succ m = succ (pred (succ m)), from congr2 succ (symm (pred_succ m)))) theorem zero_or_exists_succ (n : ℕ) : n = 0 ∨ ∃k, n = succ k := or_imp_or_right (zero_or_succ_pred n) (assume H : n = succ (pred n), exists_intro (pred n) H) -- name clash with kernel.case theorem case {P : nat → Bool} (n : ℕ) (H1: P 0) (H2 : ∀m, P (succ m)) : P n := induction_on n H1 (take m IH, H2 m) theorem discriminate {B : Bool} {n : ℕ} (H1: n = 0 → B) (H2 : ∀m, n = succ m → B) : B := or_elim (zero_or_succ_pred n) (take H3 : n = 0, H1 H3) (take H3 : n = succ (pred n), H2 (pred n) H3) theorem succ_inj {n m : ℕ} (H : succ n = succ m) : n = m := calc n = pred (succ n) : symm (pred_succ n) ... = pred (succ m) : {H} ... = m : pred_succ m theorem succ_ne_self (n : ℕ) : succ n ≠ n := not_intro (induction_on n (take H : 1 = 0, have ne : 1 ≠ 0, from succ_ne_zero 0, absurd H ne) (take k IH H, IH (succ_inj H))) theorem decidable_equality (n m : ℕ) : n = m ∨ n ≠ m := have general : ∀n, n = m ∨ n ≠ m, from induction_on m (take n : nat, discriminate (assume H : n = 0, or_intro_left _ H) (take l : nat, assume H : n = succ l, have H2 : n ≠ 0, from subst (succ_ne_zero l) (symm H), or_intro_right _ H2)) (take k : nat, assume IH : ∀n, n = k ∨ n ≠ k, take n : nat, discriminate (assume H : n = 0, have H2 : n ≠ succ k, from subst (ne_symm (succ_ne_zero k)) (symm H), or_intro_right _ H2) (take l : nat, assume H : n = succ l, or_imp_or (IH l) (take H2 : l = k, show n = succ k, from trans H (congr2 succ H2)) (take H2 : l ≠ k, show n ≠ succ k, from not_intro (take H4 : n = succ k, have H5 : succ l = succ k, from trans (symm H) H4, have H6 : l = k, from succ_inj H5, absurd H6 H2)))), general n theorem two_step_induction_on {P : nat → Bool} (a : ℕ) (H1 : P 0) (H2 : P 1) (H3 : ∀ (n : ℕ) (IH1 : P n) (IH2 : P (succ n)), P (succ (succ n))) : P a := have stronger : P a ∧ P (succ a), from induction_on a (and_intro H1 H2) (take k IH, have IH1 : P k, from and_elim_left IH, have IH2 : P (succ k), from and_elim_right IH, and_intro IH2 (H3 k IH1 IH2)), and_elim_left stronger theorem sub_induction {P : nat → nat → Bool} (n m : ℕ) (H1 : ∀m, P 0 m) (H2 : ∀n, P (succ n) 0) (H3 : ∀n m, P n m → P (succ n) (succ m)) : P n m := have general : ∀m, P n m, from induction_on n (take m : nat, H1 m) (take k : nat, assume IH : ∀m, P k m, take m : nat, discriminate (assume Hm : m = 0, subst (H2 k) (symm Hm)) (take l : nat, assume Hm : m = succ l, subst (H3 k l (IH l)) (symm Hm))), general m add_rewrite succ_ne_zero -- Addition -- -------- definition add (n m : ℕ) := rec n (fun k x, succ x) m infixl 65 + : add theorem add_zero_right (n : ℕ) : n + 0 = n := rec_zero _ _ theorem add_succ_right (n m : ℕ) : n + succ m = succ (n + m) := rec_succ _ _ _ set_opaque add true -- ### commutativity and associativity theorem add_zero_left (n : ℕ) : 0 + n = n := induction_on n (add_zero_right 0) (take m IH, show 0 + succ m = succ m, from calc 0 + succ m = succ (0 + m) : add_succ_right _ _ ... = succ m : {IH}) theorem add_succ_left (n m : ℕ) : (succ n) + m = succ (n + m) := induction_on m (calc succ n + 0 = succ n : add_zero_right (succ n) ... = succ (n + 0) : {symm (add_zero_right n)}) (take k IH, calc succ n + succ k = succ (succ n + k) : add_succ_right _ _ ... = succ (succ (n + k)) : {IH} ... = succ (n + succ k) : {symm (add_succ_right _ _)}) theorem add_comm (n m : ℕ) : n + m = m + n := induction_on m (trans (add_zero_right _) (symm (add_zero_left _))) (take k IH, calc n + succ k = succ (n+k) : add_succ_right _ _ ... = succ (k + n) : {IH} ... = succ k + n : symm (add_succ_left _ _)) theorem add_move_succ (n m : ℕ) : succ n + m = n + succ m := calc succ n + m = succ (n + m) : add_succ_left n m ... = n +succ m : symm (add_succ_right n m) theorem add_comm_succ (n m : ℕ) : n + succ m = m + succ n := calc n + succ m = succ n + m : symm (add_move_succ n m) ... = m + succ n : add_comm (succ n) m theorem add_assoc (n m k : ℕ) : (n + m) + k = n + (m + k) := induction_on k (calc (n + m) + 0 = n + m : add_zero_right _ ... = n + (m + 0) : {symm (add_zero_right m)}) (take l IH, calc (n + m) + succ l = succ ((n + m) + l) : add_succ_right _ _ ... = succ (n + (m + l)) : {IH} ... = n + succ (m + l) : symm (add_succ_right _ _) ... = n + (m + succ l) : {symm (add_succ_right _ _)}) theorem add_left_comm (n m k : ℕ) : n + (m + k) = m + (n + k) := left_comm add_comm add_assoc n m k theorem add_right_comm (n m k : ℕ) : n + m + k = n + k + m := right_comm add_comm add_assoc n m k add_rewrite add_zero_left add_zero_right add_rewrite add_succ_left add_succ_right add_rewrite add_comm add_assoc add_left_comm --- the following was used a couple of times in int.lean -- theorem add_switch (n m k l : ℕ) : n + m + (k + l) = n + k + (m + l) -- := by simp -- ### cancelation theorem add_cancel_left {m k n : ℕ} : n + m = n + k → m = k := induction_on n (take H : 0 + m = 0 + k, calc m = 0 + m : symm (add_zero_left m) ... = 0 + k : H ... = k : add_zero_left k) (take (n : ℕ) (IH : n + m = n + k → m = k) (H : succ n + m = succ n + k), have H2 : succ (n + m) = succ (n + k), from calc succ (n + m) = succ n + m : symm (add_succ_left n m) ... = succ n + k : H ... = succ (n + k) : add_succ_left n k, have H3 : n + m = n + k, from succ_inj H2, IH H3) theorem add_cancel_right {n m k : ℕ} (H : n + k = m + k) : n = m := have H2 : k + n = k + m, from calc k + n = n + k : add_comm k n ... = m + k : H ... = k + m : add_comm m k, add_cancel_left H2 theorem add_eq_zero_left {n m : ℕ} : n + m = 0 → n = 0 := induction_on n (take (H : 0 + m = 0), refl 0) (take k IH, assume (H : succ k + m = 0), absurd_elim (succ k = 0) (show succ (k + m) = 0, from calc succ (k + m) = succ k + m : symm (add_succ_left k m) ... = 0 : H) (succ_ne_zero (k + m))) theorem add_eq_zero_right {n m : ℕ} (H : n + m = 0) : m = 0 := add_eq_zero_left (trans (add_comm m n) H) theorem add_eq_zero {n m : ℕ} (H : n + m = 0) : n = 0 ∧ m = 0 := and_intro (add_eq_zero_left H) (add_eq_zero_right H) theorem add_eq_self {n m : ℕ} (H : n + m = n) : m = 0 := have H2 : n + m = n + 0, from subst H (symm (add_zero_right n)), add_cancel_left H2 -- See also add_eq_self below. -- ### misc theorem add_one (n : ℕ) : n + 1 = succ n := calc n + 1 = succ (n + 0) : add_succ_right _ _ ... = succ n : {add_zero_right _} theorem add_one_left (n : ℕ) : 1 + n = succ n := calc 1 + n = succ (0 + n) : add_succ_left _ _ ... = succ n : {add_zero_left _} --- rename? remove? theorem induction_plus_one {P : nat → Bool} (a : ℕ) (H1 : P 0) (H2 : ∀ (n : ℕ) (IH : P n), P (n + 1)) : P a := rec H1 (take n IH, subst (H2 n IH) (add_one n)) a -- Multiplication -- -------------- definition mul (n m : ℕ) := rec 0 (fun m x, x + n) m infixl 70 * : mul theorem mul_zero_right (n : ℕ) : n * 0 = 0 := rec_zero _ _ theorem mul_succ_right (n m : ℕ) : n * succ m = n * m + n := rec_succ _ _ _ set_opaque mul true -- ### commutativity, distributivity, associativity, identity theorem mul_zero_left (n : ℕ) : 0 * n = 0 := induction_on n (mul_zero_right 0) (take m IH, calc 0 * succ m = 0 * m + 0 : mul_succ_right _ _ ... = 0 * m : add_zero_right _ ... = 0 : IH) theorem mul_succ_left (n m : ℕ) : (succ n) * m = (n * m) + m := induction_on m (calc succ n * 0 = 0 : mul_zero_right _ ... = n * 0 : symm (mul_zero_right _) ... = n * 0 + 0 : symm (add_zero_right _)) (take k IH, calc succ n * succ k = (succ n * k) + succ n : mul_succ_right _ _ ... = (n * k) + k + succ n : {IH} ... = (n * k) + (k + succ n) : add_assoc _ _ _ ... = (n * k) + (n + succ k) : {add_comm_succ _ _} ... = (n * k) + n + succ k : symm (add_assoc _ _ _) ... = (n * succ k) + succ k : {symm (mul_succ_right n k)}) theorem mul_comm (n m : ℕ) : n * m = m * n := induction_on m (trans (mul_zero_right _) (symm (mul_zero_left _))) (take k IH, calc n * succ k = n * k + n : mul_succ_right _ _ ... = k * n + n : {IH} ... = (succ k) * n : symm (mul_succ_left _ _)) theorem mul_distr_right (n m k : ℕ) : (n + m) * k = n * k + m * k := induction_on k (calc (n + m) * 0 = 0 : mul_zero_right _ ... = 0 + 0 : symm (add_zero_right _) ... = n * 0 + 0 : {symm (mul_zero_right _)} ... = n * 0 + m * 0 : {symm (mul_zero_right _)}) (take l IH, calc (n + m) * succ l = (n + m) * l + (n + m) : mul_succ_right _ _ ... = n * l + m * l + (n + m) : {IH} ... = n * l + m * l + n + m : symm (add_assoc _ _ _) ... = n * l + n + m * l + m : {add_right_comm _ _ _} ... = n * l + n + (m * l + m) : add_assoc _ _ _ ... = n * succ l + (m * l + m) : {symm (mul_succ_right _ _)} ... = n * succ l + m * succ l : {symm (mul_succ_right _ _)}) theorem mul_distr_left (n m k : ℕ) : n * (m + k) = n * m + n * k := calc n * (m + k) = (m + k) * n : mul_comm _ _ ... = m * n + k * n : mul_distr_right _ _ _ ... = n * m + k * n : {mul_comm _ _} ... = n * m + n * k : {mul_comm _ _} theorem mul_assoc (n m k : ℕ) : (n * m) * k = n * (m * k) := induction_on k (calc (n * m) * 0 = 0 : mul_zero_right _ ... = n * 0 : symm (mul_zero_right _) ... = n * (m * 0) : {symm (mul_zero_right _)}) (take l IH, calc (n * m) * succ l = (n * m) * l + n * m : mul_succ_right _ _ ... = n * (m * l) + n * m : {IH} ... = n * (m * l + m) : symm (mul_distr_left _ _ _) ... = n * (m * succ l) : {symm (mul_succ_right _ _)}) theorem mul_left_comm (n m k : ℕ) : n * (m * k) = m * (n * k) := left_comm mul_comm mul_assoc n m k theorem mul_right_comm (n m k : ℕ) : n * m * k = n * k * m := right_comm mul_comm mul_assoc n m k theorem mul_one_right (n : ℕ) : n * 1 = n := calc n * 1 = n * 0 + n : mul_succ_right n 0 ... = 0 + n : {mul_zero_right n} ... = n : add_zero_left n theorem mul_one_left (n : ℕ) : 1 * n = n := calc 1 * n = n * 1 : mul_comm _ _ ... = n : mul_one_right n theorem mul_eq_zero {n m : ℕ} (H : n * m = 0) : n = 0 ∨ m = 0 := discriminate (take Hn : n = 0, or_intro_left _ Hn) (take (k : ℕ), assume (Hk : n = succ k), discriminate (take (Hm : m = 0), or_intro_right _ Hm) (take (l : ℕ), assume (Hl : m = succ l), have Heq : succ (k * succ l + l) = n * m, from symm (calc n * m = n * succ l : {Hl} ... = succ k * succ l : {Hk} ... = k * succ l + succ l : mul_succ_left _ _ ... = succ (k * succ l + l) : add_succ_right _ _), absurd_elim _ (trans Heq H) (succ_ne_zero _))) ---other inversion theorems appear below add_rewrite mul_zero_left mul_zero_right mul_one_right mul_one_left add_rewrite mul_succ_left mul_succ_right add_rewrite mul_comm mul_assoc mul_left_comm add_rewrite mul_distr_right mul_distr_left -- Less than or equal -- ------------------ definition le (n m : ℕ) : Bool := exists k : nat, n + k = m infix 50 <= : le infix 50 ≤ : le theorem le_intro {n m k : ℕ} (H : n + k = m) : n ≤ m := exists_intro k H theorem le_elim {n m : ℕ} (H : n ≤ m) : ∃ k, n + k = m := H set_opaque le true -- ### partial order (totality is part of less than) theorem le_refl (n : ℕ) : n ≤ n := le_intro (add_zero_right n) theorem zero_le (n : ℕ) : 0 ≤ n := le_intro (add_zero_left n) theorem le_zero {n : ℕ} (H : n ≤ 0) : n = 0 := obtain (k : ℕ) (Hk : n + k = 0), from le_elim H, add_eq_zero_left Hk theorem not_succ_zero_le (n : ℕ) : ¬ succ n ≤ 0 := not_intro (assume H : succ n ≤ 0, have H2 : succ n = 0, from le_zero H, absurd H2 (succ_ne_zero n)) theorem le_trans {n m k : ℕ} (H1 : n ≤ m) (H2 : m ≤ k) : n ≤ k := obtain (l1 : ℕ) (Hl1 : n + l1 = m), from le_elim H1, obtain (l2 : ℕ) (Hl2 : m + l2 = k), from le_elim H2, le_intro (calc n + (l1 + l2) = n + l1 + l2 : symm (add_assoc n l1 l2) ... = m + l2 : {Hl1} ... = k : Hl2) theorem le_antisym {n m : ℕ} (H1 : n ≤ m) (H2 : m ≤ n) : n = m := obtain (k : ℕ) (Hk : n + k = m), from (le_elim H1), obtain (l : ℕ) (Hl : m + l = n), from (le_elim H2), have L1 : k + l = 0, from add_cancel_left (calc n + (k + l) = n + k + l : symm (add_assoc n k l) ... = m + l : {Hk} ... = n : Hl ... = n + 0 : symm (add_zero_right n)), have L2 : k = 0, from add_eq_zero_left L1, calc n = n + 0 : symm (add_zero_right n) ... = n + k : {symm L2} ... = m : Hk -- ### interaction with addition theorem le_add_right (n m : ℕ) : n ≤ n + m := le_intro (refl (n + m)) theorem le_add_left (n m : ℕ) : n ≤ m + n := le_intro (add_comm n m) theorem add_le_left {n m : ℕ} (H : n ≤ m) (k : ℕ) : k + n ≤ k + m := obtain (l : ℕ) (Hl : n + l = m), from (le_elim H), le_intro (calc k + n + l = k + (n + l) : add_assoc k n l ... = k + m : {Hl}) theorem add_le_right {n m : ℕ} (H : n ≤ m) (k : ℕ) : n + k ≤ m + k := subst (subst (add_le_left H k) (add_comm k n)) (add_comm k m) theorem add_le {n m k l : ℕ} (H1 : n ≤ k) (H2 : m ≤ l) : n + m ≤ k + l := le_trans (add_le_right H1 m) (add_le_left H2 k) theorem add_le_cancel_left {n m k : ℕ} (H : k + n ≤ k + m) : n ≤ m := obtain (l : ℕ) (Hl : k + n + l = k + m), from (le_elim H), le_intro (add_cancel_left calc k + (n + l) = k + n + l : symm (add_assoc k n l) ... = k + m : Hl ) theorem add_le_cancel_right {n m k : ℕ} (H : n + k ≤ m + k) : n ≤ m := add_le_cancel_left (subst (subst H (add_comm n k)) (add_comm m k)) theorem add_le_inv {n m k l : ℕ} (H1 : n + m ≤ k + l) (H2 : k ≤ n) : m ≤ l := obtain (a : ℕ) (Ha : k + a = n), from le_elim H2, have H3 : k + (a + m) ≤ k + l, from subst (subst H1 (symm Ha)) (add_assoc k a m), have H4 : a + m ≤ l, from add_le_cancel_left H3, show m ≤ l, from le_trans (le_add_left m a) H4 add_rewrite le_add_right le_add_left -- ### interaction with successor and predecessor theorem succ_le {n m : ℕ} (H : n ≤ m) : succ n ≤ succ m := subst (subst (add_le_right H 1) (add_one n)) (add_one m) theorem succ_le_cancel {n m : ℕ} (H : succ n ≤ succ m) : n ≤ m := add_le_cancel_right (subst (subst H (symm (add_one n))) (symm (add_one m))) theorem self_le_succ (n : ℕ) : n ≤ succ n := le_intro (add_one n) theorem le_imp_le_succ {n m : ℕ} (H : n ≤ m) : n ≤ succ m := le_trans H (self_le_succ m) theorem le_imp_succ_le_or_eq {n m : ℕ} (H : n ≤ m) : succ n ≤ m ∨ n = m := obtain (k : ℕ) (Hk : n + k = m), from (le_elim H), discriminate (assume H3 : k = 0, have Heq : n = m, from calc n = n + 0 : symm (add_zero_right n) ... = n + k : {symm H3} ... = m : Hk, or_intro_right _ Heq) (take l : nat, assume H3 : k = succ l, have Hlt : succ n ≤ m, from (le_intro (calc succ n + l = n + succ l : add_move_succ n l ... = n + k : {symm H3} ... = m : Hk)), or_intro_left _ Hlt) theorem le_ne_imp_succ_le {n m : ℕ} (H1 : n ≤ m) (H2 : n ≠ m) : succ n ≤ m := resolve_left (le_imp_succ_le_or_eq H1) H2 theorem le_succ_imp_le_or_eq {n m : ℕ} (H : n ≤ succ m) : n ≤ m ∨ n = succ m := or_imp_or_left (le_imp_succ_le_or_eq H) (take H2 : succ n ≤ succ m, show n ≤ m, from succ_le_cancel H2) theorem succ_le_imp_le_and_ne {n m : ℕ} (H : succ n ≤ m) : n ≤ m ∧ n ≠ m := and_intro (le_trans (self_le_succ n) H) (not_intro (assume H2 : n = m, have H3 : succ n ≤ n, from subst H (symm H2), have H4 : succ n = n, from le_antisym H3 (self_le_succ n), show false, from absurd H4 (succ_ne_self n))) theorem pred_le_self (n : ℕ) : pred n ≤ n := case n (subst (le_refl 0) (symm pred_zero)) (take k : nat, subst (self_le_succ k) (symm (pred_succ k))) theorem pred_le {n m : ℕ} (H : n ≤ m) : pred n ≤ pred m := discriminate (take Hn : n = 0, have H2 : pred n = 0, from calc pred n = pred 0 : {Hn} ... = 0 : pred_zero, subst (zero_le (pred m)) (symm H2)) (take k : nat, assume Hn : n = succ k, obtain (l : ℕ) (Hl : n + l = m), from le_elim H, have H2 : pred n + l = pred m, from calc pred n + l = pred (succ k) + l : {Hn} ... = k + l : {pred_succ k} ... = pred (succ (k + l)) : symm (pred_succ (k + l)) ... = pred (succ k + l) : {symm (add_succ_left k l)} ... = pred (n + l) : {symm Hn} ... = pred m : {Hl}, le_intro H2) theorem pred_le_imp_le_or_eq {n m : ℕ} (H : pred n ≤ m) : n ≤ m ∨ n = succ m := discriminate (take Hn : n = 0, or_intro_left _ (subst (zero_le m) (symm Hn))) (take k : nat, assume Hn : n = succ k, have H2 : pred n = k, from calc pred n = pred (succ k) : {Hn} ... = k : pred_succ k, have H3 : k ≤ m, from subst H H2, have H4 : succ k ≤ m ∨ k = m, from le_imp_succ_le_or_eq H3, show n ≤ m ∨ n = succ m, from or_imp_or H4 (take H5 : succ k ≤ m, show n ≤ m, from subst H5 (symm Hn)) (take H5 : k = m, show n = succ m, from subst Hn H5)) -- ### interaction with multiplication theorem mul_le_left {n m : ℕ} (H : n ≤ m) (k : ℕ) : k * n ≤ k * m := obtain (l : ℕ) (Hl : n + l = m), from (le_elim H), have H2 : k * n + k * l = k * m, from calc k * n + k * l = k * (n + l) : by simp_no_assump ... = k * m : {Hl}, le_intro H2 theorem mul_le_right {n m : ℕ} (H : n ≤ m) (k : ℕ) : n * k ≤ m * k := subst (subst (mul_le_left H k) (mul_comm k n)) (mul_comm k m) theorem mul_le {n m k l : ℕ} (H1 : n ≤ k) (H2 : m ≤ l) : n * m ≤ k * l := le_trans (mul_le_right H1 m) (mul_le_left H2 k) --- mul_le_[left|right]_inv below -- Less than, Greater than, Greater than or equal -- ---------------------------------------------- -- ge and gt will be transparent, so we don't need to reprove theorems for le and lt for them definition lt (n m : ℕ) := succ n ≤ m infix 50 < : lt definition ge (n m : ℕ) := m ≤ n infix 50 >= : ge infix 50 ≥ : ge definition gt (n m : ℕ) := m < n infix 50 > : gt theorem lt_def (n m : ℕ) : n < m ↔ succ n ≤ m := refl (n < m) theorem gt_def (n m : ℕ) : n > m ↔ m < n := refl (n > m) theorem ge_def (n m : ℕ) : n ≥ m ↔ m ≤ n := refl (n ≥ m) add_rewrite gt_def ge_def --it might be possible to remove this in Lean 0.2 theorem lt_intro {n m k : ℕ} (H : succ n + k = m) : n < m := le_intro H theorem lt_elim {n m : ℕ} (H : n < m) : ∃ k, succ n + k = m := le_elim H theorem lt_add_succ (n m : ℕ) : n < n + succ m := lt_intro (add_move_succ n m) -- ### basic facts theorem lt_imp_ne {n m : ℕ} (H : n < m) : n ≠ m := and_elim_right (succ_le_imp_le_and_ne H) theorem lt_irrefl (n : ℕ) : ¬ n < n := not_intro (assume H : n < n, absurd (refl n) (lt_imp_ne H)) theorem succ_pos (n : ℕ) : 0 < succ n := succ_le (zero_le n) theorem not_lt_zero (n : ℕ) : ¬ n < 0 := not_succ_zero_le n theorem lt_imp_eq_succ {n m : ℕ} (H : n < m) : exists k, m = succ k := discriminate (take (Hm : m = 0), absurd_elim _ (subst H Hm) (not_lt_zero n)) (take (l : ℕ) (Hm : m = succ l), exists_intro l Hm) -- ### interaction with le theorem lt_imp_le_succ {n m : ℕ} (H : n < m) : succ n ≤ m := H theorem le_succ_imp_lt {n m : ℕ} (H : succ n ≤ m) : n < m := H theorem self_lt_succ (n : ℕ) : n < succ n := le_refl (succ n) theorem lt_imp_le {n m : ℕ} (H : n < m) : n ≤ m := and_elim_left (succ_le_imp_le_and_ne H) theorem le_imp_lt_or_eq {n m : ℕ} (H : n ≤ m) : n < m ∨ n = m := le_imp_succ_le_or_eq H theorem le_ne_imp_lt {n m : ℕ} (H1 : n ≤ m) (H2 : n ≠ m) : n < m := le_ne_imp_succ_le H1 H2 theorem le_imp_lt_succ {n m : ℕ} (H : n ≤ m) : n < succ m := succ_le H theorem lt_succ_imp_le {n m : ℕ} (H : n < succ m) : n ≤ m := succ_le_cancel H -- ### transitivity, antisymmmetry theorem lt_le_trans {n m k : ℕ} (H1 : n < m) (H2 : m ≤ k) : n < k := le_trans H1 H2 theorem le_lt_trans {n m k : ℕ} (H1 : n ≤ m) (H2 : m < k) : n < k := le_trans (succ_le H1) H2 theorem lt_trans {n m k : ℕ} (H1 : n < m) (H2 : m < k) : n < k := lt_le_trans H1 (lt_imp_le H2) theorem le_imp_not_gt {n m : ℕ} (H : n ≤ m) : ¬ n > m := not_intro (assume H2 : m < n, absurd (le_lt_trans H H2) (lt_irrefl n)) theorem lt_imp_not_ge {n m : ℕ} (H : n < m) : ¬ n ≥ m := not_intro (assume H2 : m ≤ n, absurd (lt_le_trans H H2) (lt_irrefl n)) theorem lt_antisym {n m : ℕ} (H : n < m) : ¬ m < n := le_imp_not_gt (lt_imp_le H) -- ### interaction with addition theorem add_lt_left {n m : ℕ} (H : n < m) (k : ℕ) : k + n < k + m := substp (fun x, x ≤ k + m) (add_le_left H k) (add_succ_right k n) theorem add_lt_right {n m : ℕ} (H : n < m) (k : ℕ) : n + k < m + k := subst (subst (add_lt_left H k) (add_comm k n)) (add_comm k m) theorem add_le_lt {n m k l : ℕ} (H1 : n ≤ k) (H2 : m < l) : n + m < k + l := le_lt_trans (add_le_right H1 m) (add_lt_left H2 k) theorem add_lt_le {n m k l : ℕ} (H1 : n < k) (H2 : m ≤ l) : n + m < k + l := lt_le_trans (add_lt_right H1 m) (add_le_left H2 k) theorem add_lt {n m k l : ℕ} (H1 : n < k) (H2 : m < l) : n + m < k + l := add_lt_le H1 (lt_imp_le H2) theorem add_lt_cancel_left {n m k : ℕ} (H : k + n < k + m) : n < m := add_le_cancel_left (subst H (symm (add_succ_right k n))) theorem add_lt_cancel_right {n m k : ℕ} (H : n + k < m + k) : n < m := add_lt_cancel_left (subst (subst H (add_comm n k)) (add_comm m k)) -- ### interaction with successor (see also the interaction with le) theorem succ_lt {n m : ℕ} (H : n < m) : succ n < succ m := subst (subst (add_lt_right H 1) (add_one n)) (add_one m) theorem succ_lt_cancel {n m : ℕ} (H : succ n < succ m) : n < m := add_lt_cancel_right (subst (subst H (symm (add_one n))) (symm (add_one m))) theorem lt_imp_lt_succ {n m : ℕ} (H : n < m) : n < succ m := lt_trans H (self_lt_succ m) -- ### totality of lt and le theorem le_or_gt (n m : ℕ) : n ≤ m ∨ n > m := induction_on n (or_intro_left _ (zero_le m)) (take (k : ℕ), assume IH : k ≤ m ∨ m < k, or_elim IH (assume H : k ≤ m, obtain (l : ℕ) (Hl : k + l = m), from le_elim H, discriminate (assume H2 : l = 0, have H3 : m = k, from calc m = k + l : symm Hl ... = k + 0 : {H2} ... = k : add_zero_right k, have H4 : m < succ k, from subst (self_lt_succ m) H3, or_intro_right _ H4) (take l2 : nat, assume H2 : l = succ l2, have H3 : succ k + l2 = m, from calc succ k + l2 = k + succ l2 : add_move_succ k l2 ... = k + l : {symm H2} ... = m : Hl, or_intro_left _ (le_intro H3))) (assume H : m < k, or_intro_right _ (lt_imp_lt_succ H))) theorem trichotomy_alt (n m : ℕ) : (n < m ∨ n = m) ∨ n > m := or_imp_or_left (le_or_gt n m) (assume H : n ≤ m, le_imp_lt_or_eq H) theorem trichotomy (n m : ℕ) : n < m ∨ n = m ∨ n > m := iff_elim_left (or_assoc _ _ _) (trichotomy_alt n m) theorem le_total (n m : ℕ) : n ≤ m ∨ m ≤ n := or_imp_or_right (le_or_gt n m) (assume H : m < n, lt_imp_le H) theorem not_lt_imp_ge {n m : ℕ} (H : ¬ n < m) : n ≥ m := resolve_left (le_or_gt m n) H theorem not_le_imp_gt {n m : ℕ} (H : ¬ n ≤ m) : n > m := resolve_right (le_or_gt n m) H -- Note: interaction with multiplication under "positivity" -- ### misc theorem strong_induction_on {P : nat → Bool} (n : ℕ) (H : ∀n, (∀m, m < n → P m) → P n) : P n := have H1 : ∀n, ∀m, m < n → P m, from take n, induction_on n (show ∀m, m < 0 → P m, from take m H, absurd_elim _ H (not_lt_zero _)) (take n', assume IH : ∀m, m < n' → P m, have H2: P n', from H n' IH, show ∀m, m < succ n' → P m, from take m, assume H3 : m < succ n', or_elim (le_imp_lt_or_eq (lt_succ_imp_le H3)) (assume H4: m < n', IH _ H4) (assume H4: m = n', subst H2 (symm H4))), H1 _ _ (self_lt_succ n) theorem case_strong_induction_on {P : nat → Bool} (a : nat) (H0 : P 0) (Hind : ∀(n : nat), (∀m, m ≤ n → P m) → P (succ n)) : P a := strong_induction_on a ( take n, show (∀m, m < n → P m) → P n, from case n (assume H : (∀m, m < 0 → P m), show P 0, from H0) (take n, assume H : (∀m, m < succ n → P m), show P (succ n), from Hind n (take m, assume H1 : m ≤ n, H _ (le_imp_lt_succ H1)))) -- Positivity -- --------- -- -- Writing "t > 0" is the preferred way to assert that a natural number is positive. -- ### basic -- See also succ_pos. theorem case_zero_pos {P : ℕ → Bool} (y : ℕ) (H0 : P 0) (H1 : ∀y, y > 0 → P y) : P y := case y H0 (take y', H1 _ (succ_pos _)) theorem zero_or_pos (n : ℕ) : n = 0 ∨ n > 0 := or_imp_or_left (or_flip (le_imp_lt_or_eq (zero_le n))) (take H : 0 = n, symm H) theorem succ_imp_pos {n m : ℕ} (H : n = succ m) : n > 0 := subst (succ_pos m) (symm H) theorem ne_zero_pos {n : ℕ} (H : n ≠ 0) : n > 0 := or_elim (zero_or_pos n) (take H2 : n = 0, absurd_elim _ H2 H) (take H2 : n > 0, H2) theorem pos_imp_eq_succ {n : ℕ} (H : n > 0) : exists l, n = succ l := lt_imp_eq_succ H theorem add_pos_right (n : ℕ) {k : ℕ} (H : k > 0) : n + k > n := subst (add_lt_left H n) (add_zero_right n) theorem add_pos_left (n : ℕ) {k : ℕ} (H : k > 0) : k + n > n := subst (add_pos_right n H) (add_comm n k) -- ### multiplication theorem mul_pos {n m : ℕ} (Hn : n > 0) (Hm : m > 0) : n * m > 0 := obtain (k : ℕ) (Hk : n = succ k), from pos_imp_eq_succ Hn, obtain (l : ℕ) (Hl : m = succ l), from pos_imp_eq_succ Hm, succ_imp_pos calc n * m = succ k * m : {Hk} ... = succ k * succ l : {Hl} ... = succ k * l + succ k : mul_succ_right (succ k) l ... = succ (succ k * l + k) : add_succ_right _ _ theorem mul_pos_imp_pos_left {n m : ℕ} (H : n * m > 0) : n > 0 := discriminate (assume H2 : n = 0, have H3 : n * m = 0, from calc n * m = 0 * m : {H2} ... = 0 : mul_zero_left m, have H4 : 0 > 0, from subst H H3, absurd_elim _ H4 (lt_irrefl 0)) (take l : nat, assume Hl : n = succ l, subst (succ_pos l) (symm Hl)) theorem mul_pos_imp_pos_right {m n : ℕ} (H : n * m > 0) : m > 0 := mul_pos_imp_pos_left (subst H (mul_comm n m)) -- See also mul_eq_one below. -- ### interaction of mul with le and lt theorem mul_lt_left {n m k : ℕ} (Hk : k > 0) (H : n < m) : k * n < k * m := have H2 : k * n < k * n + k, from add_pos_right (k * n) Hk, have H3 : k * n + k ≤ k * m, from subst (mul_le_left H k) (mul_succ_right k n), lt_le_trans H2 H3 theorem mul_lt_right {n m k : ℕ} (Hk : k > 0) (H : n < m) : n * k < m * k := subst (subst (mul_lt_left Hk H) (mul_comm k n)) (mul_comm k m) theorem mul_le_lt {n m k l : ℕ} (Hk : k > 0) (H1 : n ≤ k) (H2 : m < l) : n * m < k * l := le_lt_trans (mul_le_right H1 m) (mul_lt_left Hk H2) theorem mul_lt_le {n m k l : ℕ} (Hl : l > 0) (H1 : n < k) (H2 : m ≤ l) : n * m < k * l := le_lt_trans (mul_le_left H2 n) (mul_lt_right Hl H1) theorem mul_lt {n m k l : ℕ} (H1 : n < k) (H2 : m < l) : n * m < k * l := have H3 : n * m ≤ k * m, from mul_le_right (lt_imp_le H1) m, have H4 : k * m < k * l, from mul_lt_left (le_lt_trans (zero_le n) H1) H2, le_lt_trans H3 H4 theorem mul_lt_cancel_left {n m k : ℕ} (H : k * n < k * m) : n < m := or_elim (le_or_gt m n) (assume H2 : m ≤ n, have H3 : k * m ≤ k * n, from mul_le_left H2 k, absurd_elim _ H3 (lt_imp_not_ge H)) (assume H2 : n < m, H2) -- previous, longer proof: -- have general : ∀ m, k * n < k * m → n < m, from -- induction_on n -- (take m : nat, -- assume H2 : k * 0 < k * m, -- have H3 : 0 < k * m, from subst H2 (mul_zero_right k), -- show 0 < m, from mul_pos_imp_pos_right H3) -- (take l : nat, -- assume IH : ∀ m, k * l < k * m → l < m, -- take m : nat, -- assume H2 : k * succ l < k * m, -- have H3 : 0 < k * m, from le_lt_trans (zero_le _) H2, -- have H4 : 0 < m, from mul_pos_imp_pos_right H3, -- obtain (l2 : ℕ) (Hl2 : m = succ l2), from pos_imp_eq_succ H4, -- have H5 : k * l + k < k * m, from subst H2 (mul_succ_right k l), -- have H6 : k * l + k < k * succ l2, from subst H5 Hl2, -- have H7 : k * l + k < k * l2 + k, from subst H6 (mul_succ_right k l2), -- have H8 : k * l < k * l2, from add_lt_cancel_right H7, -- have H9 : l < l2, from IH l2 H8, -- have H10 : succ l < succ l2, from succ_lt H9, -- show succ l < m, from subst H10 (symm Hl2)), -- general m H theorem mul_lt_cancel_right {n m k : ℕ} (H : n * k < m * k) : n < m := mul_lt_cancel_left (subst (subst H (mul_comm n k)) (mul_comm m k)) theorem mul_le_cancel_left {n m k : ℕ} (Hk : k > 0) (H : k * n ≤ k * m) : n ≤ m := have H2 : k * n < k * m + k, from le_lt_trans H (add_pos_right _ Hk), have H3 : k * n < k * succ m, from subst H2 (symm (mul_succ_right k m)), have H4 : n < succ m, from mul_lt_cancel_left H3, show n ≤ m, from lt_succ_imp_le H4 theorem mul_le_cancel_right {n k m : ℕ} (Hm : m > 0) (H : n * m ≤ k * m) : n ≤ k := mul_le_cancel_left Hm (subst (subst H (mul_comm n m)) (mul_comm k m)) theorem mul_cancel_left {m k n : ℕ} (Hn : n > 0) (H : n * m = n * k) : m = k := have H2 : n * m ≤ n * k, from subst (le_refl (n * m)) H, have H3 : n * k ≤ n * m, from subst (le_refl (n * m)) H, have H4 : m ≤ k, from mul_le_cancel_left Hn H2, have H5 : k ≤ m, from mul_le_cancel_left Hn H3, le_antisym H4 H5 theorem mul_cancel_left_or {n m k : ℕ} (H : n * m = n * k) : n = 0 ∨ m = k := or_imp_or_right (zero_or_pos n) (assume Hn : n > 0, mul_cancel_left Hn H) theorem mul_cancel_right {n m k : ℕ} (Hm : m > 0) (H : n * m = k * m) : n = k := mul_cancel_left Hm (subst (subst H (mul_comm n m)) (mul_comm k m)) theorem mul_cancel_right_or {n m k : ℕ} (H : n * m = k * m) : m = 0 ∨ n = k := mul_cancel_left_or (subst (subst H (mul_comm n m)) (mul_comm k m)) theorem mul_eq_one_left {n m : ℕ} (H : n * m = 1) : n = 1 := have H2 : n * m > 0, from subst (succ_pos 0) (symm H), have H3 : n > 0, from mul_pos_imp_pos_left H2, have H4 : m > 0, from mul_pos_imp_pos_right H2, or_elim (le_or_gt n 1) (assume H5 : n ≤ 1, show n = 1, from le_antisym H5 H3) (assume H5 : n > 1, have H6 : n * m ≥ 2 * 1, from mul_le H5 H4, have H7 : 1 ≥ 2, from subst (subst H6 H) (mul_one_right 2), absurd_elim _ (self_lt_succ 1) (le_imp_not_gt H7)) theorem mul_eq_one_right {n m : ℕ} (H : n * m = 1) : m = 1 := mul_eq_one_left (subst H (mul_comm n m)) --- theorem mul_eq_one {n m : ℕ} (H : n * m = 1) : n = 1 ∧ m = 1 --- := and_intro (mul_eq_one_left H) (mul_eq_one_right H) set_opaque lt true -- ### sub definition sub (n m : ℕ) : nat := rec n (fun m x, pred x) m infixl 65 - : sub theorem sub_zero_right (n : ℕ) : n - 0 = n := rec_zero _ _ theorem sub_succ_right (n m : ℕ) : n - succ m = pred (n - m) := rec_succ _ _ _ set_opaque sub true theorem sub_zero_left (n : ℕ) : 0 - n = 0 := induction_on n (sub_zero_right 0) (take k : nat, assume IH : 0 - k = 0, calc 0 - succ k = pred (0 - k) : sub_succ_right 0 k ... = pred 0 : {IH} ... = 0 : pred_zero) --( --theorem sub_succ_left (n m : ℕ) : pred (succ n - m) = n - m -- := -- induction_on m -- (calc -- pred (succ n - 0) = pred (succ n) : {sub_zero_right (succ n)} -- ... = n : pred_succ n -- ... = n - 0 : symm (sub_zero_right n)) -- (take k : nat, -- assume IH : pred (succ n - k) = n - k, -- _) --) --succ_sub_succ theorem sub_succ_succ (n m : ℕ) : succ n - succ m = n - m := induction_on m (calc succ n - 1 = pred (succ n - 0) : sub_succ_right (succ n) 0 ... = pred (succ n) : {sub_zero_right (succ n)} ... = n : pred_succ n ... = n - 0 : symm (sub_zero_right n)) (take k : nat, assume IH : succ n - succ k = n - k, calc succ n - succ (succ k) = pred (succ n - succ k) : sub_succ_right (succ n) (succ k) ... = pred (n - k) : {IH} ... = n - succ k : symm (sub_succ_right n k)) theorem sub_self (n : ℕ) : n - n = 0 := induction_on n (sub_zero_right 0) (take k IH, trans (sub_succ_succ k k) IH) --add_sub_add_right theorem sub_add_add_right (n k m : ℕ) : (n + k) - (m + k) = n - m := induction_on k (calc (n + 0) - (m + 0) = n - (m + 0) : {add_zero_right _} ... = n - m : {add_zero_right _}) (take l : nat, assume IH : (n + l) - (m + l) = n - m, calc (n + succ l) - (m + succ l) = succ (n + l) - (m + succ l) : {add_succ_right _ _} ... = succ (n + l) - succ (m + l) : {add_succ_right _ _} ... = (n + l) - (m + l) : sub_succ_succ _ _ ... = n - m : IH) theorem sub_add_add_left (k n m : ℕ) : (k + n) - (k + m) = n - m := subst (subst (sub_add_add_right n k m) (add_comm n k)) (add_comm m k) --add_sub_inv theorem sub_add_left (n m : ℕ) : n + m - m = n := induction_on m (subst (sub_zero_right n) (symm (add_zero_right n))) (take k : nat, assume IH : n + k - k = n, calc n + succ k - succ k = succ (n + k) - succ k : {add_succ_right n k} ... = n + k - k : sub_succ_succ _ _ ... = n : IH) --add_sub_inv' theorem sub_add_left2 (n m : ℕ) : n + m - n = m := subst (sub_add_left m n) (add_comm m n) theorem sub_sub (n m k : ℕ) : n - m - k = n - (m + k) := induction_on k (calc n - m - 0 = n - m : sub_zero_right _ ... = n - (m + 0) : {symm (add_zero_right m)}) (take l : nat, assume IH : n - m - l = n - (m + l), calc n - m - succ l = pred (n - m - l) : sub_succ_right (n - m) l ... = pred (n - (m + l)) : {IH} ... = n - succ (m + l) : symm (sub_succ_right n (m + l)) ... = n - (m + succ l) : {symm (add_succ_right m l)}) theorem succ_sub_sub (n m k : ℕ) : succ n - m - succ k = n - m - k := calc succ n - m - succ k = succ n - (m + succ k) : sub_sub _ _ _ ... = succ n - succ (m + k) : {add_succ_right m k} ... = n - (m + k) : sub_succ_succ _ _ ... = n - m - k : symm (sub_sub n m k) theorem sub_add_right_eq_zero (n m : ℕ) : n - (n + m) = 0 := calc n - (n + m) = n - n - m : symm (sub_sub n n m) ... = 0 - m : {sub_self n} ... = 0 : sub_zero_left m theorem sub_comm (m n k : ℕ) : m - n - k = m - k - n := calc m - n - k = m - (n + k) : sub_sub m n k ... = m - (k + n) : {add_comm n k} ... = m - k - n : symm (sub_sub m k n) theorem sub_one (n : ℕ) : n - 1 = pred n := calc n - 1 = pred (n - 0) : sub_succ_right n 0 ... = pred n : {sub_zero_right n} theorem succ_sub_one (n : ℕ) : succ n - 1 = n := trans (sub_succ_succ n 0) (sub_zero_right n) add_rewrite sub_add_left -- ### interaction with multiplication theorem mul_pred_left (n m : ℕ) : pred n * m = n * m - m := induction_on n (calc pred 0 * m = 0 * m : {pred_zero} ... = 0 : mul_zero_left _ ... = 0 - m : symm (sub_zero_left m) ... = 0 * m - m : {symm (mul_zero_left m)}) (take k : nat, assume IH : pred k * m = k * m - m, calc pred (succ k) * m = k * m : {pred_succ k} ... = k * m + m - m : symm (sub_add_left _ _) ... = succ k * m - m : {symm (mul_succ_left k m)}) theorem mul_pred_right (n m : ℕ) : n * pred m = n * m - n := calc n * pred m = pred m * n : mul_comm _ _ ... = m * n - n : mul_pred_left m n ... = n * m - n : {mul_comm m n} theorem mul_sub_distr_right (n m k : ℕ) : (n - m) * k = n * k - m * k := induction_on m (calc (n - 0) * k = n * k : {sub_zero_right n} ... = n * k - 0 : symm (sub_zero_right _) ... = n * k - 0 * k : {symm (mul_zero_left _)}) (take l : nat, assume IH : (n - l) * k = n * k - l * k, calc (n - succ l) * k = pred (n - l) * k : {sub_succ_right n l} ... = (n - l) * k - k : mul_pred_left _ _ ... = n * k - l * k - k : {IH} ... = n * k - (l * k + k) : sub_sub _ _ _ ... = n * k - (succ l * k) : {symm (mul_succ_left l k)}) theorem mul_sub_distr_left (n m k : ℕ) : n * (m - k) = n * m - n * k := calc n * (m - k) = (m - k) * n : mul_comm _ _ ... = m * n - k * n : mul_sub_distr_right _ _ _ ... = n * m - k * n : {mul_comm _ _} ... = n * m - n * k : {mul_comm _ _} -- ### interaction with inequalities theorem succ_sub {m n : ℕ} : m ≥ n → succ m - n = succ (m - n) := sub_induction n m (take k, assume H : 0 ≤ k, calc succ k - 0 = succ k : sub_zero_right (succ k) ... = succ (k - 0) : {symm (sub_zero_right k)}) (take k, assume H : succ k ≤ 0, absurd_elim _ H (not_succ_zero_le k)) (take k l, assume IH : k ≤ l → succ l - k = succ (l - k), take H : succ k ≤ succ l, calc succ (succ l) - succ k = succ l - k : sub_succ_succ (succ l) k ... = succ (l - k) : IH (succ_le_cancel H) ... = succ (succ l - succ k) : {symm (sub_succ_succ l k)}) theorem le_imp_sub_eq_zero {n m : ℕ} (H : n ≤ m) : n - m = 0 := obtain (k : ℕ) (Hk : n + k = m), from le_elim H, subst (sub_add_right_eq_zero n k) Hk theorem add_sub_le {n m : ℕ} : n ≤ m → n + (m - n) = m := sub_induction n m (take k, assume H : 0 ≤ k, calc 0 + (k - 0) = k - 0 : add_zero_left (k - 0) ... = k : sub_zero_right k) (take k, assume H : succ k ≤ 0, absurd_elim _ H (not_succ_zero_le k)) (take k l, assume IH : k ≤ l → k + (l - k) = l, take H : succ k ≤ succ l, calc succ k + (succ l - succ k) = succ k + (l - k) : {sub_succ_succ l k} ... = succ (k + (l - k)) : add_succ_left k (l - k) ... = succ l : {IH (succ_le_cancel H)}) theorem add_sub_ge_left {n m : ℕ} : n ≥ m → n - m + m = n := subst add_sub_le (add_comm m (n - m)) theorem add_sub_ge {n m : ℕ} (H : n ≥ m) : n + (m - n) = n := calc n + (m - n) = n + 0 : {le_imp_sub_eq_zero H} ... = n : add_zero_right n theorem add_sub_le_left {n m : ℕ} : n ≤ m → n - m + m = m := subst add_sub_ge (add_comm m (n - m)) theorem le_add_sub_left (n m : ℕ) : n ≤ n + (m - n) := or_elim (le_total n m) (assume H : n ≤ m, subst H (symm (add_sub_le H))) (assume H : m ≤ n, subst (le_refl n) (symm (add_sub_ge H))) theorem le_add_sub_right (n m : ℕ) : m ≤ n + (m - n) := or_elim (le_total n m) (assume H : n ≤ m, subst (le_refl m) (symm (add_sub_le H))) (assume H : m ≤ n, subst H (symm (add_sub_ge H))) theorem sub_split {P : nat → Bool} {n m : ℕ} (H1 : n ≤ m → P 0) (H2 : ∀k, m + k = n -> P k) : P (n - m) := or_elim (le_total n m) (assume H3 : n ≤ m, subst (H1 H3) (symm (le_imp_sub_eq_zero H3))) (assume H3 : m ≤ n, H2 (n - m) (add_sub_le H3)) theorem sub_le_self (n m : ℕ) : n - m ≤ n := sub_split (assume H : n ≤ m, zero_le n) (take k : nat, assume H : m + k = n, le_intro (subst H (add_comm m k))) theorem le_elim_sub (n m : ℕ) (H : n ≤ m) : exists k, m - k = n := obtain (k : ℕ) (Hk : n + k = m), from le_elim H, exists_intro k calc m - k = n + k - k : {symm Hk} ... = n : sub_add_left n k theorem add_sub_assoc {m k : ℕ} (H : k ≤ m) (n : ℕ) : n + m - k = n + (m - k) := have lemma : k ≤ m → n + m - k = n + (m - k), from sub_induction k m (take m : nat, assume H : 0 ≤ m, calc n + m - 0 = n + m : sub_zero_right (n + m) ... = n + (m - 0) : {symm (sub_zero_right m)}) (take k : nat, assume H : succ k ≤ 0, absurd_elim _ H (not_succ_zero_le k)) (take k m, assume IH : k ≤ m → n + m - k = n + (m - k), take H : succ k ≤ succ m, calc n + succ m - succ k = succ (n + m) - succ k : {add_succ_right n m} ... = n + m - k : sub_succ_succ (n + m) k ... = n + (m - k) : IH (succ_le_cancel H) ... = n + (succ m - succ k) : {symm (sub_succ_succ m k)}), lemma H theorem sub_eq_zero_imp_le {n m : ℕ} : n - m = 0 → n ≤ m := sub_split (assume H1 : n ≤ m, assume H2 : 0 = 0, H1) (take k : nat, assume H1 : m + k = n, assume H2 : k = 0, have H3 : n = m, from subst (subst (symm H1) H2) (add_zero_right m), subst (le_refl n) H3) theorem sub_sub_split {P : nat → nat → Bool} {n m : ℕ} (H1 : ∀k, n = m + k -> P k 0) (H2 : ∀k, m = n + k → P 0 k) : P (n - m) (m - n) := or_elim (le_total n m) (assume H3 : n ≤ m, subst (H2 (m - n) (symm (add_sub_le H3))) (symm (le_imp_sub_eq_zero H3))) (assume H3 : m ≤ n, subst (H1 (n - m) (symm (add_sub_le H3))) (symm (le_imp_sub_eq_zero H3))) theorem sub_intro {n m k : ℕ} (H : n + m = k) : k - n = m := have H2 : k - n + n = m + n, from calc k - n + n = k : add_sub_ge_left (le_intro H) ... = n + m : symm H ... = m + n : add_comm n m, add_cancel_right H2 theorem sub_lt {x y : ℕ} (xpos : x > 0) (ypos : y > 0) : x - y < x := obtain (x' : ℕ) (xeq : x = succ x'), from pos_imp_eq_succ xpos, obtain (y' : ℕ) (yeq : y = succ y'), from pos_imp_eq_succ ypos, have xsuby_eq : x - y = x' - y', from calc x - y = succ x' - y : {xeq} ... = succ x' - succ y' : {yeq} ... = x' - y' : sub_succ_succ _ _, have H1 : x' - y' ≤ x', from sub_le_self _ _, have H2 : x' < succ x', from self_lt_succ _, show x - y < x, from subst (subst (le_lt_trans H1 H2) (symm xsuby_eq)) (symm xeq) theorem sub_le_right {n m : ℕ} (H : n ≤ m) (k : nat) : n - k ≤ m - k := obtain (l : ℕ) (Hl : n + l = m), from le_elim H, or_elim (le_total n k) (assume H2 : n ≤ k, subst (zero_le (m - k)) (symm (le_imp_sub_eq_zero H2))) (assume H2 : k ≤ n, have H3 : n - k + l = m - k, from calc n - k + l = l + (n - k) : by simp ... = l + n - k : symm (add_sub_assoc H2 l) ... = n + l - k : {add_comm l n} ... = m - k : {Hl}, le_intro H3) theorem sub_le_left {n m : ℕ} (H : n ≤ m) (k : nat) : k - m ≤ k - n := obtain (l : ℕ) (Hl : n + l = m), from le_elim H, sub_split (assume H2 : k ≤ m, zero_le (k - n)) (take m' : ℕ, assume Hm : m + m' = k, have H3 : n ≤ k, from le_trans H (le_intro Hm), have H4 : m' + l + n = k - n + n, from calc m' + l + n = n + l + m' : by simp_no_assump ... = m + m' : {Hl} ... = k : Hm ... = k - n + n : symm (add_sub_ge_left H3), le_intro (add_cancel_right H4)) -- theorem sub_lt_cancel_right {n m k : ℕ) (H : n - k < m - k) : n < m -- := -- _ -- theorem sub_lt_cancel_left {n m k : ℕ) (H : n - m < n - k) : k < m -- := -- _ theorem sub_triangle_inequality (n m k : ℕ) : n - k ≤ (n - m) + (m - k) := sub_split (assume H : n ≤ m, subst (sub_le_right H k) (symm (add_zero_left (m - k)))) (take mn : ℕ, assume Hmn : m + mn = n, sub_split (assume H : m ≤ k, have H2 : n - k ≤ n - m, from sub_le_left H n, have H3 : n - k ≤ mn, from subst H2 (sub_intro Hmn), show n - k ≤ mn + 0, from subst H3 (symm (add_zero_right mn))) (take km : ℕ, assume Hkm : k + km = m, have H : k + (mn + km) = n, from calc k + (mn + km) = k + km + mn : by simp_no_assump ... = m + mn : {Hkm} ... = n : Hmn, have H2 : n - k = mn + km, from sub_intro H, subst (le_refl (n - k)) H2)) add_rewrite sub_self mul_sub_distr_left mul_sub_distr_right -- Max, min, iteration, and absolute difference -- -------------------------------------------- definition max (n m : ℕ) : ℕ := n + (m - n) definition min (n m : ℕ) : ℕ := m - (m - n) theorem max_le {n m : ℕ} (H : n ≤ m) : n + (m - n) = m := add_sub_le H theorem max_ge {n m : ℕ} (H : n ≥ m) : n + (m - n) = n := add_sub_ge H theorem left_le_max (n m : ℕ) : n ≤ n + (m - n) := le_add_sub_left n m theorem right_le_max (n m : ℕ) : m ≤ max n m := le_add_sub_right n m -- ### absolute difference -- This section is still incomplete definition dist (n m : ℕ) := (n - m) + (m - n) theorem dist_comm (n m : ℕ) : dist n m = dist m n := add_comm (n - m) (m - n) theorem dist_self (n : ℕ) : dist n n = 0 := calc (n - n) + (n - n) = 0 + 0 : by simp ... = 0 : by simp theorem dist_eq_zero {n m : ℕ} (H : dist n m = 0) : n = m := have H2 : n - m = 0, from add_eq_zero_left H, have H3 : n ≤ m, from sub_eq_zero_imp_le H2, have H4 : m - n = 0, from add_eq_zero_right H, have H5 : m ≤ n, from sub_eq_zero_imp_le H4, le_antisym H3 H5 theorem dist_le {n m : ℕ} (H : n ≤ m) : dist n m = m - n := calc dist n m = 0 + (m - n) : {le_imp_sub_eq_zero H} ... = m - n : add_zero_left (m - n) theorem dist_ge {n m : ℕ} (H : n ≥ m) : dist n m = n - m := subst (dist_le H) (dist_comm m n) theorem dist_zero_right (n : ℕ) : dist n 0 = n := trans (dist_ge (zero_le n)) (sub_zero_right n) theorem dist_zero_left (n : ℕ) : dist 0 n = n := trans (dist_le (zero_le n)) (sub_zero_right n) theorem dist_intro {n m k : ℕ} (H : n + m = k) : dist k n = m := calc dist k n = k - n : dist_ge (le_intro H) ... = m : sub_intro H theorem dist_add_right (n k m : ℕ) : dist (n + k) (m + k) = dist n m := calc dist (n + k) (m + k) = n - m + ((m + k) - (n + k)) : {sub_add_add_right n k m} ... = n - m + (m - n) : {sub_add_add_right m k n} theorem dist_add_left (k n m : ℕ) : dist (k + n) (k + m) = dist n m := subst (subst (dist_add_right n k m) (add_comm n k)) (add_comm m k) add_rewrite dist_self dist_add_right dist_add_left dist_zero_left dist_zero_right theorem dist_ge_add_right {n m : ℕ} (H : n ≥ m) : dist n m + m = n := calc dist n m + m = n - m + m : {dist_ge H} ... = n : add_sub_ge_left H theorem dist_eq_intro {n m k l : ℕ} (H : n + m = k + l) : dist n k = dist l m := calc dist n k = dist (n + m) (k + m) : symm (dist_add_right n m k) ... = dist (k + l) (k + m) : {H} ... = dist l m : dist_add_left k l m theorem dist_sub_move_add {n m : ℕ} (H : n ≥ m) (k : ℕ) : dist (n - m) k = dist n (k + m) := have H2 : n - m + (k + m) = k + n, from calc n - m + (k + m) = n - m + m + k : by simp ... = n + k : {add_sub_ge_left H} ... = k + n : by simp, dist_eq_intro H2 theorem dist_sub_move_add' {k m : ℕ} (H : k ≥ m) (n : ℕ) : dist n (k - m) = dist (n + m) k := subst (subst (dist_sub_move_add H n) (dist_comm (k - m) n)) (dist_comm k (n + m)) --triangle inequality formulated with dist theorem triangle_inequality (n m k : ℕ) : dist n k ≤ dist n m + dist m k := have H : (n - m) + (m - k) + ((k - m) + (m - n)) = (n - m) + (m - n) + ((m - k) + (k - m)), by simp, subst (add_le (sub_triangle_inequality n m k) (sub_triangle_inequality k m n)) H theorem dist_add_le_add_dist (n m k l : ℕ) : dist (n + m) (k + l) ≤ dist n k + dist m l := have H : dist (n + m) (k + m) + dist (k + m) (k + l) = dist n k + dist m l, from calc _ = dist n k + dist (k + m) (k + l) : {dist_add_right n m k} ... = _ : {dist_add_left k m l}, subst (triangle_inequality (n + m) (k + m) (k + l)) H --interaction with multiplication theorem dist_mul_left (k n m : ℕ) : dist (k * n) (k * m) = k * dist n m := have H : ∀n m, dist n m = n - m + (m - n), from take n m, refl _, by simp theorem dist_mul_right (n k m : ℕ) : dist (n * k) (m * k) = dist n m * k := have H : ∀n m, dist n m = n - m + (m - n), from take n m, refl _, by simp add_rewrite dist_mul_right dist_mul_left dist_comm --needed to prove |a| * |b| = |a * b| in int theorem dist_mul_dist (n m k l : ℕ) : dist n m * dist k l = dist (n * k + m * l) (n * l + m * k) := have lemma : ∀k l, k ≥ l → dist n m * dist k l = dist (n * k + m * l) (n * l + m * k), from take k l : ℕ, assume H : k ≥ l, have H2 : m * k ≥ m * l, from mul_le_left H m, have H3 : n * l + m * k ≥ m * l, from le_trans H2 (le_add_left _ _), calc dist n m * dist k l = dist n m * (k - l) : {dist_ge H} ... = dist (n * (k - l)) (m * (k - l)) : symm (dist_mul_right n (k - l) m) ... = dist (n * k - n * l) (m * k - m * l) : by simp ... = dist (n * k) (m * k - m * l + n * l) : dist_sub_move_add (mul_le_left H n) _ ... = dist (n * k) (n * l + (m * k - m * l)) : {add_comm _ _} ... = dist (n * k) (n * l + m * k - m * l) : {symm (add_sub_assoc H2 (n * l))} ... = dist (n * k + m * l) (n * l + m * k) : dist_sub_move_add' H3 _, or_elim (le_total k l) (assume H : k ≤ l, subst (subst (lemma l k H) (dist_comm _ _)) (dist_comm l k)) (assume H : l ≤ k, lemma k l H) set_opaque dist true --- subst (subst (refl (((n + k) - (m + k)) + ((m + k) - (n + k)))) --- (sub_add_add_right n m k)) (sub_add_add_right m n k) --- end --namespace nat
f44ac259cbb3299f5890afb86949ef8f37cf61e1
3bdd27ffdff3ffa22d4bb010eba695afcc96bc4a
/src/barycentric_subdivision.lean
ccd51554eb38d64af96556ce406588c5c63fd5bd
[]
no_license
mmasdeu/brouwerfixedpoint
684d712c982c6a8b258b4e2c6b2eab923f2f1289
548270f79ecf12d7e20a256806ccb9fcf57b87e2
refs/heads/main
1,690,539,793,996
1,631,801,831,000
1,631,801,831,000
368,139,809
4
3
null
1,624,453,250,000
1,621,246,034,000
Lean
UTF-8
Lean
false
false
2,044
lean
import data.real.basic import combinatorics.simplicial_complex.basic import combinatorics.simplicial_complex.finite import combinatorics.simplicial_complex.sperner variables {m n : ℕ} local notation `E` := fin m → ℝ noncomputable def barycenter(L: list E): E := (list.foldl (λ x:E , λ y:E, x + y) (0 : E) L) / L.length variables (L : list E) [pseudo_metric_space E] namespace list -- Given an order of the vertices of each face, we have a simplex of the barycentric subdivision. -- Recursive definition -- If L.length > 1, -- add the point `barycenter L` and all the points of `face_barycentric_subdivision L.tail` -- If L.length = 1, add the point. noncomputable def face_baricentric_subdivision(L : list E) : finset E := begin induction L with head tail smaller_dim_baricentric_subdivision, { exact (∅ : finset E) }, { exact { barycenter (list.cons head tail) } ∪ smaller_dim_baricentric_subdivision }, end open affine -- The goal is to complete this definition. noncomputable def simplicial_complex.barycentric_subdivision (S : simplicial_complex E) : simplicial_complex E := { faces := {X | ∃ {L : list E}, list.nodup L ∧ list.to_finset L ∈ S.faces ∧ X = face_baricentric_subdivision L}, indep := begin rintros face ⟨vertex_list, ⟨vertex_nodup, ⟨h_face3, face_from_vertex_list⟩⟩⟩, cases S, simp at h_face3, -- help? sorry, end, down_closed := sorry, disjoint := sorry } -- The maximum distance bewteen any pair of vertices in a simplicial complex. noncomputable def distance_vertices (A : set E) : ℝ := metric.diam A --Sup {d | ∃ x y ∈ A, d = edist x y} lemma distance_vertices_eq_diam_convexhull(A : set E): metric.diam (convex_hull A) = metric.diam A := begin -- apply convex_hull_diam, sorry, end open affine open set -- simplicial_complex.mesh_size def diameter (S: simplicial_complex E) [simplicial_complex.finite S]: ℝ := sorry -- maxim diametre entre totes les cares. end list
5b4ebcf0fd2ee7eeccce5e47a5035ea0b4bbecb1
00f20606b80ff481edcb3343798a879c6c568f6c
/src/Euclid/axioms.lean
5b4fe51116356e74554b5cbbfb5dda597a3943e5
[ "Apache-2.0" ]
permissive
SG4316/xena-UROP-2018
53c21afcc6909bde186e3d9b8e8bf4c888a5f32e
d20b155d317e210917e9ce88d91457a7c0f2efac
refs/heads/master
1,584,877,373,507
1,530,817,535,000
1,530,817,535,000
139,990,847
0
0
null
1,530,884,893,000
1,530,884,893,000
null
UTF-8
Lean
false
false
247
lean
import Euclid.unordered_pairs structure Euclidean_plane := (Point : Type) (Line : Type) (ends) (is_on : Point → Line → Prop) (line_through_points : ∀ p q : Point, p ≠ q → ∃! L : Line, is_on p L ∧ is_on q L) (Line_segment : Type)
d86d0b55d15e49b12532ab98566d035f7de10767
4727251e0cd73359b15b664c3170e5d754078599
/src/topology/algebra/continuous_affine_map.lean
3b560a01d7064caf93030f6aae87f94bb893f026
[ "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
8,347
lean
/- Copyright (c) 2021 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import linear_algebra.affine_space.affine_map import topology.continuous_function.basic import topology.algebra.module.basic /-! # Continuous affine maps. This file defines a type of bundled continuous affine maps. Note that the definition and basic properties established here require minimal assumptions, and do not even assume compatibility between the topological and algebraic structures. Of course it is necessary to assume some compatibility in order to obtain a useful theory. Such a theory is developed elsewhere for affine spaces modelled on _normed_ vector spaces, but not yet for general topological affine spaces (since we have not defined these yet). ## Main definitions: * `continuous_affine_map` ## Notation: We introduce the notation `P →A[R] Q` for `continuous_affine_map R P Q`. Note that this is parallel to the notation `E →L[R] F` for `continuous_linear_map R E F`. -/ /-- A continuous map of affine spaces. -/ structure continuous_affine_map (R : Type*) {V W : Type*} (P Q : Type*) [ring R] [add_comm_group V] [module R V] [topological_space P] [add_torsor V P] [add_comm_group W] [module R W] [topological_space Q] [add_torsor W Q] extends P →ᵃ[R] Q := (cont : continuous to_fun) notation P ` →A[`:25 R `] ` Q := continuous_affine_map R P Q namespace continuous_affine_map variables {R V W P Q : Type*} [ring R] variables [add_comm_group V] [module R V] [topological_space P] [add_torsor V P] variables [add_comm_group W] [module R W] [topological_space Q] [add_torsor W Q] include V W /-- see Note [function coercion] -/ instance : has_coe_to_fun (P →A[R] Q) (λ _, P → Q) := ⟨λ f, f.to_affine_map.to_fun⟩ lemma to_fun_eq_coe (f : P →A[R] Q) : f.to_fun = ⇑f := rfl lemma coe_injective : @function.injective (P →A[R] Q) (P → Q) coe_fn := begin rintros ⟨⟨f, ⟨f', hf₁, hf₂⟩, hf₀⟩, hf₁⟩ ⟨⟨g, ⟨g', hg₁, hg₂⟩, hg₀⟩, hg₁⟩ h, have : f = g ∧ f' = g', { simpa only using affine_map.coe_fn_injective h, }, congr, exacts [this.1, this.2], end @[ext] lemma ext {f g : P →A[R] Q} (h : ∀ x, f x = g x) : f = g := coe_injective $ funext h lemma ext_iff {f g : P →A[R] Q} : f = g ↔ ∀ x, f x = g x := ⟨by { rintro rfl x, refl, }, ext⟩ lemma congr_fun {f g : P →A[R] Q} (h : f = g) (x : P) : f x = g x := h ▸ rfl instance : has_coe (P →A[R] Q) (P →ᵃ[R] Q) := ⟨to_affine_map⟩ /-- Forgetting its algebraic properties, a continuous affine map is a continuous map. -/ def to_continuous_map (f : P →A[R] Q) : C(P, Q) := ⟨f, f.cont⟩ instance : has_coe (P →A[R] Q) (C(P, Q)) := ⟨to_continuous_map⟩ @[simp] lemma to_affine_map_eq_coe (f : P →A[R] Q) : f.to_affine_map = ↑f := rfl @[simp] lemma to_continuous_map_coe (f : P →A[R] Q) : f.to_continuous_map = ↑f := rfl @[simp, norm_cast] lemma coe_to_affine_map (f : P →A[R] Q) : ((f : P →ᵃ[R] Q) : P → Q) = f := rfl @[simp, norm_cast] lemma coe_to_continuous_map (f : P →A[R] Q) : ((f : C(P, Q)) : P → Q) = f := rfl lemma to_affine_map_injective {f g : P →A[R] Q} (h : (f : P →ᵃ[R] Q) = (g : P →ᵃ[R] Q)) : f = g := by { ext a, exact affine_map.congr_fun h a, } lemma to_continuous_map_injective {f g : P →A[R] Q} (h : (f : C(P, Q)) = (g : C(P, Q))) : f = g := by { ext a, exact continuous_map.congr_fun h a, } @[norm_cast] lemma coe_affine_map_mk (f : P →ᵃ[R] Q) (h) : ((⟨f, h⟩ : P →A[R] Q) : P →ᵃ[R] Q) = f := rfl @[norm_cast] lemma coe_continuous_map_mk (f : P →ᵃ[R] Q) (h) : ((⟨f, h⟩ : P →A[R] Q) : C(P, Q)) = ⟨f, h⟩ := rfl @[simp] lemma coe_mk (f : P →ᵃ[R] Q) (h) : ((⟨f, h⟩ : P →A[R] Q) : P → Q) = f := rfl @[simp] lemma mk_coe (f : P →A[R] Q) (h) : (⟨(f : P →ᵃ[R] Q), h⟩ : P →A[R] Q) = f := by { ext, refl, } @[continuity] protected lemma continuous (f : P →A[R] Q) : continuous f := f.2 variables (R P) /-- The constant map is a continuous affine map. -/ def const (q : Q) : P →A[R] Q := { to_fun := affine_map.const R P q, cont := continuous_const, .. affine_map.const R P q, } @[simp] lemma coe_const (q : Q) : (const R P q : P → Q) = function.const P q := rfl noncomputable instance : inhabited (P →A[R] Q) := ⟨const R P $ nonempty.some (by apply_instance : nonempty Q)⟩ variables {R P} {W₂ Q₂ : Type*} variables [add_comm_group W₂] [module R W₂] [topological_space Q₂] [add_torsor W₂ Q₂] include W₂ /-- The composition of morphisms is a morphism. -/ def comp (f : Q →A[R] Q₂) (g : P →A[R] Q) : P →A[R] Q₂ := { cont := f.cont.comp g.cont, .. (f : Q →ᵃ[R] Q₂).comp (g : P →ᵃ[R] Q), } @[simp, norm_cast] lemma coe_comp (f : Q →A[R] Q₂) (g : P →A[R] Q) : (f.comp g : P → Q₂) = (f : Q → Q₂) ∘ (g : P → Q) := rfl lemma comp_apply (f : Q →A[R] Q₂) (g : P →A[R] Q) (x : P) : f.comp g x = f (g x) := rfl omit W₂ section module_valued_maps variables {S : Type*} variables [topological_space W] instance : has_zero (P →A[R] W) := ⟨continuous_affine_map.const R P 0⟩ @[norm_cast, simp] lemma coe_zero : ((0 : P →A[R] W) : P → W) = 0 := rfl lemma zero_apply (x : P) : (0 : P →A[R] W) x = 0 := rfl section mul_action variables [monoid S] [distrib_mul_action S W] [smul_comm_class R S W] variables [has_continuous_const_smul S W] instance : has_scalar S (P →A[R] W) := { smul := λ t f, { cont := f.continuous.const_smul t, .. (t • (f : P →ᵃ[R] W)) } } @[norm_cast, simp] lemma coe_smul (t : S) (f : P →A[R] W) : ⇑(t • f) = t • f := rfl lemma smul_apply (t : S) (f : P →A[R] W) (x : P) : (t • f) x = t • (f x) := rfl instance [distrib_mul_action Sᵐᵒᵖ W] [is_central_scalar S W] : is_central_scalar S (P →A[R] W) := { op_smul_eq_smul := λ t f, ext $ λ _, op_smul_eq_smul _ _ } instance : mul_action S (P →A[R] W) := function.injective.mul_action _ coe_injective coe_smul end mul_action variables [topological_add_group W] instance : has_add (P →A[R] W) := { add := λ f g, { cont := f.continuous.add g.continuous, .. ((f : P →ᵃ[R] W) + (g : P →ᵃ[R] W)) }, } @[norm_cast, simp] lemma coe_add (f g : P →A[R] W) : ⇑(f + g) = f + g := rfl lemma add_apply (f g : P →A[R] W) (x : P) : (f + g) x = f x + g x := rfl instance : has_sub (P →A[R] W) := { sub := λ f g, { cont := f.continuous.sub g.continuous, .. ((f : P →ᵃ[R] W) - (g : P →ᵃ[R] W)) }, } @[norm_cast, simp] lemma coe_sub (f g : P →A[R] W) : ⇑(f - g) = f - g := rfl lemma sub_apply (f g : P →A[R] W) (x : P) : (f - g) x = f x - g x := rfl instance : has_neg (P →A[R] W) := { neg := λ f, { cont := f.continuous.neg, .. (-(f : P →ᵃ[R] W)) }, } @[norm_cast, simp] lemma coe_neg (f : P →A[R] W) : ⇑(-f) = -f := rfl lemma neg_apply (f : P →A[R] W) (x : P) : (-f) x = -(f x) := rfl instance : add_comm_group (P →A[R] W) := coe_injective.add_comm_group _ coe_zero coe_add coe_neg coe_sub (λ _ _, coe_smul _ _) (λ _ _, coe_smul _ _) instance [monoid S] [distrib_mul_action S W] [smul_comm_class R S W] [has_continuous_const_smul S W] : distrib_mul_action S (P →A[R] W) := function.injective.distrib_mul_action ⟨λ f, f.to_affine_map.to_fun, rfl, coe_add⟩ coe_injective coe_smul instance [semiring S] [module S W] [smul_comm_class R S W] [has_continuous_const_smul S W] : module S (P →A[R] W) := function.injective.module S ⟨λ f, f.to_affine_map.to_fun, rfl, coe_add⟩ coe_injective coe_smul end module_valued_maps end continuous_affine_map namespace continuous_linear_map variables {R V W : Type*} [ring R] variables [add_comm_group V] [module R V] [topological_space V] variables [add_comm_group W] [module R W] [topological_space W] /-- A continuous linear map can be regarded as a continuous affine map. -/ def to_continuous_affine_map (f : V →L[R] W) : V →A[R] W := { to_fun := f, linear := f, map_vadd' := by simp, cont := f.cont, } @[simp] lemma coe_to_continuous_affine_map (f : V →L[R] W) : ⇑f.to_continuous_affine_map = f := rfl @[simp] lemma to_continuous_affine_map_map_zero (f : V →L[R] W) : f.to_continuous_affine_map 0 = 0 := by simp end continuous_linear_map
9bca1061ca74ec8aadc39276344dd2179affa807
4fa161becb8ce7378a709f5992a594764699e268
/src/order/filter/at_top_bot.lean
fb896f7fb39f8c10817d684d2aacc3ecadad6ddb
[ "Apache-2.0" ]
permissive
laughinggas/mathlib
e4aa4565ae34e46e834434284cb26bd9d67bc373
86dcd5cda7a5017c8b3c8876c89a510a19d49aad
refs/heads/master
1,669,496,232,688
1,592,831,995,000
1,592,831,995,000
274,155,979
0
0
Apache-2.0
1,592,835,190,000
1,592,835,189,000
null
UTF-8
Lean
false
false
19,968
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, Yury Kudryashov, Patrick Massot -/ import order.filter.basic /-! # `at_top` and `at_bot` filters on preorded sets, monoids and groups. In this file we define the filters * `at_top`: corresponds to `n → +∞`; * `at_bot`: corresponds to `n → -∞`. Then we prove many lemmas like “if `f → +∞`, then `f ± c → +∞`”. -/ variables {α β γ : Type*} open set open_locale classical namespace filter /-- `at_top` is the filter representing the limit `→ ∞` on an ordered set. It is generated by the collection of up-sets `{b | a ≤ b}`. (The preorder need not have a top element for this to be well defined, and indeed is trivial when a top element exists.) -/ def at_top [preorder α] : filter α := ⨅ a, principal {b | a ≤ b} /-- `at_bot` is the filter representing the limit `→ -∞` on an ordered set. It is generated by the collection of down-sets `{b | b ≤ a}`. (The preorder need not have a bottom element for this to be well defined, and indeed is trivial when a bottom element exists.) -/ def at_bot [preorder α] : filter α := ⨅ a, principal {b | b ≤ a} lemma mem_at_top [preorder α] (a : α) : {b : α | a ≤ b} ∈ @at_top α _ := mem_infi_sets a $ subset.refl _ lemma Ioi_mem_at_top [preorder α] [no_top_order α] (x : α) : Ioi x ∈ (at_top : filter α) := let ⟨z, hz⟩ := no_top x in mem_sets_of_superset (mem_at_top z) $ λ y h, lt_of_lt_of_le hz h lemma mem_at_bot [preorder α] (a : α) : {b : α | b ≤ a} ∈ @at_bot α _ := mem_infi_sets a $ subset.refl _ lemma Iio_mem_at_bot [preorder α] [no_bot_order α] (x : α) : Iio x ∈ (at_bot : filter α) := let ⟨z, hz⟩ := no_bot x in mem_sets_of_superset (mem_at_bot z) $ λ y h, lt_of_le_of_lt h hz @[simp] lemma at_top_ne_bot [nonempty α] [semilattice_sup α] : (at_top : filter α) ≠ ⊥ := infi_ne_bot_of_directed (by apply_instance) (assume a b, ⟨a ⊔ b, by simp only [ge, le_principal_iff, forall_const, set_of_subset_set_of, mem_principal_sets, and_self, sup_le_iff, forall_true_iff] {contextual := tt}⟩) (assume a, principal_ne_bot_iff.2 nonempty_Ici) @[simp, nolint ge_or_gt] lemma mem_at_top_sets [nonempty α] [semilattice_sup α] {s : set α} : s ∈ (at_top : filter α) ↔ ∃a:α, ∀b≥a, b ∈ s := let ⟨a⟩ := ‹nonempty α› in iff.intro (assume h, infi_sets_induct h ⟨a, by simp only [forall_const, mem_univ, forall_true_iff]⟩ (assume a s₁ s₂ ha ⟨b, hb⟩, ⟨a ⊔ b, assume c hc, ⟨ha $ le_trans le_sup_left hc, hb _ $ le_trans le_sup_right hc⟩⟩) (assume s₁ s₂ h ⟨a, ha⟩, ⟨a, assume b hb, h $ ha _ hb⟩)) (assume ⟨a, h⟩, mem_infi_sets a $ assume x, h x) @[nolint ge_or_gt] lemma eventually_at_top {α} [semilattice_sup α] [nonempty α] {p : α → Prop} : (∀ᶠ x in at_top, p x) ↔ (∃ a, ∀ b ≥ a, p b) := by simp only [filter.eventually, filter.mem_at_top_sets, mem_set_of_eq] @[nolint ge_or_gt] lemma eventually.exists_forall_of_at_top {α} [semilattice_sup α] [nonempty α] {p : α → Prop} (h : ∀ᶠ x in at_top, p x) : ∃ a, ∀ b ≥ a, p b := eventually_at_top.mp h @[nolint ge_or_gt] lemma frequently_at_top {α} [semilattice_sup α] [nonempty α] {p : α → Prop} : (∃ᶠ x in at_top, p x) ↔ (∀ a, ∃ b ≥ a, p b) := by simp only [filter.frequently, eventually_at_top, not_exists, not_forall, not_not] @[nolint ge_or_gt] lemma frequently_at_top' {α} [semilattice_sup α] [nonempty α] [no_top_order α] {p : α → Prop} : (∃ᶠ x in at_top, p x) ↔ (∀ a, ∃ b > a, p b) := begin rw frequently_at_top, split ; intros h a, { cases no_top a with a' ha', rcases h a' with ⟨b, hb, hb'⟩, exact ⟨b, lt_of_lt_of_le ha' hb, hb'⟩ }, { rcases h a with ⟨b, hb, hb'⟩, exact ⟨b, le_of_lt hb, hb'⟩ }, end @[nolint ge_or_gt] lemma frequently.forall_exists_of_at_top {α} [semilattice_sup α] [nonempty α] {p : α → Prop} (h : ∃ᶠ x in at_top, p x) : ∀ a, ∃ b ≥ a, p b := frequently_at_top.mp h lemma map_at_top_eq [nonempty α] [semilattice_sup α] {f : α → β} : at_top.map f = (⨅a, principal $ f '' {a' | a ≤ a'}) := calc map f (⨅a, principal {a' | a ≤ a'}) = (⨅a, map f $ principal {a' | a ≤ a'}) : map_infi_eq (assume a b, ⟨a ⊔ b, by simp only [ge, le_principal_iff, forall_const, set_of_subset_set_of, mem_principal_sets, and_self, sup_le_iff, forall_true_iff] {contextual := tt}⟩) (by apply_instance) ... = (⨅a, principal $ f '' {a' | a ≤ a'}) : by simp only [map_principal, eq_self_iff_true] lemma tendsto_at_top [preorder β] (m : α → β) (f : filter α) : tendsto m f at_top ↔ (∀b, {a | b ≤ m a} ∈ f) := by simp only [at_top, tendsto_infi, tendsto_principal]; refl lemma tendsto_at_top_mono' [preorder β] (l : filter α) ⦃f₁ f₂ : α → β⦄ (h : {x | f₁ x ≤ f₂ x} ∈ l) : tendsto f₁ l at_top → tendsto f₂ l at_top := assume h₁, (tendsto_at_top _ _).2 $ λ b, mp_sets ((tendsto_at_top _ _).1 h₁ b) (monotone_mem_sets (λ a ha ha₁, le_trans ha₁ ha) h) lemma tendsto_at_top_mono [preorder β] (l : filter α) : monotone (λ f : α → β, tendsto f l at_top) := λ f₁ f₂ h, tendsto_at_top_mono' l $ univ_mem_sets' h /-! ### Sequences -/ @[nolint ge_or_gt] -- see Note [nolint_ge] lemma map_at_top_inf_ne_bot_iff [semilattice_sup α] [nonempty α] {F : filter β} {u : α → β} : (map u at_top) ⊓ F ≠ ⊥ ↔ ∀ U ∈ F, ∀ N, ∃ n ≥ N, u n ∈ U := by simp_rw [inf_ne_bot_iff_frequently_right, frequently_map, frequently_at_top] ; trivial lemma extraction_of_frequently_at_top' {P : ℕ → Prop} (h : ∀ N, ∃ n > N, P n) : ∃ φ : ℕ → ℕ, strict_mono φ ∧ ∀ n, P (φ n) := begin choose u hu using h, cases forall_and_distrib.mp hu with hu hu', exact ⟨u ∘ (nat.rec 0 (λ n v, u v)), strict_mono.nat (λ n, hu _), λ n, hu' _⟩, end lemma extraction_of_frequently_at_top {P : ℕ → Prop} (h : ∃ᶠ n in at_top, P n) : ∃ φ : ℕ → ℕ, strict_mono φ ∧ ∀ n, P (φ n) := begin rw frequently_at_top' at h, exact extraction_of_frequently_at_top' h, end lemma extraction_of_eventually_at_top {P : ℕ → Prop} (h : ∀ᶠ n in at_top, P n) : ∃ φ : ℕ → ℕ, strict_mono φ ∧ ∀ n, P (φ n) := extraction_of_frequently_at_top (eventually.frequently at_top_ne_bot h) @[nolint ge_or_gt] -- see Note [nolint_ge] lemma exists_le_of_tendsto_at_top [semilattice_sup α] [preorder β] {u : α → β} (h : tendsto u at_top at_top) : ∀ a b, ∃ a' ≥ a, b ≤ u a' := begin intros a b, have : ∀ᶠ x in at_top, a ≤ x ∧ b ≤ u x := inter_mem_sets (mem_at_top a) (h $ mem_at_top b), haveI : nonempty α := ⟨a⟩, rcases this.exists at_top_ne_bot with ⟨a', ha, hb⟩, exact ⟨a', ha, hb⟩ end @[nolint ge_or_gt] -- see Note [nolint_ge] lemma exists_lt_of_tendsto_at_top [semilattice_sup α] [preorder β] [no_top_order β] {u : α → β} (h : tendsto u at_top at_top) : ∀ a b, ∃ a' ≥ a, b < u a' := begin intros a b, cases no_top b with b' hb', rcases exists_le_of_tendsto_at_top h a b' with ⟨a', ha', ha''⟩, exact ⟨a', ha', lt_of_lt_of_le hb' ha''⟩ end /-- If `u` is a sequence which is unbounded above, then after any point, it reaches a value strictly greater than all previous values. -/ @[nolint ge_or_gt] -- see Note [nolint_ge] lemma high_scores [linear_order β] [no_top_order β] {u : ℕ → β} (hu : tendsto u at_top at_top) : ∀ N, ∃ n ≥ N, ∀ k < n, u k < u n := begin letI := classical.DLO β, intros N, let A := finset.image u (finset.range $ N+1), -- A = {u 0, ..., u N} have Ane : A.nonempty, from ⟨u 0, finset.mem_image_of_mem _ (finset.mem_range.mpr $ nat.zero_lt_succ _)⟩, let M := finset.max' A Ane, have ex : ∃ n ≥ N, M < u n, from exists_lt_of_tendsto_at_top hu _ _, obtain ⟨n, hnN, hnM, hn_min⟩ : ∃ n, N ≤ n ∧ M < u n ∧ ∀ k, N ≤ k → k < n → u k ≤ M, { use nat.find ex, rw ← and_assoc, split, { simpa using nat.find_spec ex }, { intros k hk hk', simpa [hk] using nat.find_min ex hk' } }, use [n, hnN], intros k hk, by_cases H : k ≤ N, { have : u k ∈ A, from finset.mem_image_of_mem _ (finset.mem_range.mpr $ nat.lt_succ_of_le H), have : u k ≤ M, from finset.le_max' A Ane (u k) this, exact lt_of_le_of_lt this hnM }, { push_neg at H, calc u k ≤ M : hn_min k (le_of_lt H) hk ... < u n : hnM }, end /-- If `u` is a sequence which is unbounded above, then it `frequently` reaches a value strictly greater than all previous values. -/ lemma frequently_high_scores [linear_order β] [no_top_order β] {u : ℕ → β} (hu : tendsto u at_top at_top) : ∃ᶠ n in at_top, ∀ k < n, u k < u n := by simpa [frequently_at_top] using high_scores hu lemma strict_mono_subseq_of_tendsto_at_top {β : Type*} [linear_order β] [no_top_order β] {u : ℕ → β} (hu : tendsto u at_top at_top) : ∃ φ : ℕ → ℕ, strict_mono φ ∧ strict_mono (u ∘ φ) := let ⟨φ, h, h'⟩ := extraction_of_frequently_at_top (frequently_high_scores hu) in ⟨φ, h, λ n m hnm, h' m _ (h hnm)⟩ lemma strict_mono_subseq_of_id_le {u : ℕ → ℕ} (hu : ∀ n, n ≤ u n) : ∃ φ : ℕ → ℕ, strict_mono φ ∧ strict_mono (u ∘ φ) := strict_mono_subseq_of_tendsto_at_top (tendsto_at_top_mono _ hu tendsto_id) lemma strict_mono_tendsto_at_top {φ : ℕ → ℕ} (h : strict_mono φ) : tendsto φ at_top at_top := tendsto_at_top_mono _ h.id_le tendsto_id section ordered_add_monoid variables [ordered_cancel_add_comm_monoid β] (l : filter α) {f g : α → β} lemma tendsto_at_top_add_nonneg_left' (hf : {x | 0 ≤ f x} ∈ l) (hg : tendsto g l at_top) : tendsto (λ x, f x + g x) l at_top := tendsto_at_top_mono' l (monotone_mem_sets (λ x, le_add_of_nonneg_left) hf) hg lemma tendsto_at_top_add_nonneg_left (hf : ∀ x, 0 ≤ f x) (hg : tendsto g l at_top) : tendsto (λ x, f x + g x) l at_top := tendsto_at_top_add_nonneg_left' l (univ_mem_sets' hf) hg lemma tendsto_at_top_add_nonneg_right' (hf : tendsto f l at_top) (hg : {x | 0 ≤ g x} ∈ l) : tendsto (λ x, f x + g x) l at_top := tendsto_at_top_mono' l (monotone_mem_sets (λ x, le_add_of_nonneg_right) hg) hf lemma tendsto_at_top_add_nonneg_right (hf : tendsto f l at_top) (hg : ∀ x, 0 ≤ g x) : tendsto (λ x, f x + g x) l at_top := tendsto_at_top_add_nonneg_right' l hf (univ_mem_sets' hg) lemma tendsto_at_top_of_add_const_left (C : β) (hf : tendsto (λ x, C + f x) l at_top) : tendsto f l at_top := (tendsto_at_top _ l).2 $ assume b, monotone_mem_sets (λ x, le_of_add_le_add_left) ((tendsto_at_top _ _).1 hf (C + b)) lemma tendsto_at_top_of_add_const_right (C : β) (hf : tendsto (λ x, f x + C) l at_top) : tendsto f l at_top := (tendsto_at_top _ l).2 $ assume b, monotone_mem_sets (λ x, le_of_add_le_add_right) ((tendsto_at_top _ _).1 hf (b + C)) lemma tendsto_at_top_of_add_bdd_above_left' (C) (hC : {x | f x ≤ C} ∈ l) (h : tendsto (λ x, f x + g x) l at_top) : tendsto g l at_top := tendsto_at_top_of_add_const_left l C (tendsto_at_top_mono' l (monotone_mem_sets (λ x (hx : f x ≤ C), add_le_add_right hx (g x)) hC) h) lemma tendsto_at_top_of_add_bdd_above_left (C) (hC : ∀ x, f x ≤ C) : tendsto (λ x, f x + g x) l at_top → tendsto g l at_top := tendsto_at_top_of_add_bdd_above_left' l C (univ_mem_sets' hC) lemma tendsto_at_top_of_add_bdd_above_right' (C) (hC : {x | g x ≤ C} ∈ l) (h : tendsto (λ x, f x + g x) l at_top) : tendsto f l at_top := tendsto_at_top_of_add_const_right l C (tendsto_at_top_mono' l (monotone_mem_sets (λ x (hx : g x ≤ C), add_le_add_left hx (f x)) hC) h) lemma tendsto_at_top_of_add_bdd_above_right (C) (hC : ∀ x, g x ≤ C) : tendsto (λ x, f x + g x) l at_top → tendsto f l at_top := tendsto_at_top_of_add_bdd_above_right' l C (univ_mem_sets' hC) end ordered_add_monoid section ordered_group variables [ordered_add_comm_group β] (l : filter α) {f g : α → β} lemma tendsto_at_top_add_left_of_le' (C : β) (hf : {x | C ≤ f x} ∈ l) (hg : tendsto g l at_top) : tendsto (λ x, f x + g x) l at_top := @tendsto_at_top_of_add_bdd_above_left' _ _ _ l (λ x, -(f x)) (λ x, f x + g x) (-C) (by simp [hf]) (by simp [hg]) lemma tendsto_at_top_add_left_of_le (C : β) (hf : ∀ x, C ≤ f x) (hg : tendsto g l at_top) : tendsto (λ x, f x + g x) l at_top := tendsto_at_top_add_left_of_le' l C (univ_mem_sets' hf) hg lemma tendsto_at_top_add_right_of_le' (C : β) (hf : tendsto f l at_top) (hg : {x | C ≤ g x} ∈ l) : tendsto (λ x, f x + g x) l at_top := @tendsto_at_top_of_add_bdd_above_right' _ _ _ l (λ x, f x + g x) (λ x, -(g x)) (-C) (by simp [hg]) (by simp [hf]) lemma tendsto_at_top_add_right_of_le (C : β) (hf : tendsto f l at_top) (hg : ∀ x, C ≤ g x) : tendsto (λ x, f x + g x) l at_top := tendsto_at_top_add_right_of_le' l C hf (univ_mem_sets' hg) lemma tendsto_at_top_add_const_left (C : β) (hf : tendsto f l at_top) : tendsto (λ x, C + f x) l at_top := tendsto_at_top_add_left_of_le' l C (univ_mem_sets' $ λ _, le_refl C) hf lemma tendsto_at_top_add_const_right (C : β) (hf : tendsto f l at_top) : tendsto (λ x, f x + C) l at_top := tendsto_at_top_add_right_of_le' l C hf (univ_mem_sets' $ λ _, le_refl C) end ordered_group open_locale filter @[nolint ge_or_gt] lemma tendsto_at_top' [nonempty α] [semilattice_sup α] (f : α → β) (l : filter β) : tendsto f at_top l ↔ (∀s ∈ l, ∃a, ∀b≥a, f b ∈ s) := by simp only [tendsto_def, mem_at_top_sets]; refl @[nolint ge_or_gt] theorem tendsto_at_top_principal [nonempty β] [semilattice_sup β] {f : β → α} {s : set α} : tendsto f at_top (principal s) ↔ ∃N, ∀n≥N, f n ∈ s := by rw [tendsto_iff_comap, comap_principal, le_principal_iff, mem_at_top_sets]; refl /-- A function `f` grows to infinity independent of an order-preserving embedding `e`. -/ lemma tendsto_at_top_embedding {α β γ : Type*} [preorder β] [preorder γ] {f : α → β} {e : β → γ} {l : filter α} (hm : ∀b₁ b₂, e b₁ ≤ e b₂ ↔ b₁ ≤ b₂) (hu : ∀c, ∃b, c ≤ e b) : tendsto (e ∘ f) l at_top ↔ tendsto f l at_top := begin rw [tendsto_at_top, tendsto_at_top], split, { assume hc b, filter_upwards [hc (e b)] assume a, (hm b (f a)).1 }, { assume hb c, rcases hu c with ⟨b, hc⟩, filter_upwards [hb b] assume a ha, le_trans hc ((hm b (f a)).2 ha) } end lemma tendsto_at_top_at_top [nonempty α] [semilattice_sup α] [preorder β] (f : α → β) : tendsto f at_top at_top ↔ ∀ b : β, ∃ i : α, ∀ a : α, i ≤ a → b ≤ f a := iff.trans tendsto_infi $ forall_congr $ assume b, tendsto_at_top_principal @[nolint ge_or_gt] lemma tendsto_at_top_at_bot [nonempty α] [decidable_linear_order α] [preorder β] (f : α → β) : tendsto f at_top at_bot ↔ ∀ (b : β), ∃ (i : α), ∀ (a : α), i ≤ a → b ≥ f a := @tendsto_at_top_at_top α (order_dual β) _ _ _ f lemma tendsto_at_top_at_top_of_monotone [nonempty α] [semilattice_sup α] [preorder β] {f : α → β} (hf : monotone f) : tendsto f at_top at_top ↔ ∀ b : β, ∃ a : α, b ≤ f a := (tendsto_at_top_at_top f).trans $ forall_congr $ λ b, exists_congr $ λ a, ⟨λ h, h a (le_refl a), λ h a' ha', le_trans h $ hf ha'⟩ alias tendsto_at_top_at_top_of_monotone ← monotone.tendsto_at_top_at_top lemma tendsto_finset_range : tendsto finset.range at_top at_top := finset.range_mono.tendsto_at_top_at_top.2 finset.exists_nat_subset_range lemma monotone.tendsto_at_top_finset [nonempty β] [semilattice_sup β] {f : β → finset α} (h : monotone f) (h' : ∀ x : α, ∃ n, x ∈ f n) : tendsto f at_top at_top := begin classical, apply (tendsto_at_top_at_top_of_monotone h).2, choose N hN using h', assume b, have : bdd_above ↑(b.image N) := finset.bdd_above _, rcases this with ⟨n, hn⟩, refine ⟨n, _⟩, assume i ib, have : N i ∈ ↑(finset.image N b), by { rw finset.mem_coe, exact finset.mem_image_of_mem _ ib }, exact (h (hn this)) (hN i) end lemma tendsto_finset_image_at_top_at_top {i : β → γ} {j : γ → β} (h : ∀x, j (i x) = x) : tendsto (finset.image j) at_top at_top := have j ∘ i = id, from funext h, (finset.image_mono j).tendsto_at_top_at_top.2 $ assume s, ⟨s.image i, by simp only [finset.image_image, this, finset.image_id, le_refl]⟩ lemma prod_at_top_at_top_eq {β₁ β₂ : Type*} [nonempty β₁] [nonempty β₂] [semilattice_sup β₁] [semilattice_sup β₂] : (@at_top β₁ _) ×ᶠ (@at_top β₂ _) = @at_top (β₁ × β₂) _ := by inhabit β₁; inhabit β₂; simp [at_top, prod_infi_left (default β₁), prod_infi_right (default β₂), infi_prod]; exact infi_comm lemma prod_map_at_top_eq {α₁ α₂ β₁ β₂ : Type*} [nonempty β₁] [nonempty β₂] [semilattice_sup β₁] [semilattice_sup β₂] (u₁ : β₁ → α₁) (u₂ : β₂ → α₂) : (map u₁ at_top) ×ᶠ (map u₂ at_top) = map (prod.map u₁ u₂) at_top := by rw [prod_map_map_eq, prod_at_top_at_top_eq, prod.map_def] /-- A function `f` maps upwards closed sets (at_top sets) to upwards closed sets when it is a Galois insertion. The Galois "insertion" and "connection" is weakened to only require it to be an insertion and a connetion above `b'`. -/ lemma map_at_top_eq_of_gc [semilattice_sup α] [semilattice_sup β] {f : α → β} (g : β → α) (b' : β) (hf : monotone f) (gc : ∀a, ∀b≥b', f a ≤ b ↔ a ≤ g b) (hgi : ∀b≥b', b ≤ f (g b)) : map f at_top = at_top := begin rw [@map_at_top_eq α _ ⟨g b'⟩], refine le_antisymm (le_infi $ assume b, infi_le_of_le (g (b ⊔ b')) $ principal_mono.2 $ image_subset_iff.2 _) (le_infi $ assume a, infi_le_of_le (f a ⊔ b') $ principal_mono.2 _), { assume a ha, exact (le_trans le_sup_left $ le_trans (hgi _ le_sup_right) $ hf ha) }, { assume b hb, have hb' : b' ≤ b := le_trans le_sup_right hb, exact ⟨g b, (gc _ _ hb').1 (le_trans le_sup_left hb), le_antisymm ((gc _ _ hb').2 (le_refl _)) (hgi _ hb')⟩ } end lemma map_add_at_top_eq_nat (k : ℕ) : map (λa, a + k) at_top = at_top := map_at_top_eq_of_gc (λa, a - k) k (assume a b h, add_le_add_right h k) (assume a b h, (nat.le_sub_right_iff_add_le h).symm) (assume a h, by rw [nat.sub_add_cancel h]) lemma map_sub_at_top_eq_nat (k : ℕ) : map (λa, a - k) at_top = at_top := map_at_top_eq_of_gc (λa, a + k) 0 (assume a b h, nat.sub_le_sub_right h _) (assume a b _, nat.sub_le_right_iff_le_add) (assume b _, by rw [nat.add_sub_cancel]) lemma tendsto_add_at_top_nat (k : ℕ) : tendsto (λa, a + k) at_top at_top := le_of_eq (map_add_at_top_eq_nat k) lemma tendsto_sub_at_top_nat (k : ℕ) : tendsto (λa, a - k) at_top at_top := le_of_eq (map_sub_at_top_eq_nat k) lemma tendsto_add_at_top_iff_nat {f : ℕ → α} {l : filter α} (k : ℕ) : tendsto (λn, f (n + k)) at_top l ↔ tendsto f at_top l := show tendsto (f ∘ (λn, n + k)) at_top l ↔ tendsto f at_top l, by rw [← tendsto_map'_iff, map_add_at_top_eq_nat] lemma map_div_at_top_eq_nat (k : ℕ) (hk : k > 0) : map (λa, a / k) at_top = at_top := map_at_top_eq_of_gc (λb, b * k + (k - 1)) 1 (assume a b h, nat.div_le_div_right h) (assume a b _, calc a / k ≤ b ↔ a / k < b + 1 : by rw [← nat.succ_eq_add_one, nat.lt_succ_iff] ... ↔ a < (b + 1) * k : nat.div_lt_iff_lt_mul _ _ hk ... ↔ _ : begin cases k, exact (lt_irrefl _ hk).elim, simp [mul_add, add_mul, nat.succ_add, nat.lt_succ_iff] end) (assume b _, calc b = (b * k) / k : by rw [nat.mul_div_cancel b hk] ... ≤ (b * k + (k - 1)) / k : nat.div_le_div_right $ nat.le_add_right _ _) end filter
96d92dddd8d0bb420f7a1527e56deaca5bfd721b
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/number_theory/quadratic_reciprocity.lean
5fa4f7b57836a69c9474ea0cf6858ea7b9a2fef5
[ "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
24,966
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import field_theory.finite.basic import data.zmod.basic import data.nat.parity /-! # Quadratic reciprocity. This file contains results about quadratic residues modulo a prime number. The main results are the law of quadratic reciprocity, `quadratic_reciprocity`, as well as the interpretations in terms of existence of square roots depending on the congruence mod 4, `exists_sq_eq_prime_iff_of_mod_four_eq_one`, and `exists_sq_eq_prime_iff_of_mod_four_eq_three`. Also proven are conditions for `-1` and `2` to be a square modulo a prime, `exists_sq_eq_neg_one_iff_mod_four_ne_three` and `exists_sq_eq_two_iff` ## Implementation notes The proof of quadratic reciprocity implemented uses Gauss' lemma and Eisenstein's lemma -/ open function finset nat finite_field zmod open_locale big_operators nat namespace zmod variables (p q : ℕ) [fact p.prime] [fact q.prime] /-- Euler's Criterion: A unit `x` of `zmod p` is a square if and only if `x ^ (p / 2) = 1`. -/ lemma euler_criterion_units (x : units (zmod p)) : (∃ y : units (zmod p), y ^ 2 = x) ↔ x ^ (p / 2) = 1 := begin cases nat.prime.eq_two_or_odd (fact.out p.prime) with hp2 hp_odd, { substI p, refine iff_of_true ⟨1, _⟩ _; apply subsingleton.elim }, obtain ⟨g, hg⟩ := is_cyclic.exists_generator (units (zmod p)), obtain ⟨n, hn⟩ : x ∈ submonoid.powers g, { rw mem_powers_iff_mem_zpowers, apply hg }, split, { rintro ⟨y, rfl⟩, rw [← pow_mul, two_mul_odd_div_two hp_odd, units_pow_card_sub_one_eq_one], }, { subst x, assume h, have key : 2 * (p / 2) ∣ n * (p / 2), { rw [← pow_mul] at h, rw [two_mul_odd_div_two hp_odd, ← card_units, ← order_of_eq_card_of_forall_mem_zpowers hg], apply order_of_dvd_of_pow_eq_one h }, have : 0 < p / 2 := nat.div_pos (fact.out (1 < p)) dec_trivial, obtain ⟨m, rfl⟩ := dvd_of_mul_dvd_mul_right this key, refine ⟨g ^ m, _⟩, rw [mul_comm, pow_mul], }, end /-- Euler's Criterion: a nonzero `a : zmod p` is a square if and only if `x ^ (p / 2) = 1`. -/ lemma euler_criterion {a : zmod p} (ha : a ≠ 0) : (∃ y : zmod p, y ^ 2 = a) ↔ a ^ (p / 2) = 1 := begin apply (iff_congr _ (by simp [units.ext_iff])).mp (euler_criterion_units p (units.mk0 a ha)), simp only [units.ext_iff, sq, units.coe_mk0, units.coe_mul], split, { rintro ⟨y, hy⟩, exact ⟨y, hy⟩ }, { rintro ⟨y, rfl⟩, have hy : y ≠ 0, { rintro rfl, simpa [zero_pow] using ha, }, refine ⟨units.mk0 y hy, _⟩, simp, } end lemma exists_sq_eq_neg_one_iff_mod_four_ne_three : (∃ y : zmod p, y ^ 2 = -1) ↔ p % 4 ≠ 3 := begin cases nat.prime.eq_two_or_odd (fact.out p.prime) with hp2 hp_odd, { substI p, exact dec_trivial }, haveI := fact.mk hp_odd, have neg_one_ne_zero : (-1 : zmod p) ≠ 0, from mt neg_eq_zero.1 one_ne_zero, rw [euler_criterion p neg_one_ne_zero, neg_one_pow_eq_pow_mod_two], cases mod_two_eq_zero_or_one (p / 2) with p_half_even p_half_odd, { rw [p_half_even, pow_zero, eq_self_iff_true, true_iff], contrapose! p_half_even with hp, rw [← nat.mod_mul_right_div_self, show 2 * 2 = 4, from rfl, hp], exact dec_trivial }, { rw [p_half_odd, pow_one, iff_false_intro (ne_neg_self p one_ne_zero).symm, false_iff, not_not], rw [← nat.mod_mul_right_div_self, show 2 * 2 = 4, from rfl] at p_half_odd, rw [← nat.mod_mul_left_mod _ 2, show 2 * 2 = 4, from rfl] at hp_odd, have hp : p % 4 < 4, from nat.mod_lt _ dec_trivial, revert hp hp_odd p_half_odd, generalize : p % 4 = k, dec_trivial! } end lemma pow_div_two_eq_neg_one_or_one {a : zmod p} (ha : a ≠ 0) : a ^ (p / 2) = 1 ∨ a ^ (p / 2) = -1 := begin cases nat.prime.eq_two_or_odd (fact.out p.prime) with hp2 hp_odd, { substI p, revert a ha, exact dec_trivial }, rw [← mul_self_eq_one_iff, ← pow_add, ← two_mul, two_mul_odd_div_two hp_odd], exact pow_card_sub_one_eq_one ha end /-- **Wilson's Lemma**: the product of `1`, ..., `p-1` is `-1` modulo `p`. -/ @[simp] lemma wilsons_lemma : ((p - 1)! : zmod p) = -1 := begin refine calc ((p - 1)! : zmod p) = (∏ x in Ico 1 (succ (p - 1)), x) : by rw [← finset.prod_Ico_id_eq_factorial, prod_nat_cast] ... = (∏ x : units (zmod p), x) : _ ... = -1 : by simp_rw [← units.coe_hom_apply, ← (units.coe_hom (zmod p)).map_prod, prod_univ_units_id_eq_neg_one, units.coe_hom_apply, units.coe_neg, units.coe_one], have hp : 0 < p := (fact.out p.prime).pos, symmetry, refine prod_bij (λ a _, (a : zmod p).val) _ _ _ _, { intros a ha, rw [mem_Ico, ← nat.succ_sub hp, nat.succ_sub_one], split, { apply nat.pos_of_ne_zero, rw ← @val_zero p, assume h, apply units.ne_zero a (val_injective p h) }, { exact val_lt _ } }, { intros a ha, simp only [cast_id, nat_cast_val], }, { intros _ _ _ _ h, rw units.ext_iff, exact val_injective p h }, { intros b hb, rw [mem_Ico, nat.succ_le_iff, ← succ_sub hp, succ_sub_one, pos_iff_ne_zero] at hb, refine ⟨units.mk0 b _, finset.mem_univ _, _⟩, { assume h, apply hb.1, apply_fun val at h, simpa only [val_cast_of_lt hb.right, val_zero] using h }, { simp only [val_cast_of_lt hb.right, units.coe_mk0], } } end @[simp] lemma prod_Ico_one_prime : (∏ x in Ico 1 p, (x : zmod p)) = -1 := begin conv in (Ico 1 p) { rw [← succ_sub_one p, succ_sub (fact.out p.prime).pos] }, rw [← prod_nat_cast, finset.prod_Ico_id_eq_factorial, wilsons_lemma] end end zmod /-- The image of the map sending a non zero natural number `x ≤ p / 2` to the absolute value of the element of interger in the interval `(-p/2, p/2]` congruent to `a * x` mod p is the set of non zero natural numbers `x` such that `x ≤ p / 2` -/ lemma Ico_map_val_min_abs_nat_abs_eq_Ico_map_id (p : ℕ) [hp : fact p.prime] (a : zmod p) (hap : a ≠ 0) : (Ico 1 (p / 2).succ).1.map (λ x, (a * x).val_min_abs.nat_abs) = (Ico 1 (p / 2).succ).1.map (λ a, a) := begin have he : ∀ {x}, x ∈ Ico 1 (p / 2).succ → x ≠ 0 ∧ x ≤ p / 2, by simp [nat.lt_succ_iff, nat.succ_le_iff, pos_iff_ne_zero] {contextual := tt}, have hep : ∀ {x}, x ∈ Ico 1 (p / 2).succ → x < p, from λ x hx, lt_of_le_of_lt (he hx).2 (nat.div_lt_self hp.1.pos dec_trivial), have hpe : ∀ {x}, x ∈ Ico 1 (p / 2).succ → ¬ p ∣ x, from λ x hx hpx, not_lt_of_ge (le_of_dvd (nat.pos_of_ne_zero (he hx).1) hpx) (hep hx), have hmem : ∀ (x : ℕ) (hx : x ∈ Ico 1 (p / 2).succ), (a * x : zmod p).val_min_abs.nat_abs ∈ Ico 1 (p / 2).succ, { assume x hx, simp [hap, char_p.cast_eq_zero_iff (zmod p) p, hpe hx, lt_succ_iff, succ_le_iff, pos_iff_ne_zero, nat_abs_val_min_abs_le _], }, have hsurj : ∀ (b : ℕ) (hb : b ∈ Ico 1 (p / 2).succ), ∃ x ∈ Ico 1 (p / 2).succ, b = (a * x : zmod p).val_min_abs.nat_abs, { assume b hb, refine ⟨(b / a : zmod p).val_min_abs.nat_abs, mem_Ico.mpr ⟨_, _⟩, _⟩, { apply nat.pos_of_ne_zero, simp only [div_eq_mul_inv, hap, char_p.cast_eq_zero_iff (zmod p) p, hpe hb, not_false_iff, val_min_abs_eq_zero, inv_eq_zero, int.nat_abs_eq_zero, ne.def, mul_eq_zero, or_self] }, { apply lt_succ_of_le, apply nat_abs_val_min_abs_le }, { rw nat_cast_nat_abs_val_min_abs, split_ifs, { erw [mul_div_cancel' _ hap, val_min_abs_def_pos, val_cast_of_lt (hep hb), if_pos (le_of_lt_succ (mem_Ico.1 hb).2), int.nat_abs_of_nat], }, { erw [mul_neg_eq_neg_mul_symm, mul_div_cancel' _ hap, nat_abs_val_min_abs_neg, val_min_abs_def_pos, val_cast_of_lt (hep hb), if_pos (le_of_lt_succ (mem_Ico.1 hb).2), int.nat_abs_of_nat] } } }, exact multiset.map_eq_map_of_bij_of_nodup _ _ (finset.nodup _) (finset.nodup _) (λ x _, (a * x : zmod p).val_min_abs.nat_abs) hmem (λ _ _, rfl) (inj_on_of_surj_on_of_card_le _ hmem hsurj (le_refl _)) hsurj end private lemma gauss_lemma_aux₁ (p : ℕ) [fact p.prime] [fact (p % 2 = 1)] {a : ℕ} (hap : (a : zmod p) ≠ 0) : (a^(p / 2) * (p / 2)! : zmod p) = (-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, ¬(a * x : zmod p).val ≤ p / 2)).card * (p / 2)! := calc (a ^ (p / 2) * (p / 2)! : zmod p) = (∏ x in Ico 1 (p / 2).succ, a * x) : by rw [prod_mul_distrib, ← prod_nat_cast, ← prod_nat_cast, prod_Ico_id_eq_factorial, prod_const, card_Ico, succ_sub_one]; simp ... = (∏ x in Ico 1 (p / 2).succ, (a * x : zmod p).val) : by simp ... = (∏ x in Ico 1 (p / 2).succ, (if (a * x : zmod p).val ≤ p / 2 then 1 else -1) * (a * x : zmod p).val_min_abs.nat_abs) : prod_congr rfl $ λ _ _, begin simp only [nat_cast_nat_abs_val_min_abs], split_ifs; simp end ... = (-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, ¬(a * x : zmod p).val ≤ p / 2)).card * (∏ x in Ico 1 (p / 2).succ, (a * x : zmod p).val_min_abs.nat_abs) : have (∏ x in Ico 1 (p / 2).succ, if (a * x : zmod p).val ≤ p / 2 then (1 : zmod p) else -1) = (∏ x in (Ico 1 (p / 2).succ).filter (λ x : ℕ, ¬(a * x : zmod p).val ≤ p / 2), -1), from prod_bij_ne_one (λ x _ _, x) (λ x, by split_ifs; simp * at * {contextual := tt}) (λ _ _ _ _ _ _, id) (λ b h _, ⟨b, by simp [-not_le, *] at *⟩) (by intros; split_ifs at *; simp * at *), by rw [prod_mul_distrib, this]; simp ... = (-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, ¬(a * x : zmod p).val ≤ p / 2)).card * (p / 2)! : by rw [← prod_nat_cast, finset.prod_eq_multiset_prod, Ico_map_val_min_abs_nat_abs_eq_Ico_map_id p a hap, ← finset.prod_eq_multiset_prod, prod_Ico_id_eq_factorial] private lemma gauss_lemma_aux₂ (p : ℕ) [hp : fact p.prime] [fact (p % 2 = 1)] {a : ℕ} (hap : (a : zmod p) ≠ 0) : (a^(p / 2) : zmod p) = (-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmod p).val)).card := (mul_left_inj' (show ((p / 2)! : zmod p) ≠ 0, by rw [ne.def, char_p.cast_eq_zero_iff (zmod p) p, hp.1.dvd_factorial, not_le]; exact nat.div_lt_self hp.1.pos dec_trivial)).1 $ by simpa using gauss_lemma_aux₁ p hap private lemma eisenstein_lemma_aux₁ (p : ℕ) [fact p.prime] [hp2 : fact (p % 2 = 1)] {a : ℕ} (hap : (a : zmod p) ≠ 0) : ((∑ x in Ico 1 (p / 2).succ, a * x : ℕ) : zmod 2) = ((Ico 1 (p / 2).succ).filter ((λ x : ℕ, p / 2 < (a * x : zmod p).val))).card + ∑ x in Ico 1 (p / 2).succ, x + (∑ x in Ico 1 (p / 2).succ, (a * x) / p : ℕ) := have hp2 : (p : zmod 2) = (1 : ℕ), from (eq_iff_modeq_nat _).2 hp2.1, calc ((∑ x in Ico 1 (p / 2).succ, a * x : ℕ) : zmod 2) = ((∑ x in Ico 1 (p / 2).succ, ((a * x) % p + p * ((a * x) / p)) : ℕ) : zmod 2) : by simp only [mod_add_div] ... = (∑ x in Ico 1 (p / 2).succ, ((a * x : ℕ) : zmod p).val : ℕ) + (∑ x in Ico 1 (p / 2).succ, (a * x) / p : ℕ) : by simp only [val_nat_cast]; simp [sum_add_distrib, mul_sum.symm, nat.cast_add, nat.cast_mul, nat.cast_sum, hp2] ... = _ : congr_arg2 (+) (calc ((∑ x in Ico 1 (p / 2).succ, ((a * x : ℕ) : zmod p).val : ℕ) : zmod 2) = ∑ x in Ico 1 (p / 2).succ, ((((a * x : zmod p).val_min_abs + (if (a * x : zmod p).val ≤ p / 2 then 0 else p)) : ℤ) : zmod 2) : by simp only [(val_eq_ite_val_min_abs _).symm]; simp [nat.cast_sum] ... = ((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmod p).val)).card + ((∑ x in Ico 1 (p / 2).succ, (a * x : zmod p).val_min_abs.nat_abs) : ℕ) : by { simp [ite_cast, add_comm, sum_add_distrib, finset.sum_ite, hp2, nat.cast_sum], } ... = _ : by rw [finset.sum_eq_multiset_sum, Ico_map_val_min_abs_nat_abs_eq_Ico_map_id p a hap, ← finset.sum_eq_multiset_sum]; simp [nat.cast_sum]) rfl private lemma eisenstein_lemma_aux₂ (p : ℕ) [fact p.prime] [fact (p % 2 = 1)] {a : ℕ} (ha2 : a % 2 = 1) (hap : (a : zmod p) ≠ 0) : ((Ico 1 (p / 2).succ).filter ((λ x : ℕ, p / 2 < (a * x : zmod p).val))).card ≡ ∑ x in Ico 1 (p / 2).succ, (x * a) / p [MOD 2] := have ha2 : (a : zmod 2) = (1 : ℕ), from (eq_iff_modeq_nat _).2 ha2, (eq_iff_modeq_nat 2).1 $ sub_eq_zero.1 $ by simpa [add_left_comm, sub_eq_add_neg, finset.mul_sum.symm, mul_comm, ha2, nat.cast_sum, add_neg_eq_iff_eq_add.symm, neg_eq_self_mod_two, add_assoc] using eq.symm (eisenstein_lemma_aux₁ p hap) lemma div_eq_filter_card {a b c : ℕ} (hb0 : 0 < b) (hc : a / b ≤ c) : a / b = ((Ico 1 c.succ).filter (λ x, x * b ≤ a)).card := calc a / b = (Ico 1 (a / b).succ).card : by simp ... = ((Ico 1 c.succ).filter (λ x, x * b ≤ a)).card : congr_arg _ $ finset.ext $ λ x, have x * b ≤ a → x ≤ c, from λ h, le_trans (by rwa [le_div_iff_mul_le _ _ hb0]) hc, by simp [lt_succ_iff, le_div_iff_mul_le _ _ hb0]; tauto /-- The given sum is the number of integer points in the triangle formed by the diagonal of the rectangle `(0, p/2) × (0, q/2)` -/ private lemma sum_Ico_eq_card_lt {p q : ℕ} : ∑ a in Ico 1 (p / 2).succ, (a * q) / p = (((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.2 * p ≤ x.1 * q)).card := if hp0 : p = 0 then by simp [hp0, finset.ext_iff] else calc ∑ a in Ico 1 (p / 2).succ, (a * q) / p = ∑ a in Ico 1 (p / 2).succ, ((Ico 1 (q / 2).succ).filter (λ x, x * p ≤ a * q)).card : finset.sum_congr rfl $ λ x hx, div_eq_filter_card (nat.pos_of_ne_zero hp0) (calc x * q / p ≤ (p / 2) * q / p : nat.div_le_div_right (mul_le_mul_of_nonneg_right (le_of_lt_succ $ by finish) (nat.zero_le _)) ... ≤ _ : nat.div_mul_div_le_div _ _ _) ... = _ : by rw [← card_sigma]; exact card_congr (λ a _, ⟨a.1, a.2⟩) (by simp only [mem_filter, mem_sigma, and_self, forall_true_iff, mem_product] {contextual := tt}) (λ ⟨_, _⟩ ⟨_, _⟩, by simp only [prod.mk.inj_iff, eq_self_iff_true, and_self, heq_iff_eq, forall_true_iff] {contextual := tt}) (λ ⟨b₁, b₂⟩ h, ⟨⟨b₁, b₂⟩, by revert h; simp only [mem_filter, eq_self_iff_true, exists_prop_of_true, mem_sigma, and_self, forall_true_iff, mem_product] {contextual := tt}⟩) /-- Each of the sums in this lemma is the cardinality of the set integer points in each of the two triangles formed by the diagonal of the rectangle `(0, p/2) × (0, q/2)`. Adding them gives the number of points in the rectangle. -/ private lemma sum_mul_div_add_sum_mul_div_eq_mul (p q : ℕ) [hp : fact p.prime] (hq0 : (q : zmod p) ≠ 0) : ∑ a in Ico 1 (p / 2).succ, (a * q) / p + ∑ a in Ico 1 (q / 2).succ, (a * p) / q = (p / 2) * (q / 2) := begin have hswap : (((Ico 1 (q / 2).succ).product (Ico 1 (p / 2).succ)).filter (λ x : ℕ × ℕ, x.2 * q ≤ x.1 * p)).card = (((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.1 * q ≤ x.2 * p)).card := card_congr (λ x _, prod.swap x) (λ ⟨_, _⟩, by simp only [mem_filter, and_self, prod.swap_prod_mk, forall_true_iff, mem_product] {contextual := tt}) (λ ⟨_, _⟩ ⟨_, _⟩, by simp only [prod.mk.inj_iff, eq_self_iff_true, and_self, prod.swap_prod_mk, forall_true_iff] {contextual := tt}) (λ ⟨x₁, x₂⟩ h, ⟨⟨x₂, x₁⟩, by revert h; simp only [mem_filter, eq_self_iff_true, and_self, exists_prop_of_true, prod.swap_prod_mk, forall_true_iff, mem_product] {contextual := tt}⟩), have hdisj : disjoint (((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.2 * p ≤ x.1 * q)) (((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.1 * q ≤ x.2 * p)), { apply disjoint_filter.2 (λ x hx hpq hqp, _), have hxp : x.1 < p, from lt_of_le_of_lt (show x.1 ≤ p / 2, by simp only [*, lt_succ_iff, mem_Ico, mem_product] at *; tauto) (nat.div_lt_self hp.1.pos dec_trivial), have : (x.1 : zmod p) = 0, { simpa [hq0] using congr_arg (coe : ℕ → zmod p) (le_antisymm hpq hqp) }, apply_fun zmod.val at this, rw [val_cast_of_lt hxp, val_zero] at this, simpa only [this, nonpos_iff_eq_zero, mem_Ico, one_ne_zero, false_and, mem_product] using hx }, have hunion : ((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.2 * p ≤ x.1 * q) ∪ ((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.1 * q ≤ x.2 * p) = ((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)), from finset.ext (λ x, by have := le_total (x.2 * p) (x.1 * q); simp only [mem_union, mem_filter, mem_Ico, mem_product]; tauto), rw [sum_Ico_eq_card_lt, sum_Ico_eq_card_lt, hswap, ← card_disjoint_union hdisj, hunion, card_product], simp only [card_Ico, tsub_zero, succ_sub_succ_eq_sub] end variables (p q : ℕ) [fact p.prime] [fact q.prime] namespace zmod /-- The Legendre symbol of `a` and `p` is an integer defined as * `0` if `a` is `0` modulo `p`; * `1` if `a ^ (p / 2)` is `1` modulo `p` (by `euler_criterion` this is equivalent to “`a` is a square modulo `p`”); * `-1` otherwise. -/ def legendre_sym (a p : ℕ) : ℤ := if (a : zmod p) = 0 then 0 else if (a : zmod p) ^ (p / 2) = 1 then 1 else -1 lemma legendre_sym_eq_pow (a p : ℕ) [hp : fact p.prime] : (legendre_sym a p : zmod p) = (a ^ (p / 2)) := begin rw legendre_sym, by_cases ha : (a : zmod p) = 0, { simp only [if_pos, ha, zero_pow (nat.div_pos (hp.1.two_le) (succ_pos 1)), int.cast_zero] }, cases hp.1.eq_two_or_odd with hp2 hp_odd, { substI p, generalize : (a : (zmod 2)) = b, revert b, dec_trivial, }, { haveI := fact.mk hp_odd, rw if_neg ha, have : (-1 : zmod p) ≠ 1, from (ne_neg_self p one_ne_zero).symm, cases pow_div_two_eq_neg_one_or_one p ha with h h, { rw [if_pos h, h, int.cast_one], }, { rw [h, if_neg this, int.cast_neg, int.cast_one], } } end lemma legendre_sym_eq_one_or_neg_one (a p : ℕ) (ha : (a : zmod p) ≠ 0) : legendre_sym a p = -1 ∨ legendre_sym a p = 1 := by unfold legendre_sym; split_ifs; simp only [*, eq_self_iff_true, or_true, true_or] at * lemma legendre_sym_eq_zero_iff (a p : ℕ) : legendre_sym a p = 0 ↔ (a : zmod p) = 0 := begin split, { classical, contrapose, assume ha, cases legendre_sym_eq_one_or_neg_one a p ha with h h, all_goals { rw h, norm_num } }, { assume ha, rw [legendre_sym, if_pos ha] } end /-- Gauss' lemma. The legendre symbol can be computed by considering the number of naturals less than `p/2` such that `(a * x) % p > p / 2` -/ lemma gauss_lemma {a : ℕ} [fact (p % 2 = 1)] (ha0 : (a : zmod p) ≠ 0) : legendre_sym a p = (-1) ^ ((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmod p).val)).card := have (legendre_sym a p : zmod p) = (((-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmod p).val)).card : ℤ) : zmod p), by rw [legendre_sym_eq_pow, gauss_lemma_aux₂ p ha0]; simp, begin cases legendre_sym_eq_one_or_neg_one a p ha0; cases neg_one_pow_eq_or ℤ ((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmod p).val)).card; simp [*, ne_neg_self p one_ne_zero, (ne_neg_self p one_ne_zero).symm] at * end lemma legendre_sym_eq_one_iff {a : ℕ} (ha0 : (a : zmod p) ≠ 0) : legendre_sym a p = 1 ↔ (∃ b : zmod p, b ^ 2 = a) := begin rw [euler_criterion p ha0, legendre_sym, if_neg ha0], split_ifs, { simp only [h, eq_self_iff_true] }, finish -- this is quite slow. I'm actually surprised that it can close the goal at all! end lemma eisenstein_lemma [fact (p % 2 = 1)] {a : ℕ} (ha1 : a % 2 = 1) (ha0 : (a : zmod p) ≠ 0) : legendre_sym a p = (-1)^∑ x in Ico 1 (p / 2).succ, (x * a) / p := by rw [neg_one_pow_eq_pow_mod_two, gauss_lemma p ha0, neg_one_pow_eq_pow_mod_two, show _ = _, from eisenstein_lemma_aux₂ p ha1 ha0] /-- **Quadratic reciprocity theorem** -/ theorem quadratic_reciprocity [hp1 : fact (p % 2 = 1)] [hq1 : fact (q % 2 = 1)] (hpq : p ≠ q) : legendre_sym p q * legendre_sym q p = (-1) ^ ((p / 2) * (q / 2)) := have hpq0 : (p : zmod q) ≠ 0, from prime_ne_zero q p hpq.symm, have hqp0 : (q : zmod p) ≠ 0, from prime_ne_zero p q hpq, by rw [eisenstein_lemma q hp1.1 hpq0, eisenstein_lemma p hq1.1 hqp0, ← pow_add, sum_mul_div_add_sum_mul_div_eq_mul q p hpq0, mul_comm] -- move this local attribute [instance] lemma fact_prime_two : fact (nat.prime 2) := ⟨nat.prime_two⟩ lemma legendre_sym_two [hp1 : fact (p % 2 = 1)] : legendre_sym 2 p = (-1) ^ (p / 4 + p / 2) := have hp2 : p ≠ 2, from mt (congr_arg (% 2)) (by simpa using hp1.1), have hp22 : p / 2 / 2 = _ := div_eq_filter_card (show 0 < 2, from dec_trivial) (nat.div_le_self (p / 2) 2), have hcard : (Ico 1 (p / 2).succ).card = p / 2, by simp, have hx2 : ∀ x ∈ Ico 1 (p / 2).succ, (2 * x : zmod p).val = 2 * x, from λ x hx, have h2xp : 2 * x < p, from calc 2 * x ≤ 2 * (p / 2) : mul_le_mul_of_nonneg_left (le_of_lt_succ $ by finish) dec_trivial ... < _ : by conv_rhs {rw [← div_add_mod p 2, hp1.1]}; exact lt_succ_self _, by rw [← nat.cast_two, ← nat.cast_mul, val_cast_of_lt h2xp], have hdisj : disjoint ((Ico 1 (p / 2).succ).filter (λ x, p / 2 < ((2 : ℕ) * x : zmod p).val)) ((Ico 1 (p / 2).succ).filter (λ x, x * 2 ≤ p / 2)), from disjoint_filter.2 (λ x hx, by simp [hx2 _ hx, mul_comm]), have hunion : ((Ico 1 (p / 2).succ).filter (λ x, p / 2 < ((2 : ℕ) * x : zmod p).val)) ∪ ((Ico 1 (p / 2).succ).filter (λ x, x * 2 ≤ p / 2)) = Ico 1 (p / 2).succ, begin rw [filter_union_right], conv_rhs {rw [← @filter_true _ (Ico 1 (p / 2).succ)]}, exact filter_congr (λ x hx, by simp [hx2 _ hx, lt_or_le, mul_comm]) end, begin rw [gauss_lemma p (prime_ne_zero p 2 hp2), neg_one_pow_eq_pow_mod_two, @neg_one_pow_eq_pow_mod_two _ _ (p / 4 + p / 2)], refine congr_arg2 _ rfl ((eq_iff_modeq_nat 2).1 _), rw [show 4 = 2 * 2, from rfl, ← nat.div_div_eq_div_mul, hp22, nat.cast_add, ← sub_eq_iff_eq_add', sub_eq_add_neg, neg_eq_self_mod_two, ← nat.cast_add, ← card_disjoint_union hdisj, hunion, hcard] end lemma exists_sq_eq_two_iff [hp1 : fact (p % 2 = 1)] : (∃ a : zmod p, a ^ 2 = 2) ↔ p % 8 = 1 ∨ p % 8 = 7 := have hp2 : ((2 : ℕ) : zmod p) ≠ 0, from prime_ne_zero p 2 (λ h, by simpa [h] using hp1.1), have hpm4 : p % 4 = p % 8 % 4, from (nat.mod_mul_left_mod p 2 4).symm, have hpm2 : p % 2 = p % 8 % 2, from (nat.mod_mul_left_mod p 4 2).symm, begin rw [show (2 : zmod p) = (2 : ℕ), by simp, ← legendre_sym_eq_one_iff p hp2, legendre_sym_two p, neg_one_pow_eq_one_iff_even (show (-1 : ℤ) ≠ 1, from dec_trivial), even_add, even_div, even_div], have := nat.mod_lt p (show 0 < 8, from dec_trivial), resetI, rw fact_iff at hp1, revert this hp1, erw [hpm4, hpm2], generalize hm : p % 8 = m, unfreezingI {clear_dependent p}, dec_trivial!, end lemma exists_sq_eq_prime_iff_of_mod_four_eq_one (hp1 : p % 4 = 1) [hq1 : fact (q % 2 = 1)] : (∃ a : zmod p, a ^ 2 = q) ↔ ∃ b : zmod q, b ^ 2 = p := if hpq : p = q then by substI hpq else have h1 : ((p / 2) * (q / 2)) % 2 = 0, from (dvd_iff_mod_eq_zero _ _).1 (dvd_mul_of_dvd_left ((dvd_iff_mod_eq_zero _ _).2 $ by rw [← mod_mul_right_div_self, show 2 * 2 = 4, from rfl, hp1]; refl) _), begin haveI hp_odd : fact (p % 2 = 1) := ⟨odd_of_mod_four_eq_one hp1⟩, have hpq0 : (p : zmod q) ≠ 0 := prime_ne_zero q p (ne.symm hpq), have hqp0 : (q : zmod p) ≠ 0 := prime_ne_zero p q hpq, have := quadratic_reciprocity p q hpq, rw [neg_one_pow_eq_pow_mod_two, h1, legendre_sym, legendre_sym, if_neg hqp0, if_neg hpq0] at this, rw [euler_criterion q hpq0, euler_criterion p hqp0], split_ifs at this; simp *; contradiction, end lemma exists_sq_eq_prime_iff_of_mod_four_eq_three (hp3 : p % 4 = 3) (hq3 : q % 4 = 3) (hpq : p ≠ q) : (∃ a : zmod p, a ^ 2 = q) ↔ ¬∃ b : zmod q, b ^ 2 = p := have h1 : ((p / 2) * (q / 2)) % 2 = 1, from nat.odd_mul_odd (by rw [← mod_mul_right_div_self, show 2 * 2 = 4, from rfl, hp3]; refl) (by rw [← mod_mul_right_div_self, show 2 * 2 = 4, from rfl, hq3]; refl), begin haveI hp_odd : fact (p % 2 = 1) := ⟨odd_of_mod_four_eq_three hp3⟩, haveI hq_odd : fact (q % 2 = 1) := ⟨odd_of_mod_four_eq_three hq3⟩, have hpq0 : (p : zmod q) ≠ 0 := prime_ne_zero q p (ne.symm hpq), have hqp0 : (q : zmod p) ≠ 0 := prime_ne_zero p q hpq, have := quadratic_reciprocity p q hpq, rw [neg_one_pow_eq_pow_mod_two, h1, legendre_sym, legendre_sym, if_neg hpq0, if_neg hqp0] at this, rw [euler_criterion q hpq0, euler_criterion p hqp0], split_ifs at this; simp *; contradiction end end zmod
583c85e27cd72cb20124fca65987525c5ddd7e10
c3f2fcd060adfa2ca29f924839d2d925e8f2c685
/tests/lean/run/structure_test.lean
fb2acec86a6f4d868e38615414a39dc2d55de6f2
[ "Apache-2.0" ]
permissive
respu/lean
6582d19a2f2838a28ecd2b3c6f81c32d07b5341d
8c76419c60b63d0d9f7bc04ebb0b99812d0ec654
refs/heads/master
1,610,882,451,231
1,427,747,084,000
1,427,747,429,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
709
lean
import logic data.sigma inductive point (A B : Type) := mk : Π (x : A) (y : B), point A B inductive color [class] := red | green | blue constant foo.{l} (A : Type.{l}) [H : decidable_eq A] : Type.{l} constants a : num section universe variable l variable A : Type.{l} variable Ha : decidable_eq A include Ha variable E : Type₂ include E -- include Ha structure point3d_color (B C : Type) (D : B → Type) extends point (foo A) B, sigma D renaming pr1→y pr2→w := mk :: (c : color) (H : x == y) check point3d_color.c check point3d_color.to_point end context universe l parameters A : Type.{l} parameters B : Type.{l} structure tst := mk :: (a : A) (b : B) end
83df84fd5bce45e2213b9644e1cc39316f6e5b6b
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/computability/turing_machine.lean
77423d845f35db2e67f5f1adee9f64adee1d02c4
[]
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
89,627
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.order import Mathlib.data.fintype.basic import Mathlib.data.pfun import Mathlib.tactic.apply_fun import Mathlib.logic.function.iterate import Mathlib.PostPort universes u_1 u_2 u v l u_3 u_4 namespace Mathlib /-! # Turing machines This file defines a sequence of simple machine languages, starting with Turing machines and working up to more complex languages based on Wang B-machines. ## Naming conventions Each model of computation in this file shares a naming convention for the elements of a model of computation. These are the parameters for the language: * `Γ` is the alphabet on the tape. * `Λ` is the set of labels, or internal machine states. * `σ` is the type of internal memory, not on the tape. This does not exist in the TM0 model, and later models achieve this by mixing it into `Λ`. * `K` is used in the TM2 model, which has multiple stacks, and denotes the number of such stacks. All of these variables denote "essentially finite" types, but for technical reasons it is convenient to allow them to be infinite anyway. When using an infinite type, we will be interested to prove that only finitely many values of the type are ever interacted with. Given these parameters, there are a few common structures for the model that arise: * `stmt` is the set of all actions that can be performed in one step. For the TM0 model this set is finite, and for later models it is an infinite inductive type representing "possible program texts". * `cfg` is the set of instantaneous configurations, that is, the state of the machine together with its environment. * `machine` is the set of all machines in the model. Usually this is approximately a function `Λ → stmt`, although different models have different ways of halting and other actions. * `step : cfg → option cfg` is the function that describes how the state evolves over one step. If `step c = none`, then `c` is a terminal state, and the result of the computation is read off from `c`. Because of the type of `step`, these models are all deterministic by construction. * `init : input → cfg` sets up the initial state. The type `input` depends on the model; in most cases it is `list Γ`. * `eval : machine → input → roption output`, given a machine `M` and input `i`, starts from `init i`, runs `step` until it reaches an output, and then applies a function `cfg → output` to the final state to obtain the result. The type `output` depends on the model. * `supports : machine → finset Λ → Prop` asserts that a machine `M` starts in `S : finset Λ`, and can only ever jump to other states inside `S`. This implies that the behavior of `M` on any input cannot depend on its values outside `S`. We use this to allow `Λ` to be an infinite set when convenient, and prove that only finitely many of these states are actually accessible. This formalizes "essentially finite" mentioned above. -/ namespace turing /-- The `blank_extends` partial order holds of `l₁` and `l₂` if `l₂` is obtained by adding blanks (`default Γ`) to the end of `l₁`. -/ def blank_extends {Γ : Type u_1} [Inhabited Γ] (l₁ : List Γ) (l₂ : List Γ) := ∃ (n : ℕ), l₂ = l₁ ++ list.repeat Inhabited.default n theorem blank_extends.refl {Γ : Type u_1} [Inhabited Γ] (l : List Γ) : blank_extends l l := sorry theorem blank_extends.trans {Γ : Type u_1} [Inhabited Γ] {l₁ : List Γ} {l₂ : List Γ} {l₃ : List Γ} : blank_extends l₁ l₂ → blank_extends l₂ l₃ → blank_extends l₁ l₃ := sorry theorem blank_extends.below_of_le {Γ : Type u_1} [Inhabited Γ] {l : List Γ} {l₁ : List Γ} {l₂ : List Γ} : blank_extends l l₁ → blank_extends l l₂ → list.length l₁ ≤ list.length l₂ → blank_extends l₁ l₂ := sorry /-- Any two extensions by blank `l₁,l₂` of `l` have a common join (which can be taken to be the longer of `l₁` and `l₂`). -/ def blank_extends.above {Γ : Type u_1} [Inhabited Γ] {l : List Γ} {l₁ : List Γ} {l₂ : List Γ} (h₁ : blank_extends l l₁) (h₂ : blank_extends l l₂) : Subtype fun (l' : List Γ) => blank_extends l₁ l' ∧ blank_extends l₂ l' := dite (list.length l₁ ≤ list.length l₂) (fun (h : list.length l₁ ≤ list.length l₂) => { val := l₂, property := sorry }) fun (h : ¬list.length l₁ ≤ list.length l₂) => { val := l₁, property := sorry } theorem blank_extends.above_of_le {Γ : Type u_1} [Inhabited Γ] {l : List Γ} {l₁ : List Γ} {l₂ : List Γ} : blank_extends l₁ l → blank_extends l₂ l → list.length l₁ ≤ list.length l₂ → blank_extends l₁ l₂ := sorry /-- `blank_rel` is the symmetric closure of `blank_extends`, turning it into an equivalence relation. Two lists are related by `blank_rel` if one extends the other by blanks. -/ def blank_rel {Γ : Type u_1} [Inhabited Γ] (l₁ : List Γ) (l₂ : List Γ) := blank_extends l₁ l₂ ∨ blank_extends l₂ l₁ theorem blank_rel.refl {Γ : Type u_1} [Inhabited Γ] (l : List Γ) : blank_rel l l := Or.inl (blank_extends.refl l) theorem blank_rel.symm {Γ : Type u_1} [Inhabited Γ] {l₁ : List Γ} {l₂ : List Γ} : blank_rel l₁ l₂ → blank_rel l₂ l₁ := or.symm theorem blank_rel.trans {Γ : Type u_1} [Inhabited Γ] {l₁ : List Γ} {l₂ : List Γ} {l₃ : List Γ} : blank_rel l₁ l₂ → blank_rel l₂ l₃ → blank_rel l₁ l₃ := sorry /-- Given two `blank_rel` lists, there exists (constructively) a common join. -/ def blank_rel.above {Γ : Type u_1} [Inhabited Γ] {l₁ : List Γ} {l₂ : List Γ} (h : blank_rel l₁ l₂) : Subtype fun (l : List Γ) => blank_extends l₁ l ∧ blank_extends l₂ l := dite (list.length l₁ ≤ list.length l₂) (fun (hl : list.length l₁ ≤ list.length l₂) => { val := l₂, property := sorry }) fun (hl : ¬list.length l₁ ≤ list.length l₂) => { val := l₁, property := sorry } /-- Given two `blank_rel` lists, there exists (constructively) a common meet. -/ def blank_rel.old_below {Γ : Type u_1} [Inhabited Γ] {l₁ : List Γ} {l₂ : List Γ} (h : blank_rel l₁ l₂) : Subtype fun (l : List Γ) => blank_extends l l₁ ∧ blank_extends l l₂ := dite (list.length l₁ ≤ list.length l₂) (fun (hl : list.length l₁ ≤ list.length l₂) => { val := l₁, property := blank_rel.below._proof_1 h hl }) fun (hl : ¬list.length l₁ ≤ list.length l₂) => { val := l₂, property := blank_rel.below._proof_2 h hl } theorem blank_rel.equivalence (Γ : Type u_1) [Inhabited Γ] : equivalence blank_rel := { left := blank_rel.refl, right := { left := blank_rel.symm, right := blank_rel.trans } } /-- Construct a setoid instance for `blank_rel`. -/ def blank_rel.setoid (Γ : Type u_1) [Inhabited Γ] : setoid (List Γ) := setoid.mk blank_rel (blank_rel.equivalence Γ) /-- A `list_blank Γ` is a quotient of `list Γ` by extension by blanks at the end. This is used to represent half-tapes of a Turing machine, so that we can pretend that the list continues infinitely with blanks. -/ def list_blank (Γ : Type u_1) [Inhabited Γ] := quotient (blank_rel.setoid Γ) protected instance list_blank.inhabited {Γ : Type u_1} [Inhabited Γ] : Inhabited (list_blank Γ) := { default := quotient.mk' [] } protected instance list_blank.has_emptyc {Γ : Type u_1} [Inhabited Γ] : has_emptyc (list_blank Γ) := has_emptyc.mk (quotient.mk' []) /-- A modified version of `quotient.lift_on'` specialized for `list_blank`, with the stronger precondition `blank_extends` instead of `blank_rel`. -/ protected def list_blank.lift_on {Γ : Type u_1} [Inhabited Γ] {α : Sort u_2} (l : list_blank Γ) (f : List Γ → α) (H : ∀ (a b : List Γ), blank_extends a b → f a = f b) : α := quotient.lift_on' l f sorry /-- The quotient map turning a `list` into a `list_blank`. -/ def list_blank.mk {Γ : Type u_1} [Inhabited Γ] : List Γ → list_blank Γ := quotient.mk' protected theorem list_blank.induction_on {Γ : Type u_1} [Inhabited Γ] {p : list_blank Γ → Prop} (q : list_blank Γ) (h : ∀ (a : List Γ), p (list_blank.mk a)) : p q := quotient.induction_on' q h /-- The head of a `list_blank` is well defined. -/ def list_blank.head {Γ : Type u_1} [Inhabited Γ] (l : list_blank Γ) : Γ := list_blank.lift_on l list.head sorry @[simp] theorem list_blank.head_mk {Γ : Type u_1} [Inhabited Γ] (l : List Γ) : list_blank.head (list_blank.mk l) = list.head l := rfl /-- The tail of a `list_blank` is well defined (up to the tail of blanks). -/ def list_blank.tail {Γ : Type u_1} [Inhabited Γ] (l : list_blank Γ) : list_blank Γ := list_blank.lift_on l (fun (l : List Γ) => list_blank.mk (list.tail l)) sorry @[simp] theorem list_blank.tail_mk {Γ : Type u_1} [Inhabited Γ] (l : List Γ) : list_blank.tail (list_blank.mk l) = list_blank.mk (list.tail l) := rfl /-- We can cons an element onto a `list_blank`. -/ def list_blank.cons {Γ : Type u_1} [Inhabited Γ] (a : Γ) (l : list_blank Γ) : list_blank Γ := list_blank.lift_on l (fun (l : List Γ) => list_blank.mk (a :: l)) sorry @[simp] theorem list_blank.cons_mk {Γ : Type u_1} [Inhabited Γ] (a : Γ) (l : List Γ) : list_blank.cons a (list_blank.mk l) = list_blank.mk (a :: l) := rfl @[simp] theorem list_blank.head_cons {Γ : Type u_1} [Inhabited Γ] (a : Γ) (l : list_blank Γ) : list_blank.head (list_blank.cons a l) = a := quotient.ind' fun (l : List Γ) => rfl @[simp] theorem list_blank.tail_cons {Γ : Type u_1} [Inhabited Γ] (a : Γ) (l : list_blank Γ) : list_blank.tail (list_blank.cons a l) = l := quotient.ind' fun (l : List Γ) => rfl /-- The `cons` and `head`/`tail` functions are mutually inverse, unlike in the case of `list` where this only holds for nonempty lists. -/ @[simp] theorem list_blank.cons_head_tail {Γ : Type u_1} [Inhabited Γ] (l : list_blank Γ) : list_blank.cons (list_blank.head l) (list_blank.tail l) = l := sorry /-- The `cons` and `head`/`tail` functions are mutually inverse, unlike in the case of `list` where this only holds for nonempty lists. -/ theorem list_blank.exists_cons {Γ : Type u_1} [Inhabited Γ] (l : list_blank Γ) : ∃ (a : Γ), ∃ (l' : list_blank Γ), l = list_blank.cons a l' := Exists.intro (list_blank.head l) (Exists.intro (list_blank.tail l) (Eq.symm (list_blank.cons_head_tail l))) /-- The n-th element of a `list_blank` is well defined for all `n : ℕ`, unlike in a `list`. -/ def list_blank.nth {Γ : Type u_1} [Inhabited Γ] (l : list_blank Γ) (n : ℕ) : Γ := list_blank.lift_on l (fun (l : List Γ) => list.inth l n) sorry @[simp] theorem list_blank.nth_mk {Γ : Type u_1} [Inhabited Γ] (l : List Γ) (n : ℕ) : list_blank.nth (list_blank.mk l) n = list.inth l n := rfl @[simp] theorem list_blank.nth_zero {Γ : Type u_1} [Inhabited Γ] (l : list_blank Γ) : list_blank.nth l 0 = list_blank.head l := sorry @[simp] theorem list_blank.nth_succ {Γ : Type u_1} [Inhabited Γ] (l : list_blank Γ) (n : ℕ) : list_blank.nth l (n + 1) = list_blank.nth (list_blank.tail l) n := sorry theorem list_blank.ext {Γ : Type u_1} [Inhabited Γ] {L₁ : list_blank Γ} {L₂ : list_blank Γ} : (∀ (i : ℕ), list_blank.nth L₁ i = list_blank.nth L₂ i) → L₁ = L₂ := sorry /-- Apply a function to a value stored at the nth position of the list. -/ @[simp] def list_blank.modify_nth {Γ : Type u_1} [Inhabited Γ] (f : Γ → Γ) : ℕ → list_blank Γ → list_blank Γ := sorry theorem list_blank.nth_modify_nth {Γ : Type u_1} [Inhabited Γ] (f : Γ → Γ) (n : ℕ) (i : ℕ) (L : list_blank Γ) : list_blank.nth (list_blank.modify_nth f n L) i = ite (i = n) (f (list_blank.nth L i)) (list_blank.nth L i) := sorry /-- A pointed map of `inhabited` types is a map that sends one default value to the other. -/ structure pointed_map (Γ : Type u) (Γ' : Type v) [Inhabited Γ] [Inhabited Γ'] where f : Γ → Γ' map_pt' : f Inhabited.default = Inhabited.default protected instance pointed_map.inhabited {Γ : Type u_1} {Γ' : Type u_2} [Inhabited Γ] [Inhabited Γ'] : Inhabited (pointed_map Γ Γ') := { default := pointed_map.mk (fun (_x : Γ) => Inhabited.default) sorry } protected instance pointed_map.has_coe_to_fun {Γ : Type u_1} {Γ' : Type u_2} [Inhabited Γ] [Inhabited Γ'] : has_coe_to_fun (pointed_map Γ Γ') := has_coe_to_fun.mk (fun (x : pointed_map Γ Γ') => Γ → Γ') pointed_map.f @[simp] theorem pointed_map.mk_val {Γ : Type u_1} {Γ' : Type u_2} [Inhabited Γ] [Inhabited Γ'] (f : Γ → Γ') (pt : f Inhabited.default = Inhabited.default) : ⇑(pointed_map.mk f pt) = f := rfl @[simp] theorem pointed_map.map_pt {Γ : Type u_1} {Γ' : Type u_2} [Inhabited Γ] [Inhabited Γ'] (f : pointed_map Γ Γ') : coe_fn f Inhabited.default = Inhabited.default := pointed_map.map_pt' f @[simp] theorem pointed_map.head_map {Γ : Type u_1} {Γ' : Type u_2} [Inhabited Γ] [Inhabited Γ'] (f : pointed_map Γ Γ') (l : List Γ) : list.head (list.map (⇑f) l) = coe_fn f (list.head l) := list.cases_on l (Eq.symm (pointed_map.map_pt f)) fun (l_hd : Γ) (l_tl : List Γ) => Eq.refl (list.head (list.map (⇑f) (l_hd :: l_tl))) /-- The `map` function on lists is well defined on `list_blank`s provided that the map is pointed. -/ def list_blank.map {Γ : Type u_1} {Γ' : Type u_2} [Inhabited Γ] [Inhabited Γ'] (f : pointed_map Γ Γ') (l : list_blank Γ) : list_blank Γ' := list_blank.lift_on l (fun (l : List Γ) => list_blank.mk (list.map (⇑f) l)) sorry @[simp] theorem list_blank.map_mk {Γ : Type u_1} {Γ' : Type u_2} [Inhabited Γ] [Inhabited Γ'] (f : pointed_map Γ Γ') (l : List Γ) : list_blank.map f (list_blank.mk l) = list_blank.mk (list.map (⇑f) l) := rfl @[simp] theorem list_blank.head_map {Γ : Type u_1} {Γ' : Type u_2} [Inhabited Γ] [Inhabited Γ'] (f : pointed_map Γ Γ') (l : list_blank Γ) : list_blank.head (list_blank.map f l) = coe_fn f (list_blank.head l) := sorry @[simp] theorem list_blank.tail_map {Γ : Type u_1} {Γ' : Type u_2} [Inhabited Γ] [Inhabited Γ'] (f : pointed_map Γ Γ') (l : list_blank Γ) : list_blank.tail (list_blank.map f l) = list_blank.map f (list_blank.tail l) := sorry @[simp] theorem list_blank.map_cons {Γ : Type u_1} {Γ' : Type u_2} [Inhabited Γ] [Inhabited Γ'] (f : pointed_map Γ Γ') (l : list_blank Γ) (a : Γ) : list_blank.map f (list_blank.cons a l) = list_blank.cons (coe_fn f a) (list_blank.map f l) := sorry @[simp] theorem list_blank.nth_map {Γ : Type u_1} {Γ' : Type u_2} [Inhabited Γ] [Inhabited Γ'] (f : pointed_map Γ Γ') (l : list_blank Γ) (n : ℕ) : list_blank.nth (list_blank.map f l) n = coe_fn f (list_blank.nth l n) := sorry /-- The `i`-th projection as a pointed map. -/ def proj {ι : Type u_1} {Γ : ι → Type u_2} [(i : ι) → Inhabited (Γ i)] (i : ι) : pointed_map ((i : ι) → Γ i) (Γ i) := pointed_map.mk (fun (a : (i : ι) → Γ i) => a i) sorry theorem proj_map_nth {ι : Type u_1} {Γ : ι → Type u_2} [(i : ι) → Inhabited (Γ i)] (i : ι) (L : list_blank ((i : ι) → Γ i)) (n : ℕ) : list_blank.nth (list_blank.map (proj i) L) n = list_blank.nth L n i := sorry theorem list_blank.map_modify_nth {Γ : Type u_1} {Γ' : Type u_2} [Inhabited Γ] [Inhabited Γ'] (F : pointed_map Γ Γ') (f : Γ → Γ) (f' : Γ' → Γ') (H : ∀ (x : Γ), coe_fn F (f x) = f' (coe_fn F x)) (n : ℕ) (L : list_blank Γ) : list_blank.map F (list_blank.modify_nth f n L) = list_blank.modify_nth f' n (list_blank.map F L) := sorry /-- Append a list on the left side of a list_blank. -/ @[simp] def list_blank.append {Γ : Type u_1} [Inhabited Γ] : List Γ → list_blank Γ → list_blank Γ := sorry @[simp] theorem list_blank.append_mk {Γ : Type u_1} [Inhabited Γ] (l₁ : List Γ) (l₂ : List Γ) : list_blank.append l₁ (list_blank.mk l₂) = list_blank.mk (l₁ ++ l₂) := sorry theorem list_blank.append_assoc {Γ : Type u_1} [Inhabited Γ] (l₁ : List Γ) (l₂ : List Γ) (l₃ : list_blank Γ) : list_blank.append (l₁ ++ l₂) l₃ = list_blank.append l₁ (list_blank.append l₂ l₃) := sorry /-- The `bind` function on lists is well defined on `list_blank`s provided that the default element is sent to a sequence of default elements. -/ def list_blank.bind {Γ : Type u_1} {Γ' : Type u_2} [Inhabited Γ] [Inhabited Γ'] (l : list_blank Γ) (f : Γ → List Γ') (hf : ∃ (n : ℕ), f Inhabited.default = list.repeat Inhabited.default n) : list_blank Γ' := list_blank.lift_on l (fun (l : List Γ) => list_blank.mk (list.bind l f)) sorry @[simp] theorem list_blank.bind_mk {Γ : Type u_1} {Γ' : Type u_2} [Inhabited Γ] [Inhabited Γ'] (l : List Γ) (f : Γ → List Γ') (hf : ∃ (n : ℕ), f Inhabited.default = list.repeat Inhabited.default n) : list_blank.bind (list_blank.mk l) f hf = list_blank.mk (list.bind l f) := rfl @[simp] theorem list_blank.cons_bind {Γ : Type u_1} {Γ' : Type u_2} [Inhabited Γ] [Inhabited Γ'] (a : Γ) (l : list_blank Γ) (f : Γ → List Γ') (hf : ∃ (n : ℕ), f Inhabited.default = list.repeat Inhabited.default n) : list_blank.bind (list_blank.cons a l) f hf = list_blank.append (f a) (list_blank.bind l f hf) := sorry /-- The tape of a Turing machine is composed of a head element (which we imagine to be the current position of the head), together with two `list_blank`s denoting the portions of the tape going off to the left and right. When the Turing machine moves right, an element is pulled from the right side and becomes the new head, while the head element is consed onto the left side. -/ structure tape (Γ : Type u_1) [Inhabited Γ] where head : Γ left : list_blank Γ right : list_blank Γ protected instance tape.inhabited {Γ : Type u_1} [Inhabited Γ] : Inhabited (tape Γ) := { default := tape.mk Inhabited.default Inhabited.default Inhabited.default } /-- A direction for the turing machine `move` command, either left or right. -/ inductive dir where | left : dir | right : dir /-- The "inclusive" left side of the tape, including both `left` and `head`. -/ def tape.left₀ {Γ : Type u_1} [Inhabited Γ] (T : tape Γ) : list_blank Γ := list_blank.cons (tape.head T) (tape.left T) /-- The "inclusive" right side of the tape, including both `right` and `head`. -/ def tape.right₀ {Γ : Type u_1} [Inhabited Γ] (T : tape Γ) : list_blank Γ := list_blank.cons (tape.head T) (tape.right T) /-- Move the tape in response to a motion of the Turing machine. Note that `T.move dir.left` makes `T.left` smaller; the Turing machine is moving left and the tape is moving right. -/ def tape.move {Γ : Type u_1} [Inhabited Γ] : dir → tape Γ → tape Γ := sorry @[simp] theorem tape.move_left_right {Γ : Type u_1} [Inhabited Γ] (T : tape Γ) : tape.move dir.right (tape.move dir.left T) = T := sorry @[simp] theorem tape.move_right_left {Γ : Type u_1} [Inhabited Γ] (T : tape Γ) : tape.move dir.left (tape.move dir.right T) = T := sorry /-- Construct a tape from a left side and an inclusive right side. -/ def tape.mk' {Γ : Type u_1} [Inhabited Γ] (L : list_blank Γ) (R : list_blank Γ) : tape Γ := tape.mk (list_blank.head R) L (list_blank.tail R) @[simp] theorem tape.mk'_left {Γ : Type u_1} [Inhabited Γ] (L : list_blank Γ) (R : list_blank Γ) : tape.left (tape.mk' L R) = L := rfl @[simp] theorem tape.mk'_head {Γ : Type u_1} [Inhabited Γ] (L : list_blank Γ) (R : list_blank Γ) : tape.head (tape.mk' L R) = list_blank.head R := rfl @[simp] theorem tape.mk'_right {Γ : Type u_1} [Inhabited Γ] (L : list_blank Γ) (R : list_blank Γ) : tape.right (tape.mk' L R) = list_blank.tail R := rfl @[simp] theorem tape.mk'_right₀ {Γ : Type u_1} [Inhabited Γ] (L : list_blank Γ) (R : list_blank Γ) : tape.right₀ (tape.mk' L R) = R := list_blank.cons_head_tail R @[simp] theorem tape.mk'_left_right₀ {Γ : Type u_1} [Inhabited Γ] (T : tape Γ) : tape.mk' (tape.left T) (tape.right₀ T) = T := sorry theorem tape.exists_mk' {Γ : Type u_1} [Inhabited Γ] (T : tape Γ) : ∃ (L : list_blank Γ), ∃ (R : list_blank Γ), T = tape.mk' L R := Exists.intro (tape.left T) (Exists.intro (tape.right₀ T) (Eq.symm (tape.mk'_left_right₀ T))) @[simp] theorem tape.move_left_mk' {Γ : Type u_1} [Inhabited Γ] (L : list_blank Γ) (R : list_blank Γ) : tape.move dir.left (tape.mk' L R) = tape.mk' (list_blank.tail L) (list_blank.cons (list_blank.head L) R) := sorry @[simp] theorem tape.move_right_mk' {Γ : Type u_1} [Inhabited Γ] (L : list_blank Γ) (R : list_blank Γ) : tape.move dir.right (tape.mk' L R) = tape.mk' (list_blank.cons (list_blank.head R) L) (list_blank.tail R) := sorry /-- Construct a tape from a left side and an inclusive right side. -/ def tape.mk₂ {Γ : Type u_1} [Inhabited Γ] (L : List Γ) (R : List Γ) : tape Γ := tape.mk' (list_blank.mk L) (list_blank.mk R) /-- Construct a tape from a list, with the head of the list at the TM head and the rest going to the right. -/ def tape.mk₁ {Γ : Type u_1} [Inhabited Γ] (l : List Γ) : tape Γ := tape.mk₂ [] l /-- The `nth` function of a tape is integer-valued, with index `0` being the head, negative indexes on the left and positive indexes on the right. (Picture a number line.) -/ def tape.nth {Γ : Type u_1} [Inhabited Γ] (T : tape Γ) : ℤ → Γ := sorry @[simp] theorem tape.nth_zero {Γ : Type u_1} [Inhabited Γ] (T : tape Γ) : tape.nth T 0 = tape.head T := rfl theorem tape.right₀_nth {Γ : Type u_1} [Inhabited Γ] (T : tape Γ) (n : ℕ) : list_blank.nth (tape.right₀ T) n = tape.nth T ↑n := sorry @[simp] theorem tape.mk'_nth_nat {Γ : Type u_1} [Inhabited Γ] (L : list_blank Γ) (R : list_blank Γ) (n : ℕ) : tape.nth (tape.mk' L R) ↑n = list_blank.nth R n := sorry @[simp] theorem tape.move_left_nth {Γ : Type u_1} [Inhabited Γ] (T : tape Γ) (i : ℤ) : tape.nth (tape.move dir.left T) i = tape.nth T (i - 1) := sorry @[simp] theorem tape.move_right_nth {Γ : Type u_1} [Inhabited Γ] (T : tape Γ) (i : ℤ) : tape.nth (tape.move dir.right T) i = tape.nth T (i + 1) := sorry @[simp] theorem tape.move_right_n_head {Γ : Type u_1} [Inhabited Γ] (T : tape Γ) (i : ℕ) : tape.head (nat.iterate (tape.move dir.right) i T) = tape.nth T ↑i := sorry /-- Replace the current value of the head on the tape. -/ def tape.write {Γ : Type u_1} [Inhabited Γ] (b : Γ) (T : tape Γ) : tape Γ := tape.mk b (tape.left T) (tape.right T) @[simp] theorem tape.write_self {Γ : Type u_1} [Inhabited Γ] (T : tape Γ) : tape.write (tape.head T) T = T := tape.cases_on T fun (T_head : Γ) (T_left T_right : list_blank Γ) => Eq.refl (tape.write (tape.head (tape.mk T_head T_left T_right)) (tape.mk T_head T_left T_right)) @[simp] theorem tape.write_nth {Γ : Type u_1} [Inhabited Γ] (b : Γ) (T : tape Γ) {i : ℤ} : tape.nth (tape.write b T) i = ite (i = 0) b (tape.nth T i) := sorry @[simp] theorem tape.write_mk' {Γ : Type u_1} [Inhabited Γ] (a : Γ) (b : Γ) (L : list_blank Γ) (R : list_blank Γ) : tape.write b (tape.mk' L (list_blank.cons a R)) = tape.mk' L (list_blank.cons b R) := sorry /-- Apply a pointed map to a tape to change the alphabet. -/ def tape.map {Γ : Type u_1} {Γ' : Type u_2} [Inhabited Γ] [Inhabited Γ'] (f : pointed_map Γ Γ') (T : tape Γ) : tape Γ' := tape.mk (coe_fn f (tape.head T)) (list_blank.map f (tape.left T)) (list_blank.map f (tape.right T)) @[simp] theorem tape.map_fst {Γ : Type u_1} {Γ' : Type u_2} [Inhabited Γ] [Inhabited Γ'] (f : pointed_map Γ Γ') (T : tape Γ) : tape.head (tape.map f T) = coe_fn f (tape.head T) := tape.cases_on T fun (T_head : Γ) (T_left T_right : list_blank Γ) => Eq.refl (tape.head (tape.map f (tape.mk T_head T_left T_right))) @[simp] theorem tape.map_write {Γ : Type u_1} {Γ' : Type u_2} [Inhabited Γ] [Inhabited Γ'] (f : pointed_map Γ Γ') (b : Γ) (T : tape Γ) : tape.map f (tape.write b T) = tape.write (coe_fn f b) (tape.map f T) := tape.cases_on T fun (T_head : Γ) (T_left T_right : list_blank Γ) => Eq.refl (tape.map f (tape.write b (tape.mk T_head T_left T_right))) @[simp] theorem tape.write_move_right_n {Γ : Type u_1} [Inhabited Γ] (f : Γ → Γ) (L : list_blank Γ) (R : list_blank Γ) (n : ℕ) : tape.write (f (list_blank.nth R n)) (nat.iterate (tape.move dir.right) n (tape.mk' L R)) = nat.iterate (tape.move dir.right) n (tape.mk' L (list_blank.modify_nth f n R)) := sorry theorem tape.map_move {Γ : Type u_1} {Γ' : Type u_2} [Inhabited Γ] [Inhabited Γ'] (f : pointed_map Γ Γ') (T : tape Γ) (d : dir) : tape.map f (tape.move d T) = tape.move d (tape.map f T) := sorry theorem tape.map_mk' {Γ : Type u_1} {Γ' : Type u_2} [Inhabited Γ] [Inhabited Γ'] (f : pointed_map Γ Γ') (L : list_blank Γ) (R : list_blank Γ) : tape.map f (tape.mk' L R) = tape.mk' (list_blank.map f L) (list_blank.map f R) := sorry theorem tape.map_mk₂ {Γ : Type u_1} {Γ' : Type u_2} [Inhabited Γ] [Inhabited Γ'] (f : pointed_map Γ Γ') (L : List Γ) (R : List Γ) : tape.map f (tape.mk₂ L R) = tape.mk₂ (list.map (⇑f) L) (list.map (⇑f) R) := sorry theorem tape.map_mk₁ {Γ : Type u_1} {Γ' : Type u_2} [Inhabited Γ] [Inhabited Γ'] (f : pointed_map Γ Γ') (l : List Γ) : tape.map f (tape.mk₁ l) = tape.mk₁ (list.map (⇑f) l) := tape.map_mk₂ f [] l /-- Run a state transition function `σ → option σ` "to completion". The return value is the last state returned before a `none` result. If the state transition function always returns `some`, then the computation diverges, returning `roption.none`. -/ def eval {σ : Type u_1} (f : σ → Option σ) : σ → roption σ := pfun.fix fun (s : σ) => roption.some (option.elim (f s) (sum.inl s) sum.inr) /-- The reflexive transitive closure of a state transition function. `reaches f a b` means there is a finite sequence of steps `f a = some a₁`, `f a₁ = some a₂`, ... such that `aₙ = b`. This relation permits zero steps of the state transition function. -/ def reaches {σ : Type u_1} (f : σ → Option σ) : σ → σ → Prop := relation.refl_trans_gen fun (a b : σ) => b ∈ f a /-- The transitive closure of a state transition function. `reaches₁ f a b` means there is a nonempty finite sequence of steps `f a = some a₁`, `f a₁ = some a₂`, ... such that `aₙ = b`. This relation does not permit zero steps of the state transition function. -/ def reaches₁ {σ : Type u_1} (f : σ → Option σ) : σ → σ → Prop := relation.trans_gen fun (a b : σ) => b ∈ f a theorem reaches₁_eq {σ : Type u_1} {f : σ → Option σ} {a : σ} {b : σ} {c : σ} (h : f a = f b) : reaches₁ f a c ↔ reaches₁ f b c := sorry theorem reaches_total {σ : Type u_1} {f : σ → Option σ} {a : σ} {b : σ} {c : σ} : reaches f a b → reaches f a c → reaches f b c ∨ reaches f c b := relation.refl_trans_gen.total_of_right_unique fun (_x _x_1 _x_2 : σ) => option.mem_unique theorem reaches₁_fwd {σ : Type u_1} {f : σ → Option σ} {a : σ} {b : σ} {c : σ} (h₁ : reaches₁ f a c) (h₂ : b ∈ f a) : reaches f b c := sorry /-- A variation on `reaches`. `reaches₀ f a b` holds if whenever `reaches₁ f b c` then `reaches₁ f a c`. This is a weaker property than `reaches` and is useful for replacing states with equivalent states without taking a step. -/ def reaches₀ {σ : Type u_1} (f : σ → Option σ) (a : σ) (b : σ) := ∀ (c : σ), reaches₁ f b c → reaches₁ f a c theorem reaches₀.trans {σ : Type u_1} {f : σ → Option σ} {a : σ} {b : σ} {c : σ} (h₁ : reaches₀ f a b) (h₂ : reaches₀ f b c) : reaches₀ f a c := fun (c_1 : σ) (ᾰ : reaches₁ f c c_1) => idRhs (reaches₁ f a c_1) (h₁ c_1 (h₂ c_1 ᾰ)) theorem reaches₀.refl {σ : Type u_1} {f : σ → Option σ} (a : σ) : reaches₀ f a a := fun (c : σ) (ᾰ : reaches₁ f a c) => idRhs (reaches₁ f a c) ᾰ theorem reaches₀.single {σ : Type u_1} {f : σ → Option σ} {a : σ} {b : σ} (h : b ∈ f a) : reaches₀ f a b := fun (c : σ) (ᾰ : reaches₁ f b c) => idRhs (relation.trans_gen (fun (a b : σ) => b ∈ f a) a c) (relation.trans_gen.head h ᾰ) theorem reaches₀.head {σ : Type u_1} {f : σ → Option σ} {a : σ} {b : σ} {c : σ} (h : b ∈ f a) (h₂ : reaches₀ f b c) : reaches₀ f a c := reaches₀.trans (reaches₀.single h) h₂ theorem reaches₀.tail {σ : Type u_1} {f : σ → Option σ} {a : σ} {b : σ} {c : σ} (h₁ : reaches₀ f a b) (h : c ∈ f b) : reaches₀ f a c := reaches₀.trans h₁ (reaches₀.single h) theorem reaches₀_eq {σ : Type u_1} {f : σ → Option σ} {a : σ} {b : σ} (e : f a = f b) : reaches₀ f a b := fun (c : σ) (ᾰ : reaches₁ f b c) => idRhs (reaches₁ f a c) (iff.mpr (reaches₁_eq e) ᾰ) theorem reaches₁.to₀ {σ : Type u_1} {f : σ → Option σ} {a : σ} {b : σ} (h : reaches₁ f a b) : reaches₀ f a b := fun (c : σ) (ᾰ : reaches₁ f b c) => idRhs (relation.trans_gen (fun (a b : σ) => b ∈ f a) a c) (relation.trans_gen.trans h ᾰ) theorem reaches.to₀ {σ : Type u_1} {f : σ → Option σ} {a : σ} {b : σ} (h : reaches f a b) : reaches₀ f a b := fun (c : σ) (ᾰ : reaches₁ f b c) => idRhs (relation.trans_gen (fun (a b : σ) => b ∈ f a) a c) (relation.trans_gen.trans_right h ᾰ) theorem reaches₀.tail' {σ : Type u_1} {f : σ → Option σ} {a : σ} {b : σ} {c : σ} (h : reaches₀ f a b) (h₂ : c ∈ f b) : reaches₁ f a c := h c (relation.trans_gen.single h₂) /-- (co-)Induction principle for `eval`. If a property `C` holds of any point `a` evaluating to `b` which is either terminal (meaning `a = b`) or where the next point also satisfies `C`, then it holds of any point where `eval f a` evaluates to `b`. This formalizes the notion that if `eval f a` evaluates to `b` then it reaches terminal state `b` in finitely many steps. -/ def eval_induction {σ : Type u_1} {f : σ → Option σ} {b : σ} {C : σ → Sort u_2} {a : σ} (h : b ∈ eval f a) (H : (a : σ) → b ∈ eval f a → ((a' : σ) → b ∈ eval f a' → f a = some a' → C a') → C a) : C a := pfun.fix_induction h fun (a' : σ) (ha' : b ∈ pfun.fix (fun (s : σ) => roption.some (option.elim (f s) (sum.inl s) sum.inr)) a') (h' : (a'_1 : σ) → b ∈ pfun.fix (fun (s : σ) => roption.some (option.elim (f s) (sum.inl s) sum.inr)) a'_1 → sum.inr a'_1 ∈ roption.some (option.elim (f a') (sum.inl a') sum.inr) → C a'_1) => H a' ha' fun (b' : σ) (hb' : b ∈ eval f b') (e : f a' = some b') => h' b' hb' sorry theorem mem_eval {σ : Type u_1} {f : σ → Option σ} {a : σ} {b : σ} : b ∈ eval f a ↔ reaches f a b ∧ f b = none := sorry theorem eval_maximal₁ {σ : Type u_1} {f : σ → Option σ} {a : σ} {b : σ} (h : b ∈ eval f a) (c : σ) : ¬reaches₁ f b c := sorry theorem eval_maximal {σ : Type u_1} {f : σ → Option σ} {a : σ} {b : σ} (h : b ∈ eval f a) {c : σ} : reaches f b c ↔ c = b := sorry theorem reaches_eval {σ : Type u_1} {f : σ → Option σ} {a : σ} {b : σ} (ab : reaches f a b) : eval f a = eval f b := sorry /-- Given a relation `tr : σ₁ → σ₂ → Prop` between state spaces, and state transition functions `f₁ : σ₁ → option σ₁` and `f₂ : σ₂ → option σ₂`, `respects f₁ f₂ tr` means that if `tr a₁ a₂` holds initially and `f₁` takes a step to `a₂` then `f₂` will take one or more steps before reaching a state `b₂` satisfying `tr a₂ b₂`, and if `f₁ a₁` terminates then `f₂ a₂` also terminates. Such a relation `tr` is also known as a refinement. -/ def respects {σ₁ : Type u_1} {σ₂ : Type u_2} (f₁ : σ₁ → Option σ₁) (f₂ : σ₂ → Option σ₂) (tr : σ₁ → σ₂ → Prop) := {a₁ : σ₁} → {a₂ : σ₂} → tr a₁ a₂ → sorry theorem tr_reaches₁ {σ₁ : Type u_1} {σ₂ : Type u_2} {f₁ : σ₁ → Option σ₁} {f₂ : σ₂ → Option σ₂} {tr : σ₁ → σ₂ → Prop} (H : respects f₁ f₂ tr) {a₁ : σ₁} {a₂ : σ₂} (aa : tr a₁ a₂) {b₁ : σ₁} (ab : reaches₁ f₁ a₁ b₁) : ∃ (b₂ : σ₂), tr b₁ b₂ ∧ reaches₁ f₂ a₂ b₂ := sorry theorem tr_reaches {σ₁ : Type u_1} {σ₂ : Type u_2} {f₁ : σ₁ → Option σ₁} {f₂ : σ₂ → Option σ₂} {tr : σ₁ → σ₂ → Prop} (H : respects f₁ f₂ tr) {a₁ : σ₁} {a₂ : σ₂} (aa : tr a₁ a₂) {b₁ : σ₁} (ab : reaches f₁ a₁ b₁) : ∃ (b₂ : σ₂), tr b₁ b₂ ∧ reaches f₂ a₂ b₂ := sorry theorem tr_reaches_rev {σ₁ : Type u_1} {σ₂ : Type u_2} {f₁ : σ₁ → Option σ₁} {f₂ : σ₂ → Option σ₂} {tr : σ₁ → σ₂ → Prop} (H : respects f₁ f₂ tr) {a₁ : σ₁} {a₂ : σ₂} (aa : tr a₁ a₂) {b₂ : σ₂} (ab : reaches f₂ a₂ b₂) : ∃ (c₁ : σ₁), ∃ (c₂ : σ₂), reaches f₂ b₂ c₂ ∧ tr c₁ c₂ ∧ reaches f₁ a₁ c₁ := sorry theorem tr_eval {σ₁ : Type u_1} {σ₂ : Type u_2} {f₁ : σ₁ → Option σ₁} {f₂ : σ₂ → Option σ₂} {tr : σ₁ → σ₂ → Prop} (H : respects f₁ f₂ tr) {a₁ : σ₁} {b₁ : σ₁} {a₂ : σ₂} (aa : tr a₁ a₂) (ab : b₁ ∈ eval f₁ a₁) : ∃ (b₂ : σ₂), tr b₁ b₂ ∧ b₂ ∈ eval f₂ a₂ := sorry theorem tr_eval_rev {σ₁ : Type u_1} {σ₂ : Type u_2} {f₁ : σ₁ → Option σ₁} {f₂ : σ₂ → Option σ₂} {tr : σ₁ → σ₂ → Prop} (H : respects f₁ f₂ tr) {a₁ : σ₁} {b₂ : σ₂} {a₂ : σ₂} (aa : tr a₁ a₂) (ab : b₂ ∈ eval f₂ a₂) : ∃ (b₁ : σ₁), tr b₁ b₂ ∧ b₁ ∈ eval f₁ a₁ := sorry theorem tr_eval_dom {σ₁ : Type u_1} {σ₂ : Type u_2} {f₁ : σ₁ → Option σ₁} {f₂ : σ₂ → Option σ₂} {tr : σ₁ → σ₂ → Prop} (H : respects f₁ f₂ tr) {a₁ : σ₁} {a₂ : σ₂} (aa : tr a₁ a₂) : roption.dom (eval f₂ a₂) ↔ roption.dom (eval f₁ a₁) := sorry /-- A simpler version of `respects` when the state transition relation `tr` is a function. -/ def frespects {σ₁ : Type u_1} {σ₂ : Type u_2} (f₂ : σ₂ → Option σ₂) (tr : σ₁ → σ₂) (a₂ : σ₂) : Option σ₁ → Prop := sorry theorem frespects_eq {σ₁ : Type u_1} {σ₂ : Type u_2} {f₂ : σ₂ → Option σ₂} {tr : σ₁ → σ₂} {a₂ : σ₂} {b₂ : σ₂} (h : f₂ a₂ = f₂ b₂) {b₁ : Option σ₁} : frespects f₂ tr a₂ b₁ ↔ frespects f₂ tr b₂ b₁ := sorry theorem fun_respects {σ₁ : Type u_1} {σ₂ : Type u_2} {f₁ : σ₁ → Option σ₁} {f₂ : σ₂ → Option σ₂} {tr : σ₁ → σ₂} : (respects f₁ f₂ fun (a : σ₁) (b : σ₂) => tr a = b) ↔ ∀ {a₁ : σ₁}, frespects f₂ tr (tr a₁) (f₁ a₁) := sorry theorem tr_eval' {σ₁ : Type u_1} {σ₂ : Type u_1} (f₁ : σ₁ → Option σ₁) (f₂ : σ₂ → Option σ₂) (tr : σ₁ → σ₂) (H : respects f₁ f₂ fun (a : σ₁) (b : σ₂) => tr a = b) (a₁ : σ₁) : eval f₂ (tr a₁) = tr <$> eval f₁ a₁ := sorry /-! ## The TM0 model A TM0 turing machine is essentially a Post-Turing machine, adapted for type theory. A Post-Turing machine with symbol type `Γ` and label type `Λ` is a function `Λ → Γ → option (Λ × stmt)`, where a `stmt` can be either `move left`, `move right` or `write a` for `a : Γ`. The machine works over a "tape", a doubly-infinite sequence of elements of `Γ`, and an instantaneous configuration, `cfg`, is a label `q : Λ` indicating the current internal state of the machine, and a `tape Γ` (which is essentially `ℤ →₀ Γ`). The evolution is described by the `step` function: * If `M q T.head = none`, then the machine halts. * If `M q T.head = some (q', s)`, then the machine performs action `s : stmt` and then transitions to state `q'`. The initial state takes a `list Γ` and produces a `tape Γ` where the head of the list is the head of the tape and the rest of the list extends to the right, with the left side all blank. The final state takes the entire right side of the tape right or equal to the current position of the machine. (This is actually a `list_blank Γ`, not a `list Γ`, because we don't know, at this level of generality, where the output ends. If equality to `default Γ` is decidable we can trim the list to remove the infinite tail of blanks.) -/ namespace TM0 /-- A Turing machine "statement" is just a command to either move left or right, or write a symbol on the tape. -/ inductive stmt (Γ : Type u_1) [Inhabited Γ] where | move : dir → stmt Γ | write : Γ → stmt Γ protected instance stmt.inhabited (Γ : Type u_1) [Inhabited Γ] : Inhabited (stmt Γ) := { default := stmt.write Inhabited.default } /-- A Post-Turing machine with symbol type `Γ` and label type `Λ` is a function which, given the current state `q : Λ` and the tape head `a : Γ`, either halts (returns `none`) or returns a new state `q' : Λ` and a `stmt` describing what to do, either a move left or right, or a write command. Both `Λ` and `Γ` are required to be inhabited; the default value for `Γ` is the "blank" tape value, and the default value of `Λ` is the initial state. -/ def machine (Γ : Type u_1) [Inhabited Γ] (Λ : Type u_2) [Inhabited Λ] := Λ → Γ → Option (Λ × stmt Γ) protected instance machine.inhabited (Γ : Type u_1) [Inhabited Γ] (Λ : Type u_2) [Inhabited Λ] : Inhabited (machine Γ Λ) := eq.mpr sorry (pi.inhabited Λ) /-- The configuration state of a Turing machine during operation consists of a label (machine state), and a tape, represented in the form `(a, L, R)` meaning the tape looks like `L.rev ++ [a] ++ R` with the machine currently reading the `a`. The lists are automatically extended with blanks as the machine moves around. -/ structure cfg (Γ : Type u_1) [Inhabited Γ] (Λ : Type u_2) [Inhabited Λ] where q : Λ tape : tape Γ protected instance cfg.inhabited (Γ : Type u_1) [Inhabited Γ] (Λ : Type u_2) [Inhabited Λ] : Inhabited (cfg Γ Λ) := { default := cfg.mk Inhabited.default Inhabited.default } /-- Execution semantics of the Turing machine. -/ def step {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] (M : machine Γ Λ) : cfg Γ Λ → Option (cfg Γ Λ) := sorry /-- The statement `reaches M s₁ s₂` means that `s₂` is obtained starting from `s₁` after a finite number of steps from `s₂`. -/ def reaches {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] (M : machine Γ Λ) : cfg Γ Λ → cfg Γ Λ → Prop := relation.refl_trans_gen fun (a b : cfg Γ Λ) => b ∈ step M a /-- The initial configuration. -/ def init {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] (l : List Γ) : cfg Γ Λ := cfg.mk Inhabited.default (tape.mk₁ l) /-- Evaluate a Turing machine on initial input to a final state, if it terminates. -/ def eval {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] (M : machine Γ Λ) (l : List Γ) : roption (list_blank Γ) := roption.map (fun (c : cfg Γ Λ) => tape.right₀ (cfg.tape c)) (eval (step M) (init l)) /-- The raw definition of a Turing machine does not require that `Γ` and `Λ` are finite, and in practice we will be interested in the infinite `Λ` case. We recover instead a notion of "effectively finite" Turing machines, which only make use of a finite subset of their states. We say that a set `S ⊆ Λ` supports a Turing machine `M` if `S` is closed under the transition function and contains the initial state. -/ def supports {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] (M : machine Γ Λ) (S : set Λ) := Inhabited.default ∈ S ∧ ∀ {q : Λ} {a : Γ} {q' : Λ} {s : stmt Γ}, (q', s) ∈ M q a → q ∈ S → q' ∈ S theorem step_supports {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] (M : machine Γ Λ) {S : set Λ} (ss : supports M S) {c : cfg Γ Λ} {c' : cfg Γ Λ} : c' ∈ step M c → cfg.q c ∈ S → cfg.q c' ∈ S := sorry theorem univ_supports {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] (M : machine Γ Λ) : supports M set.univ := { left := trivial, right := fun (q : Λ) (a : Γ) (q' : Λ) (s : stmt Γ) (h₁ : (q', s) ∈ M q a) (h₂ : q ∈ set.univ) => trivial } /-- Map a TM statement across a function. This does nothing to move statements and maps the write values. -/ def stmt.map {Γ : Type u_1} [Inhabited Γ] {Γ' : Type u_2} [Inhabited Γ'] (f : pointed_map Γ Γ') : stmt Γ → stmt Γ' := sorry /-- Map a configuration across a function, given `f : Γ → Γ'` a map of the alphabets and `g : Λ → Λ'` a map of the machine states. -/ def cfg.map {Γ : Type u_1} [Inhabited Γ] {Γ' : Type u_2} [Inhabited Γ'] {Λ : Type u_3} [Inhabited Λ] {Λ' : Type u_4} [Inhabited Λ'] (f : pointed_map Γ Γ') (g : Λ → Λ') : cfg Γ Λ → cfg Γ' Λ' := sorry /-- Because the state transition function uses the alphabet and machine states in both the input and output, to map a machine from one alphabet and machine state space to another we need functions in both directions, essentially an `equiv` without the laws. -/ def machine.map {Γ : Type u_1} [Inhabited Γ] {Γ' : Type u_2} [Inhabited Γ'] {Λ : Type u_3} [Inhabited Λ] {Λ' : Type u_4} [Inhabited Λ'] (M : machine Γ Λ) (f₁ : pointed_map Γ Γ') (f₂ : pointed_map Γ' Γ) (g₁ : Λ → Λ') (g₂ : Λ' → Λ) : machine Γ' Λ' := sorry theorem machine.map_step {Γ : Type u_1} [Inhabited Γ] {Γ' : Type u_2} [Inhabited Γ'] {Λ : Type u_3} [Inhabited Λ] {Λ' : Type u_4} [Inhabited Λ'] (M : machine Γ Λ) (f₁ : pointed_map Γ Γ') (f₂ : pointed_map Γ' Γ) (g₁ : Λ → Λ') (g₂ : Λ' → Λ) {S : set Λ} (f₂₁ : function.right_inverse ⇑f₁ ⇑f₂) (g₂₁ : ∀ (q : Λ), q ∈ S → g₂ (g₁ q) = q) (c : cfg Γ Λ) : cfg.q c ∈ S → option.map (cfg.map f₁ g₁) (step M c) = step (machine.map M f₁ f₂ g₁ g₂) (cfg.map f₁ g₁ c) := sorry theorem map_init {Γ : Type u_1} [Inhabited Γ] {Γ' : Type u_2} [Inhabited Γ'] {Λ : Type u_3} [Inhabited Λ] {Λ' : Type u_4} [Inhabited Λ'] (f₁ : pointed_map Γ Γ') (g₁ : pointed_map Λ Λ') (l : List Γ) : cfg.map f₁ (⇑g₁) (init l) = init (list.map (⇑f₁) l) := congr (congr_arg cfg.mk (pointed_map.map_pt g₁)) (tape.map_mk₁ f₁ l) theorem machine.map_respects {Γ : Type u_1} [Inhabited Γ] {Γ' : Type u_2} [Inhabited Γ'] {Λ : Type u_3} [Inhabited Λ] {Λ' : Type u_4} [Inhabited Λ'] (M : machine Γ Λ) (f₁ : pointed_map Γ Γ') (f₂ : pointed_map Γ' Γ) (g₁ : pointed_map Λ Λ') (g₂ : Λ' → Λ) {S : set Λ} (ss : supports M S) (f₂₁ : function.right_inverse ⇑f₁ ⇑f₂) (g₂₁ : ∀ (q : Λ), q ∈ S → g₂ (coe_fn g₁ q) = q) : respects (step M) (step (machine.map M f₁ f₂ (⇑g₁) g₂)) fun (a : cfg Γ Λ) (b : cfg Γ' Λ') => cfg.q a ∈ S ∧ cfg.map f₁ (⇑g₁) a = b := sorry end TM0 /-! ## The TM1 model The TM1 model is a simplification and extension of TM0 (Post-Turing model) in the direction of Wang B-machines. The machine's internal state is extended with a (finite) store `σ` of variables that may be accessed and updated at any time. A machine is given by a `Λ` indexed set of procedures or functions. Each function has a body which is a `stmt`. Most of the regular commands are allowed to use the current value `a` of the local variables and the value `T.head` on the tape to calculate what to write or how to change local state, but the statements themselves have a fixed structure. The `stmt`s can be as follows: * `move d q`: move left or right, and then do `q` * `write (f : Γ → σ → Γ) q`: write `f a T.head` to the tape, then do `q` * `load (f : Γ → σ → σ) q`: change the internal state to `f a T.head` * `branch (f : Γ → σ → bool) qtrue qfalse`: If `f a T.head` is true, do `qtrue`, else `qfalse` * `goto (f : Γ → σ → Λ)`: Go to label `f a T.head` * `halt`: Transition to the halting state, which halts on the following step Note that here most statements do not have labels; `goto` commands can only go to a new function. Only the `goto` and `halt` statements actually take a step; the rest is done by recursion on statements and so take 0 steps. (There is a uniform bound on many statements can be executed before the next `goto`, so this is an `O(1)` speedup with the constant depending on the machine.) The `halt` command has a one step stutter before actually halting so that any changes made before the halt have a chance to be "committed", since the `eval` relation uses the final configuration before the halt as the output, and `move` and `write` etc. take 0 steps in this model. -/ namespace TM1 /-- The TM1 model is a simplification and extension of TM0 (Post-Turing model) in the direction of Wang B-machines. The machine's internal state is extended with a (finite) store `σ` of variables that may be accessed and updated at any time. A machine is given by a `Λ` indexed set of procedures or functions. Each function has a body which is a `stmt`, which can either be a `move` or `write` command, a `branch` (if statement based on the current tape value), a `load` (set the variable value), a `goto` (call another function), or `halt`. Note that here most statements do not have labels; `goto` commands can only go to a new function. All commands have access to the variable value and current tape value. -/ inductive stmt (Γ : Type u_1) [Inhabited Γ] (Λ : Type u_2) (σ : Type u_3) where | move : dir → stmt Γ Λ σ → stmt Γ Λ σ | write : (Γ → σ → Γ) → stmt Γ Λ σ → stmt Γ Λ σ | load : (Γ → σ → σ) → stmt Γ Λ σ → stmt Γ Λ σ | branch : (Γ → σ → Bool) → stmt Γ Λ σ → stmt Γ Λ σ → stmt Γ Λ σ | goto : (Γ → σ → Λ) → stmt Γ Λ σ | halt : stmt Γ Λ σ protected instance stmt.inhabited (Γ : Type u_1) [Inhabited Γ] (Λ : Type u_2) (σ : Type u_3) : Inhabited (stmt Γ Λ σ) := { default := stmt.halt } /-- The configuration of a TM1 machine is given by the currently evaluating statement, the variable store value, and the tape. -/ structure cfg (Γ : Type u_1) [Inhabited Γ] (Λ : Type u_2) (σ : Type u_3) where l : Option Λ var : σ tape : tape Γ protected instance cfg.inhabited (Γ : Type u_1) [Inhabited Γ] (Λ : Type u_2) (σ : Type u_3) [Inhabited σ] : Inhabited (cfg Γ Λ σ) := { default := cfg.mk Inhabited.default Inhabited.default Inhabited.default } /-- The semantics of TM1 evaluation. -/ def step_aux {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} {σ : Type u_3} : stmt Γ Λ σ → σ → tape Γ → cfg Γ Λ σ := sorry /-- The state transition function. -/ def step {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} {σ : Type u_3} (M : Λ → stmt Γ Λ σ) : cfg Γ Λ σ → Option (cfg Γ Λ σ) := sorry /-- A set `S` of labels supports the statement `q` if all the `goto` statements in `q` refer only to other functions in `S`. -/ def supports_stmt {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} {σ : Type u_3} (S : finset Λ) : stmt Γ Λ σ → Prop := sorry /-- The subterm closure of a statement. -/ def stmts₁ {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} {σ : Type u_3} : stmt Γ Λ σ → finset (stmt Γ Λ σ) := sorry theorem stmts₁_self {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} {σ : Type u_3} {q : stmt Γ Λ σ} : q ∈ stmts₁ q := sorry theorem stmts₁_trans {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} {σ : Type u_3} {q₁ : stmt Γ Λ σ} {q₂ : stmt Γ Λ σ} : q₁ ∈ stmts₁ q₂ → stmts₁ q₁ ⊆ stmts₁ q₂ := sorry theorem stmts₁_supports_stmt_mono {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} {σ : Type u_3} {S : finset Λ} {q₁ : stmt Γ Λ σ} {q₂ : stmt Γ Λ σ} (h : q₁ ∈ stmts₁ q₂) (hs : supports_stmt S q₂) : supports_stmt S q₁ := sorry /-- The set of all statements in a turing machine, plus one extra value `none` representing the halt state. This is used in the TM1 to TM0 reduction. -/ def stmts {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} {σ : Type u_3} (M : Λ → stmt Γ Λ σ) (S : finset Λ) : finset (Option (stmt Γ Λ σ)) := finset.insert_none (finset.bUnion S fun (q : Λ) => stmts₁ (M q)) theorem stmts_trans {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} {σ : Type u_3} {M : Λ → stmt Γ Λ σ} {S : finset Λ} {q₁ : stmt Γ Λ σ} {q₂ : stmt Γ Λ σ} (h₁ : q₁ ∈ stmts₁ q₂) : some q₂ ∈ stmts M S → some q₁ ∈ stmts M S := sorry /-- A set `S` of labels supports machine `M` if all the `goto` statements in the functions in `S` refer only to other functions in `S`. -/ def supports {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} {σ : Type u_3} [Inhabited Λ] (M : Λ → stmt Γ Λ σ) (S : finset Λ) := Inhabited.default ∈ S ∧ ∀ (q : Λ), q ∈ S → supports_stmt S (M q) theorem stmts_supports_stmt {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} {σ : Type u_3} [Inhabited Λ] {M : Λ → stmt Γ Λ σ} {S : finset Λ} {q : stmt Γ Λ σ} (ss : supports M S) : some q ∈ stmts M S → supports_stmt S q := sorry theorem step_supports {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} {σ : Type u_3} [Inhabited Λ] (M : Λ → stmt Γ Λ σ) {S : finset Λ} (ss : supports M S) {c : cfg Γ Λ σ} {c' : cfg Γ Λ σ} : c' ∈ step M c → cfg.l c ∈ finset.insert_none S → cfg.l c' ∈ finset.insert_none S := sorry /-- The initial state, given a finite input that is placed on the tape starting at the TM head and going to the right. -/ def init {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} {σ : Type u_3} [Inhabited Λ] [Inhabited σ] (l : List Γ) : cfg Γ Λ σ := cfg.mk (some Inhabited.default) Inhabited.default (tape.mk₁ l) /-- Evaluate a TM to completion, resulting in an output list on the tape (with an indeterminate number of blanks on the end). -/ def eval {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} {σ : Type u_3} [Inhabited Λ] [Inhabited σ] (M : Λ → stmt Γ Λ σ) (l : List Γ) : roption (list_blank Γ) := roption.map (fun (c : cfg Γ Λ σ) => tape.right₀ (cfg.tape c)) (eval (step M) (init l)) end TM1 /-! ## TM1 emulator in TM0 To prove that TM1 computable functions are TM0 computable, we need to reduce each TM1 program to a TM0 program. So suppose a TM1 program is given. We take the following: * The alphabet `Γ` is the same for both TM1 and TM0 * The set of states `Λ'` is defined to be `option stmt₁ × σ`, that is, a TM1 statement or `none` representing halt, and the possible settings of the internal variables. Note that this is an infinite set, because `stmt₁` is infinite. This is okay because we assume that from the initial TM1 state, only finitely many other labels are reachable, and there are only finitely many statements that appear in all of these functions. Even though `stmt₁` contains a statement called `halt`, we must separate it from `none` (`some halt` steps to `none` and `none` actually halts) because there is a one step stutter in the TM1 semantics. -/ namespace TM1to0 /-- The base machine state space is a pair of an `option stmt₁` representing the current program to be executed, or `none` for the halt state, and a `σ` which is the local state (stored in the TM, not the tape). Because there are an infinite number of programs, this state space is infinite, but for a finitely supported TM1 machine and a finite type `σ`, only finitely many of these states are reachable. -/ -- because of the inhabited instance, but we could avoid the inhabited instances on Λ and σ here. -- But they are parameters so we cannot easily skip them for just this definition. def Λ' {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] {σ : Type u_3} [Inhabited σ] (M : Λ → TM1.stmt Γ Λ σ) := Option (TM1.stmt Γ Λ σ) × σ protected instance Λ'.inhabited {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] {σ : Type u_3} [Inhabited σ] (M : Λ → TM1.stmt Γ Λ σ) : Inhabited (Λ' M) := { default := (some (M Inhabited.default), Inhabited.default) } /-- The core TM1 → TM0 translation function. Here `s` is the current value on the tape, and the `stmt₁` is the TM1 statement to translate, with local state `v : σ`. We evaluate all regular instructions recursively until we reach either a `move` or `write` command, or a `goto`; in the latter case we emit a dummy `write s` step and transition to the new target location. -/ def tr_aux {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] {σ : Type u_3} [Inhabited σ] (M : Λ → TM1.stmt Γ Λ σ) (s : Γ) : TM1.stmt Γ Λ σ → σ → Λ' M × TM0.stmt Γ := sorry /-- The translated TM0 machine (given the TM1 machine input). -/ def tr {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] {σ : Type u_3} [Inhabited σ] (M : Λ → TM1.stmt Γ Λ σ) : TM0.machine Γ (Λ' M) := sorry /-- Translate configurations from TM1 to TM0. -/ def tr_cfg {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] {σ : Type u_3} [Inhabited σ] (M : Λ → TM1.stmt Γ Λ σ) : TM1.cfg Γ Λ σ → TM0.cfg Γ (Λ' M) := sorry theorem tr_respects {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] {σ : Type u_3} [Inhabited σ] (M : Λ → TM1.stmt Γ Λ σ) : respects (TM1.step M) (TM0.step (tr M)) fun (c₁ : TM1.cfg Γ Λ σ) (c₂ : TM0.cfg Γ (Λ' M)) => tr_cfg M c₁ = c₂ := sorry theorem tr_eval {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] {σ : Type u_3} [Inhabited σ] (M : Λ → TM1.stmt Γ Λ σ) (l : List Γ) : TM0.eval (tr M) l = TM1.eval M l := sorry /-- Given a finite set of accessible `Λ` machine states, there is a finite set of accessible machine states in the target (even though the type `Λ'` is infinite). -/ def tr_stmts {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] {σ : Type u_3} [Inhabited σ] (M : Λ → TM1.stmt Γ Λ σ) [fintype σ] (S : finset Λ) : finset (Λ' M) := finset.product (TM1.stmts M S) finset.univ theorem tr_supports {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] {σ : Type u_3} [Inhabited σ] (M : Λ → TM1.stmt Γ Λ σ) [fintype σ] {S : finset Λ} (ss : TM1.supports M S) : TM0.supports (tr M) ↑(tr_stmts M S) := sorry end TM1to0 /-! ## TM1(Γ) emulator in TM1(bool) The most parsimonious Turing machine model that is still Turing complete is `TM0` with `Γ = bool`. Because our construction in the previous section reducing `TM1` to `TM0` doesn't change the alphabet, we can do the alphabet reduction on `TM1` instead of `TM0` directly. The basic idea is to use a bijection between `Γ` and a subset of `vector bool n`, where `n` is a fixed constant. Each tape element is represented as a block of `n` bools. Whenever the machine wants to read a symbol from the tape, it traverses over the block, performing `n` `branch` instructions to each any of the `2^n` results. For the `write` instruction, we have to use a `goto` because we need to follow a different code path depending on the local state, which is not available in the TM1 model, so instead we jump to a label computed using the read value and the local state, which performs the writing and returns to normal execution. Emulation overhead is `O(1)`. If not for the above `write` behavior it would be 1-1 because we are exploiting the 0-step behavior of regular commands to avoid taking steps, but there are nevertheless a bounded number of `write` calls between `goto` statements because TM1 statements are finitely long. -/ namespace TM1to1 theorem exists_enc_dec {Γ : Type u_1} [Inhabited Γ] [fintype Γ] : ∃ (n : ℕ), ∃ (enc : Γ → vector Bool n), ∃ (dec : vector Bool n → Γ), enc Inhabited.default = vector.repeat false n ∧ ∀ (a : Γ), dec (enc a) = a := sorry /-- The configuration state of the TM. -/ inductive Λ' {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] {σ : Type u_3} [Inhabited σ] where | normal : Λ → Λ' | write : Γ → TM1.stmt Γ Λ σ → Λ' protected instance Λ'.inhabited {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] {σ : Type u_3} [Inhabited σ] : Inhabited Λ' := { default := Λ'.normal Inhabited.default } /-- Read a vector of length `n` from the tape. -/ def read_aux {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] {σ : Type u_3} [Inhabited σ] (n : ℕ) : (vector Bool n → TM1.stmt Bool Λ' σ) → TM1.stmt Bool Λ' σ := sorry /-- A move left or right corresponds to `n` moves across the super-cell. -/ def move {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] {σ : Type u_3} [Inhabited σ] {n : ℕ} (d : dir) (q : TM1.stmt Bool Λ' σ) : TM1.stmt Bool Λ' σ := nat.iterate (TM1.stmt.move d) n q /-- To read a symbol from the tape, we use `read_aux` to traverse the symbol, then return to the original position with `n` moves to the left. -/ def read {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] {σ : Type u_3} [Inhabited σ] {n : ℕ} (dec : vector Bool n → Γ) (f : Γ → TM1.stmt Bool Λ' σ) : TM1.stmt Bool Λ' σ := read_aux n fun (v : vector Bool n) => move dir.left (f (dec v)) /-- Write a list of bools on the tape. -/ def write {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] {σ : Type u_3} [Inhabited σ] : List Bool → TM1.stmt Bool Λ' σ → TM1.stmt Bool Λ' σ := sorry /-- Translate a normal instruction. For the `write` command, we use a `goto` indirection so that we can access the current value of the tape. -/ def tr_normal {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] {σ : Type u_3} [Inhabited σ] {n : ℕ} (dec : vector Bool n → Γ) : TM1.stmt Γ Λ σ → TM1.stmt Bool Λ' σ := sorry theorem step_aux_move {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] {σ : Type u_3} [Inhabited σ] {n : ℕ} (d : dir) (q : TM1.stmt Bool Λ' σ) (v : σ) (T : tape Bool) : TM1.step_aux (move d q) v T = TM1.step_aux q v (nat.iterate (tape.move d) n T) := sorry theorem supports_stmt_move {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] {σ : Type u_3} [Inhabited σ] {n : ℕ} {S : finset Λ'} {d : dir} {q : TM1.stmt Bool Λ' σ} : TM1.supports_stmt S (move d q) = TM1.supports_stmt S q := sorry theorem supports_stmt_write {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] {σ : Type u_3} [Inhabited σ] {S : finset Λ'} {l : List Bool} {q : TM1.stmt Bool Λ' σ} : TM1.supports_stmt S (write l q) = TM1.supports_stmt S q := sorry theorem supports_stmt_read {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] {σ : Type u_3} [Inhabited σ] {n : ℕ} (dec : vector Bool n → Γ) {S : finset Λ'} {f : Γ → TM1.stmt Bool Λ' σ} : (∀ (a : Γ), TM1.supports_stmt S (f a)) → TM1.supports_stmt S (read dec f) := sorry /-- The low level tape corresponding to the given tape over alphabet `Γ`. -/ def tr_tape' {Γ : Type u_1} [Inhabited Γ] {n : ℕ} {enc : Γ → vector Bool n} (enc0 : enc Inhabited.default = vector.repeat false n) (L : list_blank Γ) (R : list_blank Γ) : tape Bool := tape.mk' (list_blank.bind L (fun (x : Γ) => list.reverse (vector.to_list (enc x))) sorry) (list_blank.bind R (fun (x : Γ) => vector.to_list (enc x)) sorry) /-- The low level tape corresponding to the given tape over alphabet `Γ`. -/ def tr_tape {Γ : Type u_1} [Inhabited Γ] {n : ℕ} {enc : Γ → vector Bool n} (enc0 : enc Inhabited.default = vector.repeat false n) (T : tape Γ) : tape Bool := tr_tape' enc0 (tape.left T) (tape.right₀ T) theorem tr_tape_mk' {Γ : Type u_1} [Inhabited Γ] {n : ℕ} {enc : Γ → vector Bool n} (enc0 : enc Inhabited.default = vector.repeat false n) (L : list_blank Γ) (R : list_blank Γ) : tr_tape enc0 (tape.mk' L R) = tr_tape' enc0 L R := sorry /-- The top level program. -/ def tr {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] {σ : Type u_3} [Inhabited σ] {n : ℕ} (enc : Γ → vector Bool n) (dec : vector Bool n → Γ) (M : Λ → TM1.stmt Γ Λ σ) : Λ' → TM1.stmt Bool Λ' σ := sorry /-- The machine configuration translation. -/ def tr_cfg {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] {σ : Type u_3} [Inhabited σ] {n : ℕ} {enc : Γ → vector Bool n} (enc0 : enc Inhabited.default = vector.repeat false n) : TM1.cfg Γ Λ σ → TM1.cfg Bool Λ' σ := sorry theorem tr_tape'_move_left {Γ : Type u_1} [Inhabited Γ] {n : ℕ} {enc : Γ → vector Bool n} (enc0 : enc Inhabited.default = vector.repeat false n) (L : list_blank Γ) (R : list_blank Γ) : nat.iterate (tape.move dir.left) n (tr_tape' enc0 L R) = tr_tape' enc0 (list_blank.tail L) (list_blank.cons (list_blank.head L) R) := sorry theorem tr_tape'_move_right {Γ : Type u_1} [Inhabited Γ] {n : ℕ} {enc : Γ → vector Bool n} (enc0 : enc Inhabited.default = vector.repeat false n) (L : list_blank Γ) (R : list_blank Γ) : nat.iterate (tape.move dir.right) n (tr_tape' enc0 L R) = tr_tape' enc0 (list_blank.cons (list_blank.head R) L) (list_blank.tail R) := sorry theorem step_aux_write {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] {σ : Type u_3} [Inhabited σ] {n : ℕ} {enc : Γ → vector Bool n} (enc0 : enc Inhabited.default = vector.repeat false n) (q : TM1.stmt Bool Λ' σ) (v : σ) (a : Γ) (b : Γ) (L : list_blank Γ) (R : list_blank Γ) : TM1.step_aux (write (vector.to_list (enc a)) q) v (tr_tape' enc0 L (list_blank.cons b R)) = TM1.step_aux q v (tr_tape' enc0 (list_blank.cons a L) R) := sorry theorem step_aux_read {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] {σ : Type u_3} [Inhabited σ] {n : ℕ} {enc : Γ → vector Bool n} (dec : vector Bool n → Γ) (enc0 : enc Inhabited.default = vector.repeat false n) (encdec : ∀ (a : Γ), dec (enc a) = a) (f : Γ → TM1.stmt Bool Λ' σ) (v : σ) (L : list_blank Γ) (R : list_blank Γ) : TM1.step_aux (read dec f) v (tr_tape' enc0 L R) = TM1.step_aux (f (list_blank.head R)) v (tr_tape' enc0 L R) := sorry theorem tr_respects {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] {σ : Type u_3} [Inhabited σ] {n : ℕ} {enc : Γ → vector Bool n} (dec : vector Bool n → Γ) (enc0 : enc Inhabited.default = vector.repeat false n) (M : Λ → TM1.stmt Γ Λ σ) (encdec : ∀ (a : Γ), dec (enc a) = a) : respects (TM1.step M) (TM1.step (tr enc dec M)) fun (c₁ : TM1.cfg Γ Λ σ) (c₂ : TM1.cfg Bool Λ' σ) => tr_cfg enc0 c₁ = c₂ := sorry /-- The set of accessible `Λ'.write` machine states. -/ def writes {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] {σ : Type u_3} [Inhabited σ] [fintype Γ] : TM1.stmt Γ Λ σ → finset Λ' := sorry /-- The set of accessible machine states, assuming that the input machine is supported on `S`, are the normal states embedded from `S`, plus all write states accessible from these states. -/ def tr_supp {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] {σ : Type u_3} [Inhabited σ] (M : Λ → TM1.stmt Γ Λ σ) [fintype Γ] (S : finset Λ) : finset Λ' := finset.bUnion S fun (l : Λ) => insert (Λ'.normal l) (writes (M l)) theorem tr_supports {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] {σ : Type u_3} [Inhabited σ] {n : ℕ} {enc : Γ → vector Bool n} (dec : vector Bool n → Γ) (M : Λ → TM1.stmt Γ Λ σ) [fintype Γ] {S : finset Λ} (ss : TM1.supports M S) : TM1.supports (tr enc dec M) (tr_supp M S) := sorry end TM1to1 /-! ## TM0 emulator in TM1 To establish that TM0 and TM1 are equivalent computational models, we must also have a TM0 emulator in TM1. The main complication here is that TM0 allows an action to depend on the value at the head and local state, while TM1 doesn't (in order to have more programming language-like semantics). So we use a computed `goto` to go to a state that performes the desired action and then returns to normal execution. One issue with this is that the `halt` instruction is supposed to halt immediately, not take a step to a halting state. To resolve this we do a check for `halt` first, then `goto` (with an unreachable branch). -/ namespace TM0to1 /-- The machine states for a TM1 emulating a TM0 machine. States of the TM0 machine are embedded as `normal q` states, but the actual operation is split into two parts, a jump to `act s q` followed by the action and a jump to the next `normal` state. -/ inductive Λ' {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] where | normal : Λ → Λ' | act : TM0.stmt Γ → Λ → Λ' protected instance Λ'.inhabited {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] : Inhabited Λ' := { default := Λ'.normal Inhabited.default } /-- The program. -/ def tr {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] (M : TM0.machine Γ Λ) : Λ' → TM1.stmt Γ Λ' Unit := sorry /-- The configuration translation. -/ def tr_cfg {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] (M : TM0.machine Γ Λ) : TM0.cfg Γ Λ → TM1.cfg Γ Λ' Unit := sorry theorem tr_respects {Γ : Type u_1} [Inhabited Γ] {Λ : Type u_2} [Inhabited Λ] (M : TM0.machine Γ Λ) : respects (TM0.step M) (TM1.step (tr M)) fun (a : TM0.cfg Γ Λ) (b : TM1.cfg Γ Λ' Unit) => tr_cfg M a = b := sorry end TM0to1 /-! ## The TM2 model The TM2 model removes the tape entirely from the TM1 model, replacing it with an arbitrary (finite) collection of stacks, each with elements of different types (the alphabet of stack `k : K` is `Γ k`). The statements are: * `push k (f : σ → Γ k) q` puts `f a` on the `k`-th stack, then does `q`. * `pop k (f : σ → option (Γ k) → σ) q` changes the state to `f a (S k).head`, where `S k` is the value of the `k`-th stack, and removes this element from the stack, then does `q`. * `peek k (f : σ → option (Γ k) → σ) q` changes the state to `f a (S k).head`, where `S k` is the value of the `k`-th stack, then does `q`. * `load (f : σ → σ) q` reads nothing but applies `f` to the internal state, then does `q`. * `branch (f : σ → bool) qtrue qfalse` does `qtrue` or `qfalse` according to `f a`. * `goto (f : σ → Λ)` jumps to label `f a`. * `halt` halts on the next step. The configuration is a tuple `(l, var, stk)` where `l : option Λ` is the current label to run or `none` for the halting state, `var : σ` is the (finite) internal state, and `stk : ∀ k, list (Γ k)` is the collection of stacks. (Note that unlike the `TM0` and `TM1` models, these are not `list_blank`s, they have definite ends that can be detected by the `pop` command.) Given a designated stack `k` and a value `L : list (Γ k)`, the initial configuration has all the stacks empty except the designated "input" stack; in `eval` this designated stack also functions as the output stack. -/ namespace TM2 /-- The TM2 model removes the tape entirely from the TM1 model, replacing it with an arbitrary (finite) collection of stacks. The operation `push` puts an element on one of the stacks, and `pop` removes an element from a stack (and modifying the internal state based on the result). `peek` modifies the internal state but does not remove an element. -/ inductive stmt {K : Type u_1} [DecidableEq K] (Γ : K → Type u_2) (Λ : Type u_3) (σ : Type u_4) where | push : (k : K) → (σ → Γ k) → stmt Γ Λ σ → stmt Γ Λ σ | peek : (k : K) → (σ → Option (Γ k) → σ) → stmt Γ Λ σ → stmt Γ Λ σ | pop : (k : K) → (σ → Option (Γ k) → σ) → stmt Γ Λ σ → stmt Γ Λ σ | load : (σ → σ) → stmt Γ Λ σ → stmt Γ Λ σ | branch : (σ → Bool) → stmt Γ Λ σ → stmt Γ Λ σ → stmt Γ Λ σ | goto : (σ → Λ) → stmt Γ Λ σ | halt : stmt Γ Λ σ protected instance stmt.inhabited {K : Type u_1} [DecidableEq K] (Γ : K → Type u_2) (Λ : Type u_3) (σ : Type u_4) : Inhabited (stmt Γ Λ σ) := { default := stmt.halt } /-- A configuration in the TM2 model is a label (or `none` for the halt state), the state of local variables, and the stacks. (Note that the stacks are not `list_blank`s, they have a definite size.) -/ structure cfg {K : Type u_1} [DecidableEq K] (Γ : K → Type u_2) (Λ : Type u_3) (σ : Type u_4) where l : Option Λ var : σ stk : (k : K) → List (Γ k) protected instance cfg.inhabited {K : Type u_1} [DecidableEq K] (Γ : K → Type u_2) (Λ : Type u_3) (σ : Type u_4) [Inhabited σ] : Inhabited (cfg Γ Λ σ) := { default := cfg.mk Inhabited.default Inhabited.default Inhabited.default } /-- The step function for the TM2 model. -/ @[simp] def step_aux {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} {σ : Type u_4} : stmt Γ Λ σ → σ → ((k : K) → List (Γ k)) → cfg Γ Λ σ := sorry /-- The step function for the TM2 model. -/ @[simp] def step {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} {σ : Type u_4} (M : Λ → stmt Γ Λ σ) : cfg Γ Λ σ → Option (cfg Γ Λ σ) := sorry /-- The (reflexive) reachability relation for the TM2 model. -/ def reaches {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} {σ : Type u_4} (M : Λ → stmt Γ Λ σ) : cfg Γ Λ σ → cfg Γ Λ σ → Prop := relation.refl_trans_gen fun (a b : cfg Γ Λ σ) => b ∈ step M a /-- Given a set `S` of states, `support_stmt S q` means that `q` only jumps to states in `S`. -/ def supports_stmt {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} {σ : Type u_4} (S : finset Λ) : stmt Γ Λ σ → Prop := sorry /-- The set of subtree statements in a statement. -/ def stmts₁ {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} {σ : Type u_4} : stmt Γ Λ σ → finset (stmt Γ Λ σ) := sorry theorem stmts₁_self {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} {σ : Type u_4} {q : stmt Γ Λ σ} : q ∈ stmts₁ q := sorry theorem stmts₁_trans {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} {σ : Type u_4} {q₁ : stmt Γ Λ σ} {q₂ : stmt Γ Λ σ} : q₁ ∈ stmts₁ q₂ → stmts₁ q₁ ⊆ stmts₁ q₂ := sorry theorem stmts₁_supports_stmt_mono {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} {σ : Type u_4} {S : finset Λ} {q₁ : stmt Γ Λ σ} {q₂ : stmt Γ Λ σ} (h : q₁ ∈ stmts₁ q₂) (hs : supports_stmt S q₂) : supports_stmt S q₁ := sorry /-- The set of statements accessible from initial set `S` of labels. -/ def stmts {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} {σ : Type u_4} (M : Λ → stmt Γ Λ σ) (S : finset Λ) : finset (Option (stmt Γ Λ σ)) := finset.insert_none (finset.bUnion S fun (q : Λ) => stmts₁ (M q)) theorem stmts_trans {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} {σ : Type u_4} {M : Λ → stmt Γ Λ σ} {S : finset Λ} {q₁ : stmt Γ Λ σ} {q₂ : stmt Γ Λ σ} (h₁ : q₁ ∈ stmts₁ q₂) : some q₂ ∈ stmts M S → some q₁ ∈ stmts M S := sorry /-- Given a TM2 machine `M` and a set `S` of states, `supports M S` means that all states in `S` jump only to other states in `S`. -/ def supports {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} {σ : Type u_4} [Inhabited Λ] (M : Λ → stmt Γ Λ σ) (S : finset Λ) := Inhabited.default ∈ S ∧ ∀ (q : Λ), q ∈ S → supports_stmt S (M q) theorem stmts_supports_stmt {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} {σ : Type u_4} [Inhabited Λ] {M : Λ → stmt Γ Λ σ} {S : finset Λ} {q : stmt Γ Λ σ} (ss : supports M S) : some q ∈ stmts M S → supports_stmt S q := sorry theorem step_supports {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} {σ : Type u_4} [Inhabited Λ] (M : Λ → stmt Γ Λ σ) {S : finset Λ} (ss : supports M S) {c : cfg Γ Λ σ} {c' : cfg Γ Λ σ} : c' ∈ step M c → cfg.l c ∈ finset.insert_none S → cfg.l c' ∈ finset.insert_none S := sorry /-- The initial state of the TM2 model. The input is provided on a designated stack. -/ def init {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} {σ : Type u_4} [Inhabited Λ] [Inhabited σ] (k : K) (L : List (Γ k)) : cfg Γ Λ σ := cfg.mk (some Inhabited.default) Inhabited.default (function.update (fun (_x : K) => []) k L) /-- Evaluates a TM2 program to completion, with the output on the same stack as the input. -/ def eval {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} {σ : Type u_4} [Inhabited Λ] [Inhabited σ] (M : Λ → stmt Γ Λ σ) (k : K) (L : List (Γ k)) : roption (List (Γ k)) := roption.map (fun (c : cfg Γ Λ σ) => cfg.stk c k) (eval (step M) (init k L)) end TM2 /-! ## TM2 emulator in TM1 To prove that TM2 computable functions are TM1 computable, we need to reduce each TM2 program to a TM1 program. So suppose a TM2 program is given. This program has to maintain a whole collection of stacks, but we have only one tape, so we must "multiplex" them all together. Pictorially, if stack 1 contains `[a, b]` and stack 2 contains `[c, d, e, f]` then the tape looks like this: ``` bottom: ... | _ | T | _ | _ | _ | _ | ... stack 1: ... | _ | b | a | _ | _ | _ | ... stack 2: ... | _ | f | e | d | c | _ | ... ``` where a tape element is a vertical slice through the diagram. Here the alphabet is `Γ' := bool × ∀ k, option (Γ k)`, where: * `bottom : bool` is marked only in one place, the initial position of the TM, and represents the tail of all stacks. It is never modified. * `stk k : option (Γ k)` is the value of the `k`-th stack, if in range, otherwise `none` (which is the blank value). Note that the head of the stack is at the far end; this is so that push and pop don't have to do any shifting. In "resting" position, the TM is sitting at the position marked `bottom`. For non-stack actions, it operates in place, but for the stack actions `push`, `peek`, and `pop`, it must shuttle to the end of the appropriate stack, make its changes, and then return to the bottom. So the states are: * `normal (l : Λ)`: waiting at `bottom` to execute function `l` * `go k (s : st_act k) (q : stmt₂)`: travelling to the right to get to the end of stack `k` in order to perform stack action `s`, and later continue with executing `q` * `ret (q : stmt₂)`: travelling to the left after having performed a stack action, and executing `q` once we arrive Because of the shuttling, emulation overhead is `O(n)`, where `n` is the current maximum of the length of all stacks. Therefore a program that takes `k` steps to run in TM2 takes `O((m+k)k)` steps to run when emulated in TM1, where `m` is the length of the input. -/ namespace TM2to1 -- A displaced lemma proved in unnecessary generality theorem stk_nth_val {K : Type u_1} {Γ : K → Type u_2} {L : list_blank ((k : K) → Option (Γ k))} {k : K} {S : List (Γ k)} (n : ℕ) (hL : list_blank.map (proj k) L = list_blank.mk (list.reverse (list.map some S))) : list_blank.nth L n k = list.nth (list.reverse S) n := sorry /-- The alphabet of the TM2 simulator on TM1 is a marker for the stack bottom, plus a vector of stack elements for each stack, or none if the stack does not extend this far. -/ -- the decidable_eq assumption, and this is a local definition anyway so it's not important. def Γ' {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} := Bool × ((k : K) → Option (Γ k)) protected instance Γ'.inhabited {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} : Inhabited Γ' := { default := (false, fun (_x : K) => none) } protected instance Γ'.fintype {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} [fintype K] [(k : K) → fintype (Γ k)] : fintype Γ' := prod.fintype Bool ((k : K) → Option (Γ k)) /-- The bottom marker is fixed throughout the calculation, so we use the `add_bottom` function to express the program state in terms of a tape with only the stacks themselves. -/ def add_bottom {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} (L : list_blank ((k : K) → Option (Γ k))) : list_blank Γ' := list_blank.cons (tt, list_blank.head L) (list_blank.map (pointed_map.mk (Prod.mk false) sorry) (list_blank.tail L)) theorem add_bottom_map {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} (L : list_blank ((k : K) → Option (Γ k))) : list_blank.map (pointed_map.mk prod.snd rfl) (add_bottom L) = L := sorry theorem add_bottom_modify_nth {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} (f : ((k : K) → Option (Γ k)) → (k : K) → Option (Γ k)) (L : list_blank ((k : K) → Option (Γ k))) (n : ℕ) : list_blank.modify_nth (fun (a : Γ') => (prod.fst a, f (prod.snd a))) n (add_bottom L) = add_bottom (list_blank.modify_nth f n L) := sorry theorem add_bottom_nth_snd {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} (L : list_blank ((k : K) → Option (Γ k))) (n : ℕ) : prod.snd (list_blank.nth (add_bottom L) n) = list_blank.nth L n := sorry theorem add_bottom_nth_succ_fst {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} (L : list_blank ((k : K) → Option (Γ k))) (n : ℕ) : prod.fst (list_blank.nth (add_bottom L) (n + 1)) = false := sorry theorem add_bottom_head_fst {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} (L : list_blank ((k : K) → Option (Γ k))) : prod.fst (list_blank.head (add_bottom L)) = tt := sorry /-- A stack action is a command that interacts with the top of a stack. Our default position is at the bottom of all the stacks, so we have to hold on to this action while going to the end to modify the stack. -/ inductive st_act {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {σ : Type u_4} [Inhabited σ] (k : K) where | push : (σ → Γ k) → st_act k | peek : (σ → Option (Γ k) → σ) → st_act k | pop : (σ → Option (Γ k) → σ) → st_act k protected instance st_act.inhabited {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {σ : Type u_4} [Inhabited σ] {k : K} : Inhabited (st_act k) := { default := st_act.peek fun (s : σ) (_x : Option (Γ k)) => s } /-- The TM2 statement corresponding to a stack action. -/ -- it is worth to omit the typeclass assumption without breaking the parameters def st_run {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} [Inhabited Λ] {σ : Type u_4} [Inhabited σ] {k : K} : st_act k → TM2.stmt Γ Λ σ → TM2.stmt Γ Λ σ := sorry /-- The effect of a stack action on the local variables, given the value of the stack. -/ def st_var {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {σ : Type u_4} [Inhabited σ] {k : K} (v : σ) (l : List (Γ k)) : st_act k → σ := sorry /-- The effect of a stack action on the stack. -/ def st_write {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {σ : Type u_4} [Inhabited σ] {k : K} (v : σ) (l : List (Γ k)) : st_act k → List (Γ k) := sorry /-- We have partitioned the TM2 statements into "stack actions", which require going to the end of the stack, and all other actions, which do not. This is a modified recursor which lumps the stack actions into one. -/ def stmt_st_rec {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} [Inhabited Λ] {σ : Type u_4} [Inhabited σ] {C : TM2.stmt Γ Λ σ → Sort l} (H₁ : (k : K) → (s : st_act k) → (q : TM2.stmt Γ Λ σ) → C q → C (st_run s q)) (H₂ : (a : σ → σ) → (q : TM2.stmt Γ Λ σ) → C q → C (TM2.stmt.load a q)) (H₃ : (p : σ → Bool) → (q₁ q₂ : TM2.stmt Γ Λ σ) → C q₁ → C q₂ → C (TM2.stmt.branch p q₁ q₂)) (H₄ : (l : σ → Λ) → C (TM2.stmt.goto l)) (H₅ : C TM2.stmt.halt) (n : TM2.stmt Γ Λ σ) : C n := sorry theorem supports_run {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} [Inhabited Λ] {σ : Type u_4} [Inhabited σ] (S : finset Λ) {k : K} (s : st_act k) (q : TM2.stmt Γ Λ σ) : TM2.supports_stmt S (st_run s q) ↔ TM2.supports_stmt S q := st_act.cases_on s (fun (s : σ → Γ k) => iff.refl (TM2.supports_stmt S (st_run (st_act.push s) q))) (fun (s : σ → Option (Γ k) → σ) => iff.refl (TM2.supports_stmt S (st_run (st_act.peek s) q))) fun (s : σ → Option (Γ k) → σ) => iff.refl (TM2.supports_stmt S (st_run (st_act.pop s) q)) /-- The machine states of the TM2 emulator. We can either be in a normal state when waiting for the next TM2 action, or we can be in the "go" and "return" states to go to the top of the stack and return to the bottom, respectively. -/ inductive Λ' {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} [Inhabited Λ] {σ : Type u_4} [Inhabited σ] where | normal : Λ → Λ' | go : (k : K) → st_act k → TM2.stmt Γ Λ σ → Λ' | ret : TM2.stmt Γ Λ σ → Λ' protected instance Λ'.inhabited {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} [Inhabited Λ] {σ : Type u_4} [Inhabited σ] : Inhabited Λ' := { default := Λ'.normal Inhabited.default } /-- The program corresponding to state transitions at the end of a stack. Here we start out just after the top of the stack, and should end just after the new top of the stack. -/ def tr_st_act {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} [Inhabited Λ] {σ : Type u_4} [Inhabited σ] {k : K} (q : TM1.stmt Γ' Λ' σ) : st_act k → TM1.stmt Γ' Λ' σ := sorry /-- The initial state for the TM2 emulator, given an initial TM2 state. All stacks start out empty except for the input stack, and the stack bottom mark is set at the head. -/ def tr_init {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} (k : K) (L : List (Γ k)) : List Γ' := let L' : List Γ' := list.map (fun (a : Γ k) => (false, function.update (fun (_x : K) => none) k ↑a)) (list.reverse L); (tt, prod.snd (list.head L')) :: list.tail L' theorem step_run {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} [Inhabited Λ] {σ : Type u_4} [Inhabited σ] {k : K} (q : TM2.stmt Γ Λ σ) (v : σ) (S : (k : K) → List (Γ k)) (s : st_act k) : TM2.step_aux (st_run s q) v S = TM2.step_aux q (st_var v (S k) s) (function.update S k (st_write v (S k) s)) := sorry /-- The translation of TM2 statements to TM1 statements. regular actions have direct equivalents, but stack actions are deferred by going to the corresponding `go` state, so that we can find the appropriate stack top. -/ def tr_normal {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} [Inhabited Λ] {σ : Type u_4} [Inhabited σ] : TM2.stmt Γ Λ σ → TM1.stmt Γ' Λ' σ := sorry theorem tr_normal_run {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} [Inhabited Λ] {σ : Type u_4} [Inhabited σ] {k : K} (s : st_act k) (q : TM2.stmt Γ Λ σ) : tr_normal (st_run s q) = TM1.stmt.goto fun (_x : Γ') (_x : σ) => Λ'.go k s q := st_act.cases_on s (fun (s : σ → Γ k) => Eq.refl (tr_normal (st_run (st_act.push s) q))) (fun (s : σ → Option (Γ k) → σ) => Eq.refl (tr_normal (st_run (st_act.peek s) q))) fun (s : σ → Option (Γ k) → σ) => Eq.refl (tr_normal (st_run (st_act.pop s) q)) /-- The set of machine states accessible from an initial TM2 statement. -/ def tr_stmts₁ {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} [Inhabited Λ] {σ : Type u_4} [Inhabited σ] : TM2.stmt Γ Λ σ → finset Λ' := sorry theorem tr_stmts₁_run {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} [Inhabited Λ] {σ : Type u_4} [Inhabited σ] {k : K} {s : st_act k} {q : TM2.stmt Γ Λ σ} : tr_stmts₁ (st_run s q) = insert (Λ'.go k s q) (singleton (Λ'.ret q)) ∪ tr_stmts₁ q := sorry theorem tr_respects_aux₂ {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} [Inhabited Λ] {σ : Type u_4} [Inhabited σ] {k : K} {q : TM1.stmt Γ' Λ' σ} {v : σ} {S : (k : K) → List (Γ k)} {L : list_blank ((k : K) → Option (Γ k))} (hL : ∀ (k : K), list_blank.map (proj k) L = list_blank.mk (list.reverse (list.map some (S k)))) (o : st_act k) : let v' : σ := st_var v (S k) o; let Sk' : List (Γ k) := st_write v (S k) o; let S' : (k : K) → List (Γ k) := function.update S k Sk'; ∃ (L' : list_blank ((k : K) → Option (Γ k))), (∀ (k : K), list_blank.map (proj k) L' = list_blank.mk (list.reverse (list.map some (S' k)))) ∧ TM1.step_aux (tr_st_act q o) v (nat.iterate (tape.move dir.right) (list.length (S k)) (tape.mk' ∅ (add_bottom L))) = TM1.step_aux q v' (nat.iterate (tape.move dir.right) (list.length (S' k)) (tape.mk' ∅ (add_bottom L'))) := sorry /-- The TM2 emulator machine states written as a TM1 program. This handles the `go` and `ret` states, which shuttle to and from a stack top. -/ def tr {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} [Inhabited Λ] {σ : Type u_4} [Inhabited σ] (M : Λ → TM2.stmt Γ Λ σ) : Λ' → TM1.stmt Γ' Λ' σ := sorry /-- The relation between TM2 configurations and TM1 configurations of the TM2 emulator. -/ inductive tr_cfg {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} [Inhabited Λ] {σ : Type u_4} [Inhabited σ] (M : Λ → TM2.stmt Γ Λ σ) : TM2.cfg Γ Λ σ → TM1.cfg Γ' Λ' σ → Prop where | mk : ∀ {q : Option Λ} {v : σ} {S : (k : K) → List (Γ k)} (L : list_blank ((k : K) → Option (Γ k))), (∀ (k : K), list_blank.map (proj k) L = list_blank.mk (list.reverse (list.map some (S k)))) → tr_cfg M (TM2.cfg.mk q v S) (TM1.cfg.mk (option.map Λ'.normal q) v (tape.mk' ∅ (add_bottom L))) theorem tr_respects_aux₁ {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} [Inhabited Λ] {σ : Type u_4} [Inhabited σ] (M : Λ → TM2.stmt Γ Λ σ) {k : K} (o : st_act k) (q : TM2.stmt Γ Λ σ) (v : σ) {S : List (Γ k)} {L : list_blank ((k : K) → Option (Γ k))} (hL : list_blank.map (proj k) L = list_blank.mk (list.reverse (list.map some S))) (n : ℕ) (H : n ≤ list.length S) : reaches₀ (TM1.step (tr M)) (TM1.cfg.mk (some (Λ'.go k o q)) v (tape.mk' ∅ (add_bottom L))) (TM1.cfg.mk (some (Λ'.go k o q)) v (nat.iterate (tape.move dir.right) n (tape.mk' ∅ (add_bottom L)))) := sorry theorem tr_respects_aux₃ {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} [Inhabited Λ] {σ : Type u_4} [Inhabited σ] (M : Λ → TM2.stmt Γ Λ σ) {q : TM2.stmt Γ Λ σ} {v : σ} {L : list_blank ((k : K) → Option (Γ k))} (n : ℕ) : reaches₀ (TM1.step (tr M)) (TM1.cfg.mk (some (Λ'.ret q)) v (nat.iterate (tape.move dir.right) n (tape.mk' ∅ (add_bottom L)))) (TM1.cfg.mk (some (Λ'.ret q)) v (tape.mk' ∅ (add_bottom L))) := sorry theorem tr_respects_aux {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} [Inhabited Λ] {σ : Type u_4} [Inhabited σ] (M : Λ → TM2.stmt Γ Λ σ) {q : TM2.stmt Γ Λ σ} {v : σ} {T : list_blank ((i : K) → Option (Γ i))} {k : K} {S : (k : K) → List (Γ k)} (hT : ∀ (k : K), list_blank.map (proj k) T = list_blank.mk (list.reverse (list.map some (S k)))) (o : st_act k) (IH : ∀ {v : σ} {S : (k : K) → List (Γ k)} {T : list_blank ((i : K) → Option (Γ i))}, (∀ (k : K), list_blank.map (proj k) T = list_blank.mk (list.reverse (list.map some (S k)))) → ∃ (b : TM1.cfg Γ' Λ' σ), tr_cfg M (TM2.step_aux q v S) b ∧ reaches (TM1.step (tr M)) (TM1.step_aux (tr_normal q) v (tape.mk' ∅ (add_bottom T))) b) : ∃ (b : TM1.cfg Γ' Λ' σ), tr_cfg M (TM2.step_aux (st_run o q) v S) b ∧ reaches (TM1.step (tr M)) (TM1.step_aux (tr_normal (st_run o q)) v (tape.mk' ∅ (add_bottom T))) b := sorry theorem tr_respects {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} [Inhabited Λ] {σ : Type u_4} [Inhabited σ] (M : Λ → TM2.stmt Γ Λ σ) : respects (TM2.step M) (TM1.step (tr M)) (tr_cfg M) := sorry theorem tr_cfg_init {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} [Inhabited Λ] {σ : Type u_4} [Inhabited σ] (M : Λ → TM2.stmt Γ Λ σ) (k : K) (L : List (Γ k)) : tr_cfg M (TM2.init k L) (TM1.init (tr_init k L)) := sorry theorem tr_eval_dom {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} [Inhabited Λ] {σ : Type u_4} [Inhabited σ] (M : Λ → TM2.stmt Γ Λ σ) (k : K) (L : List (Γ k)) : roption.dom (TM1.eval (tr M) (tr_init k L)) ↔ roption.dom (TM2.eval M k L) := tr_eval_dom (tr_respects M) (tr_cfg_init M k L) theorem tr_eval {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} [Inhabited Λ] {σ : Type u_4} [Inhabited σ] (M : Λ → TM2.stmt Γ Λ σ) (k : K) (L : List (Γ k)) {L₁ : list_blank Γ'} {L₂ : List (Γ k)} (H₁ : L₁ ∈ TM1.eval (tr M) (tr_init k L)) (H₂ : L₂ ∈ TM2.eval M k L) : ∃ (S : (k : K) → List (Γ k)), ∃ (L' : list_blank ((k : K) → Option (Γ k))), add_bottom L' = L₁ ∧ (∀ (k : K), list_blank.map (proj k) L' = list_blank.mk (list.reverse (list.map some (S k)))) ∧ S k = L₂ := sorry /-- The support of a set of TM2 states in the TM2 emulator. -/ def tr_supp {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} [Inhabited Λ] {σ : Type u_4} [Inhabited σ] (M : Λ → TM2.stmt Γ Λ σ) (S : finset Λ) : finset Λ' := finset.bUnion S fun (l : Λ) => insert (Λ'.normal l) (tr_stmts₁ (M l)) theorem tr_supports {K : Type u_1} [DecidableEq K] {Γ : K → Type u_2} {Λ : Type u_3} [Inhabited Λ] {σ : Type u_4} [Inhabited σ] (M : Λ → TM2.stmt Γ Λ σ) {S : finset Λ} (ss : TM2.supports M S) : TM1.supports (tr M) (tr_supp M S) := sorry
6e229f5e034747d29da9920ff4dd0f9fb8251f3c
f3a5af2927397cf346ec0e24312bfff077f00425
/src/game/world10/level18q.lean
badb4128345cff8f9ca40f10d7ed9bed42a412c5
[ "Apache-2.0" ]
permissive
ImperialCollegeLondon/natural_number_game
05c39e1586408cfb563d1a12e1085a90726ab655
f29b6c2884299fc63fdfc81ae5d7daaa3219f9fd
refs/heads/master
1,688,570,964,990
1,636,908,242,000
1,636,908,242,000
195,403,790
277
84
Apache-2.0
1,694,547,955,000
1,562,328,792,000
Lean
UTF-8
Lean
false
false
4,070
lean
import game.world10.level17 -- every level of the natural number game on the web import game.world3.level7 -- semiring instance namespace mynat /- Here are the remaining lt (less than) levels. -/ lemma lt_irrefl (a : mynat) : ¬ (a < a) := begin [nat_num_game] sorry end lemma ne_of_lt {a b : mynat} : a < b → a ≠ b := begin [nat_num_game] sorry end theorem not_lt_zero (a : mynat) : ¬(a < 0) := begin [nat_num_game] sorry end theorem lt_of_lt_of_le {a b c : mynat} : a < b → b ≤ c → a < c := begin sorry end theorem lt_of_le_of_lt {a b c : mynat} : a ≤ b → b < c → a < c := begin [nat_num_game] sorry end theorem lt_trans (a b c : mynat) : a < b → b < c → a < c := begin [nat_num_game] sorry end theorem lt_iff_le_and_ne (a b : mynat) : a < b ↔ a ≤ b ∧ a ≠ b := begin [nat_num_game] sorry end theorem lt_succ_self (n : mynat) : n < succ n := begin [nat_num_game] sorry end -- This one isn't about < but it's convenient for the next level lemma succ_le_succ_iff (m n : mynat) : succ m ≤ succ n ↔ m ≤ n := begin [nat_num_game] sorry end lemma lt_succ_iff_le (m n : mynat) : m < succ n ↔ m ≤ n := begin [nat_num_game] sorry end -- note: needs add_left_cancel but otherwise is easy. lemma le_of_add_le_add_left (a b c : mynat) : a + b ≤ a + c → b ≤ c := begin [nat_num_game] sorry end lemma lt_of_add_lt_add_left (a b c : mynat) : a + b < a + c → b < c := begin [nat_num_game] sorry end lemma add_lt_add_right (a b : mynat) : a < b → ∀ c : mynat, a + c < b + c := begin [nat_num_game] sorry end -- and now we get three achievements! instance : ordered_comm_monoid mynat := { add_le_add_left := λ _ _, add_le_add_left, lt_of_add_lt_add_left := lt_of_add_lt_add_left, ..mynat.add_comm_monoid, ..mynat.partial_order} instance : canonically_ordered_monoid mynat := { le_iff_exists_add := le_iff_exists_add, bot := 0, bot_le := zero_le, ..mynat.ordered_comm_monoid, } instance : ordered_cancel_comm_monoid mynat := { add_left_cancel := add_left_cancel, add_right_cancel := add_right_cancel, le_of_add_le_add_left := le_of_add_le_add_left, ..mynat.ordered_comm_monoid} -- But these are all about the relation between < and +; we now need to -- understand the difference between < and *. def succ_lt_succ_iff (a b : mynat) : succ a < succ b ↔ a < b := begin [nat_num_game] sorry end -- multiplication theorem mul_le_mul_of_nonneg_left (a b c : mynat) : a ≤ b → 0 ≤ c → c * a ≤ c * b := begin [nat_num_game] sorry end theorem mul_le_mul_of_nonneg_right (a b c : mynat) : a ≤ b → 0 ≤ c → a * c ≤ b * c := begin [nat_num_game] sorry end -- this is long theorem mul_lt_mul_of_pos_left (a b c : mynat) : a < b → 0 < c → c * a < c * b := begin [nat_num_game] sorry end theorem mul_lt_mul_of_pos_right (a b c : mynat) : a < b → 0 < c → a * c < b * c := begin [nat_num_game] sorry end -- And now another achievement! The naturals are an ordered semiring. instance : ordered_semiring mynat := { mul_le_mul_of_nonneg_left := mul_le_mul_of_nonneg_left, mul_le_mul_of_nonneg_right := mul_le_mul_of_nonneg_right, mul_lt_mul_of_pos_left := mul_lt_mul_of_pos_left, mul_lt_mul_of_pos_right := mul_lt_mul_of_pos_right, ..mynat.semiring, ..mynat.ordered_cancel_comm_monoid } -- a couple more bits and bobs just for fun lemma le_mul (a b c d : mynat) : a ≤ b → c ≤ d → a * c ≤ b * d := begin [nat_num_game] sorry end lemma pow_le (m n a : mynat) : m ≤ n → m ^ a ≤ n ^ a := begin [nat_num_game] sorry end -- Now the boss level: prove strong induction! -- The trick is to prove this first. lemma strong_induction_aux (P : mynat → Prop) (IH : ∀ m : mynat, (∀ b : mynat, b < m → P b) → P m) (n : mynat) : ∀ c < n, P c := begin [nat_num_game] sorry end -- Now strong induction should be easy. @[elab_as_eliminator] theorem strong_induction (P : mynat → Prop) (IH : ∀ m : mynat, (∀ d : mynat, d < m → P d) → P m) : ∀ n, P n := begin [nat_num_game] sorry end end mynat
ae85627f650a4dd07de6ac3e732108414044b268
c777c32c8e484e195053731103c5e52af26a25d1
/src/linear_algebra/annihilating_polynomial.lean
784fee1a4b31bf16ab66728efb3ec11c5ee2e223
[ "Apache-2.0" ]
permissive
kbuzzard/mathlib
2ff9e85dfe2a46f4b291927f983afec17e946eb8
58537299e922f9c77df76cb613910914a479c1f7
refs/heads/master
1,685,313,702,744
1,683,974,212,000
1,683,974,212,000
128,185,277
1
0
null
1,522,920,600,000
1,522,920,600,000
null
UTF-8
Lean
false
false
6,632
lean
/- Copyright (c) 2022 Justin Thomas. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Justin Thomas -/ import field_theory.minpoly.field import ring_theory.principal_ideal_domain /-! # Annihilating Ideal Given a commutative ring `R` and an `R`-algebra `A` Every element `a : A` defines an ideal `polynomial.ann_ideal a ⊆ R[X]`. Simply put, this is the set of polynomials `p` where the polynomial evaluation `p(a)` is 0. ## Special case where the ground ring is a field In the special case that `R` is a field, we use the notation `R = 𝕜`. Here `𝕜[X]` is a PID, so there is a polynomial `g ∈ polynomial.ann_ideal a` which generates the ideal. We show that if this generator is chosen to be monic, then it is the minimal polynomial of `a`, as defined in `field_theory.minpoly`. ## Special case: endomorphism algebra Given an `R`-module `M` (`[add_comm_group M] [module R M]`) there are some common specializations which may be more familiar. * Example 1: `A = M →ₗ[R] M`, the endomorphism algebra of an `R`-module M. * Example 2: `A = n × n` matrices with entries in `R`. -/ open_locale polynomial namespace polynomial section semiring variables {R A : Type*} [comm_semiring R] [semiring A] [algebra R A] variables (R) /-- `ann_ideal R a` is the *annihilating ideal* of all `p : R[X]` such that `p(a) = 0`. The informal notation `p(a)` stand for `polynomial.aeval a p`. Again informally, the annihilating ideal of `a` is `{ p ∈ R[X] | p(a) = 0 }`. This is an ideal in `R[X]`. The formal definition uses the kernel of the aeval map. -/ noncomputable def ann_ideal (a : A) : ideal R[X] := ((aeval a).to_ring_hom : R[X] →+* A).ker variables {R} /-- It is useful to refer to ideal membership sometimes and the annihilation condition other times. -/ lemma mem_ann_ideal_iff_aeval_eq_zero {a : A} {p : R[X]} : p ∈ ann_ideal R a ↔ aeval a p = 0 := iff.rfl end semiring section field variables {𝕜 A : Type*} [field 𝕜] [ring A] [algebra 𝕜 A] variable (𝕜) open submodule /-- `ann_ideal_generator 𝕜 a` is the monic generator of `ann_ideal 𝕜 a` if one exists, otherwise `0`. Since `𝕜[X]` is a principal ideal domain there is a polynomial `g` such that `span 𝕜 {g} = ann_ideal a`. This picks some generator. We prefer the monic generator of the ideal. -/ noncomputable def ann_ideal_generator (a : A) : 𝕜[X] := let g := is_principal.generator $ ann_ideal 𝕜 a in g * (C g.leading_coeff⁻¹) section variables {𝕜} @[simp] lemma ann_ideal_generator_eq_zero_iff {a : A} : ann_ideal_generator 𝕜 a = 0 ↔ ann_ideal 𝕜 a = ⊥ := by simp only [ann_ideal_generator, mul_eq_zero, is_principal.eq_bot_iff_generator_eq_zero, polynomial.C_eq_zero, inv_eq_zero, polynomial.leading_coeff_eq_zero, or_self] end /-- `ann_ideal_generator 𝕜 a` is indeed a generator. -/ @[simp] lemma span_singleton_ann_ideal_generator (a : A) : ideal.span {ann_ideal_generator 𝕜 a} = ann_ideal 𝕜 a := begin by_cases h : ann_ideal_generator 𝕜 a = 0, { rw [h, ann_ideal_generator_eq_zero_iff.mp h, set.singleton_zero, ideal.span_zero] }, { rw [ann_ideal_generator, ideal.span_singleton_mul_right_unit, ideal.span_singleton_generator], apply polynomial.is_unit_C.mpr, apply is_unit.mk0, apply inv_eq_zero.not.mpr, apply polynomial.leading_coeff_eq_zero.not.mpr, apply (mul_ne_zero_iff.mp h).1 } end /-- The annihilating ideal generator is a member of the annihilating ideal. -/ lemma ann_ideal_generator_mem (a : A) : ann_ideal_generator 𝕜 a ∈ ann_ideal 𝕜 a := ideal.mul_mem_right _ _ (submodule.is_principal.generator_mem _) lemma mem_iff_eq_smul_ann_ideal_generator {p : 𝕜[X]} (a : A) : p ∈ ann_ideal 𝕜 a ↔ ∃ s : 𝕜[X], p = s • ann_ideal_generator 𝕜 a := by simp_rw [@eq_comm _ p, ← mem_span_singleton, ← span_singleton_ann_ideal_generator 𝕜 a, ideal.span] /-- The generator we chose for the annihilating ideal is monic when the ideal is non-zero. -/ lemma monic_ann_ideal_generator (a : A) (hg : ann_ideal_generator 𝕜 a ≠ 0) : monic (ann_ideal_generator 𝕜 a) := monic_mul_leading_coeff_inv (mul_ne_zero_iff.mp hg).1 /-! We are working toward showing the generator of the annihilating ideal in the field case is the minimal polynomial. We are going to use a uniqueness theorem of the minimal polynomial. This is the first condition: it must annihilate the original element `a : A`. -/ lemma ann_ideal_generator_aeval_eq_zero (a : A) : aeval a (ann_ideal_generator 𝕜 a) = 0 := mem_ann_ideal_iff_aeval_eq_zero.mp (ann_ideal_generator_mem 𝕜 a) variables {𝕜} lemma mem_iff_ann_ideal_generator_dvd {p : 𝕜[X]} {a : A} : p ∈ ann_ideal 𝕜 a ↔ ann_ideal_generator 𝕜 a ∣ p := by rw [← ideal.mem_span_singleton, span_singleton_ann_ideal_generator] /-- The generator of the annihilating ideal has minimal degree among the non-zero members of the annihilating ideal -/ lemma degree_ann_ideal_generator_le_of_mem (a : A) (p : 𝕜[X]) (hp : p ∈ ann_ideal 𝕜 a) (hpn0 : p ≠ 0) : degree (ann_ideal_generator 𝕜 a) ≤ degree p := degree_le_of_dvd (mem_iff_ann_ideal_generator_dvd.1 hp) hpn0 variables (𝕜) /-- The generator of the annihilating ideal is the minimal polynomial. -/ lemma ann_ideal_generator_eq_minpoly (a : A) : ann_ideal_generator 𝕜 a = minpoly 𝕜 a := begin by_cases h : ann_ideal_generator 𝕜 a = 0, { rw [h, minpoly.eq_zero], rintro ⟨p, p_monic, (hp : aeval a p = 0)⟩, refine p_monic.ne_zero (ideal.mem_bot.mp _), simpa only [ann_ideal_generator_eq_zero_iff.mp h] using mem_ann_ideal_iff_aeval_eq_zero.mpr hp }, { exact minpoly.unique _ _ (monic_ann_ideal_generator _ _ h) (ann_ideal_generator_aeval_eq_zero _ _) (λ q q_monic hq, (degree_ann_ideal_generator_le_of_mem a q (mem_ann_ideal_iff_aeval_eq_zero.mpr hq) q_monic.ne_zero)) } end /-- If a monic generates the annihilating ideal, it must match our choice of the annihilating ideal generator. -/ lemma monic_generator_eq_minpoly (a : A) (p : 𝕜[X]) (p_monic : p.monic) (p_gen : ideal.span {p} = ann_ideal 𝕜 a) : ann_ideal_generator 𝕜 a = p := begin by_cases h : p = 0, { rwa [h, ann_ideal_generator_eq_zero_iff, ← p_gen, ideal.span_singleton_eq_bot.mpr], }, { rw [← span_singleton_ann_ideal_generator, ideal.span_singleton_eq_span_singleton] at p_gen, rw eq_comm, apply eq_of_monic_of_associated p_monic _ p_gen, { apply monic_ann_ideal_generator _ _ ((associated.ne_zero_iff p_gen).mp h), }, }, end end field end polynomial
4fff31487b75350b400c1d764bd252d488fbcf78
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/measure_theory/card_measurable_space.lean
33bda612637229406066f3d93924ab2cdf5319d6
[ "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
8,540
lean
/- Copyright (c) 2022 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel, Violeta Hernández Palacios -/ import measure_theory.measurable_space_def import set_theory.cardinal.cofinality import set_theory.cardinal.continuum /-! # Cardinal of sigma-algebras If a sigma-algebra is generated by a set of sets `s`, then the cardinality of the sigma-algebra is bounded by `(max (#s) 2) ^ ℵ₀`. This is stated in `measurable_space.cardinal_generate_measurable_le` and `measurable_space.cardinal_measurable_set_le`. In particular, if `#s ≤ 𝔠`, then the generated sigma-algebra has cardinality at most `𝔠`, see `measurable_space.cardinal_measurable_set_le_continuum`. For the proof, we rely on an explicit inductive construction of the sigma-algebra generated by `s` (instead of the inductive predicate `generate_measurable`). This transfinite inductive construction is parameterized by an ordinal `< ω₁`, and the cardinality bound is preserved along each step of the construction. We show in `measurable_space.generate_measurable_eq_rec` that this indeed generates this sigma-algebra. -/ universe u variables {α : Type u} open_locale cardinal open cardinal set local notation `ω₁` := (aleph 1 : cardinal.{u}).ord.out.α namespace measurable_space /-- Transfinite induction construction of the sigma-algebra generated by a set of sets `s`. At each step, we add all elements of `s`, the empty set, the complements of already constructed sets, and countable unions of already constructed sets. We index this construction by an ordinal `< ω₁`, as this will be enough to generate all sets in the sigma-algebra. This construction is very similar to that of the Borel hierarchy. -/ def generate_measurable_rec (s : set (set α)) : ω₁ → set (set α) | i := let S := ⋃ j : Iio i, generate_measurable_rec j.1 in s ∪ {∅} ∪ compl '' S ∪ set.range (λ (f : ℕ → S), ⋃ n, (f n).1) using_well_founded {dec_tac := `[exact j.2]} theorem self_subset_generate_measurable_rec (s : set (set α)) (i : ω₁) : s ⊆ generate_measurable_rec s i := begin unfold generate_measurable_rec, apply_rules [subset_union_of_subset_left], exact subset_rfl end theorem empty_mem_generate_measurable_rec (s : set (set α)) (i : ω₁) : ∅ ∈ generate_measurable_rec s i := begin unfold generate_measurable_rec, exact mem_union_left _ (mem_union_left _ (mem_union_right _ (mem_singleton ∅))) end theorem compl_mem_generate_measurable_rec {s : set (set α)} {i j : ω₁} (h : j < i) {t : set α} (ht : t ∈ generate_measurable_rec s j) : tᶜ ∈ generate_measurable_rec s i := begin unfold generate_measurable_rec, exact mem_union_left _ (mem_union_right _ ⟨t, mem_Union.2 ⟨⟨j, h⟩, ht⟩, rfl⟩) end theorem Union_mem_generate_measurable_rec {s : set (set α)} {i : ω₁} {f : ℕ → set α} (hf : ∀ n, ∃ j < i, f n ∈ generate_measurable_rec s j) : (⋃ n, f n) ∈ generate_measurable_rec s i := begin unfold generate_measurable_rec, exact mem_union_right _ ⟨λ n, ⟨f n, let ⟨j, hj, hf⟩ := hf n in mem_Union.2 ⟨⟨j, hj⟩, hf⟩⟩, rfl⟩ end theorem generate_measurable_rec_subset (s : set (set α)) {i j : ω₁} (h : i ≤ j) : generate_measurable_rec s i ⊆ generate_measurable_rec s j := λ x hx, begin rcases eq_or_lt_of_le h with rfl | h, { exact hx }, { convert Union_mem_generate_measurable_rec (λ n, ⟨i, h, hx⟩), exact (Union_const x).symm } end /-- At each step of the inductive construction, the cardinality bound `≤ (max (#s) 2) ^ ℵ₀` holds. -/ lemma cardinal_generate_measurable_rec_le (s : set (set α)) (i : ω₁) : #(generate_measurable_rec s i) ≤ (max (#s) 2) ^ aleph_0.{u} := begin apply (aleph 1).ord.out.wo.wf.induction i, assume i IH, have A := aleph_0_le_aleph 1, have B : aleph 1 ≤ (max (#s) 2) ^ aleph_0.{u} := aleph_one_le_continuum.trans (power_le_power_right (le_max_right _ _)), have C : ℵ₀ ≤ (max (#s) 2) ^ aleph_0.{u} := A.trans B, have J : #(⋃ j : Iio i, generate_measurable_rec s j.1) ≤ (max (#s) 2) ^ aleph_0.{u}, { apply (mk_Union_le _).trans, have D : (⨆ j : Iio i, #(generate_measurable_rec s j)) ≤ _ := csupr_le' (λ ⟨j, hj⟩, IH j hj), apply (mul_le_mul' ((mk_subtype_le _).trans (aleph 1).mk_ord_out.le) D).trans, rw mul_eq_max A C, exact max_le B le_rfl }, rw [generate_measurable_rec], apply_rules [(mk_union_le _ _).trans, add_le_of_le C, mk_image_le.trans], { exact (le_max_left _ _).trans (self_le_power _ one_lt_aleph_0.le) }, { rw [mk_singleton], exact one_lt_aleph_0.le.trans C }, { apply mk_range_le.trans, simp only [mk_pi, subtype.val_eq_coe, prod_const, lift_uzero, mk_denumerable, lift_aleph_0], have := @power_le_power_right _ _ ℵ₀ J, rwa [← power_mul, aleph_0_mul_aleph_0] at this } end /-- `generate_measurable_rec s` generates precisely the smallest sigma-algebra containing `s`. -/ theorem generate_measurable_eq_rec (s : set (set α)) : {t | generate_measurable s t} = ⋃ i, generate_measurable_rec s i := begin ext t, refine ⟨λ ht, _, λ ht, _⟩, { inhabit ω₁, induction ht with u hu u hu IH f hf IH, { exact mem_Union.2 ⟨default, self_subset_generate_measurable_rec s _ hu⟩ }, { exact mem_Union.2 ⟨default, empty_mem_generate_measurable_rec s _⟩ }, { rcases mem_Union.1 IH with ⟨i, hi⟩, obtain ⟨j, hj⟩ := exists_gt i, exact mem_Union.2 ⟨j, compl_mem_generate_measurable_rec hj hi⟩ }, { have : ∀ n, ∃ i, f n ∈ generate_measurable_rec s i := λ n, by simpa using IH n, choose I hI using this, refine mem_Union.2 ⟨ordinal.enum (<) (ordinal.lsub (λ n, ordinal.typein.{u} (<) (I n))) _, Union_mem_generate_measurable_rec (λ n, ⟨I n, _, hI n⟩)⟩, { rw ordinal.type_lt, refine ordinal.lsub_lt_ord_lift _ (λ i, ordinal.typein_lt_self _), rw [mk_denumerable, lift_aleph_0, is_regular_aleph_one.cof_eq], exact aleph_0_lt_aleph_one }, { rw [←ordinal.typein_lt_typein (<), ordinal.typein_enum], apply ordinal.lt_lsub (λ n : ℕ, _) } } }, { rcases ht with ⟨t, ⟨i, rfl⟩, hx⟩, revert t, apply (aleph 1).ord.out.wo.wf.induction i, intros j H t ht, unfold generate_measurable_rec at ht, rcases ht with (((h | h) | ⟨u, ⟨-, ⟨⟨k, hk⟩, rfl⟩, hu⟩, rfl⟩) | ⟨f, rfl⟩), { exact generate_measurable.basic t h }, { convert generate_measurable.empty }, { exact generate_measurable.compl u (H k hk u hu) }, { apply generate_measurable.union _ (λ n, _), obtain ⟨-, ⟨⟨k, hk⟩, rfl⟩, hf⟩ := (f n).prop, exact H k hk _ hf } } end /-- If a sigma-algebra is generated by a set of sets `s`, then the sigma-algebra has cardinality at most `(max (#s) 2) ^ ℵ₀`. -/ theorem cardinal_generate_measurable_le (s : set (set α)) : #{t | generate_measurable s t} ≤ (max (#s) 2) ^ aleph_0.{u} := begin rw generate_measurable_eq_rec, apply (mk_Union_le _).trans, rw (aleph 1).mk_ord_out, refine le_trans (mul_le_mul' aleph_one_le_continuum (csupr_le' (λ i, cardinal_generate_measurable_rec_le s i))) _, have := power_le_power_right (le_max_right (#s) 2), rw mul_eq_max aleph_0_le_continuum (aleph_0_le_continuum.trans this), exact max_le this le_rfl end /-- If a sigma-algebra is generated by a set of sets `s`, then the sigma algebra has cardinality at most `(max (#s) 2) ^ ℵ₀`. -/ theorem cardinal_measurable_set_le (s : set (set α)) : #{t | @measurable_set α (generate_from s) t} ≤ (max (#s) 2) ^ aleph_0.{u} := cardinal_generate_measurable_le s /-- If a sigma-algebra is generated by a set of sets `s` with cardinality at most the continuum, then the sigma algebra has the same cardinality bound. -/ theorem cardinal_generate_measurable_le_continuum {s : set (set α)} (hs : #s ≤ 𝔠) : #{t | generate_measurable s t} ≤ 𝔠 := (cardinal_generate_measurable_le s).trans begin rw ←continuum_power_aleph_0, exact_mod_cast power_le_power_right (max_le hs (nat_lt_continuum 2).le) end /-- If a sigma-algebra is generated by a set of sets `s` with cardinality at most the continuum, then the sigma algebra has the same cardinality bound. -/ theorem cardinal_measurable_set_le_continuum {s : set (set α)} : #s ≤ 𝔠 → #{t | @measurable_set α (generate_from s) t} ≤ 𝔠 := cardinal_generate_measurable_le_continuum end measurable_space
1fc75635cf20e03b6750c041286edbe9fec5b232
453dcd7c0d1ef170b0843a81d7d8caedc9741dce
/analysis/topology/continuity.lean
34a9bb78c304052ddcea901b53b5a757dcc9ed8f
[ "Apache-2.0" ]
permissive
amswerdlow/mathlib
9af77a1f08486d8fa059448ae2d97795bd12ec0c
27f96e30b9c9bf518341705c99d641c38638dfd0
refs/heads/master
1,585,200,953,598
1,534,275,532,000
1,534,275,532,000
144,564,700
0
0
null
1,534,156,197,000
1,534,156,197,000
null
UTF-8
Lean
false
false
45,875
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 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_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 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_induced_le {t₁ : tspace α} {t₂ : tspace β} : cont t₁ t₂ f ↔ induced f t₂ ≤ t₁ := ⟨assume hc s ⟨t, ht, s_eq⟩, s_eq.symm ▸ hc t ht, assume hle s h, hle _ ⟨_, h, rfl⟩⟩ lemma continuous_iff_le_coinduced {t₁ : tspace α} {t₂ : tspace β} : cont t₁ t₂ f ↔ t₂ ≤ coinduced f t₁ := iff.rfl theorem continuous_generated_from {t : tspace α} {b : set (set β)} (h : ∀s∈b, is_open (f ⁻¹' s)) : cont t (generate_from b) f := assume s hs, generate_open.rec_on hs h is_open_univ (assume s t _ _, is_open_inter) (assume t _ h, by rw [preimage_sUnion]; exact (is_open_Union $ assume s, is_open_Union $ assume hs, h s hs)) 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 := assume s hs t ht, h t ht s hs lemma continuous_Inf_rng {t₁ : tspace α} {t₂ : set (tspace β)} {t : tspace β} (h₁ : t ∈ t₂) (hf : cont t₁ t f) : cont t₁ (Inf t₂) f := assume s hs, hf s $ hs t h₁ 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 := assume s h, trivial lemma continuous_bot {t : tspace α} : cont t ⊥ f := continuous_Inf_rng (mem_univ $ coinduced f t) continuous_coinduced_rng end constructions section embedding lemma induced_id [t : topological_space α] : t.induced id = t := topological_space_eq $ funext $ assume s, propext $ ⟨assume ⟨s', hs, h⟩, h.symm ▸ hs, assume hs, ⟨s, hs, rfl⟩⟩ lemma induced_compose [tβ : topological_space β] [tγ : topological_space γ] {f : α → β} {g : β → γ} : (tγ.induced g).induced f = tγ.induced (g ∘ f) := topological_space_eq $ funext $ assume s, propext $ ⟨assume ⟨s', ⟨s, hs, h₂⟩, h₁⟩, h₁.symm ▸ h₂.symm ▸ ⟨s, hs, rfl⟩, assume ⟨s, hs, h⟩, ⟨preimage g s, ⟨s, hs, rfl⟩, h ▸ rfl⟩⟩ /-- 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] end embedding section quotient_map lemma coinduced_id [t : topological_space α] : t.coinduced id = t := topological_space_eq rfl lemma coinduced_compose [tα : topological_space α] {f : α → β} {g : β → γ} : (tα.coinduced f).coinduced g = tα.coinduced (g ∘ f) := topological_space_eq rfl /-- 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 variables [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] lemma quotient_map_id : quotient_map (@id α) := ⟨assume a, ⟨a, rfl⟩, coinduced_id.symm⟩ lemma quotient_map_compose {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]⟩ lemma quotient_map_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)⟩ end quotient_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 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_vmap {a : α} : @nhds α (induced f t) a = vmap 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).upwards_sets 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 [vmap, 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 vmap f (nhds (f a) ⊓ principal (f '' s)) ≠ ⊥ ↔ nhds (f a) ⊓ principal (f '' s) ≠ ⊥, from ⟨assume h₁ h₂, h₁ $ h₂.symm ▸ vmap_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 ... ↔ vmap f (nhds (f a)) ⊓ principal (f ⁻¹' (f '' s)) ≠ ⊥ : by rw [nhds_induced_eq_vmap, preimage_image_eq _ hf] ... ↔ vmap f (nhds (f a) ⊓ principal (f '' s)) ≠ ⊥ : by rw [vmap_inf, ←vmap_principal] ... ↔ _ : by rwa [closure_eq_nhds] end induced 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_vmap, nhds_induced_eq_vmap] 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 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] 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 simpa) 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.upwards_sets, 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 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 end sum section subtype variables [topological_space α] [topological_space β] [topological_space γ] {p : α → Prop} lemma embedding.tendsto_nhds_iff {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_vmap, ← map_le_iff_le_vmap, 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 α β γ _ _ _ f g _ _ hg] 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 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⟩ 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_vmap {a : α} {h : p a} : nhds (⟨a, h⟩ : subtype p) = vmap subtype.val (nhds a) := nhds_induced_eq_vmap 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 {p : α → Prop} {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 end subtype section quotient variables [topological_space α] [topological_space β] [topological_space γ] variables {r : α → α → Prop} {s : setoid α} lemma quotient_map.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] 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 end quotient section pi variables {ι : Type*} {π : ι → Type*} 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, vmap (λ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, vmap (λx, x i) (nhds (a i))) : by simp [nhds_induced_eq_vmap] /-- 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 f.upwards_sets 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_vmap.mp $ (ha i).right⟩ end end pi -- 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, vmap 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_vmap.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_vmap] end⟩ protected lemma tendsto (de : dense_embedding e) {a : α} : tendsto e (nhds a) (nhds (e a)) := by rw [←de.induced a]; exact tendsto_vmap 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⟩ 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 vmap_nhds_neq_bot (de : dense_embedding e) {b : β} : vmap 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 `β → γ`. -/ 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⟩, @lim _ (classical.inhabited_of_nonempty this) _ (map f (vmap e (nhds b))) lemma extend_eq [t2_space γ] {b : β} {c : γ} {f : α → γ} (hf : map f (vmap e (nhds b)) ≤ nhds c) : de.extend f b = c := @lim_eq _ (id _) _ _ _ _ (by simp; exact vmap_nhds_neq_bot de) hf lemma extend_e_eq [t2_space γ] {a : α} {f : α → γ} (de : dense_embedding e) (hf : map f (nhds a) ≤ nhds (f a)) : de.extend f (e a) = f a := de.extend_eq begin rw de.induced; exact hf end lemma tendsto_extend [regular_space γ] {b : β} {f : α → γ} (de : dense_embedding e) (hf : {b | ∃c, tendsto f (vmap e $ nhds b) (nhds c)} ∈ (nhds b).sets) : tendsto (de.extend f) (nhds b) (nhds (de.extend f b)) := let φ := {b | tendsto f (vmap e $ nhds b) (nhds $ de.extend f b)} in have hφ : φ ∈ (nhds b).sets, from (nhds b).upwards_sets hf $ assume b ⟨c, hc⟩, show tendsto f (vmap 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 (vmap e (nhds b')) ≤ nhds (de.extend f b') ⊓ principal (f '' (e ⁻¹' t)), from calc _ ≤ map f (vmap e (nhds b' ⊓ principal t)) : map_mono $ vmap_mono $ le_inf (le_refl _) this ... ≤ map f (vmap e (nhds b')) ⊓ map f (vmap e (principal t)) : le_inf (map_mono $ vmap_mono $ inf_le_left) (map_mono $ vmap_mono $ inf_le_right) ... ≤ map f (vmap 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.vmap_nhds_neq_bot end, (nhds b).upwards_sets (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 (vmap 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_vmap_vmap_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_vmap, vmap_vmap_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₂
f4f9b8e64f712e0e271701808ab70f4d0446b8cc
f7315930643edc12e76c229a742d5446dad77097
/hott/algebra/precategory/iso.hlean
9c59835e922009bdc3b486705b076c3146ad14b0
[ "Apache-2.0" ]
permissive
bmalehorn/lean
8f77b762a76c59afff7b7403f9eb5fc2c3ce70c1
53653c352643751c4b62ff63ec5e555f11dae8eb
refs/heads/master
1,610,945,684,489
1,429,681,220,000
1,429,681,449,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
14,265
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: algebra.precategory.iso Author: Floris van Doorn, Jakob von Raumer -/ import algebra.precategory.basic types.sigma arity open eq category prod equiv is_equiv sigma sigma.ops is_trunc namespace iso structure split_mono [class] {ob : Type} [C : precategory ob] {a b : ob} (f : a ⟶ b) := {retraction_of : b ⟶ a} (retraction_comp : retraction_of ∘ f = id) structure split_epi [class] {ob : Type} [C : precategory ob] {a b : ob} (f : a ⟶ b) := {section_of : b ⟶ a} (comp_section : f ∘ section_of = id) structure is_iso [class] {ob : Type} [C : precategory ob] {a b : ob} (f : a ⟶ b) := {inverse : b ⟶ a} (left_inverse : inverse ∘ f = id) (right_inverse : f ∘ inverse = id) attribute is_iso.inverse [quasireducible] attribute is_iso [multiple-instances] open split_mono split_epi is_iso definition retraction_of [reducible] := @split_mono.retraction_of definition retraction_comp [reducible] := @split_mono.retraction_comp definition section_of [reducible] := @split_epi.section_of definition comp_section [reducible] := @split_epi.comp_section definition inverse [reducible] := @is_iso.inverse definition left_inverse [reducible] := @is_iso.left_inverse definition right_inverse [reducible] := @is_iso.right_inverse postfix `⁻¹` := inverse --a second notation for the inverse, which is not overloaded postfix [parsing-only] `⁻¹ʰ`:std.prec.max_plus := inverse -- input using \-1h variables {ob : Type} [C : precategory ob] variables {a b c : ob} {g : b ⟶ c} {f : a ⟶ b} {h : b ⟶ a} include C definition split_mono_of_is_iso [instance] [priority 300] [reducible] (f : a ⟶ b) [H : is_iso f] : split_mono f := split_mono.mk !left_inverse definition split_epi_of_is_iso [instance] [priority 300] [reducible] (f : a ⟶ b) [H : is_iso f] : split_epi f := split_epi.mk !right_inverse definition is_iso_id [instance] [priority 500] (a : ob) : is_iso (ID a) := is_iso.mk !id_comp !id_comp definition is_iso_inverse [instance] [priority 200] (f : a ⟶ b) [H : is_iso f] : is_iso f⁻¹ := is_iso.mk !right_inverse !left_inverse definition left_inverse_eq_right_inverse {f : a ⟶ b} {g g' : hom b a} (Hl : g ∘ f = id) (Hr : f ∘ g' = id) : g = g' := by rewrite [-(id_right g), -Hr, assoc, Hl, id_left] definition retraction_eq [H : split_mono f] (H2 : f ∘ h = id) : retraction_of f = h := left_inverse_eq_right_inverse !retraction_comp H2 definition section_eq [H : split_epi f] (H2 : h ∘ f = id) : section_of f = h := (left_inverse_eq_right_inverse H2 !comp_section)⁻¹ definition inverse_eq_right [H : is_iso f] (H2 : f ∘ h = id) : f⁻¹ = h := left_inverse_eq_right_inverse !left_inverse H2 definition inverse_eq_left [H : is_iso f] (H2 : h ∘ f = id) : f⁻¹ = h := (left_inverse_eq_right_inverse H2 !right_inverse)⁻¹ definition retraction_eq_section (f : a ⟶ b) [Hl : split_mono f] [Hr : split_epi f] : retraction_of f = section_of f := retraction_eq !comp_section definition is_iso_of_split_epi_of_split_mono (f : a ⟶ b) [Hl : split_mono f] [Hr : split_epi f] : is_iso f := is_iso.mk ((retraction_eq_section f) ▹ (retraction_comp f)) (comp_section f) definition inverse_unique (H H' : is_iso f) : @inverse _ _ _ _ f H = @inverse _ _ _ _ f H' := inverse_eq_left !left_inverse definition inverse_involutive (f : a ⟶ b) [H : is_iso f] [H : is_iso (f⁻¹)] : (f⁻¹)⁻¹ = f := inverse_eq_right !left_inverse definition retraction_id (a : ob) : retraction_of (ID a) = id := retraction_eq !id_comp definition section_id (a : ob) : section_of (ID a) = id := section_eq !id_comp definition id_inverse (a : ob) [H : is_iso (ID a)] : (ID a)⁻¹ = id := inverse_eq_left !id_comp definition split_mono_comp [instance] [priority 150] (g : b ⟶ c) (f : a ⟶ b) [Hf : split_mono f] [Hg : split_mono g] : split_mono (g ∘ f) := split_mono.mk (show (retraction_of f ∘ retraction_of g) ∘ g ∘ f = id, by rewrite [-assoc, assoc _ g f, retraction_comp, id_left, retraction_comp]) definition split_epi_comp [instance] [priority 150] (g : b ⟶ c) (f : a ⟶ b) [Hf : split_epi f] [Hg : split_epi g] : split_epi (g ∘ f) := split_epi.mk (show (g ∘ f) ∘ section_of f ∘ section_of g = id, by rewrite [-assoc, {f ∘ _}assoc, comp_section, id_left, comp_section]) definition is_iso_comp [instance] [priority 150] (g : b ⟶ c) (f : a ⟶ b) [Hf : is_iso f] [Hg : is_iso g] : is_iso (g ∘ f) := !is_iso_of_split_epi_of_split_mono -- "is_iso f" is equivalent to a certain sigma type -- definition is_iso.sigma_char (f : hom a b) : -- (Σ (g : hom b a), (g ∘ f = id) × (f ∘ g = id)) ≃ is_iso f := -- begin -- fapply equiv.MK, -- {intro S, apply is_iso.mk, -- exact (pr₁ S.2), -- exact (pr₂ S.2)}, -- {intro H, cases H with (g, η, ε), -- exact (sigma.mk g (pair η ε))}, -- {intro H, cases H, apply idp}, -- {intro S, cases S with (g, ηε), cases ηε, apply idp}, -- end definition is_hprop_is_iso [instance] (f : hom a b) : is_hprop (is_iso f) := begin apply is_hprop.mk, intros [H, H'], cases H with [g, li, ri], cases H' with [g', li', ri'], fapply (apD0111 (@is_iso.mk ob C a b f)), apply left_inverse_eq_right_inverse, apply li, apply ri', apply is_hprop.elim, apply is_hprop.elim, end /- iso objects -/ structure iso (a b : ob) := (to_hom : hom a b) [struct : is_iso to_hom] infix `≅`:50 := iso.iso attribute iso.struct [instance] [priority 400] namespace iso attribute to_hom [coercion] definition MK (f : a ⟶ b) (g : b ⟶ a) (H1 : g ∘ f = id) (H2 : f ∘ g = id) := @mk _ _ _ _ f (is_iso.mk H1 H2) definition to_inv (f : a ≅ b) : b ⟶ a := (to_hom f)⁻¹ protected definition refl (a : ob) : a ≅ a := mk (ID a) protected definition symm ⦃a b : ob⦄ (H : a ≅ b) : b ≅ a := mk (to_hom H)⁻¹ protected definition trans ⦃a b c : ob⦄ (H1 : a ≅ b) (H2 : b ≅ c) : a ≅ c := mk (to_hom H2 ∘ to_hom H1) protected definition eq_mk' {f f' : a ⟶ b} [H : is_iso f] [H' : is_iso f'] (p : f = f') : iso.mk f = iso.mk f' := apD011 iso.mk p !is_hprop.elim protected definition eq_mk {f f' : a ≅ b} (p : to_hom f = to_hom f') : f = f' := by (cases f; cases f'; apply (iso.eq_mk' p)) -- The structure for isomorphism can be characterized up to equivalence by a sigma type. definition sigma_char ⦃a b : ob⦄ : (Σ (f : hom a b), is_iso f) ≃ (a ≅ b) := begin fapply (equiv.mk), {intro S, apply iso.mk, apply (S.2)}, {fapply adjointify, {intro p, cases p with [f, H], exact (sigma.mk f H)}, {intro p, cases p, apply idp}, {intro S, cases S, apply idp}}, end end iso -- The type of isomorphisms between two objects is a set definition is_hset_iso [instance] : is_hset (a ≅ b) := begin apply is_trunc_is_equiv_closed, apply (equiv.to_is_equiv (!iso.sigma_char)), end definition iso_of_eq (p : a = b) : a ≅ b := eq.rec_on p (iso.refl a) definition hom_of_eq [reducible] (p : a = b) : a ⟶ b := iso.to_hom (iso_of_eq p) definition inv_of_eq [reducible] (p : a = b) : b ⟶ a := iso.to_inv (iso_of_eq p) definition iso_of_eq_inv (p : a = b) : iso_of_eq p⁻¹ = iso.symm (iso_of_eq p) := eq.rec_on p idp definition iso_of_eq_con (p : a = b) (q : b = c) : iso_of_eq (p ⬝ q) = iso.trans (iso_of_eq p) (iso_of_eq q) := eq.rec_on q (eq.rec_on p (iso.eq_mk !id_comp⁻¹)) section open funext variables {X : Type} {x y : X} {F G : X → ob} definition transport_hom_of_eq (p : F = G) (f : hom (F x) (F y)) : p ▹ f = hom_of_eq (apD10 p y) ∘ f ∘ inv_of_eq (apD10 p x) := eq.rec_on p !id_leftright⁻¹ definition transport_hom (p : F ∼ G) (f : hom (F x) (F y)) : eq_of_homotopy p ▹ f = hom_of_eq (p y) ∘ f ∘ inv_of_eq (p x) := calc eq_of_homotopy p ▹ f = hom_of_eq (apD10 (eq_of_homotopy p) y) ∘ f ∘ inv_of_eq (apD10 (eq_of_homotopy p) x) : transport_hom_of_eq ... = hom_of_eq (p y) ∘ f ∘ inv_of_eq (p x) : {retr apD10 p} end structure mono [class] (f : a ⟶ b) := (elim : ∀c (g h : hom c a), f ∘ g = f ∘ h → g = h) structure epi [class] (f : a ⟶ b) := (elim : ∀c (g h : hom b c), g ∘ f = h ∘ f → g = h) definition mono_of_split_mono [instance] (f : a ⟶ b) [H : split_mono f] : mono f := mono.mk (λ c g h H, calc g = id ∘ g : by rewrite id_left ... = (retraction_of f ∘ f) ∘ g : by rewrite retraction_comp ... = (retraction_of f ∘ f) ∘ h : by rewrite [-assoc, H, -assoc] ... = id ∘ h : by rewrite retraction_comp ... = h : by rewrite id_left) definition epi_of_split_epi [instance] (f : a ⟶ b) [H : split_epi f] : epi f := epi.mk (λ c g h H, calc g = g ∘ id : by rewrite id_right ... = g ∘ f ∘ section_of f : by rewrite -comp_section ... = h ∘ f ∘ section_of f : by rewrite [assoc, H, -assoc] ... = h ∘ id : by rewrite comp_section ... = h : by rewrite id_right) definition mono_comp [instance] (g : b ⟶ c) (f : a ⟶ b) [Hf : mono f] [Hg : mono g] : mono (g ∘ f) := mono.mk (λ d h₁ h₂ H, have H2 : g ∘ (f ∘ h₁) = g ∘ (f ∘ h₂), begin rewrite *assoc, exact H end, !mono.elim (!mono.elim H2)) definition epi_comp [instance] (g : b ⟶ c) (f : a ⟶ b) [Hf : epi f] [Hg : epi g] : epi (g ∘ f) := epi.mk (λ d h₁ h₂ H, have H2 : (h₁ ∘ g) ∘ f = (h₂ ∘ g) ∘ f, begin rewrite -*assoc, exact H end, !epi.elim (!epi.elim H2)) end iso namespace iso /- rewrite lemmas for inverses, modified from https://github.com/JasonGross/HoTT-categories/blob/master/theories/Categories/Category/Morphisms.v -/ section variables {ob : Type} [C : precategory ob] include C variables {a b c d : ob} (f : b ⟶ a) (r : c ⟶ d) (q : b ⟶ c) (p : a ⟶ b) (g : d ⟶ c) variable [Hq : is_iso q] include Hq definition comp.right_inverse : q ∘ q⁻¹ = id := !right_inverse definition comp.left_inverse : q⁻¹ ∘ q = id := !left_inverse definition inverse_comp_cancel_left : q⁻¹ ∘ (q ∘ p) = p := by rewrite [assoc, left_inverse, id_left] definition comp_inverse_cancel_left : q ∘ (q⁻¹ ∘ g) = g := by rewrite [assoc, right_inverse, id_left] definition comp_inverse_cancel_right : (r ∘ q) ∘ q⁻¹ = r := by rewrite [-assoc, right_inverse, id_right] definition inverse_comp_cancel_right : (f ∘ q⁻¹) ∘ q = f := by rewrite [-assoc, left_inverse, id_right] definition comp_inverse [Hp : is_iso p] [Hpq : is_iso (q ∘ p)] : (q ∘ p)⁻¹ʰ = p⁻¹ʰ ∘ q⁻¹ʰ := inverse_eq_left (show (p⁻¹ʰ ∘ q⁻¹ʰ) ∘ q ∘ p = id, from by rewrite [-assoc, inverse_comp_cancel_left, left_inverse]) definition inverse_comp_inverse_left [H' : is_iso g] : (q⁻¹ ∘ g)⁻¹ = g⁻¹ ∘ q := inverse_involutive q ▹ comp_inverse q⁻¹ g definition inverse_comp_inverse_right [H' : is_iso f] : (q ∘ f⁻¹)⁻¹ = f ∘ q⁻¹ := inverse_involutive f ▹ comp_inverse q f⁻¹ definition inverse_comp_inverse_inverse [H' : is_iso r] : (q⁻¹ ∘ r⁻¹)⁻¹ = r ∘ q := inverse_involutive r ▹ inverse_comp_inverse_left q r⁻¹ end section variables {ob : Type} {C : precategory ob} include C variables {d c b a : ob} {i : b ⟶ c} {f : b ⟶ a} {r : c ⟶ d} {q : b ⟶ c} {p : a ⟶ b} {g : d ⟶ c} {h : c ⟶ b} {x : b ⟶ d} {z : a ⟶ c} {y : d ⟶ b} {w : c ⟶ a} variable [Hq : is_iso q] include Hq definition comp_eq_of_eq_inverse_comp (H : y = q⁻¹ ∘ g) : q ∘ y = g := H⁻¹ ▹ comp_inverse_cancel_left q g definition comp_eq_of_eq_comp_inverse (H : w = f ∘ q⁻¹) : w ∘ q = f := H⁻¹ ▹ inverse_comp_cancel_right f q definition inverse_comp_eq_of_eq_comp (H : z = q ∘ p) : q⁻¹ ∘ z = p := H⁻¹ ▹ inverse_comp_cancel_left q p definition comp_inverse_eq_of_eq_comp (H : x = r ∘ q) : x ∘ q⁻¹ = r := H⁻¹ ▹ comp_inverse_cancel_right r q definition eq_comp_of_inverse_comp_eq (H : q⁻¹ ∘ g = y) : g = q ∘ y := (comp_eq_of_eq_inverse_comp H⁻¹)⁻¹ definition eq_comp_of_comp_inverse_eq (H : f ∘ q⁻¹ = w) : f = w ∘ q := (comp_eq_of_eq_comp_inverse H⁻¹)⁻¹ definition eq_inverse_comp_of_comp_eq (H : q ∘ p = z) : p = q⁻¹ ∘ z := (inverse_comp_eq_of_eq_comp H⁻¹)⁻¹ definition eq_comp_inverse_of_comp_eq (H : r ∘ q = x) : r = x ∘ q⁻¹ := (comp_inverse_eq_of_eq_comp H⁻¹)⁻¹ definition eq_inverse_of_comp_eq_id' (H : h ∘ q = id) : h = q⁻¹ := (inverse_eq_left H)⁻¹ definition eq_inverse_of_comp_eq_id (H : q ∘ h = id) : h = q⁻¹ := (inverse_eq_right H)⁻¹ definition eq_of_comp_inverse_eq_id (H : i ∘ q⁻¹ = id) : i = q := eq_inverse_of_comp_eq_id' H ⬝ inverse_involutive q definition eq_of_inverse_comp_eq_id (H : q⁻¹ ∘ i = id) : i = q := eq_inverse_of_comp_eq_id H ⬝ inverse_involutive q definition eq_of_id_eq_comp_inverse (H : id = i ∘ q⁻¹) : q = i := (eq_of_comp_inverse_eq_id H⁻¹)⁻¹ definition eq_of_id_eq_inverse_comp (H : id = q⁻¹ ∘ i) : q = i := (eq_of_inverse_comp_eq_id H⁻¹)⁻¹ definition inverse_eq_of_id_eq_comp (H : id = h ∘ q) : q⁻¹ = h := (eq_inverse_of_comp_eq_id' H⁻¹)⁻¹ definition inverse_eq_of_id_eq_comp' (H : id = q ∘ h) : q⁻¹ = h := (eq_inverse_of_comp_eq_id H⁻¹)⁻¹ end end iso
afd1c9fa24eb4cadf2262d071a65606834244cc2
9dc8cecdf3c4634764a18254e94d43da07142918
/src/algebra/smul_with_zero.lean
92cba278685acd21dae78fe9f7148907ecf0261d
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
7,237
lean
/- Copyright (c) 2021 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa -/ import algebra.group_power.basic import algebra.ring.opposite import group_theory.group_action.opposite import group_theory.group_action.prod /-! # Introduce `smul_with_zero` In analogy with the usual monoid action on a Type `M`, we introduce an action of a `monoid_with_zero` on a Type with `0`. In particular, for Types `R` and `M`, both containing `0`, we define `smul_with_zero R M` to be the typeclass where the products `r • 0` and `0 • m` vanish for all `r : R` and all `m : M`. Moreover, in the case in which `R` is a `monoid_with_zero`, we introduce the typeclass `mul_action_with_zero R M`, mimicking group actions and having an absorbing `0` in `R`. Thus, the action is required to be compatible with * the unit of the monoid, acting as the identity; * the zero of the monoid_with_zero, acting as zero; * associativity of the monoid. We also add an `instance`: * any `monoid_with_zero` has a `mul_action_with_zero R R` acting on itself. ## Main declarations * `smul_monoid_with_zero_hom`: Scalar multiplication bundled as a morphism of monoids with zero. -/ variables {R R' M M' : Type*} section has_zero variables (R M) /-- `smul_with_zero` is a class consisting of a Type `R` with `0 ∈ R` and a scalar multiplication of `R` on a Type `M` with `0`, such that the equality `r • m = 0` holds if at least one among `r` or `m` equals `0`. -/ class smul_with_zero [has_zero R] [has_zero M] extends has_smul R M := (smul_zero : ∀ r : R, r • (0 : M) = 0) (zero_smul : ∀ m : M, (0 : R) • m = 0) instance mul_zero_class.to_smul_with_zero [mul_zero_class R] : smul_with_zero R R := { smul := (*), smul_zero := mul_zero, zero_smul := zero_mul } /-- Like `mul_zero_class.to_smul_with_zero`, but multiplies on the right. -/ instance mul_zero_class.to_opposite_smul_with_zero [mul_zero_class R] : smul_with_zero Rᵐᵒᵖ R := { smul := (•), smul_zero := λ r, zero_mul _, zero_smul := mul_zero } variables (R) {M} [has_zero R] [has_zero M] [smul_with_zero R M] @[simp] lemma zero_smul (m : M) : (0 : R) • m = 0 := smul_with_zero.zero_smul m variables {R} (M) /-- Note that this lemma has different typeclass assumptions to `smul_zero`. -/ @[simp] lemma smul_zero' (r : R) : r • (0 : M) = 0 := smul_with_zero.smul_zero r variables {R M} [has_zero R'] [has_zero M'] [has_smul R M'] /-- Pullback a `smul_with_zero` structure along an injective zero-preserving homomorphism. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.smul_with_zero (f : zero_hom M' M) (hf : function.injective f) (smul : ∀ (a : R) b, f (a • b) = a • f b) : smul_with_zero R M' := { smul := (•), zero_smul := λ a, hf $ by simp [smul], smul_zero := λ a, hf $ by simp [smul]} /-- Pushforward a `smul_with_zero` structure along a surjective zero-preserving homomorphism. See note [reducible non-instances]. -/ @[reducible] protected def function.surjective.smul_with_zero (f : zero_hom M M') (hf : function.surjective f) (smul : ∀ (a : R) b, f (a • b) = a • f b) : smul_with_zero R M' := { smul := (•), zero_smul := λ m, by { rcases hf m with ⟨x, rfl⟩, simp [←smul] }, smul_zero := λ c, by simp only [← f.map_zero, ← smul, smul_zero'] } variables (M) /-- Compose a `smul_with_zero` with a `zero_hom`, with action `f r' • m` -/ def smul_with_zero.comp_hom (f : zero_hom R' R) : smul_with_zero R' M := { smul := (•) ∘ f, smul_zero := λ m, by simp, zero_smul := λ m, by simp } end has_zero instance add_monoid.nat_smul_with_zero [add_monoid M] : smul_with_zero ℕ M := { smul_zero := nsmul_zero, zero_smul := zero_nsmul } instance add_group.int_smul_with_zero [add_group M] : smul_with_zero ℤ M := { smul_zero := zsmul_zero, zero_smul := zero_zsmul } section monoid_with_zero variables [monoid_with_zero R] [monoid_with_zero R'] [has_zero M] variables (R M) /-- An action of a monoid with zero `R` on a Type `M`, also with `0`, extends `mul_action` and is compatible with `0` (both in `R` and in `M`), with `1 ∈ R`, and with associativity of multiplication on the monoid `M`. -/ class mul_action_with_zero extends mul_action R M := -- these fields are copied from `smul_with_zero`, as `extends` behaves poorly (smul_zero : ∀ r : R, r • (0 : M) = 0) (zero_smul : ∀ m : M, (0 : R) • m = 0) @[priority 100] -- see Note [lower instance priority] instance mul_action_with_zero.to_smul_with_zero [m : mul_action_with_zero R M] : smul_with_zero R M := {..m} /-- See also `semiring.to_module` -/ instance monoid_with_zero.to_mul_action_with_zero : mul_action_with_zero R R := { ..mul_zero_class.to_smul_with_zero R, ..monoid.to_mul_action R } /-- Like `monoid_with_zero.to_mul_action_with_zero`, but multiplies on the right. See also `semiring.to_opposite_module` -/ instance monoid_with_zero.to_opposite_mul_action_with_zero : mul_action_with_zero Rᵐᵒᵖ R := { ..mul_zero_class.to_opposite_smul_with_zero R, ..monoid.to_opposite_mul_action R } variables {R M} [mul_action_with_zero R M] [has_zero M'] [has_smul R M'] /-- Pullback a `mul_action_with_zero` structure along an injective zero-preserving homomorphism. See note [reducible non-instances]. -/ @[reducible] protected def function.injective.mul_action_with_zero (f : zero_hom M' M) (hf : function.injective f) (smul : ∀ (a : R) b, f (a • b) = a • f b) : mul_action_with_zero R M' := { ..hf.mul_action f smul, ..hf.smul_with_zero f smul } /-- Pushforward a `mul_action_with_zero` structure along a surjective zero-preserving homomorphism. See note [reducible non-instances]. -/ @[reducible] protected def function.surjective.mul_action_with_zero (f : zero_hom M M') (hf : function.surjective f) (smul : ∀ (a : R) b, f (a • b) = a • f b) : mul_action_with_zero R M' := { ..hf.mul_action f smul, ..hf.smul_with_zero f smul } variables (M) /-- Compose a `mul_action_with_zero` with a `monoid_with_zero_hom`, with action `f r' • m` -/ def mul_action_with_zero.comp_hom (f : R' →*₀ R) : mul_action_with_zero R' M := { smul := (•) ∘ f, mul_smul := λ r s m, by simp [mul_smul], one_smul := λ m, by simp, .. smul_with_zero.comp_hom M f.to_zero_hom} end monoid_with_zero section group_with_zero variables {α β : Type*} [group_with_zero α] [group_with_zero β] [mul_action_with_zero α β] lemma smul_inv₀ [smul_comm_class α β β] [is_scalar_tower α β β] (c : α) (x : β) : (c • x)⁻¹ = c⁻¹ • x⁻¹ := begin obtain rfl | hc := eq_or_ne c 0, { simp only [inv_zero, zero_smul] }, obtain rfl | hx := eq_or_ne x 0, { simp only [inv_zero, smul_zero'] }, { refine inv_eq_of_mul_eq_one_left _, rw [smul_mul_smul, inv_mul_cancel hc, inv_mul_cancel hx, one_smul] } end end group_with_zero /-- Scalar multiplication as a monoid homomorphism with zero. -/ @[simps] def smul_monoid_with_zero_hom {α β : Type*} [monoid_with_zero α] [mul_zero_one_class β] [mul_action_with_zero α β] [is_scalar_tower α β β] [smul_comm_class α β β] : α × β →*₀ β := { map_zero' := smul_zero' _ _, .. smul_monoid_hom }
36c0521e16bff96cece480734d9fcd9c6eb612a1
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/analysis/convex/basic.lean
f6ab1c3f43f598ec76df4dd58ad7164e8ffb59d4
[ "Apache-2.0" ]
permissive
ilitzroth/mathlib
ea647e67f1fdfd19a0f7bdc5504e8acec6180011
5254ef14e3465f6504306132fe3ba9cec9ffff16
refs/heads/master
1,680,086,661,182
1,617,715,647,000
1,617,715,647,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
58,311
lean
/- Copyright (c) 2019 Alexander Bentkamp. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Alexander Bentkamp, Yury Kudriashov -/ import data.set.intervals.ord_connected import data.set.intervals.image_preimage import data.complex.module import linear_algebra.affine_space.affine_map import algebra.module.ordered /-! # Convex sets and functions on real vector spaces In a real vector space, we define the following objects and properties. * `segment x y` is the closed segment joining `x` and `y`. * A set `s` is `convex` if for any two points `x y ∈ s` it includes `segment x y`; * A function `f : E → β` is `convex_on` a set `s` if `s` is itself a convex set, and for any two points `x y ∈ s` the segment joining `(x, f x)` to `(y, f y)` is (non-strictly) above the graph of `f`; equivalently, `convex_on f s` means that the epigraph `{p : E × β | p.1 ∈ s ∧ f p.1 ≤ p.2}` is a convex set; * Center mass of a finite set of points with prescribed weights. * Convex hull of a set `s` is the minimal convex set that includes `s`. * Standard simplex `std_simplex ι [fintype ι]` is the intersection of the positive quadrant with the hyperplane `s.sum = 1` in the space `ι → ℝ`. We also provide various equivalent versions of the definitions above, prove that some specific sets are convex, and prove Jensen's inequality. Note: To define convexity for functions `f : E → β`, we need `β` to be an ordered vector space, defined using the instance `ordered_semimodule ℝ β`. ## Notations We use the following local notations: * `I = Icc (0:ℝ) 1`; * `[x, y] = segment x y`. They are defined using `local notation`, so they are not available outside of this file. -/ universes u' u v v' w x variables {E : Type u} {F : Type v} {ι : Type w} {ι' : Type x} {α : Type v'} [add_comm_group E] [vector_space ℝ E] [add_comm_group F] [vector_space ℝ F] [linear_ordered_field α] {s : set E} open set linear_map open_locale classical big_operators local notation `I` := (Icc 0 1 : set ℝ) section sets /-! ### Segment -/ /-- Segments in a vector space. -/ def segment (x y : E) : set E := {z : E | ∃ (a b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1), a • x + b • y = z} local notation `[`x `, ` y `]` := segment x y lemma segment_symm (x y : E) : [x, y] = [y, x] := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, H⟩, ⟨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans H⟩, λ ⟨a, b, ha, hb, hab, H⟩, ⟨b, a, hb, ha, (add_comm _ _).trans hab, (add_comm _ _).trans H⟩⟩ lemma left_mem_segment (x y : E) : x ∈ [x, y] := ⟨1, 0, zero_le_one, le_refl 0, add_zero 1, by rw [zero_smul, one_smul, add_zero]⟩ lemma right_mem_segment (x y : E) : y ∈ [x, y] := segment_symm y x ▸ left_mem_segment y x lemma segment_same (x : E) : [x, x] = {x} := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, hz⟩, by simpa only [(add_smul _ _ _).symm, mem_singleton_iff, hab, one_smul, eq_comm] using hz, λ h, mem_singleton_iff.1 h ▸ left_mem_segment z z⟩ lemma segment_eq_image (x y : E) : segment x y = (λ (θ : ℝ), (1 - θ) • x + θ • y) '' I := set.ext $ λ z, ⟨λ ⟨a, b, ha, hb, hab, hz⟩, ⟨b, ⟨hb, hab ▸ le_add_of_nonneg_left ha⟩, hab ▸ hz ▸ by simp only [add_sub_cancel]⟩, λ ⟨θ, ⟨hθ₀, hθ₁⟩, hz⟩, ⟨1-θ, θ, sub_nonneg.2 hθ₁, hθ₀, sub_add_cancel _ _, hz⟩⟩ lemma segment_eq_image' (x y : E) : segment x y = (λ (θ : ℝ), x + θ • (y - x)) '' I := by { convert segment_eq_image x y, ext θ, simp only [smul_sub, sub_smul, one_smul], abel } lemma segment_eq_image₂ (x y : E) : segment x y = (λ p:ℝ×ℝ, p.1 • x + p.2 • y) '' {p | 0 ≤ p.1 ∧ 0 ≤ p.2 ∧ p.1 + p.2 = 1} := by simp only [segment, image, prod.exists, mem_set_of_eq, exists_prop, and_assoc] lemma segment_eq_Icc {a b : ℝ} (h : a ≤ b) : [a, b] = Icc a b := begin rw [segment_eq_image'], show (((+) a) ∘ (λ t, t * (b - a))) '' Icc 0 1 = Icc a b, rw [image_comp, image_mul_right_Icc (@zero_le_one ℝ _) (sub_nonneg.2 h), image_const_add_Icc], simp end lemma segment_eq_Icc' (a b : ℝ) : [a, b] = Icc (min a b) (max a b) := by cases le_total a b; [skip, rw segment_symm]; simp [segment_eq_Icc, *] lemma segment_eq_interval (a b : ℝ) : segment a b = interval a b := segment_eq_Icc' _ _ lemma mem_segment_translate (a : E) {x b c} : a + x ∈ [a + b, a + c] ↔ x ∈ [b, c] := begin rw [segment_eq_image', segment_eq_image'], refine exists_congr (λ θ, and_congr iff.rfl _), simp only [add_sub_add_left_eq_sub, add_assoc, add_right_inj] end lemma segment_translate_preimage (a b c : E) : (λ x, a + x) ⁻¹' [a + b, a + c] = [b, c] := set.ext $ λ x, mem_segment_translate a lemma segment_translate_image (a b c: E) : (λx, a + x) '' [b, c] = [a + b, a + c] := segment_translate_preimage a b c ▸ image_preimage_eq _ $ add_left_surjective a lemma segment_image (f : E →ₗ[ℝ] F) (a b : E) : f '' [a, b] = [f a, f b] := set.ext (λ x, by simp [segment_eq_image]) /-! ### Convexity of sets -/ /-- Convexity of sets. -/ def convex (s : set E) := ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → a • x + b • y ∈ s lemma convex_iff_forall_pos : convex s ↔ ∀ ⦃x y⦄, x ∈ s → y ∈ s → ∀ ⦃a b : ℝ⦄, 0 < a → 0 < b → a + b = 1 → a • x + b • y ∈ s := begin refine ⟨λ h x y hx hy a b ha hb hab, h hx hy (le_of_lt ha) (le_of_lt hb) hab, _⟩, intros h x y hx hy a b ha hb hab, cases eq_or_lt_of_le ha with ha ha, { subst a, rw [zero_add] at hab, simp [hab, hy] }, cases eq_or_lt_of_le hb with hb hb, { subst b, rw [add_zero] at hab, simp [hab, hx] }, exact h hx hy ha hb hab end lemma convex_iff_segment_subset : convex s ↔ ∀ ⦃x y⦄, x ∈ s → y ∈ s → [x, y] ⊆ s := by simp only [convex, segment_eq_image₂, subset_def, ball_image_iff, prod.forall, mem_set_of_eq, and_imp] lemma convex.segment_subset (h : convex s) {x y:E} (hx : x ∈ s) (hy : y ∈ s) : [x, y] ⊆ s := convex_iff_segment_subset.1 h hx hy /-- Alternative definition of set convexity, in terms of pointwise set operations. -/ lemma convex_iff_pointwise_add_subset: convex s ↔ ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → a • s + b • s ⊆ s := iff.intro begin rintros hA a b ha hb hab w ⟨au, bv, ⟨u, hu, rfl⟩, ⟨v, hv, rfl⟩, rfl⟩, exact hA hu hv ha hb hab end (λ h x y hx hy a b ha hb hab, (h ha hb hab) (set.add_mem_add ⟨_, hx, rfl⟩ ⟨_, hy, rfl⟩)) /-- Alternative definition of set convexity, using division. -/ lemma convex_iff_div: convex s ↔ ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → 0 < a + b → (a/(a+b)) • x + (b/(a+b)) • y ∈ s := ⟨begin assume h x y hx hy a b ha hb hab, apply h hx hy, have ha', from mul_le_mul_of_nonneg_left ha (le_of_lt (inv_pos.2 hab)), rwa [mul_zero, ←div_eq_inv_mul] at ha', have hb', from mul_le_mul_of_nonneg_left hb (le_of_lt (inv_pos.2 hab)), rwa [mul_zero, ←div_eq_inv_mul] at hb', rw [←add_div], exact div_self (ne_of_lt hab).symm end, begin assume h x y hx hy a b ha hb hab, have h', from h hx hy ha hb, rw [hab, div_one, div_one] at h', exact h' zero_lt_one end⟩ /-! ### Examples of convex sets -/ lemma convex_empty : convex (∅ : set E) := by finish lemma convex_singleton (c : E) : convex ({c} : set E) := begin intros x y hx hy a b ha hb hab, rw [set.eq_of_mem_singleton hx, set.eq_of_mem_singleton hy, ←add_smul, hab, one_smul], exact mem_singleton c end lemma convex_univ : convex (set.univ : set E) := λ _ _ _ _ _ _ _ _ _, trivial lemma convex.inter {t : set E} (hs: convex s) (ht: convex t) : convex (s ∩ t) := λ x y (hx : x ∈ s ∩ t) (hy : y ∈ s ∩ t) a b (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1), ⟨hs hx.left hy.left ha hb hab, ht hx.right hy.right ha hb hab⟩ lemma convex_sInter {S : set (set E)} (h : ∀ s ∈ S, convex s) : convex (⋂₀ S) := assume x y hx hy a b ha hb hab s hs, h s hs (hx s hs) (hy s hs) ha hb hab lemma convex_Inter {ι : Sort*} {s: ι → set E} (h: ∀ i : ι, convex (s i)) : convex (⋂ i, s i) := (sInter_range s) ▸ convex_sInter $ forall_range_iff.2 h lemma convex.prod {s : set E} {t : set F} (hs : convex s) (ht : convex t) : convex (s.prod t) := begin intros x y hx hy a b ha hb hab, apply mem_prod.2, exact ⟨hs (mem_prod.1 hx).1 (mem_prod.1 hy).1 ha hb hab, ht (mem_prod.1 hx).2 (mem_prod.1 hy).2 ha hb hab⟩ end lemma convex.combo_to_vadd {a b : ℝ} {x y : E} (h : a + b = 1) : a • x + b • y = b • (y - x) + x := calc a • x + b • y = (b • y - b • x) + (a • x + b • x) : by abel ... = b • (y - x) + (a + b) • x : by rw [smul_sub, add_smul] ... = b • (y - x) + (1 : ℝ) • x : by rw [h] ... = b • (y - x) + x : by rw [one_smul] /-- Applying an affine map to an affine combination of two points yields an affine combination of the images. -/ lemma convex.combo_affine_apply {a b : ℝ} {x y : E} {f : E →ᵃ[ℝ] F} (h : a + b = 1) : f (a • x + b • y) = a • f x + b • f y := begin simp only [convex.combo_to_vadd h, ← vsub_eq_sub], exact f.apply_line_map _ _ _, end /-- The preimage of a convex set under an affine map is convex. -/ lemma convex.affine_preimage (f : E →ᵃ[ℝ] F) {s : set F} (hs : convex s) : convex (f ⁻¹' s) := begin intros x y xs ys a b ha hb hab, rw [mem_preimage, convex.combo_affine_apply hab], exact hs xs ys ha hb hab, end /-- The image of a convex set under an affine map is convex. -/ lemma convex.affine_image (f : E →ᵃ[ℝ] F) {s : set E} (hs : convex s) : convex (f '' s) := begin rintros x y ⟨x', ⟨hx', hx'f⟩⟩ ⟨y', ⟨hy', hy'f⟩⟩ a b ha hb hab, refine ⟨a • x' + b • y', ⟨hs hx' hy' ha hb hab, _⟩⟩, rw [convex.combo_affine_apply hab, hx'f, hy'f] end lemma convex.linear_image (hs : convex s) (f : E →ₗ[ℝ] F) : convex (image f s) := hs.affine_image f.to_affine_map lemma convex.is_linear_image (hs : convex s) {f : E → F} (hf : is_linear_map ℝ f) : convex (f '' s) := hs.linear_image $ hf.mk' f lemma convex.linear_preimage {s : set F} (hs : convex s) (f : E →ₗ[ℝ] F) : convex (preimage f s) := hs.affine_preimage f.to_affine_map lemma convex.is_linear_preimage {s : set F} (hs : convex s) {f : E → F} (hf : is_linear_map ℝ f) : convex (preimage f s) := hs.linear_preimage $ hf.mk' f lemma convex.neg (hs : convex s) : convex ((λ z, -z) '' s) := hs.is_linear_image is_linear_map.is_linear_map_neg lemma convex.neg_preimage (hs : convex s) : convex ((λ z, -z) ⁻¹' s) := hs.is_linear_preimage is_linear_map.is_linear_map_neg lemma convex.smul (c : ℝ) (hs : convex s) : convex (c • s) := hs.linear_image (linear_map.lsmul _ _ c) lemma convex.smul_preimage (c : ℝ) (hs : convex s) : convex ((λ z, c • z) ⁻¹' s) := hs.linear_preimage (linear_map.lsmul _ _ c) lemma convex.add {t : set E} (hs : convex s) (ht : convex t) : convex (s + t) := by { rw ← add_image_prod, exact (hs.prod ht).is_linear_image is_linear_map.is_linear_map_add } lemma convex.sub {t : set E} (hs : convex s) (ht : convex t) : convex ((λx : E × E, x.1 - x.2) '' (s.prod t)) := (hs.prod ht).is_linear_image is_linear_map.is_linear_map_sub lemma convex.translate (hs : convex s) (z : E) : convex ((λx, z + x) '' s) := hs.affine_image $ affine_map.const ℝ E z +ᵥ affine_map.id ℝ E /-- The translation of a convex set is also convex. -/ lemma convex.translate_preimage_right (hs : convex s) (a : E) : convex ((λ z, a + z) ⁻¹' s) := hs.affine_preimage $ affine_map.const ℝ E a +ᵥ affine_map.id ℝ E /-- The translation of a convex set is also convex. -/ lemma convex.translate_preimage_left (hs : convex s) (a : E) : convex ((λ z, z + a) ⁻¹' s) := by simpa only [add_comm] using hs.translate_preimage_right a lemma convex.affinity (hs : convex s) (z : E) (c : ℝ) : convex ((λx, z + c • x) '' s) := hs.affine_image $ affine_map.const ℝ E z +ᵥ c • affine_map.id ℝ E lemma real.convex_iff_ord_connected {s : set ℝ} : convex s ↔ ord_connected s := begin simp only [convex_iff_segment_subset, segment_eq_interval, ord_connected_iff_interval_subset], exact forall_congr (λ x, forall_swap) end alias real.convex_iff_ord_connected ↔ convex.ord_connected set.ord_connected.convex lemma convex_Iio (r : ℝ) : convex (Iio r) := ord_connected_Iio.convex lemma convex_Ioi (r : ℝ) : convex (Ioi r) := ord_connected_Ioi.convex lemma convex_Iic (r : ℝ) : convex (Iic r) := ord_connected_Iic.convex lemma convex_Ici (r : ℝ) : convex (Ici r) := ord_connected_Ici.convex lemma convex_Ioo (r s : ℝ) : convex (Ioo r s) := ord_connected_Ioo.convex lemma convex_Ico (r s : ℝ) : convex (Ico r s) := ord_connected_Ico.convex lemma convex_Ioc (r : ℝ) (s : ℝ) : convex (Ioc r s) := ord_connected_Ioc.convex lemma convex_Icc (r : ℝ) (s : ℝ) : convex (Icc r s) := ord_connected_Icc.convex lemma convex_interval (r : ℝ) (s : ℝ) : convex (interval r s) := ord_connected_interval.convex lemma convex_segment (a b : E) : convex [a, b] := begin have : (λ (t : ℝ), a + t • (b - a)) = (λz : E, a + z) ∘ (λt:ℝ, t • (b - a)) := rfl, rw [segment_eq_image', this, image_comp], refine ((convex_Icc _ _).is_linear_image _).translate _, exact is_linear_map.is_linear_map_smul' _ end lemma convex_halfspace_lt {f : E → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex {w | f w < r} := (convex_Iio r).is_linear_preimage h lemma convex_halfspace_le {f : E → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex {w | f w ≤ r} := (convex_Iic r).is_linear_preimage h lemma convex_halfspace_gt {f : E → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex {w | r < f w} := (convex_Ioi r).is_linear_preimage h lemma convex_halfspace_ge {f : E → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex {w | r ≤ f w} := (convex_Ici r).is_linear_preimage h lemma convex_hyperplane {f : E → ℝ} (h : is_linear_map ℝ f) (r : ℝ) : convex {w | f w = r} := begin show convex (f ⁻¹' {p | p = r}), rw set_of_eq_eq_singleton, exact (convex_singleton r).is_linear_preimage h end lemma convex_halfspace_re_lt (r : ℝ) : convex {c : ℂ | c.re < r} := convex_halfspace_lt (is_linear_map.mk complex.add_re complex.smul_re) _ lemma convex_halfspace_re_le (r : ℝ) : convex {c : ℂ | c.re ≤ r} := convex_halfspace_le (is_linear_map.mk complex.add_re complex.smul_re) _ lemma convex_halfspace_re_gt (r : ℝ) : convex {c : ℂ | r < c.re } := convex_halfspace_gt (is_linear_map.mk complex.add_re complex.smul_re) _ lemma convex_halfspace_re_lge (r : ℝ) : convex {c : ℂ | r ≤ c.re} := convex_halfspace_ge (is_linear_map.mk complex.add_re complex.smul_re) _ lemma convex_halfspace_im_lt (r : ℝ) : convex {c : ℂ | c.im < r} := convex_halfspace_lt (is_linear_map.mk complex.add_im complex.smul_im) _ lemma convex_halfspace_im_le (r : ℝ) : convex {c : ℂ | c.im ≤ r} := convex_halfspace_le (is_linear_map.mk complex.add_im complex.smul_im) _ lemma convex_halfspace_im_gt (r : ℝ) : convex {c : ℂ | r < c.im } := convex_halfspace_gt (is_linear_map.mk complex.add_im complex.smul_im) _ lemma convex_halfspace_im_lge (r : ℝ) : convex {c : ℂ | r ≤ c.im} := convex_halfspace_ge (is_linear_map.mk complex.add_im complex.smul_im) _ /-! ### Convex combinations in intervals -/ lemma convex.combo_self (a : α) {x y : α} (h : x + y = 1) : a = x * a + y * a := calc a = 1 * a : by rw [one_mul] ... = (x + y) * a : by rw [h] ... = x * a + y * a : by rw [add_mul] /-- If `x` is in an `Ioo`, it can be expressed as a convex combination of the endpoints. -/ lemma convex.mem_Ioo {a b x : α} (h : a < b) : x ∈ Ioo a b ↔ ∃ (x_a x_b : α), 0 < x_a ∧ 0 < x_b ∧ x_a + x_b = 1 ∧ x_a * a + x_b * b = x := begin split, { rintros ⟨h_ax, h_bx⟩, by_cases hab : ¬a < b, { exfalso; exact hab h }, { refine ⟨(b-x) / (b-a), (x-a) / (b-a), _⟩, refine ⟨div_pos (by linarith) (by linarith), div_pos (by linarith) (by linarith),_,_⟩; { field_simp [show b - a ≠ 0, by linarith], ring } } }, { rw [mem_Ioo], rintros ⟨xa, xb, ⟨hxa, hxb, hxaxb, h₂⟩⟩, rw [←h₂], exact ⟨by nlinarith [convex.combo_self a hxaxb], by nlinarith [convex.combo_self b hxaxb]⟩ } end /-- If `x` is in an `Ioc`, it can be expressed as a convex combination of the endpoints. -/ lemma convex.mem_Ioc {a b x : α} (h : a < b) : x ∈ Ioc a b ↔ ∃ (x_a x_b : α), 0 ≤ x_a ∧ 0 < x_b ∧ x_a + x_b = 1 ∧ x_a * a + x_b * b = x := begin split, { rintros ⟨h_ax, h_bx⟩, by_cases h_x : x = b, { exact ⟨0, 1, by linarith, by linarith, by ring, by {rw [h_x], ring}⟩ }, { rcases (convex.mem_Ioo h).mp ⟨h_ax, lt_of_le_of_ne h_bx h_x⟩ with ⟨x_a, x_b, Ioo_case⟩, exact ⟨x_a, x_b, by linarith, Ioo_case.2⟩ } }, { rw [mem_Ioc], rintros ⟨xa, xb, ⟨hxa, hxb, hxaxb, h₂⟩⟩, rw [←h₂], exact ⟨by nlinarith [convex.combo_self a hxaxb], by nlinarith [convex.combo_self b hxaxb]⟩ } end /-- If `x` is in an `Ico`, it can be expressed as a convex combination of the endpoints. -/ lemma convex.mem_Ico {a b x : α} (h : a < b) : x ∈ Ico a b ↔ ∃ (x_a x_b : α), 0 < x_a ∧ 0 ≤ x_b ∧ x_a + x_b = 1 ∧ x_a * a + x_b * b = x := begin split, { rintros ⟨h_ax, h_bx⟩, by_cases h_x : x = a, { exact ⟨1, 0, by linarith, by linarith, by ring, by {rw [h_x], ring}⟩ }, { rcases (convex.mem_Ioo h).mp ⟨lt_of_le_of_ne h_ax (ne.symm h_x), h_bx⟩ with ⟨x_a, x_b, Ioo_case⟩, exact ⟨x_a, x_b, Ioo_case.1, by linarith, (Ioo_case.2).2⟩ } }, { rw [mem_Ico], rintros ⟨xa, xb, ⟨hxa, hxb, hxaxb, h₂⟩⟩, rw [←h₂], exact ⟨by nlinarith [convex.combo_self a hxaxb], by nlinarith [convex.combo_self b hxaxb]⟩ } end /-- If `x` is in an `Icc`, it can be expressed as a convex combination of the endpoints. -/ lemma convex.mem_Icc {a b x : α} (h : a ≤ b) : x ∈ Icc a b ↔ ∃ (x_a x_b : α), 0 ≤ x_a ∧ 0 ≤ x_b ∧ x_a + x_b = 1 ∧ x_a * a + x_b * b = x := begin split, { intro x_in_I, rw [Icc, mem_set_of_eq] at x_in_I, rcases x_in_I with ⟨h_ax, h_bx⟩, by_cases hab' : a = b, { exact ⟨0, 1, le_refl 0, by linarith, by ring, by linarith⟩ }, change a ≠ b at hab', replace h : a < b, exact lt_of_le_of_ne h hab', by_cases h_x : x = a, { exact ⟨1, 0, by linarith, by linarith, by ring, by {rw [h_x], ring}⟩ }, { rcases (convex.mem_Ioc h).mp ⟨lt_of_le_of_ne h_ax (ne.symm h_x), h_bx⟩ with ⟨x_a, x_b, Ioo_case⟩, exact ⟨x_a, x_b, Ioo_case.1, by linarith, (Ioo_case.2).2⟩ } }, { rw [mem_Icc], rintros ⟨xa, xb, ⟨hxa, hxb, hxaxb, h₂⟩⟩, rw [←h₂], exact ⟨by nlinarith [convex.combo_self a hxaxb], by nlinarith [convex.combo_self b hxaxb]⟩ } end section submodule open submodule lemma submodule.convex (K : submodule ℝ E) : convex (↑K : set E) := by { repeat {intro}, refine add_mem _ (smul_mem _ _ _) (smul_mem _ _ _); assumption } lemma subspace.convex (K : subspace ℝ E) : convex (↑K : set E) := K.convex end submodule end sets /-! ### Convex and concave functions -/ section functions variables {β : Type*} [ordered_add_comm_monoid β] [semimodule ℝ β] local notation `[`x `, ` y `]` := segment x y /-- Convexity of functions -/ def convex_on (s : set E) (f : E → β) : Prop := convex s ∧ ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → f (a • x + b • y) ≤ a • f x + b • f y /-- Concavity of functions -/ def concave_on (s : set E) (f : E → β) : Prop := convex s ∧ ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → a • f x + b • f y ≤ f (a • x + b • y) section variables [ordered_semimodule ℝ β] /-- A function `f` is concave iff `-f` is convex. -/ @[simp] lemma neg_convex_on_iff {γ : Type*} [ordered_add_comm_group γ] [semimodule ℝ γ] (s : set E) (f : E → γ) : convex_on s (-f) ↔ concave_on s f := begin split, { rintros ⟨hconv, h⟩, refine ⟨hconv, _⟩, intros x y xs ys a b ha hb hab, specialize h xs ys ha hb hab, simp [neg_apply, neg_le, add_comm] at h, exact h }, { rintros ⟨hconv, h⟩, refine ⟨hconv, _⟩, intros x y xs ys a b ha hb hab, specialize h xs ys ha hb hab, simp [neg_apply, neg_le, add_comm, h] } end /-- A function `f` is concave iff `-f` is convex. -/ @[simp] lemma neg_concave_on_iff {γ : Type*} [ordered_add_comm_group γ] [semimodule ℝ γ] (s : set E) (f : E → γ) : concave_on s (-f) ↔ convex_on s f:= by rw [← neg_convex_on_iff s (-f), neg_neg f] end lemma convex_on_id {s : set ℝ} (hs : convex s) : convex_on s id := ⟨hs, by { intros, refl }⟩ lemma concave_on_id {s : set ℝ} (hs : convex s) : concave_on s id := ⟨hs, by { intros, refl }⟩ lemma convex_on_const (c : β) (hs : convex s) : convex_on s (λ x:E, c) := ⟨hs, by { intros, simp only [← add_smul, *, one_smul] }⟩ lemma concave_on_const (c : β) (hs : convex s) : concave_on s (λ x:E, c) := @convex_on_const _ _ _ _ (order_dual β) _ _ c hs variables {t : set E} lemma convex_on_iff_div {f : E → β} : convex_on s f ↔ convex s ∧ ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → 0 < a + b → f ((a/(a+b)) • x + (b/(a+b)) • y) ≤ (a/(a+b)) • f x + (b/(a+b)) • f y := and_congr iff.rfl ⟨begin intros h x y hx hy a b ha hb hab, apply h hx hy (div_nonneg ha $ le_of_lt hab) (div_nonneg hb $ le_of_lt hab), rw [←add_div], exact div_self (ne_of_gt hab) end, begin intros h x y hx hy a b ha hb hab, simpa [hab, zero_lt_one] using h hx hy ha hb, end⟩ lemma concave_on_iff_div {f : E → β} : concave_on s f ↔ convex s ∧ ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → ∀ ⦃a b : ℝ⦄, 0 ≤ a → 0 ≤ b → 0 < a + b → (a/(a+b)) • f x + (b/(a+b)) • f y ≤ f ((a/(a+b)) • x + (b/(a+b)) • y) := @convex_on_iff_div _ _ _ _ (order_dual β) _ _ _ /-- For a function on a convex set in a linear ordered space, in order to prove that it is convex it suffices to verify the inequality `f (a • x + b • y) ≤ a • f x + b • f y` only for `x < y` and positive `a`, `b`. The main use case is `E = ℝ` however one can apply it, e.g., to `ℝ^n` with lexicographic order. -/ lemma linear_order.convex_on_of_lt {f : E → β} [linear_order E] (hs : convex s) (hf : ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → x < y → ∀ ⦃a b : ℝ⦄, 0 < a → 0 < b → a + b = 1 → f (a • x + b • y) ≤ a • f x + b • f y) : convex_on s f := begin use hs, intros x y hx hy a b ha hb hab, wlog hxy : x<=y using [x y a b, y x b a], { exact le_total _ _ }, { cases eq_or_lt_of_le hxy with hxy hxy, by { subst y, rw [← add_smul, ← add_smul, hab, one_smul, one_smul] }, cases eq_or_lt_of_le ha with ha ha, by { subst a, rw [zero_add] at hab, subst b, simp }, cases eq_or_lt_of_le hb with hb hb, by { subst b, rw [add_zero] at hab, subst a, simp }, exact hf hx hy hxy ha hb hab } end /-- For a function on a convex set in a linear ordered space, in order to prove that it is concave it suffices to verify the inequality `a • f x + b • f y ≤ f (a • x + b • y)` only for `x < y` and positive `a`, `b`. The main use case is `E = ℝ` however one can apply it, e.g., to `ℝ^n` with lexicographic order. -/ lemma linear_order.concave_on_of_lt {f : E → β} [linear_order E] (hs : convex s) (hf : ∀ ⦃x y : E⦄, x ∈ s → y ∈ s → x < y → ∀ ⦃a b : ℝ⦄, 0 < a → 0 < b → a + b = 1 → a • f x + b • f y ≤ f (a • x + b • y)) : concave_on s f := @linear_order.convex_on_of_lt _ _ _ _ (order_dual β) _ _ f _ hs hf /-- For a function `f` defined on a convex subset `D` of `ℝ`, if for any three points `x<y<z` the slope of the secant line of `f` on `[x, y]` is less than or equal to the slope of the secant line of `f` on `[x, z]`, then `f` is convex on `D`. This way of proving convexity of a function is used in the proof of convexity of a function with a monotone derivative. -/ lemma convex_on_real_of_slope_mono_adjacent {s : set ℝ} (hs : convex s) {f : ℝ → ℝ} (hf : ∀ {x y z : ℝ}, x ∈ s → z ∈ s → x < y → y < z → (f y - f x) / (y - x) ≤ (f z - f y) / (z - y)) : convex_on s f := linear_order.convex_on_of_lt hs begin assume x z hx hz hxz a b ha hb hab, let y := a * x + b * z, have hxy : x < y, { rw [← one_mul x, ← hab, add_mul], exact add_lt_add_left ((mul_lt_mul_left hb).2 hxz) _ }, have hyz : y < z, { rw [← one_mul z, ← hab, add_mul], exact add_lt_add_right ((mul_lt_mul_left ha).2 hxz) _ }, have : (f y - f x) * (z - y) ≤ (f z - f y) * (y - x), from (div_le_div_iff (sub_pos.2 hxy) (sub_pos.2 hyz)).1 (hf hx hz hxy hyz), have A : z - y + (y - x) = z - x, by abel, have B : 0 < z - x, from sub_pos.2 (lt_trans hxy hyz), rw [sub_mul, sub_mul, sub_le_iff_le_add', ← add_sub_assoc, le_sub_iff_add_le, ← mul_add, A, ← le_div_iff B, add_div, mul_div_assoc, mul_div_assoc, mul_comm (f x), mul_comm (f z)] at this, rw [eq_comm, ← sub_eq_iff_eq_add] at hab; subst a, convert this; symmetry; simp only [div_eq_iff (ne_of_gt B), y]; ring end /-- For a function `f` defined on a subset `D` of `ℝ`, if `f` is convex on `D`, then for any three points `x<y<z`, the slope of the secant line of `f` on `[x, y]` is less than or equal to the slope of the secant line of `f` on `[x, z]`. -/ lemma convex_on.slope_mono_adjacent {s : set ℝ} {f : ℝ → ℝ} (hf : convex_on s f) {x y z : ℝ} (hx : x ∈ s) (hz : z ∈ s) (hxy : x < y) (hyz : y < z) : (f y - f x) / (y - x) ≤ (f z - f y) / (z - y) := begin have h₁ : 0 < y - x := by linarith, have h₂ : 0 < z - y := by linarith, have h₃ : 0 < z - x := by linarith, suffices : f y / (y - x) + f y / (z - y) ≤ f x / (y - x) + f z / (z - y), by { ring_nf at this ⊢, linarith }, set a := (z - y) / (z - x), set b := (y - x) / (z - x), have heqz : a • x + b • z = y, by { field_simp, rw div_eq_iff; [ring, linarith], }, have key, from hf.2 hx hz (show 0 ≤ a, by apply div_nonneg; linarith) (show 0 ≤ b, by apply div_nonneg; linarith) (show a + b = 1, by { field_simp, rw div_eq_iff; [ring, linarith], }), rw heqz at key, replace key := mul_le_mul_of_nonneg_left key (le_of_lt h₃), field_simp [ne_of_gt h₁, ne_of_gt h₂, ne_of_gt h₃, mul_comm (z - x) _] at key ⊢, rw div_le_div_right, { linarith, }, { nlinarith, }, end /-- For a function `f` defined on a convex subset `D` of `ℝ`, `f` is convex on `D` iff for any three points `x<y<z` the slope of the secant line of `f` on `[x, y]` is less than or equal to the slope of the secant line of `f` on `[x, z]`. -/ lemma convex_on_real_iff_slope_mono_adjacent {s : set ℝ} (hs : convex s) {f : ℝ → ℝ} : convex_on s f ↔ (∀ {x y z : ℝ}, x ∈ s → z ∈ s → x < y → y < z → (f y - f x) / (y - x) ≤ (f z - f y) / (z - y)) := ⟨convex_on.slope_mono_adjacent, convex_on_real_of_slope_mono_adjacent hs⟩ /-- For a function `f` defined on a convex subset `D` of `ℝ`, if for any three points `x<y<z` the slope of the secant line of `f` on `[x, y]` is greater than or equal to the slope of the secant line of `f` on `[x, z]`, then `f` is concave on `D`. -/ lemma concave_on_real_of_slope_mono_adjacent {s : set ℝ} (hs : convex s) {f : ℝ → ℝ} (hf : ∀ {x y z : ℝ}, x ∈ s → z ∈ s → x < y → y < z → (f z - f y) / (z - y) ≤ (f y - f x) / (y - x)) : concave_on s f := begin rw [←neg_convex_on_iff], apply convex_on_real_of_slope_mono_adjacent hs, intros x y z xs zs xy yz, rw [←neg_le_neg_iff, ←neg_div, ←neg_div, neg_sub, neg_sub], simp only [hf xs zs xy yz, neg_sub_neg, pi.neg_apply], end /-- For a function `f` defined on a subset `D` of `ℝ`, if `f` is concave on `D`, then for any three points `x<y<z`, the slope of the secant line of `f` on `[x, y]` is greater than or equal to the slope of the secant line of `f` on `[x, z]`. -/ lemma concave_on.slope_mono_adjacent {s : set ℝ} {f : ℝ → ℝ} (hf : concave_on s f) {x y z : ℝ} (hx : x ∈ s) (hz : z ∈ s) (hxy : x < y) (hyz : y < z) : (f z - f y) / (z - y) ≤ (f y - f x) / (y - x) := begin rw [←neg_le_neg_iff, ←neg_div, ←neg_div, neg_sub, neg_sub], rw [←neg_sub_neg (f y), ←neg_sub_neg (f z)], simp_rw [←pi.neg_apply], rw [←neg_convex_on_iff] at hf, apply convex_on.slope_mono_adjacent hf; assumption, end /-- For a function `f` defined on a convex subset `D` of `ℝ`, `f` is concave on `D` iff for any three points `x<y<z` the slope of the secant line of `f` on `[x, y]` is greater than or equal to the slope of the secant line of `f` on `[x, z]`. -/ lemma concave_on_real_iff_slope_mono_adjacent {s : set ℝ} (hs : convex s) {f : ℝ → ℝ} : concave_on s f ↔ (∀ {x y z : ℝ}, x ∈ s → z ∈ s → x < y → y < z → (f z - f y) / (z - y) ≤ (f y - f x) / (y - x)) := ⟨concave_on.slope_mono_adjacent, concave_on_real_of_slope_mono_adjacent hs⟩ lemma convex_on.subset {f : E → β} (h_convex_on : convex_on t f) (h_subset : s ⊆ t) (h_convex : convex s) : convex_on s f := begin apply and.intro h_convex, intros x y hx hy, exact h_convex_on.2 (h_subset hx) (h_subset hy), end lemma concave_on.subset {f : E → β} (h_concave_on : concave_on t f) (h_subset : s ⊆ t) (h_convex : convex s) : concave_on s f := @convex_on.subset _ _ _ _ (order_dual β) _ _ t f h_concave_on h_subset h_convex lemma convex_on.add {f g : E → β} (hf : convex_on s f) (hg : convex_on s g) : convex_on s (λx, f x + g x) := begin apply and.intro hf.1, intros x y hx hy a b ha hb hab, calc f (a • x + b • y) + g (a • x + b • y) ≤ (a • f x + b • f y) + (a • g x + b • g y) : add_le_add (hf.2 hx hy ha hb hab) (hg.2 hx hy ha hb hab) ... = a • f x + a • g x + b • f y + b • g y : by abel ... = a • (f x + g x) + b • (f y + g y) : by simp [smul_add, add_assoc] end lemma concave_on.add {f g : E → β} (hf : concave_on s f) (hg : concave_on s g) : concave_on s (λx, f x + g x) := @convex_on.add _ _ _ _ (order_dual β) _ _ f g hf hg lemma convex_on.smul [ordered_semimodule ℝ β] {f : E → β} {c : ℝ} (hc : 0 ≤ c) (hf : convex_on s f) : convex_on s (λx, c • f x) := begin apply and.intro hf.1, intros x y hx hy a b ha hb hab, calc c • f (a • x + b • y) ≤ c • (a • f x + b • f y) : smul_le_smul_of_nonneg (hf.2 hx hy ha hb hab) hc ... = a • (c • f x) + b • (c • f y) : by simp only [smul_add, smul_comm c] end lemma concave_on.smul [ordered_semimodule ℝ β] {f : E → β} {c : ℝ} (hc : 0 ≤ c) (hf : concave_on s f) : concave_on s (λx, c • f x) := @convex_on.smul _ _ _ _ (order_dual β) _ _ _ f c hc hf /-- A convex function on a segment is upper-bounded by the max of its endpoints. -/ lemma convex_on.le_on_segment' {γ : Type*} [linear_ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} {x y : E} {a b : ℝ} (hf : convex_on s f) (hx : x ∈ s) (hy : y ∈ s) (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1) : f (a • x + b • y) ≤ max (f x) (f y) := calc f (a • x + b • y) ≤ a • f x + b • f y : hf.2 hx hy ha hb hab ... ≤ a • max (f x) (f y) + b • max (f x) (f y) : add_le_add (smul_le_smul_of_nonneg (le_max_left _ _) ha) (smul_le_smul_of_nonneg (le_max_right _ _) hb) ... ≤ max (f x) (f y) : by rw [←add_smul, hab, one_smul] /-- A concave function on a segment is lower-bounded by the min of its endpoints. -/ lemma concave_on.le_on_segment' {γ : Type*} [linear_ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} {x y : E} {a b : ℝ} (hf : concave_on s f) (hx : x ∈ s) (hy : y ∈ s) (ha : 0 ≤ a) (hb : 0 ≤ b) (hab : a + b = 1) : min (f x) (f y) ≤ f (a • x + b • y) := @convex_on.le_on_segment' _ _ _ _ (order_dual γ) _ _ _ f x y a b hf hx hy ha hb hab /-- A convex function on a segment is upper-bounded by the max of its endpoints. -/ lemma convex_on.le_on_segment {γ : Type*} [linear_ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} (hf : convex_on s f) {x y z : E} (hx : x ∈ s) (hy : y ∈ s) (hz : z ∈ [x, y]) : f z ≤ max (f x) (f y) := let ⟨a, b, ha, hb, hab, hz⟩ := hz in hz ▸ hf.le_on_segment' hx hy ha hb hab /-- A concave function on a segment is lower-bounded by the min of its endpoints. -/ lemma concave_on.le_on_segment {γ : Type*} [linear_ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} (hf : concave_on s f) {x y z : E} (hx : x ∈ s) (hy : y ∈ s) (hz : z ∈ [x, y]) : min (f x) (f y) ≤ f z := @convex_on.le_on_segment _ _ _ _ (order_dual γ) _ _ _ f hf x y z hx hy hz lemma convex_on.convex_le [ordered_semimodule ℝ β] {f : E → β} (hf : convex_on s f) (r : β) : convex {x ∈ s | f x ≤ r} := convex_iff_segment_subset.2 $ λ x y hx hy z hz, begin refine ⟨hf.1.segment_subset hx.1 hy.1 hz,_⟩, rcases hz with ⟨za,zb,hza,hzb,hzazb,H⟩, rw ←H, calc f (za • x + zb • y) ≤ za • (f x) + zb • (f y) : hf.2 hx.1 hy.1 hza hzb hzazb ... ≤ za • r + zb • r : add_le_add (smul_le_smul_of_nonneg hx.2 hza) (smul_le_smul_of_nonneg hy.2 hzb) ... ≤ r : by simp [←add_smul, hzazb] end lemma concave_on.concave_le [ordered_semimodule ℝ β] {f : E → β} (hf : concave_on s f) (r : β) : convex {x ∈ s | r ≤ f x} := @convex_on.convex_le _ _ _ _ (order_dual β) _ _ _ f hf r lemma convex_on.convex_lt {γ : Type*} [ordered_cancel_add_comm_monoid γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} (hf : convex_on s f) (r : γ) : convex {x ∈ s | f x < r} := begin intros a b as bs xa xb hxa hxb hxaxb, refine ⟨hf.1 as.1 bs.1 hxa hxb hxaxb, _⟩, dsimp, by_cases H : xa = 0, { have H' : xb = 1 := by rwa [H, zero_add] at hxaxb, rw [H, H', zero_smul, one_smul, zero_add], exact bs.2 }, { calc f (xa • a + xb • b) ≤ xa • (f a) + xb • (f b) : hf.2 as.1 bs.1 hxa hxb hxaxb ... < xa • r + xb • (f b) : (add_lt_add_iff_right (xb • (f b))).mpr (smul_lt_smul_of_pos as.2 (lt_of_le_of_ne hxa (ne.symm H))) ... ≤ xa • r + xb • r : (add_le_add_iff_left (xa • r)).mpr (smul_le_smul_of_nonneg bs.2.le hxb) ... = r : by simp only [←add_smul, hxaxb, one_smul] } end lemma concave_on.convex_lt {γ : Type*} [ordered_cancel_add_comm_monoid γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} (hf : concave_on s f) (r : γ) : convex {x ∈ s | r < f x} := @convex_on.convex_lt _ _ _ _ (order_dual γ) _ _ _ f hf r lemma convex_on.convex_epigraph {γ : Type*} [ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} (hf : convex_on s f) : convex {p : E × γ | p.1 ∈ s ∧ f p.1 ≤ p.2} := begin rintros ⟨x, r⟩ ⟨y, t⟩ ⟨hx, hr⟩ ⟨hy, ht⟩ a b ha hb hab, refine ⟨hf.1 hx hy ha hb hab, _⟩, calc f (a • x + b • y) ≤ a • f x + b • f y : hf.2 hx hy ha hb hab ... ≤ a • r + b • t : add_le_add (smul_le_smul_of_nonneg hr ha) (smul_le_smul_of_nonneg ht hb) end lemma concave_on.convex_hypograph {γ : Type*} [ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} (hf : concave_on s f) : convex {p : E × γ | p.1 ∈ s ∧ p.2 ≤ f p.1} := @convex_on.convex_epigraph _ _ _ _ (order_dual γ) _ _ _ f hf lemma convex_on_iff_convex_epigraph {γ : Type*} [ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} : convex_on s f ↔ convex {p : E × γ | p.1 ∈ s ∧ f p.1 ≤ p.2} := begin refine ⟨convex_on.convex_epigraph, λ h, ⟨_, _⟩⟩, { assume x y hx hy a b ha hb hab, exact (@h (x, f x) (y, f y) ⟨hx, le_refl _⟩ ⟨hy, le_refl _⟩ a b ha hb hab).1 }, { assume x y hx hy a b ha hb hab, exact (@h (x, f x) (y, f y) ⟨hx, le_refl _⟩ ⟨hy, le_refl _⟩ a b ha hb hab).2 } end lemma concave_on_iff_convex_hypograph {γ : Type*} [ordered_add_comm_group γ] [semimodule ℝ γ] [ordered_semimodule ℝ γ] {f : E → γ} : concave_on s f ↔ convex {p : E × γ | p.1 ∈ s ∧ p.2 ≤ f p.1} := @convex_on_iff_convex_epigraph _ _ _ _ (order_dual γ) _ _ _ f /-- If a function is convex on `s`, it remains convex when precomposed by an affine map. -/ lemma convex_on.comp_affine_map {f : F → β} (g : E →ᵃ[ℝ] F) {s : set F} (hf : convex_on s f) : convex_on (g ⁻¹' s) (f ∘ g) := begin refine ⟨hf.1.affine_preimage _,_⟩, intros x y xs ys a b ha hb hab, calc (f ∘ g) (a • x + b • y) = f (g (a • x + b • y)) : rfl ... = f (a • (g x) + b • (g y)) : by rw [convex.combo_affine_apply hab] ... ≤ a • f (g x) + b • f (g y) : hf.2 xs ys ha hb hab ... = a • (f ∘ g) x + b • (f ∘ g) y : rfl end /-- If a function is concave on `s`, it remains concave when precomposed by an affine map. -/ lemma concave_on.comp_affine_map {f : F → β} (g : E →ᵃ[ℝ] F) {s : set F} (hf : concave_on s f) : concave_on (g ⁻¹' s) (f ∘ g) := @convex_on.comp_affine_map _ _ _ _ _ _ (order_dual β) _ _ f g s hf /-- If `g` is convex on `s`, so is `(g ∘ f)` on `f ⁻¹' s` for a linear `f`. -/ lemma convex_on.comp_linear_map {g : F → β} {s : set F} (hg : convex_on s g) (f : E →ₗ[ℝ] F) : convex_on (f ⁻¹' s) (g ∘ f) := hg.comp_affine_map f.to_affine_map /-- If `g` is concave on `s`, so is `(g ∘ f)` on `f ⁻¹' s` for a linear `f`. -/ lemma concave_on.comp_linear_map {g : F → β} {s : set F} (hg : concave_on s g) (f : E →ₗ[ℝ] F) : concave_on (f ⁻¹' s) (g ∘ f) := hg.comp_affine_map f.to_affine_map /-- If a function is convex on `s`, it remains convex after a translation. -/ lemma convex_on.translate_right {f : E → β} {s : set E} {a : E} (hf : convex_on s f) : convex_on ((λ z, a + z) ⁻¹' s) (f ∘ (λ z, a + z)) := hf.comp_affine_map $ affine_map.const ℝ E a +ᵥ affine_map.id ℝ E /-- If a function is concave on `s`, it remains concave after a translation. -/ lemma concave_on.translate_right {f : E → β} {s : set E} {a : E} (hf : concave_on s f) : concave_on ((λ z, a + z) ⁻¹' s) (f ∘ (λ z, a + z)) := hf.comp_affine_map $ affine_map.const ℝ E a +ᵥ affine_map.id ℝ E /-- If a function is convex on `s`, it remains convex after a translation. -/ lemma convex_on.translate_left {f : E → β} {s : set E} {a : E} (hf : convex_on s f) : convex_on ((λ z, a + z) ⁻¹' s) (f ∘ (λ z, z + a)) := by simpa only [add_comm] using hf.translate_right /-- If a function is concave on `s`, it remains concave after a translation. -/ lemma concave_on.translate_left {f : E → β} {s : set E} {a : E} (hf : concave_on s f) : concave_on ((λ z, a + z) ⁻¹' s) (f ∘ (λ z, z + a)) := by simpa only [add_comm] using hf.translate_right end functions /-! ### Center of mass -/ section center_mass /-- Center of mass of a finite collection of points with prescribed weights. Note that we require neither `0 ≤ w i` nor `∑ w = 1`. -/ noncomputable def finset.center_mass (t : finset ι) (w : ι → ℝ) (z : ι → E) : E := (∑ i in t, w i)⁻¹ • (∑ i in t, w i • z i) variables (i j : ι) (c : ℝ) (t : finset ι) (w : ι → ℝ) (z : ι → E) open finset lemma finset.center_mass_empty : (∅ : finset ι).center_mass w z = 0 := by simp only [center_mass, sum_empty, smul_zero] lemma finset.center_mass_pair (hne : i ≠ j) : ({i, j} : finset ι).center_mass w z = (w i / (w i + w j)) • z i + (w j / (w i + w j)) • z j := by simp only [center_mass, sum_pair hne, smul_add, (mul_smul _ _ _).symm, div_eq_inv_mul] variable {w} lemma finset.center_mass_insert (ha : i ∉ t) (hw : ∑ j in t, w j ≠ 0) : (insert i t).center_mass w z = (w i / (w i + ∑ j in t, w j)) • z i + ((∑ j in t, w j) / (w i + ∑ j in t, w j)) • t.center_mass w z := begin simp only [center_mass, sum_insert ha, smul_add, (mul_smul _ _ _).symm, ← div_eq_inv_mul], congr' 2, rw [div_mul_eq_mul_div, mul_inv_cancel hw, one_div] end lemma finset.center_mass_singleton (hw : w i ≠ 0) : ({i} : finset ι).center_mass w z = z i := by rw [center_mass, sum_singleton, sum_singleton, ← mul_smul, inv_mul_cancel hw, one_smul] lemma finset.center_mass_eq_of_sum_1 (hw : ∑ i in t, w i = 1) : t.center_mass w z = ∑ i in t, w i • z i := by simp only [finset.center_mass, hw, inv_one, one_smul] lemma finset.center_mass_smul : t.center_mass w (λ i, c • z i) = c • t.center_mass w z := by simp only [finset.center_mass, finset.smul_sum, (mul_smul _ _ _).symm, mul_comm c, mul_assoc] /-- A convex combination of two centers of mass is a center of mass as well. This version deals with two different index types. -/ lemma finset.center_mass_segment' (s : finset ι) (t : finset ι') (ws : ι → ℝ) (zs : ι → E) (wt : ι' → ℝ) (zt : ι' → E) (hws : ∑ i in s, ws i = 1) (hwt : ∑ i in t, wt i = 1) (a b : ℝ) (hab : a + b = 1) : a • s.center_mass ws zs + b • t.center_mass wt zt = (s.map function.embedding.inl ∪ t.map function.embedding.inr).center_mass (sum.elim (λ i, a * ws i) (λ j, b * wt j)) (sum.elim zs zt) := begin rw [s.center_mass_eq_of_sum_1 _ hws, t.center_mass_eq_of_sum_1 _ hwt, smul_sum, smul_sum, ← finset.sum_sum_elim, finset.center_mass_eq_of_sum_1], { congr' with ⟨⟩; simp only [sum.elim_inl, sum.elim_inr, mul_smul] }, { rw [sum_sum_elim, ← mul_sum, ← mul_sum, hws, hwt, mul_one, mul_one, hab] } end /-- A convex combination of two centers of mass is a center of mass as well. This version works if two centers of mass share the set of original points. -/ lemma finset.center_mass_segment (s : finset ι) (w₁ w₂ : ι → ℝ) (z : ι → E) (hw₁ : ∑ i in s, w₁ i = 1) (hw₂ : ∑ i in s, w₂ i = 1) (a b : ℝ) (hab : a + b = 1) : a • s.center_mass w₁ z + b • s.center_mass w₂ z = s.center_mass (λ i, a * w₁ i + b * w₂ i) z := have hw : ∑ i in s, (a * w₁ i + b * w₂ i) = 1, by simp only [mul_sum.symm, sum_add_distrib, mul_one, *], by simp only [finset.center_mass_eq_of_sum_1, smul_sum, sum_add_distrib, add_smul, mul_smul, *] lemma finset.center_mass_ite_eq (hi : i ∈ t) : t.center_mass (λ j, if (i = j) then 1 else 0) z = z i := begin rw [finset.center_mass_eq_of_sum_1], transitivity ∑ j in t, if (i = j) then z i else 0, { congr' with i, split_ifs, exacts [h ▸ one_smul _ _, zero_smul _ _] }, { rw [sum_ite_eq, if_pos hi] }, { rw [sum_ite_eq, if_pos hi] } end variables {t w} lemma finset.center_mass_subset {t' : finset ι} (ht : t ⊆ t') (h : ∀ i ∈ t', i ∉ t → w i = 0) : t.center_mass w z = t'.center_mass w z := begin rw [center_mass, sum_subset ht h, smul_sum, center_mass, smul_sum], apply sum_subset ht, assume i hit' hit, rw [h i hit' hit, zero_smul, smul_zero] end lemma finset.center_mass_filter_ne_zero : (t.filter (λ i, w i ≠ 0)).center_mass w z = t.center_mass w z := finset.center_mass_subset z (filter_subset _ _) $ λ i hit hit', by simpa only [hit, mem_filter, true_and, ne.def, not_not] using hit' variable {z} /-- The center of mass of a finite subset of a convex set belongs to the set provided that all weights are non-negative, and the total weight is positive. -/ lemma convex.center_mass_mem (hs : convex s) : (∀ i ∈ t, 0 ≤ w i) → (0 < ∑ i in t, w i) → (∀ i ∈ t, z i ∈ s) → t.center_mass w z ∈ s := begin induction t using finset.induction with i t hi ht, { simp [lt_irrefl] }, intros h₀ hpos hmem, have zi : z i ∈ s, from hmem _ (mem_insert_self _ _), have hs₀ : ∀ j ∈ t, 0 ≤ w j, from λ j hj, h₀ j $ mem_insert_of_mem hj, rw [sum_insert hi] at hpos, by_cases hsum_t : ∑ j in t, w j = 0, { have ws : ∀ j ∈ t, w j = 0, from (sum_eq_zero_iff_of_nonneg hs₀).1 hsum_t, have wz : ∑ j in t, w j • z j = 0, from sum_eq_zero (λ i hi, by simp [ws i hi]), simp only [center_mass, sum_insert hi, wz, hsum_t, add_zero], simp only [hsum_t, add_zero] at hpos, rw [← mul_smul, inv_mul_cancel (ne_of_gt hpos), one_smul], exact zi }, { rw [finset.center_mass_insert _ _ _ hi hsum_t], refine convex_iff_div.1 hs zi (ht hs₀ _ _) _ (sum_nonneg hs₀) hpos, { exact lt_of_le_of_ne (sum_nonneg hs₀) (ne.symm hsum_t) }, { intros j hj, exact hmem j (mem_insert_of_mem hj) }, { exact h₀ _ (mem_insert_self _ _) } } end lemma convex.sum_mem (hs : convex s) (h₀ : ∀ i ∈ t, 0 ≤ w i) (h₁ : ∑ i in t, w i = 1) (hz : ∀ i ∈ t, z i ∈ s) : ∑ i in t, w i • z i ∈ s := by simpa only [h₁, center_mass, inv_one, one_smul] using hs.center_mass_mem h₀ (h₁.symm ▸ zero_lt_one) hz lemma convex_iff_sum_mem : convex s ↔ (∀ (t : finset E) (w : E → ℝ), (∀ i ∈ t, 0 ≤ w i) → ∑ i in t, w i = 1 → (∀ x ∈ t, x ∈ s) → ∑ x in t, w x • x ∈ s ) := begin refine ⟨λ hs t w hw₀ hw₁ hts, hs.sum_mem hw₀ hw₁ hts, _⟩, intros h x y hx hy a b ha hb hab, by_cases h_cases: x = y, { rw [h_cases, ←add_smul, hab, one_smul], exact hy }, { convert h {x, y} (λ z, if z = y then b else a) _ _ _, { simp only [sum_pair h_cases, if_neg h_cases, if_pos rfl] }, { simp_intros i hi, cases hi; subst i; simp [ha, hb, if_neg h_cases] }, { simp only [sum_pair h_cases, if_neg h_cases, if_pos rfl, hab] }, { simp_intros i hi, cases hi; subst i; simp [hx, hy, if_neg h_cases] } } end /-- Jensen's inequality, `finset.center_mass` version. -/ lemma convex_on.map_center_mass_le {f : E → ℝ} (hf : convex_on s f) (h₀ : ∀ i ∈ t, 0 ≤ w i) (hpos : 0 < ∑ i in t, w i) (hmem : ∀ i ∈ t, z i ∈ s) : f (t.center_mass w z) ≤ t.center_mass w (f ∘ z) := begin have hmem' : ∀ i ∈ t, (z i, (f ∘ z) i) ∈ {p : E × ℝ | p.1 ∈ s ∧ f p.1 ≤ p.2}, from λ i hi, ⟨hmem i hi, le_refl _⟩, convert (hf.convex_epigraph.center_mass_mem h₀ hpos hmem').2; simp only [center_mass, function.comp, prod.smul_fst, prod.fst_sum, prod.smul_snd, prod.snd_sum] end /-- Jensen's inequality, `finset.sum` version. -/ lemma convex_on.map_sum_le {f : E → ℝ} (hf : convex_on s f) (h₀ : ∀ i ∈ t, 0 ≤ w i) (h₁ : ∑ i in t, w i = 1) (hmem : ∀ i ∈ t, z i ∈ s) : f (∑ i in t, w i • z i) ≤ ∑ i in t, w i * (f (z i)) := by simpa only [center_mass, h₁, inv_one, one_smul] using hf.map_center_mass_le h₀ (h₁.symm ▸ zero_lt_one) hmem /-- If a function `f` is convex on `s` takes value `y` at the center of mass of some points `z i ∈ s`, then for some `i` we have `y ≤ f (z i)`. -/ lemma convex_on.exists_ge_of_center_mass {f : E → ℝ} (h : convex_on s f) (hw₀ : ∀ i ∈ t, 0 ≤ w i) (hws : 0 < ∑ i in t, w i) (hz : ∀ i ∈ t, z i ∈ s) : ∃ i ∈ t, f (t.center_mass w z) ≤ f (z i) := begin set y := t.center_mass w z, have : f y ≤ t.center_mass w (f ∘ z) := h.map_center_mass_le hw₀ hws hz, rw ← sum_filter_ne_zero at hws, rw [← finset.center_mass_filter_ne_zero (f ∘ z), center_mass, smul_eq_mul, ← div_eq_inv_mul, le_div_iff hws, mul_sum] at this, replace : ∃ i ∈ t.filter (λ i, w i ≠ 0), f y * w i ≤ w i • (f ∘ z) i := exists_le_of_sum_le (nonempty_of_sum_ne_zero (ne_of_gt hws)) this, rcases this with ⟨i, hi, H⟩, rw [mem_filter] at hi, use [i, hi.1], simp only [smul_eq_mul, mul_comm (w i)] at H, refine (mul_le_mul_right _).1 H, exact lt_of_le_of_ne (hw₀ i hi.1) hi.2.symm end end center_mass /-! ### Convex hull -/ section convex_hull variable {t : set E} /-- The convex hull of a set `s` is the minimal convex set that includes `s`. -/ def convex_hull (s : set E) : set E := ⋂ (t : set E) (hst : s ⊆ t) (ht : convex t), t variable (s) lemma subset_convex_hull : s ⊆ convex_hull s := set.subset_Inter $ λ t, set.subset_Inter $ λ hst, set.subset_Inter $ λ ht, hst lemma convex_convex_hull : convex (convex_hull s) := convex_Inter $ λ t, convex_Inter $ λ ht, convex_Inter id variable {s} lemma convex_hull_min (hst : s ⊆ t) (ht : convex t) : convex_hull s ⊆ t := set.Inter_subset_of_subset t $ set.Inter_subset_of_subset hst $ set.Inter_subset _ ht lemma convex_hull_mono (hst : s ⊆ t) : convex_hull s ⊆ convex_hull t := convex_hull_min (set.subset.trans hst $ subset_convex_hull t) (convex_convex_hull t) lemma convex.convex_hull_eq {s : set E} (hs : convex s) : convex_hull s = s := set.subset.antisymm (convex_hull_min (set.subset.refl _) hs) (subset_convex_hull s) @[simp] lemma convex_hull_singleton {x : E} : convex_hull ({x} : set E) = {x} := (convex_singleton x).convex_hull_eq lemma is_linear_map.image_convex_hull {f : E → F} (hf : is_linear_map ℝ f) : f '' (convex_hull s) = convex_hull (f '' s) := begin refine set.subset.antisymm _ _, { rw [set.image_subset_iff], exact convex_hull_min (set.image_subset_iff.1 $ subset_convex_hull $ f '' s) ((convex_convex_hull (f '' s)).is_linear_preimage hf) }, { exact convex_hull_min (set.image_subset _ $ subset_convex_hull s) ((convex_convex_hull s).is_linear_image hf) } end lemma linear_map.image_convex_hull (f : E →ₗ[ℝ] F) : f '' (convex_hull s) = convex_hull (f '' s) := f.is_linear.image_convex_hull lemma finset.center_mass_mem_convex_hull (t : finset ι) {w : ι → ℝ} (hw₀ : ∀ i ∈ t, 0 ≤ w i) (hws : 0 < ∑ i in t, w i) {z : ι → E} (hz : ∀ i ∈ t, z i ∈ s) : t.center_mass w z ∈ convex_hull s := (convex_convex_hull s).center_mass_mem hw₀ hws (λ i hi, subset_convex_hull s $ hz i hi) -- TODO : Do we need other versions of the next lemma? /-- Convex hull of `s` is equal to the set of all centers of masses of `finset`s `t`, `z '' t ⊆ s`. This version allows finsets in any type in any universe. -/ lemma convex_hull_eq (s : set E) : convex_hull s = {x : E | ∃ (ι : Type u') (t : finset ι) (w : ι → ℝ) (z : ι → E) (hw₀ : ∀ i ∈ t, 0 ≤ w i) (hw₁ : ∑ i in t, w i = 1) (hz : ∀ i ∈ t, z i ∈ s), t.center_mass w z = x} := begin refine subset.antisymm (convex_hull_min _ _) _, { intros x hx, use [punit, {punit.star}, λ _, 1, λ _, x, λ _ _, zero_le_one, finset.sum_singleton, λ _ _, hx], simp only [finset.center_mass, finset.sum_singleton, inv_one, one_smul] }, { rintros x y ⟨ι, sx, wx, zx, hwx₀, hwx₁, hzx, rfl⟩ ⟨ι', sy, wy, zy, hwy₀, hwy₁, hzy, rfl⟩ a b ha hb hab, rw [finset.center_mass_segment' _ _ _ _ _ _ hwx₁ hwy₁ _ _ hab], refine ⟨_, _, _, _, _, _, _, rfl⟩, { rintros i hi, rw [finset.mem_union, finset.mem_map, finset.mem_map] at hi, rcases hi with ⟨j, hj, rfl⟩|⟨j, hj, rfl⟩; simp only [sum.elim_inl, sum.elim_inr]; apply_rules [mul_nonneg, hwx₀, hwy₀] }, { simp [finset.sum_sum_elim, finset.mul_sum.symm, *] }, { intros i hi, rw [finset.mem_union, finset.mem_map, finset.mem_map] at hi, rcases hi with ⟨j, hj, rfl⟩|⟨j, hj, rfl⟩; apply_rules [hzx, hzy] } }, { rintros _ ⟨ι, t, w, z, hw₀, hw₁, hz, rfl⟩, exact t.center_mass_mem_convex_hull hw₀ (hw₁.symm ▸ zero_lt_one) hz } end /-- Maximum principle for convex functions. If a function `f` is convex on the convex hull of `s`, then `f` can't have a maximum on `convex_hull s` outside of `s`. -/ lemma convex_on.exists_ge_of_mem_convex_hull {f : E → ℝ} (hf : convex_on (convex_hull s) f) {x} (hx : x ∈ convex_hull s) : ∃ y ∈ s, f x ≤ f y := begin rw convex_hull_eq at hx, rcases hx with ⟨α, t, w, z, hw₀, hw₁, hz, rfl⟩, rcases hf.exists_ge_of_center_mass hw₀ (hw₁.symm ▸ zero_lt_one) (λ i hi, subset_convex_hull s (hz i hi)) with ⟨i, hit, Hi⟩, exact ⟨z i, hz i hit, Hi⟩ end lemma finset.convex_hull_eq (s : finset E) : convex_hull ↑s = {x : E | ∃ (w : E → ℝ) (hw₀ : ∀ y ∈ s, 0 ≤ w y) (hw₁ : ∑ y in s, w y = 1), s.center_mass w id = x} := begin refine subset.antisymm (convex_hull_min _ _) _, { intros x hx, rw [finset.mem_coe] at hx, refine ⟨_, _, _, finset.center_mass_ite_eq _ _ _ hx⟩, { intros, split_ifs, exacts [zero_le_one, le_refl 0] }, { rw [finset.sum_ite_eq, if_pos hx] } }, { rintros x y ⟨wx, hwx₀, hwx₁, rfl⟩ ⟨wy, hwy₀, hwy₁, rfl⟩ a b ha hb hab, rw [finset.center_mass_segment _ _ _ _ hwx₁ hwy₁ _ _ hab], refine ⟨_, _, _, rfl⟩, { rintros i hi, apply_rules [add_nonneg, mul_nonneg, hwx₀, hwy₀], }, { simp only [finset.sum_add_distrib, finset.mul_sum.symm, mul_one, *] } }, { rintros _ ⟨w, hw₀, hw₁, rfl⟩, exact s.center_mass_mem_convex_hull (λ x hx, hw₀ _ hx) (hw₁.symm ▸ zero_lt_one) (λ x hx, hx) } end lemma set.finite.convex_hull_eq {s : set E} (hs : finite s) : convex_hull s = {x : E | ∃ (w : E → ℝ) (hw₀ : ∀ y ∈ s, 0 ≤ w y) (hw₁ : ∑ y in hs.to_finset, w y = 1), hs.to_finset.center_mass w id = x} := by simpa only [set.finite.coe_to_finset, set.finite.mem_to_finset, exists_prop] using hs.to_finset.convex_hull_eq lemma convex_hull_eq_union_convex_hull_finite_subsets (s : set E) : convex_hull s = ⋃ (t : finset E) (w : ↑t ⊆ s), convex_hull ↑t := begin refine subset.antisymm _ _, { rw [convex_hull_eq.{u}], rintros x ⟨ι, t, w, z, hw₀, hw₁, hz, rfl⟩, simp only [mem_Union], refine ⟨t.image z, _, _⟩, { rw [finset.coe_image, image_subset_iff], exact hz }, { apply t.center_mass_mem_convex_hull hw₀, { simp only [hw₁, zero_lt_one] }, { exact λ i hi, finset.mem_coe.2 (finset.mem_image_of_mem _ hi) } } }, { exact Union_subset (λ i, Union_subset convex_hull_mono), }, end lemma is_linear_map.convex_hull_image {f : E → F} (hf : is_linear_map ℝ f) (s : set E) : convex_hull (f '' s) = f '' convex_hull s := set.subset.antisymm (convex_hull_min (image_subset _ (subset_convex_hull s)) $ (convex_convex_hull s).is_linear_image hf) (image_subset_iff.2 $ convex_hull_min (image_subset_iff.1 $ subset_convex_hull _) ((convex_convex_hull _).is_linear_preimage hf)) lemma linear_map.convex_hull_image (f : E →ₗ[ℝ] F) (s : set E) : convex_hull (f '' s) = f '' convex_hull s := f.is_linear.convex_hull_image s end convex_hull /-! ### Simplex -/ section simplex variables (ι) [fintype ι] {f : ι → ℝ} /-- The standard simplex in the space of functions `ι → ℝ` is the set of vectors with non-negative coordinates with total sum `1`. -/ def std_simplex (ι : Type*) [fintype ι] : set (ι → ℝ) := {f | (∀ x, 0 ≤ f x) ∧ ∑ x, f x = 1} lemma std_simplex_eq_inter : std_simplex ι = (⋂ x, {f | 0 ≤ f x}) ∩ {f | ∑ x, f x = 1} := by { ext f, simp only [std_simplex, set.mem_inter_eq, set.mem_Inter, set.mem_set_of_eq] } lemma convex_std_simplex : convex (std_simplex ι) := begin refine λ f g hf hg a b ha hb hab, ⟨λ x, _, _⟩, { apply_rules [add_nonneg, mul_nonneg, hf.1, hg.1] }, { erw [finset.sum_add_distrib, ← finset.smul_sum, ← finset.smul_sum, hf.2, hg.2, smul_eq_mul, smul_eq_mul, mul_one, mul_one], exact hab } end variable {ι} lemma ite_eq_mem_std_simplex (i : ι) : (λ j, ite (i = j) (1:ℝ) 0) ∈ std_simplex ι := ⟨λ j, by simp only; split_ifs; norm_num, by rw [finset.sum_ite_eq, if_pos (finset.mem_univ _)]⟩ /-- `std_simplex ι` is the convex hull of the canonical basis in `ι → ℝ`. -/ lemma convex_hull_basis_eq_std_simplex : convex_hull (range $ λ(i j:ι), if i = j then (1:ℝ) else 0) = std_simplex ι := begin refine subset.antisymm (convex_hull_min _ (convex_std_simplex ι)) _, { rintros _ ⟨i, rfl⟩, exact ite_eq_mem_std_simplex i }, { rintros w ⟨hw₀, hw₁⟩, rw [pi_eq_sum_univ w, ← finset.univ.center_mass_eq_of_sum_1 _ hw₁], exact finset.univ.center_mass_mem_convex_hull (λ i hi, hw₀ i) (hw₁.symm ▸ zero_lt_one) (λ i hi, mem_range_self i) } end variable {ι} /-- The convex hull of a finite set is the image of the standard simplex in `s → ℝ` under the linear map sending each function `w` to `∑ x in s, w x • x`. Since we have no sums over finite sets, we use sum over `@finset.univ _ hs.fintype`. The map is defined in terms of operations on `(s → ℝ) →ₗ[ℝ] ℝ` so that later we will not need to prove that this map is linear. -/ lemma set.finite.convex_hull_eq_image {s : set E} (hs : finite s) : convex_hull s = by haveI := hs.fintype; exact (⇑(∑ x : s, (@linear_map.proj ℝ s _ (λ i, ℝ) _ _ x).smul_right x.1)) '' (std_simplex s) := begin rw [← convex_hull_basis_eq_std_simplex, ← linear_map.convex_hull_image, ← set.range_comp, (∘)], apply congr_arg, convert subtype.range_coe.symm, ext x, simp [linear_map.sum_apply, ite_smul, finset.filter_eq] end /-- All values of a function `f ∈ std_simplex ι` belong to `[0, 1]`. -/ lemma mem_Icc_of_mem_std_simplex (hf : f ∈ std_simplex ι) (x) : f x ∈ I := ⟨hf.1 x, hf.2 ▸ finset.single_le_sum (λ y hy, hf.1 y) (finset.mem_univ x)⟩ end simplex
7636df8c1e8bbd239298e57d50a9f54d1da2215a
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/blast_fun_info_bug.lean
1d04e482619097890bc65dc9afda7cb852707aa0
[ "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
267
lean
definition set (A : Type) := A → Prop set_option blast.strategy "preprocess" example {A : Type} (s : set A) (a b : A) : a = b → s a → s b := by blast set_option blast.strategy "cc" example {A : Type} (s : set A) (a b : A) : a = b → s a → s b := by blast
e515c5aea8d6afa8ddbdc43588ac68a54d4b9366
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/analysis/topology.lean
36a161a35f71c80351e3cfc28de9bfe4715b0f49
[ "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
9,919
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.analysis.filter import topology.bases /-! # Computational realization of topological spaces (experimental) This file provides infrastructure to compute with topological spaces. ## Main declarations * `ctop`: Realization of a topology basis. * `ctop.realizer`: Realization of a topological space. `ctop` that generates the given topology. * `locally_finite.realizer`: Realization of the local finiteness of an indexed family of sets. * `compact.realizer`: Realization of the compactness of a set. -/ open set open filter (hiding realizer) open_locale topological_space /-- A `ctop α σ` is a realization of a topology (basis) on `α`, represented by a type `σ` together with operations for the top element and the intersection operation. -/ structure ctop (α σ : Type*) := (f : σ → set α) (top : α → σ) (top_mem : ∀ x : α, x ∈ f (top x)) (inter : Π a b (x : α), x ∈ f a ∩ f b → σ) (inter_mem : ∀ a b x h, x ∈ f (inter a b x h)) (inter_sub : ∀ a b x h, f (inter a b x h) ⊆ f a ∩ f b) variables {α : Type*} {β : Type*} {σ : Type*} {τ : Type*} instance : inhabited (ctop α (set α)) := ⟨{ f := id, top := singleton, top_mem := mem_singleton, inter := λ s t _ _, s ∩ t, inter_mem := λ s t a, id, inter_sub := λ s t a ha, subset.rfl }⟩ namespace ctop section variables (F : ctop α σ) instance : has_coe_to_fun (ctop α σ) (λ _, σ → set α) := ⟨ctop.f⟩ @[simp] theorem coe_mk (f T h₁ I h₂ h₃ a) : (@ctop.mk α σ f T h₁ I h₂ h₃) a = f a := rfl /-- Map a ctop to an equivalent representation type. -/ def of_equiv (E : σ ≃ τ) : ctop α σ → ctop α τ | ⟨f, T, h₁, I, h₂, h₃⟩ := { f := λ a, f (E.symm a), top := λ x, E (T x), top_mem := λ x, by simpa using h₁ x, inter := λ a b x h, E (I (E.symm a) (E.symm b) x h), inter_mem := λ a b x h, by simpa using h₂ (E.symm a) (E.symm b) x h, inter_sub := λ a b x h, by simpa using h₃ (E.symm a) (E.symm b) x h } @[simp] theorem of_equiv_val (E : σ ≃ τ) (F : ctop α σ) (a : τ) : F.of_equiv E a = F (E.symm a) := by cases F; refl end /-- Every `ctop` is a topological space. -/ def to_topsp (F : ctop α σ) : topological_space α := topological_space.generate_from (set.range F.f) theorem to_topsp_is_topological_basis (F : ctop α σ) : @topological_space.is_topological_basis _ F.to_topsp (set.range F.f) := by letI := F.to_topsp; exact ⟨λ u ⟨a, e₁⟩ v ⟨b, e₂⟩, e₁ ▸ e₂ ▸ λ x h, ⟨_, ⟨_, rfl⟩, F.inter_mem a b x h, F.inter_sub a b x h⟩, eq_univ_iff_forall.2 $ λ x, ⟨_, ⟨_, rfl⟩, F.top_mem x⟩, rfl⟩ @[simp] theorem mem_nhds_to_topsp (F : ctop α σ) {s : set α} {a : α} : s ∈ @nhds _ F.to_topsp a ↔ ∃ b, a ∈ F b ∧ F b ⊆ s := (@topological_space.is_topological_basis.mem_nhds_iff _ F.to_topsp _ _ _ F.to_topsp_is_topological_basis).trans $ ⟨λ ⟨_, ⟨x, rfl⟩, h⟩, ⟨x, h⟩, λ ⟨x, h⟩, ⟨_, ⟨x, rfl⟩, h⟩⟩ end ctop /-- A `ctop` realizer for the topological space `T` is a `ctop` which generates `T`. -/ structure ctop.realizer (α) [T : topological_space α] := (σ : Type*) (F : ctop α σ) (eq : F.to_topsp = T) open ctop /-- A `ctop` realizes the topological space it generates. -/ protected def ctop.to_realizer (F : ctop α σ) : @ctop.realizer _ F.to_topsp := @ctop.realizer.mk _ F.to_topsp σ F rfl instance (F : ctop α σ) : inhabited (@ctop.realizer _ F.to_topsp) := ⟨F.to_realizer⟩ namespace ctop.realizer protected theorem is_basis [T : topological_space α] (F : realizer α) : topological_space.is_topological_basis (set.range F.F.f) := by have := to_topsp_is_topological_basis F.F; rwa F.eq at this protected theorem mem_nhds [T : topological_space α] (F : realizer α) {s : set α} {a : α} : s ∈ 𝓝 a ↔ ∃ b, a ∈ F.F b ∧ F.F b ⊆ s := by have := mem_nhds_to_topsp F.F; rwa F.eq at this theorem is_open_iff [topological_space α] (F : realizer α) {s : set α} : is_open s ↔ ∀ a ∈ s, ∃ b, a ∈ F.F b ∧ F.F b ⊆ s := is_open_iff_mem_nhds.trans $ ball_congr $ λ a h, F.mem_nhds theorem is_closed_iff [topological_space α] (F : realizer α) {s : set α} : is_closed s ↔ ∀ a, (∀ b, a ∈ F.F b → ∃ z, z ∈ F.F b ∩ s) → a ∈ s := is_open_compl_iff.symm.trans $ F.is_open_iff.trans $ forall_congr $ λ a, show (a ∉ s → (∃ (b : F.σ), a ∈ F.F b ∧ ∀ z ∈ F.F b, z ∉ s)) ↔ _, by haveI := classical.prop_decidable; rw [not_imp_comm]; simp [not_exists, not_and, not_forall, and_comm] theorem mem_interior_iff [topological_space α] (F : realizer α) {s : set α} {a : α} : a ∈ interior s ↔ ∃ b, a ∈ F.F b ∧ F.F b ⊆ s := mem_interior_iff_mem_nhds.trans F.mem_nhds protected theorem is_open [topological_space α] (F : realizer α) (s : F.σ) : is_open (F.F s) := is_open_iff_nhds.2 $ λ a m, by simpa using F.mem_nhds.2 ⟨s, m, subset.refl _⟩ theorem ext' [T : topological_space α] {σ : Type*} {F : ctop α σ} (H : ∀ a s, s ∈ 𝓝 a ↔ ∃ b, a ∈ F b ∧ F b ⊆ s) : F.to_topsp = T := begin refine eq_of_nhds_eq_nhds (λ x, _), ext s, rw [mem_nhds_to_topsp, H] end theorem ext [T : topological_space α] {σ : Type*} {F : ctop α σ} (H₁ : ∀ a, is_open (F a)) (H₂ : ∀ a s, s ∈ 𝓝 a → ∃ b, a ∈ F b ∧ F b ⊆ s) : F.to_topsp = T := ext' $ λ a s, ⟨H₂ a s, λ ⟨b, h₁, h₂⟩, mem_nhds_iff.2 ⟨_, h₂, H₁ _, h₁⟩⟩ variable [topological_space α] /-- The topological space realizer made of the open sets. -/ protected def id : realizer α := ⟨{x:set α // is_open x}, { f := subtype.val, top := λ _, ⟨univ, is_open_univ⟩, top_mem := mem_univ, inter := λ ⟨x, h₁⟩ ⟨y, h₂⟩ a h₃, ⟨_, h₁.inter h₂⟩, inter_mem := λ ⟨x, h₁⟩ ⟨y, h₂⟩ a, id, inter_sub := λ ⟨x, h₁⟩ ⟨y, h₂⟩ a h₃, subset.refl _ }, ext subtype.property $ λ x s h, let ⟨t, h, o, m⟩ := mem_nhds_iff.1 h in ⟨⟨t, o⟩, m, h⟩⟩ /-- Replace the representation type of a `ctop` realizer. -/ def of_equiv (F : realizer α) (E : F.σ ≃ τ) : realizer α := ⟨τ, F.F.of_equiv E, ext' (λ a s, F.mem_nhds.trans $ ⟨λ ⟨s, h⟩, ⟨E s, by simpa using h⟩, λ ⟨t, h⟩, ⟨E.symm t, by simpa using h⟩⟩)⟩ @[simp] theorem of_equiv_σ (F : realizer α) (E : F.σ ≃ τ) : (F.of_equiv E).σ = τ := rfl @[simp] theorem of_equiv_F (F : realizer α) (E : F.σ ≃ τ) (s : τ) : (F.of_equiv E).F s = F.F (E.symm s) := by delta of_equiv; simp /-- A realizer of the neighborhood of a point. -/ protected def nhds (F : realizer α) (a : α) : (𝓝 a).realizer := ⟨{s : F.σ // a ∈ F.F s}, { f := λ s, F.F s.1, pt := ⟨_, F.F.top_mem a⟩, inf := λ ⟨x, h₁⟩ ⟨y, h₂⟩, ⟨_, F.F.inter_mem x y a ⟨h₁, h₂⟩⟩, inf_le_left := λ ⟨x, h₁⟩ ⟨y, h₂⟩ z h, (F.F.inter_sub x y a ⟨h₁, h₂⟩ h).1, inf_le_right := λ ⟨x, h₁⟩ ⟨y, h₂⟩ z h, (F.F.inter_sub x y a ⟨h₁, h₂⟩ h).2 }, filter_eq $ set.ext $ λ x, ⟨λ ⟨⟨s, as⟩, h⟩, mem_nhds_iff.2 ⟨_, h, F.is_open _, as⟩, λ h, let ⟨s, h, as⟩ := F.mem_nhds.1 h in ⟨⟨s, h⟩, as⟩⟩⟩ @[simp] lemma nhds_σ (F : realizer α) (a : α) : (F.nhds a).σ = {s : F.σ // a ∈ F.F s} := rfl @[simp] lemma nhds_F (F : realizer α) (a : α) (s) : (F.nhds a).F s = F.F s.1 := rfl theorem tendsto_nhds_iff {m : β → α} {f : filter β} (F : f.realizer) (R : realizer α) {a : α} : tendsto m f (𝓝 a) ↔ ∀ t, a ∈ R.F t → ∃ s, ∀ x ∈ F.F s, m x ∈ R.F t := (F.tendsto_iff _ (R.nhds a)).trans subtype.forall end ctop.realizer /-- A `locally_finite.realizer F f` is a realization that `f` is locally finite, namely it is a choice of open sets from the basis of `F` such that they intersect only finitely many of the values of `f`. -/ structure locally_finite.realizer [topological_space α] (F : realizer α) (f : β → set α) := (bas : ∀ a, {s // a ∈ F.F s}) (sets : ∀ x:α, fintype {i | (f i ∩ F.F (bas x)).nonempty}) theorem locally_finite.realizer.to_locally_finite [topological_space α] {F : realizer α} {f : β → set α} (R : locally_finite.realizer F f) : locally_finite f := λ a, ⟨_, F.mem_nhds.2 ⟨(R.bas a).1, (R.bas a).2, subset.refl _⟩, ⟨R.sets a⟩⟩ theorem locally_finite_iff_exists_realizer [topological_space α] (F : realizer α) {f : β → set α} : locally_finite f ↔ nonempty (locally_finite.realizer F f) := ⟨λ h, let ⟨g, h₁⟩ := classical.axiom_of_choice h, ⟨g₂, h₂⟩ := classical.axiom_of_choice (λ x, show ∃ (b : F.σ), x ∈ (F.F) b ∧ (F.F) b ⊆ g x, from let ⟨h, h'⟩ := h₁ x in F.mem_nhds.1 h) in ⟨⟨λ x, ⟨g₂ x, (h₂ x).1⟩, λ x, finite.fintype $ let ⟨h, h'⟩ := h₁ x in h'.subset $ λ i hi, hi.mono (inter_subset_inter_right _ (h₂ x).2)⟩⟩, λ ⟨R⟩, R.to_locally_finite⟩ instance [topological_space α] [finite β] (F : realizer α) (f : β → set α) : nonempty (locally_finite.realizer F f) := (locally_finite_iff_exists_realizer _).1 $ locally_finite_of_finite _ /-- A `compact.realizer s` is a realization that `s` is compact, namely it is a choice of finite open covers for each set family covering `s`. -/ def compact.realizer [topological_space α] (s : set α) := ∀ {f : filter α} (F : f.realizer) (x : F.σ), f ≠ ⊥ → F.F x ⊆ s → {a // a∈s ∧ 𝓝 a ⊓ f ≠ ⊥} instance [topological_space α] : inhabited (compact.realizer (∅ : set α)) := ⟨λ f F x h hF, by { cases h _, rw [←F.eq, eq_bot_iff], exact λ s _, ⟨x, hF.trans s.empty_subset⟩ }⟩
956b66a1d65a6b0f95e1fbd4110b65cb029c8b42
957a80ea22c5abb4f4670b250d55534d9db99108
/tests/lean/run/array2.lean
ba8a73fdd48b8e486824fb90007ab2e3c009ae6c
[ "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
304
lean
#check @array.mk local infix ` << `:20 := array.push_back def test1 := let v1 := mk_array 3 2, v2 := v1 << 3 << 4, v3 := (v2 << 5)^.write' 0 0 in (v1, v2, v3) #eval test1 def tst1 (n : nat) := let v1 := (mk_array n 1), v2 := array.map (λ v, v + 1) v1 in v2^.read' 1 #eval tst1 10
3b6fa4e83ae0541883cfd8afaa5fee619032310d
a721fe7446524f18ba361625fc01033d9c8b7a78
/src/principia/myring/sum.lean
defa026258a74f1dcfe908cb46b9472f28b58693
[]
no_license
Sterrs/leaning
8fd80d1f0a6117a220bb2e57ece639b9a63deadc
3901cc953694b33adda86cb88ca30ba99594db31
refs/heads/master
1,627,023,822,744
1,616,515,221,000
1,616,515,221,000
245,512,190
2
0
null
1,616,429,050,000
1,583,527,118,000
Lean
UTF-8
Lean
false
false
7,749
lean
import ..sequence import .basic import ..mynat.induction import ..mynat.nat_sub namespace hidden namespace myring open sequence open myring variables {α : Type} [myring α] variables a b c d : α variables m n k : mynat variables term f g : sequence α variables (op: α → α → α) (dflt: α) @[simp] theorem sum_zero: sum term 0 = 0 := rfl @[simp] theorem sum_succ: sum term (mynat.succ n) = sum term n + term n := rfl @[simp] theorem sum_one: sum term 1 = term 0 := begin apply zero_add, end @[simp] theorem prod_zero: product term 0 = 1 := rfl @[simp] theorem prod_succ: product term (mynat.succ n) = product term n * term n := rfl @[simp] theorem prod_one: product term 1 = term 0 := begin apply one_mul, end -- theorem constant_sum: ∀ n : mynat, sum ↑(1 : α) n = n -- | zero := rfl -- | (succ n) := by rw [sum_succ, constant_sum, coe_triv, add_one_succ] theorem mul_sum: ∀ n, sum (↑a * f) n = a * (sum f n) | mynat.zero := begin rw mynat.zz, change (↑a * f).sum 0 = a * 0, rw mul_zero, refl, end | (mynat.succ n) := by rw [sum_succ, sum_succ, mul_add, mul_sum, coe_mul] theorem sum_distr: ∀ n, sum (f + g) n = (sum f n) + (sum g n) | mynat.zero := begin rw mynat.zz, change 0 = 0 + (0 : α), rw add_zero, end | (mynat.succ n) := by conv { rw [sum_succ, sum_succ, sum_succ, sum_distr, addition], to_rhs, rw [←add_assoc, add_assoc (sum f n), add_comm (f n), ←add_assoc (sum f n), add_assoc], } theorem sum_cancel: (∀ n, sum f n = sum g n) ↔ ∀ n, f n = g n := begin split, { assume h, intro n, have hsn := h (mynat.succ n), repeat {rw sum_succ at hsn}, rw h n at hsn, apply add_cancel_left (g.sum n), assumption, }, { assume h n, induction n with n hn, refl, rw [sum_succ, sum_succ, hn, h n], }, end theorem apply_accumulate: (∀ n, f n = g n) → accumulate op dflt f k = accumulate op dflt g k := begin assume h, induction k with k hk, { refl, }, { dsimp [accumulate], rw h, rw hk, }, end theorem apply_sum: (∀ n, f n = g n) → sum f k = sum g k := λ h, apply_accumulate k f g (+) 0 h theorem apply_prod: (∀ n, f n = g n) → product f k = product g k := λ h, apply_accumulate k f g (*) 1 h theorem accumulate_tail [hcomm: is_commutative _ op] [hassoc: is_associative _ op]: accumulate op dflt f (mynat.succ n) = op (accumulate op dflt (λ k, f (mynat.succ k)) n) (f 0) := begin induction n with n hn, { refl, }, { unfold accumulate at *, rw hn, ac_refl, }, end theorem sum_tail: sum f (mynat.succ n) = sum (λ k, f (mynat.succ k)) n + f 0 := accumulate_tail n f (+) 0 theorem prod_tail: product f (mynat.succ n) = product (λ k, f (mynat.succ k)) n * f 0 := accumulate_tail n f (*) 1 private theorem restricted_mpr {m : mynat} {f g : sequence α} (h : ∀ n, n < m → f n = g n) : ∀ n, n ≤ m → sum f n = sum g n | mynat.zero := λ _, rfl | (mynat.succ n) := (assume hnm, by rw [sum_succ, sum_succ, restricted_mpr n (@mynat.le_cancel_strong n m 1 hnm), h n (mynat.lt_iff_succ_le.mpr hnm)]) -- this might be necessary/useful when working with -, since it lets -- you basically assume k < n in order to rewrite the terms theorem sum_cancel_restricted: (∀ n, n ≤ m → sum f n = sum g n) ↔ (∀ n, n < m → f n = g n) := ⟨assume h n hnm, have hsn : _ := h (mynat.succ n) (mynat.lt_iff_succ_le.mp hnm), by {rw [sum_succ, sum_succ, h n (mynat.lt_impl_le hnm)] at hsn, from add_cancel_left _ _ _ hsn}, restricted_mpr⟩ private theorem add_two: ∀ k, k + 2 = mynat.succ (mynat.succ k) := (λ k, rfl) theorem sum_reverse: sum f n = sum (λ k, f (n - mynat.succ k)) n := begin revert n f, -- easy way to access the n - 2 case of IH apply duo_induction (λ n, ∀ f, sum f n = sum (λ k, f (n - mynat.succ k)) n), { intro f, refl, }, { intro f, refl, }, { intro n, assume h_ih _, intro f, rw [add_two, sum_succ, sum_tail, h_ih, sum_succ, sum_tail, mynat.sub_succ_succ, mynat.sub_zero, mynat.sub_self_eq_zero], conv { congr, rw [add_assoc, add_comm], skip, rw [add_assoc, add_comm], congr, rw add_comm, }, apply add_left, -- help lean with type inference a bit have h_aesthetic := @mynat.le_refl n, revert h_aesthetic, apply (sum_cancel_restricted _ _ _).mpr, intro m, assume hmn, congr, rw [mynat.sub_succ_succ, mynat.sub_succ_succ], cases mynat.sub_succ_converse hmn with d hd, symmetry, rw [mynat.sub_succ_rearrange, mynat.sub_succ, hd, mynat.succ_sub_one], from mynat.sub_succ_rearrange.mp hd, }, end theorem sum_split: sum f (n + m) = sum f n + sum (λ k, f (n + k)) m := begin induction m with m hm, { symmetry, from add_zero _, }, { rw mynat.add_succ, rw sum_succ, rw hm, rw sum_succ, rw add_assoc, }, end theorem sum_split_lots: sum f (n * m) = sum (λ k, sum (λ ℓ, f (ℓ + k * n)) n) m := begin induction m with m ih_m generalizing f, { refl, }, { rw mynat.mul_succ, rw sum_split, conv { to_lhs, rw add_comm, congr, rw ih_m _, }, rw sum_tail, apply congr, { apply congr, refl, apply (sum_cancel _ _).mpr, intro k, apply (sum_cancel _ _).mpr, intro ℓ, rw mynat.succ_mul, ac_refl, }, { apply (sum_cancel _ _).mpr, intro ℓ, rw mynat.zero_mul, refl, }, }, end theorem sum_square_limit_swap (f: mynat → mynat → α): sum (λ k, sum (λ ℓ, f k ℓ) n) m = sum (λ ℓ, sum (λ k, f k ℓ) m) n := begin induction m with m ih_m, { conv { to_lhs, change (0: α), }, induction n with n ih_n, { refl, }, { symmetry, rw ih_n, from add_zero _, }, }, { rw sum_succ, rw ih_m, clear ih_m, induction n with n ih_n, { from add_zero _, }, { conv { to_rhs, rw sum_succ, rw ←ih_n, }, repeat {rw sum_succ}, repeat {rw add_assoc}, apply congr, refl, rw add_comm, repeat {rw add_assoc}, apply congr, refl, from add_comm _ _, }, }, end theorem sum_triangle_limit_swap (f: mynat → mynat → α): sum (λ k, sum (λ ℓ, f k ℓ) k.succ) n = sum (λ ℓ, sum (λ k, f (k + ℓ) ℓ) (n - ℓ)) n := begin induction n with n ih_n, { refl, }, { rw sum_succ, rw ih_n, clear ih_n, rw sum_succ, rw sum_succ, rw mynat.succ_sub_self, repeat {rw ←add_assoc}, apply congr, { apply congr, refl, rw ←sum_distr, apply (sum_cancel_restricted n _ _).mpr, { intro ℓ, assume hln, have: n.succ - ℓ = (n - ℓ).succ, { cases mynat.sub_succ_converse hln with w hw, rw hw, rw mynat.sub_succ_rearrange at hw, rw hw, rw ←mynat.succ_add, rw mynat.add_sub, }, rw this, clear this, rw sum_succ, dsimp only [], conv { to_lhs, change (sum (λ (k : mynat), f (k + ℓ) ℓ) (n - ℓ) + f n ℓ), }, apply congr, { refl, }, { rw mynat.sub_add_condition.mpr (mynat.lt_impl_le hln), }, }, { refl, }, }, { conv { to_rhs, change 0 + f (0 + n) n, }, rw zero_add, rw mynat.zero_add, }, }, end theorem prod_congr: (∀ k, f k = g k) → (∀ n, product f n = product g n) := begin assume heq, intro n, induction n with n hn, { refl, }, { repeat {rw prod_succ}, rw hn, rw heq n, }, end end myring end hidden
d71c2964a97af090d03a89de282917c33e938bf1
271e26e338b0c14544a889c31c30b39c989f2e0f
/stage0/src/Init/Lean.lean
c8fc6010e6d9ab4053055d35e2c15c0f48d2f82e
[ "Apache-2.0" ]
permissive
dgorokho/lean4
805f99b0b60c545b64ac34ab8237a8504f89d7d4
e949a052bad59b1c7b54a82d24d516a656487d8a
refs/heads/master
1,607,061,363,851
1,578,006,086,000
1,578,006,086,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
651
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.Compiler import Init.Lean.Environment import Init.Lean.Modifiers import Init.Lean.ProjFns import Init.Lean.Runtime import Init.Lean.Attributes import Init.Lean.Parser import Init.Lean.ReducibilityAttrs import Init.Lean.Elab import Init.Lean.EqnCompiler import Init.Lean.Class import Init.Lean.LocalContext import Init.Lean.MetavarContext import Init.Lean.AuxRecursor import Init.Lean.Linter import Init.Lean.Meta import Init.Lean.Eval import Init.Lean.Structure
5cb62cef07a8ccd5fda19f6f7791a4f22490561f
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/real/golden_ratio.lean
c5963b5fe0114483be54c5cbf30f2d45e4cf9094
[ "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
5,789
lean
/- Copyright (c) 2020 Anatole Dedecker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anatole Dedecker, Alexey Soloyev, Junyan Xu -/ import data.real.irrational import data.nat.fib import data.nat.prime_norm_num import data.fin.vec_notation import tactic.ring_exp import algebra.linear_recurrence /-! # The golden ratio and its conjugate > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines the golden ratio `φ := (1 + √5)/2` and its conjugate `ψ := (1 - √5)/2`, which are the two real roots of `X² - X - 1`. Along with various computational facts about them, we prove their irrationality, and we link them to the Fibonacci sequence by proving Binet's formula. -/ noncomputable theory open_locale polynomial /-- The golden ratio `φ := (1 + √5)/2`. -/ @[reducible] def golden_ratio := (1 + real.sqrt 5)/2 /-- The conjugate of the golden ratio `ψ := (1 - √5)/2`. -/ @[reducible] def golden_conj := (1 - real.sqrt 5)/2 localized "notation (name := golden_ratio) `φ` := golden_ratio" in real localized "notation (name := golden_conj) `ψ` := golden_conj" in real /-- The inverse of the golden ratio is the opposite of its conjugate. -/ lemma inv_gold : φ⁻¹ = -ψ := begin have : 1 + real.sqrt 5 ≠ 0, from ne_of_gt (add_pos (by norm_num) $ real.sqrt_pos.mpr (by norm_num)), field_simp [sub_mul, mul_add], norm_num end /-- The opposite of the golden ratio is the inverse of its conjugate. -/ lemma inv_gold_conj : ψ⁻¹ = -φ := begin rw [inv_eq_iff_eq_inv, ← neg_inv, ← neg_eq_iff_eq_neg], exact inv_gold.symm, end @[simp] lemma gold_mul_gold_conj : φ * ψ = -1 := by {field_simp, rw ← sq_sub_sq, norm_num} @[simp] lemma gold_conj_mul_gold : ψ * φ = -1 := by {rw mul_comm, exact gold_mul_gold_conj} @[simp] lemma gold_add_gold_conj : φ + ψ = 1 := by {rw [golden_ratio, golden_conj], ring} lemma one_sub_gold_conj : 1 - φ = ψ := by linarith [gold_add_gold_conj] lemma one_sub_gold : 1 - ψ = φ := by linarith [gold_add_gold_conj] @[simp] lemma gold_sub_gold_conj : φ - ψ = real.sqrt 5 := by {rw [golden_ratio, golden_conj], ring} @[simp] lemma gold_sq : φ^2 = φ + 1 := begin rw [golden_ratio, ←sub_eq_zero], ring_exp, rw real.sq_sqrt; norm_num, end @[simp] lemma gold_conj_sq : ψ^2 = ψ + 1 := begin rw [golden_conj, ←sub_eq_zero], ring_exp, rw real.sq_sqrt; norm_num, end lemma gold_pos : 0 < φ := mul_pos (by apply add_pos; norm_num) $ inv_pos.2 zero_lt_two lemma gold_ne_zero : φ ≠ 0 := ne_of_gt gold_pos lemma one_lt_gold : 1 < φ := begin refine lt_of_mul_lt_mul_left _ (le_of_lt gold_pos), simp [← sq, gold_pos, zero_lt_one] end lemma gold_conj_neg : ψ < 0 := by linarith [one_sub_gold_conj, one_lt_gold] lemma gold_conj_ne_zero : ψ ≠ 0 := ne_of_lt gold_conj_neg lemma neg_one_lt_gold_conj : -1 < ψ := begin rw [neg_lt, ← inv_gold], exact inv_lt_one one_lt_gold, end /-! ## Irrationality -/ /-- The golden ratio is irrational. -/ theorem gold_irrational : irrational φ := begin have := nat.prime.irrational_sqrt (show nat.prime 5, by norm_num), have := this.rat_add 1, have := this.rat_mul (show (0.5 : ℚ) ≠ 0, by norm_num), convert this, field_simp end /-- The conjugate of the golden ratio is irrational. -/ theorem gold_conj_irrational : irrational ψ := begin have := nat.prime.irrational_sqrt (show nat.prime 5, by norm_num), have := this.rat_sub 1, have := this.rat_mul (show (0.5 : ℚ) ≠ 0, by norm_num), convert this, field_simp end /-! ## Links with Fibonacci sequence -/ section fibrec variables {α : Type*} [comm_semiring α] /-- The recurrence relation satisfied by the Fibonacci sequence. -/ def fib_rec : linear_recurrence α := { order := 2, coeffs := ![1, 1]} section poly open polynomial /-- The characteristic polynomial of `fib_rec` is `X² - (X + 1)`. -/ lemma fib_rec_char_poly_eq {β : Type*} [comm_ring β] : fib_rec.char_poly = X^2 - (X + (1 : β[X])) := begin rw [fib_rec, linear_recurrence.char_poly], simp [finset.sum_fin_eq_sum_range, finset.sum_range_succ', ← smul_X_eq_monomial] end end poly /-- As expected, the Fibonacci sequence is a solution of `fib_rec`. -/ lemma fib_is_sol_fib_rec : fib_rec.is_solution (λ x, x.fib : ℕ → α) := begin rw fib_rec, intros n, simp only, rw [nat.fib_add_two, add_comm], simp [finset.sum_fin_eq_sum_range, finset.sum_range_succ'], end /-- The geometric sequence `λ n, φ^n` is a solution of `fib_rec`. -/ lemma geom_gold_is_sol_fib_rec : fib_rec.is_solution (pow φ) := begin rw [fib_rec.geom_sol_iff_root_char_poly, fib_rec_char_poly_eq], simp [sub_eq_zero] end /-- The geometric sequence `λ n, ψ^n` is a solution of `fib_rec`. -/ lemma geom_gold_conj_is_sol_fib_rec : fib_rec.is_solution (pow ψ) := begin rw [fib_rec.geom_sol_iff_root_char_poly, fib_rec_char_poly_eq], simp [sub_eq_zero] end end fibrec /-- Binet's formula as a function equality. -/ theorem real.coe_fib_eq' : (λ n, nat.fib n : ℕ → ℝ) = λ n, (φ^n - ψ^n) / real.sqrt 5 := begin rw fib_rec.sol_eq_of_eq_init, { intros i hi, fin_cases hi, { simp }, { simp only [golden_ratio, golden_conj], ring_exp, rw mul_inv_cancel; norm_num } }, { exact fib_is_sol_fib_rec }, { ring_nf, exact (@fib_rec ℝ _).sol_space.sub_mem (submodule.smul_mem fib_rec.sol_space (real.sqrt 5)⁻¹ geom_gold_is_sol_fib_rec) (submodule.smul_mem fib_rec.sol_space (real.sqrt 5)⁻¹ geom_gold_conj_is_sol_fib_rec) } end /-- Binet's formula as a dependent equality. -/ theorem real.coe_fib_eq : ∀ n, (nat.fib n : ℝ) = (φ^n - ψ^n) / real.sqrt 5 := by rw [← function.funext_iff, real.coe_fib_eq']
ba50d7035a7300214e730e6af4f8c56023aa1979
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/p_series.lean
20ecaa558a71ab507475a1ed2d623520793538f6
[ "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,626
lean
/- Copyright (c) 2020 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov -/ import analysis.special_functions.pow.nnreal /-! # Convergence of `p`-series > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we prove that the series `∑' k in ℕ, 1 / k ^ p` converges if and only if `p > 1`. The proof is based on the [Cauchy condensation test](https://en.wikipedia.org/wiki/Cauchy_condensation_test): `∑ k, f k` converges if and only if so does `∑ k, 2 ^ k f (2 ^ k)`. We prove this test in `nnreal.summable_condensed_iff` and `summable_condensed_iff_of_nonneg`, then use it to prove `summable_one_div_rpow`. After this transformation, a `p`-series turns into a geometric series. ## TODO It should be easy to generalize arguments to Schlömilch's generalization of the Cauchy condensation test once we need it. ## Tags p-series, Cauchy condensation test -/ open filter open_locale big_operators ennreal nnreal topology /-! ### Cauchy condensation test In this section we prove the Cauchy condensation test: for `f : ℕ → ℝ≥0` or `f : ℕ → ℝ`, `∑ k, f k` converges if and only if so does `∑ k, 2 ^ k f (2 ^ k)`. Instead of giving a monolithic proof, we split it into a series of lemmas with explicit estimates of partial sums of each series in terms of the partial sums of the other series. -/ namespace finset variables {M : Type*} [ordered_add_comm_monoid M] {f : ℕ → M} lemma le_sum_condensed' (hf : ∀ ⦃m n⦄, 0 < m → m ≤ n → f n ≤ f m) (n : ℕ) : (∑ k in Ico 1 (2 ^ n), f k) ≤ ∑ k in range n, (2 ^ k) • f (2 ^ k) := begin induction n with n ihn, { simp }, suffices : (∑ k in Ico (2 ^ n) (2 ^ (n + 1)), f k) ≤ (2 ^ n) • f (2 ^ n), { rw [sum_range_succ, ← sum_Ico_consecutive], exact add_le_add ihn this, exacts [n.one_le_two_pow, nat.pow_le_pow_of_le_right zero_lt_two n.le_succ] }, have : ∀ k ∈ Ico (2 ^ n) (2 ^ (n + 1)), f k ≤ f (2 ^ n) := λ k hk, hf (pow_pos zero_lt_two _) (mem_Ico.mp hk).1, convert sum_le_sum this, simp [pow_succ, two_mul] end lemma le_sum_condensed (hf : ∀ ⦃m n⦄, 0 < m → m ≤ n → f n ≤ f m) (n : ℕ) : (∑ k in range (2 ^ n), f k) ≤ f 0 + ∑ k in range n, (2 ^ k) • f (2 ^ k) := begin convert add_le_add_left (le_sum_condensed' hf n) (f 0), rw [← sum_range_add_sum_Ico _ n.one_le_two_pow, sum_range_succ, sum_range_zero, zero_add] end lemma sum_condensed_le' (hf : ∀ ⦃m n⦄, 1 < m → m ≤ n → f n ≤ f m) (n : ℕ) : (∑ k in range n, (2 ^ k) • f (2 ^ (k + 1))) ≤ ∑ k in Ico 2 (2 ^ n + 1), f k := begin induction n with n ihn, { simp }, suffices : (2 ^ n) • f (2 ^ (n + 1)) ≤ ∑ k in Ico (2 ^ n + 1) (2 ^ (n + 1) + 1), f k, { rw [sum_range_succ, ← sum_Ico_consecutive], exact add_le_add ihn this, exacts [add_le_add_right n.one_le_two_pow _, add_le_add_right (nat.pow_le_pow_of_le_right zero_lt_two n.le_succ) _] }, have : ∀ k ∈ Ico (2 ^ n + 1) (2 ^ (n + 1) + 1), f (2 ^ (n + 1)) ≤ f k := λ k hk, hf (n.one_le_two_pow.trans_lt $ (nat.lt_succ_of_le le_rfl).trans_le (mem_Ico.mp hk).1) (nat.le_of_lt_succ $ (mem_Ico.mp hk).2), convert sum_le_sum this, simp [pow_succ, two_mul] end lemma sum_condensed_le (hf : ∀ ⦃m n⦄, 1 < m → m ≤ n → f n ≤ f m) (n : ℕ) : (∑ k in range (n + 1), (2 ^ k) • f (2 ^ k)) ≤ f 1 + 2 • ∑ k in Ico 2 (2 ^ n + 1), f k := begin convert add_le_add_left (nsmul_le_nsmul_of_le_right (sum_condensed_le' hf n) 2) (f 1), simp [sum_range_succ', add_comm, pow_succ, mul_nsmul, sum_nsmul] end end finset namespace ennreal variable {f : ℕ → ℝ≥0∞} lemma le_tsum_condensed (hf : ∀ ⦃m n⦄, 0 < m → m ≤ n → f n ≤ f m) : ∑' k, f k ≤ f 0 + ∑' k : ℕ, (2 ^ k) * f (2 ^ k) := begin rw [ennreal.tsum_eq_supr_nat' (nat.tendsto_pow_at_top_at_top_of_one_lt _root_.one_lt_two)], refine supr_le (λ n, (finset.le_sum_condensed hf n).trans (add_le_add_left _ _)), simp only [nsmul_eq_mul, nat.cast_pow, nat.cast_two], apply ennreal.sum_le_tsum end lemma tsum_condensed_le (hf : ∀ ⦃m n⦄, 1 < m → m ≤ n → f n ≤ f m) : ∑' k : ℕ, (2 ^ k * f (2 ^ k)) ≤ f 1 + 2 * ∑' k, f k := begin rw [ennreal.tsum_eq_supr_nat' (tendsto_at_top_mono nat.le_succ tendsto_id), two_mul, ← two_nsmul], refine supr_le (λ n, le_trans _ (add_le_add_left (nsmul_le_nsmul_of_le_right (ennreal.sum_le_tsum $ finset.Ico 2 (2^n + 1)) _) _)), simpa using finset.sum_condensed_le hf n end end ennreal namespace nnreal /-- Cauchy condensation test for a series of `nnreal` version. -/ lemma summable_condensed_iff {f : ℕ → ℝ≥0} (hf : ∀ ⦃m n⦄, 0 < m → m ≤ n → f n ≤ f m) : summable (λ k : ℕ, (2 ^ k) * f (2 ^ k)) ↔ summable f := begin simp only [← ennreal.tsum_coe_ne_top_iff_summable, ne.def, not_iff_not, ennreal.coe_mul, ennreal.coe_pow, ennreal.coe_two], split; intro h, { replace hf : ∀ m n, 1 < m → m ≤ n → (f n : ℝ≥0∞) ≤ f m := λ m n hm hmn, ennreal.coe_le_coe.2 (hf (zero_lt_one.trans hm) hmn), simpa [h, ennreal.add_eq_top, ennreal.mul_eq_top] using ennreal.tsum_condensed_le hf }, { replace hf : ∀ m n, 0 < m → m ≤ n → (f n : ℝ≥0∞) ≤ f m := λ m n hm hmn, ennreal.coe_le_coe.2 (hf hm hmn), simpa [h, ennreal.add_eq_top] using (ennreal.le_tsum_condensed hf) } end end nnreal /-- Cauchy condensation test for series of nonnegative real numbers. -/ lemma summable_condensed_iff_of_nonneg {f : ℕ → ℝ} (h_nonneg : ∀ n, 0 ≤ f n) (h_mono : ∀ ⦃m n⦄, 0 < m → m ≤ n → f n ≤ f m) : summable (λ k : ℕ, (2 ^ k) * f (2 ^ k)) ↔ summable f := begin lift f to ℕ → ℝ≥0 using h_nonneg, simp only [nnreal.coe_le_coe] at *, exact_mod_cast nnreal.summable_condensed_iff h_mono end open real /-! ### Convergence of the `p`-series In this section we prove that for a real number `p`, the series `∑' n : ℕ, 1 / (n ^ p)` converges if and only if `1 < p`. There are many different proofs of this fact. The proof in this file uses the Cauchy condensation test we formalized above. This test implies that `∑ n, 1 / (n ^ p)` converges if and only if `∑ n, 2 ^ n / ((2 ^ n) ^ p)` converges, and the latter series is a geometric series with common ratio `2 ^ {1 - p}`. -/ /-- Test for convergence of the `p`-series: the real-valued series `∑' n : ℕ, (n ^ p)⁻¹` converges if and only if `1 < p`. -/ @[simp] lemma real.summable_nat_rpow_inv {p : ℝ} : summable (λ n, (n ^ p)⁻¹ : ℕ → ℝ) ↔ 1 < p := begin cases le_or_lt 0 p with hp hp, /- Cauchy condensation test applies only to antitone sequences, so we consider the cases `0 ≤ p` and `p < 0` separately. -/ { rw ← summable_condensed_iff_of_nonneg, { simp_rw [nat.cast_pow, nat.cast_two, ← rpow_nat_cast, ← rpow_mul zero_lt_two.le, mul_comm _ p, rpow_mul zero_lt_two.le, rpow_nat_cast, ← inv_pow, ← mul_pow, summable_geometric_iff_norm_lt_1], nth_rewrite 0 [← rpow_one 2], rw [← division_def, ← rpow_sub zero_lt_two, norm_eq_abs, abs_of_pos (rpow_pos_of_pos zero_lt_two _), rpow_lt_one_iff zero_lt_two.le], norm_num }, { intro n, exact inv_nonneg.2 (rpow_nonneg_of_nonneg n.cast_nonneg _) }, { intros m n hm hmn, exact inv_le_inv_of_le (rpow_pos_of_pos (nat.cast_pos.2 hm) _) (rpow_le_rpow m.cast_nonneg (nat.cast_le.2 hmn) hp) } }, /- If `p < 0`, then `1 / n ^ p` tends to infinity, thus the series diverges. -/ { suffices : ¬summable (λ n, (n ^ p)⁻¹ : ℕ → ℝ), { have : ¬(1 < p) := λ hp₁, hp.not_le (zero_le_one.trans hp₁.le), simpa [this, -one_div] }, { intro h, obtain ⟨k : ℕ, hk₁ : ((k ^ p)⁻¹ : ℝ) < 1, hk₀ : k ≠ 0⟩ := ((h.tendsto_cofinite_zero.eventually (gt_mem_nhds zero_lt_one)).and (eventually_cofinite_ne 0)).exists, apply hk₀, rw [← pos_iff_ne_zero, ← @nat.cast_pos ℝ] at hk₀, simpa [inv_lt_one_iff_of_pos (rpow_pos_of_pos hk₀ _), one_lt_rpow_iff_of_pos hk₀, hp, hp.not_lt, hk₀] using hk₁ } } end @[simp] lemma real.summable_nat_rpow {p : ℝ} : summable (λ n, n ^ p : ℕ → ℝ) ↔ p < -1 := by { rcases neg_surjective p with ⟨p, rfl⟩, simp [rpow_neg] } /-- Test for convergence of the `p`-series: the real-valued series `∑' n : ℕ, 1 / n ^ p` converges if and only if `1 < p`. -/ lemma real.summable_one_div_nat_rpow {p : ℝ} : summable (λ n, 1 / n ^ p : ℕ → ℝ) ↔ 1 < p := by simp /-- Test for convergence of the `p`-series: the real-valued series `∑' n : ℕ, (n ^ p)⁻¹` converges if and only if `1 < p`. -/ @[simp] lemma real.summable_nat_pow_inv {p : ℕ} : summable (λ n, (n ^ p)⁻¹ : ℕ → ℝ) ↔ 1 < p := by simp only [← rpow_nat_cast, real.summable_nat_rpow_inv, nat.one_lt_cast] /-- Test for convergence of the `p`-series: the real-valued series `∑' n : ℕ, 1 / n ^ p` converges if and only if `1 < p`. -/ lemma real.summable_one_div_nat_pow {p : ℕ} : summable (λ n, 1 / n ^ p : ℕ → ℝ) ↔ 1 < p := by simp /-- Summability of the `p`-series over `ℤ`. -/ lemma real.summable_one_div_int_pow {p : ℕ} : summable (λ n:ℤ, 1 / (n : ℝ) ^ p) ↔ 1 < p := begin refine ⟨λ h, real.summable_one_div_nat_pow.mp (h.comp_injective nat.cast_injective), λ h, summable_int_of_summable_nat (real.summable_one_div_nat_pow.mpr h) (((real.summable_one_div_nat_pow.mpr h).mul_left $ 1 / (-1) ^ p).congr $ λ n, _)⟩, conv_rhs { rw [int.cast_neg, neg_eq_neg_one_mul, mul_pow, ←div_div] }, conv_lhs { rw [mul_div, mul_one], }, refl, end lemma real.summable_abs_int_rpow {b : ℝ} (hb : 1 < b) : summable (λ n : ℤ, |(n : ℝ)| ^ (-b)) := begin refine summable_int_of_summable_nat (_ : summable (λ n : ℕ, |(n : ℝ)| ^ _)) (_ : summable (λ n : ℕ, |((-n : ℤ) : ℝ)| ^ _)), work_on_goal 2 { simp_rw [int.cast_neg, int.cast_coe_nat, abs_neg] }, all_goals { simp_rw (λ n : ℕ, abs_of_nonneg (n.cast_nonneg : 0 ≤ (n : ℝ))), rwa [real.summable_nat_rpow, neg_lt_neg_iff] }, end /-- Harmonic series is not unconditionally summable. -/ lemma real.not_summable_nat_cast_inv : ¬summable (λ n, n⁻¹ : ℕ → ℝ) := have ¬summable (λ n, (n^1)⁻¹ : ℕ → ℝ), from mt real.summable_nat_pow_inv.1 (lt_irrefl 1), by simpa /-- Harmonic series is not unconditionally summable. -/ lemma real.not_summable_one_div_nat_cast : ¬summable (λ n, 1 / n : ℕ → ℝ) := by simpa only [inv_eq_one_div] using real.not_summable_nat_cast_inv /-- **Divergence of the Harmonic Series** -/ lemma real.tendsto_sum_range_one_div_nat_succ_at_top : tendsto (λ n, ∑ i in finset.range n, (1 / (i + 1) : ℝ)) at_top at_top := begin rw ← not_summable_iff_tendsto_nat_at_top_of_nonneg, { exact_mod_cast mt (summable_nat_add_iff 1).1 real.not_summable_one_div_nat_cast }, { exact λ i, div_nonneg zero_le_one i.cast_add_one_pos.le } end @[simp] lemma nnreal.summable_rpow_inv {p : ℝ} : summable (λ n, (n ^ p)⁻¹ : ℕ → ℝ≥0) ↔ 1 < p := by simp [← nnreal.summable_coe] @[simp] lemma nnreal.summable_rpow {p : ℝ} : summable (λ n, n ^ p : ℕ → ℝ≥0) ↔ p < -1 := by simp [← nnreal.summable_coe] lemma nnreal.summable_one_div_rpow {p : ℝ} : summable (λ n, 1 / n ^ p : ℕ → ℝ≥0) ↔ 1 < p := by simp section open finset variables {α : Type*} [linear_ordered_field α] lemma sum_Ioc_inv_sq_le_sub {k n : ℕ} (hk : k ≠ 0) (h : k ≤ n) : ∑ i in Ioc k n, ((i ^ 2) ⁻¹ : α) ≤ k ⁻¹ - n ⁻¹ := begin refine nat.le_induction _ _ n h, { simp only [Ioc_self, sum_empty, sub_self] }, assume n hn IH, rw [sum_Ioc_succ_top hn], apply (add_le_add IH le_rfl).trans, simp only [sub_eq_add_neg, add_assoc, nat.cast_add, nat.cast_one, le_add_neg_iff_add_le, add_le_iff_nonpos_right, neg_add_le_iff_le_add, add_zero], have A : 0 < (n : α), by simpa using hk.bot_lt.trans_le hn, have B : 0 < (n : α) + 1, by linarith, field_simp [B.ne'], rw [div_le_div_iff _ A, ← sub_nonneg], { ring_nf, exact B.le }, { nlinarith }, end lemma sum_Ioo_inv_sq_le (k n : ℕ) : ∑ i in Ioo k n, ((i ^ 2) ⁻¹ : α) ≤ 2 / (k + 1) := calc ∑ i in Ioo k n, ((i ^ 2) ⁻¹ : α) ≤ ∑ i in Ioc k (max (k+1) n), (i ^ 2) ⁻¹ : begin apply sum_le_sum_of_subset_of_nonneg, { assume x hx, simp only [mem_Ioo] at hx, simp only [hx, hx.2.le, mem_Ioc, le_max_iff, or_true, and_self] }, { assume i hi hident, exact inv_nonneg.2 (sq_nonneg _), } end ... ≤ ((k + 1) ^ 2) ⁻¹ + ∑ i in Ioc k.succ (max (k + 1) n), (i ^ 2) ⁻¹ : begin rw [← nat.Icc_succ_left, ← nat.Ico_succ_right, sum_eq_sum_Ico_succ_bot], swap, { exact nat.succ_lt_succ ((nat.lt_succ_self k).trans_le (le_max_left _ _)) }, rw [nat.Ico_succ_right, nat.Icc_succ_left, nat.cast_succ], end ... ≤ ((k + 1) ^ 2) ⁻¹ + (k + 1) ⁻¹ : begin refine add_le_add le_rfl ((sum_Ioc_inv_sq_le_sub _ (le_max_left _ _)).trans _), { simp only [ne.def, nat.succ_ne_zero, not_false_iff] }, { simp only [nat.cast_succ, one_div, sub_le_self_iff, inv_nonneg, nat.cast_nonneg] } end ... ≤ 1 / (k + 1) + 1 / (k + 1) : begin have A : (1 : α) ≤ k + 1, by simp only [le_add_iff_nonneg_left, nat.cast_nonneg], simp_rw ← one_div, apply add_le_add_right, refine div_le_div zero_le_one le_rfl (zero_lt_one.trans_le A) _, simpa using pow_le_pow A one_le_two, end ... = 2 / (k + 1) : by ring end
4247b612aafd170b126c13b2e92a7fa275f8961e
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/run/sec_bug.lean
e84e2e3d09985ca6fd856fd2d8becbff13af2bfe
[ "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
82
lean
namespace foo section bla definition tst := true end bla end foo check foo.tst
1f46b4406195bebede15aec2a4698baf1ca2b452
7afc29faca4febb6e5005c20aa4aa5c3df5cf35c
/src/grow.lean
3199719b9de6ef987aaae52713d11aa70a30c8e3
[ "MIT" ]
permissive
Piwry/Proof-of-Surreal
ad2883027e275050b43a578c5513ae3fe350515b
6b92baf2382ac23dd0d700f5c958aa910ad4b754
refs/heads/master
1,670,521,185,736
1,599,657,591,000
1,599,657,591,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,681
lean
import data.list tactic.omega defs single branch lemma grow_trans : ∀ t1 t2 t3 : bintree, (t1 ↣ t2) → (t2 ↣ t3) → (t1 ↣ t3) := begin intro t1, induction t1, begin intros, auto_grow end, repeat { begin intros t2 t3 H1 H2, cases H1, cases H2, have H3 : (t1_a ↣ H2_t'), apply t1_ih, repeat {assumption}, auto_grow end }, intros t2 t3 H1 H2, cases H1, cases H2, have H3 : (t1_a ↣ H2_t1'), apply t1_ih_a, repeat {assumption}, have H4 : (t1_a_1 ↣ H2_t2'), apply t1_ih_a_1, repeat {assumption}, auto_grow end lemma grow_list_exists_one : ∀ (l : list bintree) (t : bintree), (l ↦ t) → ∃ t', (t' ∈ l) ∧ (t' ↣ t) := begin intros l t h, induction h, begin existsi h_t, split, apply list.mem_cons_self, assumption end, begin cases h_ih, existsi h_ih_w, split, apply list.mem_cons_of_mem, tauto, tauto end end lemma exists_one_grow_list : ∀ l t, (∃ t', ((t' ∈ l) ∧ (t' ↣ t))) → (l ↦ t) := begin intros l, induction l, begin intros, cases a, cases a_h.left end, begin intros t h, cases h, unfold has_mem.mem list.mem at h_h, cases h_h.left, begin apply grow_list.head_grow, rewrite h at h_h, exact h_h.right end, begin apply grow_list.tail_grow, apply l_ih, existsi h_w, split, assumption, exact h_h.right end end end lemma kernel_lemma : ∀ (t : bintree) (h : ℕ), h ≥ 1 → h ≤ height t → ∃ b : bintree, ⟨b⟩ ∧ h = height b ∧ (b ↣ t) := begin intros t, induction t, begin -- single node intros, unfold height at a_1, have H : h = 1, omega, rewrite H, fapply exists.intro, exact ●, split, apply is_branch.single, split, unfold height, auto_grow, end, repeat { -- left and right intros h H1 H2, cases h, cases H1, unfold height at H2, have H1' : h ≥ 0, omega, have H2' : h ≤ height t_a, omega, cases h, begin -- h = 0, which is trivial fapply exists.intro, exact ●, split, exact is_branch.single, split, unfold height, apply grow.single_grow end, begin -- h ≠ 0, apply induction hypothesis have H3 : ∃ (b : bintree), ⟨b⟩ ∧ h.succ = height b ∧ (b↣t_a), apply t_ih, omega, omega, cases H3, try { existsi ⟦H3_w∣⟧, split, apply is_branch.left_nl, tauto, split, unfold height, have Ht : h.succ = height H3_w, tauto, omega, apply grow.left_grow, tauto, }, try { existsi ⟦∣H3_w⟧, split, apply is_branch.right_nl, tauto, split, unfold height, have Ht : h.succ = height H3_w, tauto, omega, apply grow.right_grow, tauto, } end }, intros h H1 H2, -- both left and right, completely similar, while more tedious cases h, cases H1, unfold height at H2, have H1' : h ≥ 0, omega, have H2' : h ≤ max (height t_a) (height t_a_1), omega, cases h, begin fapply exists.intro, exact ●, split, exact is_branch.single, split, unfold height, apply grow.single_grow end, begin have H3 : h.succ ≤ height t_a ∨ h.succ ≤ height t_a_1, begin have H3' : max (height t_a) (height t_a_1) = height t_a ∨ max (height t_a) (height t_a_1) = height t_a_1, apply max_choice, destruct H3', intro H4, left, rewrite H4 at H2', assumption, intro H4, right, rewrite H4 at H2', assumption end, destruct H3, begin intros H4, have H5 : h.succ ≥ 1, omega, have H6 : ∃ (b : bintree), ⟨ b ⟩ ∧ h.succ = height b ∧ (b ↣ t_a), apply t_ih_a, repeat {assumption}, cases H6, cases H6_h, cases H6_h_right, existsi (⟦H6_w, ●⟧), split, apply is_branch.left_l, assumption, split, unfold height, have Ht : (1 ≤ height H6_w), apply ge.le, apply height_ge1, have Ht' : max (height H6_w) 1 = height H6_w, apply max_eq_left, assumption, omega, auto_grow end, begin intros H4, have H5 : h.succ ≥ 1, omega, have H6 : ∃ (b : bintree), ⟨ b ⟩ ∧ h.succ = height b ∧ (b ↣ t_a_1), apply t_ih_a_1, repeat {assumption}, cases H6, cases H6_h, cases H6_h_right, existsi (⟦●, H6_w⟧), split, apply is_branch.right_l, assumption, split, unfold height, have Ht : (1 ≤ height H6_w), apply ge.le, apply height_ge1, have Ht' : max 1 (height H6_w) = height H6_w, apply max_eq_right, assumption, omega, auto_grow end end end
f2ca6d7397b92d52ccc5c76f53c7d45f4763d4b5
271e26e338b0c14544a889c31c30b39c989f2e0f
/tests/compiler/termparsertest1.lean
772e6efd52ecf600aacc736f2a76305d0b30cdf6
[ "Apache-2.0" ]
permissive
dgorokho/lean4
805f99b0b60c545b64ac34ab8237a8504f89d7d4
e949a052bad59b1c7b54a82d24d516a656487d8a
refs/heads/master
1,607,061,363,851
1,578,006,086,000
1,578,006,086,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,698
lean
import Init.Lean.Parser.Term open Lean open Lean.Parser def testParser (input : String) : IO Unit := do env ← mkEmptyEnvironment; termPTables ← builtinTermParsingTable.get; stx ← IO.ofExcept $ runParser env termPTables input "<input>" "expr"; IO.println stx def test (is : List String) : IO Unit := is.forM $ fun input => do IO.println input; testParser input def testParserFailure (input : String) : IO Unit := do env ← mkEmptyEnvironment; termPTables ← builtinTermParsingTable.get; match runParser env termPTables input "<input>" "expr" with | Except.ok stx => throw (IO.userError ("unexpected success\n" ++ toString stx)) | Except.error msg => IO.println ("failed as expected, error: " ++ msg) def testFailures (is : List String) : IO Unit := is.forM $ fun input => do IO.println input; testParserFailure input def main (xs : List String) : IO Unit := do test [ "Prod.mk", "x.{u, v+1}", "x.{u}", "x", "x.{max u v}", "x.{max u v, 0}", "f 0 1", "f.{u+1} \"foo\" x", "(f x, 0, 1)", "()", "(f x)", "(f x : Type)", "h (f x) (g y)", "if x then f x else g x", "if h : x then f x h else g x h", "have p x y from f x; g this", "suffices h : p x y from f x; g this", "show p x y from f x", "fun x y => f y x", "fun (x y : Nat) => f y x", "fun (x, y) => f y x", "fun z (x, y) => f y x", "fun ⟨x, y⟩ ⟨z, w⟩ => f y x w z", "fun (Prod.mk x y) => f y x", "{ x := 10, y := 20 }", "{ x := 10, y := 20, }", "{ x // p x 10 }", "{ x : Nat // p x 10 }", "{ .. }", "{ Prod . fst := 10, .. }", "a[i]", "f [10, 20]", "g a[x+2]", "g f.a.1.2.bla x.1.a", "x+y*z < 10/3", "id (α := Nat) 10", "(x : a)", "a -> b", "{x : a} -> b", "{a : Type} -> [HasToString a] -> (x : a) -> b", "f ({x : a} -> b)", "f (x : a) -> b", "f ((x : a) -> b)", "(f : (n : Nat) → Vector Nat n) -> Nat", "∀ x y (z : Nat), x > y -> x > y - z", " match x with | some x => true | none => false", " match x with | some y => match y with | some (a, b) => a + b | none => 1 | none => 0 ", "Type u", "Sort v", "Type 1", "f Type 1", "let x := 0; x + 1", "let x : Nat := 0; x + 1", "let f (x : Nat) := x + 1; f 0", "let f {α : Type} (a : α) : α := a; f 10", "let f (x) := x + 1; f 10 + f 20", "let (x, y) := f 10; x + y", "let { fst := x, .. } := f 10; x + x", "let x.y := f 10; x", "let x.1 := f 10; x", "let x[i].y := f 10; x", "let x[i] := f 20; x", "-x + y", "!x", "¬ a ∧ b", " do x ← f a; x : Nat ← f a; g x; let y := g x; (a, b) <- h x y; let (a, b) := (b, a); pure (a + b)", "do { x ← f a; pure $ a + a }", "let f : Nat → Nat → Nat | 0, a => a + 10 | n+1, b => n * b; f 20", "max a b" ]; testFailures [ "f {x : a} -> b", "(x := 20)", "let x 10; x", "let x := y" ]
1e1c465bb5cc121086c71a52d7103e9b2ad5761a
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/topology/maps.lean
cbd80ed6314c7747ea1753078775142377443686
[ "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
26,131
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot -/ import topology.order import topology.nhds_set /-! # Specific classes of maps between topological spaces > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file introduces the following properties of a map `f : X → Y` between topological spaces: * `is_open_map f` means the image of an open set under `f` is open. * `is_closed_map f` means the image of a closed set under `f` is closed. (Open and closed maps need not be continuous.) * `inducing f` means the topology on `X` is the one induced via `f` from the topology on `Y`. These behave like embeddings except they need not be injective. Instead, points of `X` which are identified by `f` are also inseparable in the topology on `X`. * `embedding f` means `f` is inducing and also injective. Equivalently, `f` identifies `X` with a subspace of `Y`. * `open_embedding f` means `f` is an embedding with open image, so it identifies `X` with an open subspace of `Y`. Equivalently, `f` is an embedding and an open map. * `closed_embedding f` similarly means `f` is an embedding with closed image, so it identifies `X` with a closed subspace of `Y`. Equivalently, `f` is an embedding and a closed map. * `quotient_map f` is the dual condition to `embedding f`: `f` is surjective and the topology on `Y` is the one coinduced via `f` from the topology on `X`. Equivalently, `f` identifies `Y` with a quotient of `X`. Quotient maps are also sometimes known as identification maps. ## References * <https://en.wikipedia.org/wiki/Open_and_closed_maps> * <https://en.wikipedia.org/wiki/Embedding#General_topology> * <https://en.wikipedia.org/wiki/Quotient_space_(topology)#Quotient_map> ## Tags open map, closed map, embedding, quotient map, identification map -/ open set filter function open_locale topology filter variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} section inducing /-- A function `f : α → β` between topological spaces is inducing if the topology on `α` is induced by the topology on `β` through `f`, meaning that a set `s : set α` is open iff it is the preimage under `f` of some open set `t : set β`. -/ @[mk_iff] structure inducing [tα : topological_space α] [tβ : topological_space β] (f : α → β) : Prop := (induced : tα = tβ.induced f) variables [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] lemma inducing_id : inducing (@id α) := ⟨induced_id.symm⟩ protected lemma inducing.comp {g : β → γ} {f : α → β} (hg : inducing g) (hf : inducing f) : inducing (g ∘ f) := ⟨by rw [hf.induced, hg.induced, induced_compose]⟩ lemma inducing_of_inducing_compose {f : α → β} {g : β → γ} (hf : continuous f) (hg : continuous g) (hgf : inducing (g ∘ f)) : inducing f := ⟨le_antisymm (by rwa ← continuous_iff_le_induced) (by { rw [hgf.induced, ← continuous_iff_le_induced], apply hg.comp continuous_induced_dom })⟩ lemma inducing_iff_nhds {f : α → β} : inducing f ↔ ∀ a, 𝓝 a = comap f (𝓝 (f a)) := (inducing_iff _).trans (induced_iff_nhds_eq f) lemma inducing.nhds_eq_comap {f : α → β} (hf : inducing f) : ∀ (a : α), 𝓝 a = comap f (𝓝 $ f a) := inducing_iff_nhds.1 hf lemma inducing.nhds_set_eq_comap {f : α → β} (hf : inducing f) (s : set α) : 𝓝ˢ s = comap f (𝓝ˢ (f '' s)) := by simp only [nhds_set, Sup_image, comap_supr, hf.nhds_eq_comap, supr_image] lemma inducing.map_nhds_eq {f : α → β} (hf : inducing f) (a : α) : (𝓝 a).map f = 𝓝[range f] (f a) := hf.induced.symm ▸ map_nhds_induced_eq a lemma inducing.map_nhds_of_mem {f : α → β} (hf : inducing f) (a : α) (h : range f ∈ 𝓝 (f a)) : (𝓝 a).map f = 𝓝 (f a) := hf.induced.symm ▸ map_nhds_induced_of_mem h lemma inducing.image_mem_nhds_within {f : α → β} (hf : inducing f) {a : α} {s : set α} (hs : s ∈ 𝓝 a) : f '' s ∈ 𝓝[range f] (f a) := hf.map_nhds_eq a ▸ image_mem_map hs lemma inducing.tendsto_nhds_iff {ι : Type*} {f : ι → β} {g : β → γ} {a : filter ι} {b : β} (hg : inducing g) : tendsto f a (𝓝 b) ↔ tendsto (g ∘ f) a (𝓝 (g b)) := by rw [hg.nhds_eq_comap, tendsto_comap_iff] lemma inducing.continuous_at_iff {f : α → β} {g : β → γ} (hg : inducing g) {x : α} : continuous_at f x ↔ continuous_at (g ∘ f) x := by simp_rw [continuous_at, inducing.tendsto_nhds_iff hg] lemma inducing.continuous_iff {f : α → β} {g : β → γ} (hg : inducing g) : continuous f ↔ continuous (g ∘ f) := by simp_rw [continuous_iff_continuous_at, hg.continuous_at_iff] lemma inducing.continuous_at_iff' {f : α → β} {g : β → γ} (hf : inducing f) {x : α} (h : range f ∈ 𝓝 (f x)) : continuous_at (g ∘ f) x ↔ continuous_at g (f x) := by { simp_rw [continuous_at, filter.tendsto, ← hf.map_nhds_of_mem _ h, filter.map_map] } protected lemma inducing.continuous {f : α → β} (hf : inducing f) : continuous f := hf.continuous_iff.mp continuous_id protected lemma inducing.inducing_iff {f : α → β} {g : β → γ} (hg : inducing g) : inducing f ↔ inducing (g ∘ f) := begin refine ⟨λ h, hg.comp h, λ hgf, inducing_of_inducing_compose _ hg.continuous hgf⟩, rw hg.continuous_iff, exact hgf.continuous end lemma inducing.closure_eq_preimage_closure_image {f : α → β} (hf : inducing f) (s : set α) : closure s = f ⁻¹' closure (f '' s) := by { ext x, rw [set.mem_preimage, ← closure_induced, hf.induced] } lemma inducing.is_closed_iff {f : α → β} (hf : inducing f) {s : set α} : is_closed s ↔ ∃ t, is_closed t ∧ f ⁻¹' t = s := by rw [hf.induced, is_closed_induced_iff] lemma inducing.is_closed_iff' {f : α → β} (hf : inducing f) {s : set α} : is_closed s ↔ ∀ x, f x ∈ closure (f '' s) → x ∈ s := by rw [hf.induced, is_closed_induced_iff'] lemma inducing.is_closed_preimage {f : α → β} (h : inducing f) (s : set β) (hs : is_closed s) : is_closed (f ⁻¹' s) := (inducing.is_closed_iff h).mpr ⟨s, hs, rfl⟩ lemma inducing.is_open_iff {f : α → β} (hf : inducing f) {s : set α} : is_open s ↔ ∃ t, is_open t ∧ f ⁻¹' t = s := by rw [hf.induced, is_open_induced_iff] lemma inducing.dense_iff {f : α → β} (hf : inducing f) {s : set α} : dense s ↔ ∀ x, f x ∈ closure (f '' s) := by simp only [dense, hf.closure_eq_preimage_closure_image, mem_preimage] end inducing 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. -/ @[mk_iff] structure embedding [tα : topological_space α] [tβ : topological_space β] (f : α → β) extends inducing f : Prop := (inj : injective f) lemma function.injective.embedding_induced [t : topological_space β] {f : α → β} (hf : injective f) : @_root_.embedding α β (t.induced f) t f := { induced := rfl, inj := hf } variables [topological_space α] [topological_space β] [topological_space γ] lemma embedding.mk' (f : α → β) (inj : injective f) (induced : ∀ a, comap f (𝓝 (f a)) = 𝓝 a) : embedding f := ⟨inducing_iff_nhds.2 (λ a, (induced a).symm), inj⟩ lemma embedding_id : embedding (@id α) := ⟨inducing_id, assume a₁ a₂ h, h⟩ lemma embedding.comp {g : β → γ} {f : α → β} (hg : embedding g) (hf : embedding f) : embedding (g ∘ f) := { inj:= assume a₁ a₂ h, hf.inj $ hg.inj h, ..hg.to_inducing.comp hf.to_inducing } lemma embedding_of_embedding_compose {f : α → β} {g : β → γ} (hf : continuous f) (hg : continuous g) (hgf : embedding (g ∘ f)) : embedding f := { induced := (inducing_of_inducing_compose hf hg hgf.to_inducing).induced, inj := assume a₁ a₂ h, hgf.inj $ by simp [h, (∘)] } protected lemma function.left_inverse.embedding {f : α → β} {g : β → α} (h : left_inverse f g) (hf : continuous f) (hg : continuous g) : embedding g := embedding_of_embedding_compose hg hf $ h.comp_eq_id.symm ▸ embedding_id lemma embedding.map_nhds_eq {f : α → β} (hf : embedding f) (a : α) : (𝓝 a).map f = 𝓝[range f] (f a) := hf.1.map_nhds_eq a lemma embedding.map_nhds_of_mem {f : α → β} (hf : embedding f) (a : α) (h : range f ∈ 𝓝 (f a)) : (𝓝 a).map f = 𝓝 (f a) := hf.1.map_nhds_of_mem a h lemma embedding.tendsto_nhds_iff {ι : Type*} {f : ι → β} {g : β → γ} {a : filter ι} {b : β} (hg : embedding g) : tendsto f a (𝓝 b) ↔ tendsto (g ∘ f) a (𝓝 (g b)) := hg.to_inducing.tendsto_nhds_iff lemma embedding.continuous_iff {f : α → β} {g : β → γ} (hg : embedding g) : continuous f ↔ continuous (g ∘ f) := inducing.continuous_iff hg.1 lemma embedding.continuous {f : α → β} (hf : embedding f) : continuous f := inducing.continuous hf.1 lemma embedding.closure_eq_preimage_closure_image {e : α → β} (he : embedding e) (s : set α) : closure s = e ⁻¹' closure (e '' s) := he.1.closure_eq_preimage_closure_image s /-- The topology induced under an inclusion `f : X → Y` from the discrete topological space `Y` is the discrete topology on `X`. -/ lemma embedding.discrete_topology {X Y : Type*} [topological_space X] [tY : topological_space Y] [discrete_topology Y] {f : X → Y} (hf : embedding f) : discrete_topology X := discrete_topology_iff_nhds.2 $ λ x, by rw [hf.nhds_eq_comap, nhds_discrete, comap_pure, ← image_singleton, hf.inj.preimage_image, principal_singleton] 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 {α : Type*} {β : Type*} [tα : topological_space α] [tβ : topological_space β] (f : α → β) : Prop := surjective f ∧ tβ = tα.coinduced f lemma quotient_map_iff [topological_space α] [topological_space β] {f : α → β} : quotient_map f ↔ surjective f ∧ ∀ s : set β, is_open s ↔ is_open (f ⁻¹' s) := and_congr iff.rfl topological_space_eq_iff lemma quotient_map_iff_closed [topological_space α] [topological_space β] {f : α → β} : quotient_map f ↔ surjective f ∧ ∀ s : set β, is_closed s ↔ is_closed (f ⁻¹' s) := quotient_map_iff.trans $ iff.rfl.and $ compl_surjective.forall.trans $ by simp only [is_open_compl_iff, preimage_compl] namespace quotient_map variables [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] {g : β → γ} {f : α → β} protected lemma id : quotient_map (@id α) := ⟨assume a, ⟨a, rfl⟩, coinduced_id.symm⟩ protected lemma comp (hg : quotient_map g) (hf : quotient_map f) : quotient_map (g ∘ f) := ⟨hg.left.comp hf.left, by rw [hg.right, hf.right, coinduced_compose]⟩ protected lemma of_quotient_map_compose (hf : continuous f) (hg : continuous g) (hgf : quotient_map (g ∘ f)) : quotient_map g := ⟨hgf.1.of_comp, le_antisymm (by { rw [hgf.right, ← continuous_iff_coinduced_le], apply continuous_coinduced_rng.comp hf }) (by rwa ← continuous_iff_coinduced_le)⟩ lemma of_inverse {g : β → α} (hf : continuous f) (hg : continuous g) (h : left_inverse g f) : quotient_map g := quotient_map.of_quotient_map_compose hf hg $ h.comp_eq_id.symm ▸ quotient_map.id protected lemma continuous_iff (hf : quotient_map f) : continuous g ↔ continuous (g ∘ f) := by rw [continuous_iff_coinduced_le, continuous_iff_coinduced_le, hf.right, coinduced_compose] protected lemma continuous (hf : quotient_map f) : continuous f := hf.continuous_iff.mp continuous_id protected lemma surjective (hf : quotient_map f) : surjective f := hf.1 protected lemma is_open_preimage (hf : quotient_map f) {s : set β} : is_open (f ⁻¹' s) ↔ is_open s := ((quotient_map_iff.1 hf).2 s).symm protected lemma is_closed_preimage (hf : quotient_map f) {s : set β} : is_closed (f ⁻¹' s) ↔ is_closed s := ((quotient_map_iff_closed.1 hf).2 s).symm end quotient_map /-- A map `f : α → β` is said to be an *open map*, if the image of any open `U : set α` is open in `β`. -/ def is_open_map [topological_space α] [topological_space β] (f : α → β) := ∀ U : set α, is_open U → is_open (f '' U) namespace is_open_map variables [topological_space α] [topological_space β] [topological_space γ] {f : α → β} protected lemma id : is_open_map (@id α) := assume s hs, by rwa [image_id] protected lemma comp {g : β → γ} {f : α → β} (hg : is_open_map g) (hf : is_open_map f) : is_open_map (g ∘ f) := by intros s hs; rw [image_comp]; exact hg _ (hf _ hs) lemma is_open_range (hf : is_open_map f) : is_open (range f) := by { rw ← image_univ, exact hf _ is_open_univ } lemma image_mem_nhds (hf : is_open_map f) {x : α} {s : set α} (hx : s ∈ 𝓝 x) : f '' s ∈ 𝓝 (f x) := let ⟨t, hts, ht, hxt⟩ := mem_nhds_iff.1 hx in mem_of_superset (is_open.mem_nhds (hf t ht) (mem_image_of_mem _ hxt)) (image_subset _ hts) lemma range_mem_nhds (hf : is_open_map f) (x : α) : range f ∈ 𝓝 (f x) := hf.is_open_range.mem_nhds $ mem_range_self _ lemma maps_to_interior (hf : is_open_map f) {s : set α} {t : set β} (h : maps_to f s t) : maps_to f (interior s) (interior t) := maps_to'.2 $ interior_maximal (h.mono interior_subset subset.rfl).image_subset (hf _ is_open_interior) lemma image_interior_subset (hf : is_open_map f) (s : set α) : f '' interior s ⊆ interior (f '' s) := (hf.maps_to_interior (maps_to_image f s)).image_subset lemma nhds_le (hf : is_open_map f) (a : α) : 𝓝 (f a) ≤ (𝓝 a).map f := le_map $ λ s, hf.image_mem_nhds lemma of_nhds_le (hf : ∀ a, 𝓝 (f a) ≤ map f (𝓝 a)) : is_open_map f := λ s hs, is_open_iff_mem_nhds.2 $ λ b ⟨a, has, hab⟩, hab ▸ hf _ (image_mem_map $ is_open.mem_nhds hs has) lemma of_sections {f : α → β} (h : ∀ x, ∃ g : β → α, continuous_at g (f x) ∧ g (f x) = x ∧ right_inverse g f) : is_open_map f := of_nhds_le $ λ x, let ⟨g, hgc, hgx, hgf⟩ := h x in calc 𝓝 (f x) = map f (map g (𝓝 (f x))) : by rw [map_map, hgf.comp_eq_id, map_id] ... ≤ map f (𝓝 (g (f x))) : map_mono hgc ... = map f (𝓝 x) : by rw hgx lemma of_inverse {f : α → β} {f' : β → α} (h : continuous f') (l_inv : left_inverse f f') (r_inv : right_inverse f f') : is_open_map f := of_sections $ λ x, ⟨f', h.continuous_at, r_inv _, l_inv⟩ /-- A continuous surjective open map is a quotient map. -/ lemma to_quotient_map {f : α → β} (open_map : is_open_map f) (cont : continuous f) (surj : surjective f) : quotient_map f := quotient_map_iff.2 ⟨surj, λ s, ⟨λ h, h.preimage cont, λ h, surj.image_preimage s ▸ open_map _ h⟩⟩ lemma interior_preimage_subset_preimage_interior (hf : is_open_map f) {s : set β} : interior (f⁻¹' s) ⊆ f⁻¹' (interior s) := hf.maps_to_interior (maps_to_preimage _ _) lemma preimage_interior_eq_interior_preimage (hf₁ : is_open_map f) (hf₂ : continuous f) (s : set β) : f⁻¹' (interior s) = interior (f⁻¹' s) := subset.antisymm (preimage_interior_subset_interior_preimage hf₂) (interior_preimage_subset_preimage_interior hf₁) lemma preimage_closure_subset_closure_preimage (hf : is_open_map f) {s : set β} : f ⁻¹' (closure s) ⊆ closure (f ⁻¹' s) := begin rw ← compl_subset_compl, simp only [← interior_compl, ← preimage_compl, hf.interior_preimage_subset_preimage_interior] end lemma preimage_closure_eq_closure_preimage (hf : is_open_map f) (hfc : continuous f) (s : set β) : f ⁻¹' (closure s) = closure (f ⁻¹' s) := hf.preimage_closure_subset_closure_preimage.antisymm (hfc.closure_preimage_subset s) lemma preimage_frontier_subset_frontier_preimage (hf : is_open_map f) {s : set β} : f ⁻¹' (frontier s) ⊆ frontier (f ⁻¹' s) := by simpa only [frontier_eq_closure_inter_closure, preimage_inter] using inter_subset_inter hf.preimage_closure_subset_closure_preimage hf.preimage_closure_subset_closure_preimage lemma preimage_frontier_eq_frontier_preimage (hf : is_open_map f) (hfc : continuous f) (s : set β) : f ⁻¹' (frontier s) = frontier (f ⁻¹' s) := by simp only [frontier_eq_closure_inter_closure, preimage_inter, preimage_compl, hf.preimage_closure_eq_closure_preimage hfc] end is_open_map lemma is_open_map_iff_nhds_le [topological_space α] [topological_space β] {f : α → β} : is_open_map f ↔ ∀(a:α), 𝓝 (f a) ≤ (𝓝 a).map f := ⟨λ hf, hf.nhds_le, is_open_map.of_nhds_le⟩ lemma is_open_map_iff_interior [topological_space α] [topological_space β] {f : α → β} : is_open_map f ↔ ∀ s, f '' (interior s) ⊆ interior (f '' s) := ⟨is_open_map.image_interior_subset, λ hs u hu, subset_interior_iff_is_open.mp $ calc f '' u = f '' (interior u) : by rw hu.interior_eq ... ⊆ interior (f '' u) : hs u⟩ /-- An inducing map with an open range is an open map. -/ protected lemma inducing.is_open_map [topological_space α] [topological_space β] {f : α → β} (hi : inducing f) (ho : is_open (range f)) : is_open_map f := is_open_map.of_nhds_le $ λ x, (hi.map_nhds_of_mem _ $ is_open.mem_nhds ho $ mem_range_self _).ge section is_closed_map variables [topological_space α] [topological_space β] /-- A map `f : α → β` is said to be a *closed map*, if the image of any closed `U : set α` is closed in `β`. -/ def is_closed_map (f : α → β) := ∀ U : set α, is_closed U → is_closed (f '' U) end is_closed_map namespace is_closed_map variables [topological_space α] [topological_space β] [topological_space γ] open function protected lemma id : is_closed_map (@id α) := assume s hs, by rwa image_id protected lemma comp {g : β → γ} {f : α → β} (hg : is_closed_map g) (hf : is_closed_map f) : is_closed_map (g ∘ f) := by { intros s hs, rw image_comp, exact hg _ (hf _ hs) } lemma closure_image_subset {f : α → β} (hf : is_closed_map f) (s : set α) : closure (f '' s) ⊆ f '' closure s := closure_minimal (image_subset _ subset_closure) (hf _ is_closed_closure) lemma of_inverse {f : α → β} {f' : β → α} (h : continuous f') (l_inv : left_inverse f f') (r_inv : right_inverse f f') : is_closed_map f := assume s hs, have f' ⁻¹' s = f '' s, by ext x; simp [mem_image_iff_of_inverse r_inv l_inv], this ▸ hs.preimage h lemma of_nonempty {f : α → β} (h : ∀ s, is_closed s → s.nonempty → is_closed (f '' s)) : is_closed_map f := begin intros s hs, cases eq_empty_or_nonempty s with h2s h2s, { simp_rw [h2s, image_empty, is_closed_empty] }, { exact h s hs h2s } end lemma closed_range {f : α → β} (hf : is_closed_map f) : is_closed (range f) := @image_univ _ _ f ▸ hf _ is_closed_univ lemma to_quotient_map {f : α → β} (hcl : is_closed_map f) (hcont : continuous f) (hsurj : surjective f) : quotient_map f := quotient_map_iff_closed.2 ⟨hsurj, λ s, ⟨λ hs, hs.preimage hcont, λ hs, hsurj.image_preimage s ▸ hcl _ hs⟩⟩ end is_closed_map lemma inducing.is_closed_map [topological_space α] [topological_space β] {f : α → β} (hf : inducing f) (h : is_closed (range f)) : is_closed_map f := begin intros s hs, rcases hf.is_closed_iff.1 hs with ⟨t, ht, rfl⟩, rw image_preimage_eq_inter_range, exact ht.inter h end lemma is_closed_map_iff_closure_image [topological_space α] [topological_space β] {f : α → β} : is_closed_map f ↔ ∀ s, closure (f '' s) ⊆ f '' closure s := ⟨is_closed_map.closure_image_subset, λ hs c hc, is_closed_of_closure_subset $ calc closure (f '' c) ⊆ f '' (closure c) : hs c ... = f '' c : by rw hc.closure_eq⟩ section open_embedding variables [topological_space α] [topological_space β] [topological_space γ] /-- An open embedding is an embedding with open image. -/ @[mk_iff] structure open_embedding (f : α → β) extends _root_.embedding f : Prop := (open_range : is_open $ range f) lemma open_embedding.is_open_map {f : α → β} (hf : open_embedding f) : is_open_map f := hf.to_embedding.to_inducing.is_open_map hf.open_range lemma open_embedding.map_nhds_eq {f : α → β} (hf : open_embedding f) (a : α) : map f (𝓝 a) = 𝓝 (f a) := hf.to_embedding.map_nhds_of_mem _ $ hf.open_range.mem_nhds $ mem_range_self _ lemma open_embedding.open_iff_image_open {f : α → β} (hf : open_embedding f) {s : set α} : is_open s ↔ is_open (f '' s) := ⟨hf.is_open_map s, λ h, begin convert ← h.preimage hf.to_embedding.continuous, apply preimage_image_eq _ hf.inj end⟩ lemma open_embedding.tendsto_nhds_iff {ι : Type*} {f : ι → β} {g : β → γ} {a : filter ι} {b : β} (hg : open_embedding g) : tendsto f a (𝓝 b) ↔ tendsto (g ∘ f) a (𝓝 (g b)) := hg.to_embedding.tendsto_nhds_iff lemma open_embedding.continuous {f : α → β} (hf : open_embedding f) : continuous f := hf.to_embedding.continuous lemma open_embedding.open_iff_preimage_open {f : α → β} (hf : open_embedding f) {s : set β} (hs : s ⊆ range f) : is_open s ↔ is_open (f ⁻¹' s) := begin convert ←hf.open_iff_image_open.symm, rwa [image_preimage_eq_inter_range, inter_eq_self_of_subset_left] end lemma open_embedding_of_embedding_open {f : α → β} (h₁ : embedding f) (h₂ : is_open_map f) : open_embedding f := ⟨h₁, h₂.is_open_range⟩ lemma open_embedding_iff_embedding_open {f : α → β} : open_embedding f ↔ embedding f ∧ is_open_map f := ⟨λ h, ⟨h.1, h.is_open_map⟩, λ h, open_embedding_of_embedding_open h.1 h.2⟩ lemma open_embedding_of_continuous_injective_open {f : α → β} (h₁ : continuous f) (h₂ : injective f) (h₃ : is_open_map f) : open_embedding f := begin simp only [open_embedding_iff_embedding_open, embedding_iff, inducing_iff_nhds, *, and_true], exact λ a, le_antisymm (h₁.tendsto _).le_comap (@comap_map _ _ (𝓝 a) _ h₂ ▸ comap_mono (h₃.nhds_le _)) end lemma open_embedding_iff_continuous_injective_open {f : α → β} : open_embedding f ↔ continuous f ∧ injective f ∧ is_open_map f := ⟨λ h, ⟨h.continuous, h.inj, h.is_open_map⟩, λ h, open_embedding_of_continuous_injective_open h.1 h.2.1 h.2.2⟩ lemma open_embedding_id : open_embedding (@id α) := ⟨embedding_id, is_open_map.id.is_open_range⟩ lemma open_embedding.comp {g : β → γ} {f : α → β} (hg : open_embedding g) (hf : open_embedding f) : open_embedding (g ∘ f) := ⟨hg.1.comp hf.1, (hg.is_open_map.comp hf.is_open_map).is_open_range⟩ lemma open_embedding.is_open_map_iff {g : β → γ} {f : α → β} (hg : open_embedding g) : is_open_map f ↔ is_open_map (g ∘ f) := by simp only [is_open_map_iff_nhds_le, ← @map_map _ _ _ _ f g, ← hg.map_nhds_eq, map_le_map_iff hg.inj] lemma open_embedding.of_comp_iff (f : α → β) {g : β → γ} (hg : open_embedding g) : open_embedding (g ∘ f) ↔ open_embedding f := by simp only [open_embedding_iff_continuous_injective_open, ← hg.is_open_map_iff, ← hg.1.continuous_iff, hg.inj.of_comp_iff] lemma open_embedding.of_comp (f : α → β) {g : β → γ} (hg : open_embedding g) (h : open_embedding (g ∘ f)) : open_embedding f := (open_embedding.of_comp_iff f hg).1 h end open_embedding section closed_embedding variables [topological_space α] [topological_space β] [topological_space γ] /-- A closed embedding is an embedding with closed image. -/ @[mk_iff] structure closed_embedding (f : α → β) extends _root_.embedding f : Prop := (closed_range : is_closed $ range f) variables {f : α → β} lemma closed_embedding.tendsto_nhds_iff {ι : Type*} {g : ι → α} {a : filter ι} {b : α} (hf : closed_embedding f) : tendsto g a (𝓝 b) ↔ tendsto (f ∘ g) a (𝓝 (f b)) := hf.to_embedding.tendsto_nhds_iff lemma closed_embedding.continuous (hf : closed_embedding f) : continuous f := hf.to_embedding.continuous lemma closed_embedding.is_closed_map (hf : closed_embedding f) : is_closed_map f := hf.to_embedding.to_inducing.is_closed_map hf.closed_range lemma closed_embedding.closed_iff_image_closed (hf : closed_embedding f) {s : set α} : is_closed s ↔ is_closed (f '' s) := ⟨hf.is_closed_map s, λ h, begin convert ←continuous_iff_is_closed.mp hf.continuous _ h, apply preimage_image_eq _ hf.inj end⟩ lemma closed_embedding.closed_iff_preimage_closed (hf : closed_embedding f) {s : set β} (hs : s ⊆ range f) : is_closed s ↔ is_closed (f ⁻¹' s) := begin convert ←hf.closed_iff_image_closed.symm, rwa [image_preimage_eq_inter_range, inter_eq_self_of_subset_left] end lemma closed_embedding_of_embedding_closed (h₁ : embedding f) (h₂ : is_closed_map f) : closed_embedding f := ⟨h₁, by convert h₂ univ is_closed_univ; simp⟩ lemma closed_embedding_of_continuous_injective_closed (h₁ : continuous f) (h₂ : injective f) (h₃ : is_closed_map f) : closed_embedding f := begin refine closed_embedding_of_embedding_closed ⟨⟨_⟩, h₂⟩ h₃, apply le_antisymm (continuous_iff_le_induced.mp h₁) _, intro s', change is_open _ ≤ is_open _, rw [←is_closed_compl_iff, ←is_closed_compl_iff], generalize : s'ᶜ = s, rw is_closed_induced_iff, refine λ hs, ⟨f '' s, h₃ s hs, _⟩, rw preimage_image_eq _ h₂ end lemma closed_embedding_id : closed_embedding (@id α) := ⟨embedding_id, by convert is_closed_univ; apply range_id⟩ lemma closed_embedding.comp {g : β → γ} {f : α → β} (hg : closed_embedding g) (hf : closed_embedding f) : closed_embedding (g ∘ f) := ⟨hg.to_embedding.comp hf.to_embedding, show is_closed (range (g ∘ f)), by rw [range_comp, ←hg.closed_iff_image_closed]; exact hf.closed_range⟩ lemma closed_embedding.closure_image_eq {f : α → β} (hf : closed_embedding f) (s : set α) : closure (f '' s) = f '' closure s := (hf.is_closed_map.closure_image_subset _).antisymm (image_closure_subset_closure_image hf.continuous) end closed_embedding
204968c51bb723473617e2a823151ee1d4d685bf
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/geometry/manifold/charted_space.lean
f1ca64288052616a92f196527525b9c7e5bdb2c5
[ "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
48,177
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import topology.local_homeomorph /-! # Charted spaces A smooth manifold is a topological space `M` locally modelled on a euclidean space (or a euclidean half-space for manifolds with boundaries, or an infinite dimensional vector space for more general notions of manifolds), i.e., the manifold is covered by open subsets on which there are local homeomorphisms (the charts) going to a model space `H`, and the changes of charts should be smooth maps. In this file, we introduce a general framework describing these notions, where the model space is an arbitrary topological space. We avoid the word *manifold*, which should be reserved for the situation where the model space is a (subset of a) vector space, and use the terminology *charted space* instead. If the changes of charts satisfy some additional property (for instance if they are smooth), then `M` inherits additional structure (it makes sense to talk about smooth manifolds). There are therefore two different ingredients in a charted space: * the set of charts, which is data * the fact that changes of charts belong to some group (in fact groupoid), which is additional Prop. We separate these two parts in the definition: the charted space structure is just the set of charts, and then the different smoothness requirements (smooth manifold, orientable manifold, contact manifold, and so on) are additional properties of these charts. These properties are formalized through the notion of structure groupoid, i.e., a set of local homeomorphisms stable under composition and inverse, to which the change of coordinates should belong. ## Main definitions * `structure_groupoid H` : a subset of local homeomorphisms of `H` stable under composition, inverse and restriction (ex: local diffeos). * `continuous_groupoid H` : the groupoid of all local homeomorphisms of `H` * `charted_space H M` : charted space structure on `M` modelled on `H`, given by an atlas of local homeomorphisms from `M` to `H` whose sources cover `M`. This is a type class. * `has_groupoid M G` : when `G` is a structure groupoid on `H` and `M` is a charted space modelled on `H`, require that all coordinate changes belong to `G`. This is a type class. * `atlas H M` : when `M` is a charted space modelled on `H`, the atlas of this charted space structure, i.e., the set of charts. * `G.maximal_atlas M` : when `M` is a charted space modelled on `H` and admitting `G` as a structure groupoid, one can consider all the local homeomorphisms from `M` to `H` such that changing coordinate from any chart to them belongs to `G`. This is a larger atlas, called the maximal atlas (for the groupoid `G`). * `structomorph G M M'` : the type of diffeomorphisms between the charted spaces `M` and `M'` for the groupoid `G`. We avoid the word diffeomorphism, keeping it for the smooth category. As a basic example, we give the instance `instance charted_space_model_space (H : Type*) [topological_space H] : charted_space H H` saying that a topological space is a charted space over itself, with the identity as unique chart. This charted space structure is compatible with any groupoid. Additional useful definitions: * `pregroupoid H` : a subset of local mas of `H` stable under composition and restriction, but not inverse (ex: smooth maps) * `groupoid_of_pregroupoid` : construct a groupoid from a pregroupoid, by requiring that a map and its inverse both belong to the pregroupoid (ex: construct diffeos from smooth maps) * `chart_at H x` is a preferred chart at `x : M` when `M` has a charted space structure modelled on `H`. * `G.compatible he he'` states that, for any two charts `e` and `e'` in the atlas, the composition of `e.symm` and `e'` belongs to the groupoid `G` when `M` admits `G` as a structure groupoid. * `G.compatible_of_mem_maximal_atlas he he'` states that, for any two charts `e` and `e'` in the maximal atlas associated to the groupoid `G`, the composition of `e.symm` and `e'` belongs to the `G` if `M` admits `G` as a structure groupoid. * `charted_space_core.to_charted_space`: consider a space without a topology, but endowed with a set of charts (which are local equivs) for which the change of coordinates are local homeos. Then one can construct a topology on the space for which the charts become local homeos, defining a genuine charted space structure. ## Implementation notes The atlas in a charted space is *not* a maximal atlas in general: the notion of maximality depends on the groupoid one considers, and changing groupoids changes the maximal atlas. With the current formalization, it makes sense first to choose the atlas, and then to ask whether this precise atlas defines a smooth manifold, an orientable manifold, and so on. A consequence is that structomorphisms between `M` and `M'` do *not* induce a bijection between the atlases of `M` and `M'`: the definition is only that, read in charts, the structomorphism locally belongs to the groupoid under consideration. (This is equivalent to inducing a bijection between elements of the maximal atlas). A consequence is that the invariance under structomorphisms of properties defined in terms of the atlas is not obvious in general, and could require some work in theory (amounting to the fact that these properties only depend on the maximal atlas, for instance). In practice, this does not create any real difficulty. We use the letter `H` for the model space thinking of the case of manifolds with boundary, where the model space is a half space. Manifolds are sometimes defined as topological spaces with an atlas of local diffeomorphisms, and sometimes as spaces with an atlas from which a topology is deduced. We use the former approach: otherwise, there would be an instance from manifolds to topological spaces, which means that any instance search for topological spaces would try to find manifold structures involving a yet unknown model space, leading to problems. However, we also introduce the latter approach, through a structure `charted_space_core` making it possible to construct a topology out of a set of local equivs with compatibility conditions (but we do not register it as an instance). In the definition of a charted space, the model space is written as an explicit parameter as there can be several model spaces for a given topological space. For instance, a complex manifold (modelled over `ℂ^n`) will also be seen sometimes as a real manifold modelled over `ℝ^(2n)`. ## Notations In the locale `manifold`, we denote the composition of local homeomorphisms with `≫ₕ`, and the composition of local equivs with `≫`. -/ noncomputable theory open_locale classical topological_space open filter universes u variables {H : Type u} {H' : Type*} {M : Type*} {M' : Type*} {M'' : Type*} /- Notational shortcut for the composition of local homeomorphisms and local equivs, i.e., `local_homeomorph.trans` and `local_equiv.trans`. Note that, as is usual for equivs, the composition is from left to right, hence the direction of the arrow. -/ localized "infixr ` ≫ₕ `:100 := local_homeomorph.trans" in manifold localized "infixr ` ≫ `:100 := local_equiv.trans" in manifold /- `simp` looks for subsingleton instances at every call. This turns out to be very inefficient, especially in `simp`-heavy parts of the library such as the manifold code. Disable two such instances to speed up things. NB: this is just a hack. TODO: fix `simp` properly. -/ localized "attribute [-instance] unique.subsingleton pi.subsingleton" in manifold open set local_homeomorph /-! ### Structure groupoids-/ section groupoid /-! One could add to the definition of a structure groupoid the fact that the restriction of an element of the groupoid to any open set still belongs to the groupoid. (This is in Kobayashi-Nomizu.) I am not sure I want this, for instance on `H × E` where `E` is a vector space, and the groupoid is made of functions respecting the fibers and linear in the fibers (so that a charted space over this groupoid is naturally a vector bundle) I prefer that the members of the groupoid are always defined on sets of the form `s × E`. There is a typeclass `closed_under_restriction` for groupoids which have the restriction property. The only nontrivial requirement is locality: if a local homeomorphism belongs to the groupoid around each point in its domain of definition, then it belongs to the groupoid. Without this requirement, the composition of structomorphisms does not have to be a structomorphism. Note that this implies that a local homeomorphism with empty source belongs to any structure groupoid, as it trivially satisfies this condition. There is also a technical point, related to the fact that a local homeomorphism is by definition a global map which is a homeomorphism when restricted to its source subset (and its values outside of the source are not relevant). Therefore, we also require that being a member of the groupoid only depends on the values on the source. We use primes in the structure names as we will reformulate them below (without primes) using a `has_mem` instance, writing `e ∈ G` instead of `e ∈ G.members`. -/ /-- A structure groupoid is a set of local homeomorphisms of a topological space stable under composition and inverse. They appear in the definition of the smoothness class of a manifold. -/ structure structure_groupoid (H : Type u) [topological_space H] := (members : set (local_homeomorph H H)) (trans' : ∀e e' : local_homeomorph H H, e ∈ members → e' ∈ members → e ≫ₕ e' ∈ members) (symm' : ∀e : local_homeomorph H H, e ∈ members → e.symm ∈ members) (id_mem' : local_homeomorph.refl H ∈ members) (locality' : ∀e : local_homeomorph H H, (∀x ∈ e.source, ∃s, is_open s ∧ x ∈ s ∧ e.restr s ∈ members) → e ∈ members) (eq_on_source' : ∀ e e' : local_homeomorph H H, e ∈ members → e' ≈ e → e' ∈ members) variable [topological_space H] instance : has_mem (local_homeomorph H H) (structure_groupoid H) := ⟨λ(e : local_homeomorph H H) (G : structure_groupoid H), e ∈ G.members⟩ lemma structure_groupoid.trans (G : structure_groupoid H) {e e' : local_homeomorph H H} (he : e ∈ G) (he' : e' ∈ G) : e ≫ₕ e' ∈ G := G.trans' e e' he he' lemma structure_groupoid.symm (G : structure_groupoid H) {e : local_homeomorph H H} (he : e ∈ G) : e.symm ∈ G := G.symm' e he lemma structure_groupoid.id_mem (G : structure_groupoid H) : local_homeomorph.refl H ∈ G := G.id_mem' lemma structure_groupoid.locality (G : structure_groupoid H) {e : local_homeomorph H H} (h : ∀x ∈ e.source, ∃s, is_open s ∧ x ∈ s ∧ e.restr s ∈ G) : e ∈ G := G.locality' e h lemma structure_groupoid.eq_on_source (G : structure_groupoid H) {e e' : local_homeomorph H H} (he : e ∈ G) (h : e' ≈ e) : e' ∈ G := G.eq_on_source' e e' he h /-- Partial order on the set of groupoids, given by inclusion of the members of the groupoid -/ instance structure_groupoid.partial_order : partial_order (structure_groupoid H) := partial_order.lift structure_groupoid.members (λa b h, by { cases a, cases b, dsimp at h, induction h, refl }) lemma structure_groupoid.le_iff {G₁ G₂ : structure_groupoid H} : G₁ ≤ G₂ ↔ ∀ e, e ∈ G₁ → e ∈ G₂ := iff.rfl /-- The trivial groupoid, containing only the identity (and maps with empty source, as this is necessary from the definition) -/ def id_groupoid (H : Type u) [topological_space H] : structure_groupoid H := { members := {local_homeomorph.refl H} ∪ {e : local_homeomorph H H | e.source = ∅}, trans' := λe e' he he', begin cases he; simp at he he', { simpa only [he, refl_trans]}, { have : (e ≫ₕ e').source ⊆ e.source := sep_subset _ _, rw he at this, have : (e ≫ₕ e') ∈ {e : local_homeomorph H H | e.source = ∅} := disjoint_iff.1 this, exact (mem_union _ _ _).2 (or.inr this) }, end, symm' := λe he, begin cases (mem_union _ _ _).1 he with E E, { finish }, { right, simpa only [e.to_local_equiv.image_source_eq_target.symm] with mfld_simps using E}, end, id_mem' := mem_union_left _ rfl, locality' := λe he, begin cases e.source.eq_empty_or_nonempty with h h, { right, exact h }, { left, rcases h with ⟨x, hx⟩, rcases he x hx with ⟨s, open_s, xs, hs⟩, have x's : x ∈ (e.restr s).source, { rw [restr_source, open_s.interior_eq], exact ⟨hx, xs⟩ }, cases hs, { replace hs : local_homeomorph.restr e s = local_homeomorph.refl H, by simpa only using hs, have : (e.restr s).source = univ, by { rw hs, simp }, change (e.to_local_equiv).source ∩ interior s = univ at this, have : univ ⊆ interior s, by { rw ← this, exact inter_subset_right _ _ }, have : s = univ, by rwa [open_s.interior_eq, univ_subset_iff] at this, simpa only [this, restr_univ] using hs }, { exfalso, rw mem_set_of_eq at hs, rwa hs at x's } }, end, eq_on_source' := λe e' he he'e, begin cases he, { left, have : e = e', { refine eq_of_eq_on_source_univ (setoid.symm he'e) _ _; rw set.mem_singleton_iff.1 he ; refl }, rwa ← this }, { right, change (e.to_local_equiv).source = ∅ at he, rwa [set.mem_set_of_eq, he'e.source_eq] } end } /-- Every structure groupoid contains the identity groupoid -/ instance : order_bot (structure_groupoid H) := { bot := id_groupoid H, bot_le := begin assume u f hf, change f ∈ {local_homeomorph.refl H} ∪ {e : local_homeomorph H H | e.source = ∅} at hf, simp only [singleton_union, mem_set_of_eq, mem_insert_iff] at hf, cases hf, { rw hf, apply u.id_mem }, { apply u.locality, assume x hx, rw [hf, mem_empty_eq] at hx, exact hx.elim } end } instance (H : Type u) [topological_space H] : inhabited (structure_groupoid H) := ⟨id_groupoid H⟩ /-- To construct a groupoid, one may consider classes of local homeos such that both the function and its inverse have some property. If this property is stable under composition, one gets a groupoid. `pregroupoid` bundles the properties needed for this construction, with the groupoid of smooth functions with smooth inverses as an application. -/ structure pregroupoid (H : Type*) [topological_space H] := (property : (H → H) → (set H) → Prop) (comp : ∀{f g u v}, property f u → property g v → is_open u → is_open v → is_open (u ∩ f ⁻¹' v) → property (g ∘ f) (u ∩ f ⁻¹' v)) (id_mem : property id univ) (locality : ∀{f u}, is_open u → (∀x∈u, ∃v, is_open v ∧ x ∈ v ∧ property f (u ∩ v)) → property f u) (congr : ∀{f g : H → H} {u}, is_open u → (∀x∈u, g x = f x) → property f u → property g u) /-- Construct a groupoid of local homeos for which the map and its inverse have some property, from a pregroupoid asserting that this property is stable under composition. -/ def pregroupoid.groupoid (PG : pregroupoid H) : structure_groupoid H := { members := {e : local_homeomorph H H | PG.property e e.source ∧ PG.property e.symm e.target}, trans' := λe e' he he', begin split, { apply PG.comp he.1 he'.1 e.open_source e'.open_source, apply e.continuous_to_fun.preimage_open_of_open e.open_source e'.open_source }, { apply PG.comp he'.2 he.2 e'.open_target e.open_target, apply e'.continuous_inv_fun.preimage_open_of_open e'.open_target e.open_target } end, symm' := λe he, ⟨he.2, he.1⟩, id_mem' := ⟨PG.id_mem, PG.id_mem⟩, locality' := λe he, begin split, { apply PG.locality e.open_source (λx xu, _), rcases he x xu with ⟨s, s_open, xs, hs⟩, refine ⟨s, s_open, xs, _⟩, convert hs.1 using 1, dsimp [local_homeomorph.restr], rw s_open.interior_eq }, { apply PG.locality e.open_target (λx xu, _), rcases he (e.symm x) (e.map_target xu) with ⟨s, s_open, xs, hs⟩, refine ⟨e.target ∩ e.symm ⁻¹' s, _, ⟨xu, xs⟩, _⟩, { exact continuous_on.preimage_open_of_open e.continuous_inv_fun e.open_target s_open }, { rw [← inter_assoc, inter_self], convert hs.2 using 1, dsimp [local_homeomorph.restr], rw s_open.interior_eq } }, end, eq_on_source' := λe e' he ee', begin split, { apply PG.congr e'.open_source ee'.2, simp only [ee'.1, he.1] }, { have A := ee'.symm', apply PG.congr e'.symm.open_source A.2, convert he.2, rw A.1, refl } end } lemma mem_groupoid_of_pregroupoid {PG : pregroupoid H} {e : local_homeomorph H H} : e ∈ PG.groupoid ↔ PG.property e e.source ∧ PG.property e.symm e.target := iff.rfl lemma groupoid_of_pregroupoid_le (PG₁ PG₂ : pregroupoid H) (h : ∀f s, PG₁.property f s → PG₂.property f s) : PG₁.groupoid ≤ PG₂.groupoid := begin refine structure_groupoid.le_iff.2 (λ e he, _), rw mem_groupoid_of_pregroupoid at he ⊢, exact ⟨h _ _ he.1, h _ _ he.2⟩ end lemma mem_pregroupoid_of_eq_on_source (PG : pregroupoid H) {e e' : local_homeomorph H H} (he' : e ≈ e') (he : PG.property e e.source) : PG.property e' e'.source := begin rw ← he'.1, exact PG.congr e.open_source he'.eq_on.symm he, end /-- The pregroupoid of all local maps on a topological space `H` -/ @[reducible] def continuous_pregroupoid (H : Type*) [topological_space H] : pregroupoid H := { property := λf s, true, comp := λf g u v hf hg hu hv huv, trivial, id_mem := trivial, locality := λf u u_open h, trivial, congr := λf g u u_open hcongr hf, trivial } instance (H : Type*) [topological_space H] : inhabited (pregroupoid H) := ⟨continuous_pregroupoid H⟩ /-- The groupoid of all local homeomorphisms on a topological space `H` -/ def continuous_groupoid (H : Type*) [topological_space H] : structure_groupoid H := pregroupoid.groupoid (continuous_pregroupoid H) /-- Every structure groupoid is contained in the groupoid of all local homeomorphisms -/ instance : order_top (structure_groupoid H) := { top := continuous_groupoid H, le_top := λ u f hf, by { split; exact dec_trivial } } /-- A groupoid is closed under restriction if it contains all restrictions of its element local homeomorphisms to open subsets of the source. -/ class closed_under_restriction (G : structure_groupoid H) : Prop := (closed_under_restriction : ∀ {e : local_homeomorph H H}, e ∈ G → ∀ (s : set H), is_open s → e.restr s ∈ G) lemma closed_under_restriction' {G : structure_groupoid H} [closed_under_restriction G] {e : local_homeomorph H H} (he : e ∈ G) {s : set H} (hs : is_open s) : e.restr s ∈ G := closed_under_restriction.closed_under_restriction he s hs /-- The trivial restriction-closed groupoid, containing only local homeomorphisms equivalent to the restriction of the identity to the various open subsets. -/ def id_restr_groupoid : structure_groupoid H := { members := {e | ∃ {s : set H} (h : is_open s), e ≈ local_homeomorph.of_set s h}, trans' := begin rintros e e' ⟨s, hs, hse⟩ ⟨s', hs', hse'⟩, refine ⟨s ∩ s', is_open.inter hs hs', _⟩, have := local_homeomorph.eq_on_source.trans' hse hse', rwa local_homeomorph.of_set_trans_of_set at this, end, symm' := begin rintros e ⟨s, hs, hse⟩, refine ⟨s, hs, _⟩, rw [← of_set_symm], exact local_homeomorph.eq_on_source.symm' hse, end, id_mem' := ⟨univ, is_open_univ, by simp only with mfld_simps⟩, locality' := begin intros e h, refine ⟨e.source, e.open_source, by simp only with mfld_simps, _⟩, intros x hx, rcases h x hx with ⟨s, hs, hxs, s', hs', hes'⟩, have hes : x ∈ (e.restr s).source, { rw e.restr_source, refine ⟨hx, _⟩, rw hs.interior_eq, exact hxs }, simpa only with mfld_simps using local_homeomorph.eq_on_source.eq_on hes' hes, end, eq_on_source' := begin rintros e e' ⟨s, hs, hse⟩ hee', exact ⟨s, hs, setoid.trans hee' hse⟩, end } lemma id_restr_groupoid_mem {s : set H} (hs : is_open s) : of_set s hs ∈ @id_restr_groupoid H _ := ⟨s, hs, by refl⟩ /-- The trivial restriction-closed groupoid is indeed `closed_under_restriction`. -/ instance closed_under_restriction_id_restr_groupoid : closed_under_restriction (@id_restr_groupoid H _) := ⟨ begin rintros e ⟨s', hs', he⟩ s hs, use [s' ∩ s, is_open.inter hs' hs], refine setoid.trans (local_homeomorph.eq_on_source.restr he s) _, exact ⟨by simp only [hs.interior_eq] with mfld_simps, by simp only with mfld_simps⟩, end ⟩ /-- A groupoid is closed under restriction if and only if it contains the trivial restriction-closed groupoid. -/ lemma closed_under_restriction_iff_id_le (G : structure_groupoid H) : closed_under_restriction G ↔ id_restr_groupoid ≤ G := begin split, { introsI _i, apply structure_groupoid.le_iff.mpr, rintros e ⟨s, hs, hes⟩, refine G.eq_on_source _ hes, convert closed_under_restriction' G.id_mem hs, change s = _ ∩ _, rw hs.interior_eq, simp only with mfld_simps }, { intros h, split, intros e he s hs, rw ← of_set_trans (e : local_homeomorph H H) hs, refine G.trans _ he, apply structure_groupoid.le_iff.mp h, exact id_restr_groupoid_mem hs }, end /-- The groupoid of all local homeomorphisms on a topological space `H` is closed under restriction. -/ instance : closed_under_restriction (continuous_groupoid H) := (closed_under_restriction_iff_id_le _).mpr (by convert le_top) end groupoid /-! ### Charted spaces -/ /-- A charted space is a topological space endowed with an atlas, i.e., a set of local homeomorphisms taking value in a model space `H`, called charts, such that the domains of the charts cover the whole space. We express the covering property by chosing for each `x` a member `chart_at H x` of the atlas containing `x` in its source: in the smooth case, this is convenient to construct the tangent bundle in an efficient way. The model space is written as an explicit parameter as there can be several model spaces for a given topological space. For instance, a complex manifold (modelled over `ℂ^n`) will also be seen sometimes as a real manifold over `ℝ^(2n)`. -/ class charted_space (H : Type*) [topological_space H] (M : Type*) [topological_space M] := (atlas [] : set (local_homeomorph M H)) (chart_at [] : M → local_homeomorph M H) (mem_chart_source [] : ∀x, x ∈ (chart_at x).source) (chart_mem_atlas [] : ∀x, chart_at x ∈ atlas) export charted_space attribute [simp, mfld_simps] mem_chart_source chart_mem_atlas section charted_space /-- Any space is a charted_space modelled over itself, by just using the identity chart -/ instance charted_space_self (H : Type*) [topological_space H] : charted_space H H := { atlas := {local_homeomorph.refl H}, chart_at := λx, local_homeomorph.refl H, mem_chart_source := λx, mem_univ x, chart_mem_atlas := λx, mem_singleton _ } /-- In the trivial charted_space structure of a space modelled over itself through the identity, the atlas members are just the identity -/ @[simp, mfld_simps] lemma charted_space_self_atlas {H : Type*} [topological_space H] {e : local_homeomorph H H} : e ∈ atlas H H ↔ e = local_homeomorph.refl H := by simp [atlas, charted_space.atlas] /-- In the model space, chart_at is always the identity -/ lemma chart_at_self_eq {H : Type*} [topological_space H] {x : H} : chart_at H x = local_homeomorph.refl H := by simpa using chart_mem_atlas H x section variables (H) [topological_space H] [topological_space M] [charted_space H M] lemma mem_chart_target (x : M) : chart_at H x x ∈ (chart_at H x).target := (chart_at H x).map_source (mem_chart_source _ _) /-- If a topological space admits an atlas with locally compact charts, then the space itself is locally compact. -/ lemma charted_space.locally_compact [locally_compact_space H] : locally_compact_space M := begin have : ∀ (x : M), (𝓝 x).has_basis (λ s, s ∈ 𝓝 (chart_at H x x) ∧ is_compact s ∧ s ⊆ (chart_at H x).target) (λ s, (chart_at H x).symm '' s), { intro x, rw [← (chart_at H x).symm_map_nhds_eq (mem_chart_source H x)], exact ((compact_basis_nhds (chart_at H x x)).has_basis_self_subset (is_open.mem_nhds (chart_at H x).open_target (mem_chart_target H x))).map _ }, refine locally_compact_space_of_has_basis this _, rintro x s ⟨h₁, h₂, h₃⟩, exact h₂.image_of_continuous_on ((chart_at H x).continuous_on_symm.mono h₃) end open topological_space lemma charted_space.second_countable_of_countable_cover [second_countable_topology H] {s : set M} (hs : (⋃ x (hx : x ∈ s), (chart_at H x).source) = univ) (hsc : countable s) : second_countable_topology M := begin haveI : ∀ x : M, second_countable_topology (chart_at H x).source := λ x, (chart_at H x).second_countable_topology_source, haveI := hsc.to_encodable, rw bUnion_eq_Union at hs, exact second_countable_topology_of_countable_cover (λ x : s, (chart_at H (x : M)).open_source) hs end lemma charted_space.second_countable_of_sigma_compact [second_countable_topology H] [sigma_compact_space M] : second_countable_topology M := begin obtain ⟨s, hsc, hsU⟩ : ∃ s, countable s ∧ (⋃ x (hx : x ∈ s), (chart_at H x).source) = univ := countable_cover_nhds_of_sigma_compact (λ x : M, is_open.mem_nhds (chart_at H x).open_source (mem_chart_source H x)), exact charted_space.second_countable_of_countable_cover H hsU hsc end end /-- For technical reasons we introduce two type tags: * `model_prod H H'` is the same as `H × H'`; * `model_pi H` is the same as `Π i, H i`, where `H : ι → Type*` and `ι` is a finite type. In both cases the reason is the same, so we explain it only in the case of the product. A charted space `M` with model `H` is a set of local charts from `M` to `H` covering the space. Every space is registered as a charted space over itself, using the only chart `id`, in `manifold_model_space`. You can also define a product of charted space `M` and `M'` (with model space `H × H'`) by taking the products of the charts. Now, on `H × H'`, there are two charted space structures with model space `H × H'` itself, the one coming from `manifold_model_space`, and the one coming from the product of the two `manifold_model_space` on each component. They are equal, but not defeq (because the product of `id` and `id` is not defeq to `id`), which is bad as we know. This expedient of renaming `H × H'` solves this problem. -/ library_note "Manifold type tags" /-- Same thing as `H × H'` We introduce it for technical reasons, see note [Manifold type tags]. -/ def model_prod (H : Type*) (H' : Type*) := H × H' /-- Same thing as `Π i, H i` We introduce it for technical reasons, see note [Manifold type tags]. -/ def model_pi {ι : Type*} (H : ι → Type*) := Π i, H i section local attribute [reducible] model_prod instance model_prod_inhabited [inhabited H] [inhabited H'] : inhabited (model_prod H H') := prod.inhabited instance (H : Type*) [topological_space H] (H' : Type*) [topological_space H'] : topological_space (model_prod H H') := prod.topological_space /- Next lemma shows up often when dealing with derivatives, register it as simp. -/ @[simp, mfld_simps] lemma model_prod_range_prod_id {H : Type*} {H' : Type*} {α : Type*} (f : H → α) : range (λ (p : model_prod H H'), (f p.1, p.2)) = set.prod (range f) univ := by rw prod_range_univ_eq end section variables {ι : Type*} {Hi : ι → Type*} instance model_pi_inhabited [Π i, inhabited (Hi i)] : inhabited (model_pi Hi) := pi.inhabited _ instance [Π i, topological_space (Hi i)] : topological_space (model_pi Hi) := Pi.topological_space end /-- The product of two charted spaces is naturally a charted space, with the canonical construction of the atlas of product maps. -/ instance prod_charted_space (H : Type*) [topological_space H] (M : Type*) [topological_space M] [charted_space H M] (H' : Type*) [topological_space H'] (M' : Type*) [topological_space M'] [charted_space H' M'] : charted_space (model_prod H H') (M × M') := { atlas := image2 local_homeomorph.prod (atlas H M) (atlas H' M'), chart_at := λ x : M × M', (chart_at H x.1).prod (chart_at H' x.2), mem_chart_source := λ x, ⟨mem_chart_source _ _, mem_chart_source _ _⟩, chart_mem_atlas := λ x, mem_image2_of_mem (chart_mem_atlas _ _) (chart_mem_atlas _ _) } section prod_charted_space variables [topological_space H] [topological_space M] [charted_space H M] [topological_space H'] [topological_space M'] [charted_space H' M'] {x : M×M'} @[simp, mfld_simps] lemma prod_charted_space_chart_at : (chart_at (model_prod H H') x) = (chart_at H x.fst).prod (chart_at H' x.snd) := rfl end prod_charted_space /-- The product of a finite family of charted spaces is naturally a charted space, with the canonical construction of the atlas of finite product maps. -/ instance pi_charted_space {ι : Type*} [fintype ι] (H : ι → Type*) [Π i, topological_space (H i)] (M : ι → Type*) [Π i, topological_space (M i)] [Π i, charted_space (H i) (M i)] : charted_space (model_pi H) (Π i, M i) := { atlas := local_homeomorph.pi '' (set.pi univ $ λ i, atlas (H i) (M i)), chart_at := λ f, local_homeomorph.pi $ λ i, chart_at (H i) (f i), mem_chart_source := λ f i hi, mem_chart_source (H i) (f i), chart_mem_atlas := λ f, mem_image_of_mem _ $ λ i hi, chart_mem_atlas (H i) (f i) } @[simp, mfld_simps] lemma pi_charted_space_chart_at {ι : Type*} [fintype ι] (H : ι → Type*) [Π i, topological_space (H i)] (M : ι → Type*) [Π i, topological_space (M i)] [Π i, charted_space (H i) (M i)] (f : Π i, M i) : chart_at (model_pi H) f = local_homeomorph.pi (λ i, chart_at (H i) (f i)) := rfl end charted_space /-! ### Constructing a topology from an atlas -/ /-- Sometimes, one may want to construct a charted space structure on a space which does not yet have a topological structure, where the topology would come from the charts. For this, one needs charts that are only local equivs, and continuity properties for their composition. This is formalised in `charted_space_core`. -/ @[nolint has_inhabited_instance] structure charted_space_core (H : Type*) [topological_space H] (M : Type*) := (atlas : set (local_equiv M H)) (chart_at : M → local_equiv M H) (mem_chart_source : ∀x, x ∈ (chart_at x).source) (chart_mem_atlas : ∀x, chart_at x ∈ atlas) (open_source : ∀e e' : local_equiv M H, e ∈ atlas → e' ∈ atlas → is_open (e.symm.trans e').source) (continuous_to_fun : ∀e e' : local_equiv M H, e ∈ atlas → e' ∈ atlas → continuous_on (e.symm.trans e') (e.symm.trans e').source) namespace charted_space_core variables [topological_space H] (c : charted_space_core H M) {e : local_equiv M H} /-- Topology generated by a set of charts on a Type. -/ protected def to_topological_space : topological_space M := topological_space.generate_from $ ⋃ (e : local_equiv M H) (he : e ∈ c.atlas) (s : set H) (s_open : is_open s), {e ⁻¹' s ∩ e.source} lemma open_source' (he : e ∈ c.atlas) : @is_open M c.to_topological_space e.source := begin apply topological_space.generate_open.basic, simp only [exists_prop, mem_Union, mem_singleton_iff], refine ⟨e, he, univ, is_open_univ, _⟩, simp only [set.univ_inter, set.preimage_univ] end lemma open_target (he : e ∈ c.atlas) : is_open e.target := begin have E : e.target ∩ e.symm ⁻¹' e.source = e.target := subset.antisymm (inter_subset_left _ _) (λx hx, ⟨hx, local_equiv.target_subset_preimage_source _ hx⟩), simpa [local_equiv.trans_source, E] using c.open_source e e he he end /-- An element of the atlas in a charted space without topology becomes a local homeomorphism for the topology constructed from this atlas. The `local_homeomorph` version is given in this definition. -/ protected def local_homeomorph (e : local_equiv M H) (he : e ∈ c.atlas) : @local_homeomorph M H c.to_topological_space _ := { open_source := by convert c.open_source' he, open_target := by convert c.open_target he, continuous_to_fun := begin letI : topological_space M := c.to_topological_space, rw continuous_on_open_iff (c.open_source' he), assume s s_open, rw inter_comm, apply topological_space.generate_open.basic, simp only [exists_prop, mem_Union, mem_singleton_iff], exact ⟨e, he, ⟨s, s_open, rfl⟩⟩ end, continuous_inv_fun := begin letI : topological_space M := c.to_topological_space, apply continuous_on_open_of_generate_from (c.open_target he), assume t ht, simp only [exists_prop, mem_Union, mem_singleton_iff] at ht, rcases ht with ⟨e', e'_atlas, s, s_open, ts⟩, rw ts, let f := e.symm.trans e', have : is_open (f ⁻¹' s ∩ f.source), by simpa [inter_comm] using (continuous_on_open_iff (c.open_source e e' he e'_atlas)).1 (c.continuous_to_fun e e' he e'_atlas) s s_open, have A : e' ∘ e.symm ⁻¹' s ∩ (e.target ∩ e.symm ⁻¹' e'.source) = e.target ∩ (e' ∘ e.symm ⁻¹' s ∩ e.symm ⁻¹' e'.source), by { rw [← inter_assoc, ← inter_assoc], congr' 1, exact inter_comm _ _ }, simpa [local_equiv.trans_source, preimage_inter, preimage_comp.symm, A] using this end, ..e } /-- Given a charted space without topology, endow it with a genuine charted space structure with respect to the topology constructed from the atlas. -/ def to_charted_space : @charted_space H _ M c.to_topological_space := { atlas := ⋃ (e : local_equiv M H) (he : e ∈ c.atlas), {c.local_homeomorph e he}, chart_at := λx, c.local_homeomorph (c.chart_at x) (c.chart_mem_atlas x), mem_chart_source := λx, c.mem_chart_source x, chart_mem_atlas := λx, begin simp only [mem_Union, mem_singleton_iff], exact ⟨c.chart_at x, c.chart_mem_atlas x, rfl⟩, end } end charted_space_core /-! ### Charted space with a given structure groupoid -/ section has_groupoid variables [topological_space H] [topological_space M] [charted_space H M] /-- A charted space has an atlas in a groupoid `G` if the change of coordinates belong to the groupoid -/ class has_groupoid {H : Type*} [topological_space H] (M : Type*) [topological_space M] [charted_space H M] (G : structure_groupoid H) : Prop := (compatible [] : ∀{e e' : local_homeomorph M H}, e ∈ atlas H M → e' ∈ atlas H M → e.symm ≫ₕ e' ∈ G) /-- Reformulate in the `structure_groupoid` namespace the compatibility condition of charts in a charted space admitting a structure groupoid, to make it more easily accessible with dot notation. -/ lemma structure_groupoid.compatible {H : Type*} [topological_space H] (G : structure_groupoid H) {M : Type*} [topological_space M] [charted_space H M] [has_groupoid M G] {e e' : local_homeomorph M H} (he : e ∈ atlas H M) (he' : e' ∈ atlas H M) : e.symm ≫ₕ e' ∈ G := has_groupoid.compatible G he he' lemma has_groupoid_of_le {G₁ G₂ : structure_groupoid H} (h : has_groupoid M G₁) (hle : G₁ ≤ G₂) : has_groupoid M G₂ := ⟨ λ e e' he he', hle ((h.compatible : _) he he') ⟩ lemma has_groupoid_of_pregroupoid (PG : pregroupoid H) (h : ∀{e e' : local_homeomorph M H}, e ∈ atlas H M → e' ∈ atlas H M → PG.property (e.symm ≫ₕ e') (e.symm ≫ₕ e').source) : has_groupoid M (PG.groupoid) := ⟨assume e e' he he', mem_groupoid_of_pregroupoid.mpr ⟨h he he', h he' he⟩⟩ /-- The trivial charted space structure on the model space is compatible with any groupoid -/ instance has_groupoid_model_space (H : Type*) [topological_space H] (G : structure_groupoid H) : has_groupoid H G := { compatible := λe e' he he', begin replace he : e ∈ atlas H H := he, replace he' : e' ∈ atlas H H := he', rw charted_space_self_atlas at he he', simp [he, he', structure_groupoid.id_mem] end } /-- Any charted space structure is compatible with the groupoid of all local homeomorphisms -/ instance has_groupoid_continuous_groupoid : has_groupoid M (continuous_groupoid H) := ⟨begin assume e e' he he', rw [continuous_groupoid, mem_groupoid_of_pregroupoid], simp only [and_self] end⟩ section maximal_atlas variables (M) (G : structure_groupoid H) /-- Given a charted space admitting a structure groupoid, the maximal atlas associated to this structure groupoid is the set of all local charts that are compatible with the atlas, i.e., such that changing coordinates with an atlas member gives an element of the groupoid. -/ def structure_groupoid.maximal_atlas : set (local_homeomorph M H) := {e | ∀ e' ∈ atlas H M, e.symm ≫ₕ e' ∈ G ∧ e'.symm ≫ₕ e ∈ G} variable {M} /-- The elements of the atlas belong to the maximal atlas for any structure groupoid -/ lemma structure_groupoid.mem_maximal_atlas_of_mem_atlas [has_groupoid M G] {e : local_homeomorph M H} (he : e ∈ atlas H M) : e ∈ G.maximal_atlas M := λ e' he', ⟨G.compatible he he', G.compatible he' he⟩ lemma structure_groupoid.chart_mem_maximal_atlas [has_groupoid M G] (x : M) : chart_at H x ∈ G.maximal_atlas M := G.mem_maximal_atlas_of_mem_atlas (chart_mem_atlas H x) variable {G} lemma mem_maximal_atlas_iff {e : local_homeomorph M H} : e ∈ G.maximal_atlas M ↔ ∀ e' ∈ atlas H M, e.symm ≫ₕ e' ∈ G ∧ e'.symm ≫ₕ e ∈ G := iff.rfl /-- Changing coordinates between two elements of the maximal atlas gives rise to an element of the structure groupoid. -/ lemma structure_groupoid.compatible_of_mem_maximal_atlas {e e' : local_homeomorph M H} (he : e ∈ G.maximal_atlas M) (he' : e' ∈ G.maximal_atlas M) : e.symm ≫ₕ e' ∈ G := begin apply G.locality (λ x hx, _), set f := chart_at H (e.symm x) with hf, let s := e.target ∩ (e.symm ⁻¹' f.source), have hs : is_open s, { apply e.symm.continuous_to_fun.preimage_open_of_open; apply open_source }, have xs : x ∈ s, by { dsimp at hx, simp [s, hx] }, refine ⟨s, hs, xs, _⟩, have A : e.symm ≫ₕ f ∈ G := (mem_maximal_atlas_iff.1 he f (chart_mem_atlas _ _)).1, have B : f.symm ≫ₕ e' ∈ G := (mem_maximal_atlas_iff.1 he' f (chart_mem_atlas _ _)).2, have C : (e.symm ≫ₕ f) ≫ₕ (f.symm ≫ₕ e') ∈ G := G.trans A B, have D : (e.symm ≫ₕ f) ≫ₕ (f.symm ≫ₕ e') ≈ (e.symm ≫ₕ e').restr s := calc (e.symm ≫ₕ f) ≫ₕ (f.symm ≫ₕ e') = e.symm ≫ₕ (f ≫ₕ f.symm) ≫ₕ e' : by simp [trans_assoc] ... ≈ e.symm ≫ₕ (of_set f.source f.open_source) ≫ₕ e' : by simp [eq_on_source.trans', trans_self_symm] ... ≈ (e.symm ≫ₕ (of_set f.source f.open_source)) ≫ₕ e' : by simp [trans_assoc] ... ≈ (e.symm.restr s) ≫ₕ e' : by simp [s, trans_of_set'] ... ≈ (e.symm ≫ₕ e').restr s : by simp [restr_trans], exact G.eq_on_source C (setoid.symm D), end variable (G) /-- In the model space, the identity is in any maximal atlas. -/ lemma structure_groupoid.id_mem_maximal_atlas : local_homeomorph.refl H ∈ G.maximal_atlas H := G.mem_maximal_atlas_of_mem_atlas (by simp) end maximal_atlas section singleton variables {α : Type*} [topological_space α] namespace local_homeomorph variable (e : local_homeomorph α H) /-- If a single local homeomorphism `e` from a space `α` into `H` has source covering the whole space `α`, then that local homeomorphism induces an `H`-charted space structure on `α`. (This condition is equivalent to `e` being an open embedding of `α` into `H`; see `open_embedding.singleton_charted_space`.) -/ def singleton_charted_space (h : e.source = set.univ) : charted_space H α := { atlas := {e}, chart_at := λ _, e, mem_chart_source := λ _, by simp only [h] with mfld_simps, chart_mem_atlas := λ _, by tauto } @[simp, mfld_simps] lemma singleton_charted_space_chart_at_eq (h : e.source = set.univ) {x : α} : @chart_at H _ α _ (e.singleton_charted_space h) x = e := rfl lemma singleton_charted_space_chart_at_source (h : e.source = set.univ) {x : α} : (@chart_at H _ α _ (e.singleton_charted_space h) x).source = set.univ := h lemma singleton_charted_space_mem_atlas_eq (h : e.source = set.univ) (e' : local_homeomorph α H) (h' : e' ∈ (e.singleton_charted_space h).atlas) : e' = e := h' /-- Given a local homeomorphism `e` from a space `α` into `H`, if its source covers the whole space `α`, then the induced charted space structure on `α` is `has_groupoid G` for any structure groupoid `G` which is closed under restrictions. -/ lemma singleton_has_groupoid (h : e.source = set.univ) (G : structure_groupoid H) [closed_under_restriction G] : @has_groupoid _ _ _ _ (e.singleton_charted_space h) G := { compatible := begin intros e' e'' he' he'', rw e.singleton_charted_space_mem_atlas_eq h e' he', rw e.singleton_charted_space_mem_atlas_eq h e'' he'', refine G.eq_on_source _ e.trans_symm_self, have hle : id_restr_groupoid ≤ G := (closed_under_restriction_iff_id_le G).mp (by assumption), exact structure_groupoid.le_iff.mp hle _ (id_restr_groupoid_mem _), end } end local_homeomorph namespace open_embedding variable [nonempty α] /-- An open embedding of `α` into `H` induces an `H`-charted space structure on `α`. See `local_homeomorph.singleton_charted_space` -/ def singleton_charted_space {f : α → H} (h : open_embedding f) : charted_space H α := (h.to_local_homeomorph f).singleton_charted_space (by simp) lemma singleton_charted_space_chart_at_eq {f : α → H} (h : open_embedding f) {x : α} : ⇑(@chart_at H _ α _ (h.singleton_charted_space) x) = f := rfl lemma singleton_has_groupoid {f : α → H} (h : open_embedding f) (G : structure_groupoid H) [closed_under_restriction G] : @has_groupoid _ _ _ _ h.singleton_charted_space G := (h.to_local_homeomorph f).singleton_has_groupoid (by simp) G end open_embedding end singleton namespace topological_space.opens open topological_space variables (G : structure_groupoid H) [has_groupoid M G] variables (s : opens M) /-- An open subset of a charted space is naturally a charted space. -/ instance : charted_space H s := { atlas := ⋃ (x : s), {@local_homeomorph.subtype_restr _ _ _ _ (chart_at H x.1) s ⟨x⟩}, chart_at := λ x, @local_homeomorph.subtype_restr _ _ _ _ (chart_at H x.1) s ⟨x⟩, mem_chart_source := λ x, by { simp only with mfld_simps, exact (mem_chart_source H x.1) }, chart_mem_atlas := λ x, by { simp only [mem_Union, mem_singleton_iff], use x } } /-- If a groupoid `G` is `closed_under_restriction`, then an open subset of a space which is `has_groupoid G` is naturally `has_groupoid G`. -/ instance [closed_under_restriction G] : has_groupoid s G := { compatible := begin rintros e e' ⟨_, ⟨x, hc⟩, he⟩ ⟨_, ⟨x', hc'⟩, he'⟩, haveI : nonempty s := ⟨x⟩, simp only [hc.symm, mem_singleton_iff, subtype.val_eq_coe] at he, simp only [hc'.symm, mem_singleton_iff, subtype.val_eq_coe] at he', rw [he, he'], convert G.eq_on_source _ (subtype_restr_symm_trans_subtype_restr s (chart_at H x) (chart_at H x')), apply closed_under_restriction', { exact G.compatible (chart_mem_atlas H x) (chart_mem_atlas H x') }, { exact preimage_open_of_open_symm (chart_at H x) s.2 }, end } end topological_space.opens /-! ### Structomorphisms -/ /-- A `G`-diffeomorphism between two charted spaces is a homeomorphism which, when read in the charts, belongs to `G`. We avoid the word diffeomorph as it is too related to the smooth category, and use structomorph instead. -/ @[nolint has_inhabited_instance] structure structomorph (G : structure_groupoid H) (M : Type*) (M' : Type*) [topological_space M] [topological_space M'] [charted_space H M] [charted_space H M'] extends homeomorph M M' := (mem_groupoid : ∀c : local_homeomorph M H, ∀c' : local_homeomorph M' H, c ∈ atlas H M → c' ∈ atlas H M' → c.symm ≫ₕ to_homeomorph.to_local_homeomorph ≫ₕ c' ∈ G) variables [topological_space M'] [topological_space M''] {G : structure_groupoid H} [charted_space H M'] [charted_space H M''] /-- The identity is a diffeomorphism of any charted space, for any groupoid. -/ def structomorph.refl (M : Type*) [topological_space M] [charted_space H M] [has_groupoid M G] : structomorph G M M := { mem_groupoid := λc c' hc hc', begin change (local_homeomorph.symm c) ≫ₕ (local_homeomorph.refl M) ≫ₕ c' ∈ G, rw local_homeomorph.refl_trans, exact has_groupoid.compatible G hc hc' end, ..homeomorph.refl M } /-- The inverse of a structomorphism is a structomorphism -/ def structomorph.symm (e : structomorph G M M') : structomorph G M' M := { mem_groupoid := begin assume c c' hc hc', have : (c'.symm ≫ₕ e.to_homeomorph.to_local_homeomorph ≫ₕ c).symm ∈ G := G.symm (e.mem_groupoid c' c hc' hc), rwa [trans_symm_eq_symm_trans_symm, trans_symm_eq_symm_trans_symm, symm_symm, trans_assoc] at this, end, ..e.to_homeomorph.symm} /-- The composition of structomorphisms is a structomorphism -/ def structomorph.trans (e : structomorph G M M') (e' : structomorph G M' M'') : structomorph G M M'' := { mem_groupoid := begin /- Let c and c' be two charts in M and M''. We want to show that e' ∘ e is smooth in these charts, around any point x. For this, let y = e (c⁻¹ x), and consider a chart g around y. Then g ∘ e ∘ c⁻¹ and c' ∘ e' ∘ g⁻¹ are both smooth as e and e' are structomorphisms, so their composition is smooth, and it coincides with c' ∘ e' ∘ e ∘ c⁻¹ around x. -/ assume c c' hc hc', refine G.locality (λx hx, _), let f₁ := e.to_homeomorph.to_local_homeomorph, let f₂ := e'.to_homeomorph.to_local_homeomorph, let f := (e.to_homeomorph.trans e'.to_homeomorph).to_local_homeomorph, have feq : f = f₁ ≫ₕ f₂ := homeomorph.trans_to_local_homeomorph _ _, -- define the atlas g around y let y := (c.symm ≫ₕ f₁) x, let g := chart_at H y, have hg₁ := chart_mem_atlas H y, have hg₂ := mem_chart_source H y, let s := (c.symm ≫ₕ f₁).source ∩ (c.symm ≫ₕ f₁) ⁻¹' g.source, have open_s : is_open s, by apply (c.symm ≫ₕ f₁).continuous_to_fun.preimage_open_of_open; apply open_source, have : x ∈ s, { split, { simp only [trans_source, preimage_univ, inter_univ, homeomorph.to_local_homeomorph_source], rw trans_source at hx, exact hx.1 }, { exact hg₂ } }, refine ⟨s, open_s, this, _⟩, let F₁ := (c.symm ≫ₕ f₁ ≫ₕ g) ≫ₕ (g.symm ≫ₕ f₂ ≫ₕ c'), have A : F₁ ∈ G := G.trans (e.mem_groupoid c g hc hg₁) (e'.mem_groupoid g c' hg₁ hc'), let F₂ := (c.symm ≫ₕ f ≫ₕ c').restr s, have : F₁ ≈ F₂ := calc F₁ ≈ c.symm ≫ₕ f₁ ≫ₕ (g ≫ₕ g.symm) ≫ₕ f₂ ≫ₕ c' : by simp [F₁, trans_assoc] ... ≈ c.symm ≫ₕ f₁ ≫ₕ (of_set g.source g.open_source) ≫ₕ f₂ ≫ₕ c' : by simp [eq_on_source.trans', trans_self_symm g] ... ≈ ((c.symm ≫ₕ f₁) ≫ₕ (of_set g.source g.open_source)) ≫ₕ (f₂ ≫ₕ c') : by simp [trans_assoc] ... ≈ ((c.symm ≫ₕ f₁).restr s) ≫ₕ (f₂ ≫ₕ c') : by simp [s, trans_of_set'] ... ≈ ((c.symm ≫ₕ f₁) ≫ₕ (f₂ ≫ₕ c')).restr s : by simp [restr_trans] ... ≈ (c.symm ≫ₕ (f₁ ≫ₕ f₂) ≫ₕ c').restr s : by simp [eq_on_source.restr, trans_assoc] ... ≈ F₂ : by simp [F₂, feq], have : F₂ ∈ G := G.eq_on_source A (setoid.symm this), exact this end, ..homeomorph.trans e.to_homeomorph e'.to_homeomorph } end has_groupoid
f15d829c43f7e43a55a7543edca32e41a0b5b6b6
a7eef317ddec01b9fc6cfbb876fe7ac00f205ac7
/src/topology/metric_space/gromov_hausdorff_realized.lean
1c168ddb679766e2bd5879d2406332a7afea7a9d
[ "Apache-2.0" ]
permissive
kmill/mathlib
ea5a007b67ae4e9e18dd50d31d8aa60f650425ee
1a419a9fea7b959317eddd556e1bb9639f4dcc05
refs/heads/master
1,668,578,197,719
1,593,629,163,000
1,593,629,163,000
276,482,939
0
0
null
1,593,637,960,000
1,593,637,959,000
null
UTF-8
Lean
false
false
26,281
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 Construction of a good coupling between nonempty compact metric spaces, minimizing their Hausdorff distance. This construction is instrumental to study the Gromov-Hausdorff distance between nonempty compact metric spaces -/ import topology.metric_space.gluing import topology.metric_space.hausdorff_distance noncomputable theory open_locale classical open_locale topological_space universes u v w open classical set function topological_space filter metric quotient open bounded_continuous_function open sum (inl inr) local attribute [instance] metric_space_sum namespace Gromov_Hausdorff section Gromov_Hausdorff_realized /- This section shows that the Gromov-Hausdorff distance is realized. For this, we consider candidate distances on the disjoint union α ⊕ β of two compact nonempty metric spaces, almost realizing the Gromov-Hausdorff distance, and show that they form a compact family by applying Arzela-Ascoli theorem. The existence of a minimizer follows. -/ section definitions variables (α : Type u) (β : Type v) [metric_space α] [compact_space α] [nonempty α] [metric_space β] [compact_space β] [nonempty β] @[reducible] private def prod_space_fun : Type* := ((α ⊕ β) × (α ⊕ β)) → ℝ @[reducible] private def Cb : Type* := bounded_continuous_function ((α ⊕ β) × (α ⊕ β)) ℝ private def max_var : nnreal := 2 * ⟨diam (univ : set α), diam_nonneg⟩ + 1 + 2 * ⟨diam (univ : set β), diam_nonneg⟩ private lemma one_le_max_var : 1 ≤ max_var α β := calc (1 : real) = 2 * 0 + 1 + 2 * 0 : by simp ... ≤ 2 * diam (univ : set α) + 1 + 2 * diam (univ : set β) : by apply_rules [add_le_add, mul_le_mul_of_nonneg_left, diam_nonneg, diam_nonneg]; norm_num /-- The set of functions on α ⊕ β that are candidates distances to realize the minimum of the Hausdorff distances between α and β in a coupling -/ def candidates : set (prod_space_fun α β) := {f | (((((∀x y : α, f (sum.inl x, sum.inl y) = dist x y) ∧ (∀x y : β, f (sum.inr x, sum.inr y) = dist x y)) ∧ (∀x y, f (x, y) = f (y, x))) ∧ (∀x y z, f (x, z) ≤ f (x, y) + f (y, z))) ∧ (∀x, f (x, x) = 0)) ∧ (∀x y, f (x, y) ≤ max_var α β) } /-- Version of the set of candidates in bounded_continuous_functions, to apply Arzela-Ascoli -/ private def candidates_b : set (Cb α β) := {f : Cb α β | f.val ∈ candidates α β} end definitions --section section constructions variables {α : Type u} {β : Type v} [metric_space α] [compact_space α] [nonempty α] [metric_space β] [compact_space β] [nonempty β] {f : prod_space_fun α β} {x y z t : α ⊕ β} local attribute [instance, priority 10] inhabited_of_nonempty' private lemma max_var_bound : dist x y ≤ max_var α β := calc dist x y ≤ diam (univ : set (α ⊕ β)) : dist_le_diam_of_mem (bounded_of_compact compact_univ) (mem_univ _) (mem_univ _) ... = diam (inl '' (univ : set α) ∪ inr '' (univ : set β)) : by apply congr_arg; ext x y z; cases x; simp [mem_univ, mem_range_self] ... ≤ diam (inl '' (univ : set α)) + dist (inl (default α)) (inr (default β)) + diam (inr '' (univ : set β)) : diam_union (mem_image_of_mem _ (mem_univ _)) (mem_image_of_mem _ (mem_univ _)) ... = diam (univ : set α) + (dist (default α) (default α) + 1 + dist (default β) (default β)) + diam (univ : set β) : by { rw [isometry_on_inl.diam_image, isometry_on_inr.diam_image], refl } ... = 1 * diam (univ : set α) + 1 + 1 * diam (univ : set β) : by simp ... ≤ 2 * diam (univ : set α) + 1 + 2 * diam (univ : set β) : begin apply_rules [add_le_add, mul_le_mul_of_nonneg_right, diam_nonneg, diam_nonneg, le_refl], norm_num, norm_num end private lemma candidates_symm (fA : f ∈ candidates α β) : f (x, y) = f (y ,x) := fA.1.1.1.2 x y private lemma candidates_triangle (fA : f ∈ candidates α β) : f (x, z) ≤ f (x, y) + f (y, z) := fA.1.1.2 x y z private lemma candidates_refl (fA : f ∈ candidates α β) : f (x, x) = 0 := fA.1.2 x private lemma candidates_nonneg (fA : f ∈ candidates α β) : 0 ≤ f (x, y) := begin have : 0 ≤ 2 * f (x, y) := calc 0 = f (x, x) : (candidates_refl fA).symm ... ≤ f (x, y) + f (y, x) : candidates_triangle fA ... = f (x, y) + f (x, y) : by rw [candidates_symm fA] ... = 2 * f (x, y) : by ring, by linarith end private lemma candidates_dist_inl (fA : f ∈ candidates α β) (x y: α) : f (inl x, inl y) = dist x y := fA.1.1.1.1.1 x y private lemma candidates_dist_inr (fA : f ∈ candidates α β) (x y : β) : f (inr x, inr y) = dist x y := fA.1.1.1.1.2 x y private lemma candidates_le_max_var (fA : f ∈ candidates α β) : f (x, y) ≤ max_var α β := fA.2 x y /-- candidates are bounded by max_var α β -/ private lemma candidates_dist_bound (fA : f ∈ candidates α β) : ∀ {x y : α ⊕ β}, f (x, y) ≤ max_var α β * dist x y | (inl x) (inl y) := calc f (inl x, inl y) = dist x y : candidates_dist_inl fA x y ... = dist (inl x) (inl y) : by { rw @sum.dist_eq α β, refl } ... = 1 * dist (inl x) (inl y) : by simp ... ≤ max_var α β * dist (inl x) (inl y) : mul_le_mul_of_nonneg_right (one_le_max_var α β) dist_nonneg | (inl x) (inr y) := calc f (inl x, inr y) ≤ max_var α β : candidates_le_max_var fA ... = max_var α β * 1 : by simp ... ≤ max_var α β * dist (inl x) (inr y) : mul_le_mul_of_nonneg_left sum.one_dist_le (le_trans (zero_le_one) (one_le_max_var α β)) | (inr x) (inl y) := calc f (inr x, inl y) ≤ max_var α β : candidates_le_max_var fA ... = max_var α β * 1 : by simp ... ≤ max_var α β * dist (inl x) (inr y) : mul_le_mul_of_nonneg_left sum.one_dist_le (le_trans (zero_le_one) (one_le_max_var α β)) | (inr x) (inr y) := calc f (inr x, inr y) = dist x y : candidates_dist_inr fA x y ... = dist (inr x) (inr y) : by { rw @sum.dist_eq α β, refl } ... = 1 * dist (inr x) (inr y) : by simp ... ≤ max_var α β * dist (inr x) (inr y) : mul_le_mul_of_nonneg_right (one_le_max_var α β) dist_nonneg /-- Technical lemma to prove that candidates are Lipschitz -/ private lemma candidates_lipschitz_aux (fA : f ∈ candidates α β) : f (x, y) - f (z, t) ≤ 2 * max_var α β * dist (x, y) (z, t) := calc f (x, y) - f(z, t) ≤ f (x, t) + f (t, y) - f (z, t) : add_le_add_right (candidates_triangle fA) _ ... ≤ (f (x, z) + f (z, t) + f(t, y)) - f (z, t) : add_le_add_right (add_le_add_right (candidates_triangle fA) _ ) _ ... = f (x, z) + f (t, y) : by simp [sub_eq_add_neg, add_assoc] ... ≤ max_var α β * dist x z + max_var α β * dist t y : add_le_add (candidates_dist_bound fA) (candidates_dist_bound fA) ... ≤ max_var α β * max (dist x z) (dist t y) + max_var α β * max (dist x z) (dist t y) : begin apply add_le_add, apply mul_le_mul_of_nonneg_left (le_max_left (dist x z) (dist t y)) (le_trans zero_le_one (one_le_max_var α β)), apply mul_le_mul_of_nonneg_left (le_max_right (dist x z) (dist t y)) (le_trans zero_le_one (one_le_max_var α β)), end ... = 2 * max_var α β * max (dist x z) (dist y t) : by { simp [dist_comm], ring } ... = 2 * max_var α β * dist (x, y) (z, t) : by refl /-- Candidates are Lipschitz -/ private lemma candidates_lipschitz (fA : f ∈ candidates α β) : lipschitz_with (2 * max_var α β) f := begin apply lipschitz_with.of_dist_le_mul, rintros ⟨x, y⟩ ⟨z, t⟩, rw real.dist_eq, apply abs_le_of_le_of_neg_le, { exact candidates_lipschitz_aux fA }, { have : -(f (x, y) - f (z, t)) = f (z, t) - f (x, y), by ring, rw [this, dist_comm], exact candidates_lipschitz_aux fA } end /-- candidates give rise to elements of bounded_continuous_functions -/ def candidates_b_of_candidates (f : prod_space_fun α β) (fA : f ∈ candidates α β) : Cb α β := bounded_continuous_function.mk_of_compact f (candidates_lipschitz fA).continuous lemma candidates_b_of_candidates_mem (f : prod_space_fun α β) (fA : f ∈ candidates α β) : candidates_b_of_candidates f fA ∈ candidates_b α β := fA /-- The distance on α ⊕ β is a candidate -/ private lemma dist_mem_candidates : (λp : (α ⊕ β) × (α ⊕ β), dist p.1 p.2) ∈ candidates α β := begin simp only [candidates, dist_comm, forall_const, and_true, add_comm, eq_self_iff_true, and_self, sum.forall, set.mem_set_of_eq, dist_self], repeat { split <|> exact (λa y z, dist_triangle_left _ _ _) <|> exact (λx y, by refl) <|> exact (λx y, max_var_bound) } end def candidates_b_dist (α : Type u) (β : Type v) [metric_space α] [compact_space α] [inhabited α] [metric_space β] [compact_space β] [inhabited β] : Cb α β := candidates_b_of_candidates _ dist_mem_candidates lemma candidates_b_dist_mem_candidates_b : candidates_b_dist α β ∈ candidates_b α β := candidates_b_of_candidates_mem _ _ private lemma candidates_b_nonempty : (candidates_b α β).nonempty := ⟨_, candidates_b_dist_mem_candidates_b⟩ /-- To apply Arzela-Ascoli, we need to check that the set of candidates is closed and equicontinuous. Equicontinuity follows from the Lipschitz control, we check closedness -/ private lemma closed_candidates_b : is_closed (candidates_b α β) := begin have I1 : ∀x y, is_closed {f : Cb α β | f (inl x, inl y) = dist x y} := λx y, is_closed_eq continuous_evalx continuous_const, have I2 : ∀x y, is_closed {f : Cb α β | f (inr x, inr y) = dist x y } := λx y, is_closed_eq continuous_evalx continuous_const, have I3 : ∀x y, is_closed {f : Cb α β | f (x, y) = f (y, x)} := λx y, is_closed_eq continuous_evalx continuous_evalx, have I4 : ∀x y z, is_closed {f : Cb α β | f (x, z) ≤ f (x, y) + f (y, z)} := λx y z, is_closed_le continuous_evalx (continuous_evalx.add continuous_evalx), have I5 : ∀x, is_closed {f : Cb α β | f (x, x) = 0} := λx, is_closed_eq continuous_evalx continuous_const, have I6 : ∀x y, is_closed {f : Cb α β | f (x, y) ≤ max_var α β} := λx y, is_closed_le continuous_evalx continuous_const, have : candidates_b α β = (⋂x y, {f : Cb α β | f ((@inl α β x), (@inl α β y)) = dist x y}) ∩ (⋂x y, {f : Cb α β | f ((@inr α β x), (@inr α β y)) = dist x y}) ∩ (⋂x y, {f : Cb α β | f (x, y) = f (y, x)}) ∩ (⋂x y z, {f : Cb α β | f (x, z) ≤ f (x, y) + f (y, z)}) ∩ (⋂x, {f : Cb α β | f (x, x) = 0}) ∩ (⋂x y, {f : Cb α β | f (x, y) ≤ max_var α β}) := begin ext, unfold candidates_b, unfold candidates, simp [-sum.forall], refl end, rw this, repeat { apply is_closed_inter _ _ <|> apply is_closed_Inter _ <|> apply I1 _ _ <|> apply I2 _ _ <|> apply I3 _ _ <|> apply I4 _ _ _ <|> apply I5 _ <|> apply I6 _ _ <|> assume x }, end /-- Compactness of candidates (in bounded_continuous_functions) follows -/ private lemma compact_candidates_b : compact (candidates_b α β) := begin refine arzela_ascoli₂ (Icc 0 (max_var α β)) compact_Icc (candidates_b α β) closed_candidates_b _ _, { rintros f ⟨x1, x2⟩ hf, simp only [set.mem_Icc], exact ⟨candidates_nonneg hf, candidates_le_max_var hf⟩ }, { refine equicontinuous_of_continuity_modulus (λt, 2 * max_var α β * t) _ _ _, { have : tendsto (λ (t : ℝ), 2 * (max_var α β : ℝ) * t) (𝓝 0) (𝓝 (2 * max_var α β * 0)) := tendsto_const_nhds.mul tendsto_id, simpa using this }, { assume x y f hf, exact (candidates_lipschitz hf).dist_le_mul _ _ } } end /-- We will then choose the candidate minimizing the Hausdorff distance. Except that we are not in a metric space setting, so we need to define our custom version of Hausdorff distance, called HD, and prove its basic properties. -/ def HD (f : Cb α β) := max (⨆ x, ⨅ y, f (inl x, inr y)) (⨆ y, ⨅ x, f (inl x, inr y)) /- We will show that HD is continuous on bounded_continuous_functions, to deduce that its minimum on the compact set candidates_b is attained. Since it is defined in terms of infimum and supremum on ℝ, which is only conditionnally complete, we will need all the time to check that the defining sets are bounded below or above. This is done in the next few technical lemmas -/ lemma HD_below_aux1 {f : Cb α β} (C : ℝ) {x : α} : bdd_below (range (λ (y : β), f (inl x, inr y) + C)) := let ⟨cf, hcf⟩ := (real.bounded_iff_bdd_below_bdd_above.1 bounded_range).1 in ⟨cf + C, forall_range_iff.2 (λi, add_le_add_right ((λx, hcf (mem_range_self x)) _) _)⟩ private lemma HD_bound_aux1 (f : Cb α β) (C : ℝ) : bdd_above (range (λ (x : α), ⨅ y, f (inl x, inr y) + C)) := begin rcases (real.bounded_iff_bdd_below_bdd_above.1 bounded_range).2 with ⟨Cf, hCf⟩, refine ⟨Cf + C, forall_range_iff.2 (λx, _)⟩, calc (⨅ y, f (inl x, inr y) + C) ≤ f (inl x, inr (default β)) + C : cinfi_le (HD_below_aux1 C) (default β) ... ≤ Cf + C : add_le_add ((λx, hCf (mem_range_self x)) _) (le_refl _) end lemma HD_below_aux2 {f : Cb α β} (C : ℝ) {y : β} : bdd_below (range (λ (x : α), f (inl x, inr y) + C)) := let ⟨cf, hcf⟩ := (real.bounded_iff_bdd_below_bdd_above.1 bounded_range).1 in ⟨cf + C, forall_range_iff.2 (λi, add_le_add_right ((λx, hcf (mem_range_self x)) _) _)⟩ private lemma HD_bound_aux2 (f : Cb α β) (C : ℝ) : bdd_above (range (λ (y : β), ⨅ x, f (inl x, inr y) + C)) := begin rcases (real.bounded_iff_bdd_below_bdd_above.1 bounded_range).2 with ⟨Cf, hCf⟩, refine ⟨Cf + C, forall_range_iff.2 (λy, _)⟩, calc (⨅ x, f (inl x, inr y) + C) ≤ f (inl (default α), inr y) + C : cinfi_le (HD_below_aux2 C) (default α) ... ≤ Cf + C : add_le_add ((λx, hCf (mem_range_self x)) _) (le_refl _) end /-- Explicit bound on HD (dist). This means that when looking for minimizers it will be sufficient to look for functions with HD(f) bounded by this bound. -/ lemma HD_candidates_b_dist_le : HD (candidates_b_dist α β) ≤ diam (univ : set α) + 1 + diam (univ : set β) := begin refine max_le (csupr_le (λx, _)) (csupr_le (λy, _)), { have A : (⨅ y, candidates_b_dist α β (inl x, inr y)) ≤ candidates_b_dist α β (inl x, inr (default β)) := cinfi_le (by simpa using HD_below_aux1 0) (default β), have B : dist (inl x) (inr (default β)) ≤ diam (univ : set α) + 1 + diam (univ : set β) := calc dist (inl x) (inr (default β)) = dist x (default α) + 1 + dist (default β) (default β) : rfl ... ≤ diam (univ : set α) + 1 + diam (univ : set β) : begin apply add_le_add (add_le_add _ (le_refl _)), exact dist_le_diam_of_mem (bounded_of_compact (compact_univ)) (mem_univ _) (mem_univ _), exact dist_le_diam_of_mem (bounded_of_compact (compact_univ)) (mem_univ _) (mem_univ _) end, exact le_trans A B }, { have A : (⨅ x, candidates_b_dist α β (inl x, inr y)) ≤ candidates_b_dist α β (inl (default α), inr y) := cinfi_le (by simpa using HD_below_aux2 0) (default α), have B : dist (inl (default α)) (inr y) ≤ diam (univ : set α) + 1 + diam (univ : set β) := calc dist (inl (default α)) (inr y) = dist (default α) (default α) + 1 + dist (default β) y : rfl ... ≤ diam (univ : set α) + 1 + diam (univ : set β) : begin apply add_le_add (add_le_add _ (le_refl _)), exact dist_le_diam_of_mem (bounded_of_compact (compact_univ)) (mem_univ _) (mem_univ _), exact dist_le_diam_of_mem (bounded_of_compact (compact_univ)) (mem_univ _) (mem_univ _) end, exact le_trans A B }, end /- To check that HD is continuous, we check that it is Lipschitz. As HD is a max, we prove separately inequalities controlling the two terms (relying too heavily on copy-paste...) -/ private lemma HD_lipschitz_aux1 (f g : Cb α β) : (⨆ x, ⨅ y, f (inl x, inr y)) ≤ (⨆ x, ⨅ y, g (inl x, inr y)) + dist f g := begin rcases (real.bounded_iff_bdd_below_bdd_above.1 bounded_range).1 with ⟨cg, hcg⟩, have Hcg : ∀x, cg ≤ g x := λx, hcg (mem_range_self x), rcases (real.bounded_iff_bdd_below_bdd_above.1 bounded_range).1 with ⟨cf, hcf⟩, have Hcf : ∀x, cf ≤ f x := λx, hcf (mem_range_self x), -- prove the inequality but with `dist f g` inside, by using inequalities comparing -- supr to supr and infi to infi have Z : (⨆ x, ⨅ y, f (inl x, inr y)) ≤ ⨆ x, ⨅ y, g (inl x, inr y) + dist f g := csupr_le_csupr (HD_bound_aux1 _ (dist f g)) (λx, cinfi_le_cinfi ⟨cf, forall_range_iff.2(λi, Hcf _)⟩ (λy, coe_le_coe_add_dist)), -- move the `dist f g` out of the infimum and the supremum, arguing that continuous monotone maps -- (here the addition of `dist f g`) preserve infimum and supremum have E1 : ∀x, (⨅ y, g (inl x, inr y)) + dist f g = ⨅ y, g (inl x, inr y) + dist f g, { assume x, refine map_cinfi_of_continuous_at_of_monotone (continuous_at_id.add continuous_at_const) _ _, { assume x y hx, simpa }, { show bdd_below (range (λ (y : β), g (inl x, inr y))), from ⟨cg, forall_range_iff.2(λi, Hcg _)⟩ } }, have E2 : (⨆ x, ⨅ y, g (inl x, inr y)) + dist f g = ⨆ x, (⨅ y, g (inl x, inr y)) + dist f g, { refine map_csupr_of_continuous_at_of_monotone (continuous_at_id.add continuous_at_const) _ _, { assume x y hx, simpa }, { by simpa using HD_bound_aux1 _ 0 } }, -- deduce the result from the above two steps simpa [E2, E1, function.comp] end private lemma HD_lipschitz_aux2 (f g : Cb α β) : (⨆ y, ⨅ x, f (inl x, inr y)) ≤ (⨆ y, ⨅ x, g (inl x, inr y)) + dist f g := begin rcases (real.bounded_iff_bdd_below_bdd_above.1 bounded_range).1 with ⟨cg, hcg⟩, have Hcg : ∀x, cg ≤ g x := λx, hcg (mem_range_self x), rcases (real.bounded_iff_bdd_below_bdd_above.1 bounded_range).1 with ⟨cf, hcf⟩, have Hcf : ∀x, cf ≤ f x := λx, hcf (mem_range_self x), -- prove the inequality but with `dist f g` inside, by using inequalities comparing -- supr to supr and infi to infi have Z : (⨆ y, ⨅ x, f (inl x, inr y)) ≤ ⨆ y, ⨅ x, g (inl x, inr y) + dist f g := csupr_le_csupr (HD_bound_aux2 _ (dist f g)) (λy, cinfi_le_cinfi ⟨cf, forall_range_iff.2(λi, Hcf _)⟩ (λy, coe_le_coe_add_dist)), -- move the `dist f g` out of the infimum and the supremum, arguing that continuous monotone maps -- (here the addition of `dist f g`) preserve infimum and supremum have E1 : ∀y, (⨅ x, g (inl x, inr y)) + dist f g = ⨅ x, g (inl x, inr y) + dist f g, { assume y, refine map_cinfi_of_continuous_at_of_monotone (continuous_at_id.add continuous_at_const) _ _, { assume x y hx, simpa }, { show bdd_below (range (λx:α, g (inl x, inr y))), from ⟨cg, forall_range_iff.2 (λi, Hcg _)⟩ } }, have E2 : (⨆ y, ⨅ x, g (inl x, inr y)) + dist f g = ⨆ y, (⨅ x, g (inl x, inr y)) + dist f g, { refine map_csupr_of_continuous_at_of_monotone (continuous_at_id.add continuous_at_const) _ _, { assume x y hx, simpa }, { by simpa using HD_bound_aux2 _ 0 } }, -- deduce the result from the above two steps simpa [E2, E1] end private lemma HD_lipschitz_aux3 (f g : Cb α β) : HD f ≤ HD g + dist f g := max_le (le_trans (HD_lipschitz_aux1 f g) (add_le_add_right (le_max_left _ _) _)) (le_trans (HD_lipschitz_aux2 f g) (add_le_add_right (le_max_right _ _) _)) /-- Conclude that HD, being Lipschitz, is continuous -/ private lemma HD_continuous : continuous (HD : Cb α β → ℝ) := lipschitz_with.continuous (lipschitz_with.of_le_add HD_lipschitz_aux3) end constructions --section section consequences variables (α : Type u) (β : Type v) [metric_space α] [compact_space α] [nonempty α] [metric_space β] [compact_space β] [nonempty β] /- Now that we have proved that the set of candidates is compact, and that HD is continuous, we can finally select a candidate minimizing HD. This will be the candidate realizing the optimal coupling. -/ private lemma exists_minimizer : ∃f ∈ candidates_b α β, ∀g ∈ candidates_b α β, HD f ≤ HD g := compact_candidates_b.exists_forall_le candidates_b_nonempty HD_continuous.continuous_on private definition optimal_GH_dist : Cb α β := classical.some (exists_minimizer α β) private lemma optimal_GH_dist_mem_candidates_b : optimal_GH_dist α β ∈ candidates_b α β := by cases (classical.some_spec (exists_minimizer α β)); assumption private lemma HD_optimal_GH_dist_le (g : Cb α β) (hg : g ∈ candidates_b α β) : HD (optimal_GH_dist α β) ≤ HD g := let ⟨Z1, Z2⟩ := classical.some_spec (exists_minimizer α β) in Z2 g hg /-- With the optimal candidate, construct a premetric space structure on α ⊕ β, on which the predistance is given by the candidate. Then, we will identify points at 0 predistance to obtain a genuine metric space -/ def premetric_optimal_GH_dist : premetric_space (α ⊕ β) := { dist := λp q, optimal_GH_dist α β (p, q), dist_self := λx, candidates_refl (optimal_GH_dist_mem_candidates_b α β), dist_comm := λx y, candidates_symm (optimal_GH_dist_mem_candidates_b α β), dist_triangle := λx y z, candidates_triangle (optimal_GH_dist_mem_candidates_b α β) } local attribute [instance] premetric_optimal_GH_dist premetric.dist_setoid /-- A metric space which realizes the optimal coupling between α and β -/ @[derive [metric_space]] definition optimal_GH_coupling : Type* := premetric.metric_quot (α ⊕ β) /-- Injection of α in the optimal coupling between α and β -/ def optimal_GH_injl (x : α) : optimal_GH_coupling α β := ⟦inl x⟧ /-- The injection of α in the optimal coupling between α and β is an isometry. -/ lemma isometry_optimal_GH_injl : isometry (optimal_GH_injl α β) := begin refine isometry_emetric_iff_metric.2 (λx y, _), change dist ⟦inl x⟧ ⟦inl y⟧ = dist x y, exact candidates_dist_inl (optimal_GH_dist_mem_candidates_b α β) _ _, end /-- Injection of β in the optimal coupling between α and β -/ def optimal_GH_injr (y : β) : optimal_GH_coupling α β := ⟦inr y⟧ /-- The injection of β in the optimal coupling between α and β is an isometry. -/ lemma isometry_optimal_GH_injr : isometry (optimal_GH_injr α β) := begin refine isometry_emetric_iff_metric.2 (λx y, _), change dist ⟦inr x⟧ ⟦inr y⟧ = dist x y, exact candidates_dist_inr (optimal_GH_dist_mem_candidates_b α β) _ _, end /-- The optimal coupling between two compact spaces α and β is still a compact space -/ instance compact_space_optimal_GH_coupling : compact_space (optimal_GH_coupling α β) := ⟨begin have : (univ : set (optimal_GH_coupling α β)) = (optimal_GH_injl α β '' univ) ∪ (optimal_GH_injr α β '' univ), { refine subset.antisymm (λxc hxc, _) (subset_univ _), rcases quotient.exists_rep xc with ⟨x, hx⟩, cases x; rw ← hx, { have : ⟦inl x⟧ = optimal_GH_injl α β x := rfl, rw this, exact mem_union_left _ (mem_image_of_mem _ (mem_univ _)) }, { have : ⟦inr x⟧ = optimal_GH_injr α β x := rfl, rw this, exact mem_union_right _ (mem_image_of_mem _ (mem_univ _)) } }, rw this, exact (compact_univ.image (isometry_optimal_GH_injl α β).continuous).union (compact_univ.image (isometry_optimal_GH_injr α β).continuous) end⟩ /-- For any candidate f, HD(f) is larger than or equal to the Hausdorff distance in the optimal coupling. This follows from the fact that HD of the optimal candidate is exactly the Hausdorff distance in the optimal coupling, although we only prove here the inequality we need. -/ lemma Hausdorff_dist_optimal_le_HD {f} (h : f ∈ candidates_b α β) : Hausdorff_dist (range (optimal_GH_injl α β)) (range (optimal_GH_injr α β)) ≤ HD f := begin refine le_trans (le_of_forall_le_of_dense (λr hr, _)) (HD_optimal_GH_dist_le α β f h), have A : ∀ x ∈ range (optimal_GH_injl α β), ∃ y ∈ range (optimal_GH_injr α β), dist x y ≤ r, { assume x hx, rcases mem_range.1 hx with ⟨z, hz⟩, rw ← hz, have I1 : (⨆ x, ⨅ y, optimal_GH_dist α β (inl x, inr y)) < r := lt_of_le_of_lt (le_max_left _ _) hr, have I2 : (⨅ y, optimal_GH_dist α β (inl z, inr y)) ≤ ⨆ x, ⨅ y, optimal_GH_dist α β (inl x, inr y) := le_cSup (by simpa using HD_bound_aux1 _ 0) (mem_range_self _), have I : (⨅ y, optimal_GH_dist α β (inl z, inr y)) < r := lt_of_le_of_lt I2 I1, rcases exists_lt_of_cInf_lt (range_nonempty _) I with ⟨r', r'range, hr'⟩, rcases mem_range.1 r'range with ⟨z', hz'⟩, existsi [optimal_GH_injr α β z', mem_range_self _], have : (optimal_GH_dist α β) (inl z, inr z') ≤ r := begin rw hz', exact le_of_lt hr' end, exact this }, refine Hausdorff_dist_le_of_mem_dist _ A _, { rcases exists_mem_of_nonempty α with ⟨xα, _⟩, have : optimal_GH_injl α β xα ∈ range (optimal_GH_injl α β) := mem_range_self _, rcases A _ this with ⟨y, yrange, hy⟩, exact le_trans dist_nonneg hy }, { assume y hy, rcases mem_range.1 hy with ⟨z, hz⟩, rw ← hz, have I1 : (⨆ y, ⨅ x, optimal_GH_dist α β (inl x, inr y)) < r := lt_of_le_of_lt (le_max_right _ _) hr, have I2 : (⨅ x, optimal_GH_dist α β (inl x, inr z)) ≤ ⨆ y, ⨅ x, optimal_GH_dist α β (inl x, inr y) := le_cSup (by simpa using HD_bound_aux2 _ 0) (mem_range_self _), have I : (⨅ x, optimal_GH_dist α β (inl x, inr z)) < r := lt_of_le_of_lt I2 I1, rcases exists_lt_of_cInf_lt (range_nonempty _) I with ⟨r', r'range, hr'⟩, rcases mem_range.1 r'range with ⟨z', hz'⟩, existsi [optimal_GH_injl α β z', mem_range_self _], have : (optimal_GH_dist α β) (inl z', inr z) ≤ r := begin rw hz', exact le_of_lt hr' end, rw dist_comm, exact this } end end consequences /- We are done with the construction of the optimal coupling -/ end Gromov_Hausdorff_realized end Gromov_Hausdorff
e423175a9c2de43232f32f7bc46415de7c8903d4
a9fe717b93ccfa4b2e64faeb24f96dfefb390240
/scalar.lean
41ebbba31ef1d49c4f31871ac959b307bd6602ea
[]
no_license
skbaek/omega
ab1f4a6daadfc8c855f14c39d9459ab841527141
715e384ed14e8eb177a326700066e7c98269e078
refs/heads/master
1,588,000,876,352
1,552,645,917,000
1,552,645,917,000
174,442,914
1
0
null
null
null
null
UTF-8
Lean
false
false
2,979
lean
import .clause open tactic meta def trisect (m : nat) : list (list nat × term) → (list (list nat × term) × list (list nat × term) × list (list nat × term)) | [] := ([],[],[]) | ((p,t)::pts) := let (neg,zero,pos) := trisect pts in if t.snd.get m < 0 then ((p,t)::neg,zero,pos) else if t.snd.get m = 0 then (neg,(p,t)::zero,pos) else (neg,zero,(p,t)::pos) meta def elim_var_aux (m : nat) : ((list nat × term) × (list nat × term)) → tactic (list nat × term) | ((p1,t1), (p2,t2)) := let n := int.nat_abs (t1.snd.get m) in let o := int.nat_abs (t2.snd.get m) in let lcm := (nat.lcm n o) in let n' := lcm / n in let o' := lcm / o in return (list.add (n' *₁ p1) (o' *₁ p2), term.add (t1.mul n') (t2.mul o')) meta def elim_var (m) (neg pos : list (list nat × term)) : tactic (list (list nat × term)) := let pairs := list.product neg pos in monad.mapm (elim_var_aux m) pairs meta def find_contra : list (list nat × term) → tactic (list nat) | [] := failed | ((π,⟨c,_⟩)::l) := if c < 0 then return π else find_contra l meta def search_core : nat → list (list nat × term) → tactic (list nat) | 0 pts := find_contra pts | (m+1) pts := let (neg,zero,pos) := trisect m pts in do new ← elim_var m neg pos, search_core m (new ++ zero) meta def search (ts : list term) : tactic (list nat) := search_core (ts.map (λ t : term, t.snd.length)).max (ts.map_with_idx (λ m t, ([]{m ↦ 1}, t))) @[omega] def comb : list term → list nat → term | [] [] := ⟨0,[]⟩ | [] (_::_) := ⟨0,[]⟩ | (_::_) [] := ⟨0,[]⟩ | (t::ts) (n::ns) := term.add (t.mul ↑n) (comb ts ns) lemma comb_holds {v} : ∀ {ts} ns, (∀ t ∈ ts, 0 ≤ term.val v t) → (0 ≤ (comb ts ns).val v) | [] [] h := by simp_omega | [] (_::_) h := by simp_omega | (_::_) [] h := by simp_omega | (t::ts) (n::ns) h := begin simp_omega, apply add_nonneg, { apply mul_nonneg, apply int.coe_nat_nonneg, apply h _ (or.inl rfl) }, { apply comb_holds, apply list.forall_mem_of_forall_mem_cons h } end def unsat_comb (ts ns) : Prop := (comb ts ns).fst < 0 ∧ ∀ x ∈ (comb ts ns).snd, x = (0 : int) lemma unsat_comb_of (ts ns) : (comb ts ns).fst < 0 → (∀ x ∈ (comb ts ns).snd, x = (0 : int)) → unsat_comb ts ns := begin intros h1 h2, exact ⟨h1,h2⟩ end lemma unsat_of_unsat_comb (ns les) : (unsat_comb les ns) → clause.unsat ([], les) := begin intros h1 h2, cases h2 with v h2, have h3 := comb_holds ns h2.right, cases h1 with hl hr, cases (comb les ns) with b as, simp_omega at h3, rw [coeffs.val_eq_zero hr, add_zero, ← not_lt] at h3, apply h3 hl end #exit lemma unsat_of_unsat_comb' (ts : polytope) (ns : list nat) : (unsat_comb' ts ns) → ts.unsat := begin intro h1, apply unsat_of_unsat_comb ns, simp only [unsat_comb'] at h1, simp only [unsat_comb], rw if_pos h1, trivial end
8e8fe42dafd615ffd21e2588de7274405343bc68
82e44445c70db0f03e30d7be725775f122d72f3e
/src/algebra/invertible.lean
0d7200cc6b7a39b9b152721d91761921eb61232e
[ "Apache-2.0" ]
permissive
stjordanis/mathlib
51e286d19140e3788ef2c470bc7b953e4991f0c9
2568d41bca08f5d6bf39d915434c8447e21f42ee
refs/heads/master
1,631,748,053,501
1,627,938,886,000
1,627,938,886,000
228,728,358
0
0
Apache-2.0
1,576,630,588,000
1,576,630,587,000
null
UTF-8
Lean
false
false
9,716
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import algebra.group.units import algebra.ring.basic /-! # Invertible elements This file defines a typeclass `invertible a` for elements `a` with a two-sided multiplicative inverse. The intent of the typeclass is to provide a way to write e.g. `⅟2` in a ring like `ℤ[1/2]` where some inverses exist but there is no general `⁻¹` operator; or to specify that a field has characteristic `≠ 2`. It is the `Type`-valued analogue to the `Prop`-valued `is_unit`. For constructions of the invertible element given a characteristic, see `algebra/char_p/invertible` and other lemmas in that file. ## Notation * `⅟a` is `invertible.inv_of a`, the inverse of `a` ## Implementation notes The `invertible` class lives in `Type`, not `Prop`, to make computation easier. If multiplication is associative, `invertible` is a subsingleton anyway. The `simp` normal form tries to normalize `⅟a` to `a ⁻¹`. Otherwise, it pushes `⅟` inside the expression as much as possible. Since `invertible a` is not a `Prop` (but it is a `subsingleton`), we have to be careful about coherence issues: we should avoid having multiple non-defeq instances for `invertible a` in the same context. This file plays it safe and uses `def` rather than `instance` for most definitions, users can choose which instances to use at the point of use. For example, here's how you can use an `invertible 1` instance: ```lean variables {α : Type*} [monoid α] def something_that_needs_inverses (x : α) [invertible x] := sorry section local attribute [instance] invertible_one def something_one := something_that_needs_inverses 1 end ``` ## Tags invertible, inverse element, inv_of, a half, one half, a third, one third, ½, ⅓ -/ universes u variables {α : Type u} /-- `invertible a` gives a two-sided multiplicative inverse of `a`. -/ class invertible [has_mul α] [has_one α] (a : α) : Type u := (inv_of : α) (inv_of_mul_self : inv_of * a = 1) (mul_inv_of_self : a * inv_of = 1) -- This notation has the same precedence as `has_inv.inv`. notation `⅟`:1034 := invertible.inv_of @[simp] lemma inv_of_mul_self [has_mul α] [has_one α] (a : α) [invertible a] : ⅟a * a = 1 := invertible.inv_of_mul_self @[simp] lemma mul_inv_of_self [has_mul α] [has_one α] (a : α) [invertible a] : a * ⅟a = 1 := invertible.mul_inv_of_self @[simp] lemma inv_of_mul_self_assoc [monoid α] (a b : α) [invertible a] : ⅟a * (a * b) = b := by rw [←mul_assoc, inv_of_mul_self, one_mul] @[simp] lemma mul_inv_of_self_assoc [monoid α] (a b : α) [invertible a] : a * (⅟a * b) = b := by rw [←mul_assoc, mul_inv_of_self, one_mul] @[simp] lemma mul_inv_of_mul_self_cancel [monoid α] (a b : α) [invertible b] : a * ⅟b * b = a := by simp [mul_assoc] @[simp] lemma mul_mul_inv_of_self_cancel [monoid α] (a b : α) [invertible b] : a * b * ⅟b = a := by simp [mul_assoc] lemma inv_of_eq_right_inv [monoid α] {a b : α} [invertible a] (hac : a * b = 1) : ⅟a = b := left_inv_eq_right_inv (inv_of_mul_self _) hac lemma inv_of_eq_left_inv [monoid α] {a b : α} [invertible a] (hac : b * a = 1) : ⅟a = b := (left_inv_eq_right_inv hac (mul_inv_of_self _)).symm lemma invertible_unique {α : Type u} [monoid α] (a b : α) (h : a = b) [invertible a] [invertible b] : ⅟a = ⅟b := by { apply inv_of_eq_right_inv, rw [h, mul_inv_of_self], } instance [monoid α] (a : α) : subsingleton (invertible a) := ⟨ λ ⟨b, hba, hab⟩ ⟨c, hca, hac⟩, by { congr, exact left_inv_eq_right_inv hba hac } ⟩ /-- If `r` is invertible and `s = r`, then `s` is invertible. -/ def invertible.copy [monoid α] {r : α} (hr : invertible r) (s : α) (hs : s = r) : invertible s := { inv_of := ⅟r, inv_of_mul_self := by rw [hs, inv_of_mul_self], mul_inv_of_self := by rw [hs, mul_inv_of_self] } /-- An `invertible` element is a unit. -/ @[simps] def unit_of_invertible [monoid α] (a : α) [invertible a] : units α := { val := a, inv := ⅟a, val_inv := by simp, inv_val := by simp, } lemma is_unit_of_invertible [monoid α] (a : α) [invertible a] : is_unit a := ⟨unit_of_invertible a, rfl⟩ /-- Units are invertible in their associated monoid. -/ def units.invertible [monoid α] (u : units α) : invertible (u : α) := { inv_of := ↑(u⁻¹), inv_of_mul_self := u.inv_mul, mul_inv_of_self := u.mul_inv } @[simp] lemma inv_of_units [monoid α] (u : units α) [invertible (u : α)] : ⅟(u : α) = ↑(u⁻¹) := inv_of_eq_right_inv u.mul_inv lemma is_unit.nonempty_invertible [monoid α] {a : α} (h : is_unit a) : nonempty (invertible a) := let ⟨x, hx⟩ := h in ⟨x.invertible.copy _ hx.symm⟩ /-- Convert `is_unit` to `invertible` using `classical.choice`. Prefer `casesI h.nonempty_invertible` over `letI := h.invertible` if you want to avoid choice. -/ noncomputable def is_unit.invertible [monoid α] {a : α} (h : is_unit a) : invertible a := classical.choice h.nonempty_invertible @[simp] lemma nonempty_invertible_iff_is_unit [monoid α] (a : α) : nonempty (invertible a) ↔ is_unit a := ⟨nonempty.rec $ @is_unit_of_invertible _ _ _, is_unit.nonempty_invertible⟩ /-- Each element of a group is invertible. -/ def invertible_of_group [group α] (a : α) : invertible a := ⟨a⁻¹, inv_mul_self a, mul_inv_self a⟩ @[simp] lemma inv_of_eq_group_inv [group α] (a : α) [invertible a] : ⅟a = a⁻¹ := inv_of_eq_right_inv (mul_inv_self a) /-- `1` is the inverse of itself -/ def invertible_one [monoid α] : invertible (1 : α) := ⟨1, mul_one _, one_mul _⟩ @[simp] lemma inv_of_one [monoid α] [invertible (1 : α)] : ⅟(1 : α) = 1 := inv_of_eq_right_inv (mul_one _) /-- `-⅟a` is the inverse of `-a` -/ def invertible_neg [ring α] (a : α) [invertible a] : invertible (-a) := ⟨ -⅟a, by simp, by simp ⟩ @[simp] lemma inv_of_neg [ring α] (a : α) [invertible a] [invertible (-a)] : ⅟(-a) = -⅟a := inv_of_eq_right_inv (by simp) @[simp] lemma one_sub_inv_of_two [ring α] [invertible (2:α)] : 1 - (⅟2:α) = ⅟2 := (is_unit_of_invertible (2:α)).mul_right_inj.1 $ by rw [mul_sub, mul_inv_of_self, mul_one, bit0, add_sub_cancel] /-- `a` is the inverse of `⅟a`. -/ instance invertible_inv_of [has_one α] [has_mul α] {a : α} [invertible a] : invertible (⅟a) := ⟨ a, mul_inv_of_self a, inv_of_mul_self a ⟩ @[simp] lemma inv_of_inv_of [monoid α] {a : α} [invertible a] [invertible (⅟a)] : ⅟(⅟a) = a := inv_of_eq_right_inv (inv_of_mul_self _) /-- `⅟b * ⅟a` is the inverse of `a * b` -/ def invertible_mul [monoid α] (a b : α) [invertible a] [invertible b] : invertible (a * b) := ⟨ ⅟b * ⅟a, by simp [←mul_assoc], by simp [←mul_assoc] ⟩ @[simp] lemma inv_of_mul [monoid α] (a b : α) [invertible a] [invertible b] [invertible (a * b)] : ⅟(a * b) = ⅟b * ⅟a := inv_of_eq_right_inv (by simp [←mul_assoc]) theorem commute.inv_of_right [monoid α] {a b : α} [invertible b] (h : commute a b) : commute a (⅟b) := calc a * (⅟b) = (⅟b) * (b * a * (⅟b)) : by simp [mul_assoc] ... = (⅟b) * (a * b * ((⅟b))) : by rw h.eq ... = (⅟b) * a : by simp [mul_assoc] theorem commute.inv_of_left [monoid α] {a b : α} [invertible b] (h : commute b a) : commute (⅟b) a := calc (⅟b) * a = (⅟b) * (a * b * (⅟b)) : by simp [mul_assoc] ... = (⅟b) * (b * a * (⅟b)) : by rw h.eq ... = a * (⅟b) : by simp [mul_assoc] lemma commute_inv_of {M : Type*} [has_one M] [has_mul M] (m : M) [invertible m] : commute m (⅟m) := calc m * ⅟m = 1 : mul_inv_of_self m ... = ⅟ m * m : (inv_of_mul_self m).symm section group_with_zero variable [group_with_zero α] lemma nonzero_of_invertible (a : α) [invertible a] : a ≠ 0 := λ ha, zero_ne_one $ calc 0 = ⅟a * a : by simp [ha] ... = 1 : inv_of_mul_self a /-- `a⁻¹` is an inverse of `a` if `a ≠ 0` -/ def invertible_of_nonzero {a : α} (h : a ≠ 0) : invertible a := ⟨ a⁻¹, inv_mul_cancel h, mul_inv_cancel h ⟩ @[simp] lemma inv_of_eq_inv (a : α) [invertible a] : ⅟a = a⁻¹ := inv_of_eq_right_inv (mul_inv_cancel (nonzero_of_invertible a)) @[simp] lemma inv_mul_cancel_of_invertible (a : α) [invertible a] : a⁻¹ * a = 1 := inv_mul_cancel (nonzero_of_invertible a) @[simp] lemma mul_inv_cancel_of_invertible (a : α) [invertible a] : a * a⁻¹ = 1 := mul_inv_cancel (nonzero_of_invertible a) @[simp] lemma div_mul_cancel_of_invertible (a b : α) [invertible b] : a / b * b = a := div_mul_cancel a (nonzero_of_invertible b) @[simp] lemma mul_div_cancel_of_invertible (a b : α) [invertible b] : a * b / b = a := mul_div_cancel a (nonzero_of_invertible b) @[simp] lemma div_self_of_invertible (a : α) [invertible a] : a / a = 1 := div_self (nonzero_of_invertible a) /-- `b / a` is the inverse of `a / b` -/ def invertible_div (a b : α) [invertible a] [invertible b] : invertible (a / b) := ⟨b / a, by simp [←mul_div_assoc], by simp [←mul_div_assoc]⟩ @[simp] lemma inv_of_div (a b : α) [invertible a] [invertible b] [invertible (a / b)] : ⅟(a / b) = b / a := inv_of_eq_right_inv (by simp [←mul_div_assoc]) /-- `a` is the inverse of `a⁻¹` -/ def invertible_inv {a : α} [invertible a] : invertible (a⁻¹) := ⟨ a, by simp, by simp ⟩ end group_with_zero /-- Monoid homs preserve invertibility. -/ def invertible.map {R : Type*} {S : Type*} [monoid R] [monoid S] (f : R →* S) (r : R) [invertible r] : invertible (f r) := { inv_of := f (⅟r), inv_of_mul_self := by rw [← f.map_mul, inv_of_mul_self, f.map_one], mul_inv_of_self := by rw [← f.map_mul, mul_inv_of_self, f.map_one] }
b5ba2ec0b1322ac5cff75b645efc8ab1362d6391
618003631150032a5676f229d13a079ac875ff77
/src/category_theory/pempty.lean
27030219aee2fd839cd4dceab70b46d789e7352b
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
1,241
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.discrete_category /-! # The empty category Defines a category structure on `pempty`, and the unique functor `pempty ⥤ C` for any category `C`. -/ universes v u w -- declare the `v`'s first; see `category_theory.category` for an explanation namespace category_theory /-- The empty category -/ instance pempty_category : small_category.{w} pempty.{w+1} := { hom := λ X Y, pempty, id := by obviously, comp := by obviously } namespace functor variables (C : Type u) [category.{v} C] /-- The unique functor from the empty category to any target category. -/ def empty : pempty.{v+1} ⥤ C := by tidy /-- The natural isomorphism between any two functors out of the empty category. -/ @[simps] def empty_ext (F G : pempty.{v+1} ⥤ C) : F ≅ G := { hom := { app := λ j, by cases j }, inv := { app := λ j, by cases j } } end functor /-- The category `pempty` is equivalent to the category `discrete pempty`. -/ instance pempty_equiv_discrete_pempty : is_equivalence (functor.empty.{v} (discrete pempty.{v+1})) := by obviously end category_theory
604e4f6c96b2ba1d59928f08e61aaac51045dcdb
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/order/filter/extr.lean
81a727c4f9126f250f57f003041b2a9ec550b19a
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
18,676
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 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_add_comm_monoid variables [ordered_add_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_add_comm_monoid /-! ### Pointwise negation and subtraction -/ section ordered_add_comm_group variables [ordered_add_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_add_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
72e235e4c12c1b90839814a4de544c87ebc916d2
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/graded_mul_action.lean
041908d6f8e2d784f0b063ea6dfce02e9b0045ab
[ "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
5,206
lean
/- Copyright (c) 2022 Jujian Zhang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jujian Zhang, Eric Wieser -/ import algebra.graded_monoid /-! # Additively-graded multiplicative action structures > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This module provides a set of heterogeneous typeclasses for defining a multiplicative structure over the sigma type `graded_monoid A` such that `(•) : A i → M j → M (i + j)`; that is to say, `A` has an additively-graded multiplicative action on `M`. The typeclasses are: * `graded_monoid.ghas_smul A M` * `graded_monoid.gmul_action A M` With the `sigma_graded` locale open, these respectively imbue: * `has_smul (graded_monoid A) (graded_monoid M)` * `mul_action (graded_monoid A) (graded_monoid M)` For now, these typeclasses are primarily used in the construction of `direct_sum.gmodule.module` and the rest of that file. ## Internally graded multiplicative actions In addition to the above typeclasses, in the most frequent case when `A` is an indexed collection of `set_like` subobjects (such as `add_submonoid`s, `add_subgroup`s, or `submodule`s), this file provides the `Prop` typeclasses: * `set_like.has_graded_smul A M` (which provides the obvious `graded_monoid.ghas_smul A` instance) which provides the API lemma * `set_like.graded_smul_mem_graded` Note that there is no need for `set_like.graded_mul_action` or similar, as all the information it would contain is already supplied by `has_graded_smul` when the objects within `A` and `M` have a `mul_action` instance. ## tags graded action -/ set_option old_structure_cmd true variables {ι : Type*} namespace graded_monoid /-! ### Typeclasses -/ section defs variables (A : ι → Type*) (M : ι → Type*) /-- A graded version of `has_smul`. Scalar multiplication combines grades additively, i.e. if `a ∈ A i` and `m ∈ M j`, then `a • b` must be in `M (i + j)`-/ class ghas_smul [has_add ι] := (smul {i j} : A i → M j → M (i + j)) /-- A graded version of `has_mul.to_has_smul` -/ instance ghas_mul.to_ghas_smul [has_add ι] [ghas_mul A] : ghas_smul A A := { smul := λ _ _, ghas_mul.mul } instance ghas_smul.to_has_smul [has_add ι] [ghas_smul A M] : has_smul (graded_monoid A) (graded_monoid M) := ⟨λ (x : graded_monoid A) (y : graded_monoid M), ⟨_, ghas_smul.smul x.snd y.snd⟩⟩ lemma mk_smul_mk [has_add ι] [ghas_smul A M] {i j} (a : A i) (b : M j) : mk i a • mk j b = mk (i + j) (ghas_smul.smul a b) := rfl /-- A graded version of `mul_action`. -/ class gmul_action [add_monoid ι] [gmonoid A] extends ghas_smul A M := (one_smul (b : graded_monoid M) : (1 : graded_monoid A) • b = b) (mul_smul (a a' : graded_monoid A) (b : graded_monoid M) : (a * a') • b = a • a' • b) /-- The graded version of `monoid.to_mul_action`. -/ instance gmonoid.to_gmul_action [add_monoid ι] [gmonoid A] : gmul_action A A := { one_smul := gmonoid.one_mul, mul_smul := gmonoid.mul_assoc, ..ghas_mul.to_ghas_smul _ } instance gmul_action.to_mul_action [add_monoid ι] [gmonoid A] [gmul_action A M] : mul_action (graded_monoid A) (graded_monoid M) := { one_smul := gmul_action.one_smul, mul_smul := gmul_action.mul_smul } end defs end graded_monoid /-! ### Shorthands for creating instance of the above typeclasses for collections of subobjects -/ section subobjects variables {R : Type*} /-- A version of `graded_monoid.ghas_smul` for internally graded objects. -/ class set_like.has_graded_smul {S R N M : Type*} [set_like S R] [set_like N M] [has_smul R M] [has_add ι] (A : ι → S) (B : ι → N) : Prop := (smul_mem : ∀ ⦃i j : ι⦄ {ai bj}, ai ∈ A i → bj ∈ B j → ai • bj ∈ B (i + j)) instance set_like.ghas_smul {S R N M : Type*} [set_like S R] [set_like N M] [has_smul R M] [has_add ι] (A : ι → S) (B : ι → N) [set_like.has_graded_smul A B] : graded_monoid.ghas_smul (λ i, A i) (λ i, B i) := { smul := λ i j a b, ⟨(a : R) • b, set_like.has_graded_smul.smul_mem a.2 b.2⟩ } @[simp] lemma set_like.coe_ghas_smul {S R N M : Type*} [set_like S R] [set_like N M] [has_smul R M] [has_add ι] (A : ι → S) (B : ι → N) [set_like.has_graded_smul A B] {i j : ι} (x : A i) (y : B j) : (@graded_monoid.ghas_smul.smul ι (λ i, A i) (λ i, B i) _ _ i j x y : M) = ((x : R) • y) := rfl /-- Internally graded version of `has_mul.to_has_smul`. -/ instance set_like.has_graded_mul.to_has_graded_smul [add_monoid ι] [monoid R] {S : Type*} [set_like S R] (A : ι → S) [set_like.graded_monoid A] : set_like.has_graded_smul A A := { smul_mem := λ i j ai bj hi hj, set_like.graded_monoid.mul_mem hi hj, } end subobjects section homogeneous_elements variables {S R N M : Type*} [set_like S R] [set_like N M] lemma set_like.is_homogeneous.graded_smul [has_add ι] [has_smul R M] {A : ι → S} {B : ι → N} [set_like.has_graded_smul A B] {a : R} {b : M} : set_like.is_homogeneous A a → set_like.is_homogeneous B b → set_like.is_homogeneous B (a • b) | ⟨i, hi⟩ ⟨j, hj⟩ := ⟨i + j, set_like.has_graded_smul.smul_mem hi hj⟩ end homogeneous_elements
2b431d4af09246194dd6aedc9eeb3eb2a3ebaaff
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/ring_theory/witt_vector/frobenius_auto.lean
f303fa719c08d20208e46b65c55444c334638914
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
8,786
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.nat.multiplicity import Mathlib.ring_theory.witt_vector.basic import Mathlib.ring_theory.witt_vector.is_poly import Mathlib.PostPort universes u_1 namespace Mathlib /-! ## The Frobenius operator If `R` has characteristic `p`, then there is a ring endomorphism `frobenius R p` that raises `r : R` to the power `p`. By applying `witt_vector.map` to `frobenius R p`, we obtain a ring endomorphism `𝕎 R →+* 𝕎 R`. It turns out that this endomorphism can be described by polynomials over `ℤ` that do not depend on `R` or the fact that it has characteristic `p`. In this way, we obtain a Frobenius endomorphism `witt_vector.frobenius_fun : 𝕎 R → 𝕎 R` for every commutative ring `R`. Unfortunately, the aforementioned polynomials can not be obtained using the machinery of `witt_structure_int` that was developed in `structure_polynomial.lean`. We therefore have to define the polynomials by hand, and check that they have the required property. In case `R` has characteristic `p`, we show in `frobenius_fun_eq_map_frobenius` that `witt_vector.frobenius_fun` is equal to `witt_vector.map (frobenius R p)`. ### Main definitions and results * `frobenius_poly`: the polynomials that describe the coefficients of `frobenius_fun`; * `frobenius_fun`: the Frobenius endomorphism on Witt vectors; * `frobenius_fun_is_poly`: the tautological assertion that Frobenius is a polynomial function; * `frobenius_fun_eq_map_frobenius`: the fact that in characteristic `p`, Frobenius is equal to `witt_vector.map (frobenius R p)`. TODO: Show that `witt_vector.frobenius_fun` is a ring homomorphism, and bundle it into `witt_vector.frobenius`. -/ namespace witt_vector /-- The rational polynomials that give the coefficients of `frobenius x`, in terms of the coefficients of `x`. These polynomials actually have integral coefficients, see `frobenius_poly` and `map_frobenius_poly`. -/ def frobenius_poly_rat (p : ℕ) [hp : fact (nat.prime p)] (n : ℕ) : mv_polynomial ℕ ℚ := coe_fn (mv_polynomial.bind₁ (witt_polynomial p ℚ ∘ fun (n : ℕ) => n + 1)) (X_in_terms_of_W p ℚ n) theorem bind₁_frobenius_poly_rat_witt_polynomial (p : ℕ) [hp : fact (nat.prime p)] (n : ℕ) : coe_fn (mv_polynomial.bind₁ (frobenius_poly_rat p)) (witt_polynomial p ℚ n) = witt_polynomial p ℚ (n + 1) := sorry /-- An auxilliary definition, to avoid an excessive amount of finiteness proofs for `multiplicity p n`. -/ /-- An auxilliary polynomial over the integers, that satisfies `(frobenius_poly_aux p n - X n ^ p) / p = frobenius_poly p n`. This makes it easy to show that `frobenius_poly p n` is congruent to `X n ^ p` modulo `p`. -/ def frobenius_poly_aux (p : ℕ) [hp : fact (nat.prime p)] : ℕ → mv_polynomial ℕ ℤ := sorry theorem frobenius_poly_aux_eq (p : ℕ) [hp : fact (nat.prime p)] (n : ℕ) : frobenius_poly_aux p n = mv_polynomial.X (n + 1) - finset.sum (finset.range n) fun (i : ℕ) => finset.sum (finset.range (p ^ (n - i))) fun (j : ℕ) => (mv_polynomial.X i ^ p) ^ (p ^ (n - i) - (j + 1)) * frobenius_poly_aux p i ^ (j + 1) * coe_fn mv_polynomial.C ↑(nat.choose (p ^ (n - i)) (j + 1) / p ^ (n - i - pnat_multiplicity p { val := j + 1, property := nat.succ_pos j }) * ↑p ^ (j - pnat_multiplicity p { val := j + 1, property := nat.succ_pos j })) := sorry /-- The polynomials that give the coefficients of `frobenius x`, in terms of the coefficients of `x`. -/ def frobenius_poly (p : ℕ) [hp : fact (nat.prime p)] (n : ℕ) : mv_polynomial ℕ ℤ := mv_polynomial.X n ^ p + coe_fn mv_polynomial.C ↑p * frobenius_poly_aux p n /- Our next goal is to prove ``` lemma map_frobenius_poly (n : ℕ) : mv_polynomial.map (int.cast_ring_hom ℚ) (frobenius_poly p n) = frobenius_poly_rat p n ``` This lemma has a rather long proof, but it mostly boils down to applying induction, and then using the following two key facts at the right point. -/ /-- A key divisibility fact for the proof of `witt_vector.map_frobenius_poly`. -/ theorem map_frobenius_poly.key₁ (p : ℕ) [hp : fact (nat.prime p)] (n : ℕ) (j : ℕ) (hj : j < p ^ n) : p ^ (n - pnat_multiplicity p { val := j + 1, property := nat.succ_pos j }) ∣ nat.choose (p ^ n) (j + 1) := sorry /-- A key numerical identity needed for the proof of `witt_vector.map_frobenius_poly`. -/ theorem map_frobenius_poly.key₂ (p : ℕ) [hp : fact (nat.prime p)] {n : ℕ} {i : ℕ} {j : ℕ} (hi : i < n) (hj : j < p ^ (n - i)) : j - pnat_multiplicity p { val := j + 1, property := nat.succ_pos j } + n = i + j + (n - i - pnat_multiplicity p { val := j + 1, property := nat.succ_pos j }) := sorry theorem map_frobenius_poly (p : ℕ) [hp : fact (nat.prime p)] (n : ℕ) : coe_fn (mv_polynomial.map (int.cast_ring_hom ℚ)) (frobenius_poly p n) = frobenius_poly_rat p n := sorry theorem frobenius_poly_zmod (p : ℕ) [hp : fact (nat.prime p)] (n : ℕ) : coe_fn (mv_polynomial.map (int.cast_ring_hom (zmod p))) (frobenius_poly p n) = mv_polynomial.X n ^ p := sorry @[simp] theorem bind₁_frobenius_poly_witt_polynomial (p : ℕ) [hp : fact (nat.prime p)] (n : ℕ) : coe_fn (mv_polynomial.bind₁ (frobenius_poly p)) (witt_polynomial p ℤ n) = witt_polynomial p ℤ (n + 1) := sorry /-- `frobenius_fun` is the function underlying the ring endomorphism `frobenius : 𝕎 R →+* frobenius 𝕎 R`. -/ def frobenius_fun {p : ℕ} {R : Type u_1} [hp : fact (nat.prime p)] [comm_ring R] (x : witt_vector p R) : witt_vector p R := mk p fun (n : ℕ) => coe_fn (mv_polynomial.aeval (coeff x)) (frobenius_poly p n) theorem coeff_frobenius_fun {p : ℕ} {R : Type u_1} [hp : fact (nat.prime p)] [comm_ring R] (x : witt_vector p R) (n : ℕ) : coeff (frobenius_fun x) n = coe_fn (mv_polynomial.aeval (coeff x)) (frobenius_poly p n) := sorry /-- `frobenius_fun` is tautologically a polynomial function. See also `frobenius_is_poly`. -/ theorem frobenius_fun_is_poly (p : ℕ) [hp : fact (nat.prime p)] : is_poly p fun (R : Type u_1) (_Rcr : comm_ring R) => frobenius_fun := Exists.intro (frobenius_poly p) fun (R : Type u_1) (_inst_4 : comm_ring R) (x : witt_vector p R) => funext fun (n : ℕ) => coeff_frobenius_fun x n theorem ghost_component_frobenius_fun {p : ℕ} {R : Type u_1} [hp : fact (nat.prime p)] [comm_ring R] (n : ℕ) (x : witt_vector p R) : coe_fn (ghost_component n) (frobenius_fun x) = coe_fn (ghost_component (n + 1)) x := sorry /-- If `R` has characteristic `p`, then there is a ring endomorphism that raises `r : R` to the power `p`. By applying `witt_vector.map` to this endomorphism, we obtain a ring endomorphism `frobenius R p : 𝕎 R →+* 𝕎 R`. The underlying function of this morphism is `witt_vector.frobenius_fun`. -/ def frobenius {p : ℕ} {R : Type u_1} [hp : fact (nat.prime p)] [comm_ring R] : witt_vector p R →+* witt_vector p R := ring_hom.mk frobenius_fun sorry sorry sorry sorry theorem coeff_frobenius {p : ℕ} {R : Type u_1} [hp : fact (nat.prime p)] [comm_ring R] (x : witt_vector p R) (n : ℕ) : coeff (coe_fn frobenius x) n = coe_fn (mv_polynomial.aeval (coeff x)) (frobenius_poly p n) := coeff_frobenius_fun x n theorem ghost_component_frobenius {p : ℕ} {R : Type u_1} [hp : fact (nat.prime p)] [comm_ring R] (n : ℕ) (x : witt_vector p R) : coe_fn (ghost_component n) (coe_fn frobenius x) = coe_fn (ghost_component (n + 1)) x := ghost_component_frobenius_fun n x /-- `frobenius` is tautologically a polynomial function. -/ theorem frobenius_is_poly (p : ℕ) [hp : fact (nat.prime p)] : is_poly p fun (R : Type u_1) (_Rcr : comm_ring R) => ⇑frobenius := frobenius_fun_is_poly p @[simp] theorem coeff_frobenius_char_p (p : ℕ) {R : Type u_1} [hp : fact (nat.prime p)] [comm_ring R] [char_p R p] (x : witt_vector p R) (n : ℕ) : coeff (coe_fn frobenius x) n = coeff x n ^ p := sorry theorem frobenius_eq_map_frobenius (p : ℕ) {R : Type u_1} [hp : fact (nat.prime p)] [comm_ring R] [char_p R p] : frobenius = map (frobenius R p) := sorry @[simp] theorem frobenius_zmodp (p : ℕ) [hp : fact (nat.prime p)] (x : witt_vector p (zmod p)) : coe_fn frobenius x = x := sorry end Mathlib
75298f46da02050dd1a336e73e9a2a9b90a3fc84
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/stage0/src/Init/Lean/Meta/Tactic/Generalize.lean
c86a6811ec88a2690b6409d3b2d8a76921bf3c1a
[ "Apache-2.0" ]
permissive
mhuisi/lean4
28d35a4febc2e251c7f05492e13f3b05d6f9b7af
dda44bc47f3e5d024508060dac2bcb59fd12e4c0
refs/heads/master
1,621,225,489,283
1,585,142,689,000
1,585,142,689,000
250,590,438
0
2
Apache-2.0
1,602,443,220,000
1,585,327,814,000
C
UTF-8
Lean
false
false
1,017
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 -/ prelude import Init.Lean.Meta.KAbstract import Init.Lean.Meta.Tactic.Util namespace Lean namespace Meta def generalize (mvarId : MVarId) (e : Expr) (x : Name) : MetaM MVarId := do withMVarContext mvarId $ do checkNotAssigned mvarId `generalize; tag ← getMVarTag mvarId; target ← getMVarType mvarId; target ← instantiateMVars target; targetAbst ← kabstract target e; unless targetAbst.hasLooseBVars $ throwTacticEx `generalize mvarId ("failed to find expression in the target"); eType ← inferType e; let targetNew := Lean.mkForall x BinderInfo.default eType targetAbst; unlessM (isTypeCorrect targetNew) $ throwTacticEx `generalize mvarId ("result is not type correct"); mvarNew ← mkFreshExprSyntheticOpaqueMVar targetNew tag; assignExprMVar mvarId (mkApp mvarNew e); pure mvarNew.mvarId! end Meta end Lean
eb1b8cdaf416d5eceab419a473cee580c13f0e13
853df553b1d6ca524e3f0a79aedd32dde5d27ec3
/src/order/filter/pointwise.lean
866d7bea2ef127422e08c3e4eded38f6d7eafed1
[ "Apache-2.0" ]
permissive
DanielFabian/mathlib
efc3a50b5dde303c59eeb6353ef4c35a345d7112
f520d07eba0c852e96fe26da71d85bf6d40fcc2a
refs/heads/master
1,668,739,922,971
1,595,201,756,000
1,595,201,756,000
279,469,476
0
0
null
1,594,696,604,000
1,594,696,604,000
null
UTF-8
Lean
false
false
6,640
lean
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou -/ import algebra.pointwise import order.filter.basic /-! # Pointwise operations on filters. The pointwise operations on filters have nice properties, such as • `map m (f₁ * f₂) = map m f₁ * map m f₂` • `𝓝 x * 𝓝 y = 𝓝 (x * y)` -/ open classical set universes u v w variables {α : Type u} {β : Type v} {γ : Type w} open_locale classical namespace filter open set @[to_additive] instance [has_one α] : has_one (filter α) := ⟨principal 1⟩ @[simp, to_additive] lemma mem_one [has_one α] (s : set α) : s ∈ (1 : filter α) ↔ (1:α) ∈ s := calc s ∈ (1:filter α) ↔ 1 ⊆ s : iff.rfl ... ↔ (1 : α) ∈ s : by simp @[to_additive] instance [monoid α] : has_mul (filter α) := ⟨λf g, { sets := { s | ∃t₁ t₂, t₁ ∈ f ∧ t₂ ∈ g ∧ t₁ * t₂ ⊆ s }, univ_sets := begin have h₁ : (∃x, x ∈ f) := ⟨univ, univ_sets f⟩, have h₂ : (∃x, x ∈ g) := ⟨univ, univ_sets g⟩, simpa using and.intro h₁ h₂ end, sets_of_superset := λx y hx hxy, begin rcases hx with ⟨t₁, ht₁, t₂, ht₂, t₁t₂⟩, exact ⟨t₁, ht₁, t₂, ht₂, subset.trans t₁t₂ hxy⟩ end, inter_sets := λx y, begin simp only [exists_prop, mem_set_of_eq, subset_inter_iff], rintros ⟨s₁, s₂, hs₁, hs₂, s₁s₂⟩ ⟨t₁, t₂, ht₁, ht₂, t₁t₂⟩, exact ⟨s₁ ∩ t₁, s₂ ∩ t₂, inter_sets f hs₁ ht₁, inter_sets g hs₂ ht₂, subset.trans (mul_subset_mul (inter_subset_left _ _) (inter_subset_left _ _)) s₁s₂, subset.trans (mul_subset_mul (inter_subset_right _ _) (inter_subset_right _ _)) t₁t₂⟩, end }⟩ @[to_additive] lemma mem_mul [monoid α] {f g : filter α} {s : set α} : s ∈ f * g ↔ ∃t₁ t₂, t₁ ∈ f ∧ t₂ ∈ g ∧ t₁ * t₂ ⊆ s := iff.rfl @[to_additive] lemma mul_mem_mul [monoid α] {f g : filter α} {s t : set α} (hs : s ∈ f) (ht : t ∈ g) : s * t ∈ f * g := ⟨_, _, hs, ht, subset.refl _⟩ @[to_additive] protected lemma mul_le_mul [monoid α] {f₁ f₂ g₁ g₂ : filter α} (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁ * g₁ ≤ f₂ * g₂ := assume _ ⟨s, t, hs, ht, hst⟩, ⟨s, t, hf hs, hg ht, hst⟩ @[to_additive] lemma mul_ne_bot [monoid α] {f g : filter α} : f ≠ ⊥ → g ≠ ⊥ → f * g ≠ ⊥ := begin simp only [forall_sets_nonempty_iff_ne_bot.symm], rintros hf hg s ⟨a, b, ha, hb, ab⟩, exact ((hf a ha).mul (hg b hb)).mono ab end @[to_additive] protected lemma mul_assoc [monoid α] (f g h : filter α) : f * g * h = f * (g * h) := begin ext s, split, { rintros ⟨a, b, ⟨a₁, a₂, ha₁, ha₂, a₁a₂⟩, hb, ab⟩, refine ⟨a₁, a₂ * b, ha₁, mul_mem_mul ha₂ hb, _⟩, rw [← mul_assoc], exact calc a₁ * a₂ * b ⊆ a * b : mul_subset_mul a₁a₂ (subset.refl _) ... ⊆ s : ab }, { rintros ⟨a, b, ha, ⟨b₁, b₂, hb₁, hb₂, b₁b₂⟩, ab⟩, refine ⟨a * b₁, b₂, mul_mem_mul ha hb₁, hb₂, _⟩, rw [mul_assoc], exact calc a * (b₁ * b₂) ⊆ a * b : mul_subset_mul (subset.refl _) b₁b₂ ... ⊆ s : ab } end @[to_additive] protected lemma one_mul [monoid α] (f : filter α) : 1 * f = f := begin ext s, split, { rintros ⟨t₁, t₂, ht₁, ht₂, t₁t₂⟩, refine mem_sets_of_superset (mem_sets_of_superset ht₂ _) t₁t₂, assume x hx, exact ⟨1, x, by rwa [← mem_one], hx, one_mul _⟩ }, { assume hs, refine ⟨(1:set α), s, mem_principal_self _, hs, by simp only [one_mul]⟩ } end @[to_additive] protected lemma mul_one [monoid α] (f : filter α) : f * 1 = f := begin ext s, split, { rintros ⟨t₁, t₂, ht₁, ht₂, t₁t₂⟩, refine mem_sets_of_superset (mem_sets_of_superset ht₁ _) t₁t₂, assume x hx, exact ⟨x, 1, hx, by rwa [← mem_one], mul_one _⟩ }, { assume hs, refine ⟨s, (1:set α), hs, mem_principal_self _, by simp only [mul_one]⟩ } end @[to_additive filter.add_monoid] instance [monoid α] : monoid (filter α) := { mul_assoc := filter.mul_assoc, one_mul := filter.one_mul, mul_one := filter.mul_one, .. filter.has_mul, .. filter.has_one } section map open is_mul_hom variables [monoid α] [monoid β] {f : filter α} (m : α → β) @[to_additive] protected lemma map_mul [is_mul_hom m] {f₁ f₂ : filter α} : map m (f₁ * f₂) = map m f₁ * map m f₂ := filter_eq $ set.ext $ assume s, begin simp only [mem_mul], split, { rintro ⟨t₁, t₂, ht₁, ht₂, t₁t₂⟩, have : m '' (t₁ * t₂) ⊆ s := subset.trans (image_subset m t₁t₂) (image_preimage_subset _ _), refine ⟨m '' t₁, m '' t₂, image_mem_map ht₁, image_mem_map ht₂, _⟩, rwa ← image_mul m }, { rintro ⟨t₁, t₂, ht₁, ht₂, t₁t₂⟩, refine ⟨m ⁻¹' t₁, m ⁻¹' t₂, ht₁, ht₂, image_subset_iff.1 _⟩, rw image_mul m, exact subset.trans (mul_subset_mul (image_preimage_subset _ _) (image_preimage_subset _ _)) t₁t₂ }, end @[to_additive] protected lemma map_one [is_monoid_hom m] : map m (1:filter α) = 1 := le_antisymm (le_principal_iff.2 $ mem_map_sets_iff.2 ⟨(1:set α), by simp, by { assume x, simp [is_monoid_hom.map_one m] }⟩) (le_map $ assume s hs, begin simp only [mem_one], exact ⟨(1:α), (mem_one s).1 hs, is_monoid_hom.map_one _⟩ end) -- TODO: prove similar statements when `m` is group homomorphism etc. @[to_additive map.is_add_monoid_hom] lemma map.is_monoid_hom [is_monoid_hom m] : is_monoid_hom (map m) := { map_one := filter.map_one m, map_mul := λ _ _, filter.map_mul m } -- The other direction does not hold in general. @[to_additive] lemma comap_mul_comap_le [is_mul_hom m] {f₁ f₂ : filter β} : comap m f₁ * comap m f₂ ≤ comap m (f₁ * f₂) := begin rintros s ⟨t, ⟨t₁, t₂, ht₁, ht₂, t₁t₂⟩, mt⟩, refine ⟨m ⁻¹' t₁, m ⁻¹' t₂, ⟨t₁, ht₁, subset.refl _⟩, ⟨t₂, ht₂, subset.refl _⟩, _⟩, have := subset.trans (preimage_mono t₁t₂) mt, exact subset.trans (preimage_mul_preimage_subset m) this end variables {m} @[to_additive] lemma tendsto.mul_mul [is_mul_hom m] {f₁ g₁ : filter α} {f₂ g₂ : filter β} : tendsto m f₁ f₂ → tendsto m g₁ g₂ → tendsto m (f₁ * g₁) (f₂ * g₂) := assume hf hg, by { rw [tendsto, filter.map_mul m], exact filter.mul_le_mul hf hg } end map end filter
ccfd499e9b8e1a9a616ecfe99f745f3fba4411b2
957a80ea22c5abb4f4670b250d55534d9db99108
/tests/lean/run/1577.lean
5f1738b4f30cd6e11e2fa8f78931369434c6b624
[ "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
175
lean
section parameter T : Type def eqT : T -> T -> Prop | t1 t2 := t1 = t2 lemma sm : forall t1 t2, eqT t1 t2 -> t1 = t2 := begin intros, simp [eqT] at a, assumption end end
2b793c9a6e33170f66b1e1110ade86ab3894b824
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/run/sub_bug.lean
d2419cd753eef87f803e260a13eaa71923770424
[ "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
42
lean
open nat subtype check { x : nat \ x > 0}
ccb6f09e2ab8991489701bff239764c5e0093004
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/algebra/archimedean.lean
59c4fca5af463a1f31c56932239adbec9be64efb
[ "Apache-2.0" ]
permissive
fpvandoorn/mathlib
b21ab4068db079cbb8590b58fda9cc4bc1f35df4
b3433a51ea8bc07c4159c1073838fc0ee9b8f227
refs/heads/master
1,624,791,089,608
1,556,715,231,000
1,556,715,231,000
165,722,980
5
0
Apache-2.0
1,552,657,455,000
1,547,494,646,000
Lean
UTF-8
Lean
false
false
15,755
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro Archimedean groups and fields. -/ import algebra.group_power algebra.field_power import data.rat tactic.linarith tactic.abel local infix ` • ` := add_monoid.smul variables {α : Type*} class floor_ring (α) [linear_ordered_ring α] := (floor : α → ℤ) (le_floor : ∀ (z : ℤ) (x : α), z ≤ floor x ↔ (z : α) ≤ x) instance : floor_ring ℤ := { floor := id, le_floor := λ _ _, by rw int.cast_id; refl } instance : floor_ring ℚ := { floor := rat.floor, le_floor := @rat.le_floor } section variables [linear_ordered_ring α] [floor_ring α] def floor : α → ℤ := floor_ring.floor notation `⌊` x `⌋` := floor x theorem le_floor : ∀ {z : ℤ} {x : α}, z ≤ ⌊x⌋ ↔ (z : α) ≤ x := floor_ring.le_floor theorem floor_lt {x : α} {z : ℤ} : ⌊x⌋ < z ↔ x < z := lt_iff_lt_of_le_iff_le le_floor theorem floor_le (x : α) : (⌊x⌋ : α) ≤ x := le_floor.1 (le_refl _) theorem floor_nonneg {x : α} : 0 ≤ ⌊x⌋ ↔ 0 ≤ x := by rw [le_floor]; refl theorem lt_succ_floor (x : α) : x < ⌊x⌋.succ := floor_lt.1 $ int.lt_succ_self _ theorem lt_floor_add_one (x : α) : x < ⌊x⌋ + 1 := by simpa only [int.succ, int.cast_add, int.cast_one] using lt_succ_floor x theorem sub_one_lt_floor (x : α) : x - 1 < ⌊x⌋ := sub_lt_iff_lt_add.2 (lt_floor_add_one x) @[simp] theorem floor_coe (z : ℤ) : ⌊(z:α)⌋ = z := eq_of_forall_le_iff $ λ a, by rw [le_floor, int.cast_le] @[simp] theorem floor_zero : ⌊(0:α)⌋ = 0 := floor_coe 0 @[simp] theorem floor_one : ⌊(1:α)⌋ = 1 := by rw [← int.cast_one, floor_coe] theorem floor_mono {a b : α} (h : a ≤ b) : ⌊a⌋ ≤ ⌊b⌋ := le_floor.2 (le_trans (floor_le _) h) @[simp] theorem floor_add_int (x : α) (z : ℤ) : ⌊x + z⌋ = ⌊x⌋ + z := eq_of_forall_le_iff $ λ a, by rw [le_floor, ← sub_le_iff_le_add, ← sub_le_iff_le_add, le_floor, int.cast_sub] theorem floor_sub_int (x : α) (z : ℤ) : ⌊x - z⌋ = ⌊x⌋ - z := eq.trans (by rw [int.cast_neg]; refl) (floor_add_int _ _) lemma abs_sub_lt_one_of_floor_eq_floor {α : Type*} [decidable_linear_ordered_comm_ring α] [floor_ring α] {x y : α} (h : ⌊x⌋ = ⌊y⌋) : abs (x - y) < 1 := begin have : x < ⌊x⌋ + 1 := lt_floor_add_one x, have : y < ⌊y⌋ + 1 := lt_floor_add_one y, have : (⌊x⌋ : α) = ⌊y⌋ := int.cast_inj.2 h, have : (⌊x⌋: α) ≤ x := floor_le x, have : (⌊y⌋ : α) ≤ y := floor_le y, exact abs_sub_lt_iff.2 ⟨by linarith, by linarith⟩ end lemma floor_eq_iff {r : α} {z : ℤ} : ⌊r⌋ = z ↔ ↑z ≤ r ∧ r < (z + 1) := by rw [←le_floor, ←int.cast_one, ←int.cast_add, ←floor_lt, int.lt_add_one_iff, le_antisymm_iff, and.comm] /-- The fractional part fract r of r is just r - ⌊r⌋ -/ def fract (r : α) : α := r - ⌊r⌋ -- Mathematical notation is usually {r}. Let's not even go there. @[simp] lemma floor_add_fract (r : α) : (⌊r⌋ : α) + fract r = r := by unfold fract; simp @[simp] lemma fract_add_floor (r : α) : fract r + ⌊r⌋ = r := sub_add_cancel _ _ theorem fract_nonneg (r : α) : 0 ≤ fract r := sub_nonneg.2 $ floor_le _ theorem fract_lt_one (r : α) : fract r < 1 := sub_lt.1 $ sub_one_lt_floor _ @[simp] lemma fract_zero : fract (0 : α) = 0 := by unfold fract; simp @[simp] lemma fract_coe (z : ℤ) : fract (z : α) = 0 := by unfold fract; rw floor_coe; exact sub_self _ @[simp] lemma fract_floor (r : α) : fract (⌊r⌋ : α) = 0 := fract_coe _ @[simp] lemma floor_fract (r : α) : ⌊fract r⌋ = 0 := by rw floor_eq_iff; exact ⟨fract_nonneg _, by rw [int.cast_zero, zero_add]; exact fract_lt_one r⟩ theorem fract_eq_iff {r s : α} : fract r = s ↔ 0 ≤ s ∧ s < 1 ∧ ∃ z : ℤ, r - s = z := ⟨λ h, by rw ←h; exact ⟨fract_nonneg _, fract_lt_one _, ⟨⌊r⌋, sub_sub_cancel _ _⟩⟩, begin intro h, show r - ⌊r⌋ = s, apply eq.symm, rw [eq_sub_iff_add_eq, add_comm, ←eq_sub_iff_add_eq], rcases h with ⟨hge, hlt, ⟨z, hz⟩⟩, rw [hz, int.cast_inj, floor_eq_iff, ←hz], clear hz, split; linarith {discharger := `[simp]} end⟩ theorem fract_eq_fract {r s : α} : fract r = fract s ↔ ∃ z : ℤ, r - s = z := ⟨λ h, ⟨⌊r⌋ - ⌊s⌋, begin unfold fract at h, rw [int.cast_sub, sub_eq_sub_iff_sub_eq_sub.1 h], end⟩, λ h, begin rcases h with ⟨z, hz⟩, rw fract_eq_iff, split, exact fract_nonneg _, split, exact fract_lt_one _, use z + ⌊s⌋, rw [eq_add_of_sub_eq hz, int.cast_add], unfold fract, simp end⟩ @[simp] lemma fract_fract (r : α) : fract (fract r) = fract r := by rw fract_eq_fract; exact ⟨-⌊r⌋, by unfold fract;simp⟩ theorem fract_add (r s : α) : ∃ z : ℤ, fract (r + s) - fract r - fract s = z := ⟨⌊r⌋ + ⌊s⌋ - ⌊r + s⌋, by unfold fract; simp⟩ theorem fract_mul_nat (r : α) (b : ℕ) : ∃ z : ℤ, fract r * b - fract (r * b) = z := begin induction b with c hc, use 0, simp, rcases hc with ⟨z, hz⟩, rw [nat.succ_eq_add_one, nat.cast_add, mul_add, mul_add, nat.cast_one, mul_one, mul_one], rcases fract_add (r * c) r with ⟨y, hy⟩, use z - y, rw [int.cast_sub, ←hz, ←hy], abel end /-- `ceil x` is the smallest integer `z` such that `x ≤ z` -/ def ceil (x : α) : ℤ := -⌊-x⌋ notation `⌈` x `⌉` := ceil x theorem ceil_le {z : ℤ} {x : α} : ⌈x⌉ ≤ z ↔ x ≤ z := by rw [ceil, neg_le, le_floor, int.cast_neg, neg_le_neg_iff] theorem lt_ceil {x : α} {z : ℤ} : z < ⌈x⌉ ↔ (z:α) < x := lt_iff_lt_of_le_iff_le ceil_le theorem le_ceil (x : α) : x ≤ ⌈x⌉ := ceil_le.1 (le_refl _) @[simp] theorem ceil_coe (z : ℤ) : ⌈(z:α)⌉ = z := by rw [ceil, ← int.cast_neg, floor_coe, neg_neg] theorem ceil_mono {a b : α} (h : a ≤ b) : ⌈a⌉ ≤ ⌈b⌉ := ceil_le.2 (le_trans h (le_ceil _)) @[simp] theorem ceil_add_int (x : α) (z : ℤ) : ⌈x + z⌉ = ⌈x⌉ + z := by rw [ceil, neg_add', floor_sub_int, neg_sub, sub_eq_neg_add]; refl theorem ceil_sub_int (x : α) (z : ℤ) : ⌈x - z⌉ = ⌈x⌉ - z := eq.trans (by rw [int.cast_neg]; refl) (ceil_add_int _ _) theorem ceil_lt_add_one (x : α) : (⌈x⌉ : α) < x + 1 := by rw [← lt_ceil, ← int.cast_one, ceil_add_int]; apply lt_add_one lemma ceil_pos {a : α} : 0 < ⌈a⌉ ↔ 0 < a := ⟨ λ h, have ⌊-a⌋ < 0, from neg_of_neg_pos h, pos_of_neg_neg $ lt_of_not_ge $ (not_iff_not_of_iff floor_nonneg).1 $ not_le_of_gt this, λ h, have -a < 0, from neg_neg_of_pos h, neg_pos_of_neg $ lt_of_not_ge $ (not_iff_not_of_iff floor_nonneg).2 $ not_le_of_gt this ⟩ @[simp] theorem ceil_zero : ⌈(0 : α)⌉ = 0 := by simp [ceil] lemma ceil_nonneg [decidable_rel ((<) : α → α → Prop)] {q : α} (hq : q ≥ 0) : ⌈q⌉ ≥ 0 := if h : q > 0 then le_of_lt $ ceil_pos.2 h else by rw [le_antisymm (le_of_not_lt h) hq, ceil_zero]; trivial end class archimedean (α) [ordered_comm_monoid α] : Prop := (arch : ∀ (x : α) {y}, 0 < y → ∃ n : ℕ, x ≤ n • y) theorem exists_nat_gt [linear_ordered_semiring α] [archimedean α] (x : α) : ∃ n : ℕ, x < n := let ⟨n, h⟩ := archimedean.arch x zero_lt_one in ⟨n+1, lt_of_le_of_lt (by rwa ← add_monoid.smul_one) (nat.cast_lt.2 (nat.lt_succ_self _))⟩ section linear_ordered_ring variables [linear_ordered_ring α] [archimedean α] lemma pow_unbounded_of_gt_one (x : α) {y : α} (hy1 : 1 < y) : ∃ n : ℕ, x < y ^ n := have hy0 : 0 < y - 1 := sub_pos_of_lt hy1, let ⟨n, h⟩ := archimedean.arch x hy0 in ⟨n, calc x ≤ n • (y - 1) : h ... < 1 + n • (y - 1) : by rw add_comm; exact lt_add_one _ ... ≤ y ^ n : pow_ge_one_add_sub_mul (le_of_lt hy1) _⟩ lemma exists_nat_pow_near {x : α} {y : α} (hx : 1 < x) (hy : 1 < y) : ∃ n : ℕ, y ^ n ≤ x ∧ x < y ^ (n + 1) := have h : ∃ n : ℕ, x < y ^ n, from pow_unbounded_of_gt_one _ hy, by classical; exact let n := nat.find h in have hn : x < y ^ n, from nat.find_spec h, have hnp : 0 < n, from nat.pos_iff_ne_zero.2 (λ hn0, by rw [hn0, pow_zero] at hn; exact (not_lt_of_gt hn hx)), have hnsp : nat.pred n + 1 = n, from nat.succ_pred_eq_of_pos hnp, have hltn : nat.pred n < n, from nat.pred_lt (ne_of_gt hnp), ⟨nat.pred n, le_of_not_lt (nat.find_min h hltn), by rwa hnsp⟩ theorem exists_int_gt (x : α) : ∃ n : ℤ, x < n := let ⟨n, h⟩ := exists_nat_gt x in ⟨n, by rwa ← coe_coe⟩ theorem exists_int_lt (x : α) : ∃ n : ℤ, (n : α) < x := let ⟨n, h⟩ := exists_int_gt (-x) in ⟨-n, by rw int.cast_neg; exact neg_lt.1 h⟩ theorem exists_floor (x : α) : ∃ (fl : ℤ), ∀ (z : ℤ), z ≤ fl ↔ (z : α) ≤ x := begin haveI := classical.prop_decidable, have : ∃ (ub : ℤ), (ub:α) ≤ x ∧ ∀ (z : ℤ), (z:α) ≤ x → z ≤ ub := int.exists_greatest_of_bdd (let ⟨n, hn⟩ := exists_int_gt x in ⟨n, λ z h', int.cast_le.1 $ le_trans h' $ le_of_lt hn⟩) (let ⟨n, hn⟩ := exists_int_lt x in ⟨n, le_of_lt hn⟩), refine this.imp (λ fl h z, _), cases h with h₁ h₂, exact ⟨λ h, le_trans (int.cast_le.2 h) h₁, h₂ z⟩, end end linear_ordered_ring section linear_ordered_field lemma exists_int_pow_near [discrete_linear_ordered_field α] [archimedean α] {x : α} {y : α} (hx : 0 < x) (hy : 1 < y) : ∃ n : ℤ, y ^ n ≤ x ∧ x < y ^ (n + 1) := by classical; exact let ⟨N, hN⟩ := pow_unbounded_of_gt_one x⁻¹ hy in have he: ∃ m : ℤ, y ^ m ≤ x, from ⟨-N, le_of_lt (by rw [(fpow_neg y (↑N)), one_div_eq_inv]; exact (inv_lt hx (lt_trans (inv_pos hx) hN)).1 hN)⟩, let ⟨M, hM⟩ := pow_unbounded_of_gt_one x hy in have hb: ∃ b : ℤ, ∀ m, y ^ m ≤ x → m ≤ b, from ⟨M, λ m hm, le_of_not_lt (λ hlt, not_lt_of_ge (fpow_le_of_le (le_of_lt hy) (le_of_lt hlt)) (lt_of_le_of_lt hm hM))⟩, let ⟨n, hn₁, hn₂⟩ := int.exists_greatest_of_bdd hb he in ⟨n, hn₁, lt_of_not_ge (λ hge, not_le_of_gt (int.lt_succ _) (hn₂ _ hge))⟩ variables [linear_ordered_field α] [floor_ring α] lemma sub_floor_div_mul_nonneg (x : α) {y : α} (hy : 0 < y) : 0 ≤ x - ⌊x / y⌋ * y := begin conv in x {rw ← div_mul_cancel x (ne_of_lt hy).symm}, rw ← sub_mul, exact mul_nonneg (sub_nonneg.2 (floor_le _)) (le_of_lt hy) end lemma sub_floor_div_mul_lt (x : α) {y : α} (hy : 0 < y) : x - ⌊x / y⌋ * y < y := sub_lt_iff_lt_add.2 begin conv in y {rw ← one_mul y}, conv in x {rw ← div_mul_cancel x (ne_of_lt hy).symm}, rw ← add_mul, exact (mul_lt_mul_right hy).2 (by rw add_comm; exact lt_floor_add_one _), end end linear_ordered_field instance : archimedean ℕ := ⟨λ n m m0, ⟨n, by simpa only [mul_one, nat.smul_eq_mul] using nat.mul_le_mul_left n m0⟩⟩ instance : archimedean ℤ := ⟨λ n m m0, ⟨n.to_nat, le_trans (int.le_to_nat _) $ by simpa only [add_monoid.smul_eq_mul, int.nat_cast_eq_coe_nat, zero_add, mul_one] using mul_le_mul_of_nonneg_left (int.add_one_le_iff.2 m0) (int.coe_zero_le n.to_nat)⟩⟩ noncomputable def archimedean.floor_ring (α) [linear_ordered_ring α] [archimedean α] : floor_ring α := { floor := λ x, classical.some (exists_floor x), le_floor := λ z x, classical.some_spec (exists_floor x) z } section linear_ordered_field variables [linear_ordered_field α] theorem archimedean_iff_nat_lt : archimedean α ↔ ∀ x : α, ∃ n : ℕ, x < n := ⟨@exists_nat_gt α _, λ H, ⟨λ x y y0, (H (x / y)).imp $ λ n h, le_of_lt $ by rwa [div_lt_iff y0, ← add_monoid.smul_eq_mul] at h⟩⟩ theorem archimedean_iff_nat_le : archimedean α ↔ ∀ x : α, ∃ n : ℕ, x ≤ n := archimedean_iff_nat_lt.trans ⟨λ H x, (H x).imp $ λ _, le_of_lt, λ H x, let ⟨n, h⟩ := H x in ⟨n+1, lt_of_le_of_lt h (nat.cast_lt.2 (lt_add_one _))⟩⟩ theorem exists_rat_gt [archimedean α] (x : α) : ∃ q : ℚ, x < q := let ⟨n, h⟩ := exists_nat_gt x in ⟨n, by rwa rat.cast_coe_nat⟩ theorem archimedean_iff_rat_lt : archimedean α ↔ ∀ x : α, ∃ q : ℚ, x < q := ⟨@exists_rat_gt α _, λ H, archimedean_iff_nat_lt.2 $ λ x, let ⟨q, h⟩ := H x in ⟨rat.nat_ceil q, lt_of_lt_of_le h $ by simpa only [rat.cast_coe_nat] using (@rat.cast_le α _ _ _).2 (rat.le_nat_ceil _)⟩⟩ theorem archimedean_iff_rat_le : archimedean α ↔ ∀ x : α, ∃ q : ℚ, x ≤ q := archimedean_iff_rat_lt.trans ⟨λ H x, (H x).imp $ λ _, le_of_lt, λ H x, let ⟨n, h⟩ := H x in ⟨n+1, lt_of_le_of_lt h (rat.cast_lt.2 (lt_add_one _))⟩⟩ variable [archimedean α] theorem exists_rat_lt (x : α) : ∃ q : ℚ, (q : α) < x := let ⟨n, h⟩ := exists_int_lt x in ⟨n, by rwa rat.cast_coe_int⟩ theorem exists_rat_btwn {x y : α} (h : x < y) : ∃ q : ℚ, x < q ∧ (q:α) < y := begin cases exists_nat_gt (y - x)⁻¹ with n nh, cases exists_floor (x * n) with z zh, refine ⟨(z + 1 : ℤ) / n, _⟩, have n0 := nat.cast_pos.1 (lt_trans (inv_pos (sub_pos.2 h)) nh), have n0' := (@nat.cast_pos α _ _).2 n0, rw [rat.cast_div_of_ne_zero, rat.cast_coe_nat, rat.cast_coe_int, div_lt_iff n0'], refine ⟨(lt_div_iff n0').2 $ (lt_iff_lt_of_le_iff_le (zh _)).1 (lt_add_one _), _⟩, rw [int.cast_add, int.cast_one], refine lt_of_le_of_lt (add_le_add_right ((zh _).1 (le_refl _)) _) _, rwa [← lt_sub_iff_add_lt', ← sub_mul, ← div_lt_iff' (sub_pos.2 h), one_div_eq_inv], { rw [rat.coe_int_denom, nat.cast_one], exact one_ne_zero }, { intro H, rw [rat.coe_nat_num, ← coe_coe, nat.cast_eq_zero] at H, subst H, cases n0 }, { rw [rat.coe_nat_denom, nat.cast_one], exact one_ne_zero } end theorem exists_nat_one_div_lt {ε : α} (hε : ε > 0) : ∃ n : ℕ, 1 / (n + 1: α) < ε := begin cases archimedean_iff_nat_lt.1 (by apply_instance) (1/ε) with n hn, existsi n, apply div_lt_of_mul_lt_of_pos, { simp, apply add_pos_of_pos_of_nonneg zero_lt_one, apply nat.cast_nonneg }, { apply (div_lt_iff' hε).1, transitivity, { exact hn }, { simp [zero_lt_one] }} end theorem exists_pos_rat_lt {x : α} (x0 : 0 < x) : ∃ q : ℚ, 0 < q ∧ (q : α) < x := by simpa only [rat.cast_pos] using exists_rat_btwn x0 include α @[simp] theorem rat.cast_floor (x : ℚ) : by haveI := archimedean.floor_ring α; exact ⌊(x:α)⌋ = ⌊x⌋ := begin haveI := archimedean.floor_ring α, apply le_antisymm, { rw [le_floor, ← @rat.cast_le α, rat.cast_coe_int], apply floor_le }, { rw [le_floor, ← rat.cast_coe_int, rat.cast_le], apply floor_le } end /-- `round` rounds a number to the nearest integer. `round (1 / 2) = 1` -/ def round [floor_ring α] (x : α) : ℤ := ⌊x + 1 / 2⌋ end linear_ordered_field section variables [discrete_linear_ordered_field α] [archimedean α] theorem exists_rat_near (x : α) {ε : α} (ε0 : ε > 0) : ∃ q : ℚ, abs (x - q) < ε := let ⟨q, h₁, h₂⟩ := exists_rat_btwn $ lt_trans ((sub_lt_self_iff x).2 ε0) ((lt_add_iff_pos_left x).2 ε0) in ⟨q, abs_sub_lt_iff.2 ⟨sub_lt.1 h₁, sub_lt_iff_lt_add.2 h₂⟩⟩ lemma abs_sub_round [floor_ring α] (x : α) : abs (x - round x) ≤ 1 / 2 := begin rw [round, abs_sub_le_iff], have := floor_le (x + 1 / 2), have := lt_floor_add_one (x + 1 / 2), split; linarith end instance : archimedean ℚ := archimedean_iff_rat_le.2 $ λ q, ⟨q, by rw rat.cast_id⟩ @[simp] theorem rat.cast_round {α : Type*} [discrete_linear_ordered_field α] [archimedean α] (x : ℚ) : by haveI := archimedean.floor_ring α; exact round (x:α) = round x := have ((x + (1 : ℚ) / (2 : ℚ) : ℚ) : α) = x + 1 / 2, by simp, by rw [round, round, ← this, rat.cast_floor] end
9ab496b7dc1d569a87994bd58f2d617ec5edf463
367134ba5a65885e863bdc4507601606690974c1
/src/number_theory/quadratic_reciprocity.lean
e12cc22c8da371278e9d597bd9df718522765bab
[ "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
25,026
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import field_theory.finite.basic import data.zmod.basic import data.nat.parity /-! # Quadratic reciprocity. This file contains results about quadratic residues modulo a prime number. The main results are the law of quadratic reciprocity, `quadratic_reciprocity`, as well as the interpretations in terms of existence of square roots depending on the congruence mod 4, `exists_pow_two_eq_prime_iff_of_mod_four_eq_one`, and `exists_pow_two_eq_prime_iff_of_mod_four_eq_three`. Also proven are conditions for `-1` and `2` to be a square modulo a prime, `exists_pow_two_eq_neg_one_iff_mod_four_ne_three` and `exists_pow_two_eq_two_iff` ## Implementation notes The proof of quadratic reciprocity implemented uses Gauss' lemma and Eisenstein's lemma -/ open function finset nat finite_field zmod open_locale big_operators nat namespace zmod variables (p q : ℕ) [fact p.prime] [fact q.prime] /-- Euler's Criterion: A unit `x` of `zmod p` is a square if and only if `x ^ (p / 2) = 1`. -/ lemma euler_criterion_units (x : units (zmod p)) : (∃ y : units (zmod p), y ^ 2 = x) ↔ x ^ (p / 2) = 1 := begin cases nat.prime.eq_two_or_odd ‹p.prime› with hp2 hp_odd, { substI p, refine iff_of_true ⟨1, _⟩ _; apply subsingleton.elim }, obtain ⟨g, hg⟩ := is_cyclic.exists_generator (units (zmod p)), obtain ⟨n, hn⟩ : x ∈ submonoid.powers g, { rw mem_powers_iff_mem_gpowers, apply hg }, split, { rintro ⟨y, rfl⟩, rw [← pow_mul, two_mul_odd_div_two hp_odd, units_pow_card_sub_one_eq_one], }, { subst x, assume h, have key : 2 * (p / 2) ∣ n * (p / 2), { rw [← pow_mul] at h, rw [two_mul_odd_div_two hp_odd, ← card_units, ← order_of_eq_card_of_forall_mem_gpowers hg], apply order_of_dvd_of_pow_eq_one h }, have : 0 < p / 2 := nat.div_pos (show fact (1 < p), by apply_instance) dec_trivial, obtain ⟨m, rfl⟩ := dvd_of_mul_dvd_mul_right this key, refine ⟨g ^ m, _⟩, rw [mul_comm, pow_mul], }, end /-- Euler's Criterion: a nonzero `a : zmod p` is a square if and only if `x ^ (p / 2) = 1`. -/ lemma euler_criterion {a : zmod p} (ha : a ≠ 0) : (∃ y : zmod p, y ^ 2 = a) ↔ a ^ (p / 2) = 1 := begin apply (iff_congr _ (by simp [units.ext_iff])).mp (euler_criterion_units p (units.mk0 a ha)), simp only [units.ext_iff, pow_two, units.coe_mk0, units.coe_mul], split, { rintro ⟨y, hy⟩, exact ⟨y, hy⟩ }, { rintro ⟨y, rfl⟩, have hy : y ≠ 0, { rintro rfl, simpa [zero_pow] using ha, }, refine ⟨units.mk0 y hy, _⟩, simp, } end lemma exists_pow_two_eq_neg_one_iff_mod_four_ne_three : (∃ y : zmod p, y ^ 2 = -1) ↔ p % 4 ≠ 3 := begin cases nat.prime.eq_two_or_odd ‹p.prime› with hp2 hp_odd, { substI p, exact dec_trivial }, change fact (p % 2 = 1) at hp_odd, resetI, have neg_one_ne_zero : (-1 : zmod p) ≠ 0, from mt neg_eq_zero.1 one_ne_zero, rw [euler_criterion p neg_one_ne_zero, neg_one_pow_eq_pow_mod_two], cases mod_two_eq_zero_or_one (p / 2) with p_half_even p_half_odd, { rw [p_half_even, pow_zero, eq_self_iff_true, true_iff], contrapose! p_half_even with hp, rw [← nat.mod_mul_right_div_self, show 2 * 2 = 4, from rfl, hp], exact dec_trivial }, { rw [p_half_odd, pow_one, iff_false_intro (ne_neg_self p one_ne_zero).symm, false_iff, not_not], rw [← nat.mod_mul_right_div_self, show 2 * 2 = 4, from rfl] at p_half_odd, rw [_root_.fact, ← nat.mod_mul_left_mod _ 2, show 2 * 2 = 4, from rfl] at hp_odd, have hp : p % 4 < 4, from nat.mod_lt _ dec_trivial, revert hp hp_odd p_half_odd, generalize : p % 4 = k, dec_trivial! } end lemma pow_div_two_eq_neg_one_or_one {a : zmod p} (ha : a ≠ 0) : a ^ (p / 2) = 1 ∨ a ^ (p / 2) = -1 := begin cases nat.prime.eq_two_or_odd ‹p.prime› with hp2 hp_odd, { substI p, revert a ha, exact dec_trivial }, rw [← mul_self_eq_one_iff, ← pow_add, ← two_mul, two_mul_odd_div_two hp_odd], exact pow_card_sub_one_eq_one ha end /-- Wilson's Lemma: the product of `1`, ..., `p-1` is `-1` modulo `p`. -/ @[simp] lemma wilsons_lemma : ((p - 1)! : zmod p) = -1 := begin refine calc ((p - 1)! : zmod p) = (∏ x in Ico 1 (succ (p - 1)), x) : by rw [← finset.prod_Ico_id_eq_factorial, prod_nat_cast] ... = (∏ x : units (zmod p), x) : _ ... = -1 : by rw [prod_hom _ (coe : units (zmod p) → zmod p), prod_univ_units_id_eq_neg_one, units.coe_neg, units.coe_one], have hp : 0 < p := nat.prime.pos ‹p.prime›, symmetry, refine prod_bij (λ a _, (a : zmod p).val) _ _ _ _, { intros a ha, rw [Ico.mem, ← nat.succ_sub hp, nat.succ_sub_one], split, { apply nat.pos_of_ne_zero, rw ← @val_zero p, assume h, apply units.ne_zero a (val_injective p h) }, { exact val_lt _ } }, { intros a ha, simp only [cast_id, nat_cast_val], }, { intros _ _ _ _ h, rw units.ext_iff, exact val_injective p h }, { intros b hb, rw [Ico.mem, nat.succ_le_iff, ← succ_sub hp, succ_sub_one, pos_iff_ne_zero] at hb, refine ⟨units.mk0 b _, finset.mem_univ _, _⟩, { assume h, apply hb.1, apply_fun val at h, simpa only [val_cast_of_lt hb.right, val_zero] using h }, { simp only [val_cast_of_lt hb.right, units.coe_mk0], } } end @[simp] lemma prod_Ico_one_prime : (∏ x in Ico 1 p, (x : zmod p)) = -1 := begin conv in (Ico 1 p) { rw [← succ_sub_one p, succ_sub (nat.prime.pos ‹p.prime›)] }, rw [← prod_nat_cast, finset.prod_Ico_id_eq_factorial, wilsons_lemma] end end zmod /-- The image of the map sending a non zero natural number `x ≤ p / 2` to the absolute value of the element of interger in the interval `(-p/2, p/2]` congruent to `a * x` mod p is the set of non zero natural numbers `x` such that `x ≤ p / 2` -/ lemma Ico_map_val_min_abs_nat_abs_eq_Ico_map_id (p : ℕ) [hp : fact p.prime] (a : zmod p) (hap : a ≠ 0) : (Ico 1 (p / 2).succ).1.map (λ x, (a * x).val_min_abs.nat_abs) = (Ico 1 (p / 2).succ).1.map (λ a, a) := begin have he : ∀ {x}, x ∈ Ico 1 (p / 2).succ → x ≠ 0 ∧ x ≤ p / 2, by simp [nat.lt_succ_iff, nat.succ_le_iff, pos_iff_ne_zero] {contextual := tt}, have hep : ∀ {x}, x ∈ Ico 1 (p / 2).succ → x < p, from λ x hx, lt_of_le_of_lt (he hx).2 (nat.div_lt_self hp.pos dec_trivial), have hpe : ∀ {x}, x ∈ Ico 1 (p / 2).succ → ¬ p ∣ x, from λ x hx hpx, not_lt_of_ge (le_of_dvd (nat.pos_of_ne_zero (he hx).1) hpx) (hep hx), have hmem : ∀ (x : ℕ) (hx : x ∈ Ico 1 (p / 2).succ), (a * x : zmod p).val_min_abs.nat_abs ∈ Ico 1 (p / 2).succ, { assume x hx, simp [hap, char_p.cast_eq_zero_iff (zmod p) p, hpe hx, lt_succ_iff, succ_le_iff, pos_iff_ne_zero, nat_abs_val_min_abs_le _], }, have hsurj : ∀ (b : ℕ) (hb : b ∈ Ico 1 (p / 2).succ), ∃ x ∈ Ico 1 (p / 2).succ, b = (a * x : zmod p).val_min_abs.nat_abs, { assume b hb, refine ⟨(b / a : zmod p).val_min_abs.nat_abs, Ico.mem.mpr ⟨_, _⟩, _⟩, { apply nat.pos_of_ne_zero, simp only [div_eq_mul_inv, hap, char_p.cast_eq_zero_iff (zmod p) p, hpe hb, not_false_iff, val_min_abs_eq_zero, inv_eq_zero, int.nat_abs_eq_zero, ne.def, mul_eq_zero, or_self] }, { apply lt_succ_of_le, apply nat_abs_val_min_abs_le }, { rw nat_cast_nat_abs_val_min_abs, split_ifs, { erw [mul_div_cancel' _ hap, val_min_abs_def_pos, val_cast_of_lt (hep hb), if_pos (le_of_lt_succ (Ico.mem.1 hb).2), int.nat_abs_of_nat], }, { erw [mul_neg_eq_neg_mul_symm, mul_div_cancel' _ hap, nat_abs_val_min_abs_neg, val_min_abs_def_pos, val_cast_of_lt (hep hb), if_pos (le_of_lt_succ (Ico.mem.1 hb).2), int.nat_abs_of_nat] } } }, exact multiset.map_eq_map_of_bij_of_nodup _ _ (finset.nodup _) (finset.nodup _) (λ x _, (a * x : zmod p).val_min_abs.nat_abs) hmem (λ _ _, rfl) (inj_on_of_surj_on_of_card_le _ hmem hsurj (le_refl _)) hsurj end private lemma gauss_lemma_aux₁ (p : ℕ) [hp : fact p.prime] [hp2 : fact (p % 2 = 1)] {a : ℕ} (hap : (a : zmod p) ≠ 0) : (a^(p / 2) * (p / 2)! : zmod p) = (-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, ¬(a * x : zmod p).val ≤ p / 2)).card * (p / 2)! := calc (a ^ (p / 2) * (p / 2)! : zmod p) = (∏ x in Ico 1 (p / 2).succ, a * x) : by rw [prod_mul_distrib, ← prod_nat_cast, ← prod_nat_cast, prod_Ico_id_eq_factorial, prod_const, Ico.card, succ_sub_one]; simp ... = (∏ x in Ico 1 (p / 2).succ, (a * x : zmod p).val) : by simp ... = (∏ x in Ico 1 (p / 2).succ, (if (a * x : zmod p).val ≤ p / 2 then 1 else -1) * (a * x : zmod p).val_min_abs.nat_abs) : prod_congr rfl $ λ _ _, begin simp only [nat_cast_nat_abs_val_min_abs], split_ifs; simp end ... = (-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, ¬(a * x : zmod p).val ≤ p / 2)).card * (∏ x in Ico 1 (p / 2).succ, (a * x : zmod p).val_min_abs.nat_abs) : have (∏ x in Ico 1 (p / 2).succ, if (a * x : zmod p).val ≤ p / 2 then (1 : zmod p) else -1) = (∏ x in (Ico 1 (p / 2).succ).filter (λ x : ℕ, ¬(a * x : zmod p).val ≤ p / 2), -1), from prod_bij_ne_one (λ x _ _, x) (λ x, by split_ifs; simp * at * {contextual := tt}) (λ _ _ _ _ _ _, id) (λ b h _, ⟨b, by simp [-not_le, *] at *⟩) (by intros; split_ifs at *; simp * at *), by rw [prod_mul_distrib, this]; simp ... = (-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, ¬(a * x : zmod p).val ≤ p / 2)).card * (p / 2)! : by rw [← prod_nat_cast, finset.prod_eq_multiset_prod, Ico_map_val_min_abs_nat_abs_eq_Ico_map_id p a hap, ← finset.prod_eq_multiset_prod, prod_Ico_id_eq_factorial] private lemma gauss_lemma_aux₂ (p : ℕ) [hp : fact p.prime] [hp2 : fact (p % 2 = 1)] {a : ℕ} (hap : (a : zmod p) ≠ 0) : (a^(p / 2) : zmod p) = (-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmod p).val)).card := (mul_left_inj' (show ((p / 2)! : zmod p) ≠ 0, by rw [ne.def, char_p.cast_eq_zero_iff (zmod p) p, hp.dvd_factorial, not_le]; exact nat.div_lt_self hp.pos dec_trivial)).1 $ by simpa using gauss_lemma_aux₁ p hap private lemma eisenstein_lemma_aux₁ (p : ℕ) [hp : fact p.prime] [hp2 : fact (p % 2 = 1)] {a : ℕ} (hap : (a : zmod p) ≠ 0) : ((∑ x in Ico 1 (p / 2).succ, a * x : ℕ) : zmod 2) = ((Ico 1 (p / 2).succ).filter ((λ x : ℕ, p / 2 < (a * x : zmod p).val))).card + ∑ x in Ico 1 (p / 2).succ, x + (∑ x in Ico 1 (p / 2).succ, (a * x) / p : ℕ) := have hp2 : (p : zmod 2) = (1 : ℕ), from (eq_iff_modeq_nat _).2 hp2, calc ((∑ x in Ico 1 (p / 2).succ, a * x : ℕ) : zmod 2) = ((∑ x in Ico 1 (p / 2).succ, ((a * x) % p + p * ((a * x) / p)) : ℕ) : zmod 2) : by simp only [mod_add_div] ... = (∑ x in Ico 1 (p / 2).succ, ((a * x : ℕ) : zmod p).val : ℕ) + (∑ x in Ico 1 (p / 2).succ, (a * x) / p : ℕ) : by simp only [val_nat_cast]; simp [sum_add_distrib, mul_sum.symm, nat.cast_add, nat.cast_mul, sum_nat_cast, hp2] ... = _ : congr_arg2 (+) (calc ((∑ x in Ico 1 (p / 2).succ, ((a * x : ℕ) : zmod p).val : ℕ) : zmod 2) = ∑ x in Ico 1 (p / 2).succ, ((((a * x : zmod p).val_min_abs + (if (a * x : zmod p).val ≤ p / 2 then 0 else p)) : ℤ) : zmod 2) : by simp only [(val_eq_ite_val_min_abs _).symm]; simp [sum_nat_cast] ... = ((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmod p).val)).card + ((∑ x in Ico 1 (p / 2).succ, (a * x : zmod p).val_min_abs.nat_abs) : ℕ) : by { simp [ite_cast, add_comm, sum_add_distrib, finset.sum_ite, hp2, sum_nat_cast], } ... = _ : by rw [finset.sum_eq_multiset_sum, Ico_map_val_min_abs_nat_abs_eq_Ico_map_id p a hap, ← finset.sum_eq_multiset_sum]; simp [sum_nat_cast]) rfl private lemma eisenstein_lemma_aux₂ (p : ℕ) [hp : fact p.prime] [hp2 : fact (p % 2 = 1)] {a : ℕ} (ha2 : a % 2 = 1) (hap : (a : zmod p) ≠ 0) : ((Ico 1 (p / 2).succ).filter ((λ x : ℕ, p / 2 < (a * x : zmod p).val))).card ≡ ∑ x in Ico 1 (p / 2).succ, (x * a) / p [MOD 2] := have ha2 : (a : zmod 2) = (1 : ℕ), from (eq_iff_modeq_nat _).2 ha2, (eq_iff_modeq_nat 2).1 $ sub_eq_zero.1 $ by simpa [add_left_comm, sub_eq_add_neg, finset.mul_sum.symm, mul_comm, ha2, sum_nat_cast, add_neg_eq_iff_eq_add.symm, neg_eq_self_mod_two, add_assoc] using eq.symm (eisenstein_lemma_aux₁ p hap) lemma div_eq_filter_card {a b c : ℕ} (hb0 : 0 < b) (hc : a / b ≤ c) : a / b = ((Ico 1 c.succ).filter (λ x, x * b ≤ a)).card := calc a / b = (Ico 1 (a / b).succ).card : by simp ... = ((Ico 1 c.succ).filter (λ x, x * b ≤ a)).card : congr_arg _ $ finset.ext $ λ x, have x * b ≤ a → x ≤ c, from λ h, le_trans (by rwa [le_div_iff_mul_le _ _ hb0]) hc, by simp [lt_succ_iff, le_div_iff_mul_le _ _ hb0]; tauto /-- The given sum is the number of integer points in the triangle formed by the diagonal of the rectangle `(0, p/2) × (0, q/2)` -/ private lemma sum_Ico_eq_card_lt {p q : ℕ} : ∑ a in Ico 1 (p / 2).succ, (a * q) / p = (((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.2 * p ≤ x.1 * q)).card := if hp0 : p = 0 then by simp [hp0, finset.ext_iff] else calc ∑ a in Ico 1 (p / 2).succ, (a * q) / p = ∑ a in Ico 1 (p / 2).succ, ((Ico 1 (q / 2).succ).filter (λ x, x * p ≤ a * q)).card : finset.sum_congr rfl $ λ x hx, div_eq_filter_card (nat.pos_of_ne_zero hp0) (calc x * q / p ≤ (p / 2) * q / p : nat.div_le_div_right (mul_le_mul_of_nonneg_right (le_of_lt_succ $ by finish) (nat.zero_le _)) ... ≤ _ : nat.div_mul_div_le_div _ _ _) ... = _ : by rw [← card_sigma]; exact card_congr (λ a _, ⟨a.1, a.2⟩) (by simp only [mem_filter, mem_sigma, and_self, forall_true_iff, mem_product] {contextual := tt}) (λ ⟨_, _⟩ ⟨_, _⟩, by simp only [prod.mk.inj_iff, eq_self_iff_true, and_self, heq_iff_eq, forall_true_iff] {contextual := tt}) (λ ⟨b₁, b₂⟩ h, ⟨⟨b₁, b₂⟩, by revert h; simp only [mem_filter, eq_self_iff_true, exists_prop_of_true, mem_sigma, and_self, forall_true_iff, mem_product] {contextual := tt}⟩) /-- Each of the sums in this lemma is the cardinality of the set integer points in each of the two triangles formed by the diagonal of the rectangle `(0, p/2) × (0, q/2)`. Adding them gives the number of points in the rectangle. -/ private lemma sum_mul_div_add_sum_mul_div_eq_mul (p q : ℕ) [hp : fact p.prime] (hq0 : (q : zmod p) ≠ 0) : ∑ a in Ico 1 (p / 2).succ, (a * q) / p + ∑ a in Ico 1 (q / 2).succ, (a * p) / q = (p / 2) * (q / 2) := begin have hswap : (((Ico 1 (q / 2).succ).product (Ico 1 (p / 2).succ)).filter (λ x : ℕ × ℕ, x.2 * q ≤ x.1 * p)).card = (((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.1 * q ≤ x.2 * p)).card := card_congr (λ x _, prod.swap x) (λ ⟨_, _⟩, by simp only [mem_filter, and_self, prod.swap_prod_mk, forall_true_iff, mem_product] {contextual := tt}) (λ ⟨_, _⟩ ⟨_, _⟩, by simp only [prod.mk.inj_iff, eq_self_iff_true, and_self, prod.swap_prod_mk, forall_true_iff] {contextual := tt}) (λ ⟨x₁, x₂⟩ h, ⟨⟨x₂, x₁⟩, by revert h; simp only [mem_filter, eq_self_iff_true, and_self, exists_prop_of_true, prod.swap_prod_mk, forall_true_iff, mem_product] {contextual := tt}⟩), have hdisj : disjoint (((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.2 * p ≤ x.1 * q)) (((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.1 * q ≤ x.2 * p)), { apply disjoint_filter.2 (λ x hx hpq hqp, _), have hxp : x.1 < p, from lt_of_le_of_lt (show x.1 ≤ p / 2, by simp only [*, lt_succ_iff, Ico.mem, mem_product] at *; tauto) (nat.div_lt_self hp.pos dec_trivial), have : (x.1 : zmod p) = 0, { simpa [hq0] using congr_arg (coe : ℕ → zmod p) (le_antisymm hpq hqp) }, apply_fun zmod.val at this, rw [val_cast_of_lt hxp, val_zero] at this, simpa only [this, nonpos_iff_eq_zero, Ico.mem, one_ne_zero, false_and, mem_product] using hx }, have hunion : ((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.2 * p ≤ x.1 * q) ∪ ((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)).filter (λ x : ℕ × ℕ, x.1 * q ≤ x.2 * p) = ((Ico 1 (p / 2).succ).product (Ico 1 (q / 2).succ)), from finset.ext (λ x, by have := le_total (x.2 * p) (x.1 * q); simp only [mem_union, mem_filter, Ico.mem, mem_product]; tauto), rw [sum_Ico_eq_card_lt, sum_Ico_eq_card_lt, hswap, ← card_disjoint_union hdisj, hunion, card_product], simp only [Ico.card, nat.sub_zero, succ_sub_succ_eq_sub] end variables (p q : ℕ) [fact p.prime] [fact q.prime] namespace zmod /-- The Legendre symbol of `a` and `p` is an integer defined as * `0` if `a` is `0` modulo `p`; * `1` if `a ^ (p / 2)` is `1` modulo `p` (by `euler_criterion` this is equivalent to “`a` is a square modulo `p`”); * `-1` otherwise. -/ def legendre_sym (a p : ℕ) : ℤ := if (a : zmod p) = 0 then 0 else if (a : zmod p) ^ (p / 2) = 1 then 1 else -1 lemma legendre_sym_eq_pow (a p : ℕ) [hp : fact p.prime] : (legendre_sym a p : zmod p) = (a ^ (p / 2)) := begin rw legendre_sym, by_cases ha : (a : zmod p) = 0, { simp only [if_pos, ha, zero_pow (nat.div_pos (hp.two_le) (succ_pos 1)), int.cast_zero] }, cases hp.eq_two_or_odd with hp2 hp_odd, { substI p, generalize : (a : (zmod 2)) = b, revert b, dec_trivial, }, { change fact (p % 2 = 1) at hp_odd, resetI, rw if_neg ha, have : (-1 : zmod p) ≠ 1, from (ne_neg_self p one_ne_zero).symm, cases pow_div_two_eq_neg_one_or_one p ha with h h, { rw [if_pos h, h, int.cast_one], }, { rw [h, if_neg this, int.cast_neg, int.cast_one], } } end lemma legendre_sym_eq_one_or_neg_one (a p : ℕ) (ha : (a : zmod p) ≠ 0) : legendre_sym a p = -1 ∨ legendre_sym a p = 1 := by unfold legendre_sym; split_ifs; simp only [*, eq_self_iff_true, or_true, true_or] at * lemma legendre_sym_eq_zero_iff (a p : ℕ) : legendre_sym a p = 0 ↔ (a : zmod p) = 0 := begin split, { classical, contrapose, assume ha, cases legendre_sym_eq_one_or_neg_one a p ha with h h, all_goals { rw h, norm_num } }, { assume ha, rw [legendre_sym, if_pos ha] } end /-- Gauss' lemma. The legendre symbol can be computed by considering the number of naturals less than `p/2` such that `(a * x) % p > p / 2` -/ lemma gauss_lemma {a : ℕ} [hp1 : fact (p % 2 = 1)] (ha0 : (a : zmod p) ≠ 0) : legendre_sym a p = (-1) ^ ((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmod p).val)).card := have (legendre_sym a p : zmod p) = (((-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmod p).val)).card : ℤ) : zmod p), by rw [legendre_sym_eq_pow, gauss_lemma_aux₂ p ha0]; simp, begin cases legendre_sym_eq_one_or_neg_one a p ha0; cases @neg_one_pow_eq_or ℤ _ ((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmod p).val)).card; simp [*, ne_neg_self p one_ne_zero, (ne_neg_self p one_ne_zero).symm] at * end lemma legendre_sym_eq_one_iff {a : ℕ} (ha0 : (a : zmod p) ≠ 0) : legendre_sym a p = 1 ↔ (∃ b : zmod p, b ^ 2 = a) := begin rw [euler_criterion p ha0, legendre_sym, if_neg ha0], split_ifs, { simp only [h, eq_self_iff_true] }, finish -- this is quite slow. I'm actually surprised that it can close the goal at all! end lemma eisenstein_lemma [hp1 : fact (p % 2 = 1)] {a : ℕ} (ha1 : a % 2 = 1) (ha0 : (a : zmod p) ≠ 0) : legendre_sym a p = (-1)^∑ x in Ico 1 (p / 2).succ, (x * a) / p := by rw [neg_one_pow_eq_pow_mod_two, gauss_lemma p ha0, neg_one_pow_eq_pow_mod_two, show _ = _, from eisenstein_lemma_aux₂ p ha1 ha0] theorem quadratic_reciprocity [hp1 : fact (p % 2 = 1)] [hq1 : fact (q % 2 = 1)] (hpq : p ≠ q) : legendre_sym p q * legendre_sym q p = (-1) ^ ((p / 2) * (q / 2)) := have hpq0 : (p : zmod q) ≠ 0, from prime_ne_zero q p hpq.symm, have hqp0 : (q : zmod p) ≠ 0, from prime_ne_zero p q hpq, by rw [eisenstein_lemma q hp1 hpq0, eisenstein_lemma p hq1 hqp0, ← pow_add, sum_mul_div_add_sum_mul_div_eq_mul q p hpq0, mul_comm] -- move this instance fact_prime_two : fact (nat.prime 2) := nat.prime_two lemma legendre_sym_two [hp1 : fact (p % 2 = 1)] : legendre_sym 2 p = (-1) ^ (p / 4 + p / 2) := have hp2 : p ≠ 2, from mt (congr_arg (% 2)) (by simpa using hp1), have hp22 : p / 2 / 2 = _ := div_eq_filter_card (show 0 < 2, from dec_trivial) (nat.div_le_self (p / 2) 2), have hcard : (Ico 1 (p / 2).succ).card = p / 2, by simp, have hx2 : ∀ x ∈ Ico 1 (p / 2).succ, (2 * x : zmod p).val = 2 * x, from λ x hx, have h2xp : 2 * x < p, from calc 2 * x ≤ 2 * (p / 2) : mul_le_mul_of_nonneg_left (le_of_lt_succ $ by finish) dec_trivial ... < _ : by conv_rhs {rw [← div_add_mod p 2, show p % 2 = 1, from hp1]}; exact lt_succ_self _, by rw [← nat.cast_two, ← nat.cast_mul, val_cast_of_lt h2xp], have hdisj : disjoint ((Ico 1 (p / 2).succ).filter (λ x, p / 2 < ((2 : ℕ) * x : zmod p).val)) ((Ico 1 (p / 2).succ).filter (λ x, x * 2 ≤ p / 2)), from disjoint_filter.2 (λ x hx, by simp [hx2 _ hx, mul_comm]), have hunion : ((Ico 1 (p / 2).succ).filter (λ x, p / 2 < ((2 : ℕ) * x : zmod p).val)) ∪ ((Ico 1 (p / 2).succ).filter (λ x, x * 2 ≤ p / 2)) = Ico 1 (p / 2).succ, begin rw [filter_union_right], conv_rhs {rw [← @filter_true _ (Ico 1 (p / 2).succ)]}, exact filter_congr (λ x hx, by simp [hx2 _ hx, lt_or_le, mul_comm]) end, begin rw [gauss_lemma p (prime_ne_zero p 2 hp2), neg_one_pow_eq_pow_mod_two, @neg_one_pow_eq_pow_mod_two _ _ (p / 4 + p / 2)], refine congr_arg2 _ rfl ((eq_iff_modeq_nat 2).1 _), rw [show 4 = 2 * 2, from rfl, ← nat.div_div_eq_div_mul, hp22, nat.cast_add, ← sub_eq_iff_eq_add', sub_eq_add_neg, neg_eq_self_mod_two, ← nat.cast_add, ← card_disjoint_union hdisj, hunion, hcard] end lemma exists_pow_two_eq_two_iff [hp1 : fact (p % 2 = 1)] : (∃ a : zmod p, a ^ 2 = 2) ↔ p % 8 = 1 ∨ p % 8 = 7 := have hp2 : ((2 : ℕ) : zmod p) ≠ 0, from prime_ne_zero p 2 (λ h, by simpa [h] using hp1), have hpm4 : p % 4 = p % 8 % 4, from (nat.mod_mul_left_mod p 2 4).symm, have hpm2 : p % 2 = p % 8 % 2, from (nat.mod_mul_left_mod p 4 2).symm, begin rw [show (2 : zmod p) = (2 : ℕ), by simp, ← legendre_sym_eq_one_iff p hp2, legendre_sym_two p, neg_one_pow_eq_one_iff_even (show (-1 : ℤ) ≠ 1, from dec_trivial), even_add, even_div, even_div], have := nat.mod_lt p (show 0 < 8, from dec_trivial), resetI, rw _root_.fact at hp1, revert this hp1, erw [hpm4, hpm2], generalize hm : p % 8 = m, unfreezingI {clear_dependent p}, dec_trivial!, end lemma exists_pow_two_eq_prime_iff_of_mod_four_eq_one (hp1 : p % 4 = 1) [hq1 : fact (q % 2 = 1)] : (∃ a : zmod p, a ^ 2 = q) ↔ ∃ b : zmod q, b ^ 2 = p := if hpq : p = q then by substI hpq else have h1 : ((p / 2) * (q / 2)) % 2 = 0, from (dvd_iff_mod_eq_zero _ _).1 (dvd_mul_of_dvd_left ((dvd_iff_mod_eq_zero _ _).2 $ by rw [← mod_mul_right_div_self, show 2 * 2 = 4, from rfl, hp1]; refl) _), begin haveI hp_odd : fact (p % 2 = 1) := odd_of_mod_four_eq_one hp1, have hpq0 : (p : zmod q) ≠ 0 := prime_ne_zero q p (ne.symm hpq), have hqp0 : (q : zmod p) ≠ 0 := prime_ne_zero p q hpq, have := quadratic_reciprocity p q hpq, rw [neg_one_pow_eq_pow_mod_two, h1, legendre_sym, legendre_sym, if_neg hqp0, if_neg hpq0] at this, rw [euler_criterion q hpq0, euler_criterion p hqp0], split_ifs at this; simp *; contradiction, end lemma exists_pow_two_eq_prime_iff_of_mod_four_eq_three (hp3 : p % 4 = 3) (hq3 : q % 4 = 3) (hpq : p ≠ q) : (∃ a : zmod p, a ^ 2 = q) ↔ ¬∃ b : zmod q, b ^ 2 = p := have h1 : ((p / 2) * (q / 2)) % 2 = 1, from nat.odd_mul_odd (by rw [← mod_mul_right_div_self, show 2 * 2 = 4, from rfl, hp3]; refl) (by rw [← mod_mul_right_div_self, show 2 * 2 = 4, from rfl, hq3]; refl), begin haveI hp_odd : fact (p % 2 = 1) := odd_of_mod_four_eq_three hp3, haveI hq_odd : fact (q % 2 = 1) := odd_of_mod_four_eq_three hq3, have hpq0 : (p : zmod q) ≠ 0 := prime_ne_zero q p (ne.symm hpq), have hqp0 : (q : zmod p) ≠ 0 := prime_ne_zero p q hpq, have := quadratic_reciprocity p q hpq, rw [neg_one_pow_eq_pow_mod_two, h1, legendre_sym, legendre_sym, if_neg hpq0, if_neg hqp0] at this, rw [euler_criterion q hpq0, euler_criterion p hqp0], split_ifs at this; simp *; contradiction end end zmod
48a4f38d917d072a434ffd6e198b6dee781020cc
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/linear_algebra/affine_space/combination.lean
f367ce75ac90c51e24c93f6e1a618b8074d6f1a4
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
32,002
lean
/- Copyright (c) 2020 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joseph Myers -/ import algebra.invertible import algebra.indicator_function import linear_algebra.affine_space.affine_map import linear_algebra.affine_space.affine_subspace import linear_algebra.finsupp import tactic.fin_cases /-! # Affine combinations of points This file defines affine combinations of points. ## Main definitions * `weighted_vsub_of_point` is a general weighted combination of subtractions with an explicit base point, yielding a vector. * `weighted_vsub` uses an arbitrary choice of base point and is intended to be used when the sum of weights is 0, in which case the result is independent of the choice of base point. * `affine_combination` adds the weighted combination to the arbitrary base point, yielding a point rather than a vector, and is intended to be used when the sum of weights is 1, in which case the result is independent of the choice of base point. These definitions are for sums over a `finset`; versions for a `fintype` may be obtained using `finset.univ`, while versions for a `finsupp` may be obtained using `finsupp.support`. ## References * https://en.wikipedia.org/wiki/Affine_space -/ noncomputable theory open_locale big_operators classical affine namespace finset lemma univ_fin2 : (univ : finset (fin 2)) = {0, 1} := by { ext x, fin_cases x; simp } variables {k : Type*} {V : Type*} {P : Type*} [ring k] [add_comm_group V] [module k V] variables [S : affine_space V P] include S variables {ι : Type*} (s : finset ι) variables {ι₂ : Type*} (s₂ : finset ι₂) /-- A weighted sum of the results of subtracting a base point from the given points, as a linear map on the weights. The main cases of interest are where the sum of the weights is 0, in which case the sum is independent of the choice of base point, and where the sum of the weights is 1, in which case the sum added to the base point is independent of the choice of base point. -/ def weighted_vsub_of_point (p : ι → P) (b : P) : (ι → k) →ₗ[k] V := ∑ i in s, (linear_map.proj i : (ι → k) →ₗ[k] k).smul_right (p i -ᵥ b) @[simp] lemma weighted_vsub_of_point_apply (w : ι → k) (p : ι → P) (b : P) : s.weighted_vsub_of_point p b w = ∑ i in s, w i • (p i -ᵥ b) := by simp [weighted_vsub_of_point, linear_map.sum_apply] /-- The weighted sum is independent of the base point when the sum of the weights is 0. -/ lemma weighted_vsub_of_point_eq_of_sum_eq_zero (w : ι → k) (p : ι → P) (h : ∑ i in s, w i = 0) (b₁ b₂ : P) : s.weighted_vsub_of_point p b₁ w = s.weighted_vsub_of_point p b₂ w := begin apply eq_of_sub_eq_zero, rw [weighted_vsub_of_point_apply, weighted_vsub_of_point_apply, ←sum_sub_distrib], conv_lhs { congr, skip, funext, rw [←smul_sub, vsub_sub_vsub_cancel_left] }, rw [←sum_smul, h, zero_smul] end /-- The weighted sum, added to the base point, is independent of the base point when the sum of the weights is 1. -/ lemma weighted_vsub_of_point_vadd_eq_of_sum_eq_one (w : ι → k) (p : ι → P) (h : ∑ i in s, w i = 1) (b₁ b₂ : P) : s.weighted_vsub_of_point p b₁ w +ᵥ b₁ = s.weighted_vsub_of_point p b₂ w +ᵥ b₂ := begin erw [weighted_vsub_of_point_apply, weighted_vsub_of_point_apply, ←@vsub_eq_zero_iff_eq V, vadd_vsub_assoc, vsub_vadd_eq_vsub_sub, ←add_sub_assoc, add_comm, add_sub_assoc, ←sum_sub_distrib], conv_lhs { congr, skip, congr, skip, funext, rw [←smul_sub, vsub_sub_vsub_cancel_left] }, rw [←sum_smul, h, one_smul, vsub_add_vsub_cancel, vsub_self] end /-- The weighted sum is unaffected by removing the base point, if present, from the set of points. -/ @[simp] lemma weighted_vsub_of_point_erase (w : ι → k) (p : ι → P) (i : ι) : (s.erase i).weighted_vsub_of_point p (p i) w = s.weighted_vsub_of_point p (p i) w := begin rw [weighted_vsub_of_point_apply, weighted_vsub_of_point_apply], apply sum_erase, rw [vsub_self, smul_zero] end /-- The weighted sum is unaffected by adding the base point, whether or not present, to the set of points. -/ @[simp] lemma weighted_vsub_of_point_insert (w : ι → k) (p : ι → P) (i : ι) : (insert i s).weighted_vsub_of_point p (p i) w = s.weighted_vsub_of_point p (p i) w := begin rw [weighted_vsub_of_point_apply, weighted_vsub_of_point_apply], apply sum_insert_zero, rw [vsub_self, smul_zero] end /-- The weighted sum is unaffected by changing the weights to the corresponding indicator function and adding points to the set. -/ lemma weighted_vsub_of_point_indicator_subset (w : ι → k) (p : ι → P) (b : P) {s₁ s₂ : finset ι} (h : s₁ ⊆ s₂) : s₁.weighted_vsub_of_point p b w = s₂.weighted_vsub_of_point p b (set.indicator ↑s₁ w) := begin rw [weighted_vsub_of_point_apply, weighted_vsub_of_point_apply], exact set.sum_indicator_subset_of_eq_zero w (λ i wi, wi • (p i -ᵥ b : V)) h (λ i, zero_smul k _) end /-- A weighted sum, over the image of an embedding, equals a weighted sum with the same points and weights over the original `finset`. -/ lemma weighted_vsub_of_point_map (e : ι₂ ↪ ι) (w : ι → k) (p : ι → P) (b : P) : (s₂.map e).weighted_vsub_of_point p b w = s₂.weighted_vsub_of_point (p ∘ e) b (w ∘ e) := begin simp_rw [weighted_vsub_of_point_apply], exact finset.sum_map _ _ _ end /-- A weighted sum of the results of subtracting a default base point from the given points, as a linear map on the weights. This is intended to be used when the sum of the weights is 0; that condition is specified as a hypothesis on those lemmas that require it. -/ def weighted_vsub (p : ι → P) : (ι → k) →ₗ[k] V := s.weighted_vsub_of_point p (classical.choice S.nonempty) /-- Applying `weighted_vsub` with given weights. This is for the case where a result involving a default base point is OK (for example, when that base point will cancel out later); a more typical use case for `weighted_vsub` would involve selecting a preferred base point with `weighted_vsub_eq_weighted_vsub_of_point_of_sum_eq_zero` and then using `weighted_vsub_of_point_apply`. -/ lemma weighted_vsub_apply (w : ι → k) (p : ι → P) : s.weighted_vsub p w = ∑ i in s, w i • (p i -ᵥ (classical.choice S.nonempty)) := by simp [weighted_vsub, linear_map.sum_apply] /-- `weighted_vsub` gives the sum of the results of subtracting any base point, when the sum of the weights is 0. -/ lemma weighted_vsub_eq_weighted_vsub_of_point_of_sum_eq_zero (w : ι → k) (p : ι → P) (h : ∑ i in s, w i = 0) (b : P) : s.weighted_vsub p w = s.weighted_vsub_of_point p b w := s.weighted_vsub_of_point_eq_of_sum_eq_zero w p h _ _ /-- The `weighted_vsub` for an empty set is 0. -/ @[simp] lemma weighted_vsub_empty (w : ι → k) (p : ι → P) : (∅ : finset ι).weighted_vsub p w = (0:V) := by simp [weighted_vsub_apply] /-- The weighted sum is unaffected by changing the weights to the corresponding indicator function and adding points to the set. -/ lemma weighted_vsub_indicator_subset (w : ι → k) (p : ι → P) {s₁ s₂ : finset ι} (h : s₁ ⊆ s₂) : s₁.weighted_vsub p w = s₂.weighted_vsub p (set.indicator ↑s₁ w) := weighted_vsub_of_point_indicator_subset _ _ _ h /-- A weighted subtraction, over the image of an embedding, equals a weighted subtraction with the same points and weights over the original `finset`. -/ lemma weighted_vsub_map (e : ι₂ ↪ ι) (w : ι → k) (p : ι → P) : (s₂.map e).weighted_vsub p w = s₂.weighted_vsub (p ∘ e) (w ∘ e) := s₂.weighted_vsub_of_point_map _ _ _ _ /-- A weighted sum of the results of subtracting a default base point from the given points, added to that base point, as an affine map on the weights. This is intended to be used when the sum of the weights is 1, in which case it is an affine combination (barycenter) of the points with the given weights; that condition is specified as a hypothesis on those lemmas that require it. -/ def affine_combination (p : ι → P) : (ι → k) →ᵃ[k] P := { to_fun := λ w, s.weighted_vsub_of_point p (classical.choice S.nonempty) w +ᵥ (classical.choice S.nonempty), linear := s.weighted_vsub p, map_vadd' := λ w₁ w₂, by simp_rw [vadd_vadd, weighted_vsub, vadd_eq_add, linear_map.map_add] } /-- The linear map corresponding to `affine_combination` is `weighted_vsub`. -/ @[simp] lemma affine_combination_linear (p : ι → P) : (s.affine_combination p : (ι → k) →ᵃ[k] P).linear = s.weighted_vsub p := rfl /-- Applying `affine_combination` with given weights. This is for the case where a result involving a default base point is OK (for example, when that base point will cancel out later); a more typical use case for `affine_combination` would involve selecting a preferred base point with `affine_combination_eq_weighted_vsub_of_point_vadd_of_sum_eq_one` and then using `weighted_vsub_of_point_apply`. -/ lemma affine_combination_apply (w : ι → k) (p : ι → P) : s.affine_combination p w = s.weighted_vsub_of_point p (classical.choice S.nonempty) w +ᵥ (classical.choice S.nonempty) := rfl /-- `affine_combination` gives the sum with any base point, when the sum of the weights is 1. -/ lemma affine_combination_eq_weighted_vsub_of_point_vadd_of_sum_eq_one (w : ι → k) (p : ι → P) (h : ∑ i in s, w i = 1) (b : P) : s.affine_combination p w = s.weighted_vsub_of_point p b w +ᵥ b := s.weighted_vsub_of_point_vadd_eq_of_sum_eq_one w p h _ _ /-- Adding a `weighted_vsub` to an `affine_combination`. -/ lemma weighted_vsub_vadd_affine_combination (w₁ w₂ : ι → k) (p : ι → P) : s.weighted_vsub p w₁ +ᵥ s.affine_combination p w₂ = s.affine_combination p (w₁ + w₂) := by rw [←vadd_eq_add, affine_map.map_vadd, affine_combination_linear] /-- Subtracting two `affine_combination`s. -/ lemma affine_combination_vsub (w₁ w₂ : ι → k) (p : ι → P) : s.affine_combination p w₁ -ᵥ s.affine_combination p w₂ = s.weighted_vsub p (w₁ - w₂) := by rw [←affine_map.linear_map_vsub, affine_combination_linear, vsub_eq_sub] /-- An `affine_combination` equals a point if that point is in the set and has weight 1 and the other points in the set have weight 0. -/ @[simp] lemma affine_combination_of_eq_one_of_eq_zero (w : ι → k) (p : ι → P) {i : ι} (his : i ∈ s) (hwi : w i = 1) (hw0 : ∀ i2 ∈ s, i2 ≠ i → w i2 = 0) : s.affine_combination p w = p i := begin have h1 : ∑ i in s, w i = 1 := hwi ▸ sum_eq_single i hw0 (λ h, false.elim (h his)), rw [s.affine_combination_eq_weighted_vsub_of_point_vadd_of_sum_eq_one w p h1 (p i), weighted_vsub_of_point_apply], convert zero_vadd V (p i), convert sum_eq_zero _, intros i2 hi2, by_cases h : i2 = i, { simp [h] }, { simp [hw0 i2 hi2 h] } end /-- An affine combination is unaffected by changing the weights to the corresponding indicator function and adding points to the set. -/ lemma affine_combination_indicator_subset (w : ι → k) (p : ι → P) {s₁ s₂ : finset ι} (h : s₁ ⊆ s₂) : s₁.affine_combination p w = s₂.affine_combination p (set.indicator ↑s₁ w) := by rw [affine_combination_apply, affine_combination_apply, weighted_vsub_of_point_indicator_subset _ _ _ h] /-- An affine combination, over the image of an embedding, equals an affine combination with the same points and weights over the original `finset`. -/ lemma affine_combination_map (e : ι₂ ↪ ι) (w : ι → k) (p : ι → P) : (s₂.map e).affine_combination p w = s₂.affine_combination (p ∘ e) (w ∘ e) := by simp_rw [affine_combination_apply, weighted_vsub_of_point_map] variables {V} /-- Suppose an indexed family of points is given, along with a subset of the index type. A vector can be expressed as `weighted_vsub_of_point` using a `finset` lying within that subset and with a given sum of weights if and only if it can be expressed as `weighted_vsub_of_point` with that sum of weights for the corresponding indexed family whose index type is the subtype corresponding to that subset. -/ lemma eq_weighted_vsub_of_point_subset_iff_eq_weighted_vsub_of_point_subtype {v : V} {x : k} {s : set ι} {p : ι → P} {b : P} : (∃ (fs : finset ι) (hfs : ↑fs ⊆ s) (w : ι → k) (hw : ∑ i in fs, w i = x), v = fs.weighted_vsub_of_point p b w) ↔ ∃ (fs : finset s) (w : s → k) (hw : ∑ i in fs, w i = x), v = fs.weighted_vsub_of_point (λ (i : s), p i) b w := begin simp_rw weighted_vsub_of_point_apply, split, { rintros ⟨fs, hfs, w, rfl, rfl⟩, use [fs.subtype s, λ i, w i, sum_subtype_of_mem _ hfs, (sum_subtype_of_mem _ hfs).symm] }, { rintros ⟨fs, w, rfl, rfl⟩, refine ⟨fs.map (function.embedding.subtype _), map_subtype_subset _, λ i, if h : i ∈ s then w ⟨i, h⟩ else 0, _, _⟩; simp } end variables (k) /-- Suppose an indexed family of points is given, along with a subset of the index type. A vector can be expressed as `weighted_vsub` using a `finset` lying within that subset and with sum of weights 0 if and only if it can be expressed as `weighted_vsub` with sum of weights 0 for the corresponding indexed family whose index type is the subtype corresponding to that subset. -/ lemma eq_weighted_vsub_subset_iff_eq_weighted_vsub_subtype {v : V} {s : set ι} {p : ι → P} : (∃ (fs : finset ι) (hfs : ↑fs ⊆ s) (w : ι → k) (hw : ∑ i in fs, w i = 0), v = fs.weighted_vsub p w) ↔ ∃ (fs : finset s) (w : s → k) (hw : ∑ i in fs, w i = 0), v = fs.weighted_vsub (λ (i : s), p i) w := eq_weighted_vsub_of_point_subset_iff_eq_weighted_vsub_of_point_subtype variables (V) /-- Suppose an indexed family of points is given, along with a subset of the index type. A point can be expressed as an `affine_combination` using a `finset` lying within that subset and with sum of weights 1 if and only if it can be expressed an `affine_combination` with sum of weights 1 for the corresponding indexed family whose index type is the subtype corresponding to that subset. -/ lemma eq_affine_combination_subset_iff_eq_affine_combination_subtype {p0 : P} {s : set ι} {p : ι → P} : (∃ (fs : finset ι) (hfs : ↑fs ⊆ s) (w : ι → k) (hw : ∑ i in fs, w i = 1), p0 = fs.affine_combination p w) ↔ ∃ (fs : finset s) (w : s → k) (hw : ∑ i in fs, w i = 1), p0 = fs.affine_combination (λ (i : s), p i) w := begin simp_rw [affine_combination_apply, eq_vadd_iff_vsub_eq], exact eq_weighted_vsub_of_point_subset_iff_eq_weighted_vsub_of_point_subtype end end finset namespace finset variables (k : Type*) {V : Type*} {P : Type*} [division_ring k] [add_comm_group V] [module k V] variables [affine_space V P] {ι : Type*} (s : finset ι) {ι₂ : Type*} (s₂ : finset ι₂) /-- The weights for the centroid of some points. -/ def centroid_weights : ι → k := function.const ι (card s : k) ⁻¹ /-- `centroid_weights` at any point. -/ @[simp] lemma centroid_weights_apply (i : ι) : s.centroid_weights k i = (card s : k) ⁻¹ := rfl /-- `centroid_weights` equals a constant function. -/ lemma centroid_weights_eq_const : s.centroid_weights k = function.const ι ((card s : k) ⁻¹) := rfl variables {k} /-- The weights in the centroid sum to 1, if the number of points, converted to `k`, is not zero. -/ lemma sum_centroid_weights_eq_one_of_cast_card_ne_zero (h : (card s : k) ≠ 0) : ∑ i in s, s.centroid_weights k i = 1 := by simp [h] variables (k) /-- In the characteristic zero case, the weights in the centroid sum to 1 if the number of points is not zero. -/ lemma sum_centroid_weights_eq_one_of_card_ne_zero [char_zero k] (h : card s ≠ 0) : ∑ i in s, s.centroid_weights k i = 1 := by simp [h] /-- In the characteristic zero case, the weights in the centroid sum to 1 if the set is nonempty. -/ lemma sum_centroid_weights_eq_one_of_nonempty [char_zero k] (h : s.nonempty) : ∑ i in s, s.centroid_weights k i = 1 := s.sum_centroid_weights_eq_one_of_card_ne_zero k (ne_of_gt (card_pos.2 h)) /-- In the characteristic zero case, the weights in the centroid sum to 1 if the number of points is `n + 1`. -/ lemma sum_centroid_weights_eq_one_of_card_eq_add_one [char_zero k] {n : ℕ} (h : card s = n + 1) : ∑ i in s, s.centroid_weights k i = 1 := s.sum_centroid_weights_eq_one_of_card_ne_zero k (h.symm ▸ nat.succ_ne_zero n) include V /-- The centroid of some points. Although defined for any `s`, this is intended to be used in the case where the number of points, converted to `k`, is not zero. -/ def centroid (p : ι → P) : P := s.affine_combination p (s.centroid_weights k) /-- The definition of the centroid. -/ lemma centroid_def (p : ι → P) : s.centroid k p = s.affine_combination p (s.centroid_weights k) := rfl /-- The centroid of a single point. -/ @[simp] lemma centroid_singleton (p : ι → P) (i : ι) : ({i} : finset ι).centroid k p = p i := by simp [centroid_def, affine_combination_apply] /-- The centroid of two points, expressed directly as adding a vector to a point. -/ lemma centroid_insert_singleton [invertible (2 : k)] (p : ι → P) (i₁ i₂ : ι) : ({i₁, i₂} : finset ι).centroid k p = (2 ⁻¹ : k) • (p i₂ -ᵥ p i₁) +ᵥ p i₁ := begin by_cases h : i₁ = i₂, { simp [h] }, { have hc : (card ({i₁, i₂} : finset ι) : k) ≠ 0, { rw [card_insert_of_not_mem (not_mem_singleton.2 h), card_singleton], norm_num, exact nonzero_of_invertible _ }, rw [centroid_def, affine_combination_eq_weighted_vsub_of_point_vadd_of_sum_eq_one _ _ _ (sum_centroid_weights_eq_one_of_cast_card_ne_zero _ hc) (p i₁)], simp [h], norm_num } end /-- The centroid of two points indexed by `fin 2`, expressed directly as adding a vector to the first point. -/ lemma centroid_insert_singleton_fin [invertible (2 : k)] (p : fin 2 → P) : univ.centroid k p = (2 ⁻¹ : k) • (p 1 -ᵥ p 0) +ᵥ p 0 := begin rw univ_fin2, convert centroid_insert_singleton k p 0 1 end /-- A centroid, over the image of an embedding, equals a centroid with the same points and weights over the original `finset`. -/ lemma centroid_map (e : ι₂ ↪ ι) (p : ι → P) : (s₂.map e).centroid k p = s₂.centroid k (p ∘ e) := by simp [centroid_def, affine_combination_map, centroid_weights] omit V /-- `centroid_weights` gives the weights for the centroid as a constant function, which is suitable when summing over the points whose centroid is being taken. This function gives the weights in a form suitable for summing over a larger set of points, as an indicator function that is zero outside the set whose centroid is being taken. In the case of a `fintype`, the sum may be over `univ`. -/ def centroid_weights_indicator : ι → k := set.indicator ↑s (s.centroid_weights k) /-- The definition of `centroid_weights_indicator`. -/ lemma centroid_weights_indicator_def : s.centroid_weights_indicator k = set.indicator ↑s (s.centroid_weights k) := rfl /-- The sum of the weights for the centroid indexed by a `fintype`. -/ lemma sum_centroid_weights_indicator [fintype ι] : ∑ i, s.centroid_weights_indicator k i = ∑ i in s, s.centroid_weights k i := (set.sum_indicator_subset _ (subset_univ _)).symm /-- In the characteristic zero case, the weights in the centroid indexed by a `fintype` sum to 1 if the number of points is not zero. -/ lemma sum_centroid_weights_indicator_eq_one_of_card_ne_zero [char_zero k] [fintype ι] (h : card s ≠ 0) : ∑ i, s.centroid_weights_indicator k i = 1 := begin rw sum_centroid_weights_indicator, exact s.sum_centroid_weights_eq_one_of_card_ne_zero k h end /-- In the characteristic zero case, the weights in the centroid indexed by a `fintype` sum to 1 if the set is nonempty. -/ lemma sum_centroid_weights_indicator_eq_one_of_nonempty [char_zero k] [fintype ι] (h : s.nonempty) : ∑ i, s.centroid_weights_indicator k i = 1 := begin rw sum_centroid_weights_indicator, exact s.sum_centroid_weights_eq_one_of_nonempty k h end /-- In the characteristic zero case, the weights in the centroid indexed by a `fintype` sum to 1 if the number of points is `n + 1`. -/ lemma sum_centroid_weights_indicator_eq_one_of_card_eq_add_one [char_zero k] [fintype ι] {n : ℕ} (h : card s = n + 1) : ∑ i, s.centroid_weights_indicator k i = 1 := begin rw sum_centroid_weights_indicator, exact s.sum_centroid_weights_eq_one_of_card_eq_add_one k h end include V /-- The centroid as an affine combination over a `fintype`. -/ lemma centroid_eq_affine_combination_fintype [fintype ι] (p : ι → P) : s.centroid k p = univ.affine_combination p (s.centroid_weights_indicator k) := affine_combination_indicator_subset _ _ (subset_univ _) /-- An indexed family of points that is injective on the given `finset` has the same centroid as the image of that `finset`. This is stated in terms of a set equal to the image to provide control of definitional equality for the index type used for the centroid of the image. -/ lemma centroid_eq_centroid_image_of_inj_on {p : ι → P} (hi : ∀ i j ∈ s, p i = p j → i = j) {ps : set P} [fintype ps] (hps : ps = p '' ↑s) : s.centroid k p = (univ : finset ps).centroid k (λ x, x) := begin let f : p '' ↑s → ι := λ x, x.property.some, have hf : ∀ x, f x ∈ s ∧ p (f x) = x := λ x, x.property.some_spec, let f' : ps → ι := λ x, f ⟨x, hps ▸ x.property⟩, have hf' : ∀ x, f' x ∈ s ∧ p (f' x) = x := λ x, hf ⟨x, hps ▸ x.property⟩, have hf'i : function.injective f', { intros x y h, rw [subtype.ext_iff, ←(hf' x).2, ←(hf' y).2, h] }, let f'e : ps ↪ ι := ⟨f', hf'i⟩, have hu : finset.univ.map f'e = s, { ext x, rw mem_map, split, { rintros ⟨i, _, rfl⟩, exact (hf' i).1 }, { intro hx, use [⟨p x, hps.symm ▸ set.mem_image_of_mem _ hx⟩, mem_univ _], refine hi _ _ (hf' _).1 hx _, rw (hf' _).2, refl } }, rw [←hu, centroid_map], congr' with x, change p (f' x) = ↑x, rw (hf' x).2 end /-- Two indexed families of points that are injective on the given `finset`s and with the same points in the image of those `finset`s have the same centroid. -/ lemma centroid_eq_of_inj_on_of_image_eq {p : ι → P} (hi : ∀ i j ∈ s, p i = p j → i = j) {p₂ : ι₂ → P} (hi₂ : ∀ i j ∈ s₂, p₂ i = p₂ j → i = j) (he : p '' ↑s = p₂ '' ↑s₂) : s.centroid k p = s₂.centroid k p₂ := by rw [s.centroid_eq_centroid_image_of_inj_on k hi rfl, s₂.centroid_eq_centroid_image_of_inj_on k hi₂ he] end finset section affine_space' variables {k : Type*} {V : Type*} {P : Type*} [ring k] [add_comm_group V] [module k V] [affine_space V P] variables {ι : Type*} include V /-- A `weighted_vsub` with sum of weights 0 is in the `vector_span` of an indexed family. -/ lemma weighted_vsub_mem_vector_span {s : finset ι} {w : ι → k} (h : ∑ i in s, w i = 0) (p : ι → P) : s.weighted_vsub p w ∈ vector_span k (set.range p) := begin by_cases hn : nonempty ι, { cases hn with i0, rw [vector_span_range_eq_span_range_vsub_right k p i0, ←set.image_univ, finsupp.mem_span_image_iff_total, finset.weighted_vsub_eq_weighted_vsub_of_point_of_sum_eq_zero s w p h (p i0), finset.weighted_vsub_of_point_apply], let w' := set.indicator ↑s w, have hwx : ∀ i, w' i ≠ 0 → i ∈ s := λ i, set.mem_of_indicator_ne_zero, use [finsupp.on_finset s w' hwx, set.subset_univ _], rw [finsupp.total_apply, finsupp.on_finset_sum hwx], { apply finset.sum_congr rfl, intros i hi, simp [w', set.indicator_apply, if_pos hi] }, { exact λ _, zero_smul k _ } }, { simp [finset.eq_empty_of_not_nonempty hn s] } end /-- An `affine_combination` with sum of weights 1 is in the `affine_span` of an indexed family, if the underlying ring is nontrivial. -/ lemma affine_combination_mem_affine_span [nontrivial k] {s : finset ι} {w : ι → k} (h : ∑ i in s, w i = 1) (p : ι → P) : s.affine_combination p w ∈ affine_span k (set.range p) := begin have hnz : ∑ i in s, w i ≠ 0 := h.symm ▸ one_ne_zero, have hn : s.nonempty := finset.nonempty_of_sum_ne_zero hnz, cases hn with i1 hi1, let w1 : ι → k := function.update (function.const ι 0) i1 1, have hw1 : ∑ i in s, w1 i = 1, { rw [finset.sum_update_of_mem hi1, finset.sum_const_zero, add_zero] }, have hw1s : s.affine_combination p w1 = p i1 := s.affine_combination_of_eq_one_of_eq_zero w1 p hi1 (function.update_same _ _ _) (λ _ _ hne, function.update_noteq hne _ _), have hv : s.affine_combination p w -ᵥ p i1 ∈ (affine_span k (set.range p)).direction, { rw [direction_affine_span, ←hw1s, finset.affine_combination_vsub], apply weighted_vsub_mem_vector_span, simp [pi.sub_apply, h, hw1] }, rw ←vsub_vadd (s.affine_combination p w) (p i1), exact affine_subspace.vadd_mem_of_mem_direction hv (mem_affine_span k (set.mem_range_self _)) end variables (k) {V} /-- A vector is in the `vector_span` of an indexed family if and only if it is a `weighted_vsub` with sum of weights 0. -/ lemma mem_vector_span_iff_eq_weighted_vsub {v : V} {p : ι → P} : v ∈ vector_span k (set.range p) ↔ ∃ (s : finset ι) (w : ι → k) (h : ∑ i in s, w i = 0), v = s.weighted_vsub p w := begin split, { by_cases hn : nonempty ι, { cases hn with i0, rw [vector_span_range_eq_span_range_vsub_right k p i0, ←set.image_univ, finsupp.mem_span_image_iff_total], rintros ⟨l, hl, hv⟩, use insert i0 l.support, set w := (l : ι → k) - function.update (function.const ι 0 : ι → k) i0 (∑ i in l.support, l i) with hwdef, use w, have hw : ∑ i in insert i0 l.support, w i = 0, { rw hwdef, simp_rw [pi.sub_apply, finset.sum_sub_distrib, finset.sum_update_of_mem (finset.mem_insert_self _ _), finset.sum_const_zero, finset.sum_insert_of_eq_zero_if_not_mem finsupp.not_mem_support_iff.1, add_zero, sub_self] }, use hw, have hz : w i0 • (p i0 -ᵥ p i0 : V) = 0 := (vsub_self (p i0)).symm ▸ smul_zero _, change (λ i, w i • (p i -ᵥ p i0 : V)) i0 = 0 at hz, rw [finset.weighted_vsub_eq_weighted_vsub_of_point_of_sum_eq_zero _ w p hw (p i0), finset.weighted_vsub_of_point_apply, ←hv, finsupp.total_apply, finset.sum_insert_zero hz], change ∑ i in l.support, l i • _ = _, congr' with i, by_cases h : i = i0, { simp [h] }, { simp [hwdef, h] } }, { rw [set.range_eq_empty.2 hn, vector_span_empty, submodule.mem_bot], intro hv, use [∅], simp [hv] } }, { rintros ⟨s, w, hw, rfl⟩, exact weighted_vsub_mem_vector_span hw p } end variables {k} /-- A point in the `affine_span` of an indexed family is an `affine_combination` with sum of weights 1. -/ lemma eq_affine_combination_of_mem_affine_span {p1 : P} {p : ι → P} (h : p1 ∈ affine_span k (set.range p)) : ∃ (s : finset ι) (w : ι → k) (hw : ∑ i in s, w i = 1), p1 = s.affine_combination p w := begin have hn : ((affine_span k (set.range p)) : set P).nonempty := ⟨p1, h⟩, rw [affine_span_nonempty, set.range_nonempty_iff_nonempty] at hn, cases hn with i0, have h0 : p i0 ∈ affine_span k (set.range p) := mem_affine_span k (set.mem_range_self i0), have hd : p1 -ᵥ p i0 ∈ (affine_span k (set.range p)).direction := affine_subspace.vsub_mem_direction h h0, rw [direction_affine_span, mem_vector_span_iff_eq_weighted_vsub] at hd, rcases hd with ⟨s, w, h, hs⟩, let s' := insert i0 s, let w' := set.indicator ↑s w, have h' : ∑ i in s', w' i = 0, { rw [←h, set.sum_indicator_subset _ (finset.subset_insert i0 s)] }, have hs' : s'.weighted_vsub p w' = p1 -ᵥ p i0, { rw hs, exact (finset.weighted_vsub_indicator_subset _ _ (finset.subset_insert i0 s)).symm }, let w0 : ι → k := function.update (function.const ι 0) i0 1, have hw0 : ∑ i in s', w0 i = 1, { rw [finset.sum_update_of_mem (finset.mem_insert_self _ _), finset.sum_const_zero, add_zero] }, have hw0s : s'.affine_combination p w0 = p i0 := s'.affine_combination_of_eq_one_of_eq_zero w0 p (finset.mem_insert_self _ _) (function.update_same _ _ _) (λ _ _ hne, function.update_noteq hne _ _), use [s', w0 + w'], split, { simp [pi.add_apply, finset.sum_add_distrib, hw0, h'] }, { rw [add_comm, ←finset.weighted_vsub_vadd_affine_combination, hw0s, hs', vsub_vadd] } end variables (k V) /-- A point is in the `affine_span` of an indexed family if and only if it is an `affine_combination` with sum of weights 1, provided the underlying ring is nontrivial. -/ lemma mem_affine_span_iff_eq_affine_combination [nontrivial k] {p1 : P} {p : ι → P} : p1 ∈ affine_span k (set.range p) ↔ ∃ (s : finset ι) (w : ι → k) (hw : ∑ i in s, w i = 1), p1 = s.affine_combination p w := begin split, { exact eq_affine_combination_of_mem_affine_span }, { rintros ⟨s, w, hw, rfl⟩, exact affine_combination_mem_affine_span hw p } end end affine_space' section division_ring variables {k : Type*} {V : Type*} {P : Type*} [division_ring k] [add_comm_group V] [module k V] variables [affine_space V P] {ι : Type*} include V open set finset /-- The centroid lies in the affine span if the number of points, converted to `k`, is not zero. -/ lemma centroid_mem_affine_span_of_cast_card_ne_zero {s : finset ι} (p : ι → P) (h : (card s : k) ≠ 0) : s.centroid k p ∈ affine_span k (range p) := affine_combination_mem_affine_span (s.sum_centroid_weights_eq_one_of_cast_card_ne_zero h) p variables (k) /-- In the characteristic zero case, the centroid lies in the affine span if the number of points is not zero. -/ lemma centroid_mem_affine_span_of_card_ne_zero [char_zero k] {s : finset ι} (p : ι → P) (h : card s ≠ 0) : s.centroid k p ∈ affine_span k (range p) := affine_combination_mem_affine_span (s.sum_centroid_weights_eq_one_of_card_ne_zero k h) p /-- In the characteristic zero case, the centroid lies in the affine span if the set is nonempty. -/ lemma centroid_mem_affine_span_of_nonempty [char_zero k] {s : finset ι} (p : ι → P) (h : s.nonempty) : s.centroid k p ∈ affine_span k (range p) := affine_combination_mem_affine_span (s.sum_centroid_weights_eq_one_of_nonempty k h) p /-- In the characteristic zero case, the centroid lies in the affine span if the number of points is `n + 1`. -/ lemma centroid_mem_affine_span_of_card_eq_add_one [char_zero k] {s : finset ι} (p : ι → P) {n : ℕ} (h : card s = n + 1) : s.centroid k p ∈ affine_span k (range p) := affine_combination_mem_affine_span (s.sum_centroid_weights_eq_one_of_card_eq_add_one k h) p end division_ring namespace affine_map variables {k : Type*} {V : Type*} (P : Type*) [comm_ring k] [add_comm_group V] [module k V] variables [affine_space V P] {ι : Type*} (s : finset ι) include V -- TODO: define `affine_map.proj`, `affine_map.fst`, `affine_map.snd` /-- A weighted sum, as an affine map on the points involved. -/ def weighted_vsub_of_point (w : ι → k) : ((ι → P) × P) →ᵃ[k] V := { to_fun := λ p, s.weighted_vsub_of_point p.fst p.snd w, linear := ∑ i in s, w i • ((linear_map.proj i).comp (linear_map.fst _ _ _) - linear_map.snd _ _ _), map_vadd' := begin rintros ⟨p, b⟩ ⟨v, b'⟩, simp [linear_map.sum_apply, finset.weighted_vsub_of_point, vsub_vadd_eq_vsub_sub, vadd_vsub_assoc, add_sub, ← sub_add_eq_add_sub, smul_add, finset.sum_add_distrib] end } end affine_map
e901c5967b8fc3cf19e08990068216e267114d43
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/category_theory/comma.lean
1ad47dedc4e0e71a8462b2802cfa818fe83722a2
[ "Apache-2.0" ]
permissive
fpvandoorn/mathlib
b21ab4068db079cbb8590b58fda9cc4bc1f35df4
b3433a51ea8bc07c4159c1073838fc0ee9b8f227
refs/heads/master
1,624,791,089,608
1,556,715,231,000
1,556,715,231,000
165,722,980
5
0
Apache-2.0
1,552,657,455,000
1,547,494,646,000
Lean
UTF-8
Lean
false
false
11,430
lean
-- Copyright (c) 2018 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Scott Morrison, Johan Commelin import category_theory.isomorphism import category_theory.punit namespace category_theory universes v₁ v₂ v₃ u₁ u₂ u₃ -- declare the `v`'s first; see `category_theory.category` for an explanation variables {A : Sort u₁} [𝒜 : category.{v₁} A] variables {B : Sort u₂} [ℬ : category.{v₂} B] variables {T : Sort u₃} [𝒯 : category.{v₃} T] include 𝒜 ℬ 𝒯 structure comma (L : A ⥤ T) (R : B ⥤ T) := (left : A . obviously) (right : B . obviously) (hom : L.obj left ⟶ R.obj right) variables {L : A ⥤ T} {R : B ⥤ T} structure comma_morphism (X Y : comma L R) := (left : X.left ⟶ Y.left . obviously) (right : X.right ⟶ Y.right . obviously) (w' : L.map left ≫ Y.hom = X.hom ≫ R.map right . obviously) restate_axiom comma_morphism.w' attribute [simp] comma_morphism.w namespace comma_morphism @[extensionality] lemma ext {X Y : comma L R} {f g : comma_morphism X Y} (l : f.left = g.left) (r : f.right = g.right) : f = g := begin cases f, cases g, congr; assumption end end comma_morphism instance comma_category : category (comma L R) := { hom := comma_morphism, id := λ X, { left := 𝟙 X.left, right := 𝟙 X.right }, comp := λ X Y Z f g, { left := f.left ≫ g.left, right := f.right ≫ g.right, w' := begin rw [functor.map_comp, category.assoc, g.w, ←category.assoc, f.w, functor.map_comp, category.assoc], end }} namespace comma section variables {X Y Z : comma L R} {f : X ⟶ Y} {g : Y ⟶ Z} @[simp] lemma comp_left : (f ≫ g).left = f.left ≫ g.left := rfl @[simp] lemma comp_right : (f ≫ g).right = f.right ≫ g.right := rfl end variables (L) (R) def fst : comma L R ⥤ A := { obj := λ X, X.left, map := λ _ _ f, f.left } def snd : comma L R ⥤ B := { obj := λ X, X.right, map := λ _ _ f, f.right } @[simp] lemma fst_obj {X : comma L R} : (fst L R).obj X = X.left := rfl @[simp] lemma snd_obj {X : comma L R} : (snd L R).obj X = X.right := rfl @[simp] lemma fst_map {X Y : comma L R} {f : X ⟶ Y} : (fst L R).map f = f.left := rfl @[simp] lemma snd_map {X Y : comma L R} {f : X ⟶ Y} : (snd L R).map f = f.right := rfl def nat_trans : fst L R ⋙ L ⟶ snd L R ⋙ R := { app := λ X, X.hom } section variables {L₁ L₂ L₃ : A ⥤ T} {R₁ R₂ R₃ : B ⥤ T} def map_left (l : L₁ ⟶ L₂) : comma L₂ R ⥤ comma L₁ R := { obj := λ X, { left := X.left, right := X.right, hom := l.app X.left ≫ X.hom }, map := λ X Y f, { left := f.left, right := f.right, w' := by tidy; rw [←category.assoc, l.naturality f.left, category.assoc]; tidy } } section variables {X Y : comma L₂ R} {f : X ⟶ Y} {l : L₁ ⟶ L₂} @[simp] lemma map_left_obj_left : ((map_left R l).obj X).left = X.left := rfl @[simp] lemma map_left_obj_right : ((map_left R l).obj X).right = X.right := rfl @[simp] lemma map_left_obj_hom : ((map_left R l).obj X).hom = l.app X.left ≫ X.hom := rfl @[simp] lemma map_left_map_left : ((map_left R l).map f).left = f.left := rfl @[simp] lemma map_left_map_right : ((map_left R l).map f).right = f.right := rfl end def map_left_id : map_left R (𝟙 L) ≅ functor.id _ := { hom := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } }, inv := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } } } section variables {X : comma L R} @[simp] lemma map_left_id_hom_app_left : (((map_left_id L R).hom).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_left_id_hom_app_right : (((map_left_id L R).hom).app X).right = 𝟙 (X.right) := rfl @[simp] lemma map_left_id_inv_app_left : (((map_left_id L R).inv).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_left_id_inv_app_right : (((map_left_id L R).inv).app X).right = 𝟙 (X.right) := rfl end def map_left_comp (l : L₁ ⟶ L₂) (l' : L₂ ⟶ L₃) : (map_left R (l ≫ l')) ≅ (map_left R l') ⋙ (map_left R l) := { hom := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } }, inv := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } } } section variables {X : comma L₃ R} {l : L₁ ⟶ L₂} {l' : L₂ ⟶ L₃} @[simp] lemma map_left_comp_hom_app_left : (((map_left_comp R l l').hom).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_left_comp_hom_app_right : (((map_left_comp R l l').hom).app X).right = 𝟙 (X.right) := rfl @[simp] lemma map_left_comp_inv_app_left : (((map_left_comp R l l').inv).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_left_comp_inv_app_right : (((map_left_comp R l l').inv).app X).right = 𝟙 (X.right) := rfl end def map_right (r : R₁ ⟶ R₂) : comma L R₁ ⥤ comma L R₂ := { obj := λ X, { left := X.left, right := X.right, hom := X.hom ≫ r.app X.right }, map := λ X Y f, { left := f.left, right := f.right, w' := by tidy; rw [←r.naturality f.right, ←category.assoc]; tidy } } section variables {X Y : comma L R₁} {f : X ⟶ Y} {r : R₁ ⟶ R₂} @[simp] lemma map_right_obj_left : ((map_right L r).obj X).left = X.left := rfl @[simp] lemma map_right_obj_right : ((map_right L r).obj X).right = X.right := rfl @[simp] lemma map_right_obj_hom : ((map_right L r).obj X).hom = X.hom ≫ r.app X.right := rfl @[simp] lemma map_right_map_left : ((map_right L r).map f).left = f.left := rfl @[simp] lemma map_right_map_right : ((map_right L r).map f).right = f.right := rfl end def map_right_id : map_right L (𝟙 R) ≅ functor.id _ := { hom := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } }, inv := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } } } section variables {X : comma L R} @[simp] lemma map_right_id_hom_app_left : (((map_right_id L R).hom).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_right_id_hom_app_right : (((map_right_id L R).hom).app X).right = 𝟙 (X.right) := rfl @[simp] lemma map_right_id_inv_app_left : (((map_right_id L R).inv).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_right_id_inv_app_right : (((map_right_id L R).inv).app X).right = 𝟙 (X.right) := rfl end def map_right_comp (r : R₁ ⟶ R₂) (r' : R₂ ⟶ R₃) : (map_right L (r ≫ r')) ≅ (map_right L r) ⋙ (map_right L r') := { hom := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } }, inv := { app := λ X, { left := 𝟙 _, right := 𝟙 _ } } } section variables {X : comma L R₁} {r : R₁ ⟶ R₂} {r' : R₂ ⟶ R₃} @[simp] lemma map_right_comp_hom_app_left : (((map_right_comp L r r').hom).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_right_comp_hom_app_right : (((map_right_comp L r r').hom).app X).right = 𝟙 (X.right) := rfl @[simp] lemma map_right_comp_inv_app_left : (((map_right_comp L r r').inv).app X).left = 𝟙 (X.left) := rfl @[simp] lemma map_right_comp_inv_app_right : (((map_right_comp L r r').inv).app X).right = 𝟙 (X.right) := rfl end end end comma omit 𝒜 ℬ def over (X : T) := comma.{v₃ 1 v₃} (functor.id T) (functor.of.obj X) namespace over variables {X : T} instance category : category (over X) := by delta over; apply_instance @[extensionality] lemma over_morphism.ext {X : T} {U V : over X} {f g : U ⟶ V} (h : f.left = g.left) : f = g := by tidy @[simp] lemma over_right (U : over X) : U.right = punit.star := by tidy @[simp] lemma over_morphism_right {U V : over X} (f : U ⟶ V) : f.right = 𝟙 punit.star := by tidy @[simp] lemma id_left (U : over X) : comma_morphism.left (𝟙 U) = 𝟙 U.left := rfl @[simp] lemma comp_left (a b c : over X) (f : a ⟶ b) (g : b ⟶ c) : (f ≫ g).left = f.left ≫ g.left := rfl @[simp] lemma w {A B : over X} (f : A ⟶ B) : f.left ≫ B.hom = A.hom := by have := f.w; tidy def mk {X Y : T} (f : Y ⟶ X) : over X := { left := Y, hom := f } @[simp] lemma mk_left {X Y : T} (f : Y ⟶ X) : (mk f).left = Y := rfl @[simp] lemma mk_hom {X Y : T} (f : Y ⟶ X) : (mk f).hom = f := rfl def hom_mk {U V : over X} (f : U.left ⟶ V.left) (w : f ≫ V.hom = U.hom . obviously) : U ⟶ V := { left := f } @[simp] lemma hom_mk_left {U V : over X} (f : U.left ⟶ V.left) (w : f ≫ V.hom = U.hom) : (hom_mk f).left = f := rfl def forget : (over X) ⥤ T := comma.fst _ _ @[simp] lemma forget_obj {U : over X} : forget.obj U = U.left := rfl @[simp] lemma forget_map {U V : over X} {f : U ⟶ V} : forget.map f = f.left := rfl def map {Y : T} (f : X ⟶ Y) : over X ⥤ over Y := comma.map_right _ $ functor.of.map f section variables {Y : T} {f : X ⟶ Y} {U V : over X} {g : U ⟶ V} @[simp] lemma map_obj_left : ((map f).obj U).left = U.left := rfl @[simp] lemma map_obj_hom : ((map f).obj U).hom = U.hom ≫ f := rfl @[simp] lemma map_map_left : ((map f).map g).left = g.left := rfl end section variables {D : Sort u₃} [𝒟 : category.{v₃} D] include 𝒟 def post (F : T ⥤ D) : over X ⥤ over (F.obj X) := { obj := λ Y, mk $ F.map Y.hom, map := λ Y₁ Y₂ f, { left := F.map f.left, w' := by tidy; erw [← F.map_comp, w] } } end end over def under (X : T) := comma.{1 v₃ v₃} (functor.of.obj X) (functor.id T) namespace under variables {X : T} instance : category (under X) := by delta under; apply_instance @[extensionality] lemma under_morphism.ext {X : T} {U V : under X} {f g : U ⟶ V} (h : f.right = g.right) : f = g := by tidy @[simp] lemma under_left (U : under X) : U.left = punit.star := by tidy @[simp] lemma under_morphism_left {U V : under X} (f : U ⟶ V) : f.left = 𝟙 punit.star := by tidy @[simp] lemma id_right (U : under X) : comma_morphism.right (𝟙 U) = 𝟙 U.right := rfl @[simp] lemma comp_right (a b c : under X) (f : a ⟶ b) (g : b ⟶ c) : (f ≫ g).right = f.right ≫ g.right := rfl @[simp] lemma w {A B : under X} (f : A ⟶ B) : A.hom ≫ f.right = B.hom := by have := f.w; tidy def mk {X Y : T} (f : X ⟶ Y) : under X := { right := Y, hom := f } @[simp] lemma mk_right {X Y : T} (f : X ⟶ Y) : (mk f).right = Y := rfl @[simp] lemma mk_hom {X Y : T} (f : X ⟶ Y) : (mk f).hom = f := rfl def hom_mk {U V : under X} (f : U.right ⟶ V.right) (w : U.hom ≫ f = V.hom . obviously) : U ⟶ V := { right := f } @[simp] lemma hom_mk_right {U V : under X} (f : U.right ⟶ V.right) (w : U.hom ≫ f = V.hom) : (hom_mk f).right = f := rfl def forget : (under X) ⥤ T := comma.snd _ _ @[simp] lemma forget_obj {U : under X} : forget.obj U = U.right := rfl @[simp] lemma forget_map {U V : under X} {f : U ⟶ V} : forget.map f = f.right := rfl def map {Y : T} (f : X ⟶ Y) : under Y ⥤ under X := comma.map_left _ $ functor.of.map f section variables {Y : T} {f : X ⟶ Y} {U V : under Y} {g : U ⟶ V} @[simp] lemma map_obj_right : ((map f).obj U).right = U.right := rfl @[simp] lemma map_obj_hom : ((map f).obj U).hom = f ≫ U.hom := rfl @[simp] lemma map_map_right : ((map f).map g).right = g.right := rfl end section variables {D : Sort u₃} [𝒟 : category.{v₃} D] include 𝒟 def post {X : T} (F : T ⥤ D) : under X ⥤ under (F.obj X) := { obj := λ Y, mk $ F.map Y.hom, map := λ Y₁ Y₂ f, { right := F.map f.right, w' := by tidy; erw [← F.map_comp, w] } } end end under end category_theory
6d8e902d817e5ca1f49279bbbfa3b3f758b568e6
ddf69e0b8ad10bfd251aa1fb492bd92f064768ec
/src/linear_algebra/basis.lean
52077a56a08e2f18c467fc9bfdb75d2723a5d33f
[ "Apache-2.0" ]
permissive
MaboroshiChan/mathlib
db1c1982df384a2604b19a5e1f5c6464c7c76de1
7f74e6b35f6bac86b9218250e83441ac3e17264c
refs/heads/master
1,671,993,587,476
1,601,911,102,000
1,601,911,102,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
61,310
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, Alexander Bentkamp -/ import linear_algebra.finsupp import linear_algebra.projection import order.zorn import data.fintype.card import data.finset.order /-! # Linear independence and bases This file defines linear independence and bases in a module or vector space. It is inspired by Isabelle/HOL's linear algebra, and hence indirectly by HOL Light. ## Main definitions All definitions are given for families of vectors, i.e. `v : ι → M` where `M` is the module or vector space and `ι : Type*` is an arbitrary indexing type. * `linear_independent R v` states that the elements of the family `v` are linearly independent. * `linear_independent.repr hv x` returns the linear combination representing `x : span R (range v)` on the linearly independent vectors `v`, given `hv : linear_independent R v` (using classical choice). `linear_independent.repr hv` is provided as a linear map. * `is_basis R v` states that the vector family `v` is a basis, i.e. it is linearly independent and spans the entire space. * `is_basis.repr hv x` is the basis version of `linear_independent.repr hv x`. It returns the linear combination representing `x : M` on a basis `v` of `M` (using classical choice). The argument `hv` must be a proof that `is_basis R v`. `is_basis.repr hv` is given as a linear map as well. * `is_basis.constr hv f` constructs a linear map `M₁ →ₗ[R] M₂` given the values `f : ι → M₂` at the basis `v : ι → M₁`, given `hv : is_basis R v`. ## Main statements * `is_basis.ext` states that two linear maps are equal if they coincide on a basis. * `exists_is_basis` states that every vector space has a basis. ## Implementation notes We use families instead of sets because it allows us to say that two identical vectors are linearly dependent. For bases, this is useful as well because we can easily derive ordered bases by using an ordered index type `ι`. If you want to use sets, use the family `(λ x, x : s → M)` given a set `s : set M`. The lemmas `linear_independent.to_subtype_range` and `linear_independent.of_subtype_range` connect those two worlds. ## Tags linearly dependent, linear dependence, linearly independent, linear independence, basis -/ noncomputable theory open function set submodule open_locale classical big_operators universe u variables {ι : Type*} {ι' : Type*} {R : Type*} {K : Type*} {M : Type*} {M' M'' : Type*} {V : Type u} {V' : Type*} section module variables {v : ι → M} variables [ring R] [add_comm_group M] [add_comm_group M'] [add_comm_group M''] variables [module R M] [module R M'] [module R M''] variables {a b : R} {x y : M} variables (R) (v) /-- Linearly independent family of vectors -/ def linear_independent : Prop := (finsupp.total ι M R v).ker = ⊥ variables {R} {v} theorem linear_independent_iff : linear_independent R v ↔ ∀l, finsupp.total ι M R v l = 0 → l = 0 := by simp [linear_independent, linear_map.ker_eq_bot'] theorem linear_independent_iff' : linear_independent R v ↔ ∀ s : finset ι, ∀ g : ι → R, ∑ i in s, g i • v i = 0 → ∀ i ∈ s, g i = 0 := linear_independent_iff.trans ⟨λ hf s g hg i his, have h : _ := hf (∑ i in s, finsupp.single i (g i)) $ by simpa only [linear_map.map_sum, finsupp.total_single] using hg, calc g i = (finsupp.lapply i : (ι →₀ R) →ₗ[R] R) (finsupp.single i (g i)) : by rw [finsupp.lapply_apply, finsupp.single_eq_same] ... = ∑ j in s, (finsupp.lapply i : (ι →₀ R) →ₗ[R] R) (finsupp.single j (g j)) : eq.symm $ finset.sum_eq_single i (λ j hjs hji, by rw [finsupp.lapply_apply, finsupp.single_eq_of_ne hji]) (λ hnis, hnis.elim his) ... = (∑ j in s, finsupp.single j (g j)) i : (finsupp.lapply i : (ι →₀ R) →ₗ[R] R).map_sum.symm ... = 0 : finsupp.ext_iff.1 h i, λ hf l hl, finsupp.ext $ λ i, classical.by_contradiction $ λ hni, hni $ hf _ _ hl _ $ finsupp.mem_support_iff.2 hni⟩ theorem linear_independent_iff'' : linear_independent R v ↔ ∀ (s : finset ι) (g : ι → R) (hg : ∀ i ∉ s, g i = 0), ∑ i in s, g i • v i = 0 → ∀ i, g i = 0 := linear_independent_iff'.trans ⟨λ H s g hg hv i, if his : i ∈ s then H s g hv i his else hg i his, λ H s g hg i hi, by { convert H s (λ j, if j ∈ s then g j else 0) (λ j hj, if_neg hj) (by simp_rw [ite_smul, zero_smul, finset.sum_extend_by_zero, hg]) i, exact (if_pos hi).symm }⟩ theorem linear_dependent_iff : ¬ linear_independent R v ↔ ∃ s : finset ι, ∃ g : ι → R, s.sum (λ i, g i • v i) = 0 ∧ (∃ i ∈ s, g i ≠ 0) := begin rw linear_independent_iff', simp only [exists_prop, not_forall], end lemma linear_independent_empty_type (h : ¬ nonempty ι) : linear_independent R v := begin rw [linear_independent_iff], intros, ext i, exact false.elim (not_nonempty_iff_imp_false.1 h i) end lemma linear_independent.ne_zero [nontrivial R] {i : ι} (hv : linear_independent R v) : v i ≠ 0 := λ h, @zero_ne_one R _ _ $ eq.symm begin suffices : (finsupp.single i 1 : ι →₀ R) i = 0, {simpa}, rw linear_independent_iff.1 hv (finsupp.single i 1), {simp}, {simp [h]} end lemma linear_independent.comp (h : linear_independent R v) (f : ι' → ι) (hf : injective f) : linear_independent R (v ∘ f) := begin rw [linear_independent_iff, finsupp.total_comp], intros l hl, have h_map_domain : ∀ x, (finsupp.map_domain f l) (f x) = 0, by rw linear_independent_iff.1 h (finsupp.map_domain f l) hl; simp, ext, convert h_map_domain a, simp only [finsupp.map_domain_apply hf], end lemma linear_independent_of_zero_eq_one (zero_eq_one : (0 : R) = 1) : linear_independent R v := linear_independent_iff.2 (λ l hl, finsupp.eq_zero_of_zero_eq_one zero_eq_one _) lemma linear_independent.unique (hv : linear_independent R v) {l₁ l₂ : ι →₀ R} : finsupp.total ι M R v l₁ = finsupp.total ι M R v l₂ → l₁ = l₂ := by apply linear_map.ker_eq_bot.1 hv lemma linear_independent.injective [nontrivial R] (hv : linear_independent R v) : injective v := begin intros i j hij, let l : ι →₀ R := finsupp.single i (1 : R) - finsupp.single j 1, have h_total : finsupp.total ι M R v l = 0, { rw finsupp.total_apply, rw finsupp.sum_sub_index, { simp [finsupp.sum_single_index, hij] }, { intros, apply sub_smul } }, have h_single_eq : finsupp.single i (1 : R) = finsupp.single j 1, { rw linear_independent_iff at hv, simp [eq_add_of_sub_eq' (hv l h_total)] }, show i = j, { apply or.elim ((finsupp.single_eq_single_iff _ _ _ _).1 h_single_eq), simp, exact λ h, false.elim (zero_ne_one.symm h.1) } end theorem linear_independent_equiv (e : ι ≃ ι') {f : ι' → M} : linear_independent R (f ∘ e) ↔ linear_independent R f := ⟨λ h, function.comp.right_id f ▸ e.self_comp_symm ▸ h.comp _ e.symm.injective, λ h, h.comp _ e.injective⟩ theorem linear_independent_equiv' (e : ι ≃ ι') {f : ι' → M} {g : ι → M} (h : f ∘ e = g) : linear_independent R g ↔ linear_independent R f := h ▸ linear_independent_equiv e theorem linear_independent_image {ι} {s : set ι} {f : ι → M} (hf : set.inj_on f s) : linear_independent R (λ x : s, f x) ↔ linear_independent R (λ x : f '' s, (x : M)) := linear_independent_equiv' (equiv.set.image_of_inj_on _ _ hf) rfl theorem linear_independent.image' {ι} {s : set ι} {f : ι → M} (hs : linear_independent R (λ x : s, f x)) : linear_independent R (λ x : f '' s, (x : M)) := or.cases_on (subsingleton_or_nontrivial R) (λ hr, linear_independent_of_zero_eq_one $ by exactI subsingleton.elim _ _) (λ hr, by exactI (linear_independent_image $ set.inj_on_iff_injective.2 hs.injective).1 hs) lemma linear_independent_span (hs : linear_independent R v) : @linear_independent ι R (span R (range v)) (λ i : ι, ⟨v i, subset_span (mem_range_self i)⟩) _ _ _ := begin rw linear_independent_iff at *, intros l hl, apply hs l, have := congr_arg (submodule.subtype (span R (range v))) hl, convert this, rw [finsupp.total_apply, finsupp.total_apply], unfold finsupp.sum, rw linear_map.map_sum (submodule.subtype (span R (range v))), simp end lemma linear_independent_of_comp (f : M →ₗ[R] M') (hfv : linear_independent R (f ∘ v)) : linear_independent R v := linear_independent_iff'.2 $ λ s g hg i his, have ∑ (i : ι) in s, g i • f (v i) = 0, by simp_rw [← f.map_smul, ← f.map_sum, hg, f.map_zero], linear_independent_iff'.1 hfv s g this i his section subtype /-! The following lemmas use the subtype defined by a set in `M` as the index set `ι`. -/ theorem linear_independent_comp_subtype {s : set ι} : linear_independent R (v ∘ coe : s → M) ↔ ∀ l ∈ (finsupp.supported R R s), (finsupp.total ι M R v) l = 0 → l = 0 := begin rw [linear_independent_iff, finsupp.total_comp], simp only [linear_map.comp_apply], split, { intros h l hl₁ hl₂, have h_bij : bij_on coe (coe ⁻¹' ↑l.support : set s) ↑l.support, { apply bij_on.mk, { apply maps_to_preimage }, { apply subtype.coe_injective.inj_on }, intros i hi, rw [image_preimage_eq_inter_range, subtype.range_coe], exact ⟨hi, (finsupp.mem_supported _ _).1 hl₁ hi⟩ }, show l = 0, { apply finsupp.eq_zero_of_comap_domain_eq_zero (coe : s → ι) _ h_bij, apply h, convert hl₂, rw [finsupp.lmap_domain_apply, finsupp.map_domain_comap_domain], exact subtype.coe_injective, rw subtype.range_coe, exact (finsupp.mem_supported _ _).1 hl₁ } }, { intros h l hl, have hl' : finsupp.total ι M R v (finsupp.emb_domain ⟨coe, subtype.coe_injective⟩ l) = 0, { rw finsupp.emb_domain_eq_map_domain ⟨coe, subtype.coe_injective⟩ l, apply hl }, apply finsupp.emb_domain_inj.1, rw [h (finsupp.emb_domain ⟨coe, subtype.coe_injective⟩ l) _ hl', finsupp.emb_domain_zero], rw [finsupp.mem_supported, finsupp.support_emb_domain], intros x hx, rw [finset.mem_coe, finset.mem_map] at hx, rcases hx with ⟨i, x', hx'⟩, rw ←hx', simp } end theorem linear_independent_subtype {s : set M} : linear_independent R (λ x, x : s → M) ↔ ∀ l ∈ (finsupp.supported R R s), (finsupp.total M M R id) l = 0 → l = 0 := by apply @linear_independent_comp_subtype _ _ _ id theorem linear_independent_comp_subtype_disjoint {s : set ι} : linear_independent R (v ∘ coe : s → M) ↔ disjoint (finsupp.supported R R s) (finsupp.total ι M R v).ker := by rw [linear_independent_comp_subtype, linear_map.disjoint_ker] theorem linear_independent_subtype_disjoint {s : set M} : linear_independent R (λ x, x : s → M) ↔ disjoint (finsupp.supported R R s) (finsupp.total M M R id).ker := by apply @linear_independent_comp_subtype_disjoint _ _ _ id theorem linear_independent_iff_total_on {s : set M} : linear_independent R (λ x, x : s → M) ↔ (finsupp.total_on M M R id s).ker = ⊥ := by rw [finsupp.total_on, linear_map.ker, linear_map.comap_cod_restrict, map_bot, comap_bot, linear_map.ker_comp, linear_independent_subtype_disjoint, disjoint, ← map_comap_subtype, map_le_iff_le_comap, comap_bot, ker_subtype, le_bot_iff] lemma linear_independent.to_subtype_range (hv : linear_independent R v) : linear_independent R (λ x, x : range v → M) := begin by_cases zero_eq_one : (0 : R) = 1, { apply linear_independent_of_zero_eq_one zero_eq_one }, haveI : nontrivial R := ⟨⟨0, 1, zero_eq_one⟩⟩, rw linear_independent_subtype, intros l hl₁ hl₂, have h_bij : bij_on v (v ⁻¹' ↑l.support) ↑l.support, { apply bij_on.mk, { apply maps_to_preimage }, { apply (linear_independent.injective hv).inj_on }, intros x hx, rcases mem_range.1 (((finsupp.mem_supported _ _).1 hl₁ : ↑(l.support) ⊆ range v) hx) with ⟨i, hi⟩, rw mem_image, use i, rw [mem_preimage, hi], exact ⟨hx, rfl⟩ }, apply finsupp.eq_zero_of_comap_domain_eq_zero v l h_bij, apply linear_independent_iff.1 hv, rw [finsupp.total_comap_domain, finset.sum_preimage_of_bij v l.support h_bij (λ (x : M), l x • x)], rwa [finsupp.total_apply, finsupp.sum] at hl₂ end lemma linear_independent.of_subtype_range (hv : injective v) (h : linear_independent R (λ x, x : range v → M)) : linear_independent R v := begin rw linear_independent_iff, intros l hl, apply finsupp.map_domain_injective hv, apply linear_independent_subtype.1 h (l.map_domain v), { rw finsupp.mem_supported, intros x hx, have := finset.mem_coe.2 (finsupp.map_domain_support hx), rw finset.coe_image at this, apply set.image_subset_range _ _ this, }, { rwa [finsupp.total_map_domain _ _ hv, left_id] } end lemma linear_independent.restrict_of_comp_subtype {s : set ι} (hs : linear_independent R (v ∘ coe : s → M)) : linear_independent R (s.restrict v) := begin have h_restrict : restrict v s = v ∘ coe := rfl, rw [linear_independent_iff, h_restrict, finsupp.total_comp], intros l hl, have h_map_domain_subtype_eq_0 : l.map_domain coe = 0, { rw linear_independent_comp_subtype at hs, apply hs (finsupp.lmap_domain R R coe l) _ hl, rw finsupp.mem_supported, simp, intros x hx, have := finset.mem_coe.2 (finsupp.map_domain_support (finset.mem_coe.1 hx)), rw finset.coe_image at this, exact subtype.coe_image_subset _ _ this }, apply @finsupp.map_domain_injective _ (subtype s) ι, { apply subtype.coe_injective }, { simpa }, end variables (R M) lemma linear_independent_empty : linear_independent R (λ x, x : (∅ : set M) → M) := by simp [linear_independent_subtype_disjoint] variables {R M} lemma linear_independent.mono {t s : set M} (h : t ⊆ s) : linear_independent R (λ x, x : s → M) → linear_independent R (λ x, x : t → M) := begin simp only [linear_independent_subtype_disjoint], exact (disjoint.mono_left (finsupp.supported_mono h)) end lemma linear_independent.union {s t : set M} (hs : linear_independent R (λ x, x : s → M)) (ht : linear_independent R (λ x, x : t → M)) (hst : disjoint (span R s) (span R t)) : linear_independent R (λ x, x : (s ∪ t) → M) := begin rw [linear_independent_subtype_disjoint, disjoint_def, finsupp.supported_union], intros l h₁ h₂, rw mem_sup at h₁, rcases h₁ with ⟨ls, hls, lt, hlt, rfl⟩, have h_ls_mem_t : finsupp.total M M R id ls ∈ span R t, { rw [← image_id t, finsupp.span_eq_map_total], apply (add_mem_iff_left (map _ _) (mem_image_of_mem _ hlt)).1, rw [← linear_map.map_add, linear_map.mem_ker.1 h₂], apply zero_mem }, have h_lt_mem_s : finsupp.total M M R id lt ∈ span R s, { rw [← image_id s, finsupp.span_eq_map_total], apply (add_mem_iff_left (map _ _) (mem_image_of_mem _ hls)).1, rw [← linear_map.map_add, add_comm, linear_map.mem_ker.1 h₂], apply zero_mem }, have h_ls_mem_s : (finsupp.total M M R id) ls ∈ span R s, { rw ← image_id s, apply (finsupp.mem_span_iff_total _).2 ⟨ls, hls, rfl⟩ }, have h_lt_mem_t : (finsupp.total M M R id) lt ∈ span R t, { rw ← image_id t, apply (finsupp.mem_span_iff_total _).2 ⟨lt, hlt, rfl⟩ }, have h_ls_0 : ls = 0 := disjoint_def.1 (linear_independent_subtype_disjoint.1 hs) _ hls (linear_map.mem_ker.2 $ disjoint_def.1 hst (finsupp.total M M R id ls) h_ls_mem_s h_ls_mem_t), have h_lt_0 : lt = 0 := disjoint_def.1 (linear_independent_subtype_disjoint.1 ht) _ hlt (linear_map.mem_ker.2 $ disjoint_def.1 hst (finsupp.total M M R id lt) h_lt_mem_s h_lt_mem_t), show ls + lt = 0, by simp [h_ls_0, h_lt_0], end lemma linear_independent_of_finite (s : set M) (H : ∀ t ⊆ s, finite t → linear_independent R (λ x, x : t → M)) : linear_independent R (λ x, x : s → M) := linear_independent_subtype.2 $ λ l hl, linear_independent_subtype.1 (H _ hl (finset.finite_to_set _)) l (subset.refl _) lemma linear_independent_Union_of_directed {η : Type*} {s : η → set M} (hs : directed (⊆) s) (h : ∀ i, linear_independent R (λ x, x : s i → M)) : linear_independent R (λ x, x : (⋃ i, s i) → M) := begin by_cases hη : nonempty η, { resetI, refine linear_independent_of_finite (⋃ i, s i) (λ t ht ft, _), rcases finite_subset_Union ft ht with ⟨I, fi, hI⟩, rcases hs.finset_le fi.to_finset with ⟨i, hi⟩, exact (h i).mono (subset.trans hI $ bUnion_subset $ λ j hj, hi j (finite.mem_to_finset.2 hj)) }, { refine (linear_independent_empty _ _).mono _, rintro _ ⟨_, ⟨i, _⟩, _⟩, exact hη ⟨i⟩ } end lemma linear_independent_sUnion_of_directed {s : set (set M)} (hs : directed_on (⊆) s) (h : ∀ a ∈ s, linear_independent R (λ x, x : (a : set M) → M)) : linear_independent R (λ x, x : (⋃₀ s) → M) := by rw sUnion_eq_Union; exact linear_independent_Union_of_directed hs.directed_coe (by simpa using h) lemma linear_independent_bUnion_of_directed {η} {s : set η} {t : η → set M} (hs : directed_on (t ⁻¹'o (⊆)) s) (h : ∀a∈s, linear_independent R (λ x, x : t a → M)) : linear_independent R (λ x, x : (⋃a∈s, t a) → M) := by rw bUnion_eq_Union; exact linear_independent_Union_of_directed (directed_comp.2 $ hs.directed_coe) (by simpa using h) lemma linear_independent_Union_finite_subtype {ι : Type*} {f : ι → set M} (hl : ∀i, linear_independent R (λ x, x : f i → M)) (hd : ∀i, ∀t:set ι, finite t → i ∉ t → disjoint (span R (f i)) (⨆i∈t, span R (f i))) : linear_independent R (λ x, x : (⋃i, f i) → M) := begin rw [Union_eq_Union_finset f], apply linear_independent_Union_of_directed, apply directed_of_sup, exact (assume t₁ t₂ ht, Union_subset_Union $ assume i, Union_subset_Union_const $ assume h, ht h), assume t, rw [set.Union, ← finset.sup_eq_supr], refine t.induction_on _ _, { rw finset.sup_empty, apply linear_independent_empty_type (not_nonempty_iff_imp_false.2 _), exact λ x, set.not_mem_empty x (subtype.mem x) }, { rintros i s his ih, rw [finset.sup_insert], refine (hl _).union ih _, rw [finset.sup_eq_supr], refine (hd i _ _ his).mono_right _, { simp only [(span_Union _).symm], refine span_mono (@supr_le_supr2 (set M) _ _ _ _ _ _), rintros i, exact ⟨i, le_refl _⟩ }, { exact s.finite_to_set } } end lemma linear_independent_Union_finite {η : Type*} {ιs : η → Type*} {f : Π j : η, ιs j → M} (hindep : ∀j, linear_independent R (f j)) (hd : ∀i, ∀t:set η, finite t → i ∉ t → disjoint (span R (range (f i))) (⨆i∈t, span R (range (f i)))) : linear_independent R (λ ji : Σ j, ιs j, f ji.1 ji.2) := begin by_cases zero_eq_one : (0 : R) = 1, { apply linear_independent_of_zero_eq_one zero_eq_one }, haveI : nontrivial R := ⟨⟨0, 1, zero_eq_one⟩⟩, apply linear_independent.of_subtype_range, { rintros ⟨x₁, x₂⟩ ⟨y₁, y₂⟩ hxy, by_cases h_cases : x₁ = y₁, subst h_cases, { apply sigma.eq, rw linear_independent.injective (hindep _) hxy, refl }, { have h0 : f x₁ x₂ = 0, { apply disjoint_def.1 (hd x₁ {y₁} (finite_singleton y₁) (λ h, h_cases (eq_of_mem_singleton h))) (f x₁ x₂) (subset_span (mem_range_self _)), rw supr_singleton, simp only at hxy, rw hxy, exact (subset_span (mem_range_self y₂)) }, exact false.elim ((hindep x₁).ne_zero h0) } }, rw range_sigma_eq_Union_range, apply linear_independent_Union_finite_subtype (λ j, (hindep j).to_subtype_range) hd, end end subtype section repr variables (hv : linear_independent R v) /-- Canonical isomorphism between linear combinations and the span of linearly independent vectors. -/ def linear_independent.total_equiv (hv : linear_independent R v) : (ι →₀ R) ≃ₗ[R] span R (range v) := begin apply linear_equiv.of_bijective (linear_map.cod_restrict (span R (range v)) (finsupp.total ι M R v) _), { rw linear_map.ker_cod_restrict, apply hv }, { rw [linear_map.range, linear_map.map_cod_restrict, ← linear_map.range_le_iff_comap, range_subtype, map_top], rw finsupp.range_total, apply le_refl (span R (range v)) }, { intro l, rw ← finsupp.range_total, rw linear_map.mem_range, apply mem_range_self l } end /-- Linear combination representing a vector in the span of linearly independent vectors. Given a family of linearly independent vectors, we can represent any vector in their span as a linear combination of these vectors. These are provided by this linear map. It is simply one direction of `linear_independent.total_equiv`. -/ def linear_independent.repr (hv : linear_independent R v) : span R (range v) →ₗ[R] ι →₀ R := hv.total_equiv.symm lemma linear_independent.total_repr (x) : finsupp.total ι M R v (hv.repr x) = x := subtype.ext_iff.1 (linear_equiv.apply_symm_apply hv.total_equiv x) lemma linear_independent.total_comp_repr : (finsupp.total ι M R v).comp hv.repr = submodule.subtype _ := linear_map.ext $ hv.total_repr lemma linear_independent.repr_ker : hv.repr.ker = ⊥ := by rw [linear_independent.repr, linear_equiv.ker] lemma linear_independent.repr_range : hv.repr.range = ⊤ := by rw [linear_independent.repr, linear_equiv.range] lemma linear_independent.repr_eq {l : ι →₀ R} {x} (eq : finsupp.total ι M R v l = ↑x) : hv.repr x = l := begin have : ↑((linear_independent.total_equiv hv : (ι →₀ R) →ₗ[R] span R (range v)) l) = finsupp.total ι M R v l := rfl, have : (linear_independent.total_equiv hv : (ι →₀ R) →ₗ[R] span R (range v)) l = x, { rw eq at this, exact subtype.ext_iff.2 this }, rw ←linear_equiv.symm_apply_apply hv.total_equiv l, rw ←this, refl, end lemma linear_independent.repr_eq_single (i) (x) (hx : ↑x = v i) : hv.repr x = finsupp.single i 1 := begin apply hv.repr_eq, simp [finsupp.total_single, hx] end -- TODO: why is this so slow? lemma linear_independent_iff_not_smul_mem_span : linear_independent R v ↔ (∀ (i : ι) (a : R), a • (v i) ∈ span R (v '' (univ \ {i})) → a = 0) := ⟨ λ hv i a ha, begin rw [finsupp.span_eq_map_total, mem_map] at ha, rcases ha with ⟨l, hl, e⟩, rw sub_eq_zero.1 (linear_independent_iff.1 hv (l - finsupp.single i a) (by simp [e])) at hl, by_contra hn, exact (not_mem_of_mem_diff (hl $ by simp [hn])) (mem_singleton _), end, λ H, linear_independent_iff.2 $ λ l hl, begin ext i, simp only [finsupp.zero_apply], by_contra hn, refine hn (H i _ _), refine (finsupp.mem_span_iff_total _).2 ⟨finsupp.single i (l i) - l, _, _⟩, { rw finsupp.mem_supported', intros j hj, have hij : j = i := not_not.1 (λ hij : j ≠ i, hj ((mem_diff _).2 ⟨mem_univ _, λ h, hij (eq_of_mem_singleton h)⟩)), simp [hij] }, { simp [hl] } end⟩ end repr lemma surjective_of_linear_independent_of_span [nontrivial R] (hv : linear_independent R v) (f : ι' ↪ ι) (hss : range v ⊆ span R (range (v ∘ f))) : surjective f := begin intros i, let repr : (span R (range (v ∘ f)) : Type*) → ι' →₀ R := (hv.comp f f.injective).repr, let l := (repr ⟨v i, hss (mem_range_self i)⟩).map_domain f, have h_total_l : finsupp.total ι M R v l = v i, { dsimp only [l], rw finsupp.total_map_domain, rw (hv.comp f f.injective).total_repr, { refl }, { exact f.injective } }, have h_total_eq : (finsupp.total ι M R v) l = (finsupp.total ι M R v) (finsupp.single i 1), by rw [h_total_l, finsupp.total_single, one_smul], have l_eq : l = _ := linear_map.ker_eq_bot.1 hv h_total_eq, dsimp only [l] at l_eq, rw ←finsupp.emb_domain_eq_map_domain at l_eq, rcases finsupp.single_of_emb_domain_single (repr ⟨v i, _⟩) f i (1 : R) zero_ne_one.symm l_eq with ⟨i', hi'⟩, use i', exact hi'.2 end lemma eq_of_linear_independent_of_span_subtype [nontrivial R] {s t : set M} (hs : linear_independent R (λ x, x : s → M)) (h : t ⊆ s) (hst : s ⊆ span R t) : s = t := begin let f : t ↪ s := ⟨λ x, ⟨x.1, h x.2⟩, λ a b hab, subtype.coe_injective (subtype.mk.inj hab)⟩, have h_surj : surjective f, { apply surjective_of_linear_independent_of_span hs f _, convert hst; simp [f, comp], }, show s = t, { apply subset.antisymm _ h, intros x hx, rcases h_surj ⟨x, hx⟩ with ⟨y, hy⟩, convert y.mem, rw ← subtype.mk.inj hy, refl } end open linear_map lemma linear_independent.image (hv : linear_independent R v) {f : M →ₗ M'} (hf_inj : disjoint (span R (range v)) f.ker) : linear_independent R (f ∘ v) := begin rw [disjoint, ← set.image_univ, finsupp.span_eq_map_total, map_inf_eq_map_inf_comap, map_le_iff_le_comap, comap_bot, finsupp.supported_univ, top_inf_eq] at hf_inj, unfold linear_independent at hv, rw hv at hf_inj, haveI : inhabited M := ⟨0⟩, rw [linear_independent, finsupp.total_comp], rw [@finsupp.lmap_domain_total _ _ R _ _ _ _ _ _ _ _ _ _ f, ker_comp, eq_bot_iff], apply hf_inj, exact λ _, rfl, end lemma linear_independent.image_subtype {s : set M} {f : M →ₗ M'} (hs : linear_independent R (λ x, x : s → M)) (hf_inj : disjoint (span R s) f.ker) : linear_independent R (λ x, x : f '' s → M') := begin rw [disjoint, ← set.image_id s, finsupp.span_eq_map_total, map_inf_eq_map_inf_comap, map_le_iff_le_comap, comap_bot] at hf_inj, haveI : inhabited M := ⟨0⟩, rw [linear_independent_subtype_disjoint, disjoint, ← finsupp.lmap_domain_supported _ _ f, map_inf_eq_map_inf_comap, map_le_iff_le_comap, ← ker_comp], rw [@finsupp.lmap_domain_total _ _ R _ _ _, ker_comp], { exact le_trans (le_inf inf_le_left hf_inj) (le_trans (linear_independent_subtype_disjoint.1 hs) bot_le) }, { simp } end lemma linear_independent.inl_union_inr {s : set M} {t : set M'} (hs : linear_independent R (λ x, x : s → M)) (ht : linear_independent R (λ x, x : t → M')) : linear_independent R (λ x, x : inl R M M' '' s ∪ inr R M M' '' t → M × M') := begin refine (hs.image_subtype _).union (ht.image_subtype _) _; [simp, simp, skip], simp only [span_image], simp [disjoint_iff, prod_inf_prod] end lemma linear_independent_inl_union_inr' {v : ι → M} {v' : ι' → M'} (hv : linear_independent R v) (hv' : linear_independent R v') : linear_independent R (sum.elim (inl R M M' ∘ v) (inr R M M' ∘ v')) := begin by_cases zero_eq_one : (0 : R) = 1, { apply linear_independent_of_zero_eq_one zero_eq_one }, haveI : nontrivial R := ⟨⟨0, 1, zero_eq_one⟩⟩, have inj_v : injective v := (linear_independent.injective hv), have inj_v' : injective v' := (linear_independent.injective hv'), apply linear_independent.of_subtype_range, { apply sum.elim_injective, { exact inl_injective.comp inj_v }, { exact inr_injective.comp inj_v' }, { intros, simp [hv.ne_zero] } }, { rw sum.elim_range, refine (hv.image _).to_subtype_range.union (hv'.image _).to_subtype_range _; [simp, simp, skip], apply disjoint_inl_inr.mono _ _; simp only [set.range_comp, span_image, linear_map.map_le_range] } end /-- Dedekind's linear independence of characters -/ -- See, for example, Keith Conrad's note <https://kconrad.math.uconn.edu/blurbs/galoistheory/linearchar.pdf> theorem linear_independent_monoid_hom (G : Type*) [monoid G] (L : Type*) [integral_domain L] : @linear_independent _ L (G → L) (λ f, f : (G →* L) → (G → L)) _ _ _ := by letI := classical.dec_eq (G →* L); letI : mul_action L L := distrib_mul_action.to_mul_action; -- We prove linear independence by showing that only the trivial linear combination vanishes. exact linear_independent_iff'.2 -- To do this, we use `finset` induction, (λ s, finset.induction_on s (λ g hg i, false.elim) $ λ a s has ih g hg, -- Here -- * `a` is a new character we will insert into the `finset` of characters `s`, -- * `ih` is the fact that only the trivial linear combination of characters in `s` is zero -- * `hg` is the fact that `g` are the coefficients of a linear combination summing to zero -- and it remains to prove that `g` vanishes on `insert a s`. -- We now make the key calculation: -- For any character `i` in the original `finset`, we have `g i • i = g i • a` as functions on the monoid `G`. have h1 : ∀ i ∈ s, (g i • i : G → L) = g i • a, from λ i his, funext $ λ x : G, -- We prove these expressions are equal by showing -- the differences of their values on each monoid element `x` is zero eq_of_sub_eq_zero $ ih (λ j, g j * j x - g j * a x) (funext $ λ y : G, calc -- After that, it's just a chase scene. (∑ i in s, ((g i * i x - g i * a x) • i : G → L)) y = ∑ i in s, (g i * i x - g i * a x) * i y : finset.sum_apply _ _ _ ... = ∑ i in s, (g i * i x * i y - g i * a x * i y) : finset.sum_congr rfl (λ _ _, sub_mul _ _ _) ... = ∑ i in s, g i * i x * i y - ∑ i in s, g i * a x * i y : finset.sum_sub_distrib ... = (g a * a x * a y + ∑ i in s, g i * i x * i y) - (g a * a x * a y + ∑ i in s, g i * a x * i y) : by rw add_sub_add_left_eq_sub ... = ∑ i in insert a s, g i * i x * i y - ∑ i in insert a s, g i * a x * i y : by rw [finset.sum_insert has, finset.sum_insert has] ... = ∑ i in insert a s, g i * i (x * y) - ∑ i in insert a s, a x * (g i * i y) : congr (congr_arg has_sub.sub (finset.sum_congr rfl $ λ i _, by rw [i.map_mul, mul_assoc])) (finset.sum_congr rfl $ λ _ _, by rw [mul_assoc, mul_left_comm]) ... = (∑ i in insert a s, (g i • i : G → L)) (x * y) - a x * (∑ i in insert a s, (g i • i : G → L)) y : by rw [finset.sum_apply, finset.sum_apply, finset.mul_sum]; refl ... = 0 - a x * 0 : by rw hg; refl ... = 0 : by rw [mul_zero, sub_zero]) i his, -- On the other hand, since `a` is not already in `s`, for any character `i ∈ s` -- there is some element of the monoid on which it differs from `a`. have h2 : ∀ i : G →* L, i ∈ s → ∃ y, i y ≠ a y, from λ i his, classical.by_contradiction $ λ h, have hia : i = a, from monoid_hom.ext $ λ y, classical.by_contradiction $ λ hy, h ⟨y, hy⟩, has $ hia ▸ his, -- From these two facts we deduce that `g` actually vanishes on `s`, have h3 : ∀ i ∈ s, g i = 0, from λ i his, let ⟨y, hy⟩ := h2 i his in have h : g i • i y = g i • a y, from congr_fun (h1 i his) y, or.resolve_right (mul_eq_zero.1 $ by rw [mul_sub, sub_eq_zero]; exact h) (sub_ne_zero_of_ne hy), -- And so, using the fact that the linear combination over `s` and over `insert a s` both vanish, -- we deduce that `g a = 0`. have h4 : g a = 0, from calc g a = g a * 1 : (mul_one _).symm ... = (g a • a : G → L) 1 : by rw ← a.map_one; refl ... = (∑ i in insert a s, (g i • i : G → L)) 1 : begin rw finset.sum_eq_single a, { intros i his hia, rw finset.mem_insert at his, rw [h3 i (his.resolve_left hia), zero_smul] }, { intros haas, exfalso, apply haas, exact finset.mem_insert_self a s } end ... = 0 : by rw hg; refl, -- Now we're done; the last two facts together imply that `g` vanishes on every element of `insert a s`. (finset.forall_mem_insert _ _ _).2 ⟨h4, h3⟩) lemma le_of_span_le_span [nontrivial R] {s t u: set M} (hl : linear_independent R (coe : u → M )) (hsu : s ⊆ u) (htu : t ⊆ u) (hst : span R s ≤ span R t) : s ⊆ t := begin have := eq_of_linear_independent_of_span_subtype (hl.mono (set.union_subset hsu htu)) (set.subset_union_right _ _) (set.union_subset (set.subset.trans subset_span hst) subset_span), rw ← this, apply set.subset_union_left end lemma span_le_span_iff [nontrivial R] {s t u: set M} (hl : linear_independent R (coe : u → M)) (hsu : s ⊆ u) (htu : t ⊆ u) : span R s ≤ span R t ↔ s ⊆ t := ⟨le_of_span_le_span hl hsu htu, span_mono⟩ variables (R) (v) /-- A family of vectors is a basis if it is linearly independent and all vectors are in the span. -/ def is_basis := linear_independent R v ∧ span R (range v) = ⊤ variables {R} {v} section is_basis variables {s t : set M} (hv : is_basis R v) lemma is_basis.mem_span (hv : is_basis R v) : ∀ x, x ∈ span R (range v) := eq_top_iff'.1 hv.2 lemma is_basis.comp (hv : is_basis R v) (f : ι' → ι) (hf : bijective f) : is_basis R (v ∘ f) := begin split, { apply hv.1.comp f hf.1 }, { rw[set.range_comp, range_iff_surjective.2 hf.2, image_univ, hv.2] } end lemma is_basis.injective [nontrivial R] (hv : is_basis R v) : injective v := λ x y h, linear_independent.injective hv.1 h lemma is_basis.range (hv : is_basis R v) : is_basis R (λ x, x : range v → M) := ⟨hv.1.to_subtype_range, by { convert hv.2, ext i, exact ⟨λ ⟨p, hp⟩, hp ▸ p.2, λ hi, ⟨⟨i, hi⟩, rfl⟩⟩ }⟩ /-- Given a basis, any vector can be written as a linear combination of the basis vectors. They are given by this linear map. This is one direction of `module_equiv_finsupp`. -/ def is_basis.repr : M →ₗ (ι →₀ R) := (hv.1.repr).comp (linear_map.id.cod_restrict _ hv.mem_span) lemma is_basis.total_repr (x) : finsupp.total ι M R v (hv.repr x) = x := hv.1.total_repr ⟨x, _⟩ lemma is_basis.total_comp_repr : (finsupp.total ι M R v).comp hv.repr = linear_map.id := linear_map.ext hv.total_repr lemma is_basis.ext {f g : M →ₗ[R] M'} (hv : is_basis R v) (h : ∀i, f (v i) = g (v i)) : f = g := linear_map.ext_on hv.2 h lemma is_basis.repr_ker : hv.repr.ker = ⊥ := linear_map.ker_eq_bot.2 $ left_inverse.injective hv.total_repr lemma is_basis.repr_range : hv.repr.range = finsupp.supported R R univ := by rw [is_basis.repr, linear_map.range, submodule.map_comp, linear_map.map_cod_restrict, submodule.map_id, comap_top, map_top, hv.1.repr_range, finsupp.supported_univ] lemma is_basis.repr_total (x : ι →₀ R) (hx : x ∈ finsupp.supported R R (univ : set ι)) : hv.repr (finsupp.total ι M R v x) = x := begin rw [← hv.repr_range, linear_map.mem_range] at hx, cases hx with w hw, rw [← hw, hv.total_repr], end lemma is_basis.repr_eq_single {i} : hv.repr (v i) = finsupp.single i 1 := by apply hv.1.repr_eq_single; simp @[simp] lemma is_basis.repr_self_apply (i j : ι) : hv.repr (v i) j = if i = j then 1 else 0 := by rw [hv.repr_eq_single, finsupp.single_apply] lemma is_basis.repr_eq_iff {f : M →ₗ[R] (ι →₀ R)} : hv.repr = f ↔ ∀ i, f (v i) = finsupp.single i 1 := begin split, { rintros rfl i, exact hv.repr_eq_single }, intro h, refine hv.ext (λ _, _), rw [h, hv.repr_eq_single] end lemma is_basis.repr_apply_eq {f : M → ι → R} (hadd : ∀ x y, f (x + y) = f x + f y) (hsmul : ∀ (c : R) (x : M), f (c • x) = c • f x) (f_eq : ∀ i, f (v i) = finsupp.single i 1) (x : M) (i : ι) : hv.repr x i = f x i := begin let f_i : M →ₗ[R] R := { to_fun := λ x, f x i, map_add' := λ _ _, by rw [hadd, pi.add_apply], map_smul' := λ _ _, by rw [hsmul, pi.smul_apply] }, show (finsupp.leval i).comp hv.repr x = f_i x, congr' 1, refine hv.ext (λ j, _), show hv.repr (v j) i = f (v j) i, rw [hv.repr_eq_single, f_eq] end lemma is_basis.range_repr_self (i : ι) : hv.range.repr (v i) = finsupp.single ⟨v i, mem_range_self i⟩ 1 := hv.1.to_subtype_range.repr_eq_single _ _ rfl @[simp] lemma is_basis.range_repr (i : ι) : hv.range.repr x ⟨v i, mem_range_self i⟩ = hv.repr x i := begin by_cases H : (0 : R) = 1, { exact eq_of_zero_eq_one H _ _ }, refine (hv.repr_apply_eq _ _ _ x i).symm, { intros x y, ext j, rw [linear_map.map_add, finsupp.add_apply], refl }, { intros c x, ext j, rw [linear_map.map_smul, finsupp.smul_apply], refl }, { intro i, ext j, haveI : nontrivial R := ⟨⟨0, 1, H⟩⟩, simp [hv.range_repr_self, finsupp.single_apply, hv.injective] } end /-- Construct a linear map given the value at the basis. -/ def is_basis.constr (f : ι → M') : M →ₗ[R] M' := (finsupp.total M' M' R id).comp $ (finsupp.lmap_domain R R f).comp hv.repr theorem is_basis.constr_apply (f : ι → M') (x : M) : (hv.constr f : M → M') x = (hv.repr x).sum (λb a, a • f b) := by dsimp [is_basis.constr] ; rw [finsupp.total_apply, finsupp.sum_map_domain_index]; simp [add_smul] @[simp] lemma constr_basis {f : ι → M'} {i : ι} (hv : is_basis R v) : (hv.constr f : M → M') (v i) = f i := by simp [is_basis.constr_apply, hv.repr_eq_single, finsupp.sum_single_index] lemma constr_eq {g : ι → M'} {f : M →ₗ[R] M'} (hv : is_basis R v) (h : ∀i, g i = f (v i)) : hv.constr g = f := hv.ext $ λ i, (constr_basis hv).trans (h i) lemma constr_self (f : M →ₗ[R] M') : hv.constr (λ i, f (v i)) = f := constr_eq hv $ λ x, rfl lemma constr_zero (hv : is_basis R v) : hv.constr (λi, (0 : M')) = 0 := constr_eq hv $ λ x, rfl lemma constr_add {g f : ι → M'} (hv : is_basis R v) : hv.constr (λi, f i + g i) = hv.constr f + hv.constr g := constr_eq hv $ λ b, by simp lemma constr_neg {f : ι → M'} (hv : is_basis R v) : hv.constr (λi, - f i) = - hv.constr f := constr_eq hv $ λ b, by simp lemma constr_sub {g f : ι → M'} (hs : is_basis R v) : hv.constr (λi, f i - g i) = hs.constr f - hs.constr g := by simp [sub_eq_add_neg, constr_add, constr_neg] -- this only works on functions if `R` is a commutative ring lemma constr_smul {ι R M} [comm_ring R] [add_comm_group M] [module R M] {v : ι → R} {f : ι → M} {a : R} (hv : is_basis R v) : hv.constr (λb, a • f b) = a • hv.constr f := constr_eq hv $ by simp [constr_basis hv] {contextual := tt} lemma constr_range [nonempty ι] (hv : is_basis R v) {f : ι → M'} : (hv.constr f).range = span R (range f) := by rw [is_basis.constr, linear_map.range_comp, linear_map.range_comp, is_basis.repr_range, finsupp.lmap_domain_supported, ←set.image_univ, ←finsupp.span_eq_map_total, image_id] /-- Canonical equivalence between a module and the linear combinations of basis vectors. -/ def module_equiv_finsupp (hv : is_basis R v) : M ≃ₗ[R] ι →₀ R := (hv.1.total_equiv.trans (linear_equiv.of_top _ hv.2)).symm @[simp] theorem module_equiv_finsupp_apply_basis (hv : is_basis R v) (i : ι) : module_equiv_finsupp hv (v i) = finsupp.single i 1 := (linear_equiv.symm_apply_eq _).2 $ by simp [linear_independent.total_equiv] /-- Isomorphism between the two modules, given two modules `M` and `M'` with respective bases `v` and `v'` and a bijection between the indexing sets of the two bases. -/ def equiv_of_is_basis {v : ι → M} {v' : ι' → M'} (hv : is_basis R v) (hv' : is_basis R v') (e : ι ≃ ι') : M ≃ₗ[R] M' := { inv_fun := hv'.constr (v ∘ e.symm), left_inv := have (hv'.constr (v ∘ e.symm)).comp (hv.constr (v' ∘ e)) = linear_map.id, from hv.ext $ by simp, λ x, congr_arg (λ h : M →ₗ[R] M, h x) this, right_inv := have (hv.constr (v' ∘ e)).comp (hv'.constr (v ∘ e.symm)) = linear_map.id, from hv'.ext $ by simp, λ y, congr_arg (λ h : M' →ₗ[R] M', h y) this, ..hv.constr (v' ∘ e) } /-- Isomorphism between the two modules, given two modules `M` and `M'` with respective bases `v` and `v'` and a bijection between the two bases. -/ def equiv_of_is_basis' {v : ι → M} {v' : ι' → M'} (f : M → M') (g : M' → M) (hv : is_basis R v) (hv' : is_basis R v') (hf : ∀i, f (v i) ∈ range v') (hg : ∀i, g (v' i) ∈ range v) (hgf : ∀i, g (f (v i)) = v i) (hfg : ∀i, f (g (v' i)) = v' i) : M ≃ₗ M' := { inv_fun := hv'.constr (g ∘ v'), left_inv := have (hv'.constr (g ∘ v')).comp (hv.constr (f ∘ v)) = linear_map.id, from hv.ext $ λ i, exists.elim (hf i) (λ i' hi', by simp [constr_basis, hi'.symm]; rw [hi', hgf]), λ x, congr_arg (λ h:M →ₗ[R] M, h x) this, right_inv := have (hv.constr (f ∘ v)).comp (hv'.constr (g ∘ v')) = linear_map.id, from hv'.ext $ λ i', exists.elim (hg i') (λ i hi, by simp [constr_basis, hi.symm]; rw [hi, hfg]), λ y, congr_arg (λ h:M' →ₗ[R] M', h y) this, ..hv.constr (f ∘ v) } @[simp] lemma equiv_of_is_basis_comp {ι'' : Type*} {v : ι → M} {v' : ι' → M'} {v'' : ι'' → M''} (hv : is_basis R v) (hv' : is_basis R v') (hv'' : is_basis R v'') (e : ι ≃ ι') (f : ι' ≃ ι'' ) : (equiv_of_is_basis hv hv' e).trans (equiv_of_is_basis hv' hv'' f) = equiv_of_is_basis hv hv'' (e.trans f) := begin apply linear_equiv.eq_of_linear_map_eq, apply hv.ext, intros i, simp [equiv_of_is_basis] end @[simp] lemma equiv_of_is_basis_refl : equiv_of_is_basis hv hv (equiv.refl ι) = linear_equiv.refl R M := begin apply linear_equiv.eq_of_linear_map_eq, apply hv.ext, intros i, simp [equiv_of_is_basis] end lemma equiv_of_is_basis_trans_symm (e : ι ≃ ι') {v' : ι' → M'} (hv' : is_basis R v') : (equiv_of_is_basis hv hv' e).trans (equiv_of_is_basis hv' hv e.symm) = linear_equiv.refl R M := by simp lemma equiv_of_is_basis_symm_trans (e : ι ≃ ι') {v' : ι' → M'} (hv' : is_basis R v') : (equiv_of_is_basis hv' hv e.symm).trans (equiv_of_is_basis hv hv' e) = linear_equiv.refl R M' := by simp lemma is_basis_inl_union_inr {v : ι → M} {v' : ι' → M'} (hv : is_basis R v) (hv' : is_basis R v') : is_basis R (sum.elim (inl R M M' ∘ v) (inr R M M' ∘ v')) := begin split, apply linear_independent_inl_union_inr' hv.1 hv'.1, rw [sum.elim_range, span_union, set.range_comp, span_image (inl R M M'), hv.2, map_top, set.range_comp, span_image (inr R M M'), hv'.2, map_top], exact linear_map.sup_range_inl_inr end end is_basis lemma is_basis_singleton_one (R : Type*) [unique ι] [ring R] : is_basis R (λ (_ : ι), (1 : R)) := begin split, { refine linear_independent_iff.2 (λ l, _), rw [finsupp.unique_single l, finsupp.total_single, smul_eq_mul, mul_one], intro hi, simp [hi] }, { refine top_unique (λ _ _, _), simp only [mem_span_singleton, range_const, mul_one, exists_eq, smul_eq_mul] } end protected lemma linear_equiv.is_basis (hs : is_basis R v) (f : M ≃ₗ[R] M') : is_basis R (f ∘ v) := begin split, { apply @linear_independent.image _ _ _ _ _ _ _ _ _ _ hs.1 (f : M →ₗ[R] M'), simp [linear_equiv.ker f] }, { rw set.range_comp, have : span R ((f : M →ₗ[R] M') '' range v) = ⊤, { rw [span_image (f : M →ₗ[R] M'), hs.2], simp }, exact this } end lemma is_basis_span (hs : linear_independent R v) : @is_basis ι R (span R (range v)) (λ i : ι, ⟨v i, subset_span (mem_range_self _)⟩) _ _ _ := begin split, { apply linear_independent_span hs }, { rw eq_top_iff', intro x, have h₁ : subtype.val '' set.range (λ i, subtype.mk (v i) _) = range v, by rw ←set.range_comp, have h₂ : map (submodule.subtype _) (span R (set.range (λ i, subtype.mk (v i) _))) = span R (range v), by rw [←span_image, submodule.subtype_eq_val, h₁], have h₃ : (x : M) ∈ map (submodule.subtype _) (span R (set.range (λ i, subtype.mk (v i) _))), by rw h₂; apply subtype.mem x, rcases mem_map.1 h₃ with ⟨y, hy₁, hy₂⟩, have h_x_eq_y : x = y, by rw [subtype.ext_iff, ← hy₂]; simp, rw h_x_eq_y, exact hy₁ } end lemma is_basis_empty (h_empty : ¬ nonempty ι) (h : ∀x:M, x = 0) : is_basis R (λ x : ι, (0 : M)) := ⟨ linear_independent_empty_type h_empty, eq_top_iff'.2 $ assume x, (h x).symm ▸ submodule.zero_mem _ ⟩ lemma is_basis_empty_bot (h_empty : ¬ nonempty ι) : is_basis R (λ _ : ι, (0 : (⊥ : submodule R M))) := begin apply is_basis_empty h_empty, intro x, apply subtype.ext_iff_val.2, exact (submodule.mem_bot R).1 (subtype.mem x), end open fintype variables [fintype ι] (h : is_basis R v) /-- A module over `R` with a finite basis is linearly equivalent to functions from its basis to `R`. -/ def is_basis.equiv_fun : M ≃ₗ[R] (ι → R) := linear_equiv.trans (module_equiv_finsupp h) { to_fun := finsupp.to_fun, map_add' := λ x y, by ext; exact finsupp.add_apply, map_smul' := λ x y, by ext; exact finsupp.smul_apply, ..finsupp.equiv_fun_on_fintype } /-- A module over a finite ring that admits a finite basis is finite. -/ def module.fintype_of_fintype [fintype R] : fintype M := fintype.of_equiv _ h.equiv_fun.to_equiv.symm theorem module.card_fintype [fintype R] [fintype M] : card M = (card R) ^ (card ι) := calc card M = card (ι → R) : card_congr h.equiv_fun.to_equiv ... = card R ^ card ι : card_fun /-- Given a basis `v` indexed by `ι`, the canonical linear equivalence between `ι → R` and `M` maps a function `x : ι → R` to the linear combination `∑_i x i • v i`. -/ @[simp] lemma is_basis.equiv_fun_symm_apply (x : ι → R) : h.equiv_fun.symm x = ∑ i, x i • v i := begin change finsupp.sum ((finsupp.equiv_fun_on_fintype.symm : (ι → R) ≃ (ι →₀ R)) x) (λ (i : ι) (a : R), a • v i) = ∑ i, x i • v i, dsimp [finsupp.equiv_fun_on_fintype, finsupp.sum], rw finset.sum_filter, refine finset.sum_congr rfl (λi hi, _), by_cases H : x i = 0, { simp [H] }, { simp [H], refl } end lemma is_basis.equiv_fun_apply (u : M) : h.equiv_fun u = h.repr u := rfl lemma is_basis.equiv_fun_total (u : M) : ∑ i, h.equiv_fun u i • v i = u:= begin conv_rhs { rw ← h.total_repr u }, simp [finsupp.total_apply, finsupp.sum_fintype, h.equiv_fun_apply] end @[simp] lemma is_basis.equiv_fun_self (i j : ι) : h.equiv_fun (v i) j = if i = j then 1 else 0 := by { rw [h.equiv_fun_apply, h.repr_self_apply] } @[simp] theorem is_basis.constr_apply_fintype (f : ι → M') (x : M) : (h.constr f : M → M') x = ∑ i, (h.equiv_fun x i) • f i := by simp [h.constr_apply, h.equiv_fun_apply, finsupp.sum_fintype] end module section vector_space variables {v : ι → V} [field K] [add_comm_group V] [add_comm_group V'] [vector_space K V] [vector_space K V'] {s t : set V} {x y z : V} include K open submodule /- TODO: some of the following proofs can generalized with a zero_ne_one predicate type class (instead of a data containing type class) -/ section lemma mem_span_insert_exchange : x ∈ span K (insert y s) → x ∉ span K s → y ∈ span K (insert x s) := begin simp [mem_span_insert], rintro a z hz rfl h, refine ⟨a⁻¹, -a⁻¹ • z, smul_mem _ _ hz, _⟩, have a0 : a ≠ 0, {rintro rfl, simp * at *}, simp [a0, smul_add, smul_smul] end end lemma linear_independent_iff_not_mem_span : linear_independent K v ↔ (∀i, v i ∉ span K (v '' (univ \ {i}))) := begin apply linear_independent_iff_not_smul_mem_span.trans, split, { intros h i h_in_span, apply one_ne_zero (h i 1 (by simp [h_in_span])) }, { intros h i a ha, by_contradiction ha', exact false.elim (h _ ((smul_mem_iff _ ha').1 ha)) } end lemma linear_independent_unique [unique ι] (h : v (default ι) ≠ 0): linear_independent K v := begin rw linear_independent_iff, intros l hl, ext i, rw [unique.eq_default i, finsupp.zero_apply], by_contra hc, have := smul_smul (l (default ι))⁻¹ (l (default ι)) (v (default ι)), rw [finsupp.unique_single l, finsupp.total_single] at hl, rw [hl, inv_mul_cancel hc, smul_zero, one_smul] at this, exact h this.symm end lemma linear_independent_singleton {x : V} (hx : x ≠ 0) : linear_independent K (λ x, x : ({x} : set V) → V) := begin apply @linear_independent_unique _ _ _ _ _ _ _ _ _, apply set.unique_singleton, apply hx, end lemma disjoint_span_singleton {p : submodule K V} {x : V} (x0 : x ≠ 0) : disjoint p (span K {x}) ↔ x ∉ p := ⟨λ H xp, x0 (disjoint_def.1 H _ xp (singleton_subset_iff.1 subset_span:_)), begin simp [disjoint_def, mem_span_singleton], rintro xp y yp a rfl, by_cases a0 : a = 0, {simp [a0]}, exact xp.elim ((smul_mem_iff p a0).1 yp), end⟩ lemma linear_independent.insert (hs : linear_independent K (λ b, b : s → V)) (hx : x ∉ span K s) : linear_independent K (λ b, b : insert x s → V) := begin rw ← union_singleton, have x0 : x ≠ 0 := mt (by rintro rfl; apply zero_mem _) hx, apply hs.union (linear_independent_singleton x0), rwa [disjoint_span_singleton x0] end theorem linear_independent_insert (hxs : x ∉ s) : linear_independent K (λ b : insert x s, (b : V)) ↔ linear_independent K (λ b : s, (b : V)) ∧ x ∉ submodule.span K s := ⟨λ h, ⟨h.mono $ set.subset_insert x s, have (λ (b : ↥(insert x s)), ↑b) '' (set.univ \ {⟨x, set.mem_insert x s⟩}) = s, from set.ext $ λ b, ⟨λ ⟨y, hy1, hy2⟩, hy2 ▸ y.2.resolve_left (λ H, hy1.2 $ subtype.eq H), λ hb, ⟨⟨b, set.mem_insert_of_mem x hb⟩, ⟨trivial, λ H, hxs $ (show b = x, from congr_arg subtype.val H) ▸ hb⟩, rfl⟩⟩, this ▸ linear_independent_iff_not_mem_span.1 h ⟨x, set.mem_insert x s⟩⟩, λ ⟨h1, h2⟩, h1.insert h2⟩ theorem linear_independent_insert' {ι} {s : set ι} {a : ι} {f : ι → V} (has : a ∉ s) : linear_independent K (λ x : insert a s, f x) ↔ linear_independent K (λ x : s, f x) ∧ f a ∉ submodule.span K (f '' s) := begin refine ⟨λ h, _, λ h, _⟩, { have hfas : f a ∉ f '' s := λ ⟨x, hxs, hfxa⟩, has (set.mem_of_eq_of_mem (congr_arg subtype.val $ (@id _ (h.injective) ⟨x, or.inr hxs⟩ ⟨a, or.inl rfl⟩ hfxa)).symm hxs), have := h.image', rwa [set.image_insert_eq, linear_independent_insert hfas, ← linear_independent_image] at this, exact (set.inj_on_iff_injective.2 h.injective).mono (set.subset_insert _ _) }, { cases h with h1 h2, have : set.inj_on f (insert a s) := (set.inj_on_insert has).2 ⟨set.inj_on_iff_injective.2 h1.injective, λ h, h2 $ submodule.subset_span h⟩, have hfas : f a ∉ f '' s := λ ⟨x, hxs, hfxa⟩, has (set.mem_of_eq_of_mem (this (or.inr hxs) (or.inl rfl) hfxa).symm hxs), rw [linear_independent_image this, set.image_insert_eq, linear_independent_insert hfas], exact ⟨h1.image', h2⟩ } end lemma exists_linear_independent (hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ t) : ∃b⊆t, s ⊆ b ∧ t ⊆ span K b ∧ linear_independent K (λ x, x : b → V) := begin rcases zorn.zorn_subset₀ {b | b ⊆ t ∧ linear_independent K (λ x, x : b → V)} _ _ ⟨hst, hs⟩ with ⟨b, ⟨bt, bi⟩, sb, h⟩, { refine ⟨b, bt, sb, λ x xt, _, bi⟩, by_contra hn, apply hn, rw ← h _ ⟨insert_subset.2 ⟨xt, bt⟩, bi.insert hn⟩ (subset_insert _ _), exact subset_span (mem_insert _ _) }, { refine λ c hc cc c0, ⟨⋃₀ c, ⟨_, _⟩, λ x, _⟩, { exact sUnion_subset (λ x xc, (hc xc).1) }, { exact linear_independent_sUnion_of_directed cc.directed_on (λ x xc, (hc xc).2) }, { exact subset_sUnion_of_mem } } end lemma exists_subset_is_basis (hs : linear_independent K (λ x, x : s → V)) : ∃b, s ⊆ b ∧ is_basis K (coe : b → V) := let ⟨b, hb₀, hx, hb₂, hb₃⟩ := exists_linear_independent hs (@subset_univ _ _) in ⟨ b, hx, @linear_independent.restrict_of_comp_subtype _ _ _ id _ _ _ _ hb₃, by simp; exact eq_top_iff.2 hb₂⟩ lemma exists_sum_is_basis (hs : linear_independent K v) : ∃ (ι' : Type u) (v' : ι' → V), is_basis K (sum.elim v v') := begin -- This is a hack: we jump through hoops to reuse `exists_subset_is_basis`. let s := set.range v, let e : ι ≃ s := equiv.set.range v hs.injective, have : (λ x, x : s → V) = v ∘ e.symm := by { funext, dsimp, rw [equiv.set.apply_range_symm v], }, have : linear_independent K (λ x, x : s → V), { rw this, exact linear_independent.comp hs _ (e.symm.injective), }, obtain ⟨b, ss, is⟩ := exists_subset_is_basis this, let e' : ι ⊕ (b \ s : set V) ≃ b := calc ι ⊕ (b \ s : set V) ≃ s ⊕ (b \ s : set V) : equiv.sum_congr e (equiv.refl _) ... ≃ b : equiv.set.sum_diff_subset ss, refine ⟨(b \ s : set V), λ x, x.1, _⟩, convert is_basis.comp is e' _, { funext x, cases x; simp; refl, }, { exact e'.bijective, }, end variables (K V) lemma exists_is_basis : ∃b : set V, is_basis K (λ i, i : b → V) := let ⟨b, _, hb⟩ := exists_subset_is_basis (linear_independent_empty K V : _) in ⟨b, hb⟩ variables {K V} -- TODO(Mario): rewrite? lemma exists_of_linear_independent_of_finite_span {t : finset V} (hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ (span K ↑t : submodule K V)) : ∃t':finset V, ↑t' ⊆ s ∪ ↑t ∧ s ⊆ ↑t' ∧ t'.card = t.card := have ∀t, ∀(s' : finset V), ↑s' ⊆ s → s ∩ ↑t = ∅ → s ⊆ (span K ↑(s' ∪ t) : submodule K V) → ∃t':finset V, ↑t' ⊆ s ∪ ↑t ∧ s ⊆ ↑t' ∧ t'.card = (s' ∪ t).card := assume t, finset.induction_on t (assume s' hs' _ hss', have s = ↑s', from eq_of_linear_independent_of_span_subtype hs hs' $ by simpa using hss', ⟨s', by simp [this]⟩) (assume b₁ t hb₁t ih s' hs' hst hss', have hb₁s : b₁ ∉ s, from assume h, have b₁ ∈ s ∩ ↑(insert b₁ t), from ⟨h, finset.mem_insert_self _ _⟩, by rwa [hst] at this, have hb₁s' : b₁ ∉ s', from assume h, hb₁s $ hs' h, have hst : s ∩ ↑t = ∅, from eq_empty_of_subset_empty $ subset.trans (by simp [inter_subset_inter, subset.refl]) (le_of_eq hst), classical.by_cases (assume : s ⊆ (span K ↑(s' ∪ t) : submodule K V), let ⟨u, hust, hsu, eq⟩ := ih _ hs' hst this in have hb₁u : b₁ ∉ u, from assume h, (hust h).elim hb₁s hb₁t, ⟨insert b₁ u, by simp [insert_subset_insert hust], subset.trans hsu (by simp), by simp [eq, hb₁t, hb₁s', hb₁u]⟩) (assume : ¬ s ⊆ (span K ↑(s' ∪ t) : submodule K V), let ⟨b₂, hb₂s, hb₂t⟩ := not_subset.mp this in have hb₂t' : b₂ ∉ s' ∪ t, from assume h, hb₂t $ subset_span h, have s ⊆ (span K ↑(insert b₂ s' ∪ t) : submodule K V), from assume b₃ hb₃, have ↑(s' ∪ insert b₁ t) ⊆ insert b₁ (insert b₂ ↑(s' ∪ t) : set V), by simp [insert_eq, -singleton_union, -union_singleton, union_subset_union, subset.refl, subset_union_right], have hb₃ : b₃ ∈ span K (insert b₁ (insert b₂ ↑(s' ∪ t) : set V)), from span_mono this (hss' hb₃), have s ⊆ (span K (insert b₁ ↑(s' ∪ t)) : submodule K V), by simpa [insert_eq, -singleton_union, -union_singleton] using hss', have hb₁ : b₁ ∈ span K (insert b₂ ↑(s' ∪ t)), from mem_span_insert_exchange (this hb₂s) hb₂t, by rw [span_insert_eq_span hb₁] at hb₃; simpa using hb₃, let ⟨u, hust, hsu, eq⟩ := ih _ (by simp [insert_subset, hb₂s, hs']) hst this in ⟨u, subset.trans hust $ union_subset_union (subset.refl _) (by simp [subset_insert]), hsu, by simp [eq, hb₂t', hb₁t, hb₁s']⟩)), begin have eq : t.filter (λx, x ∈ s) ∪ t.filter (λx, x ∉ s) = t, { ext1 x, by_cases x ∈ s; simp * }, apply exists.elim (this (t.filter (λx, x ∉ s)) (t.filter (λx, x ∈ s)) (by simp [set.subset_def]) (by simp [set.ext_iff] {contextual := tt}) (by rwa [eq])), intros u h, exact ⟨u, subset.trans h.1 (by simp [subset_def, and_imp, or_imp_distrib] {contextual:=tt}), h.2.1, by simp only [h.2.2, eq]⟩ end lemma exists_finite_card_le_of_finite_of_linear_independent_of_span (ht : finite t) (hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ span K t) : ∃h : finite s, h.to_finset.card ≤ ht.to_finset.card := have s ⊆ (span K ↑(ht.to_finset) : submodule K V), by simp; assumption, let ⟨u, hust, hsu, eq⟩ := exists_of_linear_independent_of_finite_span hs this in have finite s, from u.finite_to_set.subset hsu, ⟨this, by rw [←eq]; exact (finset.card_le_of_subset $ finset.coe_subset.mp $ by simp [hsu])⟩ lemma linear_map.exists_left_inverse_of_injective (f : V →ₗ[K] V') (hf_inj : f.ker = ⊥) : ∃g:V' →ₗ V, g.comp f = linear_map.id := begin rcases exists_is_basis K V with ⟨B, hB⟩, have hB₀ : _ := hB.1.to_subtype_range, have : linear_independent K (λ x, x : f '' B → V'), { have h₁ := hB₀.image_subtype (show disjoint (span K (range (λ i : B, i.val))) (linear_map.ker f), by simp [hf_inj]), rwa subtype.range_coe at h₁ }, rcases exists_subset_is_basis this with ⟨C, BC, hC⟩, haveI : inhabited V := ⟨0⟩, use hC.constr (C.restrict (inv_fun f)), refine hB.ext (λ b, _), rw image_subset_iff at BC, have : f b = (⟨f b, BC b.2⟩ : C) := rfl, dsimp, rw [this, constr_basis hC], exact left_inverse_inv_fun (linear_map.ker_eq_bot.1 hf_inj) _ end lemma submodule.exists_is_compl (p : submodule K V) : ∃ q : submodule K V, is_compl p q := let ⟨f, hf⟩ := p.subtype.exists_left_inverse_of_injective p.ker_subtype in ⟨f.ker, linear_map.is_compl_of_proj $ linear_map.ext_iff.1 hf⟩ lemma linear_map.exists_right_inverse_of_surjective (f : V →ₗ[K] V') (hf_surj : f.range = ⊤) : ∃g:V' →ₗ V, f.comp g = linear_map.id := begin rcases exists_is_basis K V' with ⟨C, hC⟩, haveI : inhabited V := ⟨0⟩, use hC.constr (C.restrict (inv_fun f)), refine hC.ext (λ c, _), simp [constr_basis hC, right_inverse_inv_fun (linear_map.range_eq_top.1 hf_surj) c] end open submodule linear_map theorem quotient_prod_linear_equiv (p : submodule K V) : nonempty ((p.quotient × p) ≃ₗ[K] V) := let ⟨q, hq⟩ := p.exists_is_compl in nonempty.intro $ ((quotient_equiv_of_is_compl p q hq).prod (linear_equiv.refl _ _)).trans (prod_equiv_of_is_compl q p hq.symm) open fintype variables (K) (V) theorem vector_space.card_fintype [fintype K] [fintype V] : ∃ n : ℕ, card V = (card K) ^ n := exists.elim (exists_is_basis K V) $ λ b hb, ⟨card b, module.card_fintype hb⟩ end vector_space namespace pi open set linear_map section module variables {η : Type*} {ιs : η → Type*} {Ms : η → Type*} variables [ring R] [∀i, add_comm_group (Ms i)] [∀i, module R (Ms i)] lemma linear_independent_std_basis (v : Πj, ιs j → (Ms j)) (hs : ∀i, linear_independent R (v i)) : linear_independent R (λ (ji : Σ j, ιs j), std_basis R Ms ji.1 (v ji.1 ji.2)) := begin have hs' : ∀j : η, linear_independent R (λ i : ιs j, std_basis R Ms j (v j i)), { intro j, apply linear_independent.image (hs j), simp [ker_std_basis] }, apply linear_independent_Union_finite hs', { assume j J _ hiJ, simp [(set.Union.equations._eqn_1 _).symm, submodule.span_image, submodule.span_Union], have h₀ : ∀ j, span R (range (λ (i : ιs j), std_basis R Ms j (v j i))) ≤ range (std_basis R Ms j), { intro j, rw [span_le, linear_map.range_coe], apply range_comp_subset_range }, have h₁ : span R (range (λ (i : ιs j), std_basis R Ms j (v j i))) ≤ ⨆ i ∈ {j}, range (std_basis R Ms i), { rw @supr_singleton _ _ _ (λ i, linear_map.range (std_basis R (λ (j : η), Ms j) i)), apply h₀ }, have h₂ : (⨆ j ∈ J, span R (range (λ (i : ιs j), std_basis R Ms j (v j i)))) ≤ ⨆ j ∈ J, range (std_basis R (λ (j : η), Ms j) j) := supr_le_supr (λ i, supr_le_supr (λ H, h₀ i)), have h₃ : disjoint (λ (i : η), i ∈ {j}) J, { convert set.disjoint_singleton_left.2 hiJ, rw ←@set_of_mem_eq _ {j}, refl }, exact (disjoint_std_basis_std_basis _ _ _ _ h₃).mono h₁ h₂ } end variable [fintype η] lemma is_basis_std_basis (s : Πj, ιs j → (Ms j)) (hs : ∀j, is_basis R (s j)) : is_basis R (λ (ji : Σ j, ιs j), std_basis R Ms ji.1 (s ji.1 ji.2)) := begin split, { apply linear_independent_std_basis _ (assume i, (hs i).1) }, have h₁ : Union (λ j, set.range (std_basis R Ms j ∘ s j)) ⊆ range (λ (ji : Σ (j : η), ιs j), (std_basis R Ms (ji.fst)) (s (ji.fst) (ji.snd))), { apply Union_subset, intro i, apply range_comp_subset_range (λ x : ιs i, (⟨i, x⟩ : Σ (j : η), ιs j)) (λ (ji : Σ (j : η), ιs j), std_basis R Ms (ji.fst) (s (ji.fst) (ji.snd))) }, have h₂ : ∀ i, span R (range (std_basis R Ms i ∘ s i)) = range (std_basis R Ms i), { intro i, rw [set.range_comp, submodule.span_image, (assume i, (hs i).2), submodule.map_top] }, apply eq_top_mono, apply span_mono h₁, rw span_Union, simp only [h₂], apply supr_range_std_basis end section variables (R η) lemma is_basis_fun₀ : is_basis R (λ (ji : Σ (j : η), unit), (std_basis R (λ (i : η), R) (ji.fst)) 1) := @is_basis_std_basis R η (λi:η, unit) (λi:η, R) _ _ _ _ (λ _ _, (1 : R)) (assume i, @is_basis_singleton_one _ _ _ _) lemma is_basis_fun : is_basis R (λ i, std_basis R (λi:η, R) i 1) := begin apply (is_basis_fun₀ R η).comp (λ i, ⟨i, punit.star⟩), apply bijective_iff_has_inverse.2, use sigma.fst, simp [function.left_inverse, function.right_inverse] end end end module end pi
3f0d1fd4ad9c58a1198ee1e6a168a655e45dee0a
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/tests/lean/run/float_cases_bug.lean
296c21e46619f0dc1b89fdc98c017aa5e75c4f81
[ "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
70
lean
def foo (xs : List Nat) := xs.span (fun n => oldCoe (decide (n = 1)))
3ce6a15930c37f5c22b98c333225d6b3199c9706
cf39355caa609c0f33405126beee2739aa3cb77e
/tmp/div2.lean
f0681087eb69945c88c2ce9a50d9317893d2b4a9
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
569
lean
def R : (Σ _ : nat, nat) → (Σ _ : nat, nat) → Prop := sigma.lex nat.lt (λ _, empty_relation) def Rwf : well_founded R := sigma.lex_wf nat.lt_wf (λ _, empty_wf) set_option trace.debug.eqn_compiler.wf_rec true set_option trace.eqn_compiler.wf_rec true set_option trace.app_builder true -- set_option trace.eqn_compiler.elim_match true def Div : nat → Prop → nat → nat | x p y := if h : 0 < y ∧ y ≤ x then have x - y < x, from nat.sub_lt (nat.lt_of_lt_of_le h.left h.right) h.left, Div (x - y) p y + 1 else 0 -- using_well_founded R Rwf
cb9d70adb441aac8de167c88226d9fcd9b489924
e39f04f6ff425fe3b3f5e26a8998b817d1dba80f
/analysis/limits.lean
498981991155df09b772ef4e3db8e951d84ebfd1
[ "Apache-2.0" ]
permissive
kristychoi/mathlib
c504b5e8f84e272ea1d8966693c42de7523bf0ec
257fd84fe98927ff4a5ffe044f68c4e9d235cc75
refs/heads/master
1,586,520,722,896
1,544,030,145,000
1,544,031,933,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
11,728
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl A collection of limit properties. -/ import algebra.big_operators algebra.group_power tactic.norm_num analysis.ennreal analysis.topology.infinite_sum noncomputable theory open classical finset function filter local attribute [instance] prop_decidable section real lemma has_sum_of_absolute_convergence {f : ℕ → ℝ} (hf : ∃r, tendsto (λn, (range n).sum (λi, abs (f i))) at_top (nhds r)) : has_sum f := let f' := λs:finset ℕ, s.sum (λi, abs (f i)) in suffices cauchy (map (λs:finset ℕ, s.sum f) at_top), from complete_space.complete this, cauchy_iff.mpr $ and.intro (map_ne_bot at_top_ne_bot) $ assume s hs, let ⟨ε, hε, hsε⟩ := mem_uniformity_dist.mp hs, ⟨r, hr⟩ := hf in have hε' : {p : ℝ × ℝ | dist p.1 p.2 < ε / 2} ∈ (@uniformity ℝ _).sets, from mem_uniformity_dist.mpr ⟨ε / 2, div_pos_of_pos_of_pos hε two_pos, assume a b h, h⟩, have cauchy (at_top.map $ λn, f' (range n)), from cauchy_downwards cauchy_nhds (map_ne_bot at_top_ne_bot) hr, have ∃n, ∀{n'}, n ≤ n' → dist (f' (range n)) (f' (range n')) < ε / 2, by simp [cauchy_iff, mem_at_top_sets] at this; from let ⟨t, ⟨u, hu⟩, ht⟩ := this _ hε' in ⟨u, assume n' hn, ht $ set.prod_mk_mem_set_prod_eq.mpr ⟨hu _ (le_refl _), hu _ hn⟩⟩, let ⟨n, hn⟩ := this in have ∀{s}, range n ⊆ s → abs ((s \ range n).sum f) < ε / 2, from assume s hs, let ⟨n', hn'⟩ := @exists_nat_subset_range s in have range n ⊆ range n', from finset.subset.trans hs hn', have f'_nn : 0 ≤ f' (range n' \ range n), from zero_le_sum $ assume _ _, abs_nonneg _, calc abs ((s \ range n).sum f) ≤ f' (s \ range n) : abs_sum_le_sum_abs ... ≤ f' (range n' \ range n) : sum_le_sum_of_subset_of_nonneg (finset.sdiff_subset_sdiff hn' (finset.subset.refl _)) (assume _ _ _, abs_nonneg _) ... = abs (f' (range n' \ range n)) : (abs_of_nonneg f'_nn).symm ... = abs (f' (range n') - f' (range n)) : by simp [f', (sum_sdiff ‹range n ⊆ range n'›).symm] ... = abs (f' (range n) - f' (range n')) : abs_sub _ _ ... < ε / 2 : hn $ range_subset.mp this, have ∀{s t}, range n ⊆ s → range n ⊆ t → dist (s.sum f) (t.sum f) < ε, from assume s t hs ht, calc abs (s.sum f - t.sum f) = abs ((s \ range n).sum f + - (t \ range n).sum f) : by rw [←sum_sdiff hs, ←sum_sdiff ht]; simp ... ≤ abs ((s \ range n).sum f) + abs ((t \ range n).sum f) : le_trans (abs_add_le_abs_add_abs _ _) $ by rw [abs_neg]; exact le_refl _ ... < ε / 2 + ε / 2 : add_lt_add (this hs) (this ht) ... = ε : by rw [←add_div, add_self_div_two], ⟨(λs:finset ℕ, s.sum f) '' {s | range n ⊆ s}, image_mem_map $ mem_at_top (range n), assume ⟨a, b⟩ ⟨⟨t, ht, ha⟩, ⟨s, hs, hb⟩⟩, by simp at ha hb; exact ha ▸ hb ▸ hsε (this ht hs)⟩ lemma is_sum_iff_tendsto_nat_of_nonneg {f : ℕ → ℝ} {r : ℝ} (hf : ∀n, 0 ≤ f n) : is_sum f r ↔ tendsto (λn, (range n).sum f) at_top (nhds r) := ⟨tendsto_sum_nat_of_is_sum, assume hr, have tendsto (λn, (range n).sum (λn, abs (f n))) at_top (nhds r), by simp [(λi, abs_of_nonneg (hf i)), hr], let ⟨p, h⟩ := has_sum_of_absolute_convergence ⟨r, this⟩ in have hp : tendsto (λn, (range n).sum f) at_top (nhds p), from tendsto_sum_nat_of_is_sum h, have p = r, from tendsto_nhds_unique at_top_ne_bot hp hr, this ▸ h⟩ end real lemma mul_add_one_le_pow {r : ℝ} (hr : 0 ≤ r) : ∀{n:ℕ}, (n:ℝ) * r + 1 ≤ (r + 1) ^ n | 0 := by simp; exact le_refl 1 | (n + 1) := let h : (n:ℝ) ≥ 0 := nat.cast_nonneg n in calc ↑(n + 1) * r + 1 ≤ ((n + 1) * r + 1) + r * r * n : le_add_of_le_of_nonneg (le_refl _) (mul_nonneg (mul_nonneg hr hr) h) ... = (r + 1) * (n * r + 1) : by simp [mul_add, add_mul, mul_comm, mul_assoc] ... ≤ (r + 1) * (r + 1) ^ n : mul_le_mul (le_refl _) mul_add_one_le_pow (add_nonneg (mul_nonneg h hr) zero_le_one) (add_nonneg hr zero_le_one) lemma tendsto_pow_at_top_at_top_of_gt_1 {r : ℝ} (h : r > 1) : tendsto (λn:ℕ, r ^ n) at_top at_top := tendsto_infi.2 $ assume p, tendsto_principal.2 $ let ⟨n, hn⟩ := exists_nat_gt (p / (r - 1)) in have hn_nn : (0:ℝ) ≤ n, from nat.cast_nonneg n, have r - 1 > 0, from sub_lt_iff_lt_add.mp $ by simp; assumption, have p ≤ r ^ n, from calc p = (p / (r - 1)) * (r - 1) : (div_mul_cancel _ $ ne_of_gt this).symm ... ≤ n * (r - 1) : mul_le_mul (le_of_lt hn) (le_refl _) (le_of_lt this) hn_nn ... ≤ n * (r - 1) + 1 : le_add_of_le_of_nonneg (le_refl _) zero_le_one ... ≤ ((r - 1) + 1) ^ n : mul_add_one_le_pow $ le_of_lt this ... ≤ r ^ n : by simp; exact le_refl _, show {n | p ≤ r ^ n} ∈ at_top.sets, from mem_at_top_sets.mpr ⟨n, assume m hnm, le_trans this (pow_le_pow (le_of_lt h) hnm)⟩ lemma tendsto_inverse_at_top_nhds_0 : tendsto (λr:ℝ, r⁻¹) at_top (nhds 0) := tendsto_orderable_unbounded (no_top 0) (no_bot 0) $ assume l u hl hu, mem_at_top_sets.mpr ⟨u⁻¹ + 1, assume b hb, have u⁻¹ < b, from lt_of_lt_of_le (lt_add_of_pos_right _ zero_lt_one) hb, ⟨lt_trans hl $ inv_pos $ lt_trans (inv_pos hu) this, lt_of_one_div_lt_one_div hu $ begin rw [inv_eq_one_div], simp [-one_div_eq_inv, div_div_eq_mul_div, div_one], simp [this] end⟩⟩ lemma map_succ_at_top_eq : map nat.succ at_top = at_top := le_antisymm (assume s hs, let ⟨b, hb⟩ := mem_at_top_sets.mp hs in mem_at_top_sets.mpr ⟨b, assume c hc, hb (c + 1) $ le_trans hc $ nat.le_succ _⟩) (assume s hs, let ⟨b, hb⟩ := mem_at_top_sets.mp hs in mem_at_top_sets.mpr ⟨b + 1, assume c, match c with | 0 := assume h, have 0 > 0, from lt_of_lt_of_le (lt_add_of_le_of_pos (nat.zero_le _) zero_lt_one) h, (lt_irrefl 0 this).elim | (c+1) := assume h, hb _ (nat.le_of_succ_le_succ h) end⟩) lemma tendsto_comp_succ_at_top_iff {α : Type*} {f : ℕ → α} {x : filter α} : tendsto (λn, f (nat.succ n)) at_top x ↔ tendsto f at_top x := calc tendsto (f ∘ nat.succ) at_top x ↔ tendsto f (map nat.succ at_top) x : by simp [tendsto, filter.map_map] ... ↔ _ : by rw [map_succ_at_top_eq] lemma tendsto_pow_at_top_nhds_0_of_lt_1 {r : ℝ} (h₁ : 0 ≤ r) (h₂ : r < 1) : tendsto (λn:ℕ, r^n) at_top (nhds 0) := by_cases (assume : r = 0, tendsto_comp_succ_at_top_iff.mp $ by simp [pow_succ, this, tendsto_const_nhds]) (assume : r ≠ 0, have tendsto (λn, (r⁻¹ ^ n)⁻¹) at_top (nhds 0), from (tendsto_pow_at_top_at_top_of_gt_1 $ one_lt_inv (lt_of_le_of_ne h₁ this.symm) h₂).comp tendsto_inverse_at_top_nhds_0, tendsto_cong this $ univ_mem_sets' $ by simp *) lemma tendsto_coe_iff {f : ℕ → ℕ} : tendsto (λ n, (f n : ℝ)) at_top at_top ↔ tendsto f at_top at_top := ⟨ λ h, tendsto_infi.2 $ λ i, tendsto_principal.2 (have _, from tendsto_infi.1 h i, by simpa using tendsto_principal.1 this), λ h, tendsto.comp h tendsto_of_nat_at_top_at_top ⟩ lemma tendsto_pow_at_top_at_top_of_gt_1_nat {k : ℕ} (h : 1 < k) : tendsto (λn:ℕ, k ^ n) at_top at_top := tendsto_coe_iff.1 $ have hr : 1 < (k : ℝ), by rw [← nat.cast_one, nat.cast_lt]; exact h, by simpa using tendsto_pow_at_top_at_top_of_gt_1 hr lemma tendsto_inverse_at_top_nhds_0_nat : tendsto (λ n : ℕ, (n : ℝ)⁻¹) at_top (nhds 0) := tendsto.comp (tendsto_coe_iff.2 tendsto_id) tendsto_inverse_at_top_nhds_0 lemma tendsto_one_div_at_top_nhds_0_nat : tendsto (λ n : ℕ, 1/(n : ℝ)) at_top (nhds 0) := by simpa only [inv_eq_one_div] using tendsto_inverse_at_top_nhds_0_nat lemma sum_geometric' {r : ℝ} (h : r ≠ 0) : ∀{n}, (finset.range n).sum (λi, (r + 1) ^ i) = ((r + 1) ^ n - 1) / r | 0 := by simp [zero_div] | (n+1) := by simp [@sum_geometric' n, h, pow_succ, range_succ, add_div_eq_mul_add_div, add_mul, mul_comm, mul_assoc] lemma sum_geometric {r : ℝ} {n : ℕ} (h : r ≠ 1) : (range n).sum (λi, r ^ i) = (r ^ n - 1) / (r - 1) := calc (range n).sum (λi, r ^ i) = (range n).sum (λi, ((r - 1) + 1) ^ i) : by simp ... = (((r - 1) + 1) ^ n - 1) / (r - 1) : sum_geometric' $ by simp [sub_eq_iff_eq_add, -sub_eq_add_neg, h] ... = (r ^ n - 1) / (r - 1) : by simp lemma is_sum_geometric {r : ℝ} (h₁ : 0 ≤ r) (h₂ : r < 1) : is_sum (λn:ℕ, r ^ n) (1 / (1 - r)) := have r ≠ 1, from ne_of_lt h₂, have r + -1 ≠ 0, by rw [←sub_eq_add_neg, ne, sub_eq_iff_eq_add]; simp; assumption, have tendsto (λn, (r ^ n - 1) * (r - 1)⁻¹) at_top (nhds ((0 - 1) * (r - 1)⁻¹)), from tendsto_mul (tendsto_sub (tendsto_pow_at_top_nhds_0_of_lt_1 h₁ h₂) tendsto_const_nhds) tendsto_const_nhds, (is_sum_iff_tendsto_nat_of_nonneg $ pow_nonneg h₁).mpr $ by simp [neg_inv, sum_geometric, div_eq_mul_inv, *] at * lemma is_sum_geometric_two (a : ℝ) : is_sum (λn:ℕ, (a / 2) / 2 ^ n) a := begin convert is_sum_mul_left (a / 2) (is_sum_geometric (le_of_lt one_half_pos) one_half_lt_one), { funext n, simp, rw ← pow_inv; [refl, exact two_ne_zero] }, { norm_num, rw div_mul_cancel _ two_ne_zero } end def pos_sum_of_encodable {ε : ℝ} (hε : 0 < ε) (ι) [encodable ι] : {ε' : ι → ℝ // (∀ i, 0 < ε' i) ∧ ∃ c, is_sum ε' c ∧ c ≤ ε} := begin let f := λ n, (ε / 2) / 2 ^ n, have hf : is_sum f ε := is_sum_geometric_two _, have f0 : ∀ n, 0 < f n := λ n, div_pos (half_pos hε) (pow_pos two_pos _), refine ⟨f ∘ encodable.encode, λ i, f0 _, _⟩, let g : ℕ → ℝ := λ n, option.cases_on (encodable.decode2 ι n) 0 (f ∘ encodable.encode), have : ∀ n, g n = 0 ∨ g n = f n, { intro n, dsimp [g], cases e : encodable.decode2 ι n with a, { exact or.inl rfl }, { simp [encodable.mem_decode2.1 e] } }, cases has_sum_of_has_sum_of_sub ⟨_, hf⟩ this with c hg, have cε : c ≤ ε, { refine is_sum_le (λ n, _) hg hf, cases this n; rw h, exact le_of_lt (f0 _) }, have hs : ∀ n, g n ≠ 0 → (encodable.decode2 ι n).is_some, { intros n h, dsimp [g] at h, cases encodable.decode2 ι n, exact (h rfl).elim, exact rfl }, refine ⟨c, _, cε⟩, refine is_sum_of_is_sum_ne_zero (λ n h, option.get (hs n h)) (λ n _, ne_of_gt (f0 _)) (λ i _, encodable.encode i) (λ n h, ne_of_gt _) (λ n h, _) (λ i _, _) (λ i _, _) hg, { dsimp [g], rw encodable.encodek2, exact f0 _ }, { exact encodable.mem_decode2.1 (option.get_mem _) }, { exact option.get_of_mem _ (encodable.encodek2 _) }, { dsimp [g], rw encodable.encodek2 } end namespace nnreal theorem exists_pos_sum_of_encodable {ε : nnreal} (hε : 0 < ε) (ι) [encodable ι] : ∃ ε' : ι → nnreal, (∀ i, 0 < ε' i) ∧ ∃c, is_sum ε' c ∧ c < ε := let ⟨a, a0, aε⟩ := dense hε in let ⟨ε', hε', c, hc, hcε⟩ := pos_sum_of_encodable a0 ι in ⟨ λi, ⟨ε' i, le_of_lt $ hε' i⟩, assume i, (nnreal.coe_lt _ _).2 $ hε' i, ⟨c, is_sum_le (assume i, le_of_lt $ hε' i) is_sum_zero hc ⟩, nnreal.is_sum_coe.1 hc, lt_of_le_of_lt ((nnreal.coe_le _ _).1 hcε) aε ⟩ end nnreal namespace ennreal theorem exists_pos_sum_of_encodable {ε : ennreal} (hε : 0 < ε) (ι) [encodable ι] : ∃ ε' : ι → nnreal, (∀ i, 0 < ε' i) ∧ (∑ i, (ε' i : ennreal)) < ε := begin rcases dense hε with ⟨r, h0r, hrε⟩, rcases lt_iff_exists_coe.1 hrε with ⟨x, rfl, hx⟩, rcases nnreal.exists_pos_sum_of_encodable (coe_lt_coe.1 h0r) ι with ⟨ε', hp, c, hc, hcr⟩, exact ⟨ε', hp, (ennreal.tsum_coe_eq hc).symm ▸ lt_trans (coe_lt_coe.2 hcr) hrε⟩ end end ennreal
9b11bf4a066bf26102d014ec33f6a36bbde16e3b
b9a81ebb9de684db509231c4469a7d2c88915808
/src/super/clause.lean
4891be5bfa2065897ba16c9fc0a755fc39d8ce5b
[]
no_license
leanprover/super
3dd81ce8d9ac3cba20bce55e84833fadb2f5716e
47b107b4cec8f3b41d72daba9cbda2f9d54025de
refs/heads/master
1,678,482,996,979
1,676,526,367,000
1,676,526,367,000
92,215,900
12
6
null
1,513,327,539,000
1,495,570,640,000
Lean
UTF-8
Lean
false
false
8,649
lean
/- Copyright (c) 2017 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner -/ import init.meta.tactic .utils .trim open expr list tactic monad decidable native namespace super meta def is_local_not (local_false : expr) (e : expr) : option expr := match e with | (pi _ _ a b) := if b = local_false then some a else none | _ := if local_false = `(false) then is_not e else none end meta structure clause := (num_quants : ℕ) (num_lits : ℕ) (proof : expr) (type : expr) (local_false : expr) namespace clause meta def num_binders (c : clause) : ℕ := num_quants c + num_lits c meta def inst (c : clause) (e : expr) : clause := (if num_quants c > 0 then mk (num_quants c - 1) (num_lits c) else mk 0 (num_lits c - 1)) (app (proof c) e) (instantiate_var (binding_body (type c)) e) c.local_false meta def instn (c : clause) (es : list expr) : clause := foldr (λe c', inst c' e) c es meta def open_const (c : clause) : tactic (clause × expr) := do n ← mk_fresh_name, b ← return $ local_const n (binding_name (type c)) (binding_info (type c)) (binding_domain (type c)), return (inst c b, b) meta def open_meta (c : clause) : tactic (clause × expr) := do b ← mk_meta_var (binding_domain (type c)), return (inst c b, b) meta def close_const (c : clause) (e : expr) : clause := match e with | local_const uniq pp info t := let abst_type' := abstract_local (type c) (local_uniq_name e) in let type' := pi pp binder_info.default t (abstract_local (type c) uniq) in let abs_prf := abstract_local (proof c) uniq in let proof' := lambdas [e] c.proof in if num_quants c > 0 ∨ has_var abst_type' then { c with num_quants := c.num_quants + 1, proof := proof', type := type' } else { c with num_lits := c.num_lits + 1, proof := proof', type := type' } | _ := ⟨0, 0, default, default, default⟩ end meta def open_constn : clause → ℕ → tactic (clause × list expr) | c 0 := return (c, nil) | c (n+1) := do (c', b) ← open_const c, (c'', bs) ← open_constn c' n, return (c'', b::bs) meta def open_metan : clause → ℕ → tactic (clause × list expr) | c 0 := return (c, nil) | c (n+1) := do (c', b) ← open_meta c, (c'', bs) ← open_metan c' n, return (c'', b::bs) meta def close_constn : clause → list expr → clause | c [] := c | c (b::bs') := close_const (close_constn c bs') b private meta def parse_clause (local_false : expr) : expr → expr → tactic clause | proof (pi n bi d b) := do lc_n ← mk_fresh_name, lc ← return $ local_const lc_n n bi d, c ← parse_clause (app proof lc) (instantiate_var b lc), return $ c.close_const $ local_const lc_n n binder_info.default d | proof `(not %%formula) := parse_clause proof (formula.imp `(false)) | proof type := if type = local_false then do return { num_quants := 0, num_lits := 0, proof := proof, type := type, local_false := local_false } else do univ ← infer_univ type, not_type ← return $ imp type local_false, parse_clause (lam `H binder_info.default not_type $ app (mk_var 0) proof) (imp not_type local_false) meta def of_proof_and_type (local_false proof type : expr) : tactic clause := parse_clause local_false proof type meta def of_proof (local_false proof : expr) : tactic clause := do type ← infer_type proof, of_proof_and_type local_false proof type meta def of_classical_proof (proof : expr) : tactic clause := of_proof `(false) proof meta def inst_mvars (c : clause) : tactic clause := do proof' ← instantiate_mvars (proof c), type' ← instantiate_mvars (type c), return { c with proof := proof', type := type' } meta inductive literal | left : expr → literal | right : expr → literal namespace literal meta instance : decidable_eq literal := by mk_dec_eq_instance meta def formula : literal → expr | (left f) := f | (right f) := f meta def is_neg : literal → bool | (left _) := tt | (right _) := ff meta def is_pos (l : literal) : bool := bnot l.is_neg meta def to_formula (l : literal) : expr := if l.is_neg then app (const ``not []) l.formula else formula l meta def type_str : literal → string | (literal.left _) := "left" | (literal.right _) := "right" meta instance : has_to_tactic_format literal := ⟨λl, do pp_f ← pp l.formula, return $ to_fmt l.type_str ++ " (" ++ pp_f ++ ")"⟩ end literal private meta def get_binding_body : expr → ℕ → expr | e 0 := e | e (i+1) := get_binding_body e.binding_body i meta def get_binder (e : expr) (i : nat) := binding_domain (get_binding_body e i) meta def validate (c : clause) : tactic unit := do concl ← return $ get_binding_body c.type c.num_binders, unify concl c.local_false <|> (do pp_concl ← pp concl, pp_lf ← pp c.local_false, fail $ to_fmt "wrong local false: " ++ pp_concl ++ " =!= " ++ pp_lf), type' ← infer_type c.proof, unify c.type type' <|> (do pp_ty ← pp c.type, pp_ty' ← pp type', fail (to_fmt "wrong type: " ++ pp_ty ++ " =!= " ++ pp_ty')) meta def get_lit (c : clause) (i : nat) : literal := let bind := get_binder (type c) (num_quants c + i) in match is_local_not c.local_false bind with | some formula := literal.right formula | none := literal.left bind end meta def lits_where (c : clause) (p : literal → bool) : list nat := list.filter (λl, p (get_lit c l)) (range (num_lits c)) meta def get_lits (c : clause) : list literal := list.map (get_lit c) (range c.num_lits) private meta def tactic_format (c : clause) : tactic format := do c ← c.open_metan c.num_quants, pp (do l ← c.1.get_lits, [l.to_formula]) meta instance : has_to_tactic_format clause := ⟨tactic_format⟩ meta def is_maximal (gt : expr → expr → bool) (c : clause) (i : nat) : bool := list.empty (list.filter (λj, gt (get_lit c j).formula (get_lit c i).formula) (range c.num_lits)) meta def normalize (c : clause) : tactic clause := do opened ← open_constn c (num_binders c), lconsts_in_types ← return $ contained_lconsts_list (list.map local_type opened.2), quants' ← return $ list.filter (λlc, rb_map.contains lconsts_in_types (local_uniq_name lc)) opened.2, lits' ← return $ list.filter (λlc, ¬rb_map.contains lconsts_in_types (local_uniq_name lc)) opened.2, return $ close_constn opened.1 (quants' ++ lits') meta def whnf_head_lit (c : clause) : tactic clause := do atom' ← whnf $ literal.formula $ get_lit c 0, return $ if literal.is_neg (get_lit c 0) then { c with type := atom'.imp (binding_body c.type) } else { c with type := `(not %%atom').imp (c.type.binding_body) } end clause meta def unify_lit (l1 l2 : clause.literal) : tactic unit := if clause.literal.is_pos l1 = clause.literal.is_pos l2 then unify (clause.literal.formula l1) (clause.literal.formula l2) transparency.none else fail "cannot unify literals" -- FIXME: this is most definitely broken with meta-variables that were already in the goal meta def sort_and_constify_metas : list expr → tactic (list expr) | exprs_with_metas := do inst_exprs ← mapm instantiate_mvars exprs_with_metas, metas ← return $ inst_exprs >>= get_metas, match list.filter (λm, ¬has_meta_var (get_meta_type m)) metas with | [] := if metas.empty then return [] else do metas.mmap' (λm, do trace (expr.to_string m), t ← infer_type m, trace (expr.to_string t)), fail "could not sort metas" | ((mvar n pp_n t) :: _) := do c ← infer_type (mvar n pp_n t) >>= mk_local_def `x, unify c (mvar n pp_n t), rest ← sort_and_constify_metas metas, c ← instantiate_mvars c, return ([c] ++ rest) | _ := failed end namespace clause meta def meta_closure (metas : list expr) (qf : clause) : tactic clause := do bs ← sort_and_constify_metas metas, qf' ← clause.inst_mvars qf, clause.inst_mvars $ clause.close_constn qf' bs private meta def distinct' (local_false : expr) : list expr → expr → clause | [] proof := ⟨ 0, 0, proof, local_false, local_false ⟩ | (h::hs) proof := let (dups, rest) := partition (λh' : expr, h.local_type = h'.local_type) hs, proof_wo_dups := foldl (λproof (h' : expr), instantiate_var (abstract_local proof h'.local_uniq_name) h) proof dups in (distinct' rest proof_wo_dups).close_const h meta def distinct (c : clause) : tactic clause := do (qf, vs) ← c.open_constn c.num_quants, (fls, hs) ← qf.open_constn qf.num_lits, return $ (distinct' c.local_false hs fls.proof).close_constn vs end clause end super
ca71f55164e57bb661b6ec014c51aa1a163bd4a4
510e96af568b060ed5858226ad954c258549f143
/data/pfun.lean
5132091ee4d92e222da05fe510581ca4908e95f0
[]
no_license
Shamrock-Frost/library_dev
cb6d1739237d81e17720118f72ba0a6db8a5906b
0245c71e4931d3aceeacf0aea776454f6ee03c9c
refs/heads/master
1,609,481,034,595
1,500,165,215,000
1,500,165,347,000
97,350,162
0
0
null
1,500,164,969,000
1,500,164,969,000
null
UTF-8
Lean
false
false
2,351
lean
import data.set ..data.set.basic universes u v w structure pfun (α : Type u) (β : Type v) : Type (max u v) := (dom : set α) (fn : Π x, dom x → β) infixr ` →. `:25 := pfun namespace pfun variables {α : Type u} {β : Type v} {γ : Type w} def eval_opt {β : Type v} (f : α →. β) [decidable_pred (dom f)] (x : α) : option β := if h : dom f x then some (f.fn x h) else none protected def ext : Π {f g : α →. β} (H1 : ∀x, x ∈ dom f ↔ x ∈ dom g) (H2 : ∀x p q, f.fn x p = g.fn x q), f = g | ⟨df, f⟩ ⟨dg, g⟩ H1 H2 := have t : df = dg, from set.ext H1, by cases t; rw [show f = g, from funext $ λx, funext $ λp, H2 x p p] def as_subtype (f : α →. β) (s : {x // f.dom x}) : β := f.fn s.1 s.2 protected def lift (f : α → β) : α →. β := ⟨set.univ, λx _, f x⟩ instance : has_coe (α → β) (α →. β) := ⟨pfun.lift⟩ def graph (f : α →. β) : set (α × β) := λ ⟨a, b⟩, ∃ ha, f.fn a ha = b def ran (f : α →. β) : set β := {b | ∃a, (a, b) ∈ f.graph} def restrict (f : α →. β) {p : set α} (H : p ⊆ f.dom) : α →. β := ⟨p, λx h, f.fn x (H h)⟩ theorem dom_iff_graph (f : α →. β) (x : α) : x ∈ f.dom ↔ ∃y, (x, y) ∈ f.graph := ⟨λh, ⟨f.fn x h, h, rfl⟩, λ⟨y, h, _⟩, h⟩ protected def pure (x : β) : α →. β := ↑(function.const α x) protected def bind (f : α →. β) (g : β → α →. γ) : α →. γ := ⟨λa, ∃b: dom f a, (g (f.fn a b)).dom a, λa ha, (g (f.fn a (exists.elim ha $ λh _, h))).fn a (exists.elim ha $ λ_ h, h)⟩ instance : monad (pfun α) := { pure := @pfun.pure _, bind := @pfun.bind _, id_map := λβ f, pfun.ext (λa, ⟨λ⟨h, _⟩, h, λh, ⟨h, trivial⟩⟩) (λ_ _ _, rfl), pure_bind := λβ γ x f, pfun.ext (λa, ⟨λ⟨_, h⟩, h, λh, ⟨trivial, h⟩⟩) (λ_ _ _, rfl), bind_assoc := λβ γ δ f g k, pfun.ext (λa, ⟨λ⟨⟨h1, h2⟩, h3⟩, ⟨h1, h2, h3⟩, λ⟨h1, h2, h3⟩, ⟨⟨h1, h2⟩, h3⟩⟩) (λ_ _ _, rfl) } theorem pure_defined (p : set α) (x : β) : p ⊆ (@pfun.pure α _ x).dom := set.subset_univ p theorem bind_defined {α : Type u} {β γ : Type v} (p : set α) {f : α →. β} {g : β → α →. γ} (H1 : p ⊆ f.dom) (H2 : ∀x, p ⊆ (g x).dom) : p ⊆ (f >>= g).dom := λa ha, (⟨H1 ha, H2 _ ha⟩ : (f >>= g).dom a) end pfun
beab45ee61065b4120bc6d9e3eaa8476c8d93b04
98beff2e97d91a54bdcee52f922c4e1866a6c9b9
/src/finite_products.lean
f5864accfd0f9d8c4c67a6b8e717e1414f8380e3
[]
no_license
b-mehta/topos
c3fc43fb04ba16bae1965ce5c26c6461172e5bc6
c9032b11789e36038bc841a1e2b486972421b983
refs/heads/master
1,629,609,492,867
1,609,907,263,000
1,609,907,263,000
240,943,034
43
3
null
1,598,210,062,000
1,581,877,668,000
Lean
UTF-8
Lean
false
false
7,869
lean
-- /- -- Copyright (c) 2020 Wojciech Nawrocki. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Wojciech Nawrocki -- -/ -- import category_theory.limits.shapes.finite_products -- import category_theory.limits.shapes.binary_products -- import category_theory.limits.shapes.terminal -- import tactic.rcases -- import pfin -- /-! # Stuff that should be in the catthy library. -/ -- namespace category_theory -- universes w w₁ -- -- def discrete.equiv_of_iso {J : Type w} {K : Type w₁} (h : J ≃ K) : (discrete J ≌ discrete K) := -- -- equivalence.mk -- -- (functor.of_function h.to_fun) -- C ⥤ D -- -- (functor.of_function h.inv_fun) -- D ⥤ C -- -- { hom := { -- -- app := λ X, begin -- -- apply eq_to_hom, -- -- simp only [functor.id_obj, functor.of_function_obj, functor.comp_obj], -- -- exact (h.left_inv X).symm, -- -- end, -- -- naturality' := λ X Y f, dec_trivial }, -- -- inv := { -- -- app := λ X, begin -- -- apply eq_to_hom, -- -- simp only [functor.id_obj, functor.of_function_obj, functor.comp_obj], -- -- exact h.left_inv X, -- -- end, -- -- naturality' := λ X Y f, dec_trivial }, -- -- hom_inv_id' := by ext1; exact dec_trivial, -- -- inv_hom_id' := by ext1; exact dec_trivial } -- -- { hom := { -- -- app := λ X, begin -- -- apply eq_to_hom, -- -- simp only [functor.id_obj, functor.of_function_obj, functor.comp_obj], -- -- exact h.right_inv X -- -- end, -- -- naturality' := λ X Y f, dec_trivial }, -- -- inv := { -- -- app := λ X, begin -- -- apply eq_to_hom, -- -- simp only [functor.id_obj, functor.of_function_obj, functor.comp_obj], -- -- exact (h.right_inv X).symm, -- -- end, -- -- naturality' := λ X Y f, dec_trivial }, -- -- hom_inv_id' := by ext1; exact dec_trivial, -- -- inv_hom_id' := by ext1; exact dec_trivial } -- namespace limits -- universes v u -- variables {C : Type u} [𝒞 : category.{v} C] -- include 𝒞 -- lemma prod.lift_uniq {X Y Z : C} [has_limit (pair X Y)] (f : Z ⟶ X) (g : Z ⟶ Y) (m : Z ⟶ X ⨯ Y) -- (hLeft : m ≫ prod.fst = f) (hRight : m ≫ prod.snd = g) -- : m = prod.lift f g := -- begin -- apply limit.hom_ext, -- intro j, -- cases hLeft, cases hRight, cases j, -- simp only [limit.lift_π, binary_fan.mk_π_app_left], -- simp only [limit.lift_π, binary_fan.mk_π_app_right], -- end -- end limits -- end category_theory -- /-! -- # Constructing finite products from binary products and a terminal object -- If a category has all binary products, and a terminal object, then it has all finite products. -- -/ -- namespace category_theory.limits -- open category_theory -- universes v u -- variables {C : Type u} [𝒞 : category.{v} C] -- include 𝒞 -- -- We hide the "implementation details" inside a namespace -- namespace has_finite_products_of_binary_products_and_terminal_object -- @[reducible] -- def match_statement_lol [has_binary_products.{v} C] -- {n : ℕ} (F: discrete (pfin (nat.succ n)) ⥤ C) (limF' : has_limit (discrete.lift pfin.succ ⋙ F)) -- : Π (j: pfin (nat.succ n)), F.obj ⟨0, nat.succ_pos n⟩ ⨯ limF'.cone.X ⟶ F.obj j -- | ⟨0, _⟩ := prod.fst -- | w@⟨nat.succ j, _⟩ := prod.snd ≫ -- limF'.cone.π.app (w.pred (λ h, nat.succ_ne_zero j (pfin.veq_of_eq h))) -- set_option eqn_compiler.zeta true -- def has_limit_for_pfin_diagram [has_binary_products.{v} C] [has_terminal.{v} C] -- : Π {n: ℕ} (F: (discrete (pfin n)) ⥤ C) -- , has_limit F -- | 0 F := -- -- In the base case, the category of cones over a diagram of shape ∅ is simply 𝒞, so -- -- the limit cone is 𝒞's terminal object. -- let absurdJ (x : pfin 0) : false := x.elim0 in -- let myCone : cone F := -- { X := terminal C, -- π := nat_trans.of_homs (λ j, (absurdJ j).elim) } in -- { cone := myCone, -- is_limit := -- { lift := λ s, terminal.from s.X -- , fac' := λ s j, (absurdJ j).elim -- , uniq' := λ s m h, dec_trivial } } -- | (nat.succ n) F := -- -- In the inductive case, we construct a limit cone with apex (F 0) ⨯ (apex of smaller limit cone) -- -- where the smaller cone is obtained from the below functor. -- let F' : discrete (pfin n) ⥤ C := discrete.lift pfin.succ ⋙ F in -- let limF' : has_limit F' := has_limit_for_pfin_diagram F' in -- let myCone : cone F := -- { X := (F.obj ⟨0, nat.succ_pos n⟩) ⨯ limF'.cone.X -- , π := nat_trans.of_homs (match_statement_lol F limF') } in -- TODO(WN): using an actual match statement here -- -- is hard to unfold later, but would obv be nicer. -- { cone := myCone, -- is_limit := -- { lift := λ s, -- -- Show that s.X is also the apex of a cone over F' .. -- let s' : cone F' := -- { X := s.X -- , π := nat_trans.of_homs (λ j, s.π.app j.succ) } in -- -- .. in order to get from s.X to limF'.cone.X in the right morphism -- -- using the fact that limF' is a limit cone over F'. -- prod.lift -- (s.π.app $ ⟨0, nat.succ_pos n⟩) -- (eq_to_hom rfl ≫ limF'.is_limit.lift s') -- -- Show that lift is in fact a morphism of cones from s into myCone. -- , fac' := λ s j, begin -- rcases j with ⟨j, hj⟩, cases j; -- simp only [category.id_comp, nat_trans.of_homs_app, eq_to_hom_refl, match_statement_lol, -- prod.lift_fst, limit.lift_π_assoc, is_limit.fac, nat_trans.of_homs_app, -- binary_fan.mk_π_app_right], congr -- end -- -- Show that lift is the unique morphism into myCone. -- , uniq' := λ s m h, begin -- have h0 := h ⟨0, nat.succ_pos n⟩, -- simp [match_statement_lol] at h0, -- let s' : cone F' := -- { X := s.X -- , π := nat_trans.of_homs (λ j, s.π.app j.succ) }, -- have hS : m ≫ prod.snd = eq_to_hom rfl ≫ limF'.is_limit.lift s', -- { -- m ≫ prod.snd is a morphism of cones over F' into limF'.X .. -- have hN : ∀ (j: discrete (pfin n)), (m ≫ prod.snd) ≫ limF'.cone.π.app j = s'.π.app j, -- { intro j, -- unfold_projs, simp [(h j.succ).symm], -- rcases j with ⟨j, hj⟩, refl }, -- -- .. and therefore unique. -- have hUniq' : m ≫ prod.snd = limF'.is_limit.lift s', -- from limF'.is_limit.uniq' s' (m ≫ prod.snd) hN, -- simp only [hUniq', category.id_comp, eq_to_hom_refl] }, -- exact prod.lift_uniq _ _ _ h0 hS -- end } } -- set_option eqn_compiler.zeta false -- end has_finite_products_of_binary_products_and_terminal_object -- open has_finite_products_of_binary_products_and_terminal_object -- -- TODO(WN): instance or def? Is there another way one might want to construct limits of shape pfin? -- instance has_limits_of_shape_pfin [has_binary_products.{v} C] [has_terminal.{v} C] (n : ℕ) -- : @has_limits_of_shape (discrete $ pfin n) _ C 𝒞 := -- ⟨λ F, has_limit_for_pfin_diagram F⟩ -- -- TODO(WN): trunc? #22 -- def has_trunc_finite_products [has_binary_products.{v} C] [has_terminal.{v} C] -- {J : Type v} [fintype J] [decidable_eq J] -- : trunc (has_limits_of_shape (discrete J) C) := -- trunc.lift_on (fintype.equiv_pfin J) -- (λ h, -- let hIso : discrete (pfin $ fintype.card J) ≌ discrete J := -- discrete.equiv_of_iso h.symm in -- let limsPfin : @has_limits_of_shape (discrete (pfin $ fintype.card J)) _ C 𝒞 := -- by apply_instance in -- trunc.mk $ has_limits_of_shape_of_equivalence hIso) -- (λ a b, trunc.eq _ _) -- end category_theory.limits
f5ff7b8bc8a2f3ef55d99e5ebd7cd72fbfa9cd7b
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/measure_theory/integral/integral_eq_improper.lean
bb5663185543cf0ed0702a0ae528d69ac5cf6b0f
[ "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
31,181
lean
/- Copyright (c) 2021 Anatole Dedecker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anatole Dedecker, Bhavik Mehta -/ import measure_theory.integral.interval_integral import order.filter.at_top_bot /-! # Links between an integral and its "improper" version In its current state, mathlib only knows how to talk about definite ("proper") integrals, in the sense that it treats integrals over `[x, +∞)` the same as it treats integrals over `[y, z]`. For example, the integral over `[1, +∞)` is **not** defined to be the limit of the integral over `[1, x]` as `x` tends to `+∞`, which is known as an **improper integral**. Indeed, the "proper" definition is stronger than the "improper" one. The usual counterexample is `x ↦ sin(x)/x`, which has an improper integral over `[1, +∞)` but no definite integral. Although definite integrals have better properties, they are hardly usable when it comes to computing integrals on unbounded sets, which is much easier using limits. Thus, in this file, we prove various ways of studying the proper integral by studying the improper one. ## Definitions The main definition of this file is `measure_theory.ae_cover`. It is a rather technical definition whose sole purpose is generalizing and factoring proofs. Given an index type `ι`, a countably generated filter `l` over `ι`, and an `ι`-indexed family `φ` of subsets of a measurable space `α` equipped with a measure `μ`, one should think of a hypothesis `hφ : ae_cover μ l φ` as a sufficient condition for being able to interpret `∫ x, f x ∂μ` (if it exists) as the limit of `∫ x in φ i, f x ∂μ` as `i` tends to `l`. When using this definition with a measure restricted to a set `s`, which happens fairly often, one should not try too hard to use a `ae_cover` of subsets of `s`, as it often makes proofs more complicated than necessary. See for example the proof of `measure_theory.integrable_on_Iic_of_interval_integral_norm_tendsto` where we use `(λ x, Ioi x)` as an `ae_cover` w.r.t. `μ.restrict (Iic b)`, instead of using `(λ x, Ioc x b)`. ## Main statements - `measure_theory.ae_cover.lintegral_tendsto_of_countably_generated` : if `φ` is a `ae_cover μ l`, where `l` is a countably generated filter, and if `f` is a measurable `ennreal`-valued function, then `∫⁻ x in φ n, f x ∂μ` tends to `∫⁻ x, f x ∂μ` as `n` tends to `l` - `measure_theory.ae_cover.integrable_of_integral_norm_tendsto` : if `φ` is a `ae_cover μ l`, where `l` is a countably generated filter, if `f` is measurable and integrable on each `φ n`, and if `∫ x in φ n, ∥f x∥ ∂μ` tends to some `I : ℝ` as n tends to `l`, then `f` is integrable - `measure_theory.ae_cover.integral_tendsto_of_countably_generated` : if `φ` is a `ae_cover μ l`, where `l` is a countably generated filter, and if `f` is measurable and integrable (globally), then `∫ x in φ n, f x ∂μ` tends to `∫ x, f x ∂μ` as `n` tends to `+∞`. We then specialize these lemmas to various use cases involving intervals, which are frequent in analysis. -/ open measure_theory filter set topological_space open_locale ennreal nnreal topological_space namespace measure_theory section ae_cover variables {α ι : Type*} [measurable_space α] (μ : measure α) (l : filter ι) /-- A sequence `φ` of subsets of `α` is a `ae_cover` w.r.t. a measure `μ` and a filter `l` if almost every point (w.r.t. `μ`) of `α` eventually belongs to `φ n` (w.r.t. `l`), and if each `φ n` is measurable. This definition is a technical way to avoid duplicating a lot of proofs. It should be thought of as a sufficient condition for being able to interpret `∫ x, f x ∂μ` (if it exists) as the limit of `∫ x in φ n, f x ∂μ` as `n` tends to `l`. See for example `measure_theory.ae_cover.lintegral_tendsto_of_countably_generated`, `measure_theory.ae_cover.integrable_of_integral_norm_tendsto` and `measure_theory.ae_cover.integral_tendsto_of_countably_generated`. -/ structure ae_cover (φ : ι → set α) : Prop := (ae_eventually_mem : ∀ᵐ x ∂μ, ∀ᶠ i in l, x ∈ φ i) (measurable : ∀ i, measurable_set $ φ i) variables {μ} {l} section preorder_α variables [preorder α] [topological_space α] [order_closed_topology α] [opens_measurable_space α] {a b : ι → α} (ha : tendsto a l at_bot) (hb : tendsto b l at_top) lemma ae_cover_Icc : ae_cover μ l (λ i, Icc (a i) (b i)) := { ae_eventually_mem := ae_of_all μ (λ x, (ha.eventually $ eventually_le_at_bot x).mp $ (hb.eventually $ eventually_ge_at_top x).mono $ λ i hbi hai, ⟨hai, hbi⟩ ), measurable := λ i, measurable_set_Icc } lemma ae_cover_Ici : ae_cover μ l (λ i, Ici $ a i) := { ae_eventually_mem := ae_of_all μ (λ x, (ha.eventually $ eventually_le_at_bot x).mono $ λ i hai, hai ), measurable := λ i, measurable_set_Ici } lemma ae_cover_Iic : ae_cover μ l (λ i, Iic $ b i) := { ae_eventually_mem := ae_of_all μ (λ x, (hb.eventually $ eventually_ge_at_top x).mono $ λ i hbi, hbi ), measurable := λ i, measurable_set_Iic } end preorder_α section linear_order_α variables [linear_order α] [topological_space α] [order_closed_topology α] [opens_measurable_space α] {a b : ι → α} (ha : tendsto a l at_bot) (hb : tendsto b l at_top) lemma ae_cover_Ioo [no_min_order α] [no_max_order α] : ae_cover μ l (λ i, Ioo (a i) (b i)) := { ae_eventually_mem := ae_of_all μ (λ x, (ha.eventually $ eventually_lt_at_bot x).mp $ (hb.eventually $ eventually_gt_at_top x).mono $ λ i hbi hai, ⟨hai, hbi⟩ ), measurable := λ i, measurable_set_Ioo } lemma ae_cover_Ioc [no_min_order α] : ae_cover μ l (λ i, Ioc (a i) (b i)) := { ae_eventually_mem := ae_of_all μ (λ x, (ha.eventually $ eventually_lt_at_bot x).mp $ (hb.eventually $ eventually_ge_at_top x).mono $ λ i hbi hai, ⟨hai, hbi⟩ ), measurable := λ i, measurable_set_Ioc } lemma ae_cover_Ico [no_max_order α] : ae_cover μ l (λ i, Ico (a i) (b i)) := { ae_eventually_mem := ae_of_all μ (λ x, (ha.eventually $ eventually_le_at_bot x).mp $ (hb.eventually $ eventually_gt_at_top x).mono $ λ i hbi hai, ⟨hai, hbi⟩ ), measurable := λ i, measurable_set_Ico } lemma ae_cover_Ioi [no_min_order α] : ae_cover μ l (λ i, Ioi $ a i) := { ae_eventually_mem := ae_of_all μ (λ x, (ha.eventually $ eventually_lt_at_bot x).mono $ λ i hai, hai ), measurable := λ i, measurable_set_Ioi } lemma ae_cover_Iio [no_max_order α] : ae_cover μ l (λ i, Iio $ b i) := { ae_eventually_mem := ae_of_all μ (λ x, (hb.eventually $ eventually_gt_at_top x).mono $ λ i hbi, hbi ), measurable := λ i, measurable_set_Iio } end linear_order_α section finite_intervals variables [linear_order α] [topological_space α] [order_closed_topology α] [opens_measurable_space α] {a b : ι → α} {A B : α} (ha : tendsto a l (𝓝 A)) (hb : tendsto b l (𝓝 B)) lemma ae_cover_Ioo_of_Icc : ae_cover (μ.restrict $ Ioo A B) l (λ i, Icc (a i) (b i)) := { ae_eventually_mem := (ae_restrict_iff' measurable_set_Ioo).mpr ( ae_of_all μ (λ x hx, (ha.eventually $ eventually_le_nhds hx.left).mp $ (hb.eventually $ eventually_ge_nhds hx.right).mono $ λ i hbi hai, ⟨hai, hbi⟩)), measurable := λ i, measurable_set_Icc, } lemma ae_cover_Ioo_of_Ico : ae_cover (μ.restrict $ Ioo A B) l (λ i, Ico (a i) (b i)) := { ae_eventually_mem := (ae_restrict_iff' measurable_set_Ioo).mpr ( ae_of_all μ (λ x hx, (ha.eventually $ eventually_le_nhds hx.left).mp $ (hb.eventually $ eventually_gt_nhds hx.right).mono $ λ i hbi hai, ⟨hai, hbi⟩)), measurable := λ i, measurable_set_Ico, } lemma ae_cover_Ioo_of_Ioc : ae_cover (μ.restrict $ Ioo A B) l (λ i, Ioc (a i) (b i)) := { ae_eventually_mem := (ae_restrict_iff' measurable_set_Ioo).mpr ( ae_of_all μ (λ x hx, (ha.eventually $ eventually_lt_nhds hx.left).mp $ (hb.eventually $ eventually_ge_nhds hx.right).mono $ λ i hbi hai, ⟨hai, hbi⟩)), measurable := λ i, measurable_set_Ioc, } lemma ae_cover_Ioo_of_Ioo : ae_cover (μ.restrict $ Ioo A B) l (λ i, Ioo (a i) (b i)) := { ae_eventually_mem := (ae_restrict_iff' measurable_set_Ioo).mpr ( ae_of_all μ (λ x hx, (ha.eventually $ eventually_lt_nhds hx.left).mp $ (hb.eventually $ eventually_gt_nhds hx.right).mono $ λ i hbi hai, ⟨hai, hbi⟩)), measurable := λ i, measurable_set_Ioo, } variables [has_no_atoms μ] lemma ae_cover_Ioc_of_Icc (ha : tendsto a l (𝓝 A)) (hb : tendsto b l (𝓝 B)) : ae_cover (μ.restrict $ Ioc A B) l (λ i, Icc (a i) (b i)) := by simp [measure.restrict_congr_set Ioo_ae_eq_Ioc.symm, ae_cover_Ioo_of_Icc ha hb] lemma ae_cover_Ioc_of_Ico (ha : tendsto a l (𝓝 A)) (hb : tendsto b l (𝓝 B)) : ae_cover (μ.restrict $ Ioc A B) l (λ i, Ico (a i) (b i)) := by simp [measure.restrict_congr_set Ioo_ae_eq_Ioc.symm, ae_cover_Ioo_of_Ico ha hb] lemma ae_cover_Ioc_of_Ioc (ha : tendsto a l (𝓝 A)) (hb : tendsto b l (𝓝 B)) : ae_cover (μ.restrict $ Ioc A B) l (λ i, Ioc (a i) (b i)) := by simp [measure.restrict_congr_set Ioo_ae_eq_Ioc.symm, ae_cover_Ioo_of_Ioc ha hb] lemma ae_cover_Ioc_of_Ioo (ha : tendsto a l (𝓝 A)) (hb : tendsto b l (𝓝 B)) : ae_cover (μ.restrict $ Ioc A B) l (λ i, Ioo (a i) (b i)) := by simp [measure.restrict_congr_set Ioo_ae_eq_Ioc.symm, ae_cover_Ioo_of_Ioo ha hb] lemma ae_cover_Ico_of_Icc (ha : tendsto a l (𝓝 A)) (hb : tendsto b l (𝓝 B)) : ae_cover (μ.restrict $ Ico A B) l (λ i, Icc (a i) (b i)) := by simp [measure.restrict_congr_set Ioo_ae_eq_Ico.symm, ae_cover_Ioo_of_Icc ha hb] lemma ae_cover_Ico_of_Ico (ha : tendsto a l (𝓝 A)) (hb : tendsto b l (𝓝 B)) : ae_cover (μ.restrict $ Ico A B) l (λ i, Ico (a i) (b i)) := by simp [measure.restrict_congr_set Ioo_ae_eq_Ico.symm, ae_cover_Ioo_of_Ico ha hb] lemma ae_cover_Ico_of_Ioc (ha : tendsto a l (𝓝 A)) (hb : tendsto b l (𝓝 B)) : ae_cover (μ.restrict $ Ico A B) l (λ i, Ioc (a i) (b i)) := by simp [measure.restrict_congr_set Ioo_ae_eq_Ico.symm, ae_cover_Ioo_of_Ioc ha hb] lemma ae_cover_Ico_of_Ioo (ha : tendsto a l (𝓝 A)) (hb : tendsto b l (𝓝 B)) : ae_cover (μ.restrict $ Ico A B) l (λ i, Ioo (a i) (b i)) := by simp [measure.restrict_congr_set Ioo_ae_eq_Ico.symm, ae_cover_Ioo_of_Ioo ha hb] lemma ae_cover_Icc_of_Icc (ha : tendsto a l (𝓝 A)) (hb : tendsto b l (𝓝 B)) : ae_cover (μ.restrict $ Icc A B) l (λ i, Icc (a i) (b i)) := by simp [measure.restrict_congr_set Ioo_ae_eq_Icc.symm, ae_cover_Ioo_of_Icc ha hb] lemma ae_cover_Icc_of_Ico (ha : tendsto a l (𝓝 A)) (hb : tendsto b l (𝓝 B)) : ae_cover (μ.restrict $ Icc A B) l (λ i, Ico (a i) (b i)) := by simp [measure.restrict_congr_set Ioo_ae_eq_Icc.symm, ae_cover_Ioo_of_Ico ha hb] lemma ae_cover_Icc_of_Ioc (ha : tendsto a l (𝓝 A)) (hb : tendsto b l (𝓝 B)) : ae_cover (μ.restrict $ Icc A B) l (λ i, Ioc (a i) (b i)) := by simp [measure.restrict_congr_set Ioo_ae_eq_Icc.symm, ae_cover_Ioo_of_Ioc ha hb] lemma ae_cover_Icc_of_Ioo (ha : tendsto a l (𝓝 A)) (hb : tendsto b l (𝓝 B)) : ae_cover (μ.restrict $ Icc A B) l (λ i, Ioo (a i) (b i)) := by simp [measure.restrict_congr_set Ioo_ae_eq_Icc.symm, ae_cover_Ioo_of_Ioo ha hb] end finite_intervals lemma ae_cover.restrict {φ : ι → set α} (hφ : ae_cover μ l φ) {s : set α} : ae_cover (μ.restrict s) l φ := { ae_eventually_mem := ae_restrict_of_ae hφ.ae_eventually_mem, measurable := hφ.measurable } lemma ae_cover_restrict_of_ae_imp {s : set α} {φ : ι → set α} (hs : measurable_set s) (ae_eventually_mem : ∀ᵐ x ∂μ, x ∈ s → ∀ᶠ n in l, x ∈ φ n) (measurable : ∀ n, measurable_set $ φ n) : ae_cover (μ.restrict s) l φ := { ae_eventually_mem := by rwa ae_restrict_iff' hs, measurable := measurable } lemma ae_cover.inter_restrict {φ : ι → set α} (hφ : ae_cover μ l φ) {s : set α} (hs : measurable_set s) : ae_cover (μ.restrict s) l (λ i, φ i ∩ s) := ae_cover_restrict_of_ae_imp hs (hφ.ae_eventually_mem.mono (λ x hx hxs, hx.mono $ λ i hi, ⟨hi, hxs⟩)) (λ i, (hφ.measurable i).inter hs) lemma ae_cover.ae_tendsto_indicator {β : Type*} [has_zero β] [topological_space β] (f : α → β) {φ : ι → set α} (hφ : ae_cover μ l φ) : ∀ᵐ x ∂μ, tendsto (λ i, (φ i).indicator f x) l (𝓝 $ f x) := hφ.ae_eventually_mem.mono (λ x hx, tendsto_const_nhds.congr' $ hx.mono $ λ n hn, (indicator_of_mem hn _).symm) lemma ae_cover.ae_measurable {β : Type*} [measurable_space β] [l.is_countably_generated] [l.ne_bot] {f : α → β} {φ : ι → set α} (hφ : ae_cover μ l φ) (hfm : ∀ i, ae_measurable f (μ.restrict $ φ i)) : ae_measurable f μ := begin obtain ⟨u, hu⟩ := l.exists_seq_tendsto, have := ae_measurable_Union_iff.mpr (λ (n : ℕ), hfm (u n)), rwa measure.restrict_eq_self_of_ae_mem at this, filter_upwards [hφ.ae_eventually_mem] with x hx using let ⟨i, hi⟩ := (hu.eventually hx).exists in mem_Union.mpr ⟨i, hi⟩ end lemma ae_cover.ae_strongly_measurable {β : Type*} [topological_space β] [pseudo_metrizable_space β] [l.is_countably_generated] [l.ne_bot] {f : α → β} {φ : ι → set α} (hφ : ae_cover μ l φ) (hfm : ∀ i, ae_strongly_measurable f (μ.restrict $ φ i)) : ae_strongly_measurable f μ := begin obtain ⟨u, hu⟩ := l.exists_seq_tendsto, have := ae_strongly_measurable_Union_iff.mpr (λ (n : ℕ), hfm (u n)), rwa measure.restrict_eq_self_of_ae_mem at this, filter_upwards [hφ.ae_eventually_mem] with x hx using let ⟨i, hi⟩ := (hu.eventually hx).exists in mem_Union.mpr ⟨i, hi⟩ end end ae_cover lemma ae_cover.comp_tendsto {α ι ι' : Type*} [measurable_space α] {μ : measure α} {l : filter ι} {l' : filter ι'} {φ : ι → set α} (hφ : ae_cover μ l φ) {u : ι' → ι} (hu : tendsto u l' l) : ae_cover μ l' (φ ∘ u) := { ae_eventually_mem := hφ.ae_eventually_mem.mono (λ x hx, hu.eventually hx), measurable := λ i, hφ.measurable (u i) } section ae_cover_Union_Inter_encodable variables {α ι : Type*} [encodable ι] [measurable_space α] {μ : measure α} lemma ae_cover.bUnion_Iic_ae_cover [preorder ι] {φ : ι → set α} (hφ : ae_cover μ at_top φ) : ae_cover μ at_top (λ (n : ι), ⋃ k (h : k ∈ Iic n), φ k) := { ae_eventually_mem := hφ.ae_eventually_mem.mono (λ x h, h.mono (λ i hi, mem_bUnion right_mem_Iic hi)), measurable := λ i, measurable_set.bUnion (to_countable _) (λ n _, hφ.measurable n) } lemma ae_cover.bInter_Ici_ae_cover [semilattice_sup ι] [nonempty ι] {φ : ι → set α} (hφ : ae_cover μ at_top φ) : ae_cover μ at_top (λ (n : ι), ⋂ k (h : k ∈ Ici n), φ k) := { ae_eventually_mem := hφ.ae_eventually_mem.mono begin intros x h, rw eventually_at_top at *, rcases h with ⟨i, hi⟩, use i, intros j hj, exact mem_bInter (λ k hk, hi k (le_trans hj hk)), end, measurable := λ i, measurable_set.bInter (to_countable _) (λ n _, hφ.measurable n) } end ae_cover_Union_Inter_encodable section lintegral variables {α ι : Type*} [measurable_space α] {μ : measure α} {l : filter ι} private lemma lintegral_tendsto_of_monotone_of_nat {φ : ℕ → set α} (hφ : ae_cover μ at_top φ) (hmono : monotone φ) {f : α → ℝ≥0∞} (hfm : ae_measurable f μ) : tendsto (λ i, ∫⁻ x in φ i, f x ∂μ) at_top (𝓝 $ ∫⁻ x, f x ∂μ) := let F := λ n, (φ n).indicator f in have key₁ : ∀ n, ae_measurable (F n) μ, from λ n, hfm.indicator (hφ.measurable n), have key₂ : ∀ᵐ (x : α) ∂μ, monotone (λ n, F n x), from ae_of_all _ (λ x i j hij, indicator_le_indicator_of_subset (hmono hij) (λ x, zero_le $ f x) x), have key₃ : ∀ᵐ (x : α) ∂μ, tendsto (λ n, F n x) at_top (𝓝 (f x)), from hφ.ae_tendsto_indicator f, (lintegral_tendsto_of_tendsto_of_monotone key₁ key₂ key₃).congr (λ n, lintegral_indicator f (hφ.measurable n)) lemma ae_cover.lintegral_tendsto_of_nat {φ : ℕ → set α} (hφ : ae_cover μ at_top φ) {f : α → ℝ≥0∞} (hfm : ae_measurable f μ) : tendsto (λ i, ∫⁻ x in φ i, f x ∂μ) at_top (𝓝 $ ∫⁻ x, f x ∂μ) := begin have lim₁ := lintegral_tendsto_of_monotone_of_nat (hφ.bInter_Ici_ae_cover) (λ i j hij, bInter_subset_bInter_left (Ici_subset_Ici.mpr hij)) hfm, have lim₂ := lintegral_tendsto_of_monotone_of_nat (hφ.bUnion_Iic_ae_cover) (λ i j hij, bUnion_subset_bUnion_left (Iic_subset_Iic.mpr hij)) hfm, have le₁ := λ n, lintegral_mono_set (bInter_subset_of_mem left_mem_Ici), have le₂ := λ n, lintegral_mono_set (subset_bUnion_of_mem right_mem_Iic), exact tendsto_of_tendsto_of_tendsto_of_le_of_le lim₁ lim₂ le₁ le₂ end lemma ae_cover.lintegral_tendsto_of_countably_generated [l.is_countably_generated] {φ : ι → set α} (hφ : ae_cover μ l φ) {f : α → ℝ≥0∞} (hfm : ae_measurable f μ) : tendsto (λ i, ∫⁻ x in φ i, f x ∂μ) l (𝓝 $ ∫⁻ x, f x ∂μ) := tendsto_of_seq_tendsto (λ u hu, (hφ.comp_tendsto hu).lintegral_tendsto_of_nat hfm) lemma ae_cover.lintegral_eq_of_tendsto [l.ne_bot] [l.is_countably_generated] {φ : ι → set α} (hφ : ae_cover μ l φ) {f : α → ℝ≥0∞} (I : ℝ≥0∞) (hfm : ae_measurable f μ) (htendsto : tendsto (λ i, ∫⁻ x in φ i, f x ∂μ) l (𝓝 I)) : ∫⁻ x, f x ∂μ = I := tendsto_nhds_unique (hφ.lintegral_tendsto_of_countably_generated hfm) htendsto lemma ae_cover.supr_lintegral_eq_of_countably_generated [nonempty ι] [l.ne_bot] [l.is_countably_generated] {φ : ι → set α} (hφ : ae_cover μ l φ) {f : α → ℝ≥0∞} (hfm : ae_measurable f μ) : (⨆ (i : ι), ∫⁻ x in φ i, f x ∂μ) = ∫⁻ x, f x ∂μ := begin have := hφ.lintegral_tendsto_of_countably_generated hfm, refine csupr_eq_of_forall_le_of_forall_lt_exists_gt (λ i, lintegral_mono' measure.restrict_le_self le_rfl) (λ w hw, _), rcases exists_between hw with ⟨m, hm₁, hm₂⟩, rcases (eventually_ge_of_tendsto_gt hm₂ this).exists with ⟨i, hi⟩, exact ⟨i, lt_of_lt_of_le hm₁ hi⟩, end end lintegral section integrable variables {α ι E : Type*} [measurable_space α] {μ : measure α} {l : filter ι} [normed_add_comm_group E] lemma ae_cover.integrable_of_lintegral_nnnorm_bounded [l.ne_bot] [l.is_countably_generated] {φ : ι → set α} (hφ : ae_cover μ l φ) {f : α → E} (I : ℝ) (hfm : ae_strongly_measurable f μ) (hbounded : ∀ᶠ i in l, ∫⁻ x in φ i, ∥f x∥₊ ∂μ ≤ ennreal.of_real I) : integrable f μ := begin refine ⟨hfm, (le_of_tendsto _ hbounded).trans_lt ennreal.of_real_lt_top⟩, exact hφ.lintegral_tendsto_of_countably_generated hfm.ennnorm end lemma ae_cover.integrable_of_lintegral_nnnorm_tendsto [l.ne_bot] [l.is_countably_generated] {φ : ι → set α} (hφ : ae_cover μ l φ) {f : α → E} (I : ℝ) (hfm : ae_strongly_measurable f μ) (htendsto : tendsto (λ i, ∫⁻ x in φ i, ∥f x∥₊ ∂μ) l (𝓝 $ ennreal.of_real I)) : integrable f μ := begin refine hφ.integrable_of_lintegral_nnnorm_bounded (max 1 (I + 1)) hfm _, refine htendsto.eventually (ge_mem_nhds _), refine (ennreal.of_real_lt_of_real_iff (lt_max_of_lt_left zero_lt_one)).2 _, exact lt_max_of_lt_right (lt_add_one I), end lemma ae_cover.integrable_of_lintegral_nnnorm_bounded' [l.ne_bot] [l.is_countably_generated] {φ : ι → set α} (hφ : ae_cover μ l φ) {f : α → E} (I : ℝ≥0) (hfm : ae_strongly_measurable f μ) (hbounded : ∀ᶠ i in l, ∫⁻ x in φ i, ∥f x∥₊ ∂μ ≤ I) : integrable f μ := hφ.integrable_of_lintegral_nnnorm_bounded I hfm (by simpa only [ennreal.of_real_coe_nnreal] using hbounded) lemma ae_cover.integrable_of_lintegral_nnnorm_tendsto' [l.ne_bot] [l.is_countably_generated] {φ : ι → set α} (hφ : ae_cover μ l φ) {f : α → E} (I : ℝ≥0) (hfm : ae_strongly_measurable f μ) (htendsto : tendsto (λ i, ∫⁻ x in φ i, ∥f x∥₊ ∂μ) l (𝓝 I)) : integrable f μ := hφ.integrable_of_lintegral_nnnorm_tendsto I hfm (by simpa only [ennreal.of_real_coe_nnreal] using htendsto) lemma ae_cover.integrable_of_integral_norm_bounded [l.ne_bot] [l.is_countably_generated] {φ : ι → set α} (hφ : ae_cover μ l φ) {f : α → E} (I : ℝ) (hfi : ∀ i, integrable_on f (φ i) μ) (hbounded : ∀ᶠ i in l, ∫ x in φ i, ∥f x∥ ∂μ ≤ I) : integrable f μ := begin have hfm : ae_strongly_measurable f μ := hφ.ae_strongly_measurable (λ i, (hfi i).ae_strongly_measurable), refine hφ.integrable_of_lintegral_nnnorm_bounded I hfm _, conv at hbounded in (integral _ _) { rw integral_eq_lintegral_of_nonneg_ae (ae_of_all _ (λ x, @norm_nonneg E _ (f x))) hfm.norm.restrict }, conv at hbounded in (ennreal.of_real _) { dsimp, rw ← coe_nnnorm, rw ennreal.of_real_coe_nnreal }, refine hbounded.mono (λ i hi, _), rw ←ennreal.of_real_to_real (ne_top_of_lt (hfi i).2), apply ennreal.of_real_le_of_real hi, end lemma ae_cover.integrable_of_integral_norm_tendsto [l.ne_bot] [l.is_countably_generated] {φ : ι → set α} (hφ : ae_cover μ l φ) {f : α → E} (I : ℝ) (hfi : ∀ i, integrable_on f (φ i) μ) (htendsto : tendsto (λ i, ∫ x in φ i, ∥f x∥ ∂μ) l (𝓝 I)) : integrable f μ := let ⟨I', hI'⟩ := htendsto.is_bounded_under_le in hφ.integrable_of_integral_norm_bounded I' hfi hI' lemma ae_cover.integrable_of_integral_bounded_of_nonneg_ae [l.ne_bot] [l.is_countably_generated] {φ : ι → set α} (hφ : ae_cover μ l φ) {f : α → ℝ} (I : ℝ) (hfi : ∀ i, integrable_on f (φ i) μ) (hnng : ∀ᵐ x ∂μ, 0 ≤ f x) (hbounded : ∀ᶠ i in l, ∫ x in φ i, f x ∂μ ≤ I) : integrable f μ := hφ.integrable_of_integral_norm_bounded I hfi $ hbounded.mono $ λ i hi, (integral_congr_ae $ ae_restrict_of_ae $ hnng.mono $ λ x, real.norm_of_nonneg).le.trans hi lemma ae_cover.integrable_of_integral_tendsto_of_nonneg_ae [l.ne_bot] [l.is_countably_generated] {φ : ι → set α} (hφ : ae_cover μ l φ) {f : α → ℝ} (I : ℝ) (hfi : ∀ i, integrable_on f (φ i) μ) (hnng : ∀ᵐ x ∂μ, 0 ≤ f x) (htendsto : tendsto (λ i, ∫ x in φ i, f x ∂μ) l (𝓝 I)) : integrable f μ := let ⟨I', hI'⟩ := htendsto.is_bounded_under_le in hφ.integrable_of_integral_bounded_of_nonneg_ae I' hfi hnng hI' end integrable section integral variables {α ι E : Type*} [measurable_space α] {μ : measure α} {l : filter ι} [normed_add_comm_group E] [normed_space ℝ E] [complete_space E] lemma ae_cover.integral_tendsto_of_countably_generated [l.is_countably_generated] {φ : ι → set α} (hφ : ae_cover μ l φ) {f : α → E} (hfi : integrable f μ) : tendsto (λ i, ∫ x in φ i, f x ∂μ) l (𝓝 $ ∫ x, f x ∂μ) := suffices h : tendsto (λ i, ∫ (x : α), (φ i).indicator f x ∂μ) l (𝓝 (∫ (x : α), f x ∂μ)), by { convert h, ext n, rw integral_indicator (hφ.measurable n) }, tendsto_integral_filter_of_dominated_convergence (λ x, ∥f x∥) (eventually_of_forall $ λ i, hfi.ae_strongly_measurable.indicator $ hφ.measurable i) (eventually_of_forall $ λ i, ae_of_all _ $ λ x, norm_indicator_le_norm_self _ _) hfi.norm (hφ.ae_tendsto_indicator f) /-- Slight reformulation of `measure_theory.ae_cover.integral_tendsto_of_countably_generated`. -/ lemma ae_cover.integral_eq_of_tendsto [l.ne_bot] [l.is_countably_generated] {φ : ι → set α} (hφ : ae_cover μ l φ) {f : α → E} (I : E) (hfi : integrable f μ) (h : tendsto (λ n, ∫ x in φ n, f x ∂μ) l (𝓝 I)) : ∫ x, f x ∂μ = I := tendsto_nhds_unique (hφ.integral_tendsto_of_countably_generated hfi) h lemma ae_cover.integral_eq_of_tendsto_of_nonneg_ae [l.ne_bot] [l.is_countably_generated] {φ : ι → set α} (hφ : ae_cover μ l φ) {f : α → ℝ} (I : ℝ) (hnng : 0 ≤ᵐ[μ] f) (hfi : ∀ n, integrable_on f (φ n) μ) (htendsto : tendsto (λ n, ∫ x in φ n, f x ∂μ) l (𝓝 I)) : ∫ x, f x ∂μ = I := have hfi' : integrable f μ, from hφ.integrable_of_integral_tendsto_of_nonneg_ae I hfi hnng htendsto, hφ.integral_eq_of_tendsto I hfi' htendsto end integral section integrable_of_interval_integral variables {ι E : Type*} {μ : measure ℝ} {l : filter ι} [filter.ne_bot l] [is_countably_generated l] [normed_add_comm_group E] {a b : ι → ℝ} {f : ℝ → E} lemma integrable_of_interval_integral_norm_bounded (I : ℝ) (hfi : ∀ i, integrable_on f (Ioc (a i) (b i)) μ) (ha : tendsto a l at_bot) (hb : tendsto b l at_top) (h : ∀ᶠ i in l, ∫ x in a i .. b i, ∥f x∥ ∂μ ≤ I) : integrable f μ := begin have hφ : ae_cover μ l _ := ae_cover_Ioc ha hb, refine hφ.integrable_of_integral_norm_bounded I hfi (h.mp _), filter_upwards [ha.eventually (eventually_le_at_bot 0), hb.eventually (eventually_ge_at_top 0)] with i hai hbi ht, rwa ←interval_integral.integral_of_le (hai.trans hbi) end lemma integrable_of_interval_integral_norm_tendsto (I : ℝ) (hfi : ∀ i, integrable_on f (Ioc (a i) (b i)) μ) (ha : tendsto a l at_bot) (hb : tendsto b l at_top) (h : tendsto (λ i, ∫ x in a i .. b i, ∥f x∥ ∂μ) l (𝓝 I)) : integrable f μ := let ⟨I', hI'⟩ := h.is_bounded_under_le in integrable_of_interval_integral_norm_bounded I' hfi ha hb hI' lemma integrable_on_Iic_of_interval_integral_norm_bounded (I b : ℝ) (hfi : ∀ i, integrable_on f (Ioc (a i) b) μ) (ha : tendsto a l at_bot) (h : ∀ᶠ i in l, (∫ x in a i .. b, ∥f x∥ ∂μ) ≤ I) : integrable_on f (Iic b) μ := begin have hφ : ae_cover (μ.restrict $ Iic b) l _ := ae_cover_Ioi ha, have hfi : ∀ i, integrable_on f (Ioi (a i)) (μ.restrict $ Iic b), { intro i, rw [integrable_on, measure.restrict_restrict (hφ.measurable i)], exact hfi i }, refine hφ.integrable_of_integral_norm_bounded I hfi (h.mp _), filter_upwards [ha.eventually (eventually_le_at_bot b)] with i hai, rw [interval_integral.integral_of_le hai, measure.restrict_restrict (hφ.measurable i)], exact id end lemma integrable_on_Iic_of_interval_integral_norm_tendsto (I b : ℝ) (hfi : ∀ i, integrable_on f (Ioc (a i) b) μ) (ha : tendsto a l at_bot) (h : tendsto (λ i, ∫ x in a i .. b, ∥f x∥ ∂μ) l (𝓝 I)) : integrable_on f (Iic b) μ := let ⟨I', hI'⟩ := h.is_bounded_under_le in integrable_on_Iic_of_interval_integral_norm_bounded I' b hfi ha hI' lemma integrable_on_Ioi_of_interval_integral_norm_bounded (I a : ℝ) (hfi : ∀ i, integrable_on f (Ioc a (b i)) μ) (hb : tendsto b l at_top) (h : ∀ᶠ i in l, (∫ x in a .. b i, ∥f x∥ ∂μ) ≤ I) : integrable_on f (Ioi a) μ := begin have hφ : ae_cover (μ.restrict $ Ioi a) l _ := ae_cover_Iic hb, have hfi : ∀ i, integrable_on f (Iic (b i)) (μ.restrict $ Ioi a), { intro i, rw [integrable_on, measure.restrict_restrict (hφ.measurable i), inter_comm], exact hfi i }, refine hφ.integrable_of_integral_norm_bounded I hfi (h.mp _), filter_upwards [hb.eventually (eventually_ge_at_top a)] with i hbi, rw [interval_integral.integral_of_le hbi, measure.restrict_restrict (hφ.measurable i), inter_comm], exact id end lemma integrable_on_Ioi_of_interval_integral_norm_tendsto (I a : ℝ) (hfi : ∀ i, integrable_on f (Ioc a (b i)) μ) (hb : tendsto b l at_top) (h : tendsto (λ i, ∫ x in a .. b i, ∥f x∥ ∂μ) l (𝓝 $ I)) : integrable_on f (Ioi a) μ := let ⟨I', hI'⟩ := h.is_bounded_under_le in integrable_on_Ioi_of_interval_integral_norm_bounded I' a hfi hb hI' lemma integrable_on_Ioc_of_interval_integral_norm_bounded {I a₀ b₀ : ℝ} (hfi : ∀ i, integrable_on f $ Ioc (a i) (b i)) (ha : tendsto a l $ 𝓝 a₀) (hb : tendsto b l $ 𝓝 b₀) (h : ∀ᶠ i in l, (∫ x in Ioc (a i) (b i), ∥f x∥) ≤ I) : integrable_on f (Ioc a₀ b₀) := begin refine (ae_cover_Ioc_of_Ioc ha hb).integrable_of_integral_norm_bounded I (λ i, (hfi i).restrict measurable_set_Ioc) (eventually.mono h _), intros i hi, simp only [measure.restrict_restrict measurable_set_Ioc], refine le_trans (set_integral_mono_set (hfi i).norm _ _) hi, { apply ae_of_all, simp only [pi.zero_apply, norm_nonneg, forall_const] }, { apply ae_of_all, intros c hc, exact hc.1 }, end lemma integrable_on_Ioc_of_interval_integral_norm_bounded_left {I a₀ b : ℝ} (hfi : ∀ i, integrable_on f $ Ioc (a i) b) (ha : tendsto a l $ 𝓝 a₀) (h : ∀ᶠ i in l, (∫ x in Ioc (a i) b, ∥f x∥ ) ≤ I) : integrable_on f (Ioc a₀ b) := integrable_on_Ioc_of_interval_integral_norm_bounded hfi ha tendsto_const_nhds h lemma integrable_on_Ioc_of_interval_integral_norm_bounded_right {I a b₀ : ℝ} (hfi : ∀ i, integrable_on f $ Ioc a (b i)) (hb : tendsto b l $ 𝓝 b₀) (h : ∀ᶠ i in l, (∫ x in Ioc a (b i), ∥f x∥ ) ≤ I) : integrable_on f (Ioc a b₀) := integrable_on_Ioc_of_interval_integral_norm_bounded hfi tendsto_const_nhds hb h end integrable_of_interval_integral section integral_of_interval_integral variables {ι E : Type*} {μ : measure ℝ} {l : filter ι} [is_countably_generated l] [normed_add_comm_group E] [normed_space ℝ E] [complete_space E] {a b : ι → ℝ} {f : ℝ → E} lemma interval_integral_tendsto_integral (hfi : integrable f μ) (ha : tendsto a l at_bot) (hb : tendsto b l at_top) : tendsto (λ i, ∫ x in a i .. b i, f x ∂μ) l (𝓝 $ ∫ x, f x ∂μ) := begin let φ := λ i, Ioc (a i) (b i), have hφ : ae_cover μ l φ := ae_cover_Ioc ha hb, refine (hφ.integral_tendsto_of_countably_generated hfi).congr' _, filter_upwards [ha.eventually (eventually_le_at_bot 0), hb.eventually (eventually_ge_at_top 0)] with i hai hbi, exact (interval_integral.integral_of_le (hai.trans hbi)).symm end lemma interval_integral_tendsto_integral_Iic (b : ℝ) (hfi : integrable_on f (Iic b) μ) (ha : tendsto a l at_bot) : tendsto (λ i, ∫ x in a i .. b, f x ∂μ) l (𝓝 $ ∫ x in Iic b, f x ∂μ) := begin let φ := λ i, Ioi (a i), have hφ : ae_cover (μ.restrict $ Iic b) l φ := ae_cover_Ioi ha, refine (hφ.integral_tendsto_of_countably_generated hfi).congr' _, filter_upwards [ha.eventually (eventually_le_at_bot $ b)] with i hai, rw [interval_integral.integral_of_le hai, measure.restrict_restrict (hφ.measurable i)], refl, end lemma interval_integral_tendsto_integral_Ioi (a : ℝ) (hfi : integrable_on f (Ioi a) μ) (hb : tendsto b l at_top) : tendsto (λ i, ∫ x in a .. b i, f x ∂μ) l (𝓝 $ ∫ x in Ioi a, f x ∂μ) := begin let φ := λ i, Iic (b i), have hφ : ae_cover (μ.restrict $ Ioi a) l φ := ae_cover_Iic hb, refine (hφ.integral_tendsto_of_countably_generated hfi).congr' _, filter_upwards [hb.eventually (eventually_ge_at_top $ a)] with i hbi, rw [interval_integral.integral_of_le hbi, measure.restrict_restrict (hφ.measurable i), inter_comm], refl, end end integral_of_interval_integral end measure_theory
69eade6a851ef4657f15db12fda607a872c49fdf
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/measure_theory/measure/measure_space_def.lean
06b26e3f2369ea72a2c0a163933b9a1d1c2330aa
[ "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
20,907
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import measure_theory.measure.outer_measure import order.filter.countable_Inter import data.set.accumulate /-! # Measure spaces This file defines measure spaces, the almost-everywhere filter and ae_measurable functions. See `measure_theory.measure_space` for their properties and for extended documentation. Given a measurable space `α`, a measure on `α` is a function that sends measurable sets to the extended nonnegative reals that satisfies the following conditions: 1. `μ ∅ = 0`; 2. `μ` is countably additive. This means that the measure of a countable union of pairwise disjoint sets is equal to the measure of the individual sets. Every measure can be canonically extended to an outer measure, so that it assigns values to all subsets, not just the measurable subsets. On the other hand, a measure that is countably additive on measurable sets can be restricted to measurable sets to obtain a measure. In this file a measure is defined to be an outer measure that is countably additive on measurable sets, with the additional assumption that the outer measure is the canonical extension of the restricted measure. Measures on `α` form a complete lattice, and are closed under scalar multiplication with `ℝ≥0∞`. ## Implementation notes Given `μ : measure α`, `μ s` is the value of the *outer measure* applied to `s`. This conveniently allows us to apply the measure to sets without proving that they are measurable. We get countable subadditivity for all sets, but only countable additivity for measurable sets. See the documentation of `measure_theory.measure_space` for ways to construct measures and proving that two measure are equal. A `measure_space` is a class that is a measurable space with a canonical measure. The measure is denoted `volume`. This file does not import `measure_theory.measurable_space`, but only `measurable_space_def`. ## References * <https://en.wikipedia.org/wiki/Measure_(mathematics)> * <https://en.wikipedia.org/wiki/Almost_everywhere> ## Tags measure, almost everywhere, measure space -/ noncomputable theory open classical set filter (hiding map) function measurable_space open_locale classical topological_space big_operators filter ennreal nnreal variables {α β γ δ ι : Type*} namespace measure_theory /-- A measure is defined to be an outer measure that is countably additive on measurable sets, with the additional assumption that the outer measure is the canonical extension of the restricted measure. -/ structure measure (α : Type*) [measurable_space α] extends outer_measure α := (m_Union ⦃f : ℕ → set α⦄ : (∀ i, measurable_set (f i)) → pairwise (disjoint on f) → measure_of (⋃ i, f i) = ∑' i, measure_of (f i)) (trimmed : to_outer_measure.trim = to_outer_measure) /-- Measure projections for a measure space. For measurable sets this returns the measure assigned by the `measure_of` field in `measure`. But we can extend this to _all_ sets, but using the outer measure. This gives us monotonicity and subadditivity for all sets. -/ instance measure.has_coe_to_fun [measurable_space α] : has_coe_to_fun (measure α) (λ _, set α → ℝ≥0∞) := ⟨λ m, m.to_outer_measure⟩ section variables [measurable_space α] {μ μ₁ μ₂ : measure α} {s s₁ s₂ t : set α} namespace measure /-! ### General facts about measures -/ /-- Obtain a measure by giving a countably additive function that sends `∅` to `0`. -/ def of_measurable (m : Π (s : set α), measurable_set s → ℝ≥0∞) (m0 : m ∅ measurable_set.empty = 0) (mU : ∀ {{f : ℕ → set α}} (h : ∀ i, measurable_set (f i)), pairwise (disjoint on f) → m (⋃ i, f i) (measurable_set.Union h) = ∑' i, m (f i) (h i)) : measure α := { m_Union := λ f hf hd, show induced_outer_measure m _ m0 (Union f) = ∑' i, induced_outer_measure m _ m0 (f i), begin rw [induced_outer_measure_eq m0 mU, mU hf hd], congr, funext n, rw induced_outer_measure_eq m0 mU end, trimmed := show (induced_outer_measure m _ m0).trim = induced_outer_measure m _ m0, begin unfold outer_measure.trim, congr, funext s hs, exact induced_outer_measure_eq m0 mU hs end, ..induced_outer_measure m _ m0 } lemma of_measurable_apply {m : Π (s : set α), measurable_set s → ℝ≥0∞} {m0 : m ∅ measurable_set.empty = 0} {mU : ∀ {{f : ℕ → set α}} (h : ∀ i, measurable_set (f i)), pairwise (disjoint on f) → m (⋃ i, f i) (measurable_set.Union h) = ∑' i, m (f i) (h i)} (s : set α) (hs : measurable_set s) : of_measurable m m0 mU s = m s hs := induced_outer_measure_eq m0 mU hs lemma to_outer_measure_injective : injective (to_outer_measure : measure α → outer_measure α) := λ ⟨m₁, u₁, h₁⟩ ⟨m₂, u₂, h₂⟩ h, by { congr, exact h } @[ext] lemma ext (h : ∀ s, measurable_set s → μ₁ s = μ₂ s) : μ₁ = μ₂ := to_outer_measure_injective $ by rw [← trimmed, outer_measure.trim_congr h, trimmed] lemma ext_iff : μ₁ = μ₂ ↔ ∀ s, measurable_set s → μ₁ s = μ₂ s := ⟨by { rintro rfl s hs, refl }, measure.ext⟩ end measure @[simp] lemma coe_to_outer_measure : ⇑μ.to_outer_measure = μ := rfl lemma to_outer_measure_apply (s : set α) : μ.to_outer_measure s = μ s := rfl lemma measure_eq_trim (s : set α) : μ s = μ.to_outer_measure.trim s := by rw μ.trimmed; refl lemma measure_eq_infi (s : set α) : μ s = ⨅ t (st : s ⊆ t) (ht : measurable_set t), μ t := by rw [measure_eq_trim, outer_measure.trim_eq_infi]; refl /-- A variant of `measure_eq_infi` which has a single `infi`. This is useful when applying a lemma next that only works for non-empty infima, in which case you can use `nonempty_measurable_superset`. -/ lemma measure_eq_infi' (μ : measure α) (s : set α) : μ s = ⨅ t : { t // s ⊆ t ∧ measurable_set t}, μ t := by simp_rw [infi_subtype, infi_and, subtype.coe_mk, ← measure_eq_infi] lemma measure_eq_induced_outer_measure : μ s = induced_outer_measure (λ s _, μ s) measurable_set.empty μ.empty s := measure_eq_trim _ lemma to_outer_measure_eq_induced_outer_measure : μ.to_outer_measure = induced_outer_measure (λ s _, μ s) measurable_set.empty μ.empty := μ.trimmed.symm lemma measure_eq_extend (hs : measurable_set s) : μ s = extend (λ t (ht : measurable_set t), μ t) s := (extend_eq _ hs).symm @[simp] lemma measure_empty : μ ∅ = 0 := μ.empty lemma nonempty_of_measure_ne_zero (h : μ s ≠ 0) : s.nonempty := ne_empty_iff_nonempty.1 $ λ h', h $ h'.symm ▸ measure_empty lemma measure_mono (h : s₁ ⊆ s₂) : μ s₁ ≤ μ s₂ := μ.mono h lemma measure_mono_null (h : s₁ ⊆ s₂) (h₂ : μ s₂ = 0) : μ s₁ = 0 := nonpos_iff_eq_zero.1 $ h₂ ▸ measure_mono h lemma measure_mono_top (h : s₁ ⊆ s₂) (h₁ : μ s₁ = ∞) : μ s₂ = ∞ := top_unique $ h₁ ▸ measure_mono h /-- For every set there exists a measurable superset of the same measure. -/ lemma exists_measurable_superset (μ : measure α) (s : set α) : ∃ t, s ⊆ t ∧ measurable_set t ∧ μ t = μ s := by simpa only [← measure_eq_trim] using μ.to_outer_measure.exists_measurable_superset_eq_trim s /-- For every set `s` and a countable collection of measures `μ i` there exists a measurable superset `t ⊇ s` such that each measure `μ i` takes the same value on `s` and `t`. -/ lemma exists_measurable_superset_forall_eq {ι} [encodable ι] (μ : ι → measure α) (s : set α) : ∃ t, s ⊆ t ∧ measurable_set t ∧ ∀ i, μ i t = μ i s := by simpa only [← measure_eq_trim] using outer_measure.exists_measurable_superset_forall_eq_trim (λ i, (μ i).to_outer_measure) s lemma exists_measurable_superset_of_null (h : μ s = 0) : ∃ t, s ⊆ t ∧ measurable_set t ∧ μ t = 0 := h ▸ exists_measurable_superset μ s lemma exists_measurable_superset_iff_measure_eq_zero : (∃ t, s ⊆ t ∧ measurable_set t ∧ μ t = 0) ↔ μ s = 0 := ⟨λ ⟨t, hst, _, ht⟩, measure_mono_null hst ht, exists_measurable_superset_of_null⟩ theorem measure_Union_le [encodable β] (s : β → set α) : μ (⋃ i, s i) ≤ ∑' i, μ (s i) := μ.to_outer_measure.Union _ lemma measure_bUnion_le {s : set β} (hs : countable s) (f : β → set α) : μ (⋃ b ∈ s, f b) ≤ ∑' p : s, μ (f p) := begin haveI := hs.to_encodable, rw [bUnion_eq_Union], apply measure_Union_le end lemma measure_bUnion_finset_le (s : finset β) (f : β → set α) : μ (⋃ b ∈ s, f b) ≤ ∑ p in s, μ (f p) := begin rw [← finset.sum_attach, finset.attach_eq_univ, ← tsum_fintype], exact measure_bUnion_le s.countable_to_set f end lemma measure_Union_fintype_le [fintype β] (f : β → set α) : μ (⋃ b, f b) ≤ ∑ p, μ (f p) := by { convert measure_bUnion_finset_le finset.univ f, simp } lemma measure_bUnion_lt_top {s : set β} {f : β → set α} (hs : finite s) (hfin : ∀ i ∈ s, μ (f i) ≠ ∞) : μ (⋃ i ∈ s, f i) < ∞ := begin convert (measure_bUnion_finset_le hs.to_finset f).trans_lt _, { ext, rw [finite.mem_to_finset] }, apply ennreal.sum_lt_top, simpa only [finite.mem_to_finset] end lemma measure_Union_null [encodable β] {s : β → set α} : (∀ i, μ (s i) = 0) → μ (⋃ i, s i) = 0 := μ.to_outer_measure.Union_null lemma measure_Union_null_iff [encodable ι] {s : ι → set α} : μ (⋃ i, s i) = 0 ↔ ∀ i, μ (s i) = 0 := ⟨λ h i, measure_mono_null (subset_Union _ _) h, measure_Union_null⟩ lemma measure_bUnion_null_iff {s : set ι} (hs : countable s) {t : ι → set α} : μ (⋃ i ∈ s, t i) = 0 ↔ ∀ i ∈ s, μ (t i) = 0 := by { haveI := hs.to_encodable, rw [bUnion_eq_Union, measure_Union_null_iff, set_coe.forall], refl } theorem measure_union_le (s₁ s₂ : set α) : μ (s₁ ∪ s₂) ≤ μ s₁ + μ s₂ := μ.to_outer_measure.union _ _ lemma measure_union_null : μ s₁ = 0 → μ s₂ = 0 → μ (s₁ ∪ s₂) = 0 := μ.to_outer_measure.union_null lemma measure_union_null_iff : μ (s₁ ∪ s₂) = 0 ↔ μ s₁ = 0 ∧ μ s₂ = 0:= ⟨λ h, ⟨measure_mono_null (subset_union_left _ _) h, measure_mono_null (subset_union_right _ _) h⟩, λ h, measure_union_null h.1 h.2⟩ lemma measure_union_lt_top (hs : μ s < ∞) (ht : μ t < ∞) : μ (s ∪ t) < ∞ := (measure_union_le s t).trans_lt (ennreal.add_lt_top.mpr ⟨hs, ht⟩) lemma measure_union_lt_top_iff : μ (s ∪ t) < ∞ ↔ μ s < ∞ ∧ μ t < ∞ := begin refine ⟨λ h, ⟨_, _⟩, λ h, measure_union_lt_top h.1 h.2⟩, { exact (measure_mono (set.subset_union_left s t)).trans_lt h, }, { exact (measure_mono (set.subset_union_right s t)).trans_lt h, }, end lemma measure_union_ne_top (hs : μ s ≠ ∞) (ht : μ t ≠ ∞) : μ (s ∪ t) ≠ ∞ := ((measure_union_le s t).trans_lt (lt_top_iff_ne_top.mpr (ennreal.add_ne_top.mpr ⟨hs, ht⟩))).ne lemma exists_measure_pos_of_not_measure_Union_null [encodable β] {s : β → set α} (hs : μ (⋃ n, s n) ≠ 0) : ∃ n, 0 < μ (s n) := begin by_contra, push_neg at h, simp_rw nonpos_iff_eq_zero at h, exact hs (measure_Union_null h), end lemma measure_inter_lt_top_of_left_ne_top (hs_finite : μ s ≠ ∞) : μ (s ∩ t) < ∞ := (measure_mono (set.inter_subset_left s t)).trans_lt hs_finite.lt_top lemma measure_inter_lt_top_of_right_ne_top (ht_finite : μ t ≠ ∞) : μ (s ∩ t) < ∞ := inter_comm t s ▸ measure_inter_lt_top_of_left_ne_top ht_finite lemma measure_inter_null_of_null_right (S : set α) {T : set α} (h : μ T = 0) : μ (S ∩ T) = 0 := measure_mono_null (inter_subset_right S T) h lemma measure_inter_null_of_null_left {S : set α} (T : set α) (h : μ S = 0) : μ (S ∩ T) = 0 := measure_mono_null (inter_subset_left S T) h /-! ### The almost everywhere filter -/ /-- The “almost everywhere” filter of co-null sets. -/ def measure.ae {α} {m : measurable_space α} (μ : measure α) : filter α := { sets := {s | μ sᶜ = 0}, univ_sets := by simp, inter_sets := λ s t hs ht, by simp only [compl_inter, mem_set_of_eq]; exact measure_union_null hs ht, sets_of_superset := λ s t hs hst, measure_mono_null (set.compl_subset_compl.2 hst) hs } notation `∀ᵐ` binders ` ∂` μ `, ` r:(scoped P, filter.eventually P (measure.ae μ)) := r notation `∃ᵐ` binders ` ∂` μ `, ` r:(scoped P, filter.frequently P (measure.ae μ)) := r notation f ` =ᵐ[`:50 μ:50 `] `:0 g:50 := f =ᶠ[measure.ae μ] g notation f ` ≤ᵐ[`:50 μ:50 `] `:0 g:50 := f ≤ᶠ[measure.ae μ] g lemma mem_ae_iff {s : set α} : s ∈ μ.ae ↔ μ sᶜ = 0 := iff.rfl lemma ae_iff {p : α → Prop} : (∀ᵐ a ∂ μ, p a) ↔ μ { a | ¬ p a } = 0 := iff.rfl lemma compl_mem_ae_iff {s : set α} : sᶜ ∈ μ.ae ↔ μ s = 0 := by simp only [mem_ae_iff, compl_compl] lemma frequently_ae_iff {p : α → Prop} : (∃ᵐ a ∂μ, p a) ↔ μ {a | p a} ≠ 0 := not_congr compl_mem_ae_iff lemma frequently_ae_mem_iff {s : set α} : (∃ᵐ a ∂μ, a ∈ s) ↔ μ s ≠ 0 := not_congr compl_mem_ae_iff lemma measure_zero_iff_ae_nmem {s : set α} : μ s = 0 ↔ ∀ᵐ a ∂ μ, a ∉ s := compl_mem_ae_iff.symm lemma ae_of_all {p : α → Prop} (μ : measure α) : (∀ a, p a) → ∀ᵐ a ∂ μ, p a := eventually_of_forall --instance ae_is_measurably_generated : is_measurably_generated μ.ae := --⟨λ s hs, let ⟨t, hst, htm, htμ⟩ := exists_measurable_superset_of_null hs in -- ⟨tᶜ, compl_mem_ae_iff.2 htμ, htm.compl, compl_subset_comm.1 hst⟩⟩ instance : countable_Inter_filter μ.ae := ⟨begin intros S hSc hS, simp only [mem_ae_iff, compl_sInter, sUnion_image, bUnion_eq_Union] at hS ⊢, haveI := hSc.to_encodable, exact measure_Union_null (subtype.forall.2 hS) end⟩ lemma ae_imp_iff {p : α → Prop} {q : Prop} : (∀ᵐ x ∂μ, q → p x) ↔ (q → ∀ᵐ x ∂μ, p x) := filter.eventually_imp_distrib_left lemma ae_all_iff [encodable ι] {p : α → ι → Prop} : (∀ᵐ a ∂ μ, ∀ i, p a i) ↔ (∀ i, ∀ᵐ a ∂ μ, p a i) := eventually_countable_forall lemma ae_ball_iff {S : set ι} (hS : countable S) {p : Π (x : α) (i ∈ S), Prop} : (∀ᵐ x ∂ μ, ∀ i ∈ S, p x i ‹_›) ↔ ∀ i ∈ S, ∀ᵐ x ∂ μ, p x i ‹_› := eventually_countable_ball hS lemma ae_eq_refl (f : α → δ) : f =ᵐ[μ] f := eventually_eq.rfl lemma ae_eq_symm {f g : α → δ} (h : f =ᵐ[μ] g) : g =ᵐ[μ] f := h.symm lemma ae_eq_trans {f g h: α → δ} (h₁ : f =ᵐ[μ] g) (h₂ : g =ᵐ[μ] h) : f =ᵐ[μ] h := h₁.trans h₂ lemma ae_le_of_ae_lt {f g : α → ℝ≥0∞} (h : ∀ᵐ x ∂μ, f x < g x) : f ≤ᵐ[μ] g := begin rw [filter.eventually_le, ae_iff], rw ae_iff at h, refine measure_mono_null (λ x hx, _) h, exact not_lt.2 (le_of_lt (not_le.1 hx)), end @[simp] lemma ae_eq_empty : s =ᵐ[μ] (∅ : set α) ↔ μ s = 0 := eventually_eq_empty.trans $ by simp [ae_iff] lemma ae_le_set : s ≤ᵐ[μ] t ↔ μ (s \ t) = 0 := calc s ≤ᵐ[μ] t ↔ ∀ᵐ x ∂μ, x ∈ s → x ∈ t : iff.rfl ... ↔ μ (s \ t) = 0 : by simp [ae_iff]; refl @[simp] lemma union_ae_eq_right : (s ∪ t : set α) =ᵐ[μ] t ↔ μ (s \ t) = 0 := by simp [eventually_le_antisymm_iff, ae_le_set, union_diff_right, diff_eq_empty.2 (set.subset_union_right _ _)] lemma diff_ae_eq_self : (s \ t : set α) =ᵐ[μ] s ↔ μ (s ∩ t) = 0 := by simp [eventually_le_antisymm_iff, ae_le_set, diff_diff_right, diff_diff, diff_eq_empty.2 (set.subset_union_right _ _)] lemma ae_eq_set {s t : set α} : s =ᵐ[μ] t ↔ μ (s \ t) = 0 ∧ μ (t \ s) = 0 := by simp [eventually_le_antisymm_iff, ae_le_set] @[to_additive] lemma _root_.set.mul_indicator_ae_eq_one {M : Type*} [has_one M] {f : α → M} {s : set α} (h : s.mul_indicator f =ᵐ[μ] 1) : μ (s ∩ function.mul_support f) = 0 := begin rw [filter.eventually_eq, ae_iff] at h, convert h, ext a, rw ← set.mul_indicator_eq_one_iff, refl end /-- If `s ⊆ t` modulo a set of measure `0`, then `μ s ≤ μ t`. -/ @[mono] lemma measure_mono_ae (H : s ≤ᵐ[μ] t) : μ s ≤ μ t := calc μ s ≤ μ (s ∪ t) : measure_mono $ subset_union_left s t ... = μ (t ∪ s \ t) : by rw [union_diff_self, set.union_comm] ... ≤ μ t + μ (s \ t) : measure_union_le _ _ ... = μ t : by rw [ae_le_set.1 H, add_zero] alias measure_mono_ae ← filter.eventually_le.measure_le /-- If two sets are equal modulo a set of measure zero, then `μ s = μ t`. -/ lemma measure_congr (H : s =ᵐ[μ] t) : μ s = μ t := le_antisymm H.le.measure_le H.symm.le.measure_le /-- A measurable set `t ⊇ s` such that `μ t = μ s`. It even satisifies `μ (t ∩ u) = μ (s ∩ u)` for any measurable set `u`, see `measure_to_measurable_inter`. If `s` is a null measurable set, then we also have `t =ᵐ[μ] s`, see `null_measurable_set.to_measurable_ae_eq`. -/ def to_measurable (μ : measure α) (s : set α) : set α := if h : ∃ t ⊇ s, measurable_set t ∧ t =ᵐ[μ] s then h.some else (exists_measurable_superset μ s).some lemma subset_to_measurable (μ : measure α) (s : set α) : s ⊆ to_measurable μ s := begin rw to_measurable, split_ifs with hs, exacts [hs.some_spec.fst, (exists_measurable_superset μ s).some_spec.1] end lemma ae_le_to_measurable : s ≤ᵐ[μ] to_measurable μ s := (subset_to_measurable _ _).eventually_le @[simp] lemma measurable_set_to_measurable (μ : measure α) (s : set α) : measurable_set (to_measurable μ s) := begin rw to_measurable, split_ifs with hs, exacts [hs.some_spec.snd.1, (exists_measurable_superset μ s).some_spec.2.1] end @[simp] lemma measure_to_measurable (s : set α) : μ (to_measurable μ s) = μ s := begin rw to_measurable, split_ifs with hs, exacts [measure_congr hs.some_spec.snd.2, (exists_measurable_superset μ s).some_spec.2.2] end /-- A measure space is a measurable space equipped with a measure, referred to as `volume`. -/ class measure_space (α : Type*) extends measurable_space α := (volume : measure α) export measure_space (volume) /-- `volume` is the canonical measure on `α`. -/ add_decl_doc volume section measure_space notation `∀ᵐ` binders `, ` r:(scoped P, filter.eventually P (measure_theory.measure.ae measure_theory.measure_space.volume)) := r notation `∃ᵐ` binders `, ` r:(scoped P, filter.frequently P (measure_theory.measure.ae measure_theory.measure_space.volume)) := r /-- The tactic `exact volume`, to be used in optional (`auto_param`) arguments. -/ meta def volume_tac : tactic unit := `[exact measure_theory.measure_space.volume] end measure_space end end measure_theory section open measure_theory /-! # Almost everywhere measurable functions A function is almost everywhere measurable if it coincides almost everywhere with a measurable function. We define this property, called `ae_measurable f μ`. It's properties are discussed in `measure_theory.measure_space`. -/ variables {m : measurable_space α} [measurable_space β] {f g : α → β} {μ ν : measure α} /-- A function is almost everywhere measurable if it coincides almost everywhere with a measurable function. -/ def ae_measurable {m : measurable_space α} (f : α → β) (μ : measure α . measure_theory.volume_tac) : Prop := ∃ g : α → β, measurable g ∧ f =ᵐ[μ] g lemma measurable.ae_measurable (h : measurable f) : ae_measurable f μ := ⟨f, h, ae_eq_refl f⟩ namespace ae_measurable /-- Given an almost everywhere measurable function `f`, associate to it a measurable function that coincides with it almost everywhere. `f` is explicit in the definition to make sure that it shows in pretty-printing. -/ def mk (f : α → β) (h : ae_measurable f μ) : α → β := classical.some h lemma measurable_mk (h : ae_measurable f μ) : measurable (h.mk f) := (classical.some_spec h).1 lemma ae_eq_mk (h : ae_measurable f μ) : f =ᵐ[μ] (h.mk f) := (classical.some_spec h).2 lemma congr (hf : ae_measurable f μ) (h : f =ᵐ[μ] g) : ae_measurable g μ := ⟨hf.mk f, hf.measurable_mk, h.symm.trans hf.ae_eq_mk⟩ end ae_measurable lemma ae_measurable_congr (h : f =ᵐ[μ] g) : ae_measurable f μ ↔ ae_measurable g μ := ⟨λ hf, ae_measurable.congr hf h, λ hg, ae_measurable.congr hg h.symm⟩ @[simp] lemma ae_measurable_const {b : β} : ae_measurable (λ a : α, b) μ := measurable_const.ae_measurable lemma ae_measurable_id : ae_measurable id μ := measurable_id.ae_measurable lemma ae_measurable_id' : ae_measurable (λ x, x) μ := measurable_id.ae_measurable lemma measurable.comp_ae_measurable [measurable_space δ] {f : α → δ} {g : δ → β} (hg : measurable g) (hf : ae_measurable f μ) : ae_measurable (g ∘ f) μ := ⟨g ∘ hf.mk f, hg.comp hf.measurable_mk, eventually_eq.fun_comp hf.ae_eq_mk _⟩ end
52aad58d21d714d4e91d5601a9ef7237ca05bbaf
491068d2ad28831e7dade8d6dff871c3e49d9431
/tests/lean/run/section2.lean
de2964a5ca9d2f250592edb32081957ab855f44d
[ "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
256
lean
import data.nat section foo variable A : Type definition id (a : A) := a variable a : nat check _root_.id nat a end foo namespace n1 section foo variable A : Type definition id (a : A) := a variable a : nat check n1.id _ a end foo end n1
a7b9ae82efeff33ee0ad1954d987d2084b751ff9
3f48345ac9bbaa421714efc9872a0409379bb4ae
/src/examples/automata/automata_coalgebra.lean
b29f8fa3dd359a98dcda082ded3f7fc6ce11f469
[]
no_license
QaisHamarneh/Coalgebra-in-Lean
b4318ee6d83780e5c734eb78fed98b1fe8016f7e
bd0452df98bc64b608e5dfd7babc42c301bb6a46
refs/heads/master
1,663,371,200,241
1,661,004,695,000
1,661,004,695,000
209,798,828
0
0
null
null
null
null
UTF-8
Lean
false
false
1,682
lean
import coalgebra.Coalgebra import examples.automata.Automaton import set_category.category_set import category_theory.types import help_functions import data.fin namespace Automata_Coalgebra open set help_functions coalgebra Automaton universes u₁ u₂ u₃ variables {Sigma : Type u₂} {Γ : Type u₃} def F : Type u₁ ⥤ Type (max u₁ u₂ u₃) := { obj := λ S, Γ × (Sigma → S), map := λ {A B} ϕ, λ ⟨d₁ , δ⟩ , ⟨d₁ , λ e, ϕ (δ e)⟩, } def α (A : Automaton Sigma Γ): A → F.obj A := λ s, ⟨A.γ s, A.δ s⟩ def Automata_Coalgebra (A : Automaton Sigma Γ): Coalgebra F := ⟨A.State , α A⟩ lemma Automata_Coalgebra_hom {A B : Automaton Sigma Γ} (ϕ : A → B): is_homomorphism ϕ ↔ @is_coalgebra_homomorphism F (Automata_Coalgebra A) (Automata_Coalgebra B) ϕ := let 𝔸 := Automata_Coalgebra A in let Β := Automata_Coalgebra B in begin split, intro h, dsimp at *, ext1 s, dsimp at *, have hs := h s, ext1, have B_fst : (Β.α (ϕ s)).fst = B.γ (ϕ s) := by tidy, have A_fst : (F.map ϕ ((Automata_Coalgebra A).α s)).fst = B.γ (ϕ s) := by tidy, simp [B_fst , A_fst], have A_snd : (Β.α (ϕ s)).snd = ((F.map ϕ) (𝔸.α s)).snd := by tidy, exact A_snd, intro co_h, dsimp at *, intro s, have h_s : (Β.α ∘ ϕ) s = (F.map ϕ ∘ 𝔸.α) s := by rw co_h, split, tidy end end Automata_Coalgebra
8d7b8b50215952876c523ae051b14bf4e6771fd3
c777c32c8e484e195053731103c5e52af26a25d1
/src/field_theory/fixed.lean
115c4b0289428be0d4b8838b6c0e5815138d0e4c
[ "Apache-2.0" ]
permissive
kbuzzard/mathlib
2ff9e85dfe2a46f4b291927f983afec17e946eb8
58537299e922f9c77df76cb613910914a479c1f7
refs/heads/master
1,685,313,702,744
1,683,974,212,000
1,683,974,212,000
128,185,277
1
0
null
1,522,920,600,000
1,522,920,600,000
null
UTF-8
Lean
false
false
14,416
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import algebra.group_ring_action.invariant import algebra.polynomial.group_ring_action import field_theory.normal import field_theory.separable import field_theory.tower /-! # Fixed field under a group action. This is the basis of the Fundamental Theorem of Galois Theory. Given a (finite) group `G` that acts on a field `F`, we define `fixed_points G F`, the subfield consisting of elements of `F` fixed_points by every element of `G`. This subfield is then normal and separable, and in addition (TODO) if `G` acts faithfully on `F` then `finrank (fixed_points G F) F = fintype.card G`. ## Main Definitions - `fixed_points G F`, the subfield consisting of elements of `F` fixed_points by every element of `G`, where `G` is a group that acts on `F`. -/ noncomputable theory open_locale classical big_operators open mul_action finset finite_dimensional universes u v w variables {M : Type u} [monoid M] variables (G : Type u) [group G] variables (F : Type v) [field F] [mul_semiring_action M F] [mul_semiring_action G F] (m : M) /-- The subfield of F fixed by the field endomorphism `m`. -/ def fixed_by.subfield : subfield F := { carrier := fixed_by M F m, zero_mem' := smul_zero m, add_mem' := λ x y hx hy, (smul_add m x y).trans $ congr_arg2 _ hx hy, neg_mem' := λ x hx, (smul_neg m x).trans $ congr_arg _ hx, one_mem' := smul_one m, mul_mem' := λ x y hx hy, (smul_mul' m x y).trans $ congr_arg2 _ hx hy, inv_mem' := λ x hx, (smul_inv'' m x).trans $ congr_arg _ hx } section invariant_subfields variables (M) {F} /-- A typeclass for subrings invariant under a `mul_semiring_action`. -/ class is_invariant_subfield (S : subfield F) : Prop := (smul_mem : ∀ (m : M) {x : F}, x ∈ S → m • x ∈ S) variable (S : subfield F) instance is_invariant_subfield.to_mul_semiring_action [is_invariant_subfield M S] : mul_semiring_action M S := { smul := λ m x, ⟨m • x, is_invariant_subfield.smul_mem m x.2⟩, one_smul := λ s, subtype.eq $ one_smul M s, mul_smul := λ m₁ m₂ s, subtype.eq $ mul_smul m₁ m₂ s, smul_add := λ m s₁ s₂, subtype.eq $ smul_add m s₁ s₂, smul_zero := λ m, subtype.eq $ smul_zero m, smul_one := λ m, subtype.eq $ smul_one m, smul_mul := λ m s₁ s₂, subtype.eq $ smul_mul' m s₁ s₂ } instance [is_invariant_subfield M S] : is_invariant_subring M (S.to_subring) := { smul_mem := is_invariant_subfield.smul_mem } end invariant_subfields namespace fixed_points variable (M) -- we use `subfield.copy` so that the underlying set is `fixed_points M F` /-- The subfield of fixed points by a monoid action. -/ def subfield : subfield F := subfield.copy (⨅ (m : M), fixed_by.subfield F m) (fixed_points M F) (by { ext z, simp [fixed_points, fixed_by.subfield, infi, subfield.mem_Inf] }) instance : is_invariant_subfield M (fixed_points.subfield M F) := { smul_mem := λ g x hx g', by rw [hx, hx] } instance : smul_comm_class M (fixed_points.subfield M F) F := { smul_comm := λ m f f', show m • (↑f * f') = f * (m • f'), by rw [smul_mul', f.prop m] } instance smul_comm_class' : smul_comm_class (fixed_points.subfield M F) M F := smul_comm_class.symm _ _ _ @[simp] theorem smul (m : M) (x : fixed_points.subfield M F) : m • x = x := subtype.eq $ x.2 m -- Why is this so slow? @[simp] theorem smul_polynomial (m : M) (p : polynomial (fixed_points.subfield M F)) : m • p = p := polynomial.induction_on p (λ x, by rw [polynomial.smul_C, smul]) (λ p q ihp ihq, by rw [smul_add, ihp, ihq]) (λ n x ih, by rw [smul_mul', polynomial.smul_C, smul, smul_pow', polynomial.smul_X]) instance : algebra (fixed_points.subfield M F) F := by apply_instance theorem coe_algebra_map : algebra_map (fixed_points.subfield M F) F = subfield.subtype (fixed_points.subfield M F) := rfl lemma linear_independent_smul_of_linear_independent {s : finset F} : linear_independent (fixed_points.subfield G F) (λ i : (s : set F), (i : F)) → linear_independent F (λ i : (s : set F), mul_action.to_fun G F i) := begin haveI : is_empty ((∅ : finset F) : set F) := ⟨subtype.prop⟩, refine finset.induction_on s (λ _, linear_independent_empty_type) (λ a s has ih hs, _), rw coe_insert at hs ⊢, rw linear_independent_insert (mt mem_coe.1 has) at hs, rw linear_independent_insert' (mt mem_coe.1 has), refine ⟨ih hs.1, λ ha, _⟩, rw finsupp.mem_span_image_iff_total at ha, rcases ha with ⟨l, hl, hla⟩, rw [finsupp.total_apply_of_mem_supported F hl] at hla, suffices : ∀ i ∈ s, l i ∈ fixed_points.subfield G F, { replace hla := (sum_apply _ _ (λ i, l i • to_fun G F i)).symm.trans (congr_fun hla 1), simp_rw [pi.smul_apply, to_fun_apply, one_smul] at hla, refine hs.2 (hla ▸ submodule.sum_mem _ (λ c hcs, _)), change (⟨l c, this c hcs⟩ : fixed_points.subfield G F) • c ∈ _, exact submodule.smul_mem _ _ (submodule.subset_span $ mem_coe.2 hcs) }, intros i his g, refine eq_of_sub_eq_zero (linear_independent_iff'.1 (ih hs.1) s.attach (λ i, g • l i - l i) _ ⟨i, his⟩ (mem_attach _ _) : _), refine (@sum_attach _ _ s _ (λ i, (g • l i - l i) • mul_action.to_fun G F i)).trans _, ext g', dsimp only, conv_lhs { rw sum_apply, congr, skip, funext, rw [pi.smul_apply, sub_smul, smul_eq_mul] }, rw [sum_sub_distrib, pi.zero_apply, sub_eq_zero], conv_lhs { congr, skip, funext, rw [to_fun_apply, ← mul_inv_cancel_left g g', mul_smul, ← smul_mul', ← to_fun_apply _ x] }, show ∑ x in s, g • (λ y, l y • mul_action.to_fun G F y) x (g⁻¹ * g') = ∑ x in s, (λ y, l y • mul_action.to_fun G F y) x g', rw [← smul_sum, ← sum_apply _ _ (λ y, l y • to_fun G F y), ← sum_apply _ _ (λ y, l y • to_fun G F y)], dsimp only, rw [hla, to_fun_apply, to_fun_apply, smul_smul, mul_inv_cancel_left] end section fintype variables [fintype G] (x : F) /-- `minpoly G F x` is the minimal polynomial of `(x : F)` over `fixed_points G F`. -/ def minpoly : polynomial (fixed_points.subfield G F) := (prod_X_sub_smul G F x).to_subring (fixed_points.subfield G F).to_subring $ λ c hc g, let ⟨n, hc0, hn⟩ := polynomial.mem_frange_iff.1 hc in hn.symm ▸ prod_X_sub_smul.coeff G F x g n namespace minpoly theorem monic : (minpoly G F x).monic := by { simp only [minpoly, polynomial.monic_to_subring], exact prod_X_sub_smul.monic G F x } theorem eval₂ : polynomial.eval₂ (subring.subtype $ (fixed_points.subfield G F).to_subring) x (minpoly G F x) = 0 := begin rw [← prod_X_sub_smul.eval G F x, polynomial.eval₂_eq_eval_map], simp only [minpoly, polynomial.map_to_subring], end theorem eval₂' : polynomial.eval₂ (subfield.subtype $ (fixed_points.subfield G F)) x (minpoly G F x) = 0 := eval₂ G F x theorem ne_one : minpoly G F x ≠ (1 : polynomial (fixed_points.subfield G F)) := λ H, have _ := eval₂ G F x, (one_ne_zero : (1 : F) ≠ 0) $ by rwa [H, polynomial.eval₂_one] at this theorem of_eval₂ (f : polynomial (fixed_points.subfield G F)) (hf : polynomial.eval₂ (subfield.subtype $ fixed_points.subfield G F) x f = 0) : minpoly G F x ∣ f := begin erw [← polynomial.map_dvd_map' (subfield.subtype $ fixed_points.subfield G F), minpoly, polynomial.map_to_subring _ (subfield G F).to_subring, prod_X_sub_smul], refine fintype.prod_dvd_of_coprime (polynomial.pairwise_coprime_X_sub_C $ mul_action.injective_of_quotient_stabilizer G x) (λ y, quotient_group.induction_on y $ λ g, _), rw [polynomial.dvd_iff_is_root, polynomial.is_root.def, mul_action.of_quotient_stabilizer_mk, polynomial.eval_smul', ← subfield.to_subring.subtype_eq_subtype, ← is_invariant_subring.coe_subtype_hom' G (fixed_points.subfield G F).to_subring, ← mul_semiring_action_hom.coe_polynomial, ← mul_semiring_action_hom.map_smul, smul_polynomial, mul_semiring_action_hom.coe_polynomial, is_invariant_subring.coe_subtype_hom', polynomial.eval_map, subfield.to_subring.subtype_eq_subtype, hf, smul_zero] end /- Why is this so slow? -/ theorem irreducible_aux (f g : polynomial (fixed_points.subfield G F)) (hf : f.monic) (hg : g.monic) (hfg : f * g = minpoly G F x) : f = 1 ∨ g = 1 := begin have hf2 : f ∣ minpoly G F x, { rw ← hfg, exact dvd_mul_right _ _ }, have hg2 : g ∣ minpoly G F x, { rw ← hfg, exact dvd_mul_left _ _ }, have := eval₂ G F x, rw [← hfg, polynomial.eval₂_mul, mul_eq_zero] at this, cases this, { right, have hf3 : f = minpoly G F x, { exact polynomial.eq_of_monic_of_associated hf (monic G F x) (associated_of_dvd_dvd hf2 $ @of_eval₂ G _ F _ _ _ x f this) }, rwa [← mul_one (minpoly G F x), hf3, mul_right_inj' (monic G F x).ne_zero] at hfg }, { left, have hg3 : g = minpoly G F x, { exact polynomial.eq_of_monic_of_associated hg (monic G F x) (associated_of_dvd_dvd hg2 $ @of_eval₂ G _ F _ _ _ x g this) }, rwa [← one_mul (minpoly G F x), hg3, mul_left_inj' (monic G F x).ne_zero] at hfg } end theorem irreducible : irreducible (minpoly G F x) := (polynomial.irreducible_of_monic (monic G F x) (ne_one G F x)).2 (irreducible_aux G F x) end minpoly end fintype theorem is_integral [finite G] (x : F) : is_integral (fixed_points.subfield G F) x := by { casesI nonempty_fintype G, exact ⟨minpoly G F x, minpoly.monic G F x, minpoly.eval₂ G F x⟩ } section fintype variables [fintype G] (x : F) theorem minpoly_eq_minpoly : minpoly G F x = _root_.minpoly (fixed_points.subfield G F) x := minpoly.eq_of_irreducible_of_monic (minpoly.irreducible G F x) (minpoly.eval₂ G F x) (minpoly.monic G F x) lemma rank_le_card : module.rank (fixed_points.subfield G F) F ≤ fintype.card G := rank_le $ λ s hs, by simpa only [rank_fun', cardinal.mk_coe_finset, finset.coe_sort_coe, cardinal.lift_nat_cast, cardinal.nat_cast_le] using cardinal_lift_le_rank_of_linear_independent' (linear_independent_smul_of_linear_independent G F hs) end fintype section finite variables [finite G] instance normal : normal (fixed_points.subfield G F) F := ⟨λ x, (is_integral G F x).is_algebraic _, λ x, (polynomial.splits_id_iff_splits _).1 $ begin casesI nonempty_fintype G, rw [←minpoly_eq_minpoly, minpoly, coe_algebra_map, ←subfield.to_subring.subtype_eq_subtype, polynomial.map_to_subring _ (subfield G F).to_subring, prod_X_sub_smul], exact polynomial.splits_prod _ (λ _ _, polynomial.splits_X_sub_C _), end⟩ instance separable : is_separable (fixed_points.subfield G F) F := ⟨is_integral G F, λ x, by { casesI nonempty_fintype G, -- this was a plain rw when we were using unbundled subrings erw [← minpoly_eq_minpoly, ← polynomial.separable_map (fixed_points.subfield G F).subtype, minpoly, polynomial.map_to_subring _ ((subfield G F).to_subring) ], exact polynomial.separable_prod_X_sub_C_iff.2 (injective_of_quotient_stabilizer G x) }⟩ instance : finite_dimensional (subfield G F) F := by { casesI nonempty_fintype G, exact is_noetherian.iff_fg.1 (is_noetherian.iff_rank_lt_aleph_0.2 $ (rank_le_card G F).trans_lt $ cardinal.nat_lt_aleph_0 _) } end finite lemma finrank_le_card [fintype G] : finrank (subfield G F) F ≤ fintype.card G := begin rw [← cardinal.nat_cast_le, finrank_eq_rank], apply rank_le_card, end end fixed_points lemma linear_independent_to_linear_map (R : Type u) (A : Type v) (B : Type w) [comm_semiring R] [ring A] [algebra R A] [comm_ring B] [is_domain B] [algebra R B] : linear_independent B (alg_hom.to_linear_map : (A →ₐ[R] B) → (A →ₗ[R] B)) := have linear_independent B (linear_map.lto_fun R A B ∘ alg_hom.to_linear_map), from ((linear_independent_monoid_hom A B).comp (coe : (A →ₐ[R] B) → (A →* B)) (λ f g hfg, alg_hom.ext $ monoid_hom.ext_iff.1 hfg) : _), this.of_comp _ lemma cardinal_mk_alg_hom (K : Type u) (V : Type v) (W : Type w) [field K] [field V] [algebra K V] [finite_dimensional K V] [field W] [algebra K W] [finite_dimensional K W] : cardinal.mk (V →ₐ[K] W) ≤ finrank W (V →ₗ[K] W) := cardinal_mk_le_finrank_of_linear_independent $ linear_independent_to_linear_map K V W noncomputable instance alg_equiv.fintype (K : Type u) (V : Type v) [field K] [field V] [algebra K V] [finite_dimensional K V] : fintype (V ≃ₐ[K] V) := fintype.of_equiv (V →ₐ[K] V) (alg_equiv_equiv_alg_hom K V).symm lemma finrank_alg_hom (K : Type u) (V : Type v) [field K] [field V] [algebra K V] [finite_dimensional K V] : fintype.card (V →ₐ[K] V) ≤ finrank V (V →ₗ[K] V) := fintype_card_le_finrank_of_linear_independent $ linear_independent_to_linear_map K V V namespace fixed_points theorem finrank_eq_card (G : Type u) (F : Type v) [group G] [field F] [fintype G] [mul_semiring_action G F] [has_faithful_smul G F] : finrank (fixed_points.subfield G F) F = fintype.card G := le_antisymm (fixed_points.finrank_le_card G F) $ calc fintype.card G ≤ fintype.card (F →ₐ[fixed_points.subfield G F] F) : fintype.card_le_of_injective _ (mul_semiring_action.to_alg_hom_injective _ F) ... ≤ finrank F (F →ₗ[fixed_points.subfield G F] F) : finrank_alg_hom (fixed_points G F) F ... = finrank (fixed_points.subfield G F) F : finrank_linear_map' _ _ _ /-- `mul_semiring_action.to_alg_hom` is bijective. -/ theorem to_alg_hom_bijective (G : Type u) (F : Type v) [group G] [field F] [finite G] [mul_semiring_action G F] [has_faithful_smul G F] : function.bijective (mul_semiring_action.to_alg_hom _ _ : G → F →ₐ[subfield G F] F) := begin casesI nonempty_fintype G, rw fintype.bijective_iff_injective_and_card, split, { exact mul_semiring_action.to_alg_hom_injective _ F }, { apply le_antisymm, { exact fintype.card_le_of_injective _ (mul_semiring_action.to_alg_hom_injective _ F) }, { rw ← finrank_eq_card G F, exact has_le.le.trans_eq (finrank_alg_hom _ F) (finrank_linear_map' _ _ _) } }, end /-- Bijection between G and algebra homomorphisms that fix the fixed points -/ def to_alg_hom_equiv (G : Type u) (F : Type v) [group G] [field F] [fintype G] [mul_semiring_action G F] [has_faithful_smul G F] : G ≃ (F →ₐ[fixed_points.subfield G F] F) := equiv.of_bijective _ (to_alg_hom_bijective G F) end fixed_points
3b91fc25ef339cacec4836c1309169ef4db64f7d
02005f45e00c7ecf2c8ca5db60251bd1e9c860b5
/src/algebra/group_power/lemmas.lean
7ec11a90347fbba8e93ce1656dcf8784b25629a9
[ "Apache-2.0" ]
permissive
anthony2698/mathlib
03cd69fe5c280b0916f6df2d07c614c8e1efe890
407615e05814e98b24b2ff322b14e8e3eb5e5d67
refs/heads/master
1,678,792,774,873
1,614,371,563,000
1,614,371,563,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
29,315
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.group_power.basic import algebra.opposites import data.list.basic import data.int.cast import data.equiv.basic import data.equiv.mul_add import deprecated.group /-! # Lemmas about power operations on monoids and groups This file contains lemmas about `monoid.pow`, `group.pow`, `nsmul`, `gsmul` which require additional imports besides those available in `.basic`. -/ 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₂} /-! ### (Additive) monoid -/ section monoid variables [monoid M] [monoid N] [add_monoid A] [add_monoid B] @[simp] theorem nsmul_one [has_one A] : ∀ n : ℕ, n •ℕ (1 : A) = n := add_monoid_hom.eq_nat_cast ⟨λ n, n •ℕ (1 : A), zero_nsmul _, λ _ _, add_nsmul _ _ _⟩ (one_nsmul _) @[simp, priority 500] theorem list.prod_repeat (a : M) (n : ℕ) : (list.repeat a n).prod = a ^ n := begin induction n with n ih, { refl }, { rw [list.repeat_succ, list.prod_cons, ih], refl, } end @[simp, priority 500] theorem list.sum_repeat : ∀ (a : A) (n : ℕ), (list.repeat a n).sum = n •ℕ a := @list.prod_repeat (multiplicative A) _ @[simp, norm_cast] lemma units.coe_pow (u : units M) (n : ℕ) : ((u ^ n : units M) : M) = u ^ n := (units.coe_hom M).map_pow u n lemma is_unit_of_pow_eq_one (x : M) (n : ℕ) (hx : x ^ n = 1) (hn : 0 < n) : is_unit x := begin cases n, { exact (nat.not_lt_zero _ hn).elim }, refine ⟨⟨x, x ^ n, _, _⟩, rfl⟩, { rwa [pow_succ] at hx }, { rwa [pow_succ'] at hx } end end monoid theorem nat.nsmul_eq_mul (m n : ℕ) : m •ℕ n = m * n := by induction m with m ih; [rw [zero_nsmul, zero_mul], rw [succ_nsmul', ih, nat.succ_mul]] section group variables [group G] [group H] [add_group A] [add_group B] open int local attribute [ematch] le_of_lt open nat theorem gsmul_one [has_one A] (n : ℤ) : n •ℤ (1 : A) = n := by cases n; simp lemma gpow_add_one (a : G) : ∀ n : ℤ, a ^ (n + 1) = a ^ n * a | (of_nat n) := by simp [← int.coe_nat_succ, pow_succ'] | -[1+0] := by simp [int.neg_succ_of_nat_eq] | -[1+(n+1)] := by rw [int.neg_succ_of_nat_eq, gpow_neg, neg_add, neg_add_cancel_right, gpow_neg, ← int.coe_nat_succ, gpow_coe_nat, gpow_coe_nat, pow_succ _ (n + 1), mul_inv_rev, inv_mul_cancel_right] theorem add_one_gsmul : ∀ (a : A) (i : ℤ), (i + 1) •ℤ a = i •ℤ a + a := @gpow_add_one (multiplicative A) _ lemma gpow_sub_one (a : G) (n : ℤ) : a ^ (n - 1) = a ^ n * a⁻¹ := calc a ^ (n - 1) = a ^ (n - 1) * a * a⁻¹ : (mul_inv_cancel_right _ _).symm ... = a^n * a⁻¹ : by rw [← gpow_add_one, sub_add_cancel] lemma gpow_add (a : G) (m n : ℤ) : a ^ (m + n) = a ^ m * a ^ n := begin induction n using int.induction_on with n ihn n ihn, case hz : { simp }, { simp only [← add_assoc, gpow_add_one, ihn, mul_assoc] }, { rw [gpow_sub_one, ← mul_assoc, ← ihn, ← gpow_sub_one, add_sub_assoc] } end lemma mul_self_gpow (b : G) (m : ℤ) : b*b^m = b^(m+1) := by { conv_lhs {congr, rw ← gpow_one b }, rw [← gpow_add, add_comm] } lemma mul_gpow_self (b : G) (m : ℤ) : b^m*b = b^(m+1) := by { conv_lhs {congr, skip, rw ← gpow_one b }, rw [← gpow_add, add_comm] } theorem add_gsmul : ∀ (a : A) (i j : ℤ), (i + j) •ℤ a = i •ℤ a + j •ℤ a := @gpow_add (multiplicative A) _ lemma gpow_sub (a : G) (m n : ℤ) : a ^ (m - n) = a ^ m * (a ^ n)⁻¹ := by rw [sub_eq_add_neg, gpow_add, gpow_neg] lemma sub_gsmul (m n : ℤ) (a : A) : (m - n) •ℤ a = m •ℤ a - n •ℤ a := by simpa only [sub_eq_add_neg] using @gpow_sub (multiplicative A) _ _ _ _ theorem gpow_one_add (a : G) (i : ℤ) : a ^ (1 + i) = a * a ^ i := by rw [gpow_add, gpow_one] theorem one_add_gsmul : ∀ (a : A) (i : ℤ), (1 + i) •ℤ a = a + i •ℤ a := @gpow_one_add (multiplicative A) _ theorem gpow_mul_comm (a : G) (i j : ℤ) : a ^ i * a ^ j = a ^ j * a ^ i := by rw [← gpow_add, ← gpow_add, add_comm] theorem gsmul_add_comm : ∀ (a : A) (i j), i •ℤ a + j •ℤ a = j •ℤ a + i •ℤ a := @gpow_mul_comm (multiplicative A) _ theorem gpow_mul (a : G) (m n : ℤ) : a ^ (m * n) = (a ^ m) ^ n := int.induction_on n (by simp) (λ n ihn, by simp [mul_add, gpow_add, ihn]) (λ n ihn, by simp only [mul_sub, gpow_sub, ihn, mul_one, gpow_one]) theorem gsmul_mul' : ∀ (a : A) (m n : ℤ), m * n •ℤ a = n •ℤ (m •ℤ a) := @gpow_mul (multiplicative A) _ theorem gpow_mul' (a : G) (m n : ℤ) : a ^ (m * n) = (a ^ n) ^ m := by rw [mul_comm, gpow_mul] theorem gsmul_mul (a : A) (m n : ℤ) : m * n •ℤ a = m •ℤ (n •ℤ a) := by rw [mul_comm, gsmul_mul'] theorem gpow_bit0 (a : G) (n : ℤ) : a ^ bit0 n = a ^ n * a ^ n := gpow_add _ _ _ theorem bit0_gsmul (a : A) (n : ℤ) : bit0 n •ℤ a = n •ℤ a + n •ℤ a := gpow_add _ _ _ theorem gpow_bit1 (a : G) (n : ℤ) : a ^ bit1 n = a ^ n * a ^ n * a := by rw [bit1, gpow_add, gpow_bit0, gpow_one] theorem bit1_gsmul : ∀ (a : A) (n : ℤ), bit1 n •ℤ a = n •ℤ a + n •ℤ a + a := @gpow_bit1 (multiplicative A) _ @[simp] theorem monoid_hom.map_gpow (f : G →* H) (a : G) (n : ℤ) : f (a ^ n) = f a ^ n := by cases n; [exact f.map_pow _ _, exact (f.map_inv _).trans (congr_arg _ $ f.map_pow _ _)] @[simp] theorem add_monoid_hom.map_gsmul (f : A →+ B) (a : A) (n : ℤ) : f (n •ℤ a) = n •ℤ f a := f.to_multiplicative.map_gpow a n @[simp, norm_cast] lemma units.coe_gpow (u : units G) (n : ℤ) : ((u ^ n : units G) : G) = u ^ n := (units.coe_hom G).map_gpow u n end group section ordered_add_comm_group variables [ordered_add_comm_group A] /-! Lemmas about `gsmul` under ordering, placed here (rather than in `algebra.group_power.basic` with their friends) because they require facts from `data.int.basic`-/ open int lemma gsmul_pos {a : A} (ha : 0 < a) {k : ℤ} (hk : (0:ℤ) < k) : 0 < k •ℤ a := begin lift k to ℕ using int.le_of_lt hk, apply nsmul_pos ha, exact coe_nat_pos.mp hk, end theorem gsmul_le_gsmul {a : A} {n m : ℤ} (ha : 0 ≤ a) (h : n ≤ m) : n •ℤ a ≤ m •ℤ a := calc n •ℤ a = n •ℤ a + 0 : (add_zero _).symm ... ≤ n •ℤ a + (m - n) •ℤ a : add_le_add_left (gsmul_nonneg ha (sub_nonneg.mpr h)) _ ... = m •ℤ a : by { rw [← add_gsmul], simp } theorem gsmul_lt_gsmul {a : A} {n m : ℤ} (ha : 0 < a) (h : n < m) : n •ℤ a < m •ℤ a := calc n •ℤ a = n •ℤ a + 0 : (add_zero _).symm ... < n •ℤ a + (m - n) •ℤ a : add_lt_add_left (gsmul_pos ha (sub_pos.mpr h)) _ ... = m •ℤ a : by { rw [← add_gsmul], simp } end ordered_add_comm_group section linear_ordered_add_comm_group variable [linear_ordered_add_comm_group A] theorem gsmul_le_gsmul_iff {a : A} {n m : ℤ} (ha : 0 < a) : n •ℤ a ≤ m •ℤ a ↔ n ≤ m := begin refine ⟨λ h, _, gsmul_le_gsmul $ le_of_lt ha⟩, by_contra H, exact lt_irrefl _ (lt_of_lt_of_le (gsmul_lt_gsmul ha (not_le.mp H)) h) end theorem gsmul_lt_gsmul_iff {a : A} {n m : ℤ} (ha : 0 < a) : n •ℤ a < m •ℤ a ↔ n < m := begin refine ⟨λ h, _, gsmul_lt_gsmul ha⟩, by_contra H, exact lt_irrefl _ (lt_of_le_of_lt (gsmul_le_gsmul (le_of_lt ha) $ not_lt.mp H) h) end theorem nsmul_le_nsmul_iff {a : A} {n m : ℕ} (ha : 0 < a) : n •ℕ a ≤ m •ℕ a ↔ n ≤ m := begin refine ⟨λ h, _, nsmul_le_nsmul $ le_of_lt ha⟩, by_contra H, exact lt_irrefl _ (lt_of_lt_of_le (nsmul_lt_nsmul ha (not_le.mp H)) h) end theorem nsmul_lt_nsmul_iff {a : A} {n m : ℕ} (ha : 0 < a) : n •ℕ a < m •ℕ a ↔ n < m := begin refine ⟨λ h, _, nsmul_lt_nsmul ha⟩, by_contra H, exact lt_irrefl _ (lt_of_le_of_lt (nsmul_le_nsmul (le_of_lt ha) $ not_lt.mp H) h) end end linear_ordered_add_comm_group @[simp] lemma with_bot.coe_nsmul [add_monoid A] (a : A) (n : ℕ) : ((nsmul n a : A) : with_bot A) = nsmul n a := add_monoid_hom.map_nsmul ⟨(coe : A → with_bot A), with_bot.coe_zero, with_bot.coe_add⟩ a n theorem nsmul_eq_mul' [semiring R] (a : R) (n : ℕ) : n •ℕ a = a * n := by induction n with n ih; [rw [zero_nsmul, nat.cast_zero, mul_zero], rw [succ_nsmul', ih, nat.cast_succ, mul_add, mul_one]] @[simp] theorem nsmul_eq_mul [semiring R] (n : ℕ) (a : R) : n •ℕ a = n * a := by rw [nsmul_eq_mul', (n.cast_commute a).eq] theorem mul_nsmul_left [semiring R] (a b : R) (n : ℕ) : n •ℕ (a * b) = a * (n •ℕ b) := by rw [nsmul_eq_mul', nsmul_eq_mul', mul_assoc] theorem mul_nsmul_assoc [semiring R] (a b : R) (n : ℕ) : n •ℕ (a * b) = n •ℕ a * b := by rw [nsmul_eq_mul, nsmul_eq_mul, mul_assoc] @[simp, norm_cast] theorem nat.cast_pow [semiring R] (n m : ℕ) : (↑(n ^ m) : R) = ↑n ^ m := by induction m with m ih; [exact nat.cast_one, rw [pow_succ', pow_succ', nat.cast_mul, ih]] @[simp, norm_cast] theorem int.coe_nat_pow (n m : ℕ) : ((n ^ m : ℕ) : ℤ) = n ^ m := by induction m with m ih; [exact int.coe_nat_one, rw [pow_succ', pow_succ', int.coe_nat_mul, ih]] theorem int.nat_abs_pow (n : ℤ) (k : ℕ) : int.nat_abs (n ^ k) = (int.nat_abs n) ^ k := by induction k with k ih; [refl, rw [pow_succ', int.nat_abs_mul, pow_succ', ih]] -- The next four lemmas allow us to replace multiplication by a numeral with a `gsmul` expression. -- They are used by the `noncomm_ring` tactic, to normalise expressions before passing to `abel`. lemma bit0_mul [ring R] {n r : R} : bit0 n * r = gsmul 2 (n * r) := by { dsimp [bit0], rw [add_mul, add_gsmul, one_gsmul], } lemma mul_bit0 [ring R] {n r : R} : r * bit0 n = gsmul 2 (r * n) := by { dsimp [bit0], rw [mul_add, add_gsmul, one_gsmul], } lemma bit1_mul [ring R] {n r : R} : bit1 n * r = gsmul 2 (n * r) + r := by { dsimp [bit1], rw [add_mul, bit0_mul, one_mul], } lemma mul_bit1 [ring R] {n r : R} : r * bit1 n = gsmul 2 (r * n) + r := by { dsimp [bit1], rw [mul_add, mul_bit0, mul_one], } @[simp] theorem gsmul_eq_mul [ring R] (a : R) : ∀ n, n •ℤ a = n * a | (n : ℕ) := nsmul_eq_mul _ _ | -[1+ n] := show -(_ •ℕ _)=-_*_, by rw [neg_mul_eq_neg_mul_symm, nsmul_eq_mul, nat.cast_succ] theorem gsmul_eq_mul' [ring R] (a : R) (n : ℤ) : n •ℤ a = a * n := by rw [gsmul_eq_mul, (n.cast_commute a).eq] theorem mul_gsmul_left [ring R] (a b : R) (n : ℤ) : n •ℤ (a * b) = a * (n •ℤ b) := by rw [gsmul_eq_mul', gsmul_eq_mul', mul_assoc] theorem mul_gsmul_assoc [ring R] (a b : R) (n : ℤ) : n •ℤ (a * b) = n •ℤ a * b := by rw [gsmul_eq_mul, gsmul_eq_mul, mul_assoc] @[simp] lemma gsmul_int_int (a b : ℤ) : a •ℤ b = a * b := by simp [gsmul_eq_mul] lemma gsmul_int_one (n : ℤ) : n •ℤ 1 = n := by simp @[simp, norm_cast] theorem int.cast_pow [ring R] (n : ℤ) (m : ℕ) : (↑(n ^ m) : R) = ↑n ^ m := by induction m with m ih; [exact int.cast_one, rw [pow_succ, pow_succ, int.cast_mul, ih]] lemma neg_one_pow_eq_pow_mod_two [ring R] {n : ℕ} : (-1 : R) ^ n = (-1) ^ (n % 2) := by rw [← nat.mod_add_div n 2, pow_add, pow_mul]; simp [pow_two] section ordered_semiring variable [ordered_semiring R] /-- Bernoulli's inequality. This version works for semirings but requires additional hypotheses `0 ≤ a * a` and `0 ≤ (1 + a) * (1 + a)`. -/ theorem one_add_mul_le_pow' {a : R} (Hsqr : 0 ≤ a * a) (Hsqr' : 0 ≤ (1 + a) * (1 + a)) (H : 0 ≤ 2 + a) : ∀ (n : ℕ), 1 + (n : R) * a ≤ (1 + a) ^ n | 0 := by simp | 1 := by simp | (n+2) := have 0 ≤ (n : R) * (a * a * (2 + a)) + a * a, from add_nonneg (mul_nonneg n.cast_nonneg (mul_nonneg Hsqr H)) Hsqr, calc 1 + (↑(n + 2) : R) * a ≤ 1 + ↑(n + 2) * a + (n * (a * a * (2 + a)) + a * a) : (le_add_iff_nonneg_right _).2 this ... = (1 + a) * (1 + a) * (1 + n * a) : by { simp [add_mul, mul_add, bit0, mul_assoc, (n.cast_commute (_ : R)).left_comm], ac_refl } ... ≤ (1 + a) * (1 + a) * (1 + a)^n : mul_le_mul_of_nonneg_left (one_add_mul_le_pow' n) Hsqr' ... = (1 + a)^(n + 2) : by simp only [pow_succ, mul_assoc] private lemma pow_lt_pow_of_lt_one_aux {a : R} (h : 0 < a) (ha : a < 1) (i : ℕ) : ∀ k : ℕ, a ^ (i + k + 1) < a ^ i | 0 := begin simp only [add_zero], rw ←one_mul (a^i), exact mul_lt_mul ha (le_refl _) (pow_pos h _) zero_le_one end | (k+1) := begin rw ←one_mul (a^i), apply mul_lt_mul ha _ _ zero_le_one, { apply le_of_lt, apply pow_lt_pow_of_lt_one_aux }, { show 0 < a ^ (i + (k + 1) + 0), apply pow_pos h } end private lemma pow_le_pow_of_le_one_aux {a : R} (h : 0 ≤ a) (ha : a ≤ 1) (i : ℕ) : ∀ k : ℕ, a ^ (i + k) ≤ a ^ i | 0 := by simp | (k+1) := by rw [←add_assoc, ←one_mul (a^i)]; exact mul_le_mul ha (pow_le_pow_of_le_one_aux _) (pow_nonneg h _) zero_le_one lemma pow_lt_pow_of_lt_one {a : R} (h : 0 < a) (ha : a < 1) {i j : ℕ} (hij : i < j) : a ^ j < a ^ i := let ⟨k, hk⟩ := nat.exists_eq_add_of_lt hij in by rw hk; exact pow_lt_pow_of_lt_one_aux h ha _ _ lemma pow_lt_pow_iff_of_lt_one {a : R} {n m : ℕ} (hpos : 0 < a) (h : a < 1) : a ^ m < a ^ n ↔ n < m := begin have : strict_mono (λ (n : order_dual ℕ), a ^ (id n : ℕ)) := λ m n, pow_lt_pow_of_lt_one hpos h, exact this.lt_iff_lt end lemma pow_le_pow_of_le_one {a : R} (h : 0 ≤ a) (ha : a ≤ 1) {i j : ℕ} (hij : i ≤ j) : a ^ j ≤ a ^ i := let ⟨k, hk⟩ := nat.exists_eq_add_of_le hij in by rw hk; exact pow_le_pow_of_le_one_aux h ha _ _ lemma pow_le_one {x : R} : ∀ (n : ℕ) (h0 : 0 ≤ x) (h1 : x ≤ 1), x ^ n ≤ 1 | 0 h0 h1 := le_refl (1 : R) | (n+1) h0 h1 := mul_le_one h1 (pow_nonneg h0 _) (pow_le_one n h0 h1) end ordered_semiring section linear_ordered_semiring variables [linear_ordered_semiring R] lemma sign_cases_of_C_mul_pow_nonneg {C r : R} (h : ∀ n : ℕ, 0 ≤ C * r ^ n) : C = 0 ∨ (0 < C ∧ 0 ≤ r) := begin have : 0 ≤ C, by simpa only [pow_zero, mul_one] using h 0, refine this.eq_or_lt.elim (λ h, or.inl h.symm) (λ hC, or.inr ⟨hC, _⟩), refine nonneg_of_mul_nonneg_left _ hC, simpa only [pow_one] using h 1 end end linear_ordered_semiring section linear_ordered_ring variables [linear_ordered_ring R] {a : R} {n : ℕ} @[simp] lemma abs_pow (a : R) (n : ℕ) : abs (a ^ n) = abs a ^ n := abs_hom.to_monoid_hom.map_pow a n @[simp] theorem pow_bit1_neg_iff : a ^ bit1 n < 0 ↔ a < 0 := ⟨λ h, not_le.1 $ λ h', not_le.2 h $ pow_nonneg h' _, λ h, mul_neg_of_neg_of_pos h (pow_bit0_pos h.ne _)⟩ @[simp] theorem pow_bit1_nonneg_iff : 0 ≤ a ^ bit1 n ↔ 0 ≤ a := le_iff_le_iff_lt_iff_lt.2 pow_bit1_neg_iff @[simp] theorem pow_bit1_nonpos_iff : a ^ bit1 n ≤ 0 ↔ a ≤ 0 := by simp only [le_iff_lt_or_eq, pow_bit1_neg_iff, pow_eq_zero_iff (bit1_pos (zero_le n))] @[simp] theorem pow_bit1_pos_iff : 0 < a ^ bit1 n ↔ 0 < a := lt_iff_lt_of_le_iff_le pow_bit1_nonpos_iff theorem pow_even_nonneg (a : R) (hn : even n) : 0 ≤ a ^ n := by cases hn with k hk; simpa only [hk, two_mul] using pow_bit0_nonneg a k theorem pow_even_pos (ha : a ≠ 0) (hn : even n) : 0 < a ^ n := by cases hn with k hk; simpa only [hk, two_mul] using pow_bit0_pos ha k theorem pow_odd_nonneg (ha : 0 ≤ a) (hn : odd n) : 0 ≤ a ^ n := by cases hn with k hk; simpa only [hk, two_mul] using pow_bit1_nonneg_iff.mpr ha theorem pow_odd_pos (ha : 0 < a) (hn : odd n) : 0 < a ^ n := by cases hn with k hk; simpa only [hk, two_mul] using pow_bit1_pos_iff.mpr ha theorem pow_odd_nonpos (ha : a ≤ 0) (hn : odd n) : a ^ n ≤ 0:= by cases hn with k hk; simpa only [hk, two_mul] using pow_bit1_nonpos_iff.mpr ha theorem pow_odd_neg (ha : a < 0) (hn : odd n) : a ^ n < 0:= by cases hn with k hk; simpa only [hk, two_mul] using pow_bit1_neg_iff.mpr ha lemma strict_mono_pow_bit1 (n : ℕ) : strict_mono (λ a : R, a ^ bit1 n) := begin intros a b hab, cases le_total a 0 with ha ha, { cases le_or_lt b 0 with hb hb, { rw [← neg_lt_neg_iff, ← neg_pow_bit1, ← neg_pow_bit1], exact pow_lt_pow_of_lt_left (neg_lt_neg hab) (neg_nonneg.2 hb) (bit1_pos (zero_le n)) }, { exact (pow_bit1_nonpos_iff.2 ha).trans_lt (pow_bit1_pos_iff.2 hb) } }, { exact pow_lt_pow_of_lt_left hab ha (bit1_pos (zero_le n)) } end /-- Bernoulli's inequality for `n : ℕ`, `-2 ≤ a`. -/ theorem one_add_mul_le_pow (H : -2 ≤ a) (n : ℕ) : 1 + (n : R) * a ≤ (1 + a) ^ n := one_add_mul_le_pow' (mul_self_nonneg _) (mul_self_nonneg _) (neg_le_iff_add_nonneg'.1 H) _ /-- Bernoulli's inequality reformulated to estimate `a^n`. -/ theorem one_add_mul_sub_le_pow (H : -1 ≤ a) (n : ℕ) : 1 + (n : R) * (a - 1) ≤ a ^ n := have -2 ≤ a - 1, by rwa [bit0, neg_add, ← sub_eq_add_neg, sub_le_sub_iff_right], by simpa only [add_sub_cancel'_right] using one_add_mul_le_pow this n end linear_ordered_ring /-- Bernoulli's inequality reformulated to estimate `(n : K)`. -/ theorem nat.cast_le_pow_sub_div_sub {K : Type*} [linear_ordered_field K] {a : K} (H : 1 < a) (n : ℕ) : (n : K) ≤ (a ^ n - 1) / (a - 1) := (le_div_iff (sub_pos.2 H)).2 $ le_sub_left_of_add_le $ one_add_mul_sub_le_pow ((neg_le_self $ @zero_le_one K _).trans H.le) _ /-- For any `a > 1` and a natural `n` we have `n ≤ a ^ n / (a - 1)`. See also `nat.cast_le_pow_sub_div_sub` for a stronger inequality with `a ^ n - 1` in the numerator. -/ theorem nat.cast_le_pow_div_sub {K : Type*} [linear_ordered_field K] {a : K} (H : 1 < a) (n : ℕ) : (n : K) ≤ a ^ n / (a - 1) := (n.cast_le_pow_sub_div_sub H).trans $ div_le_div_of_le (sub_nonneg.2 H.le) (sub_le_self _ zero_le_one) namespace int lemma units_pow_two (u : units ℤ) : u ^ 2 = 1 := (pow_two u).symm ▸ units_mul_self u lemma units_pow_eq_pow_mod_two (u : units ℤ) (n : ℕ) : u ^ n = u ^ (n % 2) := by conv {to_lhs, rw ← nat.mod_add_div n 2}; rw [pow_add, pow_mul, units_pow_two, one_pow, mul_one] @[simp] lemma nat_abs_pow_two (x : ℤ) : (x.nat_abs ^ 2 : ℤ) = x ^ 2 := by rw [pow_two, int.nat_abs_mul_self', pow_two] lemma abs_le_self_pow_two (a : ℤ) : (int.nat_abs a : ℤ) ≤ a ^ 2 := by { rw [← int.nat_abs_pow_two a, pow_two], norm_cast, apply nat.le_mul_self } lemma le_self_pow_two (b : ℤ) : b ≤ b ^ 2 := le_trans (le_nat_abs) (abs_le_self_pow_two _) end int variables (M G A) /-- Monoid homomorphisms from `multiplicative ℕ` are defined by the image of `multiplicative.of_add 1`. -/ def powers_hom [monoid M] : M ≃ (multiplicative ℕ →* M) := { to_fun := λ x, ⟨λ n, x ^ n.to_add, pow_zero x, λ m n, pow_add x m n⟩, inv_fun := λ f, f (multiplicative.of_add 1), left_inv := pow_one, right_inv := λ f, monoid_hom.ext $ λ n, by { simp [← f.map_pow, ← of_add_nsmul] } } /-- Monoid homomorphisms from `multiplicative ℤ` are defined by the image of `multiplicative.of_add 1`. -/ def gpowers_hom [group G] : G ≃ (multiplicative ℤ →* G) := { to_fun := λ x, ⟨λ n, x ^ n.to_add, gpow_zero x, λ m n, gpow_add x m n⟩, inv_fun := λ f, f (multiplicative.of_add 1), left_inv := gpow_one, right_inv := λ f, monoid_hom.ext $ λ n, by { simp [← f.map_gpow, ← of_add_gsmul ] } } /-- Additive homomorphisms from `ℕ` are defined by the image of `1`. -/ def multiples_hom [add_monoid A] : A ≃ (ℕ →+ A) := { to_fun := λ x, ⟨λ n, n •ℕ x, zero_nsmul x, λ m n, add_nsmul _ _ _⟩, inv_fun := λ f, f 1, left_inv := one_nsmul, right_inv := λ f, add_monoid_hom.ext_nat $ one_nsmul (f 1) } /-- Additive homomorphisms from `ℤ` are defined by the image of `1`. -/ def gmultiples_hom [add_group A] : A ≃ (ℤ →+ A) := { to_fun := λ x, ⟨λ n, n •ℤ x, zero_gsmul x, λ m n, add_gsmul _ _ _⟩, inv_fun := λ f, f 1, left_inv := one_gsmul, right_inv := λ f, add_monoid_hom.ext_int $ one_gsmul (f 1) } variables {M G A} @[simp] lemma powers_hom_apply [monoid M] (x : M) (n : multiplicative ℕ) : powers_hom M x n = x ^ n.to_add := rfl @[simp] lemma powers_hom_symm_apply [monoid M] (f : multiplicative ℕ →* M) : (powers_hom M).symm f = f (multiplicative.of_add 1) := rfl @[simp] lemma gpowers_hom_apply [group G] (x : G) (n : multiplicative ℤ) : gpowers_hom G x n = x ^ n.to_add := rfl @[simp] lemma gpowers_hom_symm_apply [group G] (f : multiplicative ℤ →* G) : (gpowers_hom G).symm f = f (multiplicative.of_add 1) := rfl @[simp] lemma multiples_hom_apply [add_monoid A] (x : A) (n : ℕ) : multiples_hom A x n = n •ℕ x := rfl @[simp] lemma multiples_hom_symm_apply [add_monoid A] (f : ℕ →+ A) : (multiples_hom A).symm f = f 1 := rfl @[simp] lemma gmultiples_hom_apply [add_group A] (x : A) (n : ℤ) : gmultiples_hom A x n = n •ℤ x := rfl @[simp] lemma gmultiples_hom_symm_apply [add_group A] (f : ℤ →+ A) : (gmultiples_hom A).symm f = f 1 := rfl lemma monoid_hom.apply_mnat [monoid M] (f : multiplicative ℕ →* M) (n : multiplicative ℕ) : f n = (f (multiplicative.of_add 1)) ^ n.to_add := by rw [← powers_hom_symm_apply, ← powers_hom_apply, equiv.apply_symm_apply] @[ext] lemma monoid_hom.ext_mnat [monoid M] ⦃f g : multiplicative ℕ →* M⦄ (h : f (multiplicative.of_add 1) = g (multiplicative.of_add 1)) : f = g := monoid_hom.ext $ λ n, by rw [f.apply_mnat, g.apply_mnat, h] lemma monoid_hom.apply_mint [group M] (f : multiplicative ℤ →* M) (n : multiplicative ℤ) : f n = (f (multiplicative.of_add 1)) ^ n.to_add := by rw [← gpowers_hom_symm_apply, ← gpowers_hom_apply, equiv.apply_symm_apply] @[ext] lemma monoid_hom.ext_mint [group M] ⦃f g : multiplicative ℤ →* M⦄ (h : f (multiplicative.of_add 1) = g (multiplicative.of_add 1)) : f = g := monoid_hom.ext $ λ n, by rw [f.apply_mint, g.apply_mint, h] lemma add_monoid_hom.apply_nat [add_monoid M] (f : ℕ →+ M) (n : ℕ) : f n = n •ℕ (f 1) := by rw [← multiples_hom_symm_apply, ← multiples_hom_apply, equiv.apply_symm_apply] /-! `add_monoid_hom.ext_nat` is defined in `data.nat.cast` -/ lemma add_monoid_hom.apply_int [add_group M] (f : ℤ →+ M) (n : ℤ) : f n = n •ℤ (f 1) := by rw [← gmultiples_hom_symm_apply, ← gmultiples_hom_apply, equiv.apply_symm_apply] /-! `add_monoid_hom.ext_int` is defined in `data.int.cast` -/ variables (M G A) /-- If `M` is commutative, `powers_hom` is a multiplicative equivalence. -/ def powers_mul_hom [comm_monoid M] : M ≃* (multiplicative ℕ →* M) := { map_mul' := λ a b, monoid_hom.ext $ by simp [mul_pow], ..powers_hom M} /-- If `M` is commutative, `gpowers_hom` is a multiplicative equivalence. -/ def gpowers_mul_hom [comm_group G] : G ≃* (multiplicative ℤ →* G) := { map_mul' := λ a b, monoid_hom.ext $ by simp [mul_gpow], ..gpowers_hom G} /-- If `M` is commutative, `multiples_hom` is an additive equivalence. -/ def multiples_add_hom [add_comm_monoid A] : A ≃+ (ℕ →+ A) := { map_add' := λ a b, add_monoid_hom.ext $ by simp [nsmul_add], ..multiples_hom A} /-- If `M` is commutative, `gmultiples_hom` is an additive equivalence. -/ def gmultiples_add_hom [add_comm_group A] : A ≃+ (ℤ →+ A) := { map_add' := λ a b, add_monoid_hom.ext $ by simp [gsmul_add], ..gmultiples_hom A} variables {M G A} @[simp] lemma powers_mul_hom_apply [comm_monoid M] (x : M) (n : multiplicative ℕ) : powers_mul_hom M x n = x ^ n.to_add := rfl @[simp] lemma powers_mul_hom_symm_apply [comm_monoid M] (f : multiplicative ℕ →* M) : (powers_mul_hom M).symm f = f (multiplicative.of_add 1) := rfl @[simp] lemma gpowers_mul_hom_apply [comm_group G] (x : G) (n : multiplicative ℤ) : gpowers_mul_hom G x n = x ^ n.to_add := rfl @[simp] lemma gpowers_mul_hom_symm_apply [comm_group G] (f : multiplicative ℤ →* G) : (gpowers_mul_hom G).symm f = f (multiplicative.of_add 1) := rfl @[simp] lemma multiples_add_hom_apply [add_comm_monoid A] (x : A) (n : ℕ) : multiples_add_hom A x n = n •ℕ x := rfl @[simp] lemma multiples_add_hom_symm_apply [add_comm_monoid A] (f : ℕ →+ A) : (multiples_add_hom A).symm f = f 1 := rfl @[simp] lemma gmultiples_add_hom_apply [add_comm_group A] (x : A) (n : ℤ) : gmultiples_add_hom A x n = n •ℤ x := rfl @[simp] lemma gmultiples_add_hom_symm_apply [add_comm_group A] (f : ℤ →+ A) : (gmultiples_add_hom A).symm f = f 1 := rfl /-! ### Commutativity (again) Facts about `semiconj_by` and `commute` that require `gpow` or `gsmul`, or the fact that integer multiplication equals semiring multiplication. -/ namespace semiconj_by section variables [semiring R] {a x y : R} @[simp] lemma cast_nat_mul_right (h : semiconj_by a x y) (n : ℕ) : semiconj_by a ((n : R) * x) (n * y) := semiconj_by.mul_right (nat.commute_cast _ _) h @[simp] lemma cast_nat_mul_left (h : semiconj_by a x y) (n : ℕ) : semiconj_by ((n : R) * a) x y := semiconj_by.mul_left (nat.cast_commute _ _) h @[simp] lemma cast_nat_mul_cast_nat_mul (h : semiconj_by a x y) (m n : ℕ) : semiconj_by ((m : R) * a) (n * x) (n * y) := (h.cast_nat_mul_left m).cast_nat_mul_right n end variables [monoid M] [group G] [ring R] @[simp] lemma units_gpow_right {a : M} {x y : units M} (h : semiconj_by a x y) : ∀ m : ℤ, semiconj_by a (↑(x^m)) (↑(y^m)) | (n : ℕ) := by simp only [gpow_coe_nat, units.coe_pow, h, pow_right] | -[1+n] := by simp only [gpow_neg_succ_of_nat, units.coe_pow, units_inv_right, h, pow_right] variables {a b x y x' y' : R} @[simp] lemma cast_int_mul_right (h : semiconj_by a x y) (m : ℤ) : semiconj_by a ((m : ℤ) * x) (m * y) := semiconj_by.mul_right (int.commute_cast _ _) h @[simp] lemma cast_int_mul_left (h : semiconj_by a x y) (m : ℤ) : semiconj_by ((m : R) * a) x y := semiconj_by.mul_left (int.cast_commute _ _) h @[simp] lemma cast_int_mul_cast_int_mul (h : semiconj_by a x y) (m n : ℤ) : semiconj_by ((m : R) * a) (n * x) (n * y) := (h.cast_int_mul_left m).cast_int_mul_right n end semiconj_by namespace commute section variables [semiring R] {a b : R} @[simp] theorem cast_nat_mul_right (h : commute a b) (n : ℕ) : commute a ((n : R) * b) := h.cast_nat_mul_right n @[simp] theorem cast_nat_mul_left (h : commute a b) (n : ℕ) : commute ((n : R) * a) b := h.cast_nat_mul_left n @[simp] theorem cast_nat_mul_cast_nat_mul (h : commute a b) (m n : ℕ) : commute ((m : R) * a) (n * b) := h.cast_nat_mul_cast_nat_mul m n @[simp] theorem self_cast_nat_mul (n : ℕ) : commute a (n * a) := (commute.refl a).cast_nat_mul_right n @[simp] theorem cast_nat_mul_self (n : ℕ) : commute ((n : R) * a) a := (commute.refl a).cast_nat_mul_left n @[simp] theorem self_cast_nat_mul_cast_nat_mul (m n : ℕ) : commute ((m : R) * a) (n * a) := (commute.refl a).cast_nat_mul_cast_nat_mul m n end variables [monoid M] [group G] [ring R] @[simp] lemma units_gpow_right {a : M} {u : units M} (h : commute a u) (m : ℤ) : commute a (↑(u^m)) := h.units_gpow_right m @[simp] lemma units_gpow_left {u : units M} {a : M} (h : commute ↑u a) (m : ℤ) : commute (↑(u^m)) a := (h.symm.units_gpow_right m).symm variables {a b : R} @[simp] lemma cast_int_mul_right (h : commute a b) (m : ℤ) : commute a (m * b) := h.cast_int_mul_right m @[simp] lemma cast_int_mul_left (h : commute a b) (m : ℤ) : commute ((m : R) * a) b := h.cast_int_mul_left m lemma cast_int_mul_cast_int_mul (h : commute a b) (m n : ℤ) : commute ((m : R) * a) (n * b) := h.cast_int_mul_cast_int_mul m n variables (a) (m n : ℤ) @[simp] theorem self_cast_int_mul : commute a (n * a) := (commute.refl a).cast_int_mul_right n @[simp] theorem cast_int_mul_self : commute ((n : R) * a) a := (commute.refl a).cast_int_mul_left n theorem self_cast_int_mul_cast_int_mul : commute ((m : R) * a) (n * a) := (commute.refl a).cast_int_mul_cast_int_mul m n end commute section multiplicative open multiplicative @[simp] lemma nat.to_add_pow (a : multiplicative ℕ) (b : ℕ) : to_add (a ^ b) = to_add a * b := begin induction b with b ih, { erw [pow_zero, to_add_one, mul_zero] }, { simp [*, pow_succ, add_comm, nat.mul_succ] } end @[simp] lemma nat.of_add_mul (a b : ℕ) : of_add (a * b) = of_add a ^ b := (nat.to_add_pow _ _).symm @[simp] lemma int.to_add_pow (a : multiplicative ℤ) (b : ℕ) : to_add (a ^ b) = to_add a * b := by induction b; simp [*, mul_add, pow_succ, add_comm] @[simp] lemma int.to_add_gpow (a : multiplicative ℤ) (b : ℤ) : to_add (a ^ b) = to_add a * b := int.induction_on b (by simp) (by simp [gpow_add, mul_add] {contextual := tt}) (by simp [gpow_add, mul_add, sub_eq_add_neg, -int.add_neg_one] {contextual := tt}) @[simp] lemma int.of_add_mul (a b : ℤ) : of_add (a * b) = of_add a ^ b := (int.to_add_gpow _ _).symm end multiplicative namespace units variables [monoid M] lemma conj_pow (u : units M) (x : M) (n : ℕ) : (↑u * x * ↑(u⁻¹))^n = u * x^n * ↑(u⁻¹) := (divp_eq_iff_mul_eq.2 ((u.mk_semiconj_by x).pow_right n).eq.symm).symm lemma conj_pow' (u : units M) (x : M) (n : ℕ) : (↑(u⁻¹) * x * u)^n = ↑(u⁻¹) * x^n * u:= (u⁻¹).conj_pow x n open opposite /-- Moving to the opposite monoid commutes with taking powers. -/ @[simp] lemma op_pow (x : M) (n : ℕ) : op (x ^ n) = (op x) ^ n := begin induction n with n h, { simp }, { rw [pow_succ', op_mul, h, pow_succ] } end @[simp] lemma unop_pow (x : Mᵒᵖ) (n : ℕ) : unop (x ^ n) = (unop x) ^ n := begin induction n with n h, { simp }, { rw [pow_succ', unop_mul, h, pow_succ] } end end units
b7e1d27f5c3c01239834bb8fde75a542e0bd28c7
1446f520c1db37e157b631385707cc28a17a595e
/tests/compiler/closure_bug1.lean
8d626fe127cd47f72a4ceaaacd80ad60914da2f6
[ "Apache-2.0" ]
permissive
bdbabiak/lean4
cab06b8a2606d99a168dd279efdd404edb4e825a
3f4d0d78b2ce3ef541cb643bbe21496bd6b057ac
refs/heads/master
1,615,045,275,530
1,583,793,696,000
1,583,793,696,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
559
lean
def f (x : Nat) : Nat × (Nat → String) := let x1 := x + 1; let x2 := x + 2; let x3 := x + 3; let x4 := x + 4; let x5 := x + 5; let x6 := x + 6; let x7 := x + 7; let x8 := x + 8; let x9 := x + 9; let x10 := x + 10; let x11 := x + 11; let x12 := x + 12; let x13 := x + 13; let x14 := x + 14; let x15 := x + 15; let x16 := x + 16; let x17 := x + 17; (x, fun y => toString [x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, y, y]) def main (xs : List String) : IO Unit := IO.println ((f (xs.headD "0").toNat).2 (xs.headD "0").toNat)
d8887b2f39ed5396c630e25067f1a9166035bccd
8e2026ac8a0660b5a490dfb895599fb445bb77a0
/tests/lean/run/cdcl_examples.lean
f9b9f836e1d17f7dea5035790031961402991a59
[ "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
712
lean
import tools.super.cdcl example {a} : a → ¬a → false := by cdcl example {a} : a ∨ ¬a := by cdcl example {a b} : a → (a → b) → b := by cdcl example {a b c} : (a → b) → (¬a → b) → (b → c) → b ∧ c := by cdcl open tactic monad private meta def lit_unification : tactic unit := do ls ← local_context, first $ do l ← ls, [do apply l, assumption] example {p : ℕ → Prop} : p 2 ∨ p 4 → (p (2*2) → p (2+0)) → p (1+1) := by cdcl_t lit_unification example {p : ℕ → Prop} : list.foldl (λf v, f ∧ (v ∨ ¬v)) true (map p (list.range 5)) := begin (target >>= whnf >>= change), cdcl end example {a b c : Prop} : (a → b) → (b → c) → (a → c) := by cdcl
04519d61f842d6e4eb54d35ce7b70e4c2270867d
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/category_theory/limits/shapes/constructions/limits_of_products_and_equalizers.lean
9f3e8f4bc0559843f93b378c3d389b8e995199cc
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
4,864
lean
/- -- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Scott Morrison -/ import category_theory.limits.shapes.products import category_theory.limits.shapes.equalizers import category_theory.limits.shapes.finite_limits import category_theory.limits.shapes.finite_products /-! # Constructing limits from products and equalizers. If a category has all products, and all equalizers, then it has all limits. Similarly, if it has all finite products, and all equalizers, then it has all finite limits. TODO: provide the dual result. -/ open category_theory open opposite namespace category_theory.limits universes v u variables {C : Type u} [𝒞 : category.{v} C] include 𝒞 variables {J : Type v} [small_category J] -- We hide the "implementation details" inside a namespace namespace has_limit_of_has_products_of_has_equalizers -- We assume here only that we have exactly the products we need, so that we can prove -- variations of the construction (all products gives all limits, finite products gives finite limits...) variables (F : J ⥤ C) [H₁ : has_limit.{v} (functor.of_function F.obj)] [H₂ : has_limit.{v} (functor.of_function (λ f : (Σ p : J × J, p.1 ⟶ p.2), F.obj f.1.2))] include H₁ H₂ /-- Corresponding to any functor `F : J ⥤ C`, we construct a new functor from the walking parallel pair of morphisms to `C`, given by the diagram ``` s ∏_j F j ===> Π_{f : j ⟶ j'} F j' t ``` where the two morphisms `s` and `t` are defined componentwise: * The `s_f` component is the projection `∏_j F j ⟶ F j` followed by `f`. * The `t_f` component is the projection `∏_j F j ⟶ F j'`. In a moment we prove that cones over `F` are isomorphic to cones over this new diagram. -/ @[simp] def diagram : walking_parallel_pair ⥤ C := let pi_obj := limits.pi_obj F.obj in let pi_hom := limits.pi_obj (λ f : (Σ p : J × J, p.1 ⟶ p.2), F.obj f.1.2) in let s : pi_obj ⟶ pi_hom := pi.lift (λ f : (Σ p : J × J, p.1 ⟶ p.2), pi.π F.obj f.1.1 ≫ F.map f.2) in let t : pi_obj ⟶ pi_hom := pi.lift (λ f : (Σ p : J × J, p.1 ⟶ p.2), pi.π F.obj f.1.2) in parallel_pair s t /-- The morphism from cones over the walking pair diagram `diagram F` to cones over the original diagram `F`. -/ @[simp] def cones_hom : (diagram F).cones ⟶ F.cones := { app := λ X c, { app := λ j, c.app walking_parallel_pair.zero ≫ pi.π _ j, naturality' := λ j j' f, begin have L := c.naturality walking_parallel_pair_hom.left, have R := c.naturality walking_parallel_pair_hom.right, have t := congr_arg (λ g, g ≫ pi.π _ (⟨(j, j'), f⟩ : Σ (p : J × J), p.fst ⟶ p.snd)) (R.symm.trans L), dsimp at t, dsimp, simpa only [limit.lift_π, fan.mk_π_app, category.assoc, category.id_comp] using t, end }, }. /-- The morphism from cones over the original diagram `F` to cones over the walking pair diagram `diagram F`. -/ @[simp] def cones_inv : F.cones ⟶ (diagram F).cones := { app := λ X c, begin refine (fork.of_ι _ _).π, { exact pi.lift c.app }, { ext f, rcases f with ⟨⟨A,B⟩,f⟩, dsimp, simp only [limit.lift_π, limit.lift_π_assoc, fan.mk_π_app, category.assoc], rw ←(c.naturality f), dsimp, simp only [category.id_comp], } end, naturality' := λ X Y f, by { ext c j, cases j; tidy, } }. /-- The natural isomorphism between cones over the walking pair diagram `diagram F` and cones over the original diagram `F`. -/ def cones_iso : (diagram F).cones ≅ F.cones := { hom := cones_hom F, inv := cones_inv F, hom_inv_id' := begin ext X c j, cases j, { ext, simp }, { ext, have t := c.naturality walking_parallel_pair_hom.left, conv at t { dsimp, to_lhs, simp only [category.id_comp] }, simp [t], } end } end has_limit_of_has_products_of_has_equalizers open has_limit_of_has_products_of_has_equalizers /-- Any category with products and equalizers has all limits. -/ -- This is not an instance, as it is not always how one wants to construct limits! def limits_from_equalizers_and_products [has_products.{v} C] [has_equalizers.{v} C] : has_limits.{v} C := { has_limits_of_shape := λ J 𝒥, by exactI { has_limit := λ F, has_limit.of_cones_iso (diagram F) F (cones_iso F) } } /-- Any category with finite products and equalizers has all finite limits. -/ -- This is not an instance, as it is not always how one wants to construct finite limits! def finite_limits_from_equalizers_and_finite_products [has_finite_products.{v} C] [has_equalizers.{v} C] : has_finite_limits.{v} C := { has_limits_of_shape := λ J _ _, by exactI { has_limit := λ F, has_limit.of_cones_iso (diagram F) F (cones_iso F) } } end category_theory.limits
c3461c6245236036aad88e9f6adc36261dec7f8f
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/linear_algebra/linear_pmap.lean
994f6149d36a244657108e6e67e4d3ce5209e893
[ "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
16,768
lean
/- Copyright (c) 2020 Yury Kudryashov All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import linear_algebra.basic import linear_algebra.prod /-! # Partially defined linear maps A `linear_pmap R E F` is a linear map from a submodule of `E` to `F`. We define a `semilattice_inf` with `order_bot` instance on this this, and define three operations: * `mk_span_singleton` defines a partial linear map defined on the span of a singleton. * `sup` takes two partial linear maps `f`, `g` that agree on the intersection of their domains, and returns the unique partial linear map on `f.domain ⊔ g.domain` that extends both `f` and `g`. * `Sup` takes a `directed_on (≤)` set of partial linear maps, and returns the unique partial linear map on the `Sup` of their domains that extends all these maps. Partially defined maps are currently used in `mathlib` to prove Hahn-Banach theorem and its variations. Namely, `linear_pmap.Sup` implies that every chain of `linear_pmap`s is bounded above. Another possible use (not yet in `mathlib`) would be the theory of unbounded linear operators. -/ open set universes u v w /-- A `linear_pmap R E F` is a linear map from a submodule of `E` to `F`. -/ structure linear_pmap (R : Type u) [ring R] (E : Type v) [add_comm_group E] [module R E] (F : Type w) [add_comm_group F] [module R F] := (domain : submodule R E) (to_fun : domain →ₗ[R] F) variables {R : Type*} [ring R] {E : Type*} [add_comm_group E] [module R E] {F : Type*} [add_comm_group F] [module R F] {G : Type*} [add_comm_group G] [module R G] namespace linear_pmap open submodule instance : has_coe_to_fun (linear_pmap R E F) (λ f : linear_pmap R E F, f.domain → F) := ⟨λ f, f.to_fun⟩ @[simp] lemma to_fun_eq_coe (f : linear_pmap R E F) (x : f.domain) : f.to_fun x = f x := rfl @[simp] lemma map_zero (f : linear_pmap R E F) : f 0 = 0 := f.to_fun.map_zero lemma map_add (f : linear_pmap R E F) (x y : f.domain) : f (x + y) = f x + f y := f.to_fun.map_add x y lemma map_neg (f : linear_pmap R E F) (x : f.domain) : f (-x) = -f x := f.to_fun.map_neg x lemma map_sub (f : linear_pmap R E F) (x y : f.domain) : f (x - y) = f x - f y := f.to_fun.map_sub x y lemma map_smul (f : linear_pmap R E F) (c : R) (x : f.domain) : f (c • x) = c • f x := f.to_fun.map_smul c x @[simp] lemma mk_apply (p : submodule R E) (f : p →ₗ[R] F) (x : p) : mk p f x = f x := rfl /-- The unique `linear_pmap` on `R ∙ x` that sends `x` to `y`. This version works for modules over rings, and requires a proof of `∀ c, c • x = 0 → c • y = 0`. -/ noncomputable def mk_span_singleton' (x : E) (y : F) (H : ∀ c : R, c • x = 0 → c • y = 0) : linear_pmap R E F := { domain := R ∙ x, to_fun := have H : ∀ c₁ c₂ : R, c₁ • x = c₂ • x → c₁ • y = c₂ • y, { intros c₁ c₂ h, rw [← sub_eq_zero, ← sub_smul] at h ⊢, exact H _ h }, { to_fun := λ z, (classical.some (mem_span_singleton.1 z.prop) • y), map_add' := λ y z, begin rw [← add_smul], apply H, simp only [add_smul, sub_smul, classical.some_spec (mem_span_singleton.1 _)], apply coe_add end, map_smul' := λ c z, begin rw [smul_smul], apply H, simp only [mul_smul, classical.some_spec (mem_span_singleton.1 _)], apply coe_smul end } } @[simp] lemma domain_mk_span_singleton (x : E) (y : F) (H : ∀ c : R, c • x = 0 → c • y = 0) : (mk_span_singleton' x y H).domain = R ∙ x := rfl @[simp] lemma mk_span_singleton_apply (x : E) (y : F) (H : ∀ c : R, c • x = 0 → c • y = 0) (c : R) (h) : mk_span_singleton' x y H ⟨c • x, h⟩ = c • y := begin dsimp [mk_span_singleton'], rw [← sub_eq_zero, ← sub_smul], apply H, simp only [sub_smul, one_smul, sub_eq_zero], apply classical.some_spec (mem_span_singleton.1 h), end /-- The unique `linear_pmap` on `span R {x}` that sends a non-zero vector `x` to `y`. This version works for modules over division rings. -/ @[reducible] noncomputable def mk_span_singleton {K E F : Type*} [division_ring K] [add_comm_group E] [module K E] [add_comm_group F] [module K F] (x : E) (y : F) (hx : x ≠ 0) : linear_pmap K E F := mk_span_singleton' x y $ λ c hc, (smul_eq_zero.1 hc).elim (λ hc, by rw [hc, zero_smul]) (λ hx', absurd hx' hx) /-- Projection to the first coordinate as a `linear_pmap` -/ protected def fst (p : submodule R E) (p' : submodule R F) : linear_pmap R (E × F) E := { domain := p.prod p', to_fun := (linear_map.fst R E F).comp (p.prod p').subtype } @[simp] lemma fst_apply (p : submodule R E) (p' : submodule R F) (x : p.prod p') : linear_pmap.fst p p' x = (x : E × F).1 := rfl /-- Projection to the second coordinate as a `linear_pmap` -/ protected def snd (p : submodule R E) (p' : submodule R F) : linear_pmap R (E × F) F := { domain := p.prod p', to_fun := (linear_map.snd R E F).comp (p.prod p').subtype } @[simp] lemma snd_apply (p : submodule R E) (p' : submodule R F) (x : p.prod p') : linear_pmap.snd p p' x = (x : E × F).2 := rfl instance : has_neg (linear_pmap R E F) := ⟨λ f, ⟨f.domain, -f.to_fun⟩⟩ @[simp] lemma neg_apply (f : linear_pmap R E F) (x) : (-f) x = -(f x) := rfl instance : has_le (linear_pmap R E F) := ⟨λ f g, f.domain ≤ g.domain ∧ ∀ ⦃x : f.domain⦄ ⦃y : g.domain⦄ (h : (x:E) = y), f x = g y⟩ lemma eq_of_le_of_domain_eq {f g : linear_pmap R E F} (hle : f ≤ g) (heq : f.domain = g.domain) : f = g := begin rcases f with ⟨f_dom, f⟩, rcases g with ⟨g_dom, g⟩, change f_dom = g_dom at heq, subst g_dom, have : f = g, from linear_map.ext (λ x, hle.2 rfl), subst g end /-- Given two partial linear maps `f`, `g`, the set of points `x` such that both `f` and `g` are defined at `x` and `f x = g x` form a submodule. -/ def eq_locus (f g : linear_pmap R E F) : submodule R E := { carrier := {x | ∃ (hf : x ∈ f.domain) (hg : x ∈ g.domain), f ⟨x, hf⟩ = g ⟨x, hg⟩}, zero_mem' := ⟨zero_mem _, zero_mem _, f.map_zero.trans g.map_zero.symm⟩, add_mem' := λ x y ⟨hfx, hgx, hx⟩ ⟨hfy, hgy, hy⟩, ⟨add_mem _ hfx hfy, add_mem _ hgx hgy, by erw [f.map_add ⟨x, hfx⟩ ⟨y, hfy⟩, g.map_add ⟨x, hgx⟩ ⟨y, hgy⟩, hx, hy]⟩, smul_mem' := λ c x ⟨hfx, hgx, hx⟩, ⟨smul_mem _ c hfx, smul_mem _ c hgx, by erw [f.map_smul c ⟨x, hfx⟩, g.map_smul c ⟨x, hgx⟩, hx]⟩ } instance : has_inf (linear_pmap R E F) := ⟨λ f g, ⟨f.eq_locus g, f.to_fun.comp $ of_le $ λ x hx, hx.fst⟩⟩ instance : has_bot (linear_pmap R E F) := ⟨⟨⊥, 0⟩⟩ instance : inhabited (linear_pmap R E F) := ⟨⊥⟩ instance : semilattice_inf (linear_pmap R E F) := { le := (≤), le_refl := λ f, ⟨le_refl f.domain, λ x y h, subtype.eq h ▸ rfl⟩, le_trans := λ f g h ⟨fg_le, fg_eq⟩ ⟨gh_le, gh_eq⟩, ⟨le_trans fg_le gh_le, λ x z hxz, have hxy : (x:E) = of_le fg_le x, from rfl, (fg_eq hxy).trans (gh_eq $ hxy.symm.trans hxz)⟩, le_antisymm := λ f g fg gf, eq_of_le_of_domain_eq fg (le_antisymm fg.1 gf.1), inf := (⊓), le_inf := λ f g h ⟨fg_le, fg_eq⟩ ⟨fh_le, fh_eq⟩, ⟨λ x hx, ⟨fg_le hx, fh_le hx, by refine (fg_eq _).symm.trans (fh_eq _); [exact ⟨x, hx⟩, refl, refl]⟩, λ x ⟨y, yg, hy⟩ h, by { apply fg_eq, exact h }⟩, inf_le_left := λ f g, ⟨λ x hx, hx.fst, λ x y h, congr_arg f $ subtype.eq $ by exact h⟩, inf_le_right := λ f g, ⟨λ x hx, hx.snd.fst, λ ⟨x, xf, xg, hx⟩ y h, hx.trans $ congr_arg g $ subtype.eq $ by exact h⟩ } instance : order_bot (linear_pmap R E F) := { bot := ⊥, bot_le := λ f, ⟨bot_le, λ x y h, have hx : x = 0, from subtype.eq ((mem_bot R).1 x.2), have hy : y = 0, from subtype.eq (h.symm.trans (congr_arg _ hx)), by rw [hx, hy, map_zero, map_zero]⟩ } lemma le_of_eq_locus_ge {f g : linear_pmap R E F} (H : f.domain ≤ f.eq_locus g) : f ≤ g := suffices f ≤ f ⊓ g, from le_trans this inf_le_right, ⟨H, λ x y hxy, ((inf_le_left : f ⊓ g ≤ f).2 hxy.symm).symm⟩ lemma domain_mono : strict_mono (@domain R _ E _ _ F _ _) := λ f g hlt, lt_of_le_of_ne hlt.1.1 $ λ heq, ne_of_lt hlt $ eq_of_le_of_domain_eq (le_of_lt hlt) heq private lemma sup_aux (f g : linear_pmap R E F) (h : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y) : ∃ fg : ↥(f.domain ⊔ g.domain) →ₗ[R] F, ∀ (x : f.domain) (y : g.domain) (z), (x:E) + y = ↑z → fg z = f x + g y := begin choose x hx y hy hxy using λ z : f.domain ⊔ g.domain, mem_sup.1 z.prop, set fg := λ z, f ⟨x z, hx z⟩ + g ⟨y z, hy z⟩, have fg_eq : ∀ (x' : f.domain) (y' : g.domain) (z' : f.domain ⊔ g.domain) (H : (x':E) + y' = z'), fg z' = f x' + g y', { intros x' y' z' H, dsimp [fg], rw [add_comm, ← sub_eq_sub_iff_add_eq_add, eq_comm, ← map_sub, ← map_sub], apply h, simp only [← eq_sub_iff_add_eq] at hxy, simp only [coe_sub, coe_mk, coe_mk, hxy, ← sub_add, ← sub_sub, sub_self, zero_sub, ← H], apply neg_add_eq_sub }, refine ⟨{ to_fun := fg, .. }, fg_eq⟩, { rintros ⟨z₁, hz₁⟩ ⟨z₂, hz₂⟩, rw [← add_assoc, add_right_comm (f _), ← map_add, add_assoc, ← map_add], apply fg_eq, simp only [coe_add, coe_mk, ← add_assoc], rw [add_right_comm (x _), hxy, add_assoc, hxy, coe_mk, coe_mk] }, { intros c z, rw [smul_add, ← map_smul, ← map_smul], apply fg_eq, simp only [coe_smul, coe_mk, ← smul_add, hxy, ring_hom.id_apply] }, end /-- Given two partial linear maps that agree on the intersection of their domains, `f.sup g h` is the unique partial linear map on `f.domain ⊔ g.domain` that agrees with `f` and `g`. -/ protected noncomputable def sup (f g : linear_pmap R E F) (h : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y) : linear_pmap R E F := ⟨_, classical.some (sup_aux f g h)⟩ @[simp] lemma domain_sup (f g : linear_pmap R E F) (h : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y) : (f.sup g h).domain = f.domain ⊔ g.domain := rfl lemma sup_apply {f g : linear_pmap R E F} (H : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y) (x y z) (hz : (↑x:E) + ↑y = ↑z) : f.sup g H z = f x + g y := classical.some_spec (sup_aux f g H) x y z hz protected lemma left_le_sup (f g : linear_pmap R E F) (h : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y) : f ≤ f.sup g h := begin refine ⟨le_sup_left, λ z₁ z₂ hz, _⟩, rw [← add_zero (f _), ← g.map_zero], refine (sup_apply h _ _ _ _).symm, simpa end protected lemma right_le_sup (f g : linear_pmap R E F) (h : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y) : g ≤ f.sup g h := begin refine ⟨le_sup_right, λ z₁ z₂ hz, _⟩, rw [← zero_add (g _), ← f.map_zero], refine (sup_apply h _ _ _ _).symm, simpa end protected lemma sup_le {f g h : linear_pmap R E F} (H : ∀ (x : f.domain) (y : g.domain), (x:E) = y → f x = g y) (fh : f ≤ h) (gh : g ≤ h) : f.sup g H ≤ h := have Hf : f ≤ (f.sup g H) ⊓ h, from le_inf (f.left_le_sup g H) fh, have Hg : g ≤ (f.sup g H) ⊓ h, from le_inf (f.right_le_sup g H) gh, le_of_eq_locus_ge $ sup_le Hf.1 Hg.1 /-- Hypothesis for `linear_pmap.sup` holds, if `f.domain` is disjoint with `g.domain`. -/ lemma sup_h_of_disjoint (f g : linear_pmap R E F) (h : disjoint f.domain g.domain) (x : f.domain) (y : g.domain) (hxy : (x:E) = y) : f x = g y := begin rw [disjoint_def] at h, have hy : y = 0, from subtype.eq (h y (hxy ▸ x.2) y.2), have hx : x = 0, from subtype.eq (hxy.trans $ congr_arg _ hy), simp [*] end section variables {K : Type*} [division_ring K] [module K E] [module K F] /-- Extend a `linear_pmap` to `f.domain ⊔ K ∙ x`. -/ noncomputable def sup_span_singleton (f : linear_pmap K E F) (x : E) (y : F) (hx : x ∉ f.domain) : linear_pmap K E F := f.sup (mk_span_singleton x y (λ h₀, hx $ h₀.symm ▸ f.domain.zero_mem)) $ sup_h_of_disjoint _ _ $ by simpa [disjoint_span_singleton] @[simp] lemma domain_sup_span_singleton (f : linear_pmap K E F) (x : E) (y : F) (hx : x ∉ f.domain) : (f.sup_span_singleton x y hx).domain = f.domain ⊔ K ∙ x := rfl @[simp] lemma sup_span_singleton_apply_mk (f : linear_pmap K E F) (x : E) (y : F) (hx : x ∉ f.domain) (x' : E) (hx' : x' ∈ f.domain) (c : K) : f.sup_span_singleton x y hx ⟨x' + c • x, mem_sup.2 ⟨x', hx', _, mem_span_singleton.2 ⟨c, rfl⟩, rfl⟩⟩ = f ⟨x', hx'⟩ + c • y := begin erw [sup_apply _ ⟨x', hx'⟩ ⟨c • x, _⟩, mk_span_singleton_apply], refl, exact mem_span_singleton.2 ⟨c, rfl⟩ end end private lemma Sup_aux (c : set (linear_pmap R E F)) (hc : directed_on (≤) c) : ∃ f : ↥(Sup (domain '' c)) →ₗ[R] F, (⟨_, f⟩ : linear_pmap R E F) ∈ upper_bounds c := begin cases c.eq_empty_or_nonempty with ceq cne, { subst c, simp }, have hdir : directed_on (≤) (domain '' c), from directed_on_image.2 (hc.mono domain_mono.monotone), have P : Π x : Sup (domain '' c), {p : c // (x : E) ∈ p.val.domain }, { rintros x, apply classical.indefinite_description, have := (mem_Sup_of_directed (cne.image _) hdir).1 x.2, rwa [bex_image_iff, set_coe.exists'] at this }, set f : Sup (domain '' c) → F := λ x, (P x).val.val ⟨x, (P x).property⟩, have f_eq : ∀ (p : c) (x : Sup (domain '' c)) (y : p.1.1) (hxy : (x : E) = y), f x = p.1 y, { intros p x y hxy, rcases hc (P x).1.1 (P x).1.2 p.1 p.2 with ⟨q, hqc, hxq, hpq⟩, refine (hxq.2 _).trans (hpq.2 _).symm, exacts [of_le hpq.1 y, hxy, rfl] }, refine ⟨{ to_fun := f, .. }, _⟩, { intros x y, rcases hc (P x).1.1 (P x).1.2 (P y).1.1 (P y).1.2 with ⟨p, hpc, hpx, hpy⟩, set x' := of_le hpx.1 ⟨x, (P x).2⟩, set y' := of_le hpy.1 ⟨y, (P y).2⟩, rw [f_eq ⟨p, hpc⟩ x x' rfl, f_eq ⟨p, hpc⟩ y y' rfl, f_eq ⟨p, hpc⟩ (x + y) (x' + y') rfl, map_add] }, { intros c x, simp [f_eq (P x).1 (c • x) (c • ⟨x, (P x).2⟩) rfl, ← map_smul] }, { intros p hpc, refine ⟨le_Sup $ mem_image_of_mem domain hpc, λ x y hxy, eq.symm _⟩, exact f_eq ⟨p, hpc⟩ _ _ hxy.symm } end /-- Glue a collection of partially defined linear maps to a linear map defined on `Sup` of these submodules. -/ protected noncomputable def Sup (c : set (linear_pmap R E F)) (hc : directed_on (≤) c) : linear_pmap R E F := ⟨_, classical.some $ Sup_aux c hc⟩ protected lemma le_Sup {c : set (linear_pmap R E F)} (hc : directed_on (≤) c) {f : linear_pmap R E F} (hf : f ∈ c) : f ≤ linear_pmap.Sup c hc := classical.some_spec (Sup_aux c hc) hf protected lemma Sup_le {c : set (linear_pmap R E F)} (hc : directed_on (≤) c) {g : linear_pmap R E F} (hg : ∀ f ∈ c, f ≤ g) : linear_pmap.Sup c hc ≤ g := le_of_eq_locus_ge $ Sup_le $ λ _ ⟨f, hf, eq⟩, eq ▸ have f ≤ (linear_pmap.Sup c hc) ⊓ g, from le_inf (linear_pmap.le_Sup _ hf) (hg f hf), this.1 end linear_pmap namespace linear_map /-- Restrict a linear map to a submodule, reinterpreting the result as a `linear_pmap`. -/ def to_pmap (f : E →ₗ[R] F) (p : submodule R E) : linear_pmap R E F := ⟨p, f.comp p.subtype⟩ @[simp] lemma to_pmap_apply (f : E →ₗ[R] F) (p : submodule R E) (x : p) : f.to_pmap p x = f x := rfl /-- Compose a linear map with a `linear_pmap` -/ def comp_pmap (g : F →ₗ[R] G) (f : linear_pmap R E F) : linear_pmap R E G := { domain := f.domain, to_fun := g.comp f.to_fun } @[simp] lemma comp_pmap_apply (g : F →ₗ[R] G) (f : linear_pmap R E F) (x) : g.comp_pmap f x = g (f x) := rfl end linear_map namespace linear_pmap /-- Restrict codomain of a `linear_pmap` -/ def cod_restrict (f : linear_pmap R E F) (p : submodule R F) (H : ∀ x, f x ∈ p) : linear_pmap R E p := { domain := f.domain, to_fun := f.to_fun.cod_restrict p H } /-- Compose two `linear_pmap`s -/ def comp (g : linear_pmap R F G) (f : linear_pmap R E F) (H : ∀ x : f.domain, f x ∈ g.domain) : linear_pmap R E G := g.to_fun.comp_pmap $ f.cod_restrict _ H /-- `f.coprod g` is the partially defined linear map defined on `f.domain × g.domain`, and sending `p` to `f p.1 + g p.2`. -/ def coprod (f : linear_pmap R E G) (g : linear_pmap R F G) : linear_pmap R (E × F) G := { domain := f.domain.prod g.domain, to_fun := (f.comp (linear_pmap.fst f.domain g.domain) (λ x, x.2.1)).to_fun + (g.comp (linear_pmap.snd f.domain g.domain) (λ x, x.2.2)).to_fun } @[simp] lemma coprod_apply (f : linear_pmap R E G) (g : linear_pmap R F G) (x) : f.coprod g x = f ⟨(x : E × F).1, x.2.1⟩ + g ⟨(x : E × F).2, x.2.2⟩ := rfl end linear_pmap
1d54cec791b46b2ced1d9d414bf0870b662da4cf
d6124c8dbe5661dcc5b8c9da0a56fbf1f0480ad6
/Papyrus/IR/Type.lean
3593e1372a5269104fdc4ca173c837e65b9e3b44
[ "Apache-2.0" ]
permissive
xubaiw/lean4-papyrus
c3fbbf8ba162eb5f210155ae4e20feb2d32c8182
02e82973a5badda26fc0f9fd15b3d37e2eb309e0
refs/heads/master
1,691,425,756,824
1,632,122,825,000
1,632,123,075,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,896
lean
import Papyrus.IR.TypeID import Papyrus.IR.TypeRefs import Papyrus.IR.TypeBases import Papyrus.IR.AddressSpace namespace Papyrus -- # The LLVM Type Inductive /-- A pure representation of an LLVM [Type](https://llvm.org/doxygen/classllvm_1_1Type.html). -/ inductive «Type» | half | bfloat | float | double | x86FP80 | fp128 | ppcFP128 | void | label | metadata | x86MMX | x86AMX | token | integer (type : IntegerType) | function (type : BaseFunctionType «Type») | pointer (type : BasePointerType «Type») | struct (type : BaseStructType «Type») | array (type : BaseArrayType «Type») | fixedVector (type : BaseFixedVectorType «Type») | scalableVector (type : BaseScalableVectorType «Type») deriving BEq, Repr /-- The LLVM type ID of this type. -/ @[extern "lean_ptr_tag"] def Type.typeID : (self : @& «Type») → TypeID | half => TypeID.half | bfloat => TypeID.bfloat | float => TypeID.float | double => TypeID.double | x86FP80 => TypeID.x86FP80 | fp128 => TypeID.fp128 | ppcFP128 => TypeID.ppcFP128 | void => TypeID.void | label => TypeID.label | metadata => TypeID.metadata | x86MMX => TypeID.x86MMX | x86AMX => TypeID.x86AMX | token => TypeID.token | integer .. => TypeID.integer | function .. => TypeID.function | pointer .. => TypeID.pointer | struct .. => TypeID.struct | array .. => TypeID.array | fixedVector .. => TypeID.fixedVector | scalableVector .. => TypeID.scalableVector -- # Type -> TypeRef open BaseStructType in /-- Get a reference to an external LLVM representation of this type. -/ partial def Type.getRef : (type : «Type») → LlvmM TypeRef | half => HalfTypeRef.get | bfloat => BFloatTypeRef.get | float => FloatTypeRef.get | double => DoubleTypeRef.get | x86FP80 => X86FP80TypeRef.get | fp128 => FP128TypeRef.get | ppcFP128 => PPCFP128TypeRef.get | void => VoidTypeRef.get | label => LabelTypeRef.get | metadata => MetadataTypeRef.get | x86MMX => X86MMXTypeRef.get | x86AMX => X86AMXTypeRef.get | token => TokenTypeRef.get | integer ⟨bitWidth⟩ => IntegerTypeRef.get bitWidth | function ⟨retType, paramTypes, isVarArg⟩ => do FunctionTypeRef.get (← getRef retType) (← paramTypes.mapM getRef) isVarArg | pointer ⟨pointeeType, addrSpace⟩ => do PointerTypeRef.get (← getRef pointeeType) addrSpace | struct type => match type with | literal ⟨elemTypes, isPacked⟩ => do LiteralStructTypeRef.get (← elemTypes.mapM getRef) isPacked | complete name ⟨elemTypes, isPacked⟩ => do IdentifiedStructTypeRef.getOrCreate name (← elemTypes.mapM getRef) isPacked | opaque name => IdentifiedStructTypeRef.getOrCreateOpaque name | array ⟨elemType, numElems⟩ => do ArrayTypeRef.get (← getRef elemType) numElems | fixedVector ⟨elemType, numElems⟩ => do FixedVectorTypeRef.get (← getRef elemType) numElems | scalableVector ⟨elemType, minNumElems⟩ => do ScalableVectorTypeRef.get (← getRef elemType) minNumElems -- # TypeRef -> Type open TypeID in /-- Lift this reference to a pure `Type`. -/ partial def TypeRef.purify (self : TypeRef) : IO «Type» := do match h:self.typeID with | half => Type.half | bfloat => Type.bfloat | float => Type.float | double => Type.double | x86FP80 => Type.x86FP80 | fp128 => Type.fp128 | ppcFP128 => Type.ppcFP128 | void => Type.void | label => Type.label | metadata => Type.metadata | x86MMX => Type.x86MMX | x86AMX => Type.x86AMX | token => Type.token | integer => let self := IntegerTypeRef.cast self h Type.integer ⟨← self.getBitWidth⟩ | function => let self := FunctionTypeRef.cast self h Type.function ⟨← purify <| ← self.getReturnType, ← Array.mapM purify <| ← self.getParameterTypes, ← self.isVarArg⟩ | pointer => let self := PointerTypeRef.cast self h Type.pointer ⟨← purify <| ← self.getPointeeType, ← self.getAddressSpace⟩ | struct => let self := StructTypeRef.cast self h if h : self.isLiteral then let self := LiteralStructTypeRef.cast self h Type.struct <| BaseStructType.literal ⟨← Array.mapM purify <| ← self.getElementTypes, ← self.isPacked⟩ else let self := IdentifiedStructTypeRef.cast self h if (← self.isOpaque) then Type.struct <| BaseStructType.opaque (← self.getName) else Type.struct <| BaseStructType.complete (← self.getName) ⟨← Array.mapM purify <| ← self.getElementTypes, ← self.isPacked⟩ | array => let self := ArrayTypeRef.cast self h Type.array ⟨← purify <| ← self.getElementType, ← self.getSize⟩ | fixedVector => let self := FixedVectorTypeRef.cast self h Type.fixedVector ⟨← purify <| ← self.getElementType, ← self.getSize⟩ | scalableVector => let self := ScalableVectorTypeRef.cast self h Type.scalableVector ⟨← purify <| ← self.getElementType, ← self.getMinSize⟩
82427c6485c8e530926542c7f486af8d65bf8f7c
3f48345ac9bbaa421714efc9872a0409379bb4ae
/src/coalgebra/subcoalgebra.lean
40f72ca2851c57079a32a9999d757774dfdb06dd
[]
no_license
QaisHamarneh/Coalgebra-in-Lean
b4318ee6d83780e5c734eb78fed98b1fe8016f7e
bd0452df98bc64b608e5dfd7babc42c301bb6a46
refs/heads/master
1,663,371,200,241
1,661,004,695,000
1,661,004,695,000
209,798,828
0
0
null
null
null
null
UTF-8
Lean
false
false
7,880
lean
import set_category.category_set import coalgebra.Coalgebra import help_functions universe u namespace subcoalgebra open category_theory set category_set coalgebra function classical help_functions variables {F : Type u ⥤ Type u} {𝔸 Β ℂ : Coalgebra F} /-- Openset is a prop that checks if there exists a coalgebra structure α : S → F(S), such that The inclusion from ⟨S, α⟩ to 𝔸 is a homomorphism -/ def openset {𝔸 : Coalgebra F} (S : set 𝔸) : Prop := ∃ α : S → F.obj S , @is_coalgebra_homomorphism F ⟨S , α⟩ 𝔸 (S ↪ 𝔸) /-- The structure SubCoalgebra from the set S ⊆ 𝔸 consists of α : S → F(S) and a proof h, that the inclusion from ⟨S, α⟩ is a homomorphism -/ structure SubCoalgebra (S : set 𝔸) := (α : S → F.obj S) (h: @is_coalgebra_homomorphism F ⟨S , α⟩ 𝔸 (S ↪ 𝔸)) noncomputable def openset_to_subcoalgebra {S : set 𝔸} (o : openset S): SubCoalgebra S := ⟨some o, some_spec o⟩ instance SubCoalgebra_is_Coalgebra (S : set 𝔸): has_coe (SubCoalgebra S) (Coalgebra F) := ⟨λ Sub , ⟨S , Sub.α⟩ ⟩ lemma subcoalgebra_unique_structure (S : set 𝔸) (h : openset S) : let α := some h in ∀ σ : S → F.obj S, @is_coalgebra_homomorphism F ⟨S , σ⟩ 𝔸 (S ↪ 𝔸) → σ = α := begin intros α σ h0, let coS : SubCoalgebra S := ⟨α , (some_spec h)⟩, cases classical.em (nonempty S) with nonemp emp, haveI inh : inhabited S := nonemptyInhabited nonemp, have hom : @is_coalgebra_homomorphism F coS 𝔸 (S ↪ 𝔸) := some_spec h, have h2 : (F.map (S ↪ 𝔸)) ∘ α = (F.map (S ↪ 𝔸)) ∘ σ := calc (F.map (S ↪ 𝔸)) ∘ α = 𝔸.α ∘ (S ↪ 𝔸) : eq.symm hom ... = (F.map (S ↪ 𝔸)) ∘ σ : h0, haveI h3 : mono (F.map (S ↪ 𝔸)) := mono_preserving_functor (S ↪ 𝔸) (inj_inclusion 𝔸 S), exact eq.symm (left_cancel (F.map (S ↪ 𝔸)) h2), have h2 : ∀ (f₁ f₂ : S → F.obj S), f₁ = f₂ := map_from_empty S (F.obj S) (nonempty_notexists emp), exact h2 σ (some h) end lemma surj_hom_to_coStructure (ϕ : homomorphism 𝔸 Β) (sur : surjective ϕ) : let χ : Β → F.obj Β := λ b, ((F.map ϕ) ∘ 𝔸.α) (some (sur b)) in Β.α = χ := begin intro χ, have elements : ∀ b, Β.α b= χ b := begin intro b, let a := some (sur b), have a_b : ϕ a = b := some_spec (sur b), have χ_b : χ b = ((F.map ϕ) ∘ 𝔸.α) a := rfl, have hom_ϕ : Β.α ∘ ϕ = (F.map ϕ) ∘ 𝔸.α := ϕ.property, have h_ϕ : ∀ a, Β.α (ϕ a) = ((F.map ϕ) ∘ 𝔸.α) a := λ a , have h1 : (Β.α ∘ ϕ) a = ((F.map ϕ) ∘ 𝔸.α) a := by rw hom_ϕ, h1, have α_a : Β.α b = ((F.map ϕ) ∘ 𝔸.α) a := a_b ▸ (h_ϕ a), rw α_a, end, exact funext elements end def congruence (ϕ : homomorphism 𝔸 Β) : 𝔸 → 𝔸 → Prop := kern ϕ def congruence2 (h: ∃ ϕ : 𝔸 → Β , is_coalgebra_homomorphism ϕ) : 𝔸 → 𝔸 → Prop := kern (some h) def homomorphic_image (𝔸 Β: Coalgebra F) : Prop := ∃ ϕ : homomorphism 𝔸 Β, surjective ϕ def decompose (f: homomorphism 𝔸 Β) : f.val = ((range f) ↪ Β) ∘ range_factorization f := rfl lemma structure_existance (ϕ : homomorphism 𝔸 Β) : ∃ α : (range ϕ) → F.obj (range ϕ), let ℝ : Coalgebra F :=⟨range ϕ , α⟩ in @is_coalgebra_homomorphism F 𝔸 ℝ (range_factorization ϕ) ∧ @is_coalgebra_homomorphism F ℝ Β ((range ϕ) ↪ Β) := begin have ex : _ := Factorization ϕ (range_factorization ϕ) ((range ϕ) ↪ Β) (decompose ϕ) ((epi_iff_surjective (range_factorization ϕ)).2 surjective_onto_range) (inj_inclusion Β (range ϕ)), cases ex with α hom, exact exists.intro α hom.left end def homomorphic_image_of_range (ϕ : homomorphism 𝔸 Β) [inhabited 𝔸] : ∃ α : (range ϕ) → F.obj (range ϕ), homomorphic_image 𝔸 ⟨range ϕ , α⟩ := begin have ex : _ := structure_existance ϕ, cases ex with α hom , let coalg : Coalgebra F:= ⟨range ϕ , α⟩ , have h : homomorphic_image 𝔸 coalg := have x : true := trivial, exists.intro ⟨range_factorization ϕ , hom.left⟩ surjective_onto_range, exact exists.intro α h end noncomputable lemma range_is_subCoalgebra (ϕ : homomorphism 𝔸 Β) : SubCoalgebra (range ϕ) := have ex : _ := structure_existance ϕ, let α : (range ϕ) → F.obj (range ϕ) := some ex in ⟨α , (some_spec ex).right⟩ noncomputable lemma empty_is_Subcoalgebra (𝔸 : Coalgebra F) : SubCoalgebra (∅ : set 𝔸) := { α := empty_map (∅ : set 𝔸) not_nonempty_empty (F.obj (∅ : set 𝔸)), h := by tidy } lemma empty_is_openset (𝔸 : Coalgebra F) : openset (∅ : set 𝔸) := begin let α := empty_map (∅ : set 𝔸) not_nonempty_empty (F.obj (∅ : set 𝔸)), use α, tidy end lemma empty_openset {S: set 𝔸} (emp : ¬ nonempty S): openset S := begin let α : S → F.obj S := empty_map S emp (F.obj S), use α, exact empty_hom_dom (inclusion S) emp end def is_largest_coalgebra {S : set 𝔸} (P : set S): Prop := (∃ α : P → F.obj P , @is_coalgebra_homomorphism F ⟨P, α⟩ 𝔸 ((S ↪ 𝔸) ∘ (P ↪ S))) ∧ ∀ P₁ : set S, (∃ α : P₁ → F.obj P₁ , @is_coalgebra_homomorphism F ⟨P₁, α⟩ 𝔸 ((S ↪ 𝔸) ∘ (P₁ ↪ S))) → P₁ ⊆ P noncomputable def largest_Coalgebra {S : set 𝔸} {P : set S} (lar : is_largest_coalgebra P): Coalgebra F := ⟨P , some lar.1⟩ -- lemma largest_subcoalgebra_exists (S : set 𝔸) : -- ∃ P ⊆ S, is_largest_subcoalgebra S P H := -- begin -- let emp : set 𝔸 := ∅, -- have h : emp ⊆ S := by tidy, -- use emp, -- use h, -- split, -- exact empty_is_openset 𝔸, -- intros p h op, -- end -- def largest_subcoalgebra_set (S : set 𝔸): -- set 𝔸 := some (largest_subcoalgebra_exists S) -- noncomputable def largest_subcoalgebra (S : set 𝔸): -- SubCoalgebra (largest_subcoalgebra_set S) := -- begin -- let P : set 𝔸 := some (largest_subcoalgebra_exists S), -- have op : openset P := (some_spec (some_spec (largest_subcoalgebra_exists S))).1, -- let α : P → F.obj P := some op, -- have hom : _ := some_spec op, -- exact ⟨α ,hom⟩ -- end end subcoalgebra
75ee43d78e0209c5fb9dd50467f2cc60df7694a7
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/library/init/meta/injection_tactic.lean
75e35bceadc7a71cc6793b6d5c01e419094690b7
[ "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
2,038
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.function namespace tactic open nat tactic environment expr list private meta_definition mk_intro_name : name → list name → name | n₁ (n₂ :: ns) := n₂ | n [] := if n = `a then `H else n -- Auxiliary function for introducing the new equalities produced by the -- injection tactic private meta_definition injection_intro : expr → list name → tactic unit | (pi n bi b d) ns := do Hname ← return $ mk_intro_name n ns, H ← intro Hname, Ht ← infer_type H, -- Clear new hypothesis if it is of the form (a = a) @try unit $ do { (lhs, rhs) ← match_eq Ht, unify lhs rhs, clear H }, -- If new hypothesis is of the form (@heq A a B b) where -- A and B can be unified then convert it into (a = b) using -- the eq_of_heq lemma @try unit $ do { (A, lhs, B, rhs) ← match_heq Ht, unify A B, Heq ← mk_app `eq [lhs, rhs], pr ← mk_app `eq_of_heq [H], assertv Hname Heq pr, clear H }, injection_intro d (tail ns) | e ns := skip meta_definition injection_with (H : expr) (ns : list name) : tactic unit := do Ht ← infer_type H, (lhs, rhs) ← match_eq Ht, env ← get_env, if is_constructor_app env lhs = tt ∧ is_constructor_app env rhs = tt ∧ const_name (get_app_fn lhs) = const_name (get_app_fn rhs) then do tgt ← target, I_name ← return $ name.get_prefix (const_name (get_app_fn lhs)), pr ← mk_app (I_name <.> "no_confusion") [tgt, lhs, rhs, H], pr_type ← infer_type pr, pr_type ← whnf pr_type, apply pr, injection_intro (binding_domain pr_type) ns else fail "injection tactic failed, argument must be an equality proof where lhs and rhs are of the form (c ...), where c is a constructor" meta_definition injection (H : expr) : tactic unit := injection_with H [] end tactic
8f22dae22984ffaa81d960b8f004d4a0b9463bae
56e5b79a7ab4f2c52e6eb94f76d8100a25273cf3
/src/utils/sexp.lean
ff259ffdfc2d48412a49273a36186d95adaca65e
[ "Apache-2.0" ]
permissive
DyeKuu/lean-tpe-public
3a9968f286ca182723ef7e7d97e155d8cb6b1e70
750ade767ab28037e80b7a80360d213a875038f8
refs/heads/master
1,682,842,633,115
1,621,330,793,000
1,621,330,793,000
368,475,816
0
0
Apache-2.0
1,621,330,745,000
1,621,330,744,000
null
UTF-8
Lean
false
false
4,184
lean
import tactic.basic section sexp universe u inductive sexp (α : Type u) : Type u | atom : α → sexp | list : (list sexp) → sexp meta def sexp_to_format {α : Type u} [has_to_format α] : sexp α → format | (sexp.atom val) := has_to_format.to_format val | (sexp.list ses) := format.of_string $ "(" ++ (" ".intercalate (ses.map $ format.to_string ∘ sexp_to_format)) ++ ")" meta instance sexp_has_to_format {α} [has_to_format α] : has_to_format (sexp α) := ⟨sexp_to_format⟩ end sexp -- #eval format.to_string $ sexp_to_format $ sexp.list [(sexp.atom 1), (sexp.atom 2), (sexp.atom 3)] open expr section sexp_of_expr -- def sexp.concat {α} : (list $ sexp α) → (list $ sexp α) → sexp α := -- λ xs ys, sexp.list $ xs ++ ys meta def sexp.concat {m} [monad m] [monad_fail m] {α} : (sexp α) → (sexp α) → m (sexp α) | (sexp.list xs) (sexp.list ys) := pure (sexp.list $ xs ++ ys) | _ _ := monad_fail.fail "sexp.concat failed" local infix `<+>`:50 := sexp.concat -- TODO(jesse): just write an applicative instance, don't want to think about `seq` now though meta def sexp.map {α β : Type*} (f : α → β) : sexp α → sexp β | (sexp.atom x) := (sexp.atom $ f x) | (sexp.list xs) := (sexp.list $ sexp.map <$> xs) meta instance : functor sexp := {map := by apply sexp.map} def mk_type_ascription : sexp string → sexp string → sexp string := λ s₁ s₂, sexp.list [(sexp.atom ":"), s₁, s₂] -- TODO(jesse): supply version with even more type annotations meta def sexp_of_expr : (option ℕ) → expr → tactic (sexp string) := λ fuel ex, do { match fuel with | none := pure () | (some x) := when (x = 0) $ tactic.fail "sexp_of_expr fuel exhausted" end, match ex with | e@(var k) := (sexp.list [sexp.atom "var"]) <+> (sexp.list [sexp.atom (to_string k)]) | e@(sort l) := (sexp.list [sexp.atom "sort"]) <+> (sexp.list [sexp.atom (to_string l)]) | e@(const nm ls) := pure $ sexp.atom nm.to_string | e@(mvar un pt tp) := do tp_sexp ← sexp_of_expr ((flip nat.sub 1) <$> fuel) tp, pure $ mk_type_ascription (sexp.atom pt.to_string) tp_sexp | e@(local_const un pt binder_info tp) := do { tp_sexp ← sexp_of_expr ((flip nat.sub 1) <$> fuel) tp, -- pure $ flip mk_type_ascription tp_sexp $ sexp.list [sexp.atom pt.to_string] -- note: drop binder info for now pure (sexp.atom pt.to_string) } | e@(app e₁ e₂) := (λ s₁ s₂, sexp.list [s₁, s₂]) <$> sexp_of_expr ((flip nat.sub 1) <$> fuel) e₁ <*> sexp_of_expr ((flip nat.sub 1) <$> fuel) e₂ -- | e@(app e₁ e₂) := sexp.list <$> ((::) <$> (sexp_of_expr ((flip nat.sub 1) <$> fuel) $ get_app_fn e) <*> (get_app_args e).mmap (sexp_of_expr ((flip nat.sub 1) <$> fuel))) | e@(lam var_name b_info var_type body) := do { ⟨[b], e'⟩ ← tactic.open_n_lambdas e 1, sexp.list <$> do { b_sexp ← sexp_of_expr ((flip nat.sub 1) <$> fuel) b, b_tp_sexp ← sexp_of_expr ((flip nat.sub 1) <$> fuel) var_type, body_sexp ← sexp_of_expr ((flip nat.sub 1) <$> fuel) e', pure $ [sexp.atom "LAMBDA", mk_type_ascription (b_sexp) b_tp_sexp, body_sexp] } } | e@(pi var_name b_info var_type body) := do { ⟨[b], e'⟩ ← tactic.open_n_pis e 1, sexp.list <$> do { b_sexp ← sexp_of_expr ((flip nat.sub 1) <$> fuel) b, b_tp_sexp ← sexp_of_expr ((flip nat.sub 1) <$> fuel) var_type, body_sexp ← sexp_of_expr ((flip nat.sub 1) <$> fuel) e', pure $ [sexp.atom "PI", mk_type_ascription (b_sexp) b_tp_sexp, body_sexp] } } -- reduce let expressions before sexpr serialization | e@(elet var_name var_type var_assignment body) := sexp_of_expr ((flip nat.sub 1) <$> fuel) e.reduce_let | e@(macro md deps) := sexp.list <$> do { deps_sexp_list ← sexp.list <$> (deps.mmap $ sexp_of_expr (((flip nat.sub 1) <$> fuel))), let deps_sexp := sexp.list [sexp.atom "MACRO_DEPS", deps_sexp_list], pure $ [sexp.atom "MACRO", sexp.atom (expr.macro_def_name md).to_string, deps_sexp] } end } meta def flattened_sexp_of_expr (e : expr) : tactic string := (format.to_string ∘ format.flatten ∘ has_to_format.to_format) <$> sexp_of_expr none e end sexp_of_expr
99a2aba94c1e7ef56bad9fad9346145932559da5
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/run/parent_struct_inst.lean
cdaaff88d86701ecb2d9d2d0dcc798fb20ec07cb
[ "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
136
lean
open nat structure [class] A := (n : ℕ) definition f [A] := A.n structure B extends A := (Hf : f = 0) example : B := ⟨0, rfl⟩
cfdd036eb4fa642cca37d79748eb6d53730d83e4
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/data/fp/basic.lean
51439d572d9870284fb4599262767b505d7b7965
[ "Apache-2.0" ]
permissive
fpvandoorn/mathlib
b21ab4068db079cbb8590b58fda9cc4bc1f35df4
b3433a51ea8bc07c4159c1073838fc0ee9b8f227
refs/heads/master
1,624,791,089,608
1,556,715,231,000
1,556,715,231,000
165,722,980
5
0
Apache-2.0
1,552,657,455,000
1,547,494,646,000
Lean
UTF-8
Lean
false
false
6,214
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro Implementation of floating-point numbers (experimental). -/ import data.rat data.semiquot def int.shift2 (a b : ℕ) : ℤ → ℕ × ℕ | (int.of_nat e) := (a.shiftl e, b) | -[1+ e] := (a, b.shiftl e.succ) namespace fp inductive rmode | NE -- round to nearest even class float_cfg := (prec emax : ℕ) (prec_pos : prec > 0) (prec_max : prec ≤ emax) variable [C : float_cfg] include C def prec := C.prec def emax := C.emax def emin : ℤ := 1 - C.emax def valid_finite (e : ℤ) (m : ℕ) : Prop := emin ≤ e + prec - 1 ∧ e + prec - 1 ≤ emax ∧ e = max (e + m.size - prec) emin instance dec_valid_finite (e m) : decidable (valid_finite e m) := by unfold valid_finite; apply_instance inductive float | inf : bool → float | nan : float | finite : bool → Π e m, valid_finite e m → float def float.is_finite : float → bool | (float.finite s e m f) := tt | _ := ff def to_rat : Π (f : float), f.is_finite → ℚ | (float.finite s e m f) _ := let (n, d) := int.shift2 m 1 e, r := rat.mk_nat n d in if s then -r else r theorem float.zero.valid : valid_finite emin 0 := ⟨begin rw add_sub_assoc, apply le_add_of_nonneg_right, apply sub_nonneg_of_le, apply int.coe_nat_le_coe_nat_of_le, exact C.prec_pos end, by simpa [emin] using show (prec : ℤ) ≤ emax + float_cfg.emax, from le_trans (int.coe_nat_le.2 C.prec_max) (le_add_of_nonneg_left (int.coe_zero_le _)), by rw max_eq_right; simp⟩ def float.zero (s : bool) : float := float.finite s emin 0 float.zero.valid protected def float.sign' : float → semiquot bool | (float.inf s) := pure s | float.nan := ⊤ | (float.finite s e m f) := pure s protected def float.sign : float → bool | (float.inf s) := s | float.nan := ff | (float.finite s e m f) := s protected def float.is_zero : float → bool | (float.finite s e 0 f) := tt | _ := ff protected def float.neg : float → float | (float.inf s) := float.inf (bnot s) | float.nan := float.nan | (float.finite s e m f) := float.finite (bnot s) e m f def div_nat_lt_two_pow (n d : ℕ) : ℤ → bool | (int.of_nat e) := n < d.shiftl e | -[1+ e] := n.shiftl e.succ < d -- TODO(Mario): Prove these and drop 'meta' meta def of_pos_rat_dn (n : ℕ+) (d : ℕ+) : float × bool := begin let e₁ : ℤ := n.1.size - d.1.size - prec, cases h₁ : int.shift2 d.1 n.1 (e₁ + prec) with d₁ n₁, let e₂ := if n₁ < d₁ then e₁ - 1 else e₁, let e₃ := max e₂ emin, cases h₂ : int.shift2 d.1 n.1 (e₃ + prec) with d₂ n₂, let r := rat.mk_nat n₂ d₂, let m := r.floor, refine (float.finite ff e₃ (int.to_nat m) _, r.denom = 1), { exact undefined } end meta def next_up_pos (e m) (v : valid_finite e m) : float := let m' := m.succ in if ss : m'.size = m.size then float.finite ff e m' (by unfold valid_finite at *; rw ss; exact v) else if h : e = emax then float.inf ff else float.finite ff e.succ (nat.div2 m') undefined meta def next_dn_pos (e m) (v : valid_finite e m) : float := match m with | 0 := next_up_pos _ _ float.zero.valid | nat.succ m' := if ss : m'.size = m.size then float.finite ff e m' (by unfold valid_finite at *; rw ss; exact v) else if h : e = emin then float.finite ff emin m' undefined else float.finite ff e.pred (bit1 m') undefined end meta def next_up : float → float | (float.finite ff e m f) := next_up_pos e m f | (float.finite tt e m f) := float.neg $ next_dn_pos e m f | f := f meta def next_dn : float → float | (float.finite ff e m f) := next_dn_pos e m f | (float.finite tt e m f) := float.neg $ next_up_pos e m f | f := f meta def of_rat_up : ℚ → float | ⟨0, _, _, _⟩ := float.zero ff | ⟨nat.succ n, d, h, _⟩ := let (f, exact) := of_pos_rat_dn n.succ_pnat ⟨d, h⟩ in if exact then f else next_up f | ⟨-[1+n], d, h, _⟩ := float.neg (of_pos_rat_dn n.succ_pnat ⟨d, h⟩).1 meta def of_rat_dn (r : ℚ) : float := float.neg $ of_rat_up (-r) meta def of_rat : rmode → ℚ → float | rmode.NE r := let low := of_rat_dn r, high := of_rat_up r in if hf : high.is_finite then if r = to_rat _ hf then high else if lf : low.is_finite then if r - to_rat _ lf > to_rat _ hf - r then high else if r - to_rat _ lf < to_rat _ hf - r then low else match low, lf with float.finite s e m f, _ := if 2 ∣ m then low else high end else float.inf tt else float.inf ff namespace float instance : has_neg float := ⟨float.neg⟩ meta def add (mode : rmode) : float → float → float | nan _ := nan | _ nan := nan | (inf tt) (inf ff) := nan | (inf ff) (inf tt) := nan | (inf s₁) _ := inf s₁ | _ (inf s₂) := inf s₂ | (finite s₁ e₁ m₁ v₁) (finite s₂ e₂ m₂ v₂) := let f₁ := finite s₁ e₁ m₁ v₁, f₂ := finite s₂ e₂ m₂ v₂ in of_rat mode (to_rat f₁ rfl + to_rat f₂ rfl) meta instance : has_add float := ⟨float.add rmode.NE⟩ meta def sub (mode : rmode) (f1 f2 : float) : float := add mode f1 (-f2) meta instance : has_sub float := ⟨float.sub rmode.NE⟩ meta def mul (mode : rmode) : float → float → float | nan _ := nan | _ nan := nan | (inf s₁) f₂ := if f₂.is_zero then nan else inf (bxor s₁ f₂.sign) | f₁ (inf s₂) := if f₁.is_zero then nan else inf (bxor f₁.sign s₂) | (finite s₁ e₁ m₁ v₁) (finite s₂ e₂ m₂ v₂) := let f₁ := finite s₁ e₁ m₁ v₁, f₂ := finite s₂ e₂ m₂ v₂ in of_rat mode (to_rat f₁ rfl * to_rat f₂ rfl) meta def div (mode : rmode) : float → float → float | nan _ := nan | _ nan := nan | (inf s₁) (inf s₂) := nan | (inf s₁) f₂ := inf (bxor s₁ f₂.sign) | f₁ (inf s₂) := zero (bxor f₁.sign s₂) | (finite s₁ e₁ m₁ v₁) (finite s₂ e₂ m₂ v₂) := let f₁ := finite s₁ e₁ m₁ v₁, f₂ := finite s₂ e₂ m₂ v₂ in if f₂.is_zero then inf (bxor s₁ s₂) else of_rat mode (to_rat f₁ rfl / to_rat f₂ rfl) end float end fp
c0ff3f255ec407339a0f7269cf08db0bea2136f2
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/topology/algebra/ordered/left_right.lean
5540e488258f4dd6cd70a249f97dd1f318c6e97e
[ "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
2,495
lean
/- Copyright (c) 2021 Anatole Dedecker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anatole Dedecker -/ import topology.continuous_on /-! # Left and right continuity In this file we prove a few lemmas about left and right continuous functions: * `continuous_within_at_Ioi_iff_Ici`: two definitions of right continuity (with `(a, ∞)` and with `[a, ∞)`) are equivalent; * `continuous_within_at_Iio_iff_Iic`: two definitions of left continuity (with `(-∞, a)` and with `(-∞, a]`) are equivalent; * `continuous_at_iff_continuous_left_right`, `continuous_at_iff_continuous_left'_right'` : a function is continuous at `a` if and only if it is left and right continuous at `a`. ## Tags left continuous, right continuous -/ open set filter open_locale topological_space section partial_order variables {α β : Type*} [topological_space α] [partial_order α] [topological_space β] lemma continuous_within_at_Ioi_iff_Ici {a : α} {f : α → β} : continuous_within_at f (Ioi a) a ↔ continuous_within_at f (Ici a) a := by simp only [← Ici_diff_left, continuous_within_at_diff_self] lemma continuous_within_at_Iio_iff_Iic {a : α} {f : α → β} : continuous_within_at f (Iio a) a ↔ continuous_within_at f (Iic a) a := @continuous_within_at_Ioi_iff_Ici (order_dual α) _ ‹topological_space α› _ _ _ f end partial_order variables {α β : Type*} [topological_space α] [linear_order α] [topological_space β] lemma nhds_left_sup_nhds_right (a : α) : 𝓝[≤] a ⊔ 𝓝[≥] a = 𝓝 a := by rw [← nhds_within_union, Iic_union_Ici, nhds_within_univ] lemma nhds_left'_sup_nhds_right (a : α) : 𝓝[<] a ⊔ 𝓝[≥] a = 𝓝 a := by rw [← nhds_within_union, Iio_union_Ici, nhds_within_univ] lemma nhds_left_sup_nhds_right' (a : α) : 𝓝[≤] a ⊔ 𝓝[>] a = 𝓝 a := by rw [← nhds_within_union, Iic_union_Ioi, nhds_within_univ] lemma continuous_at_iff_continuous_left_right {a : α} {f : α → β} : continuous_at f a ↔ continuous_within_at f (Iic a) a ∧ continuous_within_at f (Ici a) a := by simp only [continuous_within_at, continuous_at, ← tendsto_sup, nhds_left_sup_nhds_right] lemma continuous_at_iff_continuous_left'_right' {a : α} {f : α → β} : continuous_at f a ↔ continuous_within_at f (Iio a) a ∧ continuous_within_at f (Ioi a) a := by rw [continuous_within_at_Ioi_iff_Ici, continuous_within_at_Iio_iff_Iic, continuous_at_iff_continuous_left_right]
a9d549e753d06ed9fb7040fea7232758048f7601
ea11767c9c6a467c4b7710ec6f371c95cfc023fd
/src/monoidal_categories/internal_objects/monoids.lean
182d627f9d55c56da62246dd54a90aa8d2ae5386
[]
no_license
RitaAhmadi/lean-monoidal-categories
68a23f513e902038e44681336b87f659bbc281e0
81f43e1e0d623a96695aa8938951d7422d6d7ba6
refs/heads/master
1,651,458,686,519
1,529,824,613,000
1,529,824,613,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
772
lean
-- Copyright (c) 2017 Scott Morrison. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Stephen Morgan, Scott Morrison import .semigroups open categories open categories.monoidal_category namespace categories.internal_objects universes u v class MonoidObject {C : Type u} [𝒞 : monoidal_category.{u v} C] (A : C) extends SemigroupObject A := ( ι : 𝒞.tensor_unit ⟶ A ) ( left_identity : (ι ⊗ (𝟙 A)) ≫ (SemigroupObject.μ A) = (left_unitor A) ≫ (𝟙 A) ) ( right_identity : ((𝟙 A) ⊗ ι) ≫ (SemigroupObject.μ A) = (right_unitor A) ≫ (𝟙 A) ) attribute [simp,ematch] MonoidObject.left_identity attribute [simp,ematch] MonoidObject.right_identity end categories.internal_objects
a35c9f72dd1c8817aefca1d3caf685cf0d81373b
9dc8cecdf3c4634764a18254e94d43da07142918
/src/algebra/order/ring.lean
9eb3aca53cfe9ae139719462d22dbb66ba6acd72
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
71,866
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro -/ import algebra.char_zero.defs import algebra.order.group import algebra.order.monoid_lemmas_zero_lt import algebra.order.sub import algebra.hom.ring import data.set.intervals.basic /-! # Ordered rings and semirings This file develops the basics of ordered (semi)rings. Each typeclass here comprises * an algebraic class (`semiring`, `comm_semiring`, `ring`, `comm_ring`) * an order class (`partial_order`, `linear_order`) * assumptions on how both interact ((strict) monotonicity, canonicity) For short, * "`+` respects `≤`" means "monotonicity of addition" * "`*` respects `<`" means "strict monotonicity of multiplication by a positive number". ## Typeclasses * `ordered_semiring`: Semiring with a partial order such that `+` respects `≤` and `*` respects `<`. * `ordered_comm_semiring`: Commutative semiring with a partial order such that `+` respects `≤` and `*` respects `<`. * `ordered_ring`: Ring with a partial order such that `+` respects `≤` and `*` respects `<`. * `ordered_comm_ring`: Commutative ring with a partial order such that `+` respects `≤` and `*` respects `<`. * `linear_ordered_semiring`: Semiring with a linear order such that `+` respects `≤` and `*` respects `<`. * `linear_ordered_comm_semiring`: Commutative semiring with a linear order such that `+` respects `≤` and `*` respects `<`. * `linear_ordered_ring`: Ring with a linear order such that `+` respects `≤` and `*` respects `<`. * `linear_ordered_comm_ring`: Commutative ring with a linear order such that `+` respects `≤` and `*` respects `<`. * `canonically_ordered_comm_semiring`: Commutative semiring with a partial order such that `+` respects `≤`, `*` respects `<`, and `a ≤ b ↔ ∃ c, b = a + c`. and some typeclasses to define ordered rings by specifying their nonegative elements: * `nonneg_ring`: To define `ordered_ring`s. * `linear_nonneg_ring`: To define `linear_ordered_ring`s. ## Hierarchy The hardest part of proving order lemmas might be to figure out the correct generality and its corresponding typeclass. Here's an attempt at demystifying it. For each typeclass, we list its immediate predecessors and what conditions are added to each of them. * `ordered_semiring` - `ordered_cancel_add_comm_monoid` & multiplication & `*` respects `<` - `semiring` & partial order structure & `+` respects `≤` & `*` respects `<` * `ordered_comm_semiring` - `ordered_semiring` & commutativity of multiplication - `comm_semiring` & partial order structure & `+` respects `≤` & `*` respects `<` * `ordered_ring` - `ordered_semiring` & additive inverses - `ordered_add_comm_group` & multiplication & `*` respects `<` - `ring` & partial order structure & `+` respects `≤` & `*` respects `<` * `ordered_comm_ring` - `ordered_ring` & commutativity of multiplication - `ordered_comm_semiring` & additive inverses - `comm_ring` & partial order structure & `+` respects `≤` & `*` respects `<` * `linear_ordered_semiring` - `ordered_semiring` & totality of the order & nontriviality * `linear_ordered_comm_semiring` - `ordered_comm_semiring` & totality of the order & nontriviality - `linear_ordered_semiring` & commutativity of multiplication - `linear_ordered_add_comm_monoid` & multiplication & nontriviality & `*` respects `<` * `linear_ordered_ring` - `ordered_ring` & totality of the order & nontriviality - `linear_ordered_semiring` & additive inverses - `linear_ordered_add_comm_group` & multiplication & `*` respects `<` - `domain` & linear order structure * `linear_ordered_comm_ring` - `ordered_comm_ring` & totality of the order & nontriviality - `linear_ordered_ring` & commutativity of multiplication - `linear_ordered_comm_semiring` & additive inverses - `is_domain` & linear order structure * `canonically_ordered_comm_semiring` - `canonically_ordered_add_monoid` & multiplication & `*` respects `<` & no zero divisors - `comm_semiring` & `a ≤ b ↔ ∃ c, b = a + c` & no zero divisors ## TODO We're still missing some typeclasses, like * `canonically_ordered_semiring` They have yet to come up in practice. -/ open function set_option old_structure_cmd true universe u variable {α : Type u} /-! Note that `order_dual` does not satisfy any of the ordered ring typeclasses due to the `zero_le_one` field. -/ lemma add_one_le_two_mul [has_le α] [semiring α] [covariant_class α α (+) (≤)] {a : α} (a1 : 1 ≤ a) : a + 1 ≤ 2 * a := calc a + 1 ≤ a + a : add_le_add_left a1 a ... = 2 * a : (two_mul _).symm /-- An `ordered_semiring α` is a semiring `α` with a partial order such that addition is monotone and multiplication by a positive number is strictly monotone. -/ @[protect_proj] class ordered_semiring (α : Type u) extends semiring α, ordered_cancel_add_comm_monoid α := (zero_le_one : (0 : α) ≤ 1) (mul_lt_mul_of_pos_left : ∀ a b c : α, a < b → 0 < c → c * a < c * b) (mul_lt_mul_of_pos_right : ∀ a b c : α, a < b → 0 < c → a * c < b * c) section ordered_semiring variables [ordered_semiring α] {a b c d : α} @[priority 200] -- see Note [lower instance priority] instance ordered_semiring.pos_mul_strict_mono : pos_mul_strict_mono α := ⟨λ x a b h, ordered_semiring.mul_lt_mul_of_pos_left _ _ _ h x.prop⟩ @[priority 200] -- see Note [lower instance priority] instance ordered_semiring.mul_pos_strict_mono : mul_pos_strict_mono α := ⟨λ x a b h, ordered_semiring.mul_lt_mul_of_pos_right _ _ _ h x.prop⟩ @[priority 100] -- see Note [lower instance priority] instance ordered_semiring.zero_le_one_class : zero_le_one_class α := { ..‹ordered_semiring α› } section nontrivial variables [nontrivial α] @[simp] lemma zero_lt_one : 0 < (1 : α) := lt_of_le_of_ne zero_le_one zero_ne_one lemma zero_lt_two : 0 < (2:α) := add_pos zero_lt_one zero_lt_one @[field_simps] lemma two_ne_zero : (2:α) ≠ 0 := zero_lt_two.ne' lemma one_lt_two : 1 < (2:α) := calc (2:α) = 1+1 : one_add_one_eq_two ... > 1+0 : add_lt_add_left zero_lt_one _ ... = 1 : add_zero 1 lemma zero_lt_three : 0 < (3:α) := add_pos zero_lt_two zero_lt_one @[field_simps] lemma three_ne_zero : (3:α) ≠ 0 := zero_lt_three.ne' lemma zero_lt_four : 0 < (4:α) := add_pos zero_lt_two zero_lt_two @[field_simps] lemma four_ne_zero : (4:α) ≠ 0 := zero_lt_four.ne' alias zero_lt_one ← one_pos alias zero_lt_two ← two_pos alias zero_lt_three ← three_pos alias zero_lt_four ← four_pos @[priority 100] -- see Note [lower instance priority] instance ordered_semiring.to_no_max_order : no_max_order α := ⟨assume a, ⟨a + 1, lt_add_of_pos_right _ one_pos⟩⟩ end nontrivial -- See Note [decidable namespace] protected lemma decidable.mul_le_mul_of_nonneg_left [@decidable_rel α (≤)] (h₁ : a ≤ b) (h₂ : 0 ≤ c) : c * a ≤ c * b := begin by_cases ba : b ≤ a, { simp [ba.antisymm h₁] }, by_cases c0 : c ≤ 0, { simp [c0.antisymm h₂] }, exact (mul_lt_mul_of_pos_left (h₁.lt_of_not_le ba) (h₂.lt_of_not_le c0)).le, end -- See Note [decidable namespace] protected lemma decidable.mul_le_mul_of_nonneg_right [@decidable_rel α (≤)] (h₁ : a ≤ b) (h₂ : 0 ≤ c) : a * c ≤ b * c := begin by_cases ba : b ≤ a, { simp [ba.antisymm h₁] }, by_cases c0 : c ≤ 0, { simp [c0.antisymm h₂] }, exact (mul_lt_mul_of_pos_right (h₁.lt_of_not_le ba) (h₂.lt_of_not_le c0)).le, end -- TODO: there are four variations, depending on which variables we assume to be nonneg -- See Note [decidable namespace] protected lemma decidable.mul_le_mul [@decidable_rel α (≤)] (hac : a ≤ c) (hbd : b ≤ d) (nn_b : 0 ≤ b) (nn_c : 0 ≤ c) : a * b ≤ c * d := calc a * b ≤ c * b : decidable.mul_le_mul_of_nonneg_right hac nn_b ... ≤ c * d : decidable.mul_le_mul_of_nonneg_left hbd nn_c -- See Note [decidable namespace] protected lemma decidable.mul_nonneg_le_one_le {α : Type*} [ordered_semiring α] [@decidable_rel α (≤)] {a b c : α} (h₁ : 0 ≤ c) (h₂ : a ≤ c) (h₃ : 0 ≤ b) (h₄ : b ≤ 1) : a * b ≤ c := by simpa only [mul_one] using decidable.mul_le_mul h₂ h₄ h₃ h₁ -- See Note [decidable namespace] protected lemma decidable.mul_nonneg [@decidable_rel α (≤)] (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a * b := have h : 0 * b ≤ a * b, from decidable.mul_le_mul_of_nonneg_right ha hb, by rwa [zero_mul] at h @[simp] theorem pow_nonneg (H : 0 ≤ a) : ∀ (n : ℕ), 0 ≤ a ^ n | 0 := by { rw pow_zero, exact zero_le_one} | (n+1) := by { rw pow_succ, exact mul_nonneg H (pow_nonneg _) } -- See Note [decidable namespace] protected lemma decidable.mul_nonpos_of_nonneg_of_nonpos [@decidable_rel α (≤)] (ha : 0 ≤ a) (hb : b ≤ 0) : a * b ≤ 0 := have h : a * b ≤ a * 0, from decidable.mul_le_mul_of_nonneg_left hb ha, by rwa mul_zero at h -- See Note [decidable namespace] protected lemma decidable.mul_nonpos_of_nonpos_of_nonneg [@decidable_rel α (≤)] (ha : a ≤ 0) (hb : 0 ≤ b) : a * b ≤ 0 := have h : a * b ≤ 0 * b, from decidable.mul_le_mul_of_nonneg_right ha hb, by rwa zero_mul at h -- See Note [decidable namespace] protected lemma decidable.mul_lt_mul [@decidable_rel α (≤)] (hac : a < c) (hbd : b ≤ d) (pos_b : 0 < b) (nn_c : 0 ≤ c) : a * b < c * d := calc a * b < c * b : mul_lt_mul_of_pos_right hac pos_b ... ≤ c * d : decidable.mul_le_mul_of_nonneg_left hbd nn_c lemma mul_lt_mul : a < c → b ≤ d → 0 < b → 0 ≤ c → a * b < c * d := by classical; exact decidable.mul_lt_mul -- See Note [decidable namespace] protected lemma decidable.mul_lt_mul' [@decidable_rel α (≤)] (h1 : a ≤ c) (h2 : b < d) (h3 : 0 ≤ b) (h4 : 0 < c) : a * b < c * d := calc a * b ≤ c * b : decidable.mul_le_mul_of_nonneg_right h1 h3 ... < c * d : mul_lt_mul_of_pos_left h2 h4 lemma mul_lt_mul' : a ≤ c → b < d → 0 ≤ b → 0 < c → a * b < c * d := by classical; exact decidable.mul_lt_mul' @[simp] theorem pow_pos (H : 0 < a) : ∀ (n : ℕ), 0 < a ^ n | 0 := by { nontriviality, rw pow_zero, exact zero_lt_one } | (n+1) := by { rw pow_succ, exact mul_pos H (pow_pos _) } -- See Note [decidable namespace] protected lemma decidable.mul_self_lt_mul_self [@decidable_rel α (≤)] (h1 : 0 ≤ a) (h2 : a < b) : a * a < b * b := decidable.mul_lt_mul' h2.le h2 h1 $ h1.trans_lt h2 lemma mul_self_lt_mul_self (h1 : 0 ≤ a) (h2 : a < b) : a * a < b * b := mul_lt_mul' h2.le h2 h1 $ h1.trans_lt h2 -- See Note [decidable namespace] protected lemma decidable.strict_mono_on_mul_self [@decidable_rel α (≤)] : strict_mono_on (λ x : α, x * x) (set.Ici 0) := λ x hx y hy hxy, decidable.mul_self_lt_mul_self hx hxy lemma strict_mono_on_mul_self : strict_mono_on (λ x : α, x * x) (set.Ici 0) := λ x hx y hy hxy, mul_self_lt_mul_self hx hxy -- See Note [decidable namespace] protected lemma decidable.mul_self_le_mul_self [@decidable_rel α (≤)] (h1 : 0 ≤ a) (h2 : a ≤ b) : a * a ≤ b * b := decidable.mul_le_mul h2 h2 h1 $ h1.trans h2 lemma mul_self_le_mul_self (h1 : 0 ≤ a) (h2 : a ≤ b) : a * a ≤ b * b := mul_le_mul h2 h2 h1 $ h1.trans h2 -- See Note [decidable namespace] protected lemma decidable.mul_lt_mul'' [@decidable_rel α (≤)] (h1 : a < c) (h2 : b < d) (h3 : 0 ≤ a) (h4 : 0 ≤ b) : a * b < c * d := h4.lt_or_eq_dec.elim (λ b0, decidable.mul_lt_mul h1 h2.le b0 $ h3.trans h1.le) (λ b0, by rw [← b0, mul_zero]; exact mul_pos (h3.trans_lt h1) (h4.trans_lt h2)) lemma mul_lt_mul'' : a < c → b < d → 0 ≤ a → 0 ≤ b → a * b < c * d := by classical; exact decidable.mul_lt_mul'' -- See Note [decidable namespace] protected lemma decidable.le_mul_of_one_le_right [@decidable_rel α (≤)] (hb : 0 ≤ b) (h : 1 ≤ a) : b ≤ b * a := suffices b * 1 ≤ b * a, by rwa mul_one at this, decidable.mul_le_mul_of_nonneg_left h hb -- See Note [decidable namespace] protected lemma decidable.le_mul_of_one_le_left [@decidable_rel α (≤)] (hb : 0 ≤ b) (h : 1 ≤ a) : b ≤ a * b := suffices 1 * b ≤ a * b, by rwa one_mul at this, decidable.mul_le_mul_of_nonneg_right h hb -- See Note [decidable namespace] protected lemma decidable.lt_mul_of_one_lt_right [@decidable_rel α (≤)] (hb : 0 < b) (h : 1 < a) : b < b * a := suffices b * 1 < b * a, by rwa mul_one at this, decidable.mul_lt_mul' le_rfl h zero_le_one hb -- See Note [decidable namespace] protected lemma decidable.lt_mul_of_one_lt_left [@decidable_rel α (≤)] (hb : 0 < b) (h : 1 < a) : b < a * b := suffices 1 * b < a * b, by rwa one_mul at this, decidable.mul_lt_mul h le_rfl hb (zero_le_one.trans h.le) lemma lt_two_mul_self (ha : 0 < a) : a < 2 * a := lt_mul_of_one_lt_left ha (@one_lt_two α _ (nontrivial_of_ne 0 a ha.ne)) lemma lt_mul_self (hn : 1 < a) : a < a * a := lt_mul_of_one_lt_left (hn.trans_le' zero_le_one) hn -- See Note [decidable namespace] protected lemma decidable.add_le_mul_two_add [@decidable_rel α (≤)] {a b : α} (a2 : 2 ≤ a) (b0 : 0 ≤ b) : a + (2 + b) ≤ a * (2 + b) := calc a + (2 + b) ≤ a + (a + a * b) : add_le_add_left (add_le_add a2 (decidable.le_mul_of_one_le_left b0 (one_le_two.trans a2))) a ... ≤ a * (2 + b) : by rw [mul_add, mul_two, add_assoc] lemma add_le_mul_two_add {a b : α} : 2 ≤ a → 0 ≤ b → a + (2 + b) ≤ a * (2 + b) := by classical; exact decidable.add_le_mul_two_add -- See Note [decidable namespace] protected lemma decidable.one_le_mul_of_one_le_of_one_le [@decidable_rel α (≤)] {a b : α} (a1 : 1 ≤ a) (b1 : 1 ≤ b) : (1 : α) ≤ a * b := (mul_one (1 : α)).symm.le.trans (decidable.mul_le_mul a1 b1 zero_le_one (zero_le_one.trans a1)) lemma one_le_mul_of_one_le_of_one_le {a b : α} : 1 ≤ a → 1 ≤ b → (1 : α) ≤ a * b := by classical; exact decidable.one_le_mul_of_one_le_of_one_le namespace decidable variables {β : Type*} [@decidable_rel α (≤)] [preorder β] {f g : β → α} lemma monotone_mul_left_of_nonneg (ha : 0 ≤ a) : monotone (λ x, a*x) := assume b c b_le_c, decidable.mul_le_mul_of_nonneg_left b_le_c ha lemma monotone_mul_right_of_nonneg (ha : 0 ≤ a) : monotone (λ x, x*a) := assume b c b_le_c, decidable.mul_le_mul_of_nonneg_right b_le_c ha lemma monotone_mul {β : Type*} [preorder β] {f g : β → α} (hf : monotone f) (hg : monotone g) (hf0 : ∀ x, 0 ≤ f x) (hg0 : ∀ x, 0 ≤ g x) : monotone (λ x, f x * g x) := λ x y h, decidable.mul_le_mul (hf h) (hg h) (hg0 x) (hf0 y) lemma strict_mono_mul_monotone (hf : strict_mono f) (hg : monotone g) (hf0 : ∀ x, 0 ≤ f x) (hg0 : ∀ x, 0 < g x) : strict_mono (λ x, f x * g x) := λ x y h, decidable.mul_lt_mul (hf h) (hg h.le) (hg0 x) (hf0 y) lemma monotone_mul_strict_mono (hf : monotone f) (hg : strict_mono g) (hf0 : ∀ x, 0 < f x) (hg0 : ∀ x, 0 ≤ g x) : strict_mono (λ x, f x * g x) := λ x y h, decidable.mul_lt_mul' (hf h.le) (hg h) (hg0 x) (hf0 y) lemma strict_mono_mul (hf : strict_mono f) (hg : strict_mono g) (hf0 : ∀ x, 0 ≤ f x) (hg0 : ∀ x, 0 ≤ g x) : strict_mono (λ x, f x * g x) := λ x y h, decidable.mul_lt_mul'' (hf h) (hg h) (hf0 x) (hg0 x) end decidable section mono open_locale classical variables {β : Type*} [preorder β] {f g : β → α} lemma monotone_mul_left_of_nonneg (ha : 0 ≤ a) : monotone (λ x, a*x) := decidable.monotone_mul_left_of_nonneg ha lemma monotone_mul_right_of_nonneg (ha : 0 ≤ a) : monotone (λ x, x*a) := decidable.monotone_mul_right_of_nonneg ha lemma monotone.mul_const (hf : monotone f) (ha : 0 ≤ a) : monotone (λ x, (f x) * a) := (monotone_mul_right_of_nonneg ha).comp hf lemma monotone.const_mul (hf : monotone f) (ha : 0 ≤ a) : monotone (λ x, a * (f x)) := (monotone_mul_left_of_nonneg ha).comp hf lemma monotone.mul (hf : monotone f) (hg : monotone g) (hf0 : ∀ x, 0 ≤ f x) (hg0 : ∀ x, 0 ≤ g x) : monotone (λ x, f x * g x) := decidable.monotone_mul hf hg hf0 hg0 lemma strict_mono_mul_left_of_pos (ha : 0 < a) : strict_mono (λ x, a * x) := assume b c b_lt_c, mul_lt_mul_of_pos_left b_lt_c ha lemma strict_mono_mul_right_of_pos (ha : 0 < a) : strict_mono (λ x, x * a) := assume b c b_lt_c, mul_lt_mul_of_pos_right b_lt_c ha lemma strict_mono.mul_const (hf : strict_mono f) (ha : 0 < a) : strict_mono (λ x, (f x) * a) := (strict_mono_mul_right_of_pos ha).comp hf lemma strict_mono.const_mul (hf : strict_mono f) (ha : 0 < a) : strict_mono (λ x, a * (f x)) := (strict_mono_mul_left_of_pos ha).comp hf lemma strict_mono.mul_monotone (hf : strict_mono f) (hg : monotone g) (hf0 : ∀ x, 0 ≤ f x) (hg0 : ∀ x, 0 < g x) : strict_mono (λ x, f x * g x) := decidable.strict_mono_mul_monotone hf hg hf0 hg0 lemma monotone.mul_strict_mono (hf : monotone f) (hg : strict_mono g) (hf0 : ∀ x, 0 < f x) (hg0 : ∀ x, 0 ≤ g x) : strict_mono (λ x, f x * g x) := decidable.monotone_mul_strict_mono hf hg hf0 hg0 lemma strict_mono.mul (hf : strict_mono f) (hg : strict_mono g) (hf0 : ∀ x, 0 ≤ f x) (hg0 : ∀ x, 0 ≤ g x) : strict_mono (λ x, f x * g x) := decidable.strict_mono_mul hf hg hf0 hg0 end mono /-- Pullback an `ordered_semiring` under an injective map. See note [reducible non-instances]. -/ @[reducible] def function.injective.ordered_semiring {β : Type*} [has_zero β] [has_one β] [has_add β] [has_mul β] [has_pow β ℕ] [has_smul ℕ β] [has_nat_cast β] (f : β → α) (hf : function.injective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) (nat_cast : ∀ n : ℕ, f n = n) : ordered_semiring β := { zero_le_one := show f 0 ≤ f 1, by simp only [zero, one, zero_le_one], mul_lt_mul_of_pos_left := λ a b c ab c0, show f (c * a) < f (c * b), begin rw [mul, mul], refine mul_lt_mul_of_pos_left ab _, rwa ← zero, end, mul_lt_mul_of_pos_right := λ a b c ab c0, show f (a * c) < f (b * c), begin rw [mul, mul], refine mul_lt_mul_of_pos_right ab _, rwa ← zero, end, ..hf.ordered_cancel_add_comm_monoid f zero add nsmul, ..hf.semiring f zero one add mul nsmul npow nat_cast } section variable [nontrivial α] lemma bit1_pos (h : 0 ≤ a) : 0 < bit1 a := lt_add_of_le_of_pos (add_nonneg h h) zero_lt_one lemma lt_add_one (a : α) : a < a + 1 := lt_add_of_le_of_pos le_rfl zero_lt_one lemma lt_one_add (a : α) : a < 1 + a := by { rw [add_comm], apply lt_add_one } end lemma bit1_pos' (h : 0 < a) : 0 < bit1 a := begin nontriviality, exact bit1_pos h.le, end -- See Note [decidable namespace] protected lemma decidable.one_lt_mul [@decidable_rel α (≤)] (ha : 1 ≤ a) (hb : 1 < b) : 1 < a * b := begin nontriviality, exact (one_mul (1 : α)) ▸ decidable.mul_lt_mul' ha hb zero_le_one (zero_lt_one.trans_le ha) end lemma one_lt_mul : 1 ≤ a → 1 < b → 1 < a * b := by classical; exact decidable.one_lt_mul -- See Note [decidable namespace] protected lemma decidable.mul_le_one [@decidable_rel α (≤)] (ha : a ≤ 1) (hb' : 0 ≤ b) (hb : b ≤ 1) : a * b ≤ 1 := one_mul (1 : α) ▸ decidable.mul_le_mul ha hb hb' zero_le_one lemma mul_le_one : a ≤ 1 → 0 ≤ b → b ≤ 1 → a * b ≤ 1 := by classical; exact decidable.mul_le_one -- See Note [decidable namespace] protected lemma decidable.one_lt_mul_of_le_of_lt [@decidable_rel α (≤)] (ha : 1 ≤ a) (hb : 1 < b) : 1 < a * b := begin nontriviality, calc 1 = 1 * 1 : by rw one_mul ... < a * b : decidable.mul_lt_mul' ha hb zero_le_one (zero_lt_one.trans_le ha) end lemma one_lt_mul_of_le_of_lt : 1 ≤ a → 1 < b → 1 < a * b := by classical; exact decidable.one_lt_mul_of_le_of_lt -- See Note [decidable namespace] protected lemma decidable.one_lt_mul_of_lt_of_le [@decidable_rel α (≤)] (ha : 1 < a) (hb : 1 ≤ b) : 1 < a * b := begin nontriviality, calc 1 = 1 * 1 : by rw one_mul ... < a * b : decidable.mul_lt_mul ha hb zero_lt_one $ zero_le_one.trans ha.le end lemma one_lt_mul_of_lt_of_le : 1 < a → 1 ≤ b → 1 < a * b := by classical; exact decidable.one_lt_mul_of_lt_of_le -- See Note [decidable namespace] protected lemma decidable.mul_le_of_le_one_right [@decidable_rel α (≤)] (ha : 0 ≤ a) (hb1 : b ≤ 1) : a * b ≤ a := calc a * b ≤ a * 1 : decidable.mul_le_mul_of_nonneg_left hb1 ha ... = a : mul_one a -- See Note [decidable namespace] protected lemma decidable.mul_le_of_le_one_left [@decidable_rel α (≤)] (hb : 0 ≤ b) (ha1 : a ≤ 1) : a * b ≤ b := calc a * b ≤ 1 * b : decidable.mul_le_mul ha1 le_rfl hb zero_le_one ... = b : one_mul b -- See Note [decidable namespace] protected lemma decidable.mul_lt_one_of_nonneg_of_lt_one_left [@decidable_rel α (≤)] (ha0 : 0 ≤ a) (ha : a < 1) (hb : b ≤ 1) : a * b < 1 := calc a * b ≤ a : decidable.mul_le_of_le_one_right ha0 hb ... < 1 : ha lemma mul_lt_one_of_nonneg_of_lt_one_left : 0 ≤ a → a < 1 → b ≤ 1 → a * b < 1 := by classical; exact decidable.mul_lt_one_of_nonneg_of_lt_one_left -- See Note [decidable namespace] protected lemma decidable.mul_lt_one_of_nonneg_of_lt_one_right [@decidable_rel α (≤)] (ha : a ≤ 1) (hb0 : 0 ≤ b) (hb : b < 1) : a * b < 1 := calc a * b ≤ b : decidable.mul_le_of_le_one_left hb0 ha ... < 1 : hb lemma mul_lt_one_of_nonneg_of_lt_one_right : a ≤ 1 → 0 ≤ b → b < 1 → a * b < 1 := by classical; exact decidable.mul_lt_one_of_nonneg_of_lt_one_right theorem nat.strict_mono_cast [nontrivial α] : strict_mono (coe : ℕ → α) := strict_mono_nat_of_lt_succ $ λ n, by rw [nat.cast_succ]; apply lt_add_one /-- Note this is not an instance as `char_zero` implies `nontrivial`, and this would risk forming a loop. -/ lemma ordered_semiring.to_char_zero [nontrivial α] : char_zero α := ⟨nat.strict_mono_cast.injective⟩ section has_exists_add_of_le variables [has_exists_add_of_le α] /-- Binary **rearrangement inequality**. -/ lemma mul_add_mul_le_mul_add_mul (hab : a ≤ b) (hcd : c ≤ d) : a * d + b * c ≤ a * c + b * d := begin obtain ⟨b, rfl⟩ := exists_add_of_le hab, obtain ⟨d, rfl⟩ := exists_add_of_le hcd, rw [mul_add, add_right_comm, mul_add, ←add_assoc], exact add_le_add_left (mul_le_mul_of_nonneg_right hab $ (le_add_iff_nonneg_right _).1 hcd) _, end /-- Binary **rearrangement inequality**. -/ lemma mul_add_mul_le_mul_add_mul' (hba : b ≤ a) (hdc : d ≤ c) : a • d + b • c ≤ a • c + b • d := by { rw [add_comm (a • d), add_comm (a • c)], exact mul_add_mul_le_mul_add_mul hba hdc } /-- Binary strict **rearrangement inequality**. -/ lemma mul_add_mul_lt_mul_add_mul (hab : a < b) (hcd : c < d) : a * d + b * c < a * c + b * d := begin obtain ⟨b, rfl⟩ := exists_add_of_le hab.le, obtain ⟨d, rfl⟩ := exists_add_of_le hcd.le, rw [mul_add, add_right_comm, mul_add, ←add_assoc], exact add_lt_add_left (mul_lt_mul_of_pos_right hab $ (lt_add_iff_pos_right _).1 hcd) _, end /-- Binary **rearrangement inequality**. -/ lemma mul_add_mul_lt_mul_add_mul' (hba : b < a) (hdc : d < c) : a • d + b • c < a • c + b • d := by { rw [add_comm (a • d), add_comm (a • c)], exact mul_add_mul_lt_mul_add_mul hba hdc } end has_exists_add_of_le end ordered_semiring section ordered_comm_semiring /-- An `ordered_comm_semiring α` is a commutative semiring `α` with a partial order such that addition is monotone and multiplication by a positive number is strictly monotone. -/ @[protect_proj] class ordered_comm_semiring (α : Type u) extends ordered_semiring α, comm_semiring α /-- Pullback an `ordered_comm_semiring` under an injective map. See note [reducible non-instances]. -/ @[reducible] def function.injective.ordered_comm_semiring [ordered_comm_semiring α] {β : Type*} [has_zero β] [has_one β] [has_add β] [has_mul β] [has_pow β ℕ] [has_smul ℕ β] [has_nat_cast β] (f : β → α) (hf : function.injective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) (nat_cast : ∀ n : ℕ, f n = n) : ordered_comm_semiring β := { ..hf.comm_semiring f zero one add mul nsmul npow nat_cast, ..hf.ordered_semiring f zero one add mul nsmul npow nat_cast } end ordered_comm_semiring /-- A `linear_ordered_semiring α` is a nontrivial semiring `α` with a linear order such that addition is monotone and multiplication by a positive number is strictly monotone. -/ -- It's not entirely clear we should assume `nontrivial` at this point; -- it would be reasonable to explore changing this, -- but be warned that the instances involving `domain` may cause -- typeclass search loops. @[protect_proj] class linear_ordered_semiring (α : Type u) extends ordered_semiring α, linear_ordered_add_comm_monoid α, nontrivial α section linear_ordered_semiring variables [linear_ordered_semiring α] {a b c d : α} local attribute [instance] linear_ordered_semiring.decidable_le -- `norm_num` expects the lemma stating `0 < 1` to have a single typeclass argument -- (see `norm_num.prove_pos_nat`). -- Rather than working out how to relax that assumption, -- we provide a synonym for `zero_lt_one` (which needs both `ordered_semiring α` and `nontrivial α`) -- with only a `linear_ordered_semiring` typeclass argument. lemma zero_lt_one' : 0 < (1 : α) := zero_lt_one lemma nonneg_and_nonneg_or_nonpos_and_nonpos_of_mul_nnonneg (hab : 0 ≤ a * b) : (0 ≤ a ∧ 0 ≤ b) ∨ (a ≤ 0 ∧ b ≤ 0) := begin refine decidable.or_iff_not_and_not.2 _, simp only [not_and, not_le], intros ab nab, apply not_lt_of_le hab _, rcases lt_trichotomy 0 a with (ha|rfl|ha), exacts [mul_neg_of_pos_of_neg ha (ab ha.le), ((ab le_rfl).asymm (nab le_rfl)).elim, mul_neg_of_neg_of_pos ha (nab ha.le)] end lemma nonneg_of_mul_nonneg_left (h : 0 ≤ a * b) (hb : 0 < b) : 0 ≤ a := le_of_not_gt $ λ ha, (mul_neg_of_neg_of_pos ha hb).not_le h lemma nonneg_of_mul_nonneg_right (h : 0 ≤ a * b) (ha : 0 < a) : 0 ≤ b := le_of_not_gt $ λ hb, (mul_neg_of_pos_of_neg ha hb).not_le h lemma neg_of_mul_neg_left (h : a * b < 0) (hb : 0 ≤ b) : a < 0 := lt_of_not_ge $ λ ha, (decidable.mul_nonneg ha hb).not_lt h lemma neg_of_mul_neg_right (h : a * b < 0) (ha : 0 ≤ a) : b < 0 := lt_of_not_ge $ λ hb, (decidable.mul_nonneg ha hb).not_lt h lemma nonpos_of_mul_nonpos_left (h : a * b ≤ 0) (hb : 0 < b) : a ≤ 0 := le_of_not_gt (assume ha : a > 0, (mul_pos ha hb).not_le h) lemma nonpos_of_mul_nonpos_right (h : a * b ≤ 0) (ha : 0 < a) : b ≤ 0 := le_of_not_gt (assume hb : b > 0, (mul_pos ha hb).not_le h) @[simp] lemma zero_le_mul_left (h : 0 < c) : 0 ≤ c * b ↔ 0 ≤ b := by { convert mul_le_mul_left h, simp } @[simp] lemma zero_le_mul_right (h : 0 < c) : 0 ≤ b * c ↔ 0 ≤ b := by { convert mul_le_mul_right h, simp } lemma add_le_mul_of_left_le_right (a2 : 2 ≤ a) (ab : a ≤ b) : a + b ≤ a * b := have 0 < b, from calc 0 < 2 : zero_lt_two ... ≤ a : a2 ... ≤ b : ab, calc a + b ≤ b + b : add_le_add_right ab b ... = 2 * b : (two_mul b).symm ... ≤ a * b : (mul_le_mul_right this).mpr a2 lemma add_le_mul_of_right_le_left (b2 : 2 ≤ b) (ba : b ≤ a) : a + b ≤ a * b := have 0 < a, from calc 0 < 2 : zero_lt_two ... ≤ b : b2 ... ≤ a : ba, calc a + b ≤ a + a : add_le_add_left ba a ... = a * 2 : (mul_two a).symm ... ≤ a * b : (mul_le_mul_left this).mpr b2 lemma add_le_mul (a2 : 2 ≤ a) (b2 : 2 ≤ b) : a + b ≤ a * b := if hab : a ≤ b then add_le_mul_of_left_le_right a2 hab else add_le_mul_of_right_le_left b2 (le_of_not_le hab) lemma add_le_mul' (a2 : 2 ≤ a) (b2 : 2 ≤ b) : a + b ≤ b * a := (le_of_eq (add_comm _ _)).trans (add_le_mul b2 a2) section @[simp] lemma bit0_le_bit0 : bit0 a ≤ bit0 b ↔ a ≤ b := by rw [bit0, bit0, ← two_mul, ← two_mul, mul_le_mul_left (zero_lt_two : 0 < (2:α))] @[simp] lemma bit0_lt_bit0 : bit0 a < bit0 b ↔ a < b := by rw [bit0, bit0, ← two_mul, ← two_mul, mul_lt_mul_left (zero_lt_two : 0 < (2:α))] @[simp] lemma bit1_le_bit1 : bit1 a ≤ bit1 b ↔ a ≤ b := (add_le_add_iff_right 1).trans bit0_le_bit0 @[simp] lemma bit1_lt_bit1 : bit1 a < bit1 b ↔ a < b := (add_lt_add_iff_right 1).trans bit0_lt_bit0 @[simp] lemma one_le_bit1 : (1 : α) ≤ bit1 a ↔ 0 ≤ a := by rw [bit1, le_add_iff_nonneg_left, bit0, ← two_mul, zero_le_mul_left (zero_lt_two : 0 < (2:α))] @[simp] lemma one_lt_bit1 : (1 : α) < bit1 a ↔ 0 < a := by rw [bit1, lt_add_iff_pos_left, bit0, ← two_mul, zero_lt_mul_left (zero_lt_two : 0 < (2:α))] @[simp] lemma zero_le_bit0 : (0 : α) ≤ bit0 a ↔ 0 ≤ a := by rw [bit0, ← two_mul, zero_le_mul_left (zero_lt_two : 0 < (2:α))] @[simp] lemma zero_lt_bit0 : (0 : α) < bit0 a ↔ 0 < a := by rw [bit0, ← two_mul, zero_lt_mul_left (zero_lt_two : 0 < (2:α))] end theorem mul_nonneg_iff_right_nonneg_of_pos (ha : 0 < a) : 0 ≤ a * b ↔ 0 ≤ b := by haveI := @linear_order.decidable_le α _; exact ⟨λ h, nonneg_of_mul_nonneg_right h ha, λ h, decidable.mul_nonneg ha.le h⟩ theorem mul_nonneg_iff_left_nonneg_of_pos (hb : 0 < b) : 0 ≤ a * b ↔ 0 ≤ a := by haveI := @linear_order.decidable_le α _; exact ⟨λ h, nonneg_of_mul_nonneg_left h hb, λ h, decidable.mul_nonneg h hb.le⟩ lemma nonpos_of_mul_nonneg_left (h : 0 ≤ a * b) (hb : b < 0) : a ≤ 0 := le_of_not_gt (λ ha, absurd h (mul_neg_of_pos_of_neg ha hb).not_le) lemma nonpos_of_mul_nonneg_right (h : 0 ≤ a * b) (ha : a < 0) : b ≤ 0 := le_of_not_gt (λ hb, absurd h (mul_neg_of_neg_of_pos ha hb).not_le) /-- Pullback a `linear_ordered_semiring` under an injective map. See note [reducible non-instances]. -/ @[reducible] def function.injective.linear_ordered_semiring {β : Type*} [has_zero β] [has_one β] [has_add β] [has_mul β] [has_pow β ℕ] [has_smul ℕ β] [has_nat_cast β] [has_sup β] [has_inf β] (f : β → α) (hf : function.injective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) (nat_cast : ∀ n : ℕ, f n = n) (hsup : ∀ x y, f (x ⊔ y) = max (f x) (f y)) (hinf : ∀ x y, f (x ⊓ y) = min (f x) (f y)) : linear_ordered_semiring β := { .. linear_order.lift f hf hsup hinf, .. pullback_nonzero f zero one, .. hf.ordered_semiring f zero one add mul nsmul npow nat_cast } @[simp] lemma units.inv_pos {u : αˣ} : (0 : α) < ↑u⁻¹ ↔ (0 : α) < u := have ∀ {u : αˣ}, (0 : α) < u → (0 : α) < ↑u⁻¹ := λ u h, (zero_lt_mul_left h).mp $ u.mul_inv.symm ▸ zero_lt_one, ⟨this, this⟩ @[simp] lemma units.inv_neg {u : αˣ} : ↑u⁻¹ < (0 : α) ↔ ↑u < (0 : α) := have ∀ {u : αˣ}, ↑u < (0 : α) → ↑u⁻¹ < (0 : α) := λ u h, neg_of_mul_pos_right (by exact (u.mul_inv.symm ▸ zero_lt_one)) h.le, ⟨this, this⟩ @[priority 100] -- see Note [lower instance priority] instance linear_ordered_semiring.to_char_zero : char_zero α := ordered_semiring.to_char_zero end linear_ordered_semiring section mono variables [linear_ordered_semiring α] {a : α} local attribute [instance] linear_ordered_semiring.decidable_lt lemma cmp_mul_pos_left (ha : 0 < a) (b c : α) : cmp (a * b) (a * c) = cmp b c := (strict_mono_mul_left_of_pos ha).cmp_map_eq b c lemma cmp_mul_pos_right (ha : 0 < a) (b c : α) : cmp (b * a) (c * a) = cmp b c := (strict_mono_mul_right_of_pos ha).cmp_map_eq b c end mono section linear_ordered_semiring variables [linear_ordered_semiring α] {a b c : α} lemma mul_max_of_nonneg (b c : α) (ha : 0 ≤ a) : a * max b c = max (a * b) (a * c) := (monotone_mul_left_of_nonneg ha).map_max lemma mul_min_of_nonneg (b c : α) (ha : 0 ≤ a) : a * min b c = min (a * b) (a * c) := (monotone_mul_left_of_nonneg ha).map_min lemma max_mul_of_nonneg (a b : α) (hc : 0 ≤ c) : max a b * c = max (a * c) (b * c) := (monotone_mul_right_of_nonneg hc).map_max lemma min_mul_of_nonneg (a b : α) (hc : 0 ≤ c) : min a b * c = min (a * c) (b * c) := (monotone_mul_right_of_nonneg hc).map_min end linear_ordered_semiring /-- A `linear_ordered_comm_semiring` is a nontrivial commutative semiring with a linear order such that addition is monotone and multiplication by a positive number is strictly monotone. -/ @[protect_proj, ancestor ordered_comm_semiring linear_ordered_semiring] class linear_ordered_comm_semiring (α : Type*) extends ordered_comm_semiring α, linear_ordered_semiring α @[priority 100] -- See note [lower instance priority] instance linear_ordered_comm_semiring.to_linear_ordered_cancel_add_comm_monoid [linear_ordered_comm_semiring α] : linear_ordered_cancel_add_comm_monoid α := { ..‹linear_ordered_comm_semiring α› } /-- Pullback a `linear_ordered_semiring` under an injective map. See note [reducible non-instances]. -/ @[reducible] def function.injective.linear_ordered_comm_semiring [linear_ordered_comm_semiring α] {β : Type*} [has_zero β] [has_one β] [has_add β] [has_mul β] [has_pow β ℕ] [has_smul ℕ β] [has_nat_cast β] [has_sup β] [has_inf β] (f : β → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) (nat_cast : ∀ n : ℕ, f n = n) (hsup : ∀ x y, f (x ⊔ y) = max (f x) (f y)) (hinf : ∀ x y, f (x ⊓ y) = min (f x) (f y)) : linear_ordered_comm_semiring β := { ..hf.linear_ordered_semiring f zero one add mul nsmul npow nat_cast hsup hinf, ..hf.ordered_comm_semiring f zero one add mul nsmul npow nat_cast } /-- An `ordered_ring α` is a ring `α` with a partial order such that addition is monotone and multiplication by a positive number is strictly monotone. -/ @[protect_proj] class ordered_ring (α : Type u) extends ring α, ordered_add_comm_group α := (zero_le_one : 0 ≤ (1 : α)) (mul_pos : ∀ a b : α, 0 < a → 0 < b → 0 < a * b) section ordered_ring variables [ordered_ring α] {a b c : α} -- See Note [decidable namespace] protected lemma decidable.ordered_ring.mul_nonneg [@decidable_rel α (≤)] {a b : α} (h₁ : 0 ≤ a) (h₂ : 0 ≤ b) : 0 ≤ a * b := begin by_cases ha : a ≤ 0, { simp [le_antisymm ha h₁] }, by_cases hb : b ≤ 0, { simp [le_antisymm hb h₂] }, exact (le_not_le_of_lt (ordered_ring.mul_pos a b (h₁.lt_of_not_le ha) (h₂.lt_of_not_le hb))).1, end lemma ordered_ring.mul_nonneg : 0 ≤ a → 0 ≤ b → 0 ≤ a * b := by classical; exact decidable.ordered_ring.mul_nonneg -- See Note [decidable namespace] protected lemma decidable.ordered_ring.mul_le_mul_of_nonneg_left [@decidable_rel α (≤)] (h₁ : a ≤ b) (h₂ : 0 ≤ c) : c * a ≤ c * b := begin rw [← sub_nonneg, ← mul_sub], exact decidable.ordered_ring.mul_nonneg h₂ (sub_nonneg.2 h₁), end lemma ordered_ring.mul_le_mul_of_nonneg_left : a ≤ b → 0 ≤ c → c * a ≤ c * b := by classical; exact decidable.ordered_ring.mul_le_mul_of_nonneg_left -- See Note [decidable namespace] protected lemma decidable.ordered_ring.mul_le_mul_of_nonneg_right [@decidable_rel α (≤)] (h₁ : a ≤ b) (h₂ : 0 ≤ c) : a * c ≤ b * c := begin rw [← sub_nonneg, ← sub_mul], exact decidable.ordered_ring.mul_nonneg (sub_nonneg.2 h₁) h₂, end lemma ordered_ring.mul_le_mul_of_nonneg_right : a ≤ b → 0 ≤ c → a * c ≤ b * c := by classical; exact decidable.ordered_ring.mul_le_mul_of_nonneg_right lemma ordered_ring.mul_lt_mul_of_pos_left (h₁ : a < b) (h₂ : 0 < c) : c * a < c * b := begin rw [← sub_pos, ← mul_sub], exact ordered_ring.mul_pos _ _ h₂ (sub_pos.2 h₁), end lemma ordered_ring.mul_lt_mul_of_pos_right (h₁ : a < b) (h₂ : 0 < c) : a * c < b * c := begin rw [← sub_pos, ← sub_mul], exact ordered_ring.mul_pos _ _ (sub_pos.2 h₁) h₂, end @[priority 100] -- see Note [lower instance priority] instance ordered_ring.to_ordered_semiring : ordered_semiring α := { le_of_add_le_add_left := @le_of_add_le_add_left α _ _ _, mul_lt_mul_of_pos_left := @ordered_ring.mul_lt_mul_of_pos_left α _, mul_lt_mul_of_pos_right := @ordered_ring.mul_lt_mul_of_pos_right α _, ..‹ordered_ring α›, ..ring.to_semiring } -- See Note [decidable namespace] protected lemma decidable.mul_le_mul_of_nonpos_left [@decidable_rel α (≤)] {a b c : α} (h : b ≤ a) (hc : c ≤ 0) : c * a ≤ c * b := have -c ≥ 0, from neg_nonneg_of_nonpos hc, have -c * b ≤ -c * a, from decidable.mul_le_mul_of_nonneg_left h this, have -(c * b) ≤ -(c * a), by rwa [neg_mul, neg_mul] at this, le_of_neg_le_neg this lemma mul_le_mul_of_nonpos_left {a b c : α} : b ≤ a → c ≤ 0 → c * a ≤ c * b := by classical; exact decidable.mul_le_mul_of_nonpos_left -- See Note [decidable namespace] protected lemma decidable.mul_le_mul_of_nonpos_right [@decidable_rel α (≤)] {a b c : α} (h : b ≤ a) (hc : c ≤ 0) : a * c ≤ b * c := have -c ≥ 0, from neg_nonneg_of_nonpos hc, have b * -c ≤ a * -c, from decidable.mul_le_mul_of_nonneg_right h this, have -(b * c) ≤ -(a * c), by rwa [mul_neg, mul_neg] at this, le_of_neg_le_neg this lemma mul_le_mul_of_nonpos_right {a b c : α} : b ≤ a → c ≤ 0 → a * c ≤ b * c := by classical; exact decidable.mul_le_mul_of_nonpos_right -- See Note [decidable namespace] protected lemma decidable.mul_nonneg_of_nonpos_of_nonpos [@decidable_rel α (≤)] {a b : α} (ha : a ≤ 0) (hb : b ≤ 0) : 0 ≤ a * b := have 0 * b ≤ a * b, from decidable.mul_le_mul_of_nonpos_right ha hb, by rwa zero_mul at this lemma mul_nonneg_of_nonpos_of_nonpos {a b : α} : a ≤ 0 → b ≤ 0 → 0 ≤ a * b := by classical; exact decidable.mul_nonneg_of_nonpos_of_nonpos lemma mul_lt_mul_of_neg_left {a b c : α} (h : b < a) (hc : c < 0) : c * a < c * b := have -c > 0, from neg_pos_of_neg hc, have -c * b < -c * a, from mul_lt_mul_of_pos_left h this, have -(c * b) < -(c * a), by rwa [neg_mul, neg_mul] at this, lt_of_neg_lt_neg this lemma mul_lt_mul_of_neg_right {a b c : α} (h : b < a) (hc : c < 0) : a * c < b * c := have -c > 0, from neg_pos_of_neg hc, have b * -c < a * -c, from mul_lt_mul_of_pos_right h this, have -(b * c) < -(a * c), by rwa [mul_neg, mul_neg] at this, lt_of_neg_lt_neg this lemma mul_pos_of_neg_of_neg {a b : α} (ha : a < 0) (hb : b < 0) : 0 < a * b := have 0 * b < a * b, from mul_lt_mul_of_neg_right ha hb, by rwa zero_mul at this lemma decidable.antitone_mul_left [@decidable_rel α (≤)] {a : α} (ha : a ≤ 0) : antitone ((*) a) := λ b c b_le_c, decidable.mul_le_mul_of_nonpos_left b_le_c ha lemma antitone_mul_left {a : α} (ha : a ≤ 0) : antitone ((*) a) := λ b c b_le_c, mul_le_mul_of_nonpos_left b_le_c ha lemma decidable.antitone_mul_right [@decidable_rel α (≤)] {a : α} (ha : a ≤ 0) : antitone (λ x, x * a) := λ b c b_le_c, decidable.mul_le_mul_of_nonpos_right b_le_c ha lemma antitone_mul_right {a : α} (ha : a ≤ 0) : antitone (λ x, x * a) := λ b c b_le_c, mul_le_mul_of_nonpos_right b_le_c ha lemma strict_anti_mul_left {a : α} (ha : a < 0) : strict_anti ((*) a) := λ b c b_lt_c, mul_lt_mul_of_neg_left b_lt_c ha lemma strict_anti_mul_right {a : α} (ha : a < 0) : strict_anti (λ x, x * a) := λ b c b_lt_c, mul_lt_mul_of_neg_right b_lt_c ha /-- Pullback an `ordered_ring` under an injective map. See note [reducible non-instances]. -/ @[reducible] def function.injective.ordered_ring {β : Type*} [has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β] [has_smul ℕ β] [has_smul ℤ β] [has_pow β ℕ] [has_nat_cast β] [has_int_cast β] (f : β → α) (hf : function.injective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (neg : ∀ x, f (- x) = - f x) (sub : ∀ x y, f (x - y) = f x - f y) (nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (zsmul : ∀ x (n : ℤ), f (n • x) = n • f x) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) (nat_cast : ∀ n : ℕ, f n = n) (int_cast : ∀ n : ℤ, f n = n) : ordered_ring β := { mul_pos := λ a b a0 b0, show f 0 < f (a * b), by { rw [zero, mul], apply mul_pos; rwa ← zero }, ..hf.ordered_semiring f zero one add mul nsmul npow nat_cast, ..hf.ring f zero one add mul neg sub nsmul zsmul npow nat_cast int_cast } lemma le_iff_exists_nonneg_add (a b : α) : a ≤ b ↔ ∃ c ≥ 0, b = a + c := ⟨λ h, ⟨b - a, sub_nonneg.mpr h, by simp⟩, λ ⟨c, hc, h⟩, by { rw [h, le_add_iff_nonneg_right], exact hc }⟩ end ordered_ring section ordered_comm_ring /-- An `ordered_comm_ring α` is a commutative ring `α` with a partial order such that addition is monotone and multiplication by a positive number is strictly monotone. -/ @[protect_proj] class ordered_comm_ring (α : Type u) extends ordered_ring α, comm_ring α @[priority 100] -- See note [lower instance priority] instance ordered_comm_ring.to_ordered_comm_semiring {α : Type u} [ordered_comm_ring α] : ordered_comm_semiring α := { .. (by apply_instance : ordered_semiring α), .. ‹ordered_comm_ring α› } /-- Pullback an `ordered_comm_ring` under an injective map. See note [reducible non-instances]. -/ @[reducible] def function.injective.ordered_comm_ring [ordered_comm_ring α] {β : Type*} [has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β] [has_pow β ℕ] [has_smul ℕ β] [has_smul ℤ β] [has_nat_cast β] [has_int_cast β] (f : β → α) (hf : function.injective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (neg : ∀ x, f (- x) = - f x) (sub : ∀ x y, f (x - y) = f x - f y) (nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (zsmul : ∀ x (n : ℤ), f (n • x) = n • f x) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) (nat_cast : ∀ n : ℕ, f n = n) (int_cast : ∀ n : ℤ, f n = n) : ordered_comm_ring β := { ..hf.ordered_ring f zero one add mul neg sub nsmul zsmul npow nat_cast int_cast, ..hf.comm_ring f zero one add mul neg sub nsmul zsmul npow nat_cast int_cast } end ordered_comm_ring /-- A `linear_ordered_ring α` is a ring `α` with a linear order such that addition is monotone and multiplication by a positive number is strictly monotone. -/ @[protect_proj] class linear_ordered_ring (α : Type u) extends ordered_ring α, linear_order α, nontrivial α @[priority 100] -- see Note [lower instance priority] instance linear_ordered_ring.to_linear_ordered_add_comm_group [s : linear_ordered_ring α] : linear_ordered_add_comm_group α := { .. s } section linear_ordered_semiring variables [linear_ordered_semiring α] {a b c : α} local attribute [instance] linear_ordered_semiring.decidable_le lemma le_of_mul_le_of_one_le {a b c : α} (h : a * c ≤ b) (hb : 0 ≤ b) (hc : 1 ≤ c) : a ≤ b := have h' : a * c ≤ b * c, from calc a * c ≤ b : h ... = b * 1 : by rewrite mul_one ... ≤ b * c : decidable.mul_le_mul_of_nonneg_left hc hb, le_of_mul_le_mul_right h' (zero_lt_one.trans_le hc) lemma nonneg_le_nonneg_of_sq_le_sq {a b : α} (hb : 0 ≤ b) (h : a * a ≤ b * b) : a ≤ b := le_of_not_gt (λhab, (decidable.mul_self_lt_mul_self hb hab).not_le h) lemma mul_self_le_mul_self_iff {a b : α} (h1 : 0 ≤ a) (h2 : 0 ≤ b) : a ≤ b ↔ a * a ≤ b * b := ⟨decidable.mul_self_le_mul_self h1, nonneg_le_nonneg_of_sq_le_sq h2⟩ lemma mul_self_lt_mul_self_iff {a b : α} (h1 : 0 ≤ a) (h2 : 0 ≤ b) : a < b ↔ a * a < b * b := ((@decidable.strict_mono_on_mul_self α _ _).lt_iff_lt h1 h2).symm lemma mul_self_inj {a b : α} (h1 : 0 ≤ a) (h2 : 0 ≤ b) : a * a = b * b ↔ a = b := (@decidable.strict_mono_on_mul_self α _ _).inj_on.eq_iff h1 h2 end linear_ordered_semiring section linear_ordered_ring variables [linear_ordered_ring α] {a b c : α} local attribute [instance] linear_ordered_ring.decidable_le linear_ordered_ring.decidable_lt @[priority 100] -- see Note [lower instance priority] instance linear_ordered_ring.to_linear_ordered_semiring : linear_ordered_semiring α := { ..‹linear_ordered_ring α›, ..ordered_ring.to_ordered_semiring } @[priority 100] -- see Note [lower instance priority] instance linear_ordered_ring.is_domain : is_domain α := { eq_zero_or_eq_zero_of_mul_eq_zero := begin intros a b hab, refine decidable.or_iff_not_and_not.2 (λ h, _), revert hab, cases lt_or_gt_of_ne h.1 with ha ha; cases lt_or_gt_of_ne h.2 with hb hb, exacts [(mul_pos_of_neg_of_neg ha hb).ne.symm, (mul_neg_of_neg_of_pos ha hb).ne, (mul_neg_of_pos_of_neg ha hb).ne, (mul_pos ha hb).ne.symm] end, .. ‹linear_ordered_ring α› } @[simp] lemma abs_one : |(1 : α)| = 1 := abs_of_pos zero_lt_one @[simp] lemma abs_two : |(2 : α)| = 2 := abs_of_pos zero_lt_two lemma abs_mul (a b : α) : |a * b| = |a| * |b| := begin haveI := @linear_order.decidable_le α _, rw [abs_eq (decidable.mul_nonneg (abs_nonneg a) (abs_nonneg b))], cases le_total a 0 with ha ha; cases le_total b 0 with hb hb; simp only [abs_of_nonpos, abs_of_nonneg, true_or, or_true, eq_self_iff_true, neg_mul, mul_neg, neg_neg, *] end /-- `abs` as a `monoid_with_zero_hom`. -/ def abs_hom : α →*₀ α := ⟨abs, abs_zero, abs_one, abs_mul⟩ @[simp] lemma abs_mul_abs_self (a : α) : |a| * |a| = a * a := abs_by_cases (λ x, x * x = a * a) rfl (neg_mul_neg a a) @[simp] lemma abs_mul_self (a : α) : |a * a| = a * a := by rw [abs_mul, abs_mul_abs_self] lemma mul_pos_iff : 0 < a * b ↔ 0 < a ∧ 0 < b ∨ a < 0 ∧ b < 0 := ⟨pos_and_pos_or_neg_and_neg_of_mul_pos, λ h, h.elim (and_imp.2 mul_pos) (and_imp.2 mul_pos_of_neg_of_neg)⟩ lemma mul_neg_iff : a * b < 0 ↔ 0 < a ∧ b < 0 ∨ a < 0 ∧ 0 < b := by rw [← neg_pos, neg_mul_eq_mul_neg, mul_pos_iff, neg_pos, neg_lt_zero] lemma mul_nonneg_iff : 0 ≤ a * b ↔ 0 ≤ a ∧ 0 ≤ b ∨ a ≤ 0 ∧ b ≤ 0 := by haveI := @linear_order.decidable_le α _; exact ⟨nonneg_and_nonneg_or_nonpos_and_nonpos_of_mul_nnonneg, λ h, h.elim (and_imp.2 decidable.mul_nonneg) (and_imp.2 decidable.mul_nonneg_of_nonpos_of_nonpos)⟩ /-- Out of three elements of a `linear_ordered_ring`, two must have the same sign. -/ lemma mul_nonneg_of_three (a b c : α) : 0 ≤ a * b ∨ 0 ≤ b * c ∨ 0 ≤ c * a := by iterate 3 { rw mul_nonneg_iff }; have := le_total 0 a; have := le_total 0 b; have := le_total 0 c; itauto lemma mul_nonpos_iff : a * b ≤ 0 ↔ 0 ≤ a ∧ b ≤ 0 ∨ a ≤ 0 ∧ 0 ≤ b := by rw [← neg_nonneg, neg_mul_eq_mul_neg, mul_nonneg_iff, neg_nonneg, neg_nonpos] lemma mul_self_nonneg (a : α) : 0 ≤ a * a := abs_mul_self a ▸ abs_nonneg _ @[simp] lemma neg_le_self_iff : -a ≤ a ↔ 0 ≤ a := by simp [neg_le_iff_add_nonneg, ← two_mul, mul_nonneg_iff, zero_le_one, (@zero_lt_two α _ _).not_le] @[simp] lemma neg_lt_self_iff : -a < a ↔ 0 < a := by simp [neg_lt_iff_pos_add, ← two_mul, mul_pos_iff, zero_lt_one, (@zero_lt_two α _ _).not_lt] @[simp] lemma le_neg_self_iff : a ≤ -a ↔ a ≤ 0 := calc a ≤ -a ↔ -(-a) ≤ -a : by rw neg_neg ... ↔ 0 ≤ -a : neg_le_self_iff ... ↔ a ≤ 0 : neg_nonneg @[simp] lemma lt_neg_self_iff : a < -a ↔ a < 0 := calc a < -a ↔ -(-a) < -a : by rw neg_neg ... ↔ 0 < -a : neg_lt_self_iff ... ↔ a < 0 : neg_pos @[simp] lemma abs_eq_self : |a| = a ↔ 0 ≤ a := by simp [abs_eq_max_neg] @[simp] lemma abs_eq_neg_self : |a| = -a ↔ a ≤ 0 := by simp [abs_eq_max_neg] /-- For an element `a` of a linear ordered ring, either `abs a = a` and `0 ≤ a`, or `abs a = -a` and `a < 0`. Use cases on this lemma to automate linarith in inequalities -/ lemma abs_cases (a : α) : (|a| = a ∧ 0 ≤ a) ∨ (|a| = -a ∧ a < 0) := begin by_cases 0 ≤ a, { left, exact ⟨abs_eq_self.mpr h, h⟩ }, { right, push_neg at h, exact ⟨abs_eq_neg_self.mpr (le_of_lt h), h⟩ } end @[simp] lemma max_zero_add_max_neg_zero_eq_abs_self (a : α) : max a 0 + max (-a) 0 = |a| := begin symmetry, rcases le_total 0 a with ha|ha; simp [ha], end lemma gt_of_mul_lt_mul_neg_left (h : c * a < c * b) (hc : c ≤ 0) : b < a := have nhc : 0 ≤ -c, from neg_nonneg_of_nonpos hc, have h2 : -(c * b) < -(c * a), from neg_lt_neg h, have h3 : (-c) * b < (-c) * a, from calc (-c) * b = - (c * b) : by rewrite neg_mul_eq_neg_mul ... < -(c * a) : h2 ... = (-c) * a : by rewrite neg_mul_eq_neg_mul, lt_of_mul_lt_mul_left h3 nhc lemma neg_one_lt_zero : -1 < (0:α) := neg_lt_zero.2 zero_lt_one @[simp] lemma mul_le_mul_left_of_neg {a b c : α} (h : c < 0) : c * a ≤ c * b ↔ b ≤ a := (strict_anti_mul_left h).le_iff_le @[simp] lemma mul_le_mul_right_of_neg {a b c : α} (h : c < 0) : a * c ≤ b * c ↔ b ≤ a := (strict_anti_mul_right h).le_iff_le @[simp] lemma mul_lt_mul_left_of_neg {a b c : α} (h : c < 0) : c * a < c * b ↔ b < a := (strict_anti_mul_left h).lt_iff_lt @[simp] lemma mul_lt_mul_right_of_neg {a b c : α} (h : c < 0) : a * c < b * c ↔ b < a := (strict_anti_mul_right h).lt_iff_lt lemma cmp_mul_neg_left {a : α} (ha : a < 0) (b c : α) : cmp (a * b) (a * c) = cmp c b := (strict_anti_mul_left ha).cmp_map_eq b c lemma cmp_mul_neg_right {a : α} (ha : a < 0) (b c : α) : cmp (b * a) (c * a) = cmp c b := (strict_anti_mul_right ha).cmp_map_eq b c lemma sub_one_lt (a : α) : a - 1 < a := sub_lt_iff_lt_add.2 (lt_add_one a) @[simp] lemma mul_self_pos {a : α} : 0 < a * a ↔ a ≠ 0 := begin split, { rintro h rfl, rw mul_zero at h, exact h.false }, { intro h, cases h.lt_or_lt with h h, exacts [mul_pos_of_neg_of_neg h h, mul_pos h h] } end lemma mul_self_le_mul_self_of_le_of_neg_le {x y : α} (h₁ : x ≤ y) (h₂ : -x ≤ y) : x * x ≤ y * y := begin haveI := @linear_order.decidable_le α _, rw [← abs_mul_abs_self x], exact decidable.mul_self_le_mul_self (abs_nonneg x) (abs_le.2 ⟨neg_le.2 h₂, h₁⟩) end lemma nonneg_of_mul_nonpos_left {a b : α} (h : a * b ≤ 0) (hb : b < 0) : 0 ≤ a := le_of_not_gt (λ ha, absurd h (mul_pos_of_neg_of_neg ha hb).not_le) lemma nonneg_of_mul_nonpos_right {a b : α} (h : a * b ≤ 0) (ha : a < 0) : 0 ≤ b := le_of_not_gt (λ hb, absurd h (mul_pos_of_neg_of_neg ha hb).not_le) lemma pos_of_mul_neg_left {a b : α} (h : a * b < 0) (hb : b ≤ 0) : 0 < a := by haveI := @linear_order.decidable_le α _; exact lt_of_not_ge (λ ha, absurd h (decidable.mul_nonneg_of_nonpos_of_nonpos ha hb).not_lt) lemma pos_of_mul_neg_right {a b : α} (h : a * b < 0) (ha : a ≤ 0) : 0 < b := by haveI := @linear_order.decidable_le α _; exact lt_of_not_ge (λ hb, absurd h (decidable.mul_nonneg_of_nonpos_of_nonpos ha hb).not_lt) lemma neg_iff_pos_of_mul_neg (hab : a * b < 0) : a < 0 ↔ 0 < b := ⟨pos_of_mul_neg_right hab ∘ le_of_lt, neg_of_mul_neg_left hab ∘ le_of_lt⟩ lemma pos_iff_neg_of_mul_neg (hab : a * b < 0) : 0 < a ↔ b < 0 := ⟨neg_of_mul_neg_right hab ∘ le_of_lt, pos_of_mul_neg_left hab ∘ le_of_lt⟩ /-- The sum of two squares is zero iff both elements are zero. -/ lemma mul_self_add_mul_self_eq_zero {x y : α} : x * x + y * y = 0 ↔ x = 0 ∧ y = 0 := by rw [add_eq_zero_iff', mul_self_eq_zero, mul_self_eq_zero]; apply mul_self_nonneg lemma eq_zero_of_mul_self_add_mul_self_eq_zero (h : a * a + b * b = 0) : a = 0 := (mul_self_add_mul_self_eq_zero.mp h).left lemma abs_eq_iff_mul_self_eq : |a| = |b| ↔ a * a = b * b := begin rw [← abs_mul_abs_self, ← abs_mul_abs_self b], exact (mul_self_inj (abs_nonneg a) (abs_nonneg b)).symm, end lemma abs_lt_iff_mul_self_lt : |a| < |b| ↔ a * a < b * b := begin rw [← abs_mul_abs_self, ← abs_mul_abs_self b], exact mul_self_lt_mul_self_iff (abs_nonneg a) (abs_nonneg b) end lemma abs_le_iff_mul_self_le : |a| ≤ |b| ↔ a * a ≤ b * b := begin rw [← abs_mul_abs_self, ← abs_mul_abs_self b], exact mul_self_le_mul_self_iff (abs_nonneg a) (abs_nonneg b) end lemma abs_le_one_iff_mul_self_le_one : |a| ≤ 1 ↔ a * a ≤ 1 := by simpa only [abs_one, one_mul] using @abs_le_iff_mul_self_le α _ a 1 /-- Pullback a `linear_ordered_ring` under an injective map. See note [reducible non-instances]. -/ @[reducible] def function.injective.linear_ordered_ring {β : Type*} [has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β] [has_smul ℕ β] [has_smul ℤ β] [has_pow β ℕ] [has_nat_cast β] [has_int_cast β] [has_sup β] [has_inf β] (f : β → α) (hf : function.injective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) (nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (zsmul : ∀ x (n : ℤ), f (n • x) = n • f x) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) (nat_cast : ∀ n : ℕ, f n = n) (int_cast : ∀ n : ℤ, f n = n) (hsup : ∀ x y, f (x ⊔ y) = max (f x) (f y)) (hinf : ∀ x y, f (x ⊓ y) = min (f x) (f y)) : linear_ordered_ring β := { .. linear_order.lift f hf hsup hinf, .. pullback_nonzero f zero one, .. hf.ordered_ring f zero one add mul neg sub nsmul zsmul npow nat_cast int_cast } end linear_ordered_ring /-- A `linear_ordered_comm_ring α` is a commutative ring `α` with a linear order such that addition is monotone and multiplication by a positive number is strictly monotone. -/ @[protect_proj] class linear_ordered_comm_ring (α : Type u) extends linear_ordered_ring α, comm_monoid α @[priority 100] -- see Note [lower instance priority] instance linear_ordered_comm_ring.to_ordered_comm_ring [d : linear_ordered_comm_ring α] : ordered_comm_ring α := { ..d } @[priority 100] -- see Note [lower instance priority] instance linear_ordered_comm_ring.to_linear_ordered_comm_semiring [d : linear_ordered_comm_ring α] : linear_ordered_comm_semiring α := { .. d, ..linear_ordered_ring.to_linear_ordered_semiring } section linear_ordered_comm_ring variables [linear_ordered_comm_ring α] {a b c d : α} lemma max_mul_mul_le_max_mul_max (b c : α) (ha : 0 ≤ a) (hd: 0 ≤ d) : max (a * b) (d * c) ≤ max a c * max d b := by haveI := @linear_order.decidable_le α _; exact have ba : b * a ≤ max d b * max c a, from decidable.mul_le_mul (le_max_right d b) (le_max_right c a) ha (le_trans hd (le_max_left d b)), have cd : c * d ≤ max a c * max b d, from decidable.mul_le_mul (le_max_right a c) (le_max_right b d) hd (le_trans ha (le_max_left a c)), max_le (by simpa [mul_comm, max_comm] using ba) (by simpa [mul_comm, max_comm] using cd) lemma abs_sub_sq (a b : α) : |a - b| * |a - b| = a * a + b * b - (1 + 1) * a * b := begin rw abs_mul_abs_self, simp only [mul_add, add_comm, add_left_comm, mul_comm, sub_eq_add_neg, mul_one, mul_neg, neg_add_rev, neg_neg], end end linear_ordered_comm_ring section variables [ring α] [linear_order α] {a b : α} @[simp] lemma abs_dvd (a b : α) : |a| ∣ b ↔ a ∣ b := by { cases abs_choice a with h h; simp only [h, neg_dvd] } lemma abs_dvd_self (a : α) : |a| ∣ a := (abs_dvd a a).mpr (dvd_refl a) @[simp] lemma dvd_abs (a b : α) : a ∣ |b| ↔ a ∣ b := by { cases abs_choice b with h h; simp only [h, dvd_neg] } lemma self_dvd_abs (a : α) : a ∣ |a| := (dvd_abs a a).mpr (dvd_refl a) lemma abs_dvd_abs (a b : α) : |a| ∣ |b| ↔ a ∣ b := (abs_dvd _ _).trans (dvd_abs _ _) end section linear_ordered_comm_ring variables [linear_ordered_comm_ring α] /-- Pullback a `linear_ordered_comm_ring` under an injective map. See note [reducible non-instances]. -/ @[reducible] def function.injective.linear_ordered_comm_ring {β : Type*} [has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β] [has_pow β ℕ] [has_smul ℕ β] [has_smul ℤ β] [has_nat_cast β] [has_int_cast β] [has_sup β] [has_inf β] (f : β → α) (hf : function.injective f) (zero : f 0 = 0) (one : f 1 = 1) (add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) (neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) (nsmul : ∀ x (n : ℕ), f (n • x) = n • f x) (zsmul : ∀ x (n : ℤ), f (n • x) = n • f x) (npow : ∀ x (n : ℕ), f (x ^ n) = f x ^ n) (nat_cast : ∀ n : ℕ, f n = n) (int_cast : ∀ n : ℤ, f n = n) (hsup : ∀ x y, f (x ⊔ y) = max (f x) (f y)) (hinf : ∀ x y, f (x ⊓ y) = min (f x) (f y)) : linear_ordered_comm_ring β := { .. linear_order.lift f hf hsup hinf, .. pullback_nonzero f zero one, .. hf.ordered_comm_ring f zero one add mul neg sub nsmul zsmul npow nat_cast int_cast } end linear_ordered_comm_ring namespace ring /-- A positive cone in a ring consists of a positive cone in underlying `add_comm_group`, which contains `1` and such that the positive elements are closed under multiplication. -/ @[nolint has_nonempty_instance] structure positive_cone (α : Type*) [ring α] extends add_comm_group.positive_cone α := (one_nonneg : nonneg 1) (mul_pos : ∀ (a b), pos a → pos b → pos (a * b)) /-- Forget that a positive cone in a ring respects the multiplicative structure. -/ add_decl_doc positive_cone.to_positive_cone /-- A positive cone in a ring induces a linear order if `1` is a positive element. -/ @[nolint has_nonempty_instance] structure total_positive_cone (α : Type*) [ring α] extends positive_cone α, add_comm_group.total_positive_cone α := (one_pos : pos 1) /-- Forget that a `total_positive_cone` in a ring is total. -/ add_decl_doc total_positive_cone.to_positive_cone /-- Forget that a `total_positive_cone` in a ring respects the multiplicative structure. -/ add_decl_doc total_positive_cone.to_total_positive_cone end ring namespace ordered_ring open ring /-- Construct an `ordered_ring` by designating a positive cone in an existing `ring`. -/ def mk_of_positive_cone {α : Type*} [ring α] (C : positive_cone α) : ordered_ring α := { zero_le_one := by { change C.nonneg (1 - 0), convert C.one_nonneg, simp, }, mul_pos := λ x y xp yp, begin change C.pos (x*y - 0), convert C.mul_pos x y (by { convert xp, simp, }) (by { convert yp, simp, }), simp, end, ..‹ring α›, ..ordered_add_comm_group.mk_of_positive_cone C.to_positive_cone } end ordered_ring namespace linear_ordered_ring open ring /-- Construct a `linear_ordered_ring` by designating a positive cone in an existing `ring`. -/ def mk_of_positive_cone {α : Type*} [ring α] (C : total_positive_cone α) : linear_ordered_ring α := { exists_pair_ne := ⟨0, 1, begin intro h, have one_pos := C.one_pos, rw [←h, C.pos_iff] at one_pos, simpa using one_pos, end⟩, ..ordered_ring.mk_of_positive_cone C.to_positive_cone, ..linear_ordered_add_comm_group.mk_of_positive_cone C.to_total_positive_cone, } end linear_ordered_ring /-- A canonically ordered commutative semiring is an ordered, commutative semiring in which `a ≤ b` iff there exists `c` with `b = a + c`. This is satisfied by the natural numbers, for example, but not the integers or other ordered groups. -/ @[protect_proj] class canonically_ordered_comm_semiring (α : Type*) extends canonically_ordered_add_monoid α, comm_semiring α := (eq_zero_or_eq_zero_of_mul_eq_zero : ∀ a b : α, a * b = 0 → a = 0 ∨ b = 0) namespace canonically_ordered_comm_semiring variables [canonically_ordered_comm_semiring α] {a b : α} @[priority 100] -- see Note [lower instance priority] instance to_no_zero_divisors : no_zero_divisors α := ⟨canonically_ordered_comm_semiring.eq_zero_or_eq_zero_of_mul_eq_zero⟩ @[priority 100] -- see Note [lower instance priority] instance to_covariant_mul_le : covariant_class α α (*) (≤) := begin refine ⟨λ a b c h, _⟩, rcases exists_add_of_le h with ⟨c, rfl⟩, rw mul_add, apply self_le_add_right end @[priority 200] -- see Note [lower instance priority] instance canonically_ordered_comm_semiring.to_pos_mul_mono : pos_mul_mono α := ⟨λ x a b h, by { obtain ⟨d, rfl⟩ := exists_add_of_le h, simp_rw [left_distrib, le_self_add], }⟩ @[priority 200] -- see Note [lower instance priority] instance canonically_ordered_comm_semiring.to_mul_pos_mono : mul_pos_mono α := ⟨λ x a b h, by { obtain ⟨d, rfl⟩ := exists_add_of_le h, simp_rw [right_distrib, le_self_add], }⟩ /-- A version of `zero_lt_one : 0 < 1` for a `canonically_ordered_comm_semiring`. -/ lemma zero_lt_one [nontrivial α] : (0:α) < 1 := (zero_le 1).lt_of_ne zero_ne_one @[simp] lemma mul_pos : 0 < a * b ↔ (0 < a) ∧ (0 < b) := by simp only [pos_iff_ne_zero, ne.def, mul_eq_zero, not_or_distrib] end canonically_ordered_comm_semiring section sub variables [canonically_ordered_comm_semiring α] {a b c : α} variables [has_sub α] [has_ordered_sub α] variables [is_total α (≤)] namespace add_le_cancellable protected lemma mul_tsub (h : add_le_cancellable (a * c)) : a * (b - c) = a * b - a * c := begin cases total_of (≤) b c with hbc hcb, { rw [tsub_eq_zero_iff_le.2 hbc, mul_zero, tsub_eq_zero_iff_le.2 (mul_le_mul_left' hbc a)] }, { apply h.eq_tsub_of_add_eq, rw [← mul_add, tsub_add_cancel_of_le hcb] } end protected lemma tsub_mul (h : add_le_cancellable (b * c)) : (a - b) * c = a * c - b * c := by { simp only [mul_comm _ c] at *, exact h.mul_tsub } end add_le_cancellable variables [contravariant_class α α (+) (≤)] lemma mul_tsub (a b c : α) : a * (b - c) = a * b - a * c := contravariant.add_le_cancellable.mul_tsub lemma tsub_mul (a b c : α) : (a - b) * c = a * c - b * c := contravariant.add_le_cancellable.tsub_mul end sub /-! ### Structures involving `*` and `0` on `with_top` and `with_bot` The main results of this section are `with_top.canonically_ordered_comm_semiring` and `with_bot.comm_monoid_with_zero`. -/ namespace with_top instance [nonempty α] : nontrivial (with_top α) := option.nontrivial variable [decidable_eq α] section has_mul variables [has_zero α] [has_mul α] instance : mul_zero_class (with_top α) := { zero := 0, mul := λ m n, if m = 0 ∨ n = 0 then 0 else m.bind (λa, n.bind $ λb, ↑(a * b)), zero_mul := assume a, if_pos $ or.inl rfl, mul_zero := assume a, if_pos $ or.inr rfl } lemma mul_def {a b : with_top α} : a * b = if a = 0 ∨ b = 0 then 0 else a.bind (λa, b.bind $ λb, ↑(a * b)) := rfl @[simp] lemma mul_top {a : with_top α} (h : a ≠ 0) : a * ⊤ = ⊤ := by cases a; simp [mul_def, h]; refl @[simp] lemma top_mul {a : with_top α} (h : a ≠ 0) : ⊤ * a = ⊤ := by cases a; simp [mul_def, h]; refl @[simp] lemma top_mul_top : (⊤ * ⊤ : with_top α) = ⊤ := top_mul top_ne_zero end has_mul section mul_zero_class variables [mul_zero_class α] @[norm_cast] lemma coe_mul {a b : α} : (↑(a * b) : with_top α) = a * b := decidable.by_cases (assume : a = 0, by simp [this]) $ assume ha, decidable.by_cases (assume : b = 0, by simp [this]) $ assume hb, by { simp [*, mul_def], refl } lemma mul_coe {b : α} (hb : b ≠ 0) : ∀{a : with_top α}, a * b = a.bind (λa:α, ↑(a * b)) | none := show (if (⊤:with_top α) = 0 ∨ (b:with_top α) = 0 then 0 else ⊤ : with_top α) = ⊤, by simp [hb] | (some a) := show ↑a * ↑b = ↑(a * b), from coe_mul.symm @[simp] lemma mul_eq_top_iff {a b : with_top α} : a * b = ⊤ ↔ (a ≠ 0 ∧ b = ⊤) ∨ (a = ⊤ ∧ b ≠ 0) := begin cases a; cases b; simp only [none_eq_top, some_eq_coe], { simp [← coe_mul] }, { by_cases hb : b = 0; simp [hb] }, { by_cases ha : a = 0; simp [ha] }, { simp [← coe_mul] } end lemma mul_lt_top [preorder α] {a b : with_top α} (ha : a ≠ ⊤) (hb : b ≠ ⊤) : a * b < ⊤ := begin lift a to α using ha, lift b to α using hb, simp only [← coe_mul, coe_lt_top] end @[simp] lemma untop'_zero_mul (a b : with_top α) : (a * b).untop' 0 = a.untop' 0 * b.untop' 0 := begin by_cases ha : a = 0, { rw [ha, zero_mul, ← coe_zero, untop'_coe, zero_mul] }, by_cases hb : b = 0, { rw [hb, mul_zero, ← coe_zero, untop'_coe, mul_zero] }, induction a using with_top.rec_top_coe, { rw [top_mul hb, untop'_top, zero_mul] }, induction b using with_top.rec_top_coe, { rw [mul_top ha, untop'_top, mul_zero] }, rw [← coe_mul, untop'_coe, untop'_coe, untop'_coe] end end mul_zero_class /-- `nontrivial α` is needed here as otherwise we have `1 * ⊤ = ⊤` but also `= 0 * ⊤ = 0`. -/ instance [mul_zero_one_class α] [nontrivial α] : mul_zero_one_class (with_top α) := { mul := (*), one := 1, zero := 0, one_mul := λ a, match a with | ⊤ := mul_top (mt coe_eq_coe.1 one_ne_zero) | (a : α) := by rw [← coe_one, ← coe_mul, one_mul] end, mul_one := λ a, match a with | ⊤ := top_mul (mt coe_eq_coe.1 one_ne_zero) | (a : α) := by rw [← coe_one, ← coe_mul, mul_one] end, .. with_top.mul_zero_class } /-- A version of `with_top.map` for `monoid_with_zero_hom`s. -/ @[simps { fully_applied := ff }] protected def _root_.monoid_with_zero_hom.with_top_map {R S : Type*} [mul_zero_one_class R] [decidable_eq R] [nontrivial R] [mul_zero_one_class S] [decidable_eq S] [nontrivial S] (f : R →*₀ S) (hf : function.injective f) : with_top R →*₀ with_top S := { to_fun := with_top.map f, map_mul' := λ x y, begin have : ∀ z, map f z = 0 ↔ z = 0, from λ z, (option.map_injective hf).eq_iff' f.to_zero_hom.with_top_map.map_zero, rcases eq_or_ne x 0 with rfl|hx, { simp }, rcases eq_or_ne y 0 with rfl|hy, { simp }, induction x using with_top.rec_top_coe, { simp [hy, this] }, induction y using with_top.rec_top_coe, { have : (f x : with_top S) ≠ 0, by simpa [hf.eq_iff' (map_zero f)] using hx, simp [hx, this] }, simp [← coe_mul] end, .. f.to_zero_hom.with_top_map, .. f.to_monoid_hom.to_one_hom.with_top_map } instance [mul_zero_class α] [no_zero_divisors α] : no_zero_divisors (with_top α) := ⟨λ a b, by cases a; cases b; dsimp [mul_def]; split_ifs; simp [*, none_eq_top, some_eq_coe, mul_eq_zero] at *⟩ instance [semigroup_with_zero α] [no_zero_divisors α] : semigroup_with_zero (with_top α) := { mul := (*), zero := 0, mul_assoc := λ a b c, begin rcases eq_or_ne a 0 with rfl|ha, { simp only [zero_mul] }, rcases eq_or_ne b 0 with rfl|hb, { simp only [zero_mul, mul_zero] }, rcases eq_or_ne c 0 with rfl|hc, { simp only [mul_zero] }, induction a using with_top.rec_top_coe, { simp [hb, hc] }, induction b using with_top.rec_top_coe, { simp [ha, hc] }, induction c using with_top.rec_top_coe, { simp [ha, hb] }, simp only [← coe_mul, mul_assoc] end, .. with_top.mul_zero_class } instance [monoid_with_zero α] [no_zero_divisors α] [nontrivial α] : monoid_with_zero (with_top α) := { .. with_top.mul_zero_one_class, .. with_top.semigroup_with_zero } instance [comm_monoid_with_zero α] [no_zero_divisors α] [nontrivial α] : comm_monoid_with_zero (with_top α) := { mul := (*), zero := 0, mul_comm := λ a b, by simp only [or_comm, mul_def, option.bind_comm a b, mul_comm], .. with_top.monoid_with_zero } variables [canonically_ordered_comm_semiring α] private lemma distrib' (a b c : with_top α) : (a + b) * c = a * c + b * c := begin induction c using with_top.rec_top_coe, { by_cases ha : a = 0; simp [ha] }, { by_cases hc : c = 0, { simp [hc] }, simp [mul_coe hc], cases a; cases b, repeat { refl <|> exact congr_arg some (add_mul _ _ _) } } end /-- This instance requires `canonically_ordered_comm_semiring` as it is the smallest class that derives from both `non_assoc_non_unital_semiring` and `canonically_ordered_add_monoid`, both of which are required for distributivity. -/ instance [nontrivial α] : comm_semiring (with_top α) := { right_distrib := distrib', left_distrib := λ a b c, by { rw [mul_comm, distrib', mul_comm b, mul_comm c], refl }, .. with_top.add_comm_monoid_with_one, .. with_top.comm_monoid_with_zero } instance [nontrivial α] : canonically_ordered_comm_semiring (with_top α) := { .. with_top.comm_semiring, .. with_top.canonically_ordered_add_monoid, .. with_top.no_zero_divisors, } /-- A version of `with_top.map` for `ring_hom`s. -/ @[simps { fully_applied := ff }] protected def _root_.ring_hom.with_top_map {R S : Type*} [canonically_ordered_comm_semiring R] [decidable_eq R] [nontrivial R] [canonically_ordered_comm_semiring S] [decidable_eq S] [nontrivial S] (f : R →+* S) (hf : function.injective f) : with_top R →+* with_top S := { to_fun := with_top.map f, .. f.to_monoid_with_zero_hom.with_top_map hf, .. f.to_add_monoid_hom.with_top_map } end with_top namespace with_bot instance [nonempty α] : nontrivial (with_bot α) := option.nontrivial variable [decidable_eq α] section has_mul variables [has_zero α] [has_mul α] instance : mul_zero_class (with_bot α) := with_top.mul_zero_class lemma mul_def {a b : with_bot α} : a * b = if a = 0 ∨ b = 0 then 0 else a.bind (λa, b.bind $ λb, ↑(a * b)) := rfl @[simp] lemma mul_bot {a : with_bot α} (h : a ≠ 0) : a * ⊥ = ⊥ := with_top.mul_top h @[simp] lemma bot_mul {a : with_bot α} (h : a ≠ 0) : ⊥ * a = ⊥ := with_top.top_mul h @[simp] lemma bot_mul_bot : (⊥ * ⊥ : with_bot α) = ⊥ := with_top.top_mul_top end has_mul section mul_zero_class variables [mul_zero_class α] @[norm_cast] lemma coe_mul {a b : α} : (↑(a * b) : with_bot α) = a * b := decidable.by_cases (assume : a = 0, by simp [this]) $ assume ha, decidable.by_cases (assume : b = 0, by simp [this]) $ assume hb, by { simp [*, mul_def], refl } lemma mul_coe {b : α} (hb : b ≠ 0) {a : with_bot α} : a * b = a.bind (λa:α, ↑(a * b)) := with_top.mul_coe hb @[simp] lemma mul_eq_bot_iff {a b : with_bot α} : a * b = ⊥ ↔ (a ≠ 0 ∧ b = ⊥) ∨ (a = ⊥ ∧ b ≠ 0) := with_top.mul_eq_top_iff lemma bot_lt_mul [preorder α] {a b : with_bot α} (ha : ⊥ < a) (hb : ⊥ < b) : ⊥ < a * b := begin lift a to α using ne_bot_of_gt ha, lift b to α using ne_bot_of_gt hb, simp only [← coe_mul, bot_lt_coe], end end mul_zero_class /-- `nontrivial α` is needed here as otherwise we have `1 * ⊥ = ⊥` but also `= 0 * ⊥ = 0`. -/ instance [mul_zero_one_class α] [nontrivial α] : mul_zero_one_class (with_bot α) := with_top.mul_zero_one_class instance [mul_zero_class α] [no_zero_divisors α] : no_zero_divisors (with_bot α) := with_top.no_zero_divisors instance [semigroup_with_zero α] [no_zero_divisors α] : semigroup_with_zero (with_bot α) := with_top.semigroup_with_zero instance [monoid_with_zero α] [no_zero_divisors α] [nontrivial α] : monoid_with_zero (with_bot α) := with_top.monoid_with_zero instance [comm_monoid_with_zero α] [no_zero_divisors α] [nontrivial α] : comm_monoid_with_zero (with_bot α) := with_top.comm_monoid_with_zero instance [canonically_ordered_comm_semiring α] [nontrivial α] : comm_semiring (with_bot α) := with_top.comm_semiring instance [canonically_ordered_comm_semiring α] [nontrivial α] : pos_mul_mono (with_bot α) := pos_mul_mono_iff_covariant_pos.2 ⟨begin rintros ⟨x, x0⟩ a b h, simp only [subtype.coe_mk], lift x to α using x0.ne_bot, induction a using with_bot.rec_bot_coe, { simp_rw [mul_bot x0.ne.symm, bot_le] }, induction b using with_bot.rec_bot_coe, { exact absurd h (bot_lt_coe a).not_le }, simp only [← coe_mul, coe_le_coe] at *, exact mul_le_mul_left' h x, end ⟩ instance [canonically_ordered_comm_semiring α] [nontrivial α] : mul_pos_mono (with_bot α) := pos_mul_mono_iff_mul_pos_mono.mp infer_instance end with_bot
6992c0864d8c9ee66b2d39e57552df9f52dbfd19
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/normed_space/algebra.lean
b3ae7a3ec2ba8ed679a337c6aee7554c87b9660b
[ "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
1,761
lean
/- Copyright (c) 2022 Frédéric Dupuis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Frédéric Dupuis -/ import topology.algebra.module.character_space import analysis.normed_space.weak_dual import analysis.normed_space.spectrum /-! # Normed algebras > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file contains basic facts about normed algebras. ## Main results * We show that the character space of a normed algebra is compact using the Banach-Alaoglu theorem. ## TODO * Show compactness for topological vector spaces; this requires the TVS version of Banach-Alaoglu. ## Tags normed algebra, character space, continuous functional calculus -/ variables {𝕜 : Type*} {A : Type*} namespace weak_dual namespace character_space variables [nontrivially_normed_field 𝕜] [normed_ring A] [normed_algebra 𝕜 A] [complete_space A] lemma norm_le_norm_one (φ : character_space 𝕜 A) : ‖to_normed_dual (φ : weak_dual 𝕜 A)‖ ≤ ‖(1 : A)‖ := continuous_linear_map.op_norm_le_bound _ (norm_nonneg (1 : A)) $ λ a, mul_comm (‖a‖) (‖(1 : A)‖) ▸ spectrum.norm_le_norm_mul_of_mem (apply_mem_spectrum φ a) instance [proper_space 𝕜] : compact_space (character_space 𝕜 A) := begin rw [←is_compact_iff_compact_space], have h : character_space 𝕜 A ⊆ to_normed_dual ⁻¹' metric.closed_ball 0 (‖(1 : A)‖), { intros φ hφ, rw [set.mem_preimage, mem_closed_ball_zero_iff], exact (norm_le_norm_one ⟨φ, ⟨hφ.1, hφ.2⟩⟩ : _), }, exact is_compact_of_is_closed_subset (is_compact_closed_ball 𝕜 0 _) character_space.is_closed h, end end character_space end weak_dual
6e0cecc1e024b5528d75edff3e371919380e05c8
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/tactic/group.lean
b58aaf983e28f0bc687679f48465c1af6ca44f2b
[ "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
3,750
lean
/- Copyright (c) 2020. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot -/ import tactic.ring import tactic.doc_commands /-! # `group` Normalizes expressions in the language of groups. The basic idea is to use the simplifier to put everything into a product of group powers (`zpow` which takes a group element and an integer), then simplify the exponents using the `ring` tactic. The process needs to be repeated since `ring` can normalize an exponent to zero, leading to a factor that can be removed before collecting exponents again. The simplifier step also uses some extra lemmas to avoid some `ring` invocations. ## Tags group_theory -/ -- The next four lemmas are not general purpose lemmas, they are intended for use only by -- the `group` tactic. @[to_additive] lemma tactic.group.zpow_trick {G : Type*} [group G] (a b : G) (n m : ℤ) : a*b^n*b^m = a*b^(n+m) := by rw [mul_assoc, ← zpow_add] @[to_additive] lemma tactic.group.zpow_trick_one {G : Type*} [group G] (a b : G) (m : ℤ) : a*b*b^m = a*b^(m+1) := by rw [mul_assoc, mul_self_zpow] @[to_additive] lemma tactic.group.zpow_trick_one' {G : Type*} [group G] (a b : G) (n : ℤ) : a*b^n*b = a*b^(n+1) := by rw [mul_assoc, mul_zpow_self] @[to_additive] lemma tactic.group.zpow_trick_sub {G : Type*} [group G] (a b : G) (n m : ℤ) : a*b^n*b^(-m) = a*b^(n-m) := by rw [mul_assoc, ← zpow_add] ; refl namespace tactic setup_tactic_parser open tactic.simp_arg_type interactive tactic.group /-- Auxiliary tactic for the `group` tactic. Calls the simplifier only. -/ meta def aux_group₁ (locat : loc) : tactic unit := simp_core {} skip tt [ expr ``(mul_one), expr ``(one_mul), expr ``(one_pow), expr ``(one_zpow), expr ``(sub_self), expr ``(add_neg_self), expr ``(neg_add_self), expr ``(neg_neg), expr ``(tsub_self), expr ``(int.coe_nat_add), expr ``(int.coe_nat_mul), expr ``(int.coe_nat_zero), expr ``(int.coe_nat_one), expr ``(int.coe_nat_bit0), expr ``(int.coe_nat_bit1), expr ``(int.mul_neg_eq_neg_mul_symm), expr ``(int.neg_mul_eq_neg_mul_symm), symm_expr ``(zpow_coe_nat), symm_expr ``(zpow_neg_one), symm_expr ``(zpow_mul), symm_expr ``(zpow_add_one), symm_expr ``(zpow_one_add), symm_expr ``(zpow_add), expr ``(mul_zpow_neg_one), expr ``(zpow_zero), expr ``(mul_zpow), symm_expr ``(mul_assoc), expr ``(zpow_trick), expr ``(zpow_trick_one), expr ``(zpow_trick_one'), expr ``(zpow_trick_sub), expr ``(tactic.ring.horner)] [] locat >> skip /-- Auxiliary tactic for the `group` tactic. Calls `ring_nf` to normalize exponents. -/ meta def aux_group₂ (locat : loc) : tactic unit := ring_nf none tactic.ring.normalize_mode.raw locat end tactic namespace tactic.interactive setup_tactic_parser open tactic /-- Tactic for normalizing expressions in multiplicative groups, without assuming commutativity, using only the group axioms without any information about which group is manipulated. (For additive commutative groups, use the `abel` tactic instead.) Example: ```lean example {G : Type} [group G] (a b c d : G) (h : c = (a*b^2)*((b*b)⁻¹*a⁻¹)*d) : a*c*d⁻¹ = a := begin group at h, -- normalizes `h` which becomes `h : c = d` rw h, -- the goal is now `a*d*d⁻¹ = a` group, -- which then normalized and closed end ``` -/ meta def group (locat : parse location) : tactic unit := do when locat.include_goal `[rw ← mul_inv_eq_one], try (aux_group₁ locat), repeat (aux_group₂ locat ; aux_group₁ locat) end tactic.interactive add_tactic_doc { name := "group", category := doc_category.tactic, decl_names := [`tactic.interactive.group], tags := ["decision procedure", "simplification"] }
0c330440fbcb28c9d9d2d88c1c601ba422cc1eae
6e41ee3ac9b96e8980a16295cc21f131e731884f
/library/algebra/binary.lean
8dcca166512e983481cf5525866f217c3b5310ca
[ "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
2,291
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: algebra.binary Authors: Leonardo de Moura, Jeremy Avigad General properties of binary operations. -/ import logic.eq open eq.ops namespace binary section variable {A : Type} variables (op₁ : A → A → A) (inv : A → A) (one : A) notation [local] a * b := op₁ a b notation [local] a ⁻¹ := inv a notation [local] 1 := one definition commutative := ∀a b, a * b = b * a definition associative := ∀a b c, (a * b) * c = a * (b * c) definition left_identity := ∀a, 1 * a = a definition right_identity := ∀a, a * 1 = a definition left_inverse := ∀a, a⁻¹ * a = 1 definition right_inverse := ∀a, a * a⁻¹ = 1 definition left_cancelative := ∀a b c, a * b = a * c → b = c definition right_cancelative := ∀a b c, a * b = c * b → a = c definition inv_op_cancel_left := ∀a b, a⁻¹ * (a * b) = b definition op_inv_cancel_left := ∀a b, a * (a⁻¹ * b) = b definition inv_op_cancel_right := ∀a b, a * b⁻¹ * b = a definition op_inv_cancel_right := ∀a b, a * b * b⁻¹ = a variable (op₂ : A → A → A) notation [local] a + b := op₂ a b definition left_distributive := ∀a b c, a * (b + c) = a * b + a * c definition right_distributive := ∀a b c, (a + b) * c = a * c + b * c end context variable {A : Type} variable {f : A → A → A} variable H_comm : commutative f variable H_assoc : associative f infixl `*` := f theorem left_comm : ∀a b c, a*(b*c) = b*(a*c) := take a b c, calc a*(b*c) = (a*b)*c : H_assoc ... = (b*a)*c : H_comm ... = b*(a*c) : H_assoc theorem right_comm : ∀a b c, (a*b)*c = (a*c)*b := take a b c, calc (a*b)*c = a*(b*c) : H_assoc ... = a*(c*b) : H_comm ... = (a*c)*b : H_assoc end context variable {A : Type} variable {f : A → A → A} variable H_assoc : associative f infixl `*` := f theorem assoc4helper (a b c d) : (a*b)*(c*d) = a*((b*c)*d) := calc (a*b)*(c*d) = a*(b*(c*d)) : H_assoc ... = a*((b*c)*d) : H_assoc end end binary
41888a5771a89f7953c77288d33dba57e9c9e79c
f00cc9c04d77f9621aa57d1406d35c522c3ff82c
/library/init/algebra/classes.lean
304623b1421a032eafc5ffe6f3506271faf70fbf
[ "Apache-2.0" ]
permissive
shonfeder/lean
444c66a74676d74fb3ef682d88cd0f5c1bf928a5
24d5a1592d80cefe86552d96410c51bb07e6d411
refs/heads/master
1,619,338,440,905
1,512,842,340,000
1,512,842,340,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
11,610
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.logic init.data.ordering.basic universes u @[algebra] class is_commutative (α : Type u) (op : α → α → α) : Prop := (comm : ∀ a b, op a b = op b a) @[algebra] class is_associative (α : Type u) (op : α → α → α) : Prop := (assoc : ∀ a b c, op (op a b) c = op a (op b c)) @[algebra] class is_left_id (α : Type u) (op : α → α → α) (o : inout α) : Prop := (left_id : ∀ a, op o a = a) @[algebra] class is_right_id (α : Type u) (op : α → α → α) (o : inout α) : Prop := (right_id : ∀ a, op a o = a) @[algebra] class is_left_null (α : Type u) (op : α → α → α) (o : inout α) : Prop := (left_null : ∀ a, op o a = o) @[algebra] class is_right_null (α : Type u) (op : α → α → α) (o : inout α) : Prop := (right_null : ∀ a, op a o = o) @[algebra] class is_left_cancel (α : Type u) (op : α → α → α) : Prop := (left_cancel : ∀ a b c, op a b = op a c → b = c) @[algebra] class is_right_cancel (α : Type u) (op : α → α → α) : Prop := (right_cancel : ∀ a b c, op a b = op c b → a = c) @[algebra] class is_idempotent (α : Type u) (op : α → α → α) : Prop := (idempotent : ∀ a, op a a = a) @[algebra] class is_left_distrib (α : Type u) (op₁ : α → α → α) (op₂ : inout α → α → α) : Prop := (left_distrib : ∀ a b c, op₁ a (op₂ b c) = op₂ (op₁ a b) (op₁ a c)) @[algebra] class is_right_distrib (α : Type u) (op₁ : α → α → α) (op₂ : inout α → α → α) : Prop := (right_distrib : ∀ a b c, op₁ (op₂ a b) c = op₂ (op₁ a c) (op₁ b c)) @[algebra] class is_left_inv (α : Type u) (op : α → α → α) (inv : inout α → α) (o : inout α) : Prop := (left_inv : ∀ a, op (inv a) a = o) @[algebra] class is_right_inv (α : Type u) (op : α → α → α) (inv : inout α → α) (o : inout α) : Prop := (right_inv : ∀ a, op a (inv a) = o) @[algebra] class is_cond_left_inv (α : Type u) (op : α → α → α) (inv : inout α → α) (o : inout α) (p : inout α → Prop) : Prop := (left_inv : ∀ a, p a → op (inv a) a = o) @[algebra] class is_cond_right_inv (α : Type u) (op : α → α → α) (inv : inout α → α) (o : inout α) (p : inout α → Prop) : Prop := (right_inv : ∀ a, p a → op a (inv a) = o) @[algebra] class is_distinct (α : Type u) (a : α) (b : α) : Prop := (distinct : a ≠ b) /- -- The following type class doesn't seem very useful, a regular simp lemma should work for this. class is_inv (α : Type u) (β : Type v) (f : α → β) (g : inout β → α) : Prop := (inv : ∀ a, g (f a) = a) -- The following one can also be handled using a regular simp lemma class is_idempotent (α : Type u) (f : α → α) : Prop := (idempotent : ∀ a, f (f a) = f a) -/ @[algebra] class is_irrefl (α : Type u) (r : α → α → Prop) : Prop := (irrefl : ∀ a, ¬ r a a) @[algebra] class is_refl (α : Type u) (r : α → α → Prop) : Prop := (refl : ∀ a, r a a) @[algebra] class is_symm (α : Type u) (r : α → α → Prop) : Prop := (symm : ∀ a b, r a b → r b a) @[algebra] class is_asymm (α : Type u) (r : α → α → Prop) : Prop := (asymm : ∀ a b, r a b → ¬ r b a) @[algebra] class is_antisymm (α : Type u) (r : α → α → Prop) : Prop := (antisymm : ∀ a b, r a b → r b a → a = b) @[algebra] class is_trans (α : Type u) (r : α → α → Prop) : Prop := (trans : ∀ a b c, r a b → r b c → r a c) @[algebra] class is_total (α : Type u) (r : α → α → Prop) : Prop := (total : ∀ a b, r a b ∨ r b a) @[algebra] class is_preorder (α : Type u) (r : α → α → Prop) extends is_refl α r, is_trans α r : Prop. @[algebra] class is_total_preorder (α : Type u) (r : α → α → Prop) extends is_trans α r, is_total α r : Prop. instance is_total_preorder_is_preorder (α : Type u) (r : α → α → Prop) [s : is_total_preorder α r] : is_preorder α r := {trans := s.trans, refl := λ a, or.elim (is_total.total r a a) id id} @[algebra] class is_partial_order (α : Type u) (r : α → α → Prop) extends is_preorder α r, is_antisymm α r : Prop. @[algebra] class is_linear_order (α : Type u) (r : α → α → Prop) extends is_partial_order α r, is_total α r : Prop. @[algebra] class is_equiv (α : Type u) (r : α → α → Prop) extends is_preorder α r, is_symm α r : Prop. @[algebra] class is_per (α : Type u) (r : α → α → Prop) extends is_symm α r, is_trans α r : Prop. @[algebra] class is_strict_order (α : Type u) (r : α → α → Prop) extends is_irrefl α r, is_trans α r : Prop. @[algebra] class is_incomp_trans (α : Type u) (lt : α → α → Prop) : Prop := (incomp_trans : ∀ a b c, (¬ lt a b ∧ ¬ lt b a) → (¬ lt b c ∧ ¬ lt c b) → (¬ lt a c ∧ ¬ lt c a)) @[algebra] class is_strict_weak_order (α : Type u) (lt : α → α → Prop) extends is_strict_order α lt, is_incomp_trans α lt : Prop. @[algebra] class is_trichotomous (α : Type u) (lt : α → α → Prop) : Prop := (trichotomous : ∀ a b, lt a b ∨ a = b ∨ lt b a) @[algebra] class is_strict_total_order (α : Type u) (lt : α → α → Prop) extends is_trichotomous α lt, is_strict_weak_order α lt. instance eq_is_equiv (α : Type u) : is_equiv α (=) := {symm := @eq.symm _, trans := @eq.trans _, refl := eq.refl} section variables {α : Type u} {r : α → α → Prop} local infix `≺`:50 := r lemma irrefl [is_irrefl α r] (a : α) : ¬ a ≺ a := is_irrefl.irrefl _ a lemma refl [is_refl α r] (a : α) : a ≺ a := is_refl.refl _ a lemma trans [is_trans α r] {a b c : α} : a ≺ b → b ≺ c → a ≺ c := is_trans.trans _ _ _ lemma symm [is_symm α r] {a b : α} : a ≺ b → b ≺ a := is_symm.symm _ _ lemma antisymm [is_antisymm α r] {a b : α} : a ≺ b → b ≺ a → a = b := is_antisymm.antisymm _ _ lemma asymm [is_asymm α r] {a b : α} : a ≺ b → ¬ b ≺ a := is_asymm.asymm _ _ lemma trichotomous [is_trichotomous α r] : ∀ (a b : α), a ≺ b ∨ a = b ∨ b ≺ a := is_trichotomous.trichotomous r lemma incomp_trans [is_incomp_trans α r] {a b c : α} : (¬ a ≺ b ∧ ¬ b ≺ a) → (¬ b ≺ c ∧ ¬ c ≺ b) → (¬ a ≺ c ∧ ¬ c ≺ a) := is_incomp_trans.incomp_trans _ _ _ instance is_asymm_of_is_trans_of_is_irrefl [is_trans α r] [is_irrefl α r] : is_asymm α r := ⟨λ a b h₁ h₂, absurd (trans h₁ h₂) (irrefl a)⟩ section explicit_relation_variants variable (r) @[elab_simple] lemma irrefl_of [is_irrefl α r] (a : α) : ¬ a ≺ a := irrefl a @[elab_simple] lemma refl_of [is_refl α r] (a : α) : a ≺ a := refl a @[elab_simple] lemma trans_of [is_trans α r] {a b c : α} : a ≺ b → b ≺ c → a ≺ c := trans @[elab_simple] lemma symm_of [is_symm α r] {a b : α} : a ≺ b → b ≺ a := symm @[elab_simple] lemma asymm_of [is_asymm α r] {a b : α} : a ≺ b → ¬ b ≺ a := asymm @[elab_simple] lemma total_of [is_total α r] (a b : α) : a ≺ b ∨ b ≺ a := is_total.total _ _ _ @[elab_simple] lemma trichotomous_of [is_trichotomous α r] : ∀ (a b : α), a ≺ b ∨ a = b ∨ b ≺ a := trichotomous @[elab_simple] lemma incomp_trans_of [is_incomp_trans α r] {a b c : α} : (¬ a ≺ b ∧ ¬ b ≺ a) → (¬ b ≺ c ∧ ¬ c ≺ b) → (¬ a ≺ c ∧ ¬ c ≺ a) := incomp_trans end explicit_relation_variants end namespace strict_weak_order section parameters {α : Type u} {r : α → α → Prop} local infix `≺`:50 := r def equiv (a b : α) : Prop := ¬ a ≺ b ∧ ¬ b ≺ a parameter [is_strict_weak_order α r] local infix ` ≈ `:50 := equiv lemma erefl (a : α) : a ≈ a := ⟨irrefl a, irrefl a⟩ lemma esymm {a b : α} : a ≈ b → b ≈ a := λ ⟨h₁, h₂⟩, ⟨h₂, h₁⟩ lemma etrans {a b c : α} : a ≈ b → b ≈ c → a ≈ c := incomp_trans lemma not_lt_of_equiv {a b : α} : a ≈ b → ¬ a ≺ b := λ h, h.1 lemma not_lt_of_equiv' {a b : α} : a ≈ b → ¬ b ≺ a := λ h, h.2 instance : is_equiv α equiv := {refl := erefl, trans := @etrans, symm := @esymm} end /- Notation for the equivalence relation induced by lt -/ notation a ` ≈[`:50 lt `]` b:50 := @equiv _ lt a b end strict_weak_order lemma is_strict_weak_order_of_is_total_preorder {α : Type u} {le : α → α → Prop} {lt : α → α → Prop} [decidable_rel le] [s : is_total_preorder α le] (h : ∀ a b, lt a b ↔ ¬ le b a) : is_strict_weak_order α lt := { trans := λ a b c hab hbc, have nba : ¬ le b a, from iff.mp (h _ _) hab, have ncb : ¬ le c b, from iff.mp (h _ _) hbc, have hab : le a b, from or.resolve_left (total_of le b a) nba, have nca : ¬ le c a, from λ hca : le c a, have hcb : le c b, from trans_of le hca hab, absurd hcb ncb, iff.mpr (h _ _) nca, irrefl := λ a hlt, absurd (refl_of le a) (iff.mp (h _ _) hlt), incomp_trans := λ a b c ⟨nab, nba⟩ ⟨nbc, ncb⟩, have hba : le b a, from decidable.of_not_not (iff.mp (not_iff_not_of_iff (h _ _)) nab), have hab : le a b, from decidable.of_not_not (iff.mp (not_iff_not_of_iff (h _ _)) nba), have hcb : le c b, from decidable.of_not_not (iff.mp (not_iff_not_of_iff (h _ _)) nbc), have hbc : le b c, from decidable.of_not_not (iff.mp (not_iff_not_of_iff (h _ _)) ncb), have hac : le a c, from trans_of le hab hbc, have hca : le c a, from trans_of le hcb hba, and.intro (λ n, absurd hca (iff.mp (h _ _) n)) (λ n, absurd hac (iff.mp (h _ _) n)) } lemma lt_of_lt_of_incomp {α : Type u} {lt : α → α → Prop} [is_strict_weak_order α lt] [decidable_rel lt] : ∀ {a b c}, lt a b → (¬ lt b c ∧ ¬ lt c b) → lt a c := λ a b c hab ⟨nbc, ncb⟩, have nca : ¬ lt c a, from λ hca, absurd (trans_of lt hca hab) ncb, decidable.by_contradiction $ λ nac : ¬ lt a c, have ¬ lt a b ∧ ¬ lt b a, from incomp_trans_of lt ⟨nac, nca⟩ ⟨ncb, nbc⟩, absurd hab this.1 lemma lt_of_incomp_of_lt {α : Type u} {lt : α → α → Prop} [is_strict_weak_order α lt] [decidable_rel lt] : ∀ {a b c}, (¬ lt a b ∧ ¬ lt b a) → lt b c → lt a c := λ a b c ⟨nab, nba⟩ hbc, have nca : ¬ lt c a, from λ hca, absurd (trans_of lt hbc hca) nba, decidable.by_contradiction $ λ nac : ¬ lt a c, have ¬ lt b c ∧ ¬ lt c b, from incomp_trans_of lt ⟨nba, nab⟩ ⟨nac, nca⟩, absurd hbc this.1 lemma eq_of_incomp {α : Type u} {lt : α → α → Prop} [is_trichotomous α lt] {a b} : (¬ lt a b ∧ ¬ lt b a) → a = b := λ ⟨nab, nba⟩, match trichotomous_of lt a b with | or.inl hab := absurd hab nab | or.inr (or.inl hab) := hab | or.inr (or.inr hba) := absurd hba nba end lemma eq_of_eqv_lt {α : Type u} {lt : α → α → Prop} [is_trichotomous α lt] {a b} : a ≈[lt] b → a = b := eq_of_incomp lemma incomp_iff_eq {α : Type u} {lt : α → α → Prop} [is_trichotomous α lt] [is_irrefl α lt] (a b) : (¬ lt a b ∧ ¬ lt b a) ↔ a = b := iff.intro eq_of_incomp (λ hab, eq.subst hab (and.intro (irrefl_of lt a) (irrefl_of lt a))) lemma eqv_lt_iff_eq {α : Type u} {lt : α → α → Prop} [is_trichotomous α lt] [is_irrefl α lt] (a b) : a ≈[lt] b ↔ a = b := incomp_iff_eq a b lemma not_lt_of_lt {α : Type u} {lt : α → α → Prop} [is_strict_order α lt] {a b} : lt a b → ¬ lt b a := λ h₁ h₂, absurd (trans_of lt h₁ h₂) (irrefl_of lt _)
801b412aba3d289cb6536048d85c10e84804c935
947b78d97130d56365ae2ec264df196ce769371a
/src/Lean/Data/Lsp/Hover.lean
111ce5c2a16a06c19e745e2a88b49c053c04d5d7
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,170
lean
/- Copyright (c) 2020 Wojciech Nawrocki. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Wojciech Nawrocki -/ import Lean.Data.Json import Lean.Data.Lsp.Basic /-! Handling of mouse hover requests. -/ namespace Lean namespace Lsp open Json structure Hover := /- NOTE we should also accept MarkedString/MarkedString[] here but they are deprecated, so maybe can get away without. -/ (contents : MarkupContent) (range? : Option Range := none) instance Hover.hasFromJson : HasFromJson Hover := ⟨fun j => do contents ← j.getObjValAs? MarkupContent "contents"; let range? := j.getObjValAs? Range "range"; pure ⟨contents, range?⟩⟩ instance Hover.hasToJson : HasToJson Hover := ⟨fun o => mkObj $ ⟨"contents", toJson o.contents⟩ :: opt "range" o.range?⟩ structure HoverParams extends TextDocumentPositionParams instance HoverParams.hasFromJson : HasFromJson HoverParams := ⟨fun j => do tdpp ← @fromJson? TextDocumentPositionParams _ j; pure ⟨tdpp⟩⟩ instance HoverParams.hasToJson : HasToJson HoverParams := ⟨fun o => toJson o.toTextDocumentPositionParams⟩ end Lsp end Lean
52508eaf63ae2d33423f5d6296604c93401394e6
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/test/observe.lean
2a6ed90bbc7999b25ed38ee4e28abe0939824d2d
[ "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
145
lean
import tactic.observe example (a b c : ℕ) : a * (b * c) = (b * a) * c := begin observe : a * b = b * a, rw [← this, nat.mul_assoc], end
0ae3c83b061231da5c191d7ed0f12b0bb9419ca3
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/05_Interacting_with_Lean.org.7.lean
73f3748e22b214be8ec0dafb561ed6fcd3b06a24
[]
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
201
lean
/- page 67 -/ import standard import standard algebra.ordered_ring open nat algebra -- BEGIN check succ_ne_zero check @mul_zero check @mul_one check @sub_add_eq_add_sub check @le_iff_lt_or_eq -- END
eb2c09f318bb76138431d85660d14c7a416fd160
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/real/cau_seq_auto.lean
78f5809041f516f8f68ee3367d4f72240427e775
[]
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
26,903
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.big_operators.order import Mathlib.PostPort universes u_1 u_2 l namespace Mathlib /-! # Cauchy sequences A basic theory of Cauchy sequences, used in the construction of the reals and p-adic numbers. Where applicable, lemmas that will be reused in other contexts have been stated in extra generality. There are other "versions" of Cauchyness in the library, in particular Cauchy filters in topology. This is a concrete implementation that is useful for simplicity and computability reasons. ## Important definitions * `is_absolute_value`: a type class stating that `f : β → α` satisfies the axioms of an abs val * `is_cau_seq`: a predicate that says `f : ℕ → β` is Cauchy. * `cau_seq`: the type of Cauchy sequences valued in type `β` with respect to an absolute value function `abv`. ## Tags sequence, cauchy, abs val, absolute value -/ /-- A function `f` is an absolute value if it is nonnegative, zero only at 0, additive, and multiplicative. -/ class is_absolute_value {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] (f : β → α) where abv_nonneg : ∀ (x : β), 0 ≤ f x abv_eq_zero : ∀ {x : β}, f x = 0 ↔ x = 0 abv_add : ∀ (x y : β), f (x + y) ≤ f x + f y abv_mul : ∀ (x y : β), f (x * y) = f x * f y namespace is_absolute_value theorem abv_zero {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] (abv : β → α) [is_absolute_value abv] : abv 0 = 0 := iff.mpr (abv_eq_zero abv) rfl theorem abv_one {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] (abv : β → α) [is_absolute_value abv] [nontrivial β] : abv 1 = 1 := sorry theorem abv_pos {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] (abv : β → α) [is_absolute_value abv] {a : β} : 0 < abv a ↔ a ≠ 0 := sorry theorem abv_neg {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] (abv : β → α) [is_absolute_value abv] (a : β) : abv (-a) = abv a := sorry theorem abv_sub {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] (abv : β → α) [is_absolute_value abv] (a : β) (b : β) : abv (a - b) = abv (b - a) := eq.mpr (id (Eq._oldrec (Eq.refl (abv (a - b) = abv (b - a))) (Eq.symm (neg_sub b a)))) (eq.mpr (id (Eq._oldrec (Eq.refl (abv (-(b - a)) = abv (b - a))) (abv_neg abv (b - a)))) (Eq.refl (abv (b - a)))) /-- `abv` as a `monoid_with_zero_hom`. -/ def abv_hom {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] (abv : β → α) [is_absolute_value abv] [nontrivial β] : monoid_with_zero_hom β α := monoid_with_zero_hom.mk abv (abv_zero abv) (abv_one abv) (abv_mul abv) theorem abv_inv {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [field β] (abv : β → α) [is_absolute_value abv] (a : β) : abv (a⁻¹) = (abv a⁻¹) := monoid_with_zero_hom.map_inv' (abv_hom abv) a theorem abv_div {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [field β] (abv : β → α) [is_absolute_value abv] (a : β) (b : β) : abv (a / b) = abv a / abv b := monoid_with_zero_hom.map_div (abv_hom abv) a b theorem abv_sub_le {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] (abv : β → α) [is_absolute_value abv] (a : β) (b : β) (c : β) : abv (a - c) ≤ abv (a - b) + abv (b - c) := sorry theorem sub_abv_le_abv_sub {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] (abv : β → α) [is_absolute_value abv] (a : β) (b : β) : abv a - abv b ≤ abv (a - b) := sorry theorem abs_abv_sub_le_abv_sub {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] (abv : β → α) [is_absolute_value abv] (a : β) (b : β) : abs (abv a - abv b) ≤ abv (a - b) := sorry theorem abv_pow {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] [nontrivial β] (abv : β → α) [is_absolute_value abv] (a : β) (n : ℕ) : abv (a ^ n) = abv a ^ n := monoid_hom.map_pow (monoid_with_zero_hom.to_monoid_hom (abv_hom abv)) a n end is_absolute_value protected instance abs_is_absolute_value {α : Type u_1} [linear_ordered_field α] : is_absolute_value abs := is_absolute_value.mk abs_nonneg (fun (_x : α) => abs_eq_zero) abs_add abs_mul theorem exists_forall_ge_and {α : Type u_1} [linear_order α] {P : α → Prop} {Q : α → Prop} : (∃ (i : α), ∀ (j : α), j ≥ i → P j) → (∃ (i : α), ∀ (j : α), j ≥ i → Q j) → ∃ (i : α), ∀ (j : α), j ≥ i → P j ∧ Q j := sorry theorem rat_add_continuous_lemma {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] (abv : β → α) [is_absolute_value abv] {ε : α} (ε0 : 0 < ε) : ∃ (δ : α), ∃ (H : δ > 0), ∀ {a₁ a₂ b₁ b₂ : β}, abv (a₁ - b₁) < δ → abv (a₂ - b₂) < δ → abv (a₁ + a₂ - (b₁ + b₂)) < ε := sorry theorem rat_mul_continuous_lemma {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] (abv : β → α) [is_absolute_value abv] {ε : α} {K₁ : α} {K₂ : α} (ε0 : 0 < ε) : ∃ (δ : α), ∃ (H : δ > 0), ∀ {a₁ a₂ b₁ b₂ : β}, abv a₁ < K₁ → abv b₂ < K₂ → abv (a₁ - b₁) < δ → abv (a₂ - b₂) < δ → abv (a₁ * a₂ - b₁ * b₂) < ε := sorry theorem rat_inv_continuous_lemma {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [field β] (abv : β → α) [is_absolute_value abv] {ε : α} {K : α} (ε0 : 0 < ε) (K0 : 0 < K) : ∃ (δ : α), ∃ (H : δ > 0), ∀ {a b : β}, K ≤ abv a → K ≤ abv b → abv (a - b) < δ → abv (a⁻¹ - (b⁻¹)) < ε := sorry /-- A sequence is Cauchy if the distance between its entries tends to zero. -/ def is_cau_seq {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] (abv : β → α) (f : ℕ → β) := ∀ (ε : α), ε > 0 → ∃ (i : ℕ), ∀ (j : ℕ), j ≥ i → abv (f j - f i) < ε namespace is_cau_seq theorem cauchy₂ {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] {f : ℕ → β} (hf : is_cau_seq abv f) {ε : α} (ε0 : 0 < ε) : ∃ (i : ℕ), ∀ (j k : ℕ), j ≥ i → k ≥ i → abv (f j - f k) < ε := sorry theorem cauchy₃ {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] {f : ℕ → β} (hf : is_cau_seq abv f) {ε : α} (ε0 : 0 < ε) : ∃ (i : ℕ), ∀ (j : ℕ), j ≥ i → ∀ (k : ℕ), k ≥ j → abv (f k - f j) < ε := sorry end is_cau_seq /-- `cau_seq β abv` is the type of `β`-valued Cauchy sequences, with respect to the absolute value function `abv`. -/ def cau_seq {α : Type u_1} [linear_ordered_field α] (β : Type u_2) [ring β] (abv : β → α) := Subtype fun (f : ℕ → β) => is_cau_seq abv f namespace cau_seq protected instance has_coe_to_fun {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} : has_coe_to_fun (cau_seq β abv) := has_coe_to_fun.mk (fun (x : cau_seq β abv) => ℕ → β) subtype.val @[simp] theorem mk_to_fun {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} (f : ℕ → β) (hf : is_cau_seq abv f) : ⇑{ val := f, property := hf } = f := rfl theorem ext {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} {f : cau_seq β abv} {g : cau_seq β abv} (h : ∀ (i : ℕ), coe_fn f i = coe_fn g i) : f = g := subtype.eq (funext h) theorem is_cau {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} (f : cau_seq β abv) : is_cau_seq abv ⇑f := subtype.property f theorem cauchy {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} (f : cau_seq β abv) {ε : α} : 0 < ε → ∃ (i : ℕ), ∀ (j : ℕ), j ≥ i → abv (coe_fn f j - coe_fn f i) < ε := subtype.property f /-- Given a Cauchy sequence `f`, create a Cauchy sequence from a sequence `g` with the same values as `f`. -/ def of_eq {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} (f : cau_seq β abv) (g : ℕ → β) (e : ∀ (i : ℕ), coe_fn f i = g i) : cau_seq β abv := { val := g, property := sorry } theorem cauchy₂ {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] (f : cau_seq β abv) {ε : α} : 0 < ε → ∃ (i : ℕ), ∀ (j k : ℕ), j ≥ i → k ≥ i → abv (coe_fn f j - coe_fn f k) < ε := is_cau_seq.cauchy₂ (subtype.property f) theorem cauchy₃ {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] (f : cau_seq β abv) {ε : α} : 0 < ε → ∃ (i : ℕ), ∀ (j : ℕ), j ≥ i → ∀ (k : ℕ), k ≥ j → abv (coe_fn f k - coe_fn f j) < ε := is_cau_seq.cauchy₃ (subtype.property f) theorem bounded {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] (f : cau_seq β abv) : ∃ (r : α), ∀ (i : ℕ), abv (coe_fn f i) < r := sorry theorem bounded' {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] (f : cau_seq β abv) (x : α) : ∃ (r : α), ∃ (H : r > x), ∀ (i : ℕ), abv (coe_fn f i) < r := sorry protected instance has_add {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] : Add (cau_seq β abv) := { add := fun (f g : cau_seq β abv) => { val := fun (i : ℕ) => coe_fn f i + coe_fn g i, property := sorry } } @[simp] theorem add_apply {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] (f : cau_seq β abv) (g : cau_seq β abv) (i : ℕ) : coe_fn (f + g) i = coe_fn f i + coe_fn g i := rfl /-- The constant Cauchy sequence. -/ def const {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] (abv : β → α) [is_absolute_value abv] (x : β) : cau_seq β abv := { val := fun (i : ℕ) => x, property := sorry } @[simp] theorem const_apply {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] (x : β) (i : ℕ) : coe_fn (const abv x) i = x := rfl theorem const_inj {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] {x : β} {y : β} : const abv x = const abv y ↔ x = y := { mp := fun (h : const abv x = const abv y) => congr_arg (fun (f : cau_seq β abv) => coe_fn f 0) h, mpr := congr_arg fun {x : β} => const abv x } protected instance has_zero {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] : HasZero (cau_seq β abv) := { zero := const abv 0 } protected instance has_one {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] : HasOne (cau_seq β abv) := { one := const abv 1 } protected instance inhabited {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] : Inhabited (cau_seq β abv) := { default := 0 } @[simp] theorem zero_apply {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] (i : ℕ) : coe_fn 0 i = 0 := rfl @[simp] theorem one_apply {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] (i : ℕ) : coe_fn 1 i = 1 := rfl theorem const_add {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] (x : β) (y : β) : const abv (x + y) = const abv x + const abv y := ext fun (i : ℕ) => rfl protected instance has_mul {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] : Mul (cau_seq β abv) := { mul := fun (f g : cau_seq β abv) => { val := fun (i : ℕ) => coe_fn f i * coe_fn g i, property := sorry } } @[simp] theorem mul_apply {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] (f : cau_seq β abv) (g : cau_seq β abv) (i : ℕ) : coe_fn (f * g) i = coe_fn f i * coe_fn g i := rfl theorem const_mul {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] (x : β) (y : β) : const abv (x * y) = const abv x * const abv y := ext fun (i : ℕ) => rfl protected instance has_neg {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] : Neg (cau_seq β abv) := { neg := fun (f : cau_seq β abv) => of_eq (const abv (-1) * f) (fun (x : ℕ) => -coe_fn f x) sorry } @[simp] theorem neg_apply {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] (f : cau_seq β abv) (i : ℕ) : coe_fn (-f) i = -coe_fn f i := rfl theorem const_neg {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] (x : β) : const abv (-x) = -const abv x := ext fun (i : ℕ) => rfl protected instance has_sub {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] : Sub (cau_seq β abv) := { sub := fun (f g : cau_seq β abv) => of_eq (f + -g) (fun (x : ℕ) => coe_fn f x - coe_fn g x) sorry } @[simp] theorem sub_apply {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] (f : cau_seq β abv) (g : cau_seq β abv) (i : ℕ) : coe_fn (f - g) i = coe_fn f i - coe_fn g i := rfl theorem const_sub {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] (x : β) (y : β) : const abv (x - y) = const abv x - const abv y := ext fun (i : ℕ) => rfl protected instance ring {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] : ring (cau_seq β abv) := ring.mk Add.add sorry 0 sorry sorry Neg.neg Sub.sub sorry sorry Mul.mul sorry 1 sorry sorry sorry sorry protected instance comm_ring {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [comm_ring β] {abv : β → α} [is_absolute_value abv] : comm_ring (cau_seq β abv) := comm_ring.mk ring.add sorry ring.zero sorry sorry ring.neg ring.sub sorry sorry ring.mul sorry ring.one sorry sorry sorry sorry sorry /-- `lim_zero f` holds when `f` approaches 0. -/ def lim_zero {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} (f : cau_seq β abv) := ∀ (ε : α), ε > 0 → ∃ (i : ℕ), ∀ (j : ℕ), j ≥ i → abv (coe_fn f j) < ε theorem add_lim_zero {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] {f : cau_seq β abv} {g : cau_seq β abv} (hf : lim_zero f) (hg : lim_zero g) : lim_zero (f + g) := sorry theorem mul_lim_zero_right {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] (f : cau_seq β abv) {g : cau_seq β abv} (hg : lim_zero g) : lim_zero (f * g) := sorry theorem mul_lim_zero_left {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] {f : cau_seq β abv} (g : cau_seq β abv) (hg : lim_zero f) : lim_zero (f * g) := sorry theorem neg_lim_zero {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] {f : cau_seq β abv} (hf : lim_zero f) : lim_zero (-f) := eq.mpr (id (Eq._oldrec (Eq.refl (lim_zero (-f))) (Eq.symm (neg_one_mul f)))) (mul_lim_zero_right (-1) hf) theorem sub_lim_zero {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] {f : cau_seq β abv} {g : cau_seq β abv} (hf : lim_zero f) (hg : lim_zero g) : lim_zero (f - g) := eq.mpr (id ((fun (f f_1 : cau_seq β abv) (e_1 : f = f_1) => congr_arg lim_zero e_1) (f - g) (f + -g) (sub_eq_add_neg f g))) (eq.mp (Eq.refl (lim_zero (f + -g))) (add_lim_zero hf (neg_lim_zero hg))) theorem lim_zero_sub_rev {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] {f : cau_seq β abv} {g : cau_seq β abv} (hfg : lim_zero (f - g)) : lim_zero (g - f) := eq.mpr (id (Eq.refl (lim_zero (g - f)))) (eq.mp ((fun (f f_1 : cau_seq β abv) (e_1 : f = f_1) => congr_arg lim_zero e_1) (-(f - g)) (g - f) (neg_sub f g)) (neg_lim_zero hfg)) theorem zero_lim_zero {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] : lim_zero 0 := sorry theorem const_lim_zero {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] {x : β} : lim_zero (const abv x) ↔ x = 0 := sorry protected instance equiv {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] : setoid (cau_seq β abv) := setoid.mk (fun (f g : cau_seq β abv) => lim_zero (f - g)) sorry theorem add_equiv_add {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] {f1 : cau_seq β abv} {f2 : cau_seq β abv} {g1 : cau_seq β abv} {g2 : cau_seq β abv} (hf : f1 ≈ f2) (hg : g1 ≈ g2) : f1 + g1 ≈ f2 + g2 := sorry theorem neg_equiv_neg {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] {f : cau_seq β abv} {g : cau_seq β abv} (hf : f ≈ g) : -f ≈ -g := sorry theorem equiv_def₃ {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] {f : cau_seq β abv} {g : cau_seq β abv} (h : f ≈ g) {ε : α} (ε0 : 0 < ε) : ∃ (i : ℕ), ∀ (j : ℕ), j ≥ i → ∀ (k : ℕ), k ≥ j → abv (coe_fn f k - coe_fn g j) < ε := sorry theorem lim_zero_congr {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] {f : cau_seq β abv} {g : cau_seq β abv} (h : f ≈ g) : lim_zero f ↔ lim_zero g := sorry theorem abv_pos_of_not_lim_zero {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] {f : cau_seq β abv} (hf : ¬lim_zero f) : ∃ (K : α), ∃ (H : K > 0), ∃ (i : ℕ), ∀ (j : ℕ), j ≥ i → K ≤ abv (coe_fn f j) := sorry theorem of_near {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] (f : ℕ → β) (g : cau_seq β abv) (h : ∀ (ε : α), ε > 0 → ∃ (i : ℕ), ∀ (j : ℕ), j ≥ i → abv (f j - coe_fn g j) < ε) : is_cau_seq abv f := sorry theorem not_lim_zero_of_not_congr_zero {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] {f : cau_seq β abv} (hf : ¬f ≈ 0) : ¬lim_zero f := sorry theorem mul_equiv_zero {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] (g : cau_seq β abv) {f : cau_seq β abv} (hf : f ≈ 0) : g * f ≈ 0 := sorry theorem mul_not_equiv_zero {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] {f : cau_seq β abv} {g : cau_seq β abv} (hf : ¬f ≈ 0) (hg : ¬g ≈ 0) : ¬f * g ≈ 0 := sorry theorem const_equiv {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [ring β] {abv : β → α} [is_absolute_value abv] {x : β} {y : β} : const abv x ≈ const abv y ↔ x = y := sorry theorem mul_equiv_zero' {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [comm_ring β] {abv : β → α} [is_absolute_value abv] (g : cau_seq β abv) {f : cau_seq β abv} (hf : f ≈ 0) : f * g ≈ 0 := eq.mpr (id (Eq._oldrec (Eq.refl (f * g ≈ 0)) (mul_comm f g))) (mul_equiv_zero g hf) theorem one_not_equiv_zero {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [integral_domain β] (abv : β → α) [is_absolute_value abv] : ¬const abv 1 ≈ const abv 0 := sorry theorem inv_aux {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [field β] {abv : β → α} [is_absolute_value abv] {f : cau_seq β abv} (hf : ¬lim_zero f) (ε : α) (H : ε > 0) : ∃ (i : ℕ), ∀ (j : ℕ), j ≥ i → abv (coe_fn f j⁻¹ - (coe_fn f i⁻¹)) < ε := sorry /-- Given a Cauchy sequence `f` with nonzero limit, create a Cauchy sequence with values equal to the inverses of the values of `f`. -/ def inv {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [field β] {abv : β → α} [is_absolute_value abv] (f : cau_seq β abv) (hf : ¬lim_zero f) : cau_seq β abv := { val := fun (j : ℕ) => coe_fn f j⁻¹, property := inv_aux hf } @[simp] theorem inv_apply {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [field β] {abv : β → α} [is_absolute_value abv] {f : cau_seq β abv} (hf : ¬lim_zero f) (i : ℕ) : coe_fn (inv f hf) i = (coe_fn f i⁻¹) := rfl theorem inv_mul_cancel {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [field β] {abv : β → α} [is_absolute_value abv] {f : cau_seq β abv} (hf : ¬lim_zero f) : inv f hf * f ≈ 1 := sorry theorem const_inv {α : Type u_1} [linear_ordered_field α] {β : Type u_2} [field β] {abv : β → α} [is_absolute_value abv] {x : β} (hx : x ≠ 0) : const abv (x⁻¹) = inv (const abv x) (eq.mpr (id (Eq._oldrec (Eq.refl (¬lim_zero (const abv x))) (propext const_lim_zero))) hx) := sorry /-- The entries of a positive Cauchy sequence eventually have a positive lower bound. -/ def pos {α : Type u_1} [linear_ordered_field α] (f : cau_seq α abs) := ∃ (K : α), ∃ (H : K > 0), ∃ (i : ℕ), ∀ (j : ℕ), j ≥ i → K ≤ coe_fn f j theorem not_lim_zero_of_pos {α : Type u_1} [linear_ordered_field α] {f : cau_seq α abs} : pos f → ¬lim_zero f := sorry theorem const_pos {α : Type u_1} [linear_ordered_field α] {x : α} : pos (const abs x) ↔ 0 < x := sorry theorem add_pos {α : Type u_1} [linear_ordered_field α] {f : cau_seq α abs} {g : cau_seq α abs} : pos f → pos g → pos (f + g) := sorry theorem pos_add_lim_zero {α : Type u_1} [linear_ordered_field α] {f : cau_seq α abs} {g : cau_seq α abs} : pos f → lim_zero g → pos (f + g) := sorry protected theorem mul_pos {α : Type u_1} [linear_ordered_field α] {f : cau_seq α abs} {g : cau_seq α abs} : pos f → pos g → pos (f * g) := sorry theorem trichotomy {α : Type u_1} [linear_ordered_field α] (f : cau_seq α abs) : pos f ∨ lim_zero f ∨ pos (-f) := sorry protected instance has_lt {α : Type u_1} [linear_ordered_field α] : HasLess (cau_seq α abs) := { Less := fun (f g : cau_seq α abs) => pos (g - f) } protected instance has_le {α : Type u_1} [linear_ordered_field α] : HasLessEq (cau_seq α abs) := { LessEq := fun (f g : cau_seq α abs) => f < g ∨ f ≈ g } theorem lt_of_lt_of_eq {α : Type u_1} [linear_ordered_field α] {f : cau_seq α abs} {g : cau_seq α abs} {h : cau_seq α abs} (fg : f < g) (gh : g ≈ h) : f < h := sorry theorem lt_of_eq_of_lt {α : Type u_1} [linear_ordered_field α] {f : cau_seq α abs} {g : cau_seq α abs} {h : cau_seq α abs} (fg : f ≈ g) (gh : g < h) : f < h := eq.mp (Eq._oldrec (Eq.refl (pos (h - g - (f - g)))) (sub_sub_sub_cancel_right h f g)) (eq.mp (Eq._oldrec (Eq.refl (pos (h - g + -(f - g)))) (Eq.symm (sub_eq_add_neg (h - g) (f - g)))) (pos_add_lim_zero gh (neg_lim_zero fg))) theorem lt_trans {α : Type u_1} [linear_ordered_field α] {f : cau_seq α abs} {g : cau_seq α abs} {h : cau_seq α abs} (fg : f < g) (gh : g < h) : f < h := sorry theorem lt_irrefl {α : Type u_1} [linear_ordered_field α] {f : cau_seq α abs} : ¬f < f := sorry theorem le_of_eq_of_le {α : Type u_1} [linear_ordered_field α] {f : cau_seq α abs} {g : cau_seq α abs} {h : cau_seq α abs} (hfg : f ≈ g) (hgh : g ≤ h) : f ≤ h := or.elim hgh (Or.inl ∘ lt_of_eq_of_lt hfg) (Or.inr ∘ setoid.trans hfg) theorem le_of_le_of_eq {α : Type u_1} [linear_ordered_field α] {f : cau_seq α abs} {g : cau_seq α abs} {h : cau_seq α abs} (hfg : f ≤ g) (hgh : g ≈ h) : f ≤ h := or.elim hfg (fun (h_1 : f < g) => Or.inl (lt_of_lt_of_eq h_1 hgh)) fun (h_1 : f ≈ g) => Or.inr (setoid.trans h_1 hgh) protected instance preorder {α : Type u_1} [linear_ordered_field α] : preorder (cau_seq α abs) := preorder.mk (fun (f g : cau_seq α abs) => f < g ∨ f ≈ g) Less sorry sorry theorem le_antisymm {α : Type u_1} [linear_ordered_field α] {f : cau_seq α abs} {g : cau_seq α abs} (fg : f ≤ g) (gf : g ≤ f) : f ≈ g := or.resolve_left fg (not_lt_of_le gf) theorem lt_total {α : Type u_1} [linear_ordered_field α] (f : cau_seq α abs) (g : cau_seq α abs) : f < g ∨ f ≈ g ∨ g < f := sorry theorem le_total {α : Type u_1} [linear_ordered_field α] (f : cau_seq α abs) (g : cau_seq α abs) : f ≤ g ∨ g ≤ f := or.imp_right Or.inl (iff.mpr or.assoc (lt_total f g)) theorem const_lt {α : Type u_1} [linear_ordered_field α] {x : α} {y : α} : const abs x < const abs y ↔ x < y := sorry theorem const_le {α : Type u_1} [linear_ordered_field α] {x : α} {y : α} : const abs x ≤ const abs y ↔ x ≤ y := eq.mpr (id (Eq._oldrec (Eq.refl (const abs x ≤ const abs y ↔ x ≤ y)) (propext le_iff_lt_or_eq))) (or_congr const_lt const_equiv) theorem le_of_exists {α : Type u_1} [linear_ordered_field α] {f : cau_seq α abs} {g : cau_seq α abs} (h : ∃ (i : ℕ), ∀ (j : ℕ), j ≥ i → coe_fn f j ≤ coe_fn g j) : f ≤ g := sorry theorem exists_gt {α : Type u_1} [linear_ordered_field α] (f : cau_seq α abs) : ∃ (a : α), f < const abs a := sorry theorem exists_lt {α : Type u_1} [linear_ordered_field α] (f : cau_seq α abs) : ∃ (a : α), const abs a < f := sorry end Mathlib
06082bd6e7868b741d569d545d79902a47c63873
947b78d97130d56365ae2ec264df196ce769371a
/stage0/src/Lean/Data/Json/Printer.lean
8c6ed569b6eaa07e65ef636934e109d64fbc2e7f
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,744
lean
/- Copyright (c) 2019 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner, Marc Huisinga -/ import Lean.Data.Format import Lean.Data.Json.Basic namespace Lean namespace Json private def escapeAux (c : Char) (acc : String) : String := -- escape ", \, \n and \r, keep all other characters ≥ 0x20 and render characters < 0x20 with \u if c = '"' then -- hack to prevent emacs from regarding the rest of the file as a string: " "\\\"" ++ acc else if c = '\\' then "\\\\" ++ acc else if c = '\n' then "\\n" ++ acc else if c = '\x0d' then "\\r" ++ acc -- the c.val ≤ 0x10ffff check is technically redundant, -- since Lean chars are unicode scalar values ≤ 0x10ffff. -- as to whether c.val > 0xffff should be split up and encoded with multiple \u, -- the JSON standard is not definite: both directly printing the character -- and encoding it with multiple \u is allowed, and it is up to parsers to make the -- decision. else if 0x0020 ≤ c.val ∧ c.val ≤ 0x10ffff then String.singleton c ++ acc else let n := c.toNat; -- since c.val < 0x20 in this case, this conversion is more involved than necessary -- (but we keep it for completeness) "\\u" ++ [ Nat.digitChar (n / 4096), Nat.digitChar ((n % 4096) / 256), Nat.digitChar ((n % 256) / 16), Nat.digitChar (n % 16) ].asString ++ acc def escape (s : String) : String := s.foldr escapeAux "" def renderString (s : String) : String := "\"" ++ escape s ++ "\"" section variables {α : Type} @[specialize] partial def render : Json → Format | null => "null" | bool true => "true" | bool false => "false" | num s => s.toString | str s => renderString s | arr elems => let elems := Format.joinSep (elems.map render).toList ("," ++ Format.line); Format.bracket "[" elems "]" | obj kvs => let renderKV : String → Json → Format := fun k v => Format.group (renderString k ++ ":" ++ Format.line ++ render v); let kvs := Format.joinSep (kvs.fold (fun acc k j => renderKV k j :: acc) []) ("," ++ Format.line); Format.bracket "{" kvs "}" end def pretty (j : Json) (lineWidth := 80) : String := Format.prettyAux (render j) lineWidth partial def compress : Json → String | null => "null" | bool true => "true" | bool false => "false" | num s => s.toString | str s => renderString s | arr elems => "[" ++ ",".intercalate (elems.map compress).toList ++ "]" | obj kvs => let ckvs := kvs.fold (fun acc k j => (renderString k ++ ":" ++ compress j) :: acc) []; "{" ++ ",".intercalate ckvs ++ "}" instance jsonHasFormat : HasFormat Json := ⟨render⟩ instance jsonHasToString : HasToString Json := ⟨pretty⟩ end Json end Lean
84c0145df257bf3eaaa79d67f86c7b8db765e969
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/topology/category/Top/adjunctions.lean
5083d17585d7c7dcfca6cc709fb3972ade34c0d4
[ "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
1,426
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Patrick Massot, Mario Carneiro -/ import topology.category.Top.basic import category_theory.adjunction.basic /-! # Adjunctions regarding the category of topological spaces > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file shows that the forgetful functor from topological spaces to types has a left and right adjoint, given by `Top.discrete`, resp. `Top.trivial`, the functors which equip a type with the discrete, resp. trivial, topology. -/ universe u open category_theory open Top namespace Top /-- Equipping a type with the discrete topology is left adjoint to the forgetful functor `Top ⥤ Type`. -/ @[simps unit counit] def adj₁ : discrete ⊣ forget Top.{u} := adjunction.mk_of_unit_counit { unit := { app := λ X, id }, counit := { app := λ X, ⟨id, continuous_bot⟩ } } /-- Equipping a type with the trivial topology is right adjoint to the forgetful functor `Top ⥤ Type`. -/ @[simps unit counit] def adj₂ : forget Top.{u} ⊣ trivial := adjunction.mk_of_unit_counit { unit := { app := λ X, ⟨id, continuous_top⟩ }, counit := { app := λ X, id } } instance : is_right_adjoint (forget Top.{u}) := ⟨_, adj₁⟩ instance : is_left_adjoint (forget Top.{u}) := ⟨_, adj₂⟩ end Top
18ca88f7826b680eae21837164cffcc58df4ed4d
80746c6dba6a866de5431094bf9f8f841b043d77
/test/tests/linarith.lean
b3dedc4a4e88c2011543b557ebd8b42bc65036b9
[ "Apache-2.0" ]
permissive
leanprover-fork/mathlib-backup
8b5c95c535b148fca858f7e8db75a76252e32987
0eb9db6a1a8a605f0cf9e33873d0450f9f0ae9b0
refs/heads/master
1,585,156,056,139
1,548,864,430,000
1,548,864,438,000
143,964,213
0
0
Apache-2.0
1,550,795,966,000
1,533,705,322,000
Lean
UTF-8
Lean
false
false
3,289
lean
import tactic.linarith example (e b c a v0 v1 : ℚ) (h1 : v0 = 5*a) (h2 : v1 = 3*b) (h3 : v0 + v1 + c = 10) : v0 + 5 + (v1 - 3) + (c - 2) = 10 := by linarith example (ε : ℚ) (h1 : ε > 0) : ε / 2 + ε / 3 + ε / 7 < ε := by linarith example (x y z : ℚ) (h1 : 2*x < 3*y) (h2 : -4*x + z/2 < 0) (h3 : 12*y - z < 0) : false := by linarith example (ε : ℚ) (h1 : ε > 0) : ε / 2 < ε := by linarith example (ε : ℚ) (h1 : ε > 0) : ε / 3 + ε / 3 + ε / 3 = ε := by linarith example (a b c : ℚ) (h2 : b + 2 > 3 + b) : false := by linarith {discharger := `[ring SOP]} example (a b c : ℚ) (h2 : b + 2 > 3 + b) : false := by linarith example (a b c : ℚ) (x y : ℤ) (h1 : x ≤ 3*y) (h2 : b + 2 > 3 + b) : false := by linarith {restrict_type := ℚ} example (g v V c h : ℚ) (h1 : h = 0) (h2 : v = V) (h3 : V > 0) (h4 : g > 0) (h5 : 0 ≤ c) (h6 : c < 1) : v ≤ V := by linarith example (x y z : ℚ) (h1 : 2*x + ((-3)*y) < 0) (h2 : (-4)*x + 2*z < 0) (h3 : 12*y + (-4)* z < 0) (h4 : nat.prime 7) : false := by linarith example (x y z : ℚ) (h1 : 2*1*x + (3)*(y*(-1)) < 0) (h2 : (-2)*x*2 < -(z + z)) (h3 : 12*y + (-4)* z < 0) (h4 : nat.prime 7) : false := by linarith example (x y z : ℤ) (h1 : 2*x < 3*y) (h2 : -4*x + 2*z < 0) (h3 : 12*y - 4* z < 0) : false := by linarith example (x y z : ℤ) (h1 : 2*x < 3*y) (h2 : -4*x + 2*z < 0) (h3 : x*y < 5) (h3 : 12*y - 4* z < 0) : false := by linarith example (x y z : ℤ) (h1 : 2*x < 3*y) (h2 : -4*x + 2*z < 0) (h3 : x*y < 5) : ¬ 12*y - 4* z < 0 := by linarith example (w x y z : ℤ) (h1 : 4*x + (-3)*y + 6*w ≤ 0) (h2 : (-1)*x < 0) (h3 : y < 0) (h4 : w ≥ 0) (h5 : nat.prime x.nat_abs) : false := by linarith example (a b c : ℚ) (h1 : a > 0) (h2 : b > 5) (h3 : c < -10) (h4 : a + b - c < 3) : false := by linarith example (a b c : ℚ) (h2 : b > 0) (h3 : ¬ b ≥ 0) : false := by linarith example (a b c : ℚ) (h2 : (2 : ℚ) > 3) : a + b - c ≥ 3 := by linarith {exfalso := ff} example (x : ℚ) (hx : x > 0) (h : x.num < 0) : false := by linarith using [rat.num_pos_iff_pos.mpr hx] example (x y z : ℚ) (hx : x ≤ 3*y) (h2 : y ≤ 2*z) (h3 : x ≥ 6*z) : x = 3*y := by linarith example (x y z : ℕ) (hx : x ≤ 3*y) (h2 : y ≤ 2*z) (h3 : x ≥ 6*z) : x = 3*y := by linarith example (x y z : ℚ) (hx : ¬ x > 3*y) (h2 : ¬ y > 2*z) (h3 : x ≥ 6*z) : x = 3*y := by linarith example (h1 : (1 : ℕ) < 1) : false := by linarith example (a b c : ℚ) (h2 : b > 0) (h3 : b < 0) : nat.prime 10 := by linarith example (a b c : ℕ) : a + b ≥ a := by linarith example (a b c : ℕ) : ¬ a + b < a := by linarith example (x y : ℚ) (h : 6 + ((x + 4) * x + (6 + 3 * y) * y) = 3) (h' : (x + 4) * x ≥ 0) (h'' : (6 + 3 * y) * y ≥ 0) : false := by linarith example (x y : ℕ) (h : 6 + ((x + 4) * x + (6 + 3 * y) * y) = 3) : false := by linarith example (a b i : ℕ) (h1 : ¬ a < i) (h2 : b < i) (h3 : a ≤ b) : false := by linarith example (a b c : ℚ) (h1 : 1 / a < b) (h2 : b < c) : 1 / a < c := by linarith example (N : ℕ) (n : ℕ) (Hirrelevant : n > N) (A : ℚ) (l : ℚ) (h : A - l ≤ -(A - l)) (h_1 : ¬A ≤ -A) (h_2 : ¬l ≤ -l) (h_3 : -(A - l) < 1) : A < l + 1 := by linarith
adbcfdf1f45bef5567036f56df4b513190814de2
1fd908b06e3f9c1252cb2285ada1102623a67f72
/arity.lean
3cd9ee24a14c4465c93dad68d9ff9307f4444620
[ "Apache-2.0" ]
permissive
avigad/hott3
609a002849182721e7c7ae536d9f1e2956d6d4d3
f64750cd2de7a81e87d4828246d1369d59f16f43
refs/heads/master
1,629,027,243,322
1,510,946,717,000
1,510,946,717,000
103,570,461
0
0
null
1,505,415,620,000
1,505,415,620,000
null
UTF-8
Lean
false
false
12,474
lean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Floris van Doorn Theorems about functions with multiple arguments -/ import .init universes u v w hott_theory namespace hott variables {A : Type _} {U : Type _} {V : Type _} {W : Type _} {X : Type _} {Y : Type _} {Z : Type _} {B : A → Type _} {C : Πa, B a → Type _} {D : Πa b, C a b → Type _} {E : Πa b c, D a b c → Type _} {F : Πa b c d, E a b c d → Type _} {G : Πa b c d e, F a b c d e → Type _} {H : Πa b c d e f, G a b c d e f → Type _} variables {a a' : A} {u u' : U} {v v' : V} {w w' : W} {x x' x'' : X} {y y' : Y} {z z' : Z} {b : B a} {b' : B a'} {c : C a b} {c' : C a' b'} {d : D a b c} {d' : D a' b' c'} {e : E a b c d} {e' : E a' b' c' d'} {ff : F a b c d e} {f' : F a' b' c' d' e'} {g : G a b c d e ff} {g' : G a' b' c' d' e' f'} {h : H a b c d e ff g} {h' : H a' b' c' d' e' f' g'} namespace eq /- Naming convention: The theorem which states how to construct an path between two function applications is api₀i₁...iₙ. Here i₀, ... iₙ are digits, n is the arity of the function(s), and iⱼ specifies the dimension of the path between the jᵗʰ argument (i₀ specifies the dimension of the path between the functions). A value iⱼ ≡ 0 means that the jᵗʰ arguments are definitionaly equal The functions are non-dependent, except when the theorem name contains trailing zeroes (where the function is dependent only in the arguments where it doesn't result in any transports in the theorem statement). For the fully-dependent versions (except that the conclusion doesn't contain a transport) we write apdi₀i₁...iₙ. For versions where only some arguments depend on some other arguments, or for versions with transport in the conclusion (like apdt), we don't have a consistent naming scheme (yet). We don't prove each theorem systematically, but prove only the ones which we actually need. -/ @[hott, reducible] def homotopy2 (f g : Πa b, C a b) : Type _ := Πa b, f a b = g a b @[hott, reducible] def homotopy3 (f g : Πa b c, D a b c) : Type _ := Πa b c, f a b c = g a b c @[hott, reducible] def homotopy4 (f g : Πa b c d, E a b c d) : Type _ := Πa b c d, f a b c d = g a b c d infix ` ~2 `:50 := homotopy2 infix ` ~3 `:50 := homotopy3 infix ` ~4 `:50 := homotopy4 @[refl] def homotopy2.refl (f : Π a b, C a b) : f ~2 f := by intros _ _; refl @[refl] def homotopy3.refl (f : Π a b c, D a b c) : f ~3 f := by intros _ _ _; refl @[refl] def homotopy4.refl (f : Π a b c d, E a b c d) : f ~4 f := by intros _ _ _ _; refl @[hott] def ap0111 (f : U → V → W → X) (Hu : u = u') (Hv : v = v') (Hw : w = w') : f u v w = f u' v' w' := by induction Hu; hsimp * @[hott] def ap01111 (f : U → V → W → X → Y) (Hu : u = u') (Hv : v = v') (Hw : w = w') (Hx : x = x') : f u v w x = f u' v' w' x' := by induction Hu; hsimp * @[hott] def ap011111 (f : U → V → W → X → Y → Z) (Hu : u = u') (Hv : v = v') (Hw : w = w') (Hx : x = x') (Hy : y = y') : f u v w x y = f u' v' w' x' y' := by induction Hu; hsimp * @[hott] def ap0111111 (f : U → V → W → X → Y → Z → A) (Hu : u = u') (Hv : v = v') (Hw : w = w') (Hx : x = x') (Hy : y = y') (Hz : z = z') : f u v w x y z = f u' v' w' x' y' z' := by induction Hu; hsimp * @[hott, elab_simple] def ap010 (f : X → Πa, B a) (Hx : x = x') : f x ~ f x' := λ b, ap (λa, f a b) Hx @[hott] def ap0100 (f : X → Πa b, C a b) (Hx : x = x') : f x ~2 f x' := by intros; induction Hx; reflexivity @[hott] def ap01000 (f : X → Πa b c, D a b c) (Hx : x = x') : f x ~3 f x' := by intros; induction Hx; reflexivity @[hott] def apdt011 (f : Πa, B a → Z) (Ha : a = a') (Hb : transport B Ha b = b') : f a b = f a' b' := by induction Ha; induction Hb; reflexivity @[hott] def apdt0111 (f : Πa b, C a b → Z) (Ha : a = a') (Hb : transport B Ha b = b') (Hc : cast (apdt011 C Ha Hb) c = c') : f a b c = f a' b' c' := by induction Ha; induction Hb; induction Hc; reflexivity @[hott] def apdt01111 (f : Πa b c, D a b c → Z) (Ha : a = a') (Hb : transport B Ha b = b') (Hc : cast (apdt011 C Ha Hb) c = c') (Hd : cast (apdt0111 D Ha Hb Hc) d = d') : f a b c d = f a' b' c' d' := by induction Ha; induction Hb; induction Hc; induction Hd; reflexivity @[hott] def apdt011111 (f : Πa b c d, E a b c d → Z) (Ha : a = a') (Hb : transport B Ha b = b') (Hc : cast (apdt011 C Ha Hb) c = c') (Hd : cast (apdt0111 D Ha Hb Hc) d = d') (He : cast (apdt01111 E Ha Hb Hc Hd) e = e') : f a b c d e = f a' b' c' d' e' := by induction Ha; induction Hb; induction Hc; induction Hd; induction He; reflexivity @[hott] def apdt0111111 (f : Πa b c d e, F a b c d e → Z) (Ha : a = a') (Hb : transport B Ha b = b') (Hc : cast (apdt011 C Ha Hb) c = c') (Hd : cast (apdt0111 D Ha Hb Hc) d = d') (He : cast (apdt01111 E Ha Hb Hc Hd) e = e') (Hf : cast (apdt011111 F Ha Hb Hc Hd He) ff = f') : f a b c d e ff = f a' b' c' d' e' f' := begin induction Ha, induction Hb, induction Hc, induction Hd, induction He, induction Hf, reflexivity end -- @[hott] def apd0111111 (f : Πa b c d e ff, G a b c d e ff → Z) (Ha : a = a') (Hb : transport B Ha b = b') -- (Hc : cast (apd011 C Ha Hb) c = c') (Hd : cast (apd0111 D Ha Hb Hc) d = d') -- (He : cast (apd01111 E Ha Hb Hc Hd) e = e') (Hf : cast (apd011111 F Ha Hb Hc Hd He) ff = f') -- (Hg : cast (apd0111111 G Ha Hb Hc Hd He Hf) g = g') -- : f a b c d e ff g = f a' b' c' d' e' f' g' := -- by induction Ha; induction Hb; induction Hc; induction Hd; induction He; induction Hf; induction Hg; reflexivity -- @[hott] def apd01111111 (f : Πa b c d e ff g, G a b c d e ff g → Z) (Ha : a = a') (Hb : transport B Ha b = b') -- (Hc : cast (apd011 C Ha Hb) c = c') (Hd : cast (apd0111 D Ha Hb Hc) d = d') -- (He : cast (apd01111 E Ha Hb Hc Hd) e = e') (Hf : cast (apd011111 F Ha Hb Hc Hd He) ff = f') -- (Hg : cast (apd0111111 G Ha Hb Hc Hd He Hf) g = g') (Hh : cast (apd01111111 H Ha Hb Hc Hd He Hf Hg) h = h') -- : f a b c d e ff g h = f a' b' c' d' e' f' g' h' := -- by induction Ha; induction Hb; induction Hc; induction Hd; induction He; induction Hf; induction Hg; induction Hh; reflexivity @[hott] def apd100 {f g : Πa b, C a b} (p : f = g) : f ~2 g := λa b, apd10 (apd10 p a) b @[hott] def apd1000 {f g : Πa b c, D a b c} (p : f = g) : f ~3 g := λa b c, apd100 (apd10 p a) b c /- some properties of these variants of ap -/ -- we only prove what we currently need @[hott] def ap010_con (f : X → Πa, B a) (p : x = x') (q : x' = x'') : ap010 f (p ⬝ q) a = ap010 f p a ⬝ ap010 f q a := eq.rec_on q (eq.rec_on p idp) @[hott] def ap010_ap (f : X → Πa, B a) (g : Y → X) (p : y = y') : ap010 f (ap g p) a = (ap010 (f ∘ g) p) a := eq.rec_on p idp @[hott] def ap_eq_ap010 {A B C : Type _} (f : A → B → C) {a a' : A} (p : a = a') (b : B) : ap (λa, f a b) p = ap010 f p b := idp @[hott] def ap011_idp {A B C : Type _} (f : A → B → C) {a a' : A} (p : a = a') (b : B) : ap011 f p idp = ap010 f p b := by reflexivity @[hott] def ap011_flip {A B C : Type _} (f : A → B → C) {a a' : A} {b b' : B} (p : a = a') (q : b = b') : ap011 f p q = (ap011 (λb a, f a b) q) p := by induction q; induction p; reflexivity /- the following theorems are function extentionality for functions with multiple arguments -/ @[hott] def eq_of_homotopy2 {f g : Πa b, C a b} (H : f ~2 g) : f = g := eq_of_homotopy (λa, eq_of_homotopy (H a)) @[hott] def eq_of_homotopy3 {f g : Πa b c, D a b c} (H : f ~3 g) : f = g := eq_of_homotopy (λa, eq_of_homotopy2 (H a)) @[hott] def eq_of_homotopy2_id (f : Πa b, C a b) : eq_of_homotopy2 (λa b, idpath (f a b)) = idpath f := begin transitivity eq_of_homotopy (λ a, idpath (f a)), {apply (ap eq_of_homotopy), apply eq_of_homotopy, intro, apply eq_of_homotopy_idp}, apply eq_of_homotopy_idp end @[hott] def eq_of_homotopy3_id (f : Πa b c, D a b c) : eq_of_homotopy3 (λa b c, idpath (f a b c)) = idpath f := begin transitivity _, {apply (ap eq_of_homotopy), apply eq_of_homotopy, intro, apply eq_of_homotopy2_id}, apply eq_of_homotopy_idp end @[hott] def eq_of_homotopy2_inv {f g : Πa b, C a b} (H : f ~2 g) : eq_of_homotopy2 (λa b, (H a b).inverse) = (eq_of_homotopy2 H)⁻¹ := begin transitivity, {dsimp [eq_of_homotopy2], apply ap, apply eq_of_homotopy, intro, apply eq_of_homotopy_inv}, {apply eq_of_homotopy_inv} end @[hott] def eq_of_homotopy3_inv {f g : Πa b c, D a b c} (H : f ~3 g) : eq_of_homotopy3 (λa b c, (H a b c).inverse) = (eq_of_homotopy3 H)⁻¹ := begin transitivity, {dsimp [eq_of_homotopy3], apply ap, apply eq_of_homotopy, intro, apply eq_of_homotopy2_inv}, {apply eq_of_homotopy_inv} end @[hott] def eq_of_homotopy2_con {f g h : Πa b, C a b} (H1 : f ~2 g) (H2 : g ~2 h) : eq_of_homotopy2 (λa b, H1 a b ⬝ H2 a b) = eq_of_homotopy2 H1 ⬝ eq_of_homotopy2 H2 := begin transitivity, {dsimp [eq_of_homotopy2], apply ap, apply eq_of_homotopy, intro, apply eq_of_homotopy_con}, {apply eq_of_homotopy_con} end @[hott] def eq_of_homotopy3_con {f g h : Πa b c, D a b c} (H1 : f ~3 g) (H2 : g ~3 h) : eq_of_homotopy3 (λa b c, H1 a b c ⬝ H2 a b c) = eq_of_homotopy3 H1 ⬝ eq_of_homotopy3 H2 := begin transitivity, {dsimp [eq_of_homotopy3], apply ap, apply eq_of_homotopy, intro, apply eq_of_homotopy2_con}, {apply eq_of_homotopy_con} end end eq open hott.eq hott.equiv hott.is_equiv namespace funext @[hott, instance] def is_equiv_apd100 (f g : Πa b, C a b) : is_equiv (@apd100 A B C f g) := adjointify _ eq_of_homotopy2 begin intro H, dsimp [apd100, eq_of_homotopy2], apply eq_of_homotopy, intro a, apply concat, apply (ap (λx : Π a, f a = g a, apd10 (x a))), apply (right_inv apd10), apply (right_inv apd10) end begin intro p, induction p, apply eq_of_homotopy2_id end @[hott, instance] def is_equiv_apd1000 (f g : Πa b c, D a b c) : is_equiv (@apd1000 A B C D f g) := adjointify _ eq_of_homotopy3 begin intro H, dsimp, apply eq_of_homotopy, intro a, transitivity apd100 (eq_of_homotopy2 (H a)), {apply ap (λ x : Π a, f a = g a, apd100 (x a)), apply right_inv apd10}, apply right_inv apd100 end begin intro p, induction p, apply eq_of_homotopy3_id end end funext namespace eq open funext local attribute [instance] funext.is_equiv_apd100 @[hott] protected def homotopy2.rec_on {f g : Πa b, C a b} {P : (f ~2 g) → Type _} (p : f ~2 g) (H : Π(q : f = g), P (apd100 q)) : P p := right_inv apd100 p ▸ H (eq_of_homotopy2 p) @[hott] protected def homotopy3.rec_on {f g : Πa b c, D a b c} {P : (f ~3 g) → Type _} (p : f ~3 g) (H : Π(q : f = g), P (apd1000 q)) : P p := right_inv apd1000 p ▸ H (eq_of_homotopy3 p) @[hott] def eq_equiv_homotopy2 (f g : Πa b, C a b) : (f = g) ≃ (f ~2 g) := equiv.mk apd100 (by apply_instance) @[hott] def eq_equiv_homotopy3 (f g : Πa b c, D a b c) : (f = g) ≃ (f ~3 g) := equiv.mk apd1000 (by apply_instance) @[hott] def apd10_ap (f : X → Πa, B a) (p : x = x') : apd10 (ap f p) = ap010 f p := eq.rec_on p idp @[hott] def eq_of_homotopy_ap010 (f : X → Πa, B a) (p : x = x') : eq_of_homotopy (ap010 f p) = ap f p := inv_eq_of_eq (apd10_ap _ _)⁻¹ @[hott] def ap_eq_ap_of_homotopy {f : X → Πa, B a} {p q : x = x'} (H : ap010 f p ~ ap010 f q) : ap f p = ap f q := calc ap f p = eq_of_homotopy (ap010 f p) : by symmetry; apply eq_of_homotopy_ap010 ... = eq_of_homotopy (ap010 f q) : by apply ap; apply eq_of_homotopy H ... = ap f q : by apply eq_of_homotopy_ap010 end eq end hott
c09475c8ccbbd50e7522ac112d41710c5aa10653
99b5e6372af1f404777312358869f95be7de84a3
/src/hott/types/trunc.lean
d3c4587b3f7c876df486eb746a831452d5887ebb
[ "Apache-2.0" ]
permissive
forked-from-1kasper/hott3
8fa064ab5e8c9d6752a783d74ab226ddc5b5232a
2db24de7a361a7793b0eae4ca5c3fd4d4a0fc691
refs/heads/master
1,584,867,131,028
1,530,766,841,000
1,530,766,841,000
139,797,034
0
0
Apache-2.0
1,530,766,961,000
1,530,766,961,000
null
UTF-8
Lean
false
false
35,636
lean
/- Copyright (c) 2015 Jakob von Raumer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn Properties of trunc_index, is_trunc, trunctype, trunc, and the pointed versions of these -/ -- NOTE: the fact that (is_trunc n A) is a mere proposition is proved in .prop_trunc import ..function .unit universes u v w hott_theory namespace hott open hott.sigma hott.pi hott.function hott.equiv trunctype nat hott.is_equiv hott.prod pointed hott.nat hott.is_trunc hott.algebra hott.sum sum hott.unit /- basic computation with ℕ₋₂, its operations and its order -/ namespace trunc_index instance has_le_trunc_index : has_le ℕ₋₂ := ⟨trunc_index.le⟩ @[hott] def minus_one_le_succ (n : ℕ₋₂) : -1 ≤ n.+1 := succ_le_succ (minus_two_le n) @[hott] def zero_le_of_nat (n : ℕ) : 0 ≤ of_nat n := succ_le_succ (minus_one_le_succ _) @[hott, reducible] protected def code : ℕ₋₂ → ℕ₋₂ → Type | -2 -2 := unit | -2 (succ m) := empty | (succ n) -2 := empty | (succ n) (succ m) := n = m @[hott] protected def refl : Πn, trunc_index.code n n | -2 := ⋆ | (succ n) := idp @[hott] protected def encode {n m : ℕ₋₂} (p : n = m) : trunc_index.code n m := p ▸ trunc_index.refl n @[hott] protected def decode : Π(n m : ℕ₋₂), trunc_index.code n m → n = m | -2 -2 := λc, idp | -2 (succ l) := λc, empty.elim c | (succ k) -2 := λc, empty.elim c | (succ k) (succ l) := λc, ap succ c @[hott] def succ_ne_zero (n : ℕ₋₂) : succ n ≠ -2 := trunc_index.encode @[hott, instance] protected def has_decidable_eq : Π(n m : ℕ₋₂), decidable (n = m) | -2 -2 := decidable.inl rfl | (n.+1) -2 := decidable.inr trunc_index.encode | -2 (m.+1) := decidable.inr trunc_index.encode | (n.+1) (m.+1) := match has_decidable_eq n m with | decidable.inl xeqy := decidable.inl (ap succ xeqy) | decidable.inr xney := decidable.inr (λh, xney (trunc_index.encode h)) end @[hott] def not_succ_le_minus_two {n : ℕ₋₂} (H : n .+1 ≤ -2) : empty := by cases H @[hott] protected def le_trans {n m k : ℕ₋₂} (H1 : n ≤ m) (H2 : m ≤ k) : n ≤ k := begin induction H2 with k H2 IH, { exact H1}, { exact le.step IH} end @[hott] def le_of_succ_le_succ {n m : ℕ₋₂} (H : n.+1 ≤ m.+1) : n ≤ m := begin cases H with m H', { apply le.tr_refl }, { exact trunc_index.le_trans (le.step (le.tr_refl _)) H'} end @[hott] def not_succ_le_self {n : ℕ₋₂} : ¬n.+1 ≤ n := begin induction n with n IH; intro H, { exact not_succ_le_minus_two H}, { exact IH (le_of_succ_le_succ H)} end @[hott] protected def le_antisymm {n m : ℕ₋₂} (H1 : n ≤ m) (H2 : m ≤ n) : n = m := begin induction H2 with n H2 IH, { refl }, { apply empty.elim, apply @not_succ_le_self n, exact trunc_index.le_trans H1 H2} end @[hott] protected def le_succ {n m : ℕ₋₂} (H1 : n ≤ m) : n ≤ m.+1 := le.step H1 @[hott] protected def self_le_succ (n : ℕ₋₂) : n ≤ n.+1 := le.step (trunc_index.le.tr_refl n) -- the order is total @[hott] protected def le_sum_le (n m : ℕ₋₂) : n ≤ m ⊎ m ≤ n := begin induction m with m IH, { exact inr (minus_two_le _)}, { cases IH with H H, { exact inl (trunc_index.le_succ H)}, { cases H with n' H, { exact inl (trunc_index.self_le_succ _)}, { exact inr (succ_le_succ H)}}} end end trunc_index open trunc_index @[hott, instance, reducible] def linear_weak_order_trunc_index : linear_weak_order trunc_index := linear_weak_order.mk le trunc_index.le.tr_refl @trunc_index.le_trans @trunc_index.le_antisymm trunc_index.le_sum_le namespace trunc_index /- more theorems about truncation indices -/ @[hott] def zero_add (n : ℕ₋₂) : (0 : ℕ₋₂) + n = n := begin cases n with n, refl, cases n with n, refl, induction n with n IH, refl, exact ap succ IH end @[hott] def add_zero (n : ℕ₋₂) : n + (0 : ℕ₋₂) = n := by refl @[hott] def succ_add_nat (n : ℕ₋₂) (m : ℕ) : n.+1 + m = (n + m).+1 := begin induction m with m IH, refl, exact ap succ IH end @[hott] def nat_add_succ (n : ℕ) (m : ℕ₋₂) : ↑n + m.+1 = (n + m).+1 := begin cases m with m, refl, cases m with m, refl, induction m with m IH, refl, exact ap succ IH end @[hott] def add_nat_succ (n : ℕ₋₂) (m : ℕ) : n + (nat.succ m) = (n + m).+1 := by refl @[hott] def nat_succ_add (n : ℕ) (m : ℕ₋₂) : ↑(nat.succ n) + m = (n + m).+1 := begin cases m with m, refl, cases m with m, refl, induction m with m IH, refl, exact ap succ IH end @[hott] def sub_two_add_two (n : ℕ₋₂) : sub_two (add_two n) = n := begin induction n with n IH, { refl }, { exact ap succ IH} end @[hott] def add_two_sub_two (n : ℕ) : add_two (sub_two n) = n := begin induction n with n IH, { refl }, { exact ap nat.succ IH} end @[hott] def of_nat_add_plus_two_of_nat (n m : ℕ) : n +2+ m = of_nat (n + m + 2) := begin induction m with m IH, { refl }, { exact ap succ IH} end @[hott] def of_nat_add_of_nat (n m : ℕ) : of_nat n + of_nat m = of_nat (n + m) := begin induction m with m IH, { refl }, { exact ap succ IH} end @[hott] def succ_add_plus_two (n m : ℕ₋₂) : n.+1 +2+ m = (n +2+ m).+1 := begin induction m with m IH, { refl }, { exact ap succ IH} end @[hott] def add_plus_two_succ (n m : ℕ₋₂) : n +2+ m.+1 = (n +2+ m).+1 := idp @[hott] def add_succ_succ (n m : ℕ₋₂) : n + m.+2 = n +2+ m := idp @[hott] def succ_add_succ (n m : ℕ₋₂) : n.+1 + m.+1 = n +2+ m := begin cases m with m IH, { refl }, { apply succ_add_plus_two} end @[hott] def succ_succ_add (n m : ℕ₋₂) : n.+2 + m = n +2+ m := begin cases m with m IH, { refl }, { exact succ_add_succ _ _ ⬝ succ_add_plus_two _ _} end @[hott] def succ_sub_two (n : ℕ) : (nat.succ n).-2 = n.-2 .+1 := rfl @[hott] def sub_two_succ_succ (n : ℕ) : n.-2.+1.+1 = n := rfl @[hott] def succ_sub_two_succ (n : ℕ) : (nat.succ n).-2.+1 = n := rfl @[hott] def of_nat_add_two (n : ℕ₋₂) : of_nat (add_two n) = n.+2 := begin induction n with n IH, refl, exact ap succ IH end @[hott] def of_nat_le_of_nat {n m : ℕ} (H : n ≤ m) : (of_nat n ≤ of_nat m) := begin induction H with m H IH, { apply le.refl }, { exact trunc_index.le_succ IH} end @[hott] def sub_two_le_sub_two {n m : ℕ} (H : n ≤ m) : n.-2 ≤ m.-2 := begin induction H with m H IH, { apply le.refl }, { exact trunc_index.le_succ IH} end @[hott] def add_two_le_add_two {n m : ℕ₋₂} (H : n ≤ m) : add_two n ≤ add_two m := begin induction H with m H IH, { refl }, { constructor, exact IH}, end @[hott] def le_of_sub_two_le_sub_two {n m : ℕ} (H : n.-2 ≤ m.-2) : n ≤ m := begin rwr [←add_two_sub_two n, ←add_two_sub_two m], exact add_two_le_add_two H, end @[hott] def le_of_of_nat_le_of_nat {n m : ℕ} (H : of_nat n ≤ of_nat m) : n ≤ m := begin apply le_of_sub_two_le_sub_two, exact le_of_succ_le_succ (le_of_succ_le_succ H) end @[hott] protected theorem succ_le_of_not_le {n m : ℕ₋₂} (H : ¬ n ≤ m) : m.+1 ≤ n := begin cases (le.total n m) with H2 H2, { apply empty.elim, exact H H2}, { cases H2 with n' H2', { apply empty.elim, exact H (le.refl _)}, { exact succ_le_succ H2'}} end @[hott, instance] def trunc_index.decidable_le : Π(n m : ℕ₋₂), decidable (n ≤ m) := begin intro n, induction n with n IH; intro m, { left, apply minus_two_le}, cases m with m, { right, apply not_succ_le_minus_two}, cases IH m with H H, { left, apply succ_le_succ H}, right, intro H2, apply H, exact le_of_succ_le_succ H2 end end trunc_index open trunc_index namespace is_trunc variables {A : Type _} {B : Type _} {n : ℕ₋₂} /- closure properties of truncatedness -/ @[hott] theorem is_trunc_is_embedding_closed (f : A → B) [Hf : is_embedding f] [HB : is_trunc n B] (Hn : -1 ≤ n) : is_trunc n A := begin unfreezeI; induction n with n, {apply empty.elim, exact not_succ_le_minus_two Hn}, {apply is_trunc_succ_intro, intros a a', fapply @is_trunc_is_equiv_closed_rev _ _ n (ap f), resetI, apply_instance } end @[hott] theorem is_trunc_is_retraction_closed (f : A → B) [Hf : is_retraction f] (n : ℕ₋₂) [HA : is_trunc n A] : is_trunc n B := begin unfreezeI; induction n with n IH generalizing A B f Hf HA, { induction Hf with g ε, fapply is_contr.mk, { exactI f (center A) }, { intro b, apply concat, { apply (ap f), exact (center_eq (g b)) }, { apply ε }}}, { induction Hf with g ε, apply is_trunc_succ_intro, intros b b', napply @IH (g b = g b') _ (λq, (ε b)⁻¹ ⬝ ap f q ⬝ ε b'), { apply (is_retraction.mk (ap g)), { intro p, induction p, dsimp [ap], apply con.left_inv }}, { apply is_trunc_eq }} end @[hott] def is_embedding_to_fun (A B : Type _) : is_embedding (@to_fun A B) := λf f', is_equiv_ap_to_fun _ _ /- theorems about trunctype -/ @[hott] protected def trunctype.sigma_char (n : ℕ₋₂) : (trunctype.{u} n) ≃ (Σ (A : Type u), is_trunc n A) := begin fapply equiv.MK, { intro A, exact (⟨carrier A, struct A⟩)}, { intro S, exact (trunctype.mk S.1 S.2)}, { intro S, induction S with S1 S2, refl }, { intro A, induction A with A1 A2, refl }, end @[hott] def trunctype_eq_equiv (n : ℕ₋₂) (A B : n-Type) : (A = B) ≃ (carrier A = carrier B) := calc (A = B) ≃ (to_fun (trunctype.sigma_char n) A = to_fun (trunctype.sigma_char n) B) : eq_equiv_fn_eq_of_equiv (trunctype.sigma_char n) A B ... ≃ ((to_fun (trunctype.sigma_char n) A).1 = (to_fun (trunctype.sigma_char n) B).1) : equiv.symm (equiv_subtype _ _) ... ≃ (carrier A = carrier B) : erfl @[hott, instance] theorem is_trunc_trunctype (n : ℕ₋₂) : is_trunc n.+1 (n-Type) := begin apply is_trunc_succ_intro, intros X Y, fapply is_trunc_equiv_closed_rev _ (trunctype_eq_equiv _ _ _), fapply is_trunc_equiv_closed_rev _ (eq_equiv_equiv _ _), induction n, { napply is_contr_of_inhabited_prop, { apply is_trunc_equiv }, { apply equiv_of_is_contr_of_is_contr }}, { apply is_trunc_equiv } end /- univalence for truncated types -/ @[hott] def teq_equiv_equiv {n : ℕ₋₂} {A B : n-Type} : (A = B) ≃ (A ≃ B) := trunctype_eq_equiv n A B ⬝e eq_equiv_equiv A B @[hott] def tua {n : ℕ₋₂} {A B : n-Type} (f : A ≃ B) : A = B := (trunctype_eq_equiv n A B)⁻¹ᶠ (ua f) @[hott] def tua_refl {n : ℕ₋₂} (A : n-Type) : tua (@erfl A) = idp := begin refine ap (trunctype_eq_equiv n A A)⁻¹ᶠ (ua_refl A) ⬝ _, refine ap (eq_of_fn_eq_fn _) _ ⬝ eq_of_fn_eq_fn'_idp _ _, apply ap (dpair_eq_dpair idp) (is_prop.elim _ idpo), apply is_trunc.is_trunc_pathover end @[hott] def tua_trans {n : ℕ₋₂} {A B C : n-Type} (f : A ≃ B) (g : B ≃ C) : tua (f ⬝e g) = tua f ⬝ tua g := begin refine ap (trunctype_eq_equiv n A C)⁻¹ᶠ (ua_trans f g) ⬝ _, refine ap (eq_of_fn_eq_fn _) _ ⬝ eq_of_fn_eq_fn'_con _ _ _, refine _ ⬝ dpair_eq_dpair_con _ _ _ _, apply ap (dpair_eq_dpair _), apply is_prop.elim end @[hott] def tua_symm {n : ℕ₋₂} {A B : n-Type} (f : A ≃ B) : tua f⁻¹ᵉ = (tua f)⁻¹ := begin apply eq_inv_of_con_eq_idp', refine (tua_trans _ _)⁻¹ ⬝ _, refine ap tua _ ⬝ (tua_refl _), apply equiv_eq, exact to_right_inv f end @[hott] def tcast {n : ℕ₋₂} {A B : n-Type} (p : A = B) (a : A) : B := cast (ap trunctype.carrier p) a @[hott] def ptcast {n : ℕ₋₂} {A B : n-Type*} (p : A = B) : ↑A →* ↑B := pcast (ap ptrunctype.to_pType p) @[hott] theorem tcast_tua_fn {n : ℕ₋₂} {A B : n-Type} (f : A ≃ B) : tcast (tua f) = to_fun f := begin cases A with A HA, cases B with B HB, dsimp at f, hinduction f using rec_on_ua_idp, have : HA = HB, from is_prop.elim _ _, cases this, exact ap tcast (tua_refl _) end /- theorems about decidable equality and axiom K -/ @[hott] theorem is_set_of_axiom_K {A : Type _} (K : Π{a : A} (p : a = a), p = idp) : is_set A := is_set.mk _ (λa b p q, begin induction q, apply K end) @[hott] theorem is_set_of_relation {A : Type u} (R : A → A → Type u) (mere : Π(a b : A), is_prop (R a b)) (refl : Π(a : A), R a a) (imp : Π{a b : A}, R a b → a = b) : is_set A := is_set_of_axiom_K (λa p, have H2 : transport (λx, R a x → a = x) p (@imp a a) = @imp a a, from apdt (@imp a) p, have H3 : Π(r : R a a), transport (λx, a = x) p (imp r) = imp (transport (λx, R a x) p r), from to_fun ((heq_pi _ _ _)⁻¹ᵉ) H2, have H4 : imp (refl a) ⬝ p = imp (refl a), from calc imp (refl a) ⬝ p = transport (λx, a = x) p (imp (refl a)) : eq_transport_r _ _ ... = imp (transport (λx, R a x) p (refl a)) : H3 _ ... = imp (refl a) : ap imp (is_prop.elim _ _), cancel_left (imp (refl a)) H4) @[hott] def relation_equiv_eq {A : Type _} (R : A → A → Type _) (mere : Π(a b : A), is_prop (R a b)) (refl : Π(a : A), R a a) (imp : Π{a b : A}, R a b → a = b) (a b : A) : R a b ≃ a = b := have is_set A, from is_set_of_relation R mere refl @imp, by exactI equiv_of_is_prop imp (λp, transport (R a) p (refl a)) local attribute [reducible] not @[hott] theorem is_set_of_double_neg_elim {A : Type _} (H : Π(a b : A), ¬¬a = b → a = b) : is_set A := is_set_of_relation (λa b, ¬¬a = b) (by apply_instance) (λa n, n idp) H section open decidable --this is proven differently in init.hedberg @[hott] theorem is_set_of_decidable_eq (A : Type _) [H : decidable_eq A] : is_set A := is_set_of_double_neg_elim (λa b, decidable.by_contradiction) end @[hott] theorem is_trunc_of_axiom_K_of_le {A : Type _} {n : ℕ₋₂} (H : -1 ≤ n) (K : Π(a : A), is_trunc n (a = a)) : is_trunc (n.+1) A := @is_trunc_succ_intro _ _ (λa b, is_trunc_of_imp_is_trunc_of_le H begin intro p; induction p; apply K end) @[hott] theorem is_trunc_succ_of_is_trunc_loop (Hn : -1 ≤ n) (Hp : Π(a : A), is_trunc n (a = a)) : is_trunc (n.+1) A := begin apply is_trunc_succ_intro, intros a a', apply is_trunc_of_imp_is_trunc_of_le Hn, intro p, induction p, apply Hp end @[hott] theorem is_prop_iff_is_contr {A : Type _} (a : A) : is_prop A ↔ is_contr A := iff.intro (λH, by exactI is_contr.mk a (is_prop.elim a)) (by introI; apply_instance) @[hott] theorem is_trunc_succ_iff_is_trunc_loop (A : Type _) (Hn : -1 ≤ n) : is_trunc (n.+1) A ↔ Π(a : A), is_trunc n (a = a) := iff.intro (by introI; apply_instance) (is_trunc_succ_of_is_trunc_loop Hn) @[hott] theorem is_trunc_iff_is_contr_loopn_succ (n : ℕ) (A : Type _) : is_trunc n A ↔ Π(a : A), is_contr (Ω[n+1](pointed.Mk a)) := begin induction n with n IH generalizing A, { dsimp [loopn], transitivity _, { apply is_trunc_succ_iff_is_trunc_loop, apply le.refl }, { apply pi_iff_pi, intro a, apply is_prop_iff_is_contr, refl }}, { dsimp [loopn], transitivity _, { apply @is_trunc_succ_iff_is_trunc_loop @n, apply minus_one_le_succ }, apply pi_iff_pi, intro a, transitivity _, apply IH, transitivity _, apply pi_iff_pi, intro p, rwr [@loopn_space_loop_irrel (pointed.MK A a) n p], exact ⟨λf, f idp, λH _, H⟩ } end @[hott] theorem is_trunc_iff_is_contr_loopn (n : ℕ) (A : Type _) : is_trunc (n.-2.+1) A ↔ (Π(a : A), is_contr (Ω[n](pointed.Mk a))) := begin induction n with n, { dsimp [sub_two,loopn], apply iff.intro, intros H a, exactI is_contr_of_inhabited_prop a, intro H, apply is_prop_of_imp_is_contr, exact H}, { applyI is_trunc_iff_is_contr_loopn_succ }, end -- rename to is_contr_loopn_of_is_trunc @[hott] theorem is_contr_loop_of_is_trunc (n : ℕ) (A : Type*) [H : is_trunc (n.-2.+1) A] : is_contr (Ω[n] A) := begin unfreezeI; induction A, apply iff.mp (is_trunc_iff_is_contr_loopn _ _) H end -- rename to is_trunc_loopn_of_is_trunc @[hott] theorem is_trunc_loop_of_is_trunc (n : ℕ₋₂) (k : ℕ) (A : Type*) [H : is_trunc n A] : is_trunc n (Ω[k] A) := begin induction k with k IH, { exact H }, { applyI is_trunc_eq } end @[hott] def is_trunc_loopn (k : ℕ₋₂) (n : ℕ) (A : Type*) [H : is_trunc (k+n) A] : is_trunc k (Ω[n] A) := begin unfreezeI; induction n with n IH generalizing k H, exact H, napply is_trunc_eq, napply IH, rwr [succ_add_nat], rwr [add_nat_succ] at H, exact H end @[hott] def is_set_loopn (n : ℕ) (A : Type*) [is_trunc n A] : is_set (Ω[n] A) := have is_trunc (0 + ↑n) A, by rwr [trunc_index.zero_add]; apply_instance, by exactI is_trunc_loopn 0 n A @[hott] def pequiv_punit_of_is_contr (A : Type*) (H : is_contr A) : A ≃* unit*.to_pType := pequiv_of_equiv (equiv_unit_of_is_contr A) (@is_prop.elim unit _ _ _) @[hott] def pequiv_punit_of_is_contr' (A : Type _) (H : is_contr A) : pointed.MK A (center A) ≃* unit*.to_pType := pequiv_punit_of_is_contr (pointed.MK A (center A)) H @[hott] def is_trunc_is_contr_fiber (n : ℕ₋₂) {A B : Type _} (f : A → B) (b : B) [is_trunc n A] [is_trunc n B] : is_trunc n (is_contr (fiber f b)) := begin unfreezeI; cases n, { applyI is_contr_of_inhabited_prop, napply is_contr_fun_of_is_equiv, apply is_equiv_of_is_contr }, { applyI is_trunc_succ_of_is_prop } end end is_trunc open is_trunc namespace trunc variables {n : ℕ₋₂} {A : Type u} {B : Type _} {a₁ a₂ a₃ a₄ : A} @[hott] def trunc_functor2 {n : ℕ₋₂} {A B C : Type _} (f : A → B → C) (x : trunc n A) (y : trunc n B) : trunc n C := by hinduction x with a; hinduction y with b; exact tr (f a b) @[hott] def tconcat (p : trunc n (a₁ = a₂)) (q : trunc n (a₂ = a₃)) : trunc n (a₁ = a₃) := trunc_functor2 concat p q @[hott] def tinverse (p : trunc n (a₁ = a₂)) : trunc n (a₂ = a₁) := trunc_functor _ inverse p @[hott, reducible] def tidp : trunc n (a₁ = a₁) := tr idp @[hott] def tassoc (p : trunc n (a₁ = a₂)) (q : trunc n (a₂ = a₃)) (r : trunc n (a₃ = a₄)) : tconcat (tconcat p q) r = tconcat p (tconcat q r) := by hinduction p; hinduction q; hinduction r; exact ap tr (con.assoc _ _ _) @[hott] def tidp_tcon (p : trunc n (a₁ = a₂)) : tconcat tidp p = p := by hinduction p; exact ap tr (idp_con _) @[hott] def tcon_tidp (p : trunc n (a₁ = a₂)) : tconcat p tidp = p := by hinduction p; refl @[hott] def left_tinv (p : trunc n (a₁ = a₂)) : tconcat (tinverse p) p = tidp := by hinduction p; exact ap tr (con.left_inv _) @[hott] def right_tinv (p : trunc n (a₁ = a₂)) : tconcat p (tinverse p) = tidp := by hinduction p; exact ap tr (con.right_inv _) @[hott] def tap (f : A → B) (p : trunc n (a₁ = a₂)) : trunc n (f a₁ = f a₂) := trunc_functor _ (ap f) p @[hott] def tap_tidp (f : A → B) : tap f (@tidp n A a₁) = tidp := idp @[hott] def tap_tcon (f : A → B) (p : trunc n (a₁ = a₂)) (q : trunc n (a₂ = a₃)) : tap f (tconcat p q) = tconcat (tap f p) (tap f q) := by hinduction p; hinduction q; exact ap tr (ap_con _ _ _) /- characterization of equality in truncated types -/ @[hott] protected def code (n : ℕ₋₂) (aa aa' : trunc n.+1 A) : trunctype.{u} n := by hinduction aa with a; hinduction aa' with a'; exact trunctype.mk' n (trunc n (a = a')) @[hott] protected def encode {n : ℕ₋₂} {aa aa' : trunc n.+1 A} : aa = aa' → trunc.code n aa aa' := begin intro p, induction p, hinduction aa with a, exact tr idp end @[hott] protected def decode {n : ℕ₋₂} (aa aa' : trunc n.+1 A) : trunc.code n aa aa' → aa = aa' := begin hinduction aa' with a', hinduction aa with a, dsimp [trunc.code, trunc.rec_on], intro x, hinduction x with p, exact ap tr p, end @[hott] def trunc_eq_equiv (n : ℕ₋₂) (aa aa' : trunc n.+1 A) : aa = aa' ≃ trunc.code n aa aa' := begin fapply equiv.MK, { apply trunc.encode}, { apply trunc.decode}, { hinduction aa', hinduction aa, intro x, hinduction x with p, induction p, refl }, { intro p, hinduction p, hinduction aa, refl }, end @[hott] def tr_eq_tr_equiv (n : ℕ₋₂) (a a' : A) : (tr a = tr a' :> trunc n.+1 A) ≃ trunc n (a = a') := trunc_eq_equiv _ _ _ @[hott] def trunc_eq {n : ℕ₋₂} {a a' : A} (p : trunc n (a = a')) :tr a = tr a' :> trunc n.+1 A := (tr_eq_tr_equiv _ _ _)⁻¹ᵉ.to_fun p @[hott] def code_mul {n : ℕ₋₂} {aa₁ aa₂ aa₃ : trunc n.+1 A} (g : trunc.code n aa₁ aa₂) (h : trunc.code n aa₂ aa₃) : trunc.code n aa₁ aa₃ := begin hinduction aa₁ with a₁, hinduction aa₂ with a₂, hinduction aa₃ with a₃, apply tconcat g h, end /- encode preserves concatenation -/ @[hott] def encode_con' {n : ℕ₋₂} {aa₁ aa₂ aa₃ : trunc n.+1 A} (p : aa₁ = aa₂) (q : aa₂ = aa₃) : trunc.encode (p ⬝ q) = code_mul (trunc.encode p) (trunc.encode q) := begin induction p, induction q, hinduction aa₁ with a₁, refl end @[hott] def encode_con {n : ℕ₋₂} {a₁ a₂ a₃ : A} (p : tr a₁ = tr a₂ :> trunc (n.+1) A) (q : tr a₂ = tr a₃ :> trunc (n.+1) A) : trunc.encode (p ⬝ q) = tconcat (trunc.encode p) (trunc.encode q) := encode_con' p q /- the principle of unique choice -/ @[hott] def unique_choice {P : A → Type _} [H : Πa, is_prop (P a)] (f : Πa, ∥ P a ∥) (a : A) : P a := (trunc_equiv _ _).to_fun (f a) /- transport over a truncated family -/ @[hott] def trunc_transport {a a' : A} {P : A → Type _} (p : a = a') (n : ℕ₋₂) (x : P a) : transport (λa, trunc n (P a)) p (tr x) = tr (p ▸ x) := by induction p; refl /- pathover over a truncated family -/ @[hott] def trunc_pathover {A : Type _} {B : A → Type _} {n : ℕ₋₂} {a a' : A} {p : a = a'} {b : B a} {b' : B a'} (q : b =[p] b') : @tr n _ b =[p; λa, trunc n (B a)] @tr n _ b' := by induction q; constructor /- truncations preserve truncatedness -/ @[hott, instance, priority 500] def is_trunc_trunc_of_is_trunc (A : Type _) (n m : ℕ₋₂) [H : is_trunc n A] : is_trunc n (trunc m A) := begin unfreezeI; induction n with n IH generalizing A m H, { napply is_contr_equiv_closed, { symmetry, napply trunc_equiv, applyI (@is_trunc_of_le _ -2), apply minus_two_le}, apply_instance }, { induction m with m, { apply (@is_trunc_of_le _ -2), apply minus_two_le}, { apply is_trunc_succ_intro, intros aa aa', hinduction aa, hinduction aa', apply is_trunc_equiv_closed_rev, { apply tr_eq_tr_equiv }, { apply IH }}} end /- equivalences between truncated types (see also hit.trunc) -/ @[hott] def trunc_trunc_equiv_left (A : Type _) {n m : ℕ₋₂} (H : n ≤ m) : trunc n (trunc m A) ≃ trunc n A := begin haveI H2 := is_trunc_of_le (trunc n A) H, fapply equiv.MK, { intro x, hinduction x with x, hinduction x with x, exact tr x }, { intro x, hinduction x with x, exact tr (tr x) }, { intro x, hinduction x with x, refl }, { intro x, hinduction x with x, hinduction x with x, refl } end @[hott] def trunc_trunc_equiv_right (A : Type _) {n m : ℕ₋₂} (H : n ≤ m) : trunc m (trunc n A) ≃ trunc n A := begin napply trunc_equiv, exact is_trunc_of_le _ H, end @[hott] def trunc_equiv_trunc_of_le {n m : ℕ₋₂} {A B : Type _} (H : n ≤ m) (f : trunc m A ≃ trunc m B) : trunc n A ≃ trunc n B := (trunc_trunc_equiv_left A H)⁻¹ᵉ ⬝e trunc_equiv_trunc n f ⬝e trunc_trunc_equiv_left B H @[hott] def trunc_trunc_equiv_trunc_trunc (n m : ℕ₋₂) (A : Type _) : trunc n (trunc m A) ≃ trunc m (trunc n A) := begin fapply equiv.MK; intro x, { hinduction x with x, hinduction x with x, exact tr (tr x) }, { hinduction x with x, hinduction x with x, exact tr (tr x) }, { hinduction x with x, hinduction x with x, refl }, { hinduction x with x, hinduction x with x, refl } end @[hott] theorem is_trunc_trunc_of_le (A : Type _) (n : ℕ₋₂) {m k : ℕ₋₂} (H : m ≤ k) [is_trunc n (trunc k A)] : is_trunc n (trunc m A) := begin apply is_trunc_equiv_closed, { apply trunc_trunc_equiv_left, exact H }, apply_instance end @[hott] def trunc_functor_homotopy {X Y : Type _} (n : ℕ₋₂) {f g : X → Y} (p : f ~ g) (x : trunc n X) : trunc_functor n f x = trunc_functor n g x := begin hinduction x with x, exact ap tr (p x) end @[hott] def trunc_functor_homotopy_of_le {n k : ℕ₋₂} {A B : Type _} (f : A → B) (H : n ≤ k) : to_fun (trunc_trunc_equiv_left B H) ∘ trunc_functor n (trunc_functor k f) ∘ to_fun (trunc_trunc_equiv_left A H)⁻¹ᵉ ~ trunc_functor n f := begin intro x, hinduction x with x, refl end @[hott] def is_equiv_trunc_functor_of_le {n k : ℕ₋₂} {A B : Type _} (f : A → B) (H : n ≤ k) [Hf : is_equiv (trunc_functor k f)] : is_equiv (trunc_functor n f) := is_equiv_of_equiv_of_homotopy (trunc_equiv_trunc_of_le H (equiv.mk (trunc_functor k f) Hf)) (trunc_functor_homotopy_of_le f H) /- trunc_functor preserves surjectivity -/ --set_option trace.hinduction true @[hott] def is_surjective_trunc_functor {A B : Type _} (n : ℕ₋₂) (f : A → B) [H : is_surjective f] : is_surjective (trunc_functor n f) := begin cases n with n; intro b, { exact tr (fiber.mk (center _) (is_prop.elim _ _)) }, { haveI : Πb, is_trunc n.+1 (image (trunc_functor n.+1 f) b), { intro b, exact is_trunc_of_le _ (minus_one_le_succ _) }, unfreezeI; hinduction b with b, hinduction H b with x p, induction p with a p, exact tr (fiber.mk (tr a) (ap tr p)) } end /- truncation of pointed types -/ @[hott] def ptrunc (n : ℕ₋₂) (X : Type*) : Type* := ⟨trunc n X, tr pt⟩ @[hott] instance is_trunc_ptrunc (n : ℕ₋₂) (X : Type*) : is_trunc n (ptrunc n X) := is_trunc_trunc n X @[hott] def pttrunc (n : ℕ₋₂) (X : Type*) : n-Type* := ptrunctype.mk (trunc n X) (is_trunc_trunc n X) (tr pt) /- pointed maps involving ptrunc -/ @[hott] def ptrunc_functor {X Y : Type*} (n : ℕ₋₂) (f : X →* Y) : ptrunc n X →* ptrunc n Y := pmap.mk (trunc_functor n f) (ap tr (respect_pt f)) @[hott] def ptr (n : ℕ₋₂) (A : Type*) : A →* ptrunc n A := pmap.mk tr idp @[hott] def puntrunc (n : ℕ₋₂) (A : Type*) [is_trunc n A] : ptrunc n A →* A := pmap.mk untrunc_of_is_trunc idp @[hott] def ptrunc.elim (n : ℕ₋₂) {X Y : Type*} [is_trunc n Y] (f : X →* Y) : ptrunc n X →* Y := pmap.mk (trunc.elim f) (respect_pt f) /- pointed equivalences involving ptrunc -/ @[hott] def ptrunc_pequiv_ptrunc (n : ℕ₋₂) {X Y : Type*} (H : X ≃* Y) : ptrunc n X ≃* ptrunc n Y := pequiv_of_equiv (trunc_equiv_trunc n (equiv_of_pequiv H)) (ap tr (respect_pt H.to_pmap)) @[hott] def ptrunc_pequiv (n : ℕ₋₂) (X : Type*) [H : is_trunc n X] : ptrunc n X ≃* X := pequiv_of_equiv (trunc_equiv n X) idp @[hott] def ptrunc_ptrunc_pequiv_left (A : Type*) {n m : ℕ₋₂} (H : n ≤ m) : ptrunc n (ptrunc m A) ≃* ptrunc n A := pequiv_of_equiv (trunc_trunc_equiv_left A H) idp @[hott] def ptrunc_ptrunc_pequiv_right (A : Type*) {n m : ℕ₋₂} (H : n ≤ m) : ptrunc m (ptrunc n A) ≃* ptrunc n A := pequiv_of_equiv (trunc_trunc_equiv_right A H) idp @[hott] def ptrunc_pequiv_ptrunc_of_le {n m : ℕ₋₂} {A B : Type*} (H : n ≤ m) (f : ptrunc m A ≃* ptrunc m B) : ptrunc n A ≃* ptrunc n B := (ptrunc_ptrunc_pequiv_left A H)⁻¹ᵉ* ⬝e* ptrunc_pequiv_ptrunc n f ⬝e* ptrunc_ptrunc_pequiv_left B H @[hott] def ptrunc_ptrunc_pequiv_ptrunc_ptrunc (n m : ℕ₋₂) (A : Type*) : ptrunc n (ptrunc m A) ≃* ptrunc m (ptrunc n A) := pequiv_of_equiv (trunc_trunc_equiv_trunc_trunc n m A) idp @[hott] def loop_ptrunc_pequiv (n : ℕ₋₂) (A : Type*) : Ω (ptrunc (n+1) A) ≃* ptrunc n (Ω A) := pequiv_of_equiv (tr_eq_tr_equiv _ _ _) idp @[hott] def loop_ptrunc_pequiv_con {n : ℕ₋₂} {A : Type*} (p q : Ω (ptrunc (n+1) A)) : (loop_ptrunc_pequiv n A).to_pmap (p ⬝ q) = tconcat ((loop_ptrunc_pequiv n A).to_pmap p) ((loop_ptrunc_pequiv n A).to_pmap q) := encode_con p q @[hott] def loopn_ptrunc_pequiv (n : ℕ₋₂) (k : ℕ) (A : Type*) : Ω[k] (ptrunc (n+k) A) ≃* ptrunc n (Ω[k] A) := begin induction k with k IH generalizing n, { refl }, { refine _ ⬝e* loop_ptrunc_pequiv n (Ω[k] A), change Ω (Ω[k] (ptrunc (n + succ k) A)) ≃* Ω (ptrunc (n + 1) (Ω[k] A)), apply loop_pequiv_loop, refine _ ⬝e* IH (n.+1), exact loopn_pequiv_loopn k (pequiv_of_eq (ap (λn, ptrunc n A) (succ_add_nat _ _)⁻¹ᵖ)) } end @[hott] def loopn_ptrunc_pequiv_con {n : ℕ₋₂} {k : ℕ} {A : Type*} (p q : Ω[succ k] (ptrunc (n+nat.succ k) A)) : (loopn_ptrunc_pequiv n (succ k) A).to_pmap (p ⬝ q) = tconcat ((loopn_ptrunc_pequiv n (succ k) A).to_pmap p) ((loopn_ptrunc_pequiv n (succ k) A).to_pmap q) := begin refine _ ⬝ loop_ptrunc_pequiv_con _ _, exact ap (loop_ptrunc_pequiv _ _).to_fun (loop_pequiv_loop_con _ _ _) end @[hott] def loopn_ptrunc_pequiv_inv_con {n : ℕ₋₂} {k : ℕ} {A : Type*} (p q : ptrunc n (Ω[succ k] A)) : (loopn_ptrunc_pequiv n (succ k) A)⁻¹ᵉ*.to_fun (tconcat p q) = (loopn_ptrunc_pequiv n (succ k) A)⁻¹ᵉ*.to_fun p ⬝ (loopn_ptrunc_pequiv n (succ k) A)⁻¹ᵉ*.to_fun q := equiv.inv_preserve_binary (loopn_ptrunc_pequiv n (succ k) A).to_equiv concat tconcat (@loopn_ptrunc_pequiv_con n k A) p q /- pointed homotopies involving ptrunc -/ @[hott] def ptrunc_functor_pcompose {X Y Z : Type*} (n : ℕ₋₂) (g : Y →* Z) (f : X →* Y) : ptrunc_functor n (g ∘* f) ~* ptrunc_functor n g ∘* ptrunc_functor n f := begin fapply phomotopy.mk, { exact trunc_functor_compose n g f }, { refine idp_con _ ⬝ _, refine whisker_right _ (ap_compose' _ _ _) ⬝ _, refine whisker_right _ (ap_compose tr g _) ⬝ _, exact (ap_con _ _ _)⁻¹ }, end @[hott] def ptrunc_functor_pid (X : Type*) (n : ℕ₋₂) : ptrunc_functor n (pid X) ~* pid (ptrunc n X) := begin fapply phomotopy.mk, { apply trunc_functor_id}, { refl }, end @[hott] def ptrunc_functor_pcast {X Y : Type*} (n : ℕ₋₂) (p : X = Y) : ptrunc_functor n (pcast p) ~* pcast (ap (ptrunc n) p) := begin fapply phomotopy.mk, { intro x, refine trunc_functor_cast _ _ _ ⬝ _, refine ap010 (@hott.eq.cast (ptrunc n X) (@ptrunc n Y)) _ x, refine ap_compose' _ _ _ ⬝ _ ⬝ ap_compose _ _ _, refl }, { induction p, refl }, end @[hott] def ptrunc_functor_phomotopy {X Y : Type*} (n : ℕ₋₂) {f g : X →* Y} (p : f ~* g) : ptrunc_functor n f ~* ptrunc_functor n g := begin fapply phomotopy.mk, { exact trunc_functor_homotopy n p}, { refine (ap_con _ _ _)⁻¹ ⬝ _, exact ap02 tr (to_homotopy_pt _)}, end @[hott] def pcast_ptrunc (n : ℕ₋₂) {A B : Type*} (p : A = B) : pcast (ap (ptrunc n) p) ~* ptrunc_functor n (pcast p) := begin fapply phomotopy.mk, { intro a, induction p, exact (trunc_functor_id _ _ _)⁻¹ }, { induction p, refl } end @[hott] def ptrunc_elim_ptr (n : ℕ₋₂) {X Y : Type*} [is_trunc n Y] (f : X →* Y) : ptrunc.elim n f ∘* ptr n X ~* f := begin fapply phomotopy.mk, { refl }, { refl } end @[hott] def ptrunc_elim_phomotopy (n : ℕ₋₂) {X Y : Type*} [is_trunc n Y] {f g : X →* Y} (H : f ~* g) : ptrunc.elim n f ~* ptrunc.elim n g := begin fapply phomotopy.mk, { intro x, hinduction x with x, exact H x }, { exact to_homotopy_pt H } end @[hott] def ap1_ptrunc_functor (n : ℕ₋₂) {A B : Type*} (f : A →* B) : Ω→ (ptrunc_functor (n.+1) f) ∘* (loop_ptrunc_pequiv n A)⁻¹ᵉ*.to_pmap ~* (loop_ptrunc_pequiv n B)⁻¹ᵉ*.to_pmap ∘* ptrunc_functor n (Ω→ f) := begin fapply phomotopy.mk, { intro p, hinduction p with p, refine ((ap_inv _ _)⁻¹ ◾ (ap_compose _ _ _)⁻¹ ◾ idp) ⬝ _ ⬝ (ap_con _ _ _)⁻¹ᵖ, apply whisker_right, refine _ ⬝ (ap_con _ _ _)⁻¹ᵖ, exact whisker_left _ (ap_compose' _ _ _)⁻¹ᵖ }, { induction B with B b, induction f with f p, dsimp at f, dsimp at p, induction p, refl } end @[hott] def ap1_ptrunc_elim (n : ℕ₋₂) {A B : Type*} (f : A →* B) [is_trunc (n.+1) B] : Ω→ (ptrunc.elim (n.+1) f) ∘* (loop_ptrunc_pequiv n A)⁻¹ᵉ*.to_pmap ~* ptrunc.elim n (Ω→ f) := begin fapply phomotopy.mk, { intro p, hinduction p with p, exact idp ◾ (ap_compose _ _ _)⁻¹ ◾ idp }, { refl } end @[hott] def ap1_ptr (n : ℕ₋₂) (A : Type*) : Ω→ (ptr (n.+1) A) ~* (loop_ptrunc_pequiv n A)⁻¹ᵉ*.to_pmap ∘* ptr n (Ω A) := begin fapply phomotopy.mk, { intro p, apply idp_con }, { refl } end @[hott] def ptrunc_elim_ptrunc_functor (n : ℕ₋₂) {A B C : Type*} (g : B →* C) (f : A →* B) [is_trunc n C] : ptrunc.elim n g ∘* ptrunc_functor n f ~* ptrunc.elim n (g ∘* f) := begin fapply phomotopy.mk, { intro x, hinduction x with a, refl }, { refine idp_con _ ⬝ whisker_right _ (ap_compose' _ _ _)⁻¹ᵖ }, end end trunc open trunc /- The truncated encode-decode method -/ namespace eq @[hott] def truncated_encode {k : ℕ₋₂} {A : Type _} {a₀ a : A} {code : A → Type _} [Πa, is_trunc k (code a)] (c₀ : code a₀) (p : trunc k (a₀ = a)) : code a := begin hinduction p with p, exact transport code p c₀ end @[hott] def truncated_encode_decode_method {k : ℕ₋₂} {A : Type _} (a₀ a : A) (code : A → Type _) [Πa, is_trunc k (code a)] (c₀ : code a₀) (decode : Π(a : A) (c : code a), trunc k (a₀ = a)) (encode_decode : Π(a : A) (c : code a), truncated_encode c₀ (decode a c) = c) (decode_encode : decode a₀ c₀ = tr idp) : trunc k (a₀ = a) ≃ code a := begin fapply equiv.MK, { exact truncated_encode c₀}, { apply decode}, { intro c, apply encode_decode}, { intro p, hinduction p with p, induction p, exact decode_encode}, end end eq /- some consequences for properties about functions (surjectivity etc.) -/ namespace function variables {A : Type _} {B : Type _} @[hott, instance] def is_surjective_of_is_equiv (f : A → B) [H : is_equiv f] : is_surjective f := λb, begin dsimp [image, image'], apply center end @[hott] def is_equiv_equiv_is_embedding_times_is_surjective (f : A → B) : is_equiv f ≃ (is_embedding f × is_surjective f) := equiv_of_is_prop (λH, (by resetI; apply_instance, by resetI; apply_instance)) (λP, prod.rec_on P (λH₁ H₂, by exactI is_equiv_of_is_surjective_of_is_embedding _)) /- @[hott] theorem 8.8.1: A function is an equivalence if it's an embedding and it's action on sets is an surjection -/ @[hott] def is_equiv_of_is_surjective_trunc_of_is_embedding {A B : Type _} (f : A → B) [H : is_embedding f] [H' : is_surjective (trunc_functor 0 f)] : is_equiv f := have is_surjective f, begin intro b, hinduction H' (tr b) with x p, hinduction p with a p, hinduction a with a, dsimp [trunc_functor] at p, hinduction ((tr_eq_tr_equiv _ _ _).to_fun p) with x q, exact image.mk a q end, by exactI is_equiv_of_is_surjective_of_is_embedding f /- Corollary 8.8.2: A function f is an equivalence if Ωf and trunc_functor 0 f are equivalences -/ @[hott] def is_equiv_of_is_equiv_ap1_of_is_equiv_trunc {A B : Type _} (f : A → B) [H : Πa, is_equiv (ap1 (pmap_of_map f a))] [H' : is_equiv (trunc_functor 0 f)] : is_equiv f := have is_embedding f, begin intros a a', apply is_equiv_of_imp_is_equiv, intro p, have q := ap (@tr 0 _) p, have r := @eq_of_fn_eq_fn' _ _ (trunc_functor 0 f) _ (tr a) (tr a') q, hinduction (tr_eq_tr_equiv _ _ _).to_fun r with x s, induction s, apply is_equiv.homotopy_closed (ap1 (pmap_of_map f a)), intro p, apply idp_con end, by exactI is_equiv_of_is_surjective_trunc_of_is_embedding f -- Whitehead's principle itself is in homotopy.homotopy_group, since it needs the @[hott] def of -- a homotopy group. end function end hott
2bfff881c1c3bb9bd9ec4f95f3a1c6dd17467bcb
453dcd7c0d1ef170b0843a81d7d8caedc9741dce
/analysis/real.lean
b5a76ffc302a5139eddc72fef5b4a6275134981b
[ "Apache-2.0" ]
permissive
amswerdlow/mathlib
9af77a1f08486d8fa059448ae2d97795bd12ec0c
27f96e30b9c9bf518341705c99d641c38638dfd0
refs/heads/master
1,585,200,953,598
1,534,275,532,000
1,534,275,532,000
144,564,700
0
0
null
1,534,156,197,000
1,534,156,197,000
null
UTF-8
Lean
false
false
15,889
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 logic.function analysis.metric_space noncomputable theory open classical set lattice filter topological_space local attribute [instance] prop_decidable 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 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 theorem uniform_continuous_of_rat : uniform_continuous (coe : ℚ → ℝ) := uniform_continuous_vmap theorem uniform_embedding_of_rat : uniform_embedding (coe : ℚ → ℝ) := metric_space.induced_uniform_embedding _ _ _ 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_metric.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.embedding theorem continuous_of_rat : continuous (coe : ℚ → ℝ) := uniform_continuous_of_rat.continuous theorem real.uniform_continuous_add : uniform_continuous (λp : ℝ × ℝ, p.1 + p.2) := uniform_continuous_of_metric.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.uniform_continuous_iff.2 $ by simp [(∘)]; exact ((uniform_continuous_fst.comp uniform_continuous_of_rat).prod_mk (uniform_continuous_snd.comp uniform_continuous_of_rat)).comp real.uniform_continuous_add theorem real.uniform_continuous_neg : uniform_continuous (@has_neg.neg ℝ _) := uniform_continuous_of_metric.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 ℚ _) := uniform_continuous_of_metric.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 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⁻¹) := uniform_continuous_of_metric.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 : ℝ → ℝ) := uniform_continuous_of_metric.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 : ℚ → ℚ) := uniform_continuous_of_metric.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⁻¹) (nhds r) (nhds 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_tendsto.mpr $ assume ⟨r, hr⟩, (continuous_iff_tendsto.mp continuous_subtype_val _).comp (real.tendsto_inv hr) 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 (continuous_subtype_mk _ hf).comp real.continuous_inv' lemma real.uniform_continuous_mul_const {x : ℝ} : uniform_continuous ((*) x) := uniform_continuous_of_metric.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₂ : ℝ} (r₁0 : 0 < r₁) (r₂0 : 0 < r₂) (H : ∀ x ∈ s, abs (x : ℝ × ℝ).1 < r₁ ∧ abs x.2 < r₂) : uniform_continuous (λp:s, p.1.1 * p.1.2) := uniform_continuous_of_metric.2 $ λ ε ε0, let ⟨δ, δ0, Hδ⟩ := rat_mul_continuous_lemma abs ε0 r₁0 r₂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₂⟩ lemma real.continuous_mul : continuous (λp : ℝ × ℝ, p.1 * p.2) := continuous_iff_tendsto.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}) (lt_of_le_of_lt (abs_nonneg _) (lt_add_one _)) (lt_of_le_of_lt (abs_nonneg _) (lt_add_one _)) (λ x, id)) (mem_nhds_sets (is_open_prod (real.continuous_abs _ $ is_open_gt' _) (real.continuous_abs _ $ is_open_gt' _)) ⟨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 lemma rat.continuous_mul : continuous (λp : ℚ × ℚ, p.1 * p.2) := embedding_of_rat.continuous_iff.2 $ by simp [(∘)]; exact ((continuous_fst.comp continuous_of_rat).prod_mk (continuous_snd.comp continuous_of_rat)).comp real.continuous_mul 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) := totally_bounded_of_metric.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 have := (cauchy_of_metric.1 cf).2, let S : ∀ ε:{ε:ℝ//ε>0}, {t : f.sets // ∀ x y ∈ t.1, dist x y < ε} := λ ε, classical.choice (let ⟨t, tf, h⟩ := (cauchy_of_metric.1 cf).2 ε ε.2 in ⟨⟨⟨t, tf⟩, h⟩⟩), let g : ℕ → {ε:ℝ//ε>0} := λ n, ⟨n.to_pnat'⁻¹, inv_pos (nat.cast_pos.2 n.to_pnat'.pos)⟩, 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 F : ∀ n : ℕ, {t : f.sets // ∀ x y ∈ t.1, dist x y < g n}, { refine λ n, ⟨⟨_, Inter_mem_sets (finite_le_nat n) (λ i _, (S (g i)).1.2)⟩, _⟩, have : (⋂ i ∈ {i : ℕ | i ≤ n}, (S (g i)).1.1) ⊆ S (g n) := bInter_subset_of_mem (le_refl n), exact λ x y xs ys, (S (g n)).2 _ _ (this xs) (this ys) }, let G : ∀ n : ℕ, F n, { refine λ n, classical.choice _, cases inhabited_of_mem_sets cf.1 (F n).1.2 with x xS, exact ⟨⟨x, xS⟩⟩ }, let c : cau_seq ℝ abs, { refine ⟨λ n, G n, λ ε ε0, _⟩, cases hg _ ε0 with n hn, refine ⟨n, λ j jn, _⟩, have : (F j).1.1 ⊆ (F n) := bInter_subset_bInter_left (λ i h, @le_trans _ _ i n j h jn), exact lt_trans ((F n).2 _ _ (this (G j).2) (G n).2) (hn _ $ le_refl _) }, refine ⟨real.lim c, λ s h, _⟩, rcases mem_nhds_iff_metric.1 h with ⟨ε, ε0, hε⟩, cases exists_forall_ge_and (hg _ $ half_pos ε0) (real.equiv_lim c _ $ half_pos ε0) with n hn, cases hn _ (le_refl _) with h₁ h₂, refine upwards_sets _ (F n).1.2 (subset.trans _ $ subset.trans (ball_half_subset (G n) h₂) hε), exact λ x h, lt_trans ((F n).2 x (G n) h (G n).2) h₁ end⟩ -- TODO(Mario): This proof has nothing to do with reals theorem real.Cauchy_eq {f g : Cauchy ℝ} : lim f.1 = lim g.1 ↔ (f, g) ∈ separation_rel (Cauchy ℝ) := begin split, { intros e s hs, rcases Cauchy.mem_uniformity'.1 hs with ⟨t, tu, ts⟩, apply ts, rcases comp_mem_uniformity_sets tu with ⟨d, du, dt⟩, refine mem_prod_iff.2 ⟨_, le_nhds_lim_of_cauchy f.2 (mem_nhds_right (lim f.1) du), _, le_nhds_lim_of_cauchy g.2 (mem_nhds_left (lim g.1) du), λ x h, _⟩, cases x with a b, cases h with h₁ h₂, rw ← e at h₂, exact dt ⟨_, h₁, h₂⟩ }, { intros H, refine separated_def.1 (by apply_instance) _ _ (λ t tu, _), rcases mem_uniformity_is_closed tu with ⟨d, du, dc, dt⟩, refine H {p | (lim p.1.1, lim p.2.1) ∈ t} (Cauchy.mem_uniformity'.2 ⟨d, du, λ f g h, _⟩), rcases mem_prod_iff.1 h with ⟨x, xf, y, yg, h⟩, have limc : ∀ (f : Cauchy ℝ) (x ∈ f.1.sets), lim f.1 ∈ closure x, { intros f x xf, rw closure_eq_nhds, exact neq_bot_of_le_neq_bot f.2.1 (le_inf (le_nhds_lim_of_cauchy f.2) (le_principal_iff.2 xf)) }, have := (closure_subset_iff_subset_of_is_closed dc).2 h, rw closure_prod_eq at this, refine dt (this ⟨_, _⟩); dsimp; apply limc; assumption } end lemma tendsto_of_nat_at_top_at_top : tendsto (coe : ℕ → ℝ) at_top at_top := tendsto_infi.2 $ assume r, tendsto_principal.2 $ let ⟨n, hn⟩ := exists_nat_gt r in mem_at_top_sets.2 ⟨n, λ m h, le_trans (le_of_lt hn) (nat.cast_le.2 h)⟩ 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ε⟩ := mem_nhds_iff_metric.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)) end
189740a1ea2b7fa32e2fdf399e5bae3d0dd84951
c8af905dcd8475f414868d303b2eb0e9d3eb32f9
/src/tactic/known_induct.lean
b14a6f44663af99e1c2e527ca930583635710ed0
[ "BSD-3-Clause" ]
permissive
continuouspi/lean-cpi
81480a13842d67ff5f3698643210d8ed5dd08de4
443bf2cb236feadc45a01387099c236ab2b78237
refs/heads/master
1,650,307,316,582
1,587,033,364,000
1,587,033,364,000
207,499,661
1
0
null
null
null
null
UTF-8
Lean
false
false
1,512
lean
open tactic interactive interactive.types lean.parser expr private meta def set_cases_tags (in_tag : tag) (rs : list name) : tactic unit := do te ← tags_enabled, gs ← get_goals, match gs with | [g] := when te (set_tag g in_tag) -- if only one goal was produced, we should not make the tag longer | _ := do let tgs : list (name × expr) := rs.map₂ (λ n g, (n, g)) gs, if te then tgs.mmap' (λ ⟨n, g⟩, set_tag g (n::in_tag)) /- If `induction/cases` is not in a `with_cases` block, we still set tags using `_case_simple` to make sure we can use the `case` notation. ``` induction h, case c { ... } ``` -/ else tgs.mmap' (λ ⟨n, g⟩, with_enable_tags (set_tag g (name.mk_numeral (unsigned.of_nat 0) `_case::n::[]))) end meta def known_induction (ty : parse ident) (gen : parse texpr) : tactic unit := do in_tag ← get_main_tag, focus1 $ do { gen <- i_to_expr gen, env ← tactic_state.env <$> read, ty ← resolve_constant ty <|> fail ("'" ++ to_string ty ++ "' cannot be found"), let ctors := environment.constructors_of env ty, rw ← apply gen, set_cases_tags in_tag ctors } run_cmd add_interactive [`known_induction] example : ∀ (n : ℕ), 0 < (nat.succ n) := λ n, begin known_induction nat (@nat.rec_on (λ n, 0 < (nat.succ n)) n), case nat.zero { from nat.zero_lt_succ _ }, case nat.succ : n ih { from nat.zero_lt_succ _ }, end